4 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Primary device and CAM interface to OpenBSD AHCI driver, for DragonFly
42 u_int32_t AhciForceGen = 0;
43 u_int32_t AhciNoFeatures = 0;
49 static int ahci_probe (device_t dev);
50 static int ahci_attach (device_t dev);
51 static int ahci_detach (device_t dev);
52 static int ahci_sysctl_link_pwr_mgmt (SYSCTL_HANDLER_ARGS);
54 static int ahci_shutdown (device_t dev);
55 static int ahci_suspend (device_t dev);
56 static int ahci_resume (device_t dev);
59 static void ahci_port_thread(void *arg);
61 static device_method_t ahci_methods[] = {
62 DEVMETHOD(device_probe, ahci_probe),
63 DEVMETHOD(device_attach, ahci_attach),
64 DEVMETHOD(device_detach, ahci_detach),
66 DEVMETHOD(device_shutdown, ahci_shutdown),
67 DEVMETHOD(device_suspend, ahci_suspend),
68 DEVMETHOD(device_resume, ahci_resume),
71 DEVMETHOD(bus_print_child, bus_generic_print_child),
72 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
76 static devclass_t ahci_devclass;
78 static driver_t ahci_driver = {
81 sizeof(struct ahci_softc)
84 MODULE_DEPEND(ahci, cam, 1, 1, 1);
85 DRIVER_MODULE(ahci, pci, ahci_driver, ahci_devclass, NULL, NULL);
86 MODULE_VERSION(ahci, 1);
89 * Device bus method procedures
92 ahci_probe (device_t dev)
94 const struct ahci_device *ad;
96 if (kgetenv("hint.ahci.disabled"))
99 ad = ahci_lookup_device(dev);
101 device_set_desc(dev, ad->name);
102 return(-5); /* higher priority the NATA */
108 ahci_attach (device_t dev)
110 struct ahci_softc *sc = device_get_softc(dev);
114 sc->sc_ad = ahci_lookup_device(dev);
115 if (sc->sc_ad == NULL)
119 * Some chipsets do not properly implement the AHCI spec and may
120 * require the link speed to be specifically requested.
122 if (kgetenv("hint.ahci.force150"))
124 if (kgetenv("hint.ahci.force300"))
126 if (kgetenv("hint.ahci.force600"))
129 if (kgetenv("hint.ahci.nofeatures"))
132 sysctl_ctx_init(&sc->sysctl_ctx);
133 ksnprintf(name, sizeof(name), "%s%d",
134 device_get_name(dev), device_get_unit(dev));
135 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
136 SYSCTL_STATIC_CHILDREN(_hw),
137 OID_AUTO, name, CTLFLAG_RD, 0, "");
139 error = sc->sc_ad->ad_attach(dev);
141 sysctl_ctx_free(&sc->sysctl_ctx);
142 sc->sysctl_tree = NULL;
148 ahci_detach (device_t dev)
150 struct ahci_softc *sc = device_get_softc(dev);
153 if (sc->sysctl_tree) {
154 sysctl_ctx_free(&sc->sysctl_ctx);
155 sc->sysctl_tree = NULL;
158 error = sc->sc_ad->ad_detach(dev);
165 ahci_sysctl_link_pwr_mgmt (SYSCTL_HANDLER_ARGS)
167 struct ahci_port *ap = arg1;
168 int error, link_pwr_mgmt;
170 link_pwr_mgmt = ap->link_pwr_mgmt;
171 error = sysctl_handle_int(oidp, &link_pwr_mgmt, 0, req);
172 if (error || req->newptr == NULL)
175 ahci_port_link_pwr_mgmt(ap, link_pwr_mgmt);
180 ahci_sysctl_link_pwr_state (SYSCTL_HANDLER_ARGS)
182 struct ahci_port *ap = arg1;
183 const char *state_names[] = {"unknown", "active", "partial", "slumber"};
187 state = ahci_port_link_pwr_state(ap);
188 if (state < 0 || state >= NELEM(state_names))
191 ksnprintf(buf, sizeof(buf), "%s", state_names[state]);
192 return sysctl_handle_string(oidp, buf, sizeof(buf), req);
198 ahci_shutdown (device_t dev)
204 ahci_suspend (device_t dev)
210 ahci_resume (device_t dev)
218 * Sleep (ms) milliseconds, error on the side of caution.
221 ahci_os_sleep(int ms)
225 ticks = hz * ms / 1000 + 1;
226 tsleep(&ticks, 0, "ahslp", ticks);
230 * Sleep for a minimum interval and return the number of milliseconds
231 * that was. The minimum value returned is 1
234 ahci_os_softsleep(void)
237 tsleep(&ticks, 0, "ahslp", hz / 1000);
240 tsleep(&ticks, 0, "ahslp", 1);
246 ahci_os_hardsleep(int us)
252 * Create the OS-specific port helper thread and per-port lock.
255 ahci_os_start_port(struct ahci_port *ap)
259 atomic_set_int(&ap->ap_signal, AP_SIGF_INIT | AP_SIGF_THREAD_SYNC);
260 lockinit(&ap->ap_lock, "ahcipo", 0, LK_CANRECURSE);
261 lockinit(&ap->ap_sim_lock, "ahcicam", 0, LK_CANRECURSE);
262 lockinit(&ap->ap_sig_lock, "ahport", 0, 0);
263 sysctl_ctx_init(&ap->sysctl_ctx);
264 ksnprintf(name, sizeof(name), "%d", ap->ap_num);
265 ap->sysctl_tree = SYSCTL_ADD_NODE(&ap->sysctl_ctx,
266 SYSCTL_CHILDREN(ap->ap_sc->sysctl_tree),
267 OID_AUTO, name, CTLFLAG_RD, 0, "");
269 if ((ap->ap_sc->sc_cap & AHCI_REG_CAP_SALP) &&
270 (ap->ap_sc->sc_cap & (AHCI_REG_CAP_PSC | AHCI_REG_CAP_SSC))) {
271 SYSCTL_ADD_PROC(&ap->sysctl_ctx,
272 SYSCTL_CHILDREN(ap->sysctl_tree), OID_AUTO,
273 "link_pwr_mgmt", CTLTYPE_INT | CTLFLAG_RW, ap, 0,
274 ahci_sysctl_link_pwr_mgmt, "I",
275 "Link power management policy "
276 "(0 = disabled, 1 = medium, 2 = aggressive)");
277 SYSCTL_ADD_PROC(&ap->sysctl_ctx,
278 SYSCTL_CHILDREN(ap->sysctl_tree), OID_AUTO,
279 "link_pwr_state", CTLTYPE_STRING | CTLFLAG_RD, ap, 0,
280 ahci_sysctl_link_pwr_state, "A",
281 "Link power management state");
285 kthread_create(ahci_port_thread, ap, &ap->ap_thread,
290 * Stop the OS-specific port helper thread and kill the per-port lock.
293 ahci_os_stop_port(struct ahci_port *ap)
295 if (ap->sysctl_tree) {
296 sysctl_ctx_free(&ap->sysctl_ctx);
297 ap->sysctl_tree = NULL;
301 ahci_os_signal_port_thread(ap, AP_SIGF_STOP);
304 kprintf("%s: Waiting for thread to terminate\n",
306 while (ap->ap_thread)
308 kprintf("%s: thread terminated\n",
312 lockuninit(&ap->ap_lock);
316 * Add (mask) to the set of bits being sent to the per-port thread helper
317 * and wake the helper up if necessary.
320 ahci_os_signal_port_thread(struct ahci_port *ap, int mask)
322 lockmgr(&ap->ap_sig_lock, LK_EXCLUSIVE);
323 atomic_set_int(&ap->ap_signal, mask);
324 lockmgr(&ap->ap_sig_lock, LK_RELEASE);
325 wakeup(&ap->ap_thread);
329 * Unconditionally lock the port structure for access.
332 ahci_os_lock_port(struct ahci_port *ap)
334 lockmgr(&ap->ap_lock, LK_EXCLUSIVE);
338 * Conditionally lock the port structure for access.
340 * Returns 0 on success, non-zero on failure.
343 ahci_os_lock_port_nb(struct ahci_port *ap)
345 return (lockmgr(&ap->ap_lock, LK_EXCLUSIVE | LK_NOWAIT));
349 * Unlock a previously locked port.
352 ahci_os_unlock_port(struct ahci_port *ap)
354 lockmgr(&ap->ap_lock, LK_RELEASE);
358 * Per-port thread helper. This helper thread is responsible for
359 * atomically retrieving and clearing the signal mask and calling
360 * the machine-independant driver core.
366 ahci_port_thread(void *arg)
368 struct ahci_port *ap = arg;
372 * Sets us up as an interrupt support thread, meaning we are
373 * given a higher priority and we can preempt normal threads.
375 lwkt_set_interrupt_support_thread();
378 * The helper thread is responsible for the initial port init,
379 * so all the ports can be inited in parallel.
381 * We also run the state machine which should do all probes.
382 * Since CAM is not attached yet we will not get out-of-order
385 ahci_os_lock_port(ap);
387 atomic_clear_int(&ap->ap_signal, AP_SIGF_THREAD_SYNC);
388 wakeup(&ap->ap_signal);
389 ahci_port_state_machine(ap, 1);
390 ahci_os_unlock_port(ap);
391 atomic_clear_int(&ap->ap_signal, AP_SIGF_INIT);
392 wakeup(&ap->ap_signal);
395 * Then loop on the helper core.
397 mask = ap->ap_signal;
398 while ((mask & AP_SIGF_STOP) == 0) {
399 ahci_port_thread_core(ap, mask);
400 lockmgr(&ap->ap_sig_lock, LK_EXCLUSIVE);
401 if (ap->ap_signal == 0) {
402 lksleep(&ap->ap_thread, &ap->ap_sig_lock, 0,
405 mask = ap->ap_signal;
406 atomic_clear_int(&ap->ap_signal, mask);
407 lockmgr(&ap->ap_sig_lock, LK_RELEASE);
409 ap->ap_thread = NULL;