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
|
'\" te
.\" Copyright (c) 2006, Sun Microsystems, Inc.
.\" All Rights Reserved
.\" 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_SOFT_STATE 9F "Jan 16, 2006"
.SH NAME
ddi_soft_state, ddi_get_soft_state, ddi_soft_state_fini, ddi_soft_state_free,
ddi_soft_state_init, ddi_soft_state_zalloc \- driver soft state utility
routines
.SH SYNOPSIS
.LP
.nf
#include <sys/ddi.h>
#include <sys/sunddi.h>
\fBvoid *\fR\fBddi_get_soft_state\fR(\fBvoid\fR \fI*state\fR, \fBint\fR \fIitem\fR);
.fi
.LP
.nf
\fBvoid\fR \fBddi_soft_state_fini\fR(\fBvoid\fR \fI**state_p\fR);
.fi
.LP
.nf
\fBvoid\fR \fBddi_soft_state_free\fR(\fBvoid\fR \fI*state\fR, \fBint\fR \fIitem\fR);
.fi
.LP
.nf
\fBint\fR \fBddi_soft_state_init\fR(\fBvoid\fR \fI**state_p\fR, \fBsize_t\fR \fIsize\fR, \fBsize_t\fR \fIn_items\fR);
.fi
.LP
.nf
\fBint\fR \fBddi_soft_state_zalloc\fR(\fBvoid\fR \fI*state\fR, \fBint\fR \fIitem\fR);
.fi
.SH INTERFACE LEVEL
.sp
.LP
Solaris DDI specific (Solaris DDI).
.SH PARAMETERS
.sp
.ne 2
.na
\fB\fIstate_p\fR\fR
.ad
.RS 11n
Address of the opaque state pointer which will be initialized by
\fBddi_soft_state_init()\fR to point to implementation dependent data.
.RE
.sp
.ne 2
.na
\fB\fIsize\fR\fR
.ad
.RS 11n
Size of the item which will be allocated by subsequent calls to
\fBddi_soft_state_zalloc()\fR.
.RE
.sp
.ne 2
.na
\fB\fIn_items\fR\fR
.ad
.RS 11n
A hint of the number of items which will be preallocated; zero is allowed.
.RE
.sp
.ne 2
.na
\fB\fIstate\fR\fR
.ad
.RS 11n
An opaque pointer to implementation-dependent data that describes the soft
state.
.RE
.sp
.ne 2
.na
\fB\fIitem\fR\fR
.ad
.RS 11n
The item number for the state structure; usually the instance number of the
associated devinfo node.
.RE
.SH DESCRIPTION
.sp
.LP
Most device drivers maintain state information with each instance of the device
they control; for example, a soft copy of a device control register, a mutex
that must be held while accessing a piece of hardware, a partition table, or a
unit structure. These utility routines are intended to help device drivers
manage the space used by the driver to hold such state information.
.sp
.LP
For example, if the driver holds the state of each instance in a single state
structure, these routines can be used to dynamically allocate and deallocate a
separate structure for each instance of the driver as the instance is attached
and detached.
.sp
.LP
To use the routines, the driver writer needs to declare a state pointer,
\fIstate_p\fR, which the implementation uses as a place to hang a set of
per-driver structures; everything else is managed by these routines.
.sp
.LP
The routine \fBddi_soft_state_init()\fR is usually called in the driver's
\fB_init\fR(9E) routine to initialize the state pointer, set the size of the
soft state structure, and to allow the driver to pre-allocate a given number of
such structures if required.
.sp
.LP
The routine \fBddi_soft_state_zalloc()\fR is usually called in the driver's
\fBattach\fR(9E) routine. The routine is passed an item number which is used
to refer to the structure in subsequent calls to \fBddi_get_soft_state()\fR and
\fBddi_soft_state_free()\fR. The item number is usually just the instance
number of the \fBdevinfo\fR node, obtained with \fBddi_get_instance\fR(9F). The
routine attempts to allocate space for the new structure, and if the space
allocation was successful, \fBDDI_SUCCESS\fR is returned to the caller.
Returned memory is zeroed.
.sp
.LP
A pointer to the space previously allocated for a soft state structure can be
obtained by calling \fBddi_get_soft_state()\fR with the appropriate item
number.
.sp
.LP
The space used by a given soft state structure can be returned to the system
using \fBddi_soft_state_free()\fR. This routine is usually called from the
driver's \fBdetach\fR(9E) entry point.
.sp
.LP
The space used by all the soft state structures allocated on a given state
pointer, together with the housekeeping information used by the implementation
can be returned to the system using \fBddi_soft_state_fini()\fR. This routine
can be called from the driver's \fB_fini\fR(9E) routine.
.sp
.LP
The \fBddi_soft_state_zalloc()\fR, \fBddi_soft_state_free()\fR and
\fBddi_get_soft_state()\fR routines coordinate access to the underlying data
structures in an MT-safe fashion, thus no additional locks should be necessary.
.SH RETURN VALUES
.sp
.LP
\fBddi_get_soft_state()\fR
.sp
.ne 2
.na
\fB\fINULL\fR\fR
.ad
.RS 11n
The requested state structure was not allocated at the time of the call.
.RE
.sp
.ne 2
.na
\fB\fIpointer\fR\fR
.ad
.RS 11n
The pointer to the state structure.
.RE
.sp
.LP
\fBddi_soft_state_init()\fR
.sp
.ne 2
.na
\fB\fB0\fR\fR
.ad
.RS 10n
The allocation was successful.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
Either the \fBsize\fR parameter was zero, or the \fIstate_p\fR parameter was
invalid.
.RE
.sp
.LP
\fBddi_soft_state_zalloc()\fR
.sp
.ne 2
.na
\fB\fBDDI_SUCCESS\fR\fR
.ad
.RS 15n
The allocation was successful.
.RE
.sp
.ne 2
.na
\fB\fBDDI_FAILURE\fR\fR
.ad
.RS 15n
The routine failed to allocate the storage required; either the \fIstate\fR
parameter was invalid, the item number was negative, or an attempt was made to
allocate an item number that was already allocated.
.RE
.SH CONTEXT
.sp
.LP
The \fBddi_soft_state_init()\fR and \fBddi_soft_state_alloc()\fR functions can
be called from user or kernel context only, since they may internally call
\fBkmem_zalloc\fR(9F) with the \fBKM_SLEEP\fR flag.
.sp
.LP
The \fBddi_soft_state_fini()\fR, \fBddi_soft_state_free()\fR and
\fBddi_get_soft_state()\fR routines can be called from any driver context.
.SH EXAMPLES
.LP
\fBExample 1 \fRCreating and Removing Data Structures
.sp
.LP
The following example shows how the routines described above can be used in
terms of the driver entry points of a character-only driver. The example
concentrates on the portions of the code that deal with creating and removing
the driver's data structures.
.sp
.in +2
.nf
typedef struct {
volatile caddr_t *csr; /* device registers */
kmutex_t csr_mutex; /* protects 'csr' field */
unsigned int state;
dev_info_t *dip; /* back pointer to devinfo */
} devstate_t;
static void *statep;
int
_init(void)
{
int error;
error = ddi_soft_state_init(&statep, sizeof (devstate_t), 0);
if (error != 0)
return (error);
if ((error = mod_install(&modlinkage)) != 0)
ddi_soft_state_fini(&statep);
return (error);
}
int
_fini(void)
{
int error;
if ((error = mod_remove(&modlinkage)) != 0)
return (error);
ddi_soft_state_fini(&statep);
return (0);
}
static int
xxattach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
int instance;
devstate_t *softc;
switch (cmd) {
case DDI_ATTACH:
instance = ddi_get_instance(dip);
if (ddi_soft_state_zalloc(statep, instance) != DDI_SUCCESS)
return (DDI_FAILURE);
softc = ddi_get_soft_state(statep, instance);
softc->dip = dip;
...
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
static int
xxdetach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
int instance;
switch (cmd) {
case DDI_DETACH:
instance = ddi_get_instance(dip);
...
ddi_soft_state_free(statep, instance);
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
static int
xxopen(dev_t *devp, int flag, int otyp, cred_t *cred_p)
{
devstate_t *softc;
int instance;
instance = getminor(*devp);
if ((softc = ddi_get_soft_state(statep, instance)) == NULL)
return (ENXIO);
...
softc->state |= XX_IN_USE;
...
return (0);
}
.fi
.in -2
.SH SEE ALSO
.sp
.LP
\fB_fini\fR(9E), \fB_init\fR(9E), \fBattach\fR(9E), \fBdetach\fR(9E),
\fBddi_get_instance\fR(9F), \fBgetminor\fR(9F), \fBkmem_zalloc\fR(9F)
.sp
.LP
\fIWriting Device Drivers\fR
.SH WARNINGS
.sp
.LP
There is no attempt to validate the \fBitem\fR parameter given to
\fBddi_soft_state_zalloc()\fR other than it must be a positive signed integer.
Therefore very large item numbers may cause the driver to hang forever waiting
for virtual memory resources that can never be satisfied.
.SH NOTES
.sp
.LP
If necessary, a hierarchy of state structures can be constructed by embedding
state pointers in higher order state structures.
.SH DIAGNOSTICS
.sp
.LP
All of the messages described below usually indicate bugs in the driver and
should not appear in normal operation of the system.
.sp
.in +2
.nf
WARNING: ddi_soft_state_zalloc: bad handle
WARNING: ddi_soft_state_free: bad handle
WARNING: ddi_soft_state_fini: bad handle
.fi
.in -2
.sp
.LP
The implementation-dependent information kept in the state variable is corrupt.
.sp
.in +2
.nf
WARNING: ddi_soft_state_free: null handle
WARNING: ddi_soft_state_fini: null handle
.fi
.in -2
.sp
.LP
The routine has been passed a null or corrupt state pointer. Check that
\fBddi_soft_state_init()\fR has been called.
.sp
.in +2
.nf
WARNING: ddi_soft_state_free: item %d not in range [0..%d]
.fi
.in -2
.sp
.LP
The routine has been asked to free an item which was never allocated. The
message prints out the invalid item number and the acceptable range.
|