Commit | Line | Data |
---|---|---|
a07b4970 CH |
1 | /* |
2 | * NVMe admin command implementation. | |
3 | * Copyright (c) 2015-2016 HGST, a Western Digital Company. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | */ | |
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
15 | #include <linux/module.h> | |
b2d09103 IM |
16 | #include <linux/rculist.h> |
17 | ||
a07b4970 | 18 | #include <generated/utsrelease.h> |
2d79c7dc | 19 | #include <asm/unaligned.h> |
a07b4970 CH |
20 | #include "nvmet.h" |
21 | ||
22 | u32 nvmet_get_log_page_len(struct nvme_command *cmd) | |
23 | { | |
24 | u32 len = le16_to_cpu(cmd->get_log_page.numdu); | |
25 | ||
26 | len <<= 16; | |
27 | len += le16_to_cpu(cmd->get_log_page.numdl); | |
28 | /* NUMD is a 0's based value */ | |
29 | len += 1; | |
30 | len *= sizeof(u32); | |
31 | ||
32 | return len; | |
33 | } | |
34 | ||
8ab0805f CH |
35 | static void nvmet_execute_get_log_page_noop(struct nvmet_req *req) |
36 | { | |
37 | nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->data_len)); | |
38 | } | |
39 | ||
2d79c7dc CK |
40 | static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req, |
41 | struct nvme_smart_log *slog) | |
42 | { | |
2d79c7dc CK |
43 | struct nvmet_ns *ns; |
44 | u64 host_reads, host_writes, data_units_read, data_units_written; | |
45 | ||
2d79c7dc CK |
46 | ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->get_log_page.nsid); |
47 | if (!ns) { | |
d93cb392 | 48 | pr_err("Could not find namespace id : %d\n", |
2d79c7dc | 49 | le32_to_cpu(req->cmd->get_log_page.nsid)); |
4185f25a | 50 | return NVME_SC_INVALID_NS; |
2d79c7dc CK |
51 | } |
52 | ||
d5eff33e CK |
53 | /* we don't have the right data for file backed ns */ |
54 | if (!ns->bdev) | |
55 | goto out; | |
56 | ||
2d79c7dc CK |
57 | host_reads = part_stat_read(ns->bdev->bd_part, ios[READ]); |
58 | data_units_read = part_stat_read(ns->bdev->bd_part, sectors[READ]); | |
59 | host_writes = part_stat_read(ns->bdev->bd_part, ios[WRITE]); | |
60 | data_units_written = part_stat_read(ns->bdev->bd_part, sectors[WRITE]); | |
61 | ||
62 | put_unaligned_le64(host_reads, &slog->host_reads[0]); | |
63 | put_unaligned_le64(data_units_read, &slog->data_units_read[0]); | |
64 | put_unaligned_le64(host_writes, &slog->host_writes[0]); | |
65 | put_unaligned_le64(data_units_written, &slog->data_units_written[0]); | |
d5eff33e | 66 | out: |
2d79c7dc | 67 | nvmet_put_namespace(ns); |
4185f25a SG |
68 | |
69 | return NVME_SC_SUCCESS; | |
2d79c7dc CK |
70 | } |
71 | ||
72 | static u16 nvmet_get_smart_log_all(struct nvmet_req *req, | |
73 | struct nvme_smart_log *slog) | |
74 | { | |
2d79c7dc CK |
75 | u64 host_reads = 0, host_writes = 0; |
76 | u64 data_units_read = 0, data_units_written = 0; | |
77 | struct nvmet_ns *ns; | |
78 | struct nvmet_ctrl *ctrl; | |
79 | ||
2d79c7dc CK |
80 | ctrl = req->sq->ctrl; |
81 | ||
82 | rcu_read_lock(); | |
83 | list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) { | |
d5eff33e CK |
84 | /* we don't have the right data for file backed ns */ |
85 | if (!ns->bdev) | |
86 | continue; | |
2d79c7dc CK |
87 | host_reads += part_stat_read(ns->bdev->bd_part, ios[READ]); |
88 | data_units_read += | |
89 | part_stat_read(ns->bdev->bd_part, sectors[READ]); | |
90 | host_writes += part_stat_read(ns->bdev->bd_part, ios[WRITE]); | |
91 | data_units_written += | |
92 | part_stat_read(ns->bdev->bd_part, sectors[WRITE]); | |
93 | ||
94 | } | |
95 | rcu_read_unlock(); | |
96 | ||
97 | put_unaligned_le64(host_reads, &slog->host_reads[0]); | |
98 | put_unaligned_le64(data_units_read, &slog->data_units_read[0]); | |
99 | put_unaligned_le64(host_writes, &slog->host_writes[0]); | |
100 | put_unaligned_le64(data_units_written, &slog->data_units_written[0]); | |
101 | ||
4185f25a | 102 | return NVME_SC_SUCCESS; |
2d79c7dc CK |
103 | } |
104 | ||
8ab0805f | 105 | static void nvmet_execute_get_log_page_smart(struct nvmet_req *req) |
a07b4970 | 106 | { |
8ab0805f CH |
107 | struct nvme_smart_log *log; |
108 | u16 status = NVME_SC_INTERNAL; | |
a07b4970 | 109 | |
8ab0805f | 110 | if (req->data_len != sizeof(*log)) |
a07b4970 | 111 | goto out; |
a07b4970 | 112 | |
8ab0805f CH |
113 | log = kzalloc(sizeof(*log), GFP_KERNEL); |
114 | if (!log) | |
115 | goto out; | |
a07b4970 | 116 | |
8ab0805f CH |
117 | if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL)) |
118 | status = nvmet_get_smart_log_all(req, log); | |
119 | else | |
120 | status = nvmet_get_smart_log_nsid(req, log); | |
121 | if (status) | |
c42d7a30 | 122 | goto out_free_log; |
a07b4970 | 123 | |
8ab0805f | 124 | status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log)); |
c42d7a30 CK |
125 | out_free_log: |
126 | kfree(log); | |
a07b4970 CH |
127 | out: |
128 | nvmet_req_complete(req, status); | |
129 | } | |
130 | ||
0866bf0c CK |
131 | static void nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req *req) |
132 | { | |
133 | u16 status = NVME_SC_INTERNAL; | |
134 | struct nvme_effects_log *log; | |
135 | ||
136 | log = kzalloc(sizeof(*log), GFP_KERNEL); | |
137 | if (!log) | |
138 | goto out; | |
139 | ||
140 | log->acs[nvme_admin_get_log_page] = cpu_to_le32(1 << 0); | |
141 | log->acs[nvme_admin_identify] = cpu_to_le32(1 << 0); | |
142 | log->acs[nvme_admin_abort_cmd] = cpu_to_le32(1 << 0); | |
143 | log->acs[nvme_admin_set_features] = cpu_to_le32(1 << 0); | |
144 | log->acs[nvme_admin_get_features] = cpu_to_le32(1 << 0); | |
145 | log->acs[nvme_admin_async_event] = cpu_to_le32(1 << 0); | |
146 | log->acs[nvme_admin_keep_alive] = cpu_to_le32(1 << 0); | |
147 | ||
148 | log->iocs[nvme_cmd_read] = cpu_to_le32(1 << 0); | |
149 | log->iocs[nvme_cmd_write] = cpu_to_le32(1 << 0); | |
150 | log->iocs[nvme_cmd_flush] = cpu_to_le32(1 << 0); | |
151 | log->iocs[nvme_cmd_dsm] = cpu_to_le32(1 << 0); | |
152 | log->iocs[nvme_cmd_write_zeroes] = cpu_to_le32(1 << 0); | |
153 | ||
154 | status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log)); | |
155 | ||
156 | kfree(log); | |
157 | out: | |
158 | nvmet_req_complete(req, status); | |
159 | } | |
160 | ||
c16734ea CH |
161 | static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req) |
162 | { | |
163 | struct nvmet_ctrl *ctrl = req->sq->ctrl; | |
164 | u16 status = NVME_SC_INTERNAL; | |
165 | size_t len; | |
166 | ||
167 | if (req->data_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32)) | |
168 | goto out; | |
169 | ||
170 | mutex_lock(&ctrl->lock); | |
171 | if (ctrl->nr_changed_ns == U32_MAX) | |
172 | len = sizeof(__le32); | |
173 | else | |
174 | len = ctrl->nr_changed_ns * sizeof(__le32); | |
175 | status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len); | |
176 | if (!status) | |
177 | status = nvmet_zero_sgl(req, len, req->data_len - len); | |
178 | ctrl->nr_changed_ns = 0; | |
7114ddeb | 179 | nvmet_clear_aen_bit(req, NVME_AEN_BIT_NS_ATTR); |
c16734ea CH |
180 | mutex_unlock(&ctrl->lock); |
181 | out: | |
182 | nvmet_req_complete(req, status); | |
183 | } | |
184 | ||
72efd25d CH |
185 | static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid, |
186 | struct nvme_ana_group_desc *desc) | |
187 | { | |
188 | struct nvmet_ctrl *ctrl = req->sq->ctrl; | |
189 | struct nvmet_ns *ns; | |
190 | u32 count = 0; | |
191 | ||
192 | if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) { | |
193 | rcu_read_lock(); | |
194 | list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) | |
195 | if (ns->anagrpid == grpid) | |
196 | desc->nsids[count++] = cpu_to_le32(ns->nsid); | |
197 | rcu_read_unlock(); | |
198 | } | |
199 | ||
200 | desc->grpid = cpu_to_le32(grpid); | |
201 | desc->nnsids = cpu_to_le32(count); | |
202 | desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt); | |
203 | desc->state = req->port->ana_state[grpid]; | |
204 | memset(desc->rsvd17, 0, sizeof(desc->rsvd17)); | |
205 | return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32); | |
206 | } | |
207 | ||
208 | static void nvmet_execute_get_log_page_ana(struct nvmet_req *req) | |
209 | { | |
210 | struct nvme_ana_rsp_hdr hdr = { 0, }; | |
211 | struct nvme_ana_group_desc *desc; | |
212 | size_t offset = sizeof(struct nvme_ana_rsp_hdr); /* start beyond hdr */ | |
213 | size_t len; | |
214 | u32 grpid; | |
215 | u16 ngrps = 0; | |
216 | u16 status; | |
217 | ||
218 | status = NVME_SC_INTERNAL; | |
219 | desc = kmalloc(sizeof(struct nvme_ana_group_desc) + | |
220 | NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL); | |
221 | if (!desc) | |
222 | goto out; | |
223 | ||
224 | down_read(&nvmet_ana_sem); | |
225 | for (grpid = 1; grpid <= NVMET_MAX_ANAGRPS; grpid++) { | |
226 | if (!nvmet_ana_group_enabled[grpid]) | |
227 | continue; | |
228 | len = nvmet_format_ana_group(req, grpid, desc); | |
229 | status = nvmet_copy_to_sgl(req, offset, desc, len); | |
230 | if (status) | |
231 | break; | |
232 | offset += len; | |
233 | ngrps++; | |
234 | } | |
be1277f5 HR |
235 | for ( ; grpid <= NVMET_MAX_ANAGRPS; grpid++) { |
236 | if (nvmet_ana_group_enabled[grpid]) | |
237 | ngrps++; | |
238 | } | |
72efd25d CH |
239 | |
240 | hdr.chgcnt = cpu_to_le64(nvmet_ana_chgcnt); | |
241 | hdr.ngrps = cpu_to_le16(ngrps); | |
7114ddeb | 242 | nvmet_clear_aen_bit(req, NVME_AEN_BIT_ANA_CHANGE); |
72efd25d CH |
243 | up_read(&nvmet_ana_sem); |
244 | ||
245 | kfree(desc); | |
246 | ||
247 | /* copy the header last once we know the number of groups */ | |
248 | status = nvmet_copy_to_sgl(req, 0, &hdr, sizeof(hdr)); | |
249 | out: | |
250 | nvmet_req_complete(req, status); | |
251 | } | |
252 | ||
a07b4970 CH |
253 | static void nvmet_execute_identify_ctrl(struct nvmet_req *req) |
254 | { | |
255 | struct nvmet_ctrl *ctrl = req->sq->ctrl; | |
256 | struct nvme_id_ctrl *id; | |
a07b4970 | 257 | u16 status = 0; |
42de82a8 | 258 | const char model[] = "Linux"; |
a07b4970 CH |
259 | |
260 | id = kzalloc(sizeof(*id), GFP_KERNEL); | |
261 | if (!id) { | |
262 | status = NVME_SC_INTERNAL; | |
263 | goto out; | |
264 | } | |
265 | ||
266 | /* XXX: figure out how to assign real vendors IDs. */ | |
267 | id->vid = 0; | |
268 | id->ssvid = 0; | |
269 | ||
c7399698 | 270 | memset(id->sn, ' ', sizeof(id->sn)); |
42de82a8 MW |
271 | bin2hex(id->sn, &ctrl->subsys->serial, |
272 | min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2)); | |
17c39d05 MW |
273 | memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' '); |
274 | memcpy_and_pad(id->fr, sizeof(id->fr), | |
275 | UTS_RELEASE, strlen(UTS_RELEASE), ' '); | |
a07b4970 | 276 | |
a07b4970 CH |
277 | id->rab = 6; |
278 | ||
279 | /* | |
280 | * XXX: figure out how we can assign a IEEE OUI, but until then | |
281 | * the safest is to leave it as zeroes. | |
282 | */ | |
283 | ||
72efd25d CH |
284 | /* we support multiple ports, multiples hosts and ANA: */ |
285 | id->cmic = (1 << 0) | (1 << 1) | (1 << 3); | |
a07b4970 CH |
286 | |
287 | /* no limit on data transfer sizes for now */ | |
288 | id->mdts = 0; | |
289 | id->cntlid = cpu_to_le16(ctrl->cntlid); | |
290 | id->ver = cpu_to_le32(ctrl->subsys->ver); | |
291 | ||
292 | /* XXX: figure out what to do about RTD3R/RTD3 */ | |
c86b8f7b | 293 | id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL); |
c09305ae SG |
294 | id->ctratt = cpu_to_le32(NVME_CTRL_ATTR_HID_128_BIT | |
295 | NVME_CTRL_ATTR_TBKAS); | |
a07b4970 CH |
296 | |
297 | id->oacs = 0; | |
298 | ||
299 | /* | |
300 | * We don't really have a practical limit on the number of abort | |
301 | * comands. But we don't do anything useful for abort either, so | |
302 | * no point in allowing more abort commands than the spec requires. | |
303 | */ | |
304 | id->acl = 3; | |
305 | ||
306 | id->aerl = NVMET_ASYNC_EVENTS - 1; | |
307 | ||
308 | /* first slot is read-only, only one slot supported */ | |
309 | id->frmw = (1 << 0) | (1 << 1); | |
0866bf0c | 310 | id->lpa = (1 << 0) | (1 << 1) | (1 << 2); |
a07b4970 CH |
311 | id->elpe = NVMET_ERROR_LOG_SLOTS - 1; |
312 | id->npss = 0; | |
313 | ||
314 | /* We support keep-alive timeout in granularity of seconds */ | |
315 | id->kas = cpu_to_le16(NVMET_KAS); | |
316 | ||
317 | id->sqes = (0x6 << 4) | 0x6; | |
318 | id->cqes = (0x4 << 4) | 0x4; | |
319 | ||
320 | /* no enforcement soft-limit for maxcmd - pick arbitrary high value */ | |
321 | id->maxcmd = cpu_to_le16(NVMET_MAX_CMD); | |
322 | ||
323 | id->nn = cpu_to_le32(ctrl->subsys->max_nsid); | |
793c7cfc | 324 | id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES); |
d2629209 CK |
325 | id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM | |
326 | NVME_CTRL_ONCS_WRITE_ZEROES); | |
a07b4970 CH |
327 | |
328 | /* XXX: don't report vwc if the underlying device is write through */ | |
329 | id->vwc = NVME_CTRL_VWC_PRESENT; | |
330 | ||
331 | /* | |
332 | * We can't support atomic writes bigger than a LBA without support | |
333 | * from the backend device. | |
334 | */ | |
335 | id->awun = 0; | |
336 | id->awupf = 0; | |
337 | ||
338 | id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */ | |
339 | if (ctrl->ops->has_keyed_sgls) | |
340 | id->sgls |= cpu_to_le32(1 << 2); | |
0d5ee2b2 | 341 | if (req->port->inline_data_size) |
a07b4970 CH |
342 | id->sgls |= cpu_to_le32(1 << 20); |
343 | ||
5eadc9cc | 344 | strlcpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn)); |
a07b4970 CH |
345 | |
346 | /* Max command capsule size is sqe + single page of in-capsule data */ | |
347 | id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) + | |
0d5ee2b2 | 348 | req->port->inline_data_size) / 16); |
a07b4970 CH |
349 | /* Max response capsule size is cqe */ |
350 | id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16); | |
351 | ||
352 | id->msdbd = ctrl->ops->msdbd; | |
353 | ||
72efd25d CH |
354 | id->anacap = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4); |
355 | id->anatt = 10; /* random value */ | |
356 | id->anagrpmax = cpu_to_le32(NVMET_MAX_ANAGRPS); | |
357 | id->nanagrpid = cpu_to_le32(NVMET_MAX_ANAGRPS); | |
358 | ||
a07b4970 CH |
359 | /* |
360 | * Meh, we don't really support any power state. Fake up the same | |
361 | * values that qemu does. | |
362 | */ | |
363 | id->psd[0].max_power = cpu_to_le16(0x9c4); | |
364 | id->psd[0].entry_lat = cpu_to_le32(0x10); | |
365 | id->psd[0].exit_lat = cpu_to_le32(0x4); | |
366 | ||
dedf0be5 CK |
367 | id->nwpc = 1 << 0; /* write protect and no write protect */ |
368 | ||
a07b4970 CH |
369 | status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); |
370 | ||
371 | kfree(id); | |
372 | out: | |
373 | nvmet_req_complete(req, status); | |
374 | } | |
375 | ||
376 | static void nvmet_execute_identify_ns(struct nvmet_req *req) | |
377 | { | |
378 | struct nvmet_ns *ns; | |
379 | struct nvme_id_ns *id; | |
380 | u16 status = 0; | |
381 | ||
f39ae471 | 382 | if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) { |
a07b4970 CH |
383 | status = NVME_SC_INVALID_NS | NVME_SC_DNR; |
384 | goto out; | |
385 | } | |
386 | ||
387 | id = kzalloc(sizeof(*id), GFP_KERNEL); | |
388 | if (!id) { | |
389 | status = NVME_SC_INTERNAL; | |
f39ae471 | 390 | goto out; |
a07b4970 CH |
391 | } |
392 | ||
f39ae471 CH |
393 | /* return an all zeroed buffer if we can't find an active namespace */ |
394 | ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid); | |
395 | if (!ns) | |
396 | goto done; | |
397 | ||
a07b4970 | 398 | /* |
18c53e40 | 399 | * nuse = ncap = nsze isn't always true, but we have no way to find |
a07b4970 CH |
400 | * that out from the underlying device. |
401 | */ | |
72efd25d CH |
402 | id->ncap = id->nsze = cpu_to_le64(ns->size >> ns->blksize_shift); |
403 | switch (req->port->ana_state[ns->anagrpid]) { | |
404 | case NVME_ANA_INACCESSIBLE: | |
405 | case NVME_ANA_PERSISTENT_LOSS: | |
406 | break; | |
407 | default: | |
408 | id->nuse = id->nsze; | |
409 | break; | |
410 | } | |
a07b4970 CH |
411 | |
412 | /* | |
413 | * We just provide a single LBA format that matches what the | |
414 | * underlying device reports. | |
415 | */ | |
416 | id->nlbaf = 0; | |
417 | id->flbas = 0; | |
418 | ||
419 | /* | |
420 | * Our namespace might always be shared. Not just with other | |
421 | * controllers, but also with any other user of the block device. | |
422 | */ | |
423 | id->nmic = (1 << 0); | |
72efd25d | 424 | id->anagrpid = cpu_to_le32(ns->anagrpid); |
a07b4970 | 425 | |
1b0d2745 | 426 | memcpy(&id->nguid, &ns->nguid, sizeof(id->nguid)); |
a07b4970 CH |
427 | |
428 | id->lbaf[0].ds = ns->blksize_shift; | |
429 | ||
dedf0be5 CK |
430 | if (ns->readonly) |
431 | id->nsattr |= (1 << 0); | |
f39ae471 CH |
432 | nvmet_put_namespace(ns); |
433 | done: | |
a07b4970 | 434 | status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); |
a07b4970 | 435 | kfree(id); |
a07b4970 CH |
436 | out: |
437 | nvmet_req_complete(req, status); | |
438 | } | |
439 | ||
440 | static void nvmet_execute_identify_nslist(struct nvmet_req *req) | |
441 | { | |
0add5e8e | 442 | static const int buf_size = NVME_IDENTIFY_DATA_SIZE; |
a07b4970 CH |
443 | struct nvmet_ctrl *ctrl = req->sq->ctrl; |
444 | struct nvmet_ns *ns; | |
445 | u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid); | |
446 | __le32 *list; | |
447 | u16 status = 0; | |
448 | int i = 0; | |
449 | ||
450 | list = kzalloc(buf_size, GFP_KERNEL); | |
451 | if (!list) { | |
452 | status = NVME_SC_INTERNAL; | |
453 | goto out; | |
454 | } | |
455 | ||
456 | rcu_read_lock(); | |
457 | list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) { | |
458 | if (ns->nsid <= min_nsid) | |
459 | continue; | |
460 | list[i++] = cpu_to_le32(ns->nsid); | |
461 | if (i == buf_size / sizeof(__le32)) | |
462 | break; | |
463 | } | |
464 | rcu_read_unlock(); | |
465 | ||
466 | status = nvmet_copy_to_sgl(req, 0, list, buf_size); | |
467 | ||
468 | kfree(list); | |
469 | out: | |
470 | nvmet_req_complete(req, status); | |
471 | } | |
472 | ||
637dc0f3 JT |
473 | static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len, |
474 | void *id, off_t *off) | |
475 | { | |
476 | struct nvme_ns_id_desc desc = { | |
477 | .nidt = type, | |
478 | .nidl = len, | |
479 | }; | |
480 | u16 status; | |
481 | ||
482 | status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc)); | |
483 | if (status) | |
484 | return status; | |
485 | *off += sizeof(desc); | |
486 | ||
487 | status = nvmet_copy_to_sgl(req, *off, id, len); | |
488 | if (status) | |
489 | return status; | |
490 | *off += len; | |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
495 | static void nvmet_execute_identify_desclist(struct nvmet_req *req) | |
496 | { | |
497 | struct nvmet_ns *ns; | |
498 | u16 status = 0; | |
499 | off_t off = 0; | |
500 | ||
501 | ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid); | |
502 | if (!ns) { | |
503 | status = NVME_SC_INVALID_NS | NVME_SC_DNR; | |
504 | goto out; | |
505 | } | |
506 | ||
507 | if (memchr_inv(&ns->uuid, 0, sizeof(ns->uuid))) { | |
508 | status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID, | |
509 | NVME_NIDT_UUID_LEN, | |
510 | &ns->uuid, &off); | |
511 | if (status) | |
512 | goto out_put_ns; | |
513 | } | |
514 | if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid))) { | |
515 | status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID, | |
516 | NVME_NIDT_NGUID_LEN, | |
517 | &ns->nguid, &off); | |
518 | if (status) | |
519 | goto out_put_ns; | |
520 | } | |
521 | ||
522 | if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off, | |
523 | off) != NVME_IDENTIFY_DATA_SIZE - off) | |
524 | status = NVME_SC_INTERNAL | NVME_SC_DNR; | |
525 | out_put_ns: | |
526 | nvmet_put_namespace(ns); | |
527 | out: | |
528 | nvmet_req_complete(req, status); | |
529 | } | |
530 | ||
a07b4970 | 531 | /* |
18c53e40 | 532 | * A "minimum viable" abort implementation: the command is mandatory in the |
a07b4970 CH |
533 | * spec, but we are not required to do any useful work. We couldn't really |
534 | * do a useful abort, so don't bother even with waiting for the command | |
535 | * to be exectuted and return immediately telling the command to abort | |
536 | * wasn't found. | |
537 | */ | |
538 | static void nvmet_execute_abort(struct nvmet_req *req) | |
539 | { | |
540 | nvmet_set_result(req, 1); | |
541 | nvmet_req_complete(req, 0); | |
542 | } | |
543 | ||
dedf0be5 CK |
544 | static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req) |
545 | { | |
546 | u16 status; | |
547 | ||
548 | if (req->ns->file) | |
549 | status = nvmet_file_flush(req); | |
550 | else | |
551 | status = nvmet_bdev_flush(req); | |
552 | ||
553 | if (status) | |
554 | pr_err("write protect flush failed nsid: %u\n", req->ns->nsid); | |
555 | return status; | |
556 | } | |
557 | ||
558 | static u16 nvmet_set_feat_write_protect(struct nvmet_req *req) | |
559 | { | |
b7c8f366 | 560 | u32 write_protect = le32_to_cpu(req->cmd->common.cdw11); |
dedf0be5 CK |
561 | struct nvmet_subsys *subsys = req->sq->ctrl->subsys; |
562 | u16 status = NVME_SC_FEATURE_NOT_CHANGEABLE; | |
563 | ||
564 | req->ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->rw.nsid); | |
565 | if (unlikely(!req->ns)) | |
566 | return status; | |
567 | ||
568 | mutex_lock(&subsys->lock); | |
569 | switch (write_protect) { | |
570 | case NVME_NS_WRITE_PROTECT: | |
571 | req->ns->readonly = true; | |
572 | status = nvmet_write_protect_flush_sync(req); | |
573 | if (status) | |
574 | req->ns->readonly = false; | |
575 | break; | |
576 | case NVME_NS_NO_WRITE_PROTECT: | |
577 | req->ns->readonly = false; | |
578 | status = 0; | |
579 | break; | |
580 | default: | |
581 | break; | |
582 | } | |
583 | ||
584 | if (!status) | |
585 | nvmet_ns_changed(subsys, req->ns->nsid); | |
586 | mutex_unlock(&subsys->lock); | |
587 | return status; | |
588 | } | |
589 | ||
90107455 JS |
590 | u16 nvmet_set_feat_kato(struct nvmet_req *req) |
591 | { | |
b7c8f366 | 592 | u32 val32 = le32_to_cpu(req->cmd->common.cdw11); |
90107455 JS |
593 | |
594 | req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000); | |
595 | ||
596 | nvmet_set_result(req, req->sq->ctrl->kato); | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
601 | u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask) | |
602 | { | |
b7c8f366 | 603 | u32 val32 = le32_to_cpu(req->cmd->common.cdw11); |
90107455 JS |
604 | |
605 | if (val32 & ~mask) | |
606 | return NVME_SC_INVALID_FIELD | NVME_SC_DNR; | |
607 | ||
608 | WRITE_ONCE(req->sq->ctrl->aen_enabled, val32); | |
609 | nvmet_set_result(req, val32); | |
610 | ||
611 | return 0; | |
612 | } | |
613 | ||
a07b4970 CH |
614 | static void nvmet_execute_set_features(struct nvmet_req *req) |
615 | { | |
616 | struct nvmet_subsys *subsys = req->sq->ctrl->subsys; | |
b7c8f366 | 617 | u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10); |
a07b4970 CH |
618 | u16 status = 0; |
619 | ||
28dd5cf7 | 620 | switch (cdw10 & 0xff) { |
a07b4970 CH |
621 | case NVME_FEAT_NUM_QUEUES: |
622 | nvmet_set_result(req, | |
623 | (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16)); | |
624 | break; | |
625 | case NVME_FEAT_KATO: | |
90107455 | 626 | status = nvmet_set_feat_kato(req); |
a07b4970 | 627 | break; |
c86b8f7b | 628 | case NVME_FEAT_ASYNC_EVENT: |
90107455 | 629 | status = nvmet_set_feat_async_event(req, NVMET_AEN_CFG_ALL); |
c86b8f7b | 630 | break; |
28dd5cf7 OM |
631 | case NVME_FEAT_HOST_ID: |
632 | status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR; | |
633 | break; | |
dedf0be5 CK |
634 | case NVME_FEAT_WRITE_PROTECT: |
635 | status = nvmet_set_feat_write_protect(req); | |
636 | break; | |
a07b4970 CH |
637 | default: |
638 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; | |
639 | break; | |
640 | } | |
641 | ||
642 | nvmet_req_complete(req, status); | |
643 | } | |
644 | ||
dedf0be5 CK |
645 | static u16 nvmet_get_feat_write_protect(struct nvmet_req *req) |
646 | { | |
647 | struct nvmet_subsys *subsys = req->sq->ctrl->subsys; | |
648 | u32 result; | |
649 | ||
650 | req->ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->common.nsid); | |
651 | if (!req->ns) | |
652 | return NVME_SC_INVALID_NS | NVME_SC_DNR; | |
653 | ||
654 | mutex_lock(&subsys->lock); | |
655 | if (req->ns->readonly == true) | |
656 | result = NVME_NS_WRITE_PROTECT; | |
657 | else | |
658 | result = NVME_NS_NO_WRITE_PROTECT; | |
659 | nvmet_set_result(req, result); | |
660 | mutex_unlock(&subsys->lock); | |
661 | ||
662 | return 0; | |
663 | } | |
664 | ||
90107455 JS |
665 | void nvmet_get_feat_kato(struct nvmet_req *req) |
666 | { | |
667 | nvmet_set_result(req, req->sq->ctrl->kato * 1000); | |
668 | } | |
669 | ||
670 | void nvmet_get_feat_async_event(struct nvmet_req *req) | |
671 | { | |
672 | nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled)); | |
673 | } | |
674 | ||
a07b4970 CH |
675 | static void nvmet_execute_get_features(struct nvmet_req *req) |
676 | { | |
677 | struct nvmet_subsys *subsys = req->sq->ctrl->subsys; | |
b7c8f366 | 678 | u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10); |
a07b4970 CH |
679 | u16 status = 0; |
680 | ||
28dd5cf7 | 681 | switch (cdw10 & 0xff) { |
a07b4970 CH |
682 | /* |
683 | * These features are mandatory in the spec, but we don't | |
684 | * have a useful way to implement them. We'll eventually | |
685 | * need to come up with some fake values for these. | |
686 | */ | |
687 | #if 0 | |
688 | case NVME_FEAT_ARBITRATION: | |
689 | break; | |
690 | case NVME_FEAT_POWER_MGMT: | |
691 | break; | |
692 | case NVME_FEAT_TEMP_THRESH: | |
693 | break; | |
694 | case NVME_FEAT_ERR_RECOVERY: | |
695 | break; | |
696 | case NVME_FEAT_IRQ_COALESCE: | |
697 | break; | |
698 | case NVME_FEAT_IRQ_CONFIG: | |
699 | break; | |
700 | case NVME_FEAT_WRITE_ATOMIC: | |
701 | break; | |
c86b8f7b | 702 | #endif |
a07b4970 | 703 | case NVME_FEAT_ASYNC_EVENT: |
90107455 | 704 | nvmet_get_feat_async_event(req); |
a07b4970 | 705 | break; |
a07b4970 CH |
706 | case NVME_FEAT_VOLATILE_WC: |
707 | nvmet_set_result(req, 1); | |
708 | break; | |
709 | case NVME_FEAT_NUM_QUEUES: | |
710 | nvmet_set_result(req, | |
711 | (subsys->max_qid-1) | ((subsys->max_qid-1) << 16)); | |
712 | break; | |
713 | case NVME_FEAT_KATO: | |
90107455 | 714 | nvmet_get_feat_kato(req); |
a07b4970 | 715 | break; |
28dd5cf7 OM |
716 | case NVME_FEAT_HOST_ID: |
717 | /* need 128-bit host identifier flag */ | |
b7c8f366 | 718 | if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) { |
28dd5cf7 OM |
719 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; |
720 | break; | |
721 | } | |
722 | ||
723 | status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid, | |
724 | sizeof(req->sq->ctrl->hostid)); | |
725 | break; | |
dedf0be5 CK |
726 | case NVME_FEAT_WRITE_PROTECT: |
727 | status = nvmet_get_feat_write_protect(req); | |
728 | break; | |
a07b4970 CH |
729 | default: |
730 | status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; | |
731 | break; | |
732 | } | |
733 | ||
734 | nvmet_req_complete(req, status); | |
735 | } | |
736 | ||
90107455 | 737 | void nvmet_execute_async_event(struct nvmet_req *req) |
a07b4970 CH |
738 | { |
739 | struct nvmet_ctrl *ctrl = req->sq->ctrl; | |
740 | ||
741 | mutex_lock(&ctrl->lock); | |
742 | if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) { | |
743 | mutex_unlock(&ctrl->lock); | |
744 | nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR); | |
745 | return; | |
746 | } | |
747 | ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req; | |
748 | mutex_unlock(&ctrl->lock); | |
749 | ||
750 | schedule_work(&ctrl->async_event_work); | |
751 | } | |
752 | ||
f9362ac1 | 753 | void nvmet_execute_keep_alive(struct nvmet_req *req) |
a07b4970 CH |
754 | { |
755 | struct nvmet_ctrl *ctrl = req->sq->ctrl; | |
756 | ||
757 | pr_debug("ctrl %d update keep-alive timer for %d secs\n", | |
758 | ctrl->cntlid, ctrl->kato); | |
759 | ||
760 | mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ); | |
761 | nvmet_req_complete(req, 0); | |
762 | } | |
763 | ||
64a0ca88 | 764 | u16 nvmet_parse_admin_cmd(struct nvmet_req *req) |
a07b4970 CH |
765 | { |
766 | struct nvme_command *cmd = req->cmd; | |
64a0ca88 | 767 | u16 ret; |
a07b4970 | 768 | |
64a0ca88 PP |
769 | ret = nvmet_check_ctrl_status(req, cmd); |
770 | if (unlikely(ret)) | |
771 | return ret; | |
a07b4970 CH |
772 | |
773 | switch (cmd->common.opcode) { | |
774 | case nvme_admin_get_log_page: | |
775 | req->data_len = nvmet_get_log_page_len(cmd); | |
776 | ||
777 | switch (cmd->get_log_page.lid) { | |
2ca0786d | 778 | case NVME_LOG_ERROR: |
8ab0805f CH |
779 | /* |
780 | * We currently never set the More bit in the status | |
781 | * field, so all error log entries are invalid and can | |
782 | * be zeroed out. This is called a minum viable | |
783 | * implementation (TM) of this mandatory log page. | |
784 | */ | |
785 | req->execute = nvmet_execute_get_log_page_noop; | |
786 | return 0; | |
2ca0786d | 787 | case NVME_LOG_SMART: |
8ab0805f CH |
788 | req->execute = nvmet_execute_get_log_page_smart; |
789 | return 0; | |
2ca0786d | 790 | case NVME_LOG_FW_SLOT: |
8ab0805f CH |
791 | /* |
792 | * We only support a single firmware slot which always | |
793 | * is active, so we can zero out the whole firmware slot | |
794 | * log and still claim to fully implement this mandatory | |
795 | * log page. | |
796 | */ | |
797 | req->execute = nvmet_execute_get_log_page_noop; | |
a07b4970 | 798 | return 0; |
c16734ea CH |
799 | case NVME_LOG_CHANGED_NS: |
800 | req->execute = nvmet_execute_get_log_changed_ns; | |
801 | return 0; | |
0866bf0c CK |
802 | case NVME_LOG_CMD_EFFECTS: |
803 | req->execute = nvmet_execute_get_log_cmd_effects_ns; | |
804 | return 0; | |
72efd25d CH |
805 | case NVME_LOG_ANA: |
806 | req->execute = nvmet_execute_get_log_page_ana; | |
807 | return 0; | |
a07b4970 CH |
808 | } |
809 | break; | |
810 | case nvme_admin_identify: | |
0add5e8e | 811 | req->data_len = NVME_IDENTIFY_DATA_SIZE; |
986994a2 | 812 | switch (cmd->identify.cns) { |
e9c9346e | 813 | case NVME_ID_CNS_NS: |
a07b4970 CH |
814 | req->execute = nvmet_execute_identify_ns; |
815 | return 0; | |
e9c9346e | 816 | case NVME_ID_CNS_CTRL: |
a07b4970 CH |
817 | req->execute = nvmet_execute_identify_ctrl; |
818 | return 0; | |
e9c9346e | 819 | case NVME_ID_CNS_NS_ACTIVE_LIST: |
a07b4970 CH |
820 | req->execute = nvmet_execute_identify_nslist; |
821 | return 0; | |
637dc0f3 JT |
822 | case NVME_ID_CNS_NS_DESC_LIST: |
823 | req->execute = nvmet_execute_identify_desclist; | |
824 | return 0; | |
a07b4970 CH |
825 | } |
826 | break; | |
827 | case nvme_admin_abort_cmd: | |
828 | req->execute = nvmet_execute_abort; | |
829 | req->data_len = 0; | |
830 | return 0; | |
831 | case nvme_admin_set_features: | |
832 | req->execute = nvmet_execute_set_features; | |
833 | req->data_len = 0; | |
834 | return 0; | |
835 | case nvme_admin_get_features: | |
836 | req->execute = nvmet_execute_get_features; | |
837 | req->data_len = 0; | |
838 | return 0; | |
839 | case nvme_admin_async_event: | |
840 | req->execute = nvmet_execute_async_event; | |
841 | req->data_len = 0; | |
842 | return 0; | |
843 | case nvme_admin_keep_alive: | |
844 | req->execute = nvmet_execute_keep_alive; | |
845 | req->data_len = 0; | |
846 | return 0; | |
847 | } | |
848 | ||
64a0ca88 PP |
849 | pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode, |
850 | req->sq->qid); | |
a07b4970 CH |
851 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
852 | } |