-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathzns_ftl.c
222 lines (179 loc) · 5.72 KB
/
zns_ftl.c
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
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/ktime.h>
#include <linux/sched/clock.h>
#include "nvmev.h"
#include "ssd.h"
#include "zns_ftl.h"
static void __init_descriptor(struct zns_ftl *zns_ftl)
{
struct zone_descriptor *zone_descs;
uint32_t zone_size = zns_ftl->zp.zone_size;
uint32_t nr_zones = zns_ftl->zp.nr_zones;
uint64_t zslba = 0;
uint32_t i = 0;
const uint32_t zrwa_buffer_size = zns_ftl->zp.zrwa_buffer_size;
const uint32_t zone_wb_size = zns_ftl->zp.zone_wb_size;
zns_ftl->zone_descs = kzalloc(sizeof(struct zone_descriptor) * nr_zones, GFP_KERNEL);
zns_ftl->report_buffer = kmalloc(
sizeof(struct zone_report) + sizeof(struct zone_descriptor) * nr_zones, GFP_KERNEL);
if (zrwa_buffer_size)
zns_ftl->zrwa_buffer = kmalloc(sizeof(struct buffer) * nr_zones, GFP_KERNEL);
if (zone_wb_size)
zns_ftl->zone_write_buffer = kmalloc(sizeof(struct buffer) * nr_zones, GFP_KERNEL);
zone_descs = zns_ftl->zone_descs;
for (i = 0; i < nr_zones; i++) {
zone_descs[i].state = ZONE_STATE_EMPTY;
zone_descs[i].type = ZONE_TYPE_SEQ_WRITE_REQUIRED;
zone_descs[i].zslba = zslba;
zone_descs[i].wp = zslba;
zslba += BYTE_TO_LBA(zone_size);
zone_descs[i].zone_capacity = BYTE_TO_LBA(zone_size);
if (zrwa_buffer_size)
buffer_init(&(zns_ftl->zrwa_buffer[i]), zrwa_buffer_size);
if (zone_wb_size)
buffer_init(&(zns_ftl->zone_write_buffer[i]), zone_wb_size);
NVMEV_ZNS_DEBUG("[%d] zslba 0x%llx zone capacity 0x%llx, wp 0x%llx\n", i,
zone_descs[i].zslba, zone_descs[i].zone_capacity, zone_descs[i].wp);
}
}
static void __remove_descriptor(struct zns_ftl *zns_ftl)
{
if (zns_ftl->zp.zrwa_buffer_size)
kfree(zns_ftl->zrwa_buffer);
if (zns_ftl->zp.zone_wb_size)
kfree(zns_ftl->zone_write_buffer);
kfree(zns_ftl->report_buffer);
kfree(zns_ftl->zone_descs);
}
static void __init_resource(struct zns_ftl *zns_ftl)
{
struct zone_resource_info *res_infos = zns_ftl->res_infos;
res_infos[ACTIVE_ZONE] = (struct zone_resource_info){
.total_cnt = zns_ftl->zp.nr_zones,
.acquired_cnt = 0,
};
res_infos[OPEN_ZONE] = (struct zone_resource_info){
.total_cnt = zns_ftl->zp.nr_zones,
.acquired_cnt = 0,
};
res_infos[ZRWA_ZONE] = (struct zone_resource_info){
.total_cnt = zns_ftl->zp.nr_zones,
.acquired_cnt = 0,
};
}
static void zns_init_params(struct znsparams *zpp, struct ssdparams *spp, uint64_t capacity)
{
*zpp = (struct znsparams){
.zone_size = ZONE_SIZE,
.nr_zones = capacity / ZONE_SIZE,
.dies_per_zone = DIES_PER_ZONE,
.nr_active_zones = capacity / ZONE_SIZE, // max
.nr_open_zones = capacity / ZONE_SIZE, // max
.nr_zrwa_zones = MAX_ZRWA_ZONES,
.zone_wb_size = ZONE_WB_SIZE,
.zrwa_size = ZRWA_SIZE,
.zrwafg_size = ZRWAFG_SIZE,
.zrwa_buffer_size = ZRWA_BUFFER_SIZE,
.lbas_per_zrwa = ZRWA_SIZE / spp->secsz,
.lbas_per_zrwafg = ZRWAFG_SIZE / spp->secsz,
};
NVMEV_ASSERT((capacity % zpp->zone_size) == 0);
/* It should be 4KB aligned, according to lpn size */
NVMEV_ASSERT((zpp->zone_size % spp->pgsz) == 0);
NVMEV_INFO("zone_size=%u(Byte),%u(MB), # zones=%d # die/zone=%d \n", zpp->zone_size,
BYTE_TO_MB(zpp->zone_size), zpp->nr_zones, zpp->dies_per_zone);
}
static void zns_init_ftl(struct zns_ftl *zns_ftl, struct znsparams *zpp, struct ssd *ssd,
void *mapped_addr)
{
*zns_ftl = (struct zns_ftl){
.zp = *zpp, /*copy znsparams*/
.ssd = ssd,
.storage_base_addr = mapped_addr,
};
__init_descriptor(zns_ftl);
__init_resource(zns_ftl);
}
void zns_init_namespace(struct nvmev_ns *ns, uint32_t id, uint64_t size, void *mapped_addr,
uint32_t cpu_nr_dispatcher)
{
struct ssd *ssd;
struct zns_ftl *zns_ftl;
struct ssdparams spp;
struct znsparams zpp;
const uint32_t nr_parts = 1; /* Not support multi partitions for zns*/
NVMEV_ASSERT(nr_parts == 1);
ssd = kmalloc(sizeof(struct ssd), GFP_KERNEL);
ssd_init_params(&spp, size, nr_parts);
ssd_init(ssd, &spp, cpu_nr_dispatcher);
zns_ftl = kmalloc(sizeof(struct zns_ftl) * nr_parts, GFP_KERNEL);
zns_init_params(&zpp, &spp, size);
zns_init_ftl(zns_ftl, &zpp, ssd, mapped_addr);
*ns = (struct nvmev_ns){
.id = id,
.csi = NVME_CSI_ZNS,
.nr_parts = nr_parts,
.ftls = (void *)zns_ftl,
.size = size,
.mapped = mapped_addr,
/*register io command handler*/
.proc_io_cmd = zns_proc_nvme_io_cmd,
};
return;
}
void zns_remove_namespace(struct nvmev_ns *ns)
{
struct zns_ftl *zns_ftl = (struct zns_ftl *)ns->ftls;
ssd_remove(zns_ftl->ssd);
__remove_descriptor(zns_ftl);
kfree(zns_ftl->ssd);
kfree(zns_ftl);
ns->ftls = NULL;
}
static void zns_flush(struct nvmev_ns *ns, struct nvmev_request *req, struct nvmev_result *ret)
{
uint64_t start, latest;
uint32_t i;
struct zns_ftl *zns_ftl = (struct zns_ftl *)ns->ftls;
start = local_clock();
latest = start;
for (i = 0; i < ns->nr_parts; i++) {
latest = max(latest, ssd_next_idle_time(zns_ftl[i].ssd));
}
NVMEV_DEBUG("%s latency=%llu\n", __func__, latest - start);
ret->status = NVME_SC_SUCCESS;
ret->nsecs_target = latest;
return;
}
bool zns_proc_nvme_io_cmd(struct nvmev_ns *ns, struct nvmev_request *req, struct nvmev_result *ret)
{
struct nvme_command *cmd = req->cmd;
NVMEV_ASSERT(ns->csi == NVME_CSI_ZNS);
/*still not support multi partitions ...*/
NVMEV_ASSERT(ns->nr_parts == 1);
switch (cmd->common.opcode) {
case nvme_cmd_write:
case nvme_cmd_zone_append:
if (!zns_write(ns, req, ret))
return false;
break;
case nvme_cmd_read:
if (!zns_read(ns, req, ret))
return false;
break;
case nvme_cmd_flush:
zns_flush(ns, req, ret);
break;
case nvme_cmd_zone_mgmt_send:
zns_zmgmt_send(ns, req, ret);
break;
case nvme_cmd_zone_mgmt_recv:
zns_zmgmt_recv(ns, req, ret);
break;
default:
NVMEV_ERROR("%s: unimplemented command: %s(%d)\n", __func__,
nvme_opcode_string(cmd->common.opcode), cmd->common.opcode);
break;
}
return true;
}