blob: 9625328427690345b86f73c4a80e0c1035635e4b [file] [log] [blame]
Christoph Hellwiga07b4972016-06-21 18:04:20 +02001/*
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>
Ingo Molnarb2d09102017-02-04 01:27:20 +010016#include <linux/rculist.h>
17
Christoph Hellwiga07b4972016-06-21 18:04:20 +020018#include <generated/utsrelease.h>
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010019#include <asm/unaligned.h>
Christoph Hellwiga07b4972016-06-21 18:04:20 +020020#include "nvmet.h"
21
22u32 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
Christoph Hellwig8ab08052018-05-22 11:10:03 +020035static 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
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010040static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
41 struct nvme_smart_log *slog)
42{
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010043 struct nvmet_ns *ns;
44 u64 host_reads, host_writes, data_units_read, data_units_written;
45
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010046 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->get_log_page.nsid);
47 if (!ns) {
Colin Ian Kingb38b9052016-12-27 16:04:09 +000048 pr_err("nvmet : Could not find namespace id : %d\n",
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010049 le32_to_cpu(req->cmd->get_log_page.nsid));
Sagi Grimberg4185f252017-11-08 12:00:30 +020050 return NVME_SC_INVALID_NS;
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010051 }
52
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040053 /* we don't have the right data for file backed ns */
54 if (!ns->bdev)
55 goto out;
56
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010057 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]);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040066out:
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010067 nvmet_put_namespace(ns);
Sagi Grimberg4185f252017-11-08 12:00:30 +020068
69 return NVME_SC_SUCCESS;
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010070}
71
72static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
73 struct nvme_smart_log *slog)
74{
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010075 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
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010080 ctrl = req->sq->ctrl;
81
82 rcu_read_lock();
83 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040084 /* we don't have the right data for file backed ns */
85 if (!ns->bdev)
86 continue;
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +010087 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
Sagi Grimberg4185f252017-11-08 12:00:30 +0200102 return NVME_SC_SUCCESS;
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +0100103}
104
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200105static void nvmet_execute_get_log_page_smart(struct nvmet_req *req)
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +0100106{
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200107 struct nvme_smart_log *log;
108 u16 status = NVME_SC_INTERNAL;
Chaitanya Kulkarni2d79c7d2016-09-01 20:45:03 +0100109
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200110 if (req->data_len != sizeof(*log))
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200111 goto out;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200112
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200113 log = kzalloc(sizeof(*log), GFP_KERNEL);
114 if (!log)
115 goto out;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200116
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200117 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)
122 goto out;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200123
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200124 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200125out:
126 nvmet_req_complete(req, status);
127}
128
Christoph Hellwigc16734e2018-05-25 17:16:09 +0200129static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req)
130{
131 struct nvmet_ctrl *ctrl = req->sq->ctrl;
132 u16 status = NVME_SC_INTERNAL;
133 size_t len;
134
135 if (req->data_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32))
136 goto out;
137
138 mutex_lock(&ctrl->lock);
139 if (ctrl->nr_changed_ns == U32_MAX)
140 len = sizeof(__le32);
141 else
142 len = ctrl->nr_changed_ns * sizeof(__le32);
143 status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len);
144 if (!status)
145 status = nvmet_zero_sgl(req, len, req->data_len - len);
146 ctrl->nr_changed_ns = 0;
Christoph Hellwig55fdd6b2018-05-30 15:05:09 +0200147 clear_bit(NVME_AEN_CFG_NS_ATTR, &ctrl->aen_masked);
Christoph Hellwigc16734e2018-05-25 17:16:09 +0200148 mutex_unlock(&ctrl->lock);
149out:
150 nvmet_req_complete(req, status);
151}
152
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200153static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
154{
155 struct nvmet_ctrl *ctrl = req->sq->ctrl;
156 struct nvme_id_ctrl *id;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200157 u16 status = 0;
Martin Wilck42de82a2017-07-14 00:25:31 +0200158 const char model[] = "Linux";
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200159
160 id = kzalloc(sizeof(*id), GFP_KERNEL);
161 if (!id) {
162 status = NVME_SC_INTERNAL;
163 goto out;
164 }
165
166 /* XXX: figure out how to assign real vendors IDs. */
167 id->vid = 0;
168 id->ssvid = 0;
169
Daniel Verkampc7399692018-04-12 09:16:13 -0600170 memset(id->sn, ' ', sizeof(id->sn));
Martin Wilck42de82a2017-07-14 00:25:31 +0200171 bin2hex(id->sn, &ctrl->subsys->serial,
172 min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
Martin Wilck17c39d02017-08-14 22:12:39 +0200173 memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' ');
174 memcpy_and_pad(id->fr, sizeof(id->fr),
175 UTS_RELEASE, strlen(UTS_RELEASE), ' ');
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200176
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200177 id->rab = 6;
178
179 /*
180 * XXX: figure out how we can assign a IEEE OUI, but until then
181 * the safest is to leave it as zeroes.
182 */
183
184 /* we support multiple ports and multiples hosts: */
Christoph Hellwiga446c082016-09-30 13:51:06 +0200185 id->cmic = (1 << 0) | (1 << 1);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200186
187 /* no limit on data transfer sizes for now */
188 id->mdts = 0;
189 id->cntlid = cpu_to_le16(ctrl->cntlid);
190 id->ver = cpu_to_le32(ctrl->subsys->ver);
191
192 /* XXX: figure out what to do about RTD3R/RTD3 */
Christoph Hellwigc86b8f72018-05-30 15:04:47 +0200193 id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200194 id->ctratt = cpu_to_le32(1 << 0);
195
196 id->oacs = 0;
197
198 /*
199 * We don't really have a practical limit on the number of abort
200 * comands. But we don't do anything useful for abort either, so
201 * no point in allowing more abort commands than the spec requires.
202 */
203 id->acl = 3;
204
205 id->aerl = NVMET_ASYNC_EVENTS - 1;
206
207 /* first slot is read-only, only one slot supported */
208 id->frmw = (1 << 0) | (1 << 1);
209 id->lpa = (1 << 0) | (1 << 2);
210 id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
211 id->npss = 0;
212
213 /* We support keep-alive timeout in granularity of seconds */
214 id->kas = cpu_to_le16(NVMET_KAS);
215
216 id->sqes = (0x6 << 4) | 0x6;
217 id->cqes = (0x4 << 4) | 0x4;
218
219 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
220 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
221
222 id->nn = cpu_to_le32(ctrl->subsys->max_nsid);
Chaitanya Kulkarnid2629202016-11-30 12:29:02 -0800223 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
224 NVME_CTRL_ONCS_WRITE_ZEROES);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200225
226 /* XXX: don't report vwc if the underlying device is write through */
227 id->vwc = NVME_CTRL_VWC_PRESENT;
228
229 /*
230 * We can't support atomic writes bigger than a LBA without support
231 * from the backend device.
232 */
233 id->awun = 0;
234 id->awupf = 0;
235
236 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
237 if (ctrl->ops->has_keyed_sgls)
238 id->sgls |= cpu_to_le32(1 << 2);
239 if (ctrl->ops->sqe_inline_size)
240 id->sgls |= cpu_to_le32(1 << 20);
241
242 strcpy(id->subnqn, ctrl->subsys->subsysnqn);
243
244 /* Max command capsule size is sqe + single page of in-capsule data */
245 id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
246 ctrl->ops->sqe_inline_size) / 16);
247 /* Max response capsule size is cqe */
248 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
249
250 id->msdbd = ctrl->ops->msdbd;
251
252 /*
253 * Meh, we don't really support any power state. Fake up the same
254 * values that qemu does.
255 */
256 id->psd[0].max_power = cpu_to_le16(0x9c4);
257 id->psd[0].entry_lat = cpu_to_le32(0x10);
258 id->psd[0].exit_lat = cpu_to_le32(0x4);
259
260 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
261
262 kfree(id);
263out:
264 nvmet_req_complete(req, status);
265}
266
267static void nvmet_execute_identify_ns(struct nvmet_req *req)
268{
269 struct nvmet_ns *ns;
270 struct nvme_id_ns *id;
271 u16 status = 0;
272
Christoph Hellwigf39ae472018-05-31 18:23:48 +0200273 if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200274 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
275 goto out;
276 }
277
278 id = kzalloc(sizeof(*id), GFP_KERNEL);
279 if (!id) {
280 status = NVME_SC_INTERNAL;
Christoph Hellwigf39ae472018-05-31 18:23:48 +0200281 goto out;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200282 }
283
Christoph Hellwigf39ae472018-05-31 18:23:48 +0200284 /* return an all zeroed buffer if we can't find an active namespace */
285 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
286 if (!ns)
287 goto done;
288
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200289 /*
Minwoo Im18c53e42017-11-07 21:10:22 +0900290 * nuse = ncap = nsze isn't always true, but we have no way to find
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200291 * that out from the underlying device.
292 */
293 id->ncap = id->nuse = id->nsze =
294 cpu_to_le64(ns->size >> ns->blksize_shift);
295
296 /*
297 * We just provide a single LBA format that matches what the
298 * underlying device reports.
299 */
300 id->nlbaf = 0;
301 id->flbas = 0;
302
303 /*
304 * Our namespace might always be shared. Not just with other
305 * controllers, but also with any other user of the block device.
306 */
307 id->nmic = (1 << 0);
308
309 memcpy(&id->nguid, &ns->nguid, sizeof(uuid_le));
310
311 id->lbaf[0].ds = ns->blksize_shift;
312
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200313 nvmet_put_namespace(ns);
Christoph Hellwigf39ae472018-05-31 18:23:48 +0200314done:
315 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
316 kfree(id);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200317out:
318 nvmet_req_complete(req, status);
319}
320
321static void nvmet_execute_identify_nslist(struct nvmet_req *req)
322{
Johannes Thumshirn0add5e82017-06-07 11:45:29 +0200323 static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200324 struct nvmet_ctrl *ctrl = req->sq->ctrl;
325 struct nvmet_ns *ns;
326 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
327 __le32 *list;
328 u16 status = 0;
329 int i = 0;
330
331 list = kzalloc(buf_size, GFP_KERNEL);
332 if (!list) {
333 status = NVME_SC_INTERNAL;
334 goto out;
335 }
336
337 rcu_read_lock();
338 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
339 if (ns->nsid <= min_nsid)
340 continue;
341 list[i++] = cpu_to_le32(ns->nsid);
342 if (i == buf_size / sizeof(__le32))
343 break;
344 }
345 rcu_read_unlock();
346
347 status = nvmet_copy_to_sgl(req, 0, list, buf_size);
348
349 kfree(list);
350out:
351 nvmet_req_complete(req, status);
352}
353
Johannes Thumshirn637dc0f2017-06-07 11:45:32 +0200354static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
355 void *id, off_t *off)
356{
357 struct nvme_ns_id_desc desc = {
358 .nidt = type,
359 .nidl = len,
360 };
361 u16 status;
362
363 status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
364 if (status)
365 return status;
366 *off += sizeof(desc);
367
368 status = nvmet_copy_to_sgl(req, *off, id, len);
369 if (status)
370 return status;
371 *off += len;
372
373 return 0;
374}
375
376static void nvmet_execute_identify_desclist(struct nvmet_req *req)
377{
378 struct nvmet_ns *ns;
379 u16 status = 0;
380 off_t off = 0;
381
382 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
383 if (!ns) {
384 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
385 goto out;
386 }
387
388 if (memchr_inv(&ns->uuid, 0, sizeof(ns->uuid))) {
389 status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
390 NVME_NIDT_UUID_LEN,
391 &ns->uuid, &off);
392 if (status)
393 goto out_put_ns;
394 }
395 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid))) {
396 status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
397 NVME_NIDT_NGUID_LEN,
398 &ns->nguid, &off);
399 if (status)
400 goto out_put_ns;
401 }
402
403 if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
404 off) != NVME_IDENTIFY_DATA_SIZE - off)
405 status = NVME_SC_INTERNAL | NVME_SC_DNR;
406out_put_ns:
407 nvmet_put_namespace(ns);
408out:
409 nvmet_req_complete(req, status);
410}
411
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200412/*
Minwoo Im18c53e42017-11-07 21:10:22 +0900413 * A "minimum viable" abort implementation: the command is mandatory in the
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200414 * spec, but we are not required to do any useful work. We couldn't really
415 * do a useful abort, so don't bother even with waiting for the command
416 * to be exectuted and return immediately telling the command to abort
417 * wasn't found.
418 */
419static void nvmet_execute_abort(struct nvmet_req *req)
420{
421 nvmet_set_result(req, 1);
422 nvmet_req_complete(req, 0);
423}
424
425static void nvmet_execute_set_features(struct nvmet_req *req)
426{
427 struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
428 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200429 u32 val32;
430 u16 status = 0;
431
Omri Mann28dd5cf2017-08-30 15:22:59 +0300432 switch (cdw10 & 0xff) {
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200433 case NVME_FEAT_NUM_QUEUES:
434 nvmet_set_result(req,
435 (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
436 break;
437 case NVME_FEAT_KATO:
Daniel Verkamp6c73f942016-12-09 12:59:46 -0700438 val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200439 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
440 nvmet_set_result(req, req->sq->ctrl->kato);
441 break;
Christoph Hellwigc86b8f72018-05-30 15:04:47 +0200442 case NVME_FEAT_ASYNC_EVENT:
443 val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
444 if (val32 & ~NVMET_AEN_CFG_ALL) {
445 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
446 break;
447 }
448
449 WRITE_ONCE(req->sq->ctrl->aen_enabled, val32);
450 nvmet_set_result(req, val32);
451 break;
Omri Mann28dd5cf2017-08-30 15:22:59 +0300452 case NVME_FEAT_HOST_ID:
453 status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
454 break;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200455 default:
456 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
457 break;
458 }
459
460 nvmet_req_complete(req, status);
461}
462
463static void nvmet_execute_get_features(struct nvmet_req *req)
464{
465 struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
466 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
467 u16 status = 0;
468
Omri Mann28dd5cf2017-08-30 15:22:59 +0300469 switch (cdw10 & 0xff) {
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200470 /*
471 * These features are mandatory in the spec, but we don't
472 * have a useful way to implement them. We'll eventually
473 * need to come up with some fake values for these.
474 */
475#if 0
476 case NVME_FEAT_ARBITRATION:
477 break;
478 case NVME_FEAT_POWER_MGMT:
479 break;
480 case NVME_FEAT_TEMP_THRESH:
481 break;
482 case NVME_FEAT_ERR_RECOVERY:
483 break;
484 case NVME_FEAT_IRQ_COALESCE:
485 break;
486 case NVME_FEAT_IRQ_CONFIG:
487 break;
488 case NVME_FEAT_WRITE_ATOMIC:
489 break;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200490#endif
Christoph Hellwigc86b8f72018-05-30 15:04:47 +0200491 case NVME_FEAT_ASYNC_EVENT:
492 nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled));
493 break;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200494 case NVME_FEAT_VOLATILE_WC:
495 nvmet_set_result(req, 1);
496 break;
497 case NVME_FEAT_NUM_QUEUES:
498 nvmet_set_result(req,
499 (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
500 break;
501 case NVME_FEAT_KATO:
502 nvmet_set_result(req, req->sq->ctrl->kato * 1000);
503 break;
Omri Mann28dd5cf2017-08-30 15:22:59 +0300504 case NVME_FEAT_HOST_ID:
505 /* need 128-bit host identifier flag */
506 if (!(req->cmd->common.cdw10[1] & cpu_to_le32(1 << 0))) {
507 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
508 break;
509 }
510
511 status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
512 sizeof(req->sq->ctrl->hostid));
513 break;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200514 default:
515 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
516 break;
517 }
518
519 nvmet_req_complete(req, status);
520}
521
522static void nvmet_execute_async_event(struct nvmet_req *req)
523{
524 struct nvmet_ctrl *ctrl = req->sq->ctrl;
525
526 mutex_lock(&ctrl->lock);
527 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
528 mutex_unlock(&ctrl->lock);
529 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
530 return;
531 }
532 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
533 mutex_unlock(&ctrl->lock);
534
535 schedule_work(&ctrl->async_event_work);
536}
537
538static void nvmet_execute_keep_alive(struct nvmet_req *req)
539{
540 struct nvmet_ctrl *ctrl = req->sq->ctrl;
541
542 pr_debug("ctrl %d update keep-alive timer for %d secs\n",
543 ctrl->cntlid, ctrl->kato);
544
545 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
546 nvmet_req_complete(req, 0);
547}
548
Parav Pandit64a0ca82017-02-27 23:21:33 -0600549u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200550{
551 struct nvme_command *cmd = req->cmd;
Parav Pandit64a0ca82017-02-27 23:21:33 -0600552 u16 ret;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200553
Parav Pandit64a0ca82017-02-27 23:21:33 -0600554 ret = nvmet_check_ctrl_status(req, cmd);
555 if (unlikely(ret))
556 return ret;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200557
558 switch (cmd->common.opcode) {
559 case nvme_admin_get_log_page:
560 req->data_len = nvmet_get_log_page_len(cmd);
561
562 switch (cmd->get_log_page.lid) {
Max Gurtovoy2ca07862017-03-06 00:30:38 +0200563 case NVME_LOG_ERROR:
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200564 /*
565 * We currently never set the More bit in the status
566 * field, so all error log entries are invalid and can
567 * be zeroed out. This is called a minum viable
568 * implementation (TM) of this mandatory log page.
569 */
570 req->execute = nvmet_execute_get_log_page_noop;
571 return 0;
Max Gurtovoy2ca07862017-03-06 00:30:38 +0200572 case NVME_LOG_SMART:
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200573 req->execute = nvmet_execute_get_log_page_smart;
574 return 0;
Max Gurtovoy2ca07862017-03-06 00:30:38 +0200575 case NVME_LOG_FW_SLOT:
Christoph Hellwig8ab08052018-05-22 11:10:03 +0200576 /*
577 * We only support a single firmware slot which always
578 * is active, so we can zero out the whole firmware slot
579 * log and still claim to fully implement this mandatory
580 * log page.
581 */
582 req->execute = nvmet_execute_get_log_page_noop;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200583 return 0;
Christoph Hellwigc16734e2018-05-25 17:16:09 +0200584 case NVME_LOG_CHANGED_NS:
585 req->execute = nvmet_execute_get_log_changed_ns;
586 return 0;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200587 }
588 break;
589 case nvme_admin_identify:
Johannes Thumshirn0add5e82017-06-07 11:45:29 +0200590 req->data_len = NVME_IDENTIFY_DATA_SIZE;
Parav Pandit986994a2017-01-26 17:17:28 +0200591 switch (cmd->identify.cns) {
Christoph Hellwige9c93462016-09-30 13:51:10 +0200592 case NVME_ID_CNS_NS:
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200593 req->execute = nvmet_execute_identify_ns;
594 return 0;
Christoph Hellwige9c93462016-09-30 13:51:10 +0200595 case NVME_ID_CNS_CTRL:
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200596 req->execute = nvmet_execute_identify_ctrl;
597 return 0;
Christoph Hellwige9c93462016-09-30 13:51:10 +0200598 case NVME_ID_CNS_NS_ACTIVE_LIST:
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200599 req->execute = nvmet_execute_identify_nslist;
600 return 0;
Johannes Thumshirn637dc0f2017-06-07 11:45:32 +0200601 case NVME_ID_CNS_NS_DESC_LIST:
602 req->execute = nvmet_execute_identify_desclist;
603 return 0;
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200604 }
605 break;
606 case nvme_admin_abort_cmd:
607 req->execute = nvmet_execute_abort;
608 req->data_len = 0;
609 return 0;
610 case nvme_admin_set_features:
611 req->execute = nvmet_execute_set_features;
612 req->data_len = 0;
613 return 0;
614 case nvme_admin_get_features:
615 req->execute = nvmet_execute_get_features;
616 req->data_len = 0;
617 return 0;
618 case nvme_admin_async_event:
619 req->execute = nvmet_execute_async_event;
620 req->data_len = 0;
621 return 0;
622 case nvme_admin_keep_alive:
623 req->execute = nvmet_execute_keep_alive;
624 req->data_len = 0;
625 return 0;
626 }
627
Parav Pandit64a0ca82017-02-27 23:21:33 -0600628 pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
629 req->sq->qid);
Christoph Hellwiga07b4972016-06-21 18:04:20 +0200630 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
631}