1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* DACF (Device Autoconfiguration Framework) client code.
*
* DACF has two clients. the first is dacf modules which implement
* configuration operations; the second is the set of hooks in the kernel
* which do rule matching and invoke configuration operations.
*
* This file implements the second part, the kernel hooks.
*
* Currently implemented are post-attach and pre-detach handlers, and the hook
* for ddi_create_minor_common() which sets up post-attach and pre-detach
* reservations.
*
* This code depends on the core dacf code (in dacf.c) but the converse should
* never be true.
*
* This file also implements '__kernel', the kernel-supplied dacf module.
* For now, this is pretty much empty, except under DEBUG, in which case it
* contains some debugging code.
*/
#include <sys/param.h>
#include <sys/modctl.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/pathname.h>
#include <sys/ddi_impldefs.h>
#include <sys/sunddi.h>
#include <sys/autoconf.h>
#include <sys/modhash.h>
#include <sys/dacf_impl.h>
#include <sys/systm.h>
#include <sys/debug.h>
/*
* dacfc_match_create_minor()
* Check to see if this minor node creation sequence matches a dacf
* (device autoconfiguration framework) rule. If so make a reservation
* for the operation to be invoked at post-attach and/or pre-detach time.
*/
void
dacfc_match_create_minor(const char *name, const char *node_type,
dev_info_t *dip, struct ddi_minor_data *dmdp, int flag)
{
dacf_rule_t *r;
char *dev_path, *dev_pathp, *drv_mname = NULL;
dacf_rsrvlist_t *pa_rsrv, *pd_rsrv;
/*
* Check the dacf rule for non-clone devices or for network devices.
*/
if ((flag & CLONE_DEV) && (strcmp(node_type, DDI_NT_NET) != 0)) {
return;
}
/*
* Because dacf currently only implements post-attach and pre-detach
* processing, we only care about minor nodes created during attach.
* However, there is no restriction on drivers about when to create
* minor nodes.
*/
if (!DEVI_IS_ATTACHING(dmdp->dip)) {
return;
}
dev_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
dev_pathp = ddi_pathname(dip, dev_path);
pa_rsrv = kmem_alloc(sizeof (dacf_rsrvlist_t), KM_SLEEP);
pd_rsrv = kmem_alloc(sizeof (dacf_rsrvlist_t), KM_SLEEP);
if (name) {
const char *drv_name = ddi_driver_name(dip);
if (drv_name == NULL)
drv_name = "???";
drv_mname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) snprintf(drv_mname, MAXPATHLEN, "%s:%s", drv_name, name);
}
mutex_enter(&dacf_lock);
/*
* Ensure that we don't wind up in a 'matching loop' against a devinfo
* node, which could cause deadlock. This could happen as follows:
*
* We match (just below)
* We invoke a task (later, at the end of devi_attach)
* this means we have taken the per-devinfo lock
* The task invoke winds up causing the same driver (that has
* just finished attaching) to create another minor node.
* We try to re-acquire the per-devinfo list lock again in the
* process of making another reservation
*/
mutex_enter(&(DEVI(dip)->devi_lock));
if (DEVI_IS_INVOKING_DACF(dip)) {
mutex_exit(&(DEVI(dip)->devi_lock));
cmn_err(CE_WARN,
"!dacf detected deadlock, aborting matching procedure\n");
mutex_exit(&dacf_lock);
kmem_free(pa_rsrv, sizeof (dacf_rsrvlist_t));
kmem_free(pd_rsrv, sizeof (dacf_rsrvlist_t));
kmem_free(dev_path, MAXPATHLEN);
if (drv_mname) {
kmem_free(drv_mname, MAXPATHLEN);
}
return;
}
mutex_exit(&(DEVI(dip)->devi_lock));
/*
* Do rule matching. It's possible to construct two rules that would
* match against the same minor node, so we match from most to least
* specific:
* device path
* minor node name (concatenation of drv_name:name
* node type
*
* Future additions to the set of device-specifiers should be
* sensitive to this ordering.
*/
/*
* post-attach matching
*/
r = NULL;
if (dev_pathp) {
r = dacf_match(DACF_OPID_POSTATTACH, DACF_DS_DEV_PATH,
dev_pathp);
}
if (!r && drv_mname) {
r = dacf_match(DACF_OPID_POSTATTACH, DACF_DS_DRV_MNAME,
drv_mname);
}
if (!r && node_type) {
r = dacf_match(DACF_OPID_POSTATTACH, DACF_DS_MIN_NT, node_type);
}
if (r) {
dacf_rsrv_make(pa_rsrv, r, dmdp, &(DEVI(dip)->devi_dacf_tasks));
if (dacfdebug & DACF_DBG_MSGS)
printf("dacf: made 'post-attach' reservation for "
"%s, %s, %s\n", name, node_type, dev_pathp);
} else {
kmem_free(pa_rsrv, sizeof (dacf_rsrvlist_t));
}
/*
* pre-detach matching
*/
r = NULL;
if (dev_pathp) {
r = dacf_match(DACF_OPID_PREDETACH, DACF_DS_DEV_PATH,
dev_pathp);
}
if (!r && drv_mname) {
r = dacf_match(DACF_OPID_PREDETACH, DACF_DS_DRV_MNAME,
drv_mname);
}
if (!r && node_type) {
r = dacf_match(DACF_OPID_PREDETACH, DACF_DS_MIN_NT, node_type);
}
if (r) {
dacf_rsrv_make(pd_rsrv, r, dmdp, &(DEVI(dip)->devi_dacf_tasks));
if (dacfdebug & DACF_DBG_MSGS) {
printf("dacf: made 'pre-detach' reservation for "
"%s, %s, %s\n", name, node_type, dev_pathp);
}
} else {
kmem_free(pd_rsrv, sizeof (dacf_rsrvlist_t));
}
mutex_exit(&dacf_lock);
kmem_free(dev_path, MAXPATHLEN);
if (drv_mname) {
kmem_free(drv_mname, MAXPATHLEN);
}
}
/*
* dacfc_postattach()
* autoconfiguration for post-attach events.
*
* strategy: try to configure. If some of the configuration operations
* fail, emit a warning.
*/
int
dacfc_postattach(dev_info_t *devi)
{
int err = DACF_SUCCESS;
char *path, *pathp;
dacf_rsrvlist_t **opsp, *op;
ASSERT(MUTEX_HELD(&dacf_lock));
/*
* Instruct dacf_process_rsrvs() to invoke each POSTATTACH op.
*/
opsp = &DEVI(devi)->devi_dacf_tasks;
dacf_process_rsrvs(opsp, DACF_OPID_POSTATTACH, DACF_PROC_INVOKE);
/*
* Check to see that all POSTATTACH's succeeded.
*/
for (op = *opsp; op != NULL; op = op->rsrv_next) {
if (op->rsrv_rule->r_opid != DACF_OPID_POSTATTACH)
continue;
if (op->rsrv_result == DACF_SUCCESS)
continue;
if (dacfdebug & DACF_DBG_DEVI) {
cmn_err(CE_WARN, "op failed, err = %d\n",
op->rsrv_result);
}
err = DACF_FAILURE;
break;
}
/*
* If one or more postattach's failed, give up.
*/
if ((err == DACF_FAILURE) && (dacfdebug & DACF_DBG_DEVI)) {
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if ((pathp = ddi_pathname(devi, path)) == NULL)
pathp = "<unknown>";
cmn_err(CE_WARN, "%s attached, but failed to auto-configure",
pathp);
kmem_free(path, MAXPATHLEN);
}
return (err);
}
/*
* dacfc_predetach()
* auto-unconfiguration for pre-detach events.
*
* strategy: call the pre-detach operation for all matching reservations.
* If any of these fail, make (one) attempt to reconfigure things back
* into a sane state. if that fails, our state is uncertain.
*/
int
dacfc_predetach(dev_info_t *devi)
{
int err = DDI_SUCCESS;
char *path, *pathp;
dacf_rsrvlist_t **opsp, *op;
ASSERT(MUTEX_HELD(&dacf_lock));
/*
* Instruct dacf_process_rsrvs() to invoke each PREDETACH op.
*/
opsp = &DEVI(devi)->devi_dacf_tasks;
dacf_process_rsrvs(opsp, DACF_OPID_PREDETACH, DACF_PROC_INVOKE);
/*
* Check to see that all PREDETACH's succeeded.
*/
for (op = *opsp; op != NULL; op = op->rsrv_next) {
if (op->rsrv_rule->r_opid != DACF_OPID_PREDETACH)
continue;
if (op->rsrv_result == 0)
continue;
err = DDI_FAILURE;
break;
}
/*
* If one or more predetach's failed, make one attempt to fix things
* by re-running all of the POST-ATTACH operations. If any of those
* fail, give up.
*/
if (err == DDI_FAILURE) {
int pa_err;
if (dacfdebug & DACF_DBG_DEVI) {
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if ((pathp = ddi_pathname(devi, path)) == NULL)
pathp = "<unknown>";
cmn_err(CE_WARN, "%s failed to auto-unconfigure, "
"attempting to reconfigure...", pathp);
kmem_free(path, MAXPATHLEN);
}
pa_err = dacfc_postattach(devi);
if (dacfdebug & DACF_DBG_DEVI) {
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if ((pathp = ddi_pathname(devi, path)) == NULL)
pathp = "<unknown>";
if (pa_err == DDI_FAILURE) {
cmn_err(CE_WARN, "%s failed to "
"auto-unconfigure, and could not be "
"re-autoconfigured.", pathp);
} else {
cmn_err(CE_WARN, "%s failed to "
"auto-unconfigure, but was successfully "
"re-autoconfigured.", pathp);
}
kmem_free(path, MAXPATHLEN);
}
}
return (err);
}
/*
* kmod_dacfsw:
* This is the declaration for the kernel-supplied '__kernel' dacf module.
* DACF supplies a framework based around loadable modules. However, it
* may be convenient (in the future) to have a module provided by the
* kernel. This is useful in cases when a module can't be loaded (early in
* boot), or for code that would never get unloaded anyway.
*/
#ifdef DEBUG
/*ARGSUSED*/
static int
kmod_test_postattach(dacf_infohdl_t info_hdl, dacf_arghdl_t arg_hdl, int flags)
{
const char *verbose = dacf_get_arg(arg_hdl, "verbose");
if (verbose && (strcmp(verbose, "true") == 0)) {
cmn_err(CE_WARN, "got kmod_test_postattach\n");
}
return (0);
}
#endif
static dacf_op_t kmod_op_test[] = {
#ifdef DEBUG
{ DACF_OPID_POSTATTACH, kmod_test_postattach },
#endif
{ DACF_OPID_END, NULL },
};
static dacf_opset_t kmod_opsets[] = {
#ifdef DEBUG
{ "kmod_test", kmod_op_test },
#endif
{ NULL, NULL },
};
struct dacfsw kmod_dacfsw = {
DACF_MODREV_1, kmod_opsets
};
|