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