platform-pci-unplug.c 6.1 KB

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