-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssd_info.c
More file actions
407 lines (318 loc) · 11.7 KB
/
Copy pathssd_info.c
File metadata and controls
407 lines (318 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <linux/slab.h>
#include "uthash/utlist.h"
#include "ftl_algorithm_page_mapping.h"
#include "option.h"
#include "ssd_info.h"
enum {
IDLE = 0
};
struct flash_page {
enum fb_pg_status_t page_status[NR_LP_IN_PP];
u32 no_logical_page_addr[NR_LP_IN_PP];
u32 nr_invalid_log_pages;
int del_flag;
};
struct flash_chip {
struct completion chip_status_lock;
int chip_status;
// the number of free blocks in a chip
u32 nr_free_blocks;
u32 nr_used_blks;
u32 nr_dirt_blks;
struct flash_block *free_blks;
struct flash_block *used_blks;
struct flash_block *dirt_blks;
// the number of dirty blocks in a chip
u32 nr_dirty_blocks;
// block array
struct flash_block *list_blocks;
};
struct flash_bus {
// chip array
struct flash_chip *list_chips;
};
struct ssd_info {
// ssd information
u32 nr_buses;
u32 nr_chips_per_bus;
u32 nr_blocks_per_chip;
u32 nr_pages_per_block;
// bus array
struct flash_bus *list_buses;
};
void set_free_blk(struct ssd_info *ssdi, struct flash_block *bi) {
struct flash_chip *ci = get_chip_info(ssdi, bi->no_bus, bi->no_chip);
DL_APPEND(ci->free_blks, bi);
ci->nr_free_blocks++;
}
void reset_free_blk(struct ssd_info *ssdi, struct flash_block *bi) {
struct flash_chip *ci = get_chip_info(ssdi, bi->no_bus, bi->no_chip);
DL_DELETE(ci->free_blks, bi);
ci->nr_free_blocks--;
}
struct flash_block *get_free_block(struct ssd_info *ssdi, u32 bus, u32 chip) {
struct flash_chip *ci = get_chip_info(ssdi, bus, chip);
return ci->free_blks;
}
u32 get_nr_free_blks_in_chip(struct flash_chip *ci) {
return ci->nr_free_blocks;
}
void set_used_blk(struct ssd_info *ssdi, struct flash_block *bi) {
struct flash_chip *ci = get_chip_info(ssdi, bi->no_bus, bi->no_chip);
DL_APPEND(ci->used_blks, bi);
ci->nr_used_blks++;
}
void reset_used_blk(struct ssd_info *ssdi, struct flash_block *bi) {
struct flash_chip *ci = get_chip_info(ssdi, bi->no_bus, bi->no_chip);
DL_DELETE(ci->used_blks, bi);
ci->nr_used_blks--;
}
struct flash_block *get_used_block(struct ssd_info *ssdi, u32 bus, u32 chip) {
struct flash_chip *ci = get_chip_info(ssdi, bus, chip);
return ci->used_blks;
}
u32 get_nr_used_blks_in_chip(struct flash_chip *ci) { return ci->nr_used_blks; }
void set_dirt_blk(struct ssd_info *ssdi, struct flash_block *bi) {
struct flash_chip *ci = get_chip_info(ssdi, bi->no_bus, bi->no_chip);
DL_APPEND(ci->dirt_blks, bi);
ci->nr_dirt_blks++;
}
void reset_dirt_blk(struct ssd_info *ssdi, struct flash_block *bi) {
struct flash_chip *ci = get_chip_info(ssdi, bi->no_bus, bi->no_chip);
DL_DELETE(ci->dirt_blks, bi);
ci->nr_dirt_blks--;
}
struct flash_block *get_dirt_block(struct ssd_info *ssdi, u32 bus, u32 chip) {
struct flash_chip *ci = get_chip_info(ssdi, bus, chip);
return ci->dirt_blks;
}
u32 get_nr_dirt_blks_in_chip(struct flash_chip *ci) { return ci->nr_dirt_blks; }
struct ssd_info *create_ssd_info(void) {
struct ssd_info *ptr_ssd_info = kmalloc(sizeof(struct ssd_info), GFP_ATOMIC);
if (!ptr_ssd_info) {
printk(KERN_ERR
"flashbench: ssd_info: Allocating ssd information structure "
"failed.\n");
goto FAIL_ALLOC_SSD_INFO;
}
ptr_ssd_info->nr_buses = NUM_BUSES;
ptr_ssd_info->nr_chips_per_bus = NUM_CHIPS_PER_BUS;
ptr_ssd_info->nr_blocks_per_chip = NUM_BLOCKS_PER_CHIP;
ptr_ssd_info->nr_pages_per_block = NUM_PAGES_PER_BLOCK;
ptr_ssd_info->list_buses = kmalloc(sizeof(struct flash_bus) * NUM_BUSES, GFP_ATOMIC);
if (!ptr_ssd_info->list_buses) {
printk(KERN_ERR
"flashbench: ssd_info: Allocating bus information structure "
"failed.\n");
goto FAIL_ALLOC_BUS_INFO;
}
for (u32 loop_bus = 0; loop_bus < NUM_BUSES; loop_bus++) {
struct flash_bus *ptr_bus = &ptr_ssd_info->list_buses[loop_bus];
// initialize flash chip
ptr_bus->list_chips = kmalloc(sizeof(struct flash_chip) * NUM_CHIPS_PER_BUS, GFP_ATOMIC);
if (!ptr_bus->list_chips) {
printk(KERN_ERR
"flashbench: ssd_info: Allocating chip information structure "
"failed.\n");
goto FAIL_ALLOC_INFOS;
}
for (u32 loop_chip = 0; loop_chip < NUM_CHIPS_PER_BUS; loop_chip++) {
struct flash_chip *ptr_chip = &ptr_bus->list_chips[loop_chip];
init_completion(&ptr_chip->chip_status_lock);
complete(&ptr_chip->chip_status_lock);
ptr_chip->chip_status = IDLE;
ptr_chip->list_blocks = NULL;
ptr_chip->nr_free_blocks = 0;
ptr_chip->nr_used_blks = 0;
ptr_chip->nr_dirt_blks = 0;
ptr_chip->nr_dirty_blocks = 0;
ptr_chip->free_blks = NULL;
ptr_chip->used_blks = NULL;
ptr_chip->dirt_blks = NULL;
// initialize blocks
ptr_chip->list_blocks = kmalloc(sizeof(struct flash_block) * NUM_BLOCKS_PER_CHIP, GFP_ATOMIC);
if (!ptr_chip->list_blocks) {
printk(KERN_ERR
"flashbench: ssd_info: Allocating block information structure "
"failed.\n");
goto FAIL_ALLOC_INFOS;
}
for (u32 loop_block = 0; loop_block < NUM_BLOCKS_PER_CHIP; loop_block++) {
struct flash_block *ptr_block = &ptr_chip->list_blocks[loop_block];
ptr_block->list_pages = NULL;
ptr_block->no_block = loop_block;
ptr_block->no_chip = loop_chip;
ptr_block->no_bus = loop_bus;
ptr_block->nr_valid_log_pages = 0;
ptr_block->nr_invalid_log_pages = 0;
ptr_block->nr_valid_pages = 0;
ptr_block->nr_invalid_pages = 0;
ptr_block->nr_free_pages = NUM_PAGES_PER_BLOCK;
ptr_block->nr_erase_cnt = 0;
ptr_block->is_bad_block = false;
ptr_block->is_reserved_block = false;
ptr_block->is_active_block = false;
ptr_block->last_modified_time = 0;
ptr_block->del_flag = false;
// timestamp is set to 0 if the block is not used after initialization
ptr_block->list_pages = kmalloc(sizeof(struct flash_page) * NUM_PAGES_PER_BLOCK, GFP_ATOMIC);
if (!ptr_block->list_pages) {
printk(KERN_ERR
"flashbench: ssd_info: Allocating page information structure "
"failed.\n");
goto FAIL_ALLOC_INFOS;
}
for (u32 loop_page = 0; loop_page < NUM_PAGES_PER_BLOCK; loop_page++) {
struct flash_page *ptr_page = &ptr_block->list_pages[loop_page];
ptr_page->nr_invalid_log_pages = 0;
for (u8 lp_loop = 0; lp_loop < NR_LP_IN_PP; lp_loop++) {
ptr_page->no_logical_page_addr[lp_loop] = -1; // free page (by default)
ptr_page->page_status[lp_loop] = PAGE_STATUS_FREE; // free page (by default)
}
ptr_page->del_flag = false;
}
set_free_blk(ptr_ssd_info, ptr_block);
}
}
}
return ptr_ssd_info;
FAIL_ALLOC_INFOS:
if (ptr_ssd_info->list_buses) {
for (u32 loop_bus = 0; loop_bus < NUM_BUSES; loop_bus++) {
struct flash_bus *ptr_bus = &ptr_ssd_info->list_buses[loop_bus];
if (!ptr_bus->list_chips) { continue; }
for (u32 loop_chip = 0; loop_chip < NUM_CHIPS_PER_BUS; loop_chip++) {
struct flash_chip *ptr_chip = &ptr_bus->list_chips[loop_chip];
if (!ptr_chip->list_blocks) { continue; }
for (u32 loop_block = 0; loop_block < NUM_BLOCKS_PER_CHIP; loop_block++) {
struct flash_block *ptr_block = &ptr_chip->list_blocks[loop_block];
if (!ptr_block->list_pages) { continue; }
kfree(ptr_block->list_pages);
}
kfree(ptr_chip->list_blocks);
}
kfree(ptr_bus->list_chips);
}
kfree(ptr_ssd_info->list_buses);
}
FAIL_ALLOC_BUS_INFO:
if (ptr_ssd_info) { kfree(ptr_ssd_info); }
FAIL_ALLOC_SSD_INFO:
return NULL;
}
void destroy_ssd_info(struct ssd_info *ptr_ssd_info) {
if (ptr_ssd_info) {
if (ptr_ssd_info->list_buses) {
for (u32 loop_bus = 0; loop_bus < NUM_BUSES; loop_bus++) {
struct flash_bus *ptr_bus = &ptr_ssd_info->list_buses[loop_bus];
if (!ptr_bus->list_chips) { continue; }
for (u32 loop_chip = 0; loop_chip < NUM_CHIPS_PER_BUS; loop_chip++) {
struct flash_chip *ptr_chip = &ptr_bus->list_chips[loop_chip];
if (!ptr_chip->list_blocks) { continue; }
for (u32 loop_block = 0; loop_block < NUM_BLOCKS_PER_CHIP; loop_block++) {
struct flash_block *ptr_block = &ptr_chip->list_blocks[loop_block];
if (!ptr_block->list_pages) { continue; }
kfree(ptr_block->list_pages);
}
kfree(ptr_chip->list_blocks);
}
kfree(ptr_bus->list_chips);
}
kfree(ptr_ssd_info->list_buses);
}
kfree(ptr_ssd_info);
}
}
struct flash_chip *get_chip_info(struct ssd_info *ptr_ssd_info, u32 bus,
u32 chip) {
return &(ptr_ssd_info->list_buses[bus].list_chips[chip]);
}
struct flash_block *get_block_info(struct ssd_info *ptr_ssd_info, u32 bus,
u32 chip, u32 block) {
return &(ptr_ssd_info->list_buses[bus].list_chips[chip].list_blocks[block]);
}
struct flash_page *get_page_info(struct ssd_info *ptr_ssd_info, u32 bus,
u32 chip, u32 block, u32 page) {
return &(ptr_ssd_info->list_buses[bus]
.list_chips[chip]
.list_blocks[block]
.list_pages[page]);
}
u32 get_mapped_lpa(struct flash_page *pgi, u8 ofs) {
return pgi->no_logical_page_addr[ofs];
}
void set_mapped_lpa(struct flash_page *pgi, u8 ofs, u32 lpa) {
pgi->no_logical_page_addr[ofs] = lpa;
}
enum fb_pg_status_t get_pg_status(struct flash_page *pgi, u8 ofs) {
return pgi->page_status[ofs];
}
void set_pg_status(struct flash_page *pgi, u8 ofs, enum fb_pg_status_t status) {
pgi->page_status[ofs] = status;
}
u32 get_nr_invalid_lps(struct flash_page *pgi) {
return pgi->nr_invalid_log_pages;
}
u32 inc_nr_invalid_lps(struct flash_page *pgi) {
return ++(pgi->nr_invalid_log_pages);
}
void init_blk_inf(struct flash_block *blki) {
u8 lp;
u32 pg;
struct flash_page *pgi;
set_act_blk_flag(blki, false);
set_rsv_blk_flag(blki, false);
blki->nr_free_pages = NUM_PAGES_PER_BLOCK;
blki->nr_valid_pages = 0;
blki->nr_invalid_pages = 0;
blki->nr_valid_log_pages = 0;
blki->nr_invalid_log_pages = 0;
blki->del_flag = false;
for (pg = 0; pg < NUM_PAGES_PER_BLOCK; pg++) {
pgi = get_pgi_from_blki(blki, pg);
pgi->nr_invalid_log_pages = 0;
pgi->del_flag = false;
for (lp = 0; lp < NR_LP_IN_PP; lp++) {
set_mapped_lpa(pgi, lp, PAGE_UNMAPPED);
set_pg_status(pgi, lp, PAGE_STATUS_FREE);
}
}
}
struct flash_page *get_pgi_from_blki(struct flash_block *blki, u32 pg) {
return (blki->list_pages + pg);
}
u32 get_nr_free_pgs(struct flash_block *blki) { return blki->nr_free_pages; }
u32 inc_nr_valid_pgs(struct flash_block *blki) {
return ++(blki->nr_valid_pages);
}
u32 inc_nr_invalid_pgs(struct flash_block *blki) {
return ++(blki->nr_invalid_pages);
}
u32 dec_nr_valid_pgs(struct flash_block *blki) {
return --(blki->nr_valid_pages);
}
u32 dec_nr_free_pgs(struct flash_block *blki) {
return --(blki->nr_free_pages);
}
u32 get_nr_valid_lps_in_blk(struct flash_block *blki) {
return blki->nr_valid_log_pages;
}
u32 get_nr_invalid_lps_in_blk(struct flash_block *blki) {
return blki->nr_invalid_log_pages;
}
u32 inc_nr_valid_lps_in_blk(struct flash_block *blki) {
return ++(blki->nr_valid_log_pages);
}
u32 inc_nr_invalid_lps_in_blk(struct flash_block *blki) {
return ++(blki->nr_invalid_log_pages);
}
u32 dec_nr_valid_lps_in_blk(struct flash_block *blki) {
return --(blki->nr_valid_log_pages);
}
u32 inc_bers_cnt(struct flash_block *blki) { return ++(blki->nr_erase_cnt); }
void set_rsv_blk_flag(struct flash_block *blki, int flag) {
blki->is_reserved_block = flag;
}
void set_act_blk_flag(struct flash_block *blki, int flag) {
blki->is_active_block = flag;
}