s390_pci_hpc.c 3.1 KB

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