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