summaryrefslogtreecommitdiffstats
path: root/testsuites/libtests/block08/test_disk.c
blob: 27aea17d279f1201ea1fff47cafc11d7402b80b9 (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
/*! @file
 *  @brief Test disk block device implementation.
 *
 * Copyright (C) 2010 OKTET Labs, St.-Petersburg, Russia
 * Author: Oleg Kravtsov <Oleg.Kravtsov@oktetlabs.ru>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems.h>
#include <rtems/libio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include <rtems/blkdev.h>

#include "bdbuf_tests.h"

static Objects_Id drvq_id = OBJECTS_ID_NONE;
static Objects_Id testq_id = OBJECTS_ID_NONE;

static int
test_disk_ioctl(rtems_disk_device *dd, uint32_t req, void *argp)
{
    rtems_status_code     rc;
    bdbuf_test_msg        msg;
    size_t                msg_size;
    rtems_blkdev_request *r;

    switch (req)
    {
        case RTEMS_BLKIO_REQUEST:
        {
            rtems_blkdev_sg_buffer *sg;
            unsigned int            i;

            r = argp;

            printf("DISK_DRV: %s ",
                   r->req == RTEMS_BLKDEV_REQ_READ ? "R" :
                   r->req == RTEMS_BLKDEV_REQ_WRITE ? "W" : "?");
            for (i = 0, sg = r->bufs; i < r->bufnum; i++, sg++)
            {
                printf("[%" PRIu32 "] ", sg->block);
            }
            printf("\n");
            break;
        }

        default:
            return rtems_blkdev_ioctl (dd, req, argp);
    }

    memset(&msg, 0, sizeof(msg));
    msg.type = BDBUF_TEST_MSG_TYPE_DRIVER_REQ;
    msg.val.driver_req.dd = dd;
    msg.val.driver_req.req = req;
    msg.val.driver_req.argp = argp;

    rc = rtems_message_queue_send(testq_id, &msg, sizeof(msg));
    if (rc != RTEMS_SUCCESSFUL)
    {
        printf("Error while sending a message to Test task: %u\n", rc);
        rtems_blkdev_request_done(r, RTEMS_IO_ERROR);
        return 0;
    }

    /* Wait for a reply from the test task */
    msg_size = sizeof(msg);
    rc = rtems_message_queue_receive(drvq_id, &msg, &msg_size,
                                     RTEMS_WAIT, RTEMS_NO_TIMEOUT);
    if (rc != RTEMS_SUCCESSFUL)
    {
        printf("Error while reading a message from Test task: %u\n", rc);
        rtems_blkdev_request_done(r, RTEMS_IO_ERROR);
        return 0;
    }
    if (msg.type != BDBUF_TEST_MSG_TYPE_DRIVER_REPLY)
    {
        printf("Unexpected message comes to test disk driver: %d\n",
               msg.type);
        rtems_blkdev_request_done(r, RTEMS_IO_ERROR);
        return 0;
    }

    if (msg.val.driver_reply.ret_val != 0)
    {
        rtems_blkdev_request_done(r, RTEMS_IO_ERROR);
    }
    else
    {
        rtems_blkdev_request_done(r, msg.val.driver_reply.res_status);
    }

    return 0;
}

rtems_status_code
test_disk_initialize(void)
{
    rtems_status_code rc;

    rc = rtems_blkdev_create(TEST_DISK_NAME,
                             TEST_DISK_BLOCK_SIZE, TEST_DISK_BLOCK_NUM,
                             test_disk_ioctl,
                             NULL);
    if (rc != RTEMS_SUCCESSFUL)
    {
        printf("Failed to create %s disk\n", TEST_DISK_NAME);
        return rc;
    }

    rc = bdbuf_test_create_drv_rx_queue(&drvq_id);
    if (rc != RTEMS_SUCCESSFUL)
    {
        printf("%s() Failed to create Msg Queue for RX: %u\n",
               __FUNCTION__, rc);
        return rc;
    }

    rc = bdbuf_test_create_drv_tx_queue(&testq_id);
    if (rc != RTEMS_SUCCESSFUL)
    {
        printf("%s() Failed to get Msg Queue for TX: %u\n",
               __FUNCTION__, rc);
        return rc;
    }

    printf("TEST DISK - OK\n");
    return RTEMS_SUCCESSFUL;
}