qib_fs.c 14 KB

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