summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/block08/bdbuf_tests.h
blob: 626c60001a53940d83e646564b445cf6dbdb8e12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/* SPDX-License-Identifier: BSD-2-Clause */

/*! @file
 * @brief Common declarations of bdbuf tests.
 *
 * Copyright (C) 2010 OKTET Labs, St.-Petersburg, Russia
 * Author: Oleg Kravtsov <Oleg.Kravtsov@oktetlabs.ru>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef BDBUF_TESTS_H
#define BDBUF_TESTS_H

#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <rtems/bspIo.h>
#include <rtems/bdbuf.h>

#ifdef __cplusplus
extern "C" {
#endif

extern void run_bdbuf_tests(void);

extern void bdbuf_test1_1_main(void);
extern void bdbuf_test1_2_main(void);
extern void bdbuf_test1_3_main(void);
extern void bdbuf_test1_4_main(void);
extern void bdbuf_test1_5_main(void);

extern void bdbuf_test2_1_main(void);
extern void bdbuf_test2_2_main(void);
extern void bdbuf_test2_3_main(void);
extern void bdbuf_test2_4_main(void);
extern void bdbuf_test2_5_main(void);

extern void bdbuf_test3_1_main(void);
extern void bdbuf_test3_2_main(void);
extern void bdbuf_test3_3_main(void);

extern void bdbuf_test4_1_main(void);
extern void bdbuf_test4_2_main(void);
extern void bdbuf_test4_3_main(void);

extern rtems_status_code test_disk_initialize(void);

#define ARRAY_NUM(a_) (sizeof(a_) / sizeof(a_[0]))

/**
 * Configuration parameters of Test disk device.
 */
#define TEST_DISK_BLOCK_SIZE   512
#define TEST_DISK_BLOCK_NUM    1024
#define TEST_DISK_NAME         "/dev/testdisk"

/** Type of messages between test disk driver and main test task */
enum bdbuf_test_msg_type {
    /** Message sent by disk driver to main test task */
    BDBUF_TEST_MSG_TYPE_DRIVER_REQ,
    /**
     * Message sent main test task to disk driver
     * in reply to request.
     */
    BDBUF_TEST_MSG_TYPE_DRIVER_REPLY,
};

/**
 * Message used in communication between test disk driver
 * and main test task.
 * All R/W requests obtained by driver (from bdbuf library)
 * are directed to main test task. Then main test task
 * sends a reply after which test disk driver notifies
 * bdbuf library about operation complete event.
 */
typedef struct bdbuf_test_msg {
    /** Message type */
    enum bdbuf_test_msg_type type;

    union {
        struct driver_req {
            const rtems_disk_device *dd;
            uint32_t  req;
            void     *argp;
        } driver_req;
        struct driver_reply {
            int               ret_val;
            int               ret_errno;
            rtems_status_code res_status;
            int               res_errno;
        } driver_reply;
    } val;
} bdbuf_test_msg;


typedef enum bdbuf_rest_thread_prio {
    BDBUF_TEST_THREAD_PRIO_NORMAL = 3,
    BDBUF_TEST_THREAD_PRIO_LOW = 7,
    BDBUF_TEST_THREAD_PRIO_HIGH = 5,
} bdbuf_rest_thread_prio;


typedef struct test_ctx {
    /**
     * Message queue used by main task to get messages from
     * disk device driver.
     */
    Objects_Id test_qid;

    /**
     * Object ID for disk driver queue.
     * Test task will send messages to this queue in reply
     * to messages received on @a test_qid.
     */
    Objects_Id test_drv_qid;

    /** Test name */
    const char *test_name;

    /**
     * Semaphore used for synchronization between test thread
     * and main test task.
     * Main test task blocks on one of these semaphores and an auxiliary thread
     * releases it in order to pass control to main task again.
     */
    rtems_id   test_sync_main[3];

    /**
     * This semaphore is used for synchornization between main test task
     * and the END of auxiliary threads.
     */
    rtems_id   test_end_main;

    /**
     * Semaphore used for synchronization between test thread
     * and main test task.
     * Thread #N blocks on this semaphore and the main task releases it
     * when it wants to pass control to the auxiliary thread #N.
     */
    rtems_id   test_sync[3];

    /** Task Id values of auxiliary threads */
    Objects_Id test_task[3];
} test_ctx;

extern test_ctx g_test_ctx;

/** Device ID used for testing */
extern rtems_disk_device *test_dd;

/**
 * Create a message queue for test driver that is used for
 * receiving messages from test task.
 *
 * @param[out] id  RTEMS message queue identifier
 *
 * @return Status of the operation
 */
extern rtems_status_code bdbuf_test_create_drv_rx_queue(Objects_Id *id);

/**
 * Obtain a message queue for test driver that is used for
 * sending messages to test task.
 *
 * @param[out] id  RTEMS message queue identifier
 *
 * @return Status of the operation
 */
extern rtems_status_code bdbuf_test_create_drv_tx_queue(Objects_Id *id);

/**
 * Start a separate test task represented by function @p func
 * and associated with task index @p idx.
 *
 * @param[in] idx   Task index
 * @param[in] func  Test task function
 */
extern rtems_status_code bdbuf_test_start_thread(unsigned int idx,
                                                 rtems_task_entry func);

/**
 * Finalize test run.
 */
extern rtems_status_code bdbuf_test_end(void);

/** Test result variable */
extern bool       good_test_result;

#define BDBUF_TEST_BLOCK_TIMEOUT (2 * rtems_clock_get_ticks_per_second())

#define SET_THREAD_PRIORITY(t_num_, prio_) \
    do {                                                        \
        rtems_task_priority old_prio;                           \
        rtems_status_code   rc_;                                \
                                                                \
        rc_ = rtems_task_set_priority(                          \
                g_test_ctx.test_task[(t_num_) - 1],             \
                BDBUF_TEST_THREAD_PRIO_ ## prio_, &old_prio);   \
        if (rc_ != RTEMS_SUCCESSFUL)                            \
        {                                                       \
            printk("Failed to change priority of test thread #" \
                   #t_num_ " in test %s\n", g_test_ctx.test_name);         \
            return;                                             \
        }                                                       \
    } while (0)

/**
 * Start thread number @p t_num_ with @p func_ function
 * as an entry point of the thread.
 *
 * @param t_num_  thread number.
 * @param func_   thread entry point.
 */
#define START_THREAD(t_num_, func_) \
    do {                                                        \
        if (bdbuf_test_start_thread((t_num_) - 1, func_) !=     \
                RTEMS_SUCCESSFUL)                               \
        {                                                       \
            return;                                             \
        }                                                       \
        printk("Thread #" #t_num_ " started\n");                \
    } while (0)

/**
 * Wait for a message from device driver.
 *
 * @param[out] msg_  Pointer to a message data structure
 */
#define WAIT_DRV_MSG(msg_) \
    do {                                                        \
        rtems_status_code rc_;                                  \
        size_t            msg_size_ = sizeof(*(msg_));          \
                                                                \
        rc_ = rtems_message_queue_receive(g_test_ctx.test_qid,  \
                                          (msg_), &msg_size_,   \
                                          RTEMS_WAIT,           \
                                          RTEMS_NO_TIMEOUT);    \
        if (rc_ != RTEMS_SUCCESSFUL ||                          \
            msg_size_ != sizeof(*(msg_)))                       \
        {                                                       \
            printk("Error at %s:%d\n", __FILE__, __LINE__);     \
            return;                                             \
        }                                                       \
        if ((msg_)->type != BDBUF_TEST_MSG_TYPE_DRIVER_REQ)     \
        {                                                       \
            printk("Unexpected message received: %d\n",         \
                   (msg_)->type);                               \
            return;                                             \
        }                                                       \
    } while (0)

#define WAIT_DRV_MSG_WR(msg_) \
  do {                                                                     \
        WAIT_DRV_MSG(msg_);                                                \
        if ((msg_)->val.driver_req.req != RTEMS_BLKIO_REQUEST ||           \
            (msg_)->val.driver_req.dd != test_dd ||                        \
            ((rtems_blkdev_request *)                                      \
                 ((msg_)->val.driver_req.argp))->req !=                    \
                 RTEMS_BLKDEV_REQ_WRITE)                                   \
        {                                                                  \
            printk("Unexpected message received by disk driver: "          \
                   "req - 0x%" PRIx32 " (0x%lx), dev - %p (%p)\n", \
                   (msg_)->val.driver_req.req, RTEMS_BLKIO_REQUEST,        \
                   (msg_)->val.driver_req.dd, test_dd);                    \
            return;                                                        \
        }                                                                  \
    } while (0)

#define CHECK_NO_DRV_MSG() \
    do {                                                        \
        rtems_status_code rc_;                                  \
        bdbuf_test_msg    msg_;                                 \
        size_t            msg_size_ = sizeof(msg_);             \
                                                                \
        rc_ = rtems_message_queue_receive(g_test_ctx.test_qid,  \
                  (&msg_), &msg_size_,                          \
                  RTEMS_WAIT,                                   \
                  BDBUF_TEST_BLOCK_TIMEOUT);                    \
        if (rc_ != RTEMS_TIMEOUT)                               \
        {                                                       \
            printk("Error at %s:%d\n", __FILE__, __LINE__);     \
            return;                                             \
        }                                                       \
    } while (0)

extern bdbuf_test_msg test_drv_msg;

/**
 * Send a message to device driver (in order to complete
 * blocked I/O operation).
 *
 * @param ret_val_     Return value to use when returning from
 *                     ioctl() function.
 * @param ret_errno_   errno value to set to errno variable.
 * @param res_status_  In case return value from the ioctl()
 *                     function is equal to 0, this value
 *                     is used as status code in asynchronous
 *                     notification.
 * @param res_errno_   In case return value from the ioctl()
 *                     function is equal to 0, this value
 *                     is used as error value in asynchronous
 *                     notification.
 *
 */
#define SEND_DRV_MSG(ret_val_, ret_errno_, res_status_, res_errno_) \
    do {                                                                \
        rtems_status_code rc_;                                          \
                                                                        \
        memset(&test_drv_msg, 0, sizeof(test_drv_msg));                 \
        test_drv_msg.type = BDBUF_TEST_MSG_TYPE_DRIVER_REPLY;           \
        test_drv_msg.val.driver_reply.ret_val = (ret_val_);             \
        test_drv_msg.val.driver_reply.ret_errno = (ret_errno_);         \
        test_drv_msg.val.driver_reply.res_status = (res_status_);       \
        test_drv_msg.val.driver_reply.res_errno = (res_errno_);         \
                                                                        \
        rc_ = rtems_message_queue_send(g_test_ctx.test_drv_qid,         \
                                       &test_drv_msg,                   \
                                       sizeof(test_drv_msg));           \
        if (rc_ != RTEMS_SUCCESSFUL)                                    \
        {                                                               \
            printk("Error while sending a message to "                  \
                   "disk driver: %u\n", rc_);                           \
            return;                                                     \
        }                                                               \
    } while (0)

/**
 * Block main test task until a thread passes back control
 * with CONTINUE_MAIN().
 *
 * @param t_num_  thread number from which the main thread
 *                is waiting for a sync.
 */
#define WAIT_THREAD_SYNC(t_num_) \
    do {                                                                \
        rtems_status_code rc_;                                          \
                                                                        \
        rc_ = rtems_semaphore_obtain(                                   \
                g_test_ctx.test_sync_main[(t_num_) - 1],                \
                RTEMS_WAIT, RTEMS_NO_TIMEOUT);                          \
        if (rc_ != RTEMS_SUCCESSFUL)                                    \
        {                                                               \
            printk("Failed to get sync with a thread: %d\n", rc_);      \
            return;                                                     \
        }                                                               \
    } while (0)

/**
 * Check that a particular thread is blocked.
 *
 * @param t_num_  thread number that we want to check for blocking
 */
#define CHECK_THREAD_BLOCKED(t_num_) \
    do {                                                                \
        rtems_status_code rc_;                                          \
                                                                        \
        rc_ = rtems_semaphore_obtain(                                   \
                g_test_ctx.test_sync_main[(t_num_) - 1],                \
                RTEMS_WAIT, BDBUF_TEST_BLOCK_TIMEOUT);                  \
        if (rc_ != RTEMS_TIMEOUT)                                       \
        {                                                               \
            printk("Thread %d is not blocked at%s:%d\n",                \
                   t_num_, __FILE__, __LINE__);                         \
            return;                                                     \
        }                                                               \
    } while (0)

/**
 * Main test task gives control to the particular thread.
 *
 * @param t_num_  thread number to which main test task wants
 *                to give control.
 */
#define CONTINUE_THREAD(t_num_) \
    do {                                                        \
        rtems_status_code rc_;                                  \
                                                                \
        rc_ = rtems_semaphore_release(                          \
                g_test_ctx.test_sync[(t_num_) - 1]);            \
        if (rc_ != RTEMS_SUCCESSFUL)                            \
        {                                                       \
            printk("Failed to give control to thread #"         \
                   #t_num_ ": %d\n", rc_);                      \
            return;                                             \
        }                                                       \
    } while (0)

/**
 * Passes control back to the main test task from a thread.
 *
 * @param t_num_ Current thread number
 */
#define CONTINUE_MAIN(t_num_) \
    do {                                                        \
        rtems_status_code rc_;                                  \
                                                                \
        rc_ = rtems_semaphore_release(                          \
                g_test_ctx.test_sync_main[(t_num_) - 1]);       \
        if (rc_ != RTEMS_SUCCESSFUL)                            \
        {                                                       \
            printk("Failed to give control to "                 \
                   "main task: %d", rc_);                       \
            return;                                             \
        }                                                       \
        rc_ = rtems_semaphore_obtain(                           \
                g_test_ctx.test_sync[(t_num_) - 1],             \
                RTEMS_WAIT, RTEMS_NO_TIMEOUT);                  \
        if (rc_ != RTEMS_SUCCESSFUL)                            \
        {                                                       \
            printk("Failed to block on thread #"                \
                   #t_num_ ": %d\n", rc_);                      \
            return;                                             \
        }                                                       \
    } while (0)

#define WAIT_MAIN_SYNC(t_num_) \
    do {                                                        \
        rtems_status_code rc_;                                  \
                                                                \
        rc_ = rtems_semaphore_obtain(                           \
                g_test_ctx.test_sync[(t_num_) - 1],             \
                RTEMS_WAIT, RTEMS_NO_TIMEOUT);                  \
        if (rc_ != RTEMS_SUCCESSFUL)                            \
        {                                                       \
            printk("Failed to block on thread #"                \
                   #t_num_ ": %d\n", rc_);                      \
            return;                                             \
        }                                                       \
    } while (0)


#define TEST_START(test_name_) \
    do {                                \
        good_test_result = true;        \
        g_test_ctx.test_name = test_name_; \
        printk("%s - STARTED\n",        \
               g_test_ctx.test_name);              \
    } while (0)

#define TEST_CHECK_RESULT(step_) \
    do {                                        \
        if (!good_test_result)                  \
        {                                       \
            printk("TEST FAILED (Step %s)\n",   \
                   step_);                      \
            rtems_task_exit();                  \
        }                                       \
        else                                    \
        {                                       \
            printk("%s: Step %s - OK\n",        \
                   g_test_ctx.test_name, step_);           \
        }                                       \
    } while (0)

#define TEST_STOP() \
    do {                                 \
        bdbuf_test_end();                \
                                         \
        if (good_test_result)            \
            printk("TEST PASSED\n");     \
        else                             \
            printk("TEST FAILED (END)"); \
    } while (0)

#define THREAD_END() \
    do {                                                        \
        rtems_status_code rc_;                                  \
                                                                \
        rc_ = rtems_semaphore_release(g_test_ctx.test_end_main);\
        if (rc_ != RTEMS_SUCCESSFUL)                            \
        {                                                       \
            printk("Failed to give control to "                 \
                   "main task: %d", rc_);                       \
            return;                                             \
        }                                                       \
        rtems_task_exit();                                      \
    } while (0)

#define TEST_FAILED() \
    do {                                \
        good_test_result = false;       \
    } while (0)

#define SLEEP() \
    rtems_task_wake_after(BDBUF_TEST_BLOCK_TIMEOUT)


extern rtems_status_code
bdbuf_test_start_aux_task(rtems_name name,
                          rtems_task_entry entry_point,
                          rtems_task_argument arg,
                          Objects_Id *id);
#ifdef __cplusplus
}
#endif

#endif /* BDBUF_TESTS_H */