iPXE
iobuf.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <strings.h>
28 #include <errno.h>
29 #include <ipxe/malloc.h>
30 #include <ipxe/iobuf.h>
31 
32 /** @file
33  *
34  * I/O buffers
35  *
36  */
37 
38 /**
39  * Allocate I/O buffer with specified alignment and offset
40  *
41  * @v len Required length of buffer
42  * @v align Physical alignment
43  * @v offset Offset from physical alignment
44  * @ret iobuf I/O buffer, or NULL if none available
45  *
46  * @c align will be rounded up to the nearest power of two.
47  */
48 struct io_buffer * alloc_iob_raw ( size_t len, size_t align, size_t offset ) {
49  struct io_buffer *iobuf;
50  size_t padding;
51  size_t threshold;
52  unsigned int align_log2;
53  void *data;
54 
55  /* Calculate padding required below alignment boundary to
56  * ensure that a correctly aligned inline struct io_buffer
57  * could fit (regardless of the requested offset).
58  */
59  padding = ( sizeof ( *iobuf ) + __alignof__ ( *iobuf ) - 1 );
60 
61  /* Round up requested alignment to at least the size of the
62  * padding, to simplify subsequent calculations.
63  */
64  if ( align < padding )
65  align = padding;
66 
67  /* Round up alignment to the nearest power of two, avoiding
68  * a potentially undefined shift operation.
69  */
70  align_log2 = fls ( align - 1 );
71  if ( align_log2 >= ( 8 * sizeof ( align ) ) )
72  return NULL;
73  align = ( 1UL << align_log2 );
74 
75  /* Calculate length threshold */
76  assert ( align >= padding );
77  threshold = ( align - padding );
78 
79  /* Allocate buffer plus an inline descriptor as a single unit,
80  * unless doing so would push the total size over the
81  * alignment boundary.
82  */
83  if ( len <= threshold ) {
84 
85  /* Round up buffer length to ensure that struct
86  * io_buffer is aligned.
87  */
88  len += ( ( - len - offset ) & ( __alignof__ ( *iobuf ) - 1 ) );
89 
90  /* Allocate memory for buffer plus descriptor */
91  data = malloc_phys_offset ( len + sizeof ( *iobuf ), align,
92  offset );
93  if ( ! data )
94  return NULL;
95  iobuf = ( data + len );
96 
97  } else {
98 
99  /* Allocate memory for buffer */
101  if ( ! data )
102  return NULL;
103 
104  /* Allocate memory for descriptor */
105  iobuf = malloc ( sizeof ( *iobuf ) );
106  if ( ! iobuf ) {
107  free_phys ( data, len );
108  return NULL;
109  }
110  }
111 
112  /* Populate descriptor */
113  memset ( &iobuf->map, 0, sizeof ( iobuf->map ) );
114  iobuf->head = iobuf->data = iobuf->tail = data;
115  iobuf->end = ( data + len );
116 
117  return iobuf;
118 }
119 
120 /**
121  * Allocate I/O buffer
122  *
123  * @v len Required length of buffer
124  * @ret iobuf I/O buffer, or NULL if none available
125  *
126  * The I/O buffer will be physically aligned on its own size (rounded
127  * up to the nearest power of two).
128  */
129 struct io_buffer * alloc_iob ( size_t len ) {
130 
131  /* Pad to minimum length */
132  if ( len < IOB_ZLEN )
133  len = IOB_ZLEN;
134 
135  /* Align buffer on its own size to avoid potential problems
136  * with boundary-crossing DMA.
137  */
138  return alloc_iob_raw ( len, len, 0 );
139 }
140 
141 /**
142  * Free I/O buffer
143  *
144  * @v iobuf I/O buffer
145  */
146 void free_iob ( struct io_buffer *iobuf ) {
147  size_t len;
148 
149  /* Allow free_iob(NULL) to be valid */
150  if ( ! iobuf )
151  return;
152 
153  /* Sanity checks */
154  assert ( iobuf->head <= iobuf->data );
155  assert ( iobuf->data <= iobuf->tail );
156  assert ( iobuf->tail <= iobuf->end );
157  assert ( ! dma_mapped ( &iobuf->map ) );
158 
159  /* Free buffer */
160  len = ( iobuf->end - iobuf->head );
161  if ( iobuf->end == iobuf ) {
162 
163  /* Descriptor is inline */
164  free_phys ( iobuf->head, ( len + sizeof ( *iobuf ) ) );
165 
166  } else {
167 
168  /* Descriptor is detached */
169  free_phys ( iobuf->head, len );
170  free ( iobuf );
171  }
172 }
173 
174 /**
175  * Allocate and map I/O buffer for receive DMA
176  *
177  * @v len Length of I/O buffer
178  * @v dma DMA device
179  * @ret iobuf I/O buffer, or NULL on error
180  */
181 struct io_buffer * alloc_rx_iob ( size_t len, struct dma_device *dma ) {
182  struct io_buffer *iobuf;
183  int rc;
184 
185  /* Allocate I/O buffer */
186  iobuf = alloc_iob ( len );
187  if ( ! iobuf )
188  goto err_alloc;
189 
190  /* Map I/O buffer */
191  if ( ( rc = iob_map_rx ( iobuf, dma ) ) != 0 )
192  goto err_map;
193 
194  return iobuf;
195 
196  iob_unmap ( iobuf );
197  err_map:
198  free_iob ( iobuf );
199  err_alloc:
200  return NULL;
201 }
202 
203 /**
204  * Unmap and free I/O buffer for receive DMA
205  *
206  * @v iobuf I/O buffer
207  */
208 void free_rx_iob ( struct io_buffer *iobuf ) {
209 
210  /* Unmap I/O buffer */
211  iob_unmap ( iobuf );
212 
213  /* Free I/O buffer */
214  free_iob ( iobuf );
215 }
216 
217 /**
218  * Ensure I/O buffer has sufficient headroom
219  *
220  * @v iobuf I/O buffer
221  * @v len Required headroom
222  *
223  * This function currently only checks for the required headroom; it
224  * does not reallocate the I/O buffer if required. If we ever have a
225  * code path that requires this functionality, it's a fairly trivial
226  * change to make.
227  */
228 int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len ) {
229 
230  if ( iob_headroom ( iobuf ) >= len )
231  return 0;
232  return -ENOBUFS;
233 }
234 
235 /**
236  * Concatenate I/O buffers into a single buffer
237  *
238  * @v list List of I/O buffers
239  * @ret iobuf Concatenated I/O buffer, or NULL on allocation failure
240  *
241  * After a successful concatenation, the list will be empty.
242  */
243 struct io_buffer * iob_concatenate ( struct list_head *list ) {
244  struct io_buffer *iobuf;
245  struct io_buffer *tmp;
246  struct io_buffer *concatenated;
247  size_t len = 0;
248 
249  /* If the list contains only a single entry, avoid an
250  * unnecessary additional allocation.
251  */
252  if ( list_is_singular ( list ) ) {
253  iobuf = list_first_entry ( list, struct io_buffer, list );
254  INIT_LIST_HEAD ( list );
255  return iobuf;
256  }
257 
258  /* Calculate total length */
259  list_for_each_entry ( iobuf, list, list )
260  len += iob_len ( iobuf );
261 
262  /* Allocate new I/O buffer */
263  concatenated = alloc_iob_raw ( len, __alignof__ ( *iobuf ), 0 );
264  if ( ! concatenated )
265  return NULL;
266 
267  /* Move data to new I/O buffer */
268  list_for_each_entry_safe ( iobuf, tmp, list, list ) {
269  list_del ( &iobuf->list );
270  memcpy ( iob_put ( concatenated, iob_len ( iobuf ) ),
271  iobuf->data, iob_len ( iobuf ) );
272  free_iob ( iobuf );
273  }
274 
275  return concatenated;
276 }
277 
278 /**
279  * Split I/O buffer
280  *
281  * @v iobuf I/O buffer
282  * @v len Length to split into a new I/O buffer
283  * @ret split New I/O buffer, or NULL on allocation failure
284  *
285  * Split the first @c len bytes of the existing I/O buffer into a
286  * separate I/O buffer. The resulting buffers are likely to have no
287  * headroom or tailroom.
288  *
289  * If this call fails, then the original buffer will be unmodified.
290  */
291 struct io_buffer * iob_split ( struct io_buffer *iobuf, size_t len ) {
292  struct io_buffer *split;
293 
294  /* Sanity checks */
295  assert ( len <= iob_len ( iobuf ) );
296 
297  /* Allocate new I/O buffer */
298  split = alloc_iob ( len );
299  if ( ! split )
300  return NULL;
301 
302  /* Copy in data */
303  memcpy ( iob_put ( split, len ), iobuf->data, len );
304  iob_pull ( iobuf, len );
305  return split;
306 }
static __always_inline void struct dma_mapping size_t size_t align
Definition: dma.h:222
#define iob_pull(iobuf, len)
Definition: iobuf.h:102
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define iob_put(iobuf, len)
Definition: iobuf.h:120
struct dma_mapping map
DMA mapping.
Definition: iobuf.h:43
Error codes.
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
struct io_buffer * alloc_iob_raw(size_t len, size_t align, size_t offset)
Allocate I/O buffer with specified alignment and offset.
Definition: iobuf.c:48
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:129
A doubly-linked list entry (or list head)
Definition: list.h:18
Dynamic memory allocation.
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
void * tail
End of data.
Definition: iobuf.h:50
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static __always_inline int dma_mapped(struct dma_mapping *map)
Check if DMA unmapping is required.
Definition: dma.h:449
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:420
static __always_inline void iob_unmap(struct io_buffer *iobuf)
Unmap I/O buffer for DMA.
Definition: iobuf.h:275
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:447
struct io_buffer * iob_concatenate(struct list_head *list)
Concatenate I/O buffers into a single buffer.
Definition: iobuf.c:243
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
struct io_buffer * alloc_rx_iob(size_t len, struct dma_device *dma)
Allocate and map I/O buffer for receive DMA.
Definition: iobuf.c:181
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
uint8_t * tmp
Definition: entropy.h:156
void * end
End of the buffer.
Definition: iobuf.h:52
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
static size_t iob_headroom(struct io_buffer *iobuf)
Calculate available space at start of an I/O buffer.
Definition: iobuf.h:165
#define list_is_singular(list)
Test whether a list has just one entry.
Definition: list.h:149
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:40
static __always_inline int iob_map_rx(struct io_buffer *iobuf, struct dma_device *dma)
Map empty I/O buffer for receive DMA.
Definition: iobuf.h:252
uint32_t len
Length.
Definition: ena.h:14
void free_rx_iob(struct io_buffer *iobuf)
Unmap and free I/O buffer for receive DMA.
Definition: iobuf.c:208
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
void * head
Start of the buffer.
Definition: iobuf.h:46
struct io_buffer * iob_split(struct io_buffer *iobuf, size_t len)
Split I/O buffer.
Definition: iobuf.c:291
#define IOB_ZLEN
Minimum I/O buffer length.
Definition: iobuf.h:24
void * data
Start of data.
Definition: iobuf.h:48
static void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition: malloc.h:77
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
static __always_inline physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
Definition: dma.h:436
#define fls(x)
Find last (i.e.
Definition: strings.h:166
int iob_ensure_headroom(struct io_buffer *iobuf, size_t len)
Ensure I/O buffer has sufficient headroom.
Definition: iobuf.c:228
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static void *__malloc malloc_phys_offset(size_t size, size_t phys_align, size_t offset)
Allocate memory with specified physical alignment and offset.
Definition: malloc.h:44
String functions.
A DMA-capable device.
Definition: dma.h:47
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:33