platform-pci-unplug.c 5.6 KB

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