2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <IOKit/IOLib.h>
30 #include <IOKit/IOInterleavedMemoryDescriptor.h>
32 #define super IOMemoryDescriptor
33 OSDefineMetaClassAndStructors(IOInterleavedMemoryDescriptor
, IOMemoryDescriptor
)
35 IOInterleavedMemoryDescriptor
* IOInterleavedMemoryDescriptor::withCapacity(
37 IODirection direction
)
40 // Create a new IOInterleavedMemoryDescriptor. The "buffer" will be made up
41 // of several memory descriptors, that are to be chained end-to-end to make up
42 // a single memory descriptor.
45 IOInterleavedMemoryDescriptor
* me
= new IOInterleavedMemoryDescriptor
;
47 if (me
&& !me
->initWithCapacity(
48 /* capacity */ capacity
,
49 /* direction */ direction
)) {
58 IOInterleavedMemoryDescriptor::initWithCapacity(
60 IODirection direction
)
63 // Initialize an IOInterleavedMemoryDescriptor. The "buffer" will be made up
64 // of several memory descriptors, that are to be chained end-to-end to make up
65 // a single memory descriptor.
70 // Ask our superclass' opinion.
71 if (super::init() == false) {
75 // Initialize our minimal state.
79 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
80 #endif /* !__LP64__ */
85 _descriptors
= IONew(IOMemoryDescriptor
*, capacity
);
86 _descriptorOffsets
= IONew(IOByteCount
, capacity
);
87 _descriptorLengths
= IONew(IOByteCount
, capacity
);
89 if ((_descriptors
== NULL
) || (_descriptorOffsets
== NULL
) || (_descriptorLengths
== NULL
)) {
93 _descriptorCapacity
= capacity
;
99 IOInterleavedMemoryDescriptor::clearMemoryDescriptors( IODirection direction
)
103 for (index
= 0; index
< _descriptorCount
; index
++) {
104 if (_descriptorPrepared
) {
105 _descriptors
[index
]->complete(getDirection());
108 _descriptors
[index
]->release();
109 _descriptors
[index
] = NULL
;
111 _descriptorOffsets
[index
] = 0;
112 _descriptorLengths
[index
] = 0;
115 if (direction
!= kIODirectionNone
) {
116 _flags
= (_flags
& ~kIOMemoryDirectionMask
) | direction
;
118 _direction
= (IODirection
) (_flags
& kIOMemoryDirectionMask
);
119 #endif /* !__LP64__ */
122 _descriptorCount
= 0;
129 IOInterleavedMemoryDescriptor::setMemoryDescriptor(
130 IOMemoryDescriptor
* descriptor
,
134 if (_descriptorPrepared
|| (_descriptorCount
== _descriptorCapacity
)) {
138 if ((offset
+ length
) > descriptor
->getLength()) {
142 // if ( descriptor->getDirection() != getDirection() )
145 descriptor
->retain();
146 _descriptors
[_descriptorCount
] = descriptor
;
147 _descriptorOffsets
[_descriptorCount
] = offset
;
148 _descriptorLengths
[_descriptorCount
] = length
;
158 IOInterleavedMemoryDescriptor::free()
161 // Free all of this object's outstanding resources.
165 for (unsigned index
= 0; index
< _descriptorCount
; index
++) {
166 _descriptors
[index
]->release();
169 if (_descriptors
!= NULL
) {
170 IODelete(_descriptors
, IOMemoryDescriptor
*, _descriptorCapacity
);
173 if (_descriptorOffsets
!= NULL
) {
174 IODelete(_descriptorOffsets
, IOMemoryDescriptor
*, _descriptorCapacity
);
177 if (_descriptorLengths
!= NULL
) {
178 IODelete(_descriptorLengths
, IOMemoryDescriptor
*, _descriptorCapacity
);
186 IOInterleavedMemoryDescriptor::prepare(IODirection forDirection
)
189 // Prepare the memory for an I/O transfer.
191 // This involves paging in the memory and wiring it down for the duration
192 // of the transfer. The complete() method finishes the processing of the
193 // memory after the I/O transfer finishes.
197 IOReturn status
= kIOReturnSuccess
;
200 if (forDirection
== kIODirectionNone
) {
201 forDirection
= getDirection();
204 for (index
= 0; index
< _descriptorCount
; index
++) {
205 status
= _descriptors
[index
]->prepare(forDirection
);
206 if (status
!= kIOReturnSuccess
) {
211 if (status
!= kIOReturnSuccess
) {
212 for (unsigned indexUndo
= 0; indexUndo
< index
; indexUndo
++) {
213 statusUndo
= _descriptors
[index
]->complete(forDirection
);
214 assert(statusUndo
== kIOReturnSuccess
);
218 if (status
== kIOReturnSuccess
) {
219 _descriptorPrepared
= true;
226 IOInterleavedMemoryDescriptor::complete(IODirection forDirection
)
229 // Complete processing of the memory after an I/O transfer finishes.
231 // This method shouldn't be called unless a prepare() was previously issued;
232 // the prepare() and complete() must occur in pairs, before and after an I/O
237 IOReturn statusFinal
= kIOReturnSuccess
;
239 if (forDirection
== kIODirectionNone
) {
240 forDirection
= getDirection();
243 for (unsigned index
= 0; index
< _descriptorCount
; index
++) {
244 status
= _descriptors
[index
]->complete(forDirection
);
245 if (status
!= kIOReturnSuccess
) {
246 statusFinal
= status
;
248 assert(status
== kIOReturnSuccess
);
251 _descriptorPrepared
= false;
257 IOInterleavedMemoryDescriptor::getPhysicalSegment(
259 IOByteCount
* length
,
260 IOOptionBits options
)
263 // This method returns the physical address of the byte at the given offset
264 // into the memory, and optionally the length of the physically contiguous
265 // segment from that offset.
270 assert(offset
<= _length
);
272 for (unsigned index
= 0; index
< _descriptorCount
; index
++) {
273 if (offset
< _descriptorLengths
[index
]) {
274 pa
= _descriptors
[index
]->getPhysicalSegment(_descriptorOffsets
[index
] + offset
, length
, options
);
275 if ((_descriptorLengths
[index
] - offset
) < *length
) {
276 *length
= _descriptorLengths
[index
] - offset
;
280 offset
-= _descriptorLengths
[index
];