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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
'\" te
.\" Copyright (c) 2009, Sun Microsystems, Inc.
.\" 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]
.TH DDI_CB_REGISTER 9F "Jan 30, 2009"
.SH NAME
ddi_cb_register, ddi_cb_unregister \- register and unregister a device driver
callback handler
.SH SYNOPSIS
.LP
.nf
#include <sys/sunddi.h>
\fBint\fR \fBddi_cb_register\fR(\fBdev_info_t *\fR\fIdip\fR, \fBddi_cb_flags_t\fR \fIflags\fR,
\fBddi_cb_func_t\fR \fIcbfunc\fR, \fBvoid *\fR\fIarg1\fR, \fBvoid *\fR\fIarg2\fR,
\fBddi_cb_handle_t *\fR \fIret_hdlp\fR);
.fi
.LP
.nf
\fBint\fR \fBddi_cb_unregister\fR(\fBddi_cb_handle_t\fR \fIhdl\fR);
.fi
.SH INTERFACE LEVEL
.sp
.LP
Solaris DDI specific (Solaris DDI).
.SH PARAMETERS
.sp
.LP
\fBddi_cb_register()\fR
.sp
.ne 2
.na
\fB\fIdip\fR\fR
.ad
.RS 12n
Pointer to the \fBdev_info\fR structure.
.RE
.sp
.ne 2
.na
\fB\fIflags\fR\fR
.ad
.RS 12n
Flags to determine which callback events can be handled.
.RE
.sp
.ne 2
.na
\fB\fIcbfunc\fR\fR
.ad
.RS 12n
Callback handler function.
.RE
.sp
.ne 2
.na
\fB\fIarg1\fR\fR
.ad
.RS 12n
First argument to the callback handler.
.RE
.sp
.ne 2
.na
\fB\fIarg2\fR\fR
.ad
.RS 12n
Second (optional) argument to the callback handler.
.RE
.sp
.ne 2
.na
\fB\fIret_hdlp\fR\fR
.ad
.RS 12n
Pointer to return a handle to the registered callback.
.RE
.sp
.LP
\fBddi_cb_unregister()\fR
.sp
.ne 2
.na
\fB\fIhdl\fR\fR
.ad
.RS 7n
Handle to the registered callback handler that is to be unregistered.
.RE
.SH DESCRIPTION
.sp
.LP
The \fBddi_cb_register()\fR function installs a callback handler which
processes various actions that require the driver's attention while it is
attached. The driver specifies which callback actions it can handle through the
flags parameter. With each relevant action, the specified callback function
passes the \fIarg1\fR and \fIarg2\fR arguments along with the description of
each callback event to the driver.
.sp
.LP
The \fBddi_cb_unregister()\fR function removes a previously installed callback
handler and prevents future processing of actions.
.sp
.LP
The \fIflags\fR parameter consists of the following:
.sp
.ne 2
.na
\fB\fBDDI_CB_FLAG_INTR\fR\fR
.ad
.RS 20n
The device driver participates in interrupt resource management. The device
driver may receive additional interrupt resources from the system, but only
because it can accept callback notices informing it when it has more or less
resources available. Callback notices can occur at anytime after the driver is
attached. Interrupt availability varies based on the overall needs of the
system.
.RE
.sp
.LP
The cdfunc is a callback handler with the following prototype:
.sp
.in +2
.nf
typedef int (*ddi_cb_func_t)(dev_info_t *dip,
ddi_cb_action_t action, void *cbarg,
void *arg1, void *arg2);
.fi
.in -2
.sp
.sp
.LP
The \fIcbfunc\fR routine with the arguments \fIdip\fR, \fIaction\fR,
\fIcbarg\fR, \fIarg1\fR and \fIarg2\fR is called upon receipt of any callbacks
for which the driver is registered. The callback handler returns
\fBDDI_SUCCESS\fR if the callback was handled successfully, \fBDDI_ENOTSUP\fR
if it received a callback action that it did not know how to process, or
\fBDDI_FAILURE\fR if it has an internal failure while processing an action.
.sp
.LP
The \fIaction\fR parameter can be one of the following:
.sp
.ne 2
.na
\fB\fIDDI_CB_INTR_ADD\fR\fR
.ad
.RS 22n
For interrupt resource management, the driver has more available interrupts.
The driver can allocate more interrupt vectors and then set up more interrupt
handling functions by using \fBddi_intr_alloc\fR(9F).
.RE
.sp
.ne 2
.na
\fB\fIDDI_CB_INTR_REMOVE\fR\fR
.ad
.RS 22n
For interrupt resource management, the driver has fewer available interrupts.
The driver must release any previously allocated interrupts in excess of what
is now available by using \fBddi_intr_free\fR(9F).
.RE
.sp
.LP
The \fIcbarg\fR parameter points to an action-specific argument. Each class of
registered actions specifies its own data structure that a callback handler
should dereference when it receives those actions.
.sp
.LP
The \fIcbarg\fR parameter is defined as an integer in the case of
\fBDDI_CB_INTR_ADD\fR and \fBDDI_CB_INTR_REMOVE\fR actions. The callback
handler should cast the \fIcbarg\fR parameter to an integer. The integer
represents how many interrupts have been added or removed from the total number
available to the device driver.
.sp
.LP
If a driver participates in interrupt resource management, it must register a
callback with the \fBDDI_CB_FLAG_INTR\fR flag. The driver then receives the
actions \fBDDI_CB_INTR_ADD\fR and \fBDDI_CB_INTR_REMOVE\fR whenever its
interrupt availability has changed. The callback handler should use the
interrupt functions \fBddi_intr_alloc\fR(9F) and \fBddi_intr_free\fR(9F)
functions to respond accordingly. A driver is not required to allocate all
interrupts that are available to it, but it is required to manage its
allocations so that it never uses more interrupts than are currently available.
.SH RETURN VALUES
.sp
.LP
The \fBddi_cb_register()\fR and \fBddi_cb_unregister()\fR functions return:
.sp
.ne 2
.na
\fB\fBDDI_SUCCESS\fR\fR
.ad
.RS 16n
on success
.RE
.sp
.ne 2
.na
\fB\fBDDI_EINVAL\fR\fR
.ad
.RS 16n
An invalid parameter was given when registering a callback handler, or an
invalid handle was given when unregistering.
.RE
.sp
.ne 2
.na
\fB\fBDDI_EALREADY\fR\fR
.ad
.RS 16n
An attempt was made to register a callback handler while a previous
registration still exists.
.RE
.sp
.LP
The \fIcbfunc\fR routine must return:
.sp
.ne 2
.na
\fB\fBDDI_SUCCESS\fR\fR
.ad
.RS 15n
on success
.RE
.sp
.ne 2
.na
\fB\fBDDI_ENOTSUP\fR\fR
.ad
.RS 15n
The device does not support the operation
.RE
.sp
.ne 2
.na
\fB\fBDDI_FAILURE\fR\fR
.ad
.RS 15n
Implementation specific failure
.RE
.SH CONTEXT
.sp
.LP
These functions can be called from kernel, non-interrupt context.
.SH EXAMPLES
.LP
\fBExample 1 \fRddi_cb_register
.sp
.in +2
.nf
/*
* attach(9F) routine.
*
* Creates soft state, registers callback handler, initializes
* hardware, and sets up interrupt handling for the driver.
*/
xx_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
xx_state_t *statep = NULL;
xx_intr_t *intrs = NULL;
ddi_intr_handle_t *hdls;
ddi_cb_handle_t cb_hdl;
int instance;
int type;
int types;
int nintrs;
int nactual;
int inum;
/* Get device instance */
instance = ddi_get_instance(dip);
switch (cmd) {
case DDI_ATTACH:
/* Get soft state */
if (ddi_soft_state_zalloc(state_list, instance) != 0)
return (DDI_FAILURE);
statep = ddi_get_soft_state(state_list, instance);
ddi_set_driver_private(dip, (caddr_t)statep);
statep->dip = dip;
/* Initialize hardware */
xx_initialize(statep);
/* Register callback handler */
if (ddi_cb_register(dip, DDI_CB_FLAG_INTR, xx_cbfunc,
statep, NULL, &cb_hdl) != 0) {
ddi_soft_state_free(state_list, instance);
return (DDI_FAILURE);
}
statep->cb_hdl = cb_hdl;
/* Select interrupt type */
ddi_intr_get_supported_types(dip, &types);
if (types & DDI_INTR_TYPE_MSIX) {
type = DDI_INTR_TYPE_MSIX;
} else if (types & DDI_INTR_TYPE_MSI) {
type = DDI_INTR_TYPE_MSI;
} else {
type = DDI_INTR_TYPE_FIXED;
}
statep->type = type;
/* Get number of supported interrupts */
ddi_intr_get_nintrs(dip, type, &nintrs);
/* Allocate interrupt handle array */
statep->hdls_size = nintrs * sizeof (ddi_intr_handle_t);
hdls = kmem_zalloc(statep->hdls_size, KMEM_SLEEP);
/* Allocate interrupt setup array */
statep->intrs_size = nintrs * sizeof (xx_intr_t);
statep->intrs = kmem_zalloc(statep->intrs_size, KMEM_SLEEP);
/* Allocate interrupt vectors */
ddi_intr_alloc(dip, hdls, type, 0, nintrs, &nactual, 0);
statep->nactual = nactual;
/* Configure interrupt handling */
xx_setup_interrupts(statep, nactual, statep->intrs);
/* Install and enable interrupt handlers */
for (inum = 0; inum < nactual; inum++) {
ddi_intr_add_handler(&statep->hdls[inum],
statep->intrs[inum].inthandler,
statep->intrs[inum].arg1,
statep->intrs[inum].arg2);
ddi_intr_enable(statep->hdls[inum]);
}
break;
case DDI_RESUME:
/* Get soft state */
statep = ddi_get_soft_state(state_list, instance);
if (statep == NULL)
return (DDI_FAILURE);
/* Resume hardware */
xx_resume(statep);
break;
}
return (DDI_SUCESS);
}
/*
* detach(9F) routine.
*
* Stops the hardware, disables interrupt handling, unregisters
* a callback handler, and destroys the soft state for the driver.
*/
xx_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
xx_state_t *statep = NULL;
int instance;
int inum;
/* Get device instance */
instance = ddi_get_instance(dip);
switch (cmd) {
case DDI_DETACH:
/* Get soft state */
statep = ddi_get_soft_state(state_list, instance);
if (statep == NULL)
return (DDI_FAILURE);
/* Stop device */
xx_uninitialize(statep);
/* Disable and free interrupts */
for (inum = 0; inum < statep->nactual; inum++) {
ddi_intr_disable(statep->hdls[inum]);
ddi_intr_remove_handler(statep->hdls[inum]);
ddi_intr_free(statep->hdls[inum]);
}
/* Unregister callback handler */
ddi_cb_unregister(statep->cb_hdl);
/* Free interrupt handle array */
kmem_free(statep->hdls, statep->hdls_size);
/* Free interrupt setup array */
kmem_free(statep->intrs, statep->intrs_size);
/* Free soft state */
ddi_soft_state_free(state_list, instance);
break;
case DDI_SUSPEND:
/* Get soft state */
statep = ddi_get_soft_state(state_list, instance);
if (statep == NULL)
return (DDI_FAILURE);
/* Suspend hardware */
xx_quiesce(statep);
break;
}
return (DDI_SUCCESS);
}
/*
* (*ddi_cbfunc)() routine.
*
* Adapt interrupt usage when availability changes.
*/
int
xx_cbfunc(dev_info_t *dip, ddi_cb_action_t cbaction, void *cbarg,
void *arg1, void *arg2)
{
xx_state_t *statep = (xx_state_t *)arg1;
int count;
int inum;
int nactual;
switch (cbaction) {
case DDI_CB_INTR_ADD:
case DDI_CB_INTR_REMOVE:
/* Get change in availability */
count = (int)(uintptr_t)cbarg;
/* Suspend hardware */
xx_quiesce(statep);
/* Tear down previous interrupt handling */
for (inum = 0; inum < statep->nactual; inum++) {
ddi_intr_disable(statep->hdls[inum]);
ddi_intr_remove_handler(statep->hdls[inum]);
}
/* Adjust interrupt vector allocations */
if (cbaction == DDI_CB_INTR_ADD) {
/* Allocate additional interrupt vectors */
ddi_intr_alloc(dip, statep->hdls, statep->type,
statep->nactual, count, &nactual, 0);
/* Update actual count of available interrupts */
statep->nactual += nactual;
} else {
/* Free removed interrupt vectors */
for (inum = statep->nactual - count;
inum < statep->nactual; inum++) {
ddi_intr_free(statep->hdls[inum]);
}
/* Update actual count of available interrupts */
statep->nactual -= count;
}
/* Configure interrupt handling */
xx_setup_interrupts(statep, statep->nactual, statep->intrs);
/* Install and enable interrupt handlers */
for (inum = 0; inum < statep->nactual; inum++) {
ddi_intr_add_handler(&statep->hdls[inum],
statep->intrs[inum].inthandler,
statep->intrs[inum].arg1,
statep->intrs[inum].arg2);
ddi_intr_enable(statep->hdls[inum]);
}
/* Resume hardware */
xx_resume(statep);
break;
default:
return (DDI_ENOTSUP);
}
return (DDI_SUCCESS);
}
.fi
.in -2
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp
.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
Interface Stability Private
_
MT-Level Unsafe
.TE
.SH SEE ALSO
.sp
.LP
\fBattributes\fR(5), \fBddi_intr_alloc\fR(9F), \fBddi_intr_free\fR(9F),
\fBddi_intr_set_nreq\fR(9F)
.SH NOTES
.sp
.LP
Users of these interfaces that register for \fBDDI_CB_FLAG_INTR\fR become
participants in interrupt resource management. With that participation comes a
responsibility to properly adjust interrupt usage. In the case of a
\fBDDI_CB_INTR_ADD\fR action, the system guarantees that a driver can allocate
a total number of interrupt resources up to its new number of available
interrupts. The total number of interrupt resources is the sum of all resources
allocated by the function \fBddi_intr_alloc\fR(9F), minus all previously
released by the function \fBddi_intr_free\fR(9F). In the case of a
\fBDDI_CB_INTR_REMOVE\fR action, the driver might have more interrupts
allocated than are now currently available. It is necessary for the driver to
release the excess interrupts, or it will have a negative impact on the
interrupt availability for other drivers in the system.
.sp
.LP
A failure to release interrupts in response to a \fBDDI_CB_INTR_REMOVE\fR
callback generates the following warning on the system console:
.sp
.in +2
.nf
WARNING: <driver><instance>: failed to release interrupts for
IRM (nintrs = ##, navail=##).
.fi
.in -2
.sp
.sp
.LP
Participation in interrupt resource management ends when a driver uses the
\fBddi_cb_unregister()\fR function to unregister its callback function. The
callback function must still operate properly until after the call to the
\fBddi_cb_unregister()\fR function completes. If \fBaddinterrupts\fR were given
to the driver because of its participation, then a final use of the callback
function occurs to release the additional interrupts. The call to the
\fBddi_cb_unregister()\fR function blocks until the final use of the
registered callback function is finished.
|