2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * Copyright (c) 2009-2013 Microsoft Corp.
28 * Copyright (c) 2012 NetApp Inc.
29 * Copyright (c) 2012 Citrix Inc.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice unmodified, this list of conditions, and the following
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
46 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/module.h>
64 #include <sys/malloc.h>
66 #include <sys/taskqueue.h>
68 #include <machine/stdarg.h>
69 #include <machine/resource.h>
70 #include <machine/bus.h>
72 #include <dev/pci/pcivar.h>
73 #include <dev/pci/pcireg.h>
74 #include <dev/ata/ata-all.h>
75 #include <dev/ata/ata-pci.h>
78 #define HV_X64_MSR_GUEST_OS_ID 0x40000000
79 #define HV_X64_CPUID_MIN 0x40000005
80 #define HV_X64_CPUID_MAX 0x4000ffff
83 static int hv_ata_pci_probe(device_t dev);
84 static int hv_ata_pci_attach(device_t dev);
85 static int hv_ata_pci_detach(device_t dev);
87 static int hv_check_for_hyper_v(void);
90 * generic PCI ATA device probe
93 hv_ata_pci_probe(device_t dev)
100 * Don't probe if not running in a Hyper-V environment
102 if (!hv_check_for_hyper_v())
107 "hv_ata_pci_probe dev_class/subslcass = %d, %d\n",
108 pci_get_class(dev), pci_get_subclass(dev));
110 /* is this a storage class device ? */
111 if (pci_get_class(dev) != PCIC_STORAGE)
114 /* is this an IDE/ATA type device ? */
115 if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
120 "Hyper-V probe for disabling ATA-PCI, emulated driver\n");
123 * On Hyper-V the default is to use the enlightened driver for
124 * IDE disks. However, if the user wishes to use the native
125 * ATA driver, the environment variable
126 * hw_ata.disk_enable must be explicitly set to 1.
128 if (getenv_int("hw.ata.disk_enable", &ata_disk_enable)) {
131 "hw.ata.disk_enable flag is disabling Hyper-V"
132 " ATA driver support\n");
137 device_printf(dev, "Hyper-V ATA storage driver enabled.\n");
139 return (BUS_PROBE_VENDOR);
143 hv_ata_pci_attach(device_t dev)
150 hv_ata_pci_detach(device_t dev)
157 * Detect Hyper-V and enable fast IDE
158 * via enlighted storage driver
161 hv_check_for_hyper_v(void)
164 int hyper_v_detected;
166 hyper_v_detected = 0;
168 if (regs[2] & 0x80000000) {
170 * if(a hypervisor is detected)
171 * make sure this really is Hyper-V
173 do_cpuid(HV_X64_MSR_GUEST_OS_ID, regs);
175 regs[0] >= HV_X64_CPUID_MIN &&
176 regs[0] <= HV_X64_CPUID_MAX &&
177 !memcmp("Microsoft Hv", ®s[1], 12);
180 return (hyper_v_detected);
183 static device_method_t hv_ata_pci_methods[] = {
184 /* device interface */
185 DEVMETHOD(device_probe, hv_ata_pci_probe),
186 DEVMETHOD(device_attach, hv_ata_pci_attach),
187 DEVMETHOD(device_detach, hv_ata_pci_detach),
188 DEVMETHOD(device_shutdown, bus_generic_shutdown),
193 devclass_t hv_ata_pci_devclass;
195 static driver_t hv_ata_pci_disengage_driver = {
198 sizeof(struct ata_pci_controller),
201 DRIVER_MODULE(atapci_dis, pci, hv_ata_pci_disengage_driver,
202 hv_ata_pci_devclass, NULL, NULL);
203 MODULE_VERSION(atapci_dis, 1);
204 MODULE_DEPEND(atapci_dis, ata, 1, 1, 1);