1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratories.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * @(#)subr_autoconf.c 8.1 (Berkeley) 6/10/93
39 *
40 * $FreeBSD: src/sys/kern/subr_autoconf.c,v 1.14 1999/10/05 21:19:41 n_hibma Exp $
41 */
42
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
46 #include <sys/thread.h>
47
48 /*
49 * Autoconfiguration subroutines.
50 */
51
52 /*
53 * "Interrupt driven config" functions.
54 */
55 static TAILQ_HEAD(, intr_config_hook) intr_config_hook_list =
56 TAILQ_HEAD_INITIALIZER(intr_config_hook_list);
57
58
59 /* ARGSUSED */
60 static void run_interrupt_driven_config_hooks (void *dummy);
61 static int ran_config_hooks;
62 static struct lock intr_config_lk = LOCK_INITIALIZER("intrcfg", 0, 0);
63
64 static void
run_interrupt_driven_config_hooks(void * dummy)65 run_interrupt_driven_config_hooks(void *dummy)
66 {
67 struct intr_config_hook *hook_entry;
68 int save_ticks = ticks;
69 int save_count;
70 int waiting;
71
72 lockmgr(&intr_config_lk, LK_EXCLUSIVE);
73 save_count = ran_config_hooks++;
74 while (!TAILQ_EMPTY(&intr_config_hook_list)) {
75 TAILQ_FOREACH(hook_entry, &intr_config_hook_list, ich_links) {
76 if (hook_entry->ich_ran == 0) {
77 hook_entry->ich_ran = 1;
78 lockmgr(&intr_config_lk, LK_RELEASE);
79 (*hook_entry->ich_func)(hook_entry->ich_arg);
80 lockmgr(&intr_config_lk, LK_EXCLUSIVE);
81 break;
82 }
83 }
84 if (hook_entry)
85 continue;
86 if (TAILQ_EMPTY(&intr_config_hook_list))
87 break;
88
89 waiting = (ticks - save_ticks + 1) / hz;
90
91 if (waiting >= 10 && (waiting % 10) == 0) {
92 kprintf("**WARNING** waiting for the following device "
93 "to finish configuring:\n");
94 TAILQ_FOREACH(hook_entry, &intr_config_hook_list,
95 ich_links) {
96 kprintf(" %s:\tfunc=%p arg=%p\n",
97 (hook_entry->ich_desc ?
98 hook_entry->ich_desc : "?"),
99 hook_entry->ich_func,
100 hook_entry->ich_arg);
101 }
102 if (save_count || waiting >= 30) {
103 kprintf("Giving up, interrupt routing is "
104 "probably hosed\n");
105 break;
106 }
107 }
108 lksleep(&intr_config_hook_list, &intr_config_lk,
109 0, "conifhk", hz);
110 ++waiting;
111 }
112 lockmgr(&intr_config_lk, LK_RELEASE);
113
114 /*
115 * Terrible hack to give U4B (usb) a chance to configure, else
116 * the root mount might not see a valid usb device. This can happen
117 * because the AHCI devices might have already finished probing
118 * before the usb ports are even registered.
119 */
120 #ifndef _KERNEL_VIRTUAL
121 if (save_count == 0) {
122 while (ticks - save_ticks < 5*hz)
123 tsleep(&intr_config_hook_list, 0, "delay", hz / 10);
124 }
125 #endif
126
127 }
128 SYSINIT(intr_config_hooks, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_FIRST,
129 run_interrupt_driven_config_hooks, NULL);
130
131 /*
132 * Register a hook that will be called after "cold"
133 * autoconfiguration is complete and interrupts can
134 * be used to complete initialization.
135 */
136 int
config_intrhook_establish(struct intr_config_hook * hook)137 config_intrhook_establish(struct intr_config_hook *hook)
138 {
139 struct intr_config_hook *hook_entry;
140
141 lockmgr(&intr_config_lk, LK_EXCLUSIVE);
142 for (hook_entry = TAILQ_FIRST(&intr_config_hook_list);
143 hook_entry != NULL;
144 hook_entry = TAILQ_NEXT(hook_entry, ich_links)) {
145 if (hook_entry == hook)
146 break;
147 if (hook_entry->ich_order > hook->ich_order)
148 break;
149 }
150 if (hook_entry == hook) {
151 kprintf("config_intrhook_establish: establishing an "
152 "already established hook.\n");
153 lockmgr(&intr_config_lk, LK_RELEASE);
154 return (1);
155 }
156 hook->ich_ran = 0;
157
158 /*
159 * Insert
160 */
161 if (hook_entry)
162 TAILQ_INSERT_BEFORE(hook_entry, hook, ich_links);
163 else
164 TAILQ_INSERT_TAIL(&intr_config_hook_list, hook, ich_links);
165
166 /*
167 * Late hook, run immediately.
168 */
169 if (ran_config_hooks) {
170 lockmgr(&intr_config_lk, LK_RELEASE);
171 run_interrupt_driven_config_hooks(NULL);
172 lockmgr(&intr_config_lk, LK_EXCLUSIVE);
173 }
174 lockmgr(&intr_config_lk, LK_RELEASE);
175
176 return (0);
177 }
178
179 void
config_intrhook_disestablish(struct intr_config_hook * hook)180 config_intrhook_disestablish(struct intr_config_hook *hook)
181 {
182 struct intr_config_hook *hook_entry;
183
184 lockmgr(&intr_config_lk, LK_EXCLUSIVE);
185 for (hook_entry = TAILQ_FIRST(&intr_config_hook_list);
186 hook_entry != NULL;
187 hook_entry = TAILQ_NEXT(hook_entry, ich_links)) {
188 if (hook_entry == hook)
189 break;
190 }
191 if (hook_entry == NULL) {
192 lockmgr(&intr_config_lk, LK_RELEASE);
193 panic("config_intrhook_disestablish: disestablishing an "
194 "unestablished hook (%p)", hook);
195 }
196
197 TAILQ_REMOVE(&intr_config_hook_list, hook, ich_links);
198 /* Wakeup anyone watching the list */
199 wakeup(&intr_config_hook_list);
200 lockmgr(&intr_config_lk, LK_RELEASE);
201 }
202