]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/lightnvm/pblk-sysfs.c
Merge tag 'backlight-next-4.12' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-bionic-kernel.git] / drivers / lightnvm / pblk-sysfs.c
1 /*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
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.
9 *
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.
14 *
15 * Implementation of a physical block-device target for Open-channel SSDs.
16 *
17 * pblk-sysfs.c - pblk's sysfs
18 *
19 */
20
21 #include "pblk.h"
22
23 static ssize_t pblk_sysfs_luns_show(struct pblk *pblk, char *page)
24 {
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;
30
31 for (i = 0; i < geo->nr_luns; i++) {
32 int active = 1;
33
34 rlun = &pblk->luns[i];
35 if (!down_trylock(&rlun->wr_sem)) {
36 active = 0;
37 up(&rlun->wr_sem);
38 }
39 sz += snprintf(page + sz, PAGE_SIZE - sz,
40 "pblk: pos:%d, ch:%d, lun:%d - %d\n",
41 i,
42 rlun->bppa.g.ch,
43 rlun->bppa.g.lun,
44 active);
45 }
46
47 return sz;
48 }
49
50 static ssize_t pblk_sysfs_rate_limiter(struct pblk *pblk, char *page)
51 {
52 struct nvm_tgt_dev *dev = pblk->dev;
53 struct nvm_geo *geo = &dev->geo;
54 int free_blocks, total_blocks;
55 int rb_user_max, rb_user_cnt;
56 int rb_gc_max, rb_gc_rsv, rb_gc_cnt, rb_budget, rb_state;
57
58 free_blocks = atomic_read(&pblk->rl.free_blocks);
59 rb_user_max = pblk->rl.rb_user_max;
60 rb_user_cnt = atomic_read(&pblk->rl.rb_user_cnt);
61 rb_gc_max = pblk->rl.rb_gc_max;
62 rb_gc_rsv = pblk->rl.rb_gc_rsv;
63 rb_gc_cnt = atomic_read(&pblk->rl.rb_gc_cnt);
64 rb_budget = pblk->rl.rb_budget;
65 rb_state = pblk->rl.rb_state;
66
67 total_blocks = geo->blks_per_lun * geo->nr_luns;
68
69 return snprintf(page, PAGE_SIZE,
70 "u:%u/%u,gc:%u/%u/%u(%u/%u)(stop:<%u,full:>%u,free:%d/%d)-%d\n",
71 rb_user_cnt,
72 rb_user_max,
73 rb_gc_cnt,
74 rb_gc_max,
75 rb_gc_rsv,
76 rb_state,
77 rb_budget,
78 pblk->rl.low,
79 pblk->rl.high,
80 free_blocks,
81 total_blocks,
82 READ_ONCE(pblk->rl.rb_user_active));
83 }
84
85 static ssize_t pblk_sysfs_gc_state_show(struct pblk *pblk, char *page)
86 {
87 int gc_enabled, gc_active;
88
89 pblk_gc_sysfs_state_show(pblk, &gc_enabled, &gc_active);
90 return snprintf(page, PAGE_SIZE, "gc_enabled=%d, gc_active=%d\n",
91 gc_enabled, gc_active);
92 }
93
94 static ssize_t pblk_sysfs_stats(struct pblk *pblk, char *page)
95 {
96 ssize_t sz;
97
98 sz = snprintf(page, PAGE_SIZE,
99 "read_failed=%lu, read_high_ecc=%lu, read_empty=%lu, read_failed_gc=%lu, write_failed=%lu, erase_failed=%lu\n",
100 atomic_long_read(&pblk->read_failed),
101 atomic_long_read(&pblk->read_high_ecc),
102 atomic_long_read(&pblk->read_empty),
103 atomic_long_read(&pblk->read_failed_gc),
104 atomic_long_read(&pblk->write_failed),
105 atomic_long_read(&pblk->erase_failed));
106
107 return sz;
108 }
109
110 static ssize_t pblk_sysfs_write_buffer(struct pblk *pblk, char *page)
111 {
112 return pblk_rb_sysfs(&pblk->rwb, page);
113 }
114
115 static ssize_t pblk_sysfs_ppaf(struct pblk *pblk, char *page)
116 {
117 struct nvm_tgt_dev *dev = pblk->dev;
118 struct nvm_geo *geo = &dev->geo;
119 ssize_t sz = 0;
120
121 sz = snprintf(page, PAGE_SIZE - sz,
122 "g:(b:%d)blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
123 pblk->ppaf_bitsize,
124 pblk->ppaf.blk_offset, geo->ppaf.blk_len,
125 pblk->ppaf.pg_offset, geo->ppaf.pg_len,
126 pblk->ppaf.lun_offset, geo->ppaf.lun_len,
127 pblk->ppaf.ch_offset, geo->ppaf.ch_len,
128 pblk->ppaf.pln_offset, geo->ppaf.pln_len,
129 pblk->ppaf.sec_offset, geo->ppaf.sect_len);
130
131 sz += snprintf(page + sz, PAGE_SIZE - sz,
132 "d:blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
133 geo->ppaf.blk_offset, geo->ppaf.blk_len,
134 geo->ppaf.pg_offset, geo->ppaf.pg_len,
135 geo->ppaf.lun_offset, geo->ppaf.lun_len,
136 geo->ppaf.ch_offset, geo->ppaf.ch_len,
137 geo->ppaf.pln_offset, geo->ppaf.pln_len,
138 geo->ppaf.sect_offset, geo->ppaf.sect_len);
139
140 return sz;
141 }
142
143 static ssize_t pblk_sysfs_lines(struct pblk *pblk, char *page)
144 {
145 struct nvm_tgt_dev *dev = pblk->dev;
146 struct nvm_geo *geo = &dev->geo;
147 struct pblk_line_meta *lm = &pblk->lm;
148 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
149 struct pblk_line *line;
150 ssize_t sz = 0;
151 int nr_free_lines;
152 int cur_data, cur_log;
153 int free_line_cnt = 0, closed_line_cnt = 0;
154 int d_line_cnt = 0, l_line_cnt = 0;
155 int gc_full = 0, gc_high = 0, gc_mid = 0, gc_low = 0, gc_empty = 0;
156 int free = 0, bad = 0, cor = 0;
157 int msecs = 0, ssecs = 0, cur_sec = 0, vsc = 0, sec_in_line = 0;
158 int map_weight = 0, meta_weight = 0;
159
160 spin_lock(&l_mg->free_lock);
161 cur_data = (l_mg->data_line) ? l_mg->data_line->id : -1;
162 cur_log = (l_mg->log_line) ? l_mg->log_line->id : -1;
163 nr_free_lines = l_mg->nr_free_lines;
164
165 list_for_each_entry(line, &l_mg->free_list, list)
166 free_line_cnt++;
167 spin_unlock(&l_mg->free_lock);
168
169 spin_lock(&l_mg->gc_lock);
170 list_for_each_entry(line, &l_mg->gc_full_list, list) {
171 if (line->type == PBLK_LINETYPE_DATA)
172 d_line_cnt++;
173 else if (line->type == PBLK_LINETYPE_LOG)
174 l_line_cnt++;
175 closed_line_cnt++;
176 gc_full++;
177 }
178
179 list_for_each_entry(line, &l_mg->gc_high_list, list) {
180 if (line->type == PBLK_LINETYPE_DATA)
181 d_line_cnt++;
182 else if (line->type == PBLK_LINETYPE_LOG)
183 l_line_cnt++;
184 closed_line_cnt++;
185 gc_high++;
186 }
187
188 list_for_each_entry(line, &l_mg->gc_mid_list, list) {
189 if (line->type == PBLK_LINETYPE_DATA)
190 d_line_cnt++;
191 else if (line->type == PBLK_LINETYPE_LOG)
192 l_line_cnt++;
193 closed_line_cnt++;
194 gc_mid++;
195 }
196
197 list_for_each_entry(line, &l_mg->gc_low_list, list) {
198 if (line->type == PBLK_LINETYPE_DATA)
199 d_line_cnt++;
200 else if (line->type == PBLK_LINETYPE_LOG)
201 l_line_cnt++;
202 closed_line_cnt++;
203 gc_low++;
204 }
205
206 list_for_each_entry(line, &l_mg->gc_empty_list, list) {
207 if (line->type == PBLK_LINETYPE_DATA)
208 d_line_cnt++;
209 else if (line->type == PBLK_LINETYPE_LOG)
210 l_line_cnt++;
211 closed_line_cnt++;
212 gc_empty++;
213 }
214
215 list_for_each_entry(line, &l_mg->free_list, list)
216 free++;
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);
222
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 ssecs = l_mg->data_line->left_ssecs;
228 vsc = l_mg->data_line->vsc;
229 sec_in_line = l_mg->data_line->sec_in_line;
230 meta_weight = bitmap_weight(&l_mg->meta_bitmap,
231 PBLK_DATA_LINES);
232 map_weight = bitmap_weight(l_mg->data_line->map_bitmap,
233 lm->sec_per_line);
234 }
235 spin_unlock(&l_mg->free_lock);
236
237 if (nr_free_lines != free_line_cnt)
238 pr_err("pblk: corrupted free line list\n");
239
240 sz = snprintf(page, PAGE_SIZE - sz,
241 "line: nluns:%d, nblks:%d, nsecs:%d\n",
242 geo->nr_luns, lm->blk_per_line, lm->sec_per_line);
243
244 sz += snprintf(page + sz, PAGE_SIZE - sz,
245 "lines:d:%d,l:%d-f:%d(%d),b:%d,co:%d,c:%d(d:%d,l:%d)t:%d\n",
246 cur_data, cur_log,
247 free, nr_free_lines, bad, cor,
248 closed_line_cnt,
249 d_line_cnt, l_line_cnt,
250 l_mg->nr_lines);
251
252 sz += snprintf(page + sz, PAGE_SIZE - sz,
253 "GC: full:%d, high:%d, mid:%d, low:%d, empty:%d, queue:%d\n",
254 gc_full, gc_high, gc_mid, gc_low, gc_empty,
255 atomic_read(&pblk->gc.inflight_gc));
256
257 sz += snprintf(page + sz, PAGE_SIZE - sz,
258 "data (%d) cur:%d, left:%d/%d, vsc:%d, s:%d, map:%d/%d (%d)\n",
259 cur_data, cur_sec, msecs, ssecs, vsc, sec_in_line,
260 map_weight, lm->sec_per_line, meta_weight);
261
262 return sz;
263 }
264
265 static ssize_t pblk_sysfs_lines_info(struct pblk *pblk, char *page)
266 {
267 struct nvm_tgt_dev *dev = pblk->dev;
268 struct nvm_geo *geo = &dev->geo;
269 struct pblk_line_meta *lm = &pblk->lm;
270 ssize_t sz = 0;
271
272 sz = snprintf(page, PAGE_SIZE - sz,
273 "smeta - len:%d, secs:%d\n",
274 lm->smeta_len, lm->smeta_sec);
275 sz += snprintf(page + sz, PAGE_SIZE - sz,
276 "emeta - len:%d, sec:%d, bb_start:%d\n",
277 lm->emeta_len, lm->emeta_sec,
278 lm->emeta_bb);
279 sz += snprintf(page + sz, PAGE_SIZE - sz,
280 "bitmap lengths: sec:%d, blk:%d, lun:%d\n",
281 lm->sec_bitmap_len,
282 lm->blk_bitmap_len,
283 lm->lun_bitmap_len);
284 sz += snprintf(page + sz, PAGE_SIZE - sz,
285 "blk_line:%d, sec_line:%d, sec_blk:%d\n",
286 lm->blk_per_line,
287 lm->sec_per_line,
288 geo->sec_per_blk);
289
290 return sz;
291 }
292
293 #ifdef CONFIG_NVM_DEBUG
294 static ssize_t pblk_sysfs_stats_debug(struct pblk *pblk, char *page)
295 {
296 return snprintf(page, PAGE_SIZE,
297 "%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",
298 atomic_long_read(&pblk->inflight_writes),
299 atomic_long_read(&pblk->inflight_reads),
300 atomic_long_read(&pblk->req_writes),
301 atomic_long_read(&pblk->nr_flush),
302 atomic_long_read(&pblk->padded_writes),
303 atomic_long_read(&pblk->padded_wb),
304 atomic_long_read(&pblk->sub_writes),
305 atomic_long_read(&pblk->sync_writes),
306 atomic_long_read(&pblk->compl_writes),
307 atomic_long_read(&pblk->recov_writes),
308 atomic_long_read(&pblk->recov_gc_writes),
309 atomic_long_read(&pblk->recov_gc_reads),
310 atomic_long_read(&pblk->sync_reads));
311 }
312 #endif
313
314 static ssize_t pblk_sysfs_rate_store(struct pblk *pblk, const char *page,
315 size_t len)
316 {
317 struct pblk_gc *gc = &pblk->gc;
318 size_t c_len;
319 int value;
320
321 c_len = strcspn(page, "\n");
322 if (c_len >= len)
323 return -EINVAL;
324
325 if (kstrtouint(page, 0, &value))
326 return -EINVAL;
327
328 spin_lock(&gc->lock);
329 pblk_rl_set_gc_rsc(&pblk->rl, value);
330 spin_unlock(&gc->lock);
331
332 return len;
333 }
334
335 static ssize_t pblk_sysfs_gc_force(struct pblk *pblk, const char *page,
336 size_t len)
337 {
338 size_t c_len;
339 int force;
340
341 c_len = strcspn(page, "\n");
342 if (c_len >= len)
343 return -EINVAL;
344
345 if (kstrtouint(page, 0, &force))
346 return -EINVAL;
347
348 if (force < 0 || force > 1)
349 return -EINVAL;
350
351 pblk_gc_sysfs_force(pblk, force);
352
353 return len;
354 }
355
356 static struct attribute sys_write_luns = {
357 .name = "write_luns",
358 .mode = 0444,
359 };
360
361 static struct attribute sys_rate_limiter_attr = {
362 .name = "rate_limiter",
363 .mode = 0444,
364 };
365
366 static struct attribute sys_gc_state = {
367 .name = "gc_state",
368 .mode = 0444,
369 };
370
371 static struct attribute sys_errors_attr = {
372 .name = "errors",
373 .mode = 0444,
374 };
375
376 static struct attribute sys_rb_attr = {
377 .name = "write_buffer",
378 .mode = 0444,
379 };
380
381 static struct attribute sys_stats_ppaf_attr = {
382 .name = "ppa_format",
383 .mode = 0444,
384 };
385
386 static struct attribute sys_lines_attr = {
387 .name = "lines",
388 .mode = 0444,
389 };
390
391 static struct attribute sys_lines_info_attr = {
392 .name = "lines_info",
393 .mode = 0444,
394 };
395
396 static struct attribute sys_gc_force = {
397 .name = "gc_force",
398 .mode = 0200,
399 };
400
401 static struct attribute sys_gc_rl_max = {
402 .name = "gc_rl_max",
403 .mode = 0200,
404 };
405
406 #ifdef CONFIG_NVM_DEBUG
407 static struct attribute sys_stats_debug_attr = {
408 .name = "stats",
409 .mode = 0444,
410 };
411 #endif
412
413 static struct attribute *pblk_attrs[] = {
414 &sys_write_luns,
415 &sys_rate_limiter_attr,
416 &sys_errors_attr,
417 &sys_gc_state,
418 &sys_gc_force,
419 &sys_gc_rl_max,
420 &sys_rb_attr,
421 &sys_stats_ppaf_attr,
422 &sys_lines_attr,
423 &sys_lines_info_attr,
424 #ifdef CONFIG_NVM_DEBUG
425 &sys_stats_debug_attr,
426 #endif
427 NULL,
428 };
429
430 static ssize_t pblk_sysfs_show(struct kobject *kobj, struct attribute *attr,
431 char *buf)
432 {
433 struct pblk *pblk = container_of(kobj, struct pblk, kobj);
434
435 if (strcmp(attr->name, "rate_limiter") == 0)
436 return pblk_sysfs_rate_limiter(pblk, buf);
437 else if (strcmp(attr->name, "write_luns") == 0)
438 return pblk_sysfs_luns_show(pblk, buf);
439 else if (strcmp(attr->name, "gc_state") == 0)
440 return pblk_sysfs_gc_state_show(pblk, buf);
441 else if (strcmp(attr->name, "errors") == 0)
442 return pblk_sysfs_stats(pblk, buf);
443 else if (strcmp(attr->name, "write_buffer") == 0)
444 return pblk_sysfs_write_buffer(pblk, buf);
445 else if (strcmp(attr->name, "ppa_format") == 0)
446 return pblk_sysfs_ppaf(pblk, buf);
447 else if (strcmp(attr->name, "lines") == 0)
448 return pblk_sysfs_lines(pblk, buf);
449 else if (strcmp(attr->name, "lines_info") == 0)
450 return pblk_sysfs_lines_info(pblk, buf);
451 #ifdef CONFIG_NVM_DEBUG
452 else if (strcmp(attr->name, "stats") == 0)
453 return pblk_sysfs_stats_debug(pblk, buf);
454 #endif
455 return 0;
456 }
457
458 static ssize_t pblk_sysfs_store(struct kobject *kobj, struct attribute *attr,
459 const char *buf, size_t len)
460 {
461 struct pblk *pblk = container_of(kobj, struct pblk, kobj);
462
463 if (strcmp(attr->name, "gc_rl_max") == 0)
464 return pblk_sysfs_rate_store(pblk, buf, len);
465 else if (strcmp(attr->name, "gc_force") == 0)
466 return pblk_sysfs_gc_force(pblk, buf, len);
467
468 return 0;
469 }
470
471 static const struct sysfs_ops pblk_sysfs_ops = {
472 .show = pblk_sysfs_show,
473 .store = pblk_sysfs_store,
474 };
475
476 static struct kobj_type pblk_ktype = {
477 .sysfs_ops = &pblk_sysfs_ops,
478 .default_attrs = pblk_attrs,
479 };
480
481 int pblk_sysfs_init(struct gendisk *tdisk)
482 {
483 struct pblk *pblk = tdisk->private_data;
484 struct device *parent_dev = disk_to_dev(pblk->disk);
485 int ret;
486
487 ret = kobject_init_and_add(&pblk->kobj, &pblk_ktype,
488 kobject_get(&parent_dev->kobj),
489 "%s", "pblk");
490 if (ret) {
491 pr_err("pblk: could not register %s/pblk\n",
492 tdisk->disk_name);
493 return ret;
494 }
495
496 kobject_uevent(&pblk->kobj, KOBJ_ADD);
497 return 0;
498 }
499
500 void pblk_sysfs_exit(struct gendisk *tdisk)
501 {
502 struct pblk *pblk = tdisk->private_data;
503
504 kobject_uevent(&pblk->kobj, KOBJ_REMOVE);
505 kobject_del(&pblk->kobj);
506 kobject_put(&pblk->kobj);
507 }