s390_pci_hpc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 struct hotplug_slot_ops s390_hotplug_slot_ops = {
  107. .enable_slot = enable_slot,
  108. .disable_slot = disable_slot,
  109. .get_power_status = get_power_status,
  110. .get_adapter_status = get_adapter_status,
  111. };
  112. int zpci_init_slot(struct zpci_dev *zdev)
  113. {
  114. struct hotplug_slot *hotplug_slot;
  115. struct hotplug_slot_info *info;
  116. char name[SLOT_NAME_SIZE];
  117. struct slot *slot;
  118. int rc;
  119. if (!zdev)
  120. return 0;
  121. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  122. if (!slot)
  123. goto error;
  124. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  125. if (!hotplug_slot)
  126. goto error_hp;
  127. hotplug_slot->private = slot;
  128. slot->hotplug_slot = hotplug_slot;
  129. slot->zdev = zdev;
  130. info = kzalloc(sizeof(*info), GFP_KERNEL);
  131. if (!info)
  132. goto error_info;
  133. hotplug_slot->info = info;
  134. hotplug_slot->ops = &s390_hotplug_slot_ops;
  135. get_power_status(hotplug_slot, &info->power_status);
  136. get_adapter_status(hotplug_slot, &info->adapter_status);
  137. snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
  138. rc = pci_hp_register(slot->hotplug_slot, zdev->bus,
  139. ZPCI_DEVFN, name);
  140. if (rc)
  141. goto error_reg;
  142. list_add(&slot->slot_list, &s390_hotplug_slot_list);
  143. return 0;
  144. error_reg:
  145. kfree(info);
  146. error_info:
  147. kfree(hotplug_slot);
  148. error_hp:
  149. kfree(slot);
  150. error:
  151. return -ENOMEM;
  152. }
  153. void zpci_exit_slot(struct zpci_dev *zdev)
  154. {
  155. struct slot *slot, *next;
  156. list_for_each_entry_safe(slot, next, &s390_hotplug_slot_list,
  157. slot_list) {
  158. if (slot->zdev != zdev)
  159. continue;
  160. list_del(&slot->slot_list);
  161. pci_hp_deregister(slot->hotplug_slot);
  162. kfree(slot->hotplug_slot->info);
  163. kfree(slot->hotplug_slot);
  164. kfree(slot);
  165. }
  166. }