s390_pci_hpc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI Hot Plug Controller Driver for System z
  4. *
  5. * Copyright 2012 IBM Corp.
  6. *
  7. * Author(s):
  8. * Jan Glauber <jang@linux.vnet.ibm.com>
  9. */
  10. #define KMSG_COMPONENT "zpci"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  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 int enable_slot(struct hotplug_slot *hotplug_slot)
  20. {
  21. struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
  22. hotplug_slot);
  23. int rc;
  24. mutex_lock(&zdev->state_lock);
  25. if (zdev->state != ZPCI_FN_STATE_STANDBY) {
  26. rc = -EIO;
  27. goto out;
  28. }
  29. rc = sclp_pci_configure(zdev->fid);
  30. zpci_dbg(3, "conf fid:%x, rc:%d\n", zdev->fid, rc);
  31. if (rc)
  32. goto out;
  33. zdev->state = ZPCI_FN_STATE_CONFIGURED;
  34. rc = zpci_scan_configured_device(zdev, zdev->fh);
  35. out:
  36. mutex_unlock(&zdev->state_lock);
  37. return rc;
  38. }
  39. static int disable_slot(struct hotplug_slot *hotplug_slot)
  40. {
  41. struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
  42. hotplug_slot);
  43. struct pci_dev *pdev = NULL;
  44. int rc;
  45. mutex_lock(&zdev->state_lock);
  46. if (zdev->state != ZPCI_FN_STATE_CONFIGURED) {
  47. rc = -EIO;
  48. goto out;
  49. }
  50. pdev = pci_get_slot(zdev->zbus->bus, zdev->devfn);
  51. if (pdev && pci_num_vf(pdev)) {
  52. rc = -EBUSY;
  53. goto out;
  54. }
  55. rc = zpci_deconfigure_device(zdev);
  56. out:
  57. if (pdev)
  58. pci_dev_put(pdev);
  59. mutex_unlock(&zdev->state_lock);
  60. return rc;
  61. }
  62. static int reset_slot(struct hotplug_slot *hotplug_slot, bool probe)
  63. {
  64. struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
  65. hotplug_slot);
  66. int rc = -EIO;
  67. /*
  68. * If we can't get the zdev->state_lock the device state is
  69. * currently undergoing a transition and we bail out - just
  70. * the same as if the device's state is not configured at all.
  71. */
  72. if (!mutex_trylock(&zdev->state_lock))
  73. return rc;
  74. /* We can reset only if the function is configured */
  75. if (zdev->state != ZPCI_FN_STATE_CONFIGURED)
  76. goto out;
  77. if (probe) {
  78. rc = 0;
  79. goto out;
  80. }
  81. rc = zpci_hot_reset_device(zdev);
  82. out:
  83. mutex_unlock(&zdev->state_lock);
  84. return rc;
  85. }
  86. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  87. {
  88. struct zpci_dev *zdev = container_of(hotplug_slot, struct zpci_dev,
  89. hotplug_slot);
  90. *value = zpci_is_device_configured(zdev) ? 1 : 0;
  91. return 0;
  92. }
  93. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  94. {
  95. /* if the slot exists it always contains a function */
  96. *value = 1;
  97. return 0;
  98. }
  99. static const struct hotplug_slot_ops s390_hotplug_slot_ops = {
  100. .enable_slot = enable_slot,
  101. .disable_slot = disable_slot,
  102. .reset_slot = reset_slot,
  103. .get_power_status = get_power_status,
  104. .get_adapter_status = get_adapter_status,
  105. };
  106. int zpci_init_slot(struct zpci_dev *zdev)
  107. {
  108. char name[SLOT_NAME_SIZE];
  109. struct zpci_bus *zbus = zdev->zbus;
  110. zdev->hotplug_slot.ops = &s390_hotplug_slot_ops;
  111. snprintf(name, SLOT_NAME_SIZE, "%08x", zdev->fid);
  112. return pci_hp_register(&zdev->hotplug_slot, zbus->bus,
  113. zdev->devfn, name);
  114. }
  115. void zpci_exit_slot(struct zpci_dev *zdev)
  116. {
  117. pci_hp_deregister(&zdev->hotplug_slot);
  118. }