s390_pci_hpc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. pci_scan_slot(slot->zdev->bus, ZPCI_DEVFN);
  66. pci_bus_add_devices(slot->zdev->bus);
  67. return rc;
  68. out_deconfigure:
  69. slot_deconfigure(slot);
  70. return rc;
  71. }
  72. static int disable_slot(struct hotplug_slot *hotplug_slot)
  73. {
  74. struct slot *slot = hotplug_slot->private;
  75. int rc;
  76. if (!zpci_fn_configured(slot->zdev->state))
  77. return -EIO;
  78. if (slot->zdev->pdev)
  79. pci_stop_and_remove_bus_device(slot->zdev->pdev);
  80. rc = zpci_disable_device(slot->zdev);
  81. if (rc)
  82. return rc;
  83. return slot_deconfigure(slot);
  84. }
  85. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  86. {
  87. struct slot *slot = hotplug_slot->private;
  88. switch (slot->zdev->state) {
  89. case ZPCI_FN_STATE_STANDBY:
  90. *value = 0;
  91. break;
  92. default:
  93. *value = 1;
  94. break;
  95. }
  96. return 0;
  97. }
  98. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  99. {
  100. /* if the slot exits it always contains a function */
  101. *value = 1;
  102. return 0;
  103. }
  104. static void release_slot(struct hotplug_slot *hotplug_slot)
  105. {
  106. struct slot *slot = hotplug_slot->private;
  107. pr_debug("%s - physical_slot = %s\n", __func__, hotplug_slot_name(hotplug_slot));
  108. kfree(slot->hotplug_slot->info);
  109. kfree(slot->hotplug_slot);
  110. kfree(slot);
  111. }
  112. static struct hotplug_slot_ops s390_hotplug_slot_ops = {
  113. .enable_slot = enable_slot,
  114. .disable_slot = disable_slot,
  115. .get_power_status = get_power_status,
  116. .get_adapter_status = get_adapter_status,
  117. };
  118. int zpci_init_slot(struct zpci_dev *zdev)
  119. {
  120. struct hotplug_slot *hotplug_slot;
  121. struct hotplug_slot_info *info;
  122. char name[SLOT_NAME_SIZE];
  123. struct slot *slot;
  124. int rc;
  125. if (!zdev)
  126. return 0;
  127. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  128. if (!slot)
  129. goto error;
  130. hotplug_slot = kzalloc(sizeof(*hotplug_slot), GFP_KERNEL);
  131. if (!hotplug_slot)
  132. goto error_hp;
  133. hotplug_slot->private = slot;
  134. slot->hotplug_slot = hotplug_slot;
  135. slot->zdev = zdev;
  136. info = kzalloc(sizeof(*info), GFP_KERNEL);
  137. if (!info)
  138. goto error_info;
  139. hotplug_slot->info = info;
  140. hotplug_slot->ops = &s390_hotplug_slot_ops;
  141. hotplug_slot->release = &release_slot;
  142. get_power_status(hotplug_slot, &info->power_status);
  143. get_adapter_status(hotplug_slot, &info->adapter_status);
  144. snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
  145. rc = pci_hp_register(slot->hotplug_slot, zdev->bus,
  146. ZPCI_DEVFN, name);
  147. if (rc) {
  148. pr_err("pci_hp_register failed with error %d\n", rc);
  149. goto error_reg;
  150. }
  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 list_head *tmp, *n;
  165. struct slot *slot;
  166. list_for_each_safe(tmp, n, &s390_hotplug_slot_list) {
  167. slot = list_entry(tmp, struct slot, slot_list);
  168. if (slot->zdev != zdev)
  169. continue;
  170. list_del(&slot->slot_list);
  171. pci_hp_deregister(slot->hotplug_slot);
  172. }
  173. }