blob: 74a307c0a2407c45af6ad42796d4944a3bec2bed [file] [log] [blame]
Jing Huangab2a9ba2010-07-08 20:02:55 -07001/*
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>
Paul Gortmaker09703662011-05-27 09:37:25 -040019#include <linux/export.h>
Jing Huangab2a9ba2010-07-08 20:02:55 -070020
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070021#include "bfad_drv.h"
22#include "bfad_im.h"
Jing Huangab2a9ba2010-07-08 20:02:55 -070023
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:
Krishna Gudipati7c38c052011-04-14 16:50:35 -070032 * - bfa/pci_dev:<pci_name>
33 * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bfa
Jing Huangab2a9ba2010-07-08 20:02:55 -070034 *
Krishna Gudipati7c38c052011-04-14 16:50:35 -070035 * Debugging service available per pci_dev:
Jing Huangab2a9ba2010-07-08 20:02:55 -070036 * 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
43struct bfad_debug_info {
44 char *debug_buffer;
45 void *i_private;
46 int buffer_len;
47};
48
49static int
50bfad_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
68static int
69bfad_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);
Maggie Zhangf7f738122010-12-09 19:08:43 -080094 rc = bfa_ioc_debug_fwtrc(&bfad->bfa.ioc,
Jing Huangab2a9ba2010-07-08 20:02:55 -070095 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
112static int
113bfad_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);
Maggie Zhangf7f738122010-12-09 19:08:43 -0800138 rc = bfa_ioc_debug_fwsave(&bfad->bfa.ioc,
Jing Huangab2a9ba2010-07-08 20:02:55 -0700139 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
156static int
157bfad_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 */
173static loff_t
174bfad_debugfs_lseek(struct file *file, loff_t offset, int orig)
175{
Al Viroc04eba72013-06-17 17:45:46 +0400176 struct bfad_debug_info *debug = file->private_data;
177 return fixed_size_llseek(file, offset, orig,
178 debug->buffer_len);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700179}
180
181static ssize_t
182bfad_debugfs_read(struct file *file, char __user *buf,
183 size_t nbytes, loff_t *pos)
184{
185 struct bfad_debug_info *debug = file->private_data;
186
187 if (!debug || !debug->debug_buffer)
188 return 0;
189
Maggie52f94b62010-11-29 18:21:32 -0800190 return simple_read_from_buffer(buf, nbytes, pos,
Jing Huangab2a9ba2010-07-08 20:02:55 -0700191 debug->debug_buffer, debug->buffer_len);
192}
193
194#define BFA_REG_CT_ADDRSZ (0x40000)
195#define BFA_REG_CB_ADDRSZ (0x20000)
Krishna Gudipati3d7fc662011-06-24 20:28:17 -0700196#define BFA_REG_ADDRSZ(__ioc) \
197 ((u32)(bfa_asic_id_ctc(bfa_ioc_devid(__ioc)) ? \
198 BFA_REG_CT_ADDRSZ : BFA_REG_CB_ADDRSZ))
199#define BFA_REG_ADDRMSK(__ioc) (BFA_REG_ADDRSZ(__ioc) - 1)
Jing Huangab2a9ba2010-07-08 20:02:55 -0700200
201static bfa_status_t
202bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len)
203{
204 u8 area;
205
206 /* check [16:15] */
207 area = (offset >> 15) & 0x7;
208 if (area == 0) {
209 /* PCIe core register */
210 if ((offset + (len<<2)) > 0x8000) /* 8k dwords or 32KB */
211 return BFA_STATUS_EINVAL;
212 } else if (area == 0x1) {
213 /* CB 32 KB memory page */
214 if ((offset + (len<<2)) > 0x10000) /* 8k dwords or 32KB */
215 return BFA_STATUS_EINVAL;
216 } else {
217 /* CB register space 64KB */
Krishna Gudipati3d7fc662011-06-24 20:28:17 -0700218 if ((offset + (len<<2)) > BFA_REG_ADDRMSK(&bfa->ioc))
Jing Huangab2a9ba2010-07-08 20:02:55 -0700219 return BFA_STATUS_EINVAL;
220 }
221 return BFA_STATUS_OK;
222}
223
224static ssize_t
225bfad_debugfs_read_regrd(struct file *file, char __user *buf,
226 size_t nbytes, loff_t *pos)
227{
228 struct bfad_debug_info *regrd_debug = file->private_data;
229 struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
230 struct bfad_s *bfad = port->bfad;
231 ssize_t rc;
232
233 if (!bfad->regdata)
234 return 0;
235
Maggie52f94b62010-11-29 18:21:32 -0800236 rc = simple_read_from_buffer(buf, nbytes, pos,
Jing Huangab2a9ba2010-07-08 20:02:55 -0700237 bfad->regdata, bfad->reglen);
238
239 if ((*pos + nbytes) >= bfad->reglen) {
240 kfree(bfad->regdata);
241 bfad->regdata = NULL;
242 bfad->reglen = 0;
243 }
244
245 return rc;
246}
247
248static ssize_t
249bfad_debugfs_write_regrd(struct file *file, const char __user *buf,
250 size_t nbytes, loff_t *ppos)
251{
252 struct bfad_debug_info *regrd_debug = file->private_data;
253 struct bfad_port_s *port = (struct bfad_port_s *)regrd_debug->i_private;
254 struct bfad_s *bfad = port->bfad;
255 struct bfa_s *bfa = &bfad->bfa;
256 struct bfa_ioc_s *ioc = &bfa->ioc;
257 int addr, len, rc, i;
258 u32 *regbuf;
259 void __iomem *rb, *reg_addr;
260 unsigned long flags;
Maggie52f94b62010-11-29 18:21:32 -0800261 void *kern_buf;
Jing Huangab2a9ba2010-07-08 20:02:55 -0700262
Fabian Frederick9f30b672014-11-14 19:49:58 +0100263 kern_buf = memdup_user(buf, nbytes);
264 if (IS_ERR(kern_buf))
265 return PTR_ERR(kern_buf);
Maggie52f94b62010-11-29 18:21:32 -0800266
267 rc = sscanf(kern_buf, "%x:%x", &addr, &len);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700268 if (rc < 2) {
269 printk(KERN_INFO
270 "bfad[%d]: %s failed to read user buf\n",
271 bfad->inst_no, __func__);
Maggie52f94b62010-11-29 18:21:32 -0800272 kfree(kern_buf);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700273 return -EINVAL;
274 }
275
Maggie52f94b62010-11-29 18:21:32 -0800276 kfree(kern_buf);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700277 kfree(bfad->regdata);
278 bfad->regdata = NULL;
279 bfad->reglen = 0;
280
281 bfad->regdata = kzalloc(len << 2, GFP_KERNEL);
282 if (!bfad->regdata) {
283 printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n",
284 bfad->inst_no);
285 return -ENOMEM;
286 }
287
288 bfad->reglen = len << 2;
289 rb = bfa_ioc_bar0(ioc);
Krishna Gudipati3d7fc662011-06-24 20:28:17 -0700290 addr &= BFA_REG_ADDRMSK(ioc);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700291
292 /* offset and len sanity check */
293 rc = bfad_reg_offset_check(bfa, addr, len);
294 if (rc) {
295 printk(KERN_INFO "bfad[%d]: Failed reg offset check\n",
296 bfad->inst_no);
297 kfree(bfad->regdata);
298 bfad->regdata = NULL;
299 bfad->reglen = 0;
300 return -EINVAL;
301 }
302
303 reg_addr = rb + addr;
304 regbuf = (u32 *)bfad->regdata;
305 spin_lock_irqsave(&bfad->bfad_lock, flags);
306 for (i = 0; i < len; i++) {
Jing Huang53440262010-10-18 17:12:29 -0700307 *regbuf = readl(reg_addr);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700308 regbuf++;
309 reg_addr += sizeof(u32);
310 }
311 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
312
313 return nbytes;
314}
315
316static ssize_t
317bfad_debugfs_write_regwr(struct file *file, const char __user *buf,
318 size_t nbytes, loff_t *ppos)
319{
320 struct bfad_debug_info *debug = file->private_data;
321 struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private;
322 struct bfad_s *bfad = port->bfad;
323 struct bfa_s *bfa = &bfad->bfa;
324 struct bfa_ioc_s *ioc = &bfa->ioc;
325 int addr, val, rc;
326 void __iomem *reg_addr;
327 unsigned long flags;
Maggie52f94b62010-11-29 18:21:32 -0800328 void *kern_buf;
Jing Huangab2a9ba2010-07-08 20:02:55 -0700329
Fabian Frederick9f30b672014-11-14 19:49:58 +0100330 kern_buf = memdup_user(buf, nbytes);
331 if (IS_ERR(kern_buf))
332 return PTR_ERR(kern_buf);
Maggie52f94b62010-11-29 18:21:32 -0800333
334 rc = sscanf(kern_buf, "%x:%x", &addr, &val);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700335 if (rc < 2) {
336 printk(KERN_INFO
337 "bfad[%d]: %s failed to read user buf\n",
338 bfad->inst_no, __func__);
Maggie52f94b62010-11-29 18:21:32 -0800339 kfree(kern_buf);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700340 return -EINVAL;
341 }
Maggie52f94b62010-11-29 18:21:32 -0800342 kfree(kern_buf);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700343
Krishna Gudipati3d7fc662011-06-24 20:28:17 -0700344 addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
Jing Huangab2a9ba2010-07-08 20:02:55 -0700345
346 /* offset and len sanity check */
347 rc = bfad_reg_offset_check(bfa, addr, 1);
348 if (rc) {
349 printk(KERN_INFO
350 "bfad[%d]: Failed reg offset check\n",
351 bfad->inst_no);
352 return -EINVAL;
353 }
354
Maggie52f94b62010-11-29 18:21:32 -0800355 reg_addr = (bfa_ioc_bar0(ioc)) + addr;
Jing Huangab2a9ba2010-07-08 20:02:55 -0700356 spin_lock_irqsave(&bfad->bfad_lock, flags);
Jing Huang53440262010-10-18 17:12:29 -0700357 writel(val, reg_addr);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700358 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
359
360 return nbytes;
361}
362
363static int
364bfad_debugfs_release(struct inode *inode, struct file *file)
365{
366 struct bfad_debug_info *debug = file->private_data;
367
368 if (!debug)
369 return 0;
370
371 file->private_data = NULL;
372 kfree(debug);
373 return 0;
374}
375
376static int
377bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file)
378{
379 struct bfad_debug_info *fw_debug = file->private_data;
380
381 if (!fw_debug)
382 return 0;
383
384 if (fw_debug->debug_buffer)
385 vfree(fw_debug->debug_buffer);
386
387 file->private_data = NULL;
388 kfree(fw_debug);
389 return 0;
390}
391
392static const struct file_operations bfad_debugfs_op_drvtrc = {
393 .owner = THIS_MODULE,
394 .open = bfad_debugfs_open_drvtrc,
395 .llseek = bfad_debugfs_lseek,
396 .read = bfad_debugfs_read,
397 .release = bfad_debugfs_release,
398};
399
400static const struct file_operations bfad_debugfs_op_fwtrc = {
401 .owner = THIS_MODULE,
402 .open = bfad_debugfs_open_fwtrc,
403 .llseek = bfad_debugfs_lseek,
404 .read = bfad_debugfs_read,
405 .release = bfad_debugfs_release_fwtrc,
406};
407
408static const struct file_operations bfad_debugfs_op_fwsave = {
409 .owner = THIS_MODULE,
410 .open = bfad_debugfs_open_fwsave,
411 .llseek = bfad_debugfs_lseek,
412 .read = bfad_debugfs_read,
413 .release = bfad_debugfs_release_fwtrc,
414};
415
416static const struct file_operations bfad_debugfs_op_regrd = {
417 .owner = THIS_MODULE,
418 .open = bfad_debugfs_open_reg,
419 .llseek = bfad_debugfs_lseek,
420 .read = bfad_debugfs_read_regrd,
421 .write = bfad_debugfs_write_regrd,
422 .release = bfad_debugfs_release,
423};
424
425static const struct file_operations bfad_debugfs_op_regwr = {
426 .owner = THIS_MODULE,
427 .open = bfad_debugfs_open_reg,
428 .llseek = bfad_debugfs_lseek,
429 .write = bfad_debugfs_write_regwr,
430 .release = bfad_debugfs_release,
431};
432
433struct bfad_debugfs_entry {
434 const char *name;
Al Virof4ae40a62011-07-24 04:33:43 -0400435 umode_t mode;
Jing Huangab2a9ba2010-07-08 20:02:55 -0700436 const struct file_operations *fops;
437};
438
439static const struct bfad_debugfs_entry bfad_debugfs_files[] = {
440 { "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, },
441 { "fwtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc, },
442 { "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, },
443 { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd, },
444 { "regwr", S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr, },
445};
446
447static struct dentry *bfa_debugfs_root;
448static atomic_t bfa_debugfs_port_count;
449
450inline void
451bfad_debugfs_init(struct bfad_port_s *port)
452{
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700453 struct bfad_s *bfad = port->bfad;
Jing Huangab2a9ba2010-07-08 20:02:55 -0700454 const struct bfad_debugfs_entry *file;
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700455 char name[64];
Jing Huangab2a9ba2010-07-08 20:02:55 -0700456 int i;
457
458 if (!bfa_debugfs_enable)
459 return;
460
461 /* Setup the BFA debugfs root directory*/
462 if (!bfa_debugfs_root) {
463 bfa_debugfs_root = debugfs_create_dir("bfa", NULL);
464 atomic_set(&bfa_debugfs_port_count, 0);
465 if (!bfa_debugfs_root) {
466 printk(KERN_WARNING
467 "BFA debugfs root dir creation failed\n");
468 goto err;
469 }
470 }
471
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700472 /* Setup the pci_dev debugfs directory for the port */
473 snprintf(name, sizeof(name), "pci_dev:%s", bfad->pci_name);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700474 if (!port->port_debugfs_root) {
475 port->port_debugfs_root =
476 debugfs_create_dir(name, bfa_debugfs_root);
477 if (!port->port_debugfs_root) {
478 printk(KERN_WARNING
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700479 "bfa %s: debugfs root creation failed\n",
480 bfad->pci_name);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700481 goto err;
482 }
483
484 atomic_inc(&bfa_debugfs_port_count);
485
486 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
487 file = &bfad_debugfs_files[i];
488 bfad->bfad_dentry_files[i] =
489 debugfs_create_file(file->name,
490 file->mode,
491 port->port_debugfs_root,
492 port,
493 file->fops);
494 if (!bfad->bfad_dentry_files[i]) {
495 printk(KERN_WARNING
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700496 "bfa %s: debugfs %s creation failed\n",
497 bfad->pci_name, file->name);
Jing Huangab2a9ba2010-07-08 20:02:55 -0700498 goto err;
499 }
500 }
501 }
502
503err:
504 return;
505}
506
507inline void
508bfad_debugfs_exit(struct bfad_port_s *port)
509{
Krishna Gudipati7c38c052011-04-14 16:50:35 -0700510 struct bfad_s *bfad = port->bfad;
Jing Huangab2a9ba2010-07-08 20:02:55 -0700511 int i;
512
513 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
514 if (bfad->bfad_dentry_files[i]) {
515 debugfs_remove(bfad->bfad_dentry_files[i]);
516 bfad->bfad_dentry_files[i] = NULL;
517 }
518 }
519
Jing Huang275a63a2011-11-16 12:28:49 -0800520 /* Remove the pci_dev debugfs directory for the port */
Jing Huangab2a9ba2010-07-08 20:02:55 -0700521 if (port->port_debugfs_root) {
522 debugfs_remove(port->port_debugfs_root);
523 port->port_debugfs_root = NULL;
524 atomic_dec(&bfa_debugfs_port_count);
525 }
526
527 /* Remove the BFA debugfs root directory */
528 if (atomic_read(&bfa_debugfs_port_count) == 0) {
529 debugfs_remove(bfa_debugfs_root);
530 bfa_debugfs_root = NULL;
531 }
532}