1 // SPDX-License-Identifier: GPL-2.0
3 * NVMe admin command implementation.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/rculist.h>
9 #include <linux/part_stat.h>
11 #include <generated/utsrelease.h>
12 #include <linux/unaligned.h>
15 static void nvmet_execute_delete_sq(struct nvmet_req *req)
17 struct nvmet_ctrl *ctrl = req->sq->ctrl;
18 u16 sqid = le16_to_cpu(req->cmd->delete_queue.qid);
21 if (!nvmet_is_pci_ctrl(ctrl)) {
22 status = nvmet_report_invalid_opcode(req);
27 status = NVME_SC_QID_INVALID | NVME_STATUS_DNR;
31 status = nvmet_check_sqid(ctrl, sqid, false);
32 if (status != NVME_SC_SUCCESS)
35 status = ctrl->ops->delete_sq(ctrl, sqid);
38 nvmet_req_complete(req, status);
41 static void nvmet_execute_create_sq(struct nvmet_req *req)
43 struct nvmet_ctrl *ctrl = req->sq->ctrl;
44 struct nvme_command *cmd = req->cmd;
45 u16 sqid = le16_to_cpu(cmd->create_sq.sqid);
46 u16 cqid = le16_to_cpu(cmd->create_sq.cqid);
47 u16 sq_flags = le16_to_cpu(cmd->create_sq.sq_flags);
48 u16 qsize = le16_to_cpu(cmd->create_sq.qsize);
49 u64 prp1 = le64_to_cpu(cmd->create_sq.prp1);
52 if (!nvmet_is_pci_ctrl(ctrl)) {
53 status = nvmet_report_invalid_opcode(req);
58 status = NVME_SC_QID_INVALID | NVME_STATUS_DNR;
62 status = nvmet_check_sqid(ctrl, sqid, true);
63 if (status != NVME_SC_SUCCESS)
67 * Note: The NVMe specification allows multiple SQs to use the same CQ.
68 * However, the target code does not really support that. So for now,
69 * prevent this and fail the command if sqid and cqid are different.
71 if (!cqid || cqid != sqid) {
72 pr_err("SQ %u: Unsupported CQID %u\n", sqid, cqid);
73 status = NVME_SC_CQ_INVALID | NVME_STATUS_DNR;
77 if (!qsize || qsize > NVME_CAP_MQES(ctrl->cap)) {
78 status = NVME_SC_QUEUE_SIZE | NVME_STATUS_DNR;
82 status = ctrl->ops->create_sq(ctrl, sqid, sq_flags, qsize, prp1);
85 nvmet_req_complete(req, status);
88 static void nvmet_execute_delete_cq(struct nvmet_req *req)
90 struct nvmet_ctrl *ctrl = req->sq->ctrl;
91 u16 cqid = le16_to_cpu(req->cmd->delete_queue.qid);
94 if (!nvmet_is_pci_ctrl(ctrl)) {
95 status = nvmet_report_invalid_opcode(req);
100 status = NVME_SC_QID_INVALID | NVME_STATUS_DNR;
104 status = nvmet_check_cqid(ctrl, cqid);
105 if (status != NVME_SC_SUCCESS)
108 status = ctrl->ops->delete_cq(ctrl, cqid);
111 nvmet_req_complete(req, status);
114 static void nvmet_execute_create_cq(struct nvmet_req *req)
116 struct nvmet_ctrl *ctrl = req->sq->ctrl;
117 struct nvme_command *cmd = req->cmd;
118 u16 cqid = le16_to_cpu(cmd->create_cq.cqid);
119 u16 cq_flags = le16_to_cpu(cmd->create_cq.cq_flags);
120 u16 qsize = le16_to_cpu(cmd->create_cq.qsize);
121 u16 irq_vector = le16_to_cpu(cmd->create_cq.irq_vector);
122 u64 prp1 = le64_to_cpu(cmd->create_cq.prp1);
125 if (!nvmet_is_pci_ctrl(ctrl)) {
126 status = nvmet_report_invalid_opcode(req);
131 status = NVME_SC_QID_INVALID | NVME_STATUS_DNR;
135 status = nvmet_check_cqid(ctrl, cqid);
136 if (status != NVME_SC_SUCCESS)
139 if (!qsize || qsize > NVME_CAP_MQES(ctrl->cap)) {
140 status = NVME_SC_QUEUE_SIZE | NVME_STATUS_DNR;
144 status = ctrl->ops->create_cq(ctrl, cqid, cq_flags, qsize,
148 nvmet_req_complete(req, status);
151 u32 nvmet_get_log_page_len(struct nvme_command *cmd)
153 u32 len = le16_to_cpu(cmd->get_log_page.numdu);
156 len += le16_to_cpu(cmd->get_log_page.numdl);
157 /* NUMD is a 0's based value */
164 static u32 nvmet_feat_data_len(struct nvmet_req *req, u32 cdw10)
166 switch (cdw10 & 0xff) {
167 case NVME_FEAT_HOST_ID:
168 return sizeof(req->sq->ctrl->hostid);
174 u64 nvmet_get_log_page_offset(struct nvme_command *cmd)
176 return le64_to_cpu(cmd->get_log_page.lpo);
179 static void nvmet_execute_get_log_page_noop(struct nvmet_req *req)
181 nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->transfer_len));
184 static void nvmet_execute_get_log_page_error(struct nvmet_req *req)
186 struct nvmet_ctrl *ctrl = req->sq->ctrl;
192 spin_lock_irqsave(&ctrl->error_lock, flags);
193 slot = ctrl->err_counter % NVMET_ERROR_LOG_SLOTS;
195 for (i = 0; i < NVMET_ERROR_LOG_SLOTS; i++) {
196 if (nvmet_copy_to_sgl(req, offset, &ctrl->slots[slot],
197 sizeof(struct nvme_error_slot)))
201 slot = NVMET_ERROR_LOG_SLOTS - 1;
204 offset += sizeof(struct nvme_error_slot);
206 spin_unlock_irqrestore(&ctrl->error_lock, flags);
207 nvmet_req_complete(req, 0);
210 static void nvmet_execute_get_supported_log_pages(struct nvmet_req *req)
212 struct nvme_supported_log *logs;
215 logs = kzalloc(sizeof(*logs), GFP_KERNEL);
217 status = NVME_SC_INTERNAL;
221 logs->lids[NVME_LOG_SUPPORTED] = cpu_to_le32(NVME_LIDS_LSUPP);
222 logs->lids[NVME_LOG_ERROR] = cpu_to_le32(NVME_LIDS_LSUPP);
223 logs->lids[NVME_LOG_SMART] = cpu_to_le32(NVME_LIDS_LSUPP);
224 logs->lids[NVME_LOG_FW_SLOT] = cpu_to_le32(NVME_LIDS_LSUPP);
225 logs->lids[NVME_LOG_CHANGED_NS] = cpu_to_le32(NVME_LIDS_LSUPP);
226 logs->lids[NVME_LOG_CMD_EFFECTS] = cpu_to_le32(NVME_LIDS_LSUPP);
227 logs->lids[NVME_LOG_ENDURANCE_GROUP] = cpu_to_le32(NVME_LIDS_LSUPP);
228 logs->lids[NVME_LOG_ANA] = cpu_to_le32(NVME_LIDS_LSUPP);
229 logs->lids[NVME_LOG_FEATURES] = cpu_to_le32(NVME_LIDS_LSUPP);
230 logs->lids[NVME_LOG_RMI] = cpu_to_le32(NVME_LIDS_LSUPP);
231 logs->lids[NVME_LOG_RESERVATION] = cpu_to_le32(NVME_LIDS_LSUPP);
233 status = nvmet_copy_to_sgl(req, 0, logs, sizeof(*logs));
236 nvmet_req_complete(req, status);
239 static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
240 struct nvme_smart_log *slog)
242 u64 host_reads, host_writes, data_units_read, data_units_written;
245 status = nvmet_req_find_ns(req);
249 /* we don't have the right data for file backed ns */
251 return NVME_SC_SUCCESS;
253 host_reads = part_stat_read(req->ns->bdev, ios[READ]);
255 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
256 host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
258 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
260 put_unaligned_le64(host_reads, &slog->host_reads[0]);
261 put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
262 put_unaligned_le64(host_writes, &slog->host_writes[0]);
263 put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
265 return NVME_SC_SUCCESS;
268 static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
269 struct nvme_smart_log *slog)
271 u64 host_reads = 0, host_writes = 0;
272 u64 data_units_read = 0, data_units_written = 0;
274 struct nvmet_ctrl *ctrl;
277 ctrl = req->sq->ctrl;
278 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
279 /* we don't have the right data for file backed ns */
282 host_reads += part_stat_read(ns->bdev, ios[READ]);
283 data_units_read += DIV_ROUND_UP(
284 part_stat_read(ns->bdev, sectors[READ]), 1000);
285 host_writes += part_stat_read(ns->bdev, ios[WRITE]);
286 data_units_written += DIV_ROUND_UP(
287 part_stat_read(ns->bdev, sectors[WRITE]), 1000);
290 put_unaligned_le64(host_reads, &slog->host_reads[0]);
291 put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
292 put_unaligned_le64(host_writes, &slog->host_writes[0]);
293 put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
295 return NVME_SC_SUCCESS;
298 static void nvmet_execute_get_log_page_rmi(struct nvmet_req *req)
300 struct nvme_rotational_media_log *log;
301 struct gendisk *disk;
304 req->cmd->common.nsid = cpu_to_le32(le16_to_cpu(
305 req->cmd->get_log_page.lsi));
306 status = nvmet_req_find_ns(req);
310 if (!req->ns->bdev || bdev_nonrot(req->ns->bdev)) {
311 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
315 if (req->transfer_len != sizeof(*log)) {
316 status = NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
320 log = kzalloc(sizeof(*log), GFP_KERNEL);
324 log->endgid = req->cmd->get_log_page.lsi;
325 disk = req->ns->bdev->bd_disk;
326 if (disk && disk->ia_ranges)
327 log->numa = cpu_to_le16(disk->ia_ranges->nr_ia_ranges);
329 log->numa = cpu_to_le16(1);
331 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
334 nvmet_req_complete(req, status);
337 static void nvmet_execute_get_log_page_smart(struct nvmet_req *req)
339 struct nvme_smart_log *log;
340 u16 status = NVME_SC_INTERNAL;
343 if (req->transfer_len != sizeof(*log))
346 log = kzalloc(sizeof(*log), GFP_KERNEL);
350 if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL))
351 status = nvmet_get_smart_log_all(req, log);
353 status = nvmet_get_smart_log_nsid(req, log);
357 spin_lock_irqsave(&req->sq->ctrl->error_lock, flags);
358 put_unaligned_le64(req->sq->ctrl->err_counter,
359 &log->num_err_log_entries);
360 spin_unlock_irqrestore(&req->sq->ctrl->error_lock, flags);
362 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
366 nvmet_req_complete(req, status);
369 static void nvmet_get_cmd_effects_admin(struct nvmet_ctrl *ctrl,
370 struct nvme_effects_log *log)
372 /* For a PCI target controller, advertize support for the . */
373 if (nvmet_is_pci_ctrl(ctrl)) {
374 log->acs[nvme_admin_delete_sq] =
375 log->acs[nvme_admin_create_sq] =
376 log->acs[nvme_admin_delete_cq] =
377 log->acs[nvme_admin_create_cq] =
378 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
381 log->acs[nvme_admin_get_log_page] =
382 log->acs[nvme_admin_identify] =
383 log->acs[nvme_admin_abort_cmd] =
384 log->acs[nvme_admin_set_features] =
385 log->acs[nvme_admin_get_features] =
386 log->acs[nvme_admin_async_event] =
387 log->acs[nvme_admin_keep_alive] =
388 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
391 static void nvmet_get_cmd_effects_nvm(struct nvme_effects_log *log)
393 log->iocs[nvme_cmd_read] =
394 log->iocs[nvme_cmd_flush] =
395 log->iocs[nvme_cmd_dsm] =
396 log->iocs[nvme_cmd_resv_acquire] =
397 log->iocs[nvme_cmd_resv_register] =
398 log->iocs[nvme_cmd_resv_release] =
399 log->iocs[nvme_cmd_resv_report] =
400 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
401 log->iocs[nvme_cmd_write] =
402 log->iocs[nvme_cmd_write_zeroes] =
403 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC);
406 static void nvmet_get_cmd_effects_zns(struct nvme_effects_log *log)
408 log->iocs[nvme_cmd_zone_append] =
409 log->iocs[nvme_cmd_zone_mgmt_send] =
410 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC);
411 log->iocs[nvme_cmd_zone_mgmt_recv] =
412 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
415 static void nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req *req)
417 struct nvmet_ctrl *ctrl = req->sq->ctrl;
418 struct nvme_effects_log *log;
419 u16 status = NVME_SC_SUCCESS;
421 log = kzalloc(sizeof(*log), GFP_KERNEL);
423 status = NVME_SC_INTERNAL;
427 switch (req->cmd->get_log_page.csi) {
429 nvmet_get_cmd_effects_admin(ctrl, log);
430 nvmet_get_cmd_effects_nvm(log);
433 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
434 status = NVME_SC_INVALID_IO_CMD_SET;
437 nvmet_get_cmd_effects_admin(ctrl, log);
438 nvmet_get_cmd_effects_nvm(log);
439 nvmet_get_cmd_effects_zns(log);
442 status = NVME_SC_INVALID_LOG_PAGE;
446 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
450 nvmet_req_complete(req, status);
453 static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req)
455 struct nvmet_ctrl *ctrl = req->sq->ctrl;
456 u16 status = NVME_SC_INTERNAL;
459 if (req->transfer_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32))
462 mutex_lock(&ctrl->lock);
463 if (ctrl->nr_changed_ns == U32_MAX)
464 len = sizeof(__le32);
466 len = ctrl->nr_changed_ns * sizeof(__le32);
467 status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len);
469 status = nvmet_zero_sgl(req, len, req->transfer_len - len);
470 ctrl->nr_changed_ns = 0;
471 nvmet_clear_aen_bit(req, NVME_AEN_BIT_NS_ATTR);
472 mutex_unlock(&ctrl->lock);
474 nvmet_req_complete(req, status);
477 static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
478 struct nvme_ana_group_desc *desc)
480 struct nvmet_ctrl *ctrl = req->sq->ctrl;
485 if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) {
486 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
487 if (ns->anagrpid == grpid)
488 desc->nsids[count++] = cpu_to_le32(ns->nsid);
492 desc->grpid = cpu_to_le32(grpid);
493 desc->nnsids = cpu_to_le32(count);
494 desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
495 desc->state = req->port->ana_state[grpid];
496 memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
497 return struct_size(desc, nsids, count);
500 static void nvmet_execute_get_log_page_endgrp(struct nvmet_req *req)
502 u64 host_reads, host_writes, data_units_read, data_units_written;
503 struct nvme_endurance_group_log *log;
507 * The target driver emulates each endurance group as its own
508 * namespace, reusing the nsid as the endurance group identifier.
510 req->cmd->common.nsid = cpu_to_le32(le16_to_cpu(
511 req->cmd->get_log_page.lsi));
512 status = nvmet_req_find_ns(req);
516 log = kzalloc(sizeof(*log), GFP_KERNEL);
518 status = NVME_SC_INTERNAL;
525 host_reads = part_stat_read(req->ns->bdev, ios[READ]);
527 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
528 host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
530 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
532 put_unaligned_le64(host_reads, &log->hrc[0]);
533 put_unaligned_le64(data_units_read, &log->dur[0]);
534 put_unaligned_le64(host_writes, &log->hwc[0]);
535 put_unaligned_le64(data_units_written, &log->duw[0]);
537 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
540 nvmet_req_complete(req, status);
543 static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
545 struct nvme_ana_rsp_hdr hdr = { 0, };
546 struct nvme_ana_group_desc *desc;
547 size_t offset = sizeof(struct nvme_ana_rsp_hdr); /* start beyond hdr */
553 status = NVME_SC_INTERNAL;
554 desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
559 down_read(&nvmet_ana_sem);
560 for (grpid = 1; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
561 if (!nvmet_ana_group_enabled[grpid])
563 len = nvmet_format_ana_group(req, grpid, desc);
564 status = nvmet_copy_to_sgl(req, offset, desc, len);
570 for ( ; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
571 if (nvmet_ana_group_enabled[grpid])
575 hdr.chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
576 hdr.ngrps = cpu_to_le16(ngrps);
577 nvmet_clear_aen_bit(req, NVME_AEN_BIT_ANA_CHANGE);
578 up_read(&nvmet_ana_sem);
582 /* copy the header last once we know the number of groups */
583 status = nvmet_copy_to_sgl(req, 0, &hdr, sizeof(hdr));
585 nvmet_req_complete(req, status);
588 static void nvmet_execute_get_log_page_features(struct nvmet_req *req)
590 struct nvme_supported_features_log *features;
593 features = kzalloc(sizeof(*features), GFP_KERNEL);
595 status = NVME_SC_INTERNAL;
599 features->fis[NVME_FEAT_NUM_QUEUES] =
600 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
601 features->fis[NVME_FEAT_KATO] =
602 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
603 features->fis[NVME_FEAT_ASYNC_EVENT] =
604 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
605 features->fis[NVME_FEAT_HOST_ID] =
606 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
607 features->fis[NVME_FEAT_WRITE_PROTECT] =
608 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_NSCPE);
609 features->fis[NVME_FEAT_RESV_MASK] =
610 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_NSCPE);
612 status = nvmet_copy_to_sgl(req, 0, features, sizeof(*features));
615 nvmet_req_complete(req, status);
618 static void nvmet_execute_get_log_page(struct nvmet_req *req)
620 if (!nvmet_check_transfer_len(req, nvmet_get_log_page_len(req->cmd)))
623 switch (req->cmd->get_log_page.lid) {
624 case NVME_LOG_SUPPORTED:
625 return nvmet_execute_get_supported_log_pages(req);
627 return nvmet_execute_get_log_page_error(req);
629 return nvmet_execute_get_log_page_smart(req);
630 case NVME_LOG_FW_SLOT:
632 * We only support a single firmware slot which always is
633 * active, so we can zero out the whole firmware slot log and
634 * still claim to fully implement this mandatory log page.
636 return nvmet_execute_get_log_page_noop(req);
637 case NVME_LOG_CHANGED_NS:
638 return nvmet_execute_get_log_changed_ns(req);
639 case NVME_LOG_CMD_EFFECTS:
640 return nvmet_execute_get_log_cmd_effects_ns(req);
641 case NVME_LOG_ENDURANCE_GROUP:
642 return nvmet_execute_get_log_page_endgrp(req);
644 return nvmet_execute_get_log_page_ana(req);
645 case NVME_LOG_FEATURES:
646 return nvmet_execute_get_log_page_features(req);
648 return nvmet_execute_get_log_page_rmi(req);
649 case NVME_LOG_RESERVATION:
650 return nvmet_execute_get_log_page_resv(req);
652 pr_debug("unhandled lid %d on qid %d\n",
653 req->cmd->get_log_page.lid, req->sq->qid);
654 req->error_loc = offsetof(struct nvme_get_log_page_command, lid);
655 nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_STATUS_DNR);
658 static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
660 struct nvmet_ctrl *ctrl = req->sq->ctrl;
661 struct nvmet_subsys *subsys = ctrl->subsys;
662 struct nvme_id_ctrl *id;
663 u32 cmd_capsule_size, ctratt;
666 if (!subsys->subsys_discovered) {
667 mutex_lock(&subsys->lock);
668 subsys->subsys_discovered = true;
669 mutex_unlock(&subsys->lock);
672 id = kzalloc(sizeof(*id), GFP_KERNEL);
674 status = NVME_SC_INTERNAL;
678 id->vid = cpu_to_le16(subsys->vendor_id);
679 id->ssvid = cpu_to_le16(subsys->subsys_vendor_id);
681 memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
682 memcpy_and_pad(id->mn, sizeof(id->mn), subsys->model_number,
683 strlen(subsys->model_number), ' ');
684 memcpy_and_pad(id->fr, sizeof(id->fr),
685 subsys->firmware_rev, strlen(subsys->firmware_rev), ' ');
687 put_unaligned_le24(subsys->ieee_oui, id->ieee);
691 if (nvmet_is_disc_subsys(ctrl->subsys))
692 id->cntrltype = NVME_CTRL_DISC;
694 id->cntrltype = NVME_CTRL_IO;
696 /* we support multiple ports, multiples hosts and ANA: */
697 id->cmic = NVME_CTRL_CMIC_MULTI_PORT | NVME_CTRL_CMIC_MULTI_CTRL |
700 /* Limit MDTS according to transport capability */
701 if (ctrl->ops->get_mdts)
702 id->mdts = ctrl->ops->get_mdts(ctrl);
706 id->cntlid = cpu_to_le16(ctrl->cntlid);
707 id->ver = cpu_to_le32(ctrl->subsys->ver);
709 /* XXX: figure out what to do about RTD3R/RTD3 */
710 id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
711 ctratt = NVME_CTRL_ATTR_HID_128_BIT | NVME_CTRL_ATTR_TBKAS;
712 if (nvmet_is_pci_ctrl(ctrl))
713 ctratt |= NVME_CTRL_ATTR_RHII;
714 id->ctratt = cpu_to_le32(ctratt);
719 * We don't really have a practical limit on the number of abort
720 * comands. But we don't do anything useful for abort either, so
721 * no point in allowing more abort commands than the spec requires.
725 id->aerl = NVMET_ASYNC_EVENTS - 1;
727 /* first slot is read-only, only one slot supported */
728 id->frmw = (1 << 0) | (1 << 1);
729 id->lpa = (1 << 0) | (1 << 1) | (1 << 2);
730 id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
733 /* We support keep-alive timeout in granularity of seconds */
734 id->kas = cpu_to_le16(NVMET_KAS);
736 id->sqes = (0x6 << 4) | 0x6;
737 id->cqes = (0x4 << 4) | 0x4;
739 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
740 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD(ctrl));
742 id->nn = cpu_to_le32(NVMET_MAX_NAMESPACES);
743 id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
744 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
745 NVME_CTRL_ONCS_WRITE_ZEROES |
746 NVME_CTRL_ONCS_RESERVATIONS);
748 /* XXX: don't report vwc if the underlying device is write through */
749 id->vwc = NVME_CTRL_VWC_PRESENT;
752 * We can't support atomic writes bigger than a LBA without support
753 * from the backend device.
758 /* we always support SGLs */
759 id->sgls = cpu_to_le32(NVME_CTRL_SGLS_BYTE_ALIGNED);
760 if (ctrl->ops->flags & NVMF_KEYED_SGLS)
761 id->sgls |= cpu_to_le32(NVME_CTRL_SGLS_KSDBDS);
762 if (req->port->inline_data_size)
763 id->sgls |= cpu_to_le32(NVME_CTRL_SGLS_SAOS);
765 strscpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
768 * Max command capsule size is sqe + in-capsule data size.
769 * Disable in-capsule data for Metadata capable controllers.
771 cmd_capsule_size = sizeof(struct nvme_command);
772 if (!ctrl->pi_support)
773 cmd_capsule_size += req->port->inline_data_size;
774 id->ioccsz = cpu_to_le32(cmd_capsule_size / 16);
776 /* Max response capsule size is cqe */
777 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
779 id->msdbd = ctrl->ops->msdbd;
782 * Endurance group identifier is 16 bits, so we can't let namespaces
783 * overflow that since we reuse the nsid
785 BUILD_BUG_ON(NVMET_MAX_NAMESPACES > USHRT_MAX);
786 id->endgidmax = cpu_to_le16(NVMET_MAX_NAMESPACES);
788 id->anacap = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
789 id->anatt = 10; /* random value */
790 id->anagrpmax = cpu_to_le32(NVMET_MAX_ANAGRPS);
791 id->nanagrpid = cpu_to_le32(NVMET_MAX_ANAGRPS);
794 * Meh, we don't really support any power state. Fake up the same
795 * values that qemu does.
797 id->psd[0].max_power = cpu_to_le16(0x9c4);
798 id->psd[0].entry_lat = cpu_to_le32(0x10);
799 id->psd[0].exit_lat = cpu_to_le32(0x4);
801 id->nwpc = 1 << 0; /* write protect and no write protect */
803 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
807 nvmet_req_complete(req, status);
810 static void nvmet_execute_identify_ns(struct nvmet_req *req)
812 struct nvme_id_ns *id;
815 if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
816 req->error_loc = offsetof(struct nvme_identify, nsid);
817 status = NVME_SC_INVALID_NS | NVME_STATUS_DNR;
821 id = kzalloc(sizeof(*id), GFP_KERNEL);
823 status = NVME_SC_INTERNAL;
827 /* return an all zeroed buffer if we can't find an active namespace */
828 status = nvmet_req_find_ns(req);
834 if (nvmet_ns_revalidate(req->ns)) {
835 mutex_lock(&req->ns->subsys->lock);
836 nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
837 mutex_unlock(&req->ns->subsys->lock);
841 * nuse = ncap = nsze isn't always true, but we have no way to find
842 * that out from the underlying device.
844 id->ncap = id->nsze =
845 cpu_to_le64(req->ns->size >> req->ns->blksize_shift);
846 switch (req->port->ana_state[req->ns->anagrpid]) {
847 case NVME_ANA_INACCESSIBLE:
848 case NVME_ANA_PERSISTENT_LOSS:
856 nvmet_bdev_set_limits(req->ns->bdev, id);
859 * We just provide a single LBA format that matches what the
860 * underlying device reports.
866 * Our namespace might always be shared. Not just with other
867 * controllers, but also with any other user of the block device.
869 id->nmic = NVME_NS_NMIC_SHARED;
870 id->anagrpid = cpu_to_le32(req->ns->anagrpid);
872 if (req->ns->pr.enable)
873 id->rescap = NVME_PR_SUPPORT_WRITE_EXCLUSIVE |
874 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS |
875 NVME_PR_SUPPORT_WRITE_EXCLUSIVE_REG_ONLY |
876 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_REG_ONLY |
877 NVME_PR_SUPPORT_WRITE_EXCLUSIVE_ALL_REGS |
878 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_ALL_REGS |
879 NVME_PR_SUPPORT_IEKEY_VER_1_3_DEF;
882 * Since we don't know any better, every namespace is its own endurance
885 id->endgid = cpu_to_le16(req->ns->nsid);
887 memcpy(&id->nguid, &req->ns->nguid, sizeof(id->nguid));
889 id->lbaf[0].ds = req->ns->blksize_shift;
891 if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns)) {
892 id->dpc = NVME_NS_DPC_PI_FIRST | NVME_NS_DPC_PI_LAST |
893 NVME_NS_DPC_PI_TYPE1 | NVME_NS_DPC_PI_TYPE2 |
894 NVME_NS_DPC_PI_TYPE3;
895 id->mc = NVME_MC_EXTENDED_LBA;
896 id->dps = req->ns->pi_type;
897 id->flbas = NVME_NS_FLBAS_META_EXT;
898 id->lbaf[0].ms = cpu_to_le16(req->ns->metadata_size);
901 if (req->ns->readonly)
902 id->nsattr |= NVME_NS_ATTR_RO;
905 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
909 nvmet_req_complete(req, status);
912 static void nvmet_execute_identify_endgrp_list(struct nvmet_req *req)
914 u16 min_endgid = le16_to_cpu(req->cmd->identify.cnssid);
915 static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
916 struct nvmet_ctrl *ctrl = req->sq->ctrl;
923 list = kzalloc(buf_size, GFP_KERNEL);
925 status = NVME_SC_INTERNAL;
929 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
930 if (ns->nsid <= min_endgid)
933 list[i++] = cpu_to_le16(ns->nsid);
934 if (i == buf_size / sizeof(__le16))
938 list[0] = cpu_to_le16(i - 1);
939 status = nvmet_copy_to_sgl(req, 0, list, buf_size);
942 nvmet_req_complete(req, status);
945 static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css)
947 static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
948 struct nvmet_ctrl *ctrl = req->sq->ctrl;
951 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
957 * NSID values 0xFFFFFFFE and NVME_NSID_ALL are invalid
958 * See NVMe Base Specification, Active Namespace ID list (CNS 02h).
960 if (min_nsid == 0xFFFFFFFE || min_nsid == NVME_NSID_ALL) {
961 req->error_loc = offsetof(struct nvme_identify, nsid);
962 status = NVME_SC_INVALID_NS | NVME_STATUS_DNR;
966 list = kzalloc(buf_size, GFP_KERNEL);
968 status = NVME_SC_INTERNAL;
972 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
973 if (ns->nsid <= min_nsid)
975 if (match_css && req->ns->csi != req->cmd->identify.csi)
977 list[i++] = cpu_to_le32(ns->nsid);
978 if (i == buf_size / sizeof(__le32))
982 status = nvmet_copy_to_sgl(req, 0, list, buf_size);
986 nvmet_req_complete(req, status);
989 static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
990 void *id, off_t *off)
992 struct nvme_ns_id_desc desc = {
998 status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
1001 *off += sizeof(desc);
1003 status = nvmet_copy_to_sgl(req, *off, id, len);
1011 static void nvmet_execute_identify_desclist(struct nvmet_req *req)
1016 status = nvmet_req_find_ns(req);
1020 if (memchr_inv(&req->ns->uuid, 0, sizeof(req->ns->uuid))) {
1021 status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
1023 &req->ns->uuid, &off);
1027 if (memchr_inv(req->ns->nguid, 0, sizeof(req->ns->nguid))) {
1028 status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
1029 NVME_NIDT_NGUID_LEN,
1030 &req->ns->nguid, &off);
1035 status = nvmet_copy_ns_identifier(req, NVME_NIDT_CSI,
1037 &req->ns->csi, &off);
1041 if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
1042 off) != NVME_IDENTIFY_DATA_SIZE - off)
1043 status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
1046 nvmet_req_complete(req, status);
1049 static void nvmet_execute_identify_ctrl_nvm(struct nvmet_req *req)
1051 /* Not supported: return zeroes */
1052 nvmet_req_complete(req,
1053 nvmet_zero_sgl(req, 0, sizeof(struct nvme_id_ctrl_nvm)));
1056 static void nvme_execute_identify_ns_nvm(struct nvmet_req *req)
1059 struct nvme_id_ns_nvm *id;
1061 status = nvmet_req_find_ns(req);
1065 id = kzalloc(sizeof(*id), GFP_KERNEL);
1067 status = NVME_SC_INTERNAL;
1070 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
1073 nvmet_req_complete(req, status);
1076 static void nvmet_execute_id_cs_indep(struct nvmet_req *req)
1078 struct nvme_id_ns_cs_indep *id;
1081 status = nvmet_req_find_ns(req);
1085 id = kzalloc(sizeof(*id), GFP_KERNEL);
1087 status = NVME_SC_INTERNAL;
1091 id->nstat = NVME_NSTAT_NRDY;
1092 id->anagrpid = cpu_to_le32(req->ns->anagrpid);
1093 id->nmic = NVME_NS_NMIC_SHARED;
1094 if (req->ns->readonly)
1095 id->nsattr |= NVME_NS_ATTR_RO;
1096 if (req->ns->bdev && !bdev_nonrot(req->ns->bdev))
1097 id->nsfeat |= NVME_NS_ROTATIONAL;
1099 * We need flush command to flush the file's metadata,
1100 * so report supporting vwc if backend is file, even
1101 * though buffered_io is disable.
1103 if (req->ns->bdev && !bdev_write_cache(req->ns->bdev))
1104 id->nsfeat |= NVME_NS_VWC_NOT_PRESENT;
1106 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
1109 nvmet_req_complete(req, status);
1112 static void nvmet_execute_identify(struct nvmet_req *req)
1114 if (!nvmet_check_transfer_len(req, NVME_IDENTIFY_DATA_SIZE))
1117 switch (req->cmd->identify.cns) {
1118 case NVME_ID_CNS_NS:
1119 nvmet_execute_identify_ns(req);
1121 case NVME_ID_CNS_CTRL:
1122 nvmet_execute_identify_ctrl(req);
1124 case NVME_ID_CNS_NS_ACTIVE_LIST:
1125 nvmet_execute_identify_nslist(req, false);
1127 case NVME_ID_CNS_NS_DESC_LIST:
1128 nvmet_execute_identify_desclist(req);
1130 case NVME_ID_CNS_CS_NS:
1131 switch (req->cmd->identify.csi) {
1133 nvme_execute_identify_ns_nvm(req);
1136 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
1137 nvmet_execute_identify_ns_zns(req);
1143 case NVME_ID_CNS_CS_CTRL:
1144 switch (req->cmd->identify.csi) {
1146 nvmet_execute_identify_ctrl_nvm(req);
1149 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
1150 nvmet_execute_identify_ctrl_zns(req);
1156 case NVME_ID_CNS_NS_ACTIVE_LIST_CS:
1157 nvmet_execute_identify_nslist(req, true);
1159 case NVME_ID_CNS_NS_CS_INDEP:
1160 nvmet_execute_id_cs_indep(req);
1162 case NVME_ID_CNS_ENDGRP_LIST:
1163 nvmet_execute_identify_endgrp_list(req);
1167 pr_debug("unhandled identify cns %d on qid %d\n",
1168 req->cmd->identify.cns, req->sq->qid);
1169 req->error_loc = offsetof(struct nvme_identify, cns);
1170 nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_STATUS_DNR);
1174 * A "minimum viable" abort implementation: the command is mandatory in the
1175 * spec, but we are not required to do any useful work. We couldn't really
1176 * do a useful abort, so don't bother even with waiting for the command
1177 * to be exectuted and return immediately telling the command to abort
1180 static void nvmet_execute_abort(struct nvmet_req *req)
1182 if (!nvmet_check_transfer_len(req, 0))
1184 nvmet_set_result(req, 1);
1185 nvmet_req_complete(req, 0);
1188 static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
1193 status = nvmet_file_flush(req);
1195 status = nvmet_bdev_flush(req);
1198 pr_err("write protect flush failed nsid: %u\n", req->ns->nsid);
1202 static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
1204 u32 write_protect = le32_to_cpu(req->cmd->common.cdw11);
1205 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1208 status = nvmet_req_find_ns(req);
1212 mutex_lock(&subsys->lock);
1213 switch (write_protect) {
1214 case NVME_NS_WRITE_PROTECT:
1215 req->ns->readonly = true;
1216 status = nvmet_write_protect_flush_sync(req);
1218 req->ns->readonly = false;
1220 case NVME_NS_NO_WRITE_PROTECT:
1221 req->ns->readonly = false;
1229 nvmet_ns_changed(subsys, req->ns->nsid);
1230 mutex_unlock(&subsys->lock);
1234 u16 nvmet_set_feat_kato(struct nvmet_req *req)
1236 u32 val32 = le32_to_cpu(req->cmd->common.cdw11);
1238 nvmet_stop_keep_alive_timer(req->sq->ctrl);
1239 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
1240 nvmet_start_keep_alive_timer(req->sq->ctrl);
1242 nvmet_set_result(req, req->sq->ctrl->kato);
1247 u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask)
1249 u32 val32 = le32_to_cpu(req->cmd->common.cdw11);
1251 if (val32 & ~mask) {
1252 req->error_loc = offsetof(struct nvme_common_command, cdw11);
1253 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1256 WRITE_ONCE(req->sq->ctrl->aen_enabled, val32);
1257 nvmet_set_result(req, val32);
1262 static u16 nvmet_set_feat_host_id(struct nvmet_req *req)
1264 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1266 if (!nvmet_is_pci_ctrl(ctrl))
1267 return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR;
1270 * The NVMe base specifications v2.1 recommends supporting 128-bits host
1271 * IDs (section 5.1.25.1.28.1). However, that same section also says
1272 * that "The controller may support a 64-bit Host Identifier and/or an
1273 * extended 128-bit Host Identifier". So simplify this support and do
1274 * not support 64-bits host IDs to avoid needing to check that all
1275 * controllers associated with the same subsystem all use the same host
1278 if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) {
1279 req->error_loc = offsetof(struct nvme_common_command, cdw11);
1280 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1283 return nvmet_copy_from_sgl(req, 0, &req->sq->ctrl->hostid,
1284 sizeof(req->sq->ctrl->hostid));
1287 static u16 nvmet_set_feat_irq_coalesce(struct nvmet_req *req)
1289 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1290 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1291 struct nvmet_feat_irq_coalesce irqc = {
1292 .time = (cdw11 >> 8) & 0xff,
1293 .thr = cdw11 & 0xff,
1297 * This feature is not supported for fabrics controllers and mandatory
1298 * for PCI controllers.
1300 if (!nvmet_is_pci_ctrl(ctrl)) {
1301 req->error_loc = offsetof(struct nvme_common_command, cdw10);
1302 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1305 return ctrl->ops->set_feature(ctrl, NVME_FEAT_IRQ_COALESCE, &irqc);
1308 static u16 nvmet_set_feat_irq_config(struct nvmet_req *req)
1310 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1311 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1312 struct nvmet_feat_irq_config irqcfg = {
1313 .iv = cdw11 & 0xffff,
1314 .cd = (cdw11 >> 16) & 0x1,
1318 * This feature is not supported for fabrics controllers and mandatory
1319 * for PCI controllers.
1321 if (!nvmet_is_pci_ctrl(ctrl)) {
1322 req->error_loc = offsetof(struct nvme_common_command, cdw10);
1323 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1326 return ctrl->ops->set_feature(ctrl, NVME_FEAT_IRQ_CONFIG, &irqcfg);
1329 static u16 nvmet_set_feat_arbitration(struct nvmet_req *req)
1331 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1332 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1333 struct nvmet_feat_arbitration arb = {
1334 .hpw = (cdw11 >> 24) & 0xff,
1335 .mpw = (cdw11 >> 16) & 0xff,
1336 .lpw = (cdw11 >> 8) & 0xff,
1340 if (!ctrl->ops->set_feature) {
1341 req->error_loc = offsetof(struct nvme_common_command, cdw10);
1342 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1345 return ctrl->ops->set_feature(ctrl, NVME_FEAT_ARBITRATION, &arb);
1348 void nvmet_execute_set_features(struct nvmet_req *req)
1350 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1351 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
1352 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1357 if (!nvmet_check_data_len_lte(req, 0))
1360 switch (cdw10 & 0xff) {
1361 case NVME_FEAT_ARBITRATION:
1362 status = nvmet_set_feat_arbitration(req);
1364 case NVME_FEAT_NUM_QUEUES:
1365 ncqr = (cdw11 >> 16) & 0xffff;
1366 nsqr = cdw11 & 0xffff;
1367 if (ncqr == 0xffff || nsqr == 0xffff) {
1368 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1371 nvmet_set_result(req,
1372 (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
1374 case NVME_FEAT_IRQ_COALESCE:
1375 status = nvmet_set_feat_irq_coalesce(req);
1377 case NVME_FEAT_IRQ_CONFIG:
1378 status = nvmet_set_feat_irq_config(req);
1380 case NVME_FEAT_KATO:
1381 status = nvmet_set_feat_kato(req);
1383 case NVME_FEAT_ASYNC_EVENT:
1384 status = nvmet_set_feat_async_event(req, NVMET_AEN_CFG_ALL);
1386 case NVME_FEAT_HOST_ID:
1387 status = nvmet_set_feat_host_id(req);
1389 case NVME_FEAT_WRITE_PROTECT:
1390 status = nvmet_set_feat_write_protect(req);
1392 case NVME_FEAT_RESV_MASK:
1393 status = nvmet_set_feat_resv_notif_mask(req, cdw11);
1396 req->error_loc = offsetof(struct nvme_common_command, cdw10);
1397 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1401 nvmet_req_complete(req, status);
1404 static u16 nvmet_get_feat_write_protect(struct nvmet_req *req)
1406 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1409 result = nvmet_req_find_ns(req);
1413 mutex_lock(&subsys->lock);
1414 if (req->ns->readonly == true)
1415 result = NVME_NS_WRITE_PROTECT;
1417 result = NVME_NS_NO_WRITE_PROTECT;
1418 nvmet_set_result(req, result);
1419 mutex_unlock(&subsys->lock);
1424 static u16 nvmet_get_feat_irq_coalesce(struct nvmet_req *req)
1426 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1427 struct nvmet_feat_irq_coalesce irqc = { };
1431 * This feature is not supported for fabrics controllers and mandatory
1432 * for PCI controllers.
1434 if (!nvmet_is_pci_ctrl(ctrl)) {
1435 req->error_loc = offsetof(struct nvme_common_command, cdw10);
1436 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1439 status = ctrl->ops->get_feature(ctrl, NVME_FEAT_IRQ_COALESCE, &irqc);
1440 if (status != NVME_SC_SUCCESS)
1443 nvmet_set_result(req, ((u32)irqc.time << 8) | (u32)irqc.thr);
1445 return NVME_SC_SUCCESS;
1448 static u16 nvmet_get_feat_irq_config(struct nvmet_req *req)
1450 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1451 u32 iv = le32_to_cpu(req->cmd->common.cdw11) & 0xffff;
1452 struct nvmet_feat_irq_config irqcfg = { .iv = iv };
1456 * This feature is not supported for fabrics controllers and mandatory
1457 * for PCI controllers.
1459 if (!nvmet_is_pci_ctrl(ctrl)) {
1460 req->error_loc = offsetof(struct nvme_common_command, cdw10);
1461 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1464 status = ctrl->ops->get_feature(ctrl, NVME_FEAT_IRQ_CONFIG, &irqcfg);
1465 if (status != NVME_SC_SUCCESS)
1468 nvmet_set_result(req, ((u32)irqcfg.cd << 16) | iv);
1470 return NVME_SC_SUCCESS;
1473 static u16 nvmet_get_feat_arbitration(struct nvmet_req *req)
1475 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1476 struct nvmet_feat_arbitration arb = { };
1479 if (!ctrl->ops->get_feature) {
1480 req->error_loc = offsetof(struct nvme_common_command, cdw10);
1481 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1484 status = ctrl->ops->get_feature(ctrl, NVME_FEAT_ARBITRATION, &arb);
1485 if (status != NVME_SC_SUCCESS)
1488 nvmet_set_result(req,
1489 ((u32)arb.hpw << 24) |
1490 ((u32)arb.mpw << 16) |
1491 ((u32)arb.lpw << 8) |
1494 return NVME_SC_SUCCESS;
1497 void nvmet_get_feat_kato(struct nvmet_req *req)
1499 nvmet_set_result(req, req->sq->ctrl->kato * 1000);
1502 void nvmet_get_feat_async_event(struct nvmet_req *req)
1504 nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled));
1507 void nvmet_execute_get_features(struct nvmet_req *req)
1509 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1510 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
1513 if (!nvmet_check_transfer_len(req, nvmet_feat_data_len(req, cdw10)))
1516 switch (cdw10 & 0xff) {
1518 * These features are mandatory in the spec, but we don't
1519 * have a useful way to implement them. We'll eventually
1520 * need to come up with some fake values for these.
1523 case NVME_FEAT_POWER_MGMT:
1525 case NVME_FEAT_TEMP_THRESH:
1527 case NVME_FEAT_ERR_RECOVERY:
1529 case NVME_FEAT_WRITE_ATOMIC:
1532 case NVME_FEAT_ARBITRATION:
1533 status = nvmet_get_feat_arbitration(req);
1535 case NVME_FEAT_IRQ_COALESCE:
1536 status = nvmet_get_feat_irq_coalesce(req);
1538 case NVME_FEAT_IRQ_CONFIG:
1539 status = nvmet_get_feat_irq_config(req);
1541 case NVME_FEAT_ASYNC_EVENT:
1542 nvmet_get_feat_async_event(req);
1544 case NVME_FEAT_VOLATILE_WC:
1545 nvmet_set_result(req, 1);
1547 case NVME_FEAT_NUM_QUEUES:
1548 nvmet_set_result(req,
1549 (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
1551 case NVME_FEAT_KATO:
1552 nvmet_get_feat_kato(req);
1554 case NVME_FEAT_HOST_ID:
1555 /* need 128-bit host identifier flag */
1556 if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) {
1558 offsetof(struct nvme_common_command, cdw11);
1559 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1563 status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
1564 sizeof(req->sq->ctrl->hostid));
1566 case NVME_FEAT_WRITE_PROTECT:
1567 status = nvmet_get_feat_write_protect(req);
1569 case NVME_FEAT_RESV_MASK:
1570 status = nvmet_get_feat_resv_notif_mask(req);
1574 offsetof(struct nvme_common_command, cdw10);
1575 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1579 nvmet_req_complete(req, status);
1582 void nvmet_execute_async_event(struct nvmet_req *req)
1584 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1586 if (!nvmet_check_transfer_len(req, 0))
1589 mutex_lock(&ctrl->lock);
1590 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
1591 mutex_unlock(&ctrl->lock);
1592 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_STATUS_DNR);
1595 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
1596 mutex_unlock(&ctrl->lock);
1598 queue_work(nvmet_wq, &ctrl->async_event_work);
1601 void nvmet_execute_keep_alive(struct nvmet_req *req)
1603 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1606 if (!nvmet_check_transfer_len(req, 0))
1610 status = NVME_SC_KA_TIMEOUT_INVALID;
1614 pr_debug("ctrl %d update keep-alive timer for %d secs\n",
1615 ctrl->cntlid, ctrl->kato);
1616 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
1618 nvmet_req_complete(req, status);
1621 u32 nvmet_admin_cmd_data_len(struct nvmet_req *req)
1623 struct nvme_command *cmd = req->cmd;
1625 if (nvme_is_fabrics(cmd))
1626 return nvmet_fabrics_admin_cmd_data_len(req);
1627 if (nvmet_is_disc_subsys(nvmet_req_subsys(req)))
1628 return nvmet_discovery_cmd_data_len(req);
1630 switch (cmd->common.opcode) {
1631 case nvme_admin_get_log_page:
1632 return nvmet_get_log_page_len(cmd);
1633 case nvme_admin_identify:
1634 return NVME_IDENTIFY_DATA_SIZE;
1635 case nvme_admin_get_features:
1636 return nvmet_feat_data_len(req, le32_to_cpu(cmd->common.cdw10));
1642 u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
1644 struct nvme_command *cmd = req->cmd;
1647 if (nvme_is_fabrics(cmd))
1648 return nvmet_parse_fabrics_admin_cmd(req);
1649 if (nvmet_is_disc_subsys(nvmet_req_subsys(req)))
1650 return nvmet_parse_discovery_cmd(req);
1652 ret = nvmet_check_ctrl_status(req);
1656 /* For PCI controllers, admin commands shall not use SGL. */
1657 if (nvmet_is_pci_ctrl(req->sq->ctrl) && !req->sq->qid &&
1658 cmd->common.flags & NVME_CMD_SGL_ALL)
1659 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1661 if (nvmet_is_passthru_req(req))
1662 return nvmet_parse_passthru_admin_cmd(req);
1664 switch (cmd->common.opcode) {
1665 case nvme_admin_delete_sq:
1666 req->execute = nvmet_execute_delete_sq;
1668 case nvme_admin_create_sq:
1669 req->execute = nvmet_execute_create_sq;
1671 case nvme_admin_get_log_page:
1672 req->execute = nvmet_execute_get_log_page;
1674 case nvme_admin_delete_cq:
1675 req->execute = nvmet_execute_delete_cq;
1677 case nvme_admin_create_cq:
1678 req->execute = nvmet_execute_create_cq;
1680 case nvme_admin_identify:
1681 req->execute = nvmet_execute_identify;
1683 case nvme_admin_abort_cmd:
1684 req->execute = nvmet_execute_abort;
1686 case nvme_admin_set_features:
1687 req->execute = nvmet_execute_set_features;
1689 case nvme_admin_get_features:
1690 req->execute = nvmet_execute_get_features;
1692 case nvme_admin_async_event:
1693 req->execute = nvmet_execute_async_event;
1695 case nvme_admin_keep_alive:
1696 req->execute = nvmet_execute_keep_alive;
1699 return nvmet_report_invalid_opcode(req);