Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /******************************************************************************
0004  * platform-pci-unplug.c
0005  *
0006  * Xen platform PCI device driver
0007  * Copyright (c) 2010, Citrix
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #include <linux/init.h>
0013 #include <linux/io.h>
0014 #include <linux/export.h>
0015 
0016 #include <xen/xen.h>
0017 #include <xen/platform_pci.h>
0018 #include "xen-ops.h"
0019 
0020 #define XEN_PLATFORM_ERR_MAGIC -1
0021 #define XEN_PLATFORM_ERR_PROTOCOL -2
0022 #define XEN_PLATFORM_ERR_BLACKLIST -3
0023 
0024 /* store the value of xen_emul_unplug after the unplug is done */
0025 static int xen_platform_pci_unplug;
0026 static int xen_emul_unplug;
0027 
0028 static int check_platform_magic(void)
0029 {
0030     short magic;
0031     char protocol;
0032 
0033     magic = inw(XEN_IOPORT_MAGIC);
0034     if (magic != XEN_IOPORT_MAGIC_VAL) {
0035         pr_err("Xen Platform PCI: unrecognised magic value\n");
0036         return XEN_PLATFORM_ERR_MAGIC;
0037     }
0038 
0039     protocol = inb(XEN_IOPORT_PROTOVER);
0040 
0041     pr_debug("Xen Platform PCI: I/O protocol version %d\n",
0042             protocol);
0043 
0044     switch (protocol) {
0045     case 1:
0046         outw(XEN_IOPORT_LINUX_PRODNUM, XEN_IOPORT_PRODNUM);
0047         outl(XEN_IOPORT_LINUX_DRVVER, XEN_IOPORT_DRVVER);
0048         if (inw(XEN_IOPORT_MAGIC) != XEN_IOPORT_MAGIC_VAL) {
0049             pr_err("Xen Platform: blacklisted by host\n");
0050             return XEN_PLATFORM_ERR_BLACKLIST;
0051         }
0052         break;
0053     default:
0054         pr_warn("Xen Platform PCI: unknown I/O protocol version\n");
0055         return XEN_PLATFORM_ERR_PROTOCOL;
0056     }
0057 
0058     return 0;
0059 }
0060 
0061 bool xen_has_pv_devices(void)
0062 {
0063     if (!xen_domain())
0064         return false;
0065 
0066     /* PV and PVH domains always have them. */
0067     if (xen_pv_domain() || xen_pvh_domain())
0068         return true;
0069 
0070     /* And user has xen_platform_pci=0 set in guest config as
0071      * driver did not modify the value. */
0072     if (xen_platform_pci_unplug == 0)
0073         return false;
0074 
0075     if (xen_platform_pci_unplug & XEN_UNPLUG_NEVER)
0076         return false;
0077 
0078     if (xen_platform_pci_unplug & XEN_UNPLUG_ALL)
0079         return true;
0080 
0081     /* This is an odd one - we are going to run legacy
0082      * and PV drivers at the same time. */
0083     if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY)
0084         return true;
0085 
0086     /* And the caller has to follow with xen_pv_{disk,nic}_devices
0087      * to be certain which driver can load. */
0088     return false;
0089 }
0090 EXPORT_SYMBOL_GPL(xen_has_pv_devices);
0091 
0092 static bool __xen_has_pv_device(int state)
0093 {
0094     /* HVM domains might or might not */
0095     if (xen_hvm_domain() && (xen_platform_pci_unplug & state))
0096         return true;
0097 
0098     return xen_has_pv_devices();
0099 }
0100 
0101 bool xen_has_pv_nic_devices(void)
0102 {
0103     return __xen_has_pv_device(XEN_UNPLUG_ALL_NICS | XEN_UNPLUG_ALL);
0104 }
0105 EXPORT_SYMBOL_GPL(xen_has_pv_nic_devices);
0106 
0107 bool xen_has_pv_disk_devices(void)
0108 {
0109     return __xen_has_pv_device(XEN_UNPLUG_ALL_IDE_DISKS |
0110                    XEN_UNPLUG_AUX_IDE_DISKS | XEN_UNPLUG_ALL);
0111 }
0112 EXPORT_SYMBOL_GPL(xen_has_pv_disk_devices);
0113 
0114 /*
0115  * This one is odd - it determines whether you want to run PV _and_
0116  * legacy (IDE) drivers together. This combination is only possible
0117  * under HVM.
0118  */
0119 bool xen_has_pv_and_legacy_disk_devices(void)
0120 {
0121     if (!xen_domain())
0122         return false;
0123 
0124     /* N.B. This is only ever used in HVM mode */
0125     if (xen_pv_domain())
0126         return false;
0127 
0128     if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY)
0129         return true;
0130 
0131     return false;
0132 }
0133 EXPORT_SYMBOL_GPL(xen_has_pv_and_legacy_disk_devices);
0134 
0135 void xen_unplug_emulated_devices(void)
0136 {
0137     int r;
0138 
0139     /* PVH guests don't have emulated devices. */
0140     if (xen_pvh_domain())
0141         return;
0142 
0143     /* user explicitly requested no unplug */
0144     if (xen_emul_unplug & XEN_UNPLUG_NEVER)
0145         return;
0146     /* check the version of the xen platform PCI device */
0147     r = check_platform_magic();
0148     /* If the version matches enable the Xen platform PCI driver.
0149      * Also enable the Xen platform PCI driver if the host does
0150      * not support the unplug protocol (XEN_PLATFORM_ERR_MAGIC)
0151      * but the user told us that unplugging is unnecessary. */
0152     if (r && !(r == XEN_PLATFORM_ERR_MAGIC &&
0153             (xen_emul_unplug & XEN_UNPLUG_UNNECESSARY)))
0154         return;
0155     /* Set the default value of xen_emul_unplug depending on whether or
0156      * not the Xen PV frontends and the Xen platform PCI driver have
0157      * been compiled for this kernel (modules or built-in are both OK). */
0158     if (!xen_emul_unplug) {
0159         if (xen_must_unplug_nics()) {
0160             pr_info("Netfront and the Xen platform PCI driver have "
0161                     "been compiled for this kernel: unplug emulated NICs.\n");
0162             xen_emul_unplug |= XEN_UNPLUG_ALL_NICS;
0163         }
0164         if (xen_must_unplug_disks()) {
0165             pr_info("Blkfront and the Xen platform PCI driver have "
0166                     "been compiled for this kernel: unplug emulated disks.\n"
0167                     "You might have to change the root device\n"
0168                     "from /dev/hd[a-d] to /dev/xvd[a-d]\n"
0169                     "in your root= kernel command line option\n");
0170             xen_emul_unplug |= XEN_UNPLUG_ALL_IDE_DISKS;
0171         }
0172     }
0173     /* Now unplug the emulated devices */
0174     if (!(xen_emul_unplug & XEN_UNPLUG_UNNECESSARY))
0175         outw(xen_emul_unplug, XEN_IOPORT_UNPLUG);
0176     xen_platform_pci_unplug = xen_emul_unplug;
0177 }
0178 
0179 static int __init parse_xen_emul_unplug(char *arg)
0180 {
0181     char *p, *q;
0182     int l;
0183 
0184     for (p = arg; p; p = q) {
0185         q = strchr(p, ',');
0186         if (q) {
0187             l = q - p;
0188             q++;
0189         } else {
0190             l = strlen(p);
0191         }
0192         if (!strncmp(p, "all", l))
0193             xen_emul_unplug |= XEN_UNPLUG_ALL;
0194         else if (!strncmp(p, "ide-disks", l))
0195             xen_emul_unplug |= XEN_UNPLUG_ALL_IDE_DISKS;
0196         else if (!strncmp(p, "aux-ide-disks", l))
0197             xen_emul_unplug |= XEN_UNPLUG_AUX_IDE_DISKS;
0198         else if (!strncmp(p, "nics", l))
0199             xen_emul_unplug |= XEN_UNPLUG_ALL_NICS;
0200         else if (!strncmp(p, "unnecessary", l))
0201             xen_emul_unplug |= XEN_UNPLUG_UNNECESSARY;
0202         else if (!strncmp(p, "never", l))
0203             xen_emul_unplug |= XEN_UNPLUG_NEVER;
0204         else
0205             pr_warn("unrecognised option '%s' "
0206                  "in parameter 'xen_emul_unplug'\n", p);
0207     }
0208     return 0;
0209 }
0210 early_param("xen_emul_unplug", parse_xen_emul_unplug);