xref: /linux/drivers/nvme/target/admin-cmd.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
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 
15 static void nvmet_execute_delete_sq(struct nvmet_req *req)
16 {
17 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
18 	u16 sqid = le16_to_cpu(req->cmd->delete_queue.qid);
19 	u16 status;
20 
21 	if (!nvmet_is_pci_ctrl(ctrl)) {
22 		status = nvmet_report_invalid_opcode(req);
23 		goto complete;
24 	}
25 
26 	if (!sqid) {
27 		status = NVME_SC_QID_INVALID | NVME_STATUS_DNR;
28 		goto complete;
29 	}
30 
31 	status = nvmet_check_sqid(ctrl, sqid, false);
32 	if (status != NVME_SC_SUCCESS)
33 		goto complete;
34 
35 	status = ctrl->ops->delete_sq(ctrl, sqid);
36 
37 complete:
38 	nvmet_req_complete(req, status);
39 }
40 
41 static void nvmet_execute_create_sq(struct nvmet_req *req)
42 {
43 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
44 	struct nvme_command *cmd = req->cmd;
45 	u16 sqid = le16_to_cpu(cmd->create_sq.sqid);
46 	u16 cqid = le16_to_cpu(cmd->create_sq.cqid);
47 	u16 sq_flags = le16_to_cpu(cmd->create_sq.sq_flags);
48 	u16 qsize = le16_to_cpu(cmd->create_sq.qsize);
49 	u64 prp1 = le64_to_cpu(cmd->create_sq.prp1);
50 	u16 status;
51 
52 	if (!nvmet_is_pci_ctrl(ctrl)) {
53 		status = nvmet_report_invalid_opcode(req);
54 		goto complete;
55 	}
56 
57 	if (!sqid) {
58 		status = NVME_SC_QID_INVALID | NVME_STATUS_DNR;
59 		goto complete;
60 	}
61 
62 	status = nvmet_check_sqid(ctrl, sqid, true);
63 	if (status != NVME_SC_SUCCESS)
64 		goto complete;
65 
66 	status = nvmet_check_io_cqid(ctrl, cqid, false);
67 	if (status != NVME_SC_SUCCESS) {
68 		pr_err("SQ %u: Invalid CQID %u\n", sqid, cqid);
69 		goto complete;
70 	}
71 
72 	if (!qsize || qsize > NVME_CAP_MQES(ctrl->cap)) {
73 		status = NVME_SC_QUEUE_SIZE | NVME_STATUS_DNR;
74 		goto complete;
75 	}
76 
77 	status = ctrl->ops->create_sq(ctrl, sqid, cqid, sq_flags, qsize, prp1);
78 
79 complete:
80 	nvmet_req_complete(req, status);
81 }
82 
83 static void nvmet_execute_delete_cq(struct nvmet_req *req)
84 {
85 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
86 	u16 cqid = le16_to_cpu(req->cmd->delete_queue.qid);
87 	u16 status;
88 
89 	if (!nvmet_is_pci_ctrl(ctrl)) {
90 		status = nvmet_report_invalid_opcode(req);
91 		goto complete;
92 	}
93 
94 	status = nvmet_check_io_cqid(ctrl, cqid, false);
95 	if (status != NVME_SC_SUCCESS)
96 		goto complete;
97 
98 	if (!ctrl->cqs[cqid] || nvmet_cq_in_use(ctrl->cqs[cqid])) {
99 		/* Some SQs are still using this CQ */
100 		status = NVME_SC_QID_INVALID | NVME_STATUS_DNR;
101 		goto complete;
102 	}
103 
104 	status = ctrl->ops->delete_cq(ctrl, cqid);
105 
106 complete:
107 	nvmet_req_complete(req, status);
108 }
109 
110 static void nvmet_execute_create_cq(struct nvmet_req *req)
111 {
112 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
113 	struct nvme_command *cmd = req->cmd;
114 	u16 cqid = le16_to_cpu(cmd->create_cq.cqid);
115 	u16 cq_flags = le16_to_cpu(cmd->create_cq.cq_flags);
116 	u16 qsize = le16_to_cpu(cmd->create_cq.qsize);
117 	u16 irq_vector = le16_to_cpu(cmd->create_cq.irq_vector);
118 	u64 prp1 = le64_to_cpu(cmd->create_cq.prp1);
119 	u16 status;
120 
121 	if (!nvmet_is_pci_ctrl(ctrl)) {
122 		status = nvmet_report_invalid_opcode(req);
123 		goto complete;
124 	}
125 
126 	status = nvmet_check_io_cqid(ctrl, cqid, true);
127 	if (status != NVME_SC_SUCCESS)
128 		goto complete;
129 
130 	if (!qsize || qsize > NVME_CAP_MQES(ctrl->cap)) {
131 		status = NVME_SC_QUEUE_SIZE | NVME_STATUS_DNR;
132 		goto complete;
133 	}
134 
135 	status = ctrl->ops->create_cq(ctrl, cqid, cq_flags, qsize,
136 				      prp1, irq_vector);
137 
138 complete:
139 	nvmet_req_complete(req, status);
140 }
141 
142 u32 nvmet_get_log_page_len(struct nvme_command *cmd)
143 {
144 	u32 len = le16_to_cpu(cmd->get_log_page.numdu);
145 
146 	len <<= 16;
147 	len += le16_to_cpu(cmd->get_log_page.numdl);
148 	/* NUMD is a 0's based value */
149 	len += 1;
150 	len *= sizeof(u32);
151 
152 	return len;
153 }
154 
155 static u32 nvmet_feat_data_len(struct nvmet_req *req, u32 cdw10)
156 {
157 	switch (cdw10 & 0xff) {
158 	case NVME_FEAT_HOST_ID:
159 		return sizeof(req->sq->ctrl->hostid);
160 	default:
161 		return 0;
162 	}
163 }
164 
165 u64 nvmet_get_log_page_offset(struct nvme_command *cmd)
166 {
167 	return le64_to_cpu(cmd->get_log_page.lpo);
168 }
169 
170 static void nvmet_execute_get_log_page_noop(struct nvmet_req *req)
171 {
172 	nvmet_req_complete(req, nvmet_zero_sgl(req, 0, req->transfer_len));
173 }
174 
175 static void nvmet_execute_get_log_page_error(struct nvmet_req *req)
176 {
177 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
178 	unsigned long flags;
179 	off_t offset = 0;
180 	u64 slot;
181 	u64 i;
182 
183 	spin_lock_irqsave(&ctrl->error_lock, flags);
184 	slot = ctrl->err_counter % NVMET_ERROR_LOG_SLOTS;
185 
186 	for (i = 0; i < NVMET_ERROR_LOG_SLOTS; i++) {
187 		if (nvmet_copy_to_sgl(req, offset, &ctrl->slots[slot],
188 				sizeof(struct nvme_error_slot)))
189 			break;
190 
191 		if (slot == 0)
192 			slot = NVMET_ERROR_LOG_SLOTS - 1;
193 		else
194 			slot--;
195 		offset += sizeof(struct nvme_error_slot);
196 	}
197 	spin_unlock_irqrestore(&ctrl->error_lock, flags);
198 	nvmet_req_complete(req, 0);
199 }
200 
201 static void nvmet_execute_get_supported_log_pages(struct nvmet_req *req)
202 {
203 	struct nvme_supported_log *logs;
204 	u16 status;
205 
206 	logs = kzalloc_obj(*logs);
207 	if (!logs) {
208 		status = NVME_SC_INTERNAL;
209 		goto out;
210 	}
211 
212 	logs->lids[NVME_LOG_SUPPORTED] = cpu_to_le32(NVME_LIDS_LSUPP);
213 	logs->lids[NVME_LOG_ERROR] = cpu_to_le32(NVME_LIDS_LSUPP);
214 	logs->lids[NVME_LOG_SMART] = cpu_to_le32(NVME_LIDS_LSUPP);
215 	logs->lids[NVME_LOG_FW_SLOT] = cpu_to_le32(NVME_LIDS_LSUPP);
216 	logs->lids[NVME_LOG_CHANGED_NS] = cpu_to_le32(NVME_LIDS_LSUPP);
217 	logs->lids[NVME_LOG_CMD_EFFECTS] = cpu_to_le32(NVME_LIDS_LSUPP);
218 	logs->lids[NVME_LOG_ENDURANCE_GROUP] = cpu_to_le32(NVME_LIDS_LSUPP);
219 	logs->lids[NVME_LOG_ANA] = cpu_to_le32(NVME_LIDS_LSUPP);
220 	logs->lids[NVME_LOG_FEATURES] = cpu_to_le32(NVME_LIDS_LSUPP);
221 	logs->lids[NVME_LOG_RMI] = cpu_to_le32(NVME_LIDS_LSUPP);
222 	logs->lids[NVME_LOG_RESERVATION] = cpu_to_le32(NVME_LIDS_LSUPP);
223 
224 	status = nvmet_copy_to_sgl(req, 0, logs, sizeof(*logs));
225 	kfree(logs);
226 out:
227 	nvmet_req_complete(req, status);
228 }
229 
230 static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,
231 		struct nvme_smart_log *slog)
232 {
233 	u64 host_reads, host_writes, data_units_read, data_units_written;
234 	u16 status;
235 
236 	status = nvmet_req_find_ns(req);
237 	if (status)
238 		return status;
239 
240 	/* we don't have the right data for file backed ns */
241 	if (!req->ns->bdev)
242 		return NVME_SC_SUCCESS;
243 
244 	host_reads = part_stat_read(req->ns->bdev, ios[READ]);
245 	data_units_read =
246 		DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
247 	host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
248 	data_units_written =
249 		DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
250 
251 	put_unaligned_le64(host_reads, &slog->host_reads[0]);
252 	put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
253 	put_unaligned_le64(host_writes, &slog->host_writes[0]);
254 	put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
255 
256 	return NVME_SC_SUCCESS;
257 }
258 
259 static u16 nvmet_get_smart_log_all(struct nvmet_req *req,
260 		struct nvme_smart_log *slog)
261 {
262 	u64 host_reads = 0, host_writes = 0;
263 	u64 data_units_read = 0, data_units_written = 0;
264 	struct nvmet_ns *ns;
265 	struct nvmet_ctrl *ctrl;
266 	unsigned long idx;
267 
268 	ctrl = req->sq->ctrl;
269 	nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
270 		/* we don't have the right data for file backed ns */
271 		if (!ns->bdev)
272 			continue;
273 		host_reads += part_stat_read(ns->bdev, ios[READ]);
274 		data_units_read += DIV_ROUND_UP(
275 			part_stat_read(ns->bdev, sectors[READ]), 1000);
276 		host_writes += part_stat_read(ns->bdev, ios[WRITE]);
277 		data_units_written += DIV_ROUND_UP(
278 			part_stat_read(ns->bdev, sectors[WRITE]), 1000);
279 	}
280 
281 	put_unaligned_le64(host_reads, &slog->host_reads[0]);
282 	put_unaligned_le64(data_units_read, &slog->data_units_read[0]);
283 	put_unaligned_le64(host_writes, &slog->host_writes[0]);
284 	put_unaligned_le64(data_units_written, &slog->data_units_written[0]);
285 
286 	return NVME_SC_SUCCESS;
287 }
288 
289 static void nvmet_execute_get_log_page_rmi(struct nvmet_req *req)
290 {
291 	struct nvme_rotational_media_log *log;
292 	struct gendisk *disk;
293 	u16 status;
294 
295 	req->cmd->common.nsid = cpu_to_le32(le16_to_cpu(
296 					    req->cmd->get_log_page.lsi));
297 	status = nvmet_req_find_ns(req);
298 	if (status)
299 		goto out;
300 
301 	if (!req->ns->bdev || !bdev_rot(req->ns->bdev)) {
302 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
303 		goto out;
304 	}
305 
306 	if (req->transfer_len != sizeof(*log)) {
307 		status = NVME_SC_SGL_INVALID_DATA | NVME_STATUS_DNR;
308 		goto out;
309 	}
310 
311 	log = kzalloc_obj(*log);
312 	if (!log) {
313 		status = NVME_SC_INTERNAL;
314 		goto out;
315 	}
316 
317 	log->endgid = req->cmd->get_log_page.lsi;
318 	disk = req->ns->bdev->bd_disk;
319 	if (disk && disk->ia_ranges)
320 		log->numa = cpu_to_le16(disk->ia_ranges->nr_ia_ranges);
321 	else
322 		log->numa = cpu_to_le16(1);
323 
324 	status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
325 	kfree(log);
326 out:
327 	nvmet_req_complete(req, status);
328 }
329 
330 static void nvmet_execute_get_log_page_smart(struct nvmet_req *req)
331 {
332 	struct nvme_smart_log *log;
333 	u16 status = NVME_SC_INTERNAL;
334 	unsigned long flags;
335 
336 	if (req->transfer_len != sizeof(*log))
337 		goto out;
338 
339 	log = kzalloc_obj(*log);
340 	if (!log)
341 		goto out;
342 
343 	if (req->cmd->get_log_page.nsid == cpu_to_le32(NVME_NSID_ALL))
344 		status = nvmet_get_smart_log_all(req, log);
345 	else
346 		status = nvmet_get_smart_log_nsid(req, log);
347 	if (status)
348 		goto out_free_log;
349 
350 	spin_lock_irqsave(&req->sq->ctrl->error_lock, flags);
351 	put_unaligned_le64(req->sq->ctrl->err_counter,
352 			&log->num_err_log_entries);
353 	spin_unlock_irqrestore(&req->sq->ctrl->error_lock, flags);
354 
355 	status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
356 out_free_log:
357 	kfree(log);
358 out:
359 	nvmet_req_complete(req, status);
360 }
361 
362 static void nvmet_get_cmd_effects_admin(struct nvmet_ctrl *ctrl,
363 					struct nvme_effects_log *log)
364 {
365 	/* For a PCI target controller, advertize support for the . */
366 	if (nvmet_is_pci_ctrl(ctrl)) {
367 		log->acs[nvme_admin_delete_sq] =
368 		log->acs[nvme_admin_create_sq] =
369 		log->acs[nvme_admin_delete_cq] =
370 		log->acs[nvme_admin_create_cq] =
371 			cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
372 	}
373 
374 	log->acs[nvme_admin_get_log_page] =
375 	log->acs[nvme_admin_identify] =
376 	log->acs[nvme_admin_abort_cmd] =
377 	log->acs[nvme_admin_set_features] =
378 	log->acs[nvme_admin_get_features] =
379 	log->acs[nvme_admin_async_event] =
380 	log->acs[nvme_admin_keep_alive] =
381 		cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
382 }
383 
384 static void nvmet_get_cmd_effects_nvm(struct nvme_effects_log *log)
385 {
386 	log->iocs[nvme_cmd_read] =
387 	log->iocs[nvme_cmd_flush] =
388 	log->iocs[nvme_cmd_dsm]	=
389 	log->iocs[nvme_cmd_resv_acquire] =
390 	log->iocs[nvme_cmd_resv_register] =
391 	log->iocs[nvme_cmd_resv_release] =
392 	log->iocs[nvme_cmd_resv_report] =
393 		cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
394 	log->iocs[nvme_cmd_write] =
395 	log->iocs[nvme_cmd_write_zeroes] =
396 		cpu_to_le32(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC);
397 }
398 
399 static void nvmet_get_cmd_effects_zns(struct nvme_effects_log *log)
400 {
401 	log->iocs[nvme_cmd_zone_append] =
402 	log->iocs[nvme_cmd_zone_mgmt_send] =
403 		cpu_to_le32(NVME_CMD_EFFECTS_CSUPP | NVME_CMD_EFFECTS_LBCC);
404 	log->iocs[nvme_cmd_zone_mgmt_recv] =
405 		cpu_to_le32(NVME_CMD_EFFECTS_CSUPP);
406 }
407 
408 static void nvmet_execute_get_log_cmd_effects_ns(struct nvmet_req *req)
409 {
410 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
411 	struct nvme_effects_log *log;
412 	u16 status = NVME_SC_SUCCESS;
413 
414 	log = kzalloc_obj(*log);
415 	if (!log) {
416 		status = NVME_SC_INTERNAL;
417 		goto out;
418 	}
419 
420 	switch (req->cmd->get_log_page.csi) {
421 	case NVME_CSI_NVM:
422 		nvmet_get_cmd_effects_admin(ctrl, log);
423 		nvmet_get_cmd_effects_nvm(log);
424 		break;
425 	case NVME_CSI_ZNS:
426 		if (!IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
427 			status = NVME_SC_INVALID_IO_CMD_SET;
428 			goto free;
429 		}
430 		nvmet_get_cmd_effects_admin(ctrl, log);
431 		nvmet_get_cmd_effects_nvm(log);
432 		nvmet_get_cmd_effects_zns(log);
433 		break;
434 	default:
435 		status = NVME_SC_INVALID_LOG_PAGE;
436 		goto free;
437 	}
438 
439 	status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
440 free:
441 	kfree(log);
442 out:
443 	nvmet_req_complete(req, status);
444 }
445 
446 static void nvmet_execute_get_log_changed_ns(struct nvmet_req *req)
447 {
448 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
449 	u16 status = NVME_SC_INTERNAL;
450 	size_t len;
451 
452 	if (req->transfer_len != NVME_MAX_CHANGED_NAMESPACES * sizeof(__le32))
453 		goto out;
454 
455 	mutex_lock(&ctrl->lock);
456 	if (ctrl->nr_changed_ns == U32_MAX)
457 		len = sizeof(__le32);
458 	else
459 		len = ctrl->nr_changed_ns * sizeof(__le32);
460 	status = nvmet_copy_to_sgl(req, 0, ctrl->changed_ns_list, len);
461 	if (!status)
462 		status = nvmet_zero_sgl(req, len, req->transfer_len - len);
463 	ctrl->nr_changed_ns = 0;
464 	nvmet_clear_aen_bit(req, NVME_AEN_BIT_NS_ATTR);
465 	mutex_unlock(&ctrl->lock);
466 out:
467 	nvmet_req_complete(req, status);
468 }
469 
470 static u32 nvmet_format_ana_group(struct nvmet_req *req, u32 grpid,
471 		struct nvme_ana_group_desc *desc)
472 {
473 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
474 	struct nvmet_ns *ns;
475 	unsigned long idx;
476 	u32 count = 0;
477 
478 	if (!(req->cmd->get_log_page.lsp & NVME_ANA_LOG_RGO)) {
479 		nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
480 			if (ns->anagrpid == grpid)
481 				desc->nsids[count++] = cpu_to_le32(ns->nsid);
482 		}
483 	}
484 
485 	desc->grpid = cpu_to_le32(grpid);
486 	desc->nnsids = cpu_to_le32(count);
487 	desc->chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
488 	desc->state = req->port->ana_state[grpid];
489 	memset(desc->rsvd17, 0, sizeof(desc->rsvd17));
490 	return struct_size(desc, nsids, count);
491 }
492 
493 static void nvmet_execute_get_log_page_endgrp(struct nvmet_req *req)
494 {
495 	u64 host_reads, host_writes, data_units_read, data_units_written;
496 	struct nvme_endurance_group_log *log;
497 	u16 status;
498 
499 	/*
500 	 * The target driver emulates each endurance group as its own
501 	 * namespace, reusing the nsid as the endurance group identifier.
502 	 */
503 	req->cmd->common.nsid = cpu_to_le32(le16_to_cpu(
504 					    req->cmd->get_log_page.lsi));
505 	status = nvmet_req_find_ns(req);
506 	if (status)
507 		goto out;
508 
509 	log = kzalloc_obj(*log);
510 	if (!log) {
511 		status = NVME_SC_INTERNAL;
512 		goto out;
513 	}
514 
515 	if (!req->ns->bdev)
516 		goto copy;
517 
518 	host_reads = part_stat_read(req->ns->bdev, ios[READ]);
519 	data_units_read =
520 		DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[READ]), 1000);
521 	host_writes = part_stat_read(req->ns->bdev, ios[WRITE]);
522 	data_units_written =
523 		DIV_ROUND_UP(part_stat_read(req->ns->bdev, sectors[WRITE]), 1000);
524 
525 	put_unaligned_le64(host_reads, &log->hrc[0]);
526 	put_unaligned_le64(data_units_read, &log->dur[0]);
527 	put_unaligned_le64(host_writes, &log->hwc[0]);
528 	put_unaligned_le64(data_units_written, &log->duw[0]);
529 copy:
530 	status = nvmet_copy_to_sgl(req, 0, log, sizeof(*log));
531 	kfree(log);
532 out:
533 	nvmet_req_complete(req, status);
534 }
535 
536 static void nvmet_execute_get_log_page_ana(struct nvmet_req *req)
537 {
538 	struct nvme_ana_rsp_hdr hdr = { 0, };
539 	struct nvme_ana_group_desc *desc;
540 	size_t offset = sizeof(struct nvme_ana_rsp_hdr); /* start beyond hdr */
541 	size_t len;
542 	u32 grpid;
543 	u16 ngrps = 0;
544 	u16 status;
545 
546 	/*
547 	 * The header and one descriptor per enabled group are the only bytes
548 	 * written below; zero the whole transfer first so the unwritten tail
549 	 * does not leak stale kernel memory to the host.
550 	 */
551 	status = nvmet_zero_sgl(req, 0, req->transfer_len);
552 	if (status)
553 		goto out;
554 
555 	status = NVME_SC_INTERNAL;
556 	desc = kmalloc_flex(*desc, nsids, NVMET_MAX_NAMESPACES);
557 	if (!desc)
558 		goto out;
559 
560 	down_read(&nvmet_ana_sem);
561 	for (grpid = 1; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
562 		if (!nvmet_ana_group_enabled[grpid])
563 			continue;
564 		len = nvmet_format_ana_group(req, grpid, desc);
565 		status = nvmet_copy_to_sgl(req, offset, desc, len);
566 		if (status)
567 			break;
568 		offset += len;
569 		ngrps++;
570 	}
571 	for ( ; grpid <= NVMET_MAX_ANAGRPS; grpid++) {
572 		if (nvmet_ana_group_enabled[grpid])
573 			ngrps++;
574 	}
575 
576 	hdr.chgcnt = cpu_to_le64(nvmet_ana_chgcnt);
577 	hdr.ngrps = cpu_to_le16(ngrps);
578 	nvmet_clear_aen_bit(req, NVME_AEN_BIT_ANA_CHANGE);
579 	up_read(&nvmet_ana_sem);
580 
581 	kfree(desc);
582 
583 	/* copy the header last once we know the number of groups */
584 	status = nvmet_copy_to_sgl(req, 0, &hdr, sizeof(hdr));
585 out:
586 	nvmet_req_complete(req, status);
587 }
588 
589 static void nvmet_execute_get_log_page_features(struct nvmet_req *req)
590 {
591 	struct nvme_supported_features_log *features;
592 	u16 status;
593 
594 	features = kzalloc_obj(*features);
595 	if (!features) {
596 		status = NVME_SC_INTERNAL;
597 		goto out;
598 	}
599 
600 	features->fis[NVME_FEAT_NUM_QUEUES] =
601 		cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
602 	features->fis[NVME_FEAT_KATO] =
603 		cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
604 	features->fis[NVME_FEAT_ASYNC_EVENT] =
605 		cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
606 	features->fis[NVME_FEAT_HOST_ID] =
607 		cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_CSCPE);
608 	features->fis[NVME_FEAT_WRITE_PROTECT] =
609 		cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_NSCPE);
610 	features->fis[NVME_FEAT_RESV_MASK] =
611 		cpu_to_le32(NVME_FIS_FSUPP | NVME_FIS_NSCPE);
612 
613 	status = nvmet_copy_to_sgl(req, 0, features, sizeof(*features));
614 	kfree(features);
615 out:
616 	nvmet_req_complete(req, status);
617 }
618 
619 static void nvmet_execute_get_log_page(struct nvmet_req *req)
620 {
621 	if (!nvmet_check_transfer_len(req, nvmet_get_log_page_len(req->cmd)))
622 		return;
623 
624 	switch (req->cmd->get_log_page.lid) {
625 	case NVME_LOG_SUPPORTED:
626 		return nvmet_execute_get_supported_log_pages(req);
627 	case NVME_LOG_ERROR:
628 		return nvmet_execute_get_log_page_error(req);
629 	case NVME_LOG_SMART:
630 		return nvmet_execute_get_log_page_smart(req);
631 	case NVME_LOG_FW_SLOT:
632 		/*
633 		 * We only support a single firmware slot which always is
634 		 * active, so we can zero out the whole firmware slot log and
635 		 * still claim to fully implement this mandatory log page.
636 		 */
637 		return nvmet_execute_get_log_page_noop(req);
638 	case NVME_LOG_CHANGED_NS:
639 		return nvmet_execute_get_log_changed_ns(req);
640 	case NVME_LOG_CMD_EFFECTS:
641 		return nvmet_execute_get_log_cmd_effects_ns(req);
642 	case NVME_LOG_ENDURANCE_GROUP:
643 		return nvmet_execute_get_log_page_endgrp(req);
644 	case NVME_LOG_ANA:
645 		return nvmet_execute_get_log_page_ana(req);
646 	case NVME_LOG_FEATURES:
647 		return nvmet_execute_get_log_page_features(req);
648 	case NVME_LOG_RMI:
649 		return nvmet_execute_get_log_page_rmi(req);
650 	case NVME_LOG_RESERVATION:
651 		return nvmet_execute_get_log_page_resv(req);
652 	}
653 	pr_debug("unhandled lid %d on qid %d\n",
654 	       req->cmd->get_log_page.lid, req->sq->qid);
655 	req->error_loc = offsetof(struct nvme_get_log_page_command, lid);
656 	nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_STATUS_DNR);
657 }
658 
659 static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
660 {
661 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
662 	struct nvmet_subsys *subsys = ctrl->subsys;
663 	struct nvme_id_ctrl *id;
664 	u32 cmd_capsule_size, ctratt;
665 	u16 status = 0;
666 
667 	if (!subsys->subsys_discovered) {
668 		mutex_lock(&subsys->lock);
669 		subsys->subsys_discovered = true;
670 		mutex_unlock(&subsys->lock);
671 	}
672 
673 	id = kzalloc_obj(*id);
674 	if (!id) {
675 		status = NVME_SC_INTERNAL;
676 		goto out;
677 	}
678 
679 	id->vid = cpu_to_le16(subsys->vendor_id);
680 	id->ssvid = cpu_to_le16(subsys->subsys_vendor_id);
681 
682 	memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
683 	memcpy_and_pad(id->mn, sizeof(id->mn), subsys->model_number,
684 		       strlen(subsys->model_number), ' ');
685 	memcpy_and_pad(id->fr, sizeof(id->fr),
686 		       subsys->firmware_rev, strlen(subsys->firmware_rev), ' ');
687 
688 	put_unaligned_le24(subsys->ieee_oui, id->ieee);
689 
690 	id->rab = 6;
691 
692 	if (nvmet_is_disc_subsys(ctrl->subsys))
693 		id->cntrltype = NVME_CTRL_DISC;
694 	else
695 		id->cntrltype = NVME_CTRL_IO;
696 
697 	/* we support multiple ports, multiples hosts and ANA: */
698 	id->cmic = NVME_CTRL_CMIC_MULTI_PORT | NVME_CTRL_CMIC_MULTI_CTRL |
699 		NVME_CTRL_CMIC_ANA;
700 
701 	/* Limit MDTS according to port config or transport capability */
702 	id->mdts = nvmet_ctrl_mdts(req);
703 	id->cntlid = cpu_to_le16(ctrl->cntlid);
704 	id->ver = cpu_to_le32(ctrl->subsys->ver);
705 
706 	/* XXX: figure out what to do about RTD3R/RTD3 */
707 	id->oaes = cpu_to_le32(NVMET_AEN_CFG_OPTIONAL);
708 	ctratt = NVME_CTRL_ATTR_HID_128_BIT;
709 	if (ctrl->tbkas)
710 		ctratt |= NVME_CTRL_ATTR_TBKAS;
711 	if (nvmet_is_pci_ctrl(ctrl))
712 		ctratt |= NVME_CTRL_ATTR_RHII;
713 	id->ctratt = cpu_to_le32(ctratt);
714 
715 	id->oacs = 0;
716 
717 	/*
718 	 * We don't really have a practical limit on the number of abort
719 	 * commands.  But we don't do anything useful for abort either, so
720 	 * no point in allowing more abort commands than the spec requires.
721 	 */
722 	id->acl = 3;
723 
724 	id->aerl = NVMET_ASYNC_EVENTS - 1;
725 
726 	/* first slot is read-only, only one slot supported */
727 	id->frmw = (1 << 0) | (1 << 1);
728 	id->lpa = (1 << 0) | (1 << 1) | (1 << 2);
729 	id->elpe = NVMET_ERROR_LOG_SLOTS - 1;
730 	id->npss = 0;
731 
732 	/* We support keep-alive timeout in granularity of seconds */
733 	id->kas = cpu_to_le16(NVMET_KAS);
734 
735 	id->sqes = (0x6 << 4) | 0x6;
736 	id->cqes = (0x4 << 4) | 0x4;
737 
738 	/* no enforcement soft-limit for maxcmd - pick arbitrary high value */
739 	id->maxcmd = cpu_to_le16(NVMET_MAX_CMD(ctrl));
740 
741 	id->nn = cpu_to_le32(NVMET_MAX_NAMESPACES);
742 	id->mnan = cpu_to_le32(NVMET_MAX_NAMESPACES);
743 	id->oncs = cpu_to_le16(NVME_CTRL_ONCS_DSM |
744 			NVME_CTRL_ONCS_WRITE_ZEROES |
745 			NVME_CTRL_ONCS_RESERVATIONS);
746 
747 	/* XXX: don't report vwc if the underlying device is write through */
748 	id->vwc = NVME_CTRL_VWC_PRESENT;
749 
750 	/*
751 	 * We can't support atomic writes bigger than a LBA without support
752 	 * from the backend device.
753 	 */
754 	id->awun = 0;
755 	id->awupf = 0;
756 
757 	/* we always support SGLs */
758 	id->sgls = cpu_to_le32(NVME_CTRL_SGLS_BYTE_ALIGNED);
759 	if (ctrl->ops->flags & NVMF_KEYED_SGLS)
760 		id->sgls |= cpu_to_le32(NVME_CTRL_SGLS_KSDBDS);
761 	if (req->port->inline_data_size)
762 		id->sgls |= cpu_to_le32(NVME_CTRL_SGLS_SAOS);
763 
764 	strscpy(id->subnqn, ctrl->subsys->subsysnqn, sizeof(id->subnqn));
765 
766 	/*
767 	 * Max command capsule size is sqe + in-capsule data size.
768 	 * Disable in-capsule data for Metadata capable controllers.
769 	 */
770 	cmd_capsule_size = sizeof(struct nvme_command);
771 	if (!ctrl->pi_support)
772 		cmd_capsule_size += req->port->inline_data_size;
773 	id->ioccsz = cpu_to_le32(cmd_capsule_size / 16);
774 
775 	/* Max response capsule size is cqe */
776 	id->iorcsz = cpu_to_le32(sizeof(struct nvme_completion) / 16);
777 
778 	id->msdbd = ctrl->ops->msdbd;
779 
780 	/*
781 	 * Endurance group identifier is 16 bits, so we can't let namespaces
782 	 * overflow that since we reuse the nsid
783 	 */
784 	BUILD_BUG_ON(NVMET_MAX_NAMESPACES > USHRT_MAX);
785 	id->endgidmax = cpu_to_le16(NVMET_MAX_NAMESPACES);
786 
787 	id->anacap = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
788 	id->anatt = 10; /* random value */
789 	id->anagrpmax = cpu_to_le32(NVMET_MAX_ANAGRPS);
790 	id->nanagrpid = cpu_to_le32(NVMET_MAX_ANAGRPS);
791 
792 	/*
793 	 * Meh, we don't really support any power state.  Fake up the same
794 	 * values that qemu does.
795 	 */
796 	id->psd[0].max_power = cpu_to_le16(0x9c4);
797 	id->psd[0].entry_lat = cpu_to_le32(0x10);
798 	id->psd[0].exit_lat = cpu_to_le32(0x4);
799 
800 	id->nwpc = 1 << 0; /* write protect and no write protect */
801 
802 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
803 
804 	kfree(id);
805 out:
806 	nvmet_req_complete(req, status);
807 }
808 
809 static void nvmet_execute_identify_ns(struct nvmet_req *req)
810 {
811 	struct nvme_id_ns *id;
812 	u16 status;
813 
814 	if (le32_to_cpu(req->cmd->identify.nsid) == NVME_NSID_ALL) {
815 		req->error_loc = offsetof(struct nvme_identify, nsid);
816 		status = NVME_SC_INVALID_NS | NVME_STATUS_DNR;
817 		goto out;
818 	}
819 
820 	id = kzalloc_obj(*id);
821 	if (!id) {
822 		status = NVME_SC_INTERNAL;
823 		goto out;
824 	}
825 
826 	/* return an all zeroed buffer if we can't find an active namespace */
827 	status = nvmet_req_find_ns(req);
828 	if (status) {
829 		status = 0;
830 		goto done;
831 	}
832 
833 	if (nvmet_ns_revalidate(req->ns)) {
834 		mutex_lock(&req->ns->subsys->lock);
835 		nvmet_ns_changed(req->ns->subsys, req->ns->nsid);
836 		mutex_unlock(&req->ns->subsys->lock);
837 	}
838 
839 	/*
840 	 * nuse = ncap = nsze isn't always true, but we have no way to find
841 	 * that out from the underlying device.
842 	 */
843 	id->ncap = id->nsze =
844 		cpu_to_le64(req->ns->size >> req->ns->blksize_shift);
845 	switch (req->port->ana_state[req->ns->anagrpid]) {
846 	case NVME_ANA_INACCESSIBLE:
847 	case NVME_ANA_PERSISTENT_LOSS:
848 		break;
849 	default:
850 		id->nuse = id->nsze;
851 		break;
852 	}
853 
854 	if (req->ns->bdev)
855 		nvmet_bdev_set_limits(req->ns->bdev, id);
856 
857 	/*
858 	 * We just provide a single LBA format that matches what the
859 	 * underlying device reports.
860 	 */
861 	id->nlbaf = 0;
862 	id->flbas = 0;
863 
864 	/*
865 	 * Our namespace might always be shared.  Not just with other
866 	 * controllers, but also with any other user of the block device.
867 	 */
868 	id->nmic = NVME_NS_NMIC_SHARED;
869 	id->anagrpid = cpu_to_le32(req->ns->anagrpid);
870 
871 	if (req->ns->pr.enable)
872 		id->rescap = NVME_PR_SUPPORT_WRITE_EXCLUSIVE |
873 			NVME_PR_SUPPORT_EXCLUSIVE_ACCESS |
874 			NVME_PR_SUPPORT_WRITE_EXCLUSIVE_REG_ONLY |
875 			NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_REG_ONLY |
876 			NVME_PR_SUPPORT_WRITE_EXCLUSIVE_ALL_REGS |
877 			NVME_PR_SUPPORT_EXCLUSIVE_ACCESS_ALL_REGS |
878 			NVME_PR_SUPPORT_IEKEY_VER_1_3_DEF;
879 
880 	/*
881 	 * Since we don't know any better, every namespace is its own endurance
882 	 * group.
883 	 */
884 	id->endgid = cpu_to_le16(req->ns->nsid);
885 
886 	memcpy(&id->nguid, &req->ns->nguid, sizeof(id->nguid));
887 
888 	id->lbaf[0].ds = req->ns->blksize_shift;
889 
890 	if (req->sq->ctrl->pi_support && nvmet_ns_has_pi(req->ns)) {
891 		id->dpc = NVME_NS_DPC_PI_FIRST | NVME_NS_DPC_PI_LAST |
892 			  NVME_NS_DPC_PI_TYPE1 | NVME_NS_DPC_PI_TYPE2 |
893 			  NVME_NS_DPC_PI_TYPE3;
894 		id->mc = NVME_MC_EXTENDED_LBA;
895 		id->dps = req->ns->pi_type;
896 		id->flbas = NVME_NS_FLBAS_META_EXT;
897 		id->lbaf[0].ms = cpu_to_le16(req->ns->metadata_size);
898 	}
899 
900 	if (req->ns->readonly)
901 		id->nsattr |= NVME_NS_ATTR_RO;
902 done:
903 	if (!status)
904 		status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
905 
906 	kfree(id);
907 out:
908 	nvmet_req_complete(req, status);
909 }
910 
911 static void nvmet_execute_identify_endgrp_list(struct nvmet_req *req)
912 {
913 	u16 min_endgid = le16_to_cpu(req->cmd->identify.cnssid);
914 	static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
915 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
916 	struct nvmet_ns *ns;
917 	unsigned long idx;
918 	__le16 *list;
919 	u16 status;
920 	int i = 1;
921 
922 	list = kzalloc(buf_size, GFP_KERNEL);
923 	if (!list) {
924 		status = NVME_SC_INTERNAL;
925 		goto out;
926 	}
927 
928 	nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
929 		if (ns->nsid <= min_endgid)
930 			continue;
931 
932 		list[i++] = cpu_to_le16(ns->nsid);
933 		if (i == buf_size / sizeof(__le16))
934 			break;
935 	}
936 
937 	list[0] = cpu_to_le16(i - 1);
938 	status = nvmet_copy_to_sgl(req, 0, list, buf_size);
939 	kfree(list);
940 out:
941 	nvmet_req_complete(req, status);
942 }
943 
944 static void nvmet_execute_identify_nslist(struct nvmet_req *req, bool match_css)
945 {
946 	static const int buf_size = NVME_IDENTIFY_DATA_SIZE;
947 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
948 	struct nvmet_ns *ns;
949 	unsigned long idx;
950 	u32 min_nsid = le32_to_cpu(req->cmd->identify.nsid);
951 	__le32 *list;
952 	u16 status = 0;
953 	int i = 0;
954 
955 	/*
956 	 * NSID values 0xFFFFFFFE and NVME_NSID_ALL are invalid
957 	 * See NVMe Base Specification, Active Namespace ID list (CNS 02h).
958 	 */
959 	if (min_nsid == 0xFFFFFFFE || min_nsid == NVME_NSID_ALL) {
960 		req->error_loc = offsetof(struct nvme_identify, nsid);
961 		status = NVME_SC_INVALID_NS | NVME_STATUS_DNR;
962 		goto out;
963 	}
964 
965 	list = kzalloc(buf_size, GFP_KERNEL);
966 	if (!list) {
967 		status = NVME_SC_INTERNAL;
968 		goto out;
969 	}
970 
971 	nvmet_for_each_enabled_ns(&ctrl->subsys->namespaces, idx, ns) {
972 		if (ns->nsid <= min_nsid)
973 			continue;
974 		if (match_css && ns->csi != req->cmd->identify.csi)
975 			continue;
976 		list[i++] = cpu_to_le32(ns->nsid);
977 		if (i == buf_size / sizeof(__le32))
978 			break;
979 	}
980 
981 	status = nvmet_copy_to_sgl(req, 0, list, buf_size);
982 
983 	kfree(list);
984 out:
985 	nvmet_req_complete(req, status);
986 }
987 
988 static u16 nvmet_copy_ns_identifier(struct nvmet_req *req, u8 type, u8 len,
989 				    void *id, off_t *off)
990 {
991 	struct nvme_ns_id_desc desc = {
992 		.nidt = type,
993 		.nidl = len,
994 	};
995 	u16 status;
996 
997 	status = nvmet_copy_to_sgl(req, *off, &desc, sizeof(desc));
998 	if (status)
999 		return status;
1000 	*off += sizeof(desc);
1001 
1002 	status = nvmet_copy_to_sgl(req, *off, id, len);
1003 	if (status)
1004 		return status;
1005 	*off += len;
1006 
1007 	return 0;
1008 }
1009 
1010 static void nvmet_execute_identify_desclist(struct nvmet_req *req)
1011 {
1012 	off_t off = 0;
1013 	u16 status;
1014 
1015 	status = nvmet_req_find_ns(req);
1016 	if (status)
1017 		goto out;
1018 
1019 	if (memchr_inv(&req->ns->uuid, 0, sizeof(req->ns->uuid))) {
1020 		status = nvmet_copy_ns_identifier(req, NVME_NIDT_UUID,
1021 						  NVME_NIDT_UUID_LEN,
1022 						  &req->ns->uuid, &off);
1023 		if (status)
1024 			goto out;
1025 	}
1026 	if (memchr_inv(req->ns->nguid, 0, sizeof(req->ns->nguid))) {
1027 		status = nvmet_copy_ns_identifier(req, NVME_NIDT_NGUID,
1028 						  NVME_NIDT_NGUID_LEN,
1029 						  &req->ns->nguid, &off);
1030 		if (status)
1031 			goto out;
1032 	}
1033 
1034 	status = nvmet_copy_ns_identifier(req, NVME_NIDT_CSI,
1035 					  NVME_NIDT_CSI_LEN,
1036 					  &req->ns->csi, &off);
1037 	if (status)
1038 		goto out;
1039 
1040 	if (sg_zero_buffer(req->sg, req->sg_cnt, NVME_IDENTIFY_DATA_SIZE - off,
1041 			off) != NVME_IDENTIFY_DATA_SIZE - off)
1042 		status = NVME_SC_INTERNAL | NVME_STATUS_DNR;
1043 
1044 out:
1045 	nvmet_req_complete(req, status);
1046 }
1047 
1048 static void nvmet_execute_identify_ctrl_nvm(struct nvmet_req *req)
1049 {
1050 	/* Not supported: return zeroes */
1051 	nvmet_req_complete(req,
1052 		   nvmet_zero_sgl(req, 0, sizeof(struct nvme_id_ctrl_nvm)));
1053 }
1054 
1055 static void nvme_execute_identify_ns_nvm(struct nvmet_req *req)
1056 {
1057 	u16 status;
1058 	struct nvme_id_ns_nvm *id;
1059 
1060 	status = nvmet_req_find_ns(req);
1061 	if (status)
1062 		goto out;
1063 
1064 	id = kzalloc_obj(*id);
1065 	if (!id) {
1066 		status = NVME_SC_INTERNAL;
1067 		goto out;
1068 	}
1069 	if (req->ns->bdev)
1070 		nvmet_bdev_set_nvm_limits(req->ns->bdev, id);
1071 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
1072 	kfree(id);
1073 out:
1074 	nvmet_req_complete(req, status);
1075 }
1076 
1077 static void nvmet_execute_id_cs_indep(struct nvmet_req *req)
1078 {
1079 	struct nvme_id_ns_cs_indep *id;
1080 	u16 status;
1081 
1082 	status = nvmet_req_find_ns(req);
1083 	if (status)
1084 		goto out;
1085 
1086 	id = kzalloc_obj(*id);
1087 	if (!id) {
1088 		status = NVME_SC_INTERNAL;
1089 		goto out;
1090 	}
1091 
1092 	id->nstat = NVME_NSTAT_NRDY;
1093 	id->anagrpid = cpu_to_le32(req->ns->anagrpid);
1094 	id->nmic = NVME_NS_NMIC_SHARED;
1095 	if (req->ns->readonly)
1096 		id->nsattr |= NVME_NS_ATTR_RO;
1097 	if (req->ns->bdev && bdev_rot(req->ns->bdev))
1098 		id->nsfeat |= NVME_NS_ROTATIONAL;
1099 	/*
1100 	 * We need flush command to flush the file's metadata,
1101 	 * so report supporting vwc if backend is file, even
1102 	 * though buffered_io is disable.
1103 	 */
1104 	if (req->ns->bdev && !bdev_write_cache(req->ns->bdev))
1105 		id->nsfeat |= NVME_NS_VWC_NOT_PRESENT;
1106 
1107 	status = nvmet_copy_to_sgl(req, 0, id, sizeof(*id));
1108 	kfree(id);
1109 out:
1110 	nvmet_req_complete(req, status);
1111 }
1112 
1113 static void nvmet_execute_identify(struct nvmet_req *req)
1114 {
1115 	if (!nvmet_check_transfer_len(req, NVME_IDENTIFY_DATA_SIZE))
1116 		return;
1117 
1118 	switch (req->cmd->identify.cns) {
1119 	case NVME_ID_CNS_NS:
1120 		nvmet_execute_identify_ns(req);
1121 		return;
1122 	case NVME_ID_CNS_CTRL:
1123 		nvmet_execute_identify_ctrl(req);
1124 		return;
1125 	case NVME_ID_CNS_NS_ACTIVE_LIST:
1126 		nvmet_execute_identify_nslist(req, false);
1127 		return;
1128 	case NVME_ID_CNS_NS_DESC_LIST:
1129 		nvmet_execute_identify_desclist(req);
1130 		return;
1131 	case NVME_ID_CNS_CS_NS:
1132 		switch (req->cmd->identify.csi) {
1133 		case NVME_CSI_NVM:
1134 			nvme_execute_identify_ns_nvm(req);
1135 			return;
1136 		case NVME_CSI_ZNS:
1137 			if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
1138 				nvmet_execute_identify_ns_zns(req);
1139 				return;
1140 			}
1141 			break;
1142 		}
1143 		break;
1144 	case NVME_ID_CNS_CS_CTRL:
1145 		switch (req->cmd->identify.csi) {
1146 		case NVME_CSI_NVM:
1147 			nvmet_execute_identify_ctrl_nvm(req);
1148 			return;
1149 		case NVME_CSI_ZNS:
1150 			if (IS_ENABLED(CONFIG_BLK_DEV_ZONED)) {
1151 				nvmet_execute_identify_ctrl_zns(req);
1152 				return;
1153 			}
1154 			break;
1155 		}
1156 		break;
1157 	case NVME_ID_CNS_NS_ACTIVE_LIST_CS:
1158 		nvmet_execute_identify_nslist(req, true);
1159 		return;
1160 	case NVME_ID_CNS_NS_CS_INDEP:
1161 		nvmet_execute_id_cs_indep(req);
1162 		return;
1163 	case NVME_ID_CNS_ENDGRP_LIST:
1164 		nvmet_execute_identify_endgrp_list(req);
1165 		return;
1166 	}
1167 
1168 	pr_debug("unhandled identify cns %d on qid %d\n",
1169 	       req->cmd->identify.cns, req->sq->qid);
1170 	req->error_loc = offsetof(struct nvme_identify, cns);
1171 	nvmet_req_complete(req, NVME_SC_INVALID_FIELD | NVME_STATUS_DNR);
1172 }
1173 
1174 /*
1175  * A "minimum viable" abort implementation: the command is mandatory in the
1176  * spec, but we are not required to do any useful work.  We couldn't really
1177  * do a useful abort, so don't bother even with waiting for the command
1178  * to be executed and return immediately telling the command to abort
1179  * wasn't found.
1180  */
1181 static void nvmet_execute_abort(struct nvmet_req *req)
1182 {
1183 	if (!nvmet_check_transfer_len(req, 0))
1184 		return;
1185 	nvmet_set_result(req, 1);
1186 	nvmet_req_complete(req, 0);
1187 }
1188 
1189 static u16 nvmet_write_protect_flush_sync(struct nvmet_req *req)
1190 {
1191 	u16 status;
1192 
1193 	if (req->ns->file)
1194 		status = nvmet_file_flush(req);
1195 	else
1196 		status = nvmet_bdev_flush(req);
1197 
1198 	if (status)
1199 		pr_err("write protect flush failed nsid: %u\n", req->ns->nsid);
1200 	return status;
1201 }
1202 
1203 static u16 nvmet_set_feat_write_protect(struct nvmet_req *req)
1204 {
1205 	u32 write_protect = le32_to_cpu(req->cmd->common.cdw11);
1206 	struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1207 	u16 status;
1208 
1209 	status = nvmet_req_find_ns(req);
1210 	if (status)
1211 		return status;
1212 
1213 	mutex_lock(&subsys->lock);
1214 	switch (write_protect) {
1215 	case NVME_NS_WRITE_PROTECT:
1216 		req->ns->readonly = true;
1217 		status = nvmet_write_protect_flush_sync(req);
1218 		if (status)
1219 			req->ns->readonly = false;
1220 		break;
1221 	case NVME_NS_NO_WRITE_PROTECT:
1222 		req->ns->readonly = false;
1223 		status = 0;
1224 		break;
1225 	default:
1226 		break;
1227 	}
1228 
1229 	if (!status)
1230 		nvmet_ns_changed(subsys, req->ns->nsid);
1231 	mutex_unlock(&subsys->lock);
1232 	return status;
1233 }
1234 
1235 u16 nvmet_set_feat_kato(struct nvmet_req *req)
1236 {
1237 	u32 val32 = le32_to_cpu(req->cmd->common.cdw11);
1238 
1239 	if (val32 > NVMET_MAX_KATO * MSEC_PER_SEC) {
1240 		req->error_loc = offsetof(struct nvme_common_command, cdw11);
1241 		return NVME_SC_KA_TIMEOUT_INVALID | NVME_STATUS_DNR;
1242 	}
1243 
1244 	nvmet_stop_keep_alive_timer(req->sq->ctrl);
1245 	req->sq->ctrl->kato = DIV_ROUND_UP_ULL(val32, MSEC_PER_SEC);
1246 	nvmet_start_keep_alive_timer(req->sq->ctrl);
1247 
1248 	nvmet_set_result(req, req->sq->ctrl->kato);
1249 
1250 	return 0;
1251 }
1252 
1253 u16 nvmet_set_feat_async_event(struct nvmet_req *req, u32 mask)
1254 {
1255 	u32 val32 = le32_to_cpu(req->cmd->common.cdw11);
1256 
1257 	if (val32 & ~mask) {
1258 		req->error_loc = offsetof(struct nvme_common_command, cdw11);
1259 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1260 	}
1261 
1262 	WRITE_ONCE(req->sq->ctrl->aen_enabled, val32);
1263 	nvmet_set_result(req, val32);
1264 
1265 	return 0;
1266 }
1267 
1268 static u16 nvmet_set_feat_host_id(struct nvmet_req *req)
1269 {
1270 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1271 
1272 	if (!nvmet_is_pci_ctrl(ctrl))
1273 		return NVME_SC_CMD_SEQ_ERROR | NVME_STATUS_DNR;
1274 
1275 	/*
1276 	 * The NVMe base specifications v2.1 recommends supporting 128-bits host
1277 	 * IDs (section 5.1.25.1.28.1). However, that same section also says
1278 	 * that "The controller may support a 64-bit Host Identifier and/or an
1279 	 * extended 128-bit Host Identifier". So simplify this support and do
1280 	 * not support 64-bits host IDs to avoid needing to check that all
1281 	 * controllers associated with the same subsystem all use the same host
1282 	 * ID size.
1283 	 */
1284 	if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) {
1285 		req->error_loc = offsetof(struct nvme_common_command, cdw11);
1286 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1287 	}
1288 
1289 	return nvmet_copy_from_sgl(req, 0, &req->sq->ctrl->hostid,
1290 				   sizeof(req->sq->ctrl->hostid));
1291 }
1292 
1293 static u16 nvmet_set_feat_irq_coalesce(struct nvmet_req *req)
1294 {
1295 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1296 	u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1297 	struct nvmet_feat_irq_coalesce irqc = {
1298 		.time = (cdw11 >> 8) & 0xff,
1299 		.thr = cdw11 & 0xff,
1300 	};
1301 
1302 	/*
1303 	 * This feature is not supported for fabrics controllers and mandatory
1304 	 * for PCI controllers.
1305 	 */
1306 	if (!nvmet_is_pci_ctrl(ctrl)) {
1307 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
1308 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1309 	}
1310 
1311 	return ctrl->ops->set_feature(ctrl, NVME_FEAT_IRQ_COALESCE, &irqc);
1312 }
1313 
1314 static u16 nvmet_set_feat_irq_config(struct nvmet_req *req)
1315 {
1316 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1317 	u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1318 	struct nvmet_feat_irq_config irqcfg = {
1319 		.iv = cdw11 & 0xffff,
1320 		.cd = (cdw11 >> 16) & 0x1,
1321 	};
1322 
1323 	/*
1324 	 * This feature is not supported for fabrics controllers and mandatory
1325 	 * for PCI controllers.
1326 	 */
1327 	if (!nvmet_is_pci_ctrl(ctrl)) {
1328 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
1329 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1330 	}
1331 
1332 	return ctrl->ops->set_feature(ctrl, NVME_FEAT_IRQ_CONFIG, &irqcfg);
1333 }
1334 
1335 static u16 nvmet_set_feat_arbitration(struct nvmet_req *req)
1336 {
1337 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1338 	u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1339 	struct nvmet_feat_arbitration arb = {
1340 		.hpw = (cdw11 >> 24) & 0xff,
1341 		.mpw = (cdw11 >> 16) & 0xff,
1342 		.lpw = (cdw11 >> 8) & 0xff,
1343 		.ab = cdw11 & 0x7,
1344 	};
1345 
1346 	if (!ctrl->ops->set_feature) {
1347 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
1348 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1349 	}
1350 
1351 	return ctrl->ops->set_feature(ctrl, NVME_FEAT_ARBITRATION, &arb);
1352 }
1353 
1354 void nvmet_execute_set_features(struct nvmet_req *req)
1355 {
1356 	struct nvmet_ctrl *ctrl = nvmet_req_ctrl(req);
1357 	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
1358 	u32 cdw11 = le32_to_cpu(req->cmd->common.cdw11);
1359 	u16 status = 0;
1360 	u16 nsqr;
1361 	u16 ncqr;
1362 
1363 	if (!nvmet_check_data_len_lte(req, 0))
1364 		return;
1365 
1366 	switch (cdw10 & 0xff) {
1367 	case NVME_FEAT_ARBITRATION:
1368 		status = nvmet_set_feat_arbitration(req);
1369 		break;
1370 	case NVME_FEAT_NUM_QUEUES:
1371 		ncqr = (cdw11 >> 16) & 0xffff;
1372 		nsqr = cdw11 & 0xffff;
1373 		if (ncqr == 0xffff || nsqr == 0xffff) {
1374 			status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1375 			break;
1376 		}
1377 		nvmet_set_result(req,
1378 			(ctrl->max_qid - 1) | ((ctrl->max_qid - 1) << 16));
1379 		break;
1380 	case NVME_FEAT_IRQ_COALESCE:
1381 		status = nvmet_set_feat_irq_coalesce(req);
1382 		break;
1383 	case NVME_FEAT_IRQ_CONFIG:
1384 		status = nvmet_set_feat_irq_config(req);
1385 		break;
1386 	case NVME_FEAT_KATO:
1387 		status = nvmet_set_feat_kato(req);
1388 		break;
1389 	case NVME_FEAT_ASYNC_EVENT:
1390 		status = nvmet_set_feat_async_event(req, NVMET_AEN_CFG_ALL);
1391 		break;
1392 	case NVME_FEAT_HOST_ID:
1393 		status = nvmet_set_feat_host_id(req);
1394 		break;
1395 	case NVME_FEAT_WRITE_PROTECT:
1396 		status = nvmet_set_feat_write_protect(req);
1397 		break;
1398 	case NVME_FEAT_RESV_MASK:
1399 		status = nvmet_set_feat_resv_notif_mask(req, cdw11);
1400 		break;
1401 	default:
1402 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
1403 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1404 		break;
1405 	}
1406 
1407 	nvmet_req_complete(req, status);
1408 }
1409 
1410 static u16 nvmet_get_feat_write_protect(struct nvmet_req *req)
1411 {
1412 	struct nvmet_subsys *subsys = nvmet_req_subsys(req);
1413 	u32 result;
1414 
1415 	result = nvmet_req_find_ns(req);
1416 	if (result)
1417 		return result;
1418 
1419 	mutex_lock(&subsys->lock);
1420 	if (req->ns->readonly == true)
1421 		result = NVME_NS_WRITE_PROTECT;
1422 	else
1423 		result = NVME_NS_NO_WRITE_PROTECT;
1424 	nvmet_set_result(req, result);
1425 	mutex_unlock(&subsys->lock);
1426 
1427 	return 0;
1428 }
1429 
1430 static u16 nvmet_get_feat_irq_coalesce(struct nvmet_req *req)
1431 {
1432 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1433 	struct nvmet_feat_irq_coalesce irqc = { };
1434 	u16 status;
1435 
1436 	/*
1437 	 * This feature is not supported for fabrics controllers and mandatory
1438 	 * for PCI controllers.
1439 	 */
1440 	if (!nvmet_is_pci_ctrl(ctrl)) {
1441 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
1442 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1443 	}
1444 
1445 	status = ctrl->ops->get_feature(ctrl, NVME_FEAT_IRQ_COALESCE, &irqc);
1446 	if (status != NVME_SC_SUCCESS)
1447 		return status;
1448 
1449 	nvmet_set_result(req, ((u32)irqc.time << 8) | (u32)irqc.thr);
1450 
1451 	return NVME_SC_SUCCESS;
1452 }
1453 
1454 static u16 nvmet_get_feat_irq_config(struct nvmet_req *req)
1455 {
1456 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1457 	u32 iv = le32_to_cpu(req->cmd->common.cdw11) & 0xffff;
1458 	struct nvmet_feat_irq_config irqcfg = { .iv = iv };
1459 	u16 status;
1460 
1461 	/*
1462 	 * This feature is not supported for fabrics controllers and mandatory
1463 	 * for PCI controllers.
1464 	 */
1465 	if (!nvmet_is_pci_ctrl(ctrl)) {
1466 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
1467 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1468 	}
1469 
1470 	status = ctrl->ops->get_feature(ctrl, NVME_FEAT_IRQ_CONFIG, &irqcfg);
1471 	if (status != NVME_SC_SUCCESS)
1472 		return status;
1473 
1474 	nvmet_set_result(req, ((u32)irqcfg.cd << 16) | iv);
1475 
1476 	return NVME_SC_SUCCESS;
1477 }
1478 
1479 static u16 nvmet_get_feat_arbitration(struct nvmet_req *req)
1480 {
1481 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1482 	struct nvmet_feat_arbitration arb = { };
1483 	u16 status;
1484 
1485 	if (!ctrl->ops->get_feature) {
1486 		req->error_loc = offsetof(struct nvme_common_command, cdw10);
1487 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1488 	}
1489 
1490 	status = ctrl->ops->get_feature(ctrl, NVME_FEAT_ARBITRATION, &arb);
1491 	if (status != NVME_SC_SUCCESS)
1492 		return status;
1493 
1494 	nvmet_set_result(req,
1495 			 ((u32)arb.hpw << 24) |
1496 			 ((u32)arb.mpw << 16) |
1497 			 ((u32)arb.lpw << 8) |
1498 			 (arb.ab & 0x7));
1499 
1500 	return NVME_SC_SUCCESS;
1501 }
1502 
1503 void nvmet_get_feat_kato(struct nvmet_req *req)
1504 {
1505 	nvmet_set_result(req, req->sq->ctrl->kato * 1000);
1506 }
1507 
1508 void nvmet_get_feat_async_event(struct nvmet_req *req)
1509 {
1510 	nvmet_set_result(req, READ_ONCE(req->sq->ctrl->aen_enabled));
1511 }
1512 
1513 void nvmet_execute_get_features(struct nvmet_req *req)
1514 {
1515 	struct nvmet_ctrl *ctrl = nvmet_req_ctrl(req);
1516 	u32 cdw10 = le32_to_cpu(req->cmd->common.cdw10);
1517 	u16 status = 0;
1518 
1519 	if (!nvmet_check_transfer_len(req, nvmet_feat_data_len(req, cdw10)))
1520 		return;
1521 
1522 	switch (cdw10 & 0xff) {
1523 	/*
1524 	 * These features are mandatory in the spec, but we don't
1525 	 * have a useful way to implement them.  We'll eventually
1526 	 * need to come up with some fake values for these.
1527 	 */
1528 #if 0
1529 	case NVME_FEAT_POWER_MGMT:
1530 		break;
1531 	case NVME_FEAT_TEMP_THRESH:
1532 		break;
1533 	case NVME_FEAT_ERR_RECOVERY:
1534 		break;
1535 	case NVME_FEAT_WRITE_ATOMIC:
1536 		break;
1537 #endif
1538 	case NVME_FEAT_ARBITRATION:
1539 		status = nvmet_get_feat_arbitration(req);
1540 		break;
1541 	case NVME_FEAT_IRQ_COALESCE:
1542 		status = nvmet_get_feat_irq_coalesce(req);
1543 		break;
1544 	case NVME_FEAT_IRQ_CONFIG:
1545 		status = nvmet_get_feat_irq_config(req);
1546 		break;
1547 	case NVME_FEAT_ASYNC_EVENT:
1548 		nvmet_get_feat_async_event(req);
1549 		break;
1550 	case NVME_FEAT_VOLATILE_WC:
1551 		nvmet_set_result(req, 1);
1552 		break;
1553 	case NVME_FEAT_NUM_QUEUES:
1554 		nvmet_set_result(req,
1555 			(ctrl->max_qid-1) | ((ctrl->max_qid-1) << 16));
1556 		break;
1557 	case NVME_FEAT_KATO:
1558 		nvmet_get_feat_kato(req);
1559 		break;
1560 	case NVME_FEAT_HOST_ID:
1561 		/* need 128-bit host identifier flag */
1562 		if (!(req->cmd->common.cdw11 & cpu_to_le32(1 << 0))) {
1563 			req->error_loc =
1564 				offsetof(struct nvme_common_command, cdw11);
1565 			status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1566 			break;
1567 		}
1568 
1569 		status = nvmet_copy_to_sgl(req, 0, &req->sq->ctrl->hostid,
1570 				sizeof(req->sq->ctrl->hostid));
1571 		break;
1572 	case NVME_FEAT_WRITE_PROTECT:
1573 		status = nvmet_get_feat_write_protect(req);
1574 		break;
1575 	case NVME_FEAT_RESV_MASK:
1576 		status = nvmet_get_feat_resv_notif_mask(req);
1577 		break;
1578 	default:
1579 		req->error_loc =
1580 			offsetof(struct nvme_common_command, cdw10);
1581 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1582 		break;
1583 	}
1584 
1585 	nvmet_req_complete(req, status);
1586 }
1587 
1588 void nvmet_execute_async_event(struct nvmet_req *req)
1589 {
1590 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1591 
1592 	if (!nvmet_check_transfer_len(req, 0))
1593 		return;
1594 
1595 	mutex_lock(&ctrl->lock);
1596 	if (ctrl->nr_async_event_cmds >= NVMET_ASYNC_EVENTS) {
1597 		mutex_unlock(&ctrl->lock);
1598 		nvmet_req_complete(req, NVME_SC_ASYNC_LIMIT | NVME_STATUS_DNR);
1599 		return;
1600 	}
1601 	ctrl->async_event_cmds[ctrl->nr_async_event_cmds++] = req;
1602 	mutex_unlock(&ctrl->lock);
1603 
1604 	queue_work(nvmet_aen_wq, &ctrl->async_event_work);
1605 }
1606 
1607 void nvmet_execute_keep_alive(struct nvmet_req *req)
1608 {
1609 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
1610 	u16 status = 0;
1611 
1612 	if (!nvmet_check_transfer_len(req, 0))
1613 		return;
1614 
1615 	if (!ctrl->kato) {
1616 		status = NVME_SC_KA_TIMEOUT_INVALID;
1617 		goto out;
1618 	}
1619 
1620 	pr_debug("ctrl %d update keep-alive timer for %d secs\n",
1621 		ctrl->cntlid, ctrl->kato);
1622 	mod_delayed_work(system_percpu_wq, &ctrl->ka_work, ctrl->kato * HZ);
1623 out:
1624 	nvmet_req_complete(req, status);
1625 }
1626 
1627 u32 nvmet_admin_cmd_data_len(struct nvmet_req *req)
1628 {
1629 	struct nvme_command *cmd = req->cmd;
1630 
1631 	if (nvme_is_fabrics(cmd))
1632 		return nvmet_fabrics_admin_cmd_data_len(req);
1633 	if (nvmet_is_disc_subsys(nvmet_req_subsys(req)))
1634 		return nvmet_discovery_cmd_data_len(req);
1635 
1636 	switch (cmd->common.opcode) {
1637 	case nvme_admin_get_log_page:
1638 		return nvmet_get_log_page_len(cmd);
1639 	case nvme_admin_identify:
1640 		return NVME_IDENTIFY_DATA_SIZE;
1641 	case nvme_admin_get_features:
1642 		return nvmet_feat_data_len(req, le32_to_cpu(cmd->common.cdw10));
1643 	default:
1644 		return 0;
1645 	}
1646 }
1647 
1648 u16 nvmet_parse_admin_cmd(struct nvmet_req *req)
1649 {
1650 	struct nvme_command *cmd = req->cmd;
1651 	u16 ret;
1652 
1653 	if (nvme_is_fabrics(cmd))
1654 		return nvmet_parse_fabrics_admin_cmd(req);
1655 	if (nvmet_is_disc_subsys(nvmet_req_subsys(req)))
1656 		return nvmet_parse_discovery_cmd(req);
1657 
1658 	ret = nvmet_check_ctrl_status(req);
1659 	if (unlikely(ret))
1660 		return ret;
1661 
1662 	/* For PCI controllers, admin commands shall not use SGL. */
1663 	if (nvmet_is_pci_ctrl(req->sq->ctrl) && !req->sq->qid &&
1664 	    cmd->common.flags & NVME_CMD_SGL_ALL)
1665 		return NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
1666 
1667 	if (nvmet_is_passthru_req(req))
1668 		return nvmet_parse_passthru_admin_cmd(req);
1669 
1670 	switch (cmd->common.opcode) {
1671 	case nvme_admin_delete_sq:
1672 		req->execute = nvmet_execute_delete_sq;
1673 		return 0;
1674 	case nvme_admin_create_sq:
1675 		req->execute = nvmet_execute_create_sq;
1676 		return 0;
1677 	case nvme_admin_get_log_page:
1678 		req->execute = nvmet_execute_get_log_page;
1679 		return 0;
1680 	case nvme_admin_delete_cq:
1681 		req->execute = nvmet_execute_delete_cq;
1682 		return 0;
1683 	case nvme_admin_create_cq:
1684 		req->execute = nvmet_execute_create_cq;
1685 		return 0;
1686 	case nvme_admin_identify:
1687 		req->execute = nvmet_execute_identify;
1688 		return 0;
1689 	case nvme_admin_abort_cmd:
1690 		req->execute = nvmet_execute_abort;
1691 		return 0;
1692 	case nvme_admin_set_features:
1693 		req->execute = nvmet_execute_set_features;
1694 		return 0;
1695 	case nvme_admin_get_features:
1696 		req->execute = nvmet_execute_get_features;
1697 		return 0;
1698 	case nvme_admin_async_event:
1699 		req->execute = nvmet_execute_async_event;
1700 		return 0;
1701 	case nvme_admin_keep_alive:
1702 		req->execute = nvmet_execute_keep_alive;
1703 		return 0;
1704 	default:
1705 		return nvmet_report_invalid_opcode(req);
1706 	}
1707 }
1708