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