a869c8c2e1a0f759b52795b00246e60f6d428044
[lunaix-os.git] / lunaix-os / includes / lunaix / blkio.h
1 #ifndef __LUNAIX_BLKIO_H
2 #define __LUNAIX_BLKIO_H
3
4 #include <lunaix/buffer.h>
5 #include <lunaix/ds/llist.h>
6 #include <lunaix/ds/waitq.h>
7 #include <lunaix/types.h>
8
9 #define BLKIO_WRITE 0x1
10 #define BLKIO_ERROR 0x2
11
12 #define BLKIO_BUSY 0x4
13 #define BLKIO_PENDING 0x8
14
15 #define BLKIO_WAIT 0x1
16 #define BLKIO_NOASYNC 0x2
17
18 // Free on complete
19 #define BLKIO_FOC 0x10
20
21 #define BLKIO_SCHED_IDEL 0x1
22
23 struct blkio_req;
24
25 typedef void (*blkio_cb)(struct blkio_req*);
26 typedef void (*req_handler)(struct blkio_req*);
27
28 struct blkio_req
29 {
30     struct llist_header reqs;
31     struct blkio_context* io_ctx;
32     struct vecbuf* vbuf;
33     u32_t flags;
34     waitq_t wait;
35     u64_t blk_addr;
36     void* evt_args;
37     blkio_cb completed;
38     int errcode;
39 };
40
41 struct blkio_context
42 {
43     struct llist_header queue;
44     struct
45     {
46         u32_t seektime;
47         u32_t rotdelay;
48     } metrics;
49     req_handler handle_one;
50     u32_t state;
51     u32_t busy;
52     void* driver;
53 };
54
55 void
56 blkio_init();
57
58 /**
59  * @brief Vectorized read request
60  *
61  * @param vbuf
62  * @param start_lba
63  * @param completed
64  * @param evt_args
65  * @param options
66  * @return struct blkio_req*
67  */
68 struct blkio_req*
69 blkio_vrd(struct vecbuf* vbuf,
70           u64_t start_lba,
71           blkio_cb completed,
72           void* evt_args,
73           u32_t options);
74
75 /**
76  * @brief Vectorized write request
77  *
78  * @param vbuf
79  * @param start_lba
80  * @param completed
81  * @param evt_args
82  * @param options
83  * @return struct blkio_req*
84  */
85 struct blkio_req*
86 blkio_vwr(struct vecbuf* vbuf,
87           u64_t start_lba,
88           blkio_cb completed,
89           void* evt_args,
90           u32_t options);
91
92 void
93 blkio_free_req(struct blkio_req* req);
94
95 /**
96  * @brief Commit an IO request to scheduler.
97  *
98  * @param ctx
99  * @param req
100  */
101 void
102 blkio_commit(struct blkio_context* ctx, struct blkio_req* req, int options);
103
104 /**
105  * @brief Schedule an IO request to be handled.
106  *
107  * @param ctx
108  */
109 void
110 blkio_schedule(struct blkio_context* ctx);
111
112 /**
113  * @brief Notify the scheduler when request is completed, either successful or
114  * failed.
115  *
116  * @param ctx
117  * @param req
118  */
119 void
120 blkio_complete(struct blkio_req* req);
121
122 /**
123  * @brief Create a new block IO scheduling context
124  *
125  * @param handler Handler to handle request
126  * @return struct blkio_context*
127  */
128 struct blkio_context*
129 blkio_newctx(req_handler handler);
130
131 #endif /* __LUNAIX_BLKIO_H */