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