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