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