Linux 4.16.11
[linux/fpc-iii.git] / drivers / lightnvm / pblk-sysfs.c
blob620bab853579c884109c95429d6ad1b84f871517
1 /*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * Implementation of a physical block-device target for Open-channel SSDs.
17 * pblk-sysfs.c - pblk's sysfs
21 #include "pblk.h"
23 static ssize_t pblk_sysfs_luns_show(struct pblk *pblk, char *page)
25 struct nvm_tgt_dev *dev = pblk->dev;
26 struct nvm_geo *geo = &dev->geo;
27 struct pblk_lun *rlun;
28 ssize_t sz = 0;
29 int i;
31 for (i = 0; i < geo->all_luns; i++) {
32 int active = 1;
34 rlun = &pblk->luns[i];
35 if (!down_trylock(&rlun->wr_sem)) {
36 active = 0;
37 up(&rlun->wr_sem);
39 sz += snprintf(page + sz, PAGE_SIZE - sz,
40 "pblk: pos:%d, ch:%d, lun:%d - %d\n",
42 rlun->bppa.g.ch,
43 rlun->bppa.g.lun,
44 active);
47 return sz;
50 static ssize_t pblk_sysfs_rate_limiter(struct pblk *pblk, char *page)
52 int free_blocks, free_user_blocks, total_blocks;
53 int rb_user_max, rb_user_cnt;
54 int rb_gc_max, rb_gc_cnt, rb_budget, rb_state;
56 free_blocks = pblk_rl_nr_free_blks(&pblk->rl);
57 free_user_blocks = pblk_rl_nr_user_free_blks(&pblk->rl);
58 rb_user_max = pblk->rl.rb_user_max;
59 rb_user_cnt = atomic_read(&pblk->rl.rb_user_cnt);
60 rb_gc_max = pblk->rl.rb_gc_max;
61 rb_gc_cnt = atomic_read(&pblk->rl.rb_gc_cnt);
62 rb_budget = pblk->rl.rb_budget;
63 rb_state = pblk->rl.rb_state;
65 total_blocks = pblk->rl.total_blocks;
67 return snprintf(page, PAGE_SIZE,
68 "u:%u/%u,gc:%u/%u(%u)(stop:<%u,full:>%u,free:%d/%d/%d)-%d\n",
69 rb_user_cnt,
70 rb_user_max,
71 rb_gc_cnt,
72 rb_gc_max,
73 rb_state,
74 rb_budget,
75 pblk->rl.high,
76 free_blocks,
77 free_user_blocks,
78 total_blocks,
79 READ_ONCE(pblk->rl.rb_user_active));
82 static ssize_t pblk_sysfs_gc_state_show(struct pblk *pblk, char *page)
84 int gc_enabled, gc_active;
86 pblk_gc_sysfs_state_show(pblk, &gc_enabled, &gc_active);
87 return snprintf(page, PAGE_SIZE, "gc_enabled=%d, gc_active=%d\n",
88 gc_enabled, gc_active);
91 static ssize_t pblk_sysfs_stats(struct pblk *pblk, char *page)
93 ssize_t sz;
95 sz = snprintf(page, PAGE_SIZE,
96 "read_failed=%lu, read_high_ecc=%lu, read_empty=%lu, read_failed_gc=%lu, write_failed=%lu, erase_failed=%lu\n",
97 atomic_long_read(&pblk->read_failed),
98 atomic_long_read(&pblk->read_high_ecc),
99 atomic_long_read(&pblk->read_empty),
100 atomic_long_read(&pblk->read_failed_gc),
101 atomic_long_read(&pblk->write_failed),
102 atomic_long_read(&pblk->erase_failed));
104 return sz;
107 static ssize_t pblk_sysfs_write_buffer(struct pblk *pblk, char *page)
109 return pblk_rb_sysfs(&pblk->rwb, page);
112 static ssize_t pblk_sysfs_ppaf(struct pblk *pblk, char *page)
114 struct nvm_tgt_dev *dev = pblk->dev;
115 struct nvm_geo *geo = &dev->geo;
116 ssize_t sz = 0;
118 sz = snprintf(page, PAGE_SIZE - sz,
119 "g:(b:%d)blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
120 pblk->ppaf_bitsize,
121 pblk->ppaf.blk_offset, geo->ppaf.blk_len,
122 pblk->ppaf.pg_offset, geo->ppaf.pg_len,
123 pblk->ppaf.lun_offset, geo->ppaf.lun_len,
124 pblk->ppaf.ch_offset, geo->ppaf.ch_len,
125 pblk->ppaf.pln_offset, geo->ppaf.pln_len,
126 pblk->ppaf.sec_offset, geo->ppaf.sect_len);
128 sz += snprintf(page + sz, PAGE_SIZE - sz,
129 "d:blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
130 geo->ppaf.blk_offset, geo->ppaf.blk_len,
131 geo->ppaf.pg_offset, geo->ppaf.pg_len,
132 geo->ppaf.lun_offset, geo->ppaf.lun_len,
133 geo->ppaf.ch_offset, geo->ppaf.ch_len,
134 geo->ppaf.pln_offset, geo->ppaf.pln_len,
135 geo->ppaf.sect_offset, geo->ppaf.sect_len);
137 return sz;
140 static ssize_t pblk_sysfs_lines(struct pblk *pblk, char *page)
142 struct nvm_tgt_dev *dev = pblk->dev;
143 struct nvm_geo *geo = &dev->geo;
144 struct pblk_line_meta *lm = &pblk->lm;
145 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
146 struct pblk_line *line;
147 ssize_t sz = 0;
148 int nr_free_lines;
149 int cur_data, cur_log;
150 int free_line_cnt = 0, closed_line_cnt = 0, emeta_line_cnt = 0;
151 int d_line_cnt = 0, l_line_cnt = 0;
152 int gc_full = 0, gc_high = 0, gc_mid = 0, gc_low = 0, gc_empty = 0;
153 int bad = 0, cor = 0;
154 int msecs = 0, cur_sec = 0, vsc = 0, sec_in_line = 0;
155 int map_weight = 0, meta_weight = 0;
157 spin_lock(&l_mg->free_lock);
158 cur_data = (l_mg->data_line) ? l_mg->data_line->id : -1;
159 cur_log = (l_mg->log_line) ? l_mg->log_line->id : -1;
160 nr_free_lines = l_mg->nr_free_lines;
162 list_for_each_entry(line, &l_mg->free_list, list)
163 free_line_cnt++;
164 spin_unlock(&l_mg->free_lock);
166 spin_lock(&l_mg->close_lock);
167 list_for_each_entry(line, &l_mg->emeta_list, list)
168 emeta_line_cnt++;
169 spin_unlock(&l_mg->close_lock);
171 spin_lock(&l_mg->gc_lock);
172 list_for_each_entry(line, &l_mg->gc_full_list, list) {
173 if (line->type == PBLK_LINETYPE_DATA)
174 d_line_cnt++;
175 else if (line->type == PBLK_LINETYPE_LOG)
176 l_line_cnt++;
177 closed_line_cnt++;
178 gc_full++;
181 list_for_each_entry(line, &l_mg->gc_high_list, list) {
182 if (line->type == PBLK_LINETYPE_DATA)
183 d_line_cnt++;
184 else if (line->type == PBLK_LINETYPE_LOG)
185 l_line_cnt++;
186 closed_line_cnt++;
187 gc_high++;
190 list_for_each_entry(line, &l_mg->gc_mid_list, list) {
191 if (line->type == PBLK_LINETYPE_DATA)
192 d_line_cnt++;
193 else if (line->type == PBLK_LINETYPE_LOG)
194 l_line_cnt++;
195 closed_line_cnt++;
196 gc_mid++;
199 list_for_each_entry(line, &l_mg->gc_low_list, list) {
200 if (line->type == PBLK_LINETYPE_DATA)
201 d_line_cnt++;
202 else if (line->type == PBLK_LINETYPE_LOG)
203 l_line_cnt++;
204 closed_line_cnt++;
205 gc_low++;
208 list_for_each_entry(line, &l_mg->gc_empty_list, list) {
209 if (line->type == PBLK_LINETYPE_DATA)
210 d_line_cnt++;
211 else if (line->type == PBLK_LINETYPE_LOG)
212 l_line_cnt++;
213 closed_line_cnt++;
214 gc_empty++;
217 list_for_each_entry(line, &l_mg->bad_list, list)
218 bad++;
219 list_for_each_entry(line, &l_mg->corrupt_list, list)
220 cor++;
221 spin_unlock(&l_mg->gc_lock);
223 spin_lock(&l_mg->free_lock);
224 if (l_mg->data_line) {
225 cur_sec = l_mg->data_line->cur_sec;
226 msecs = l_mg->data_line->left_msecs;
227 vsc = le32_to_cpu(*l_mg->data_line->vsc);
228 sec_in_line = l_mg->data_line->sec_in_line;
229 meta_weight = bitmap_weight(&l_mg->meta_bitmap,
230 PBLK_DATA_LINES);
231 map_weight = bitmap_weight(l_mg->data_line->map_bitmap,
232 lm->sec_per_line);
234 spin_unlock(&l_mg->free_lock);
236 if (nr_free_lines != free_line_cnt)
237 pr_err("pblk: corrupted free line list:%d/%d\n",
238 nr_free_lines, free_line_cnt);
240 sz = snprintf(page, PAGE_SIZE - sz,
241 "line: nluns:%d, nblks:%d, nsecs:%d\n",
242 geo->all_luns, lm->blk_per_line, lm->sec_per_line);
244 sz += snprintf(page + sz, PAGE_SIZE - sz,
245 "lines:d:%d,l:%d-f:%d,m:%d/%d,c:%d,b:%d,co:%d(d:%d,l:%d)t:%d\n",
246 cur_data, cur_log,
247 nr_free_lines,
248 emeta_line_cnt, meta_weight,
249 closed_line_cnt,
250 bad, cor,
251 d_line_cnt, l_line_cnt,
252 l_mg->nr_lines);
254 sz += snprintf(page + sz, PAGE_SIZE - sz,
255 "GC: full:%d, high:%d, mid:%d, low:%d, empty:%d, queue:%d\n",
256 gc_full, gc_high, gc_mid, gc_low, gc_empty,
257 atomic_read(&pblk->gc.read_inflight_gc));
259 sz += snprintf(page + sz, PAGE_SIZE - sz,
260 "data (%d) cur:%d, left:%d, vsc:%d, s:%d, map:%d/%d (%d)\n",
261 cur_data, cur_sec, msecs, vsc, sec_in_line,
262 map_weight, lm->sec_per_line,
263 atomic_read(&pblk->inflight_io));
265 return sz;
268 static ssize_t pblk_sysfs_lines_info(struct pblk *pblk, char *page)
270 struct nvm_tgt_dev *dev = pblk->dev;
271 struct nvm_geo *geo = &dev->geo;
272 struct pblk_line_meta *lm = &pblk->lm;
273 ssize_t sz = 0;
275 sz = snprintf(page, PAGE_SIZE - sz,
276 "smeta - len:%d, secs:%d\n",
277 lm->smeta_len, lm->smeta_sec);
278 sz += snprintf(page + sz, PAGE_SIZE - sz,
279 "emeta - len:%d, sec:%d, bb_start:%d\n",
280 lm->emeta_len[0], lm->emeta_sec[0],
281 lm->emeta_bb);
282 sz += snprintf(page + sz, PAGE_SIZE - sz,
283 "bitmap lengths: sec:%d, blk:%d, lun:%d\n",
284 lm->sec_bitmap_len,
285 lm->blk_bitmap_len,
286 lm->lun_bitmap_len);
287 sz += snprintf(page + sz, PAGE_SIZE - sz,
288 "blk_line:%d, sec_line:%d, sec_blk:%d\n",
289 lm->blk_per_line,
290 lm->sec_per_line,
291 geo->sec_per_chk);
293 return sz;
296 static ssize_t pblk_sysfs_get_sec_per_write(struct pblk *pblk, char *page)
298 return snprintf(page, PAGE_SIZE, "%d\n", pblk->sec_per_write);
301 #ifdef CONFIG_NVM_DEBUG
302 static ssize_t pblk_sysfs_stats_debug(struct pblk *pblk, char *page)
304 return snprintf(page, PAGE_SIZE,
305 "%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\n",
306 atomic_long_read(&pblk->inflight_writes),
307 atomic_long_read(&pblk->inflight_reads),
308 atomic_long_read(&pblk->req_writes),
309 atomic_long_read(&pblk->nr_flush),
310 atomic_long_read(&pblk->padded_writes),
311 atomic_long_read(&pblk->padded_wb),
312 atomic_long_read(&pblk->sub_writes),
313 atomic_long_read(&pblk->sync_writes),
314 atomic_long_read(&pblk->recov_writes),
315 atomic_long_read(&pblk->recov_gc_writes),
316 atomic_long_read(&pblk->recov_gc_reads),
317 atomic_long_read(&pblk->cache_reads),
318 atomic_long_read(&pblk->sync_reads));
320 #endif
322 static ssize_t pblk_sysfs_gc_force(struct pblk *pblk, const char *page,
323 size_t len)
325 size_t c_len;
326 int force;
328 c_len = strcspn(page, "\n");
329 if (c_len >= len)
330 return -EINVAL;
332 if (kstrtouint(page, 0, &force))
333 return -EINVAL;
335 pblk_gc_sysfs_force(pblk, force);
337 return len;
340 static ssize_t pblk_sysfs_set_sec_per_write(struct pblk *pblk,
341 const char *page, size_t len)
343 size_t c_len;
344 int sec_per_write;
346 c_len = strcspn(page, "\n");
347 if (c_len >= len)
348 return -EINVAL;
350 if (kstrtouint(page, 0, &sec_per_write))
351 return -EINVAL;
353 if (sec_per_write < pblk->min_write_pgs
354 || sec_per_write > pblk->max_write_pgs
355 || sec_per_write % pblk->min_write_pgs != 0)
356 return -EINVAL;
358 pblk_set_sec_per_write(pblk, sec_per_write);
360 return len;
363 static struct attribute sys_write_luns = {
364 .name = "write_luns",
365 .mode = 0444,
368 static struct attribute sys_rate_limiter_attr = {
369 .name = "rate_limiter",
370 .mode = 0444,
373 static struct attribute sys_gc_state = {
374 .name = "gc_state",
375 .mode = 0444,
378 static struct attribute sys_errors_attr = {
379 .name = "errors",
380 .mode = 0444,
383 static struct attribute sys_rb_attr = {
384 .name = "write_buffer",
385 .mode = 0444,
388 static struct attribute sys_stats_ppaf_attr = {
389 .name = "ppa_format",
390 .mode = 0444,
393 static struct attribute sys_lines_attr = {
394 .name = "lines",
395 .mode = 0444,
398 static struct attribute sys_lines_info_attr = {
399 .name = "lines_info",
400 .mode = 0444,
403 static struct attribute sys_gc_force = {
404 .name = "gc_force",
405 .mode = 0200,
408 static struct attribute sys_max_sec_per_write = {
409 .name = "max_sec_per_write",
410 .mode = 0644,
413 #ifdef CONFIG_NVM_DEBUG
414 static struct attribute sys_stats_debug_attr = {
415 .name = "stats",
416 .mode = 0444,
418 #endif
420 static struct attribute *pblk_attrs[] = {
421 &sys_write_luns,
422 &sys_rate_limiter_attr,
423 &sys_errors_attr,
424 &sys_gc_state,
425 &sys_gc_force,
426 &sys_max_sec_per_write,
427 &sys_rb_attr,
428 &sys_stats_ppaf_attr,
429 &sys_lines_attr,
430 &sys_lines_info_attr,
431 #ifdef CONFIG_NVM_DEBUG
432 &sys_stats_debug_attr,
433 #endif
434 NULL,
437 static ssize_t pblk_sysfs_show(struct kobject *kobj, struct attribute *attr,
438 char *buf)
440 struct pblk *pblk = container_of(kobj, struct pblk, kobj);
442 if (strcmp(attr->name, "rate_limiter") == 0)
443 return pblk_sysfs_rate_limiter(pblk, buf);
444 else if (strcmp(attr->name, "write_luns") == 0)
445 return pblk_sysfs_luns_show(pblk, buf);
446 else if (strcmp(attr->name, "gc_state") == 0)
447 return pblk_sysfs_gc_state_show(pblk, buf);
448 else if (strcmp(attr->name, "errors") == 0)
449 return pblk_sysfs_stats(pblk, buf);
450 else if (strcmp(attr->name, "write_buffer") == 0)
451 return pblk_sysfs_write_buffer(pblk, buf);
452 else if (strcmp(attr->name, "ppa_format") == 0)
453 return pblk_sysfs_ppaf(pblk, buf);
454 else if (strcmp(attr->name, "lines") == 0)
455 return pblk_sysfs_lines(pblk, buf);
456 else if (strcmp(attr->name, "lines_info") == 0)
457 return pblk_sysfs_lines_info(pblk, buf);
458 else if (strcmp(attr->name, "max_sec_per_write") == 0)
459 return pblk_sysfs_get_sec_per_write(pblk, buf);
460 #ifdef CONFIG_NVM_DEBUG
461 else if (strcmp(attr->name, "stats") == 0)
462 return pblk_sysfs_stats_debug(pblk, buf);
463 #endif
464 return 0;
467 static ssize_t pblk_sysfs_store(struct kobject *kobj, struct attribute *attr,
468 const char *buf, size_t len)
470 struct pblk *pblk = container_of(kobj, struct pblk, kobj);
472 if (strcmp(attr->name, "gc_force") == 0)
473 return pblk_sysfs_gc_force(pblk, buf, len);
474 else if (strcmp(attr->name, "max_sec_per_write") == 0)
475 return pblk_sysfs_set_sec_per_write(pblk, buf, len);
477 return 0;
480 static const struct sysfs_ops pblk_sysfs_ops = {
481 .show = pblk_sysfs_show,
482 .store = pblk_sysfs_store,
485 static struct kobj_type pblk_ktype = {
486 .sysfs_ops = &pblk_sysfs_ops,
487 .default_attrs = pblk_attrs,
490 int pblk_sysfs_init(struct gendisk *tdisk)
492 struct pblk *pblk = tdisk->private_data;
493 struct device *parent_dev = disk_to_dev(pblk->disk);
494 int ret;
496 ret = kobject_init_and_add(&pblk->kobj, &pblk_ktype,
497 kobject_get(&parent_dev->kobj),
498 "%s", "pblk");
499 if (ret) {
500 pr_err("pblk: could not register %s/pblk\n",
501 tdisk->disk_name);
502 return ret;
505 kobject_uevent(&pblk->kobj, KOBJ_ADD);
506 return 0;
509 void pblk_sysfs_exit(struct gendisk *tdisk)
511 struct pblk *pblk = tdisk->private_data;
513 kobject_uevent(&pblk->kobj, KOBJ_REMOVE);
514 kobject_del(&pblk->kobj);
515 kobject_put(&pblk->kobj);