ipath_fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. 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. #include <linux/version.h>
  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. #include <linux/pci.h>
  41. #include "ipath_kernel.h"
  42. #define IPATHFS_MAGIC 0x726a77
  43. static struct super_block *ipath_super;
  44. static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
  45. int mode, struct file_operations *fops,
  46. void *data)
  47. {
  48. int error;
  49. struct inode *inode = new_inode(dir->i_sb);
  50. if (!inode) {
  51. error = -EPERM;
  52. goto bail;
  53. }
  54. inode->i_mode = mode;
  55. inode->i_uid = 0;
  56. inode->i_gid = 0;
  57. inode->i_blocks = 0;
  58. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  59. inode->i_private = data;
  60. if ((mode & S_IFMT) == S_IFDIR) {
  61. inode->i_op = &simple_dir_inode_operations;
  62. inc_nlink(inode);
  63. inc_nlink(dir);
  64. }
  65. inode->i_fop = fops;
  66. d_instantiate(dentry, inode);
  67. error = 0;
  68. bail:
  69. return error;
  70. }
  71. static int create_file(const char *name, mode_t mode,
  72. struct dentry *parent, struct dentry **dentry,
  73. struct file_operations *fops, void *data)
  74. {
  75. int error;
  76. *dentry = NULL;
  77. mutex_lock(&parent->d_inode->i_mutex);
  78. *dentry = lookup_one_len(name, parent, strlen(name));
  79. if (!IS_ERR(dentry))
  80. error = ipathfs_mknod(parent->d_inode, *dentry,
  81. mode, fops, data);
  82. else
  83. error = PTR_ERR(dentry);
  84. mutex_unlock(&parent->d_inode->i_mutex);
  85. return error;
  86. }
  87. static ssize_t atomic_stats_read(struct file *file, char __user *buf,
  88. size_t count, loff_t *ppos)
  89. {
  90. return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
  91. sizeof ipath_stats);
  92. }
  93. static struct file_operations atomic_stats_ops = {
  94. .read = atomic_stats_read,
  95. };
  96. #define NUM_COUNTERS sizeof(struct infinipath_counters) / sizeof(u64)
  97. static ssize_t atomic_counters_read(struct file *file, char __user *buf,
  98. size_t count, loff_t *ppos)
  99. {
  100. u64 counters[NUM_COUNTERS];
  101. u16 i;
  102. struct ipath_devdata *dd;
  103. dd = file->f_dentry->d_inode->i_private;
  104. for (i = 0; i < NUM_COUNTERS; i++)
  105. counters[i] = ipath_snap_cntr(dd, i);
  106. return simple_read_from_buffer(buf, count, ppos, counters,
  107. sizeof counters);
  108. }
  109. static struct file_operations atomic_counters_ops = {
  110. .read = atomic_counters_read,
  111. };
  112. static ssize_t atomic_node_info_read(struct file *file, char __user *buf,
  113. size_t count, loff_t *ppos)
  114. {
  115. u32 nodeinfo[10];
  116. struct ipath_devdata *dd;
  117. u64 guid;
  118. dd = file->f_dentry->d_inode->i_private;
  119. guid = be64_to_cpu(dd->ipath_guid);
  120. nodeinfo[0] = /* BaseVersion is SMA */
  121. /* ClassVersion is SMA */
  122. (1 << 8) /* NodeType */
  123. | (1 << 0); /* NumPorts */
  124. nodeinfo[1] = (u32) (guid >> 32);
  125. nodeinfo[2] = (u32) (guid & 0xffffffff);
  126. /* PortGUID == SystemImageGUID for us */
  127. nodeinfo[3] = nodeinfo[1];
  128. /* PortGUID == SystemImageGUID for us */
  129. nodeinfo[4] = nodeinfo[2];
  130. /* PortGUID == NodeGUID for us */
  131. nodeinfo[5] = nodeinfo[3];
  132. /* PortGUID == NodeGUID for us */
  133. nodeinfo[6] = nodeinfo[4];
  134. nodeinfo[7] = (4 << 16) /* we support 4 pkeys */
  135. | (dd->ipath_deviceid << 0);
  136. /* our chip version as 16 bits major, 16 bits minor */
  137. nodeinfo[8] = dd->ipath_minrev | (dd->ipath_majrev << 16);
  138. nodeinfo[9] = (dd->ipath_unit << 24) | (dd->ipath_vendorid << 0);
  139. return simple_read_from_buffer(buf, count, ppos, nodeinfo,
  140. sizeof nodeinfo);
  141. }
  142. static struct file_operations atomic_node_info_ops = {
  143. .read = atomic_node_info_read,
  144. };
  145. static ssize_t atomic_port_info_read(struct file *file, char __user *buf,
  146. size_t count, loff_t *ppos)
  147. {
  148. u32 portinfo[13];
  149. u32 tmp, tmp2;
  150. struct ipath_devdata *dd;
  151. dd = file->f_dentry->d_inode->i_private;
  152. /* so we only initialize non-zero fields. */
  153. memset(portinfo, 0, sizeof portinfo);
  154. /*
  155. * Notimpl yet M_Key (64)
  156. * Notimpl yet GID (64)
  157. */
  158. portinfo[4] = (dd->ipath_lid << 16);
  159. /*
  160. * Notimpl yet SMLID.
  161. * CapabilityMask is 0, we don't support any of these
  162. * DiagCode is 0; we don't store any diag info for now Notimpl yet
  163. * M_KeyLeasePeriod (we don't support M_Key)
  164. */
  165. /* LocalPortNum is whichever port number they ask for */
  166. portinfo[7] = (dd->ipath_unit << 24)
  167. /* LinkWidthEnabled */
  168. | (2 << 16)
  169. /* LinkWidthSupported (really 2, but not IB valid) */
  170. | (3 << 8)
  171. /* LinkWidthActive */
  172. | (2 << 0);
  173. tmp = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
  174. tmp2 = 5;
  175. if (tmp == IPATH_IBSTATE_INIT)
  176. tmp = 2;
  177. else if (tmp == IPATH_IBSTATE_ARM)
  178. tmp = 3;
  179. else if (tmp == IPATH_IBSTATE_ACTIVE)
  180. tmp = 4;
  181. else {
  182. tmp = 0; /* down */
  183. tmp2 = tmp & 0xf;
  184. }
  185. portinfo[8] = (1 << 28) /* LinkSpeedSupported */
  186. | (tmp << 24) /* PortState */
  187. | (tmp2 << 20) /* PortPhysicalState */
  188. | (2 << 16)
  189. /* LinkDownDefaultState */
  190. /* M_KeyProtectBits == 0 */
  191. /* NotImpl yet LMC == 0 (we can support all values) */
  192. | (1 << 4) /* LinkSpeedActive */
  193. | (1 << 0); /* LinkSpeedEnabled */
  194. switch (dd->ipath_ibmtu) {
  195. case 4096:
  196. tmp = 5;
  197. break;
  198. case 2048:
  199. tmp = 4;
  200. break;
  201. case 1024:
  202. tmp = 3;
  203. break;
  204. case 512:
  205. tmp = 2;
  206. break;
  207. case 256:
  208. tmp = 1;
  209. break;
  210. default: /* oops, something is wrong */
  211. ipath_dbg("Problem, ipath_ibmtu 0x%x not a valid IB MTU, "
  212. "treat as 2048\n", dd->ipath_ibmtu);
  213. tmp = 4;
  214. break;
  215. }
  216. portinfo[9] = (tmp << 28)
  217. /* NeighborMTU */
  218. /* Notimpl MasterSMSL */
  219. | (1 << 20)
  220. /* VLCap */
  221. /* Notimpl InitType (actually, an SMA decision) */
  222. /* VLHighLimit is 0 (only one VL) */
  223. ; /* VLArbitrationHighCap is 0 (only one VL) */
  224. portinfo[10] = /* VLArbitrationLowCap is 0 (only one VL) */
  225. /* InitTypeReply is SMA decision */
  226. (5 << 16) /* MTUCap 4096 */
  227. | (7 << 13) /* VLStallCount */
  228. | (0x1f << 8) /* HOQLife */
  229. | (1 << 4)
  230. /* OperationalVLs 0 */
  231. /* PartitionEnforcementInbound */
  232. /* PartitionEnforcementOutbound not enforced */
  233. /* FilterRawinbound not enforced */
  234. ; /* FilterRawOutbound not enforced */
  235. /* M_KeyViolations are not counted by hardware, SMA can count */
  236. tmp = ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
  237. /* P_KeyViolations are counted by hardware. */
  238. portinfo[11] = ((tmp & 0xffff) << 0);
  239. portinfo[12] =
  240. /* Q_KeyViolations are not counted by hardware */
  241. (1 << 8)
  242. /* GUIDCap */
  243. /* SubnetTimeOut handled by SMA */
  244. /* RespTimeValue handled by SMA */
  245. ;
  246. /* LocalPhyErrors are programmed to max */
  247. portinfo[12] |= (0xf << 20)
  248. | (0xf << 16) /* OverRunErrors are programmed to max */
  249. ;
  250. return simple_read_from_buffer(buf, count, ppos, portinfo,
  251. sizeof portinfo);
  252. }
  253. static struct file_operations atomic_port_info_ops = {
  254. .read = atomic_port_info_read,
  255. };
  256. static ssize_t flash_read(struct file *file, char __user *buf,
  257. size_t count, loff_t *ppos)
  258. {
  259. struct ipath_devdata *dd;
  260. ssize_t ret;
  261. loff_t pos;
  262. char *tmp;
  263. pos = *ppos;
  264. if ( pos < 0) {
  265. ret = -EINVAL;
  266. goto bail;
  267. }
  268. if (pos >= sizeof(struct ipath_flash)) {
  269. ret = 0;
  270. goto bail;
  271. }
  272. if (count > sizeof(struct ipath_flash) - pos)
  273. count = sizeof(struct ipath_flash) - pos;
  274. tmp = kmalloc(count, GFP_KERNEL);
  275. if (!tmp) {
  276. ret = -ENOMEM;
  277. goto bail;
  278. }
  279. dd = file->f_dentry->d_inode->i_private;
  280. if (ipath_eeprom_read(dd, pos, tmp, count)) {
  281. ipath_dev_err(dd, "failed to read from flash\n");
  282. ret = -ENXIO;
  283. goto bail_tmp;
  284. }
  285. if (copy_to_user(buf, tmp, count)) {
  286. ret = -EFAULT;
  287. goto bail_tmp;
  288. }
  289. *ppos = pos + count;
  290. ret = count;
  291. bail_tmp:
  292. kfree(tmp);
  293. bail:
  294. return ret;
  295. }
  296. static ssize_t flash_write(struct file *file, const char __user *buf,
  297. size_t count, loff_t *ppos)
  298. {
  299. struct ipath_devdata *dd;
  300. ssize_t ret;
  301. loff_t pos;
  302. char *tmp;
  303. pos = *ppos;
  304. if (pos != 0) {
  305. ret = -EINVAL;
  306. goto bail;
  307. }
  308. if (count != sizeof(struct ipath_flash)) {
  309. ret = -EINVAL;
  310. goto bail;
  311. }
  312. tmp = kmalloc(count, GFP_KERNEL);
  313. if (!tmp) {
  314. ret = -ENOMEM;
  315. goto bail;
  316. }
  317. if (copy_from_user(tmp, buf, count)) {
  318. ret = -EFAULT;
  319. goto bail_tmp;
  320. }
  321. dd = file->f_dentry->d_inode->i_private;
  322. if (ipath_eeprom_write(dd, pos, tmp, count)) {
  323. ret = -ENXIO;
  324. ipath_dev_err(dd, "failed to write to flash\n");
  325. goto bail_tmp;
  326. }
  327. *ppos = pos + count;
  328. ret = count;
  329. bail_tmp:
  330. kfree(tmp);
  331. bail:
  332. return ret;
  333. }
  334. static struct file_operations flash_ops = {
  335. .read = flash_read,
  336. .write = flash_write,
  337. };
  338. static int create_device_files(struct super_block *sb,
  339. struct ipath_devdata *dd)
  340. {
  341. struct dentry *dir, *tmp;
  342. char unit[10];
  343. int ret;
  344. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  345. ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
  346. (struct file_operations *) &simple_dir_operations,
  347. dd);
  348. if (ret) {
  349. printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
  350. goto bail;
  351. }
  352. ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
  353. &atomic_counters_ops, dd);
  354. if (ret) {
  355. printk(KERN_ERR "create_file(%s/atomic_counters) "
  356. "failed: %d\n", unit, ret);
  357. goto bail;
  358. }
  359. ret = create_file("node_info", S_IFREG|S_IRUGO, dir, &tmp,
  360. &atomic_node_info_ops, dd);
  361. if (ret) {
  362. printk(KERN_ERR "create_file(%s/node_info) "
  363. "failed: %d\n", unit, ret);
  364. goto bail;
  365. }
  366. ret = create_file("port_info", S_IFREG|S_IRUGO, dir, &tmp,
  367. &atomic_port_info_ops, dd);
  368. if (ret) {
  369. printk(KERN_ERR "create_file(%s/port_info) "
  370. "failed: %d\n", unit, ret);
  371. goto bail;
  372. }
  373. ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
  374. &flash_ops, dd);
  375. if (ret) {
  376. printk(KERN_ERR "create_file(%s/flash) "
  377. "failed: %d\n", unit, ret);
  378. goto bail;
  379. }
  380. bail:
  381. return ret;
  382. }
  383. static void remove_file(struct dentry *parent, char *name)
  384. {
  385. struct dentry *tmp;
  386. tmp = lookup_one_len(name, parent, strlen(name));
  387. spin_lock(&dcache_lock);
  388. spin_lock(&tmp->d_lock);
  389. if (!(d_unhashed(tmp) && tmp->d_inode)) {
  390. dget_locked(tmp);
  391. __d_drop(tmp);
  392. spin_unlock(&tmp->d_lock);
  393. spin_unlock(&dcache_lock);
  394. simple_unlink(parent->d_inode, tmp);
  395. } else {
  396. spin_unlock(&tmp->d_lock);
  397. spin_unlock(&dcache_lock);
  398. }
  399. }
  400. static int remove_device_files(struct super_block *sb,
  401. struct ipath_devdata *dd)
  402. {
  403. struct dentry *dir, *root;
  404. char unit[10];
  405. int ret;
  406. root = dget(sb->s_root);
  407. mutex_lock(&root->d_inode->i_mutex);
  408. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  409. dir = lookup_one_len(unit, root, strlen(unit));
  410. if (IS_ERR(dir)) {
  411. ret = PTR_ERR(dir);
  412. printk(KERN_ERR "Lookup of %s failed\n", unit);
  413. goto bail;
  414. }
  415. remove_file(dir, "flash");
  416. remove_file(dir, "port_info");
  417. remove_file(dir, "node_info");
  418. remove_file(dir, "atomic_counters");
  419. d_delete(dir);
  420. ret = simple_rmdir(root->d_inode, dir);
  421. bail:
  422. mutex_unlock(&root->d_inode->i_mutex);
  423. dput(root);
  424. return ret;
  425. }
  426. static int ipathfs_fill_super(struct super_block *sb, void *data,
  427. int silent)
  428. {
  429. struct ipath_devdata *dd, *tmp;
  430. unsigned long flags;
  431. int ret;
  432. static struct tree_descr files[] = {
  433. [1] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
  434. {""},
  435. };
  436. ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
  437. if (ret) {
  438. printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
  439. goto bail;
  440. }
  441. spin_lock_irqsave(&ipath_devs_lock, flags);
  442. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  443. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  444. ret = create_device_files(sb, dd);
  445. if (ret) {
  446. deactivate_super(sb);
  447. goto bail;
  448. }
  449. spin_lock_irqsave(&ipath_devs_lock, flags);
  450. }
  451. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  452. bail:
  453. return ret;
  454. }
  455. static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
  456. const char *dev_name, void *data, struct vfsmount *mnt)
  457. {
  458. int ret = get_sb_single(fs_type, flags, data,
  459. ipathfs_fill_super, mnt);
  460. if (ret >= 0)
  461. ipath_super = mnt->mnt_sb;
  462. return ret;
  463. }
  464. static void ipathfs_kill_super(struct super_block *s)
  465. {
  466. kill_litter_super(s);
  467. ipath_super = NULL;
  468. }
  469. int ipathfs_add_device(struct ipath_devdata *dd)
  470. {
  471. int ret;
  472. if (ipath_super == NULL) {
  473. ret = 0;
  474. goto bail;
  475. }
  476. ret = create_device_files(ipath_super, dd);
  477. bail:
  478. return ret;
  479. }
  480. int ipathfs_remove_device(struct ipath_devdata *dd)
  481. {
  482. int ret;
  483. if (ipath_super == NULL) {
  484. ret = 0;
  485. goto bail;
  486. }
  487. ret = remove_device_files(ipath_super, dd);
  488. bail:
  489. return ret;
  490. }
  491. static struct file_system_type ipathfs_fs_type = {
  492. .owner = THIS_MODULE,
  493. .name = "ipathfs",
  494. .get_sb = ipathfs_get_sb,
  495. .kill_sb = ipathfs_kill_super,
  496. };
  497. int __init ipath_init_ipathfs(void)
  498. {
  499. return register_filesystem(&ipathfs_fs_type);
  500. }
  501. void __exit ipath_exit_ipathfs(void)
  502. {
  503. unregister_filesystem(&ipathfs_fs_type);
  504. }