qib_fs.c 15 KB

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