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