platform-pci-unplug.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0
  2. /******************************************************************************
  3. * platform-pci-unplug.c
  4. *
  5. * Xen platform PCI device driver
  6. * Copyright (c) 2010, Citrix
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/export.h>
  12. #include <xen/xen.h>
  13. #include <xen/platform_pci.h>
  14. #include "xen-ops.h"
  15. #define XEN_PLATFORM_ERR_MAGIC -1
  16. #define XEN_PLATFORM_ERR_PROTOCOL -2
  17. #define XEN_PLATFORM_ERR_BLACKLIST -3
  18. /* store the value of xen_emul_unplug after the unplug is done */
  19. static int xen_platform_pci_unplug;
  20. static int xen_emul_unplug;
  21. static int check_platform_magic(void)
  22. {
  23. short magic;
  24. char protocol;
  25. magic = inw(XEN_IOPORT_MAGIC);
  26. if (magic != XEN_IOPORT_MAGIC_VAL) {
  27. pr_err("Xen Platform PCI: unrecognised magic value\n");
  28. return XEN_PLATFORM_ERR_MAGIC;
  29. }
  30. protocol = inb(XEN_IOPORT_PROTOVER);
  31. pr_debug("Xen Platform PCI: I/O protocol version %d\n",
  32. protocol);
  33. switch (protocol) {
  34. case 1:
  35. outw(XEN_IOPORT_LINUX_PRODNUM, XEN_IOPORT_PRODNUM);
  36. outl(XEN_IOPORT_LINUX_DRVVER, XEN_IOPORT_DRVVER);
  37. if (inw(XEN_IOPORT_MAGIC) != XEN_IOPORT_MAGIC_VAL) {
  38. pr_err("Xen Platform: blacklisted by host\n");
  39. return XEN_PLATFORM_ERR_BLACKLIST;
  40. }
  41. break;
  42. default:
  43. pr_warn("Xen Platform PCI: unknown I/O protocol version\n");
  44. return XEN_PLATFORM_ERR_PROTOCOL;
  45. }
  46. return 0;
  47. }
  48. bool xen_has_pv_devices(void)
  49. {
  50. if (!xen_domain())
  51. return false;
  52. /* PV and PVH domains always have them. */
  53. if (xen_pv_domain() || xen_pvh_domain())
  54. return true;
  55. /* And user has xen_platform_pci=0 set in guest config as
  56. * driver did not modify the value. */
  57. if (xen_platform_pci_unplug == 0)
  58. return false;
  59. if (xen_platform_pci_unplug & XEN_UNPLUG_NEVER)
  60. return false;
  61. if (xen_platform_pci_unplug & XEN_UNPLUG_ALL)
  62. return true;
  63. /* This is an odd one - we are going to run legacy
  64. * and PV drivers at the same time. */
  65. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY)
  66. return true;
  67. /* And the caller has to follow with xen_pv_{disk,nic}_devices
  68. * to be certain which driver can load. */
  69. return false;
  70. }
  71. EXPORT_SYMBOL_GPL(xen_has_pv_devices);
  72. static bool __xen_has_pv_device(int state)
  73. {
  74. /* HVM domains might or might not */
  75. if (xen_hvm_domain() && (xen_platform_pci_unplug & state))
  76. return true;
  77. return xen_has_pv_devices();
  78. }
  79. bool xen_has_pv_nic_devices(void)
  80. {
  81. return __xen_has_pv_device(XEN_UNPLUG_ALL_NICS | XEN_UNPLUG_ALL);
  82. }
  83. EXPORT_SYMBOL_GPL(xen_has_pv_nic_devices);
  84. bool xen_has_pv_disk_devices(void)
  85. {
  86. return __xen_has_pv_device(XEN_UNPLUG_ALL_IDE_DISKS |
  87. XEN_UNPLUG_AUX_IDE_DISKS | XEN_UNPLUG_ALL);
  88. }
  89. EXPORT_SYMBOL_GPL(xen_has_pv_disk_devices);
  90. /*
  91. * This one is odd - it determines whether you want to run PV _and_
  92. * legacy (IDE) drivers together. This combination is only possible
  93. * under HVM.
  94. */
  95. bool xen_has_pv_and_legacy_disk_devices(void)
  96. {
  97. if (!xen_domain())
  98. return false;
  99. /* N.B. This is only ever used in HVM mode */
  100. if (xen_pv_domain())
  101. return false;
  102. if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY)
  103. return true;
  104. return false;
  105. }
  106. EXPORT_SYMBOL_GPL(xen_has_pv_and_legacy_disk_devices);
  107. void xen_unplug_emulated_devices(void)
  108. {
  109. int r;
  110. /* PVH guests don't have emulated devices. */
  111. if (xen_pvh_domain())
  112. return;
  113. /* user explicitly requested no unplug */
  114. if (xen_emul_unplug & XEN_UNPLUG_NEVER)
  115. return;
  116. /* check the version of the xen platform PCI device */
  117. r = check_platform_magic();
  118. /* If the version matches enable the Xen platform PCI driver.
  119. * Also enable the Xen platform PCI driver if the host does
  120. * not support the unplug protocol (XEN_PLATFORM_ERR_MAGIC)
  121. * but the user told us that unplugging is unnecessary. */
  122. if (r && !(r == XEN_PLATFORM_ERR_MAGIC &&
  123. (xen_emul_unplug & XEN_UNPLUG_UNNECESSARY)))
  124. return;
  125. /* Set the default value of xen_emul_unplug depending on whether or
  126. * not the Xen PV frontends and the Xen platform PCI driver have
  127. * been compiled for this kernel (modules or built-in are both OK). */
  128. if (!xen_emul_unplug) {
  129. if (xen_must_unplug_nics()) {
  130. pr_info("Netfront and the Xen platform PCI driver have "
  131. "been compiled for this kernel: unplug emulated NICs.\n");
  132. xen_emul_unplug |= XEN_UNPLUG_ALL_NICS;
  133. }
  134. if (xen_must_unplug_disks()) {
  135. pr_info("Blkfront and the Xen platform PCI driver have "
  136. "been compiled for this kernel: unplug emulated disks.\n"
  137. "You might have to change the root device\n"
  138. "from /dev/hd[a-d] to /dev/xvd[a-d]\n"
  139. "in your root= kernel command line option\n");
  140. xen_emul_unplug |= XEN_UNPLUG_ALL_IDE_DISKS;
  141. }
  142. }
  143. /* Now unplug the emulated devices */
  144. if (!(xen_emul_unplug & XEN_UNPLUG_UNNECESSARY))
  145. outw(xen_emul_unplug, XEN_IOPORT_UNPLUG);
  146. xen_platform_pci_unplug = xen_emul_unplug;
  147. }
  148. static int __init parse_xen_emul_unplug(char *arg)
  149. {
  150. char *p, *q;
  151. int l;
  152. for (p = arg; p; p = q) {
  153. q = strchr(p, ',');
  154. if (q) {
  155. l = q - p;
  156. q++;
  157. } else {
  158. l = strlen(p);
  159. }
  160. if (!strncmp(p, "all", l))
  161. xen_emul_unplug |= XEN_UNPLUG_ALL;
  162. else if (!strncmp(p, "ide-disks", l))
  163. xen_emul_unplug |= XEN_UNPLUG_ALL_IDE_DISKS;
  164. else if (!strncmp(p, "aux-ide-disks", l))
  165. xen_emul_unplug |= XEN_UNPLUG_AUX_IDE_DISKS;
  166. else if (!strncmp(p, "nics", l))
  167. xen_emul_unplug |= XEN_UNPLUG_ALL_NICS;
  168. else if (!strncmp(p, "unnecessary", l))
  169. xen_emul_unplug |= XEN_UNPLUG_UNNECESSARY;
  170. else if (!strncmp(p, "never", l))
  171. xen_emul_unplug |= XEN_UNPLUG_NEVER;
  172. else
  173. pr_warn("unrecognised option '%s' "
  174. "in parameter 'xen_emul_unplug'\n", p);
  175. }
  176. return 0;
  177. }
  178. early_param("xen_emul_unplug", parse_xen_emul_unplug);