qib_fs.c 15 KB

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