]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/stordisengage/hv_ata_pci_disengage.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / dev / hyperv / stordisengage / hv_ata_pci_disengage.c
1 /*-
2  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
14  *
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.
25  */
26 /*-
27  * Copyright (c) 2009-2013 Microsoft Corp.
28  * Copyright (c) 2012 NetApp Inc.
29  * Copyright (c) 2012 Citrix Inc.
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice unmodified, this list of conditions, and the following
37  *    disclaimer.
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.
41  *
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.
52  */
53
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
56
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/module.h>
61 #include <sys/ata.h>
62 #include <sys/bus.h>
63 #include <sys/conf.h>
64 #include <sys/malloc.h>
65 #include <sys/sema.h>
66 #include <sys/taskqueue.h>
67 #include <vm/uma.h>
68 #include <machine/stdarg.h>
69 #include <machine/resource.h>
70 #include <machine/bus.h>
71 #include <sys/rman.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>
76 #include <ata_if.h>
77
78 #define HV_X64_MSR_GUEST_OS_ID  0x40000000
79 #define HV_X64_CPUID_MIN        0x40000005
80 #define HV_X64_CPUID_MAX        0x4000ffff
81
82 /* prototypes */
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);
86
87 static int hv_check_for_hyper_v(void);
88
89 /*
90  * generic PCI ATA device probe
91  */
92 static int
93 hv_ata_pci_probe(device_t dev)
94 {
95         int ata_disk_enable;
96
97         ata_disk_enable = 0;
98
99         /*
100          * Don't probe if not running in a Hyper-V environment
101          */
102         if (!hv_check_for_hyper_v())
103                 return (ENXIO);
104
105         if (bootverbose)
106                 device_printf(dev,
107                     "hv_ata_pci_probe dev_class/subslcass = %d, %d\n",
108                         pci_get_class(dev), pci_get_subclass(dev));
109                         
110         /* is this a storage class device ? */
111         if (pci_get_class(dev) != PCIC_STORAGE)
112                 return (ENXIO);
113
114         /* is this an IDE/ATA type device ? */
115         if (pci_get_subclass(dev) != PCIS_STORAGE_IDE)
116                 return (ENXIO);
117
118         if(bootverbose)
119                 device_printf(dev,
120                     "Hyper-V probe for disabling ATA-PCI, emulated driver\n");
121
122         /*
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.
127          */
128         if (getenv_int("hw.ata.disk_enable", &ata_disk_enable)) {
129                 if(bootverbose)
130                         device_printf(dev,
131                             "hw.ata.disk_enable flag is disabling Hyper-V"
132                             " ATA driver support\n");
133                         return (ENXIO);
134         }
135
136         if (bootverbose)
137                 device_printf(dev, "Hyper-V ATA storage driver enabled.\n");
138
139         return (BUS_PROBE_VENDOR);
140 }
141
142 static int
143 hv_ata_pci_attach(device_t dev)
144 {
145
146         return (0);
147 }
148
149 static int
150 hv_ata_pci_detach(device_t dev)
151 {
152
153         return (0);
154 }
155
156 /**
157 * Detect Hyper-V and enable fast IDE
158 * via enlighted storage driver
159 */
160 static int
161 hv_check_for_hyper_v(void)
162 {
163         u_int regs[4];
164         int hyper_v_detected;
165
166         hyper_v_detected = 0;
167         do_cpuid(1, regs);
168         if (regs[2] & 0x80000000) {
169                 /*
170                  * if(a hypervisor is detected)
171                  *  make sure this really is Hyper-V
172                  */
173                 do_cpuid(HV_X64_MSR_GUEST_OS_ID, regs);
174                 hyper_v_detected =
175                         regs[0] >= HV_X64_CPUID_MIN &&
176                         regs[0] <= HV_X64_CPUID_MAX &&
177                         !memcmp("Microsoft Hv", &regs[1], 12);
178         }
179
180         return (hyper_v_detected);
181 }
182
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),
189
190         DEVMETHOD_END
191 };
192
193 devclass_t hv_ata_pci_devclass;
194
195 static driver_t hv_ata_pci_disengage_driver = {
196         "pciata-disable",
197         hv_ata_pci_methods,
198         sizeof(struct ata_pci_controller),
199 };
200
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);