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/random.h>
17 #include <generated/utsrelease.h>
20 u32 nvmet_get_log_page_len(struct nvme_command *cmd)
22 u32 len = le16_to_cpu(cmd->get_log_page.numdu);
25 len += le16_to_cpu(cmd->get_log_page.numdl);
26 /* NUMD is a 0's based value */
33 static void nvmet_execute_get_log_page(struct nvmet_req *req)
35 size_t data_len = nvmet_get_log_page_len(req->cmd);
39 buf = kzalloc(data_len, GFP_KERNEL);
41 status = NVME_SC_INTERNAL;
45 switch (req->cmd->get_log_page.lid) {
48 * We currently never set the More bit in the status field,
49 * so all error log entries are invalid and can be zeroed out.
50 * This is called a minum viable implementation (TM) of this
56 * XXX: fill out actual smart log
58 * We might have a hard time coming up with useful values for
59 * many of the fields, and even when we have useful data
60 * available (e.g. units or commands read/written) those aren't
61 * persistent over power loss.
66 * We only support a single firmware slot which always is
67 * active, so we can zero out the whole firmware slot log and
68 * still claim to fully implement this mandatory log page.
75 status = nvmet_copy_to_sgl(req, 0, buf, data_len);
79 nvmet_req_complete(req, status);
82 static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
84 struct nvmet_ctrl *ctrl = req->sq->ctrl;
85 struct nvme_id_ctrl *id;
89 id = kzalloc(sizeof(*id), GFP_KERNEL);
91 status = NVME_SC_INTERNAL;
95 /* XXX: figure out how to assign real vendors IDs. */
99 /* generate a random serial number as our controllers are ephemeral: */
100 get_random_bytes(&serial, sizeof(serial));
101 memset(id->sn, ' ', sizeof(id->sn));
102 snprintf(id->sn, sizeof(id->sn), "%llx", serial);
104 memset(id->mn, ' ', sizeof(id->mn));
105 strncpy((char *)id->mn, "Linux", sizeof(id->mn));
107 memset(id->fr, ' ', sizeof(id->fr));
108 strncpy((char *)id->fr, UTS_RELEASE, sizeof(id->fr));
113 * XXX: figure out how we can assign a IEEE OUI, but until then
114 * the safest is to leave it as zeroes.
117 /* we support multiple ports and multiples hosts: */
118 id->mic = (1 << 0) | (1 << 1);
120 /* no limit on data transfer sizes for now */
122 id->cntlid = cpu_to_le16(ctrl->cntlid);
123 id->ver = cpu_to_le32(ctrl->subsys->ver);
125 /* XXX: figure out what to do about RTD3R/RTD3 */
126 id->oaes = cpu_to_le32(1 << 8);
127 id->ctratt = cpu_to_le32(1 << 0);
132 * We don't really have a practical limit on the number of abort
133 * comands. But we don't do anything useful for abort either, so
134 * no point in allowing more abort commands than the spec requires.
138 id->aerl = NVMET_ASYNC_EVENTS - 1;
140 /* first slot is read-only, only one slot supported */
141 id->frmw = (1 << 0) | (1 << 1);
142 id->lpa = (1 << 0) | (1 << 2);
143 id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
146 /* We support keep-alive timeout in granularity of seconds */
147 id->kas = cpu_to_le16(NVMET_KAS);
149 id->sqes = (0x6 << 4) | 0x6;
150 id->cqes = (0x4 << 4) | 0x4;
152 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
153 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
155 id->nn = cpu_to_le32(ctrl->subsys->max_nsid);
156 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM);
158 /* XXX: don't report vwc if the underlying device is write through */
159 id->vwc = NVME_CTRL_VWC_PRESENT;
162 * We can't support atomic writes bigger than a LBA without support
163 * from the backend device.
168 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
169 if (ctrl->ops->has_keyed_sgls)
170 id->sgls |= cpu_to_le32(1 << 2);
171 if (ctrl->ops->sqe_inline_size)
172 id->sgls |= cpu_to_le32(1 << 20);
174 strcpy(id->subnqn, ctrl->subsys->subsysnqn);
176 /* Max command capsule size is sqe + single page of in-capsule data */
177 id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
178 ctrl->ops->sqe_inline_size) / 16);
179 /* Max response capsule size is cqe */
180 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
182 id->msdbd = ctrl->ops->msdbd;
185 * Meh, we don't really support any power state. Fake up the same
186 * values that qemu does.
188 id->psd[0].max_power = cpu_to_le16(0x9c4);
189 id->psd[0].entry_lat = cpu_to_le32(0x10);
190 id->psd[0].exit_lat = cpu_to_le32(0x4);
192 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
196 nvmet_req_complete(req, status);
199 static void nvmet_execute_identify_ns(struct nvmet_req *req)
202 struct nvme_id_ns *id;
205 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
207 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
211 id = kzalloc(sizeof(*id), GFP_KERNEL);
213 status = NVME_SC_INTERNAL;
218 * nuse = ncap = nsze isn't aways true, but we have no way to find
219 * that out from the underlying device.
221 id->ncap = id->nuse = id->nsze =
222 cpu_to_le64(ns->size >> ns->blksize_shift);
225 * We just provide a single LBA format that matches what the
226 * underlying device reports.
232 * Our namespace might always be shared. Not just with other
233 * controllers, but also with any other user of the block device.
237 memcpy(&id->nguid, &ns->nguid, sizeof(uuid_le));
239 id->lbaf[0].ds = ns->blksize_shift;
241 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
245 nvmet_put_namespace(ns);
247 nvmet_req_complete(req, status);
250 static void nvmet_execute_identify_nslist(struct nvmet_req *req)
252 static const int buf_size = 4096;
253 struct nvmet_ctrl *ctrl = req->sq->ctrl;
255 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
260 list = kzalloc(buf_size, GFP_KERNEL);
262 status = NVME_SC_INTERNAL;
267 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
268 if (ns->nsid <= min_nsid)
270 list[i++] = cpu_to_le32(ns->nsid);
271 if (i == buf_size / sizeof(__le32))
276 status = nvmet_copy_to_sgl(req, 0, list, buf_size);
280 nvmet_req_complete(req, status);
284 * A "mimimum viable" abort implementation: the command is mandatory in the
285 * spec, but we are not required to do any useful work. We couldn't really
286 * do a useful abort, so don't bother even with waiting for the command
287 * to be exectuted and return immediately telling the command to abort
290 static void nvmet_execute_abort(struct nvmet_req *req)
292 nvmet_set_result(req, 1);
293 nvmet_req_complete(req, 0);
296 static void nvmet_execute_set_features(struct nvmet_req *req)
298 struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
299 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
304 switch (cdw10 & 0xf) {
305 case NVME_FEAT_NUM_QUEUES:
306 nvmet_set_result(req,
307 (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
310 val = le64_to_cpu(req->cmd->prop_set.value);
311 val32 = val & 0xffff;
312 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
313 nvmet_set_result(req, req->sq->ctrl->kato);
316 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
320 nvmet_req_complete(req, status);
323 static void nvmet_execute_get_features(struct nvmet_req *req)
325 struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
326 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
329 switch (cdw10 & 0xf) {
331 * These features are mandatory in the spec, but we don't
332 * have a useful way to implement them. We'll eventually
333 * need to come up with some fake values for these.
336 case NVME_FEAT_ARBITRATION:
338 case NVME_FEAT_POWER_MGMT:
340 case NVME_FEAT_TEMP_THRESH:
342 case NVME_FEAT_ERR_RECOVERY:
344 case NVME_FEAT_IRQ_COALESCE:
346 case NVME_FEAT_IRQ_CONFIG:
348 case NVME_FEAT_WRITE_ATOMIC:
350 case NVME_FEAT_ASYNC_EVENT:
353 case NVME_FEAT_VOLATILE_WC:
354 nvmet_set_result(req, 1);
356 case NVME_FEAT_NUM_QUEUES:
357 nvmet_set_result(req,
358 (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
361 nvmet_set_result(req, req->sq->ctrl->kato * 1000);
364 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
368 nvmet_req_complete(req, status);
371 static void nvmet_execute_async_event(struct nvmet_req *req)
373 struct nvmet_ctrl *ctrl = req->sq->ctrl;
375 mutex_lock(&ctrl->lock);
376 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
377 mutex_unlock(&ctrl->lock);
378 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
381 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
382 mutex_unlock(&ctrl->lock);
384 schedule_work(&ctrl->async_event_work);
387 static void nvmet_execute_keep_alive(struct nvmet_req *req)
389 struct nvmet_ctrl *ctrl = req->sq->ctrl;
391 pr_debug("ctrl %d update keep-alive timer for %d secs\n",
392 ctrl->cntlid, ctrl->kato);
394 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
395 nvmet_req_complete(req, 0);
398 int nvmet_parse_admin_cmd(struct nvmet_req *req)
400 struct nvme_command *cmd = req->cmd;
404 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) {
405 pr_err("nvmet: got admin cmd %d while CC.EN == 0\n",
407 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
409 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) {
410 pr_err("nvmet: got admin cmd %d while CSTS.RDY == 0\n",
412 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
415 switch (cmd->common.opcode) {
416 case nvme_admin_get_log_page:
417 req->data_len = nvmet_get_log_page_len(cmd);
419 switch (cmd->get_log_page.lid) {
423 req->execute = nvmet_execute_get_log_page;
427 case nvme_admin_identify:
428 req->data_len = 4096;
429 switch (le32_to_cpu(cmd->identify.cns)) {
431 req->execute = nvmet_execute_identify_ns;
434 req->execute = nvmet_execute_identify_ctrl;
437 req->execute = nvmet_execute_identify_nslist;
441 case nvme_admin_abort_cmd:
442 req->execute = nvmet_execute_abort;
445 case nvme_admin_set_features:
446 req->execute = nvmet_execute_set_features;
449 case nvme_admin_get_features:
450 req->execute = nvmet_execute_get_features;
453 case nvme_admin_async_event:
454 req->execute = nvmet_execute_async_event;
457 case nvme_admin_keep_alive:
458 req->execute = nvmet_execute_keep_alive;
463 pr_err("nvmet: unhandled cmd %d\n", cmd->common.opcode);
464 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;