ipath_fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. * Copyright (c) 2006, 2007 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. #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. /*
  224. * Note: the chips support a maximum MTU of 4096, but the driver
  225. * hasn't implemented this feature yet, so set the maximum
  226. * to 2048.
  227. */
  228. portinfo[10] = /* VLArbitrationLowCap is 0 (only one VL) */
  229. /* InitTypeReply is SMA decision */
  230. (4 << 16) /* MTUCap 2048 */
  231. | (7 << 13) /* VLStallCount */
  232. | (0x1f << 8) /* HOQLife */
  233. | (1 << 4)
  234. /* OperationalVLs 0 */
  235. /* PartitionEnforcementInbound */
  236. /* PartitionEnforcementOutbound not enforced */
  237. /* FilterRawinbound not enforced */
  238. ; /* FilterRawOutbound not enforced */
  239. /* M_KeyViolations are not counted by hardware, SMA can count */
  240. tmp = ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
  241. /* P_KeyViolations are counted by hardware. */
  242. portinfo[11] = ((tmp & 0xffff) << 0);
  243. portinfo[12] =
  244. /* Q_KeyViolations are not counted by hardware */
  245. (1 << 8)
  246. /* GUIDCap */
  247. /* SubnetTimeOut handled by SMA */
  248. /* RespTimeValue handled by SMA */
  249. ;
  250. /* LocalPhyErrors are programmed to max */
  251. portinfo[12] |= (0xf << 20)
  252. | (0xf << 16) /* OverRunErrors are programmed to max */
  253. ;
  254. return simple_read_from_buffer(buf, count, ppos, portinfo,
  255. sizeof portinfo);
  256. }
  257. static const struct file_operations atomic_port_info_ops = {
  258. .read = atomic_port_info_read,
  259. };
  260. static ssize_t flash_read(struct file *file, char __user *buf,
  261. size_t count, loff_t *ppos)
  262. {
  263. struct ipath_devdata *dd;
  264. ssize_t ret;
  265. loff_t pos;
  266. char *tmp;
  267. pos = *ppos;
  268. if ( pos < 0) {
  269. ret = -EINVAL;
  270. goto bail;
  271. }
  272. if (pos >= sizeof(struct ipath_flash)) {
  273. ret = 0;
  274. goto bail;
  275. }
  276. if (count > sizeof(struct ipath_flash) - pos)
  277. count = sizeof(struct ipath_flash) - pos;
  278. tmp = kmalloc(count, GFP_KERNEL);
  279. if (!tmp) {
  280. ret = -ENOMEM;
  281. goto bail;
  282. }
  283. dd = file->f_path.dentry->d_inode->i_private;
  284. if (ipath_eeprom_read(dd, pos, tmp, count)) {
  285. ipath_dev_err(dd, "failed to read from flash\n");
  286. ret = -ENXIO;
  287. goto bail_tmp;
  288. }
  289. if (copy_to_user(buf, tmp, count)) {
  290. ret = -EFAULT;
  291. goto bail_tmp;
  292. }
  293. *ppos = pos + count;
  294. ret = count;
  295. bail_tmp:
  296. kfree(tmp);
  297. bail:
  298. return ret;
  299. }
  300. static ssize_t flash_write(struct file *file, const char __user *buf,
  301. size_t count, loff_t *ppos)
  302. {
  303. struct ipath_devdata *dd;
  304. ssize_t ret;
  305. loff_t pos;
  306. char *tmp;
  307. pos = *ppos;
  308. if (pos != 0) {
  309. ret = -EINVAL;
  310. goto bail;
  311. }
  312. if (count != sizeof(struct ipath_flash)) {
  313. ret = -EINVAL;
  314. goto bail;
  315. }
  316. tmp = kmalloc(count, GFP_KERNEL);
  317. if (!tmp) {
  318. ret = -ENOMEM;
  319. goto bail;
  320. }
  321. if (copy_from_user(tmp, buf, count)) {
  322. ret = -EFAULT;
  323. goto bail_tmp;
  324. }
  325. dd = file->f_path.dentry->d_inode->i_private;
  326. if (ipath_eeprom_write(dd, pos, tmp, count)) {
  327. ret = -ENXIO;
  328. ipath_dev_err(dd, "failed to write to flash\n");
  329. goto bail_tmp;
  330. }
  331. *ppos = pos + count;
  332. ret = count;
  333. bail_tmp:
  334. kfree(tmp);
  335. bail:
  336. return ret;
  337. }
  338. static const struct file_operations flash_ops = {
  339. .read = flash_read,
  340. .write = flash_write,
  341. };
  342. static int create_device_files(struct super_block *sb,
  343. struct ipath_devdata *dd)
  344. {
  345. struct dentry *dir, *tmp;
  346. char unit[10];
  347. int ret;
  348. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  349. ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
  350. (struct file_operations *) &simple_dir_operations,
  351. dd);
  352. if (ret) {
  353. printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
  354. goto bail;
  355. }
  356. ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
  357. &atomic_counters_ops, dd);
  358. if (ret) {
  359. printk(KERN_ERR "create_file(%s/atomic_counters) "
  360. "failed: %d\n", unit, ret);
  361. goto bail;
  362. }
  363. ret = create_file("node_info", S_IFREG|S_IRUGO, dir, &tmp,
  364. &atomic_node_info_ops, dd);
  365. if (ret) {
  366. printk(KERN_ERR "create_file(%s/node_info) "
  367. "failed: %d\n", unit, ret);
  368. goto bail;
  369. }
  370. ret = create_file("port_info", S_IFREG|S_IRUGO, dir, &tmp,
  371. &atomic_port_info_ops, dd);
  372. if (ret) {
  373. printk(KERN_ERR "create_file(%s/port_info) "
  374. "failed: %d\n", unit, ret);
  375. goto bail;
  376. }
  377. ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
  378. &flash_ops, dd);
  379. if (ret) {
  380. printk(KERN_ERR "create_file(%s/flash) "
  381. "failed: %d\n", unit, ret);
  382. goto bail;
  383. }
  384. bail:
  385. return ret;
  386. }
  387. static int remove_file(struct dentry *parent, char *name)
  388. {
  389. struct dentry *tmp;
  390. int ret;
  391. tmp = lookup_one_len(name, parent, strlen(name));
  392. if (IS_ERR(tmp)) {
  393. ret = PTR_ERR(tmp);
  394. goto bail;
  395. }
  396. spin_lock(&dcache_lock);
  397. spin_lock(&tmp->d_lock);
  398. if (!(d_unhashed(tmp) && tmp->d_inode)) {
  399. dget_locked(tmp);
  400. __d_drop(tmp);
  401. spin_unlock(&tmp->d_lock);
  402. spin_unlock(&dcache_lock);
  403. simple_unlink(parent->d_inode, tmp);
  404. } else {
  405. spin_unlock(&tmp->d_lock);
  406. spin_unlock(&dcache_lock);
  407. }
  408. ret = 0;
  409. bail:
  410. /*
  411. * We don't expect clients to care about the return value, but
  412. * it's there if they need it.
  413. */
  414. return ret;
  415. }
  416. static int remove_device_files(struct super_block *sb,
  417. struct ipath_devdata *dd)
  418. {
  419. struct dentry *dir, *root;
  420. char unit[10];
  421. int ret;
  422. root = dget(sb->s_root);
  423. mutex_lock(&root->d_inode->i_mutex);
  424. snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
  425. dir = lookup_one_len(unit, root, strlen(unit));
  426. if (IS_ERR(dir)) {
  427. ret = PTR_ERR(dir);
  428. printk(KERN_ERR "Lookup of %s failed\n", unit);
  429. goto bail;
  430. }
  431. remove_file(dir, "flash");
  432. remove_file(dir, "port_info");
  433. remove_file(dir, "node_info");
  434. remove_file(dir, "atomic_counters");
  435. d_delete(dir);
  436. ret = simple_rmdir(root->d_inode, dir);
  437. bail:
  438. mutex_unlock(&root->d_inode->i_mutex);
  439. dput(root);
  440. return ret;
  441. }
  442. static int ipathfs_fill_super(struct super_block *sb, void *data,
  443. int silent)
  444. {
  445. struct ipath_devdata *dd, *tmp;
  446. unsigned long flags;
  447. int ret;
  448. static struct tree_descr files[] = {
  449. [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
  450. {""},
  451. };
  452. ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
  453. if (ret) {
  454. printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
  455. goto bail;
  456. }
  457. spin_lock_irqsave(&ipath_devs_lock, flags);
  458. list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
  459. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  460. ret = create_device_files(sb, dd);
  461. if (ret) {
  462. deactivate_super(sb);
  463. goto bail;
  464. }
  465. spin_lock_irqsave(&ipath_devs_lock, flags);
  466. }
  467. spin_unlock_irqrestore(&ipath_devs_lock, flags);
  468. bail:
  469. return ret;
  470. }
  471. static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
  472. const char *dev_name, void *data, struct vfsmount *mnt)
  473. {
  474. int ret = get_sb_single(fs_type, flags, data,
  475. ipathfs_fill_super, mnt);
  476. if (ret >= 0)
  477. ipath_super = mnt->mnt_sb;
  478. return ret;
  479. }
  480. static void ipathfs_kill_super(struct super_block *s)
  481. {
  482. kill_litter_super(s);
  483. ipath_super = NULL;
  484. }
  485. int ipathfs_add_device(struct ipath_devdata *dd)
  486. {
  487. int ret;
  488. if (ipath_super == NULL) {
  489. ret = 0;
  490. goto bail;
  491. }
  492. ret = create_device_files(ipath_super, dd);
  493. bail:
  494. return ret;
  495. }
  496. int ipathfs_remove_device(struct ipath_devdata *dd)
  497. {
  498. int ret;
  499. if (ipath_super == NULL) {
  500. ret = 0;
  501. goto bail;
  502. }
  503. ret = remove_device_files(ipath_super, dd);
  504. bail:
  505. return ret;
  506. }
  507. static struct file_system_type ipathfs_fs_type = {
  508. .owner = THIS_MODULE,
  509. .name = "ipathfs",
  510. .get_sb = ipathfs_get_sb,
  511. .kill_sb = ipathfs_kill_super,
  512. };
  513. int __init ipath_init_ipathfs(void)
  514. {
  515. return register_filesystem(&ipathfs_fs_type);
  516. }
  517. void __exit ipath_exit_ipathfs(void)
  518. {
  519. unregister_filesystem(&ipathfs_fs_type);
  520. }