forked from snu-csl/nvmevirt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zns_mgmt_send.c
285 lines (233 loc) · 7.48 KB
/
zns_mgmt_send.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
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
// SPDX-License-Identifier: GPL-2.0-only
#include "nvmev.h"
#include "ssd.h"
#include "zns_ftl.h"
static uint32_t __zmgmt_send_close_zone(struct zns_ftl *zns_ftl, uint64_t zid)
{
struct zone_descriptor *zone_descs = zns_ftl->zone_descs;
enum zone_state cur_state = zone_descs[zid].state;
uint32_t status = NVME_SC_SUCCESS;
switch (cur_state) {
case ZONE_STATE_OPENED_IMPL:
case ZONE_STATE_OPENED_EXPL:
change_zone_state(zns_ftl, zid, ZONE_STATE_CLOSED);
release_zone_resource(zns_ftl, OPEN_ZONE);
break;
case ZONE_STATE_CLOSED:
break;
default:
status = NVME_SC_ZNS_INVALID_TRANSITION;
break;
}
return status;
}
static uint32_t __zmgmt_send_finish_zone(struct zns_ftl *zns_ftl, uint64_t zid)
{
struct zone_descriptor *zone_descs = zns_ftl->zone_descs;
enum zone_state cur_state = zone_descs[zid].state;
bool is_zrwa_zone = zone_descs[zid].zrwav;
uint32_t status = NVME_SC_SUCCESS;
switch (cur_state) {
case ZONE_STATE_OPENED_IMPL:
case ZONE_STATE_OPENED_EXPL:
release_zone_resource(zns_ftl, OPEN_ZONE);
// fall through
case ZONE_STATE_CLOSED:
release_zone_resource(zns_ftl, ACTIVE_ZONE);
if (is_zrwa_zone)
release_zone_resource(zns_ftl, ZRWA_ZONE);
change_zone_state(zns_ftl, zid, ZONE_STATE_FULL);
break;
case ZONE_STATE_EMPTY:
change_zone_state(zns_ftl, zid, ZONE_STATE_FULL);
break;
case ZONE_STATE_FULL:
break;
default:
status = NVME_SC_ZNS_INVALID_TRANSITION;
break;
}
return status;
}
static uint32_t __zmgmt_send_open_zone(struct zns_ftl *zns_ftl, uint64_t zid, uint32_t zrwa)
{
struct zone_descriptor *zone_descs = zns_ftl->zone_descs;
enum zone_state cur_state = zone_descs[zid].state;
uint32_t status = NVME_SC_SUCCESS;
switch (cur_state) {
case ZONE_STATE_EMPTY:
if (is_zone_resource_full(zns_ftl, ACTIVE_ZONE))
return NVME_SC_ZNS_NO_ACTIVE_ZONE;
if (is_zone_resource_full(zns_ftl, OPEN_ZONE))
return NVME_SC_ZNS_NO_OPEN_ZONE;
if (zrwa) {
if (is_zone_resource_full(zns_ftl, ZRWA_ZONE))
return NVME_SC_ZNS_ZRWA_RSRC_UNAVAIL;
acquire_zone_resource(zns_ftl, ZRWA_ZONE);
zone_descs[zid].zrwav = 1;
}
acquire_zone_resource(zns_ftl, ACTIVE_ZONE);
// fall through
case ZONE_STATE_CLOSED:
if (acquire_zone_resource(zns_ftl, OPEN_ZONE) == false)
return NVME_SC_ZNS_NO_OPEN_ZONE;
// fall through
case ZONE_STATE_OPENED_IMPL:
change_zone_state(zns_ftl, zid, ZONE_STATE_OPENED_EXPL);
break;
case ZONE_STATE_OPENED_EXPL:
break;
default:
status = NVME_SC_ZNS_INVALID_TRANSITION;
break;
}
return status;
}
static void __reset_zone(struct zns_ftl *zns_ftl, uint64_t zid)
{
struct zone_descriptor *zone_descs = zns_ftl->zone_descs;
uint32_t zone_size = zns_ftl->zp.zone_size;
uint8_t *zone_start_addr = (uint8_t *)get_storage_addr_from_zid(zns_ftl, zid);
NVMEV_ZNS_DEBUG("%s ns %d zid %lu start addres 0x%llx zone_size %x \n", __FUNCTION__,
zns_ftl->ns, zid, (uint64_t)zone_start_addr, zone_size);
memset(zone_start_addr, 0, zone_size);
zone_descs[zid].wp = zone_descs[zid].zslba;
zone_descs[zid].zrwav = 0;
if (zns_ftl->zp.zrwa_buffer_size)
buffer_refill(&zns_ftl->zwra_buffer[zid]);
}
static uint32_t __zmgmt_send_reset_zone(struct zns_ftl *zns_ftl, uint64_t zid)
{
struct zone_descriptor *zone_descs = zns_ftl->zone_descs;
enum zone_state cur_state = zone_descs[zid].state;
bool is_zrwa_zone = zone_descs[zid].zrwav;
uint32_t status = NVME_SC_SUCCESS;
switch (cur_state) {
case ZONE_STATE_OPENED_IMPL:
case ZONE_STATE_OPENED_EXPL:
release_zone_resource(zns_ftl, OPEN_ZONE);
// fall through
case ZONE_STATE_CLOSED:
release_zone_resource(zns_ftl, ACTIVE_ZONE);
if (is_zrwa_zone)
release_zone_resource(zns_ftl, ZRWA_ZONE);
// fall through
case ZONE_STATE_FULL:
case ZONE_STATE_EMPTY:
change_zone_state(zns_ftl, zid, ZONE_STATE_EMPTY);
__reset_zone(zns_ftl, zid);
break;
default:
status = NVME_SC_ZNS_INVALID_TRANSITION;
break;
}
return status;
}
static uint32_t __zmgmt_send_offline_zone(struct zns_ftl *zns_ftl, uint64_t zid)
{
enum zone_state cur_state = zns_ftl->zone_descs[zid].state;
uint32_t status = NVME_SC_SUCCESS;
switch (cur_state) {
case ZONE_STATE_READ_ONLY:
change_zone_state(zns_ftl, zid, ZONE_STATE_OFFLINE);
break;
case ZONE_STATE_OFFLINE:
break;
default:
status = NVME_SC_ZNS_INVALID_TRANSITION;
break;
}
return status;
}
static uint32_t __zmgmt_send_flush_explicit_zrwa(struct zns_ftl *zns_ftl, uint64_t slba)
{
struct zone_descriptor *zone_descs = zns_ftl->zone_descs;
uint64_t zid = lba_to_zone(zns_ftl, slba);
uint64_t wp = zone_descs[zid].wp;
uint32_t status = NVME_SC_SUCCESS;
enum zone_state cur_state = zone_descs[zid].state;
uint64_t zone_capacity = zone_descs[zid].zone_capacity;
const uint32_t lbas_per_zrwafg = zns_ftl->zp.lbas_per_zrwafg;
const uint32_t lbas_per_zrwa = zns_ftl->zp.lbas_per_zrwa;
uint64_t zrwa_start = wp;
uint64_t zrwa_end = min(zrwa_start + lbas_per_zrwa - 1,
(size_t)zone_to_slba(zns_ftl, zid) + zone_capacity - 1);
uint64_t nr_lbas_flush = slba - wp + 1;
NVMEV_ZNS_DEBUG(
"%s slba 0x%llx zrwa_start 0x%llx zrwa_end 0x%llx zone_descs[zid].zrwav %d\n",
__FUNCTION__, slba, zrwa_start, zrwa_end, zone_descs[zid].zrwav);
if (zone_descs[zid].zrwav == 0)
return NVME_SC_ZNS_INVALID_ZONE_OPERATION;
if (!(slba >= zrwa_start && slba <= zrwa_end))
return NVME_SC_ZNS_INVALID_ZONE_OPERATION;
if ((nr_lbas_flush % lbas_per_zrwafg) != 0)
return NVME_SC_INVALID_FIELD;
switch (cur_state) {
case ZONE_STATE_OPENED_EXPL:
case ZONE_STATE_OPENED_IMPL:
case ZONE_STATE_CLOSED:
zone_descs[zid].wp = slba + 1;
if (zone_descs[zid].wp == (zone_to_slba(zns_ftl, zid) + zone_capacity)) {
//change state to ZSF
if (cur_state != ZONE_STATE_CLOSED)
release_zone_resource(zns_ftl, OPEN_ZONE);
release_zone_resource(zns_ftl, ACTIVE_ZONE);
release_zone_resource(zns_ftl, ZRWA_ZONE);
change_zone_state(zns_ftl, zid, ZONE_STATE_FULL);
}
break;
default:
status = NVME_SC_ZNS_INVALID_ZONE_OPERATION;
break;
}
return status;
}
static uint32_t __zmgmt_send(struct zns_ftl *zns_ftl, uint64_t slba, uint32_t action,
uint32_t option)
{
uint32_t status;
uint64_t zid = lba_to_zone(zns_ftl, slba);
switch (action) {
case ZSA_CLOSE_ZONE:
status = __zmgmt_send_close_zone(zns_ftl, zid);
break;
case ZSA_FINISH_ZONE:
status = __zmgmt_send_finish_zone(zns_ftl, zid);
break;
case ZSA_OPEN_ZONE:
status = __zmgmt_send_open_zone(zns_ftl, zid, option);
break;
case ZSA_RESET_ZONE:
status = __zmgmt_send_reset_zone(zns_ftl, zid);
break;
case ZSA_OFFLINE_ZONE:
status = __zmgmt_send_offline_zone(zns_ftl, zid);
break;
case ZSA_FLUSH_EXPL_ZRWA:
status = __zmgmt_send_flush_explicit_zrwa(zns_ftl, slba);
break;
}
return status;
}
void zns_zmgmt_send(struct nvmev_ns *ns, struct nvmev_request *req, struct nvmev_result *ret)
{
struct zns_ftl *zns_ftl = (struct zns_ftl *)ns->ftls;
struct nvme_zone_mgmt_send *cmd = (struct nvme_zone_mgmt_send *)req->cmd;
uint32_t select_all = cmd->select_all;
uint32_t status = NVME_SC_SUCCESS;
uint32_t action = cmd->zsa;
uint32_t option = cmd->zsaso;
uint64_t slba = cmd->slba;
uint64_t zid = lba_to_zone(zns_ftl, slba);
if (select_all) {
for (zid = 0; zid < zns_ftl->zp.nr_zones; zid++)
__zmgmt_send(zns_ftl, zone_to_slba(zns_ftl, zid), action, option);
} else {
status = __zmgmt_send(zns_ftl, slba, action, option);
}
NVMEV_ZNS_DEBUG("%s slba %llx zid %lu select_all %lu action %u status %lu option %lu\n",
__FUNCTION__, cmd->slba, zid, select_all, cmd->zsa, status, option);
ret->nsecs_target = req->nsecs_start; // no delay
ret->status = status;
return;
}