1 // SPDX-License-Identifier: GPL-2.0
3 * Sysfs interface for the NVMe core driver.
5 * Copyright (c) 2011-2014, Intel Corporation.
8 #include <linux/nvme-auth.h>
13 static ssize_t nvme_sysfs_reset(struct device *dev,
14 struct device_attribute *attr, const char *buf,
17 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
20 ret = nvme_reset_ctrl_sync(ctrl);
25 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
27 static ssize_t nvme_sysfs_rescan(struct device *dev,
28 struct device_attribute *attr, const char *buf,
31 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
33 nvme_queue_scan(ctrl);
36 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
38 static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
40 struct gendisk *disk = dev_to_disk(dev);
42 if (disk->fops == &nvme_bdev_ops)
43 return nvme_get_ns_from_dev(dev)->head;
45 return disk->private_data;
48 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
51 struct nvme_ns_head *head = dev_to_ns_head(dev);
52 struct nvme_ns_ids *ids = &head->ids;
53 struct nvme_subsystem *subsys = head->subsys;
54 int serial_len = sizeof(subsys->serial);
55 int model_len = sizeof(subsys->model);
57 if (!uuid_is_null(&ids->uuid))
58 return sysfs_emit(buf, "uuid.%pU\n", &ids->uuid);
60 if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
61 return sysfs_emit(buf, "eui.%16phN\n", ids->nguid);
63 if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
64 return sysfs_emit(buf, "eui.%8phN\n", ids->eui64);
66 while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
67 subsys->serial[serial_len - 1] == '\0'))
69 while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
70 subsys->model[model_len - 1] == '\0'))
73 return sysfs_emit(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
74 serial_len, subsys->serial, model_len, subsys->model,
77 static DEVICE_ATTR_RO(wwid);
79 static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
82 return sysfs_emit(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
84 static DEVICE_ATTR_RO(nguid);
86 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
89 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
91 /* For backward compatibility expose the NGUID to userspace if
94 if (uuid_is_null(&ids->uuid)) {
96 "No UUID available providing old NGUID\n");
97 return sysfs_emit(buf, "%pU\n", ids->nguid);
99 return sysfs_emit(buf, "%pU\n", &ids->uuid);
101 static DEVICE_ATTR_RO(uuid);
103 static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
106 return sysfs_emit(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
108 static DEVICE_ATTR_RO(eui);
110 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
113 return sysfs_emit(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
115 static DEVICE_ATTR_RO(nsid);
117 static struct attribute *nvme_ns_id_attrs[] = {
120 &dev_attr_nguid.attr,
123 #ifdef CONFIG_NVME_MULTIPATH
124 &dev_attr_ana_grpid.attr,
125 &dev_attr_ana_state.attr,
130 static umode_t nvme_ns_id_attrs_are_visible(struct kobject *kobj,
131 struct attribute *a, int n)
133 struct device *dev = container_of(kobj, struct device, kobj);
134 struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
136 if (a == &dev_attr_uuid.attr) {
137 if (uuid_is_null(&ids->uuid) &&
138 !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
141 if (a == &dev_attr_nguid.attr) {
142 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
145 if (a == &dev_attr_eui.attr) {
146 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
149 #ifdef CONFIG_NVME_MULTIPATH
150 if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) {
151 if (dev_to_disk(dev)->fops != &nvme_bdev_ops) /* per-path attr */
153 if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
160 static const struct attribute_group nvme_ns_id_attr_group = {
161 .attrs = nvme_ns_id_attrs,
162 .is_visible = nvme_ns_id_attrs_are_visible,
165 const struct attribute_group *nvme_ns_id_attr_groups[] = {
166 &nvme_ns_id_attr_group,
170 #define nvme_show_str_function(field) \
171 static ssize_t field##_show(struct device *dev, \
172 struct device_attribute *attr, char *buf) \
174 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
175 return sysfs_emit(buf, "%.*s\n", \
176 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field); \
178 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
180 nvme_show_str_function(model);
181 nvme_show_str_function(serial);
182 nvme_show_str_function(firmware_rev);
184 #define nvme_show_int_function(field) \
185 static ssize_t field##_show(struct device *dev, \
186 struct device_attribute *attr, char *buf) \
188 struct nvme_ctrl *ctrl = dev_get_drvdata(dev); \
189 return sysfs_emit(buf, "%d\n", ctrl->field); \
191 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
193 nvme_show_int_function(cntlid);
194 nvme_show_int_function(numa_node);
195 nvme_show_int_function(queue_count);
196 nvme_show_int_function(sqsize);
197 nvme_show_int_function(kato);
199 static ssize_t nvme_sysfs_delete(struct device *dev,
200 struct device_attribute *attr, const char *buf,
203 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
205 if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
208 if (device_remove_file_self(dev, attr))
209 nvme_delete_ctrl_sync(ctrl);
212 static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
214 static ssize_t nvme_sysfs_show_transport(struct device *dev,
215 struct device_attribute *attr,
218 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
220 return sysfs_emit(buf, "%s\n", ctrl->ops->name);
222 static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
224 static ssize_t nvme_sysfs_show_state(struct device *dev,
225 struct device_attribute *attr,
228 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
229 static const char *const state_name[] = {
230 [NVME_CTRL_NEW] = "new",
231 [NVME_CTRL_LIVE] = "live",
232 [NVME_CTRL_RESETTING] = "resetting",
233 [NVME_CTRL_CONNECTING] = "connecting",
234 [NVME_CTRL_DELETING] = "deleting",
235 [NVME_CTRL_DELETING_NOIO]= "deleting (no IO)",
236 [NVME_CTRL_DEAD] = "dead",
239 if ((unsigned)ctrl->state < ARRAY_SIZE(state_name) &&
240 state_name[ctrl->state])
241 return sysfs_emit(buf, "%s\n", state_name[ctrl->state]);
243 return sysfs_emit(buf, "unknown state\n");
246 static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
248 static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
249 struct device_attribute *attr,
252 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
254 return sysfs_emit(buf, "%s\n", ctrl->subsys->subnqn);
256 static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
258 static ssize_t nvme_sysfs_show_hostnqn(struct device *dev,
259 struct device_attribute *attr,
262 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
264 return sysfs_emit(buf, "%s\n", ctrl->opts->host->nqn);
266 static DEVICE_ATTR(hostnqn, S_IRUGO, nvme_sysfs_show_hostnqn, NULL);
268 static ssize_t nvme_sysfs_show_hostid(struct device *dev,
269 struct device_attribute *attr,
272 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
274 return sysfs_emit(buf, "%pU\n", &ctrl->opts->host->id);
276 static DEVICE_ATTR(hostid, S_IRUGO, nvme_sysfs_show_hostid, NULL);
278 static ssize_t nvme_sysfs_show_address(struct device *dev,
279 struct device_attribute *attr,
282 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
284 return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
286 static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
288 static ssize_t nvme_ctrl_loss_tmo_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
291 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
292 struct nvmf_ctrl_options *opts = ctrl->opts;
294 if (ctrl->opts->max_reconnects == -1)
295 return sysfs_emit(buf, "off\n");
296 return sysfs_emit(buf, "%d\n",
297 opts->max_reconnects * opts->reconnect_delay);
300 static ssize_t nvme_ctrl_loss_tmo_store(struct device *dev,
301 struct device_attribute *attr, const char *buf, size_t count)
303 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
304 struct nvmf_ctrl_options *opts = ctrl->opts;
305 int ctrl_loss_tmo, err;
307 err = kstrtoint(buf, 10, &ctrl_loss_tmo);
311 if (ctrl_loss_tmo < 0)
312 opts->max_reconnects = -1;
314 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
315 opts->reconnect_delay);
318 static DEVICE_ATTR(ctrl_loss_tmo, S_IRUGO | S_IWUSR,
319 nvme_ctrl_loss_tmo_show, nvme_ctrl_loss_tmo_store);
321 static ssize_t nvme_ctrl_reconnect_delay_show(struct device *dev,
322 struct device_attribute *attr, char *buf)
324 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
326 if (ctrl->opts->reconnect_delay == -1)
327 return sysfs_emit(buf, "off\n");
328 return sysfs_emit(buf, "%d\n", ctrl->opts->reconnect_delay);
331 static ssize_t nvme_ctrl_reconnect_delay_store(struct device *dev,
332 struct device_attribute *attr, const char *buf, size_t count)
334 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
338 err = kstrtou32(buf, 10, &v);
342 ctrl->opts->reconnect_delay = v;
345 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR,
346 nvme_ctrl_reconnect_delay_show, nvme_ctrl_reconnect_delay_store);
348 static ssize_t nvme_ctrl_fast_io_fail_tmo_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
351 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
353 if (ctrl->opts->fast_io_fail_tmo == -1)
354 return sysfs_emit(buf, "off\n");
355 return sysfs_emit(buf, "%d\n", ctrl->opts->fast_io_fail_tmo);
358 static ssize_t nvme_ctrl_fast_io_fail_tmo_store(struct device *dev,
359 struct device_attribute *attr, const char *buf, size_t count)
361 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
362 struct nvmf_ctrl_options *opts = ctrl->opts;
363 int fast_io_fail_tmo, err;
365 err = kstrtoint(buf, 10, &fast_io_fail_tmo);
369 if (fast_io_fail_tmo < 0)
370 opts->fast_io_fail_tmo = -1;
372 opts->fast_io_fail_tmo = fast_io_fail_tmo;
375 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
376 nvme_ctrl_fast_io_fail_tmo_show, nvme_ctrl_fast_io_fail_tmo_store);
378 static ssize_t cntrltype_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 static const char * const type[] = {
382 [NVME_CTRL_IO] = "io\n",
383 [NVME_CTRL_DISC] = "discovery\n",
384 [NVME_CTRL_ADMIN] = "admin\n",
386 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
388 if (ctrl->cntrltype > NVME_CTRL_ADMIN || !type[ctrl->cntrltype])
389 return sysfs_emit(buf, "reserved\n");
391 return sysfs_emit(buf, type[ctrl->cntrltype]);
393 static DEVICE_ATTR_RO(cntrltype);
395 static ssize_t dctype_show(struct device *dev,
396 struct device_attribute *attr, char *buf)
398 static const char * const type[] = {
399 [NVME_DCTYPE_NOT_REPORTED] = "none\n",
400 [NVME_DCTYPE_DDC] = "ddc\n",
401 [NVME_DCTYPE_CDC] = "cdc\n",
403 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
405 if (ctrl->dctype > NVME_DCTYPE_CDC || !type[ctrl->dctype])
406 return sysfs_emit(buf, "reserved\n");
408 return sysfs_emit(buf, type[ctrl->dctype]);
410 static DEVICE_ATTR_RO(dctype);
412 #ifdef CONFIG_NVME_AUTH
413 static ssize_t nvme_ctrl_dhchap_secret_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
417 struct nvmf_ctrl_options *opts = ctrl->opts;
419 if (!opts->dhchap_secret)
420 return sysfs_emit(buf, "none\n");
421 return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
424 static ssize_t nvme_ctrl_dhchap_secret_store(struct device *dev,
425 struct device_attribute *attr, const char *buf, size_t count)
427 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
428 struct nvmf_ctrl_options *opts = ctrl->opts;
431 if (!ctrl->opts->dhchap_secret)
435 if (memcmp(buf, "DHHC-1:", 7))
438 dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
441 memcpy(dhchap_secret, buf, count);
442 nvme_auth_stop(ctrl);
443 if (strcmp(dhchap_secret, opts->dhchap_secret)) {
444 struct nvme_dhchap_key *key, *host_key;
447 ret = nvme_auth_generate_key(dhchap_secret, &key);
449 kfree(dhchap_secret);
452 kfree(opts->dhchap_secret);
453 opts->dhchap_secret = dhchap_secret;
454 host_key = ctrl->host_key;
455 mutex_lock(&ctrl->dhchap_auth_mutex);
456 ctrl->host_key = key;
457 mutex_unlock(&ctrl->dhchap_auth_mutex);
458 nvme_auth_free_key(host_key);
460 kfree(dhchap_secret);
461 /* Start re-authentication */
462 dev_info(ctrl->device, "re-authenticating controller\n");
463 queue_work(nvme_wq, &ctrl->dhchap_auth_work);
468 static DEVICE_ATTR(dhchap_secret, S_IRUGO | S_IWUSR,
469 nvme_ctrl_dhchap_secret_show, nvme_ctrl_dhchap_secret_store);
471 static ssize_t nvme_ctrl_dhchap_ctrl_secret_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
474 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
475 struct nvmf_ctrl_options *opts = ctrl->opts;
477 if (!opts->dhchap_ctrl_secret)
478 return sysfs_emit(buf, "none\n");
479 return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
482 static ssize_t nvme_ctrl_dhchap_ctrl_secret_store(struct device *dev,
483 struct device_attribute *attr, const char *buf, size_t count)
485 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
486 struct nvmf_ctrl_options *opts = ctrl->opts;
489 if (!ctrl->opts->dhchap_ctrl_secret)
493 if (memcmp(buf, "DHHC-1:", 7))
496 dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
499 memcpy(dhchap_secret, buf, count);
500 nvme_auth_stop(ctrl);
501 if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
502 struct nvme_dhchap_key *key, *ctrl_key;
505 ret = nvme_auth_generate_key(dhchap_secret, &key);
507 kfree(dhchap_secret);
510 kfree(opts->dhchap_ctrl_secret);
511 opts->dhchap_ctrl_secret = dhchap_secret;
512 ctrl_key = ctrl->ctrl_key;
513 mutex_lock(&ctrl->dhchap_auth_mutex);
514 ctrl->ctrl_key = key;
515 mutex_unlock(&ctrl->dhchap_auth_mutex);
516 nvme_auth_free_key(ctrl_key);
518 kfree(dhchap_secret);
519 /* Start re-authentication */
520 dev_info(ctrl->device, "re-authenticating controller\n");
521 queue_work(nvme_wq, &ctrl->dhchap_auth_work);
526 static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR,
527 nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
530 static struct attribute *nvme_dev_attrs[] = {
531 &dev_attr_reset_controller.attr,
532 &dev_attr_rescan_controller.attr,
533 &dev_attr_model.attr,
534 &dev_attr_serial.attr,
535 &dev_attr_firmware_rev.attr,
536 &dev_attr_cntlid.attr,
537 &dev_attr_delete_controller.attr,
538 &dev_attr_transport.attr,
539 &dev_attr_subsysnqn.attr,
540 &dev_attr_address.attr,
541 &dev_attr_state.attr,
542 &dev_attr_numa_node.attr,
543 &dev_attr_queue_count.attr,
544 &dev_attr_sqsize.attr,
545 &dev_attr_hostnqn.attr,
546 &dev_attr_hostid.attr,
547 &dev_attr_ctrl_loss_tmo.attr,
548 &dev_attr_reconnect_delay.attr,
549 &dev_attr_fast_io_fail_tmo.attr,
551 &dev_attr_cntrltype.attr,
552 &dev_attr_dctype.attr,
553 #ifdef CONFIG_NVME_AUTH
554 &dev_attr_dhchap_secret.attr,
555 &dev_attr_dhchap_ctrl_secret.attr,
560 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
561 struct attribute *a, int n)
563 struct device *dev = container_of(kobj, struct device, kobj);
564 struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
566 if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
568 if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
570 if (a == &dev_attr_hostnqn.attr && !ctrl->opts)
572 if (a == &dev_attr_hostid.attr && !ctrl->opts)
574 if (a == &dev_attr_ctrl_loss_tmo.attr && !ctrl->opts)
576 if (a == &dev_attr_reconnect_delay.attr && !ctrl->opts)
578 if (a == &dev_attr_fast_io_fail_tmo.attr && !ctrl->opts)
580 #ifdef CONFIG_NVME_AUTH
581 if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
583 if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
590 const struct attribute_group nvme_dev_attrs_group = {
591 .attrs = nvme_dev_attrs,
592 .is_visible = nvme_dev_attrs_are_visible,
594 EXPORT_SYMBOL_GPL(nvme_dev_attrs_group);
596 const struct attribute_group *nvme_dev_attr_groups[] = {
597 &nvme_dev_attrs_group,
601 #define SUBSYS_ATTR_RO(_name, _mode, _show) \
602 struct device_attribute subsys_attr_##_name = \
603 __ATTR(_name, _mode, _show, NULL)
605 static ssize_t nvme_subsys_show_nqn(struct device *dev,
606 struct device_attribute *attr,
609 struct nvme_subsystem *subsys =
610 container_of(dev, struct nvme_subsystem, dev);
612 return sysfs_emit(buf, "%s\n", subsys->subnqn);
614 static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
616 static ssize_t nvme_subsys_show_type(struct device *dev,
617 struct device_attribute *attr,
620 struct nvme_subsystem *subsys =
621 container_of(dev, struct nvme_subsystem, dev);
623 switch (subsys->subtype) {
625 return sysfs_emit(buf, "discovery\n");
627 return sysfs_emit(buf, "nvm\n");
629 return sysfs_emit(buf, "reserved\n");
632 static SUBSYS_ATTR_RO(subsystype, S_IRUGO, nvme_subsys_show_type);
634 #define nvme_subsys_show_str_function(field) \
635 static ssize_t subsys_##field##_show(struct device *dev, \
636 struct device_attribute *attr, char *buf) \
638 struct nvme_subsystem *subsys = \
639 container_of(dev, struct nvme_subsystem, dev); \
640 return sysfs_emit(buf, "%.*s\n", \
641 (int)sizeof(subsys->field), subsys->field); \
643 static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
645 nvme_subsys_show_str_function(model);
646 nvme_subsys_show_str_function(serial);
647 nvme_subsys_show_str_function(firmware_rev);
649 static struct attribute *nvme_subsys_attrs[] = {
650 &subsys_attr_model.attr,
651 &subsys_attr_serial.attr,
652 &subsys_attr_firmware_rev.attr,
653 &subsys_attr_subsysnqn.attr,
654 &subsys_attr_subsystype.attr,
655 #ifdef CONFIG_NVME_MULTIPATH
656 &subsys_attr_iopolicy.attr,
661 static const struct attribute_group nvme_subsys_attrs_group = {
662 .attrs = nvme_subsys_attrs,
665 const struct attribute_group *nvme_subsys_attrs_groups[] = {
666 &nvme_subsys_attrs_group,