ipath_fs.c 14 KB

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