HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / drivers / scsi / bfa / bfad_debugfs.c
blob8f1c58d4d5b5cd51eef59719b07e723ea90dbe1f
1 /*
2 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3 * All rights reserved
4 * www.brocade.com
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
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
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.
18 #include <linux/debugfs.h>
19 #include <linux/export.h>
21 #include "bfad_drv.h"
22 #include "bfad_im.h"
25 * BFA debufs interface
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
31 * BFA Hierarchy:
32 * - bfa/pci_dev:<pci_name>
33 * where the pci_name corresponds to the one under /sys/bus/pci/drivers/bfa
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.
43 struct bfad_debug_info {
44 char *debug_buffer;
45 void *i_private;
46 int buffer_len;
49 static int
50 bfad_debugfs_open_drvtrc(struct inode *inode, struct file *file)
52 struct bfad_port_s *port = inode->i_private;
53 struct bfad_s *bfad = port->bfad;
54 struct bfad_debug_info *debug;
56 debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
57 if (!debug)
58 return -ENOMEM;
60 debug->debug_buffer = (void *) bfad->trcmod;
61 debug->buffer_len = sizeof(struct bfa_trc_mod_s);
63 file->private_data = debug;
65 return 0;
68 static int
69 bfad_debugfs_open_fwtrc(struct inode *inode, struct file *file)
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;
77 fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
78 if (!fw_debug)
79 return -ENOMEM;
81 fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
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;
91 memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len);
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;
107 file->private_data = fw_debug;
109 return 0;
112 static int
113 bfad_debugfs_open_fwsave(struct inode *inode, struct file *file)
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;
121 fw_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
122 if (!fw_debug)
123 return -ENOMEM;
125 fw_debug->buffer_len = sizeof(struct bfa_trc_mod_s);
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;
135 memset(fw_debug->debug_buffer, 0, fw_debug->buffer_len);
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;
151 file->private_data = fw_debug;
153 return 0;
156 static int
157 bfad_debugfs_open_reg(struct inode *inode, struct file *file)
159 struct bfad_debug_info *reg_debug;
161 reg_debug = kzalloc(sizeof(struct bfad_debug_info), GFP_KERNEL);
162 if (!reg_debug)
163 return -ENOMEM;
165 reg_debug->i_private = inode->i_private;
167 file->private_data = reg_debug;
169 return 0;
172 /* Changes the current file position */
173 static loff_t
174 bfad_debugfs_lseek(struct file *file, loff_t offset, int orig)
176 struct bfad_debug_info *debug = file->private_data;
177 return fixed_size_llseek(file, offset, orig,
178 debug->buffer_len);
181 static ssize_t
182 bfad_debugfs_read(struct file *file, char __user *buf,
183 size_t nbytes, loff_t *pos)
185 struct bfad_debug_info *debug = file->private_data;
187 if (!debug || !debug->debug_buffer)
188 return 0;
190 return simple_read_from_buffer(buf, nbytes, pos,
191 debug->debug_buffer, debug->buffer_len);
194 #define BFA_REG_CT_ADDRSZ (0x40000)
195 #define BFA_REG_CB_ADDRSZ (0x20000)
196 #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)
201 static bfa_status_t
202 bfad_reg_offset_check(struct bfa_s *bfa, u32 offset, u32 len)
204 u8 area;
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 */
218 if ((offset + (len<<2)) > BFA_REG_ADDRMSK(&bfa->ioc))
219 return BFA_STATUS_EINVAL;
221 return BFA_STATUS_OK;
224 static ssize_t
225 bfad_debugfs_read_regrd(struct file *file, char __user *buf,
226 size_t nbytes, loff_t *pos)
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;
233 if (!bfad->regdata)
234 return 0;
236 rc = simple_read_from_buffer(buf, nbytes, pos,
237 bfad->regdata, bfad->reglen);
239 if ((*pos + nbytes) >= bfad->reglen) {
240 kfree(bfad->regdata);
241 bfad->regdata = NULL;
242 bfad->reglen = 0;
245 return rc;
248 static ssize_t
249 bfad_debugfs_write_regrd(struct file *file, const char __user *buf,
250 size_t nbytes, loff_t *ppos)
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, rc, i;
258 u32 len;
259 u32 *regbuf;
260 void __iomem *rb, *reg_addr;
261 unsigned long flags;
262 void *kern_buf;
264 kern_buf = memdup_user(buf, nbytes);
265 if (IS_ERR(kern_buf))
266 return PTR_ERR(kern_buf);
268 rc = sscanf(kern_buf, "%x:%x", &addr, &len);
269 if (rc < 2 || len > (UINT_MAX >> 2)) {
270 printk(KERN_INFO
271 "bfad[%d]: %s failed to read user buf\n",
272 bfad->inst_no, __func__);
273 kfree(kern_buf);
274 return -EINVAL;
277 kfree(kern_buf);
278 kfree(bfad->regdata);
279 bfad->regdata = NULL;
280 bfad->reglen = 0;
282 bfad->regdata = kzalloc(len << 2, GFP_KERNEL);
283 if (!bfad->regdata) {
284 printk(KERN_INFO "bfad[%d]: Failed to allocate regrd buffer\n",
285 bfad->inst_no);
286 return -ENOMEM;
289 bfad->reglen = len << 2;
290 rb = bfa_ioc_bar0(ioc);
291 addr &= BFA_REG_ADDRMSK(ioc);
293 /* offset and len sanity check */
294 rc = bfad_reg_offset_check(bfa, addr, len);
295 if (rc) {
296 printk(KERN_INFO "bfad[%d]: Failed reg offset check\n",
297 bfad->inst_no);
298 kfree(bfad->regdata);
299 bfad->regdata = NULL;
300 bfad->reglen = 0;
301 return -EINVAL;
304 reg_addr = rb + addr;
305 regbuf = (u32 *)bfad->regdata;
306 spin_lock_irqsave(&bfad->bfad_lock, flags);
307 for (i = 0; i < len; i++) {
308 *regbuf = readl(reg_addr);
309 regbuf++;
310 reg_addr += sizeof(u32);
312 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
314 return nbytes;
317 static ssize_t
318 bfad_debugfs_write_regwr(struct file *file, const char __user *buf,
319 size_t nbytes, loff_t *ppos)
321 struct bfad_debug_info *debug = file->private_data;
322 struct bfad_port_s *port = (struct bfad_port_s *)debug->i_private;
323 struct bfad_s *bfad = port->bfad;
324 struct bfa_s *bfa = &bfad->bfa;
325 struct bfa_ioc_s *ioc = &bfa->ioc;
326 int addr, val, rc;
327 void __iomem *reg_addr;
328 unsigned long flags;
329 void *kern_buf;
331 kern_buf = memdup_user(buf, nbytes);
332 if (IS_ERR(kern_buf))
333 return PTR_ERR(kern_buf);
335 rc = sscanf(kern_buf, "%x:%x", &addr, &val);
336 if (rc < 2) {
337 printk(KERN_INFO
338 "bfad[%d]: %s failed to read user buf\n",
339 bfad->inst_no, __func__);
340 kfree(kern_buf);
341 return -EINVAL;
343 kfree(kern_buf);
345 addr &= BFA_REG_ADDRMSK(ioc); /* offset only 17 bit and word align */
347 /* offset and len sanity check */
348 rc = bfad_reg_offset_check(bfa, addr, 1);
349 if (rc) {
350 printk(KERN_INFO
351 "bfad[%d]: Failed reg offset check\n",
352 bfad->inst_no);
353 return -EINVAL;
356 reg_addr = (bfa_ioc_bar0(ioc)) + addr;
357 spin_lock_irqsave(&bfad->bfad_lock, flags);
358 writel(val, reg_addr);
359 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
361 return nbytes;
364 static int
365 bfad_debugfs_release(struct inode *inode, struct file *file)
367 struct bfad_debug_info *debug = file->private_data;
369 if (!debug)
370 return 0;
372 file->private_data = NULL;
373 kfree(debug);
374 return 0;
377 static int
378 bfad_debugfs_release_fwtrc(struct inode *inode, struct file *file)
380 struct bfad_debug_info *fw_debug = file->private_data;
382 if (!fw_debug)
383 return 0;
385 if (fw_debug->debug_buffer)
386 vfree(fw_debug->debug_buffer);
388 file->private_data = NULL;
389 kfree(fw_debug);
390 return 0;
393 static const struct file_operations bfad_debugfs_op_drvtrc = {
394 .owner = THIS_MODULE,
395 .open = bfad_debugfs_open_drvtrc,
396 .llseek = bfad_debugfs_lseek,
397 .read = bfad_debugfs_read,
398 .release = bfad_debugfs_release,
401 static const struct file_operations bfad_debugfs_op_fwtrc = {
402 .owner = THIS_MODULE,
403 .open = bfad_debugfs_open_fwtrc,
404 .llseek = bfad_debugfs_lseek,
405 .read = bfad_debugfs_read,
406 .release = bfad_debugfs_release_fwtrc,
409 static const struct file_operations bfad_debugfs_op_fwsave = {
410 .owner = THIS_MODULE,
411 .open = bfad_debugfs_open_fwsave,
412 .llseek = bfad_debugfs_lseek,
413 .read = bfad_debugfs_read,
414 .release = bfad_debugfs_release_fwtrc,
417 static const struct file_operations bfad_debugfs_op_regrd = {
418 .owner = THIS_MODULE,
419 .open = bfad_debugfs_open_reg,
420 .llseek = bfad_debugfs_lseek,
421 .read = bfad_debugfs_read_regrd,
422 .write = bfad_debugfs_write_regrd,
423 .release = bfad_debugfs_release,
426 static const struct file_operations bfad_debugfs_op_regwr = {
427 .owner = THIS_MODULE,
428 .open = bfad_debugfs_open_reg,
429 .llseek = bfad_debugfs_lseek,
430 .write = bfad_debugfs_write_regwr,
431 .release = bfad_debugfs_release,
434 struct bfad_debugfs_entry {
435 const char *name;
436 umode_t mode;
437 const struct file_operations *fops;
440 static const struct bfad_debugfs_entry bfad_debugfs_files[] = {
441 { "drvtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_drvtrc, },
442 { "fwtrc", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwtrc, },
443 { "fwsave", S_IFREG|S_IRUGO, &bfad_debugfs_op_fwsave, },
444 { "regrd", S_IFREG|S_IRUGO|S_IWUSR, &bfad_debugfs_op_regrd, },
445 { "regwr", S_IFREG|S_IWUSR, &bfad_debugfs_op_regwr, },
448 static struct dentry *bfa_debugfs_root;
449 static atomic_t bfa_debugfs_port_count;
451 inline void
452 bfad_debugfs_init(struct bfad_port_s *port)
454 struct bfad_s *bfad = port->bfad;
455 const struct bfad_debugfs_entry *file;
456 char name[64];
457 int i;
459 if (!bfa_debugfs_enable)
460 return;
462 /* Setup the BFA debugfs root directory*/
463 if (!bfa_debugfs_root) {
464 bfa_debugfs_root = debugfs_create_dir("bfa", NULL);
465 atomic_set(&bfa_debugfs_port_count, 0);
466 if (!bfa_debugfs_root) {
467 printk(KERN_WARNING
468 "BFA debugfs root dir creation failed\n");
469 goto err;
473 /* Setup the pci_dev debugfs directory for the port */
474 snprintf(name, sizeof(name), "pci_dev:%s", bfad->pci_name);
475 if (!port->port_debugfs_root) {
476 port->port_debugfs_root =
477 debugfs_create_dir(name, bfa_debugfs_root);
478 if (!port->port_debugfs_root) {
479 printk(KERN_WARNING
480 "bfa %s: debugfs root creation failed\n",
481 bfad->pci_name);
482 goto err;
485 atomic_inc(&bfa_debugfs_port_count);
487 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
488 file = &bfad_debugfs_files[i];
489 bfad->bfad_dentry_files[i] =
490 debugfs_create_file(file->name,
491 file->mode,
492 port->port_debugfs_root,
493 port,
494 file->fops);
495 if (!bfad->bfad_dentry_files[i]) {
496 printk(KERN_WARNING
497 "bfa %s: debugfs %s creation failed\n",
498 bfad->pci_name, file->name);
499 goto err;
504 err:
505 return;
508 inline void
509 bfad_debugfs_exit(struct bfad_port_s *port)
511 struct bfad_s *bfad = port->bfad;
512 int i;
514 for (i = 0; i < ARRAY_SIZE(bfad_debugfs_files); i++) {
515 if (bfad->bfad_dentry_files[i]) {
516 debugfs_remove(bfad->bfad_dentry_files[i]);
517 bfad->bfad_dentry_files[i] = NULL;
521 /* Remove the pci_dev debugfs directory for the port */
522 if (port->port_debugfs_root) {
523 debugfs_remove(port->port_debugfs_root);
524 port->port_debugfs_root = NULL;
525 atomic_dec(&bfa_debugfs_port_count);
528 /* Remove the BFA debugfs root directory */
529 if (atomic_read(&bfa_debugfs_port_count) == 0) {
530 debugfs_remove(bfa_debugfs_root);
531 bfa_debugfs_root = NULL;