2 * NVMe admin command implementation.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
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.
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
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/rculist.h>
18 #include <generated/utsrelease.h>
19 #include <asm/unaligned.h>
22 u32 nvmet_get_log_page_len(struct nvme_command *cmd)
24 u32 len = le16_to_cpu(cmd->get_log_page.numdu);
27 len += le16_to_cpu(cmd->get_log_page.numdl);
28 /* NUMD is a 0's based value */
35 static void nvmet_execute_get_log_page_noop(struct nvmet_req *req)
37 nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->data_len));
40 static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
41 struct nvme_smart_log *slog)
44 u64 host_reads, host_writes, data_units_read, data_units_written;
46 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->get_log_page.nsid);
48 pr_err("nvmet : Could not find namespace id : %d\n",
49 le32_to_cpu(req->cmd->get_log_page.nsid));
50 return NVME_SC_INVALID_NS;
53 /* we don't have the right data for file backed ns */
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]);
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]);
67 nvmet_put_namespace(ns);
69 return NVME_SC_SUCCESS;
72 static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
73 struct nvme_smart_log *slog)
75 u64 host_reads = 0, host_writes = 0;
76 u64 data_units_read = 0, data_units_written = 0;
78 struct nvmet_ctrl *ctrl;
83 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
84 /* we don't have the right data for file backed ns */
87 host_reads += part_stat_read(ns->bdev->bd_part, ios[READ]);
89 part_stat_read(ns->bdev->bd_part, sectors[READ]);
90 host_writes += part_stat_read(ns->bdev->bd_part, ios[WRITE]);
92 part_stat_read(ns->bdev->bd_part, sectors[WRITE]);
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]);
102 return NVME_SC_SUCCESS;
105 static void nvmet_execute_get_log_page_smart(struct nvmet_req *req)
107 struct nvme_smart_log *log;
108 u16 status = NVME_SC_INTERNAL;
110 if (req->data_len != sizeof(*log))
113 log = kzalloc(sizeof(*log), GFP_KERNEL);
117 if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL))
118 status = nvmet_get_smart_log_all(req, log);
120 status = nvmet_get_smart_log_nsid(req, log);
124 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
126 nvmet_req_complete(req, status);
129 static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req)
131 struct nvmet_ctrl *ctrl = req->sq->ctrl;
132 u16 status = NVME_SC_INTERNAL;
135 if (req->data_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32))
138 mutex_lock(&ctrl->lock);
139 if (ctrl->nr_changed_ns == U32_MAX)
140 len = sizeof(__le32);
142 len = ctrl->nr_changed_ns * sizeof(__le32);
143 status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len);
145 status = nvmet_zero_sgl(req, len, req->data_len - len);
146 ctrl->nr_changed_ns = 0;
147 mutex_unlock(&ctrl->lock);
149 nvmet_req_complete(req, status);
152 static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
154 struct nvmet_ctrl *ctrl = req->sq->ctrl;
155 struct nvme_id_ctrl *id;
157 const char model[] = "Linux";
159 id = kzalloc(sizeof(*id), GFP_KERNEL);
161 status = NVME_SC_INTERNAL;
165 /* XXX: figure out how to assign real vendors IDs. */
169 memset(id->sn, ' ', sizeof(id->sn));
170 bin2hex(id->sn, &ctrl->subsys->serial,
171 min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
172 memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' ');
173 memcpy_and_pad(id->fr, sizeof(id->fr),
174 UTS_RELEASE, strlen(UTS_RELEASE), ' ');
179 * XXX: figure out how we can assign a IEEE OUI, but until then
180 * the safest is to leave it as zeroes.
183 /* we support multiple ports and multiples hosts: */
184 id->cmic = (1 << 0) | (1 << 1);
186 /* no limit on data transfer sizes for now */
188 id->cntlid = cpu_to_le16(ctrl->cntlid);
189 id->ver = cpu_to_le32(ctrl->subsys->ver);
191 /* XXX: figure out what to do about RTD3R/RTD3 */
192 id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
193 id->ctratt = cpu_to_le32(1 << 0);
198 * We don't really have a practical limit on the number of abort
199 * comands. But we don't do anything useful for abort either, so
200 * no point in allowing more abort commands than the spec requires.
204 id->aerl = NVMET_ASYNC_EVENTS - 1;
206 /* first slot is read-only, only one slot supported */
207 id->frmw = (1 << 0) | (1 << 1);
208 id->lpa = (1 << 0) | (1 << 2);
209 id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
212 /* We support keep-alive timeout in granularity of seconds */
213 id->kas = cpu_to_le16(NVMET_KAS);
215 id->sqes = (0x6 << 4) | 0x6;
216 id->cqes = (0x4 << 4) | 0x4;
218 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
219 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
221 id->nn = cpu_to_le32(ctrl->subsys->max_nsid);
222 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
223 NVME_CTRL_ONCS_WRITE_ZEROES);
225 /* XXX: don't report vwc if the underlying device is write through */
226 id->vwc = NVME_CTRL_VWC_PRESENT;
229 * We can't support atomic writes bigger than a LBA without support
230 * from the backend device.
235 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
236 if (ctrl->ops->has_keyed_sgls)
237 id->sgls |= cpu_to_le32(1 << 2);
238 if (ctrl->ops->sqe_inline_size)
239 id->sgls |= cpu_to_le32(1 << 20);
241 strcpy(id->subnqn, ctrl->subsys->subsysnqn);
243 /* Max command capsule size is sqe + single page of in-capsule data */
244 id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
245 ctrl->ops->sqe_inline_size) / 16);
246 /* Max response capsule size is cqe */
247 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
249 id->msdbd = ctrl->ops->msdbd;
252 * Meh, we don't really support any power state. Fake up the same
253 * values that qemu does.
255 id->psd[0].max_power = cpu_to_le16(0x9c4);
256 id->psd[0].entry_lat = cpu_to_le32(0x10);
257 id->psd[0].exit_lat = cpu_to_le32(0x4);
259 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
263 nvmet_req_complete(req, status);
266 static void nvmet_execute_identify_ns(struct nvmet_req *req)
269 struct nvme_id_ns *id;
272 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
274 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
278 id = kzalloc(sizeof(*id), GFP_KERNEL);
280 status = NVME_SC_INTERNAL;
285 * nuse = ncap = nsze isn't always true, but we have no way to find
286 * that out from the underlying device.
288 id->ncap = id->nuse = id->nsze =
289 cpu_to_le64(ns->size >> ns->blksize_shift);
292 * We just provide a single LBA format that matches what the
293 * underlying device reports.
299 * Our namespace might always be shared. Not just with other
300 * controllers, but also with any other user of the block device.
304 memcpy(&id->nguid, &ns->nguid, sizeof(uuid_le));
306 id->lbaf[0].ds = ns->blksize_shift;
308 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
312 nvmet_put_namespace(ns);
314 nvmet_req_complete(req, status);
317 static void nvmet_execute_identify_nslist(struct nvmet_req *req)
319 static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
320 struct nvmet_ctrl *ctrl = req->sq->ctrl;
322 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
327 list = kzalloc(buf_size, GFP_KERNEL);
329 status = NVME_SC_INTERNAL;
334 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
335 if (ns->nsid <= min_nsid)
337 list[i++] = cpu_to_le32(ns->nsid);
338 if (i == buf_size / sizeof(__le32))
343 status = nvmet_copy_to_sgl(req, 0, list, buf_size);
347 nvmet_req_complete(req, status);
350 static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
351 void *id, off_t *off)
353 struct nvme_ns_id_desc desc = {
359 status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
362 *off += sizeof(desc);
364 status = nvmet_copy_to_sgl(req, *off, id, len);
372 static void nvmet_execute_identify_desclist(struct nvmet_req *req)
378 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
380 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
384 if (memchr_inv(&ns->uuid, 0, sizeof(ns->uuid))) {
385 status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
391 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid))) {
392 status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
399 if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
400 off) != NVME_IDENTIFY_DATA_SIZE - off)
401 status = NVME_SC_INTERNAL | NVME_SC_DNR;
403 nvmet_put_namespace(ns);
405 nvmet_req_complete(req, status);
409 * A "minimum viable" abort implementation: the command is mandatory in the
410 * spec, but we are not required to do any useful work. We couldn't really
411 * do a useful abort, so don't bother even with waiting for the command
412 * to be exectuted and return immediately telling the command to abort
415 static void nvmet_execute_abort(struct nvmet_req *req)
417 nvmet_set_result(req, 1);
418 nvmet_req_complete(req, 0);
421 static void nvmet_execute_set_features(struct nvmet_req *req)
423 struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
424 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
428 switch (cdw10 & 0xff) {
429 case NVME_FEAT_NUM_QUEUES:
430 nvmet_set_result(req,
431 (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
434 val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
435 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
436 nvmet_set_result(req, req->sq->ctrl->kato);
438 case NVME_FEAT_ASYNC_EVENT:
439 val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
440 if (val32 & ~NVMET_AEN_CFG_ALL) {
441 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
445 WRITE_ONCE(req->sq->ctrl->aen_enabled, val32);
446 nvmet_set_result(req, val32);
448 case NVME_FEAT_HOST_ID:
449 status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
452 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
456 nvmet_req_complete(req, status);
459 static void nvmet_execute_get_features(struct nvmet_req *req)
461 struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
462 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
465 switch (cdw10 & 0xff) {
467 * These features are mandatory in the spec, but we don't
468 * have a useful way to implement them. We'll eventually
469 * need to come up with some fake values for these.
472 case NVME_FEAT_ARBITRATION:
474 case NVME_FEAT_POWER_MGMT:
476 case NVME_FEAT_TEMP_THRESH:
478 case NVME_FEAT_ERR_RECOVERY:
480 case NVME_FEAT_IRQ_COALESCE:
482 case NVME_FEAT_IRQ_CONFIG:
484 case NVME_FEAT_WRITE_ATOMIC:
487 case NVME_FEAT_ASYNC_EVENT:
488 nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled));
490 case NVME_FEAT_VOLATILE_WC:
491 nvmet_set_result(req, 1);
493 case NVME_FEAT_NUM_QUEUES:
494 nvmet_set_result(req,
495 (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
498 nvmet_set_result(req, req->sq->ctrl->kato * 1000);
500 case NVME_FEAT_HOST_ID:
501 /* need 128-bit host identifier flag */
502 if (!(req->cmd->common.cdw10[1] & cpu_to_le32(1 << 0))) {
503 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
507 status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
508 sizeof(req->sq->ctrl->hostid));
511 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
515 nvmet_req_complete(req, status);
518 static void nvmet_execute_async_event(struct nvmet_req *req)
520 struct nvmet_ctrl *ctrl = req->sq->ctrl;
522 mutex_lock(&ctrl->lock);
523 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
524 mutex_unlock(&ctrl->lock);
525 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
528 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
529 mutex_unlock(&ctrl->lock);
531 schedule_work(&ctrl->async_event_work);
534 static void nvmet_execute_keep_alive(struct nvmet_req *req)
536 struct nvmet_ctrl *ctrl = req->sq->ctrl;
538 pr_debug("ctrl %d update keep-alive timer for %d secs\n",
539 ctrl->cntlid, ctrl->kato);
541 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
542 nvmet_req_complete(req, 0);
545 u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
547 struct nvme_command *cmd = req->cmd;
550 ret = nvmet_check_ctrl_status(req, cmd);
554 switch (cmd->common.opcode) {
555 case nvme_admin_get_log_page:
556 req->data_len = nvmet_get_log_page_len(cmd);
558 switch (cmd->get_log_page.lid) {
561 * We currently never set the More bit in the status
562 * field, so all error log entries are invalid and can
563 * be zeroed out. This is called a minum viable
564 * implementation (TM) of this mandatory log page.
566 req->execute = nvmet_execute_get_log_page_noop;
569 req->execute = nvmet_execute_get_log_page_smart;
571 case NVME_LOG_FW_SLOT:
573 * We only support a single firmware slot which always
574 * is active, so we can zero out the whole firmware slot
575 * log and still claim to fully implement this mandatory
578 req->execute = nvmet_execute_get_log_page_noop;
580 case NVME_LOG_CHANGED_NS:
581 req->execute = nvmet_execute_get_log_changed_ns;
585 case nvme_admin_identify:
586 req->data_len = NVME_IDENTIFY_DATA_SIZE;
587 switch (cmd->identify.cns) {
589 req->execute = nvmet_execute_identify_ns;
591 case NVME_ID_CNS_CTRL:
592 req->execute = nvmet_execute_identify_ctrl;
594 case NVME_ID_CNS_NS_ACTIVE_LIST:
595 req->execute = nvmet_execute_identify_nslist;
597 case NVME_ID_CNS_NS_DESC_LIST:
598 req->execute = nvmet_execute_identify_desclist;
602 case nvme_admin_abort_cmd:
603 req->execute = nvmet_execute_abort;
606 case nvme_admin_set_features:
607 req->execute = nvmet_execute_set_features;
610 case nvme_admin_get_features:
611 req->execute = nvmet_execute_get_features;
614 case nvme_admin_async_event:
615 req->execute = nvmet_execute_async_event;
618 case nvme_admin_keep_alive:
619 req->execute = nvmet_execute_keep_alive;
624 pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
626 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;