]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/nvme/target/admin-cmd.c
nvmet: support for traffic based keep-alive
[linux.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 <linux/rculist.h>
17
18 #include <generated/utsrelease.h>
19 #include <asm/unaligned.h>
20 #include "nvmet.h"
21
22 /*
23  * This helper allows us to clear the AEN based on the RAE bit,
24  * Please use this helper when processing the log pages which are
25  * associated with the AEN.
26  */
27 static inline void nvmet_clear_aen(struct nvmet_req *req, u32 aen_bit)
28 {
29         int rae = le32_to_cpu(req->cmd->common.cdw10[0]) & 1 << 15;
30
31         if (!rae)
32                 clear_bit(aen_bit, &req->sq->ctrl->aen_masked);
33 }
34
35 u32 nvmet_get_log_page_len(struct nvme_command *cmd)
36 {
37         u32 len = le16_to_cpu(cmd->get_log_page.numdu);
38
39         len <<= 16;
40         len += le16_to_cpu(cmd->get_log_page.numdl);
41         /* NUMD is a 0's based value */
42         len += 1;
43         len *= sizeof(u32);
44
45         return len;
46 }
47
48 static void nvmet_execute_get_log_page_noop(struct nvmet_req *req)
49 {
50         nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->data_len));
51 }
52
53 static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
54                 struct nvme_smart_log *slog)
55 {
56         struct nvmet_ns *ns;
57         u64 host_reads, host_writes, data_units_read, data_units_written;
58
59         ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->get_log_page.nsid);
60         if (!ns) {
61                 pr_err("Could not find namespace id : %d\n",
62                                 le32_to_cpu(req->cmd->get_log_page.nsid));
63                 return NVME_SC_INVALID_NS;
64         }
65
66         /* we don't have the right data for file backed ns */
67         if (!ns->bdev)
68                 goto out;
69
70         host_reads = part_stat_read(ns->bdev->bd_part, ios[READ]);
71         data_units_read = part_stat_read(ns->bdev->bd_part, sectors[READ]);
72         host_writes = part_stat_read(ns->bdev->bd_part, ios[WRITE]);
73         data_units_written = part_stat_read(ns->bdev->bd_part, sectors[WRITE]);
74
75         put_unaligned_le64(host_reads, &slog->host_reads[0]);
76         put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
77         put_unaligned_le64(host_writes, &slog->host_writes[0]);
78         put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
79 out:
80         nvmet_put_namespace(ns);
81
82         return NVME_SC_SUCCESS;
83 }
84
85 static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
86                 struct nvme_smart_log *slog)
87 {
88         u64 host_reads = 0, host_writes = 0;
89         u64 data_units_read = 0, data_units_written = 0;
90         struct nvmet_ns *ns;
91         struct nvmet_ctrl *ctrl;
92
93         ctrl = req->sq->ctrl;
94
95         rcu_read_lock();
96         list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
97                 /* we don't have the right data for file backed ns */
98                 if (!ns->bdev)
99                         continue;
100                 host_reads += part_stat_read(ns->bdev->bd_part, ios[READ]);
101                 data_units_read +=
102                         part_stat_read(ns->bdev->bd_part, sectors[READ]);
103                 host_writes += part_stat_read(ns->bdev->bd_part, ios[WRITE]);
104                 data_units_written +=
105                         part_stat_read(ns->bdev->bd_part, sectors[WRITE]);
106
107         }
108         rcu_read_unlock();
109
110         put_unaligned_le64(host_reads, &slog->host_reads[0]);
111         put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
112         put_unaligned_le64(host_writes, &slog->host_writes[0]);
113         put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
114
115         return NVME_SC_SUCCESS;
116 }
117
118 static void nvmet_execute_get_log_page_smart(struct nvmet_req *req)
119 {
120         struct nvme_smart_log *log;
121         u16 status = NVME_SC_INTERNAL;
122
123         if (req->data_len != sizeof(*log))
124                 goto out;
125
126         log = kzalloc(sizeof(*log), GFP_KERNEL);
127         if (!log)
128                 goto out;
129
130         if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL))
131                 status = nvmet_get_smart_log_all(req, log);
132         else
133                 status = nvmet_get_smart_log_nsid(req, log);
134         if (status)
135                 goto out_free_log;
136
137         status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
138 out_free_log:
139         kfree(log);
140 out:
141         nvmet_req_complete(req, status);
142 }
143
144 static void nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req *req)
145 {
146         u16 status = NVME_SC_INTERNAL;
147         struct nvme_effects_log *log;
148
149         log = kzalloc(sizeof(*log), GFP_KERNEL);
150         if (!log)
151                 goto out;
152
153         log->acs[nvme_admin_get_log_page]       = cpu_to_le32(1 << 0);
154         log->acs[nvme_admin_identify]           = cpu_to_le32(1 << 0);
155         log->acs[nvme_admin_abort_cmd]          = cpu_to_le32(1 << 0);
156         log->acs[nvme_admin_set_features]       = cpu_to_le32(1 << 0);
157         log->acs[nvme_admin_get_features]       = cpu_to_le32(1 << 0);
158         log->acs[nvme_admin_async_event]        = cpu_to_le32(1 << 0);
159         log->acs[nvme_admin_keep_alive]         = cpu_to_le32(1 << 0);
160
161         log->iocs[nvme_cmd_read]                = cpu_to_le32(1 << 0);
162         log->iocs[nvme_cmd_write]               = cpu_to_le32(1 << 0);
163         log->iocs[nvme_cmd_flush]               = cpu_to_le32(1 << 0);
164         log->iocs[nvme_cmd_dsm]                 = cpu_to_le32(1 << 0);
165         log->iocs[nvme_cmd_write_zeroes]        = cpu_to_le32(1 << 0);
166
167         status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
168
169         kfree(log);
170 out:
171         nvmet_req_complete(req, status);
172 }
173
174 static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req)
175 {
176         struct nvmet_ctrl *ctrl = req->sq->ctrl;
177         u16 status = NVME_SC_INTERNAL;
178         size_t len;
179
180         if (req->data_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32))
181                 goto out;
182
183         mutex_lock(&ctrl->lock);
184         if (ctrl->nr_changed_ns == U32_MAX)
185                 len = sizeof(__le32);
186         else
187                 len = ctrl->nr_changed_ns * sizeof(__le32);
188         status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len);
189         if (!status)
190                 status = nvmet_zero_sgl(req, len, req->data_len - len);
191         ctrl->nr_changed_ns = 0;
192         nvmet_clear_aen(req, NVME_AEN_CFG_NS_ATTR);
193         mutex_unlock(&ctrl->lock);
194 out:
195         nvmet_req_complete(req, status);
196 }
197
198 static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
199                 struct nvme_ana_group_desc *desc)
200 {
201         struct nvmet_ctrl *ctrl = req->sq->ctrl;
202         struct nvmet_ns *ns;
203         u32 count = 0;
204
205         if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) {
206                 rcu_read_lock();
207                 list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link)
208                         if (ns->anagrpid == grpid)
209                                 desc->nsids[count++] = cpu_to_le32(ns->nsid);
210                 rcu_read_unlock();
211         }
212
213         desc->grpid = cpu_to_le32(grpid);
214         desc->nnsids = cpu_to_le32(count);
215         desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
216         desc->state = req->port->ana_state[grpid];
217         memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
218         return sizeof(struct nvme_ana_group_desc) + count * sizeof(__le32);
219 }
220
221 static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
222 {
223         struct nvme_ana_rsp_hdr hdr = { 0, };
224         struct nvme_ana_group_desc *desc;
225         size_t offset = sizeof(struct nvme_ana_rsp_hdr); /* start beyond hdr */
226         size_t len;
227         u32 grpid;
228         u16 ngrps = 0;
229         u16 status;
230
231         status = NVME_SC_INTERNAL;
232         desc = kmalloc(sizeof(struct nvme_ana_group_desc) +
233                         NVMET_MAX_NAMESPACES * sizeof(__le32), GFP_KERNEL);
234         if (!desc)
235                 goto out;
236
237         down_read(&nvmet_ana_sem);
238         for (grpid = 1; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
239                 if (!nvmet_ana_group_enabled[grpid])
240                         continue;
241                 len = nvmet_format_ana_group(req, grpid, desc);
242                 status = nvmet_copy_to_sgl(req, offset, desc, len);
243                 if (status)
244                         break;
245                 offset += len;
246                 ngrps++;
247         }
248         for ( ; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
249                 if (nvmet_ana_group_enabled[grpid])
250                         ngrps++;
251         }
252
253         hdr.chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
254         hdr.ngrps = cpu_to_le16(ngrps);
255         nvmet_clear_aen(req, NVME_AEN_CFG_ANA_CHANGE);
256         up_read(&nvmet_ana_sem);
257
258         kfree(desc);
259
260         /* copy the header last once we know the number of groups */
261         status = nvmet_copy_to_sgl(req, 0, &hdr, sizeof(hdr));
262 out:
263         nvmet_req_complete(req, status);
264 }
265
266 static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
267 {
268         struct nvmet_ctrl *ctrl = req->sq->ctrl;
269         struct nvme_id_ctrl *id;
270         u16 status = 0;
271         const char model[] = "Linux";
272
273         id = kzalloc(sizeof(*id), GFP_KERNEL);
274         if (!id) {
275                 status = NVME_SC_INTERNAL;
276                 goto out;
277         }
278
279         /* XXX: figure out how to assign real vendors IDs. */
280         id->vid = 0;
281         id->ssvid = 0;
282
283         memset(id->sn, ' ', sizeof(id->sn));
284         bin2hex(id->sn, &ctrl->subsys->serial,
285                 min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
286         memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' ');
287         memcpy_and_pad(id->fr, sizeof(id->fr),
288                        UTS_RELEASE, strlen(UTS_RELEASE), ' ');
289
290         id->rab = 6;
291
292         /*
293          * XXX: figure out how we can assign a IEEE OUI, but until then
294          * the safest is to leave it as zeroes.
295          */
296
297         /* we support multiple ports, multiples hosts and ANA: */
298         id->cmic = (1 << 0) | (1 << 1) | (1 << 3);
299
300         /* no limit on data transfer sizes for now */
301         id->mdts = 0;
302         id->cntlid = cpu_to_le16(ctrl->cntlid);
303         id->ver = cpu_to_le32(ctrl->subsys->ver);
304
305         /* XXX: figure out what to do about RTD3R/RTD3 */
306         id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
307         id->ctratt = cpu_to_le32(NVME_CTRL_ATTR_HID_128_BIT |
308                 NVME_CTRL_ATTR_TBKAS);
309
310         id->oacs = 0;
311
312         /*
313          * We don't really have a practical limit on the number of abort
314          * comands.  But we don't do anything useful for abort either, so
315          * no point in allowing more abort commands than the spec requires.
316          */
317         id->acl = 3;
318
319         id->aerl = NVMET_ASYNC_EVENTS - 1;
320
321         /* first slot is read-only, only one slot supported */
322         id->frmw = (1 << 0) | (1 << 1);
323         id->lpa = (1 << 0) | (1 << 1) | (1 << 2);
324         id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
325         id->npss = 0;
326
327         /* We support keep-alive timeout in granularity of seconds */
328         id->kas = cpu_to_le16(NVMET_KAS);
329
330         id->sqes = (0x6 << 4) | 0x6;
331         id->cqes = (0x4 << 4) | 0x4;
332
333         /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
334         id->maxcmd = cpu_to_le16(NVMET_MAX_CMD);
335
336         id->nn = cpu_to_le32(ctrl->subsys->max_nsid);
337         id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
338         id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
339                         NVME_CTRL_ONCS_WRITE_ZEROES);
340
341         /* XXX: don't report vwc if the underlying device is write through */
342         id->vwc = NVME_CTRL_VWC_PRESENT;
343
344         /*
345          * We can't support atomic writes bigger than a LBA without support
346          * from the backend device.
347          */
348         id->awun = 0;
349         id->awupf = 0;
350
351         id->sgls = cpu_to_le32(1 << 0); /* we always support SGLs */
352         if (ctrl->ops->has_keyed_sgls)
353                 id->sgls |= cpu_to_le32(1 << 2);
354         if (req->port->inline_data_size)
355                 id->sgls |= cpu_to_le32(1 << 20);
356
357         strlcpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
358
359         /* Max command capsule size is sqe + single page of in-capsule data */
360         id->ioccsz = cpu_to_le32((sizeof(struct nvme_command) +
361                                   req->port->inline_data_size) / 16);
362         /* Max response capsule size is cqe */
363         id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
364
365         id->msdbd = ctrl->ops->msdbd;
366
367         id->anacap = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
368         id->anatt = 10; /* random value */
369         id->anagrpmax = cpu_to_le32(NVMET_MAX_ANAGRPS);
370         id->nanagrpid = cpu_to_le32(NVMET_MAX_ANAGRPS);
371
372         /*
373          * Meh, we don't really support any power state.  Fake up the same
374          * values that qemu does.
375          */
376         id->psd[0].max_power = cpu_to_le16(0x9c4);
377         id->psd[0].entry_lat = cpu_to_le32(0x10);
378         id->psd[0].exit_lat = cpu_to_le32(0x4);
379
380         id->nwpc = 1 << 0; /* write protect and no write protect */
381
382         status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
383
384         kfree(id);
385 out:
386         nvmet_req_complete(req, status);
387 }
388
389 static void nvmet_execute_identify_ns(struct nvmet_req *req)
390 {
391         struct nvmet_ns *ns;
392         struct nvme_id_ns *id;
393         u16 status = 0;
394
395         if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
396                 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
397                 goto out;
398         }
399
400         id = kzalloc(sizeof(*id), GFP_KERNEL);
401         if (!id) {
402                 status = NVME_SC_INTERNAL;
403                 goto out;
404         }
405
406         /* return an all zeroed buffer if we can't find an active namespace */
407         ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
408         if (!ns)
409                 goto done;
410
411         /*
412          * nuse = ncap = nsze isn't always true, but we have no way to find
413          * that out from the underlying device.
414          */
415         id->ncap = id->nsze = cpu_to_le64(ns->size >> ns->blksize_shift);
416         switch (req->port->ana_state[ns->anagrpid]) {
417         case NVME_ANA_INACCESSIBLE:
418         case NVME_ANA_PERSISTENT_LOSS:
419                 break;
420         default:
421                 id->nuse = id->nsze;
422                 break;
423         }
424
425         /*
426          * We just provide a single LBA format that matches what the
427          * underlying device reports.
428          */
429         id->nlbaf = 0;
430         id->flbas = 0;
431
432         /*
433          * Our namespace might always be shared.  Not just with other
434          * controllers, but also with any other user of the block device.
435          */
436         id->nmic = (1 << 0);
437         id->anagrpid = cpu_to_le32(ns->anagrpid);
438
439         memcpy(&id->nguid, &ns->nguid, sizeof(id->nguid));
440
441         id->lbaf[0].ds = ns->blksize_shift;
442
443         if (ns->readonly)
444                 id->nsattr |= (1 << 0);
445         nvmet_put_namespace(ns);
446 done:
447         status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
448         kfree(id);
449 out:
450         nvmet_req_complete(req, status);
451 }
452
453 static void nvmet_execute_identify_nslist(struct nvmet_req *req)
454 {
455         static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
456         struct nvmet_ctrl *ctrl = req->sq->ctrl;
457         struct nvmet_ns *ns;
458         u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
459         __le32 *list;
460         u16 status = 0;
461         int i = 0;
462
463         list = kzalloc(buf_size, GFP_KERNEL);
464         if (!list) {
465                 status = NVME_SC_INTERNAL;
466                 goto out;
467         }
468
469         rcu_read_lock();
470         list_for_each_entry_rcu(ns, &ctrl->subsys->namespaces, dev_link) {
471                 if (ns->nsid <= min_nsid)
472                         continue;
473                 list[i++] = cpu_to_le32(ns->nsid);
474                 if (i == buf_size / sizeof(__le32))
475                         break;
476         }
477         rcu_read_unlock();
478
479         status = nvmet_copy_to_sgl(req, 0, list, buf_size);
480
481         kfree(list);
482 out:
483         nvmet_req_complete(req, status);
484 }
485
486 static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
487                                     void *id, off_t *off)
488 {
489         struct nvme_ns_id_desc desc = {
490                 .nidt = type,
491                 .nidl = len,
492         };
493         u16 status;
494
495         status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
496         if (status)
497                 return status;
498         *off += sizeof(desc);
499
500         status = nvmet_copy_to_sgl(req, *off, id, len);
501         if (status)
502                 return status;
503         *off += len;
504
505         return 0;
506 }
507
508 static void nvmet_execute_identify_desclist(struct nvmet_req *req)
509 {
510         struct nvmet_ns *ns;
511         u16 status = 0;
512         off_t off = 0;
513
514         ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->identify.nsid);
515         if (!ns) {
516                 status = NVME_SC_INVALID_NS | NVME_SC_DNR;
517                 goto out;
518         }
519
520         if (memchr_inv(&ns->uuid, 0, sizeof(ns->uuid))) {
521                 status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
522                                                   NVME_NIDT_UUID_LEN,
523                                                   &ns->uuid, &off);
524                 if (status)
525                         goto out_put_ns;
526         }
527         if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid))) {
528                 status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
529                                                   NVME_NIDT_NGUID_LEN,
530                                                   &ns->nguid, &off);
531                 if (status)
532                         goto out_put_ns;
533         }
534
535         if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
536                         off) != NVME_IDENTIFY_DATA_SIZE - off)
537                 status = NVME_SC_INTERNAL | NVME_SC_DNR;
538 out_put_ns:
539         nvmet_put_namespace(ns);
540 out:
541         nvmet_req_complete(req, status);
542 }
543
544 /*
545  * A "minimum viable" abort implementation: the command is mandatory in the
546  * spec, but we are not required to do any useful work.  We couldn't really
547  * do a useful abort, so don't bother even with waiting for the command
548  * to be exectuted and return immediately telling the command to abort
549  * wasn't found.
550  */
551 static void nvmet_execute_abort(struct nvmet_req *req)
552 {
553         nvmet_set_result(req, 1);
554         nvmet_req_complete(req, 0);
555 }
556
557 static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
558 {
559         u16 status;
560
561         if (req->ns->file)
562                 status = nvmet_file_flush(req);
563         else
564                 status = nvmet_bdev_flush(req);
565
566         if (status)
567                 pr_err("write protect flush failed nsid: %u\n", req->ns->nsid);
568         return status;
569 }
570
571 static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
572 {
573         u32 write_protect = le32_to_cpu(req->cmd->common.cdw10[1]);
574         struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
575         u16 status = NVME_SC_FEATURE_NOT_CHANGEABLE;
576
577         req->ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->rw.nsid);
578         if (unlikely(!req->ns))
579                 return status;
580
581         mutex_lock(&subsys->lock);
582         switch (write_protect) {
583         case NVME_NS_WRITE_PROTECT:
584                 req->ns->readonly = true;
585                 status = nvmet_write_protect_flush_sync(req);
586                 if (status)
587                         req->ns->readonly = false;
588                 break;
589         case NVME_NS_NO_WRITE_PROTECT:
590                 req->ns->readonly = false;
591                 status = 0;
592                 break;
593         default:
594                 break;
595         }
596
597         if (!status)
598                 nvmet_ns_changed(subsys, req->ns->nsid);
599         mutex_unlock(&subsys->lock);
600         return status;
601 }
602
603 static void nvmet_execute_set_features(struct nvmet_req *req)
604 {
605         struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
606         u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
607         u32 val32;
608         u16 status = 0;
609
610         switch (cdw10 & 0xff) {
611         case NVME_FEAT_NUM_QUEUES:
612                 nvmet_set_result(req,
613                         (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
614                 break;
615         case NVME_FEAT_KATO:
616                 val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
617                 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
618                 nvmet_set_result(req, req->sq->ctrl->kato);
619                 break;
620         case NVME_FEAT_ASYNC_EVENT:
621                 val32 = le32_to_cpu(req->cmd->common.cdw10[1]);
622                 if (val32 & ~NVMET_AEN_CFG_ALL) {
623                         status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
624                         break;
625                 }
626
627                 WRITE_ONCE(req->sq->ctrl->aen_enabled, val32);
628                 nvmet_set_result(req, val32);
629                 break;
630         case NVME_FEAT_HOST_ID:
631                 status = NVME_SC_CMD_SEQ_ERROR | NVME_SC_DNR;
632                 break;
633         case NVME_FEAT_WRITE_PROTECT:
634                 status = nvmet_set_feat_write_protect(req);
635                 break;
636         default:
637                 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
638                 break;
639         }
640
641         nvmet_req_complete(req, status);
642 }
643
644 static u16 nvmet_get_feat_write_protect(struct nvmet_req *req)
645 {
646         struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
647         u32 result;
648
649         req->ns = nvmet_find_namespace(req->sq->ctrl, req->cmd->common.nsid);
650         if (!req->ns)
651                 return NVME_SC_INVALID_NS | NVME_SC_DNR;
652
653         mutex_lock(&subsys->lock);
654         if (req->ns->readonly == true)
655                 result = NVME_NS_WRITE_PROTECT;
656         else
657                 result = NVME_NS_NO_WRITE_PROTECT;
658         nvmet_set_result(req, result);
659         mutex_unlock(&subsys->lock);
660
661         return 0;
662 }
663
664 static void nvmet_execute_get_features(struct nvmet_req *req)
665 {
666         struct nvmet_subsys *subsys = req->sq->ctrl->subsys;
667         u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10[0]);
668         u16 status = 0;
669
670         switch (cdw10 & 0xff) {
671         /*
672          * These features are mandatory in the spec, but we don't
673          * have a useful way to implement them.  We'll eventually
674          * need to come up with some fake values for these.
675          */
676 #if 0
677         case NVME_FEAT_ARBITRATION:
678                 break;
679         case NVME_FEAT_POWER_MGMT:
680                 break;
681         case NVME_FEAT_TEMP_THRESH:
682                 break;
683         case NVME_FEAT_ERR_RECOVERY:
684                 break;
685         case NVME_FEAT_IRQ_COALESCE:
686                 break;
687         case NVME_FEAT_IRQ_CONFIG:
688                 break;
689         case NVME_FEAT_WRITE_ATOMIC:
690                 break;
691 #endif
692         case NVME_FEAT_ASYNC_EVENT:
693                 nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled));
694                 break;
695         case NVME_FEAT_VOLATILE_WC:
696                 nvmet_set_result(req, 1);
697                 break;
698         case NVME_FEAT_NUM_QUEUES:
699                 nvmet_set_result(req,
700                         (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
701                 break;
702         case NVME_FEAT_KATO:
703                 nvmet_set_result(req, req->sq->ctrl->kato * 1000);
704                 break;
705         case NVME_FEAT_HOST_ID:
706                 /* need 128-bit host identifier flag */
707                 if (!(req->cmd->common.cdw10[1] & cpu_to_le32(1 << 0))) {
708                         status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
709                         break;
710                 }
711
712                 status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
713                                 sizeof(req->sq->ctrl->hostid));
714                 break;
715         case NVME_FEAT_WRITE_PROTECT:
716                 status = nvmet_get_feat_write_protect(req);
717                 break;
718         default:
719                 status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
720                 break;
721         }
722
723         nvmet_req_complete(req, status);
724 }
725
726 static void nvmet_execute_async_event(struct nvmet_req *req)
727 {
728         struct nvmet_ctrl *ctrl = req->sq->ctrl;
729
730         mutex_lock(&ctrl->lock);
731         if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
732                 mutex_unlock(&ctrl->lock);
733                 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_SC_DNR);
734                 return;
735         }
736         ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
737         mutex_unlock(&ctrl->lock);
738
739         schedule_work(&ctrl->async_event_work);
740 }
741
742 static void nvmet_execute_keep_alive(struct nvmet_req *req)
743 {
744         struct nvmet_ctrl *ctrl = req->sq->ctrl;
745
746         pr_debug("ctrl %d update keep-alive timer for %d secs\n",
747                 ctrl->cntlid, ctrl->kato);
748
749         mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
750         nvmet_req_complete(req, 0);
751 }
752
753 u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
754 {
755         struct nvme_command *cmd = req->cmd;
756         u16 ret;
757
758         ret = nvmet_check_ctrl_status(req, cmd);
759         if (unlikely(ret))
760                 return ret;
761
762         switch (cmd->common.opcode) {
763         case nvme_admin_get_log_page:
764                 req->data_len = nvmet_get_log_page_len(cmd);
765
766                 switch (cmd->get_log_page.lid) {
767                 case NVME_LOG_ERROR:
768                         /*
769                          * We currently never set the More bit in the status
770                          * field, so all error log entries are invalid and can
771                          * be zeroed out.  This is called a minum viable
772                          * implementation (TM) of this mandatory log page.
773                          */
774                         req->execute = nvmet_execute_get_log_page_noop;
775                         return 0;
776                 case NVME_LOG_SMART:
777                         req->execute = nvmet_execute_get_log_page_smart;
778                         return 0;
779                 case NVME_LOG_FW_SLOT:
780                         /*
781                          * We only support a single firmware slot which always
782                          * is active, so we can zero out the whole firmware slot
783                          * log and still claim to fully implement this mandatory
784                          * log page.
785                          */
786                         req->execute = nvmet_execute_get_log_page_noop;
787                         return 0;
788                 case NVME_LOG_CHANGED_NS:
789                         req->execute = nvmet_execute_get_log_changed_ns;
790                         return 0;
791                 case NVME_LOG_CMD_EFFECTS:
792                         req->execute = nvmet_execute_get_log_cmd_effects_ns;
793                         return 0;
794                 case NVME_LOG_ANA:
795                         req->execute = nvmet_execute_get_log_page_ana;
796                         return 0;
797                 }
798                 break;
799         case nvme_admin_identify:
800                 req->data_len = NVME_IDENTIFY_DATA_SIZE;
801                 switch (cmd->identify.cns) {
802                 case NVME_ID_CNS_NS:
803                         req->execute = nvmet_execute_identify_ns;
804                         return 0;
805                 case NVME_ID_CNS_CTRL:
806                         req->execute = nvmet_execute_identify_ctrl;
807                         return 0;
808                 case NVME_ID_CNS_NS_ACTIVE_LIST:
809                         req->execute = nvmet_execute_identify_nslist;
810                         return 0;
811                 case NVME_ID_CNS_NS_DESC_LIST:
812                         req->execute = nvmet_execute_identify_desclist;
813                         return 0;
814                 }
815                 break;
816         case nvme_admin_abort_cmd:
817                 req->execute = nvmet_execute_abort;
818                 req->data_len = 0;
819                 return 0;
820         case nvme_admin_set_features:
821                 req->execute = nvmet_execute_set_features;
822                 req->data_len = 0;
823                 return 0;
824         case nvme_admin_get_features:
825                 req->execute = nvmet_execute_get_features;
826                 req->data_len = 0;
827                 return 0;
828         case nvme_admin_async_event:
829                 req->execute = nvmet_execute_async_event;
830                 req->data_len = 0;
831                 return 0;
832         case nvme_admin_keep_alive:
833                 req->execute = nvmet_execute_keep_alive;
834                 req->data_len = 0;
835                 return 0;
836         }
837
838         pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
839                req->sq->qid);
840         return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
841 }