s390_pci_hpc.c 4.4 KB

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