xref: /linux/drivers/ufs/core/ufs-debugfs.c (revision 7a5f1cd22d47f8ca4b760b6334378ae42c1bd24b)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2020 Intel Corporation
3 
4 #include <linux/debugfs.h>
5 
6 #include "ufs-debugfs.h"
7 #include <ufs/ufshcd.h>
8 #include "ufshcd-priv.h"
9 
10 static struct dentry *ufs_debugfs_root;
11 
12 struct ufs_debugfs_attr {
13 	const char			*name;
14 	mode_t				mode;
15 	const struct file_operations	*fops;
16 };
17 
18 /* @file corresponds to a debugfs attribute in directory hba->debugfs_root. */
19 static inline struct ufs_hba *hba_from_file(const struct file *file)
20 {
21 	return d_inode(file->f_path.dentry->d_parent)->i_private;
22 }
23 
24 void __init ufs_debugfs_init(void)
25 {
26 	ufs_debugfs_root = debugfs_create_dir("ufshcd", NULL);
27 }
28 
29 void ufs_debugfs_exit(void)
30 {
31 	debugfs_remove_recursive(ufs_debugfs_root);
32 }
33 
34 static int ufs_debugfs_stats_show(struct seq_file *s, void *data)
35 {
36 	struct ufs_hba *hba = hba_from_file(s->file);
37 	struct ufs_event_hist *e = hba->ufs_stats.event;
38 
39 #define PRT(fmt, typ) \
40 	seq_printf(s, fmt, e[UFS_EVT_ ## typ].cnt)
41 
42 	PRT("PHY Adapter Layer errors (except LINERESET): %llu\n", PA_ERR);
43 	PRT("Data Link Layer errors: %llu\n", DL_ERR);
44 	PRT("Network Layer errors: %llu\n", NL_ERR);
45 	PRT("Transport Layer errors: %llu\n", TL_ERR);
46 	PRT("Generic DME errors: %llu\n", DME_ERR);
47 	PRT("Auto-hibernate errors: %llu\n", AUTO_HIBERN8_ERR);
48 	PRT("IS Fatal errors (CEFES, SBFES, HCFES, DFES): %llu\n", FATAL_ERR);
49 	PRT("DME Link Startup errors: %llu\n", LINK_STARTUP_FAIL);
50 	PRT("PM Resume errors: %llu\n", RESUME_ERR);
51 	PRT("PM Suspend errors : %llu\n", SUSPEND_ERR);
52 	PRT("Logical Unit Resets: %llu\n", DEV_RESET);
53 	PRT("Host Resets: %llu\n", HOST_RESET);
54 	PRT("SCSI command aborts: %llu\n", ABORT);
55 #undef PRT
56 	return 0;
57 }
58 DEFINE_SHOW_ATTRIBUTE(ufs_debugfs_stats);
59 
60 static int ee_usr_mask_get(void *data, u64 *val)
61 {
62 	struct ufs_hba *hba = data;
63 
64 	*val = hba->ee_usr_mask;
65 	return 0;
66 }
67 
68 static int ufs_debugfs_get_user_access(struct ufs_hba *hba)
69 __acquires(&hba->host_sem)
70 {
71 	down(&hba->host_sem);
72 	if (!ufshcd_is_user_access_allowed(hba)) {
73 		up(&hba->host_sem);
74 		return -EBUSY;
75 	}
76 	ufshcd_rpm_get_sync(hba);
77 	return 0;
78 }
79 
80 static void ufs_debugfs_put_user_access(struct ufs_hba *hba)
81 __releases(&hba->host_sem)
82 {
83 	ufshcd_rpm_put_sync(hba);
84 	up(&hba->host_sem);
85 }
86 
87 static int ee_usr_mask_set(void *data, u64 val)
88 {
89 	struct ufs_hba *hba = data;
90 	int err;
91 
92 	if (val & ~(u64)MASK_EE_STATUS)
93 		return -EINVAL;
94 	err = ufs_debugfs_get_user_access(hba);
95 	if (err)
96 		return err;
97 	err = ufshcd_update_ee_usr_mask(hba, val, MASK_EE_STATUS);
98 	ufs_debugfs_put_user_access(hba);
99 	return err;
100 }
101 
102 DEFINE_DEBUGFS_ATTRIBUTE(ee_usr_mask_fops, ee_usr_mask_get, ee_usr_mask_set, "%#llx\n");
103 
104 void ufs_debugfs_exception_event(struct ufs_hba *hba, u16 status)
105 {
106 	bool chgd = false;
107 	u16 ee_ctrl_mask;
108 	int err = 0;
109 
110 	if (!hba->debugfs_ee_rate_limit_ms || !status)
111 		return;
112 
113 	mutex_lock(&hba->ee_ctrl_mutex);
114 	ee_ctrl_mask = hba->ee_drv_mask | (hba->ee_usr_mask & ~status);
115 	chgd = ee_ctrl_mask != hba->ee_ctrl_mask;
116 	if (chgd) {
117 		err = __ufshcd_write_ee_control(hba, ee_ctrl_mask);
118 		if (err)
119 			dev_err(hba->dev, "%s: failed to write ee control %d\n",
120 				__func__, err);
121 	}
122 	mutex_unlock(&hba->ee_ctrl_mutex);
123 
124 	if (chgd && !err) {
125 		unsigned long delay = msecs_to_jiffies(hba->debugfs_ee_rate_limit_ms);
126 
127 		queue_delayed_work(system_freezable_wq, &hba->debugfs_ee_work, delay);
128 	}
129 }
130 
131 static void ufs_debugfs_restart_ee(struct work_struct *work)
132 {
133 	struct ufs_hba *hba = container_of(work, struct ufs_hba, debugfs_ee_work.work);
134 
135 	if (!hba->ee_usr_mask || pm_runtime_suspended(hba->dev) ||
136 	    ufs_debugfs_get_user_access(hba))
137 		return;
138 	ufshcd_write_ee_control(hba);
139 	ufs_debugfs_put_user_access(hba);
140 }
141 
142 static int ufs_saved_err_show(struct seq_file *s, void *data)
143 {
144 	struct ufs_debugfs_attr *attr = s->private;
145 	struct ufs_hba *hba = hba_from_file(s->file);
146 	const int *p;
147 
148 	if (strcmp(attr->name, "saved_err") == 0) {
149 		p = &hba->saved_err;
150 	} else if (strcmp(attr->name, "saved_uic_err") == 0) {
151 		p = &hba->saved_uic_err;
152 	} else {
153 		return -ENOENT;
154 	}
155 
156 	seq_printf(s, "%d\n", *p);
157 	return 0;
158 }
159 
160 static ssize_t ufs_saved_err_write(struct file *file, const char __user *buf,
161 				   size_t count, loff_t *ppos)
162 {
163 	struct ufs_debugfs_attr *attr = file->f_inode->i_private;
164 	struct ufs_hba *hba = hba_from_file(file);
165 	char val_str[16] = { };
166 	int val, ret;
167 
168 	if (count > sizeof(val_str))
169 		return -EINVAL;
170 	if (copy_from_user(val_str, buf, count))
171 		return -EFAULT;
172 	ret = kstrtoint(val_str, 0, &val);
173 	if (ret < 0)
174 		return ret;
175 
176 	spin_lock_irq(hba->host->host_lock);
177 	if (strcmp(attr->name, "saved_err") == 0) {
178 		hba->saved_err = val;
179 	} else if (strcmp(attr->name, "saved_uic_err") == 0) {
180 		hba->saved_uic_err = val;
181 	} else {
182 		ret = -ENOENT;
183 	}
184 	if (ret == 0)
185 		ufshcd_schedule_eh_work(hba);
186 	spin_unlock_irq(hba->host->host_lock);
187 
188 	return ret < 0 ? ret : count;
189 }
190 
191 static int ufs_saved_err_open(struct inode *inode, struct file *file)
192 {
193 	return single_open(file, ufs_saved_err_show, inode->i_private);
194 }
195 
196 static const struct file_operations ufs_saved_err_fops = {
197 	.owner		= THIS_MODULE,
198 	.open		= ufs_saved_err_open,
199 	.read		= seq_read,
200 	.write		= ufs_saved_err_write,
201 	.llseek		= seq_lseek,
202 	.release	= single_release,
203 };
204 
205 static const struct ufs_debugfs_attr ufs_attrs[] = {
206 	{ "stats", 0400, &ufs_debugfs_stats_fops },
207 	{ "saved_err", 0600, &ufs_saved_err_fops },
208 	{ "saved_uic_err", 0600, &ufs_saved_err_fops },
209 	{ }
210 };
211 
212 static int ufs_tx_eq_params_show(struct seq_file *s, void *data)
213 {
214 	const char *file_name = s->file->f_path.dentry->d_name.name;
215 	u32 gear = (u32)(uintptr_t)s->file->f_inode->i_private;
216 	struct ufs_hba *hba = hba_from_file(s->file);
217 	struct ufshcd_tx_eq_settings *settings;
218 	struct ufs_pa_layer_attr *pwr_info;
219 	struct ufshcd_tx_eq_params *params;
220 	u32 rate = hba->pwr_info.hs_rate;
221 	u32 num_lanes;
222 	int lane;
223 
224 	if (!ufshcd_is_tx_eq_supported(hba))
225 		return -EOPNOTSUPP;
226 
227 	if (gear < UFS_HS_G1 || gear > UFS_HS_GEAR_MAX) {
228 		seq_printf(s, "Invalid gear selected: %u\n", gear);
229 		return 0;
230 	}
231 
232 	if (!hba->max_pwr_info.is_valid) {
233 		seq_puts(s, "Max power info is invalid\n");
234 		return 0;
235 	}
236 
237 	pwr_info = &hba->max_pwr_info.info;
238 	params = &hba->tx_eq_params[gear - 1];
239 	if (!params->is_valid) {
240 		seq_printf(s, "TX EQ params are invalid for HS-G%u, Rate-%s\n",
241 			   gear, ufs_hs_rate_to_str(rate));
242 		return 0;
243 	}
244 
245 	if (strcmp(file_name, "host_tx_eq_params") == 0) {
246 		settings = params->host;
247 		num_lanes = pwr_info->lane_tx;
248 		seq_printf(s, "Host TX EQ PreShoot Cap: 0x%02x, DeEmphasis Cap: 0x%02x\n",
249 			   hba->host_preshoot_cap, hba->host_deemphasis_cap);
250 	} else if (strcmp(file_name, "device_tx_eq_params") == 0) {
251 		settings = params->device;
252 		num_lanes = pwr_info->lane_rx;
253 		seq_printf(s, "Device TX EQ PreShoot Cap: 0x%02x, DeEmphasis Cap: 0x%02x\n",
254 			   hba->device_preshoot_cap, hba->device_deemphasis_cap);
255 	} else {
256 		return -ENOENT;
257 	}
258 
259 	seq_printf(s, "TX EQ setting for HS-G%u, Rate-%s:\n", gear,
260 		   ufs_hs_rate_to_str(rate));
261 	for (lane = 0; lane < num_lanes; lane++)
262 		seq_printf(s, "TX Lane %d - PreShoot: %d, DeEmphasis: %d, Pre-Coding %senabled\n",
263 			   lane, settings[lane].preshoot,
264 			   settings[lane].deemphasis,
265 			   settings[lane].precode_en ? "" : "not ");
266 
267 	return 0;
268 }
269 
270 static int ufs_tx_eq_params_open(struct inode *inode, struct file *file)
271 {
272 	return single_open(file, ufs_tx_eq_params_show, inode->i_private);
273 }
274 
275 static const struct file_operations ufs_tx_eq_params_fops = {
276 	.owner		= THIS_MODULE,
277 	.open		= ufs_tx_eq_params_open,
278 	.read		= seq_read,
279 	.llseek		= seq_lseek,
280 	.release	= single_release,
281 };
282 
283 static const struct ufs_debugfs_attr ufs_tx_eq_attrs[] = {
284 	{ "host_tx_eq_params", 0400, &ufs_tx_eq_params_fops },
285 	{ "device_tx_eq_params", 0400, &ufs_tx_eq_params_fops },
286 	{ }
287 };
288 
289 static int ufs_tx_eqtr_record_show(struct seq_file *s, void *data)
290 {
291 	const char *file_name = s->file->f_path.dentry->d_name.name;
292 	u8 (*fom_array)[TX_HS_NUM_PRESHOOT][TX_HS_NUM_DEEMPHASIS];
293 	u32 gear = (u32)(uintptr_t)s->file->f_inode->i_private;
294 	unsigned long preshoot_bitmap, deemphasis_bitmap;
295 	struct ufs_hba *hba = hba_from_file(s->file);
296 	struct ufs_pa_layer_attr *pwr_info;
297 	struct ufshcd_tx_eq_params *params;
298 	struct ufshcd_tx_eqtr_record *rec;
299 	u32 rate = hba->pwr_info.hs_rate;
300 	u8 preshoot, deemphasis;
301 	u32 num_lanes;
302 	char name[32];
303 	int lane;
304 
305 	if (!ufshcd_is_tx_eq_supported(hba))
306 		return -EOPNOTSUPP;
307 
308 	if (gear < UFS_HS_G1 || gear > UFS_HS_GEAR_MAX) {
309 		seq_printf(s, "Invalid gear selected: %u\n", gear);
310 		return 0;
311 	}
312 
313 	if (!hba->max_pwr_info.is_valid) {
314 		seq_puts(s, "Max power info is invalid\n");
315 		return 0;
316 	}
317 
318 	pwr_info = &hba->max_pwr_info.info;
319 	params = &hba->tx_eq_params[gear - 1];
320 	if (!params->is_valid) {
321 		seq_printf(s, "TX EQ params are invalid for HS-G%u, Rate-%s\n",
322 			   gear, ufs_hs_rate_to_str(rate));
323 		return 0;
324 	}
325 
326 	rec = params->eqtr_record;
327 	if (!rec || !rec->last_record_index) {
328 		seq_printf(s, "No TX EQTR records found for HS-G%u, Rate-%s.\n",
329 			   gear, ufs_hs_rate_to_str(rate));
330 		return 0;
331 	}
332 
333 	if (strcmp(file_name, "host_tx_eqtr_record") == 0) {
334 		preshoot_bitmap = (hba->host_preshoot_cap << 0x1) | 0x1;
335 		deemphasis_bitmap = (hba->host_deemphasis_cap << 0x1) | 0x1;
336 		num_lanes = pwr_info->lane_tx;
337 		fom_array = rec->host_fom;
338 		snprintf(name, sizeof(name), "%s", "Host");
339 	} else if (strcmp(file_name, "device_tx_eqtr_record") == 0) {
340 		preshoot_bitmap = (hba->device_preshoot_cap << 0x1) | 0x1;
341 		deemphasis_bitmap = (hba->device_deemphasis_cap << 0x1) | 0x1;
342 		num_lanes = pwr_info->lane_rx;
343 		fom_array = rec->device_fom;
344 		snprintf(name, sizeof(name), "%s", "Device");
345 	} else {
346 		return -ENOENT;
347 	}
348 
349 	seq_printf(s, "%s TX EQTR record summary -\n", name);
350 	seq_printf(s, "Target Power Mode: HS-G%u, Rate-%s\n", gear,
351 		   ufs_hs_rate_to_str(rate));
352 	seq_printf(s, "Most recent record index: %d\n",
353 		   rec->last_record_index);
354 	seq_printf(s, "Most recent record timestamp: %llu us\n",
355 		   ktime_to_us(rec->last_record_ts));
356 
357 	for (lane = 0; lane < num_lanes; lane++) {
358 		seq_printf(s, "\nTX Lane %d FOM - %s\n", lane, "PreShoot\\DeEmphasis");
359 		seq_puts(s, "\\");
360 		/* Print DeEmphasis header as X-axis. */
361 		for (deemphasis = 0; deemphasis < TX_HS_NUM_DEEMPHASIS; deemphasis++)
362 			seq_printf(s, "%8d%s", deemphasis, " ");
363 		seq_puts(s, "\n");
364 		/* Print matrix rows with PreShoot as Y-axis. */
365 		for (preshoot = 0; preshoot < TX_HS_NUM_PRESHOOT; preshoot++) {
366 			seq_printf(s, "%d", preshoot);
367 			for (deemphasis = 0; deemphasis < TX_HS_NUM_DEEMPHASIS; deemphasis++) {
368 				if (test_bit(preshoot, &preshoot_bitmap) &&
369 				    test_bit(deemphasis, &deemphasis_bitmap)) {
370 					u8 fom = fom_array[lane][preshoot][deemphasis];
371 					u8 fom_val = fom & RX_FOM_VALUE_MASK;
372 					bool precode_en = fom & RX_FOM_PRECODING_EN_BIT;
373 
374 					if (ufshcd_is_txeq_presets_used(hba) &&
375 					    !ufshcd_is_txeq_preset_selected(preshoot, deemphasis))
376 						seq_printf(s, "%8s%s", "-", " ");
377 					else
378 						seq_printf(s, "%8u%s", fom_val,
379 							   precode_en ? "*" : " ");
380 				} else {
381 					seq_printf(s, "%8s%s", "x", " ");
382 				}
383 			}
384 			seq_puts(s, "\n");
385 		}
386 	}
387 
388 	return 0;
389 }
390 
391 static int ufs_tx_eqtr_record_open(struct inode *inode, struct file *file)
392 {
393 	return single_open(file, ufs_tx_eqtr_record_show, inode->i_private);
394 }
395 
396 static const struct file_operations ufs_tx_eqtr_record_fops = {
397 	.owner		= THIS_MODULE,
398 	.open		= ufs_tx_eqtr_record_open,
399 	.read		= seq_read,
400 	.llseek		= seq_lseek,
401 	.release	= single_release,
402 };
403 
404 static ssize_t ufs_tx_eq_ctrl_write(struct file *file, const char __user *buf,
405 				    size_t count, loff_t *ppos)
406 {
407 	u32 gear = (u32)(uintptr_t)file->f_inode->i_private;
408 	struct ufs_hba *hba = hba_from_file(file);
409 	char kbuf[32];
410 	int ret;
411 
412 	if (count >= sizeof(kbuf))
413 		return -EINVAL;
414 
415 	if (copy_from_user(kbuf, buf, count))
416 		return -EFAULT;
417 
418 	if (!ufshcd_is_tx_eq_supported(hba))
419 		return -EOPNOTSUPP;
420 
421 	if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
422 	    !hba->max_pwr_info.is_valid)
423 		return -EBUSY;
424 
425 	if (!hba->ufs_device_wlun)
426 		return -ENODEV;
427 
428 	kbuf[count] = '\0';
429 
430 	if (sysfs_streq(kbuf, "retrain")) {
431 		ret = ufs_debugfs_get_user_access(hba);
432 		if (ret)
433 			return ret;
434 		ret = ufshcd_retrain_tx_eq(hba, gear);
435 		ufs_debugfs_put_user_access(hba);
436 	} else {
437 		/* Unknown operation */
438 		return -EINVAL;
439 	}
440 
441 	return ret ? ret : count;
442 }
443 
444 static int ufs_tx_eq_ctrl_show(struct seq_file *s, void *data)
445 {
446 	seq_puts(s, "write 'retrain' to retrain TX Equalization settings\n");
447 	return 0;
448 }
449 
450 static int ufs_tx_eq_ctrl_open(struct inode *inode, struct file *file)
451 {
452 	return single_open(file, ufs_tx_eq_ctrl_show, inode->i_private);
453 }
454 
455 static const struct file_operations ufs_tx_eq_ctrl_fops = {
456 	.owner		= THIS_MODULE,
457 	.open		= ufs_tx_eq_ctrl_open,
458 	.read		= seq_read,
459 	.llseek		= seq_lseek,
460 	.write		= ufs_tx_eq_ctrl_write,
461 	.release	= single_release,
462 };
463 
464 static const struct ufs_debugfs_attr ufs_tx_eqtr_attrs[] = {
465 	{ "host_tx_eqtr_record", 0400, &ufs_tx_eqtr_record_fops },
466 	{ "device_tx_eqtr_record", 0400, &ufs_tx_eqtr_record_fops },
467 	{ "tx_eq_ctrl", 0600, &ufs_tx_eq_ctrl_fops },
468 	{ }
469 };
470 
471 void ufs_debugfs_hba_init(struct ufs_hba *hba)
472 {
473 	const struct ufs_debugfs_attr *attr;
474 	struct dentry *root;
475 
476 	/* Set default exception event rate limit period to 20ms */
477 	hba->debugfs_ee_rate_limit_ms = 20;
478 	INIT_DELAYED_WORK(&hba->debugfs_ee_work, ufs_debugfs_restart_ee);
479 
480 	root = debugfs_create_dir(dev_name(hba->dev), ufs_debugfs_root);
481 	if (IS_ERR_OR_NULL(root))
482 		return;
483 	hba->debugfs_root = root;
484 	d_inode(root)->i_private = hba;
485 	for (attr = ufs_attrs; attr->name; attr++)
486 		debugfs_create_file(attr->name, attr->mode, root, (void *)attr,
487 				    attr->fops);
488 	debugfs_create_file("exception_event_mask", 0600, hba->debugfs_root,
489 			    hba, &ee_usr_mask_fops);
490 	debugfs_create_u32("exception_event_rate_limit_ms", 0600, hba->debugfs_root,
491 			   &hba->debugfs_ee_rate_limit_ms);
492 
493 	if (!(hba->caps & UFSHCD_CAP_TX_EQUALIZATION))
494 		return;
495 
496 	for (u32 gear = UFS_HS_G1; gear <= UFS_HS_GEAR_MAX; gear++) {
497 		struct dentry *txeq_dir;
498 		char name[32];
499 
500 		snprintf(name, sizeof(name), "tx_eq_hs_gear%d", gear);
501 		txeq_dir = debugfs_create_dir(name, hba->debugfs_root);
502 		if (IS_ERR_OR_NULL(txeq_dir))
503 			return;
504 
505 		d_inode(txeq_dir)->i_private = hba;
506 
507 		/* Create files for TX Equalization parameters */
508 		for (attr = ufs_tx_eq_attrs; attr->name; attr++)
509 			debugfs_create_file(attr->name, attr->mode, txeq_dir,
510 					    (void *)(uintptr_t)gear,
511 					    attr->fops);
512 
513 		/* TX EQTR is supported for HS-G4 and higher Gears */
514 		if (gear < UFS_HS_G4)
515 			continue;
516 
517 		/* Create files for TX EQTR related attributes */
518 		for (attr = ufs_tx_eqtr_attrs; attr->name; attr++)
519 			debugfs_create_file(attr->name, attr->mode, txeq_dir,
520 					    (void *)(uintptr_t)gear,
521 					    attr->fops);
522 	}
523 }
524 
525 void ufs_debugfs_hba_exit(struct ufs_hba *hba)
526 {
527 	debugfs_remove_recursive(hba->debugfs_root);
528 	cancel_delayed_work_sync(&hba->debugfs_ee_work);
529 }
530