1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * NVMe admin command implementation.
4 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
5 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/module.h>
8 #include <linux/rculist.h>
9 #include <linux/part_stat.h>
10
11 #include <generated/utsrelease.h>
12 #include <linux/unaligned.h>
13 #include "nvmet.h"
14
nvmet_get_log_page_len(struct nvme_command * cmd)15 u32 nvmet_get_log_page_len(struct nvme_command *cmd)
16 {
17 u32 len = le16_to_cpu(cmd->get_log_page.numdu);
18
19 len <<= 16;
20 len += le16_to_cpu(cmd->get_log_page.numdl);
21 /* NUMD is a 0's based value */
22 len += 1;
23 len *= sizeof(u32);
24
25 return len;
26 }
27
nvmet_feat_data_len(struct nvmet_req * req,u32 cdw10)28 static u32 nvmet_feat_data_len(struct nvmet_req *req, u32 cdw10)
29 {
30 switch (cdw10 & 0xff) {
31 case NVME_FEAT_HOST_ID:
32 return sizeof(req->sq->ctrl->hostid);
33 default:
34 return 0;
35 }
36 }
37
nvmet_get_log_page_offset(struct nvme_command * cmd)38 u64 nvmet_get_log_page_offset(struct nvme_command *cmd)
39 {
40 return le64_to_cpu(cmd->get_log_page.lpo);
41 }
42
nvmet_execute_get_log_page_noop(struct nvmet_req * req)43 static void nvmet_execute_get_log_page_noop(struct nvmet_req *req)
44 {
45 nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->transfer_len));
46 }
47
nvmet_execute_get_log_page_error(struct nvmet_req * req)48 static void nvmet_execute_get_log_page_error(struct nvmet_req *req)
49 {
50 struct nvmet_ctrl *ctrl = req->sq->ctrl;
51 unsigned long flags;
52 off_t offset = 0;
53 u64 slot;
54 u64 i;
55
56 spin_lock_irqsave(&ctrl->error_lock, flags);
57 slot = ctrl->err_counter % NVMET_ERROR_LOG_SLOTS;
58
59 for (i = 0; i < NVMET_ERROR_LOG_SLOTS; i++) {
60 if (nvmet_copy_to_sgl(req, offset, &ctrl->slots[slot],
61 sizeof(struct nvme_error_slot)))
62 break;
63
64 if (slot == 0)
65 slot = NVMET_ERROR_LOG_SLOTS - 1;
66 else
67 slot--;
68 offset += sizeof(struct nvme_error_slot);
69 }
70 spin_unlock_irqrestore(&ctrl->error_lock, flags);
71 nvmet_req_complete(req, 0);
72 }
73
nvmet_execute_get_supported_log_pages(struct nvmet_req * req)74 static void nvmet_execute_get_supported_log_pages(struct nvmet_req *req)
75 {
76 struct nvme_supported_log *logs;
77 u16 status;
78
79 logs = kzalloc(sizeof(*logs), GFP_KERNEL);
80 if (!logs) {
81 status = NVME_SC_INTERNAL;
82 goto out;
83 }
84
85 logs->lids[NVME_LOG_SUPPORTED] = cpu_to_le32(NVME_LIDS_LSUPP);
86 logs->lids[NVME_LOG_ERROR] = cpu_to_le32(NVME_LIDS_LSUPP);
87 logs->lids[NVME_LOG_SMART] = cpu_to_le32(NVME_LIDS_LSUPP);
88 logs->lids[NVME_LOG_FW_SLOT] = cpu_to_le32(NVME_LIDS_LSUPP);
89 logs->lids[NVME_LOG_CHANGED_NS] = cpu_to_le32(NVME_LIDS_LSUPP);
90 logs->lids[NVME_LOG_CMD_EFFECTS] = cpu_to_le32(NVME_LIDS_LSUPP);
91 logs->lids[NVME_LOG_ENDURANCE_GROUP] = cpu_to_le32(NVME_LIDS_LSUPP);
92 logs->lids[NVME_LOG_ANA] = cpu_to_le32(NVME_LIDS_LSUPP);
93 logs->lids[NVME_LOG_FEATURES] = cpu_to_le32(NVME_LIDS_LSUPP);
94 logs->lids[NVME_LOG_RMI] = cpu_to_le32(NVME_LIDS_LSUPP);
95 logs->lids[NVME_LOG_RESERVATION] = cpu_to_le32(NVME_LIDS_LSUPP);
96
97 status = nvmet_copy_to_sgl(req, 0, logs, sizeof(*logs));
98 kfree(logs);
99 out:
100 nvmet_req_complete(req, status);
101 }
102
nvmet_get_smart_log_nsid(struct nvmet_req * req,struct nvme_smart_log * slog)103 static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
104 struct nvme_smart_log *slog)
105 {
106 u64 host_reads, host_writes, data_units_read, data_units_written;
107 u16 status;
108
109 status = nvmet_req_find_ns(req);
110 if (status)
111 return status;
112
113 /* we don't have the right data for file backed ns */
114 if (!req->ns->bdev)
115 return NVME_SC_SUCCESS;
116
117 host_reads = part_stat_read(req->ns->bdev, ios[READ]);
118 data_units_read =
119 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
120 host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
121 data_units_written =
122 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
123
124 put_unaligned_le64(host_reads, &slog->host_reads[0]);
125 put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
126 put_unaligned_le64(host_writes, &slog->host_writes[0]);
127 put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
128
129 return NVME_SC_SUCCESS;
130 }
131
nvmet_get_smart_log_all(struct nvmet_req * req,struct nvme_smart_log * slog)132 static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
133 struct nvme_smart_log *slog)
134 {
135 u64 host_reads = 0, host_writes = 0;
136 u64 data_units_read = 0, data_units_written = 0;
137 struct nvmet_ns *ns;
138 struct nvmet_ctrl *ctrl;
139 unsigned long idx;
140
141 ctrl = req->sq->ctrl;
142 xa_for_each(&ctrl->subsys->namespaces, idx, ns) {
143 /* we don't have the right data for file backed ns */
144 if (!ns->bdev)
145 continue;
146 host_reads += part_stat_read(ns->bdev, ios[READ]);
147 data_units_read += DIV_ROUND_UP(
148 part_stat_read(ns->bdev, sectors[READ]), 1000);
149 host_writes += part_stat_read(ns->bdev, ios[WRITE]);
150 data_units_written += DIV_ROUND_UP(
151 part_stat_read(ns->bdev, sectors[WRITE]), 1000);
152 }
153
154 put_unaligned_le64(host_reads, &slog->host_reads[0]);
155 put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
156 put_unaligned_le64(host_writes, &slog->host_writes[0]);
157 put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
158
159 return NVME_SC_SUCCESS;
160 }
161
nvmet_execute_get_log_page_rmi(struct nvmet_req * req)162 static void nvmet_execute_get_log_page_rmi(struct nvmet_req *req)
163 {
164 struct nvme_rotational_media_log *log;
165 struct gendisk *disk;
166 u16 status;
167
168 req->cmd->common.nsid = cpu_to_le32(le16_to_cpu(
169 req->cmd->get_log_page.lsi));
170 status = nvmet_req_find_ns(req);
171 if (status)
172 goto out;
173
174 if (!req->ns->bdev || bdev_nonrot(req->ns->bdev)) {
175 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
176 goto out;
177 }
178
179 if (req->transfer_len != sizeof(*log)) {
180 status = NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
181 goto out;
182 }
183
184 log = kzalloc(sizeof(*log), GFP_KERNEL);
185 if (!log)
186 goto out;
187
188 log->endgid = req->cmd->get_log_page.lsi;
189 disk = req->ns->bdev->bd_disk;
190 if (disk && disk->ia_ranges)
191 log->numa = cpu_to_le16(disk->ia_ranges->nr_ia_ranges);
192 else
193 log->numa = cpu_to_le16(1);
194
195 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
196 kfree(log);
197 out:
198 nvmet_req_complete(req, status);
199 }
200
nvmet_execute_get_log_page_smart(struct nvmet_req * req)201 static void nvmet_execute_get_log_page_smart(struct nvmet_req *req)
202 {
203 struct nvme_smart_log *log;
204 u16 status = NVME_SC_INTERNAL;
205 unsigned long flags;
206
207 if (req->transfer_len != sizeof(*log))
208 goto out;
209
210 log = kzalloc(sizeof(*log), GFP_KERNEL);
211 if (!log)
212 goto out;
213
214 if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL))
215 status = nvmet_get_smart_log_all(req, log);
216 else
217 status = nvmet_get_smart_log_nsid(req, log);
218 if (status)
219 goto out_free_log;
220
221 spin_lock_irqsave(&req->sq->ctrl->error_lock, flags);
222 put_unaligned_le64(req->sq->ctrl->err_counter,
223 &log->num_err_log_entries);
224 spin_unlock_irqrestore(&req->sq->ctrl->error_lock, flags);
225
226 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
227 out_free_log:
228 kfree(log);
229 out:
230 nvmet_req_complete(req, status);
231 }
232
nvmet_get_cmd_effects_nvm(struct nvme_effects_log * log)233 static void nvmet_get_cmd_effects_nvm(struct nvme_effects_log *log)
234 {
235 log->acs[nvme_admin_get_log_page] =
236 log->acs[nvme_admin_identify] =
237 log->acs[nvme_admin_abort_cmd] =
238 log->acs[nvme_admin_set_features] =
239 log->acs[nvme_admin_get_features] =
240 log->acs[nvme_admin_async_event] =
241 log->acs[nvme_admin_keep_alive] =
242 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
243
244 log->iocs[nvme_cmd_read] =
245 log->iocs[nvme_cmd_flush] =
246 log->iocs[nvme_cmd_dsm] =
247 log->iocs[nvme_cmd_resv_acquire] =
248 log->iocs[nvme_cmd_resv_register] =
249 log->iocs[nvme_cmd_resv_release] =
250 log->iocs[nvme_cmd_resv_report] =
251 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
252 log->iocs[nvme_cmd_write] =
253 log->iocs[nvme_cmd_write_zeroes] =
254 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC);
255 }
256
nvmet_get_cmd_effects_zns(struct nvme_effects_log * log)257 static void nvmet_get_cmd_effects_zns(struct nvme_effects_log *log)
258 {
259 log->iocs[nvme_cmd_zone_append] =
260 log->iocs[nvme_cmd_zone_mgmt_send] =
261 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC);
262 log->iocs[nvme_cmd_zone_mgmt_recv] =
263 cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
264 }
265
nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req * req)266 static void nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req *req)
267 {
268 struct nvme_effects_log *log;
269 u16 status = NVME_SC_SUCCESS;
270
271 log = kzalloc(sizeof(*log), GFP_KERNEL);
272 if (!log) {
273 status = NVME_SC_INTERNAL;
274 goto out;
275 }
276
277 switch (req->cmd->get_log_page.csi) {
278 case NVME_CSI_NVM:
279 nvmet_get_cmd_effects_nvm(log);
280 break;
281 case NVME_CSI_ZNS:
282 if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
283 status = NVME_SC_INVALID_IO_CMD_SET;
284 goto free;
285 }
286 nvmet_get_cmd_effects_nvm(log);
287 nvmet_get_cmd_effects_zns(log);
288 break;
289 default:
290 status = NVME_SC_INVALID_LOG_PAGE;
291 goto free;
292 }
293
294 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
295 free:
296 kfree(log);
297 out:
298 nvmet_req_complete(req, status);
299 }
300
nvmet_execute_get_log_changed_ns(struct nvmet_req * req)301 static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req)
302 {
303 struct nvmet_ctrl *ctrl = req->sq->ctrl;
304 u16 status = NVME_SC_INTERNAL;
305 size_t len;
306
307 if (req->transfer_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32))
308 goto out;
309
310 mutex_lock(&ctrl->lock);
311 if (ctrl->nr_changed_ns == U32_MAX)
312 len = sizeof(__le32);
313 else
314 len = ctrl->nr_changed_ns * sizeof(__le32);
315 status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len);
316 if (!status)
317 status = nvmet_zero_sgl(req, len, req->transfer_len - len);
318 ctrl->nr_changed_ns = 0;
319 nvmet_clear_aen_bit(req, NVME_AEN_BIT_NS_ATTR);
320 mutex_unlock(&ctrl->lock);
321 out:
322 nvmet_req_complete(req, status);
323 }
324
nvmet_format_ana_group(struct nvmet_req * req,u32 grpid,struct nvme_ana_group_desc * desc)325 static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
326 struct nvme_ana_group_desc *desc)
327 {
328 struct nvmet_ctrl *ctrl = req->sq->ctrl;
329 struct nvmet_ns *ns;
330 unsigned long idx;
331 u32 count = 0;
332
333 if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) {
334 xa_for_each(&ctrl->subsys->namespaces, idx, ns)
335 if (ns->anagrpid == grpid)
336 desc->nsids[count++] = cpu_to_le32(ns->nsid);
337 }
338
339 desc->grpid = cpu_to_le32(grpid);
340 desc->nnsids = cpu_to_le32(count);
341 desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
342 desc->state = req->port->ana_state[grpid];
343 memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
344 return struct_size(desc, nsids, count);
345 }
346
nvmet_execute_get_log_page_endgrp(struct nvmet_req * req)347 static void nvmet_execute_get_log_page_endgrp(struct nvmet_req *req)
348 {
349 u64 host_reads, host_writes, data_units_read, data_units_written;
350 struct nvme_endurance_group_log *log;
351 u16 status;
352
353 /*
354 * The target driver emulates each endurance group as its own
355 * namespace, reusing the nsid as the endurance group identifier.
356 */
357 req->cmd->common.nsid = cpu_to_le32(le16_to_cpu(
358 req->cmd->get_log_page.lsi));
359 status = nvmet_req_find_ns(req);
360 if (status)
361 goto out;
362
363 log = kzalloc(sizeof(*log), GFP_KERNEL);
364 if (!log) {
365 status = NVME_SC_INTERNAL;
366 goto out;
367 }
368
369 if (!req->ns->bdev)
370 goto copy;
371
372 host_reads = part_stat_read(req->ns->bdev, ios[READ]);
373 data_units_read =
374 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
375 host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
376 data_units_written =
377 DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
378
379 put_unaligned_le64(host_reads, &log->hrc[0]);
380 put_unaligned_le64(data_units_read, &log->dur[0]);
381 put_unaligned_le64(host_writes, &log->hwc[0]);
382 put_unaligned_le64(data_units_written, &log->duw[0]);
383 copy:
384 status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
385 kfree(log);
386 out:
387 nvmet_req_complete(req, status);
388 }
389
nvmet_execute_get_log_page_ana(struct nvmet_req * req)390 static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
391 {
392 struct nvme_ana_rsp_hdr hdr = { 0, };
393 struct nvme_ana_group_desc *desc;
394 size_t offset = sizeof(struct nvme_ana_rsp_hdr); /* start beyond hdr */
395 size_t len;
396 u32 grpid;
397 u16 ngrps = 0;
398 u16 status;
399
400 status = NVME_SC_INTERNAL;
401 desc = kmalloc(struct_size(desc, nsids, NVMET_MAX_NAMESPACES),
402 GFP_KERNEL);
403 if (!desc)
404 goto out;
405
406 down_read(&nvmet_ana_sem);
407 for (grpid = 1; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
408 if (!nvmet_ana_group_enabled[grpid])
409 continue;
410 len = nvmet_format_ana_group(req, grpid, desc);
411 status = nvmet_copy_to_sgl(req, offset, desc, len);
412 if (status)
413 break;
414 offset += len;
415 ngrps++;
416 }
417 for ( ; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
418 if (nvmet_ana_group_enabled[grpid])
419 ngrps++;
420 }
421
422 hdr.chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
423 hdr.ngrps = cpu_to_le16(ngrps);
424 nvmet_clear_aen_bit(req, NVME_AEN_BIT_ANA_CHANGE);
425 up_read(&nvmet_ana_sem);
426
427 kfree(desc);
428
429 /* copy the header last once we know the number of groups */
430 status = nvmet_copy_to_sgl(req, 0, &hdr, sizeof(hdr));
431 out:
432 nvmet_req_complete(req, status);
433 }
434
nvmet_execute_get_log_page_features(struct nvmet_req * req)435 static void nvmet_execute_get_log_page_features(struct nvmet_req *req)
436 {
437 struct nvme_supported_features_log *features;
438 u16 status;
439
440 features = kzalloc(sizeof(*features), GFP_KERNEL);
441 if (!features) {
442 status = NVME_SC_INTERNAL;
443 goto out;
444 }
445
446 features->fis[NVME_FEAT_NUM_QUEUES] =
447 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
448 features->fis[NVME_FEAT_KATO] =
449 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
450 features->fis[NVME_FEAT_ASYNC_EVENT] =
451 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
452 features->fis[NVME_FEAT_HOST_ID] =
453 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
454 features->fis[NVME_FEAT_WRITE_PROTECT] =
455 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_NSCPE);
456 features->fis[NVME_FEAT_RESV_MASK] =
457 cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_NSCPE);
458
459 status = nvmet_copy_to_sgl(req, 0, features, sizeof(*features));
460 kfree(features);
461 out:
462 nvmet_req_complete(req, status);
463 }
464
nvmet_execute_get_log_page(struct nvmet_req * req)465 static void nvmet_execute_get_log_page(struct nvmet_req *req)
466 {
467 if (!nvmet_check_transfer_len(req, nvmet_get_log_page_len(req->cmd)))
468 return;
469
470 switch (req->cmd->get_log_page.lid) {
471 case NVME_LOG_SUPPORTED:
472 return nvmet_execute_get_supported_log_pages(req);
473 case NVME_LOG_ERROR:
474 return nvmet_execute_get_log_page_error(req);
475 case NVME_LOG_SMART:
476 return nvmet_execute_get_log_page_smart(req);
477 case NVME_LOG_FW_SLOT:
478 /*
479 * We only support a single firmware slot which always is
480 * active, so we can zero out the whole firmware slot log and
481 * still claim to fully implement this mandatory log page.
482 */
483 return nvmet_execute_get_log_page_noop(req);
484 case NVME_LOG_CHANGED_NS:
485 return nvmet_execute_get_log_changed_ns(req);
486 case NVME_LOG_CMD_EFFECTS:
487 return nvmet_execute_get_log_cmd_effects_ns(req);
488 case NVME_LOG_ENDURANCE_GROUP:
489 return nvmet_execute_get_log_page_endgrp(req);
490 case NVME_LOG_ANA:
491 return nvmet_execute_get_log_page_ana(req);
492 case NVME_LOG_FEATURES:
493 return nvmet_execute_get_log_page_features(req);
494 case NVME_LOG_RMI:
495 return nvmet_execute_get_log_page_rmi(req);
496 case NVME_LOG_RESERVATION:
497 return nvmet_execute_get_log_page_resv(req);
498 }
499 pr_debug("unhandled lid %d on qid %d\n",
500 req->cmd->get_log_page.lid, req->sq->qid);
501 req->error_loc = offsetof(struct nvme_get_log_page_command, lid);
502 nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_STATUS_DNR);
503 }
504
nvmet_execute_identify_ctrl(struct nvmet_req * req)505 static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
506 {
507 struct nvmet_ctrl *ctrl = req->sq->ctrl;
508 struct nvmet_subsys *subsys = ctrl->subsys;
509 struct nvme_id_ctrl *id;
510 u32 cmd_capsule_size;
511 u16 status = 0;
512
513 if (!subsys->subsys_discovered) {
514 mutex_lock(&subsys->lock);
515 subsys->subsys_discovered = true;
516 mutex_unlock(&subsys->lock);
517 }
518
519 id = kzalloc(sizeof(*id), GFP_KERNEL);
520 if (!id) {
521 status = NVME_SC_INTERNAL;
522 goto out;
523 }
524
525 /* XXX: figure out how to assign real vendors IDs. */
526 id->vid = 0;
527 id->ssvid = 0;
528
529 memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
530 memcpy_and_pad(id->mn, sizeof(id->mn), subsys->model_number,
531 strlen(subsys->model_number), ' ');
532 memcpy_and_pad(id->fr, sizeof(id->fr),
533 subsys->firmware_rev, strlen(subsys->firmware_rev), ' ');
534
535 put_unaligned_le24(subsys->ieee_oui, id->ieee);
536
537 id->rab = 6;
538
539 if (nvmet_is_disc_subsys(ctrl->subsys))
540 id->cntrltype = NVME_CTRL_DISC;
541 else
542 id->cntrltype = NVME_CTRL_IO;
543
544 /* we support multiple ports, multiples hosts and ANA: */
545 id->cmic = NVME_CTRL_CMIC_MULTI_PORT | NVME_CTRL_CMIC_MULTI_CTRL |
546 NVME_CTRL_CMIC_ANA;
547
548 /* Limit MDTS according to transport capability */
549 if (ctrl->ops->get_mdts)
550 id->mdts = ctrl->ops->get_mdts(ctrl);
551 else
552 id->mdts = 0;
553
554 id->cntlid = cpu_to_le16(ctrl->cntlid);
555 id->ver = cpu_to_le32(ctrl->subsys->ver);
556
557 /* XXX: figure out what to do about RTD3R/RTD3 */
558 id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
559 id->ctratt = cpu_to_le32(NVME_CTRL_ATTR_HID_128_BIT |
560 NVME_CTRL_ATTR_TBKAS);
561
562 id->oacs = 0;
563
564 /*
565 * We don't really have a practical limit on the number of abort
566 * comands. But we don't do anything useful for abort either, so
567 * no point in allowing more abort commands than the spec requires.
568 */
569 id->acl = 3;
570
571 id->aerl = NVMET_ASYNC_EVENTS - 1;
572
573 /* first slot is read-only, only one slot supported */
574 id->frmw = (1 << 0) | (1 << 1);
575 id->lpa = (1 << 0) | (1 << 1) | (1 << 2);
576 id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
577 id->npss = 0;
578
579 /* We support keep-alive timeout in granularity of seconds */
580 id->kas = cpu_to_le16(NVMET_KAS);
581
582 id->sqes = (0x6 << 4) | 0x6;
583 id->cqes = (0x4 << 4) | 0x4;
584
585 /* no enforcement soft-limit for maxcmd - pick arbitrary high value */
586 id->maxcmd = cpu_to_le16(NVMET_MAX_CMD(ctrl));
587
588 id->nn = cpu_to_le32(NVMET_MAX_NAMESPACES);
589 id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
590 id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
591 NVME_CTRL_ONCS_WRITE_ZEROES |
592 NVME_CTRL_ONCS_RESERVATIONS);
593
594 /* XXX: don't report vwc if the underlying device is write through */
595 id->vwc = NVME_CTRL_VWC_PRESENT;
596
597 /*
598 * We can't support atomic writes bigger than a LBA without support
599 * from the backend device.
600 */
601 id->awun = 0;
602 id->awupf = 0;
603
604 /* we always support SGLs */
605 id->sgls = cpu_to_le32(NVME_CTRL_SGLS_BYTE_ALIGNED);
606 if (ctrl->ops->flags & NVMF_KEYED_SGLS)
607 id->sgls |= cpu_to_le32(NVME_CTRL_SGLS_KSDBDS);
608 if (req->port->inline_data_size)
609 id->sgls |= cpu_to_le32(NVME_CTRL_SGLS_SAOS);
610
611 strscpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
612
613 /*
614 * Max command capsule size is sqe + in-capsule data size.
615 * Disable in-capsule data for Metadata capable controllers.
616 */
617 cmd_capsule_size = sizeof(struct nvme_command);
618 if (!ctrl->pi_support)
619 cmd_capsule_size += req->port->inline_data_size;
620 id->ioccsz = cpu_to_le32(cmd_capsule_size / 16);
621
622 /* Max response capsule size is cqe */
623 id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
624
625 id->msdbd = ctrl->ops->msdbd;
626
627 /*
628 * Endurance group identifier is 16 bits, so we can't let namespaces
629 * overflow that since we reuse the nsid
630 */
631 BUILD_BUG_ON(NVMET_MAX_NAMESPACES > USHRT_MAX);
632 id->endgidmax = cpu_to_le16(NVMET_MAX_NAMESPACES);
633
634 id->anacap = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
635 id->anatt = 10; /* random value */
636 id->anagrpmax = cpu_to_le32(NVMET_MAX_ANAGRPS);
637 id->nanagrpid = cpu_to_le32(NVMET_MAX_ANAGRPS);
638
639 /*
640 * Meh, we don't really support any power state. Fake up the same
641 * values that qemu does.
642 */
643 id->psd[0].max_power = cpu_to_le16(0x9c4);
644 id->psd[0].entry_lat = cpu_to_le32(0x10);
645 id->psd[0].exit_lat = cpu_to_le32(0x4);
646
647 id->nwpc = 1 << 0; /* write protect and no write protect */
648
649 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
650
651 kfree(id);
652 out:
653 nvmet_req_complete(req, status);
654 }
655
nvmet_execute_identify_ns(struct nvmet_req * req)656 static void nvmet_execute_identify_ns(struct nvmet_req *req)
657 {
658 struct nvme_id_ns *id;
659 u16 status;
660
661 if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
662 req->error_loc = offsetof(struct nvme_identify, nsid);
663 status = NVME_SC_INVALID_NS | NVME_STATUS_DNR;
664 goto out;
665 }
666
667 id = kzalloc(sizeof(*id), GFP_KERNEL);
668 if (!id) {
669 status = NVME_SC_INTERNAL;
670 goto out;
671 }
672
673 /* return an all zeroed buffer if we can't find an active namespace */
674 status = nvmet_req_find_ns(req);
675 if (status) {
676 status = 0;
677 goto done;
678 }
679
680 if (nvmet_ns_revalidate(req->ns)) {
681 mutex_lock(&req->ns->subsys->lock);
682 nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
683 mutex_unlock(&req->ns->subsys->lock);
684 }
685
686 /*
687 * nuse = ncap = nsze isn't always true, but we have no way to find
688 * that out from the underlying device.
689 */
690 id->ncap = id->nsze =
691 cpu_to_le64(req->ns->size >> req->ns->blksize_shift);
692 switch (req->port->ana_state[req->ns->anagrpid]) {
693 case NVME_ANA_INACCESSIBLE:
694 case NVME_ANA_PERSISTENT_LOSS:
695 break;
696 default:
697 id->nuse = id->nsze;
698 break;
699 }
700
701 if (req->ns->bdev)
702 nvmet_bdev_set_limits(req->ns->bdev, id);
703
704 /*
705 * We just provide a single LBA format that matches what the
706 * underlying device reports.
707 */
708 id->nlbaf = 0;
709 id->flbas = 0;
710
711 /*
712 * Our namespace might always be shared. Not just with other
713 * controllers, but also with any other user of the block device.
714 */
715 id->nmic = NVME_NS_NMIC_SHARED;
716 id->anagrpid = cpu_to_le32(req->ns->anagrpid);
717
718 if (req->ns->pr.enable)
719 id->rescap = NVME_PR_SUPPORT_WRITE_EXCLUSIVE |
720 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS |
721 NVME_PR_SUPPORT_WRITE_EXCLUSIVE_REG_ONLY |
722 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_REG_ONLY |
723 NVME_PR_SUPPORT_WRITE_EXCLUSIVE_ALL_REGS |
724 NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_ALL_REGS |
725 NVME_PR_SUPPORT_IEKEY_VER_1_3_DEF;
726
727 /*
728 * Since we don't know any better, every namespace is its own endurance
729 * group.
730 */
731 id->endgid = cpu_to_le16(req->ns->nsid);
732
733 memcpy(&id->nguid, &req->ns->nguid, sizeof(id->nguid));
734
735 id->lbaf[0].ds = req->ns->blksize_shift;
736
737 if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns)) {
738 id->dpc = NVME_NS_DPC_PI_FIRST | NVME_NS_DPC_PI_LAST |
739 NVME_NS_DPC_PI_TYPE1 | NVME_NS_DPC_PI_TYPE2 |
740 NVME_NS_DPC_PI_TYPE3;
741 id->mc = NVME_MC_EXTENDED_LBA;
742 id->dps = req->ns->pi_type;
743 id->flbas = NVME_NS_FLBAS_META_EXT;
744 id->lbaf[0].ms = cpu_to_le16(req->ns->metadata_size);
745 }
746
747 if (req->ns->readonly)
748 id->nsattr |= NVME_NS_ATTR_RO;
749 done:
750 if (!status)
751 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
752
753 kfree(id);
754 out:
755 nvmet_req_complete(req, status);
756 }
757
nvmet_execute_identify_endgrp_list(struct nvmet_req * req)758 static void nvmet_execute_identify_endgrp_list(struct nvmet_req *req)
759 {
760 u16 min_endgid = le16_to_cpu(req->cmd->identify.cnssid);
761 static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
762 struct nvmet_ctrl *ctrl = req->sq->ctrl;
763 struct nvmet_ns *ns;
764 unsigned long idx;
765 __le16 *list;
766 u16 status;
767 int i = 1;
768
769 list = kzalloc(buf_size, GFP_KERNEL);
770 if (!list) {
771 status = NVME_SC_INTERNAL;
772 goto out;
773 }
774
775 xa_for_each(&ctrl->subsys->namespaces, idx, ns) {
776 if (ns->nsid <= min_endgid)
777 continue;
778
779 list[i++] = cpu_to_le16(ns->nsid);
780 if (i == buf_size / sizeof(__le16))
781 break;
782 }
783
784 list[0] = cpu_to_le16(i - 1);
785 status = nvmet_copy_to_sgl(req, 0, list, buf_size);
786 kfree(list);
787 out:
788 nvmet_req_complete(req, status);
789 }
790
nvmet_execute_identify_nslist(struct nvmet_req * req,bool match_css)791 static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css)
792 {
793 static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
794 struct nvmet_ctrl *ctrl = req->sq->ctrl;
795 struct nvmet_ns *ns;
796 unsigned long idx;
797 u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
798 __le32 *list;
799 u16 status = 0;
800 int i = 0;
801
802 /*
803 * NSID values 0xFFFFFFFE and NVME_NSID_ALL are invalid
804 * See NVMe Base Specification, Active Namespace ID list (CNS 02h).
805 */
806 if (min_nsid == 0xFFFFFFFE || min_nsid == NVME_NSID_ALL) {
807 req->error_loc = offsetof(struct nvme_identify, nsid);
808 status = NVME_SC_INVALID_NS | NVME_STATUS_DNR;
809 goto out;
810 }
811
812 list = kzalloc(buf_size, GFP_KERNEL);
813 if (!list) {
814 status = NVME_SC_INTERNAL;
815 goto out;
816 }
817
818 xa_for_each(&ctrl->subsys->namespaces, idx, ns) {
819 if (ns->nsid <= min_nsid)
820 continue;
821 if (match_css && req->ns->csi != req->cmd->identify.csi)
822 continue;
823 list[i++] = cpu_to_le32(ns->nsid);
824 if (i == buf_size / sizeof(__le32))
825 break;
826 }
827
828 status = nvmet_copy_to_sgl(req, 0, list, buf_size);
829
830 kfree(list);
831 out:
832 nvmet_req_complete(req, status);
833 }
834
nvmet_copy_ns_identifier(struct nvmet_req * req,u8 type,u8 len,void * id,off_t * off)835 static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
836 void *id, off_t *off)
837 {
838 struct nvme_ns_id_desc desc = {
839 .nidt = type,
840 .nidl = len,
841 };
842 u16 status;
843
844 status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
845 if (status)
846 return status;
847 *off += sizeof(desc);
848
849 status = nvmet_copy_to_sgl(req, *off, id, len);
850 if (status)
851 return status;
852 *off += len;
853
854 return 0;
855 }
856
nvmet_execute_identify_desclist(struct nvmet_req * req)857 static void nvmet_execute_identify_desclist(struct nvmet_req *req)
858 {
859 off_t off = 0;
860 u16 status;
861
862 status = nvmet_req_find_ns(req);
863 if (status)
864 goto out;
865
866 if (memchr_inv(&req->ns->uuid, 0, sizeof(req->ns->uuid))) {
867 status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
868 NVME_NIDT_UUID_LEN,
869 &req->ns->uuid, &off);
870 if (status)
871 goto out;
872 }
873 if (memchr_inv(req->ns->nguid, 0, sizeof(req->ns->nguid))) {
874 status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
875 NVME_NIDT_NGUID_LEN,
876 &req->ns->nguid, &off);
877 if (status)
878 goto out;
879 }
880
881 status = nvmet_copy_ns_identifier(req, NVME_NIDT_CSI,
882 NVME_NIDT_CSI_LEN,
883 &req->ns->csi, &off);
884 if (status)
885 goto out;
886
887 if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
888 off) != NVME_IDENTIFY_DATA_SIZE - off)
889 status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
890
891 out:
892 nvmet_req_complete(req, status);
893 }
894
nvmet_execute_identify_ctrl_nvm(struct nvmet_req * req)895 static void nvmet_execute_identify_ctrl_nvm(struct nvmet_req *req)
896 {
897 /* Not supported: return zeroes */
898 nvmet_req_complete(req,
899 nvmet_zero_sgl(req, 0, sizeof(struct nvme_id_ctrl_nvm)));
900 }
901
nvme_execute_identify_ns_nvm(struct nvmet_req * req)902 static void nvme_execute_identify_ns_nvm(struct nvmet_req *req)
903 {
904 u16 status;
905 struct nvme_id_ns_nvm *id;
906
907 status = nvmet_req_find_ns(req);
908 if (status)
909 goto out;
910
911 id = kzalloc(sizeof(*id), GFP_KERNEL);
912 if (!id) {
913 status = NVME_SC_INTERNAL;
914 goto out;
915 }
916 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
917 out:
918 nvmet_req_complete(req, status);
919 }
920
nvmet_execute_id_cs_indep(struct nvmet_req * req)921 static void nvmet_execute_id_cs_indep(struct nvmet_req *req)
922 {
923 struct nvme_id_ns_cs_indep *id;
924 u16 status;
925
926 status = nvmet_req_find_ns(req);
927 if (status)
928 goto out;
929
930 id = kzalloc(sizeof(*id), GFP_KERNEL);
931 if (!id) {
932 status = NVME_SC_INTERNAL;
933 goto out;
934 }
935
936 id->nstat = NVME_NSTAT_NRDY;
937 id->anagrpid = cpu_to_le32(req->ns->anagrpid);
938 id->nmic = NVME_NS_NMIC_SHARED;
939 if (req->ns->readonly)
940 id->nsattr |= NVME_NS_ATTR_RO;
941 if (req->ns->bdev && !bdev_nonrot(req->ns->bdev))
942 id->nsfeat |= NVME_NS_ROTATIONAL;
943 /*
944 * We need flush command to flush the file's metadata,
945 * so report supporting vwc if backend is file, even
946 * though buffered_io is disable.
947 */
948 if (req->ns->bdev && !bdev_write_cache(req->ns->bdev))
949 id->nsfeat |= NVME_NS_VWC_NOT_PRESENT;
950
951 status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
952 kfree(id);
953 out:
954 nvmet_req_complete(req, status);
955 }
956
nvmet_execute_identify(struct nvmet_req * req)957 static void nvmet_execute_identify(struct nvmet_req *req)
958 {
959 if (!nvmet_check_transfer_len(req, NVME_IDENTIFY_DATA_SIZE))
960 return;
961
962 switch (req->cmd->identify.cns) {
963 case NVME_ID_CNS_NS:
964 nvmet_execute_identify_ns(req);
965 return;
966 case NVME_ID_CNS_CTRL:
967 nvmet_execute_identify_ctrl(req);
968 return;
969 case NVME_ID_CNS_NS_ACTIVE_LIST:
970 nvmet_execute_identify_nslist(req, false);
971 return;
972 case NVME_ID_CNS_NS_DESC_LIST:
973 nvmet_execute_identify_desclist(req);
974 return;
975 case NVME_ID_CNS_CS_NS:
976 switch (req->cmd->identify.csi) {
977 case NVME_CSI_NVM:
978 nvme_execute_identify_ns_nvm(req);
979 return;
980 case NVME_CSI_ZNS:
981 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
982 nvmet_execute_identify_ns_zns(req);
983 return;
984 }
985 break;
986 }
987 break;
988 case NVME_ID_CNS_CS_CTRL:
989 switch (req->cmd->identify.csi) {
990 case NVME_CSI_NVM:
991 nvmet_execute_identify_ctrl_nvm(req);
992 return;
993 case NVME_CSI_ZNS:
994 if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
995 nvmet_execute_identify_ctrl_zns(req);
996 return;
997 }
998 break;
999 }
1000 break;
1001 case NVME_ID_CNS_NS_ACTIVE_LIST_CS:
1002 nvmet_execute_identify_nslist(req, true);
1003 return;
1004 case NVME_ID_CNS_NS_CS_INDEP:
1005 nvmet_execute_id_cs_indep(req);
1006 return;
1007 case NVME_ID_CNS_ENDGRP_LIST:
1008 nvmet_execute_identify_endgrp_list(req);
1009 return;
1010 }
1011
1012 pr_debug("unhandled identify cns %d on qid %d\n",
1013 req->cmd->identify.cns, req->sq->qid);
1014 req->error_loc = offsetof(struct nvme_identify, cns);
1015 nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_STATUS_DNR);
1016 }
1017
1018 /*
1019 * A "minimum viable" abort implementation: the command is mandatory in the
1020 * spec, but we are not required to do any useful work. We couldn't really
1021 * do a useful abort, so don't bother even with waiting for the command
1022 * to be exectuted and return immediately telling the command to abort
1023 * wasn't found.
1024 */
nvmet_execute_abort(struct nvmet_req * req)1025 static void nvmet_execute_abort(struct nvmet_req *req)
1026 {
1027 if (!nvmet_check_transfer_len(req, 0))
1028 return;
1029 nvmet_set_result(req, 1);
1030 nvmet_req_complete(req, 0);
1031 }
1032
nvmet_write_protect_flush_sync(struct nvmet_req * req)1033 static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
1034 {
1035 u16 status;
1036
1037 if (req->ns->file)
1038 status = nvmet_file_flush(req);
1039 else
1040 status = nvmet_bdev_flush(req);
1041
1042 if (status)
1043 pr_err("write protect flush failed nsid: %u\n", req->ns->nsid);
1044 return status;
1045 }
1046
nvmet_set_feat_write_protect(struct nvmet_req * req)1047 static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
1048 {
1049 u32 write_protect = le32_to_cpu(req->cmd->common.cdw11);
1050 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1051 u16 status;
1052
1053 status = nvmet_req_find_ns(req);
1054 if (status)
1055 return status;
1056
1057 mutex_lock(&subsys->lock);
1058 switch (write_protect) {
1059 case NVME_NS_WRITE_PROTECT:
1060 req->ns->readonly = true;
1061 status = nvmet_write_protect_flush_sync(req);
1062 if (status)
1063 req->ns->readonly = false;
1064 break;
1065 case NVME_NS_NO_WRITE_PROTECT:
1066 req->ns->readonly = false;
1067 status = 0;
1068 break;
1069 default:
1070 break;
1071 }
1072
1073 if (!status)
1074 nvmet_ns_changed(subsys, req->ns->nsid);
1075 mutex_unlock(&subsys->lock);
1076 return status;
1077 }
1078
nvmet_set_feat_kato(struct nvmet_req * req)1079 u16 nvmet_set_feat_kato(struct nvmet_req *req)
1080 {
1081 u32 val32 = le32_to_cpu(req->cmd->common.cdw11);
1082
1083 nvmet_stop_keep_alive_timer(req->sq->ctrl);
1084 req->sq->ctrl->kato = DIV_ROUND_UP(val32, 1000);
1085 nvmet_start_keep_alive_timer(req->sq->ctrl);
1086
1087 nvmet_set_result(req, req->sq->ctrl->kato);
1088
1089 return 0;
1090 }
1091
nvmet_set_feat_async_event(struct nvmet_req * req,u32 mask)1092 u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask)
1093 {
1094 u32 val32 = le32_to_cpu(req->cmd->common.cdw11);
1095
1096 if (val32 & ~mask) {
1097 req->error_loc = offsetof(struct nvme_common_command, cdw11);
1098 return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1099 }
1100
1101 WRITE_ONCE(req->sq->ctrl->aen_enabled, val32);
1102 nvmet_set_result(req, val32);
1103
1104 return 0;
1105 }
1106
nvmet_execute_set_features(struct nvmet_req * req)1107 void nvmet_execute_set_features(struct nvmet_req *req)
1108 {
1109 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1110 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
1111 u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1112 u16 status = 0;
1113 u16 nsqr;
1114 u16 ncqr;
1115
1116 if (!nvmet_check_data_len_lte(req, 0))
1117 return;
1118
1119 switch (cdw10 & 0xff) {
1120 case NVME_FEAT_NUM_QUEUES:
1121 ncqr = (cdw11 >> 16) & 0xffff;
1122 nsqr = cdw11 & 0xffff;
1123 if (ncqr == 0xffff || nsqr == 0xffff) {
1124 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1125 break;
1126 }
1127 nvmet_set_result(req,
1128 (subsys->max_qid - 1) | ((subsys->max_qid - 1) << 16));
1129 break;
1130 case NVME_FEAT_KATO:
1131 status = nvmet_set_feat_kato(req);
1132 break;
1133 case NVME_FEAT_ASYNC_EVENT:
1134 status = nvmet_set_feat_async_event(req, NVMET_AEN_CFG_ALL);
1135 break;
1136 case NVME_FEAT_HOST_ID:
1137 status = NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR;
1138 break;
1139 case NVME_FEAT_WRITE_PROTECT:
1140 status = nvmet_set_feat_write_protect(req);
1141 break;
1142 case NVME_FEAT_RESV_MASK:
1143 status = nvmet_set_feat_resv_notif_mask(req, cdw11);
1144 break;
1145 default:
1146 req->error_loc = offsetof(struct nvme_common_command, cdw10);
1147 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1148 break;
1149 }
1150
1151 nvmet_req_complete(req, status);
1152 }
1153
nvmet_get_feat_write_protect(struct nvmet_req * req)1154 static u16 nvmet_get_feat_write_protect(struct nvmet_req *req)
1155 {
1156 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1157 u32 result;
1158
1159 result = nvmet_req_find_ns(req);
1160 if (result)
1161 return result;
1162
1163 mutex_lock(&subsys->lock);
1164 if (req->ns->readonly == true)
1165 result = NVME_NS_WRITE_PROTECT;
1166 else
1167 result = NVME_NS_NO_WRITE_PROTECT;
1168 nvmet_set_result(req, result);
1169 mutex_unlock(&subsys->lock);
1170
1171 return 0;
1172 }
1173
nvmet_get_feat_kato(struct nvmet_req * req)1174 void nvmet_get_feat_kato(struct nvmet_req *req)
1175 {
1176 nvmet_set_result(req, req->sq->ctrl->kato * 1000);
1177 }
1178
nvmet_get_feat_async_event(struct nvmet_req * req)1179 void nvmet_get_feat_async_event(struct nvmet_req *req)
1180 {
1181 nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled));
1182 }
1183
nvmet_execute_get_features(struct nvmet_req * req)1184 void nvmet_execute_get_features(struct nvmet_req *req)
1185 {
1186 struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1187 u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
1188 u16 status = 0;
1189
1190 if (!nvmet_check_transfer_len(req, nvmet_feat_data_len(req, cdw10)))
1191 return;
1192
1193 switch (cdw10 & 0xff) {
1194 /*
1195 * These features are mandatory in the spec, but we don't
1196 * have a useful way to implement them. We'll eventually
1197 * need to come up with some fake values for these.
1198 */
1199 #if 0
1200 case NVME_FEAT_ARBITRATION:
1201 break;
1202 case NVME_FEAT_POWER_MGMT:
1203 break;
1204 case NVME_FEAT_TEMP_THRESH:
1205 break;
1206 case NVME_FEAT_ERR_RECOVERY:
1207 break;
1208 case NVME_FEAT_IRQ_COALESCE:
1209 break;
1210 case NVME_FEAT_IRQ_CONFIG:
1211 break;
1212 case NVME_FEAT_WRITE_ATOMIC:
1213 break;
1214 #endif
1215 case NVME_FEAT_ASYNC_EVENT:
1216 nvmet_get_feat_async_event(req);
1217 break;
1218 case NVME_FEAT_VOLATILE_WC:
1219 nvmet_set_result(req, 1);
1220 break;
1221 case NVME_FEAT_NUM_QUEUES:
1222 nvmet_set_result(req,
1223 (subsys->max_qid-1) | ((subsys->max_qid-1) << 16));
1224 break;
1225 case NVME_FEAT_KATO:
1226 nvmet_get_feat_kato(req);
1227 break;
1228 case NVME_FEAT_HOST_ID:
1229 /* need 128-bit host identifier flag */
1230 if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) {
1231 req->error_loc =
1232 offsetof(struct nvme_common_command, cdw11);
1233 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1234 break;
1235 }
1236
1237 status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
1238 sizeof(req->sq->ctrl->hostid));
1239 break;
1240 case NVME_FEAT_WRITE_PROTECT:
1241 status = nvmet_get_feat_write_protect(req);
1242 break;
1243 case NVME_FEAT_RESV_MASK:
1244 status = nvmet_get_feat_resv_notif_mask(req);
1245 break;
1246 default:
1247 req->error_loc =
1248 offsetof(struct nvme_common_command, cdw10);
1249 status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1250 break;
1251 }
1252
1253 nvmet_req_complete(req, status);
1254 }
1255
nvmet_execute_async_event(struct nvmet_req * req)1256 void nvmet_execute_async_event(struct nvmet_req *req)
1257 {
1258 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1259
1260 if (!nvmet_check_transfer_len(req, 0))
1261 return;
1262
1263 mutex_lock(&ctrl->lock);
1264 if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
1265 mutex_unlock(&ctrl->lock);
1266 nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_STATUS_DNR);
1267 return;
1268 }
1269 ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
1270 mutex_unlock(&ctrl->lock);
1271
1272 queue_work(nvmet_wq, &ctrl->async_event_work);
1273 }
1274
nvmet_execute_keep_alive(struct nvmet_req * req)1275 void nvmet_execute_keep_alive(struct nvmet_req *req)
1276 {
1277 struct nvmet_ctrl *ctrl = req->sq->ctrl;
1278 u16 status = 0;
1279
1280 if (!nvmet_check_transfer_len(req, 0))
1281 return;
1282
1283 if (!ctrl->kato) {
1284 status = NVME_SC_KA_TIMEOUT_INVALID;
1285 goto out;
1286 }
1287
1288 pr_debug("ctrl %d update keep-alive timer for %d secs\n",
1289 ctrl->cntlid, ctrl->kato);
1290 mod_delayed_work(system_wq, &ctrl->ka_work, ctrl->kato * HZ);
1291 out:
1292 nvmet_req_complete(req, status);
1293 }
1294
nvmet_parse_admin_cmd(struct nvmet_req * req)1295 u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
1296 {
1297 struct nvme_command *cmd = req->cmd;
1298 u16 ret;
1299
1300 if (nvme_is_fabrics(cmd))
1301 return nvmet_parse_fabrics_admin_cmd(req);
1302 if (nvmet_is_disc_subsys(nvmet_req_subsys(req)))
1303 return nvmet_parse_discovery_cmd(req);
1304
1305 ret = nvmet_check_ctrl_status(req);
1306 if (unlikely(ret))
1307 return ret;
1308
1309 if (nvmet_is_passthru_req(req))
1310 return nvmet_parse_passthru_admin_cmd(req);
1311
1312 switch (cmd->common.opcode) {
1313 case nvme_admin_get_log_page:
1314 req->execute = nvmet_execute_get_log_page;
1315 return 0;
1316 case nvme_admin_identify:
1317 req->execute = nvmet_execute_identify;
1318 return 0;
1319 case nvme_admin_abort_cmd:
1320 req->execute = nvmet_execute_abort;
1321 return 0;
1322 case nvme_admin_set_features:
1323 req->execute = nvmet_execute_set_features;
1324 return 0;
1325 case nvme_admin_get_features:
1326 req->execute = nvmet_execute_get_features;
1327 return 0;
1328 case nvme_admin_async_event:
1329 req->execute = nvmet_execute_async_event;
1330 return 0;
1331 case nvme_admin_keep_alive:
1332 req->execute = nvmet_execute_keep_alive;
1333 return 0;
1334 default:
1335 return nvmet_report_invalid_opcode(req);
1336 }
1337 }
1338