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