2 * Copyright (c) 2006 QLogic, Inc. 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
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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
34 #include <linux/version.h>
35 #include <linux/module.h>
37 #include <linux/mount.h>
38 #include <linux/pagemap.h>
39 #include <linux/init.h>
40 #include <linux/namei.h>
41 #include <linux/pci.h>
43 #include "ipath_kernel.h"
45 #define IPATHFS_MAGIC 0x726a77
47 static struct super_block *ipath_super;
49 static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
50 int mode, struct file_operations *fops,
54 struct inode *inode = new_inode(dir->i_sb);
65 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
66 inode->i_private = data;
67 if ((mode & S_IFMT) == S_IFDIR) {
68 inode->i_op = &simple_dir_inode_operations;
75 d_instantiate(dentry, inode);
82 static int create_file(const char *name, mode_t mode,
83 struct dentry *parent, struct dentry **dentry,
84 struct file_operations *fops, void *data)
89 mutex_lock(&parent->d_inode->i_mutex);
90 *dentry = lookup_one_len(name, parent, strlen(name));
92 error = ipathfs_mknod(parent->d_inode, *dentry,
95 error = PTR_ERR(dentry);
96 mutex_unlock(&parent->d_inode->i_mutex);
101 static ssize_t atomic_stats_read(struct file *file, char __user *buf,
102 size_t count, loff_t *ppos)
104 return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
108 static struct file_operations atomic_stats_ops = {
109 .read = atomic_stats_read,
112 #define NUM_COUNTERS sizeof(struct infinipath_counters) / sizeof(u64)
114 static ssize_t atomic_counters_read(struct file *file, char __user *buf,
115 size_t count, loff_t *ppos)
117 u64 counters[NUM_COUNTERS];
119 struct ipath_devdata *dd;
121 dd = file->f_path.dentry->d_inode->i_private;
123 for (i = 0; i < NUM_COUNTERS; i++)
124 counters[i] = ipath_snap_cntr(dd, i);
126 return simple_read_from_buffer(buf, count, ppos, counters,
130 static struct file_operations atomic_counters_ops = {
131 .read = atomic_counters_read,
134 static ssize_t atomic_node_info_read(struct file *file, char __user *buf,
135 size_t count, loff_t *ppos)
138 struct ipath_devdata *dd;
141 dd = file->f_path.dentry->d_inode->i_private;
143 guid = be64_to_cpu(dd->ipath_guid);
145 nodeinfo[0] = /* BaseVersion is SMA */
146 /* ClassVersion is SMA */
147 (1 << 8) /* NodeType */
148 | (1 << 0); /* NumPorts */
149 nodeinfo[1] = (u32) (guid >> 32);
150 nodeinfo[2] = (u32) (guid & 0xffffffff);
151 /* PortGUID == SystemImageGUID for us */
152 nodeinfo[3] = nodeinfo[1];
153 /* PortGUID == SystemImageGUID for us */
154 nodeinfo[4] = nodeinfo[2];
155 /* PortGUID == NodeGUID for us */
156 nodeinfo[5] = nodeinfo[3];
157 /* PortGUID == NodeGUID for us */
158 nodeinfo[6] = nodeinfo[4];
159 nodeinfo[7] = (4 << 16) /* we support 4 pkeys */
160 | (dd->ipath_deviceid << 0);
161 /* our chip version as 16 bits major, 16 bits minor */
162 nodeinfo[8] = dd->ipath_minrev | (dd->ipath_majrev << 16);
163 nodeinfo[9] = (dd->ipath_unit << 24) | (dd->ipath_vendorid << 0);
165 return simple_read_from_buffer(buf, count, ppos, nodeinfo,
169 static struct file_operations atomic_node_info_ops = {
170 .read = atomic_node_info_read,
173 static ssize_t atomic_port_info_read(struct file *file, char __user *buf,
174 size_t count, loff_t *ppos)
178 struct ipath_devdata *dd;
180 dd = file->f_path.dentry->d_inode->i_private;
182 /* so we only initialize non-zero fields. */
183 memset(portinfo, 0, sizeof portinfo);
186 * Notimpl yet M_Key (64)
187 * Notimpl yet GID (64)
190 portinfo[4] = (dd->ipath_lid << 16);
194 * CapabilityMask is 0, we don't support any of these
195 * DiagCode is 0; we don't store any diag info for now Notimpl yet
196 * M_KeyLeasePeriod (we don't support M_Key)
199 /* LocalPortNum is whichever port number they ask for */
200 portinfo[7] = (dd->ipath_unit << 24)
201 /* LinkWidthEnabled */
203 /* LinkWidthSupported (really 2, but not IB valid) */
205 /* LinkWidthActive */
207 tmp = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
209 if (tmp == IPATH_IBSTATE_INIT)
211 else if (tmp == IPATH_IBSTATE_ARM)
213 else if (tmp == IPATH_IBSTATE_ACTIVE)
220 portinfo[8] = (1 << 28) /* LinkSpeedSupported */
221 | (tmp << 24) /* PortState */
222 | (tmp2 << 20) /* PortPhysicalState */
225 /* LinkDownDefaultState */
226 /* M_KeyProtectBits == 0 */
227 /* NotImpl yet LMC == 0 (we can support all values) */
228 | (1 << 4) /* LinkSpeedActive */
229 | (1 << 0); /* LinkSpeedEnabled */
230 switch (dd->ipath_ibmtu) {
246 default: /* oops, something is wrong */
247 ipath_dbg("Problem, ipath_ibmtu 0x%x not a valid IB MTU, "
248 "treat as 2048\n", dd->ipath_ibmtu);
252 portinfo[9] = (tmp << 28)
254 /* Notimpl MasterSMSL */
258 /* Notimpl InitType (actually, an SMA decision) */
259 /* VLHighLimit is 0 (only one VL) */
260 ; /* VLArbitrationHighCap is 0 (only one VL) */
261 portinfo[10] = /* VLArbitrationLowCap is 0 (only one VL) */
262 /* InitTypeReply is SMA decision */
263 (5 << 16) /* MTUCap 4096 */
264 | (7 << 13) /* VLStallCount */
265 | (0x1f << 8) /* HOQLife */
268 /* OperationalVLs 0 */
269 /* PartitionEnforcementInbound */
270 /* PartitionEnforcementOutbound not enforced */
271 /* FilterRawinbound not enforced */
272 ; /* FilterRawOutbound not enforced */
273 /* M_KeyViolations are not counted by hardware, SMA can count */
274 tmp = ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
275 /* P_KeyViolations are counted by hardware. */
276 portinfo[11] = ((tmp & 0xffff) << 0);
278 /* Q_KeyViolations are not counted by hardware */
282 /* SubnetTimeOut handled by SMA */
283 /* RespTimeValue handled by SMA */
285 /* LocalPhyErrors are programmed to max */
286 portinfo[12] |= (0xf << 20)
287 | (0xf << 16) /* OverRunErrors are programmed to max */
290 return simple_read_from_buffer(buf, count, ppos, portinfo,
294 static struct file_operations atomic_port_info_ops = {
295 .read = atomic_port_info_read,
298 static ssize_t flash_read(struct file *file, char __user *buf,
299 size_t count, loff_t *ppos)
301 struct ipath_devdata *dd;
313 if (pos >= sizeof(struct ipath_flash)) {
318 if (count > sizeof(struct ipath_flash) - pos)
319 count = sizeof(struct ipath_flash) - pos;
321 tmp = kmalloc(count, GFP_KERNEL);
327 dd = file->f_path.dentry->d_inode->i_private;
328 if (ipath_eeprom_read(dd, pos, tmp, count)) {
329 ipath_dev_err(dd, "failed to read from flash\n");
334 if (copy_to_user(buf, tmp, count)) {
349 static ssize_t flash_write(struct file *file, const char __user *buf,
350 size_t count, loff_t *ppos)
352 struct ipath_devdata *dd;
364 if (count != sizeof(struct ipath_flash)) {
369 tmp = kmalloc(count, GFP_KERNEL);
375 if (copy_from_user(tmp, buf, count)) {
380 dd = file->f_path.dentry->d_inode->i_private;
381 if (ipath_eeprom_write(dd, pos, tmp, count)) {
383 ipath_dev_err(dd, "failed to write to flash\n");
397 static struct file_operations flash_ops = {
399 .write = flash_write,
402 static int create_device_files(struct super_block *sb,
403 struct ipath_devdata *dd)
405 struct dentry *dir, *tmp;
409 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
410 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
411 (struct file_operations *) &simple_dir_operations,
414 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
418 ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
419 &atomic_counters_ops, dd);
421 printk(KERN_ERR "create_file(%s/atomic_counters) "
422 "failed: %d\n", unit, ret);
426 ret = create_file("node_info", S_IFREG|S_IRUGO, dir, &tmp,
427 &atomic_node_info_ops, dd);
429 printk(KERN_ERR "create_file(%s/node_info) "
430 "failed: %d\n", unit, ret);
434 ret = create_file("port_info", S_IFREG|S_IRUGO, dir, &tmp,
435 &atomic_port_info_ops, dd);
437 printk(KERN_ERR "create_file(%s/port_info) "
438 "failed: %d\n", unit, ret);
442 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
445 printk(KERN_ERR "create_file(%s/flash) "
446 "failed: %d\n", unit, ret);
454 static void remove_file(struct dentry *parent, char *name)
458 tmp = lookup_one_len(name, parent, strlen(name));
460 spin_lock(&dcache_lock);
461 spin_lock(&tmp->d_lock);
462 if (!(d_unhashed(tmp) && tmp->d_inode)) {
465 spin_unlock(&tmp->d_lock);
466 spin_unlock(&dcache_lock);
467 simple_unlink(parent->d_inode, tmp);
469 spin_unlock(&tmp->d_lock);
470 spin_unlock(&dcache_lock);
474 static int remove_device_files(struct super_block *sb,
475 struct ipath_devdata *dd)
477 struct dentry *dir, *root;
481 root = dget(sb->s_root);
482 mutex_lock(&root->d_inode->i_mutex);
483 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
484 dir = lookup_one_len(unit, root, strlen(unit));
488 printk(KERN_ERR "Lookup of %s failed\n", unit);
492 remove_file(dir, "flash");
493 remove_file(dir, "port_info");
494 remove_file(dir, "node_info");
495 remove_file(dir, "atomic_counters");
497 ret = simple_rmdir(root->d_inode, dir);
500 mutex_unlock(&root->d_inode->i_mutex);
505 static int ipathfs_fill_super(struct super_block *sb, void *data,
508 struct ipath_devdata *dd, *tmp;
512 static struct tree_descr files[] = {
513 [1] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
517 ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
519 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
523 spin_lock_irqsave(&ipath_devs_lock, flags);
525 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
526 spin_unlock_irqrestore(&ipath_devs_lock, flags);
527 ret = create_device_files(sb, dd);
529 deactivate_super(sb);
532 spin_lock_irqsave(&ipath_devs_lock, flags);
535 spin_unlock_irqrestore(&ipath_devs_lock, flags);
541 static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
542 const char *dev_name, void *data, struct vfsmount *mnt)
544 int ret = get_sb_single(fs_type, flags, data,
545 ipathfs_fill_super, mnt);
547 ipath_super = mnt->mnt_sb;
551 static void ipathfs_kill_super(struct super_block *s)
553 kill_litter_super(s);
557 int ipathfs_add_device(struct ipath_devdata *dd)
561 if (ipath_super == NULL) {
566 ret = create_device_files(ipath_super, dd);
572 int ipathfs_remove_device(struct ipath_devdata *dd)
576 if (ipath_super == NULL) {
581 ret = remove_device_files(ipath_super, dd);
587 static struct file_system_type ipathfs_fs_type = {
588 .owner = THIS_MODULE,
590 .get_sb = ipathfs_get_sb,
591 .kill_sb = ipathfs_kill_super,
594 int __init ipath_init_ipathfs(void)
596 return register_filesystem(&ipathfs_fs_type);
599 void __exit ipath_exit_ipathfs(void)
601 unregister_filesystem(&ipathfs_fs_type);