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