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