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 
pblk_sysfs_luns_show(struct pblk * pblk,char * page)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->all_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.a.ch,
43 					rlun->bppa.a.lun,
44 					active);
45 	}
46 
47 	return sz;
48 }
49 
pblk_sysfs_rate_limiter(struct pblk * pblk,char * page)50 static ssize_t pblk_sysfs_rate_limiter(struct pblk *pblk, char *page)
51 {
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;
55 
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;
64 
65 	total_blocks = pblk->rl.total_blocks;
66 
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));
80 }
81 
pblk_sysfs_gc_state_show(struct pblk * pblk,char * page)82 static ssize_t pblk_sysfs_gc_state_show(struct pblk *pblk, char *page)
83 {
84 	int gc_enabled, gc_active;
85 
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);
89 }
90 
pblk_sysfs_stats(struct pblk * pblk,char * page)91 static ssize_t pblk_sysfs_stats(struct pblk *pblk, char *page)
92 {
93 	ssize_t sz;
94 
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));
103 
104 	return sz;
105 }
106 
pblk_sysfs_write_buffer(struct pblk * pblk,char * page)107 static ssize_t pblk_sysfs_write_buffer(struct pblk *pblk, char *page)
108 {
109 	return pblk_rb_sysfs(&pblk->rwb, page);
110 }
111 
pblk_sysfs_ppaf(struct pblk * pblk,char * page)112 static ssize_t pblk_sysfs_ppaf(struct pblk *pblk, char *page)
113 {
114 	struct nvm_tgt_dev *dev = pblk->dev;
115 	struct nvm_geo *geo = &dev->geo;
116 	ssize_t sz = 0;
117 
118 	if (geo->version == NVM_OCSSD_SPEC_12) {
119 		struct nvm_addrf_12 *ppaf = (struct nvm_addrf_12 *)&pblk->addrf;
120 		struct nvm_addrf_12 *gppaf = (struct nvm_addrf_12 *)&geo->addrf;
121 
122 		sz = snprintf(page, PAGE_SIZE,
123 			"g:(b:%d)blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
124 			pblk->addrf_len,
125 			ppaf->blk_offset, ppaf->blk_len,
126 			ppaf->pg_offset, ppaf->pg_len,
127 			ppaf->lun_offset, ppaf->lun_len,
128 			ppaf->ch_offset, ppaf->ch_len,
129 			ppaf->pln_offset, ppaf->pln_len,
130 			ppaf->sec_offset, ppaf->sec_len);
131 
132 		sz += snprintf(page + sz, PAGE_SIZE - sz,
133 			"d:blk:%d/%d,pg:%d/%d,lun:%d/%d,ch:%d/%d,pl:%d/%d,sec:%d/%d\n",
134 			gppaf->blk_offset, gppaf->blk_len,
135 			gppaf->pg_offset, gppaf->pg_len,
136 			gppaf->lun_offset, gppaf->lun_len,
137 			gppaf->ch_offset, gppaf->ch_len,
138 			gppaf->pln_offset, gppaf->pln_len,
139 			gppaf->sec_offset, gppaf->sec_len);
140 	} else {
141 		struct nvm_addrf *ppaf = &pblk->addrf;
142 		struct nvm_addrf *gppaf = &geo->addrf;
143 
144 		sz = snprintf(page, PAGE_SIZE,
145 			"pblk:(s:%d)ch:%d/%d,lun:%d/%d,chk:%d/%d/sec:%d/%d\n",
146 			pblk->addrf_len,
147 			ppaf->ch_offset, ppaf->ch_len,
148 			ppaf->lun_offset, ppaf->lun_len,
149 			ppaf->chk_offset, ppaf->chk_len,
150 			ppaf->sec_offset, ppaf->sec_len);
151 
152 		sz += snprintf(page + sz, PAGE_SIZE - sz,
153 			"device:ch:%d/%d,lun:%d/%d,chk:%d/%d,sec:%d/%d\n",
154 			gppaf->ch_offset, gppaf->ch_len,
155 			gppaf->lun_offset, gppaf->lun_len,
156 			gppaf->chk_offset, gppaf->chk_len,
157 			gppaf->sec_offset, gppaf->sec_len);
158 	}
159 
160 	return sz;
161 }
162 
pblk_sysfs_lines(struct pblk * pblk,char * page)163 static ssize_t pblk_sysfs_lines(struct pblk *pblk, char *page)
164 {
165 	struct nvm_tgt_dev *dev = pblk->dev;
166 	struct nvm_geo *geo = &dev->geo;
167 	struct pblk_line_meta *lm = &pblk->lm;
168 	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
169 	struct pblk_line *line;
170 	ssize_t sz = 0;
171 	int nr_free_lines;
172 	int cur_data, cur_log;
173 	int free_line_cnt = 0, closed_line_cnt = 0, emeta_line_cnt = 0;
174 	int d_line_cnt = 0, l_line_cnt = 0;
175 	int gc_full = 0, gc_high = 0, gc_mid = 0, gc_low = 0, gc_empty = 0;
176 	int gc_werr = 0;
177 
178 	int bad = 0, cor = 0;
179 	int msecs = 0, cur_sec = 0, vsc = 0, sec_in_line = 0;
180 	int map_weight = 0, meta_weight = 0;
181 
182 	spin_lock(&l_mg->free_lock);
183 	cur_data = (l_mg->data_line) ? l_mg->data_line->id : -1;
184 	cur_log = (l_mg->log_line) ? l_mg->log_line->id : -1;
185 	nr_free_lines = l_mg->nr_free_lines;
186 
187 	list_for_each_entry(line, &l_mg->free_list, list)
188 		free_line_cnt++;
189 	spin_unlock(&l_mg->free_lock);
190 
191 	spin_lock(&l_mg->close_lock);
192 	list_for_each_entry(line, &l_mg->emeta_list, list)
193 		emeta_line_cnt++;
194 	spin_unlock(&l_mg->close_lock);
195 
196 	spin_lock(&l_mg->gc_lock);
197 	list_for_each_entry(line, &l_mg->gc_full_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_full++;
204 	}
205 
206 	list_for_each_entry(line, &l_mg->gc_high_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_high++;
213 	}
214 
215 	list_for_each_entry(line, &l_mg->gc_mid_list, list) {
216 		if (line->type == PBLK_LINETYPE_DATA)
217 			d_line_cnt++;
218 		else if (line->type == PBLK_LINETYPE_LOG)
219 			l_line_cnt++;
220 		closed_line_cnt++;
221 		gc_mid++;
222 	}
223 
224 	list_for_each_entry(line, &l_mg->gc_low_list, list) {
225 		if (line->type == PBLK_LINETYPE_DATA)
226 			d_line_cnt++;
227 		else if (line->type == PBLK_LINETYPE_LOG)
228 			l_line_cnt++;
229 		closed_line_cnt++;
230 		gc_low++;
231 	}
232 
233 	list_for_each_entry(line, &l_mg->gc_empty_list, list) {
234 		if (line->type == PBLK_LINETYPE_DATA)
235 			d_line_cnt++;
236 		else if (line->type == PBLK_LINETYPE_LOG)
237 			l_line_cnt++;
238 		closed_line_cnt++;
239 		gc_empty++;
240 	}
241 
242 	list_for_each_entry(line, &l_mg->gc_werr_list, list) {
243 		if (line->type == PBLK_LINETYPE_DATA)
244 			d_line_cnt++;
245 		else if (line->type == PBLK_LINETYPE_LOG)
246 			l_line_cnt++;
247 		closed_line_cnt++;
248 		gc_werr++;
249 	}
250 
251 	list_for_each_entry(line, &l_mg->bad_list, list)
252 		bad++;
253 	list_for_each_entry(line, &l_mg->corrupt_list, list)
254 		cor++;
255 	spin_unlock(&l_mg->gc_lock);
256 
257 	spin_lock(&l_mg->free_lock);
258 	if (l_mg->data_line) {
259 		cur_sec = l_mg->data_line->cur_sec;
260 		msecs = l_mg->data_line->left_msecs;
261 		vsc = le32_to_cpu(*l_mg->data_line->vsc);
262 		sec_in_line = l_mg->data_line->sec_in_line;
263 		meta_weight = bitmap_weight(&l_mg->meta_bitmap,
264 							PBLK_DATA_LINES);
265 
266 		spin_lock(&l_mg->data_line->lock);
267 		if (l_mg->data_line->map_bitmap)
268 			map_weight = bitmap_weight(l_mg->data_line->map_bitmap,
269 							lm->sec_per_line);
270 		else
271 			map_weight = 0;
272 		spin_unlock(&l_mg->data_line->lock);
273 	}
274 	spin_unlock(&l_mg->free_lock);
275 
276 	if (nr_free_lines != free_line_cnt)
277 		pblk_err(pblk, "corrupted free line list:%d/%d\n",
278 						nr_free_lines, free_line_cnt);
279 
280 	sz = snprintf(page, PAGE_SIZE - sz,
281 		"line: nluns:%d, nblks:%d, nsecs:%d\n",
282 		geo->all_luns, lm->blk_per_line, lm->sec_per_line);
283 
284 	sz += snprintf(page + sz, PAGE_SIZE - sz,
285 		"lines:d:%d,l:%d-f:%d,m:%d/%d,c:%d,b:%d,co:%d(d:%d,l:%d)t:%d\n",
286 					cur_data, cur_log,
287 					nr_free_lines,
288 					emeta_line_cnt, meta_weight,
289 					closed_line_cnt,
290 					bad, cor,
291 					d_line_cnt, l_line_cnt,
292 					l_mg->nr_lines);
293 
294 	sz += snprintf(page + sz, PAGE_SIZE - sz,
295 		"GC: full:%d, high:%d, mid:%d, low:%d, empty:%d, werr: %d, queue:%d\n",
296 			gc_full, gc_high, gc_mid, gc_low, gc_empty, gc_werr,
297 			atomic_read(&pblk->gc.read_inflight_gc));
298 
299 	sz += snprintf(page + sz, PAGE_SIZE - sz,
300 		"data (%d) cur:%d, left:%d, vsc:%d, s:%d, map:%d/%d (%d)\n",
301 			cur_data, cur_sec, msecs, vsc, sec_in_line,
302 			map_weight, lm->sec_per_line,
303 			atomic_read(&pblk->inflight_io));
304 
305 	return sz;
306 }
307 
pblk_sysfs_lines_info(struct pblk * pblk,char * page)308 static ssize_t pblk_sysfs_lines_info(struct pblk *pblk, char *page)
309 {
310 	struct nvm_tgt_dev *dev = pblk->dev;
311 	struct nvm_geo *geo = &dev->geo;
312 	struct pblk_line_meta *lm = &pblk->lm;
313 	ssize_t sz = 0;
314 
315 	sz = snprintf(page, PAGE_SIZE - sz,
316 				"smeta - len:%d, secs:%d\n",
317 					lm->smeta_len, lm->smeta_sec);
318 	sz += snprintf(page + sz, PAGE_SIZE - sz,
319 				"emeta - len:%d, sec:%d, bb_start:%d\n",
320 					lm->emeta_len[0], lm->emeta_sec[0],
321 					lm->emeta_bb);
322 	sz += snprintf(page + sz, PAGE_SIZE - sz,
323 				"bitmap lengths: sec:%d, blk:%d, lun:%d\n",
324 					lm->sec_bitmap_len,
325 					lm->blk_bitmap_len,
326 					lm->lun_bitmap_len);
327 	sz += snprintf(page + sz, PAGE_SIZE - sz,
328 				"blk_line:%d, sec_line:%d, sec_blk:%d\n",
329 					lm->blk_per_line,
330 					lm->sec_per_line,
331 					geo->clba);
332 
333 	return sz;
334 }
335 
pblk_sysfs_get_sec_per_write(struct pblk * pblk,char * page)336 static ssize_t pblk_sysfs_get_sec_per_write(struct pblk *pblk, char *page)
337 {
338 	return snprintf(page, PAGE_SIZE, "%d\n", pblk->sec_per_write);
339 }
340 
pblk_get_write_amp(u64 user,u64 gc,u64 pad,char * page)341 static ssize_t pblk_get_write_amp(u64 user, u64 gc, u64 pad,
342 				  char *page)
343 {
344 	int sz;
345 
346 	sz = snprintf(page, PAGE_SIZE,
347 			"user:%lld gc:%lld pad:%lld WA:",
348 			user, gc, pad);
349 
350 	if (!user) {
351 		sz += snprintf(page + sz, PAGE_SIZE - sz, "NaN\n");
352 	} else {
353 		u64 wa_int;
354 		u32 wa_frac;
355 
356 		wa_int = (user + gc + pad) * 100000;
357 		wa_int = div64_u64(wa_int, user);
358 		wa_int = div_u64_rem(wa_int, 100000, &wa_frac);
359 
360 		sz += snprintf(page + sz, PAGE_SIZE - sz, "%llu.%05u\n",
361 							wa_int, wa_frac);
362 	}
363 
364 	return sz;
365 }
366 
pblk_sysfs_get_write_amp_mileage(struct pblk * pblk,char * page)367 static ssize_t pblk_sysfs_get_write_amp_mileage(struct pblk *pblk, char *page)
368 {
369 	return pblk_get_write_amp(atomic64_read(&pblk->user_wa),
370 		atomic64_read(&pblk->gc_wa), atomic64_read(&pblk->pad_wa),
371 		page);
372 }
373 
pblk_sysfs_get_write_amp_trip(struct pblk * pblk,char * page)374 static ssize_t pblk_sysfs_get_write_amp_trip(struct pblk *pblk, char *page)
375 {
376 	return pblk_get_write_amp(
377 		atomic64_read(&pblk->user_wa) - pblk->user_rst_wa,
378 		atomic64_read(&pblk->gc_wa) - pblk->gc_rst_wa,
379 		atomic64_read(&pblk->pad_wa) - pblk->pad_rst_wa, page);
380 }
381 
bucket_percentage(unsigned long long bucket,unsigned long long total)382 static long long bucket_percentage(unsigned long long bucket,
383 				   unsigned long long total)
384 {
385 	int p = bucket * 100;
386 
387 	p = div_u64(p, total);
388 
389 	return p;
390 }
391 
pblk_sysfs_get_padding_dist(struct pblk * pblk,char * page)392 static ssize_t pblk_sysfs_get_padding_dist(struct pblk *pblk, char *page)
393 {
394 	int sz = 0;
395 	unsigned long long total;
396 	unsigned long long total_buckets = 0;
397 	int buckets = pblk->min_write_pgs - 1;
398 	int i;
399 
400 	total = atomic64_read(&pblk->nr_flush) - pblk->nr_flush_rst;
401 	if (!total) {
402 		for (i = 0; i < (buckets + 1); i++)
403 			sz += snprintf(page + sz, PAGE_SIZE - sz,
404 				"%d:0 ", i);
405 		sz += snprintf(page + sz, PAGE_SIZE - sz, "\n");
406 
407 		return sz;
408 	}
409 
410 	for (i = 0; i < buckets; i++)
411 		total_buckets += atomic64_read(&pblk->pad_dist[i]);
412 
413 	sz += snprintf(page + sz, PAGE_SIZE - sz, "0:%lld%% ",
414 		bucket_percentage(total - total_buckets, total));
415 
416 	for (i = 0; i < buckets; i++) {
417 		unsigned long long p;
418 
419 		p = bucket_percentage(atomic64_read(&pblk->pad_dist[i]),
420 					  total);
421 		sz += snprintf(page + sz, PAGE_SIZE - sz, "%d:%lld%% ",
422 				i + 1, p);
423 	}
424 	sz += snprintf(page + sz, PAGE_SIZE - sz, "\n");
425 
426 	return sz;
427 }
428 
429 #ifdef CONFIG_NVM_PBLK_DEBUG
pblk_sysfs_stats_debug(struct pblk * pblk,char * page)430 static ssize_t pblk_sysfs_stats_debug(struct pblk *pblk, char *page)
431 {
432 	return snprintf(page, PAGE_SIZE,
433 		"%lu\t%lu\t%ld\t%llu\t%ld\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\t%lu\n",
434 			atomic_long_read(&pblk->inflight_writes),
435 			atomic_long_read(&pblk->inflight_reads),
436 			atomic_long_read(&pblk->req_writes),
437 			(u64)atomic64_read(&pblk->nr_flush),
438 			atomic_long_read(&pblk->padded_writes),
439 			atomic_long_read(&pblk->padded_wb),
440 			atomic_long_read(&pblk->sub_writes),
441 			atomic_long_read(&pblk->sync_writes),
442 			atomic_long_read(&pblk->recov_writes),
443 			atomic_long_read(&pblk->recov_gc_writes),
444 			atomic_long_read(&pblk->recov_gc_reads),
445 			atomic_long_read(&pblk->cache_reads),
446 			atomic_long_read(&pblk->sync_reads));
447 }
448 #endif
449 
pblk_sysfs_gc_force(struct pblk * pblk,const char * page,size_t len)450 static ssize_t pblk_sysfs_gc_force(struct pblk *pblk, const char *page,
451 				   size_t len)
452 {
453 	size_t c_len;
454 	int force;
455 
456 	c_len = strcspn(page, "\n");
457 	if (c_len >= len)
458 		return -EINVAL;
459 
460 	if (kstrtouint(page, 0, &force))
461 		return -EINVAL;
462 
463 	pblk_gc_sysfs_force(pblk, force);
464 
465 	return len;
466 }
467 
pblk_sysfs_set_sec_per_write(struct pblk * pblk,const char * page,size_t len)468 static ssize_t pblk_sysfs_set_sec_per_write(struct pblk *pblk,
469 					     const char *page, size_t len)
470 {
471 	size_t c_len;
472 	int sec_per_write;
473 
474 	c_len = strcspn(page, "\n");
475 	if (c_len >= len)
476 		return -EINVAL;
477 
478 	if (kstrtouint(page, 0, &sec_per_write))
479 		return -EINVAL;
480 
481 	if (sec_per_write < pblk->min_write_pgs
482 				|| sec_per_write > pblk->max_write_pgs
483 				|| sec_per_write % pblk->min_write_pgs != 0)
484 		return -EINVAL;
485 
486 	pblk_set_sec_per_write(pblk, sec_per_write);
487 
488 	return len;
489 }
490 
pblk_sysfs_set_write_amp_trip(struct pblk * pblk,const char * page,size_t len)491 static ssize_t pblk_sysfs_set_write_amp_trip(struct pblk *pblk,
492 			const char *page, size_t len)
493 {
494 	size_t c_len;
495 	int reset_value;
496 
497 	c_len = strcspn(page, "\n");
498 	if (c_len >= len)
499 		return -EINVAL;
500 
501 	if (kstrtouint(page, 0, &reset_value))
502 		return -EINVAL;
503 
504 	if (reset_value !=  0)
505 		return -EINVAL;
506 
507 	pblk->user_rst_wa = atomic64_read(&pblk->user_wa);
508 	pblk->pad_rst_wa = atomic64_read(&pblk->pad_wa);
509 	pblk->gc_rst_wa = atomic64_read(&pblk->gc_wa);
510 
511 	return len;
512 }
513 
514 
pblk_sysfs_set_padding_dist(struct pblk * pblk,const char * page,size_t len)515 static ssize_t pblk_sysfs_set_padding_dist(struct pblk *pblk,
516 			const char *page, size_t len)
517 {
518 	size_t c_len;
519 	int reset_value;
520 	int buckets = pblk->min_write_pgs - 1;
521 	int i;
522 
523 	c_len = strcspn(page, "\n");
524 	if (c_len >= len)
525 		return -EINVAL;
526 
527 	if (kstrtouint(page, 0, &reset_value))
528 		return -EINVAL;
529 
530 	if (reset_value !=  0)
531 		return -EINVAL;
532 
533 	for (i = 0; i < buckets; i++)
534 		atomic64_set(&pblk->pad_dist[i], 0);
535 
536 	pblk->nr_flush_rst = atomic64_read(&pblk->nr_flush);
537 
538 	return len;
539 }
540 
541 static struct attribute sys_write_luns = {
542 	.name = "write_luns",
543 	.mode = 0444,
544 };
545 
546 static struct attribute sys_rate_limiter_attr = {
547 	.name = "rate_limiter",
548 	.mode = 0444,
549 };
550 
551 static struct attribute sys_gc_state = {
552 	.name = "gc_state",
553 	.mode = 0444,
554 };
555 
556 static struct attribute sys_errors_attr = {
557 	.name = "errors",
558 	.mode = 0444,
559 };
560 
561 static struct attribute sys_rb_attr = {
562 	.name = "write_buffer",
563 	.mode = 0444,
564 };
565 
566 static struct attribute sys_stats_ppaf_attr = {
567 	.name = "ppa_format",
568 	.mode = 0444,
569 };
570 
571 static struct attribute sys_lines_attr = {
572 	.name = "lines",
573 	.mode = 0444,
574 };
575 
576 static struct attribute sys_lines_info_attr = {
577 	.name = "lines_info",
578 	.mode = 0444,
579 };
580 
581 static struct attribute sys_gc_force = {
582 	.name = "gc_force",
583 	.mode = 0200,
584 };
585 
586 static struct attribute sys_max_sec_per_write = {
587 	.name = "max_sec_per_write",
588 	.mode = 0644,
589 };
590 
591 static struct attribute sys_write_amp_mileage = {
592 	.name = "write_amp_mileage",
593 	.mode = 0444,
594 };
595 
596 static struct attribute sys_write_amp_trip = {
597 	.name = "write_amp_trip",
598 	.mode = 0644,
599 };
600 
601 static struct attribute sys_padding_dist = {
602 	.name = "padding_dist",
603 	.mode = 0644,
604 };
605 
606 #ifdef CONFIG_NVM_PBLK_DEBUG
607 static struct attribute sys_stats_debug_attr = {
608 	.name = "stats",
609 	.mode = 0444,
610 };
611 #endif
612 
613 static struct attribute *pblk_attrs[] = {
614 	&sys_write_luns,
615 	&sys_rate_limiter_attr,
616 	&sys_errors_attr,
617 	&sys_gc_state,
618 	&sys_gc_force,
619 	&sys_max_sec_per_write,
620 	&sys_rb_attr,
621 	&sys_stats_ppaf_attr,
622 	&sys_lines_attr,
623 	&sys_lines_info_attr,
624 	&sys_write_amp_mileage,
625 	&sys_write_amp_trip,
626 	&sys_padding_dist,
627 #ifdef CONFIG_NVM_PBLK_DEBUG
628 	&sys_stats_debug_attr,
629 #endif
630 	NULL,
631 };
632 
pblk_sysfs_show(struct kobject * kobj,struct attribute * attr,char * buf)633 static ssize_t pblk_sysfs_show(struct kobject *kobj, struct attribute *attr,
634 			       char *buf)
635 {
636 	struct pblk *pblk = container_of(kobj, struct pblk, kobj);
637 
638 	if (strcmp(attr->name, "rate_limiter") == 0)
639 		return pblk_sysfs_rate_limiter(pblk, buf);
640 	else if (strcmp(attr->name, "write_luns") == 0)
641 		return pblk_sysfs_luns_show(pblk, buf);
642 	else if (strcmp(attr->name, "gc_state") == 0)
643 		return pblk_sysfs_gc_state_show(pblk, buf);
644 	else if (strcmp(attr->name, "errors") == 0)
645 		return pblk_sysfs_stats(pblk, buf);
646 	else if (strcmp(attr->name, "write_buffer") == 0)
647 		return pblk_sysfs_write_buffer(pblk, buf);
648 	else if (strcmp(attr->name, "ppa_format") == 0)
649 		return pblk_sysfs_ppaf(pblk, buf);
650 	else if (strcmp(attr->name, "lines") == 0)
651 		return pblk_sysfs_lines(pblk, buf);
652 	else if (strcmp(attr->name, "lines_info") == 0)
653 		return pblk_sysfs_lines_info(pblk, buf);
654 	else if (strcmp(attr->name, "max_sec_per_write") == 0)
655 		return pblk_sysfs_get_sec_per_write(pblk, buf);
656 	else if (strcmp(attr->name, "write_amp_mileage") == 0)
657 		return pblk_sysfs_get_write_amp_mileage(pblk, buf);
658 	else if (strcmp(attr->name, "write_amp_trip") == 0)
659 		return pblk_sysfs_get_write_amp_trip(pblk, buf);
660 	else if (strcmp(attr->name, "padding_dist") == 0)
661 		return pblk_sysfs_get_padding_dist(pblk, buf);
662 #ifdef CONFIG_NVM_PBLK_DEBUG
663 	else if (strcmp(attr->name, "stats") == 0)
664 		return pblk_sysfs_stats_debug(pblk, buf);
665 #endif
666 	return 0;
667 }
668 
pblk_sysfs_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)669 static ssize_t pblk_sysfs_store(struct kobject *kobj, struct attribute *attr,
670 				const char *buf, size_t len)
671 {
672 	struct pblk *pblk = container_of(kobj, struct pblk, kobj);
673 
674 	if (strcmp(attr->name, "gc_force") == 0)
675 		return pblk_sysfs_gc_force(pblk, buf, len);
676 	else if (strcmp(attr->name, "max_sec_per_write") == 0)
677 		return pblk_sysfs_set_sec_per_write(pblk, buf, len);
678 	else if (strcmp(attr->name, "write_amp_trip") == 0)
679 		return pblk_sysfs_set_write_amp_trip(pblk, buf, len);
680 	else if (strcmp(attr->name, "padding_dist") == 0)
681 		return pblk_sysfs_set_padding_dist(pblk, buf, len);
682 	return 0;
683 }
684 
685 static const struct sysfs_ops pblk_sysfs_ops = {
686 	.show = pblk_sysfs_show,
687 	.store = pblk_sysfs_store,
688 };
689 
690 static struct kobj_type pblk_ktype = {
691 	.sysfs_ops	= &pblk_sysfs_ops,
692 	.default_attrs	= pblk_attrs,
693 };
694 
pblk_sysfs_init(struct gendisk * tdisk)695 int pblk_sysfs_init(struct gendisk *tdisk)
696 {
697 	struct pblk *pblk = tdisk->private_data;
698 	struct device *parent_dev = disk_to_dev(pblk->disk);
699 	int ret;
700 
701 	ret = kobject_init_and_add(&pblk->kobj, &pblk_ktype,
702 					kobject_get(&parent_dev->kobj),
703 					"%s", "pblk");
704 	if (ret) {
705 		pblk_err(pblk, "could not register\n");
706 		return ret;
707 	}
708 
709 	kobject_uevent(&pblk->kobj, KOBJ_ADD);
710 	return 0;
711 }
712 
pblk_sysfs_exit(struct gendisk * tdisk)713 void pblk_sysfs_exit(struct gendisk *tdisk)
714 {
715 	struct pblk *pblk = tdisk->private_data;
716 
717 	kobject_uevent(&pblk->kobj, KOBJ_REMOVE);
718 	kobject_del(&pblk->kobj);
719 	kobject_put(&pblk->kobj);
720 }
721