1 /* 2 * NVMe admin command implementation. 3 * Copyright (c) 2015-2016 HGST, a Western Digital Company. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms and conditions of the GNU General Public License, 7 * version 2, as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 * more details. 13 */ 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 #include <linux/module.h> 16 #include <linux/rculist.h> 17 18 #include <generated/utsrelease.h> 19 #include <asm/unaligned.h> 20 #include "nvmet.h" 21 22 u32 nvmet_get_log_page_len(struct nvme_command *cmd) 23 { 24 u32 len = le16_to_cpu(cmd->get_log_page.numdu); 25 26 len <<= 16; 27 len += le16_to_cpu(cmd->get_log_page.numdl); 28 /* NUMD is a 0's based value */ 29 len += 1; 30 len *= sizeof(u32); 31 32 return len; 33 } 34 35 static void nvmet_execute_get_log_page_noop(struct nvmet_req *req) 36 { 37 nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->data_len)); 38 } 39 40 static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req, 41 struct nvme_smart_log *slog) 42 { 43 struct nvmet_ns *ns; 44 u64 host_reads, host_writes, data_units_read, data_units_written; 45 46 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->get_log_page.nsid); 47 if (!ns) { 48 pr_err("nvmet : Could not find namespace id : %d\n", 49 le32_to_cpu(req->cmd->get_log_page.nsid)); 50 return NVME_SC_INVALID_NS; 51 } 52 53 /* we don't have the right data for file backed ns */ 54 if (!ns->bdev) 55 goto out; 56 57 host_reads = part_stat_read(ns->bdev->bd_part, ios[READ]); 58 data_units_read = part_stat_read(ns->bdev->bd_part, sectors[READ]); 59 host_writes = part_stat_read(ns->bdev->bd_part, ios[WRITE]); 60 data_units_written = part_stat_read(ns->bdev->bd_part, sectors[WRITE]); 61 62 put_unaligned_le64(host_reads, &slog->host_reads[0]); 63 put_unaligned_le64(data_units_read, &slog->data_units_read[0]); 64 put_unaligned_le64(host_writes, &slog->host_writes[0]); 65 put_unaligned_le64(data_units_written, &slog->data_units_written[0]); 66 out: 67 nvmet_put_namespace(ns); 68 69 return NVME_SC_SUCCESS; 70 } 71 72 static u16 nvmet_get_smart_log_all(struct nvmet_req *req, 73 struct nvme_smart_log *slog) 74 { 75 u64 host_reads = 0, host_writes = 0; 76 u64 data_units_read = 0, data_units_written = 0; 77 struct nvmet_ns *ns; 78 struct nvmet_ctrl *ctrl; 79 80 ctrl = req->sq->ctrl; 81 82 rcu_read_lock(); 83 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) { 84 /* we don't have the right data for file backed ns */ 85 if (!ns->bdev) 86 continue; 87 host_reads += part_stat_read(ns->bdev->bd_part, ios[READ]); 88 data_units_read += 89 part_stat_read(ns->bdev->bd_part, sectors[READ]); 90 host_writes += part_stat_read(ns->bdev->bd_part, ios[WRITE]); 91 data_units_written += 92 part_stat_read(ns->bdev->bd_part, sectors[WRITE]); 93 94 } 95 rcu_read_unlock(); 96 97 put_unaligned_le64(host_reads, &slog->host_reads[0]); 98 put_unaligned_le64(data_units_read, &slog->data_units_read[0]); 99 put_unaligned_le64(host_writes, &slog->host_writes[0]); 100 put_unaligned_le64(data_units_written, &slog->data_units_written[0]); 101 102 return NVME_SC_SUCCESS; 103 } 104 105 static void nvmet_execute_get_log_page_smart(struct nvmet_req *req) 106 { 107 struct nvme_smart_log *log; 108 u16 status = NVME_SC_INTERNAL; 109 110 if (req->data_len != sizeof(*log)) 111 goto out; 112 113 log = kzalloc(sizeof(*log), GFP_KERNEL); 114 if (!log) 115 goto out; 116 117 if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL)) 118 status = nvmet_get_smart_log_all(req, log); 119 else 120 status = nvmet_get_smart_log_nsid(req, log); 121 if (status) 122 goto out_free_log; 123 124 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log)); 125 out_free_log: 126 kfree(log); 127 out: 128 nvmet_req_complete(req, status); 129 } 130 131 static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req) 132 { 133 struct nvmet_ctrl *ctrl = req->sq->ctrl; 134 u16 status = NVME_SC_INTERNAL; 135 size_t len; 136 137 if (req->data_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32)) 138 goto out; 139 140 mutex_lock(&ctrl->lock); 141 if (ctrl->nr_changed_ns == U32_MAX) 142 len = sizeof(__le32); 143 else 144 len = ctrl->nr_changed_ns * sizeof(__le32); 145 status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len); 146 if (!status) 147 status = nvmet_zero_sgl(req, len, req->data_len - len); 148 ctrl->nr_changed_ns = 0; 149 clear_bit(NVME_AEN_CFG_NS_ATTR, &ctrl->aen_masked); 150 mutex_unlock(&ctrl->lock); 151 out: 152 nvmet_req_complete(req, status); 153 } 154 155 static void nvmet_execute_identify_ctrl(struct nvmet_req *req) 156 { 157 struct nvmet_ctrl *ctrl = req->sq->ctrl; 158 struct nvme_id_ctrl *id; 159 u16 status = 0; 160 const char model[] = "Linux"; 161 162 id = kzalloc(sizeof(*id), GFP_KERNEL); 163 if (!id) { 164 status = NVME_SC_INTERNAL; 165 goto out; 166 } 167 168 /* XXX: figure out how to assign real vendors IDs. */ 169 id->vid = 0; 170 id->ssvid = 0; 171 172 memset(id->sn, ' ', sizeof(id->sn)); 173 bin2hex(id->sn, &ctrl->subsys->serial, 174 min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2)); 175 memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' '); 176 memcpy_and_pad(id->fr, sizeof(id->fr), 177 UTS_RELEASE, strlen(UTS_RELEASE), ' '); 178 179 id->rab = 6; 180 181 /* 182 * XXX: figure out how we can assign a IEEE OUI, but until then 183 * the safest is to leave it as zeroes. 184 */ 185 186 /* we support multiple ports and multiples hosts: */ 187 id->cmic = (1 << 0) | (1 << 1); 188 189 /* no limit on data transfer sizes for now */ 190 id->mdts = 0; 191 id->cntlid = cpu_to_le16(ctrl->cntlid); 192 id->ver = cpu_to_le32(ctrl->subsys->ver); 193 194 /* XXX: figure out what to do about RTD3R/RTD3 */ 195 id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL); 196 id->ctratt = cpu_to_le32(1 << 0); 197 198 id->oacs = 0; 199 200 /* 201 * We don't really have a practical limit on the number of abort 202 * comands. But we don't do anything useful for abort either, so 203 * no point in allowing more abort commands than the spec requires. 204 */ 205 id->acl = 3; 206 207 id->aerl = NVMET_ASYNC_EVENTS - 1; 208 209 /* first slot is read-only, only one slot supported */ 210 id->frmw = (1 << 0) | (1 << 1); 211 id->lpa = (1 << 0) | (1 << 2); 212 id->elpe = NVMET_ERROR_LOG_SLOTS - 1; 213 id->npss = 0; 214 215 /* We support keep-alive timeout in granularity of seconds */ 216 id->kas = cpu_to_le16(NVMET_KAS); 217 218 id->sqes = (0x6 << 4) | 0x6; 219 id->cqes = (0x4 << 4) | 0x4; 220 221 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */ 222 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD); 223 224 id->nn = cpu_to_le32(ctrl->subsys->max_nsid); 225 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM | 226 NVME_CTRL_ONCS_WRITE_ZEROES); 227 228 /* XXX: don't report vwc if the underlying device is write through */ 229 id->vwc = NVME_CTRL_VWC_PRESENT; 230 231 /* 232 * We can't support atomic writes bigger than a LBA without support 233 * from the backend device. 234 */ 235 id->awun = 0; 236 id->awupf = 0; 237 238 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */ 239 if (ctrl->ops->has_keyed_sgls) 240 id->sgls |= cpu_to_le32(1 << 2); 241 if (ctrl->ops->sqe_inline_size) 242 id->sgls |= cpu_to_le32(1 << 20); 243 244 strcpy(id->subnqn, ctrl->subsys->subsysnqn); 245 246 /* Max command capsule size is sqe + single page of in-capsule data */ 247 id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) + 248 ctrl->ops->sqe_inline_size) / 16); 249 /* Max response capsule size is cqe */ 250 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16); 251 252 id->msdbd = ctrl->ops->msdbd; 253 254 /* 255 * Meh, we don't really support any power state. Fake up the same 256 * values that qemu does. 257 */ 258 id->psd[0].max_power = cpu_to_le16(0x9c4); 259 id->psd[0].entry_lat = cpu_to_le32(0x10); 260 id->psd[0].exit_lat = cpu_to_le32(0x4); 261 262 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); 263 264 kfree(id); 265 out: 266 nvmet_req_complete(req, status); 267 } 268 269 static void nvmet_execute_identify_ns(struct nvmet_req *req) 270 { 271 struct nvmet_ns *ns; 272 struct nvme_id_ns *id; 273 u16 status = 0; 274 275 if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) { 276 status = NVME_SC_INVALID_NS | NVME_SC_DNR; 277 goto out; 278 } 279 280 id = kzalloc(sizeof(*id), GFP_KERNEL); 281 if (!id) { 282 status = NVME_SC_INTERNAL; 283 goto out; 284 } 285 286 /* return an all zeroed buffer if we can't find an active namespace */ 287 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid); 288 if (!ns) 289 goto done; 290 291 /* 292 * nuse = ncap = nsze isn't always true, but we have no way to find 293 * that out from the underlying device. 294 */ 295 id->ncap = id->nuse = id->nsze = 296 cpu_to_le64(ns->size >> ns->blksize_shift); 297 298 /* 299 * We just provide a single LBA format that matches what the 300 * underlying device reports. 301 */ 302 id->nlbaf = 0; 303 id->flbas = 0; 304 305 /* 306 * Our namespace might always be shared. Not just with other 307 * controllers, but also with any other user of the block device. 308 */ 309 id->nmic = (1 << 0); 310 311 memcpy(&id->nguid, &ns->nguid, sizeof(uuid_le)); 312 313 id->lbaf[0].ds = ns->blksize_shift; 314 315 nvmet_put_namespace(ns); 316 done: 317 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); 318 kfree(id); 319 out: 320 nvmet_req_complete(req, status); 321 } 322 323 static void nvmet_execute_identify_nslist(struct nvmet_req *req) 324 { 325 static const int buf_size = NVME_IDENTIFY_DATA_SIZE; 326 struct nvmet_ctrl *ctrl = req->sq->ctrl; 327 struct nvmet_ns *ns; 328 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid); 329 __le32 *list; 330 u16 status = 0; 331 int i = 0; 332 333 list = kzalloc(buf_size, GFP_KERNEL); 334 if (!list) { 335 status = NVME_SC_INTERNAL; 336 goto out; 337 } 338 339 rcu_read_lock(); 340 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) { 341 if (ns->nsid <= min_nsid) 342 continue; 343 list[i++] = cpu_to_le32(ns->nsid); 344 if (i == buf_size / sizeof(__le32)) 345 break; 346 } 347 rcu_read_unlock(); 348 349 status = nvmet_copy_to_sgl(req, 0, list, buf_size); 350 351 kfree(list); 352 out: 353 nvmet_req_complete(req, status); 354 } 355 356 static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len, 357 void *id, off_t *off) 358 { 359 struct nvme_ns_id_desc desc = { 360 .nidt = type, 361 .nidl = len, 362 }; 363 u16 status; 364 365 status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc)); 366 if (status) 367 return status; 368 *off += sizeof(desc); 369 370 status = nvmet_copy_to_sgl(req, *off, id, len); 371 if (status) 372 return status; 373 *off += len; 374 375 return 0; 376 } 377 378 static void nvmet_execute_identify_desclist(struct nvmet_req *req) 379 { 380 struct nvmet_ns *ns; 381 u16 status = 0; 382 off_t off = 0; 383 384 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid); 385 if (!ns) { 386 status = NVME_SC_INVALID_NS | NVME_SC_DNR; 387 goto out; 388 } 389 390 if (memchr_inv(&ns->uuid, 0, sizeof(ns->uuid))) { 391 status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID, 392 NVME_NIDT_UUID_LEN, 393 &ns->uuid, &off); 394 if (status) 395 goto out_put_ns; 396 } 397 if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid))) { 398 status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID, 399 NVME_NIDT_NGUID_LEN, 400 &ns->nguid, &off); 401 if (status) 402 goto out_put_ns; 403 } 404 405 if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off, 406 off) != NVME_IDENTIFY_DATA_SIZE - off) 407 status = NVME_SC_INTERNAL | NVME_SC_DNR; 408 out_put_ns: 409 nvmet_put_namespace(ns); 410 out: 411 nvmet_req_complete(req, status); 412 } 413 414 /* 415 * A "minimum viable" abort implementation: the command is mandatory in the 416 * spec, but we are not required to do any useful work. We couldn't really 417 * do a useful abort, so don't bother even with waiting for the command 418 * to be exectuted and return immediately telling the command to abort 419 * wasn't found. 420 */ 421 static void nvmet_execute_abort(struct nvmet_req *req) 422 { 423 nvmet_set_result(req, 1); 424 nvmet_req_complete(req, 0); 425 } 426 427 static void nvmet_execute_set_features(struct nvmet_req *req) 428 { 429 struct nvmet_subsys *subsys = req->sq->ctrl->subsys; 430 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]); 431 u32 val32; 432 u16 status = 0; 433 434 switch (cdw10 & 0xff) { 435 case NVME_FEAT_NUM_QUEUES: 436 nvmet_set_result(req, 437 (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16)); 438 break; 439 case NVME_FEAT_KATO: 440 val32 = le32_to_cpu(req->cmd->common.cdw10[1]); 441 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000); 442 nvmet_set_result(req, req->sq->ctrl->kato); 443 break; 444 case NVME_FEAT_ASYNC_EVENT: 445 val32 = le32_to_cpu(req->cmd->common.cdw10[1]); 446 if (val32 & ~NVMET_AEN_CFG_ALL) { 447 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; 448 break; 449 } 450 451 WRITE_ONCE(req->sq->ctrl->aen_enabled, val32); 452 nvmet_set_result(req, val32); 453 break; 454 case NVME_FEAT_HOST_ID: 455 status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR; 456 break; 457 default: 458 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; 459 break; 460 } 461 462 nvmet_req_complete(req, status); 463 } 464 465 static void nvmet_execute_get_features(struct nvmet_req *req) 466 { 467 struct nvmet_subsys *subsys = req->sq->ctrl->subsys; 468 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]); 469 u16 status = 0; 470 471 switch (cdw10 & 0xff) { 472 /* 473 * These features are mandatory in the spec, but we don't 474 * have a useful way to implement them. We'll eventually 475 * need to come up with some fake values for these. 476 */ 477 #if 0 478 case NVME_FEAT_ARBITRATION: 479 break; 480 case NVME_FEAT_POWER_MGMT: 481 break; 482 case NVME_FEAT_TEMP_THRESH: 483 break; 484 case NVME_FEAT_ERR_RECOVERY: 485 break; 486 case NVME_FEAT_IRQ_COALESCE: 487 break; 488 case NVME_FEAT_IRQ_CONFIG: 489 break; 490 case NVME_FEAT_WRITE_ATOMIC: 491 break; 492 #endif 493 case NVME_FEAT_ASYNC_EVENT: 494 nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled)); 495 break; 496 case NVME_FEAT_VOLATILE_WC: 497 nvmet_set_result(req, 1); 498 break; 499 case NVME_FEAT_NUM_QUEUES: 500 nvmet_set_result(req, 501 (subsys->max_qid-1) | ((subsys->max_qid-1) << 16)); 502 break; 503 case NVME_FEAT_KATO: 504 nvmet_set_result(req, req->sq->ctrl->kato * 1000); 505 break; 506 case NVME_FEAT_HOST_ID: 507 /* need 128-bit host identifier flag */ 508 if (!(req->cmd->common.cdw10[1] & cpu_to_le32(1 << 0))) { 509 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; 510 break; 511 } 512 513 status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid, 514 sizeof(req->sq->ctrl->hostid)); 515 break; 516 default: 517 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; 518 break; 519 } 520 521 nvmet_req_complete(req, status); 522 } 523 524 static void nvmet_execute_async_event(struct nvmet_req *req) 525 { 526 struct nvmet_ctrl *ctrl = req->sq->ctrl; 527 528 mutex_lock(&ctrl->lock); 529 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) { 530 mutex_unlock(&ctrl->lock); 531 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR); 532 return; 533 } 534 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req; 535 mutex_unlock(&ctrl->lock); 536 537 schedule_work(&ctrl->async_event_work); 538 } 539 540 static void nvmet_execute_keep_alive(struct nvmet_req *req) 541 { 542 struct nvmet_ctrl *ctrl = req->sq->ctrl; 543 544 pr_debug("ctrl %d update keep-alive timer for %d secs\n", 545 ctrl->cntlid, ctrl->kato); 546 547 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ); 548 nvmet_req_complete(req, 0); 549 } 550 551 u16 nvmet_parse_admin_cmd(struct nvmet_req *req) 552 { 553 struct nvme_command *cmd = req->cmd; 554 u16 ret; 555 556 ret = nvmet_check_ctrl_status(req, cmd); 557 if (unlikely(ret)) 558 return ret; 559 560 switch (cmd->common.opcode) { 561 case nvme_admin_get_log_page: 562 req->data_len = nvmet_get_log_page_len(cmd); 563 564 switch (cmd->get_log_page.lid) { 565 case NVME_LOG_ERROR: 566 /* 567 * We currently never set the More bit in the status 568 * field, so all error log entries are invalid and can 569 * be zeroed out. This is called a minum viable 570 * implementation (TM) of this mandatory log page. 571 */ 572 req->execute = nvmet_execute_get_log_page_noop; 573 return 0; 574 case NVME_LOG_SMART: 575 req->execute = nvmet_execute_get_log_page_smart; 576 return 0; 577 case NVME_LOG_FW_SLOT: 578 /* 579 * We only support a single firmware slot which always 580 * is active, so we can zero out the whole firmware slot 581 * log and still claim to fully implement this mandatory 582 * log page. 583 */ 584 req->execute = nvmet_execute_get_log_page_noop; 585 return 0; 586 case NVME_LOG_CHANGED_NS: 587 req->execute = nvmet_execute_get_log_changed_ns; 588 return 0; 589 } 590 break; 591 case nvme_admin_identify: 592 req->data_len = NVME_IDENTIFY_DATA_SIZE; 593 switch (cmd->identify.cns) { 594 case NVME_ID_CNS_NS: 595 req->execute = nvmet_execute_identify_ns; 596 return 0; 597 case NVME_ID_CNS_CTRL: 598 req->execute = nvmet_execute_identify_ctrl; 599 return 0; 600 case NVME_ID_CNS_NS_ACTIVE_LIST: 601 req->execute = nvmet_execute_identify_nslist; 602 return 0; 603 case NVME_ID_CNS_NS_DESC_LIST: 604 req->execute = nvmet_execute_identify_desclist; 605 return 0; 606 } 607 break; 608 case nvme_admin_abort_cmd: 609 req->execute = nvmet_execute_abort; 610 req->data_len = 0; 611 return 0; 612 case nvme_admin_set_features: 613 req->execute = nvmet_execute_set_features; 614 req->data_len = 0; 615 return 0; 616 case nvme_admin_get_features: 617 req->execute = nvmet_execute_get_features; 618 req->data_len = 0; 619 return 0; 620 case nvme_admin_async_event: 621 req->execute = nvmet_execute_async_event; 622 req->data_len = 0; 623 return 0; 624 case nvme_admin_keep_alive: 625 req->execute = nvmet_execute_keep_alive; 626 req->data_len = 0; 627 return 0; 628 } 629 630 pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode, 631 req->sq->qid); 632 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; 633 } 634