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