Merge tag 'ipsec-next-2024-03-06' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / nvme / host / sysfs.c
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
10 #include "nvme.h"
11 #include "fabrics.h"
12
13 static ssize_t nvme_sysfs_reset(struct device *dev,
14                                 struct device_attribute *attr, const char *buf,
15                                 size_t count)
16 {
17         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
18         int ret;
19
20         ret = nvme_reset_ctrl_sync(ctrl);
21         if (ret < 0)
22                 return ret;
23         return count;
24 }
25 static DEVICE_ATTR(reset_controller, S_IWUSR, NULL, nvme_sysfs_reset);
26
27 static ssize_t nvme_sysfs_rescan(struct device *dev,
28                                 struct device_attribute *attr, const char *buf,
29                                 size_t count)
30 {
31         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
32
33         nvme_queue_scan(ctrl);
34         return count;
35 }
36 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
37
38 static ssize_t nvme_adm_passthru_err_log_enabled_show(struct device *dev,
39                 struct device_attribute *attr, char *buf)
40 {
41         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
42
43         return sysfs_emit(buf,
44                           ctrl->passthru_err_log_enabled ? "on\n" : "off\n");
45 }
46
47 static ssize_t nvme_adm_passthru_err_log_enabled_store(struct device *dev,
48                 struct device_attribute *attr, const char *buf, size_t count)
49 {
50         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
51         bool passthru_err_log_enabled;
52         int err;
53
54         err = kstrtobool(buf, &passthru_err_log_enabled);
55         if (err)
56                 return -EINVAL;
57
58         ctrl->passthru_err_log_enabled = passthru_err_log_enabled;
59
60         return count;
61 }
62
63 static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
64 {
65         struct gendisk *disk = dev_to_disk(dev);
66
67         if (nvme_disk_is_ns_head(disk))
68                 return disk->private_data;
69         return nvme_get_ns_from_dev(dev)->head;
70 }
71
72 static ssize_t nvme_io_passthru_err_log_enabled_show(struct device *dev,
73                 struct device_attribute *attr, char *buf)
74 {
75         struct nvme_ns_head *head = dev_to_ns_head(dev);
76
77         return sysfs_emit(buf, head->passthru_err_log_enabled ? "on\n" : "off\n");
78 }
79
80 static ssize_t nvme_io_passthru_err_log_enabled_store(struct device *dev,
81                 struct device_attribute *attr, const char *buf, size_t count)
82 {
83         struct nvme_ns_head *head = dev_to_ns_head(dev);
84         bool passthru_err_log_enabled;
85         int err;
86
87         err = kstrtobool(buf, &passthru_err_log_enabled);
88         if (err)
89                 return -EINVAL;
90         head->passthru_err_log_enabled = passthru_err_log_enabled;
91
92         return count;
93 }
94
95 static struct device_attribute dev_attr_adm_passthru_err_log_enabled = \
96         __ATTR(passthru_err_log_enabled, S_IRUGO | S_IWUSR, \
97         nvme_adm_passthru_err_log_enabled_show, nvme_adm_passthru_err_log_enabled_store);
98
99 static struct device_attribute dev_attr_io_passthru_err_log_enabled = \
100         __ATTR(passthru_err_log_enabled, S_IRUGO | S_IWUSR, \
101         nvme_io_passthru_err_log_enabled_show, nvme_io_passthru_err_log_enabled_store);
102
103 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
104                 char *buf)
105 {
106         struct nvme_ns_head *head = dev_to_ns_head(dev);
107         struct nvme_ns_ids *ids = &head->ids;
108         struct nvme_subsystem *subsys = head->subsys;
109         int serial_len = sizeof(subsys->serial);
110         int model_len = sizeof(subsys->model);
111
112         if (!uuid_is_null(&ids->uuid))
113                 return sysfs_emit(buf, "uuid.%pU\n", &ids->uuid);
114
115         if (memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
116                 return sysfs_emit(buf, "eui.%16phN\n", ids->nguid);
117
118         if (memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
119                 return sysfs_emit(buf, "eui.%8phN\n", ids->eui64);
120
121         while (serial_len > 0 && (subsys->serial[serial_len - 1] == ' ' ||
122                                   subsys->serial[serial_len - 1] == '\0'))
123                 serial_len--;
124         while (model_len > 0 && (subsys->model[model_len - 1] == ' ' ||
125                                  subsys->model[model_len - 1] == '\0'))
126                 model_len--;
127
128         return sysfs_emit(buf, "nvme.%04x-%*phN-%*phN-%08x\n", subsys->vendor_id,
129                 serial_len, subsys->serial, model_len, subsys->model,
130                 head->ns_id);
131 }
132 static DEVICE_ATTR_RO(wwid);
133
134 static ssize_t nguid_show(struct device *dev, struct device_attribute *attr,
135                 char *buf)
136 {
137         return sysfs_emit(buf, "%pU\n", dev_to_ns_head(dev)->ids.nguid);
138 }
139 static DEVICE_ATTR_RO(nguid);
140
141 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr,
142                 char *buf)
143 {
144         struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
145
146         /* For backward compatibility expose the NGUID to userspace if
147          * we have no UUID set
148          */
149         if (uuid_is_null(&ids->uuid)) {
150                 dev_warn_once(dev,
151                         "No UUID available providing old NGUID\n");
152                 return sysfs_emit(buf, "%pU\n", ids->nguid);
153         }
154         return sysfs_emit(buf, "%pU\n", &ids->uuid);
155 }
156 static DEVICE_ATTR_RO(uuid);
157
158 static ssize_t eui_show(struct device *dev, struct device_attribute *attr,
159                 char *buf)
160 {
161         return sysfs_emit(buf, "%8ph\n", dev_to_ns_head(dev)->ids.eui64);
162 }
163 static DEVICE_ATTR_RO(eui);
164
165 static ssize_t nsid_show(struct device *dev, struct device_attribute *attr,
166                 char *buf)
167 {
168         return sysfs_emit(buf, "%d\n", dev_to_ns_head(dev)->ns_id);
169 }
170 static DEVICE_ATTR_RO(nsid);
171
172 static ssize_t csi_show(struct device *dev, struct device_attribute *attr,
173                 char *buf)
174 {
175         return sysfs_emit(buf, "%u\n", dev_to_ns_head(dev)->ids.csi);
176 }
177 static DEVICE_ATTR_RO(csi);
178
179 static ssize_t metadata_bytes_show(struct device *dev,
180                 struct device_attribute *attr, char *buf)
181 {
182         return sysfs_emit(buf, "%u\n", dev_to_ns_head(dev)->ms);
183 }
184 static DEVICE_ATTR_RO(metadata_bytes);
185
186 static int ns_head_update_nuse(struct nvme_ns_head *head)
187 {
188         struct nvme_id_ns *id;
189         struct nvme_ns *ns;
190         int srcu_idx, ret = -EWOULDBLOCK;
191
192         /* Avoid issuing commands too often by rate limiting the update */
193         if (!__ratelimit(&head->rs_nuse))
194                 return 0;
195
196         srcu_idx = srcu_read_lock(&head->srcu);
197         ns = nvme_find_path(head);
198         if (!ns)
199                 goto out_unlock;
200
201         ret = nvme_identify_ns(ns->ctrl, head->ns_id, &id);
202         if (ret)
203                 goto out_unlock;
204
205         head->nuse = le64_to_cpu(id->nuse);
206         kfree(id);
207
208 out_unlock:
209         srcu_read_unlock(&head->srcu, srcu_idx);
210         return ret;
211 }
212
213 static int ns_update_nuse(struct nvme_ns *ns)
214 {
215         struct nvme_id_ns *id;
216         int ret;
217
218         /* Avoid issuing commands too often by rate limiting the update. */
219         if (!__ratelimit(&ns->head->rs_nuse))
220                 return 0;
221
222         ret = nvme_identify_ns(ns->ctrl, ns->head->ns_id, &id);
223         if (ret)
224                 goto out_free_id;
225
226         ns->head->nuse = le64_to_cpu(id->nuse);
227
228 out_free_id:
229         kfree(id);
230
231         return ret;
232 }
233
234 static ssize_t nuse_show(struct device *dev, struct device_attribute *attr,
235                 char *buf)
236 {
237         struct nvme_ns_head *head = dev_to_ns_head(dev);
238         struct gendisk *disk = dev_to_disk(dev);
239         struct block_device *bdev = disk->part0;
240         int ret;
241
242         if (IS_ENABLED(CONFIG_NVME_MULTIPATH) &&
243             bdev->bd_disk->fops == &nvme_ns_head_ops)
244                 ret = ns_head_update_nuse(head);
245         else
246                 ret = ns_update_nuse(bdev->bd_disk->private_data);
247         if (ret)
248                 return ret;
249
250         return sysfs_emit(buf, "%llu\n", head->nuse);
251 }
252 static DEVICE_ATTR_RO(nuse);
253
254 static struct attribute *nvme_ns_attrs[] = {
255         &dev_attr_wwid.attr,
256         &dev_attr_uuid.attr,
257         &dev_attr_nguid.attr,
258         &dev_attr_eui.attr,
259         &dev_attr_csi.attr,
260         &dev_attr_nsid.attr,
261         &dev_attr_metadata_bytes.attr,
262         &dev_attr_nuse.attr,
263 #ifdef CONFIG_NVME_MULTIPATH
264         &dev_attr_ana_grpid.attr,
265         &dev_attr_ana_state.attr,
266 #endif
267         &dev_attr_io_passthru_err_log_enabled.attr,
268         NULL,
269 };
270
271 static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
272                 struct attribute *a, int n)
273 {
274         struct device *dev = container_of(kobj, struct device, kobj);
275         struct nvme_ns_ids *ids = &dev_to_ns_head(dev)->ids;
276
277         if (a == &dev_attr_uuid.attr) {
278                 if (uuid_is_null(&ids->uuid) &&
279                     !memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
280                         return 0;
281         }
282         if (a == &dev_attr_nguid.attr) {
283                 if (!memchr_inv(ids->nguid, 0, sizeof(ids->nguid)))
284                         return 0;
285         }
286         if (a == &dev_attr_eui.attr) {
287                 if (!memchr_inv(ids->eui64, 0, sizeof(ids->eui64)))
288                         return 0;
289         }
290 #ifdef CONFIG_NVME_MULTIPATH
291         if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) {
292                 /* per-path attr */
293                 if (nvme_disk_is_ns_head(dev_to_disk(dev)))
294                         return 0;
295                 if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
296                         return 0;
297         }
298 #endif
299         return a->mode;
300 }
301
302 static const struct attribute_group nvme_ns_attr_group = {
303         .attrs          = nvme_ns_attrs,
304         .is_visible     = nvme_ns_attrs_are_visible,
305 };
306
307 const struct attribute_group *nvme_ns_attr_groups[] = {
308         &nvme_ns_attr_group,
309         NULL,
310 };
311
312 #define nvme_show_str_function(field)                                           \
313 static ssize_t  field##_show(struct device *dev,                                \
314                             struct device_attribute *attr, char *buf)           \
315 {                                                                               \
316         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);                          \
317         return sysfs_emit(buf, "%.*s\n",                                        \
318                 (int)sizeof(ctrl->subsys->field), ctrl->subsys->field);         \
319 }                                                                               \
320 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
321
322 nvme_show_str_function(model);
323 nvme_show_str_function(serial);
324 nvme_show_str_function(firmware_rev);
325
326 #define nvme_show_int_function(field)                                           \
327 static ssize_t  field##_show(struct device *dev,                                \
328                             struct device_attribute *attr, char *buf)           \
329 {                                                                               \
330         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);                          \
331         return sysfs_emit(buf, "%d\n", ctrl->field);                            \
332 }                                                                               \
333 static DEVICE_ATTR(field, S_IRUGO, field##_show, NULL);
334
335 nvme_show_int_function(cntlid);
336 nvme_show_int_function(numa_node);
337 nvme_show_int_function(queue_count);
338 nvme_show_int_function(sqsize);
339 nvme_show_int_function(kato);
340
341 static ssize_t nvme_sysfs_delete(struct device *dev,
342                                 struct device_attribute *attr, const char *buf,
343                                 size_t count)
344 {
345         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
346
347         if (!test_bit(NVME_CTRL_STARTED_ONCE, &ctrl->flags))
348                 return -EBUSY;
349
350         if (device_remove_file_self(dev, attr))
351                 nvme_delete_ctrl_sync(ctrl);
352         return count;
353 }
354 static DEVICE_ATTR(delete_controller, S_IWUSR, NULL, nvme_sysfs_delete);
355
356 static ssize_t nvme_sysfs_show_transport(struct device *dev,
357                                          struct device_attribute *attr,
358                                          char *buf)
359 {
360         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
361
362         return sysfs_emit(buf, "%s\n", ctrl->ops->name);
363 }
364 static DEVICE_ATTR(transport, S_IRUGO, nvme_sysfs_show_transport, NULL);
365
366 static ssize_t nvme_sysfs_show_state(struct device *dev,
367                                      struct device_attribute *attr,
368                                      char *buf)
369 {
370         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
371         unsigned state = (unsigned)nvme_ctrl_state(ctrl);
372         static const char *const state_name[] = {
373                 [NVME_CTRL_NEW]         = "new",
374                 [NVME_CTRL_LIVE]        = "live",
375                 [NVME_CTRL_RESETTING]   = "resetting",
376                 [NVME_CTRL_CONNECTING]  = "connecting",
377                 [NVME_CTRL_DELETING]    = "deleting",
378                 [NVME_CTRL_DELETING_NOIO]= "deleting (no IO)",
379                 [NVME_CTRL_DEAD]        = "dead",
380         };
381
382         if (state < ARRAY_SIZE(state_name) && state_name[state])
383                 return sysfs_emit(buf, "%s\n", state_name[state]);
384
385         return sysfs_emit(buf, "unknown state\n");
386 }
387
388 static DEVICE_ATTR(state, S_IRUGO, nvme_sysfs_show_state, NULL);
389
390 static ssize_t nvme_sysfs_show_subsysnqn(struct device *dev,
391                                          struct device_attribute *attr,
392                                          char *buf)
393 {
394         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
395
396         return sysfs_emit(buf, "%s\n", ctrl->subsys->subnqn);
397 }
398 static DEVICE_ATTR(subsysnqn, S_IRUGO, nvme_sysfs_show_subsysnqn, NULL);
399
400 static ssize_t nvme_sysfs_show_hostnqn(struct device *dev,
401                                         struct device_attribute *attr,
402                                         char *buf)
403 {
404         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
405
406         return sysfs_emit(buf, "%s\n", ctrl->opts->host->nqn);
407 }
408 static DEVICE_ATTR(hostnqn, S_IRUGO, nvme_sysfs_show_hostnqn, NULL);
409
410 static ssize_t nvme_sysfs_show_hostid(struct device *dev,
411                                         struct device_attribute *attr,
412                                         char *buf)
413 {
414         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
415
416         return sysfs_emit(buf, "%pU\n", &ctrl->opts->host->id);
417 }
418 static DEVICE_ATTR(hostid, S_IRUGO, nvme_sysfs_show_hostid, NULL);
419
420 static ssize_t nvme_sysfs_show_address(struct device *dev,
421                                          struct device_attribute *attr,
422                                          char *buf)
423 {
424         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
425
426         return ctrl->ops->get_address(ctrl, buf, PAGE_SIZE);
427 }
428 static DEVICE_ATTR(address, S_IRUGO, nvme_sysfs_show_address, NULL);
429
430 static ssize_t nvme_ctrl_loss_tmo_show(struct device *dev,
431                 struct device_attribute *attr, char *buf)
432 {
433         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
434         struct nvmf_ctrl_options *opts = ctrl->opts;
435
436         if (ctrl->opts->max_reconnects == -1)
437                 return sysfs_emit(buf, "off\n");
438         return sysfs_emit(buf, "%d\n",
439                           opts->max_reconnects * opts->reconnect_delay);
440 }
441
442 static ssize_t nvme_ctrl_loss_tmo_store(struct device *dev,
443                 struct device_attribute *attr, const char *buf, size_t count)
444 {
445         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
446         struct nvmf_ctrl_options *opts = ctrl->opts;
447         int ctrl_loss_tmo, err;
448
449         err = kstrtoint(buf, 10, &ctrl_loss_tmo);
450         if (err)
451                 return -EINVAL;
452
453         if (ctrl_loss_tmo < 0)
454                 opts->max_reconnects = -1;
455         else
456                 opts->max_reconnects = DIV_ROUND_UP(ctrl_loss_tmo,
457                                                 opts->reconnect_delay);
458         return count;
459 }
460 static DEVICE_ATTR(ctrl_loss_tmo, S_IRUGO | S_IWUSR,
461         nvme_ctrl_loss_tmo_show, nvme_ctrl_loss_tmo_store);
462
463 static ssize_t nvme_ctrl_reconnect_delay_show(struct device *dev,
464                 struct device_attribute *attr, char *buf)
465 {
466         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
467
468         if (ctrl->opts->reconnect_delay == -1)
469                 return sysfs_emit(buf, "off\n");
470         return sysfs_emit(buf, "%d\n", ctrl->opts->reconnect_delay);
471 }
472
473 static ssize_t nvme_ctrl_reconnect_delay_store(struct device *dev,
474                 struct device_attribute *attr, const char *buf, size_t count)
475 {
476         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
477         unsigned int v;
478         int err;
479
480         err = kstrtou32(buf, 10, &v);
481         if (err)
482                 return err;
483
484         ctrl->opts->reconnect_delay = v;
485         return count;
486 }
487 static DEVICE_ATTR(reconnect_delay, S_IRUGO | S_IWUSR,
488         nvme_ctrl_reconnect_delay_show, nvme_ctrl_reconnect_delay_store);
489
490 static ssize_t nvme_ctrl_fast_io_fail_tmo_show(struct device *dev,
491                 struct device_attribute *attr, char *buf)
492 {
493         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
494
495         if (ctrl->opts->fast_io_fail_tmo == -1)
496                 return sysfs_emit(buf, "off\n");
497         return sysfs_emit(buf, "%d\n", ctrl->opts->fast_io_fail_tmo);
498 }
499
500 static ssize_t nvme_ctrl_fast_io_fail_tmo_store(struct device *dev,
501                 struct device_attribute *attr, const char *buf, size_t count)
502 {
503         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
504         struct nvmf_ctrl_options *opts = ctrl->opts;
505         int fast_io_fail_tmo, err;
506
507         err = kstrtoint(buf, 10, &fast_io_fail_tmo);
508         if (err)
509                 return -EINVAL;
510
511         if (fast_io_fail_tmo < 0)
512                 opts->fast_io_fail_tmo = -1;
513         else
514                 opts->fast_io_fail_tmo = fast_io_fail_tmo;
515         return count;
516 }
517 static DEVICE_ATTR(fast_io_fail_tmo, S_IRUGO | S_IWUSR,
518         nvme_ctrl_fast_io_fail_tmo_show, nvme_ctrl_fast_io_fail_tmo_store);
519
520 static ssize_t cntrltype_show(struct device *dev,
521                               struct device_attribute *attr, char *buf)
522 {
523         static const char * const type[] = {
524                 [NVME_CTRL_IO] = "io\n",
525                 [NVME_CTRL_DISC] = "discovery\n",
526                 [NVME_CTRL_ADMIN] = "admin\n",
527         };
528         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
529
530         if (ctrl->cntrltype > NVME_CTRL_ADMIN || !type[ctrl->cntrltype])
531                 return sysfs_emit(buf, "reserved\n");
532
533         return sysfs_emit(buf, type[ctrl->cntrltype]);
534 }
535 static DEVICE_ATTR_RO(cntrltype);
536
537 static ssize_t dctype_show(struct device *dev,
538                            struct device_attribute *attr, char *buf)
539 {
540         static const char * const type[] = {
541                 [NVME_DCTYPE_NOT_REPORTED] = "none\n",
542                 [NVME_DCTYPE_DDC] = "ddc\n",
543                 [NVME_DCTYPE_CDC] = "cdc\n",
544         };
545         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
546
547         if (ctrl->dctype > NVME_DCTYPE_CDC || !type[ctrl->dctype])
548                 return sysfs_emit(buf, "reserved\n");
549
550         return sysfs_emit(buf, type[ctrl->dctype]);
551 }
552 static DEVICE_ATTR_RO(dctype);
553
554 #ifdef CONFIG_NVME_HOST_AUTH
555 static ssize_t nvme_ctrl_dhchap_secret_show(struct device *dev,
556                 struct device_attribute *attr, char *buf)
557 {
558         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
559         struct nvmf_ctrl_options *opts = ctrl->opts;
560
561         if (!opts->dhchap_secret)
562                 return sysfs_emit(buf, "none\n");
563         return sysfs_emit(buf, "%s\n", opts->dhchap_secret);
564 }
565
566 static ssize_t nvme_ctrl_dhchap_secret_store(struct device *dev,
567                 struct device_attribute *attr, const char *buf, size_t count)
568 {
569         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
570         struct nvmf_ctrl_options *opts = ctrl->opts;
571         char *dhchap_secret;
572
573         if (!ctrl->opts->dhchap_secret)
574                 return -EINVAL;
575         if (count < 7)
576                 return -EINVAL;
577         if (memcmp(buf, "DHHC-1:", 7))
578                 return -EINVAL;
579
580         dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
581         if (!dhchap_secret)
582                 return -ENOMEM;
583         memcpy(dhchap_secret, buf, count);
584         nvme_auth_stop(ctrl);
585         if (strcmp(dhchap_secret, opts->dhchap_secret)) {
586                 struct nvme_dhchap_key *key, *host_key;
587                 int ret;
588
589                 ret = nvme_auth_generate_key(dhchap_secret, &key);
590                 if (ret) {
591                         kfree(dhchap_secret);
592                         return ret;
593                 }
594                 kfree(opts->dhchap_secret);
595                 opts->dhchap_secret = dhchap_secret;
596                 host_key = ctrl->host_key;
597                 mutex_lock(&ctrl->dhchap_auth_mutex);
598                 ctrl->host_key = key;
599                 mutex_unlock(&ctrl->dhchap_auth_mutex);
600                 nvme_auth_free_key(host_key);
601         } else
602                 kfree(dhchap_secret);
603         /* Start re-authentication */
604         dev_info(ctrl->device, "re-authenticating controller\n");
605         queue_work(nvme_wq, &ctrl->dhchap_auth_work);
606
607         return count;
608 }
609
610 static DEVICE_ATTR(dhchap_secret, S_IRUGO | S_IWUSR,
611         nvme_ctrl_dhchap_secret_show, nvme_ctrl_dhchap_secret_store);
612
613 static ssize_t nvme_ctrl_dhchap_ctrl_secret_show(struct device *dev,
614                 struct device_attribute *attr, char *buf)
615 {
616         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
617         struct nvmf_ctrl_options *opts = ctrl->opts;
618
619         if (!opts->dhchap_ctrl_secret)
620                 return sysfs_emit(buf, "none\n");
621         return sysfs_emit(buf, "%s\n", opts->dhchap_ctrl_secret);
622 }
623
624 static ssize_t nvme_ctrl_dhchap_ctrl_secret_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         struct nvmf_ctrl_options *opts = ctrl->opts;
629         char *dhchap_secret;
630
631         if (!ctrl->opts->dhchap_ctrl_secret)
632                 return -EINVAL;
633         if (count < 7)
634                 return -EINVAL;
635         if (memcmp(buf, "DHHC-1:", 7))
636                 return -EINVAL;
637
638         dhchap_secret = kzalloc(count + 1, GFP_KERNEL);
639         if (!dhchap_secret)
640                 return -ENOMEM;
641         memcpy(dhchap_secret, buf, count);
642         nvme_auth_stop(ctrl);
643         if (strcmp(dhchap_secret, opts->dhchap_ctrl_secret)) {
644                 struct nvme_dhchap_key *key, *ctrl_key;
645                 int ret;
646
647                 ret = nvme_auth_generate_key(dhchap_secret, &key);
648                 if (ret) {
649                         kfree(dhchap_secret);
650                         return ret;
651                 }
652                 kfree(opts->dhchap_ctrl_secret);
653                 opts->dhchap_ctrl_secret = dhchap_secret;
654                 ctrl_key = ctrl->ctrl_key;
655                 mutex_lock(&ctrl->dhchap_auth_mutex);
656                 ctrl->ctrl_key = key;
657                 mutex_unlock(&ctrl->dhchap_auth_mutex);
658                 nvme_auth_free_key(ctrl_key);
659         } else
660                 kfree(dhchap_secret);
661         /* Start re-authentication */
662         dev_info(ctrl->device, "re-authenticating controller\n");
663         queue_work(nvme_wq, &ctrl->dhchap_auth_work);
664
665         return count;
666 }
667
668 static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR,
669         nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);
670 #endif
671
672 #ifdef CONFIG_NVME_TCP_TLS
673 static ssize_t tls_key_show(struct device *dev,
674                             struct device_attribute *attr, char *buf)
675 {
676         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
677
678         if (!ctrl->tls_key)
679                 return 0;
680         return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key));
681 }
682 static DEVICE_ATTR_RO(tls_key);
683 #endif
684
685 static struct attribute *nvme_dev_attrs[] = {
686         &dev_attr_reset_controller.attr,
687         &dev_attr_rescan_controller.attr,
688         &dev_attr_model.attr,
689         &dev_attr_serial.attr,
690         &dev_attr_firmware_rev.attr,
691         &dev_attr_cntlid.attr,
692         &dev_attr_delete_controller.attr,
693         &dev_attr_transport.attr,
694         &dev_attr_subsysnqn.attr,
695         &dev_attr_address.attr,
696         &dev_attr_state.attr,
697         &dev_attr_numa_node.attr,
698         &dev_attr_queue_count.attr,
699         &dev_attr_sqsize.attr,
700         &dev_attr_hostnqn.attr,
701         &dev_attr_hostid.attr,
702         &dev_attr_ctrl_loss_tmo.attr,
703         &dev_attr_reconnect_delay.attr,
704         &dev_attr_fast_io_fail_tmo.attr,
705         &dev_attr_kato.attr,
706         &dev_attr_cntrltype.attr,
707         &dev_attr_dctype.attr,
708 #ifdef CONFIG_NVME_HOST_AUTH
709         &dev_attr_dhchap_secret.attr,
710         &dev_attr_dhchap_ctrl_secret.attr,
711 #endif
712 #ifdef CONFIG_NVME_TCP_TLS
713         &dev_attr_tls_key.attr,
714 #endif
715         &dev_attr_adm_passthru_err_log_enabled.attr,
716         NULL
717 };
718
719 static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,
720                 struct attribute *a, int n)
721 {
722         struct device *dev = container_of(kobj, struct device, kobj);
723         struct nvme_ctrl *ctrl = dev_get_drvdata(dev);
724
725         if (a == &dev_attr_delete_controller.attr && !ctrl->ops->delete_ctrl)
726                 return 0;
727         if (a == &dev_attr_address.attr && !ctrl->ops->get_address)
728                 return 0;
729         if (a == &dev_attr_hostnqn.attr && !ctrl->opts)
730                 return 0;
731         if (a == &dev_attr_hostid.attr && !ctrl->opts)
732                 return 0;
733         if (a == &dev_attr_ctrl_loss_tmo.attr && !ctrl->opts)
734                 return 0;
735         if (a == &dev_attr_reconnect_delay.attr && !ctrl->opts)
736                 return 0;
737         if (a == &dev_attr_fast_io_fail_tmo.attr && !ctrl->opts)
738                 return 0;
739 #ifdef CONFIG_NVME_HOST_AUTH
740         if (a == &dev_attr_dhchap_secret.attr && !ctrl->opts)
741                 return 0;
742         if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)
743                 return 0;
744 #endif
745 #ifdef CONFIG_NVME_TCP_TLS
746         if (a == &dev_attr_tls_key.attr &&
747             (!ctrl->opts || strcmp(ctrl->opts->transport, "tcp")))
748                 return 0;
749 #endif
750
751         return a->mode;
752 }
753
754 const struct attribute_group nvme_dev_attrs_group = {
755         .attrs          = nvme_dev_attrs,
756         .is_visible     = nvme_dev_attrs_are_visible,
757 };
758 EXPORT_SYMBOL_GPL(nvme_dev_attrs_group);
759
760 const struct attribute_group *nvme_dev_attr_groups[] = {
761         &nvme_dev_attrs_group,
762         NULL,
763 };
764
765 #define SUBSYS_ATTR_RO(_name, _mode, _show)                     \
766         struct device_attribute subsys_attr_##_name = \
767                 __ATTR(_name, _mode, _show, NULL)
768
769 static ssize_t nvme_subsys_show_nqn(struct device *dev,
770                                     struct device_attribute *attr,
771                                     char *buf)
772 {
773         struct nvme_subsystem *subsys =
774                 container_of(dev, struct nvme_subsystem, dev);
775
776         return sysfs_emit(buf, "%s\n", subsys->subnqn);
777 }
778 static SUBSYS_ATTR_RO(subsysnqn, S_IRUGO, nvme_subsys_show_nqn);
779
780 static ssize_t nvme_subsys_show_type(struct device *dev,
781                                     struct device_attribute *attr,
782                                     char *buf)
783 {
784         struct nvme_subsystem *subsys =
785                 container_of(dev, struct nvme_subsystem, dev);
786
787         switch (subsys->subtype) {
788         case NVME_NQN_DISC:
789                 return sysfs_emit(buf, "discovery\n");
790         case NVME_NQN_NVME:
791                 return sysfs_emit(buf, "nvm\n");
792         default:
793                 return sysfs_emit(buf, "reserved\n");
794         }
795 }
796 static SUBSYS_ATTR_RO(subsystype, S_IRUGO, nvme_subsys_show_type);
797
798 #define nvme_subsys_show_str_function(field)                            \
799 static ssize_t subsys_##field##_show(struct device *dev,                \
800                             struct device_attribute *attr, char *buf)   \
801 {                                                                       \
802         struct nvme_subsystem *subsys =                                 \
803                 container_of(dev, struct nvme_subsystem, dev);          \
804         return sysfs_emit(buf, "%.*s\n",                                \
805                            (int)sizeof(subsys->field), subsys->field);  \
806 }                                                                       \
807 static SUBSYS_ATTR_RO(field, S_IRUGO, subsys_##field##_show);
808
809 nvme_subsys_show_str_function(model);
810 nvme_subsys_show_str_function(serial);
811 nvme_subsys_show_str_function(firmware_rev);
812
813 static struct attribute *nvme_subsys_attrs[] = {
814         &subsys_attr_model.attr,
815         &subsys_attr_serial.attr,
816         &subsys_attr_firmware_rev.attr,
817         &subsys_attr_subsysnqn.attr,
818         &subsys_attr_subsystype.attr,
819 #ifdef CONFIG_NVME_MULTIPATH
820         &subsys_attr_iopolicy.attr,
821 #endif
822         NULL,
823 };
824
825 static const struct attribute_group nvme_subsys_attrs_group = {
826         .attrs = nvme_subsys_attrs,
827 };
828
829 const struct attribute_group *nvme_subsys_attrs_groups[] = {
830         &nvme_subsys_attrs_group,
831         NULL,
832 };