uwb: Use kcalloc instead of kzalloc to allocate array
[zen-stable.git] / drivers / infiniband / hw / qib / qib_fs.c
blobdf7fa251dcdc5e6e32135be4d1867398d63588e3
1 /*
2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2006 PathScale, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
34 #include <linux/module.h>
35 #include <linux/fs.h>
36 #include <linux/mount.h>
37 #include <linux/pagemap.h>
38 #include <linux/init.h>
39 #include <linux/namei.h>
41 #include "qib.h"
43 #define QIBFS_MAGIC 0x726a77
45 static struct super_block *qib_super;
47 #define private2dd(file) ((file)->f_dentry->d_inode->i_private)
49 static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
50 int mode, const struct file_operations *fops,
51 void *data)
53 int error;
54 struct inode *inode = new_inode(dir->i_sb);
56 if (!inode) {
57 error = -EPERM;
58 goto bail;
61 inode->i_ino = get_next_ino();
62 inode->i_mode = mode;
63 inode->i_uid = 0;
64 inode->i_gid = 0;
65 inode->i_blocks = 0;
66 inode->i_atime = CURRENT_TIME;
67 inode->i_mtime = inode->i_atime;
68 inode->i_ctime = inode->i_atime;
69 inode->i_private = data;
70 if ((mode & S_IFMT) == S_IFDIR) {
71 inode->i_op = &simple_dir_inode_operations;
72 inc_nlink(inode);
73 inc_nlink(dir);
76 inode->i_fop = fops;
78 d_instantiate(dentry, inode);
79 error = 0;
81 bail:
82 return error;
85 static int create_file(const char *name, mode_t mode,
86 struct dentry *parent, struct dentry **dentry,
87 const struct file_operations *fops, void *data)
89 int error;
91 *dentry = NULL;
92 mutex_lock(&parent->d_inode->i_mutex);
93 *dentry = lookup_one_len(name, parent, strlen(name));
94 if (!IS_ERR(*dentry))
95 error = qibfs_mknod(parent->d_inode, *dentry,
96 mode, fops, data);
97 else
98 error = PTR_ERR(*dentry);
99 mutex_unlock(&parent->d_inode->i_mutex);
101 return error;
104 static ssize_t driver_stats_read(struct file *file, char __user *buf,
105 size_t count, loff_t *ppos)
107 return simple_read_from_buffer(buf, count, ppos, &qib_stats,
108 sizeof qib_stats);
112 * driver stats field names, one line per stat, single string. Used by
113 * programs like ipathstats to print the stats in a way which works for
114 * different versions of drivers, without changing program source.
115 * if qlogic_ib_stats changes, this needs to change. Names need to be
116 * 12 chars or less (w/o newline), for proper display by ipathstats utility.
118 static const char qib_statnames[] =
119 "KernIntr\n"
120 "ErrorIntr\n"
121 "Tx_Errs\n"
122 "Rcv_Errs\n"
123 "H/W_Errs\n"
124 "NoPIOBufs\n"
125 "CtxtsOpen\n"
126 "RcvLen_Errs\n"
127 "EgrBufFull\n"
128 "EgrHdrFull\n"
131 static ssize_t driver_names_read(struct file *file, char __user *buf,
132 size_t count, loff_t *ppos)
134 return simple_read_from_buffer(buf, count, ppos, qib_statnames,
135 sizeof qib_statnames - 1); /* no null */
138 static const struct file_operations driver_ops[] = {
139 { .read = driver_stats_read, .llseek = generic_file_llseek, },
140 { .read = driver_names_read, .llseek = generic_file_llseek, },
143 /* read the per-device counters */
144 static ssize_t dev_counters_read(struct file *file, char __user *buf,
145 size_t count, loff_t *ppos)
147 u64 *counters;
148 size_t avail;
149 struct qib_devdata *dd = private2dd(file);
151 avail = dd->f_read_cntrs(dd, *ppos, NULL, &counters);
152 return simple_read_from_buffer(buf, count, ppos, counters, avail);
155 /* read the per-device counters */
156 static ssize_t dev_names_read(struct file *file, char __user *buf,
157 size_t count, loff_t *ppos)
159 char *names;
160 size_t avail;
161 struct qib_devdata *dd = private2dd(file);
163 avail = dd->f_read_cntrs(dd, *ppos, &names, NULL);
164 return simple_read_from_buffer(buf, count, ppos, names, avail);
167 static const struct file_operations cntr_ops[] = {
168 { .read = dev_counters_read, .llseek = generic_file_llseek, },
169 { .read = dev_names_read, .llseek = generic_file_llseek, },
173 * Could use file->f_dentry->d_inode->i_ino to figure out which file,
174 * instead of separate routine for each, but for now, this works...
177 /* read the per-port names (same for each port) */
178 static ssize_t portnames_read(struct file *file, char __user *buf,
179 size_t count, loff_t *ppos)
181 char *names;
182 size_t avail;
183 struct qib_devdata *dd = private2dd(file);
185 avail = dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL);
186 return simple_read_from_buffer(buf, count, ppos, names, avail);
189 /* read the per-port counters for port 1 (pidx 0) */
190 static ssize_t portcntrs_1_read(struct file *file, char __user *buf,
191 size_t count, loff_t *ppos)
193 u64 *counters;
194 size_t avail;
195 struct qib_devdata *dd = private2dd(file);
197 avail = dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters);
198 return simple_read_from_buffer(buf, count, ppos, counters, avail);
201 /* read the per-port counters for port 2 (pidx 1) */
202 static ssize_t portcntrs_2_read(struct file *file, char __user *buf,
203 size_t count, loff_t *ppos)
205 u64 *counters;
206 size_t avail;
207 struct qib_devdata *dd = private2dd(file);
209 avail = dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters);
210 return simple_read_from_buffer(buf, count, ppos, counters, avail);
213 static const struct file_operations portcntr_ops[] = {
214 { .read = portnames_read, .llseek = generic_file_llseek, },
215 { .read = portcntrs_1_read, .llseek = generic_file_llseek, },
216 { .read = portcntrs_2_read, .llseek = generic_file_llseek, },
220 * read the per-port QSFP data for port 1 (pidx 0)
222 static ssize_t qsfp_1_read(struct file *file, char __user *buf,
223 size_t count, loff_t *ppos)
225 struct qib_devdata *dd = private2dd(file);
226 char *tmp;
227 int ret;
229 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
230 if (!tmp)
231 return -ENOMEM;
233 ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
234 if (ret > 0)
235 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
236 kfree(tmp);
237 return ret;
241 * read the per-port QSFP data for port 2 (pidx 1)
243 static ssize_t qsfp_2_read(struct file *file, char __user *buf,
244 size_t count, loff_t *ppos)
246 struct qib_devdata *dd = private2dd(file);
247 char *tmp;
248 int ret;
250 if (dd->num_pports < 2)
251 return -ENODEV;
253 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
254 if (!tmp)
255 return -ENOMEM;
257 ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
258 if (ret > 0)
259 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
260 kfree(tmp);
261 return ret;
264 static const struct file_operations qsfp_ops[] = {
265 { .read = qsfp_1_read, .llseek = generic_file_llseek, },
266 { .read = qsfp_2_read, .llseek = generic_file_llseek, },
269 static ssize_t flash_read(struct file *file, char __user *buf,
270 size_t count, loff_t *ppos)
272 struct qib_devdata *dd;
273 ssize_t ret;
274 loff_t pos;
275 char *tmp;
277 pos = *ppos;
279 if (pos < 0) {
280 ret = -EINVAL;
281 goto bail;
284 if (pos >= sizeof(struct qib_flash)) {
285 ret = 0;
286 goto bail;
289 if (count > sizeof(struct qib_flash) - pos)
290 count = sizeof(struct qib_flash) - pos;
292 tmp = kmalloc(count, GFP_KERNEL);
293 if (!tmp) {
294 ret = -ENOMEM;
295 goto bail;
298 dd = private2dd(file);
299 if (qib_eeprom_read(dd, pos, tmp, count)) {
300 qib_dev_err(dd, "failed to read from flash\n");
301 ret = -ENXIO;
302 goto bail_tmp;
305 if (copy_to_user(buf, tmp, count)) {
306 ret = -EFAULT;
307 goto bail_tmp;
310 *ppos = pos + count;
311 ret = count;
313 bail_tmp:
314 kfree(tmp);
316 bail:
317 return ret;
320 static ssize_t flash_write(struct file *file, const char __user *buf,
321 size_t count, loff_t *ppos)
323 struct qib_devdata *dd;
324 ssize_t ret;
325 loff_t pos;
326 char *tmp;
328 pos = *ppos;
330 if (pos != 0) {
331 ret = -EINVAL;
332 goto bail;
335 if (count != sizeof(struct qib_flash)) {
336 ret = -EINVAL;
337 goto bail;
340 tmp = kmalloc(count, GFP_KERNEL);
341 if (!tmp) {
342 ret = -ENOMEM;
343 goto bail;
346 if (copy_from_user(tmp, buf, count)) {
347 ret = -EFAULT;
348 goto bail_tmp;
351 dd = private2dd(file);
352 if (qib_eeprom_write(dd, pos, tmp, count)) {
353 ret = -ENXIO;
354 qib_dev_err(dd, "failed to write to flash\n");
355 goto bail_tmp;
358 *ppos = pos + count;
359 ret = count;
361 bail_tmp:
362 kfree(tmp);
364 bail:
365 return ret;
368 static const struct file_operations flash_ops = {
369 .read = flash_read,
370 .write = flash_write,
371 .llseek = default_llseek,
374 static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
376 struct dentry *dir, *tmp;
377 char unit[10];
378 int ret, i;
380 /* create the per-unit directory */
381 snprintf(unit, sizeof unit, "%u", dd->unit);
382 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
383 &simple_dir_operations, dd);
384 if (ret) {
385 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
386 goto bail;
389 /* create the files in the new directory */
390 ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
391 &cntr_ops[0], dd);
392 if (ret) {
393 printk(KERN_ERR "create_file(%s/counters) failed: %d\n",
394 unit, ret);
395 goto bail;
397 ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
398 &cntr_ops[1], dd);
399 if (ret) {
400 printk(KERN_ERR "create_file(%s/counter_names) failed: %d\n",
401 unit, ret);
402 goto bail;
404 ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
405 &portcntr_ops[0], dd);
406 if (ret) {
407 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
408 unit, "portcounter_names", ret);
409 goto bail;
411 for (i = 1; i <= dd->num_pports; i++) {
412 char fname[24];
414 sprintf(fname, "port%dcounters", i);
415 /* create the files in the new directory */
416 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
417 &portcntr_ops[i], dd);
418 if (ret) {
419 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
420 unit, fname, ret);
421 goto bail;
423 if (!(dd->flags & QIB_HAS_QSFP))
424 continue;
425 sprintf(fname, "qsfp%d", i);
426 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
427 &qsfp_ops[i - 1], dd);
428 if (ret) {
429 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
430 unit, fname, ret);
431 goto bail;
435 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
436 &flash_ops, dd);
437 if (ret)
438 printk(KERN_ERR "create_file(%s/flash) failed: %d\n",
439 unit, ret);
440 bail:
441 return ret;
444 static int remove_file(struct dentry *parent, char *name)
446 struct dentry *tmp;
447 int ret;
449 tmp = lookup_one_len(name, parent, strlen(name));
451 if (IS_ERR(tmp)) {
452 ret = PTR_ERR(tmp);
453 goto bail;
456 spin_lock(&tmp->d_lock);
457 if (!(d_unhashed(tmp) && tmp->d_inode)) {
458 dget_dlock(tmp);
459 __d_drop(tmp);
460 spin_unlock(&tmp->d_lock);
461 simple_unlink(parent->d_inode, tmp);
462 } else {
463 spin_unlock(&tmp->d_lock);
466 ret = 0;
467 bail:
469 * We don't expect clients to care about the return value, but
470 * it's there if they need it.
472 return ret;
475 static int remove_device_files(struct super_block *sb,
476 struct qib_devdata *dd)
478 struct dentry *dir, *root;
479 char unit[10];
480 int ret, i;
482 root = dget(sb->s_root);
483 mutex_lock(&root->d_inode->i_mutex);
484 snprintf(unit, sizeof unit, "%u", dd->unit);
485 dir = lookup_one_len(unit, root, strlen(unit));
487 if (IS_ERR(dir)) {
488 ret = PTR_ERR(dir);
489 printk(KERN_ERR "Lookup of %s failed\n", unit);
490 goto bail;
493 remove_file(dir, "counters");
494 remove_file(dir, "counter_names");
495 remove_file(dir, "portcounter_names");
496 for (i = 0; i < dd->num_pports; i++) {
497 char fname[24];
499 sprintf(fname, "port%dcounters", i + 1);
500 remove_file(dir, fname);
501 if (dd->flags & QIB_HAS_QSFP) {
502 sprintf(fname, "qsfp%d", i + 1);
503 remove_file(dir, fname);
506 remove_file(dir, "flash");
507 d_delete(dir);
508 ret = simple_rmdir(root->d_inode, dir);
510 bail:
511 mutex_unlock(&root->d_inode->i_mutex);
512 dput(root);
513 return ret;
517 * This fills everything in when the fs is mounted, to handle umount/mount
518 * after device init. The direct add_cntr_files() call handles adding
519 * them from the init code, when the fs is already mounted.
521 static int qibfs_fill_super(struct super_block *sb, void *data, int silent)
523 struct qib_devdata *dd, *tmp;
524 unsigned long flags;
525 int ret;
527 static struct tree_descr files[] = {
528 [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
529 [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
530 {""},
533 ret = simple_fill_super(sb, QIBFS_MAGIC, files);
534 if (ret) {
535 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
536 goto bail;
539 spin_lock_irqsave(&qib_devs_lock, flags);
541 list_for_each_entry_safe(dd, tmp, &qib_dev_list, list) {
542 spin_unlock_irqrestore(&qib_devs_lock, flags);
543 ret = add_cntr_files(sb, dd);
544 if (ret)
545 goto bail;
546 spin_lock_irqsave(&qib_devs_lock, flags);
549 spin_unlock_irqrestore(&qib_devs_lock, flags);
551 bail:
552 return ret;
555 static struct dentry *qibfs_mount(struct file_system_type *fs_type, int flags,
556 const char *dev_name, void *data)
558 struct dentry *ret;
559 ret = mount_single(fs_type, flags, data, qibfs_fill_super);
560 if (!IS_ERR(ret))
561 qib_super = ret->d_sb;
562 return ret;
565 static void qibfs_kill_super(struct super_block *s)
567 kill_litter_super(s);
568 qib_super = NULL;
571 int qibfs_add(struct qib_devdata *dd)
573 int ret;
576 * On first unit initialized, qib_super will not yet exist
577 * because nobody has yet tried to mount the filesystem, so
578 * we can't consider that to be an error; if an error occurs
579 * during the mount, that will get a complaint, so this is OK.
580 * add_cntr_files() for all units is done at mount from
581 * qibfs_fill_super(), so one way or another, everything works.
583 if (qib_super == NULL)
584 ret = 0;
585 else
586 ret = add_cntr_files(qib_super, dd);
587 return ret;
590 int qibfs_remove(struct qib_devdata *dd)
592 int ret = 0;
594 if (qib_super)
595 ret = remove_device_files(qib_super, dd);
597 return ret;
600 static struct file_system_type qibfs_fs_type = {
601 .owner = THIS_MODULE,
602 .name = "ipathfs",
603 .mount = qibfs_mount,
604 .kill_sb = qibfs_kill_super,
607 int __init qib_init_qibfs(void)
609 return register_filesystem(&qibfs_fs_type);
612 int __exit qib_exit_qibfs(void)
614 return unregister_filesystem(&qibfs_fs_type);