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