blob: 90d1b83cf35f2ae72f7bde1b63fcbb2c9fb219ec [file] [log] [blame]
Stefano Stabellinic1c54132010-05-14 12:44:30 +01001/******************************************************************************
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
22#include <linux/init.h>
23#include <linux/io.h>
Paul Gortmaker7a2463d2016-07-13 20:18:59 -040024#include <linux/export.h>
Stefano Stabellinic1c54132010-05-14 12:44:30 +010025
26#include <xen/platform_pci.h>
Konrad Rzeszutek Wilkb8b0f552012-08-21 14:49:34 -040027#include "xen-ops.h"
Stefano Stabellinic1c54132010-05-14 12:44:30 +010028
29#define XEN_PLATFORM_ERR_MAGIC -1
30#define XEN_PLATFORM_ERR_PROTOCOL -2
31#define XEN_PLATFORM_ERR_BLACKLIST -3
32
Stefano Stabellinica65f9f2010-07-29 14:37:48 +010033#ifdef CONFIG_XEN_PVHVM
Konrad Rzeszutek Wilk6f6c15e2013-12-11 15:22:01 -050034/* store the value of xen_emul_unplug after the unplug is done */
35static int xen_platform_pci_unplug;
Stefano Stabellinic1c54132010-05-14 12:44:30 +010036static int xen_emul_unplug;
37
Raghavendra D Prabhu3c52b7b2011-07-09 21:59:07 +053038static int check_platform_magic(void)
Stefano Stabellinic1c54132010-05-14 12:44:30 +010039{
40 short magic;
41 char protocol;
42
43 magic = inw(XEN_IOPORT_MAGIC);
44 if (magic != XEN_IOPORT_MAGIC_VAL) {
45 printk(KERN_ERR "Xen Platform PCI: unrecognised magic value\n");
46 return XEN_PLATFORM_ERR_MAGIC;
47 }
48
49 protocol = inb(XEN_IOPORT_PROTOVER);
50
51 printk(KERN_DEBUG "Xen Platform PCI: I/O protocol version %d\n",
52 protocol);
53
54 switch (protocol) {
55 case 1:
56 outw(XEN_IOPORT_LINUX_PRODNUM, XEN_IOPORT_PRODNUM);
57 outl(XEN_IOPORT_LINUX_DRVVER, XEN_IOPORT_DRVVER);
58 if (inw(XEN_IOPORT_MAGIC) != XEN_IOPORT_MAGIC_VAL) {
59 printk(KERN_ERR "Xen Platform: blacklisted by host\n");
60 return XEN_PLATFORM_ERR_BLACKLIST;
61 }
62 break;
63 default:
Colin Ian King81295542016-09-12 11:20:46 +010064 printk(KERN_WARNING "Xen Platform PCI: unknown I/O protocol version\n");
Stefano Stabellinic1c54132010-05-14 12:44:30 +010065 return XEN_PLATFORM_ERR_PROTOCOL;
66 }
67
68 return 0;
69}
70
Colin Ian King772f95e2015-07-16 20:34:42 +010071bool xen_has_pv_devices(void)
Konrad Rzeszutek Wilk51c71a32013-11-26 15:05:40 -050072{
73 if (!xen_domain())
74 return false;
75
76 /* PV domains always have them. */
77 if (xen_pv_domain())
78 return true;
79
80 /* And user has xen_platform_pci=0 set in guest config as
81 * driver did not modify the value. */
82 if (xen_platform_pci_unplug == 0)
83 return false;
84
85 if (xen_platform_pci_unplug & XEN_UNPLUG_NEVER)
86 return false;
87
88 if (xen_platform_pci_unplug & XEN_UNPLUG_ALL)
89 return true;
90
91 /* This is an odd one - we are going to run legacy
92 * and PV drivers at the same time. */
93 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY)
94 return true;
95
96 /* And the caller has to follow with xen_pv_{disk,nic}_devices
97 * to be certain which driver can load. */
98 return false;
99}
100EXPORT_SYMBOL_GPL(xen_has_pv_devices);
101
102static bool __xen_has_pv_device(int state)
103{
104 /* HVM domains might or might not */
105 if (xen_hvm_domain() && (xen_platform_pci_unplug & state))
106 return true;
107
108 return xen_has_pv_devices();
109}
110
111bool xen_has_pv_nic_devices(void)
112{
113 return __xen_has_pv_device(XEN_UNPLUG_ALL_NICS | XEN_UNPLUG_ALL);
114}
115EXPORT_SYMBOL_GPL(xen_has_pv_nic_devices);
116
117bool xen_has_pv_disk_devices(void)
118{
119 return __xen_has_pv_device(XEN_UNPLUG_ALL_IDE_DISKS |
120 XEN_UNPLUG_AUX_IDE_DISKS | XEN_UNPLUG_ALL);
121}
122EXPORT_SYMBOL_GPL(xen_has_pv_disk_devices);
123
124/*
125 * This one is odd - it determines whether you want to run PV _and_
126 * legacy (IDE) drivers together. This combination is only possible
127 * under HVM.
128 */
129bool xen_has_pv_and_legacy_disk_devices(void)
130{
131 if (!xen_domain())
132 return false;
133
134 /* N.B. This is only ever used in HVM mode */
135 if (xen_pv_domain())
136 return false;
137
138 if (xen_platform_pci_unplug & XEN_UNPLUG_UNNECESSARY)
139 return true;
140
141 return false;
142}
143EXPORT_SYMBOL_GPL(xen_has_pv_and_legacy_disk_devices);
144
Stefano Stabellini512b1092010-12-01 14:51:44 +0000145void xen_unplug_emulated_devices(void)
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100146{
147 int r;
148
Ian Campbellc93a4df2010-08-23 11:59:28 +0100149 /* user explicitly requested no unplug */
150 if (xen_emul_unplug & XEN_UNPLUG_NEVER)
151 return;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100152 /* check the version of the xen platform PCI device */
153 r = check_platform_magic();
154 /* If the version matches enable the Xen platform PCI driver.
Ian Campbell1dc7ce92010-08-23 11:59:29 +0100155 * Also enable the Xen platform PCI driver if the host does
156 * not support the unplug protocol (XEN_PLATFORM_ERR_MAGIC)
157 * but the user told us that unplugging is unnecessary. */
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100158 if (r && !(r == XEN_PLATFORM_ERR_MAGIC &&
Ian Campbell1dc7ce92010-08-23 11:59:29 +0100159 (xen_emul_unplug & XEN_UNPLUG_UNNECESSARY)))
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100160 return;
161 /* Set the default value of xen_emul_unplug depending on whether or
162 * not the Xen PV frontends and the Xen platform PCI driver have
163 * been compiled for this kernel (modules or built-in are both OK). */
164 if (!xen_emul_unplug) {
165 if (xen_must_unplug_nics()) {
166 printk(KERN_INFO "Netfront and the Xen platform PCI driver have "
167 "been compiled for this kernel: unplug emulated NICs.\n");
168 xen_emul_unplug |= XEN_UNPLUG_ALL_NICS;
169 }
170 if (xen_must_unplug_disks()) {
171 printk(KERN_INFO "Blkfront and the Xen platform PCI driver have "
172 "been compiled for this kernel: unplug emulated disks.\n"
173 "You might have to change the root device\n"
174 "from /dev/hd[a-d] to /dev/xvd[a-d]\n"
175 "in your root= kernel command line option\n");
176 xen_emul_unplug |= XEN_UNPLUG_ALL_IDE_DISKS;
177 }
178 }
179 /* Now unplug the emulated devices */
Ian Campbell1dc7ce92010-08-23 11:59:29 +0100180 if (!(xen_emul_unplug & XEN_UNPLUG_UNNECESSARY))
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100181 outw(xen_emul_unplug, XEN_IOPORT_UNPLUG);
182 xen_platform_pci_unplug = xen_emul_unplug;
183}
184
185static int __init parse_xen_emul_unplug(char *arg)
186{
187 char *p, *q;
188 int l;
189
190 for (p = arg; p; p = q) {
191 q = strchr(p, ',');
192 if (q) {
193 l = q - p;
194 q++;
195 } else {
196 l = strlen(p);
197 }
198 if (!strncmp(p, "all", l))
199 xen_emul_unplug |= XEN_UNPLUG_ALL;
200 else if (!strncmp(p, "ide-disks", l))
201 xen_emul_unplug |= XEN_UNPLUG_ALL_IDE_DISKS;
202 else if (!strncmp(p, "aux-ide-disks", l))
203 xen_emul_unplug |= XEN_UNPLUG_AUX_IDE_DISKS;
204 else if (!strncmp(p, "nics", l))
205 xen_emul_unplug |= XEN_UNPLUG_ALL_NICS;
Ian Campbell1dc7ce92010-08-23 11:59:29 +0100206 else if (!strncmp(p, "unnecessary", l))
207 xen_emul_unplug |= XEN_UNPLUG_UNNECESSARY;
Ian Campbellc93a4df2010-08-23 11:59:28 +0100208 else if (!strncmp(p, "never", l))
209 xen_emul_unplug |= XEN_UNPLUG_NEVER;
Stefano Stabellinic1c54132010-05-14 12:44:30 +0100210 else
211 printk(KERN_WARNING "unrecognised option '%s' "
212 "in parameter 'xen_emul_unplug'\n", p);
213 }
214 return 0;
215}
216early_param("xen_emul_unplug", parse_xen_emul_unplug);
Stefano Stabellinica65f9f2010-07-29 14:37:48 +0100217#endif