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 SILI driver, for DragonFly
42 u_int32_t SiliForceGen1
= 0;
43 u_int32_t SiliNoFeatures
= 0;
49 static int sili_probe (device_t dev
);
50 static int sili_attach (device_t dev
);
51 static int sili_detach (device_t dev
);
53 static int sili_shutdown (device_t dev
);
54 static int sili_suspend (device_t dev
);
55 static int sili_resume (device_t dev
);
58 static void sili_port_thread(void *arg
);
60 static device_method_t sili_methods
[] = {
61 DEVMETHOD(device_probe
, sili_probe
),
62 DEVMETHOD(device_attach
, sili_attach
),
63 DEVMETHOD(device_detach
, sili_detach
),
65 DEVMETHOD(device_shutdown
, sili_shutdown
),
66 DEVMETHOD(device_suspend
, sili_suspend
),
67 DEVMETHOD(device_resume
, sili_resume
),
70 DEVMETHOD(bus_print_child
, bus_generic_print_child
),
71 DEVMETHOD(bus_driver_added
, bus_generic_driver_added
),
75 static devclass_t sili_devclass
;
77 static driver_t sili_driver
= {
80 sizeof(struct sili_softc
)
83 MODULE_DEPEND(sili
, cam
, 1, 1, 1);
84 DRIVER_MODULE(sili
, pci
, sili_driver
, sili_devclass
, NULL
, NULL
);
87 * Device bus method procedures
90 sili_probe (device_t dev
)
92 const struct sili_device
*ad
;
94 if (kgetenv("hint.sili.disabled"))
96 if (kgetenv("hint.sili.force150"))
98 if (kgetenv("hint.sili.nofeatures"))
101 ad
= sili_lookup_device(dev
);
103 device_set_desc(dev
, ad
->name
);
104 return(-5); /* higher priority the NATA */
110 sili_attach (device_t dev
)
112 struct sili_softc
*sc
= device_get_softc(dev
);
115 sc
->sc_ad
= sili_lookup_device(dev
);
116 if (sc
->sc_ad
== NULL
)
118 error
= sc
->sc_ad
->ad_attach(dev
);
123 sili_detach (device_t dev
)
125 struct sili_softc
*sc
= device_get_softc(dev
);
129 error
= sc
->sc_ad
->ad_detach(dev
);
138 sili_shutdown (device_t dev
)
144 sili_suspend (device_t dev
)
150 sili_resume (device_t dev
)
158 * Sleep (ms) milliseconds, error on the side of caution.
161 sili_os_sleep(int ms
)
165 ticks
= hz
* ms
/ 1000 + 1;
166 tsleep(&ticks
, 0, "ahslp", ticks
);
170 * Sleep for a minimum interval and return the number of milliseconds
171 * that was. The minimum value returned is 1
174 sili_os_softsleep(void)
177 tsleep(&ticks
, 0, "ahslp", hz
/ 1000);
180 tsleep(&ticks
, 0, "ahslp", 1);
186 sili_os_hardsleep(int us
)
192 * Create the OS-specific port helper thread and per-port lock.
195 sili_os_start_port(struct sili_port
*ap
)
197 atomic_set_int(&ap
->ap_signal
, AP_SIGF_INIT
);
198 lockinit(&ap
->ap_lock
, "silipo", 0, LK_CANRECURSE
);
199 lockinit(&ap
->ap_sim_lock
, "silicam", 0, LK_CANRECURSE
);
200 lockinit(&ap
->ap_sig_lock
, "siport", 0, 0);
201 kthread_create(sili_port_thread
, ap
, &ap
->ap_thread
,
206 * Stop the OS-specific port helper thread and kill the per-port lock.
209 sili_os_stop_port(struct sili_port
*ap
)
212 sili_os_signal_port_thread(ap
, AP_SIGF_STOP
);
215 kprintf("%s: Waiting for thread to terminate\n",
217 while (ap
->ap_thread
)
219 kprintf("%s: thread terminated\n",
223 lockuninit(&ap
->ap_lock
);
227 * Add (mask) to the set of bits being sent to the per-port thread helper
228 * and wake the helper up if necessary.
231 sili_os_signal_port_thread(struct sili_port
*ap
, int mask
)
233 lockmgr(&ap
->ap_sig_lock
, LK_EXCLUSIVE
);
234 atomic_set_int(&ap
->ap_signal
, mask
);
235 wakeup(&ap
->ap_thread
);
236 lockmgr(&ap
->ap_sig_lock
, LK_RELEASE
);
240 * Unconditionally lock the port structure for access.
243 sili_os_lock_port(struct sili_port
*ap
)
245 lockmgr(&ap
->ap_lock
, LK_EXCLUSIVE
);
249 * Conditionally lock the port structure for access.
251 * Returns 0 on success, non-zero on failure.
254 sili_os_lock_port_nb(struct sili_port
*ap
)
256 return (lockmgr(&ap
->ap_lock
, LK_EXCLUSIVE
| LK_NOWAIT
));
260 * Unlock a previously locked port.
263 sili_os_unlock_port(struct sili_port
*ap
)
265 lockmgr(&ap
->ap_lock
, LK_RELEASE
);
269 * Per-port thread helper. This helper thread is responsible for
270 * atomically retrieving and clearing the signal mask and calling
271 * the machine-independant driver core.
277 sili_port_thread(void *arg
)
279 struct sili_port
*ap
= arg
;
283 * The helper thread is responsible for the initial port init,
284 * so all the ports can be inited in parallel.
286 * We also run the state machine which should do all probes.
287 * Since CAM is not attached yet we will not get out-of-order
290 sili_os_lock_port(ap
);
292 sili_port_state_machine(ap
, 1);
293 sili_os_unlock_port(ap
);
294 atomic_clear_int(&ap
->ap_signal
, AP_SIGF_INIT
);
295 wakeup(&ap
->ap_signal
);
298 * Then loop on the helper core.
300 mask
= ap
->ap_signal
;
301 while ((mask
& AP_SIGF_STOP
) == 0) {
302 atomic_clear_int(&ap
->ap_signal
, mask
);
303 sili_port_thread_core(ap
, mask
);
304 lockmgr(&ap
->ap_sig_lock
, LK_EXCLUSIVE
);
305 if (ap
->ap_signal
== 0) {
306 lksleep(&ap
->ap_thread
, &ap
->ap_sig_lock
, 0,
309 lockmgr(&ap
->ap_sig_lock
, LK_RELEASE
);
310 mask
= ap
->ap_signal
;
312 ap
->ap_thread
= NULL
;