Browse Source

Add alloc_iob() and free_iob(). (Direct search-and-replace

equivalents for alloc_pkb() and free_pkb(), which will be retired in
due course).
tags/v0.9.3
Michael Brown 17 years ago
parent
commit
68dd826b86
2 changed files with 133 additions and 39 deletions
  1. 74
    0
      src/core/iobuf.c
  2. 59
    39
      src/include/gpxe/iobuf.h

+ 74
- 0
src/core/iobuf.c View 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <stdint.h>
20
+#include <gpxe/malloc.h>
21
+#include <gpxe/iobuf.h>
22
+
23
+/** @file
24
+ *
25
+ * I/O buffers
26
+ *
27
+ */
28
+
29
+/**
30
+ * Allocate I/O buffer
31
+ *
32
+ * @v len	Required length of buffer
33
+ * @ret iobuf	I/O buffer, or NULL if none available
34
+ *
35
+ * The I/O buffer will be physically aligned to a multiple of
36
+ * @c IOBUF_SIZE.
37
+ */
38
+struct io_buffer * alloc_iob ( size_t len ) {
39
+	struct io_buffer *iobuf = NULL;
40
+	void *data;
41
+
42
+	/* Pad to minimum length */
43
+	if ( len < IOB_ZLEN )
44
+		len = IOB_ZLEN;
45
+
46
+	/* Align buffer length */
47
+	len = ( len + __alignof__( *iobuf ) - 1 ) &
48
+		~( __alignof__( *iobuf ) - 1 );
49
+	
50
+	/* Allocate memory for buffer plus descriptor */
51
+	data = malloc_dma ( len + sizeof ( *iobuf ), IOB_ALIGN );
52
+	if ( ! data )
53
+		return NULL;
54
+
55
+	iobuf = ( struct io_buffer * ) ( data + len );
56
+	iobuf->head = iobuf->data = iobuf->tail = data;
57
+	iobuf->end = iobuf;
58
+	return iobuf;
59
+}
60
+
61
+/**
62
+ * Free I/O buffer
63
+ *
64
+ * @v iobuf	I/O buffer
65
+ */
66
+void free_iob ( struct io_buffer *iobuf ) {
67
+	if ( iobuf ) {
68
+		assert ( iobuf->head <= iobuf->data );
69
+		assert ( iobuf->data <= iobuf->tail );
70
+		assert ( iobuf->tail <= iobuf->end );
71
+		free_dma ( iobuf->head,
72
+			   ( iobuf->end - iobuf->head ) + sizeof ( *iobuf ) );
73
+	}
74
+}

+ 59
- 39
src/include/gpxe/iobuf.h View File

11
 #include <assert.h>
11
 #include <assert.h>
12
 #include <gpxe/list.h>
12
 #include <gpxe/list.h>
13
 
13
 
14
+/**
15
+ * I/O buffer alignment
16
+ *
17
+ * I/O buffers allocated via alloc_iob() are guaranteed to be
18
+ * physically aligned to this boundary.  Some cards cannot DMA across
19
+ * a 4kB boundary.  With a standard Ethernet MTU, aligning to a 2kB
20
+ * boundary is sufficient to guarantee no 4kB boundary crossings.  For
21
+ * a jumbo Ethernet MTU, a packet may be larger than 4kB anyway.
22
+ */
23
+#define IOB_ALIGN 2048
24
+
25
+/**
26
+ * Minimum I/O buffer length
27
+ *
28
+ * alloc_iob() will round up the allocated length to this size if
29
+ * necessary.  This is used on behalf of hardware that is not capable
30
+ * of auto-padding.
31
+ */
32
+#define IOB_ZLEN 64
33
+
14
 /**
34
 /**
15
  * A persistent I/O buffer
35
  * A persistent I/O buffer
16
  *
36
  *
40
 /**
60
 /**
41
  * Reserve space at start of I/O buffer
61
  * Reserve space at start of I/O buffer
42
  *
62
  *
43
- * @v iob	I/O buffer
63
+ * @v iobuf	I/O buffer
44
  * @v len	Length to reserve
64
  * @v len	Length to reserve
45
  * @ret data	Pointer to new start of buffer
65
  * @ret data	Pointer to new start of buffer
46
  */
66
  */
47
-static inline void * iob_reserve ( struct io_buffer *iob, size_t len ) {
48
-	iob->data += len;
49
-	iob->tail += len;
50
-	assert ( iob->tail <= iob->end );
51
-	return iob->data;
67
+static inline void * iob_reserve ( struct io_buffer *iobuf, size_t len ) {
68
+	iobuf->data += len;
69
+	iobuf->tail += len;
70
+	assert ( iobuf->tail <= iobuf->end );
71
+	return iobuf->data;
52
 }
72
 }
53
 
73
 
54
 /**
74
 /**
55
  * Add data to start of I/O buffer
75
  * Add data to start of I/O buffer
56
  *
76
  *
57
- * @v iob	I/O buffer
77
+ * @v iobuf	I/O buffer
58
  * @v len	Length to add
78
  * @v len	Length to add
59
  * @ret data	Pointer to new start of buffer
79
  * @ret data	Pointer to new start of buffer
60
  */
80
  */
61
-static inline void * iob_push ( struct io_buffer *iob, size_t len ) {
62
-	iob->data -= len;
63
-	assert ( iob->data >= iob->head );
64
-	return iob->data;
81
+static inline void * iob_push ( struct io_buffer *iobuf, size_t len ) {
82
+	iobuf->data -= len;
83
+	assert ( iobuf->data >= iobuf->head );
84
+	return iobuf->data;
65
 }
85
 }
66
 
86
 
67
 /**
87
 /**
68
  * Remove data from start of I/O buffer
88
  * Remove data from start of I/O buffer
69
  *
89
  *
70
- * @v iob	I/O buffer
90
+ * @v iobuf	I/O buffer
71
  * @v len	Length to remove
91
  * @v len	Length to remove
72
  * @ret data	Pointer to new start of buffer
92
  * @ret data	Pointer to new start of buffer
73
  */
93
  */
74
-static inline void * iob_pull ( struct io_buffer *iob, size_t len ) {
75
-	iob->data += len;
76
-	assert ( iob->data <= iob->tail );
77
-	return iob->data;
94
+static inline void * iob_pull ( struct io_buffer *iobuf, size_t len ) {
95
+	iobuf->data += len;
96
+	assert ( iobuf->data <= iobuf->tail );
97
+	return iobuf->data;
78
 }
98
 }
79
 
99
 
80
 /**
100
 /**
81
  * Add data to end of I/O buffer
101
  * Add data to end of I/O buffer
82
  *
102
  *
83
- * @v iob	I/O buffer
103
+ * @v iobuf	I/O buffer
84
  * @v len	Length to add
104
  * @v len	Length to add
85
  * @ret data	Pointer to newly added space
105
  * @ret data	Pointer to newly added space
86
  */
106
  */
87
-static inline void * iob_put ( struct io_buffer *iob, size_t len ) {
88
-	void *old_tail = iob->tail;
89
-	iob->tail += len;
90
-	assert ( iob->tail <= iob->end );
107
+static inline void * iob_put ( struct io_buffer *iobuf, size_t len ) {
108
+	void *old_tail = iobuf->tail;
109
+	iobuf->tail += len;
110
+	assert ( iobuf->tail <= iobuf->end );
91
 	return old_tail;
111
 	return old_tail;
92
 }
112
 }
93
 
113
 
94
 /**
114
 /**
95
  * Remove data from end of I/O buffer
115
  * Remove data from end of I/O buffer
96
  *
116
  *
97
- * @v iob	I/O buffer
117
+ * @v iobuf	I/O buffer
98
  * @v len	Length to remove
118
  * @v len	Length to remove
99
  */
119
  */
100
-static inline void iob_unput ( struct io_buffer *iob, size_t len ) {
101
-	iob->tail -= len;
102
-	assert ( iob->tail >= iob->data );
120
+static inline void iob_unput ( struct io_buffer *iobuf, size_t len ) {
121
+	iobuf->tail -= len;
122
+	assert ( iobuf->tail >= iobuf->data );
103
 }
123
 }
104
 
124
 
105
 /**
125
 /**
106
  * Empty an I/O buffer
126
  * Empty an I/O buffer
107
  *
127
  *
108
- * @v iob	I/O buffer
128
+ * @v iobuf	I/O buffer
109
  */
129
  */
110
-static inline void iob_empty ( struct io_buffer *iob ) {
111
-	iob->tail = iob->data;
130
+static inline void iob_empty ( struct io_buffer *iobuf ) {
131
+	iobuf->tail = iobuf->data;
112
 }
132
 }
113
 
133
 
114
 /**
134
 /**
115
  * Calculate length of data in an I/O buffer
135
  * Calculate length of data in an I/O buffer
116
  *
136
  *
117
- * @v iob	I/O buffer
137
+ * @v iobuf	I/O buffer
118
  * @ret len	Length of data in buffer
138
  * @ret len	Length of data in buffer
119
  */
139
  */
120
-static inline size_t iob_len ( struct io_buffer *iob ) {
121
-	return ( iob->tail - iob->data );
140
+static inline size_t iob_len ( struct io_buffer *iobuf ) {
141
+	return ( iobuf->tail - iobuf->data );
122
 }
142
 }
123
 
143
 
124
 /**
144
 /**
125
  * Calculate available space at start of an I/O buffer
145
  * Calculate available space at start of an I/O buffer
126
  *
146
  *
127
- * @v iob	I/O buffer
147
+ * @v iobuf	I/O buffer
128
  * @ret len	Length of data available at start of buffer
148
  * @ret len	Length of data available at start of buffer
129
  */
149
  */
130
-static inline size_t iob_headroom ( struct io_buffer *iob ) {
131
-	return ( iob->data - iob->head );
150
+static inline size_t iob_headroom ( struct io_buffer *iobuf ) {
151
+	return ( iobuf->data - iobuf->head );
132
 }
152
 }
133
 
153
 
134
 /**
154
 /**
135
  * Calculate available space at end of an I/O buffer
155
  * Calculate available space at end of an I/O buffer
136
  *
156
  *
137
- * @v iob	I/O buffer
157
+ * @v iobuf	I/O buffer
138
  * @ret len	Length of data available at end of buffer
158
  * @ret len	Length of data available at end of buffer
139
  */
159
  */
140
-static inline size_t iob_tailroom ( struct io_buffer *iob ) {
141
-	return ( iob->end - iob->tail );
160
+static inline size_t iob_tailroom ( struct io_buffer *iobuf ) {
161
+	return ( iobuf->end - iobuf->tail );
142
 }
162
 }
143
 
163
 
144
 extern struct io_buffer * alloc_iob ( size_t len );
164
 extern struct io_buffer * alloc_iob ( size_t len );
145
-extern void free_iob ( struct io_buffer *iob );
146
-extern void iob_pad ( struct io_buffer *iob, size_t min_len );
165
+extern void free_iob ( struct io_buffer *iobuf );
166
+extern void iob_pad ( struct io_buffer *iobuf, size_t min_len );
147
 
167
 
148
 #endif /* _GPXE_IOBUF_H */
168
 #endif /* _GPXE_IOBUF_H */

Loading…
Cancel
Save