platform-pci-unplug.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/module.h>
  24. #include <xen/platform_pci.h>
  25. #define XEN_PLATFORM_ERR_MAGIC -1
  26. #define XEN_PLATFORM_ERR_PROTOCOL -2
  27. #define XEN_PLATFORM_ERR_BLACKLIST -3
  28. /* store the value of xen_emul_unplug after the unplug is done */
  29. int xen_platform_pci_unplug;
  30. EXPORT_SYMBOL_GPL(xen_platform_pci_unplug);
  31. #ifdef CONFIG_XEN_PVHVM
  32. static int xen_emul_unplug;
  33. static int __init 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");
  56. return XEN_PLATFORM_ERR_PROTOCOL;
  57. }
  58. return 0;
  59. }
  60. void xen_unplug_emulated_devices(void)
  61. {
  62. int r;
  63. /* user explicitly requested no unplug */
  64. if (xen_emul_unplug & XEN_UNPLUG_NEVER)
  65. return;
  66. /* check the version of the xen platform PCI device */
  67. r = check_platform_magic();
  68. /* If the version matches enable the Xen platform PCI driver.
  69. * Also enable the Xen platform PCI driver if the host does
  70. * not support the unplug protocol (XEN_PLATFORM_ERR_MAGIC)
  71. * but the user told us that unplugging is unnecessary. */
  72. if (r && !(r == XEN_PLATFORM_ERR_MAGIC &&
  73. (xen_emul_unplug & XEN_UNPLUG_UNNECESSARY)))
  74. return;
  75. /* Set the default value of xen_emul_unplug depending on whether or
  76. * not the Xen PV frontends and the Xen platform PCI driver have
  77. * been compiled for this kernel (modules or built-in are both OK). */
  78. if (!xen_emul_unplug) {
  79. if (xen_must_unplug_nics()) {
  80. printk(KERN_INFO "Netfront and the Xen platform PCI driver have "
  81. "been compiled for this kernel: unplug emulated NICs.\n");
  82. xen_emul_unplug |= XEN_UNPLUG_ALL_NICS;
  83. }
  84. if (xen_must_unplug_disks()) {
  85. printk(KERN_INFO "Blkfront and the Xen platform PCI driver have "
  86. "been compiled for this kernel: unplug emulated disks.\n"
  87. "You might have to change the root device\n"
  88. "from /dev/hd[a-d] to /dev/xvd[a-d]\n"
  89. "in your root= kernel command line option\n");
  90. xen_emul_unplug |= XEN_UNPLUG_ALL_IDE_DISKS;
  91. }
  92. }
  93. /* Now unplug the emulated devices */
  94. if (!(xen_emul_unplug & XEN_UNPLUG_UNNECESSARY))
  95. outw(xen_emul_unplug, XEN_IOPORT_UNPLUG);
  96. xen_platform_pci_unplug = xen_emul_unplug;
  97. }
  98. static int __init parse_xen_emul_unplug(char *arg)
  99. {
  100. char *p, *q;
  101. int l;
  102. for (p = arg; p; p = q) {
  103. q = strchr(p, ',');
  104. if (q) {
  105. l = q - p;
  106. q++;
  107. } else {
  108. l = strlen(p);
  109. }
  110. if (!strncmp(p, "all", l))
  111. xen_emul_unplug |= XEN_UNPLUG_ALL;
  112. else if (!strncmp(p, "ide-disks", l))
  113. xen_emul_unplug |= XEN_UNPLUG_ALL_IDE_DISKS;
  114. else if (!strncmp(p, "aux-ide-disks", l))
  115. xen_emul_unplug |= XEN_UNPLUG_AUX_IDE_DISKS;
  116. else if (!strncmp(p, "nics", l))
  117. xen_emul_unplug |= XEN_UNPLUG_ALL_NICS;
  118. else if (!strncmp(p, "unnecessary", l))
  119. xen_emul_unplug |= XEN_UNPLUG_UNNECESSARY;
  120. else if (!strncmp(p, "never", l))
  121. xen_emul_unplug |= XEN_UNPLUG_NEVER;
  122. else
  123. printk(KERN_WARNING "unrecognised option '%s' "
  124. "in parameter 'xen_emul_unplug'\n", p);
  125. }
  126. return 0;
  127. }
  128. early_param("xen_emul_unplug", parse_xen_emul_unplug);
  129. #endif