qib_fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009 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/module.h>
  34. #include <linux/fs.h>
  35. #include <linux/mount.h>
  36. #include <linux/pagemap.h>
  37. #include <linux/init.h>
  38. #include <linux/namei.h>
  39. #include "qib.h"
  40. #define QIBFS_MAGIC 0x726a77
  41. static struct super_block *qib_super;
  42. #define private2dd(file) ((file)->f_dentry->d_inode->i_private)
  43. static int qibfs_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 = CURRENT_TIME;
  58. inode->i_mtime = inode->i_atime;
  59. inode->i_ctime = inode->i_atime;
  60. inode->i_private = data;
  61. if ((mode & S_IFMT) == S_IFDIR) {
  62. inode->i_op = &simple_dir_inode_operations;
  63. inc_nlink(inode);
  64. inc_nlink(dir);
  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. const 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 = qibfs_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 driver_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, &qib_stats,
  92. sizeof qib_stats);
  93. }
  94. /*
  95. * driver stats field names, one line per stat, single string. Used by
  96. * programs like ipathstats to print the stats in a way which works for
  97. * different versions of drivers, without changing program source.
  98. * if qlogic_ib_stats changes, this needs to change. Names need to be
  99. * 12 chars or less (w/o newline), for proper display by ipathstats utility.
  100. */
  101. static const char qib_statnames[] =
  102. "KernIntr\n"
  103. "ErrorIntr\n"
  104. "Tx_Errs\n"
  105. "Rcv_Errs\n"
  106. "H/W_Errs\n"
  107. "NoPIOBufs\n"
  108. "CtxtsOpen\n"
  109. "RcvLen_Errs\n"
  110. "EgrBufFull\n"
  111. "EgrHdrFull\n"
  112. ;
  113. static ssize_t driver_names_read(struct file *file, char __user *buf,
  114. size_t count, loff_t *ppos)
  115. {
  116. return simple_read_from_buffer(buf, count, ppos, qib_statnames,
  117. sizeof qib_statnames - 1); /* no null */
  118. }
  119. static const struct file_operations driver_ops[] = {
  120. { .read = driver_stats_read, },
  121. { .read = driver_names_read, },
  122. };
  123. /* read the per-device counters */
  124. static ssize_t dev_counters_read(struct file *file, char __user *buf,
  125. size_t count, loff_t *ppos)
  126. {
  127. u64 *counters;
  128. struct qib_devdata *dd = private2dd(file);
  129. return simple_read_from_buffer(buf, count, ppos, counters,
  130. dd->f_read_cntrs(dd, *ppos, NULL, &counters));
  131. }
  132. /* read the per-device counters */
  133. static ssize_t dev_names_read(struct file *file, char __user *buf,
  134. size_t count, loff_t *ppos)
  135. {
  136. char *names;
  137. struct qib_devdata *dd = private2dd(file);
  138. return simple_read_from_buffer(buf, count, ppos, names,
  139. dd->f_read_cntrs(dd, *ppos, &names, NULL));
  140. }
  141. static const struct file_operations cntr_ops[] = {
  142. { .read = dev_counters_read, },
  143. { .read = dev_names_read, },
  144. };
  145. /*
  146. * Could use file->f_dentry->d_inode->i_ino to figure out which file,
  147. * instead of separate routine for each, but for now, this works...
  148. */
  149. /* read the per-port names (same for each port) */
  150. static ssize_t portnames_read(struct file *file, char __user *buf,
  151. size_t count, loff_t *ppos)
  152. {
  153. char *names;
  154. struct qib_devdata *dd = private2dd(file);
  155. return simple_read_from_buffer(buf, count, ppos, names,
  156. dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL));
  157. }
  158. /* read the per-port counters for port 1 (pidx 0) */
  159. static ssize_t portcntrs_1_read(struct file *file, char __user *buf,
  160. size_t count, loff_t *ppos)
  161. {
  162. u64 *counters;
  163. struct qib_devdata *dd = private2dd(file);
  164. return simple_read_from_buffer(buf, count, ppos, counters,
  165. dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters));
  166. }
  167. /* read the per-port counters for port 2 (pidx 1) */
  168. static ssize_t portcntrs_2_read(struct file *file, char __user *buf,
  169. size_t count, loff_t *ppos)
  170. {
  171. u64 *counters;
  172. struct qib_devdata *dd = private2dd(file);
  173. return simple_read_from_buffer(buf, count, ppos, counters,
  174. dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters));
  175. }
  176. static const struct file_operations portcntr_ops[] = {
  177. { .read = portnames_read, },
  178. { .read = portcntrs_1_read, },
  179. { .read = portcntrs_2_read, },
  180. };
  181. /*
  182. * read the per-port QSFP data for port 1 (pidx 0)
  183. */
  184. static ssize_t qsfp_1_read(struct file *file, char __user *buf,
  185. size_t count, loff_t *ppos)
  186. {
  187. struct qib_devdata *dd = private2dd(file);
  188. char *tmp;
  189. int ret;
  190. tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
  191. if (!tmp)
  192. return -ENOMEM;
  193. ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
  194. if (ret > 0)
  195. ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
  196. kfree(tmp);
  197. return ret;
  198. }
  199. /*
  200. * read the per-port QSFP data for port 2 (pidx 1)
  201. */
  202. static ssize_t qsfp_2_read(struct file *file, char __user *buf,
  203. size_t count, loff_t *ppos)
  204. {
  205. struct qib_devdata *dd = private2dd(file);
  206. char *tmp;
  207. int ret;
  208. if (dd->num_pports < 2)
  209. return -ENODEV;
  210. tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
  211. if (!tmp)
  212. return -ENOMEM;
  213. ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
  214. if (ret > 0)
  215. ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
  216. kfree(tmp);
  217. return ret;
  218. }
  219. static const struct file_operations qsfp_ops[] = {
  220. { .read = qsfp_1_read, },
  221. { .read = qsfp_2_read, },
  222. };
  223. static ssize_t flash_read(struct file *file, char __user *buf,
  224. size_t count, loff_t *ppos)
  225. {
  226. struct qib_devdata *dd;
  227. ssize_t ret;
  228. loff_t pos;
  229. char *tmp;
  230. pos = *ppos;
  231. if (pos < 0) {
  232. ret = -EINVAL;
  233. goto bail;
  234. }
  235. if (pos >= sizeof(struct qib_flash)) {
  236. ret = 0;
  237. goto bail;
  238. }
  239. if (count > sizeof(struct qib_flash) - pos)
  240. count = sizeof(struct qib_flash) - pos;
  241. tmp = kmalloc(count, GFP_KERNEL);
  242. if (!tmp) {
  243. ret = -ENOMEM;
  244. goto bail;
  245. }
  246. dd = private2dd(file);
  247. if (qib_eeprom_read(dd, pos, tmp, count)) {
  248. qib_dev_err(dd, "failed to read from flash\n");
  249. ret = -ENXIO;
  250. goto bail_tmp;
  251. }
  252. if (copy_to_user(buf, tmp, count)) {
  253. ret = -EFAULT;
  254. goto bail_tmp;
  255. }
  256. *ppos = pos + count;
  257. ret = count;
  258. bail_tmp:
  259. kfree(tmp);
  260. bail:
  261. return ret;
  262. }
  263. static ssize_t flash_write(struct file *file, const char __user *buf,
  264. size_t count, loff_t *ppos)
  265. {
  266. struct qib_devdata *dd;
  267. ssize_t ret;
  268. loff_t pos;
  269. char *tmp;
  270. pos = *ppos;
  271. if (pos != 0) {
  272. ret = -EINVAL;
  273. goto bail;
  274. }
  275. if (count != sizeof(struct qib_flash)) {
  276. ret = -EINVAL;
  277. goto bail;
  278. }
  279. tmp = kmalloc(count, GFP_KERNEL);
  280. if (!tmp) {
  281. ret = -ENOMEM;
  282. goto bail;
  283. }
  284. if (copy_from_user(tmp, buf, count)) {
  285. ret = -EFAULT;
  286. goto bail_tmp;
  287. }
  288. dd = private2dd(file);
  289. if (qib_eeprom_write(dd, pos, tmp, count)) {
  290. ret = -ENXIO;
  291. qib_dev_err(dd, "failed to write to flash\n");
  292. goto bail_tmp;
  293. }
  294. *ppos = pos + count;
  295. ret = count;
  296. bail_tmp:
  297. kfree(tmp);
  298. bail:
  299. return ret;
  300. }
  301. static const struct file_operations flash_ops = {
  302. .read = flash_read,
  303. .write = flash_write,
  304. };
  305. static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
  306. {
  307. struct dentry *dir, *tmp;
  308. char unit[10];
  309. int ret, i;
  310. /* create the per-unit directory */
  311. snprintf(unit, sizeof unit, "%u", dd->unit);
  312. ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
  313. &simple_dir_operations, dd);
  314. if (ret) {
  315. printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
  316. goto bail;
  317. }
  318. /* create the files in the new directory */
  319. ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
  320. &cntr_ops[0], dd);
  321. if (ret) {
  322. printk(KERN_ERR "create_file(%s/counters) failed: %d\n",
  323. unit, ret);
  324. goto bail;
  325. }
  326. ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
  327. &cntr_ops[1], dd);
  328. if (ret) {
  329. printk(KERN_ERR "create_file(%s/counter_names) failed: %d\n",
  330. unit, ret);
  331. goto bail;
  332. }
  333. ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
  334. &portcntr_ops[0], dd);
  335. if (ret) {
  336. printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
  337. unit, "portcounter_names", ret);
  338. goto bail;
  339. }
  340. for (i = 1; i <= dd->num_pports; i++) {
  341. char fname[24];
  342. sprintf(fname, "port%dcounters", i);
  343. /* create the files in the new directory */
  344. ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
  345. &portcntr_ops[i], dd);
  346. if (ret) {
  347. printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
  348. unit, fname, ret);
  349. goto bail;
  350. }
  351. if (!(dd->flags & QIB_HAS_QSFP))
  352. continue;
  353. sprintf(fname, "qsfp%d", i);
  354. ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
  355. &qsfp_ops[i - 1], dd);
  356. if (ret) {
  357. printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
  358. unit, fname, ret);
  359. goto bail;
  360. }
  361. }
  362. ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
  363. &flash_ops, dd);
  364. if (ret)
  365. printk(KERN_ERR "create_file(%s/flash) failed: %d\n",
  366. unit, ret);
  367. bail:
  368. return ret;
  369. }
  370. static int remove_file(struct dentry *parent, char *name)
  371. {
  372. struct dentry *tmp;
  373. int ret;
  374. tmp = lookup_one_len(name, parent, strlen(name));
  375. if (IS_ERR(tmp)) {
  376. ret = PTR_ERR(tmp);
  377. goto bail;
  378. }
  379. spin_lock(&dcache_lock);
  380. spin_lock(&tmp->d_lock);
  381. if (!(d_unhashed(tmp) && tmp->d_inode)) {
  382. dget_locked(tmp);
  383. __d_drop(tmp);
  384. spin_unlock(&tmp->d_lock);
  385. spin_unlock(&dcache_lock);
  386. simple_unlink(parent->d_inode, tmp);
  387. } else {
  388. spin_unlock(&tmp->d_lock);
  389. spin_unlock(&dcache_lock);
  390. }
  391. ret = 0;
  392. bail:
  393. /*
  394. * We don't expect clients to care about the return value, but
  395. * it's there if they need it.
  396. */
  397. return ret;
  398. }
  399. static int remove_device_files(struct super_block *sb,
  400. struct qib_devdata *dd)
  401. {
  402. struct dentry *dir, *root;
  403. char unit[10];
  404. int ret, i;
  405. root = dget(sb->s_root);
  406. mutex_lock(&root->d_inode->i_mutex);
  407. snprintf(unit, sizeof unit, "%u", dd->unit);
  408. dir = lookup_one_len(unit, root, strlen(unit));
  409. if (IS_ERR(dir)) {
  410. ret = PTR_ERR(dir);
  411. printk(KERN_ERR "Lookup of %s failed\n", unit);
  412. goto bail;
  413. }
  414. remove_file(dir, "counters");
  415. remove_file(dir, "counter_names");
  416. remove_file(dir, "portcounter_names");
  417. for (i = 0; i < dd->num_pports; i++) {
  418. char fname[24];
  419. sprintf(fname, "port%dcounters", i + 1);
  420. remove_file(dir, fname);
  421. if (dd->flags & QIB_HAS_QSFP) {
  422. sprintf(fname, "qsfp%d", i + 1);
  423. remove_file(dir, fname);
  424. }
  425. }
  426. remove_file(dir, "flash");
  427. d_delete(dir);
  428. ret = simple_rmdir(root->d_inode, dir);
  429. bail:
  430. mutex_unlock(&root->d_inode->i_mutex);
  431. dput(root);
  432. return ret;
  433. }
  434. /*
  435. * This fills everything in when the fs is mounted, to handle umount/mount
  436. * after device init. The direct add_cntr_files() call handles adding
  437. * them from the init code, when the fs is already mounted.
  438. */
  439. static int qibfs_fill_super(struct super_block *sb, void *data, int silent)
  440. {
  441. struct qib_devdata *dd, *tmp;
  442. unsigned long flags;
  443. int ret;
  444. static struct tree_descr files[] = {
  445. [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
  446. [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
  447. {""},
  448. };
  449. ret = simple_fill_super(sb, QIBFS_MAGIC, files);
  450. if (ret) {
  451. printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
  452. goto bail;
  453. }
  454. spin_lock_irqsave(&qib_devs_lock, flags);
  455. list_for_each_entry_safe(dd, tmp, &qib_dev_list, list) {
  456. spin_unlock_irqrestore(&qib_devs_lock, flags);
  457. ret = add_cntr_files(sb, dd);
  458. if (ret) {
  459. deactivate_super(sb);
  460. goto bail;
  461. }
  462. spin_lock_irqsave(&qib_devs_lock, flags);
  463. }
  464. spin_unlock_irqrestore(&qib_devs_lock, flags);
  465. bail:
  466. return ret;
  467. }
  468. static int qibfs_get_sb(struct file_system_type *fs_type, int flags,
  469. const char *dev_name, void *data, struct vfsmount *mnt)
  470. {
  471. int ret = get_sb_single(fs_type, flags, data,
  472. qibfs_fill_super, mnt);
  473. if (ret >= 0)
  474. qib_super = mnt->mnt_sb;
  475. return ret;
  476. }
  477. static void qibfs_kill_super(struct super_block *s)
  478. {
  479. kill_litter_super(s);
  480. qib_super = NULL;
  481. }
  482. int qibfs_add(struct qib_devdata *dd)
  483. {
  484. int ret;
  485. /*
  486. * On first unit initialized, qib_super will not yet exist
  487. * because nobody has yet tried to mount the filesystem, so
  488. * we can't consider that to be an error; if an error occurs
  489. * during the mount, that will get a complaint, so this is OK.
  490. * add_cntr_files() for all units is done at mount from
  491. * qibfs_fill_super(), so one way or another, everything works.
  492. */
  493. if (qib_super == NULL)
  494. ret = 0;
  495. else
  496. ret = add_cntr_files(qib_super, dd);
  497. return ret;
  498. }
  499. int qibfs_remove(struct qib_devdata *dd)
  500. {
  501. int ret = 0;
  502. if (qib_super)
  503. ret = remove_device_files(qib_super, dd);
  504. return ret;
  505. }
  506. static struct file_system_type qibfs_fs_type = {
  507. .owner = THIS_MODULE,
  508. .name = "ipathfs",
  509. .get_sb = qibfs_get_sb,
  510. .kill_sb = qibfs_kill_super,
  511. };
  512. int __init qib_init_qibfs(void)
  513. {
  514. return register_filesystem(&qibfs_fs_type);
  515. }
  516. int __exit qib_exit_qibfs(void)
  517. {
  518. return unregister_filesystem(&qibfs_fs_type);
  519. }