xref: /openbmc/linux/drivers/scsi/bfa/bfad_debugfs.c (revision feac8c8b)
1 /*
2  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
3  * Copyright (c) 2014- QLogic Corporation.
4  * All rights reserved
5  * www.qlogic.com
6  *
7  * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License (GPL) Version 2 as
11  * published by the Free Software Foundation
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  */
18 
19 #include <linux/debugfs.h>
20 #include <linux/export.h>
21 
22 #include "bfad_drv.h"
23 #include "bfad_im.h"
24 
25 /*
26  * BFA debufs interface
27  *
28  * To access the interface, debugfs file system should be mounted
29  * if not already mounted using:
30  * mount -t debugfs none /sys/kernel/debug
31  *
32  * BFA Hierarchy:
33  *	- bfa/pci_dev:<pci_name>
34  * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bfa
35  *
36  * Debugging service available per pci_dev:
37  * fwtrc:  To collect current firmware trace.
38  * drvtrc: To collect current driver trace
39  * fwsave: To collect last saved fw trace as a result of firmware crash.
40  * regwr:  To write one word to chip register
41  * regrd:  To read one or more words from chip register.
42  */
43 
44 struct bfad_debug_info {
45 	char *debug_buffer;
46 	void *i_private;
47 	int buffer_len;
48 };
49 
50 static int
51 bfad_debugfs_open_drvtrc(struct inode *inode, struct file *file)
52 {
53 	struct bfad_port_s *port = inode->i_private;
54 	struct bfad_s *bfad = port->bfad;
55 	struct bfad_debug_info *debug;
56 
57 	debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
58 	if (!debug)
59 		return -ENOMEM;
60 
61 	debug->debug_buffer = (void *) bfad->trcmod;
62 	debug->buffer_len = sizeof(struct bfa_trc_mod_s);
63 
64 	file->private_data = debug;
65 
66 	return 0;
67 }
68 
69 static int
70 bfad_debugfs_open_fwtrc(struct inode *inode, struct file *file)
71 {
72 	struct bfad_port_s *port = inode->i_private;
73 	struct bfad_s *bfad = port->bfad;
74 	struct bfad_debug_info *fw_debug;
75 	unsigned long flags;
76 	int rc;
77 
78 	fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
79 	if (!fw_debug)
80 		return -ENOMEM;
81 
82 	fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
83 
84 	fw_debug->debug_buffer = vzalloc(fw_debug->buffer_len);
85 	if (!fw_debug->debug_buffer) {
86 		kfree(fw_debug);
87 		printk(KERN_INFO "bfad[%d]: Failed to allocate fwtrc buffer\n",
88 				bfad->inst_no);
89 		return -ENOMEM;
90 	}
91 
92 	spin_lock_irqsave(&bfad->bfad_lock, flags);
93 	rc = bfa_ioc_debug_fwtrc(&bfad->bfa.ioc,
94 			fw_debug->debug_buffer,
95 			&fw_debug->buffer_len);
96 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
97 	if (rc != BFA_STATUS_OK) {
98 		vfree(fw_debug->debug_buffer);
99 		fw_debug->debug_buffer = NULL;
100 		kfree(fw_debug);
101 		printk(KERN_INFO "bfad[%d]: Failed to collect fwtrc\n",
102 				bfad->inst_no);
103 		return -ENOMEM;
104 	}
105 
106 	file->private_data = fw_debug;
107 
108 	return 0;
109 }
110 
111 static int
112 bfad_debugfs_open_fwsave(struct inode *inode, struct file *file)
113 {
114 	struct bfad_port_s *port = inode->i_private;
115 	struct bfad_s *bfad = port->bfad;
116 	struct bfad_debug_info *fw_debug;
117 	unsigned long flags;
118 	int rc;
119 
120 	fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
121 	if (!fw_debug)
122 		return -ENOMEM;
123 
124 	fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
125 
126 	fw_debug->debug_buffer = vzalloc(fw_debug->buffer_len);
127 	if (!fw_debug->debug_buffer) {
128 		kfree(fw_debug);
129 		printk(KERN_INFO "bfad[%d]: Failed to allocate fwsave buffer\n",
130 				bfad->inst_no);
131 		return -ENOMEM;
132 	}
133 
134 	spin_lock_irqsave(&bfad->bfad_lock, flags);
135 	rc = bfa_ioc_debug_fwsave(&bfad->bfa.ioc,
136 			fw_debug->debug_buffer,
137 			&fw_debug->buffer_len);
138 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
139 	if (rc != BFA_STATUS_OK) {
140 		vfree(fw_debug->debug_buffer);
141 		fw_debug->debug_buffer = NULL;
142 		kfree(fw_debug);
143 		printk(KERN_INFO "bfad[%d]: Failed to collect fwsave\n",
144 				bfad->inst_no);
145 		return -ENOMEM;
146 	}
147 
148 	file->private_data = fw_debug;
149 
150 	return 0;
151 }
152 
153 static int
154 bfad_debugfs_open_reg(struct inode *inode, struct file *file)
155 {
156 	struct bfad_debug_info *reg_debug;
157 
158 	reg_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
159 	if (!reg_debug)
160 		return -ENOMEM;
161 
162 	reg_debug->i_private = inode->i_private;
163 
164 	file->private_data = reg_debug;
165 
166 	return 0;
167 }
168 
169 /* Changes the current file position */
170 static loff_t
171 bfad_debugfs_lseek(struct file *file, loff_t offset, int orig)
172 {
173 	struct bfad_debug_info *debug = file->private_data;
174 	return fixed_size_llseek(file, offset, orig,
175 				debug->buffer_len);
176 }
177 
178 static ssize_t
179 bfad_debugfs_read(struct file *file, char __user *buf,
180 			size_t nbytes, loff_t *pos)
181 {
182 	struct bfad_debug_info *debug = file->private_data;
183 
184 	if (!debug || !debug->debug_buffer)
185 		return 0;
186 
187 	return simple_read_from_buffer(buf, nbytes, pos,
188 				debug->debug_buffer, debug->buffer_len);
189 }
190 
191 #define BFA_REG_CT_ADDRSZ	(0x40000)
192 #define BFA_REG_CB_ADDRSZ	(0x20000)
193 #define BFA_REG_ADDRSZ(__ioc)	\
194 	((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ?	\
195 	 BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
196 #define BFA_REG_ADDRMSK(__ioc)	(BFA_REG_ADDRSZ(__ioc) - 1)
197 
198 static bfa_status_t
199 bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len)
200 {
201 	u8	area;
202 
203 	/* check [16:15] */
204 	area = (offset >> 15) & 0x7;
205 	if (area == 0) {
206 		/* PCIe core register */
207 		if ((offset + (len<<2)) > 0x8000)    /* 8k dwords or 32KB */
208 			return BFA_STATUS_EINVAL;
209 	} else if (area == 0x1) {
210 		/* CB 32 KB memory page */
211 		if ((offset + (len<<2)) > 0x10000)    /* 8k dwords or 32KB */
212 			return BFA_STATUS_EINVAL;
213 	} else {
214 		/* CB register space 64KB */
215 		if ((offset + (len<<2)) > BFA_REG_ADDRMSK(&bfa->ioc))
216 			return BFA_STATUS_EINVAL;
217 	}
218 	return BFA_STATUS_OK;
219 }
220 
221 static ssize_t
222 bfad_debugfs_read_regrd(struct file *file, char __user *buf,
223 		size_t nbytes, loff_t *pos)
224 {
225 	struct bfad_debug_info *regrd_debug = file->private_data;
226 	struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
227 	struct bfad_s *bfad = port->bfad;
228 	ssize_t rc;
229 
230 	if (!bfad->regdata)
231 		return 0;
232 
233 	rc = simple_read_from_buffer(buf, nbytes, pos,
234 			bfad->regdata, bfad->reglen);
235 
236 	if ((*pos + nbytes) >= bfad->reglen) {
237 		kfree(bfad->regdata);
238 		bfad->regdata = NULL;
239 		bfad->reglen = 0;
240 	}
241 
242 	return rc;
243 }
244 
245 static ssize_t
246 bfad_debugfs_write_regrd(struct file *file, const char __user *buf,
247 		size_t nbytes, loff_t *ppos)
248 {
249 	struct bfad_debug_info *regrd_debug = file->private_data;
250 	struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
251 	struct bfad_s *bfad = port->bfad;
252 	struct bfa_s *bfa = &bfad->bfa;
253 	struct bfa_ioc_s *ioc = &bfa->ioc;
254 	int addr, rc, i;
255 	u32 len;
256 	u32 *regbuf;
257 	void __iomem *rb, *reg_addr;
258 	unsigned long flags;
259 	void *kern_buf;
260 
261 	kern_buf = memdup_user(buf, nbytes);
262 	if (IS_ERR(kern_buf))
263 		return PTR_ERR(kern_buf);
264 
265 	rc = sscanf(kern_buf, "%x:%x", &addr, &len);
266 	if (rc < 2 || len > (UINT_MAX >> 2)) {
267 		printk(KERN_INFO
268 			"bfad[%d]: %s failed to read user buf\n",
269 			bfad->inst_no, __func__);
270 		kfree(kern_buf);
271 		return -EINVAL;
272 	}
273 
274 	kfree(kern_buf);
275 	kfree(bfad->regdata);
276 	bfad->regdata = NULL;
277 	bfad->reglen = 0;
278 
279 	bfad->regdata = kzalloc(len << 2, GFP_KERNEL);
280 	if (!bfad->regdata) {
281 		printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n",
282 				bfad->inst_no);
283 		return -ENOMEM;
284 	}
285 
286 	bfad->reglen = len << 2;
287 	rb = bfa_ioc_bar0(ioc);
288 	addr &= BFA_REG_ADDRMSK(ioc);
289 
290 	/* offset and len sanity check */
291 	rc = bfad_reg_offset_check(bfa, addr, len);
292 	if (rc) {
293 		printk(KERN_INFO "bfad[%d]: Failed reg offset check\n",
294 				bfad->inst_no);
295 		kfree(bfad->regdata);
296 		bfad->regdata = NULL;
297 		bfad->reglen = 0;
298 		return -EINVAL;
299 	}
300 
301 	reg_addr = rb + addr;
302 	regbuf =  (u32 *)bfad->regdata;
303 	spin_lock_irqsave(&bfad->bfad_lock, flags);
304 	for (i = 0; i < len; i++) {
305 		*regbuf = readl(reg_addr);
306 		regbuf++;
307 		reg_addr += sizeof(u32);
308 	}
309 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
310 
311 	return nbytes;
312 }
313 
314 static ssize_t
315 bfad_debugfs_write_regwr(struct file *file, const char __user *buf,
316 		size_t nbytes, loff_t *ppos)
317 {
318 	struct bfad_debug_info *debug = file->private_data;
319 	struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private;
320 	struct bfad_s *bfad = port->bfad;
321 	struct bfa_s *bfa = &bfad->bfa;
322 	struct bfa_ioc_s *ioc = &bfa->ioc;
323 	int addr, val, rc;
324 	void __iomem *reg_addr;
325 	unsigned long flags;
326 	void *kern_buf;
327 
328 	kern_buf = memdup_user(buf, nbytes);
329 	if (IS_ERR(kern_buf))
330 		return PTR_ERR(kern_buf);
331 
332 	rc = sscanf(kern_buf, "%x:%x", &addr, &val);
333 	if (rc < 2) {
334 		printk(KERN_INFO
335 			"bfad[%d]: %s failed to read user buf\n",
336 			bfad->inst_no, __func__);
337 		kfree(kern_buf);
338 		return -EINVAL;
339 	}
340 	kfree(kern_buf);
341 
342 	addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
343 
344 	/* offset and len sanity check */
345 	rc = bfad_reg_offset_check(bfa, addr, 1);
346 	if (rc) {
347 		printk(KERN_INFO
348 			"bfad[%d]: Failed reg offset check\n",
349 			bfad->inst_no);
350 		return -EINVAL;
351 	}
352 
353 	reg_addr = (bfa_ioc_bar0(ioc)) + addr;
354 	spin_lock_irqsave(&bfad->bfad_lock, flags);
355 	writel(val, reg_addr);
356 	spin_unlock_irqrestore(&bfad->bfad_lock, flags);
357 
358 	return nbytes;
359 }
360 
361 static int
362 bfad_debugfs_release(struct inode *inode, struct file *file)
363 {
364 	struct bfad_debug_info *debug = file->private_data;
365 
366 	if (!debug)
367 		return 0;
368 
369 	file->private_data = NULL;
370 	kfree(debug);
371 	return 0;
372 }
373 
374 static int
375 bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file)
376 {
377 	struct bfad_debug_info *fw_debug = file->private_data;
378 
379 	if (!fw_debug)
380 		return 0;
381 
382 	if (fw_debug->debug_buffer)
383 		vfree(fw_debug->debug_buffer);
384 
385 	file->private_data = NULL;
386 	kfree(fw_debug);
387 	return 0;
388 }
389 
390 static const struct file_operations bfad_debugfs_op_drvtrc = {
391 	.owner		=	THIS_MODULE,
392 	.open		=	bfad_debugfs_open_drvtrc,
393 	.llseek		=	bfad_debugfs_lseek,
394 	.read		=	bfad_debugfs_read,
395 	.release	=	bfad_debugfs_release,
396 };
397 
398 static const struct file_operations bfad_debugfs_op_fwtrc = {
399 	.owner		=	THIS_MODULE,
400 	.open		=	bfad_debugfs_open_fwtrc,
401 	.llseek		=	bfad_debugfs_lseek,
402 	.read		=	bfad_debugfs_read,
403 	.release	=	bfad_debugfs_release_fwtrc,
404 };
405 
406 static const struct file_operations bfad_debugfs_op_fwsave = {
407 	.owner		=	THIS_MODULE,
408 	.open		=	bfad_debugfs_open_fwsave,
409 	.llseek		=	bfad_debugfs_lseek,
410 	.read		=	bfad_debugfs_read,
411 	.release	=	bfad_debugfs_release_fwtrc,
412 };
413 
414 static const struct file_operations bfad_debugfs_op_regrd = {
415 	.owner		=	THIS_MODULE,
416 	.open		=	bfad_debugfs_open_reg,
417 	.llseek		=	bfad_debugfs_lseek,
418 	.read		=	bfad_debugfs_read_regrd,
419 	.write		=	bfad_debugfs_write_regrd,
420 	.release	=	bfad_debugfs_release,
421 };
422 
423 static const struct file_operations bfad_debugfs_op_regwr = {
424 	.owner		=	THIS_MODULE,
425 	.open		=	bfad_debugfs_open_reg,
426 	.llseek		=	bfad_debugfs_lseek,
427 	.write		=	bfad_debugfs_write_regwr,
428 	.release	=	bfad_debugfs_release,
429 };
430 
431 struct bfad_debugfs_entry {
432 	const char *name;
433 	umode_t	mode;
434 	const struct file_operations *fops;
435 };
436 
437 static const struct bfad_debugfs_entry bfad_debugfs_files[] = {
438 	{ "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, },
439 	{ "fwtrc",  S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc,  },
440 	{ "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, },
441 	{ "regrd",  S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd,  },
442 	{ "regwr",  S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr,  },
443 };
444 
445 static struct dentry *bfa_debugfs_root;
446 static atomic_t bfa_debugfs_port_count;
447 
448 inline void
449 bfad_debugfs_init(struct bfad_port_s *port)
450 {
451 	struct bfad_s *bfad = port->bfad;
452 	const struct bfad_debugfs_entry *file;
453 	char name[64];
454 	int i;
455 
456 	if (!bfa_debugfs_enable)
457 		return;
458 
459 	/* Setup the BFA debugfs root directory*/
460 	if (!bfa_debugfs_root) {
461 		bfa_debugfs_root = debugfs_create_dir("bfa", NULL);
462 		atomic_set(&bfa_debugfs_port_count, 0);
463 		if (!bfa_debugfs_root) {
464 			printk(KERN_WARNING
465 				"BFA debugfs root dir creation failed\n");
466 			goto err;
467 		}
468 	}
469 
470 	/* Setup the pci_dev debugfs directory for the port */
471 	snprintf(name, sizeof(name), "pci_dev:%s", bfad->pci_name);
472 	if (!port->port_debugfs_root) {
473 		port->port_debugfs_root =
474 			debugfs_create_dir(name, bfa_debugfs_root);
475 		if (!port->port_debugfs_root) {
476 			printk(KERN_WARNING
477 				"bfa %s: debugfs root creation failed\n",
478 				bfad->pci_name);
479 			goto err;
480 		}
481 
482 		atomic_inc(&bfa_debugfs_port_count);
483 
484 		for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
485 			file = &bfad_debugfs_files[i];
486 			bfad->bfad_dentry_files[i] =
487 					debugfs_create_file(file->name,
488 							file->mode,
489 							port->port_debugfs_root,
490 							port,
491 							file->fops);
492 			if (!bfad->bfad_dentry_files[i]) {
493 				printk(KERN_WARNING
494 					"bfa %s: debugfs %s creation failed\n",
495 					bfad->pci_name, file->name);
496 				goto err;
497 			}
498 		}
499 	}
500 
501 err:
502 	return;
503 }
504 
505 inline void
506 bfad_debugfs_exit(struct bfad_port_s *port)
507 {
508 	struct bfad_s *bfad = port->bfad;
509 	int i;
510 
511 	for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
512 		if (bfad->bfad_dentry_files[i]) {
513 			debugfs_remove(bfad->bfad_dentry_files[i]);
514 			bfad->bfad_dentry_files[i] = NULL;
515 		}
516 	}
517 
518 	/* Remove the pci_dev debugfs directory for the port */
519 	if (port->port_debugfs_root) {
520 		debugfs_remove(port->port_debugfs_root);
521 		port->port_debugfs_root = NULL;
522 		atomic_dec(&bfa_debugfs_port_count);
523 	}
524 
525 	/* Remove the BFA debugfs root directory */
526 	if (atomic_read(&bfa_debugfs_port_count) == 0) {
527 		debugfs_remove(bfa_debugfs_root);
528 		bfa_debugfs_root = NULL;
529 	}
530 }
531