s390_pci_hpc.c 4.3 KB

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