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