2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
35 * Primary device and CAM interface to OpenBSD AHCI driver, for DragonFly
40 u_int32_t AhciForceGen1 = 0;
41 u_int32_t AhciNoFeatures = 0;
47 static int ahci_probe (device_t dev);
48 static int ahci_attach (device_t dev);
49 static int ahci_detach (device_t dev);
50 static int ahci_systcl_link_pwr_mgmt (SYSCTL_HANDLER_ARGS);
52 static int ahci_shutdown (device_t dev);
53 static int ahci_suspend (device_t dev);
54 static int ahci_resume (device_t dev);
57 static void ahci_port_thread(void *arg);
59 static device_method_t ahci_methods[] = {
60 DEVMETHOD(device_probe, ahci_probe),
61 DEVMETHOD(device_attach, ahci_attach),
62 DEVMETHOD(device_detach, ahci_detach),
64 DEVMETHOD(device_shutdown, ahci_shutdown),
65 DEVMETHOD(device_suspend, ahci_suspend),
66 DEVMETHOD(device_resume, ahci_resume),
69 DEVMETHOD(bus_print_child, bus_generic_print_child),
70 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
74 static devclass_t ahci_devclass;
76 static driver_t ahci_driver = {
79 sizeof(struct ahci_softc)
82 MODULE_DEPEND(ahci, cam, 1, 1, 1);
83 DRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, 0, 0);
86 * Device bus method procedures
89 ahci_probe (device_t dev)
91 const struct ahci_device *ad;
93 if (kgetenv("hint.ahci.disabled"))
95 if (kgetenv("hint.ahci.force150"))
97 if (kgetenv("hint.ahci.nofeatures"))
100 ad = ahci_lookup_device(dev);
102 device_set_desc(dev, ad->name);
103 return(-5); /* higher priority the NATA */
109 ahci_attach (device_t dev)
111 struct ahci_softc *sc = device_get_softc(dev);
115 sc->sc_ad = ahci_lookup_device(dev);
116 if (sc->sc_ad == NULL)
119 sysctl_ctx_init(&sc->sysctl_ctx);
120 ksnprintf(name, sizeof(name), "%s%d",
121 device_get_name(dev), device_get_unit(dev));
122 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
123 SYSCTL_STATIC_CHILDREN(_hw),
124 OID_AUTO, name, CTLFLAG_RD, 0, "");
126 error = sc->sc_ad->ad_attach(dev);
128 sysctl_ctx_free(&sc->sysctl_ctx);
129 sc->sysctl_tree = NULL;
135 ahci_detach (device_t dev)
137 struct ahci_softc *sc = device_get_softc(dev);
140 if (sc->sysctl_tree) {
141 sysctl_ctx_free(&sc->sysctl_ctx);
142 sc->sysctl_tree = NULL;
145 error = sc->sc_ad->ad_detach(dev);
152 ahci_systcl_link_pwr_mgmt (SYSCTL_HANDLER_ARGS)
154 struct ahci_port *ap = arg1;
155 int error, link_pwr_mgmt;
157 link_pwr_mgmt = ap->link_pwr_mgmt;
158 error = sysctl_handle_int(oidp, &link_pwr_mgmt, 0, req);
159 if (error || req->newptr == NULL)
162 ahci_port_link_pwr_mgmt(ap, link_pwr_mgmt);
169 ahci_shutdown (device_t dev)
175 ahci_suspend (device_t dev)
181 ahci_resume (device_t dev)
189 * Sleep (ms) milliseconds, error on the side of caution.
192 ahci_os_sleep(int ms)
196 ticks = hz * ms / 1000 + 1;
197 tsleep(&ticks, 0, "ahslp", ticks);
201 * Sleep for a minimum interval and return the number of milliseconds
202 * that was. The minimum value returned is 1
205 ahci_os_softsleep(void)
208 tsleep(&ticks, 0, "ahslp", hz / 1000);
211 tsleep(&ticks, 0, "ahslp", 1);
217 ahci_os_hardsleep(int us)
223 * Create the OS-specific port helper thread and per-port lock.
226 ahci_os_start_port(struct ahci_port *ap)
230 atomic_set_int(&ap->ap_signal, AP_SIGF_INIT | AP_SIGF_THREAD_SYNC);
231 lockinit(&ap->ap_lock, "ahcipo", 0, 0);
232 sysctl_ctx_init(&ap->sysctl_ctx);
233 ksnprintf(name, sizeof(name), "%d", ap->ap_num);
234 ap->sysctl_tree = SYSCTL_ADD_NODE(&ap->sysctl_ctx,
235 SYSCTL_CHILDREN(ap->ap_sc->sysctl_tree),
236 OID_AUTO, name, CTLFLAG_RD, 0, "");
238 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) &&
239 (ap->ap_sc->sc_cap & (AHCI_REG_CAP_PSC | AHCI_REG_CAP_SSC))) {
240 SYSCTL_ADD_PROC(&ap->sysctl_ctx,
241 SYSCTL_CHILDREN(ap->sysctl_tree), OID_AUTO,
242 "link_pwr_mgmt", CTLTYPE_INT | CTLFLAG_RW, ap, 0,
243 ahci_systcl_link_pwr_mgmt, "I",
244 "Link power management "
245 "(0 = disabled, 1 = medium, 2 = aggressive)");
249 kthread_create(ahci_port_thread, ap, &ap->ap_thread,
254 * Stop the OS-specific port helper thread and kill the per-port lock.
257 ahci_os_stop_port(struct ahci_port *ap)
259 if (ap->sysctl_tree) {
260 sysctl_ctx_free(&ap->sysctl_ctx);
261 ap->sysctl_tree = NULL;
265 ahci_os_signal_port_thread(ap, AP_SIGF_STOP);
268 kprintf("%s: Waiting for thread to terminate\n",
270 while (ap->ap_thread)
272 kprintf("%s: thread terminated\n",
276 lockuninit(&ap->ap_lock);
280 * Add (mask) to the set of bits being sent to the per-port thread helper
281 * and wake the helper up if necessary.
284 ahci_os_signal_port_thread(struct ahci_port *ap, int mask)
286 atomic_set_int(&ap->ap_signal, mask);
287 wakeup(&ap->ap_thread);
291 * Unconditionally lock the port structure for access.
294 ahci_os_lock_port(struct ahci_port *ap)
296 lockmgr(&ap->ap_lock, LK_EXCLUSIVE);
300 * Conditionally lock the port structure for access.
302 * Returns 0 on success, non-zero on failure.
305 ahci_os_lock_port_nb(struct ahci_port *ap)
307 return (lockmgr(&ap->ap_lock, LK_EXCLUSIVE | LK_NOWAIT));
311 * Unlock a previously locked port.
314 ahci_os_unlock_port(struct ahci_port *ap)
316 lockmgr(&ap->ap_lock, LK_RELEASE);
320 * Per-port thread helper. This helper thread is responsible for
321 * atomically retrieving and clearing the signal mask and calling
322 * the machine-independant driver core.
326 ahci_port_thread(void *arg)
328 struct ahci_port *ap = arg;
332 * The helper thread is responsible for the initial port init,
333 * so all the ports can be inited in parallel.
335 * We also run the state machine which should do all probes.
336 * Since CAM is not attached yet we will not get out-of-order
339 ahci_os_lock_port(ap);
341 atomic_clear_int(&ap->ap_signal, AP_SIGF_THREAD_SYNC);
342 wakeup(&ap->ap_signal);
343 ahci_port_state_machine(ap, 1);
344 ahci_os_unlock_port(ap);
345 atomic_clear_int(&ap->ap_signal, AP_SIGF_INIT);
346 wakeup(&ap->ap_signal);
349 * Then loop on the helper core.
351 mask = ap->ap_signal;
352 while ((mask & AP_SIGF_STOP) == 0) {
353 atomic_clear_int(&ap->ap_signal, mask);
354 ahci_port_thread_core(ap, mask);
355 tsleep_interlock(&ap->ap_thread, 0);
356 if (ap->ap_signal == 0)
357 tsleep(&ap->ap_thread, PINTERLOCKED, "ahport", 0);
358 mask = ap->ap_signal;
360 ap->ap_thread = NULL;