Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2012 Intel Corporation. All rights reserved.
0003  * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
0004  * Copyright (c) 2006 PathScale, Inc. All rights reserved.
0005  *
0006  * This software is available to you under a choice of one of two
0007  * licenses.  You may choose to be licensed under the terms of the GNU
0008  * General Public License (GPL) Version 2, available from the file
0009  * COPYING in the main directory of this source tree, or the
0010  * OpenIB.org BSD license below:
0011  *
0012  *     Redistribution and use in source and binary forms, with or
0013  *     without modification, are permitted provided that the following
0014  *     conditions are met:
0015  *
0016  *      - Redistributions of source code must retain the above
0017  *        copyright notice, this list of conditions and the following
0018  *        disclaimer.
0019  *
0020  *      - Redistributions in binary form must reproduce the above
0021  *        copyright notice, this list of conditions and the following
0022  *        disclaimer in the documentation and/or other materials
0023  *        provided with the distribution.
0024  *
0025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0026  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0027  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0028  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
0029  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0030  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0031  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0032  * SOFTWARE.
0033  */
0034 
0035 #include <linux/fs.h>
0036 #include <linux/fs_context.h>
0037 #include <linux/mount.h>
0038 #include <linux/pagemap.h>
0039 #include <linux/init.h>
0040 #include <linux/namei.h>
0041 
0042 #include "qib.h"
0043 
0044 #define QIBFS_MAGIC 0x726a77
0045 
0046 static struct super_block *qib_super;
0047 
0048 #define private2dd(file) (file_inode(file)->i_private)
0049 
0050 static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
0051                umode_t mode, const struct file_operations *fops,
0052                void *data)
0053 {
0054     int error;
0055     struct inode *inode = new_inode(dir->i_sb);
0056 
0057     if (!inode) {
0058         error = -EPERM;
0059         goto bail;
0060     }
0061 
0062     inode->i_ino = get_next_ino();
0063     inode->i_mode = mode;
0064     inode->i_uid = GLOBAL_ROOT_UID;
0065     inode->i_gid = GLOBAL_ROOT_GID;
0066     inode->i_blocks = 0;
0067     inode->i_atime = current_time(inode);
0068     inode->i_mtime = inode->i_atime;
0069     inode->i_ctime = inode->i_atime;
0070     inode->i_private = data;
0071     if (S_ISDIR(mode)) {
0072         inode->i_op = &simple_dir_inode_operations;
0073         inc_nlink(inode);
0074         inc_nlink(dir);
0075     }
0076 
0077     inode->i_fop = fops;
0078 
0079     d_instantiate(dentry, inode);
0080     error = 0;
0081 
0082 bail:
0083     return error;
0084 }
0085 
0086 static int create_file(const char *name, umode_t mode,
0087                struct dentry *parent, struct dentry **dentry,
0088                const struct file_operations *fops, void *data)
0089 {
0090     int error;
0091 
0092     inode_lock(d_inode(parent));
0093     *dentry = lookup_one_len(name, parent, strlen(name));
0094     if (!IS_ERR(*dentry))
0095         error = qibfs_mknod(d_inode(parent), *dentry,
0096                     mode, fops, data);
0097     else
0098         error = PTR_ERR(*dentry);
0099     inode_unlock(d_inode(parent));
0100 
0101     return error;
0102 }
0103 
0104 static ssize_t driver_stats_read(struct file *file, char __user *buf,
0105                  size_t count, loff_t *ppos)
0106 {
0107     qib_stats.sps_ints = qib_sps_ints();
0108     return simple_read_from_buffer(buf, count, ppos, &qib_stats,
0109                        sizeof(qib_stats));
0110 }
0111 
0112 /*
0113  * driver stats field names, one line per stat, single string.  Used by
0114  * programs like ipathstats to print the stats in a way which works for
0115  * different versions of drivers, without changing program source.
0116  * if qlogic_ib_stats changes, this needs to change.  Names need to be
0117  * 12 chars or less (w/o newline), for proper display by ipathstats utility.
0118  */
0119 static const char qib_statnames[] =
0120     "KernIntr\n"
0121     "ErrorIntr\n"
0122     "Tx_Errs\n"
0123     "Rcv_Errs\n"
0124     "H/W_Errs\n"
0125     "NoPIOBufs\n"
0126     "CtxtsOpen\n"
0127     "RcvLen_Errs\n"
0128     "EgrBufFull\n"
0129     "EgrHdrFull\n"
0130     ;
0131 
0132 static ssize_t driver_names_read(struct file *file, char __user *buf,
0133                  size_t count, loff_t *ppos)
0134 {
0135     return simple_read_from_buffer(buf, count, ppos, qib_statnames,
0136         sizeof(qib_statnames) - 1); /* no null */
0137 }
0138 
0139 static const struct file_operations driver_ops[] = {
0140     { .read = driver_stats_read, .llseek = generic_file_llseek, },
0141     { .read = driver_names_read, .llseek = generic_file_llseek, },
0142 };
0143 
0144 /* read the per-device counters */
0145 static ssize_t dev_counters_read(struct file *file, char __user *buf,
0146                  size_t count, loff_t *ppos)
0147 {
0148     u64 *counters;
0149     size_t avail;
0150     struct qib_devdata *dd = private2dd(file);
0151 
0152     avail = dd->f_read_cntrs(dd, *ppos, NULL, &counters);
0153     return simple_read_from_buffer(buf, count, ppos, counters, avail);
0154 }
0155 
0156 /* read the per-device counters */
0157 static ssize_t dev_names_read(struct file *file, char __user *buf,
0158                   size_t count, loff_t *ppos)
0159 {
0160     char *names;
0161     size_t avail;
0162     struct qib_devdata *dd = private2dd(file);
0163 
0164     avail = dd->f_read_cntrs(dd, *ppos, &names, NULL);
0165     return simple_read_from_buffer(buf, count, ppos, names, avail);
0166 }
0167 
0168 static const struct file_operations cntr_ops[] = {
0169     { .read = dev_counters_read, .llseek = generic_file_llseek, },
0170     { .read = dev_names_read, .llseek = generic_file_llseek, },
0171 };
0172 
0173 /*
0174  * Could use file_inode(file)->i_ino to figure out which file,
0175  * instead of separate routine for each, but for now, this works...
0176  */
0177 
0178 /* read the per-port names (same for each port) */
0179 static ssize_t portnames_read(struct file *file, char __user *buf,
0180                   size_t count, loff_t *ppos)
0181 {
0182     char *names;
0183     size_t avail;
0184     struct qib_devdata *dd = private2dd(file);
0185 
0186     avail = dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL);
0187     return simple_read_from_buffer(buf, count, ppos, names, avail);
0188 }
0189 
0190 /* read the per-port counters for port 1 (pidx 0) */
0191 static ssize_t portcntrs_1_read(struct file *file, char __user *buf,
0192                 size_t count, loff_t *ppos)
0193 {
0194     u64 *counters;
0195     size_t avail;
0196     struct qib_devdata *dd = private2dd(file);
0197 
0198     avail = dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters);
0199     return simple_read_from_buffer(buf, count, ppos, counters, avail);
0200 }
0201 
0202 /* read the per-port counters for port 2 (pidx 1) */
0203 static ssize_t portcntrs_2_read(struct file *file, char __user *buf,
0204                 size_t count, loff_t *ppos)
0205 {
0206     u64 *counters;
0207     size_t avail;
0208     struct qib_devdata *dd = private2dd(file);
0209 
0210     avail = dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters);
0211     return simple_read_from_buffer(buf, count, ppos, counters, avail);
0212 }
0213 
0214 static const struct file_operations portcntr_ops[] = {
0215     { .read = portnames_read, .llseek = generic_file_llseek, },
0216     { .read = portcntrs_1_read, .llseek = generic_file_llseek, },
0217     { .read = portcntrs_2_read, .llseek = generic_file_llseek, },
0218 };
0219 
0220 /*
0221  * read the per-port QSFP data for port 1 (pidx 0)
0222  */
0223 static ssize_t qsfp_1_read(struct file *file, char __user *buf,
0224                size_t count, loff_t *ppos)
0225 {
0226     struct qib_devdata *dd = private2dd(file);
0227     char *tmp;
0228     int ret;
0229 
0230     tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
0231     if (!tmp)
0232         return -ENOMEM;
0233 
0234     ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
0235     if (ret > 0)
0236         ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
0237     kfree(tmp);
0238     return ret;
0239 }
0240 
0241 /*
0242  * read the per-port QSFP data for port 2 (pidx 1)
0243  */
0244 static ssize_t qsfp_2_read(struct file *file, char __user *buf,
0245                size_t count, loff_t *ppos)
0246 {
0247     struct qib_devdata *dd = private2dd(file);
0248     char *tmp;
0249     int ret;
0250 
0251     if (dd->num_pports < 2)
0252         return -ENODEV;
0253 
0254     tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
0255     if (!tmp)
0256         return -ENOMEM;
0257 
0258     ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
0259     if (ret > 0)
0260         ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
0261     kfree(tmp);
0262     return ret;
0263 }
0264 
0265 static const struct file_operations qsfp_ops[] = {
0266     { .read = qsfp_1_read, .llseek = generic_file_llseek, },
0267     { .read = qsfp_2_read, .llseek = generic_file_llseek, },
0268 };
0269 
0270 static ssize_t flash_read(struct file *file, char __user *buf,
0271               size_t count, loff_t *ppos)
0272 {
0273     struct qib_devdata *dd;
0274     ssize_t ret;
0275     loff_t pos;
0276     char *tmp;
0277 
0278     pos = *ppos;
0279 
0280     if (pos < 0) {
0281         ret = -EINVAL;
0282         goto bail;
0283     }
0284 
0285     if (pos >= sizeof(struct qib_flash)) {
0286         ret = 0;
0287         goto bail;
0288     }
0289 
0290     if (count > sizeof(struct qib_flash) - pos)
0291         count = sizeof(struct qib_flash) - pos;
0292 
0293     tmp = kmalloc(count, GFP_KERNEL);
0294     if (!tmp) {
0295         ret = -ENOMEM;
0296         goto bail;
0297     }
0298 
0299     dd = private2dd(file);
0300     if (qib_eeprom_read(dd, pos, tmp, count)) {
0301         qib_dev_err(dd, "failed to read from flash\n");
0302         ret = -ENXIO;
0303         goto bail_tmp;
0304     }
0305 
0306     if (copy_to_user(buf, tmp, count)) {
0307         ret = -EFAULT;
0308         goto bail_tmp;
0309     }
0310 
0311     *ppos = pos + count;
0312     ret = count;
0313 
0314 bail_tmp:
0315     kfree(tmp);
0316 
0317 bail:
0318     return ret;
0319 }
0320 
0321 static ssize_t flash_write(struct file *file, const char __user *buf,
0322                size_t count, loff_t *ppos)
0323 {
0324     struct qib_devdata *dd;
0325     ssize_t ret;
0326     loff_t pos;
0327     char *tmp;
0328 
0329     pos = *ppos;
0330 
0331     if (pos != 0 || count != sizeof(struct qib_flash))
0332         return -EINVAL;
0333 
0334     tmp = memdup_user(buf, count);
0335     if (IS_ERR(tmp))
0336         return PTR_ERR(tmp);
0337 
0338     dd = private2dd(file);
0339     if (qib_eeprom_write(dd, pos, tmp, count)) {
0340         ret = -ENXIO;
0341         qib_dev_err(dd, "failed to write to flash\n");
0342         goto bail_tmp;
0343     }
0344 
0345     *ppos = pos + count;
0346     ret = count;
0347 
0348 bail_tmp:
0349     kfree(tmp);
0350     return ret;
0351 }
0352 
0353 static const struct file_operations flash_ops = {
0354     .read = flash_read,
0355     .write = flash_write,
0356     .llseek = default_llseek,
0357 };
0358 
0359 static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
0360 {
0361     struct dentry *dir, *tmp;
0362     char unit[10];
0363     int ret, i;
0364 
0365     /* create the per-unit directory */
0366     snprintf(unit, sizeof(unit), "%u", dd->unit);
0367     ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
0368               &simple_dir_operations, dd);
0369     if (ret) {
0370         pr_err("create_file(%s) failed: %d\n", unit, ret);
0371         goto bail;
0372     }
0373 
0374     /* create the files in the new directory */
0375     ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
0376               &cntr_ops[0], dd);
0377     if (ret) {
0378         pr_err("create_file(%s/counters) failed: %d\n",
0379                unit, ret);
0380         goto bail;
0381     }
0382     ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
0383               &cntr_ops[1], dd);
0384     if (ret) {
0385         pr_err("create_file(%s/counter_names) failed: %d\n",
0386                unit, ret);
0387         goto bail;
0388     }
0389     ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
0390               &portcntr_ops[0], dd);
0391     if (ret) {
0392         pr_err("create_file(%s/%s) failed: %d\n",
0393                unit, "portcounter_names", ret);
0394         goto bail;
0395     }
0396     for (i = 1; i <= dd->num_pports; i++) {
0397         char fname[24];
0398 
0399         sprintf(fname, "port%dcounters", i);
0400         /* create the files in the new directory */
0401         ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
0402                   &portcntr_ops[i], dd);
0403         if (ret) {
0404             pr_err("create_file(%s/%s) failed: %d\n",
0405                 unit, fname, ret);
0406             goto bail;
0407         }
0408         if (!(dd->flags & QIB_HAS_QSFP))
0409             continue;
0410         sprintf(fname, "qsfp%d", i);
0411         ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
0412                   &qsfp_ops[i - 1], dd);
0413         if (ret) {
0414             pr_err("create_file(%s/%s) failed: %d\n",
0415                 unit, fname, ret);
0416             goto bail;
0417         }
0418     }
0419 
0420     ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
0421               &flash_ops, dd);
0422     if (ret)
0423         pr_err("create_file(%s/flash) failed: %d\n",
0424             unit, ret);
0425 bail:
0426     return ret;
0427 }
0428 
0429 static int remove_device_files(struct super_block *sb,
0430                    struct qib_devdata *dd)
0431 {
0432     struct dentry *dir;
0433     char unit[10];
0434 
0435     snprintf(unit, sizeof(unit), "%u", dd->unit);
0436     dir = lookup_one_len_unlocked(unit, sb->s_root, strlen(unit));
0437 
0438     if (IS_ERR(dir)) {
0439         pr_err("Lookup of %s failed\n", unit);
0440         return PTR_ERR(dir);
0441     }
0442     simple_recursive_removal(dir, NULL);
0443     return 0;
0444 }
0445 
0446 /*
0447  * This fills everything in when the fs is mounted, to handle umount/mount
0448  * after device init.  The direct add_cntr_files() call handles adding
0449  * them from the init code, when the fs is already mounted.
0450  */
0451 static int qibfs_fill_super(struct super_block *sb, struct fs_context *fc)
0452 {
0453     struct qib_devdata *dd;
0454     unsigned long index;
0455     int ret;
0456 
0457     static const struct tree_descr files[] = {
0458         [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
0459         [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
0460         {""},
0461     };
0462 
0463     ret = simple_fill_super(sb, QIBFS_MAGIC, files);
0464     if (ret) {
0465         pr_err("simple_fill_super failed: %d\n", ret);
0466         goto bail;
0467     }
0468 
0469     xa_for_each(&qib_dev_table, index, dd) {
0470         ret = add_cntr_files(sb, dd);
0471         if (ret)
0472             goto bail;
0473     }
0474 
0475 bail:
0476     return ret;
0477 }
0478 
0479 static int qibfs_get_tree(struct fs_context *fc)
0480 {
0481     int ret = get_tree_single(fc, qibfs_fill_super);
0482     if (ret == 0)
0483         qib_super = fc->root->d_sb;
0484     return ret;
0485 }
0486 
0487 static const struct fs_context_operations qibfs_context_ops = {
0488     .get_tree   = qibfs_get_tree,
0489 };
0490 
0491 static int qibfs_init_fs_context(struct fs_context *fc)
0492 {
0493     fc->ops = &qibfs_context_ops;
0494     return 0;
0495 }
0496 
0497 static void qibfs_kill_super(struct super_block *s)
0498 {
0499     kill_litter_super(s);
0500     qib_super = NULL;
0501 }
0502 
0503 int qibfs_add(struct qib_devdata *dd)
0504 {
0505     int ret;
0506 
0507     /*
0508      * On first unit initialized, qib_super will not yet exist
0509      * because nobody has yet tried to mount the filesystem, so
0510      * we can't consider that to be an error; if an error occurs
0511      * during the mount, that will get a complaint, so this is OK.
0512      * add_cntr_files() for all units is done at mount from
0513      * qibfs_fill_super(), so one way or another, everything works.
0514      */
0515     if (qib_super == NULL)
0516         ret = 0;
0517     else
0518         ret = add_cntr_files(qib_super, dd);
0519     return ret;
0520 }
0521 
0522 int qibfs_remove(struct qib_devdata *dd)
0523 {
0524     int ret = 0;
0525 
0526     if (qib_super)
0527         ret = remove_device_files(qib_super, dd);
0528 
0529     return ret;
0530 }
0531 
0532 static struct file_system_type qibfs_fs_type = {
0533     .owner =        THIS_MODULE,
0534     .name =         "ipathfs",
0535     .init_fs_context = qibfs_init_fs_context,
0536     .kill_sb =      qibfs_kill_super,
0537 };
0538 MODULE_ALIAS_FS("ipathfs");
0539 
0540 int __init qib_init_qibfs(void)
0541 {
0542     return register_filesystem(&qibfs_fs_type);
0543 }
0544 
0545 int __exit qib_exit_qibfs(void)
0546 {
0547     return unregister_filesystem(&qibfs_fs_type);
0548 }