xref: /linux/drivers/nvme/host/sysfs.c (revision 7fce38923253c27650835c91201b222214aeb149)
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 	return sysfs_emit(buf, "%d\n", ctrl->opts->reconnect_delay);
622 }
623 
624 static ssize_t nvme_ctrl_reconnect_delay_store(struct device *dev,
625 		struct device_attribute *attr, const char *buf, size_t count)
626 {
627 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
628 	unsigned int v;
629 	int err;
630 
631 	err = kstrtou32(buf, 10, &v);
632 	if (err)
633 		return err;
634 
635 	if (!v || v > INT_MAX)
636 		return -EINVAL;
637 
638 	ctrl->opts->reconnect_delay = v;
639 	return count;
640 }
641 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR,
642 	nvme_ctrl_reconnect_delay_show, nvme_ctrl_reconnect_delay_store);
643 
644 static ssize_t nvme_ctrl_fast_io_fail_tmo_show(struct device *dev,
645 		struct device_attribute *attr, char *buf)
646 {
647 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
648 
649 	if (ctrl->opts->fast_io_fail_tmo == -1)
650 		return sysfs_emit(buf, "off\n");
651 	return sysfs_emit(buf, "%d\n", ctrl->opts->fast_io_fail_tmo);
652 }
653 
654 static ssize_t nvme_ctrl_fast_io_fail_tmo_store(struct device *dev,
655 		struct device_attribute *attr, const char *buf, size_t count)
656 {
657 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
658 	struct nvmf_ctrl_options *opts = ctrl->opts;
659 	int fast_io_fail_tmo, err;
660 
661 	err = kstrtoint(buf, 10, &fast_io_fail_tmo);
662 	if (err)
663 		return -EINVAL;
664 
665 	if (fast_io_fail_tmo < 0)
666 		opts->fast_io_fail_tmo = -1;
667 	else
668 		opts->fast_io_fail_tmo = fast_io_fail_tmo;
669 	return count;
670 }
671 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
672 	nvme_ctrl_fast_io_fail_tmo_show, nvme_ctrl_fast_io_fail_tmo_store);
673 
674 static ssize_t cntrltype_show(struct device *dev,
675 			      struct device_attribute *attr, char *buf)
676 {
677 	static const char * const type[] = {
678 		[NVME_CTRL_IO] = "io\n",
679 		[NVME_CTRL_DISC] = "discovery\n",
680 		[NVME_CTRL_ADMIN] = "admin\n",
681 	};
682 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
683 
684 	if (ctrl->cntrltype > NVME_CTRL_ADMIN || !type[ctrl->cntrltype])
685 		return sysfs_emit(buf, "reserved\n");
686 
687 	return sysfs_emit(buf, "%s", type[ctrl->cntrltype]);
688 }
689 static DEVICE_ATTR_RO(cntrltype);
690 
691 static ssize_t dctype_show(struct device *dev,
692 			   struct device_attribute *attr, char *buf)
693 {
694 	static const char * const type[] = {
695 		[NVME_DCTYPE_NOT_REPORTED] = "none\n",
696 		[NVME_DCTYPE_DDC] = "ddc\n",
697 		[NVME_DCTYPE_CDC] = "cdc\n",
698 	};
699 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
700 
701 	if (ctrl->dctype > NVME_DCTYPE_CDC || !type[ctrl->dctype])
702 		return sysfs_emit(buf, "reserved\n");
703 
704 	return sysfs_emit(buf, "%s", type[ctrl->dctype]);
705 }
706 static DEVICE_ATTR_RO(dctype);
707 
708 static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
709                 char *buf)
710 {
711 	int count = 0, i;
712 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
713 	unsigned long quirks = ctrl->quirks;
714 
715 	if (!quirks)
716 		return sysfs_emit(buf, "none\n");
717 
718 	for (i = 0; quirks; ++i) {
719 		if (quirks & 1) {
720 			count += sysfs_emit_at(buf, count, "%s\n",
721 					nvme_quirk_name(BIT(i)));
722 		}
723 		quirks >>= 1;
724 	}
725 
726 	return count;
727 }
728 static DEVICE_ATTR_RO(quirks);
729 
730 static ssize_t nvme_admin_timeout_show(struct device *dev,
731 		struct device_attribute *attr, char *buf)
732 {
733 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
734 
735 	return sysfs_emit(buf, "%u\n",
736 				jiffies_to_msecs(ctrl->admin_timeout));
737 }
738 
739 static ssize_t nvme_admin_timeout_store(struct device *dev,
740 		struct device_attribute *attr,
741 		const char *buf, size_t count)
742 {
743 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
744 	u32 timeout;
745 	int err;
746 
747 	/*
748 	 * Wait until the controller reaches the LIVE state to be sure that
749 	 * admin_q and fabrics_q are properly initialized.
750 	 */
751 	if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
752 		return -EBUSY;
753 
754 	err = kstrtou32(buf, 10, &timeout);
755 	if (err || !timeout)
756 		return -EINVAL;
757 
758 	ctrl->admin_timeout = msecs_to_jiffies(timeout);
759 
760 	blk_queue_rq_timeout(ctrl->admin_q, ctrl->admin_timeout);
761 	if (ctrl->fabrics_q)
762 		blk_queue_rq_timeout(ctrl->fabrics_q, ctrl->admin_timeout);
763 
764 	return count;
765 }
766 
767 static DEVICE_ATTR(admin_timeout, S_IRUGO | S_IWUSR,
768 	nvme_admin_timeout_show, nvme_admin_timeout_store);
769 
770 static ssize_t nvme_io_timeout_show(struct device *dev,
771 		struct device_attribute *attr, char *buf)
772 {
773 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
774 
775 	return sysfs_emit(buf, "%u\n", jiffies_to_msecs(ctrl->io_timeout));
776 }
777 
778 static ssize_t nvme_io_timeout_store(struct device *dev,
779 		struct device_attribute *attr,
780 		const char *buf, size_t count)
781 {
782 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
783 	struct nvme_ns *ns;
784 	u32 timeout;
785 	int err;
786 
787 	/*
788 	 * Wait until the controller reaches the LIVE state to be sure that
789 	 * connect_q is properly initialized.
790 	 */
791 	if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
792 		return -EBUSY;
793 
794 	err = kstrtou32(buf, 10, &timeout);
795 	if (err || !timeout)
796 		return -EINVAL;
797 
798 	/* Take the namespaces_lock to avoid racing against nvme_alloc_ns() */
799 	mutex_lock(&ctrl->namespaces_lock);
800 
801 	ctrl->io_timeout = msecs_to_jiffies(timeout);
802 	list_for_each_entry(ns, &ctrl->namespaces, list)
803 		blk_queue_rq_timeout(ns->queue, ctrl->io_timeout);
804 
805 	mutex_unlock(&ctrl->namespaces_lock);
806 
807 	if (ctrl->connect_q)
808 		blk_queue_rq_timeout(ctrl->connect_q, ctrl->io_timeout);
809 
810 	return count;
811 }
812 
813 static DEVICE_ATTR(io_timeout, S_IRUGO | S_IWUSR,
814 	nvme_io_timeout_show, nvme_io_timeout_store);
815 
816 #ifdef CONFIG_NVME_HOST_AUTH
817 static ssize_t dhchap_secret_show(struct device *dev,
818 		struct device_attribute *attr, char *buf)
819 {
820 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
821 	struct nvmf_ctrl_options *opts = ctrl->opts;
822 
823 	if (!opts->dhchap_secret)
824 		return sysfs_emit(buf, "none\n");
825 	return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
826 }
827 
828 static ssize_t dhchap_secret_store(struct device *dev,
829 		struct device_attribute *attr, const char *buf, size_t count)
830 {
831 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
832 	struct nvmf_ctrl_options *opts = ctrl->opts;
833 	char *dhchap_secret;
834 
835 	if (!ctrl->opts->dhchap_secret)
836 		return -EINVAL;
837 	if (count < 7)
838 		return -EINVAL;
839 	if (memcmp(buf, "DHHC-1:", 7))
840 		return -EINVAL;
841 
842 	dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
843 	if (!dhchap_secret)
844 		return -ENOMEM;
845 	memcpy(dhchap_secret, buf, count);
846 	nvme_auth_stop(ctrl);
847 	if (strcmp(dhchap_secret, opts->dhchap_secret)) {
848 		struct nvme_dhchap_key *key, *host_key;
849 		int ret;
850 
851 		ret = nvme_auth_parse_key(dhchap_secret, &key);
852 		if (ret) {
853 			kfree_sensitive(dhchap_secret);
854 			return ret;
855 		}
856 		kfree_sensitive(opts->dhchap_secret);
857 		opts->dhchap_secret = dhchap_secret;
858 		host_key = ctrl->host_key;
859 		mutex_lock(&ctrl->dhchap_auth_mutex);
860 		ctrl->host_key = key;
861 		mutex_unlock(&ctrl->dhchap_auth_mutex);
862 		nvme_auth_free_key(host_key);
863 	} else
864 		kfree_sensitive(dhchap_secret);
865 	/* Start re-authentication */
866 	dev_info(ctrl->device, "re-authenticating controller\n");
867 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
868 
869 	return count;
870 }
871 
872 static DEVICE_ATTR_ADMIN_RW(dhchap_secret);
873 
874 static ssize_t dhchap_ctrl_secret_show(struct device *dev,
875 		struct device_attribute *attr, char *buf)
876 {
877 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
878 	struct nvmf_ctrl_options *opts = ctrl->opts;
879 
880 	if (!opts->dhchap_ctrl_secret)
881 		return sysfs_emit(buf, "none\n");
882 	return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
883 }
884 
885 static ssize_t dhchap_ctrl_secret_store(struct device *dev,
886 		struct device_attribute *attr, const char *buf, size_t count)
887 {
888 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
889 	struct nvmf_ctrl_options *opts = ctrl->opts;
890 	char *dhchap_secret;
891 
892 	if (!ctrl->opts->dhchap_ctrl_secret)
893 		return -EINVAL;
894 	if (count < 7)
895 		return -EINVAL;
896 	if (memcmp(buf, "DHHC-1:", 7))
897 		return -EINVAL;
898 
899 	dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
900 	if (!dhchap_secret)
901 		return -ENOMEM;
902 	memcpy(dhchap_secret, buf, count);
903 	nvme_auth_stop(ctrl);
904 	if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
905 		struct nvme_dhchap_key *key, *ctrl_key;
906 		int ret;
907 
908 		ret = nvme_auth_parse_key(dhchap_secret, &key);
909 		if (ret) {
910 			kfree_sensitive(dhchap_secret);
911 			return ret;
912 		}
913 		kfree_sensitive(opts->dhchap_ctrl_secret);
914 		opts->dhchap_ctrl_secret = dhchap_secret;
915 		ctrl_key = ctrl->ctrl_key;
916 		mutex_lock(&ctrl->dhchap_auth_mutex);
917 		ctrl->ctrl_key = key;
918 		mutex_unlock(&ctrl->dhchap_auth_mutex);
919 		nvme_auth_free_key(ctrl_key);
920 	} else
921 		kfree_sensitive(dhchap_secret);
922 	/* Start re-authentication */
923 	dev_info(ctrl->device, "re-authenticating controller\n");
924 	queue_work(nvme_wq, &ctrl->dhchap_auth_work);
925 
926 	return count;
927 }
928 
929 static DEVICE_ATTR_ADMIN_RW(dhchap_ctrl_secret);
930 #endif
931 
932 static struct attribute *nvme_dev_attrs[] = {
933 	&dev_attr_reset_controller.attr,
934 	&dev_attr_rescan_controller.attr,
935 	&dev_attr_model.attr,
936 	&dev_attr_serial.attr,
937 	&dev_attr_firmware_rev.attr,
938 	&dev_attr_cntlid.attr,
939 	&dev_attr_delete_controller.attr,
940 	&dev_attr_transport.attr,
941 	&dev_attr_subsysnqn.attr,
942 	&dev_attr_address.attr,
943 	&dev_attr_state.attr,
944 	&dev_attr_numa_node.attr,
945 	&dev_attr_queue_count.attr,
946 	&dev_attr_sqsize.attr,
947 	&dev_attr_hostnqn.attr,
948 	&dev_attr_hostid.attr,
949 	&dev_attr_ctrl_loss_tmo.attr,
950 	&dev_attr_reconnect_delay.attr,
951 	&dev_attr_fast_io_fail_tmo.attr,
952 	&dev_attr_kato.attr,
953 	&dev_attr_cntrltype.attr,
954 	&dev_attr_dctype.attr,
955 	&dev_attr_quirks.attr,
956 	&dev_attr_admin_timeout.attr,
957 	&dev_attr_io_timeout.attr,
958 #ifdef CONFIG_NVME_HOST_AUTH
959 	&dev_attr_dhchap_secret.attr,
960 	&dev_attr_dhchap_ctrl_secret.attr,
961 #endif
962 	&dev_attr_adm_passthru_err_log_enabled.attr,
963 	NULL
964 };
965 
966 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
967 		struct attribute *a, int n)
968 {
969 	struct device *dev = container_of(kobj, struct device, kobj);
970 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
971 
972 	if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
973 		return 0;
974 	if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
975 		return 0;
976 	if (a == &dev_attr_hostnqn.attr && !ctrl->opts)
977 		return 0;
978 	if (a == &dev_attr_hostid.attr && !ctrl->opts)
979 		return 0;
980 	if (a == &dev_attr_ctrl_loss_tmo.attr && !ctrl->opts)
981 		return 0;
982 	if (a == &dev_attr_reconnect_delay.attr && !ctrl->opts)
983 		return 0;
984 	if (a == &dev_attr_fast_io_fail_tmo.attr && !ctrl->opts)
985 		return 0;
986 #ifdef CONFIG_NVME_HOST_AUTH
987 	if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
988 		return 0;
989 	if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
990 		return 0;
991 #endif
992 
993 	return a->mode;
994 }
995 
996 const struct attribute_group nvme_dev_attrs_group = {
997 	.attrs		= nvme_dev_attrs,
998 	.is_visible	= nvme_dev_attrs_are_visible,
999 };
1000 EXPORT_SYMBOL_GPL(nvme_dev_attrs_group);
1001 
1002 #ifdef CONFIG_NVME_TCP_TLS
1003 static ssize_t tls_key_show(struct device *dev,
1004 			    struct device_attribute *attr, char *buf)
1005 {
1006 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1007 
1008 	if (!ctrl->tls_pskid)
1009 		return 0;
1010 	return sysfs_emit(buf, "%08x\n", ctrl->tls_pskid);
1011 }
1012 static DEVICE_ATTR_RO(tls_key);
1013 
1014 static ssize_t tls_configured_key_show(struct device *dev,
1015 		struct device_attribute *attr, char *buf)
1016 {
1017 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1018 	struct key *key = ctrl->opts->tls_key;
1019 
1020 	return sysfs_emit(buf, "%08x\n", key_serial(key));
1021 }
1022 
1023 static ssize_t tls_configured_key_store(struct device *dev,
1024 					struct device_attribute *attr,
1025 					const char *buf, size_t count)
1026 {
1027 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1028 	int error, qid;
1029 
1030 	error = kstrtoint(buf, 10, &qid);
1031 	if (error)
1032 		return error;
1033 
1034 	/*
1035 	 * We currently only allow userspace to write a `0` indicating
1036 	 * generate a new key.
1037 	 */
1038 	if (qid)
1039 		return -EINVAL;
1040 
1041 	if (!ctrl->opts || !ctrl->opts->concat)
1042 		return -EOPNOTSUPP;
1043 
1044 	error = nvme_auth_negotiate(ctrl, 0);
1045 	if (error < 0) {
1046 		nvme_reset_ctrl(ctrl);
1047 		return error;
1048 	}
1049 
1050 	error = nvme_auth_wait(ctrl, 0);
1051 	if (error < 0) {
1052 		nvme_reset_ctrl(ctrl);
1053 		return error;
1054 	}
1055 
1056 	/*
1057 	 * We need to reset the TLS connection, so let's just
1058 	 * reset the controller.
1059 	 */
1060 	nvme_reset_ctrl(ctrl);
1061 
1062 	return count;
1063 }
1064 static DEVICE_ATTR_RW(tls_configured_key);
1065 
1066 static ssize_t tls_keyring_show(struct device *dev,
1067 		struct device_attribute *attr, char *buf)
1068 {
1069 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1070 	struct key *keyring = ctrl->opts->keyring;
1071 
1072 	return sysfs_emit(buf, "%s\n", keyring->description);
1073 }
1074 static DEVICE_ATTR_RO(tls_keyring);
1075 
1076 static ssize_t tls_mode_show(struct device *dev,
1077 			     struct device_attribute *attr, char *buf)
1078 {
1079 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1080 	const char *mode;
1081 
1082 	if (ctrl->opts->tls)
1083 		mode = "tls";
1084 	else
1085 		mode = "concat";
1086 
1087 	return sysfs_emit(buf, "%s\n", mode);
1088 }
1089 static DEVICE_ATTR_RO(tls_mode);
1090 
1091 static struct attribute *nvme_tls_attrs[] = {
1092 	&dev_attr_tls_key.attr,
1093 	&dev_attr_tls_configured_key.attr,
1094 	&dev_attr_tls_keyring.attr,
1095 	&dev_attr_tls_mode.attr,
1096 	NULL,
1097 };
1098 
1099 static umode_t nvme_tls_attrs_are_visible(struct kobject *kobj,
1100 		struct attribute *a, int n)
1101 {
1102 	struct device *dev = container_of(kobj, struct device, kobj);
1103 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1104 
1105 	if (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp"))
1106 		return 0;
1107 
1108 	if (a == &dev_attr_tls_key.attr &&
1109 	    !ctrl->opts->tls && !ctrl->opts->concat)
1110 		return 0;
1111 	if (a == &dev_attr_tls_configured_key.attr &&
1112 	    !ctrl->opts->concat)
1113 		return 0;
1114 	if (a == &dev_attr_tls_keyring.attr &&
1115 	    !ctrl->opts->keyring)
1116 		return 0;
1117 	if (a == &dev_attr_tls_mode.attr &&
1118 	    !ctrl->opts->tls && !ctrl->opts->concat)
1119 		return 0;
1120 
1121 	return a->mode;
1122 }
1123 
1124 static const struct attribute_group nvme_tls_attrs_group = {
1125 	.attrs		= nvme_tls_attrs,
1126 	.is_visible	= nvme_tls_attrs_are_visible,
1127 };
1128 #endif
1129 
1130 static ssize_t nvme_adm_errors_show(struct device *dev,
1131 		struct device_attribute *attr, char *buf)
1132 {
1133 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1134 
1135 	return sysfs_emit(buf, "%lu\n",
1136 			(unsigned long)atomic_long_read(&ctrl->errors));
1137 }
1138 
1139 static ssize_t nvme_adm_errors_store(struct device *dev,
1140 		struct device_attribute *attr, const char *buf, size_t count)
1141 {
1142 	unsigned long errors;
1143 	int err;
1144 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1145 
1146 	err = kstrtoul(buf, 0, &errors);
1147 	if (err)
1148 		return -EINVAL;
1149 
1150 	atomic_long_set(&ctrl->errors, errors);
1151 
1152 	return count;
1153 }
1154 
1155 static struct device_attribute dev_attr_adm_errors =
1156 	__ATTR(command_error_count, 0644,
1157 		nvme_adm_errors_show, nvme_adm_errors_store);
1158 
1159 static ssize_t reset_count_show(struct device *dev,
1160 		   struct device_attribute *attr, char *buf)
1161 {
1162 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1163 
1164 	return sysfs_emit(buf, "%lu\n", atomic_long_read(&ctrl->nr_reset));
1165 }
1166 
1167 static ssize_t reset_count_store(struct device *dev,
1168 		struct device_attribute *attr, const char *buf, size_t count)
1169 {
1170 	int err;
1171 	unsigned long reset_cnt;
1172 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1173 
1174 	err = kstrtoul(buf, 0, &reset_cnt);
1175 	if (err)
1176 		return -EINVAL;
1177 
1178 	atomic_long_set(&ctrl->nr_reset, reset_cnt);
1179 
1180 	return count;
1181 }
1182 
1183 static ssize_t reconnect_count_show(struct device *dev,
1184 		   struct device_attribute *attr, char *buf)
1185 {
1186 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1187 
1188 	return sysfs_emit(buf, "%lu\n",
1189 			  atomic_long_read(&ctrl->acc_reconnects) +
1190 			  ctrl->nr_reconnects);
1191 }
1192 
1193 static ssize_t reconnect_count_store(struct device *dev,
1194 		struct device_attribute *attr, const char *buf, size_t count)
1195 {
1196 	int err;
1197 	unsigned long reconnect_cnt;
1198 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1199 
1200 	err = kstrtoul(buf, 0, &reconnect_cnt);
1201 	if (err)
1202 		return -EINVAL;
1203 
1204 	atomic_long_set(&ctrl->acc_reconnects, reconnect_cnt);
1205 
1206 	return count;
1207 }
1208 
1209 static DEVICE_ATTR_RW(reconnect_count);
1210 
1211 static DEVICE_ATTR_RW(reset_count);
1212 
1213 static struct attribute *nvme_dev_diag_attrs[] = {
1214 	&dev_attr_adm_errors.attr,
1215 	&dev_attr_reset_count.attr,
1216 	&dev_attr_reconnect_count.attr,
1217 	NULL,
1218 };
1219 
1220 static umode_t nvme_dev_diag_attrs_are_visible(struct kobject *kobj,
1221 		struct attribute *a, int n)
1222 {
1223 	struct device *dev = container_of(kobj, struct device, kobj);
1224 	struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
1225 
1226 	if (a == &dev_attr_reconnect_count.attr && !ctrl->opts)
1227 		return 0;
1228 
1229 	return a->mode;
1230 }
1231 
1232 const struct attribute_group nvme_dev_diag_attrs_group = {
1233 	.name		= "diag",
1234 	.attrs		= nvme_dev_diag_attrs,
1235 	.is_visible	= nvme_dev_diag_attrs_are_visible,
1236 };
1237 EXPORT_SYMBOL_GPL(nvme_dev_diag_attrs_group);
1238 
1239 const struct attribute_group *nvme_dev_attr_groups[] = {
1240 	&nvme_dev_attrs_group,
1241 #ifdef CONFIG_NVME_TCP_TLS
1242 	&nvme_tls_attrs_group,
1243 #endif
1244 	&nvme_dev_diag_attrs_group,
1245 	NULL,
1246 };
1247 
1248 #define SUBSYS_ATTR_RO(_name, _mode, _show)			\
1249 	struct device_attribute subsys_attr_##_name = \
1250 		__ATTR(_name, _mode, _show, NULL)
1251 
1252 static ssize_t nvme_subsys_show_nqn(struct device *dev,
1253 				    struct device_attribute *attr,
1254 				    char *buf)
1255 {
1256 	struct nvme_subsystem *subsys =
1257 		container_of(dev, struct nvme_subsystem, dev);
1258 
1259 	return sysfs_emit(buf, "%s\n", subsys->subnqn);
1260 }
1261 static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
1262 
1263 static ssize_t nvme_subsys_show_type(struct device *dev,
1264 				    struct device_attribute *attr,
1265 				    char *buf)
1266 {
1267 	struct nvme_subsystem *subsys =
1268 		container_of(dev, struct nvme_subsystem, dev);
1269 
1270 	switch (subsys->subtype) {
1271 	case NVME_NQN_DISC:
1272 		return sysfs_emit(buf, "discovery\n");
1273 	case NVME_NQN_NVME:
1274 		return sysfs_emit(buf, "nvm\n");
1275 	default:
1276 		return sysfs_emit(buf, "reserved\n");
1277 	}
1278 }
1279 static SUBSYS_ATTR_RO(subsystype, S_IRUGO, nvme_subsys_show_type);
1280 
1281 #define nvme_subsys_show_str_function(field)				\
1282 static ssize_t subsys_##field##_show(struct device *dev,		\
1283 			    struct device_attribute *attr, char *buf)	\
1284 {									\
1285 	struct nvme_subsystem *subsys =					\
1286 		container_of(dev, struct nvme_subsystem, dev);		\
1287 	return sysfs_emit(buf, "%.*s\n",				\
1288 			   (int)sizeof(subsys->field), subsys->field);	\
1289 }									\
1290 static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
1291 
1292 nvme_subsys_show_str_function(model);
1293 nvme_subsys_show_str_function(serial);
1294 nvme_subsys_show_str_function(firmware_rev);
1295 
1296 static struct attribute *nvme_subsys_attrs[] = {
1297 	&subsys_attr_model.attr,
1298 	&subsys_attr_serial.attr,
1299 	&subsys_attr_firmware_rev.attr,
1300 	&subsys_attr_subsysnqn.attr,
1301 	&subsys_attr_subsystype.attr,
1302 #ifdef CONFIG_NVME_MULTIPATH
1303 	&subsys_attr_iopolicy.attr,
1304 	&subsys_attr_iopolicies.attr,
1305 #endif
1306 	NULL,
1307 };
1308 
1309 static const struct attribute_group nvme_subsys_attrs_group = {
1310 	.attrs = nvme_subsys_attrs,
1311 };
1312 
1313 const struct attribute_group *nvme_subsys_attrs_groups[] = {
1314 	&nvme_subsys_attrs_group,
1315 	NULL,
1316 };
1317