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