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 u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
36 struct nvme_smart_log *slog)
40 u64 host_reads, host_writes, data_units_read, data_units_written;
42 status = NVME_SC_SUCCESS;
43 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->get_log_page.nsid);
45 status = NVME_SC_INVALID_NS;
46 pr_err("nvmet : Could not find namespace id : %d\n",
47 le32_to_cpu(req->cmd->get_log_page.nsid));
51 host_reads = part_stat_read(ns->bdev->bd_part, ios[READ]);
52 data_units_read = part_stat_read(ns->bdev->bd_part, sectors[READ]);
53 host_writes = part_stat_read(ns->bdev->bd_part, ios[WRITE]);
54 data_units_written = part_stat_read(ns->bdev->bd_part, sectors[WRITE]);
56 put_unaligned_le64(host_reads, &slog->host_reads[0]);
57 put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
58 put_unaligned_le64(host_writes, &slog->host_writes[0]);
59 put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
60 nvmet_put_namespace(ns);
65 static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
66 struct nvme_smart_log *slog)
69 u64 host_reads = 0, host_writes = 0;
70 u64 data_units_read = 0, data_units_written = 0;
72 struct nvmet_ctrl *ctrl;
74 status = NVME_SC_SUCCESS;
78 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
79 host_reads += part_stat_read(ns->bdev->bd_part, ios[READ]);
81 part_stat_read(ns->bdev->bd_part, sectors[READ]);
82 host_writes += part_stat_read(ns->bdev->bd_part, ios[WRITE]);
84 part_stat_read(ns->bdev->bd_part, sectors[WRITE]);
89 put_unaligned_le64(host_reads, &slog->host_reads[0]);
90 put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
91 put_unaligned_le64(host_writes, &slog->host_writes[0]);
92 put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
97 static u16 nvmet_get_smart_log(struct nvmet_req *req,
98 struct nvme_smart_log *slog)
102 WARN_ON(req == NULL || slog == NULL);
103 if (req->cmd->get_log_page.nsid == 0xFFFFFFFF)
104 status = nvmet_get_smart_log_all(req, slog);
106 status = nvmet_get_smart_log_nsid(req, slog);
110 static void nvmet_execute_get_log_page(struct nvmet_req *req)
112 struct nvme_smart_log *smart_log;
113 size_t data_len = nvmet_get_log_page_len(req->cmd);
117 buf = kzalloc(data_len, GFP_KERNEL);
119 status = NVME_SC_INTERNAL;
123 switch (req->cmd->get_log_page.lid) {
126 * We currently never set the More bit in the status field,
127 * so all error log entries are invalid and can be zeroed out.
128 * This is called a minum viable implementation (TM) of this
129 * mandatory log page.
134 * XXX: fill out actual smart log
136 * We might have a hard time coming up with useful values for
137 * many of the fields, and even when we have useful data
138 * available (e.g. units or commands read/written) those aren't
139 * persistent over power loss.
141 if (data_len != sizeof(*smart_log)) {
142 status = NVME_SC_INTERNAL;
146 status = nvmet_get_smart_log(req, smart_log);
148 memset(buf, '\0', data_len);
152 case NVME_LOG_FW_SLOT:
154 * We only support a single firmware slot which always is
155 * active, so we can zero out the whole firmware slot log and
156 * still claim to fully implement this mandatory log page.
163 status = nvmet_copy_to_sgl(req, 0, buf, data_len);
168 nvmet_req_complete(req, status);
171 static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
173 struct nvmet_ctrl *ctrl = req->sq->ctrl;
174 struct nvme_id_ctrl *id;
177 id = kzalloc(sizeof(*id), GFP_KERNEL);
179 status = NVME_SC_INTERNAL;
183 /* XXX: figure out how to assign real vendors IDs. */
187 memset(id->sn, ' ', sizeof(id->sn));
188 snprintf(id->sn, sizeof(id->sn), "%llx", ctrl->serial);
190 memset(id->mn, ' ', sizeof(id->mn));
191 strncpy((char *)id->mn, "Linux", sizeof(id->mn));
193 memset(id->fr, ' ', sizeof(id->fr));
194 strncpy((char *)id->fr, UTS_RELEASE, sizeof(id->fr));
199 * XXX: figure out how we can assign a IEEE OUI, but until then
200 * the safest is to leave it as zeroes.
203 /* we support multiple ports and multiples hosts: */
204 id->cmic = (1 << 0) | (1 << 1);
206 /* no limit on data transfer sizes for now */
208 id->cntlid = cpu_to_le16(ctrl->cntlid);
209 id->ver = cpu_to_le32(ctrl->subsys->ver);
211 /* XXX: figure out what to do about RTD3R/RTD3 */
212 id->oaes = cpu_to_le32(1 << 8);
213 id->ctratt = cpu_to_le32(1 << 0);
218 * We don't really have a practical limit on the number of abort
219 * comands. But we don't do anything useful for abort either, so
220 * no point in allowing more abort commands than the spec requires.
224 id->aerl = NVMET_ASYNC_EVENTS - 1;
226 /* first slot is read-only, only one slot supported */
227 id->frmw = (1 << 0) | (1 << 1);
228 id->lpa = (1 << 0) | (1 << 2);
229 id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
232 /* We support keep-alive timeout in granularity of seconds */
233 id->kas = cpu_to_le16(NVMET_KAS);
235 id->sqes = (0x6 << 4) | 0x6;
236 id->cqes = (0x4 << 4) | 0x4;
238 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
239 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
241 id->nn = cpu_to_le32(ctrl->subsys->max_nsid);
242 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
243 NVME_CTRL_ONCS_WRITE_ZEROES);
245 /* XXX: don't report vwc if the underlying device is write through */
246 id->vwc = NVME_CTRL_VWC_PRESENT;
249 * We can't support atomic writes bigger than a LBA without support
250 * from the backend device.
255 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
256 if (ctrl->ops->has_keyed_sgls)
257 id->sgls |= cpu_to_le32(1 << 2);
258 if (ctrl->ops->sqe_inline_size)
259 id->sgls |= cpu_to_le32(1 << 20);
261 strcpy(id->subnqn, ctrl->subsys->subsysnqn);
263 /* Max command capsule size is sqe + single page of in-capsule data */
264 id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
265 ctrl->ops->sqe_inline_size) / 16);
266 /* Max response capsule size is cqe */
267 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
269 id->msdbd = ctrl->ops->msdbd;
272 * Meh, we don't really support any power state. Fake up the same
273 * values that qemu does.
275 id->psd[0].max_power = cpu_to_le16(0x9c4);
276 id->psd[0].entry_lat = cpu_to_le32(0x10);
277 id->psd[0].exit_lat = cpu_to_le32(0x4);
279 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
283 nvmet_req_complete(req, status);
286 static void nvmet_execute_identify_ns(struct nvmet_req *req)
289 struct nvme_id_ns *id;
292 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
294 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
298 id = kzalloc(sizeof(*id), GFP_KERNEL);
300 status = NVME_SC_INTERNAL;
305 * nuse = ncap = nsze isn't aways true, but we have no way to find
306 * that out from the underlying device.
308 id->ncap = id->nuse = id->nsze =
309 cpu_to_le64(ns->size >> ns->blksize_shift);
312 * We just provide a single LBA format that matches what the
313 * underlying device reports.
319 * Our namespace might always be shared. Not just with other
320 * controllers, but also with any other user of the block device.
324 memcpy(&id->nguid, &ns->nguid, sizeof(uuid_le));
326 id->lbaf[0].ds = ns->blksize_shift;
328 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
332 nvmet_put_namespace(ns);
334 nvmet_req_complete(req, status);
337 static void nvmet_execute_identify_nslist(struct nvmet_req *req)
339 static const int buf_size = 4096;
340 struct nvmet_ctrl *ctrl = req->sq->ctrl;
342 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
347 list = kzalloc(buf_size, GFP_KERNEL);
349 status = NVME_SC_INTERNAL;
354 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
355 if (ns->nsid <= min_nsid)
357 list[i++] = cpu_to_le32(ns->nsid);
358 if (i == buf_size / sizeof(__le32))
363 status = nvmet_copy_to_sgl(req, 0, list, buf_size);
367 nvmet_req_complete(req, status);
371 * A "mimimum viable" abort implementation: the command is mandatory in the
372 * spec, but we are not required to do any useful work. We couldn't really
373 * do a useful abort, so don't bother even with waiting for the command
374 * to be exectuted and return immediately telling the command to abort
377 static void nvmet_execute_abort(struct nvmet_req *req)
379 nvmet_set_result(req, 1);
380 nvmet_req_complete(req, 0);
383 static void nvmet_execute_set_features(struct nvmet_req *req)
385 struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
386 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
390 switch (cdw10 & 0xf) {
391 case NVME_FEAT_NUM_QUEUES:
392 nvmet_set_result(req,
393 (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
396 val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
397 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
398 nvmet_set_result(req, req->sq->ctrl->kato);
401 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
405 nvmet_req_complete(req, status);
408 static void nvmet_execute_get_features(struct nvmet_req *req)
410 struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
411 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
414 switch (cdw10 & 0xf) {
416 * These features are mandatory in the spec, but we don't
417 * have a useful way to implement them. We'll eventually
418 * need to come up with some fake values for these.
421 case NVME_FEAT_ARBITRATION:
423 case NVME_FEAT_POWER_MGMT:
425 case NVME_FEAT_TEMP_THRESH:
427 case NVME_FEAT_ERR_RECOVERY:
429 case NVME_FEAT_IRQ_COALESCE:
431 case NVME_FEAT_IRQ_CONFIG:
433 case NVME_FEAT_WRITE_ATOMIC:
435 case NVME_FEAT_ASYNC_EVENT:
438 case NVME_FEAT_VOLATILE_WC:
439 nvmet_set_result(req, 1);
441 case NVME_FEAT_NUM_QUEUES:
442 nvmet_set_result(req,
443 (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
446 nvmet_set_result(req, req->sq->ctrl->kato * 1000);
449 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
453 nvmet_req_complete(req, status);
456 static void nvmet_execute_async_event(struct nvmet_req *req)
458 struct nvmet_ctrl *ctrl = req->sq->ctrl;
460 mutex_lock(&ctrl->lock);
461 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
462 mutex_unlock(&ctrl->lock);
463 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
466 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
467 mutex_unlock(&ctrl->lock);
469 schedule_work(&ctrl->async_event_work);
472 static void nvmet_execute_keep_alive(struct nvmet_req *req)
474 struct nvmet_ctrl *ctrl = req->sq->ctrl;
476 pr_debug("ctrl %d update keep-alive timer for %d secs\n",
477 ctrl->cntlid, ctrl->kato);
479 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
480 nvmet_req_complete(req, 0);
483 u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
485 struct nvme_command *cmd = req->cmd;
490 ret = nvmet_check_ctrl_status(req, cmd);
494 switch (cmd->common.opcode) {
495 case nvme_admin_get_log_page:
496 req->data_len = nvmet_get_log_page_len(cmd);
498 switch (cmd->get_log_page.lid) {
501 case NVME_LOG_FW_SLOT:
502 req->execute = nvmet_execute_get_log_page;
506 case nvme_admin_identify:
507 req->data_len = 4096;
508 switch (cmd->identify.cns) {
510 req->execute = nvmet_execute_identify_ns;
512 case NVME_ID_CNS_CTRL:
513 req->execute = nvmet_execute_identify_ctrl;
515 case NVME_ID_CNS_NS_ACTIVE_LIST:
516 req->execute = nvmet_execute_identify_nslist;
520 case nvme_admin_abort_cmd:
521 req->execute = nvmet_execute_abort;
524 case nvme_admin_set_features:
525 req->execute = nvmet_execute_set_features;
528 case nvme_admin_get_features:
529 req->execute = nvmet_execute_get_features;
532 case nvme_admin_async_event:
533 req->execute = nvmet_execute_async_event;
536 case nvme_admin_keep_alive:
537 req->execute = nvmet_execute_keep_alive;
542 pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
544 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;