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 SILI driver, for DragonFly
40 u_int32_t SiliForceGen1 = 0;
41 u_int32_t SiliNoFeatures = 0;
47 static int sili_probe (device_t dev);
48 static int sili_attach (device_t dev);
49 static int sili_detach (device_t dev);
51 static int sili_shutdown (device_t dev);
52 static int sili_suspend (device_t dev);
53 static int sili_resume (device_t dev);
56 static void sili_port_thread(void *arg);
58 static device_method_t sili_methods[] = {
59 DEVMETHOD(device_probe, sili_probe),
60 DEVMETHOD(device_attach, sili_attach),
61 DEVMETHOD(device_detach, sili_detach),
63 DEVMETHOD(device_shutdown, sili_shutdown),
64 DEVMETHOD(device_suspend, sili_suspend),
65 DEVMETHOD(device_resume, sili_resume),
68 DEVMETHOD(bus_print_child, bus_generic_print_child),
69 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
73 static devclass_t sili_devclass;
75 static driver_t sili_driver = {
78 sizeof(struct sili_softc)
81 MODULE_DEPEND(sili, cam, 1, 1, 1);
82 DRIVER_MODULE(sili, pci, sili_driver, sili_devclass, 0, 0);
85 * Device bus method procedures
88 sili_probe (device_t dev)
90 const struct sili_device *ad;
92 if (kgetenv("hint.sili.disabled"))
94 if (kgetenv("hint.sili.force150"))
96 if (kgetenv("hint.sili.nofeatures"))
99 ad = sili_lookup_device(dev);
101 device_set_desc(dev, ad->name);
102 return(-5); /* higher priority the NATA */
108 sili_attach (device_t dev)
110 struct sili_softc *sc = device_get_softc(dev);
113 sc->sc_ad = sili_lookup_device(dev);
114 if (sc->sc_ad == NULL)
116 error = sc->sc_ad->ad_attach(dev);
121 sili_detach (device_t dev)
123 struct sili_softc *sc = device_get_softc(dev);
127 error = sc->sc_ad->ad_detach(dev);
136 sili_shutdown (device_t dev)
142 sili_suspend (device_t dev)
148 sili_resume (device_t dev)
156 * Sleep (ms) milliseconds, error on the side of caution.
159 sili_os_sleep(int ms)
163 ticks = hz * ms / 1000 + 1;
164 tsleep(&ticks, 0, "ahslp", ticks);
168 * Sleep for a minimum interval and return the number of milliseconds
169 * that was. The minimum value returned is 1
172 sili_os_softsleep(void)
175 tsleep(&ticks, 0, "ahslp", hz / 1000);
178 tsleep(&ticks, 0, "ahslp", 1);
184 sili_os_hardsleep(int us)
190 * Create the OS-specific port helper thread and per-port lock.
193 sili_os_start_port(struct sili_port *ap)
195 atomic_set_int(&ap->ap_signal, AP_SIGF_INIT);
196 lockinit(&ap->ap_lock, "silipo", 0, 0);
197 kthread_create(sili_port_thread, ap, &ap->ap_thread,
202 * Stop the OS-specific port helper thread and kill the per-port lock.
205 sili_os_stop_port(struct sili_port *ap)
208 sili_os_signal_port_thread(ap, AP_SIGF_STOP);
211 kprintf("%s: Waiting for thread to terminate\n",
213 while (ap->ap_thread)
215 kprintf("%s: thread terminated\n",
219 lockuninit(&ap->ap_lock);
223 * Add (mask) to the set of bits being sent to the per-port thread helper
224 * and wake the helper up if necessary.
227 sili_os_signal_port_thread(struct sili_port *ap, int mask)
229 atomic_set_int(&ap->ap_signal, mask);
230 wakeup(&ap->ap_thread);
234 * Unconditionally lock the port structure for access.
237 sili_os_lock_port(struct sili_port *ap)
239 lockmgr(&ap->ap_lock, LK_EXCLUSIVE);
243 * Conditionally lock the port structure for access.
245 * Returns 0 on success, non-zero on failure.
248 sili_os_lock_port_nb(struct sili_port *ap)
250 return (lockmgr(&ap->ap_lock, LK_EXCLUSIVE | LK_NOWAIT));
254 * Unlock a previously locked port.
257 sili_os_unlock_port(struct sili_port *ap)
259 lockmgr(&ap->ap_lock, LK_RELEASE);
263 * Per-port thread helper. This helper thread is responsible for
264 * atomically retrieving and clearing the signal mask and calling
265 * the machine-independant driver core.
269 sili_port_thread(void *arg)
271 struct sili_port *ap = arg;
275 * The helper thread is responsible for the initial port init,
276 * so all the ports can be inited in parallel.
278 * We also run the state machine which should do all probes.
279 * Since CAM is not attached yet we will not get out-of-order
282 sili_os_lock_port(ap);
284 sili_port_state_machine(ap, 1);
285 sili_os_unlock_port(ap);
286 atomic_clear_int(&ap->ap_signal, AP_SIGF_INIT);
287 wakeup(&ap->ap_signal);
290 * Then loop on the helper core.
292 mask = ap->ap_signal;
293 while ((mask & AP_SIGF_STOP) == 0) {
294 atomic_clear_int(&ap->ap_signal, mask);
295 sili_port_thread_core(ap, mask);
297 tsleep_interlock(&ap->ap_thread);
298 if (ap->ap_signal == 0)
299 tsleep(&ap->ap_thread, PINTERLOCKED, "ahport", 0);
301 mask = ap->ap_signal;
303 ap->ap_thread = NULL;