Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / sys / dev / disk / ahci / ahci_dragonfly.c
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 /*
35  * Primary device and CAM interface to OpenBSD AHCI driver, for DragonFly
36  */
37
38 #include "ahci.h"
39
40 /*
41  * Device bus methods
42  */
43
44 static int      ahci_probe (device_t dev);
45 static int      ahci_attach (device_t dev);
46 static int      ahci_detach (device_t dev);
47 #if 0
48 static int      ahci_shutdown (device_t dev);
49 static int      ahci_suspend (device_t dev);
50 static int      ahci_resume (device_t dev);
51 #endif
52
53 static void     ahci_port_thread(void *arg);
54
55 static device_method_t ahci_methods[] = {
56         DEVMETHOD(device_probe,         ahci_probe),
57         DEVMETHOD(device_attach,        ahci_attach),
58         DEVMETHOD(device_detach,        ahci_detach),
59 #if 0
60         DEVMETHOD(device_shutdown,      ahci_shutdown),
61         DEVMETHOD(device_suspend,       ahci_suspend),
62         DEVMETHOD(device_resume,        ahci_resume),
63 #endif
64
65         DEVMETHOD(bus_print_child,      bus_generic_print_child),
66         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
67         {0, 0}
68 };
69
70 static devclass_t       ahci_devclass;
71
72 static driver_t ahci_driver = {
73         "ahci",
74         ahci_methods,
75         sizeof(struct ahci_softc)
76 };
77
78 MODULE_DEPEND(ahci, cam, 1, 1, 1);
79 DRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0);
80
81 /*
82  * Device bus method procedures
83  */
84 static int
85 ahci_probe (device_t dev)
86 {
87         const struct ahci_device *ad;
88
89         if (kgetenv("hint.ahci.disabled"))
90                 return(ENXIO);
91
92         ad = ahci_lookup_device(dev);
93         if (ad) {
94                 device_set_desc(dev, ad->name);
95                 return(-5);     /* higher priority the NATA */
96         }
97         return(ENXIO);
98 }
99
100 static int
101 ahci_attach (device_t dev)
102 {
103         struct ahci_softc *sc = device_get_softc(dev);
104         int error;
105
106         sc->sc_ad = ahci_lookup_device(dev);
107         if (sc->sc_ad == NULL)
108                 return(ENXIO);
109         error = sc->sc_ad->ad_attach(dev);
110         return (error);
111 }
112
113 static int
114 ahci_detach (device_t dev)
115 {
116         struct ahci_softc *sc = device_get_softc(dev);
117         int error = 0;
118
119         if (sc->sc_ad) {
120                 error = sc->sc_ad->ad_detach(dev);
121                 sc->sc_ad = NULL;
122         }
123         return(error);
124 }
125
126 #if 0
127
128 static int
129 ahci_shutdown (device_t dev)
130 {
131         return (0);
132 }
133
134 static int
135 ahci_suspend (device_t dev)
136 {
137         return (0);
138 }
139
140 static int
141 ahci_resume (device_t dev)
142 {
143         return (0);
144 }
145
146 #endif
147
148 /*
149  * Sleep (ms) milliseconds, error on the side of caution.
150  */
151 void
152 ahci_os_sleep(int ms)
153 {
154         int ticks;
155
156         ticks = hz * ms / 1000 + 1;
157         tsleep(&ticks, 0, "ahslp", ticks);
158 }
159
160 /*
161  * Sleep for a minimum interval and return the number of milliseconds
162  * that was.  The minimum value returned is 1
163  */
164 int
165 ahci_os_softsleep(void)
166 {
167         if (hz >= 1000) {
168                 tsleep(&ticks, 0, "ahslp", hz / 1000);
169                 return(1);
170         } else {
171                 tsleep(&ticks, 0, "ahslp", 1);
172                 return(1000 / hz);
173         }
174 }
175
176 void
177 ahci_os_hardsleep(int us)
178 {
179         DELAY(us);
180 }
181
182 /*
183  * Create the OS-specific port helper thread and per-port lock.
184  */
185 void
186 ahci_os_start_port(struct ahci_port *ap)
187 {
188         atomic_set_int(&ap->ap_signal, AP_SIGF_INIT);
189         lockinit(&ap->ap_lock, "ahcipo", 0, 0);
190         kthread_create(ahci_port_thread, ap, &ap->ap_thread,
191                        "%s", PORTNAME(ap));
192 }
193
194 /*
195  * Stop the OS-specific port helper thread and kill the per-port lock.
196  */
197 void
198 ahci_os_stop_port(struct ahci_port *ap)
199 {
200         if (ap->ap_thread) {
201                 ahci_os_signal_port_thread(ap, AP_SIGF_STOP);
202                 ahci_os_sleep(10);
203                 if (ap->ap_thread) {
204                         kprintf("%s: Waiting for thread to terminate\n",
205                                 PORTNAME(ap));
206                         while (ap->ap_thread)
207                                 ahci_os_sleep(100);
208                         kprintf("%s: thread terminated\n",
209                                 PORTNAME(ap));
210                 }
211         }
212         lockuninit(&ap->ap_lock);
213 }
214
215 /*
216  * Add (mask) to the set of bits being sent to the per-port thread helper
217  * and wake the helper up if necessary.
218  */
219 void
220 ahci_os_signal_port_thread(struct ahci_port *ap, int mask)
221 {
222         atomic_set_int(&ap->ap_signal, mask);
223         wakeup(&ap->ap_thread);
224 }
225
226 /*
227  * Unconditionally lock the port structure for access.
228  */
229 void
230 ahci_os_lock_port(struct ahci_port *ap)
231 {
232         lockmgr(&ap->ap_lock, LK_EXCLUSIVE);
233 }
234
235 /*
236  * Conditionally lock the port structure for access.
237  *
238  * Returns 0 on success, non-zero on failure.
239  */
240 int
241 ahci_os_lock_port_nb(struct ahci_port *ap)
242 {
243         return (lockmgr(&ap->ap_lock, LK_EXCLUSIVE | LK_NOWAIT));
244 }
245
246 /*
247  * Unlock a previously locked port.
248  */
249 void
250 ahci_os_unlock_port(struct ahci_port *ap)
251 {
252         lockmgr(&ap->ap_lock, LK_RELEASE);
253 }
254
255 /*
256  * Per-port thread helper.  This helper thread is responsible for
257  * atomically retrieving and clearing the signal mask and calling
258  * the machine-independant driver core.
259  */
260 static
261 void
262 ahci_port_thread(void *arg)
263 {
264         struct ahci_port *ap = arg;
265         int mask;
266
267         /*
268          * The helper thread is responsible for the initial port init,
269          * so all the ports can be inited in parallel.
270          *
271          * We also run the state machine which should do all probes.
272          * Since CAM is not attached yet we will not get out-of-order
273          * SCSI attachments.
274          */
275         ahci_os_lock_port(ap);
276         ahci_port_init(ap, NULL);
277         ahci_port_state_machine(ap, 1);
278         ahci_os_unlock_port(ap);
279         atomic_clear_int(&ap->ap_signal, AP_SIGF_INIT);
280         wakeup(&ap->ap_signal);
281
282         /*
283          * Then loop on the helper core.
284          */
285         mask = ap->ap_signal;
286         while ((mask & AP_SIGF_STOP) == 0) {
287                 atomic_clear_int(&ap->ap_signal, mask);
288                 ahci_port_thread_core(ap, mask);
289                 crit_enter();
290                 tsleep_interlock(&ap->ap_thread);
291                 if (ap->ap_signal == 0)
292                         tsleep(&ap->ap_thread, 0, "ahport", 0);
293                 crit_exit();
294                 mask = ap->ap_signal;
295         }
296         ap->ap_thread = NULL;
297 }