13 #include <gpxe/list.h>
16 * I/O buffer alignment
18 * I/O buffers allocated via alloc_iob() are guaranteed to be
19 * physically aligned to this boundary. Some cards cannot DMA across
20 * a 4kB boundary. With a standard Ethernet MTU, aligning to a 2kB
21 * boundary is sufficient to guarantee no 4kB boundary crossings. For
22 * a jumbo Ethernet MTU, a packet may be larger than 4kB anyway.
24 #define IOB_ALIGN 2048
27 * Minimum I/O buffer length
29 * alloc_iob() will round up the allocated length to this size if
30 * necessary. This is used on behalf of hardware that is not capable
36 * A persistent I/O buffer
38 * This data structure encapsulates a long-lived I/O buffer. The
39 * buffer may be passed between multiple owners, queued for possible
40 * retransmission, etc.
43 /** List of which this buffer is a member
45 * The list must belong to the current owner of the buffer.
46 * Different owners may maintain different lists (e.g. a
47 * retransmission list for TCP).
49 struct list_head list;
51 /** Start of the buffer */
57 /** End of the buffer */
62 * Reserve space at start of I/O buffer
65 * @v len Length to reserve
66 * @ret data Pointer to new start of buffer
68 static inline void * iob_reserve ( struct io_buffer *iobuf, size_t len ) {
71 assert ( iobuf->tail <= iobuf->end );
76 * Add data to start of I/O buffer
79 * @v len Length to add
80 * @ret data Pointer to new start of buffer
82 static inline void * iob_push ( struct io_buffer *iobuf, size_t len ) {
84 assert ( iobuf->data >= iobuf->head );
89 * Remove data from start of I/O buffer
92 * @v len Length to remove
93 * @ret data Pointer to new start of buffer
95 static inline void * iob_pull ( struct io_buffer *iobuf, size_t len ) {
97 assert ( iobuf->data <= iobuf->tail );
102 * Add data to end of I/O buffer
104 * @v iobuf I/O buffer
105 * @v len Length to add
106 * @ret data Pointer to newly added space
108 static inline void * iob_put ( struct io_buffer *iobuf, size_t len ) {
109 void *old_tail = iobuf->tail;
111 assert ( iobuf->tail <= iobuf->end );
116 * Remove data from end of I/O buffer
118 * @v iobuf I/O buffer
119 * @v len Length to remove
121 static inline void iob_unput ( struct io_buffer *iobuf, size_t len ) {
123 assert ( iobuf->tail >= iobuf->data );
127 * Empty an I/O buffer
129 * @v iobuf I/O buffer
131 static inline void iob_empty ( struct io_buffer *iobuf ) {
132 iobuf->tail = iobuf->data;
136 * Calculate length of data in an I/O buffer
138 * @v iobuf I/O buffer
139 * @ret len Length of data in buffer
141 static inline size_t iob_len ( struct io_buffer *iobuf ) {
142 return ( iobuf->tail - iobuf->data );
146 * Calculate available space at start of an I/O buffer
148 * @v iobuf I/O buffer
149 * @ret len Length of data available at start of buffer
151 static inline size_t iob_headroom ( struct io_buffer *iobuf ) {
152 return ( iobuf->data - iobuf->head );
156 * Calculate available space at end of an I/O buffer
158 * @v iobuf I/O buffer
159 * @ret len Length of data available at end of buffer
161 static inline size_t iob_tailroom ( struct io_buffer *iobuf ) {
162 return ( iobuf->end - iobuf->tail );
166 * Ensure I/O buffer has sufficient headroom
168 * @v iobuf I/O buffer
169 * @v len Required headroom
171 * This function currently only checks for the required headroom; it
172 * does not reallocate the I/O buffer if required. If we ever have a
173 * code path that requires this functionality, it's a fairly trivial
176 static inline __attribute__ (( always_inline )) int
177 iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
178 if ( iob_headroom ( iobuf ) >= len )
183 extern struct io_buffer * alloc_iob ( size_t len );
184 extern void free_iob ( struct io_buffer *iobuf );
185 extern void iob_pad ( struct io_buffer *iobuf, size_t min_len );
187 #endif /* _GPXE_IOBUF_H */