3 * Copyright (c) 2003 David H. Hovemeyer <daveho@cs.umd.edu>
4 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
7 * This is free software. You are permitted to use,
8 * redistribute, and modify it as specified in the file "COPYING".
11 #ifndef GEEKOS_BLOCKDEV_H
12 #define GEEKOS_BLOCKDEV_H
14 #include <geekos/ktypes.h>
15 #include <geekos/kthread.h>
16 #include <geekos/list.h>
17 #include <geekos/fileio.h>
22 * Type of block device request.
25 BLOCK_READ, BLOCK_WRITE
29 * State of a block I/O request.
32 PENDING, COMPLETED, ERROR
38 * List of block I/O requests.
40 DEFINE_LIST(Block_Request_List, Block_Request);
43 * An I/O request for a block device.
45 struct Block_Request {
46 struct Block_Device *dev;
47 enum Request_Type type;
50 volatile enum Request_State state;
51 volatile int errorCode;
52 struct Thread_Queue waitQueue;
54 DEFINE_LINK(Block_Request_List, Block_Request);
57 IMPLEMENT_LIST(Block_Request_List, Block_Request);
60 struct Block_Device_Ops;
66 char name[BLOCKDEV_MAX_NAME_LEN];
67 struct Block_Device_Ops *ops;
71 struct Thread_Queue *waitQueue;
72 struct Block_Request_List *requestQueue;
74 DEFINE_LINK(Block_Device_List, Block_Device);
78 * Operations that may be requested on block devices.
80 struct Block_Device_Ops {
81 int (*Open)(struct Block_Device *dev);
82 int (*Close)(struct Block_Device *dev);
83 int (*Get_Num_Blocks)(struct Block_Device *dev);
87 * Low level block device API.
88 * Only block device drivers need to use these functions.
90 int Register_Block_Device(const char *name, struct Block_Device_Ops *ops,
91 int unit, void *driverData, struct Thread_Queue *waitQueue,
92 struct Block_Request_List *requestQueue);
93 int Open_Block_Device(const char *name, struct Block_Device **pDev);
94 int Close_Block_Device(struct Block_Device *dev);
95 struct Block_Request *Create_Request(struct Block_Device *dev, enum Request_Type type,
96 int blockNum, void *buf);
97 void Post_Request_And_Wait(struct Block_Request *request);
98 struct Block_Request *Dequeue_Request(struct Block_Request_List *requestQueue,
99 struct Thread_Queue *waitQueue);
100 void Notify_Request_Completion(struct Block_Request *request, enum Request_State state, int errorCode);
103 * High level block device API.
104 * For use by filesystem and disk paging code.
106 int Block_Read(struct Block_Device *dev, int blockNum, void *buf);
107 int Block_Write(struct Block_Device *dev, int blockNum, void *buf);
108 int Get_Num_Blocks(struct Block_Device *dev);
115 * Round offset up to nearest sector.
117 static __inline__ ulong_t Round_Up_To_Block(ulong_t offset)
119 return (offset % SECTOR_SIZE) == 0
121 : offset + (SECTOR_SIZE - (offset % SECTOR_SIZE));
125 * Round offset down to nearest sector.
127 static __inline__ ulong_t Round_Down_To_Block(ulong_t offset)
129 return (offset % SECTOR_SIZE) == 0
131 : offset - (offset % SECTOR_SIZE);