s390_pci_hpc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #define COMPONENT "zPCI hpc"
  10. #define pr_fmt(fmt) COMPONENT ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/pci.h>
  15. #include <linux/pci_hotplug.h>
  16. #include <linux/init.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. MODULE_AUTHOR("Jan Glauber <jang@linux.vnet.ibm.com");
  22. MODULE_DESCRIPTION("Hot Plug PCI Controller for System z");
  23. MODULE_LICENSE("GPL");
  24. static int zpci_fn_configured(enum zpci_state state)
  25. {
  26. return state == ZPCI_FN_STATE_CONFIGURED ||
  27. state == ZPCI_FN_STATE_ONLINE;
  28. }
  29. /*
  30. * struct slot - slot information for each *physical* slot
  31. */
  32. struct slot {
  33. struct list_head slot_list;
  34. struct hotplug_slot *hotplug_slot;
  35. struct zpci_dev *zdev;
  36. };
  37. static int enable_slot(struct hotplug_slot *hotplug_slot)
  38. {
  39. struct slot *slot = hotplug_slot->private;
  40. int rc;
  41. if (slot->zdev->state != ZPCI_FN_STATE_STANDBY)
  42. return -EIO;
  43. rc = sclp_pci_configure(slot->zdev->fid);
  44. zpci_dbg(3, "conf fid:%x, rc:%d\n", slot->zdev->fid, rc);
  45. if (!rc) {
  46. slot->zdev->state = ZPCI_FN_STATE_CONFIGURED;
  47. /* automatically scan the device after is was configured */
  48. zpci_enable_device(slot->zdev);
  49. zpci_scan_device(slot->zdev);
  50. }
  51. return rc;
  52. }
  53. static int disable_slot(struct hotplug_slot *hotplug_slot)
  54. {
  55. struct slot *slot = hotplug_slot->private;
  56. int rc;
  57. if (!zpci_fn_configured(slot->zdev->state))
  58. return -EIO;
  59. rc = zpci_disable_device(slot->zdev);
  60. if (rc)
  61. return rc;
  62. /* TODO: we rely on the user to unbind/remove the device, is that plausible
  63. * or do we need to trigger that here?
  64. */
  65. rc = sclp_pci_deconfigure(slot->zdev->fid);
  66. zpci_dbg(3, "deconf fid:%x, rc:%d\n", slot->zdev->fid, rc);
  67. if (!rc)
  68. slot->zdev->state = ZPCI_FN_STATE_STANDBY;
  69. return rc;
  70. }
  71. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  72. {
  73. struct slot *slot = hotplug_slot->private;
  74. switch (slot->zdev->state) {
  75. case ZPCI_FN_STATE_STANDBY:
  76. *value = 0;
  77. break;
  78. default:
  79. *value = 1;
  80. break;
  81. }
  82. return 0;
  83. }
  84. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  85. {
  86. /* if the slot exits it always contains a function */
  87. *value = 1;
  88. return 0;
  89. }
  90. static void release_slot(struct hotplug_slot *hotplug_slot)
  91. {
  92. struct slot *slot = hotplug_slot->private;
  93. pr_debug("%s - physical_slot = %s\n", __func__, hotplug_slot_name(hotplug_slot));
  94. kfree(slot->hotplug_slot->info);
  95. kfree(slot->hotplug_slot);
  96. kfree(slot);
  97. }
  98. static struct hotplug_slot_ops s390_hotplug_slot_ops = {
  99. .enable_slot = enable_slot,
  100. .disable_slot = disable_slot,
  101. .get_power_status = get_power_status,
  102. .get_adapter_status = get_adapter_status,
  103. };
  104. static int init_pci_slot(struct zpci_dev *zdev)
  105. {
  106. struct hotplug_slot *hotplug_slot;
  107. struct hotplug_slot_info *info;
  108. char name[SLOT_NAME_SIZE];
  109. struct slot *slot;
  110. int rc;
  111. if (!zdev)
  112. return 0;
  113. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  114. if (!slot)
  115. goto error;
  116. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  117. if (!hotplug_slot)
  118. goto error_hp;
  119. hotplug_slot->private = slot;
  120. slot->hotplug_slot = hotplug_slot;
  121. slot->zdev = zdev;
  122. info = kzalloc(sizeof(*info), GFP_KERNEL);
  123. if (!info)
  124. goto error_info;
  125. hotplug_slot->info = info;
  126. hotplug_slot->ops = &s390_hotplug_slot_ops;
  127. hotplug_slot->release = &release_slot;
  128. get_power_status(hotplug_slot, &info->power_status);
  129. get_adapter_status(hotplug_slot, &info->adapter_status);
  130. snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
  131. rc = pci_hp_register(slot->hotplug_slot, zdev->bus,
  132. ZPCI_DEVFN, name);
  133. if (rc) {
  134. pr_err("pci_hp_register failed with error %d\n", rc);
  135. goto error_reg;
  136. }
  137. list_add(&slot->slot_list, &s390_hotplug_slot_list);
  138. return 0;
  139. error_reg:
  140. kfree(info);
  141. error_info:
  142. kfree(hotplug_slot);
  143. error_hp:
  144. kfree(slot);
  145. error:
  146. return -ENOMEM;
  147. }
  148. static void exit_pci_slot(struct zpci_dev *zdev)
  149. {
  150. struct list_head *tmp, *n;
  151. struct slot *slot;
  152. list_for_each_safe(tmp, n, &s390_hotplug_slot_list) {
  153. slot = list_entry(tmp, struct slot, slot_list);
  154. if (slot->zdev != zdev)
  155. continue;
  156. list_del(&slot->slot_list);
  157. pci_hp_deregister(slot->hotplug_slot);
  158. }
  159. }
  160. static struct pci_hp_callback_ops hp_ops = {
  161. .create_slot = init_pci_slot,
  162. .remove_slot = exit_pci_slot,
  163. };
  164. static void __init init_pci_slots(void)
  165. {
  166. struct zpci_dev *zdev;
  167. /*
  168. * Create a structure for each slot, and register that slot
  169. * with the pci_hotplug subsystem.
  170. */
  171. mutex_lock(&zpci_list_lock);
  172. list_for_each_entry(zdev, &zpci_list, entry) {
  173. init_pci_slot(zdev);
  174. }
  175. mutex_unlock(&zpci_list_lock);
  176. }
  177. static void __exit exit_pci_slots(void)
  178. {
  179. struct list_head *tmp, *n;
  180. struct slot *slot;
  181. /*
  182. * Unregister all of our slots with the pci_hotplug subsystem.
  183. * Memory will be freed in release_slot() callback after slot's
  184. * lifespan is finished.
  185. */
  186. list_for_each_safe(tmp, n, &s390_hotplug_slot_list) {
  187. slot = list_entry(tmp, struct slot, slot_list);
  188. list_del(&slot->slot_list);
  189. pci_hp_deregister(slot->hotplug_slot);
  190. }
  191. }
  192. static int __init pci_hotplug_s390_init(void)
  193. {
  194. if (!s390_pci_probe)
  195. return -EOPNOTSUPP;
  196. zpci_register_hp_ops(&hp_ops);
  197. init_pci_slots();
  198. return 0;
  199. }
  200. static void __exit pci_hotplug_s390_exit(void)
  201. {
  202. exit_pci_slots();
  203. zpci_deregister_hp_ops();
  204. }
  205. module_init(pci_hotplug_s390_init);
  206. module_exit(pci_hotplug_s390_exit);