qib_fs.c 14 KB

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