1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * NVMe admin command implementation. 4 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 5 */ 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 #include <linux/module.h> 8 #include <linux/rculist.h> 9 #include <linux/part_stat.h> 10 11 #include <generated/utsrelease.h> 12 #include <linux/unaligned.h> 13 #include "nvmet.h" 14 15 static void nvmet_execute_delete_sq(struct nvmet_req *req) 16 { 17 struct nvmet_ctrl *ctrl = req->sq->ctrl; 18 u16 sqid = le16_to_cpu(req->cmd->delete_queue.qid); 19 u16 status; 20 21 if (!nvmet_is_pci_ctrl(ctrl)) { 22 status = nvmet_report_invalid_opcode(req); 23 goto complete; 24 } 25 26 if (!sqid) { 27 status = NVME_SC_QID_INVALID | NVME_STATUS_DNR; 28 goto complete; 29 } 30 31 status = nvmet_check_sqid(ctrl, sqid, false); 32 if (status != NVME_SC_SUCCESS) 33 goto complete; 34 35 status = ctrl->ops->delete_sq(ctrl, sqid); 36 37 complete: 38 nvmet_req_complete(req, status); 39 } 40 41 static void nvmet_execute_create_sq(struct nvmet_req *req) 42 { 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); 50 u16 status; 51 52 if (!nvmet_is_pci_ctrl(ctrl)) { 53 status = nvmet_report_invalid_opcode(req); 54 goto complete; 55 } 56 57 if (!sqid) { 58 status = NVME_SC_QID_INVALID | NVME_STATUS_DNR; 59 goto complete; 60 } 61 62 status = nvmet_check_sqid(ctrl, sqid, true); 63 if (status != NVME_SC_SUCCESS) 64 goto complete; 65 66 status = nvmet_check_io_cqid(ctrl, cqid, false); 67 if (status != NVME_SC_SUCCESS) { 68 pr_err("SQ %u: Invalid CQID %u\n", sqid, cqid); 69 goto complete; 70 } 71 72 if (!qsize || qsize > NVME_CAP_MQES(ctrl->cap)) { 73 status = NVME_SC_QUEUE_SIZE | NVME_STATUS_DNR; 74 goto complete; 75 } 76 77 status = ctrl->ops->create_sq(ctrl, sqid, cqid, sq_flags, qsize, prp1); 78 79 complete: 80 nvmet_req_complete(req, status); 81 } 82 83 static void nvmet_execute_delete_cq(struct nvmet_req *req) 84 { 85 struct nvmet_ctrl *ctrl = req->sq->ctrl; 86 u16 cqid = le16_to_cpu(req->cmd->delete_queue.qid); 87 u16 status; 88 89 if (!nvmet_is_pci_ctrl(ctrl)) { 90 status = nvmet_report_invalid_opcode(req); 91 goto complete; 92 } 93 94 status = nvmet_check_io_cqid(ctrl, cqid, false); 95 if (status != NVME_SC_SUCCESS) 96 goto complete; 97 98 if (!ctrl->cqs[cqid] || nvmet_cq_in_use(ctrl->cqs[cqid])) { 99 /* Some SQs are still using this CQ */ 100 status = NVME_SC_QID_INVALID | NVME_STATUS_DNR; 101 goto complete; 102 } 103 104 status = ctrl->ops->delete_cq(ctrl, cqid); 105 106 complete: 107 nvmet_req_complete(req, status); 108 } 109 110 static void nvmet_execute_create_cq(struct nvmet_req *req) 111 { 112 struct nvmet_ctrl *ctrl = req->sq->ctrl; 113 struct nvme_command *cmd = req->cmd; 114 u16 cqid = le16_to_cpu(cmd->create_cq.cqid); 115 u16 cq_flags = le16_to_cpu(cmd->create_cq.cq_flags); 116 u16 qsize = le16_to_cpu(cmd->create_cq.qsize); 117 u16 irq_vector = le16_to_cpu(cmd->create_cq.irq_vector); 118 u64 prp1 = le64_to_cpu(cmd->create_cq.prp1); 119 u16 status; 120 121 if (!nvmet_is_pci_ctrl(ctrl)) { 122 status = nvmet_report_invalid_opcode(req); 123 goto complete; 124 } 125 126 status = nvmet_check_io_cqid(ctrl, cqid, true); 127 if (status != NVME_SC_SUCCESS) 128 goto complete; 129 130 if (!qsize || qsize > NVME_CAP_MQES(ctrl->cap)) { 131 status = NVME_SC_QUEUE_SIZE | NVME_STATUS_DNR; 132 goto complete; 133 } 134 135 status = ctrl->ops->create_cq(ctrl, cqid, cq_flags, qsize, 136 prp1, irq_vector); 137 138 complete: 139 nvmet_req_complete(req, status); 140 } 141 142 u32 nvmet_get_log_page_len(struct nvme_command *cmd) 143 { 144 u32 len = le16_to_cpu(cmd->get_log_page.numdu); 145 146 len <<= 16; 147 len += le16_to_cpu(cmd->get_log_page.numdl); 148 /* NUMD is a 0's based value */ 149 len += 1; 150 len *= sizeof(u32); 151 152 return len; 153 } 154 155 static u32 nvmet_feat_data_len(struct nvmet_req *req, u32 cdw10) 156 { 157 switch (cdw10 & 0xff) { 158 case NVME_FEAT_HOST_ID: 159 return sizeof(req->sq->ctrl->hostid); 160 default: 161 return 0; 162 } 163 } 164 165 u64 nvmet_get_log_page_offset(struct nvme_command *cmd) 166 { 167 return le64_to_cpu(cmd->get_log_page.lpo); 168 } 169 170 static void nvmet_execute_get_log_page_noop(struct nvmet_req *req) 171 { 172 nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->transfer_len)); 173 } 174 175 static void nvmet_execute_get_log_page_error(struct nvmet_req *req) 176 { 177 struct nvmet_ctrl *ctrl = req->sq->ctrl; 178 unsigned long flags; 179 u32 offset = 0; 180 u32 len; 181 u64 slot; 182 u64 i; 183 u16 status = 0; 184 185 status = nvmet_zero_sgl(req, 0, req->transfer_len); 186 if (status) { 187 nvmet_req_complete(req, status); 188 return; 189 } 190 191 len = min_t(u32, req->transfer_len, 192 NVMET_ERROR_LOG_SLOTS * sizeof(struct nvme_error_slot)); 193 194 spin_lock_irqsave(&ctrl->error_lock, flags); 195 196 /* 197 * The NVMe base specification requires the most recent error to be 198 * the first log entry, so walk the ring backwards from the slot of 199 * the last error and stop at the end of the requested buffer. 200 */ 201 slot = ctrl->err_counter % NVMET_ERROR_LOG_SLOTS; 202 203 for (i = 0; i < NVMET_ERROR_LOG_SLOTS && 204 offset + sizeof(struct nvme_error_slot) <= len; i++) { 205 status = nvmet_copy_to_sgl(req, offset, &ctrl->slots[slot], 206 sizeof(struct nvme_error_slot)); 207 if (status) 208 break; 209 210 if (slot == 0) 211 slot = NVMET_ERROR_LOG_SLOTS - 1; 212 else 213 slot--; 214 offset += sizeof(struct nvme_error_slot); 215 } 216 spin_unlock_irqrestore(&ctrl->error_lock, flags); 217 nvmet_req_complete(req, status); 218 } 219 220 static void nvmet_execute_get_supported_log_pages(struct nvmet_req *req) 221 { 222 struct nvme_supported_log *logs; 223 u16 status; 224 225 logs = kzalloc_obj(*logs); 226 if (!logs) { 227 status = NVME_SC_INTERNAL; 228 goto out; 229 } 230 231 logs->lids[NVME_LOG_SUPPORTED] = cpu_to_le32(NVME_LIDS_LSUPP); 232 logs->lids[NVME_LOG_ERROR] = cpu_to_le32(NVME_LIDS_LSUPP); 233 logs->lids[NVME_LOG_SMART] = cpu_to_le32(NVME_LIDS_LSUPP); 234 logs->lids[NVME_LOG_FW_SLOT] = cpu_to_le32(NVME_LIDS_LSUPP); 235 logs->lids[NVME_LOG_CHANGED_NS] = cpu_to_le32(NVME_LIDS_LSUPP); 236 logs->lids[NVME_LOG_CMD_EFFECTS] = cpu_to_le32(NVME_LIDS_LSUPP); 237 logs->lids[NVME_LOG_ENDURANCE_GROUP] = cpu_to_le32(NVME_LIDS_LSUPP); 238 logs->lids[NVME_LOG_ANA] = cpu_to_le32(NVME_LIDS_LSUPP); 239 logs->lids[NVME_LOG_FEATURES] = cpu_to_le32(NVME_LIDS_LSUPP); 240 logs->lids[NVME_LOG_RMI] = cpu_to_le32(NVME_LIDS_LSUPP); 241 logs->lids[NVME_LOG_RESERVATION] = cpu_to_le32(NVME_LIDS_LSUPP); 242 243 status = nvmet_zero_sgl(req, 0, req->transfer_len); 244 if (!status) 245 status = nvmet_copy_to_sgl(req, 0, logs, sizeof(*logs)); 246 kfree(logs); 247 out: 248 nvmet_req_complete(req, status); 249 } 250 251 static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req, 252 struct nvme_smart_log *slog) 253 { 254 u64 host_reads, host_writes, data_units_read, data_units_written; 255 u16 status; 256 257 status = nvmet_req_find_ns(req); 258 if (status) 259 return status; 260 261 /* we don't have the right data for file backed ns */ 262 if (!req->ns->bdev) 263 return NVME_SC_SUCCESS; 264 265 host_reads = part_stat_read(req->ns->bdev, ios[READ]); 266 data_units_read = 267 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000); 268 host_writes = part_stat_read(req->ns->bdev, ios[WRITE]); 269 data_units_written = 270 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000); 271 272 put_unaligned_le64(host_reads, &slog->host_reads[0]); 273 put_unaligned_le64(data_units_read, &slog->data_units_read[0]); 274 put_unaligned_le64(host_writes, &slog->host_writes[0]); 275 put_unaligned_le64(data_units_written, &slog->data_units_written[0]); 276 277 return NVME_SC_SUCCESS; 278 } 279 280 static u16 nvmet_get_smart_log_all(struct nvmet_req *req, 281 struct nvme_smart_log *slog) 282 { 283 u64 host_reads = 0, host_writes = 0; 284 u64 data_units_read = 0, data_units_written = 0; 285 struct nvmet_ns *ns; 286 struct nvmet_ctrl *ctrl; 287 unsigned long idx; 288 289 ctrl = req->sq->ctrl; 290 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) { 291 /* we don't have the right data for file backed ns */ 292 if (!ns->bdev) 293 continue; 294 host_reads += part_stat_read(ns->bdev, ios[READ]); 295 data_units_read += DIV_ROUND_UP( 296 part_stat_read(ns->bdev, sectors[READ]), 1000); 297 host_writes += part_stat_read(ns->bdev, ios[WRITE]); 298 data_units_written += DIV_ROUND_UP( 299 part_stat_read(ns->bdev, sectors[WRITE]), 1000); 300 } 301 302 put_unaligned_le64(host_reads, &slog->host_reads[0]); 303 put_unaligned_le64(data_units_read, &slog->data_units_read[0]); 304 put_unaligned_le64(host_writes, &slog->host_writes[0]); 305 put_unaligned_le64(data_units_written, &slog->data_units_written[0]); 306 307 return NVME_SC_SUCCESS; 308 } 309 310 static void nvmet_execute_get_log_page_rmi(struct nvmet_req *req) 311 { 312 struct nvme_rotational_media_log *log; 313 struct gendisk *disk; 314 u16 status; 315 316 req->cmd->common.nsid = cpu_to_le32(le16_to_cpu( 317 req->cmd->get_log_page.lsi)); 318 status = nvmet_req_find_ns(req); 319 if (status) 320 goto out; 321 322 if (!req->ns->bdev || !bdev_rot(req->ns->bdev)) { 323 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 324 goto out; 325 } 326 327 if (req->transfer_len < sizeof(*log)) { 328 status = NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR; 329 goto out; 330 } 331 332 log = kzalloc_obj(*log); 333 if (!log) { 334 status = NVME_SC_INTERNAL; 335 goto out; 336 } 337 338 log->endgid = req->cmd->get_log_page.lsi; 339 disk = req->ns->bdev->bd_disk; 340 if (disk && disk->ia_ranges) 341 log->numa = cpu_to_le16(disk->ia_ranges->nr_ia_ranges); 342 else 343 log->numa = cpu_to_le16(1); 344 345 status = nvmet_zero_sgl(req, 0, req->transfer_len); 346 if (!status) 347 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log)); 348 kfree(log); 349 out: 350 nvmet_req_complete(req, status); 351 } 352 353 static void nvmet_execute_get_log_page_smart(struct nvmet_req *req) 354 { 355 struct nvme_smart_log *log; 356 u16 status = NVME_SC_INTERNAL; 357 unsigned long flags; 358 359 if (req->transfer_len != sizeof(*log)) 360 goto out; 361 362 log = kzalloc_obj(*log); 363 if (!log) 364 goto out; 365 366 if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL)) 367 status = nvmet_get_smart_log_all(req, log); 368 else 369 status = nvmet_get_smart_log_nsid(req, log); 370 if (status) 371 goto out_free_log; 372 373 spin_lock_irqsave(&req->sq->ctrl->error_lock, flags); 374 put_unaligned_le64(req->sq->ctrl->err_counter, 375 &log->num_err_log_entries); 376 spin_unlock_irqrestore(&req->sq->ctrl->error_lock, flags); 377 378 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log)); 379 out_free_log: 380 kfree(log); 381 out: 382 nvmet_req_complete(req, status); 383 } 384 385 static void nvmet_get_cmd_effects_admin(struct nvmet_ctrl *ctrl, 386 struct nvme_effects_log *log) 387 { 388 /* For a PCI target controller, advertize support for the . */ 389 if (nvmet_is_pci_ctrl(ctrl)) { 390 log->acs[nvme_admin_delete_sq] = 391 log->acs[nvme_admin_create_sq] = 392 log->acs[nvme_admin_delete_cq] = 393 log->acs[nvme_admin_create_cq] = 394 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP); 395 } 396 397 log->acs[nvme_admin_get_log_page] = 398 log->acs[nvme_admin_identify] = 399 log->acs[nvme_admin_abort_cmd] = 400 log->acs[nvme_admin_set_features] = 401 log->acs[nvme_admin_get_features] = 402 log->acs[nvme_admin_async_event] = 403 log->acs[nvme_admin_keep_alive] = 404 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP); 405 } 406 407 static void nvmet_get_cmd_effects_nvm(struct nvme_effects_log *log) 408 { 409 log->iocs[nvme_cmd_read] = 410 log->iocs[nvme_cmd_flush] = 411 log->iocs[nvme_cmd_dsm] = 412 log->iocs[nvme_cmd_resv_acquire] = 413 log->iocs[nvme_cmd_resv_register] = 414 log->iocs[nvme_cmd_resv_release] = 415 log->iocs[nvme_cmd_resv_report] = 416 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP); 417 log->iocs[nvme_cmd_write] = 418 log->iocs[nvme_cmd_write_zeroes] = 419 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC); 420 } 421 422 static void nvmet_get_cmd_effects_zns(struct nvme_effects_log *log) 423 { 424 log->iocs[nvme_cmd_zone_append] = 425 log->iocs[nvme_cmd_zone_mgmt_send] = 426 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC); 427 log->iocs[nvme_cmd_zone_mgmt_recv] = 428 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP); 429 } 430 431 static void nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req *req) 432 { 433 struct nvmet_ctrl *ctrl = req->sq->ctrl; 434 struct nvme_effects_log *log; 435 u16 status = NVME_SC_SUCCESS; 436 437 log = kzalloc_obj(*log); 438 if (!log) { 439 status = NVME_SC_INTERNAL; 440 goto out; 441 } 442 443 switch (req->cmd->get_log_page.csi) { 444 case NVME_CSI_NVM: 445 nvmet_get_cmd_effects_admin(ctrl, log); 446 nvmet_get_cmd_effects_nvm(log); 447 break; 448 case NVME_CSI_ZNS: 449 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) { 450 status = NVME_SC_INVALID_IO_CMD_SET; 451 goto free; 452 } 453 nvmet_get_cmd_effects_admin(ctrl, log); 454 nvmet_get_cmd_effects_nvm(log); 455 nvmet_get_cmd_effects_zns(log); 456 break; 457 default: 458 status = NVME_SC_INVALID_LOG_PAGE; 459 goto free; 460 } 461 462 status = nvmet_zero_sgl(req, 0, req->transfer_len); 463 if (!status) 464 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log)); 465 free: 466 kfree(log); 467 out: 468 nvmet_req_complete(req, status); 469 } 470 471 static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req) 472 { 473 struct nvmet_ctrl *ctrl = req->sq->ctrl; 474 u16 status = NVME_SC_INTERNAL; 475 size_t len; 476 477 if (req->transfer_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32)) 478 goto out; 479 480 mutex_lock(&ctrl->lock); 481 if (ctrl->nr_changed_ns == U32_MAX) 482 len = sizeof(__le32); 483 else 484 len = ctrl->nr_changed_ns * sizeof(__le32); 485 status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len); 486 if (!status) 487 status = nvmet_zero_sgl(req, len, req->transfer_len - len); 488 ctrl->nr_changed_ns = 0; 489 nvmet_clear_aen_bit(req, NVME_AEN_BIT_NS_ATTR); 490 mutex_unlock(&ctrl->lock); 491 out: 492 nvmet_req_complete(req, status); 493 } 494 495 static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid, 496 struct nvme_ana_group_desc *desc) 497 { 498 struct nvmet_ctrl *ctrl = req->sq->ctrl; 499 struct nvmet_ns *ns; 500 unsigned long idx; 501 u32 count = 0; 502 503 if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) { 504 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) { 505 if (ns->anagrpid == grpid) 506 desc->nsids[count++] = cpu_to_le32(ns->nsid); 507 } 508 } 509 510 desc->grpid = cpu_to_le32(grpid); 511 desc->nnsids = cpu_to_le32(count); 512 desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt); 513 desc->state = req->port->ana_state[grpid]; 514 memset(desc->rsvd17, 0, sizeof(desc->rsvd17)); 515 return struct_size(desc, nsids, count); 516 } 517 518 static void nvmet_execute_get_log_page_endgrp(struct nvmet_req *req) 519 { 520 u64 host_reads, host_writes, data_units_read, data_units_written; 521 struct nvme_endurance_group_log *log; 522 u16 status; 523 524 /* 525 * The target driver emulates each endurance group as its own 526 * namespace, reusing the nsid as the endurance group identifier. 527 */ 528 req->cmd->common.nsid = cpu_to_le32(le16_to_cpu( 529 req->cmd->get_log_page.lsi)); 530 status = nvmet_req_find_ns(req); 531 if (status) 532 goto out; 533 534 log = kzalloc_obj(*log); 535 if (!log) { 536 status = NVME_SC_INTERNAL; 537 goto out; 538 } 539 540 if (!req->ns->bdev) 541 goto copy; 542 543 host_reads = part_stat_read(req->ns->bdev, ios[READ]); 544 data_units_read = 545 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000); 546 host_writes = part_stat_read(req->ns->bdev, ios[WRITE]); 547 data_units_written = 548 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000); 549 550 put_unaligned_le64(host_reads, &log->hrc[0]); 551 put_unaligned_le64(data_units_read, &log->dur[0]); 552 put_unaligned_le64(host_writes, &log->hwc[0]); 553 put_unaligned_le64(data_units_written, &log->duw[0]); 554 copy: 555 status = nvmet_zero_sgl(req, 0, req->transfer_len); 556 if (!status) 557 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log)); 558 kfree(log); 559 out: 560 nvmet_req_complete(req, status); 561 } 562 563 static void nvmet_execute_get_log_page_ana(struct nvmet_req *req) 564 { 565 struct nvme_ana_rsp_hdr hdr = { 0, }; 566 struct nvme_ana_group_desc *desc; 567 size_t offset = sizeof(struct nvme_ana_rsp_hdr); /* start beyond hdr */ 568 size_t len; 569 u32 grpid; 570 u16 ngrps = 0; 571 u16 status; 572 573 /* 574 * The header and one descriptor per enabled group are the only bytes 575 * written below; zero the whole transfer first so the unwritten tail 576 * does not leak stale kernel memory to the host. 577 */ 578 status = nvmet_zero_sgl(req, 0, req->transfer_len); 579 if (status) 580 goto out; 581 582 status = NVME_SC_INTERNAL; 583 desc = kmalloc_flex(*desc, nsids, NVMET_MAX_NAMESPACES); 584 if (!desc) 585 goto out; 586 587 down_read(&nvmet_ana_sem); 588 for (grpid = 1; grpid <= NVMET_MAX_ANAGRPS; grpid++) { 589 if (!nvmet_ana_group_enabled[grpid]) 590 continue; 591 len = nvmet_format_ana_group(req, grpid, desc); 592 status = nvmet_copy_to_sgl(req, offset, desc, len); 593 if (status) 594 break; 595 offset += len; 596 ngrps++; 597 } 598 for ( ; grpid <= NVMET_MAX_ANAGRPS; grpid++) { 599 if (nvmet_ana_group_enabled[grpid]) 600 ngrps++; 601 } 602 603 hdr.chgcnt = cpu_to_le64(nvmet_ana_chgcnt); 604 hdr.ngrps = cpu_to_le16(ngrps); 605 nvmet_clear_aen_bit(req, NVME_AEN_BIT_ANA_CHANGE); 606 up_read(&nvmet_ana_sem); 607 608 kfree(desc); 609 610 /* copy the header last once we know the number of groups */ 611 status = nvmet_copy_to_sgl(req, 0, &hdr, sizeof(hdr)); 612 out: 613 nvmet_req_complete(req, status); 614 } 615 616 static void nvmet_execute_get_log_page_features(struct nvmet_req *req) 617 { 618 struct nvme_supported_features_log *features; 619 u16 status; 620 621 features = kzalloc_obj(*features); 622 if (!features) { 623 status = NVME_SC_INTERNAL; 624 goto out; 625 } 626 627 features->fis[NVME_FEAT_NUM_QUEUES] = 628 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE); 629 features->fis[NVME_FEAT_KATO] = 630 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE); 631 features->fis[NVME_FEAT_ASYNC_EVENT] = 632 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE); 633 features->fis[NVME_FEAT_HOST_ID] = 634 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE); 635 features->fis[NVME_FEAT_WRITE_PROTECT] = 636 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_NSCPE); 637 features->fis[NVME_FEAT_RESV_MASK] = 638 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_NSCPE); 639 640 status = nvmet_zero_sgl(req, 0, req->transfer_len); 641 if (!status) 642 status = nvmet_copy_to_sgl(req, 0, features, sizeof(*features)); 643 kfree(features); 644 out: 645 nvmet_req_complete(req, status); 646 } 647 648 static void nvmet_execute_get_log_page(struct nvmet_req *req) 649 { 650 if (!nvmet_check_transfer_len(req, nvmet_get_log_page_len(req->cmd))) 651 return; 652 653 switch (req->cmd->get_log_page.lid) { 654 case NVME_LOG_SUPPORTED: 655 return nvmet_execute_get_supported_log_pages(req); 656 case NVME_LOG_ERROR: 657 return nvmet_execute_get_log_page_error(req); 658 case NVME_LOG_SMART: 659 return nvmet_execute_get_log_page_smart(req); 660 case NVME_LOG_FW_SLOT: 661 /* 662 * We only support a single firmware slot which always is 663 * active, so we can zero out the whole firmware slot log and 664 * still claim to fully implement this mandatory log page. 665 */ 666 return nvmet_execute_get_log_page_noop(req); 667 case NVME_LOG_CHANGED_NS: 668 return nvmet_execute_get_log_changed_ns(req); 669 case NVME_LOG_CMD_EFFECTS: 670 return nvmet_execute_get_log_cmd_effects_ns(req); 671 case NVME_LOG_ENDURANCE_GROUP: 672 return nvmet_execute_get_log_page_endgrp(req); 673 case NVME_LOG_ANA: 674 return nvmet_execute_get_log_page_ana(req); 675 case NVME_LOG_FEATURES: 676 return nvmet_execute_get_log_page_features(req); 677 case NVME_LOG_RMI: 678 return nvmet_execute_get_log_page_rmi(req); 679 case NVME_LOG_RESERVATION: 680 return nvmet_execute_get_log_page_resv(req); 681 } 682 pr_debug("unhandled lid %d on qid %d\n", 683 req->cmd->get_log_page.lid, req->sq->qid); 684 req->error_loc = offsetof(struct nvme_get_log_page_command, lid); 685 nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_STATUS_DNR); 686 } 687 688 static void nvmet_execute_identify_ctrl(struct nvmet_req *req) 689 { 690 struct nvmet_ctrl *ctrl = req->sq->ctrl; 691 struct nvmet_subsys *subsys = ctrl->subsys; 692 struct nvme_id_ctrl *id; 693 u32 cmd_capsule_size, ctratt; 694 u16 status = 0; 695 696 if (!subsys->subsys_discovered) { 697 mutex_lock(&subsys->lock); 698 subsys->subsys_discovered = true; 699 mutex_unlock(&subsys->lock); 700 } 701 702 id = kzalloc_obj(*id); 703 if (!id) { 704 status = NVME_SC_INTERNAL; 705 goto out; 706 } 707 708 id->vid = cpu_to_le16(subsys->vendor_id); 709 id->ssvid = cpu_to_le16(subsys->subsys_vendor_id); 710 711 memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE); 712 memcpy_and_pad(id->mn, sizeof(id->mn), subsys->model_number, 713 strlen(subsys->model_number), ' '); 714 memcpy_and_pad(id->fr, sizeof(id->fr), 715 subsys->firmware_rev, strlen(subsys->firmware_rev), ' '); 716 717 put_unaligned_le24(subsys->ieee_oui, id->ieee); 718 719 id->rab = 6; 720 721 if (nvmet_is_disc_subsys(ctrl->subsys)) 722 id->cntrltype = NVME_CTRL_DISC; 723 else 724 id->cntrltype = NVME_CTRL_IO; 725 726 /* we support multiple ports, multiples hosts and ANA: */ 727 id->cmic = NVME_CTRL_CMIC_MULTI_PORT | NVME_CTRL_CMIC_MULTI_CTRL | 728 NVME_CTRL_CMIC_ANA; 729 730 /* Limit MDTS according to port config or transport capability */ 731 id->mdts = nvmet_ctrl_mdts(req); 732 id->cntlid = cpu_to_le16(ctrl->cntlid); 733 id->ver = cpu_to_le32(ctrl->subsys->ver); 734 735 /* XXX: figure out what to do about RTD3R/RTD3 */ 736 id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL); 737 ctratt = NVME_CTRL_ATTR_HID_128_BIT; 738 if (ctrl->tbkas) 739 ctratt |= NVME_CTRL_ATTR_TBKAS; 740 if (nvmet_is_pci_ctrl(ctrl)) 741 ctratt |= NVME_CTRL_ATTR_RHII; 742 id->ctratt = cpu_to_le32(ctratt); 743 744 id->oacs = 0; 745 746 /* 747 * We don't really have a practical limit on the number of abort 748 * commands. But we don't do anything useful for abort either, so 749 * no point in allowing more abort commands than the spec requires. 750 */ 751 id->acl = 3; 752 753 id->aerl = NVMET_ASYNC_EVENTS - 1; 754 755 /* first slot is read-only, only one slot supported */ 756 id->frmw = (1 << 0) | (1 << 1); 757 id->lpa = (1 << 0) | (1 << 1) | (1 << 2); 758 id->elpe = NVMET_ERROR_LOG_SLOTS - 1; 759 id->npss = 0; 760 761 /* We support keep-alive timeout in granularity of seconds */ 762 id->kas = cpu_to_le16(NVMET_KAS); 763 764 id->sqes = (0x6 << 4) | 0x6; 765 id->cqes = (0x4 << 4) | 0x4; 766 767 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */ 768 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD(ctrl)); 769 770 id->nn = cpu_to_le32(NVMET_MAX_NAMESPACES); 771 id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES); 772 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM | 773 NVME_CTRL_ONCS_WRITE_ZEROES | 774 NVME_CTRL_ONCS_RESERVATIONS); 775 776 /* XXX: don't report vwc if the underlying device is write through */ 777 id->vwc = NVME_CTRL_VWC_PRESENT; 778 779 /* 780 * We can't support atomic writes bigger than a LBA without support 781 * from the backend device. 782 */ 783 id->awun = 0; 784 id->awupf = 0; 785 786 /* we always support SGLs */ 787 id->sgls = cpu_to_le32(NVME_CTRL_SGLS_BYTE_ALIGNED); 788 if (ctrl->ops->flags & NVMF_KEYED_SGLS) 789 id->sgls |= cpu_to_le32(NVME_CTRL_SGLS_KSDBDS); 790 if (req->port->inline_data_size) 791 id->sgls |= cpu_to_le32(NVME_CTRL_SGLS_SAOS); 792 793 strscpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn)); 794 795 /* 796 * Max command capsule size is sqe + in-capsule data size. 797 * Disable in-capsule data for Metadata capable controllers. 798 */ 799 cmd_capsule_size = sizeof(struct nvme_command); 800 if (!ctrl->pi_support) 801 cmd_capsule_size += req->port->inline_data_size; 802 id->ioccsz = cpu_to_le32(cmd_capsule_size / 16); 803 804 /* Max response capsule size is cqe */ 805 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16); 806 807 id->msdbd = ctrl->ops->msdbd; 808 809 /* 810 * Endurance group identifier is 16 bits, so we can't let namespaces 811 * overflow that since we reuse the nsid 812 */ 813 BUILD_BUG_ON(NVMET_MAX_NAMESPACES > USHRT_MAX); 814 id->endgidmax = cpu_to_le16(NVMET_MAX_NAMESPACES); 815 816 id->anacap = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4); 817 id->anatt = 10; /* random value */ 818 id->anagrpmax = cpu_to_le32(NVMET_MAX_ANAGRPS); 819 id->nanagrpid = cpu_to_le32(NVMET_MAX_ANAGRPS); 820 821 /* 822 * Meh, we don't really support any power state. Fake up the same 823 * values that qemu does. 824 */ 825 id->psd[0].max_power = cpu_to_le16(0x9c4); 826 id->psd[0].entry_lat = cpu_to_le32(0x10); 827 id->psd[0].exit_lat = cpu_to_le32(0x4); 828 829 id->nwpc = 1 << 0; /* write protect and no write protect */ 830 831 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); 832 833 kfree(id); 834 out: 835 nvmet_req_complete(req, status); 836 } 837 838 static void nvmet_execute_identify_ns(struct nvmet_req *req) 839 { 840 struct nvme_id_ns *id; 841 u16 status; 842 843 if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) { 844 req->error_loc = offsetof(struct nvme_identify, nsid); 845 status = NVME_SC_INVALID_NS | NVME_STATUS_DNR; 846 goto out; 847 } 848 849 id = kzalloc_obj(*id); 850 if (!id) { 851 status = NVME_SC_INTERNAL; 852 goto out; 853 } 854 855 /* return an all zeroed buffer if we can't find an active namespace */ 856 status = nvmet_req_find_ns(req); 857 if (status) { 858 status = 0; 859 goto done; 860 } 861 862 if (nvmet_ns_revalidate(req->ns)) { 863 mutex_lock(&req->ns->subsys->lock); 864 nvmet_ns_changed(req->ns->subsys, req->ns->nsid); 865 mutex_unlock(&req->ns->subsys->lock); 866 } 867 868 /* 869 * nuse = ncap = nsze isn't always true, but we have no way to find 870 * that out from the underlying device. 871 */ 872 id->ncap = id->nsze = 873 cpu_to_le64(req->ns->size >> req->ns->blksize_shift); 874 switch (req->port->ana_state[req->ns->anagrpid]) { 875 case NVME_ANA_INACCESSIBLE: 876 case NVME_ANA_PERSISTENT_LOSS: 877 break; 878 default: 879 id->nuse = id->nsze; 880 break; 881 } 882 883 if (req->ns->bdev) 884 nvmet_bdev_set_limits(req->ns->bdev, id); 885 886 /* 887 * We just provide a single LBA format that matches what the 888 * underlying device reports. 889 */ 890 id->nlbaf = 0; 891 id->flbas = 0; 892 893 /* 894 * Our namespace might always be shared. Not just with other 895 * controllers, but also with any other user of the block device. 896 */ 897 id->nmic = NVME_NS_NMIC_SHARED; 898 id->anagrpid = cpu_to_le32(req->ns->anagrpid); 899 900 if (req->ns->pr.enable) 901 id->rescap = NVME_PR_SUPPORT_WRITE_EXCLUSIVE | 902 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS | 903 NVME_PR_SUPPORT_WRITE_EXCLUSIVE_REG_ONLY | 904 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_REG_ONLY | 905 NVME_PR_SUPPORT_WRITE_EXCLUSIVE_ALL_REGS | 906 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_ALL_REGS | 907 NVME_PR_SUPPORT_IEKEY_VER_1_3_DEF; 908 909 /* 910 * Since we don't know any better, every namespace is its own endurance 911 * group. 912 */ 913 id->endgid = cpu_to_le16(req->ns->nsid); 914 915 memcpy(&id->nguid, &req->ns->nguid, sizeof(id->nguid)); 916 917 id->lbaf[0].ds = req->ns->blksize_shift; 918 919 if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns)) { 920 id->dpc = NVME_NS_DPC_PI_FIRST | NVME_NS_DPC_PI_LAST | 921 NVME_NS_DPC_PI_TYPE1 | NVME_NS_DPC_PI_TYPE2 | 922 NVME_NS_DPC_PI_TYPE3; 923 id->mc = NVME_MC_EXTENDED_LBA; 924 id->dps = req->ns->pi_type; 925 id->flbas = NVME_NS_FLBAS_META_EXT; 926 id->lbaf[0].ms = cpu_to_le16(req->ns->metadata_size); 927 } 928 929 if (req->ns->readonly) 930 id->nsattr |= NVME_NS_ATTR_RO; 931 done: 932 if (!status) 933 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); 934 935 kfree(id); 936 out: 937 nvmet_req_complete(req, status); 938 } 939 940 static void nvmet_execute_identify_endgrp_list(struct nvmet_req *req) 941 { 942 u16 min_endgid = le16_to_cpu(req->cmd->identify.cnssid); 943 static const int buf_size = NVME_IDENTIFY_DATA_SIZE; 944 struct nvmet_ctrl *ctrl = req->sq->ctrl; 945 struct nvmet_ns *ns; 946 unsigned long idx; 947 __le16 *list; 948 u16 status; 949 int i = 1; 950 951 list = kzalloc(buf_size, GFP_KERNEL); 952 if (!list) { 953 status = NVME_SC_INTERNAL; 954 goto out; 955 } 956 957 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) { 958 if (ns->nsid <= min_endgid) 959 continue; 960 961 list[i++] = cpu_to_le16(ns->nsid); 962 if (i == buf_size / sizeof(__le16)) 963 break; 964 } 965 966 list[0] = cpu_to_le16(i - 1); 967 status = nvmet_copy_to_sgl(req, 0, list, buf_size); 968 kfree(list); 969 out: 970 nvmet_req_complete(req, status); 971 } 972 973 static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css) 974 { 975 static const int buf_size = NVME_IDENTIFY_DATA_SIZE; 976 struct nvmet_ctrl *ctrl = req->sq->ctrl; 977 struct nvmet_ns *ns; 978 unsigned long idx; 979 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid); 980 __le32 *list; 981 u16 status = 0; 982 int i = 0; 983 984 /* 985 * NSID values 0xFFFFFFFE and NVME_NSID_ALL are invalid 986 * See NVMe Base Specification, Active Namespace ID list (CNS 02h). 987 */ 988 if (min_nsid == 0xFFFFFFFE || min_nsid == NVME_NSID_ALL) { 989 req->error_loc = offsetof(struct nvme_identify, nsid); 990 status = NVME_SC_INVALID_NS | NVME_STATUS_DNR; 991 goto out; 992 } 993 994 list = kzalloc(buf_size, GFP_KERNEL); 995 if (!list) { 996 status = NVME_SC_INTERNAL; 997 goto out; 998 } 999 1000 nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) { 1001 if (ns->nsid <= min_nsid) 1002 continue; 1003 if (match_css && ns->csi != req->cmd->identify.csi) 1004 continue; 1005 list[i++] = cpu_to_le32(ns->nsid); 1006 if (i == buf_size / sizeof(__le32)) 1007 break; 1008 } 1009 1010 status = nvmet_copy_to_sgl(req, 0, list, buf_size); 1011 1012 kfree(list); 1013 out: 1014 nvmet_req_complete(req, status); 1015 } 1016 1017 static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len, 1018 void *id, off_t *off) 1019 { 1020 struct nvme_ns_id_desc desc = { 1021 .nidt = type, 1022 .nidl = len, 1023 }; 1024 u16 status; 1025 1026 status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc)); 1027 if (status) 1028 return status; 1029 *off += sizeof(desc); 1030 1031 status = nvmet_copy_to_sgl(req, *off, id, len); 1032 if (status) 1033 return status; 1034 *off += len; 1035 1036 return 0; 1037 } 1038 1039 static void nvmet_execute_identify_desclist(struct nvmet_req *req) 1040 { 1041 off_t off = 0; 1042 u16 status; 1043 1044 status = nvmet_req_find_ns(req); 1045 if (status) 1046 goto out; 1047 1048 if (memchr_inv(&req->ns->uuid, 0, sizeof(req->ns->uuid))) { 1049 status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID, 1050 NVME_NIDT_UUID_LEN, 1051 &req->ns->uuid, &off); 1052 if (status) 1053 goto out; 1054 } 1055 if (memchr_inv(req->ns->nguid, 0, sizeof(req->ns->nguid))) { 1056 status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID, 1057 NVME_NIDT_NGUID_LEN, 1058 &req->ns->nguid, &off); 1059 if (status) 1060 goto out; 1061 } 1062 1063 status = nvmet_copy_ns_identifier(req, NVME_NIDT_CSI, 1064 NVME_NIDT_CSI_LEN, 1065 &req->ns->csi, &off); 1066 if (status) 1067 goto out; 1068 1069 if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off, 1070 off) != NVME_IDENTIFY_DATA_SIZE - off) 1071 status = NVME_SC_INTERNAL | NVME_STATUS_DNR; 1072 1073 out: 1074 nvmet_req_complete(req, status); 1075 } 1076 1077 static void nvmet_execute_identify_ctrl_nvm(struct nvmet_req *req) 1078 { 1079 /* Not supported: return zeroes */ 1080 nvmet_req_complete(req, 1081 nvmet_zero_sgl(req, 0, sizeof(struct nvme_id_ctrl_nvm))); 1082 } 1083 1084 static void nvme_execute_identify_ns_nvm(struct nvmet_req *req) 1085 { 1086 u16 status; 1087 struct nvme_id_ns_nvm *id; 1088 1089 status = nvmet_req_find_ns(req); 1090 if (status) 1091 goto out; 1092 1093 id = kzalloc_obj(*id); 1094 if (!id) { 1095 status = NVME_SC_INTERNAL; 1096 goto out; 1097 } 1098 if (req->ns->bdev) 1099 nvmet_bdev_set_nvm_limits(req->ns->bdev, id); 1100 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); 1101 kfree(id); 1102 out: 1103 nvmet_req_complete(req, status); 1104 } 1105 1106 static void nvmet_execute_id_cs_indep(struct nvmet_req *req) 1107 { 1108 struct nvme_id_ns_cs_indep *id; 1109 u16 status; 1110 1111 status = nvmet_req_find_ns(req); 1112 if (status) 1113 goto out; 1114 1115 id = kzalloc_obj(*id); 1116 if (!id) { 1117 status = NVME_SC_INTERNAL; 1118 goto out; 1119 } 1120 1121 id->nstat = NVME_NSTAT_NRDY; 1122 id->anagrpid = cpu_to_le32(req->ns->anagrpid); 1123 id->nmic = NVME_NS_NMIC_SHARED; 1124 if (req->ns->readonly) 1125 id->nsattr |= NVME_NS_ATTR_RO; 1126 if (req->ns->bdev && bdev_rot(req->ns->bdev)) 1127 id->nsfeat |= NVME_NS_ROTATIONAL; 1128 /* 1129 * We need flush command to flush the file's metadata, 1130 * so report supporting vwc if backend is file, even 1131 * though buffered_io is disable. 1132 */ 1133 if (req->ns->bdev && !bdev_write_cache(req->ns->bdev)) 1134 id->nsfeat |= NVME_NS_VWC_NOT_PRESENT; 1135 1136 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); 1137 kfree(id); 1138 out: 1139 nvmet_req_complete(req, status); 1140 } 1141 1142 static void nvmet_execute_identify(struct nvmet_req *req) 1143 { 1144 if (!nvmet_check_transfer_len(req, NVME_IDENTIFY_DATA_SIZE)) 1145 return; 1146 1147 switch (req->cmd->identify.cns) { 1148 case NVME_ID_CNS_NS: 1149 nvmet_execute_identify_ns(req); 1150 return; 1151 case NVME_ID_CNS_CTRL: 1152 nvmet_execute_identify_ctrl(req); 1153 return; 1154 case NVME_ID_CNS_NS_ACTIVE_LIST: 1155 nvmet_execute_identify_nslist(req, false); 1156 return; 1157 case NVME_ID_CNS_NS_DESC_LIST: 1158 nvmet_execute_identify_desclist(req); 1159 return; 1160 case NVME_ID_CNS_CS_NS: 1161 switch (req->cmd->identify.csi) { 1162 case NVME_CSI_NVM: 1163 nvme_execute_identify_ns_nvm(req); 1164 return; 1165 case NVME_CSI_ZNS: 1166 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) { 1167 nvmet_execute_identify_ns_zns(req); 1168 return; 1169 } 1170 break; 1171 } 1172 break; 1173 case NVME_ID_CNS_CS_CTRL: 1174 switch (req->cmd->identify.csi) { 1175 case NVME_CSI_NVM: 1176 nvmet_execute_identify_ctrl_nvm(req); 1177 return; 1178 case NVME_CSI_ZNS: 1179 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) { 1180 nvmet_execute_identify_ctrl_zns(req); 1181 return; 1182 } 1183 break; 1184 } 1185 break; 1186 case NVME_ID_CNS_NS_ACTIVE_LIST_CS: 1187 nvmet_execute_identify_nslist(req, true); 1188 return; 1189 case NVME_ID_CNS_NS_CS_INDEP: 1190 nvmet_execute_id_cs_indep(req); 1191 return; 1192 case NVME_ID_CNS_ENDGRP_LIST: 1193 nvmet_execute_identify_endgrp_list(req); 1194 return; 1195 } 1196 1197 pr_debug("unhandled identify cns %d on qid %d\n", 1198 req->cmd->identify.cns, req->sq->qid); 1199 req->error_loc = offsetof(struct nvme_identify, cns); 1200 nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_STATUS_DNR); 1201 } 1202 1203 /* 1204 * A "minimum viable" abort implementation: the command is mandatory in the 1205 * spec, but we are not required to do any useful work. We couldn't really 1206 * do a useful abort, so don't bother even with waiting for the command 1207 * to be executed and return immediately telling the command to abort 1208 * wasn't found. 1209 */ 1210 static void nvmet_execute_abort(struct nvmet_req *req) 1211 { 1212 if (!nvmet_check_transfer_len(req, 0)) 1213 return; 1214 nvmet_set_result(req, 1); 1215 nvmet_req_complete(req, 0); 1216 } 1217 1218 static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req) 1219 { 1220 u16 status; 1221 1222 if (req->ns->file) 1223 status = nvmet_file_flush(req); 1224 else 1225 status = nvmet_bdev_flush(req); 1226 1227 if (status) 1228 pr_err("write protect flush failed nsid: %u\n", req->ns->nsid); 1229 return status; 1230 } 1231 1232 static u16 nvmet_set_feat_write_protect(struct nvmet_req *req) 1233 { 1234 u32 write_protect = le32_to_cpu(req->cmd->common.cdw11); 1235 struct nvmet_subsys *subsys = nvmet_req_subsys(req); 1236 u16 status; 1237 1238 status = nvmet_req_find_ns(req); 1239 if (status) 1240 return status; 1241 1242 mutex_lock(&subsys->lock); 1243 switch (write_protect) { 1244 case NVME_NS_WRITE_PROTECT: 1245 req->ns->readonly = true; 1246 status = nvmet_write_protect_flush_sync(req); 1247 if (status) 1248 req->ns->readonly = false; 1249 break; 1250 case NVME_NS_NO_WRITE_PROTECT: 1251 req->ns->readonly = false; 1252 status = 0; 1253 break; 1254 default: 1255 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1256 break; 1257 } 1258 1259 if (!status) 1260 nvmet_ns_changed(subsys, req->ns->nsid); 1261 mutex_unlock(&subsys->lock); 1262 return status; 1263 } 1264 1265 u16 nvmet_set_feat_kato(struct nvmet_req *req) 1266 { 1267 u32 val32 = le32_to_cpu(req->cmd->common.cdw11); 1268 1269 if (val32 > NVMET_MAX_KATO * MSEC_PER_SEC) { 1270 req->error_loc = offsetof(struct nvme_common_command, cdw11); 1271 return NVME_SC_KA_TIMEOUT_INVALID | NVME_STATUS_DNR; 1272 } 1273 1274 nvmet_stop_keep_alive_timer(req->sq->ctrl); 1275 req->sq->ctrl->kato = DIV_ROUND_UP_ULL(val32, MSEC_PER_SEC); 1276 nvmet_start_keep_alive_timer(req->sq->ctrl); 1277 1278 nvmet_set_result(req, req->sq->ctrl->kato); 1279 1280 return 0; 1281 } 1282 1283 u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask) 1284 { 1285 u32 val32 = le32_to_cpu(req->cmd->common.cdw11); 1286 1287 if (val32 & ~mask) { 1288 req->error_loc = offsetof(struct nvme_common_command, cdw11); 1289 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1290 } 1291 1292 WRITE_ONCE(req->sq->ctrl->aen_enabled, val32); 1293 nvmet_set_result(req, val32); 1294 1295 return 0; 1296 } 1297 1298 static u16 nvmet_set_feat_host_id(struct nvmet_req *req) 1299 { 1300 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1301 1302 if (!nvmet_is_pci_ctrl(ctrl)) 1303 return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR; 1304 1305 /* 1306 * The NVMe base specifications v2.1 recommends supporting 128-bits host 1307 * IDs (section 5.1.25.1.28.1). However, that same section also says 1308 * that "The controller may support a 64-bit Host Identifier and/or an 1309 * extended 128-bit Host Identifier". So simplify this support and do 1310 * not support 64-bits host IDs to avoid needing to check that all 1311 * controllers associated with the same subsystem all use the same host 1312 * ID size. 1313 */ 1314 if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) { 1315 req->error_loc = offsetof(struct nvme_common_command, cdw11); 1316 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1317 } 1318 1319 return nvmet_copy_from_sgl(req, 0, &req->sq->ctrl->hostid, 1320 sizeof(req->sq->ctrl->hostid)); 1321 } 1322 1323 static u16 nvmet_set_feat_irq_coalesce(struct nvmet_req *req) 1324 { 1325 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1326 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11); 1327 struct nvmet_feat_irq_coalesce irqc = { 1328 .time = (cdw11 >> 8) & 0xff, 1329 .thr = cdw11 & 0xff, 1330 }; 1331 1332 /* 1333 * This feature is not supported for fabrics controllers and mandatory 1334 * for PCI controllers. 1335 */ 1336 if (!nvmet_is_pci_ctrl(ctrl)) { 1337 req->error_loc = offsetof(struct nvme_common_command, cdw10); 1338 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1339 } 1340 1341 return ctrl->ops->set_feature(ctrl, NVME_FEAT_IRQ_COALESCE, &irqc); 1342 } 1343 1344 static u16 nvmet_set_feat_irq_config(struct nvmet_req *req) 1345 { 1346 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1347 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11); 1348 struct nvmet_feat_irq_config irqcfg = { 1349 .iv = cdw11 & 0xffff, 1350 .cd = (cdw11 >> 16) & 0x1, 1351 }; 1352 1353 /* 1354 * This feature is not supported for fabrics controllers and mandatory 1355 * for PCI controllers. 1356 */ 1357 if (!nvmet_is_pci_ctrl(ctrl)) { 1358 req->error_loc = offsetof(struct nvme_common_command, cdw10); 1359 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1360 } 1361 1362 return ctrl->ops->set_feature(ctrl, NVME_FEAT_IRQ_CONFIG, &irqcfg); 1363 } 1364 1365 static u16 nvmet_set_feat_arbitration(struct nvmet_req *req) 1366 { 1367 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1368 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11); 1369 struct nvmet_feat_arbitration arb = { 1370 .hpw = (cdw11 >> 24) & 0xff, 1371 .mpw = (cdw11 >> 16) & 0xff, 1372 .lpw = (cdw11 >> 8) & 0xff, 1373 .ab = cdw11 & 0x7, 1374 }; 1375 1376 if (!ctrl->ops->set_feature) { 1377 req->error_loc = offsetof(struct nvme_common_command, cdw10); 1378 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1379 } 1380 1381 return ctrl->ops->set_feature(ctrl, NVME_FEAT_ARBITRATION, &arb); 1382 } 1383 1384 void nvmet_execute_set_features(struct nvmet_req *req) 1385 { 1386 struct nvmet_ctrl *ctrl = nvmet_req_ctrl(req); 1387 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10); 1388 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11); 1389 u16 status = 0; 1390 u16 nsqr; 1391 u16 ncqr; 1392 1393 if (!nvmet_check_data_len_lte(req, 0)) 1394 return; 1395 1396 switch (cdw10 & 0xff) { 1397 case NVME_FEAT_ARBITRATION: 1398 status = nvmet_set_feat_arbitration(req); 1399 break; 1400 case NVME_FEAT_NUM_QUEUES: 1401 ncqr = (cdw11 >> 16) & 0xffff; 1402 nsqr = cdw11 & 0xffff; 1403 if (ncqr == 0xffff || nsqr == 0xffff) { 1404 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1405 break; 1406 } 1407 nvmet_set_result(req, 1408 (ctrl->max_qid - 1) | ((ctrl->max_qid - 1) << 16)); 1409 break; 1410 case NVME_FEAT_IRQ_COALESCE: 1411 status = nvmet_set_feat_irq_coalesce(req); 1412 break; 1413 case NVME_FEAT_IRQ_CONFIG: 1414 status = nvmet_set_feat_irq_config(req); 1415 break; 1416 case NVME_FEAT_KATO: 1417 status = nvmet_set_feat_kato(req); 1418 break; 1419 case NVME_FEAT_ASYNC_EVENT: 1420 status = nvmet_set_feat_async_event(req, NVMET_AEN_CFG_ALL); 1421 break; 1422 case NVME_FEAT_HOST_ID: 1423 status = nvmet_set_feat_host_id(req); 1424 break; 1425 case NVME_FEAT_WRITE_PROTECT: 1426 status = nvmet_set_feat_write_protect(req); 1427 break; 1428 case NVME_FEAT_RESV_MASK: 1429 status = nvmet_set_feat_resv_notif_mask(req, cdw11); 1430 break; 1431 default: 1432 req->error_loc = offsetof(struct nvme_common_command, cdw10); 1433 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1434 break; 1435 } 1436 1437 nvmet_req_complete(req, status); 1438 } 1439 1440 static u16 nvmet_get_feat_write_protect(struct nvmet_req *req) 1441 { 1442 struct nvmet_subsys *subsys = nvmet_req_subsys(req); 1443 u32 result; 1444 1445 result = nvmet_req_find_ns(req); 1446 if (result) 1447 return result; 1448 1449 mutex_lock(&subsys->lock); 1450 if (req->ns->readonly == true) 1451 result = NVME_NS_WRITE_PROTECT; 1452 else 1453 result = NVME_NS_NO_WRITE_PROTECT; 1454 nvmet_set_result(req, result); 1455 mutex_unlock(&subsys->lock); 1456 1457 return 0; 1458 } 1459 1460 static u16 nvmet_get_feat_irq_coalesce(struct nvmet_req *req) 1461 { 1462 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1463 struct nvmet_feat_irq_coalesce irqc = { }; 1464 u16 status; 1465 1466 /* 1467 * This feature is not supported for fabrics controllers and mandatory 1468 * for PCI controllers. 1469 */ 1470 if (!nvmet_is_pci_ctrl(ctrl)) { 1471 req->error_loc = offsetof(struct nvme_common_command, cdw10); 1472 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1473 } 1474 1475 status = ctrl->ops->get_feature(ctrl, NVME_FEAT_IRQ_COALESCE, &irqc); 1476 if (status != NVME_SC_SUCCESS) 1477 return status; 1478 1479 nvmet_set_result(req, ((u32)irqc.time << 8) | (u32)irqc.thr); 1480 1481 return NVME_SC_SUCCESS; 1482 } 1483 1484 static u16 nvmet_get_feat_irq_config(struct nvmet_req *req) 1485 { 1486 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1487 u32 iv = le32_to_cpu(req->cmd->common.cdw11) & 0xffff; 1488 struct nvmet_feat_irq_config irqcfg = { .iv = iv }; 1489 u16 status; 1490 1491 /* 1492 * This feature is not supported for fabrics controllers and mandatory 1493 * for PCI controllers. 1494 */ 1495 if (!nvmet_is_pci_ctrl(ctrl)) { 1496 req->error_loc = offsetof(struct nvme_common_command, cdw10); 1497 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1498 } 1499 1500 status = ctrl->ops->get_feature(ctrl, NVME_FEAT_IRQ_CONFIG, &irqcfg); 1501 if (status != NVME_SC_SUCCESS) 1502 return status; 1503 1504 nvmet_set_result(req, ((u32)irqcfg.cd << 16) | iv); 1505 1506 return NVME_SC_SUCCESS; 1507 } 1508 1509 static u16 nvmet_get_feat_arbitration(struct nvmet_req *req) 1510 { 1511 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1512 struct nvmet_feat_arbitration arb = { }; 1513 u16 status; 1514 1515 if (!ctrl->ops->get_feature) { 1516 req->error_loc = offsetof(struct nvme_common_command, cdw10); 1517 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1518 } 1519 1520 status = ctrl->ops->get_feature(ctrl, NVME_FEAT_ARBITRATION, &arb); 1521 if (status != NVME_SC_SUCCESS) 1522 return status; 1523 1524 nvmet_set_result(req, 1525 ((u32)arb.hpw << 24) | 1526 ((u32)arb.mpw << 16) | 1527 ((u32)arb.lpw << 8) | 1528 (arb.ab & 0x7)); 1529 1530 return NVME_SC_SUCCESS; 1531 } 1532 1533 void nvmet_get_feat_kato(struct nvmet_req *req) 1534 { 1535 nvmet_set_result(req, req->sq->ctrl->kato * 1000); 1536 } 1537 1538 void nvmet_get_feat_async_event(struct nvmet_req *req) 1539 { 1540 nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled)); 1541 } 1542 1543 void nvmet_execute_get_features(struct nvmet_req *req) 1544 { 1545 struct nvmet_ctrl *ctrl = nvmet_req_ctrl(req); 1546 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10); 1547 u16 status = 0; 1548 1549 if (!nvmet_check_transfer_len(req, nvmet_feat_data_len(req, cdw10))) 1550 return; 1551 1552 switch (cdw10 & 0xff) { 1553 /* 1554 * These features are mandatory in the spec, but we don't 1555 * have a useful way to implement them. We'll eventually 1556 * need to come up with some fake values for these. 1557 */ 1558 #if 0 1559 case NVME_FEAT_POWER_MGMT: 1560 break; 1561 case NVME_FEAT_TEMP_THRESH: 1562 break; 1563 case NVME_FEAT_ERR_RECOVERY: 1564 break; 1565 case NVME_FEAT_WRITE_ATOMIC: 1566 break; 1567 #endif 1568 case NVME_FEAT_ARBITRATION: 1569 status = nvmet_get_feat_arbitration(req); 1570 break; 1571 case NVME_FEAT_IRQ_COALESCE: 1572 status = nvmet_get_feat_irq_coalesce(req); 1573 break; 1574 case NVME_FEAT_IRQ_CONFIG: 1575 status = nvmet_get_feat_irq_config(req); 1576 break; 1577 case NVME_FEAT_ASYNC_EVENT: 1578 nvmet_get_feat_async_event(req); 1579 break; 1580 case NVME_FEAT_VOLATILE_WC: 1581 nvmet_set_result(req, 1); 1582 break; 1583 case NVME_FEAT_NUM_QUEUES: 1584 nvmet_set_result(req, 1585 (ctrl->max_qid-1) | ((ctrl->max_qid-1) << 16)); 1586 break; 1587 case NVME_FEAT_KATO: 1588 nvmet_get_feat_kato(req); 1589 break; 1590 case NVME_FEAT_HOST_ID: 1591 /* need 128-bit host identifier flag */ 1592 if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) { 1593 req->error_loc = 1594 offsetof(struct nvme_common_command, cdw11); 1595 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1596 break; 1597 } 1598 1599 status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid, 1600 sizeof(req->sq->ctrl->hostid)); 1601 break; 1602 case NVME_FEAT_WRITE_PROTECT: 1603 status = nvmet_get_feat_write_protect(req); 1604 break; 1605 case NVME_FEAT_RESV_MASK: 1606 status = nvmet_get_feat_resv_notif_mask(req); 1607 break; 1608 default: 1609 req->error_loc = 1610 offsetof(struct nvme_common_command, cdw10); 1611 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1612 break; 1613 } 1614 1615 nvmet_req_complete(req, status); 1616 } 1617 1618 void nvmet_execute_async_event(struct nvmet_req *req) 1619 { 1620 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1621 1622 if (!nvmet_check_transfer_len(req, 0)) 1623 return; 1624 1625 mutex_lock(&ctrl->lock); 1626 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) { 1627 mutex_unlock(&ctrl->lock); 1628 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_STATUS_DNR); 1629 return; 1630 } 1631 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req; 1632 mutex_unlock(&ctrl->lock); 1633 1634 queue_work(nvmet_aen_wq, &ctrl->async_event_work); 1635 } 1636 1637 void nvmet_execute_keep_alive(struct nvmet_req *req) 1638 { 1639 struct nvmet_ctrl *ctrl = req->sq->ctrl; 1640 u16 status = 0; 1641 1642 if (!nvmet_check_transfer_len(req, 0)) 1643 return; 1644 1645 if (!ctrl->kato) { 1646 status = NVME_SC_KA_TIMEOUT_INVALID; 1647 goto out; 1648 } 1649 1650 pr_debug("ctrl %d update keep-alive timer for %d secs\n", 1651 ctrl->cntlid, ctrl->kato); 1652 mod_delayed_work(system_percpu_wq, &ctrl->ka_work, ctrl->kato * HZ); 1653 out: 1654 nvmet_req_complete(req, status); 1655 } 1656 1657 u32 nvmet_admin_cmd_data_len(struct nvmet_req *req) 1658 { 1659 struct nvme_command *cmd = req->cmd; 1660 1661 if (nvme_is_fabrics(cmd)) 1662 return nvmet_fabrics_admin_cmd_data_len(req); 1663 if (nvmet_is_disc_subsys(nvmet_req_subsys(req))) 1664 return nvmet_discovery_cmd_data_len(req); 1665 1666 switch (cmd->common.opcode) { 1667 case nvme_admin_get_log_page: 1668 return nvmet_get_log_page_len(cmd); 1669 case nvme_admin_identify: 1670 return NVME_IDENTIFY_DATA_SIZE; 1671 case nvme_admin_get_features: 1672 return nvmet_feat_data_len(req, le32_to_cpu(cmd->common.cdw10)); 1673 default: 1674 return 0; 1675 } 1676 } 1677 1678 u16 nvmet_parse_admin_cmd(struct nvmet_req *req) 1679 { 1680 struct nvme_command *cmd = req->cmd; 1681 u16 ret; 1682 1683 if (nvme_is_fabrics(cmd)) 1684 return nvmet_parse_fabrics_admin_cmd(req); 1685 if (nvmet_is_disc_subsys(nvmet_req_subsys(req))) 1686 return nvmet_parse_discovery_cmd(req); 1687 1688 ret = nvmet_check_ctrl_status(req); 1689 if (unlikely(ret)) 1690 return ret; 1691 1692 /* For PCI controllers, admin commands shall not use SGL. */ 1693 if (nvmet_is_pci_ctrl(req->sq->ctrl) && !req->sq->qid && 1694 cmd->common.flags & NVME_CMD_SGL_ALL) 1695 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR; 1696 1697 if (nvmet_is_passthru_req(req)) 1698 return nvmet_parse_passthru_admin_cmd(req); 1699 1700 switch (cmd->common.opcode) { 1701 case nvme_admin_delete_sq: 1702 req->execute = nvmet_execute_delete_sq; 1703 return 0; 1704 case nvme_admin_create_sq: 1705 req->execute = nvmet_execute_create_sq; 1706 return 0; 1707 case nvme_admin_get_log_page: 1708 req->execute = nvmet_execute_get_log_page; 1709 return 0; 1710 case nvme_admin_delete_cq: 1711 req->execute = nvmet_execute_delete_cq; 1712 return 0; 1713 case nvme_admin_create_cq: 1714 req->execute = nvmet_execute_create_cq; 1715 return 0; 1716 case nvme_admin_identify: 1717 req->execute = nvmet_execute_identify; 1718 return 0; 1719 case nvme_admin_abort_cmd: 1720 req->execute = nvmet_execute_abort; 1721 return 0; 1722 case nvme_admin_set_features: 1723 req->execute = nvmet_execute_set_features; 1724 return 0; 1725 case nvme_admin_get_features: 1726 req->execute = nvmet_execute_get_features; 1727 return 0; 1728 case nvme_admin_async_event: 1729 req->execute = nvmet_execute_async_event; 1730 return 0; 1731 case nvme_admin_keep_alive: 1732 req->execute = nvmet_execute_keep_alive; 1733 return 0; 1734 default: 1735 return nvmet_report_invalid_opcode(req); 1736 } 1737 } 1738