2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
25 * IOMbufMemoryCursor.h created by gvdl on 1999-1-20
29 #ifndef _IOKIT_NETWORK_IOMBUFMEMORYCURSOR_H
30 #define _IOKIT_NETWORK_IOMBUFMEMORYCURSOR_H
32 #include <IOKit/IOMemoryCursor.h>
36 /*! @class IOMbufMemoryCursor : public IOMemoryCursor
37 @abstract A mechanism to convert mbuf chains to physical addresses.
38 @discussion The IOMbufMemoryCursor defines the super class that all
39 specific mbuf cursors must inherit from, but a mbuf cursor can be created
40 without a specific formal subclass by just providing a segment function to
41 the initializers. This class performs the task of walking a given
42 mbuf chain and creating a physical scatter/gather list appropriate for
43 the target hardware. When necessary, this class may also coalesce
44 mbuf chains when the generated scatter/gather list exceeds the specified
45 hardware limit. However, this should be avoided since it exacts a
48 A driver is expected to create a mbuf cursor and configure it to match the
49 limitations of it's DMA hardware; for instance the mbuf cursor used by
50 an Ethernet controller driver may have a maximum physical segment size
51 of 1520, and allow for up to 6 physical segments. Thus it would create a
52 mbuf cursor with a maxSegmentSize of 1520 and a maxNumSegments of 6.
53 The driver may choose to supply an OutputSegmentFunc function to
54 format the output of each scatter/gather segment to match the
55 hardware descriptor format, or it may use a subclass of
56 IOMbufMemoryCursor to generate IOPhysicalSegment segments with
59 A driver may also create more than one mbuf cursor, perhaps one
60 dedicated for the transmit thread, and the other for the receive thread.
61 This becomes a requirement when the driver is multi-threaded, since the
62 mbuf cursor maintains state and does not support reentrancy. */
64 class IOMbufMemoryCursor
: public IOMemoryCursor
66 OSDeclareAbstractStructors(IOMbufMemoryCursor
)
69 UInt32 maxNumSegments
;
71 UInt32 packetTooBigErrors
;
73 struct ExpansionData
{ };
75 Reserved for future use. (Internal use only) */
76 ExpansionData
*reserved
;
78 virtual bool initWithSpecification(OutputSegmentFunc outSeg
,
79 UInt32 maxSegmentSize
,
80 UInt32 maxTransferSize
,
84 /*! @function initWithSpecification
85 @abstract Primary initializer for the IOMbufMemoryCursor class.
86 @param outSeg Function to call to output one physical segment.
87 @param maxSegmentSize Maximum allowable size for one segment.
88 @param maxNumSegments Maximum number of segments.
89 @result true if the inherited classes and this instance initialized
92 virtual bool initWithSpecification(OutputSegmentFunc outSeg
,
93 UInt32 maxSegmentSize
,
94 UInt32 maxNumSegments
);
96 /*! @function genPhysicalSegments
97 @abstract Generate a physical scatter/gather list given a mbuf packet.
98 @discussion Generates a list of physical segments from the given mbuf.
99 @param packet The mbuf packet.
100 @param vector Void pointer to base of output physical scatter/gather list.
101 Always passed directly onto the OutputSegmentFunc without interpretation
103 @param maxSegs Maximum number of segments that can be written to segments
105 @param doCoalesce Set to true to perform coalescing when the required
106 number of segments exceeds the specified limit, otherwise abort and
108 @result The number of segments that were filled in is returned, or
109 0 if an error occurred. */
111 virtual UInt32
genPhysicalSegments(struct mbuf
* packet
, void * vector
,
112 UInt32 maxSegs
, bool doCoalesce
);
114 /*! @function getAndResetCoalesceCount
115 @abstract Returns a count of the total number of mbuf chains coalesced
116 by genPhysicalSegments(). The counter is then reset to 0.
117 @result The coalesce count. */
119 UInt32
getAndResetCoalesceCount();
121 // Virtual function padding
122 OSMetaClassDeclareReservedUnused( IOMbufMemoryCursor
, 0);
123 OSMetaClassDeclareReservedUnused( IOMbufMemoryCursor
, 1);
124 OSMetaClassDeclareReservedUnused( IOMbufMemoryCursor
, 2);
125 OSMetaClassDeclareReservedUnused( IOMbufMemoryCursor
, 3);
129 /*! @class IOMbufNaturalMemoryCursor : public IOMbufMemoryCursor
130 @abstract A IOMbufMemoryCursor subclass that outputs a vector of
131 IOPhysicalSegments in the natural byte orientation for the cpu.
132 @discussion The IOMbufNaturalMemoryCursor would be used when it is too
133 difficult to implement an OutputSegmentFunc that is more appropriate for
134 your hardware. This cursor just outputs an array of IOPhysicalSegments. */
136 class IOMbufNaturalMemoryCursor
: public IOMbufMemoryCursor
138 OSDeclareDefaultStructors(IOMbufNaturalMemoryCursor
)
142 /*! @function withSpecification
143 @abstract Factory function to create and initialize an
144 IOMbufNaturalMemoryCursor in one operation, see
145 IOMbufMemoryCursor::initWithSpecification.
146 @param maxSegmentSize Maximum allowable size for one segment.
147 @param maxNumSegments Maximum number of segments.
148 @result A new mbuf cursor if successfully created and initialized,
151 static IOMbufNaturalMemoryCursor
* withSpecification(UInt32 maxSegmentSize
,
152 UInt32 maxNumSegments
);
154 /*! @function getPhysicalSegments
155 @abstract Generate a cpu natural physical scatter/gather list from a given
157 @param packet The mbuf packet.
158 @param vector Pointer to an array of IOPhysicalSegments for the output
159 physical scatter/gather list.
160 @param numVectorSegments Maximum number of IOPhysicalSegments accepted.
161 @result The number of segments that were filled in is returned, or
162 0 if an error occurred. */
164 UInt32
getPhysicalSegments(struct mbuf
* packet
,
165 struct IOPhysicalSegment
* vector
,
166 UInt32 numVectorSegments
= 0);
168 /*! @function getPhysicalSegmentsWithCoalesce
169 @abstract Generate a cpu natural physical scatter/gather list from a given
171 @discussion Generate a cpu natural physical scatter/gather list from a
172 given mbuf. Coalesce mbuf chain when the number of segments in the
173 scatter/gather list exceeds numVectorSegments.
174 @param packet The mbuf packet.
175 @param vector Pointer to an array of IOPhysicalSegments for the output
176 physical scatter/gather list.
177 @param numVectorSegments Maximum number of IOPhysicalSegments accepted.
178 @result The number of segments that were filled in is returned, or
179 0 if an error occurred. */
181 UInt32
getPhysicalSegmentsWithCoalesce(struct mbuf
* packet
,
182 struct IOPhysicalSegment
* vector
,
183 UInt32 numVectorSegments
= 0);
186 //===========================================================================
187 //===========================================================================
189 /*! @class IOMbufBigMemoryCursor : public IOMbufMemoryCursor
190 @abstract A IOMbufMemoryCursor subclass that outputs a vector of
191 IOPhysicalSegments in the big endian byte order.
192 @discussion The IOMbufBigMemoryCursor would be used when the DMA hardware
193 requires a big endian address and length pair. This cursor outputs an
194 array of IOPhysicalSegments that are encoded in big-endian format. */
196 class IOMbufBigMemoryCursor
: public IOMbufMemoryCursor
198 OSDeclareDefaultStructors(IOMbufBigMemoryCursor
)
202 /*! @function withSpecification
203 @abstract Factory function to create and initialize an
204 IOMbufBigMemoryCursor in one operation, see
205 IOMbufMemoryCursor::initWithSpecification.
206 @param maxSegmentSize Maximum allowable size for one segment.
207 @param maxNumSegments Maximum number of segments.
208 @result A new mbuf cursor if successfully created and initialized,
211 static IOMbufBigMemoryCursor
* withSpecification(UInt32 maxSegmentSize
,
212 UInt32 maxNumSegments
);
214 /*! @function getPhysicalSegments
215 @abstract Generate a big endian physical scatter/gather list from a given
217 @param packet The mbuf packet.
218 @param vector Pointer to an array of IOPhysicalSegments for the output
219 physical scatter/gather list.
220 @param numVectorSegments Maximum number of IOPhysicalSegments accepted.
221 @result The number of segments that were filled in is returned, or
222 0 if an error occurred. */
224 UInt32
getPhysicalSegments(struct mbuf
* packet
,
225 struct IOPhysicalSegment
* vector
,
226 UInt32 numVectorSegments
= 0);
228 /*! @function getPhysicalSegmentsWithCoalesce
229 @abstract Generate a big endian physical scatter/gather list from a given
231 @discussion Generate a big endian physical scatter/gather list from a
232 given mbuf. Coalesce mbuf chain when the number of segments in the
233 scatter/gather list exceeds numVectorSegments.
234 @param packet The mbuf packet.
235 @param vector Pointer to an array of IOPhysicalSegments for the output
236 physical scatter/gather list.
237 @param numVectorSegments Maximum number of IOPhysicalSegments accepted.
238 @result The number of segments that were filled in is returned, or
239 0 if an error occurred. */
241 UInt32
getPhysicalSegmentsWithCoalesce(struct mbuf
* packet
,
242 struct IOPhysicalSegment
* vector
,
243 UInt32 numVectorSegments
= 0);
246 //===========================================================================
247 //===========================================================================
249 /*! @class IOMbufLittleMemoryCursor : public IOMbufMemoryCursor
250 @abstract A IOMbufMemoryCursor subclass that outputs a vector of
251 IOPhysicalSegments in the little endian byte order.
252 @discussion The IOMbufLittleMemoryCursor would be used when the DMA
253 hardware requires a little endian address and length pair. This cursor
254 outputs an array of IOPhysicalSegments that are encoded in little endian
257 class IOMbufLittleMemoryCursor
: public IOMbufMemoryCursor
259 OSDeclareDefaultStructors(IOMbufLittleMemoryCursor
)
263 /*! @function withSpecification
264 @abstract Factory function to create and initialize an
265 IOMbufLittleMemoryCursor in one operation, see
266 IOMbufMemoryCursor::initWithSpecification.
267 @param maxSegmentSize Maximum allowable size for one segment.
268 @param maxNumSegments Maximum number of segments.
269 @result A new mbuf cursor if successfully created and initialized,
272 static IOMbufLittleMemoryCursor
* withSpecification(UInt32 maxSegmentSize
,
273 UInt32 maxNumSegments
);
275 /*! @function getPhysicalSegments
276 @abstract Generate a little endian physical scatter/gather list from a
278 @param packet The mbuf packet.
279 @param vector Pointer to an array of IOPhysicalSegments for the output
280 physical scatter/gather list.
281 @param numVectorSegments Maximum number of IOPhysicalSegments accepted.
282 @result The number of segments that were filled in is returned, or
283 0 if an error occurred. */
285 UInt32
getPhysicalSegments(struct mbuf
* packet
,
286 struct IOPhysicalSegment
* vector
,
287 UInt32 numVectorSegments
= 0);
289 /*! @function getPhysicalSegmentsWithCoalesce
290 @abstract Generate a little endian physical scatter/gather list from a
292 @discussion Generate a little endian physical scatter/gather list from a
293 given mbuf. Coalesce mbuf chain when the number of segments in the
294 scatter/gather list exceeds numVectorSegments.
295 @param packet The mbuf packet.
296 @param vector Pointer to an array of IOPhysicalSegments for the output
297 physical scatter/gather list.
298 @param numVectorSegments Maximum number of IOPhysicalSegments accepted.
299 @result The number of segments that were filled in is returned, or
300 0 if an error occurred. */
302 UInt32
getPhysicalSegmentsWithCoalesce(struct mbuf
* packet
,
303 struct IOPhysicalSegment
* vector
,
304 UInt32 numVectorSegments
= 0);
309 struct IODBDMADescriptor
;
311 //===========================================================================
312 //===========================================================================
314 /*! @class IOMbufDBDMAMemoryCursor : public IOMbufMemoryCursor
315 @abstract A IOMbufMemoryCursor subclass that outputs a vector of
316 IODBDMADescriptors. */
318 class IOMbufDBDMAMemoryCursor
: public IOMbufMemoryCursor
320 OSDeclareDefaultStructors(IOMbufDBDMAMemoryCursor
)
324 /*! @function withSpecification
325 @abstract Factory function to create and initialize an
326 IOMbufDBDMAMemoryCursor in one operation, see
327 IOMbufMemoryCursor::initWithSpecification.
328 @param maxSegmentSize Maximum allowable size for one segment.
329 @param maxNumSegments Maximum number of segments.
330 @result A new mbuf cursor if successfully created and initialized,
333 static IOMbufDBDMAMemoryCursor
* withSpecification(UInt32 maxSegmentSize
,
334 UInt32 maxNumSegments
);
336 /*! @function getPhysicalSegments
337 @abstract Generate a DBDMA descriptor list from a given mbuf.
338 @param packet The mbuf packet.
339 @param vector Pointer to an array of IODBDMADescriptor for the output list.
340 @param numVectorSegments Maximum number of IODBDMADescriptors accepted.
341 @result The number of segments that were filled in is returned, or
342 0 if an error occurred. */
344 UInt32
getPhysicalSegments(struct mbuf
* packet
,
345 struct IODBDMADescriptor
*vector
,
346 UInt32 numVectorSegments
= 0);
348 /*! @function getPhysicalSegmentsWithCoalesce
349 @abstract Generate a DBDMA descriptor list from a given mbuf.
350 @discussion Generate a DBDMA descriptor list from a given mbuf.
351 Coalesce mbuf chain when the number of elements in the list exceeds
353 @param packet The mbuf packet.
354 @param vector Pointer to an array of IODBDMADescriptor for the output list.
355 @param numVectorSegments Maximum number of IODBDMADescriptors accepted.
356 @result The number of segments that were filled in is returned, or
357 0 if an error occurred. */
359 UInt32
getPhysicalSegmentsWithCoalesce(struct mbuf
* packet
,
360 struct IODBDMADescriptor
* vector
,
361 UInt32 numVectorSegments
= 0);
366 inline UInt32
IOMbufMemoryCursor::getAndResetCoalesceCount()
368 UInt32 cnt
= coalesceCount
; coalesceCount
= 0; return cnt
;
372 IOMbufNaturalMemoryCursor::getPhysicalSegments(struct mbuf
*packet
,
373 struct IOPhysicalSegment
*vector
,
374 UInt32 numVectorSegments
= 0)
376 return genPhysicalSegments(packet
, vector
, numVectorSegments
, false);
380 IOMbufNaturalMemoryCursor::getPhysicalSegmentsWithCoalesce(struct mbuf
*packet
,
381 struct IOPhysicalSegment
*vector
,
382 UInt32 numVectorSegments
= 0)
384 return genPhysicalSegments(packet
, vector
, numVectorSegments
, true);
388 IOMbufBigMemoryCursor::getPhysicalSegments(struct mbuf
*packet
,
389 struct IOPhysicalSegment
*vector
,
390 UInt32 numVectorSegments
= 0)
392 return genPhysicalSegments(packet
, vector
, numVectorSegments
, false);
396 IOMbufBigMemoryCursor::getPhysicalSegmentsWithCoalesce(struct mbuf
*packet
,
397 struct IOPhysicalSegment
*vector
,
398 UInt32 numVectorSegments
= 0)
400 return genPhysicalSegments(packet
, vector
, numVectorSegments
, true);
404 IOMbufLittleMemoryCursor::getPhysicalSegments(struct mbuf
*packet
,
405 struct IOPhysicalSegment
*vector
,
406 UInt32 numVectorSegments
= 0)
408 return genPhysicalSegments(packet
, vector
, numVectorSegments
, false);
412 IOMbufLittleMemoryCursor::getPhysicalSegmentsWithCoalesce(struct mbuf
*packet
,
413 struct IOPhysicalSegment
*vector
,
414 UInt32 numVectorSegments
= 0)
416 return genPhysicalSegments(packet
, vector
, numVectorSegments
, true);
421 IOMbufDBDMAMemoryCursor::getPhysicalSegments(struct mbuf
*packet
,
422 struct IODBDMADescriptor
*vector
,
423 UInt32 numVectorSegments
= 0)
425 return genPhysicalSegments(packet
, vector
, numVectorSegments
, false);
429 IOMbufDBDMAMemoryCursor::getPhysicalSegmentsWithCoalesce(struct mbuf
*packet
,
430 struct IODBDMADescriptor
*vector
,
431 UInt32 numVectorSegments
= 0)
433 return genPhysicalSegments(packet
, vector
, numVectorSegments
, true);
437 #endif /* !_IOKIT_NETWORK_IOMBUFMEMORYCURSOR_H */