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