s390_pci_hpc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 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 = hotplug_slot->private;
  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. slot->zdev->state = ZPCI_FN_STATE_ONLINE;
  66. pci_scan_slot(slot->zdev->bus, ZPCI_DEVFN);
  67. pci_bus_add_devices(slot->zdev->bus);
  68. return rc;
  69. out_deconfigure:
  70. slot_deconfigure(slot);
  71. return rc;
  72. }
  73. static int disable_slot(struct hotplug_slot *hotplug_slot)
  74. {
  75. struct slot *slot = hotplug_slot->private;
  76. int rc;
  77. if (!zpci_fn_configured(slot->zdev->state))
  78. return -EIO;
  79. if (slot->zdev->pdev)
  80. pci_stop_and_remove_bus_device(slot->zdev->pdev);
  81. rc = zpci_disable_device(slot->zdev);
  82. if (rc)
  83. return rc;
  84. return slot_deconfigure(slot);
  85. }
  86. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  87. {
  88. struct slot *slot = hotplug_slot->private;
  89. switch (slot->zdev->state) {
  90. case ZPCI_FN_STATE_STANDBY:
  91. *value = 0;
  92. break;
  93. default:
  94. *value = 1;
  95. break;
  96. }
  97. return 0;
  98. }
  99. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  100. {
  101. /* if the slot exits it always contains a function */
  102. *value = 1;
  103. return 0;
  104. }
  105. static void release_slot(struct hotplug_slot *hotplug_slot)
  106. {
  107. struct slot *slot = hotplug_slot->private;
  108. pr_debug("%s - physical_slot = %s\n", __func__, hotplug_slot_name(hotplug_slot));
  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. static int init_pci_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. pr_err("pci_hp_register failed with error %d\n", rc);
  150. goto error_reg;
  151. }
  152. list_add(&slot->slot_list, &s390_hotplug_slot_list);
  153. return 0;
  154. error_reg:
  155. kfree(info);
  156. error_info:
  157. kfree(hotplug_slot);
  158. error_hp:
  159. kfree(slot);
  160. error:
  161. return -ENOMEM;
  162. }
  163. static void exit_pci_slot(struct zpci_dev *zdev)
  164. {
  165. struct list_head *tmp, *n;
  166. struct slot *slot;
  167. list_for_each_safe(tmp, n, &s390_hotplug_slot_list) {
  168. slot = list_entry(tmp, struct slot, slot_list);
  169. if (slot->zdev != zdev)
  170. continue;
  171. list_del(&slot->slot_list);
  172. pci_hp_deregister(slot->hotplug_slot);
  173. }
  174. }
  175. static struct pci_hp_callback_ops hp_ops = {
  176. .create_slot = init_pci_slot,
  177. .remove_slot = exit_pci_slot,
  178. };
  179. static void __init init_pci_slots(void)
  180. {
  181. struct zpci_dev *zdev;
  182. /*
  183. * Create a structure for each slot, and register that slot
  184. * with the pci_hotplug subsystem.
  185. */
  186. mutex_lock(&zpci_list_lock);
  187. list_for_each_entry(zdev, &zpci_list, entry) {
  188. init_pci_slot(zdev);
  189. }
  190. mutex_unlock(&zpci_list_lock);
  191. }
  192. static void __exit exit_pci_slots(void)
  193. {
  194. struct list_head *tmp, *n;
  195. struct slot *slot;
  196. /*
  197. * Unregister all of our slots with the pci_hotplug subsystem.
  198. * Memory will be freed in release_slot() callback after slot's
  199. * lifespan is finished.
  200. */
  201. list_for_each_safe(tmp, n, &s390_hotplug_slot_list) {
  202. slot = list_entry(tmp, struct slot, slot_list);
  203. list_del(&slot->slot_list);
  204. pci_hp_deregister(slot->hotplug_slot);
  205. }
  206. }
  207. static int __init pci_hotplug_s390_init(void)
  208. {
  209. if (!s390_pci_probe)
  210. return -EOPNOTSUPP;
  211. zpci_register_hp_ops(&hp_ops);
  212. init_pci_slots();
  213. return 0;
  214. }
  215. static void __exit pci_hotplug_s390_exit(void)
  216. {
  217. exit_pci_slots();
  218. zpci_deregister_hp_ops();
  219. }
  220. module_init(pci_hotplug_s390_init);
  221. module_exit(pci_hotplug_s390_exit);