1 | /* |
---|
2 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include "tmacros.h" |
---|
20 | |
---|
21 | #include <errno.h> |
---|
22 | #include <string.h> |
---|
23 | |
---|
24 | #include <rtems/blkdev.h> |
---|
25 | #include <rtems/bdbuf.h> |
---|
26 | |
---|
27 | const char rtems_test_name[] = "BLOCK 14"; |
---|
28 | |
---|
29 | #define ACTION_COUNT 7 |
---|
30 | |
---|
31 | #define BLOCK_COUNT 6 |
---|
32 | |
---|
33 | typedef struct { |
---|
34 | rtems_blkdev_bnum block; |
---|
35 | rtems_status_code (*get)( |
---|
36 | rtems_disk_device *dd, |
---|
37 | rtems_blkdev_bnum block, |
---|
38 | rtems_bdbuf_buffer **bd_ptr |
---|
39 | ); |
---|
40 | rtems_status_code expected_get_status; |
---|
41 | rtems_status_code (*release)(rtems_bdbuf_buffer *bd); |
---|
42 | } test_action; |
---|
43 | |
---|
44 | static const test_action actions [ACTION_COUNT] = { |
---|
45 | { 0, rtems_bdbuf_read, RTEMS_SUCCESSFUL, rtems_bdbuf_release }, |
---|
46 | { 1, rtems_bdbuf_read, RTEMS_SUCCESSFUL, rtems_bdbuf_release }, |
---|
47 | { 2, rtems_bdbuf_read, RTEMS_SUCCESSFUL, rtems_bdbuf_release }, |
---|
48 | { 0, rtems_bdbuf_read, RTEMS_SUCCESSFUL, rtems_bdbuf_release }, |
---|
49 | { 4, rtems_bdbuf_get, RTEMS_SUCCESSFUL, rtems_bdbuf_sync }, |
---|
50 | { 5, rtems_bdbuf_read, RTEMS_IO_ERROR, rtems_bdbuf_release }, |
---|
51 | { 5, rtems_bdbuf_get, RTEMS_SUCCESSFUL, rtems_bdbuf_sync } |
---|
52 | }; |
---|
53 | |
---|
54 | #define STATS(a, b, c, d, e, f, g, h) \ |
---|
55 | { \ |
---|
56 | .read_hits = a, \ |
---|
57 | .read_misses = b, \ |
---|
58 | .read_ahead_transfers = c, \ |
---|
59 | .read_blocks = d, \ |
---|
60 | .read_errors = e, \ |
---|
61 | .write_transfers = f, \ |
---|
62 | .write_blocks = g, \ |
---|
63 | .write_errors = h \ |
---|
64 | } |
---|
65 | |
---|
66 | static const rtems_blkdev_stats expected_stats [ACTION_COUNT] = { |
---|
67 | STATS(0, 1, 0, 1, 0, 0, 0, 0), |
---|
68 | STATS(0, 2, 1, 3, 0, 0, 0, 0), |
---|
69 | STATS(1, 2, 2, 4, 0, 0, 0, 0), |
---|
70 | STATS(2, 2, 2, 4, 0, 0, 0, 0), |
---|
71 | STATS(2, 2, 2, 4, 0, 1, 1, 0), |
---|
72 | STATS(2, 3, 2, 5, 1, 1, 1, 0), |
---|
73 | STATS(2, 3, 2, 5, 1, 2, 2, 1) |
---|
74 | }; |
---|
75 | |
---|
76 | static const int expected_block_access_counts [ACTION_COUNT] [BLOCK_COUNT] = { |
---|
77 | { 1, 0, 0, 0, 0, 0 }, |
---|
78 | { 1, 1, 1, 0, 0, 0 }, |
---|
79 | { 1, 1, 1, 1, 0, 0 }, |
---|
80 | { 1, 1, 1, 1, 0, 0 }, |
---|
81 | { 1, 1, 1, 1, 1, 0 }, |
---|
82 | { 1, 1, 1, 1, 1, 1 }, |
---|
83 | { 1, 1, 1, 1, 1, 2 } |
---|
84 | }; |
---|
85 | |
---|
86 | static int block_access_counts [BLOCK_COUNT]; |
---|
87 | |
---|
88 | static int test_disk_ioctl(rtems_disk_device *dd, uint32_t req, void *arg) |
---|
89 | { |
---|
90 | int rv = 0; |
---|
91 | |
---|
92 | if (req == RTEMS_BLKIO_REQUEST) { |
---|
93 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
94 | rtems_blkdev_request *breq = arg; |
---|
95 | rtems_blkdev_sg_buffer *sg = breq->bufs; |
---|
96 | uint32_t i; |
---|
97 | |
---|
98 | for (i = 0; i < breq->bufnum; ++i) { |
---|
99 | rtems_blkdev_bnum block = sg [i].block; |
---|
100 | |
---|
101 | rtems_test_assert(block < BLOCK_COUNT); |
---|
102 | |
---|
103 | ++block_access_counts [block]; |
---|
104 | |
---|
105 | if (block == 5) { |
---|
106 | sc = RTEMS_IO_ERROR; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | rtems_blkdev_request_done(breq, sc); |
---|
111 | } else { |
---|
112 | errno = EINVAL; |
---|
113 | rv = -1; |
---|
114 | } |
---|
115 | |
---|
116 | return rv; |
---|
117 | } |
---|
118 | |
---|
119 | static void test_actions(rtems_disk_device *dd) |
---|
120 | { |
---|
121 | int i; |
---|
122 | |
---|
123 | for (i = 0; i < ACTION_COUNT; ++i) { |
---|
124 | const test_action *action = &actions [i]; |
---|
125 | rtems_status_code sc; |
---|
126 | rtems_bdbuf_buffer *bd; |
---|
127 | rtems_blkdev_stats stats; |
---|
128 | |
---|
129 | printf("action %i\n", i); |
---|
130 | |
---|
131 | sc = (*action->get)(dd, action->block, &bd); |
---|
132 | rtems_test_assert(sc == action->expected_get_status); |
---|
133 | |
---|
134 | if (sc == RTEMS_SUCCESSFUL) { |
---|
135 | sc = (*action->release)(bd); |
---|
136 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
137 | } |
---|
138 | |
---|
139 | rtems_test_assert( |
---|
140 | memcmp( |
---|
141 | block_access_counts, |
---|
142 | expected_block_access_counts [i], |
---|
143 | sizeof(block_access_counts) |
---|
144 | ) == 0 |
---|
145 | ); |
---|
146 | |
---|
147 | rtems_bdbuf_get_device_stats(dd, &stats); |
---|
148 | |
---|
149 | rtems_test_assert( |
---|
150 | memcmp( |
---|
151 | &stats, |
---|
152 | &expected_stats [i], |
---|
153 | sizeof(stats) |
---|
154 | ) == 0 |
---|
155 | ); |
---|
156 | } |
---|
157 | |
---|
158 | rtems_blkdev_print_stats(&dd->stats, rtems_printf_plugin, NULL); |
---|
159 | } |
---|
160 | |
---|
161 | static void test(void) |
---|
162 | { |
---|
163 | rtems_status_code sc; |
---|
164 | dev_t dev = 0; |
---|
165 | rtems_disk_device *dd; |
---|
166 | |
---|
167 | sc = rtems_disk_io_initialize(); |
---|
168 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
169 | |
---|
170 | sc = rtems_disk_create_phys( |
---|
171 | dev, |
---|
172 | 1, |
---|
173 | BLOCK_COUNT, |
---|
174 | test_disk_ioctl, |
---|
175 | NULL, |
---|
176 | NULL |
---|
177 | ); |
---|
178 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
179 | |
---|
180 | dd = rtems_disk_obtain(dev); |
---|
181 | rtems_test_assert(dd != NULL); |
---|
182 | |
---|
183 | test_actions(dd); |
---|
184 | |
---|
185 | sc = rtems_disk_release(dd); |
---|
186 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
187 | |
---|
188 | sc = rtems_disk_delete(dev); |
---|
189 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
190 | } |
---|
191 | |
---|
192 | static void Init(rtems_task_argument arg) |
---|
193 | { |
---|
194 | TEST_BEGIN(); |
---|
195 | |
---|
196 | test(); |
---|
197 | |
---|
198 | TEST_END(); |
---|
199 | |
---|
200 | rtems_test_exit(0); |
---|
201 | } |
---|
202 | |
---|
203 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
204 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
205 | #define CONFIGURE_APPLICATION_NEEDS_LIBBLOCK |
---|
206 | |
---|
207 | #define CONFIGURE_BDBUF_BUFFER_MIN_SIZE 1 |
---|
208 | #define CONFIGURE_BDBUF_BUFFER_MAX_SIZE 1 |
---|
209 | #define CONFIGURE_BDBUF_CACHE_MEMORY_SIZE BLOCK_COUNT |
---|
210 | #define CONFIGURE_BDBUF_MAX_READ_AHEAD_BLOCKS 1 |
---|
211 | #define CONFIGURE_BDBUF_READ_AHEAD_TASK_PRIORITY 1 |
---|
212 | |
---|
213 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
214 | |
---|
215 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
216 | |
---|
217 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
218 | |
---|
219 | #define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES |
---|
220 | #define CONFIGURE_INIT_TASK_PRIORITY 2 |
---|
221 | |
---|
222 | #define CONFIGURE_INIT |
---|
223 | |
---|
224 | #include <rtems/confdefs.h> |
---|