xref: /linux/drivers/nvme/host/sysfs.c (revision b23545529c21737dd7939e86b6f649f06480796d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Sysfs interface for the NVMe core driver.
4  *
5  * Copyright (c) 2011-2014, Intel Corporation.
6  */
7 
8 #include <linux/nvme-auth.h>
9 #include <linux/blkdev.h>
10 
11 #include "nvme.h"
12 #include "fabrics.h"
13 
14 static ssize_t nvme_sysfs_reset(struct device *dev,
15 				struct device_attribute *attr, const char *buf,
16 				size_t count)
17 {
18 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
19 	int ret;
20 
21 	ret = nvme_reset_ctrl_sync(ctrl);
22 	if (ret < 0)
23 		return ret;
24 	return count;
25 }
26 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
27 
28 static ssize_t nvme_sysfs_rescan(struct device *dev,
29 				struct device_attribute *attr, const char *buf,
30 				size_t count)
31 {
32 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
33 
34 	nvme_queue_scan(ctrl);
35 	return count;
36 }
37 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
38 
39 static ssize_t nvme_adm_passthru_err_log_enabled_show(struct device *dev,
40 		struct device_attribute *attr, char *buf)
41 {
42 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
43 
44 	return sysfs_emit(buf,
45 			  ctrl->passthru_err_log_enabled ? "on\n" : "off\n");
46 }
47 
48 static ssize_t nvme_adm_passthru_err_log_enabled_store(struct device *dev,
49 		struct device_attribute *attr, const char *buf, size_t count)
50 {
51 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
52 	bool passthru_err_log_enabled;
53 	int err;
54 
55 	err = kstrtobool(buf, &passthru_err_log_enabled);
56 	if (err)
57 		return -EINVAL;
58 
59 	ctrl->passthru_err_log_enabled = passthru_err_log_enabled;
60 
61 	return count;
62 }
63 
64 static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
65 {
66 	struct gendisk *disk = dev_to_disk(dev);
67 
68 	if (nvme_disk_is_ns_head(disk))
69 		return disk->private_data;
70 	return nvme_get_ns_from_dev(dev)->head;
71 }
72 
73 static ssize_t nvme_io_passthru_err_log_enabled_show(struct device *dev,
74 		struct device_attribute *attr, char *buf)
75 {
76 	struct nvme_ns_head *head = dev_to_ns_head(dev);
77 
78 	return sysfs_emit(buf, head->passthru_err_log_enabled ? "on\n" : "off\n");
79 }
80 
81 static ssize_t nvme_io_passthru_err_log_enabled_store(struct device *dev,
82 		struct device_attribute *attr, const char *buf, size_t count)
83 {
84 	struct nvme_ns_head *head = dev_to_ns_head(dev);
85 	bool passthru_err_log_enabled;
86 	int err;
87 
88 	err = kstrtobool(buf, &passthru_err_log_enabled);
89 	if (err)
90 		return -EINVAL;
91 	head->passthru_err_log_enabled = passthru_err_log_enabled;
92 
93 	return count;
94 }
95 
96 static struct device_attribute dev_attr_adm_passthru_err_log_enabled = \
97 	__ATTR(passthru_err_log_enabled, S_IRUGO | S_IWUSR, \
98 	nvme_adm_passthru_err_log_enabled_show, nvme_adm_passthru_err_log_enabled_store);
99 
100 static struct device_attribute dev_attr_io_passthru_err_log_enabled = \
101 	__ATTR(passthru_err_log_enabled, S_IRUGO | S_IWUSR, \
102 	nvme_io_passthru_err_log_enabled_show, nvme_io_passthru_err_log_enabled_store);
103 
104 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
105 		char *buf)
106 {
107 	struct nvme_ns_head *head = dev_to_ns_head(dev);
108 	struct nvme_ns_ids *ids = &head->ids;
109 	struct nvme_subsystem *subsys = head->subsys;
110 	int serial_len = sizeof(subsys->serial);
111 	int model_len = sizeof(subsys->model);
112 
113 	if (!uuid_is_null(&ids->uuid))
114 		return sysfs_emit(buf, "uuid.%pU\n", &ids->uuid);
115 
116 	if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
117 		return sysfs_emit(buf, "eui.%16phN\n", ids->nguid);
118 
119 	if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
120 		return sysfs_emit(buf, "eui.%8phN\n", ids->eui64);
121 
122 	while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
123 				  subsys->serial[serial_len - 1] == '\0'))
124 		serial_len--;
125 	while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
126 				 subsys->model[model_len - 1] == '\0'))
127 		model_len--;
128 
129 	return sysfs_emit(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
130 		serial_len, subsys->serial, model_len, subsys->model,
131 		head->ns_id);
132 }
133 static DEVICE_ATTR_RO(wwid);
134 
135 static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
136 		char *buf)
137 {
138 	return sysfs_emit(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
139 }
140 static DEVICE_ATTR_RO(nguid);
141 
142 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
143 		char *buf)
144 {
145 	struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
146 
147 	/* For backward compatibility expose the NGUID to userspace if
148 	 * we have no UUID set
149 	 */
150 	if (uuid_is_null(&ids->uuid)) {
151 		dev_warn_once(dev,
152 			"No UUID available providing old NGUID\n");
153 		return sysfs_emit(buf, "%pU\n", ids->nguid);
154 	}
155 	return sysfs_emit(buf, "%pU\n", &ids->uuid);
156 }
157 static DEVICE_ATTR_RO(uuid);
158 
159 static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
160 		char *buf)
161 {
162 	return sysfs_emit(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
163 }
164 static DEVICE_ATTR_RO(eui);
165 
166 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
167 		char *buf)
168 {
169 	return sysfs_emit(buf, "%u\n", dev_to_ns_head(dev)->ns_id);
170 }
171 static DEVICE_ATTR_RO(nsid);
172 
173 static ssize_t csi_show(struct device *dev, struct device_attribute *attr,
174 		char *buf)
175 {
176 	return sysfs_emit(buf, "%u\n", dev_to_ns_head(dev)->ids.csi);
177 }
178 static DEVICE_ATTR_RO(csi);
179 
180 static ssize_t metadata_bytes_show(struct device *dev,
181 		struct device_attribute *attr, char *buf)
182 {
183 	return sysfs_emit(buf, "%u\n", dev_to_ns_head(dev)->ms);
184 }
185 static DEVICE_ATTR_RO(metadata_bytes);
186 
187 static int ns_head_update_nuse(struct nvme_ns_head *head)
188 {
189 	struct nvme_id_ns *id;
190 	struct nvme_ns *ns;
191 	int srcu_idx, ret = -EWOULDBLOCK;
192 
193 	/* Avoid issuing commands too often by rate limiting the update */
194 	if (!__ratelimit(&head->rs_nuse))
195 		return 0;
196 
197 	srcu_idx = srcu_read_lock(&head->srcu);
198 	ns = nvme_find_path(head);
199 	if (!ns)
200 		goto out_unlock;
201 
202 	ret = nvme_identify_ns(ns->ctrl, head->ns_id, &id);
203 	if (ret)
204 		goto out_unlock;
205 
206 	head->nuse = le64_to_cpu(id->nuse);
207 	kfree(id);
208 
209 out_unlock:
210 	srcu_read_unlock(&head->srcu, srcu_idx);
211 	return ret;
212 }
213 
214 static int ns_update_nuse(struct nvme_ns *ns)
215 {
216 	struct nvme_id_ns *id;
217 	int ret;
218 
219 	/* Avoid issuing commands too often by rate limiting the update. */
220 	if (!__ratelimit(&ns->head->rs_nuse))
221 		return 0;
222 
223 	ret = nvme_identify_ns(ns->ctrl, ns->head->ns_id, &id);
224 	if (ret)
225 		return ret;
226 
227 	ns->head->nuse = le64_to_cpu(id->nuse);
228 	kfree(id);
229 	return 0;
230 }
231 
232 static ssize_t nuse_show(struct device *dev, struct device_attribute *attr,
233 		char *buf)
234 {
235 	struct nvme_ns_head *head = dev_to_ns_head(dev);
236 	struct gendisk *disk = dev_to_disk(dev);
237 	int ret;
238 
239 	if (nvme_disk_is_ns_head(disk))
240 		ret = ns_head_update_nuse(head);
241 	else
242 		ret = ns_update_nuse(disk->private_data);
243 	if (ret < 0)
244 		return ret;
245 	else if (ret > 0)
246 		return -EIO;
247 
248 	return sysfs_emit(buf, "%llu\n", head->nuse);
249 }
250 static DEVICE_ATTR_RO(nuse);
251 
252 static struct attribute *nvme_ns_attrs[] = {
253 	&dev_attr_wwid.attr,
254 	&dev_attr_uuid.attr,
255 	&dev_attr_nguid.attr,
256 	&dev_attr_eui.attr,
257 	&dev_attr_csi.attr,
258 	&dev_attr_nsid.attr,
259 	&dev_attr_metadata_bytes.attr,
260 	&dev_attr_nuse.attr,
261 #ifdef CONFIG_NVME_MULTIPATH
262 	&dev_attr_ana_grpid.attr,
263 	&dev_attr_ana_state.attr,
264 	&dev_attr_queue_depth.attr,
265 	&dev_attr_numa_nodes.attr,
266 	&dev_attr_path_state.attr,
267 	&dev_attr_delayed_removal_secs.attr,
268 	&dev_attr_fail_on_ctrl_loss.attr,
269 #endif
270 	&dev_attr_io_passthru_err_log_enabled.attr,
271 	NULL,
272 };
273 
274 static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
275 		struct attribute *a, int n)
276 {
277 	struct device *dev = container_of(kobj, struct device, kobj);
278 	struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
279 
280 	if (a == &dev_attr_uuid.attr) {
281 		if (uuid_is_null(&ids->uuid) &&
282 		    !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
283 			return 0;
284 	}
285 	if (a == &dev_attr_nguid.attr) {
286 		if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
287 			return 0;
288 	}
289 	if (a == &dev_attr_eui.attr) {
290 		if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
291 			return 0;
292 	}
293 #ifdef CONFIG_NVME_MULTIPATH
294 	if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) {
295 		/* per-path attr */
296 		if (nvme_disk_is_ns_head(dev_to_disk(dev)))
297 			return 0;
298 		if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
299 			return 0;
300 	}
301 	if (a == &dev_attr_queue_depth.attr || a == &dev_attr_numa_nodes.attr ||
302 	    a == &dev_attr_path_state.attr) {
303 		if (nvme_disk_is_ns_head(dev_to_disk(dev)))
304 			return 0;
305 	}
306 	if (a == &dev_attr_delayed_removal_secs.attr ||
307 	    a == &dev_attr_fail_on_ctrl_loss.attr) {
308 		struct gendisk *disk = dev_to_disk(dev);
309 
310 		if (!nvme_disk_is_ns_head(disk))
311 			return 0;
312 	}
313 #endif
314 	return a->mode;
315 }
316 
317 static const struct attribute_group nvme_ns_attr_group = {
318 	.attrs		= nvme_ns_attrs,
319 	.is_visible	= nvme_ns_attrs_are_visible,
320 };
321 
322 #ifdef CONFIG_NVME_MULTIPATH
323 /*
324  * NOTE: The dummy attribute does not appear in sysfs. It exists solely to allow
325  * control over the visibility of the multipath sysfs node. Without at least one
326  * attribute defined in nvme_ns_mpath_attrs[], the sysfs implementation does not
327  * invoke the multipath_sysfs_group_visible() method. As a result, we would not
328  * be able to control the visibility of the multipath sysfs node.
329  */
330 static struct attribute dummy_attr = {
331 	.name = "dummy",
332 };
333 
334 static struct attribute *nvme_ns_mpath_attrs[] = {
335 	&dummy_attr,
336 	NULL,
337 };
338 
339 static bool multipath_sysfs_group_visible(struct kobject *kobj)
340 {
341 	struct device *dev = container_of(kobj, struct device, kobj);
342 
343 	return nvme_disk_is_ns_head(dev_to_disk(dev));
344 }
345 DEFINE_SIMPLE_SYSFS_GROUP_VISIBLE(multipath_sysfs)
346 
347 const struct attribute_group nvme_ns_mpath_attr_group = {
348 	.name           = "multipath",
349 	.attrs		= nvme_ns_mpath_attrs,
350 	.is_visible     = SYSFS_GROUP_VISIBLE(multipath_sysfs),
351 };
352 #endif
353 
354 static ssize_t command_retries_count_show(struct device *dev,
355 		struct device_attribute *attr, char *buf)
356 {
357 	struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
358 
359 	return sysfs_emit(buf, "%lu\n", atomic_long_read(&ns->retries));
360 }
361 
362 static ssize_t command_retries_count_store(struct device *dev,
363 		struct device_attribute *attr, const char *buf, size_t count)
364 {
365 	unsigned long retries;
366 	int err;
367 	struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
368 
369 	err = kstrtoul(buf, 0, &retries);
370 	if (err)
371 		return -EINVAL;
372 
373 	atomic_long_set(&ns->retries, retries);
374 
375 	return count;
376 }
377 static DEVICE_ATTR_RW(command_retries_count);
378 
379 static ssize_t nvme_io_errors_show(struct device *dev,
380 		struct device_attribute *attr, char *buf)
381 {
382 	struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
383 
384 	return sysfs_emit(buf, "%lu\n", atomic_long_read(&ns->errors));
385 }
386 
387 static ssize_t nvme_io_errors_store(struct device *dev,
388 		struct device_attribute *attr, const char *buf, size_t count)
389 {
390 	unsigned long errors;
391 	int err;
392 	struct nvme_ns *ns = nvme_get_ns_from_dev(dev);
393 
394 	err = kstrtoul(buf, 0, &errors);
395 	if (err)
396 		return -EINVAL;
397 
398 	atomic_long_set(&ns->errors, errors);
399 
400 	return count;
401 }
402 
403 static struct device_attribute dev_attr_io_errors =
404 	__ATTR(command_error_count, 0644,
405 		nvme_io_errors_show, nvme_io_errors_store);
406 
407 static struct attribute *nvme_ns_diag_attrs[] = {
408 	&dev_attr_command_retries_count.attr,
409 	&dev_attr_io_errors.attr,
410 #ifdef CONFIG_NVME_MULTIPATH
411 	&dev_attr_multipath_failover_count.attr,
412 	&dev_attr_io_requeue_no_usable_path_count.attr,
413 	&dev_attr_io_fail_no_available_path_count.attr,
414 #endif
415 	NULL,
416 };
417 
418 static umode_t nvme_ns_diag_attrs_are_visible(struct kobject *kobj,
419 		struct attribute *a, int n)
420 {
421 	struct device *dev = container_of(kobj, struct device, kobj);
422 
423 	if (a == &dev_attr_command_retries_count.attr) {
424 		if (nvme_disk_is_ns_head(dev_to_disk(dev)))
425 			return 0;
426 	}
427 	if (a == &dev_attr_io_errors.attr) {
428 		struct gendisk *disk = dev_to_disk(dev);
429 
430 		if (nvme_disk_is_ns_head(disk))
431 			return 0;
432 	}
433 #ifdef CONFIG_NVME_MULTIPATH
434 	if (a == &dev_attr_multipath_failover_count.attr) {
435 		if (nvme_disk_is_ns_head(dev_to_disk(dev)))
436 			return 0;
437 	}
438 	if (a == &dev_attr_io_requeue_no_usable_path_count.attr) {
439 		if (!nvme_disk_is_ns_head(dev_to_disk(dev)))
440 			return 0;
441 	}
442 	if (a == &dev_attr_io_fail_no_available_path_count.attr) {
443 		if (!nvme_disk_is_ns_head(dev_to_disk(dev)))
444 			return 0;
445 	}
446 #endif
447 	return a->mode;
448 }
449 
450 static const struct attribute_group nvme_ns_diag_attr_group = {
451 	.name		= "diag",
452 	.attrs		= nvme_ns_diag_attrs,
453 	.is_visible	= nvme_ns_diag_attrs_are_visible,
454 };
455 
456 const struct attribute_group *nvme_ns_attr_groups[] = {
457 	&nvme_ns_attr_group,
458 #ifdef CONFIG_NVME_MULTIPATH
459 	&nvme_ns_mpath_attr_group,
460 #endif
461 	&nvme_ns_diag_attr_group,
462 	NULL,
463 };
464 
465 #define nvme_show_str_function(field)						\
466 static ssize_t  field##_show(struct device *dev,				\
467 			    struct device_attribute *attr, char *buf)		\
468 {										\
469         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);				\
470         return sysfs_emit(buf, "%.*s\n",					\
471 		(int)sizeof(ctrl->subsys->field), ctrl->subsys->field);		\
472 }										\
473 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
474 
475 nvme_show_str_function(model);
476 nvme_show_str_function(serial);
477 nvme_show_str_function(firmware_rev);
478 
479 #define nvme_show_int_function(field)						\
480 static ssize_t  field##_show(struct device *dev,				\
481 			    struct device_attribute *attr, char *buf)		\
482 {										\
483         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);				\
484         return sysfs_emit(buf, "%d\n", ctrl->field);				\
485 }										\
486 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
487 
488 nvme_show_int_function(cntlid);
489 nvme_show_int_function(numa_node);
490 nvme_show_int_function(queue_count);
491 nvme_show_int_function(sqsize);
492 nvme_show_int_function(kato);
493 
494 static ssize_t nvme_sysfs_delete(struct device *dev,
495 				struct device_attribute *attr, const char *buf,
496 				size_t count)
497 {
498 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
499 
500 	if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
501 		return -EBUSY;
502 
503 	if (device_remove_file_self(dev, attr))
504 		nvme_delete_ctrl_sync(ctrl);
505 	return count;
506 }
507 static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
508 
509 static ssize_t nvme_sysfs_show_transport(struct device *dev,
510 					 struct device_attribute *attr,
511 					 char *buf)
512 {
513 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
514 
515 	return sysfs_emit(buf, "%s\n", ctrl->ops->name);
516 }
517 static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
518 
519 static ssize_t nvme_sysfs_show_state(struct device *dev,
520 				     struct device_attribute *attr,
521 				     char *buf)
522 {
523 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
524 	unsigned state = (unsigned)nvme_ctrl_state(ctrl);
525 	static const char *const state_name[] = {
526 		[NVME_CTRL_NEW]		= "new",
527 		[NVME_CTRL_LIVE]	= "live",
528 		[NVME_CTRL_RESETTING]	= "resetting",
529 		[NVME_CTRL_CONNECTING]	= "connecting",
530 		[NVME_CTRL_DELETING]	= "deleting",
531 		[NVME_CTRL_DELETING_NOIO]= "deleting (no IO)",
532 		[NVME_CTRL_DEAD]	= "dead",
533 	};
534 
535 	if (state < ARRAY_SIZE(state_name) && state_name[state])
536 		return sysfs_emit(buf, "%s\n", state_name[state]);
537 
538 	return sysfs_emit(buf, "unknown state\n");
539 }
540 
541 static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
542 
543 static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
544 					 struct device_attribute *attr,
545 					 char *buf)
546 {
547 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
548 
549 	return sysfs_emit(buf, "%s\n", ctrl->subsys->subnqn);
550 }
551 static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
552 
553 static ssize_t nvme_sysfs_show_hostnqn(struct device *dev,
554 					struct device_attribute *attr,
555 					char *buf)
556 {
557 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
558 
559 	return sysfs_emit(buf, "%s\n", ctrl->opts->host->nqn);
560 }
561 static DEVICE_ATTR(hostnqn, S_IRUGO, nvme_sysfs_show_hostnqn, NULL);
562 
563 static ssize_t nvme_sysfs_show_hostid(struct device *dev,
564 					struct device_attribute *attr,
565 					char *buf)
566 {
567 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
568 
569 	return sysfs_emit(buf, "%pU\n", &ctrl->opts->host->id);
570 }
571 static DEVICE_ATTR(hostid, S_IRUGO, nvme_sysfs_show_hostid, NULL);
572 
573 static ssize_t nvme_sysfs_show_address(struct device *dev,
574 					 struct device_attribute *attr,
575 					 char *buf)
576 {
577 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
578 
579 	return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
580 }
581 static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
582 
583 static ssize_t nvme_ctrl_loss_tmo_show(struct device *dev,
584 		struct device_attribute *attr, char *buf)
585 {
586 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
587 	struct nvmf_ctrl_options *opts = ctrl->opts;
588 
589 	if (ctrl->opts->max_reconnects == -1)
590 		return sysfs_emit(buf, "off\n");
591 	return sysfs_emit(buf, "%d\n",
592 			  opts->max_reconnects * opts->reconnect_delay);
593 }
594 
595 static ssize_t nvme_ctrl_loss_tmo_store(struct device *dev,
596 		struct device_attribute *attr, const char *buf, size_t count)
597 {
598 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
599 	struct nvmf_ctrl_options *opts = ctrl->opts;
600 	int ctrl_loss_tmo, err;
601 
602 	err = kstrtoint(buf, 10, &ctrl_loss_tmo);
603 	if (err)
604 		return -EINVAL;
605 
606 	if (ctrl_loss_tmo < 0)
607 		opts->max_reconnects = -1;
608 	else
609 		opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
610 						opts->reconnect_delay);
611 	return count;
612 }
613 static DEVICE_ATTR(ctrl_loss_tmo, S_IRUGO | S_IWUSR,
614 	nvme_ctrl_loss_tmo_show, nvme_ctrl_loss_tmo_store);
615 
616 static ssize_t nvme_ctrl_reconnect_delay_show(struct device *dev,
617 		struct device_attribute *attr, char *buf)
618 {
619 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
620 
621 	if (ctrl->opts->reconnect_delay == -1)
622 		return sysfs_emit(buf, "off\n");
623 	return sysfs_emit(buf, "%d\n", ctrl->opts->reconnect_delay);
624 }
625 
626 static ssize_t nvme_ctrl_reconnect_delay_store(struct device *dev,
627 		struct device_attribute *attr, const char *buf, size_t count)
628 {
629 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
630 	unsigned int v;
631 	int err;
632 
633 	err = kstrtou32(buf, 10, &v);
634 	if (err)
635 		return err;
636 
637 	ctrl->opts->reconnect_delay = v;
638 	return count;
639 }
640 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR,
641 	nvme_ctrl_reconnect_delay_show, nvme_ctrl_reconnect_delay_store);
642 
643 static ssize_t nvme_ctrl_fast_io_fail_tmo_show(struct device *dev,
644 		struct device_attribute *attr, char *buf)
645 {
646 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
647 
648 	if (ctrl->opts->fast_io_fail_tmo == -1)
649 		return sysfs_emit(buf, "off\n");
650 	return sysfs_emit(buf, "%d\n", ctrl->opts->fast_io_fail_tmo);
651 }
652 
653 static ssize_t nvme_ctrl_fast_io_fail_tmo_store(struct device *dev,
654 		struct device_attribute *attr, const char *buf, size_t count)
655 {
656 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
657 	struct nvmf_ctrl_options *opts = ctrl->opts;
658 	int fast_io_fail_tmo, err;
659 
660 	err = kstrtoint(buf, 10, &fast_io_fail_tmo);
661 	if (err)
662 		return -EINVAL;
663 
664 	if (fast_io_fail_tmo < 0)
665 		opts->fast_io_fail_tmo = -1;
666 	else
667 		opts->fast_io_fail_tmo = fast_io_fail_tmo;
668 	return count;
669 }
670 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
671 	nvme_ctrl_fast_io_fail_tmo_show, nvme_ctrl_fast_io_fail_tmo_store);
672 
673 static ssize_t cntrltype_show(struct device *dev,
674 			      struct device_attribute *attr, char *buf)
675 {
676 	static const char * const type[] = {
677 		[NVME_CTRL_IO] = "io\n",
678 		[NVME_CTRL_DISC] = "discovery\n",
679 		[NVME_CTRL_ADMIN] = "admin\n",
680 	};
681 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
682 
683 	if (ctrl->cntrltype > NVME_CTRL_ADMIN || !type[ctrl->cntrltype])
684 		return sysfs_emit(buf, "reserved\n");
685 
686 	return sysfs_emit(buf, "%s", type[ctrl->cntrltype]);
687 }
688 static DEVICE_ATTR_RO(cntrltype);
689 
690 static ssize_t dctype_show(struct device *dev,
691 			   struct device_attribute *attr, char *buf)
692 {
693 	static const char * const type[] = {
694 		[NVME_DCTYPE_NOT_REPORTED] = "none\n",
695 		[NVME_DCTYPE_DDC] = "ddc\n",
696 		[NVME_DCTYPE_CDC] = "cdc\n",
697 	};
698 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
699 
700 	if (ctrl->dctype > NVME_DCTYPE_CDC || !type[ctrl->dctype])
701 		return sysfs_emit(buf, "reserved\n");
702 
703 	return sysfs_emit(buf, "%s", type[ctrl->dctype]);
704 }
705 static DEVICE_ATTR_RO(dctype);
706 
707 static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
708                 char *buf)
709 {
710 	int count = 0, i;
711 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
712 	unsigned long quirks = ctrl->quirks;
713 
714 	if (!quirks)
715 		return sysfs_emit(buf, "none\n");
716 
717 	for (i = 0; quirks; ++i) {
718 		if (quirks & 1) {
719 			count += sysfs_emit_at(buf, count, "%s\n",
720 					nvme_quirk_name(BIT(i)));
721 		}
722 		quirks >>= 1;
723 	}
724 
725 	return count;
726 }
727 static DEVICE_ATTR_RO(quirks);
728 
729 static ssize_t nvme_admin_timeout_show(struct device *dev,
730 		struct device_attribute *attr, char *buf)
731 {
732 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
733 
734 	return sysfs_emit(buf, "%u\n",
735 				jiffies_to_msecs(ctrl->admin_timeout));
736 }
737 
738 static ssize_t nvme_admin_timeout_store(struct device *dev,
739 		struct device_attribute *attr,
740 		const char *buf, size_t count)
741 {
742 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
743 	u32 timeout;
744 	int err;
745 
746 	/*
747 	 * Wait until the controller reaches the LIVE state to be sure that
748 	 * admin_q and fabrics_q are properly initialized.
749 	 */
750 	if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
751 		return -EBUSY;
752 
753 	err = kstrtou32(buf, 10, &timeout);
754 	if (err || !timeout)
755 		return -EINVAL;
756 
757 	ctrl->admin_timeout = msecs_to_jiffies(timeout);
758 
759 	blk_queue_rq_timeout(ctrl->admin_q, ctrl->admin_timeout);
760 	if (ctrl->fabrics_q)
761 		blk_queue_rq_timeout(ctrl->fabrics_q, ctrl->admin_timeout);
762 
763 	return count;
764 }
765 
766 static DEVICE_ATTR(admin_timeout, S_IRUGO | S_IWUSR,
767 	nvme_admin_timeout_show, nvme_admin_timeout_store);
768 
769 static ssize_t nvme_io_timeout_show(struct device *dev,
770 		struct device_attribute *attr, char *buf)
771 {
772 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
773 
774 	return sysfs_emit(buf, "%u\n", jiffies_to_msecs(ctrl->io_timeout));
775 }
776 
777 static ssize_t nvme_io_timeout_store(struct device *dev,
778 		struct device_attribute *attr,
779 		const char *buf, size_t count)
780 {
781 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
782 	struct nvme_ns *ns;
783 	u32 timeout;
784 	int err;
785 
786 	/*
787 	 * Wait until the controller reaches the LIVE state to be sure that
788 	 * connect_q is properly initialized.
789 	 */
790 	if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
791 		return -EBUSY;
792 
793 	err = kstrtou32(buf, 10, &timeout);
794 	if (err || !timeout)
795 		return -EINVAL;
796 
797 	/* Take the namespaces_lock to avoid racing against nvme_alloc_ns() */
798 	mutex_lock(&ctrl->namespaces_lock);
799 
800 	ctrl->io_timeout = msecs_to_jiffies(timeout);
801 	list_for_each_entry(ns, &ctrl->namespaces, list)
802 		blk_queue_rq_timeout(ns->queue, ctrl->io_timeout);
803 
804 	mutex_unlock(&ctrl->namespaces_lock);
805 
806 	if (ctrl->connect_q)
807 		blk_queue_rq_timeout(ctrl->connect_q, ctrl->io_timeout);
808 
809 	return count;
810 }
811 
812 static DEVICE_ATTR(io_timeout, S_IRUGO | S_IWUSR,
813 	nvme_io_timeout_show, nvme_io_timeout_store);
814 
815 #ifdef CONFIG_NVME_HOST_AUTH
816 static ssize_t dhchap_secret_show(struct device *dev,
817 		struct device_attribute *attr, char *buf)
818 {
819 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
820 	struct nvmf_ctrl_options *opts = ctrl->opts;
821 
822 	if (!opts->dhchap_secret)
823 		return sysfs_emit(buf, "none\n");
824 	return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
825 }
826 
827 static ssize_t dhchap_secret_store(struct device *dev,
828 		struct device_attribute *attr, const char *buf, size_t count)
829 {
830 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
831 	struct nvmf_ctrl_options *opts = ctrl->opts;
832 	char *dhchap_secret;
833 
834 	if (!ctrl->opts->dhchap_secret)
835 		return -EINVAL;
836 	if (count < 7)
837 		return -EINVAL;
838 	if (memcmp(buf, "DHHC-1:", 7))
839 		return -EINVAL;
840 
841 	dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
842 	if (!dhchap_secret)
843 		return -ENOMEM;
844 	memcpy(dhchap_secret, buf, count);
845 	nvme_auth_stop(ctrl);
846 	if (strcmp(dhchap_secret, opts->dhchap_secret)) {
847 		struct nvme_dhchap_key *key, *host_key;
848 		int ret;
849 
850 		ret = nvme_auth_parse_key(dhchap_secret, &key);
851 		if (ret) {
852 			kfree(dhchap_secret);
853 			return ret;
854 		}
855 		kfree(opts->dhchap_secret);
856 		opts->dhchap_secret = dhchap_secret;
857 		host_key = ctrl->host_key;
858 		mutex_lock(&ctrl->dhchap_auth_mutex);
859 		ctrl->host_key = key;
860 		mutex_unlock(&ctrl->dhchap_auth_mutex);
861 		nvme_auth_free_key(host_key);
862 	} else
863 		kfree(dhchap_secret);
864 	/* Start re-authentication */
865 	dev_info(ctrl->device, "re-authenticating controller\n");
866 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
867 
868 	return count;
869 }
870 
871 static DEVICE_ATTR_ADMIN_RW(dhchap_secret);
872 
873 static ssize_t dhchap_ctrl_secret_show(struct device *dev,
874 		struct device_attribute *attr, char *buf)
875 {
876 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
877 	struct nvmf_ctrl_options *opts = ctrl->opts;
878 
879 	if (!opts->dhchap_ctrl_secret)
880 		return sysfs_emit(buf, "none\n");
881 	return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
882 }
883 
884 static ssize_t dhchap_ctrl_secret_store(struct device *dev,
885 		struct device_attribute *attr, const char *buf, size_t count)
886 {
887 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
888 	struct nvmf_ctrl_options *opts = ctrl->opts;
889 	char *dhchap_secret;
890 
891 	if (!ctrl->opts->dhchap_ctrl_secret)
892 		return -EINVAL;
893 	if (count < 7)
894 		return -EINVAL;
895 	if (memcmp(buf, "DHHC-1:", 7))
896 		return -EINVAL;
897 
898 	dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
899 	if (!dhchap_secret)
900 		return -ENOMEM;
901 	memcpy(dhchap_secret, buf, count);
902 	nvme_auth_stop(ctrl);
903 	if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
904 		struct nvme_dhchap_key *key, *ctrl_key;
905 		int ret;
906 
907 		ret = nvme_auth_parse_key(dhchap_secret, &key);
908 		if (ret) {
909 			kfree(dhchap_secret);
910 			return ret;
911 		}
912 		kfree(opts->dhchap_ctrl_secret);
913 		opts->dhchap_ctrl_secret = dhchap_secret;
914 		ctrl_key = ctrl->ctrl_key;
915 		mutex_lock(&ctrl->dhchap_auth_mutex);
916 		ctrl->ctrl_key = key;
917 		mutex_unlock(&ctrl->dhchap_auth_mutex);
918 		nvme_auth_free_key(ctrl_key);
919 	} else
920 		kfree(dhchap_secret);
921 	/* Start re-authentication */
922 	dev_info(ctrl->device, "re-authenticating controller\n");
923 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
924 
925 	return count;
926 }
927 
928 static DEVICE_ATTR_ADMIN_RW(dhchap_ctrl_secret);
929 #endif
930 
931 static struct attribute *nvme_dev_attrs[] = {
932 	&dev_attr_reset_controller.attr,
933 	&dev_attr_rescan_controller.attr,
934 	&dev_attr_model.attr,
935 	&dev_attr_serial.attr,
936 	&dev_attr_firmware_rev.attr,
937 	&dev_attr_cntlid.attr,
938 	&dev_attr_delete_controller.attr,
939 	&dev_attr_transport.attr,
940 	&dev_attr_subsysnqn.attr,
941 	&dev_attr_address.attr,
942 	&dev_attr_state.attr,
943 	&dev_attr_numa_node.attr,
944 	&dev_attr_queue_count.attr,
945 	&dev_attr_sqsize.attr,
946 	&dev_attr_hostnqn.attr,
947 	&dev_attr_hostid.attr,
948 	&dev_attr_ctrl_loss_tmo.attr,
949 	&dev_attr_reconnect_delay.attr,
950 	&dev_attr_fast_io_fail_tmo.attr,
951 	&dev_attr_kato.attr,
952 	&dev_attr_cntrltype.attr,
953 	&dev_attr_dctype.attr,
954 	&dev_attr_quirks.attr,
955 	&dev_attr_admin_timeout.attr,
956 	&dev_attr_io_timeout.attr,
957 #ifdef CONFIG_NVME_HOST_AUTH
958 	&dev_attr_dhchap_secret.attr,
959 	&dev_attr_dhchap_ctrl_secret.attr,
960 #endif
961 	&dev_attr_adm_passthru_err_log_enabled.attr,
962 	NULL
963 };
964 
965 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
966 		struct attribute *a, int n)
967 {
968 	struct device *dev = container_of(kobj, struct device, kobj);
969 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
970 
971 	if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
972 		return 0;
973 	if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
974 		return 0;
975 	if (a == &dev_attr_hostnqn.attr && !ctrl->opts)
976 		return 0;
977 	if (a == &dev_attr_hostid.attr && !ctrl->opts)
978 		return 0;
979 	if (a == &dev_attr_ctrl_loss_tmo.attr && !ctrl->opts)
980 		return 0;
981 	if (a == &dev_attr_reconnect_delay.attr && !ctrl->opts)
982 		return 0;
983 	if (a == &dev_attr_fast_io_fail_tmo.attr && !ctrl->opts)
984 		return 0;
985 #ifdef CONFIG_NVME_HOST_AUTH
986 	if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
987 		return 0;
988 	if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
989 		return 0;
990 #endif
991 
992 	return a->mode;
993 }
994 
995 const struct attribute_group nvme_dev_attrs_group = {
996 	.attrs		= nvme_dev_attrs,
997 	.is_visible	= nvme_dev_attrs_are_visible,
998 };
999 EXPORT_SYMBOL_GPL(nvme_dev_attrs_group);
1000 
1001 #ifdef CONFIG_NVME_TCP_TLS
1002 static ssize_t tls_key_show(struct device *dev,
1003 			    struct device_attribute *attr, char *buf)
1004 {
1005 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1006 
1007 	if (!ctrl->tls_pskid)
1008 		return 0;
1009 	return sysfs_emit(buf, "%08x\n", ctrl->tls_pskid);
1010 }
1011 static DEVICE_ATTR_RO(tls_key);
1012 
1013 static ssize_t tls_configured_key_show(struct device *dev,
1014 		struct device_attribute *attr, char *buf)
1015 {
1016 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1017 	struct key *key = ctrl->opts->tls_key;
1018 
1019 	return sysfs_emit(buf, "%08x\n", key_serial(key));
1020 }
1021 
1022 static ssize_t tls_configured_key_store(struct device *dev,
1023 					struct device_attribute *attr,
1024 					const char *buf, size_t count)
1025 {
1026 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1027 	int error, qid;
1028 
1029 	error = kstrtoint(buf, 10, &qid);
1030 	if (error)
1031 		return error;
1032 
1033 	/*
1034 	 * We currently only allow userspace to write a `0` indicating
1035 	 * generate a new key.
1036 	 */
1037 	if (qid)
1038 		return -EINVAL;
1039 
1040 	if (!ctrl->opts || !ctrl->opts->concat)
1041 		return -EOPNOTSUPP;
1042 
1043 	error = nvme_auth_negotiate(ctrl, 0);
1044 	if (error < 0) {
1045 		nvme_reset_ctrl(ctrl);
1046 		return error;
1047 	}
1048 
1049 	error = nvme_auth_wait(ctrl, 0);
1050 	if (error < 0) {
1051 		nvme_reset_ctrl(ctrl);
1052 		return error;
1053 	}
1054 
1055 	/*
1056 	 * We need to reset the TLS connection, so let's just
1057 	 * reset the controller.
1058 	 */
1059 	nvme_reset_ctrl(ctrl);
1060 
1061 	return count;
1062 }
1063 static DEVICE_ATTR_RW(tls_configured_key);
1064 
1065 static ssize_t tls_keyring_show(struct device *dev,
1066 		struct device_attribute *attr, char *buf)
1067 {
1068 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1069 	struct key *keyring = ctrl->opts->keyring;
1070 
1071 	return sysfs_emit(buf, "%s\n", keyring->description);
1072 }
1073 static DEVICE_ATTR_RO(tls_keyring);
1074 
1075 static ssize_t tls_mode_show(struct device *dev,
1076 			     struct device_attribute *attr, char *buf)
1077 {
1078 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1079 	const char *mode;
1080 
1081 	if (ctrl->opts->tls)
1082 		mode = "tls";
1083 	else
1084 		mode = "concat";
1085 
1086 	return sysfs_emit(buf, "%s\n", mode);
1087 }
1088 static DEVICE_ATTR_RO(tls_mode);
1089 
1090 static struct attribute *nvme_tls_attrs[] = {
1091 	&dev_attr_tls_key.attr,
1092 	&dev_attr_tls_configured_key.attr,
1093 	&dev_attr_tls_keyring.attr,
1094 	&dev_attr_tls_mode.attr,
1095 	NULL,
1096 };
1097 
1098 static umode_t nvme_tls_attrs_are_visible(struct kobject *kobj,
1099 		struct attribute *a, int n)
1100 {
1101 	struct device *dev = container_of(kobj, struct device, kobj);
1102 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1103 
1104 	if (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp"))
1105 		return 0;
1106 
1107 	if (a == &dev_attr_tls_key.attr &&
1108 	    !ctrl->opts->tls && !ctrl->opts->concat)
1109 		return 0;
1110 	if (a == &dev_attr_tls_configured_key.attr &&
1111 	    !ctrl->opts->concat)
1112 		return 0;
1113 	if (a == &dev_attr_tls_keyring.attr &&
1114 	    !ctrl->opts->keyring)
1115 		return 0;
1116 	if (a == &dev_attr_tls_mode.attr &&
1117 	    !ctrl->opts->tls && !ctrl->opts->concat)
1118 		return 0;
1119 
1120 	return a->mode;
1121 }
1122 
1123 static const struct attribute_group nvme_tls_attrs_group = {
1124 	.attrs		= nvme_tls_attrs,
1125 	.is_visible	= nvme_tls_attrs_are_visible,
1126 };
1127 #endif
1128 
1129 static ssize_t nvme_adm_errors_show(struct device *dev,
1130 		struct device_attribute *attr, char *buf)
1131 {
1132 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1133 
1134 	return sysfs_emit(buf, "%lu\n",
1135 			(unsigned long)atomic_long_read(&ctrl->errors));
1136 }
1137 
1138 static ssize_t nvme_adm_errors_store(struct device *dev,
1139 		struct device_attribute *attr, const char *buf, size_t count)
1140 {
1141 	unsigned long errors;
1142 	int err;
1143 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1144 
1145 	err = kstrtoul(buf, 0, &errors);
1146 	if (err)
1147 		return -EINVAL;
1148 
1149 	atomic_long_set(&ctrl->errors, errors);
1150 
1151 	return count;
1152 }
1153 
1154 static struct device_attribute dev_attr_adm_errors =
1155 	__ATTR(command_error_count, 0644,
1156 		nvme_adm_errors_show, nvme_adm_errors_store);
1157 
1158 static ssize_t reset_count_show(struct device *dev,
1159 		   struct device_attribute *attr, char *buf)
1160 {
1161 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1162 
1163 	return sysfs_emit(buf, "%lu\n", atomic_long_read(&ctrl->nr_reset));
1164 }
1165 
1166 static ssize_t reset_count_store(struct device *dev,
1167 		struct device_attribute *attr, const char *buf, size_t count)
1168 {
1169 	int err;
1170 	unsigned long reset_cnt;
1171 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1172 
1173 	err = kstrtoul(buf, 0, &reset_cnt);
1174 	if (err)
1175 		return -EINVAL;
1176 
1177 	atomic_long_set(&ctrl->nr_reset, reset_cnt);
1178 
1179 	return count;
1180 }
1181 
1182 static ssize_t reconnect_count_show(struct device *dev,
1183 		   struct device_attribute *attr, char *buf)
1184 {
1185 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1186 
1187 	return sysfs_emit(buf, "%lu\n",
1188 			  atomic_long_read(&ctrl->acc_reconnects) +
1189 			  ctrl->nr_reconnects);
1190 }
1191 
1192 static ssize_t reconnect_count_store(struct device *dev,
1193 		struct device_attribute *attr, const char *buf, size_t count)
1194 {
1195 	int err;
1196 	unsigned long reconnect_cnt;
1197 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1198 
1199 	err = kstrtoul(buf, 0, &reconnect_cnt);
1200 	if (err)
1201 		return -EINVAL;
1202 
1203 	atomic_long_set(&ctrl->acc_reconnects, reconnect_cnt);
1204 
1205 	return count;
1206 }
1207 
1208 static DEVICE_ATTR_RW(reconnect_count);
1209 
1210 static DEVICE_ATTR_RW(reset_count);
1211 
1212 static struct attribute *nvme_dev_diag_attrs[] = {
1213 	&dev_attr_adm_errors.attr,
1214 	&dev_attr_reset_count.attr,
1215 	&dev_attr_reconnect_count.attr,
1216 	NULL,
1217 };
1218 
1219 static umode_t nvme_dev_diag_attrs_are_visible(struct kobject *kobj,
1220 		struct attribute *a, int n)
1221 {
1222 	struct device *dev = container_of(kobj, struct device, kobj);
1223 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1224 
1225 	if (a == &dev_attr_reconnect_count.attr && !ctrl->opts)
1226 		return 0;
1227 
1228 	return a->mode;
1229 }
1230 
1231 const struct attribute_group nvme_dev_diag_attrs_group = {
1232 	.name		= "diag",
1233 	.attrs		= nvme_dev_diag_attrs,
1234 	.is_visible	= nvme_dev_diag_attrs_are_visible,
1235 };
1236 EXPORT_SYMBOL_GPL(nvme_dev_diag_attrs_group);
1237 
1238 const struct attribute_group *nvme_dev_attr_groups[] = {
1239 	&nvme_dev_attrs_group,
1240 #ifdef CONFIG_NVME_TCP_TLS
1241 	&nvme_tls_attrs_group,
1242 #endif
1243 	&nvme_dev_diag_attrs_group,
1244 	NULL,
1245 };
1246 
1247 #define SUBSYS_ATTR_RO(_name, _mode, _show)			\
1248 	struct device_attribute subsys_attr_##_name = \
1249 		__ATTR(_name, _mode, _show, NULL)
1250 
1251 static ssize_t nvme_subsys_show_nqn(struct device *dev,
1252 				    struct device_attribute *attr,
1253 				    char *buf)
1254 {
1255 	struct nvme_subsystem *subsys =
1256 		container_of(dev, struct nvme_subsystem, dev);
1257 
1258 	return sysfs_emit(buf, "%s\n", subsys->subnqn);
1259 }
1260 static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
1261 
1262 static ssize_t nvme_subsys_show_type(struct device *dev,
1263 				    struct device_attribute *attr,
1264 				    char *buf)
1265 {
1266 	struct nvme_subsystem *subsys =
1267 		container_of(dev, struct nvme_subsystem, dev);
1268 
1269 	switch (subsys->subtype) {
1270 	case NVME_NQN_DISC:
1271 		return sysfs_emit(buf, "discovery\n");
1272 	case NVME_NQN_NVME:
1273 		return sysfs_emit(buf, "nvm\n");
1274 	default:
1275 		return sysfs_emit(buf, "reserved\n");
1276 	}
1277 }
1278 static SUBSYS_ATTR_RO(subsystype, S_IRUGO, nvme_subsys_show_type);
1279 
1280 #define nvme_subsys_show_str_function(field)				\
1281 static ssize_t subsys_##field##_show(struct device *dev,		\
1282 			    struct device_attribute *attr, char *buf)	\
1283 {									\
1284 	struct nvme_subsystem *subsys =					\
1285 		container_of(dev, struct nvme_subsystem, dev);		\
1286 	return sysfs_emit(buf, "%.*s\n",				\
1287 			   (int)sizeof(subsys->field), subsys->field);	\
1288 }									\
1289 static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
1290 
1291 nvme_subsys_show_str_function(model);
1292 nvme_subsys_show_str_function(serial);
1293 nvme_subsys_show_str_function(firmware_rev);
1294 
1295 static struct attribute *nvme_subsys_attrs[] = {
1296 	&subsys_attr_model.attr,
1297 	&subsys_attr_serial.attr,
1298 	&subsys_attr_firmware_rev.attr,
1299 	&subsys_attr_subsysnqn.attr,
1300 	&subsys_attr_subsystype.attr,
1301 #ifdef CONFIG_NVME_MULTIPATH
1302 	&subsys_attr_iopolicy.attr,
1303 	&subsys_attr_iopolicies.attr,
1304 #endif
1305 	NULL,
1306 };
1307 
1308 static const struct attribute_group nvme_subsys_attrs_group = {
1309 	.attrs = nvme_subsys_attrs,
1310 };
1311 
1312 const struct attribute_group *nvme_subsys_attrs_groups[] = {
1313 	&nvme_subsys_attrs_group,
1314 	NULL,
1315 };
1316