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