ipath_fs.c 14 KB

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