]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/infiniband/hw/qib/qib_fs.c
IB/qib: Add new qib driver for QLogic PCIe InfiniBand adapters
[net-next-2.6.git] / drivers / infiniband / hw / qib / qib_fs.c
CommitLineData
f931551b
RC
1/*
2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2006 PathScale, Inc. All rights reserved.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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.
32 */
33
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>
40
41#include "qib.h"
42
43#define QIBFS_MAGIC 0x726a77
44
45static struct super_block *qib_super;
46
47#define private2dd(file) ((file)->f_dentry->d_inode->i_private)
48
49static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
50 int mode, const struct file_operations *fops,
51 void *data)
52{
53 int error;
54 struct inode *inode = new_inode(dir->i_sb);
55
56 if (!inode) {
57 error = -EPERM;
58 goto bail;
59 }
60
61 inode->i_mode = mode;
62 inode->i_uid = 0;
63 inode->i_gid = 0;
64 inode->i_blocks = 0;
65 inode->i_atime = CURRENT_TIME;
66 inode->i_mtime = inode->i_atime;
67 inode->i_ctime = inode->i_atime;
68 inode->i_private = data;
69 if ((mode & S_IFMT) == S_IFDIR) {
70 inode->i_op = &simple_dir_inode_operations;
71 inc_nlink(inode);
72 inc_nlink(dir);
73 }
74
75 inode->i_fop = fops;
76
77 d_instantiate(dentry, inode);
78 error = 0;
79
80bail:
81 return error;
82}
83
84static int create_file(const char *name, mode_t mode,
85 struct dentry *parent, struct dentry **dentry,
86 const struct file_operations *fops, void *data)
87{
88 int error;
89
90 *dentry = NULL;
91 mutex_lock(&parent->d_inode->i_mutex);
92 *dentry = lookup_one_len(name, parent, strlen(name));
93 if (!IS_ERR(*dentry))
94 error = qibfs_mknod(parent->d_inode, *dentry,
95 mode, fops, data);
96 else
97 error = PTR_ERR(*dentry);
98 mutex_unlock(&parent->d_inode->i_mutex);
99
100 return error;
101}
102
103static ssize_t driver_stats_read(struct file *file, char __user *buf,
104 size_t count, loff_t *ppos)
105{
106 return simple_read_from_buffer(buf, count, ppos, &qib_stats,
107 sizeof qib_stats);
108}
109
110/*
111 * driver stats field names, one line per stat, single string. Used by
112 * programs like ipathstats to print the stats in a way which works for
113 * different versions of drivers, without changing program source.
114 * if qlogic_ib_stats changes, this needs to change. Names need to be
115 * 12 chars or less (w/o newline), for proper display by ipathstats utility.
116 */
117static const char qib_statnames[] =
118 "KernIntr\n"
119 "ErrorIntr\n"
120 "Tx_Errs\n"
121 "Rcv_Errs\n"
122 "H/W_Errs\n"
123 "NoPIOBufs\n"
124 "CtxtsOpen\n"
125 "RcvLen_Errs\n"
126 "EgrBufFull\n"
127 "EgrHdrFull\n"
128 ;
129
130static ssize_t driver_names_read(struct file *file, char __user *buf,
131 size_t count, loff_t *ppos)
132{
133 return simple_read_from_buffer(buf, count, ppos, qib_statnames,
134 sizeof qib_statnames - 1); /* no null */
135}
136
137static const struct file_operations driver_ops[] = {
138 { .read = driver_stats_read, },
139 { .read = driver_names_read, },
140};
141
142/* read the per-device counters */
143static ssize_t dev_counters_read(struct file *file, char __user *buf,
144 size_t count, loff_t *ppos)
145{
146 u64 *counters;
147 struct qib_devdata *dd = private2dd(file);
148
149 return simple_read_from_buffer(buf, count, ppos, counters,
150 dd->f_read_cntrs(dd, *ppos, NULL, &counters));
151}
152
153/* read the per-device counters */
154static ssize_t dev_names_read(struct file *file, char __user *buf,
155 size_t count, loff_t *ppos)
156{
157 char *names;
158 struct qib_devdata *dd = private2dd(file);
159
160 return simple_read_from_buffer(buf, count, ppos, names,
161 dd->f_read_cntrs(dd, *ppos, &names, NULL));
162}
163
164static const struct file_operations cntr_ops[] = {
165 { .read = dev_counters_read, },
166 { .read = dev_names_read, },
167};
168
169/*
170 * Could use file->f_dentry->d_inode->i_ino to figure out which file,
171 * instead of separate routine for each, but for now, this works...
172 */
173
174/* read the per-port names (same for each port) */
175static ssize_t portnames_read(struct file *file, char __user *buf,
176 size_t count, loff_t *ppos)
177{
178 char *names;
179 struct qib_devdata *dd = private2dd(file);
180
181 return simple_read_from_buffer(buf, count, ppos, names,
182 dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL));
183}
184
185/* read the per-port counters for port 1 (pidx 0) */
186static ssize_t portcntrs_1_read(struct file *file, char __user *buf,
187 size_t count, loff_t *ppos)
188{
189 u64 *counters;
190 struct qib_devdata *dd = private2dd(file);
191
192 return simple_read_from_buffer(buf, count, ppos, counters,
193 dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters));
194}
195
196/* read the per-port counters for port 2 (pidx 1) */
197static ssize_t portcntrs_2_read(struct file *file, char __user *buf,
198 size_t count, loff_t *ppos)
199{
200 u64 *counters;
201 struct qib_devdata *dd = private2dd(file);
202
203 return simple_read_from_buffer(buf, count, ppos, counters,
204 dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters));
205}
206
207static const struct file_operations portcntr_ops[] = {
208 { .read = portnames_read, },
209 { .read = portcntrs_1_read, },
210 { .read = portcntrs_2_read, },
211};
212
213/*
214 * read the per-port QSFP data for port 1 (pidx 0)
215 */
216static ssize_t qsfp_1_read(struct file *file, char __user *buf,
217 size_t count, loff_t *ppos)
218{
219 struct qib_devdata *dd = private2dd(file);
220 char *tmp;
221 int ret;
222
223 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
224 if (!tmp)
225 return -ENOMEM;
226
227 ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
228 if (ret > 0)
229 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
230 kfree(tmp);
231 return ret;
232}
233
234/*
235 * read the per-port QSFP data for port 2 (pidx 1)
236 */
237static ssize_t qsfp_2_read(struct file *file, char __user *buf,
238 size_t count, loff_t *ppos)
239{
240 struct qib_devdata *dd = private2dd(file);
241 char *tmp;
242 int ret;
243
244 if (dd->num_pports < 2)
245 return -ENODEV;
246
247 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
248 if (!tmp)
249 return -ENOMEM;
250
251 ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
252 if (ret > 0)
253 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
254 kfree(tmp);
255 return ret;
256}
257
258static const struct file_operations qsfp_ops[] = {
259 { .read = qsfp_1_read, },
260 { .read = qsfp_2_read, },
261};
262
263static ssize_t flash_read(struct file *file, char __user *buf,
264 size_t count, loff_t *ppos)
265{
266 struct qib_devdata *dd;
267 ssize_t ret;
268 loff_t pos;
269 char *tmp;
270
271 pos = *ppos;
272
273 if (pos < 0) {
274 ret = -EINVAL;
275 goto bail;
276 }
277
278 if (pos >= sizeof(struct qib_flash)) {
279 ret = 0;
280 goto bail;
281 }
282
283 if (count > sizeof(struct qib_flash) - pos)
284 count = sizeof(struct qib_flash) - pos;
285
286 tmp = kmalloc(count, GFP_KERNEL);
287 if (!tmp) {
288 ret = -ENOMEM;
289 goto bail;
290 }
291
292 dd = private2dd(file);
293 if (qib_eeprom_read(dd, pos, tmp, count)) {
294 qib_dev_err(dd, "failed to read from flash\n");
295 ret = -ENXIO;
296 goto bail_tmp;
297 }
298
299 if (copy_to_user(buf, tmp, count)) {
300 ret = -EFAULT;
301 goto bail_tmp;
302 }
303
304 *ppos = pos + count;
305 ret = count;
306
307bail_tmp:
308 kfree(tmp);
309
310bail:
311 return ret;
312}
313
314static ssize_t flash_write(struct file *file, const char __user *buf,
315 size_t count, loff_t *ppos)
316{
317 struct qib_devdata *dd;
318 ssize_t ret;
319 loff_t pos;
320 char *tmp;
321
322 pos = *ppos;
323
324 if (pos != 0) {
325 ret = -EINVAL;
326 goto bail;
327 }
328
329 if (count != sizeof(struct qib_flash)) {
330 ret = -EINVAL;
331 goto bail;
332 }
333
334 tmp = kmalloc(count, GFP_KERNEL);
335 if (!tmp) {
336 ret = -ENOMEM;
337 goto bail;
338 }
339
340 if (copy_from_user(tmp, buf, count)) {
341 ret = -EFAULT;
342 goto bail_tmp;
343 }
344
345 dd = private2dd(file);
346 if (qib_eeprom_write(dd, pos, tmp, count)) {
347 ret = -ENXIO;
348 qib_dev_err(dd, "failed to write to flash\n");
349 goto bail_tmp;
350 }
351
352 *ppos = pos + count;
353 ret = count;
354
355bail_tmp:
356 kfree(tmp);
357
358bail:
359 return ret;
360}
361
362static const struct file_operations flash_ops = {
363 .read = flash_read,
364 .write = flash_write,
365};
366
367static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
368{
369 struct dentry *dir, *tmp;
370 char unit[10];
371 int ret, i;
372
373 /* create the per-unit directory */
374 snprintf(unit, sizeof unit, "%u", dd->unit);
375 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
376 &simple_dir_operations, dd);
377 if (ret) {
378 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
379 goto bail;
380 }
381
382 /* create the files in the new directory */
383 ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
384 &cntr_ops[0], dd);
385 if (ret) {
386 printk(KERN_ERR "create_file(%s/counters) failed: %d\n",
387 unit, ret);
388 goto bail;
389 }
390 ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
391 &cntr_ops[1], dd);
392 if (ret) {
393 printk(KERN_ERR "create_file(%s/counter_names) failed: %d\n",
394 unit, ret);
395 goto bail;
396 }
397 ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
398 &portcntr_ops[0], dd);
399 if (ret) {
400 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
401 unit, "portcounter_names", ret);
402 goto bail;
403 }
404 for (i = 1; i <= dd->num_pports; i++) {
405 char fname[24];
406
407 sprintf(fname, "port%dcounters", i);
408 /* create the files in the new directory */
409 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
410 &portcntr_ops[i], dd);
411 if (ret) {
412 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
413 unit, fname, ret);
414 goto bail;
415 }
416 if (!(dd->flags & QIB_HAS_QSFP))
417 continue;
418 sprintf(fname, "qsfp%d", i);
419 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
420 &qsfp_ops[i - 1], dd);
421 if (ret) {
422 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
423 unit, fname, ret);
424 goto bail;
425 }
426 }
427
428 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
429 &flash_ops, dd);
430 if (ret)
431 printk(KERN_ERR "create_file(%s/flash) failed: %d\n",
432 unit, ret);
433bail:
434 return ret;
435}
436
437static int remove_file(struct dentry *parent, char *name)
438{
439 struct dentry *tmp;
440 int ret;
441
442 tmp = lookup_one_len(name, parent, strlen(name));
443
444 if (IS_ERR(tmp)) {
445 ret = PTR_ERR(tmp);
446 goto bail;
447 }
448
449 spin_lock(&dcache_lock);
450 spin_lock(&tmp->d_lock);
451 if (!(d_unhashed(tmp) && tmp->d_inode)) {
452 dget_locked(tmp);
453 __d_drop(tmp);
454 spin_unlock(&tmp->d_lock);
455 spin_unlock(&dcache_lock);
456 simple_unlink(parent->d_inode, tmp);
457 } else {
458 spin_unlock(&tmp->d_lock);
459 spin_unlock(&dcache_lock);
460 }
461
462 ret = 0;
463bail:
464 /*
465 * We don't expect clients to care about the return value, but
466 * it's there if they need it.
467 */
468 return ret;
469}
470
471static int remove_device_files(struct super_block *sb,
472 struct qib_devdata *dd)
473{
474 struct dentry *dir, *root;
475 char unit[10];
476 int ret, i;
477
478 root = dget(sb->s_root);
479 mutex_lock(&root->d_inode->i_mutex);
480 snprintf(unit, sizeof unit, "%u", dd->unit);
481 dir = lookup_one_len(unit, root, strlen(unit));
482
483 if (IS_ERR(dir)) {
484 ret = PTR_ERR(dir);
485 printk(KERN_ERR "Lookup of %s failed\n", unit);
486 goto bail;
487 }
488
489 remove_file(dir, "counters");
490 remove_file(dir, "counter_names");
491 remove_file(dir, "portcounter_names");
492 for (i = 0; i < dd->num_pports; i++) {
493 char fname[24];
494
495 sprintf(fname, "port%dcounters", i + 1);
496 remove_file(dir, fname);
497 if (dd->flags & QIB_HAS_QSFP) {
498 sprintf(fname, "qsfp%d", i + 1);
499 remove_file(dir, fname);
500 }
501 }
502 remove_file(dir, "flash");
503 d_delete(dir);
504 ret = simple_rmdir(root->d_inode, dir);
505
506bail:
507 mutex_unlock(&root->d_inode->i_mutex);
508 dput(root);
509 return ret;
510}
511
512/*
513 * This fills everything in when the fs is mounted, to handle umount/mount
514 * after device init. The direct add_cntr_files() call handles adding
515 * them from the init code, when the fs is already mounted.
516 */
517static int qibfs_fill_super(struct super_block *sb, void *data, int silent)
518{
519 struct qib_devdata *dd, *tmp;
520 unsigned long flags;
521 int ret;
522
523 static struct tree_descr files[] = {
524 [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
525 [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
526 {""},
527 };
528
529 ret = simple_fill_super(sb, QIBFS_MAGIC, files);
530 if (ret) {
531 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
532 goto bail;
533 }
534
535 spin_lock_irqsave(&qib_devs_lock, flags);
536
537 list_for_each_entry_safe(dd, tmp, &qib_dev_list, list) {
538 spin_unlock_irqrestore(&qib_devs_lock, flags);
539 ret = add_cntr_files(sb, dd);
540 if (ret) {
541 deactivate_super(sb);
542 goto bail;
543 }
544 spin_lock_irqsave(&qib_devs_lock, flags);
545 }
546
547 spin_unlock_irqrestore(&qib_devs_lock, flags);
548
549bail:
550 return ret;
551}
552
553static int qibfs_get_sb(struct file_system_type *fs_type, int flags,
554 const char *dev_name, void *data, struct vfsmount *mnt)
555{
556 int ret = get_sb_single(fs_type, flags, data,
557 qibfs_fill_super, mnt);
558 if (ret >= 0)
559 qib_super = mnt->mnt_sb;
560 return ret;
561}
562
563static void qibfs_kill_super(struct super_block *s)
564{
565 kill_litter_super(s);
566 qib_super = NULL;
567}
568
569int qibfs_add(struct qib_devdata *dd)
570{
571 int ret;
572
573 /*
574 * On first unit initialized, qib_super will not yet exist
575 * because nobody has yet tried to mount the filesystem, so
576 * we can't consider that to be an error; if an error occurs
577 * during the mount, that will get a complaint, so this is OK.
578 * add_cntr_files() for all units is done at mount from
579 * qibfs_fill_super(), so one way or another, everything works.
580 */
581 if (qib_super == NULL)
582 ret = 0;
583 else
584 ret = add_cntr_files(qib_super, dd);
585 return ret;
586}
587
588int qibfs_remove(struct qib_devdata *dd)
589{
590 int ret = 0;
591
592 if (qib_super)
593 ret = remove_device_files(qib_super, dd);
594
595 return ret;
596}
597
598static struct file_system_type qibfs_fs_type = {
599 .owner = THIS_MODULE,
600 .name = "ipathfs",
601 .get_sb = qibfs_get_sb,
602 .kill_sb = qibfs_kill_super,
603};
604
605int __init qib_init_qibfs(void)
606{
607 return register_filesystem(&qibfs_fs_type);
608}
609
610int __exit qib_exit_qibfs(void)
611{
612 return unregister_filesystem(&qibfs_fs_type);
613}