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/random.h> 17 #include <generated/utsrelease.h> 18 #include "nvmet.h" 19 20 u32 nvmet_get_log_page_len(struct nvme_command *cmd) 21 { 22 u32 len = le16_to_cpu(cmd->get_log_page.numdu); 23 24 len <<= 16; 25 len += le16_to_cpu(cmd->get_log_page.numdl); 26 /* NUMD is a 0's based value */ 27 len += 1; 28 len *= sizeof(u32); 29 30 return len; 31 } 32 33 static void nvmet_execute_get_log_page(struct nvmet_req *req) 34 { 35 size_t data_len = nvmet_get_log_page_len(req->cmd); 36 void *buf; 37 u16 status = 0; 38 39 buf = kzalloc(data_len, GFP_KERNEL); 40 if (!buf) { 41 status = NVME_SC_INTERNAL; 42 goto out; 43 } 44 45 switch (req->cmd->get_log_page.lid) { 46 case 0x01: 47 /* 48 * We currently never set the More bit in the status field, 49 * so all error log entries are invalid and can be zeroed out. 50 * This is called a minum viable implementation (TM) of this 51 * mandatory log page. 52 */ 53 break; 54 case 0x02: 55 /* 56 * XXX: fill out actual smart log 57 * 58 * We might have a hard time coming up with useful values for 59 * many of the fields, and even when we have useful data 60 * available (e.g. units or commands read/written) those aren't 61 * persistent over power loss. 62 */ 63 break; 64 case 0x03: 65 /* 66 * We only support a single firmware slot which always is 67 * active, so we can zero out the whole firmware slot log and 68 * still claim to fully implement this mandatory log page. 69 */ 70 break; 71 default: 72 BUG(); 73 } 74 75 status = nvmet_copy_to_sgl(req, 0, buf, data_len); 76 77 kfree(buf); 78 out: 79 nvmet_req_complete(req, status); 80 } 81 82 static void nvmet_execute_identify_ctrl(struct nvmet_req *req) 83 { 84 struct nvmet_ctrl *ctrl = req->sq->ctrl; 85 struct nvme_id_ctrl *id; 86 u64 serial; 87 u16 status = 0; 88 89 id = kzalloc(sizeof(*id), GFP_KERNEL); 90 if (!id) { 91 status = NVME_SC_INTERNAL; 92 goto out; 93 } 94 95 /* XXX: figure out how to assign real vendors IDs. */ 96 id->vid = 0; 97 id->ssvid = 0; 98 99 /* generate a random serial number as our controllers are ephemeral: */ 100 get_random_bytes(&serial, sizeof(serial)); 101 memset(id->sn, ' ', sizeof(id->sn)); 102 snprintf(id->sn, sizeof(id->sn), "%llx", serial); 103 104 memset(id->mn, ' ', sizeof(id->mn)); 105 strncpy((char *)id->mn, "Linux", sizeof(id->mn)); 106 107 memset(id->fr, ' ', sizeof(id->fr)); 108 strncpy((char *)id->fr, UTS_RELEASE, sizeof(id->fr)); 109 110 id->rab = 6; 111 112 /* 113 * XXX: figure out how we can assign a IEEE OUI, but until then 114 * the safest is to leave it as zeroes. 115 */ 116 117 /* we support multiple ports and multiples hosts: */ 118 id->mic = (1 << 0) | (1 << 1); 119 120 /* no limit on data transfer sizes for now */ 121 id->mdts = 0; 122 id->cntlid = cpu_to_le16(ctrl->cntlid); 123 id->ver = cpu_to_le32(ctrl->subsys->ver); 124 125 /* XXX: figure out what to do about RTD3R/RTD3 */ 126 id->oaes = cpu_to_le32(1 << 8); 127 id->ctratt = cpu_to_le32(1 << 0); 128 129 id->oacs = 0; 130 131 /* 132 * We don't really have a practical limit on the number of abort 133 * comands. But we don't do anything useful for abort either, so 134 * no point in allowing more abort commands than the spec requires. 135 */ 136 id->acl = 3; 137 138 id->aerl = NVMET_ASYNC_EVENTS - 1; 139 140 /* first slot is read-only, only one slot supported */ 141 id->frmw = (1 << 0) | (1 << 1); 142 id->lpa = (1 << 0) | (1 << 2); 143 id->elpe = NVMET_ERROR_LOG_SLOTS - 1; 144 id->npss = 0; 145 146 /* We support keep-alive timeout in granularity of seconds */ 147 id->kas = cpu_to_le16(NVMET_KAS); 148 149 id->sqes = (0x6 << 4) | 0x6; 150 id->cqes = (0x4 << 4) | 0x4; 151 152 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */ 153 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD); 154 155 id->nn = cpu_to_le32(ctrl->subsys->max_nsid); 156 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM); 157 158 /* XXX: don't report vwc if the underlying device is write through */ 159 id->vwc = NVME_CTRL_VWC_PRESENT; 160 161 /* 162 * We can't support atomic writes bigger than a LBA without support 163 * from the backend device. 164 */ 165 id->awun = 0; 166 id->awupf = 0; 167 168 id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */ 169 if (ctrl->ops->has_keyed_sgls) 170 id->sgls |= cpu_to_le32(1 << 2); 171 if (ctrl->ops->sqe_inline_size) 172 id->sgls |= cpu_to_le32(1 << 20); 173 174 strcpy(id->subnqn, ctrl->subsys->subsysnqn); 175 176 /* Max command capsule size is sqe + single page of in-capsule data */ 177 id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) + 178 ctrl->ops->sqe_inline_size) / 16); 179 /* Max response capsule size is cqe */ 180 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16); 181 182 id->msdbd = ctrl->ops->msdbd; 183 184 /* 185 * Meh, we don't really support any power state. Fake up the same 186 * values that qemu does. 187 */ 188 id->psd[0].max_power = cpu_to_le16(0x9c4); 189 id->psd[0].entry_lat = cpu_to_le32(0x10); 190 id->psd[0].exit_lat = cpu_to_le32(0x4); 191 192 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); 193 194 kfree(id); 195 out: 196 nvmet_req_complete(req, status); 197 } 198 199 static void nvmet_execute_identify_ns(struct nvmet_req *req) 200 { 201 struct nvmet_ns *ns; 202 struct nvme_id_ns *id; 203 u16 status = 0; 204 205 ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid); 206 if (!ns) { 207 status = NVME_SC_INVALID_NS | NVME_SC_DNR; 208 goto out; 209 } 210 211 id = kzalloc(sizeof(*id), GFP_KERNEL); 212 if (!id) { 213 status = NVME_SC_INTERNAL; 214 goto out_put_ns; 215 } 216 217 /* 218 * nuse = ncap = nsze isn't aways true, but we have no way to find 219 * that out from the underlying device. 220 */ 221 id->ncap = id->nuse = id->nsze = 222 cpu_to_le64(ns->size >> ns->blksize_shift); 223 224 /* 225 * We just provide a single LBA format that matches what the 226 * underlying device reports. 227 */ 228 id->nlbaf = 0; 229 id->flbas = 0; 230 231 /* 232 * Our namespace might always be shared. Not just with other 233 * controllers, but also with any other user of the block device. 234 */ 235 id->nmic = (1 << 0); 236 237 memcpy(&id->nguid, &ns->nguid, sizeof(uuid_le)); 238 239 id->lbaf[0].ds = ns->blksize_shift; 240 241 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id)); 242 243 kfree(id); 244 out_put_ns: 245 nvmet_put_namespace(ns); 246 out: 247 nvmet_req_complete(req, status); 248 } 249 250 static void nvmet_execute_identify_nslist(struct nvmet_req *req) 251 { 252 static const int buf_size = 4096; 253 struct nvmet_ctrl *ctrl = req->sq->ctrl; 254 struct nvmet_ns *ns; 255 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid); 256 __le32 *list; 257 u16 status = 0; 258 int i = 0; 259 260 list = kzalloc(buf_size, GFP_KERNEL); 261 if (!list) { 262 status = NVME_SC_INTERNAL; 263 goto out; 264 } 265 266 rcu_read_lock(); 267 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) { 268 if (ns->nsid <= min_nsid) 269 continue; 270 list[i++] = cpu_to_le32(ns->nsid); 271 if (i == buf_size / sizeof(__le32)) 272 break; 273 } 274 rcu_read_unlock(); 275 276 status = nvmet_copy_to_sgl(req, 0, list, buf_size); 277 278 kfree(list); 279 out: 280 nvmet_req_complete(req, status); 281 } 282 283 /* 284 * A "mimimum viable" abort implementation: the command is mandatory in the 285 * spec, but we are not required to do any useful work. We couldn't really 286 * do a useful abort, so don't bother even with waiting for the command 287 * to be exectuted and return immediately telling the command to abort 288 * wasn't found. 289 */ 290 static void nvmet_execute_abort(struct nvmet_req *req) 291 { 292 nvmet_set_result(req, 1); 293 nvmet_req_complete(req, 0); 294 } 295 296 static void nvmet_execute_set_features(struct nvmet_req *req) 297 { 298 struct nvmet_subsys *subsys = req->sq->ctrl->subsys; 299 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]); 300 u64 val; 301 u32 val32; 302 u16 status = 0; 303 304 switch (cdw10 & 0xf) { 305 case NVME_FEAT_NUM_QUEUES: 306 nvmet_set_result(req, 307 (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16)); 308 break; 309 case NVME_FEAT_KATO: 310 val = le64_to_cpu(req->cmd->prop_set.value); 311 val32 = val & 0xffff; 312 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000); 313 nvmet_set_result(req, req->sq->ctrl->kato); 314 break; 315 default: 316 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; 317 break; 318 } 319 320 nvmet_req_complete(req, status); 321 } 322 323 static void nvmet_execute_get_features(struct nvmet_req *req) 324 { 325 struct nvmet_subsys *subsys = req->sq->ctrl->subsys; 326 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]); 327 u16 status = 0; 328 329 switch (cdw10 & 0xf) { 330 /* 331 * These features are mandatory in the spec, but we don't 332 * have a useful way to implement them. We'll eventually 333 * need to come up with some fake values for these. 334 */ 335 #if 0 336 case NVME_FEAT_ARBITRATION: 337 break; 338 case NVME_FEAT_POWER_MGMT: 339 break; 340 case NVME_FEAT_TEMP_THRESH: 341 break; 342 case NVME_FEAT_ERR_RECOVERY: 343 break; 344 case NVME_FEAT_IRQ_COALESCE: 345 break; 346 case NVME_FEAT_IRQ_CONFIG: 347 break; 348 case NVME_FEAT_WRITE_ATOMIC: 349 break; 350 case NVME_FEAT_ASYNC_EVENT: 351 break; 352 #endif 353 case NVME_FEAT_VOLATILE_WC: 354 nvmet_set_result(req, 1); 355 break; 356 case NVME_FEAT_NUM_QUEUES: 357 nvmet_set_result(req, 358 (subsys->max_qid-1) | ((subsys->max_qid-1) << 16)); 359 break; 360 case NVME_FEAT_KATO: 361 nvmet_set_result(req, req->sq->ctrl->kato * 1000); 362 break; 363 default: 364 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR; 365 break; 366 } 367 368 nvmet_req_complete(req, status); 369 } 370 371 static void nvmet_execute_async_event(struct nvmet_req *req) 372 { 373 struct nvmet_ctrl *ctrl = req->sq->ctrl; 374 375 mutex_lock(&ctrl->lock); 376 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) { 377 mutex_unlock(&ctrl->lock); 378 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR); 379 return; 380 } 381 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req; 382 mutex_unlock(&ctrl->lock); 383 384 schedule_work(&ctrl->async_event_work); 385 } 386 387 static void nvmet_execute_keep_alive(struct nvmet_req *req) 388 { 389 struct nvmet_ctrl *ctrl = req->sq->ctrl; 390 391 pr_debug("ctrl %d update keep-alive timer for %d secs\n", 392 ctrl->cntlid, ctrl->kato); 393 394 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ); 395 nvmet_req_complete(req, 0); 396 } 397 398 int nvmet_parse_admin_cmd(struct nvmet_req *req) 399 { 400 struct nvme_command *cmd = req->cmd; 401 402 req->ns = NULL; 403 404 if (unlikely(!(req->sq->ctrl->cc & NVME_CC_ENABLE))) { 405 pr_err("nvmet: got admin cmd %d while CC.EN == 0\n", 406 cmd->common.opcode); 407 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR; 408 } 409 if (unlikely(!(req->sq->ctrl->csts & NVME_CSTS_RDY))) { 410 pr_err("nvmet: got admin cmd %d while CSTS.RDY == 0\n", 411 cmd->common.opcode); 412 return NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR; 413 } 414 415 switch (cmd->common.opcode) { 416 case nvme_admin_get_log_page: 417 req->data_len = nvmet_get_log_page_len(cmd); 418 419 switch (cmd->get_log_page.lid) { 420 case 0x01: 421 case 0x02: 422 case 0x03: 423 req->execute = nvmet_execute_get_log_page; 424 return 0; 425 } 426 break; 427 case nvme_admin_identify: 428 req->data_len = 4096; 429 switch (le32_to_cpu(cmd->identify.cns)) { 430 case 0x00: 431 req->execute = nvmet_execute_identify_ns; 432 return 0; 433 case 0x01: 434 req->execute = nvmet_execute_identify_ctrl; 435 return 0; 436 case 0x02: 437 req->execute = nvmet_execute_identify_nslist; 438 return 0; 439 } 440 break; 441 case nvme_admin_abort_cmd: 442 req->execute = nvmet_execute_abort; 443 req->data_len = 0; 444 return 0; 445 case nvme_admin_set_features: 446 req->execute = nvmet_execute_set_features; 447 req->data_len = 0; 448 return 0; 449 case nvme_admin_get_features: 450 req->execute = nvmet_execute_get_features; 451 req->data_len = 0; 452 return 0; 453 case nvme_admin_async_event: 454 req->execute = nvmet_execute_async_event; 455 req->data_len = 0; 456 return 0; 457 case nvme_admin_keep_alive: 458 req->execute = nvmet_execute_keep_alive; 459 req->data_len = 0; 460 return 0; 461 } 462 463 pr_err("nvmet: unhandled cmd %d\n", cmd->common.opcode); 464 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; 465 } 466