fnic_debugfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Copyright 2012 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/debugfs.h>
  20. #include "fnic.h"
  21. static struct dentry *fnic_trace_debugfs_root;
  22. static struct dentry *fnic_trace_debugfs_file;
  23. static struct dentry *fnic_trace_enable;
  24. static struct dentry *fnic_stats_debugfs_root;
  25. static struct dentry *fnic_fc_trace_debugfs_file;
  26. static struct dentry *fnic_fc_rdata_trace_debugfs_file;
  27. static struct dentry *fnic_fc_trace_enable;
  28. static struct dentry *fnic_fc_trace_clear;
  29. struct fc_trace_flag_type {
  30. u8 fc_row_file;
  31. u8 fc_normal_file;
  32. u8 fnic_trace;
  33. u8 fc_trace;
  34. u8 fc_clear;
  35. };
  36. static struct fc_trace_flag_type *fc_trc_flag;
  37. /*
  38. * fnic_debugfs_init - Initialize debugfs for fnic debug logging
  39. *
  40. * Description:
  41. * When Debugfs is configured this routine sets up the fnic debugfs
  42. * file system. If not already created, this routine will create the
  43. * fnic directory and statistics directory for trace buffer and
  44. * stats logging.
  45. */
  46. int fnic_debugfs_init(void)
  47. {
  48. int rc = -1;
  49. fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
  50. if (!fnic_trace_debugfs_root) {
  51. printk(KERN_DEBUG "Cannot create debugfs root\n");
  52. return rc;
  53. }
  54. if (!fnic_trace_debugfs_root) {
  55. printk(KERN_DEBUG
  56. "fnic root directory doesn't exist in debugfs\n");
  57. return rc;
  58. }
  59. fnic_stats_debugfs_root = debugfs_create_dir("statistics",
  60. fnic_trace_debugfs_root);
  61. if (!fnic_stats_debugfs_root) {
  62. printk(KERN_DEBUG "Cannot create Statistics directory\n");
  63. return rc;
  64. }
  65. /* Allocate memory to structure */
  66. fc_trc_flag = (struct fc_trace_flag_type *)
  67. vmalloc(sizeof(struct fc_trace_flag_type));
  68. if (fc_trc_flag) {
  69. fc_trc_flag->fc_row_file = 0;
  70. fc_trc_flag->fc_normal_file = 1;
  71. fc_trc_flag->fnic_trace = 2;
  72. fc_trc_flag->fc_trace = 3;
  73. fc_trc_flag->fc_clear = 4;
  74. }
  75. rc = 0;
  76. return rc;
  77. }
  78. /*
  79. * fnic_debugfs_terminate - Tear down debugfs infrastructure
  80. *
  81. * Description:
  82. * When Debugfs is configured this routine removes debugfs file system
  83. * elements that are specific to fnic.
  84. */
  85. void fnic_debugfs_terminate(void)
  86. {
  87. debugfs_remove(fnic_stats_debugfs_root);
  88. fnic_stats_debugfs_root = NULL;
  89. debugfs_remove(fnic_trace_debugfs_root);
  90. fnic_trace_debugfs_root = NULL;
  91. if (fc_trc_flag)
  92. vfree(fc_trc_flag);
  93. }
  94. /*
  95. * fnic_trace_ctrl_open - Open the trace_enable file for fnic_trace
  96. * Or Open fc_trace_enable file for fc_trace
  97. * @inode: The inode pointer.
  98. * @file: The file pointer to attach the trace enable/disable flag.
  99. *
  100. * Description:
  101. * This routine opens a debugsfs file trace_enable or fc_trace_enable.
  102. *
  103. * Returns:
  104. * This function returns zero if successful.
  105. */
  106. static int fnic_trace_ctrl_open(struct inode *inode, struct file *filp)
  107. {
  108. filp->private_data = inode->i_private;
  109. return 0;
  110. }
  111. /*
  112. * fnic_trace_ctrl_read -
  113. * Read trace_enable ,fc_trace_enable
  114. * or fc_trace_clear debugfs file
  115. * @filp: The file pointer to read from.
  116. * @ubuf: The buffer to copy the data to.
  117. * @cnt: The number of bytes to read.
  118. * @ppos: The position in the file to start reading from.
  119. *
  120. * Description:
  121. * This routine reads value of variable fnic_tracing_enabled or
  122. * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
  123. * and stores into local @buf.
  124. * It will start reading file at @ppos and
  125. * copy up to @cnt of data to @ubuf from @buf.
  126. *
  127. * Returns:
  128. * This function returns the amount of data that was read.
  129. */
  130. static ssize_t fnic_trace_ctrl_read(struct file *filp,
  131. char __user *ubuf,
  132. size_t cnt, loff_t *ppos)
  133. {
  134. char buf[64];
  135. int len;
  136. u8 *trace_type;
  137. len = 0;
  138. trace_type = (u8 *)filp->private_data;
  139. if (*trace_type == fc_trc_flag->fnic_trace)
  140. len = sprintf(buf, "%u\n", fnic_tracing_enabled);
  141. else if (*trace_type == fc_trc_flag->fc_trace)
  142. len = sprintf(buf, "%u\n", fnic_fc_tracing_enabled);
  143. else if (*trace_type == fc_trc_flag->fc_clear)
  144. len = sprintf(buf, "%u\n", fnic_fc_trace_cleared);
  145. else
  146. pr_err("fnic: Cannot read to any debugfs file\n");
  147. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  148. }
  149. /*
  150. * fnic_trace_ctrl_write -
  151. * Write to trace_enable, fc_trace_enable or
  152. * fc_trace_clear debugfs file
  153. * @filp: The file pointer to write from.
  154. * @ubuf: The buffer to copy the data from.
  155. * @cnt: The number of bytes to write.
  156. * @ppos: The position in the file to start writing to.
  157. *
  158. * Description:
  159. * This routine writes data from user buffer @ubuf to buffer @buf and
  160. * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
  161. * value as per user input.
  162. *
  163. * Returns:
  164. * This function returns the amount of data that was written.
  165. */
  166. static ssize_t fnic_trace_ctrl_write(struct file *filp,
  167. const char __user *ubuf,
  168. size_t cnt, loff_t *ppos)
  169. {
  170. char buf[64];
  171. unsigned long val;
  172. int ret;
  173. u8 *trace_type;
  174. trace_type = (u8 *)filp->private_data;
  175. if (cnt >= sizeof(buf))
  176. return -EINVAL;
  177. if (copy_from_user(&buf, ubuf, cnt))
  178. return -EFAULT;
  179. buf[cnt] = 0;
  180. ret = kstrtoul(buf, 10, &val);
  181. if (ret < 0)
  182. return ret;
  183. if (*trace_type == fc_trc_flag->fnic_trace)
  184. fnic_tracing_enabled = val;
  185. else if (*trace_type == fc_trc_flag->fc_trace)
  186. fnic_fc_tracing_enabled = val;
  187. else if (*trace_type == fc_trc_flag->fc_clear)
  188. fnic_fc_trace_cleared = val;
  189. else
  190. pr_err("fnic: cannot write to any debugfs file\n");
  191. (*ppos)++;
  192. return cnt;
  193. }
  194. static const struct file_operations fnic_trace_ctrl_fops = {
  195. .owner = THIS_MODULE,
  196. .open = fnic_trace_ctrl_open,
  197. .read = fnic_trace_ctrl_read,
  198. .write = fnic_trace_ctrl_write,
  199. };
  200. /*
  201. * fnic_trace_debugfs_open - Open the fnic trace log
  202. * @inode: The inode pointer
  203. * @file: The file pointer to attach the log output
  204. *
  205. * Description:
  206. * This routine is the entry point for the debugfs open file operation.
  207. * It allocates the necessary buffer for the log, fills the buffer from
  208. * the in-memory log and then returns a pointer to that log in
  209. * the private_data field in @file.
  210. *
  211. * Returns:
  212. * This function returns zero if successful. On error it will return
  213. * a negative error value.
  214. */
  215. static int fnic_trace_debugfs_open(struct inode *inode,
  216. struct file *file)
  217. {
  218. fnic_dbgfs_t *fnic_dbg_prt;
  219. u8 *rdata_ptr;
  220. rdata_ptr = (u8 *)inode->i_private;
  221. fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
  222. if (!fnic_dbg_prt)
  223. return -ENOMEM;
  224. if (*rdata_ptr == fc_trc_flag->fnic_trace) {
  225. fnic_dbg_prt->buffer = vmalloc(3 *
  226. (trace_max_pages * PAGE_SIZE));
  227. if (!fnic_dbg_prt->buffer) {
  228. kfree(fnic_dbg_prt);
  229. return -ENOMEM;
  230. }
  231. memset((void *)fnic_dbg_prt->buffer, 0,
  232. 3 * (trace_max_pages * PAGE_SIZE));
  233. fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
  234. } else {
  235. fnic_dbg_prt->buffer =
  236. vmalloc(3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
  237. if (!fnic_dbg_prt->buffer) {
  238. kfree(fnic_dbg_prt);
  239. return -ENOMEM;
  240. }
  241. memset((void *)fnic_dbg_prt->buffer, 0,
  242. 3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
  243. fnic_dbg_prt->buffer_len =
  244. fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
  245. }
  246. file->private_data = fnic_dbg_prt;
  247. return 0;
  248. }
  249. /*
  250. * fnic_trace_debugfs_lseek - Seek through a debugfs file
  251. * @file: The file pointer to seek through.
  252. * @offset: The offset to seek to or the amount to seek by.
  253. * @howto: Indicates how to seek.
  254. *
  255. * Description:
  256. * This routine is the entry point for the debugfs lseek file operation.
  257. * The @howto parameter indicates whether @offset is the offset to directly
  258. * seek to, or if it is a value to seek forward or reverse by. This function
  259. * figures out what the new offset of the debugfs file will be and assigns
  260. * that value to the f_pos field of @file.
  261. *
  262. * Returns:
  263. * This function returns the new offset if successful and returns a negative
  264. * error if unable to process the seek.
  265. */
  266. static loff_t fnic_trace_debugfs_lseek(struct file *file,
  267. loff_t offset,
  268. int howto)
  269. {
  270. fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
  271. return fixed_size_llseek(file, offset, howto,
  272. fnic_dbg_prt->buffer_len);
  273. }
  274. /*
  275. * fnic_trace_debugfs_read - Read a debugfs file
  276. * @file: The file pointer to read from.
  277. * @ubuf: The buffer to copy the data to.
  278. * @nbytes: The number of bytes to read.
  279. * @pos: The position in the file to start reading from.
  280. *
  281. * Description:
  282. * This routine reads data from the buffer indicated in the private_data
  283. * field of @file. It will start reading at @pos and copy up to @nbytes of
  284. * data to @ubuf.
  285. *
  286. * Returns:
  287. * This function returns the amount of data that was read (this could be
  288. * less than @nbytes if the end of the file was reached).
  289. */
  290. static ssize_t fnic_trace_debugfs_read(struct file *file,
  291. char __user *ubuf,
  292. size_t nbytes,
  293. loff_t *pos)
  294. {
  295. fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
  296. int rc = 0;
  297. rc = simple_read_from_buffer(ubuf, nbytes, pos,
  298. fnic_dbg_prt->buffer,
  299. fnic_dbg_prt->buffer_len);
  300. return rc;
  301. }
  302. /*
  303. * fnic_trace_debugfs_release - Release the buffer used to store
  304. * debugfs file data
  305. * @inode: The inode pointer
  306. * @file: The file pointer that contains the buffer to release
  307. *
  308. * Description:
  309. * This routine frees the buffer that was allocated when the debugfs
  310. * file was opened.
  311. *
  312. * Returns:
  313. * This function returns zero.
  314. */
  315. static int fnic_trace_debugfs_release(struct inode *inode,
  316. struct file *file)
  317. {
  318. fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
  319. vfree(fnic_dbg_prt->buffer);
  320. kfree(fnic_dbg_prt);
  321. return 0;
  322. }
  323. static const struct file_operations fnic_trace_debugfs_fops = {
  324. .owner = THIS_MODULE,
  325. .open = fnic_trace_debugfs_open,
  326. .llseek = fnic_trace_debugfs_lseek,
  327. .read = fnic_trace_debugfs_read,
  328. .release = fnic_trace_debugfs_release,
  329. };
  330. /*
  331. * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
  332. *
  333. * Description:
  334. * When Debugfs is configured this routine sets up the fnic debugfs
  335. * file system. If not already created, this routine will create the
  336. * create file trace to log fnic trace buffer output into debugfs and
  337. * it will also create file trace_enable to control enable/disable of
  338. * trace logging into trace buffer.
  339. */
  340. int fnic_trace_debugfs_init(void)
  341. {
  342. int rc = -1;
  343. if (!fnic_trace_debugfs_root) {
  344. printk(KERN_DEBUG
  345. "FNIC Debugfs root directory doesn't exist\n");
  346. return rc;
  347. }
  348. fnic_trace_enable = debugfs_create_file("tracing_enable",
  349. S_IFREG|S_IRUGO|S_IWUSR,
  350. fnic_trace_debugfs_root,
  351. &(fc_trc_flag->fnic_trace),
  352. &fnic_trace_ctrl_fops);
  353. if (!fnic_trace_enable) {
  354. printk(KERN_DEBUG
  355. "Cannot create trace_enable file under debugfs\n");
  356. return rc;
  357. }
  358. fnic_trace_debugfs_file = debugfs_create_file("trace",
  359. S_IFREG|S_IRUGO|S_IWUSR,
  360. fnic_trace_debugfs_root,
  361. &(fc_trc_flag->fnic_trace),
  362. &fnic_trace_debugfs_fops);
  363. if (!fnic_trace_debugfs_file) {
  364. printk(KERN_DEBUG
  365. "Cannot create trace file under debugfs\n");
  366. return rc;
  367. }
  368. rc = 0;
  369. return rc;
  370. }
  371. /*
  372. * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
  373. *
  374. * Description:
  375. * When Debugfs is configured this routine removes debugfs file system
  376. * elements that are specific to fnic trace logging.
  377. */
  378. void fnic_trace_debugfs_terminate(void)
  379. {
  380. debugfs_remove(fnic_trace_debugfs_file);
  381. fnic_trace_debugfs_file = NULL;
  382. debugfs_remove(fnic_trace_enable);
  383. fnic_trace_enable = NULL;
  384. }
  385. /*
  386. * fnic_fc_trace_debugfs_init -
  387. * Initialize debugfs for fnic control frame trace logging
  388. *
  389. * Description:
  390. * When Debugfs is configured this routine sets up the fnic_fc debugfs
  391. * file system. If not already created, this routine will create the
  392. * create file trace to log fnic fc trace buffer output into debugfs and
  393. * it will also create file fc_trace_enable to control enable/disable of
  394. * trace logging into trace buffer.
  395. */
  396. int fnic_fc_trace_debugfs_init(void)
  397. {
  398. int rc = -1;
  399. if (!fnic_trace_debugfs_root) {
  400. pr_err("fnic:Debugfs root directory doesn't exist\n");
  401. return rc;
  402. }
  403. fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
  404. S_IFREG|S_IRUGO|S_IWUSR,
  405. fnic_trace_debugfs_root,
  406. &(fc_trc_flag->fc_trace),
  407. &fnic_trace_ctrl_fops);
  408. if (!fnic_fc_trace_enable) {
  409. pr_err("fnic: Failed create fc_trace_enable file\n");
  410. return rc;
  411. }
  412. fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
  413. S_IFREG|S_IRUGO|S_IWUSR,
  414. fnic_trace_debugfs_root,
  415. &(fc_trc_flag->fc_clear),
  416. &fnic_trace_ctrl_fops);
  417. if (!fnic_fc_trace_clear) {
  418. pr_err("fnic: Failed to create fc_trace_enable file\n");
  419. return rc;
  420. }
  421. fnic_fc_rdata_trace_debugfs_file =
  422. debugfs_create_file("fc_trace_rdata",
  423. S_IFREG|S_IRUGO|S_IWUSR,
  424. fnic_trace_debugfs_root,
  425. &(fc_trc_flag->fc_normal_file),
  426. &fnic_trace_debugfs_fops);
  427. if (!fnic_fc_rdata_trace_debugfs_file) {
  428. pr_err("fnic: Failed create fc_rdata_trace file\n");
  429. return rc;
  430. }
  431. fnic_fc_trace_debugfs_file =
  432. debugfs_create_file("fc_trace",
  433. S_IFREG|S_IRUGO|S_IWUSR,
  434. fnic_trace_debugfs_root,
  435. &(fc_trc_flag->fc_row_file),
  436. &fnic_trace_debugfs_fops);
  437. if (!fnic_fc_trace_debugfs_file) {
  438. pr_err("fnic: Failed to create fc_trace file\n");
  439. return rc;
  440. }
  441. rc = 0;
  442. return rc;
  443. }
  444. /*
  445. * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
  446. *
  447. * Description:
  448. * When Debugfs is configured this routine removes debugfs file system
  449. * elements that are specific to fnic_fc trace logging.
  450. */
  451. void fnic_fc_trace_debugfs_terminate(void)
  452. {
  453. debugfs_remove(fnic_fc_trace_debugfs_file);
  454. fnic_fc_trace_debugfs_file = NULL;
  455. debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
  456. fnic_fc_rdata_trace_debugfs_file = NULL;
  457. debugfs_remove(fnic_fc_trace_enable);
  458. fnic_fc_trace_enable = NULL;
  459. debugfs_remove(fnic_fc_trace_clear);
  460. fnic_fc_trace_clear = NULL;
  461. }
  462. /*
  463. * fnic_reset_stats_open - Open the reset_stats file
  464. * @inode: The inode pointer.
  465. * @file: The file pointer to attach the stats reset flag.
  466. *
  467. * Description:
  468. * This routine opens a debugsfs file reset_stats and stores i_private data
  469. * to debug structure to retrieve later for while performing other
  470. * file oprations.
  471. *
  472. * Returns:
  473. * This function returns zero if successful.
  474. */
  475. static int fnic_reset_stats_open(struct inode *inode, struct file *file)
  476. {
  477. struct stats_debug_info *debug;
  478. debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
  479. if (!debug)
  480. return -ENOMEM;
  481. debug->i_private = inode->i_private;
  482. file->private_data = debug;
  483. return 0;
  484. }
  485. /*
  486. * fnic_reset_stats_read - Read a reset_stats debugfs file
  487. * @filp: The file pointer to read from.
  488. * @ubuf: The buffer to copy the data to.
  489. * @cnt: The number of bytes to read.
  490. * @ppos: The position in the file to start reading from.
  491. *
  492. * Description:
  493. * This routine reads value of variable reset_stats
  494. * and stores into local @buf. It will start reading file at @ppos and
  495. * copy up to @cnt of data to @ubuf from @buf.
  496. *
  497. * Returns:
  498. * This function returns the amount of data that was read.
  499. */
  500. static ssize_t fnic_reset_stats_read(struct file *file,
  501. char __user *ubuf,
  502. size_t cnt, loff_t *ppos)
  503. {
  504. struct stats_debug_info *debug = file->private_data;
  505. struct fnic *fnic = (struct fnic *)debug->i_private;
  506. char buf[64];
  507. int len;
  508. len = sprintf(buf, "%u\n", fnic->reset_stats);
  509. return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
  510. }
  511. /*
  512. * fnic_reset_stats_write - Write to reset_stats debugfs file
  513. * @filp: The file pointer to write from.
  514. * @ubuf: The buffer to copy the data from.
  515. * @cnt: The number of bytes to write.
  516. * @ppos: The position in the file to start writing to.
  517. *
  518. * Description:
  519. * This routine writes data from user buffer @ubuf to buffer @buf and
  520. * resets cumulative stats of fnic.
  521. *
  522. * Returns:
  523. * This function returns the amount of data that was written.
  524. */
  525. static ssize_t fnic_reset_stats_write(struct file *file,
  526. const char __user *ubuf,
  527. size_t cnt, loff_t *ppos)
  528. {
  529. struct stats_debug_info *debug = file->private_data;
  530. struct fnic *fnic = (struct fnic *)debug->i_private;
  531. struct fnic_stats *stats = &fnic->fnic_stats;
  532. u64 *io_stats_p = (u64 *)&stats->io_stats;
  533. u64 *fw_stats_p = (u64 *)&stats->fw_stats;
  534. char buf[64];
  535. unsigned long val;
  536. int ret;
  537. if (cnt >= sizeof(buf))
  538. return -EINVAL;
  539. if (copy_from_user(&buf, ubuf, cnt))
  540. return -EFAULT;
  541. buf[cnt] = 0;
  542. ret = kstrtoul(buf, 10, &val);
  543. if (ret < 0)
  544. return ret;
  545. fnic->reset_stats = val;
  546. if (fnic->reset_stats) {
  547. /* Skip variable is used to avoid descrepancies to Num IOs
  548. * and IO Completions stats. Skip incrementing No IO Compls
  549. * for pending active IOs after reset stats
  550. */
  551. atomic64_set(&fnic->io_cmpl_skip,
  552. atomic64_read(&stats->io_stats.active_ios));
  553. memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
  554. memset(&stats->term_stats, 0,
  555. sizeof(struct terminate_stats));
  556. memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
  557. memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
  558. memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
  559. memset(io_stats_p+1, 0,
  560. sizeof(struct io_path_stats) - sizeof(u64));
  561. memset(fw_stats_p+1, 0,
  562. sizeof(struct fw_stats) - sizeof(u64));
  563. }
  564. (*ppos)++;
  565. return cnt;
  566. }
  567. /*
  568. * fnic_reset_stats_release - Release the buffer used to store
  569. * debugfs file data
  570. * @inode: The inode pointer
  571. * @file: The file pointer that contains the buffer to release
  572. *
  573. * Description:
  574. * This routine frees the buffer that was allocated when the debugfs
  575. * file was opened.
  576. *
  577. * Returns:
  578. * This function returns zero.
  579. */
  580. static int fnic_reset_stats_release(struct inode *inode,
  581. struct file *file)
  582. {
  583. struct stats_debug_info *debug = file->private_data;
  584. kfree(debug);
  585. return 0;
  586. }
  587. /*
  588. * fnic_stats_debugfs_open - Open the stats file for specific host
  589. * and get fnic stats.
  590. * @inode: The inode pointer.
  591. * @file: The file pointer to attach the specific host statistics.
  592. *
  593. * Description:
  594. * This routine opens a debugsfs file stats of specific host and print
  595. * fnic stats.
  596. *
  597. * Returns:
  598. * This function returns zero if successful.
  599. */
  600. static int fnic_stats_debugfs_open(struct inode *inode,
  601. struct file *file)
  602. {
  603. struct fnic *fnic = inode->i_private;
  604. struct fnic_stats *fnic_stats = &fnic->fnic_stats;
  605. struct stats_debug_info *debug;
  606. int buf_size = 2 * PAGE_SIZE;
  607. debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
  608. if (!debug)
  609. return -ENOMEM;
  610. debug->debug_buffer = vmalloc(buf_size);
  611. if (!debug->debug_buffer) {
  612. kfree(debug);
  613. return -ENOMEM;
  614. }
  615. debug->buf_size = buf_size;
  616. memset((void *)debug->debug_buffer, 0, buf_size);
  617. debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
  618. file->private_data = debug;
  619. return 0;
  620. }
  621. /*
  622. * fnic_stats_debugfs_read - Read a debugfs file
  623. * @file: The file pointer to read from.
  624. * @ubuf: The buffer to copy the data to.
  625. * @nbytes: The number of bytes to read.
  626. * @pos: The position in the file to start reading from.
  627. *
  628. * Description:
  629. * This routine reads data from the buffer indicated in the private_data
  630. * field of @file. It will start reading at @pos and copy up to @nbytes of
  631. * data to @ubuf.
  632. *
  633. * Returns:
  634. * This function returns the amount of data that was read (this could be
  635. * less than @nbytes if the end of the file was reached).
  636. */
  637. static ssize_t fnic_stats_debugfs_read(struct file *file,
  638. char __user *ubuf,
  639. size_t nbytes,
  640. loff_t *pos)
  641. {
  642. struct stats_debug_info *debug = file->private_data;
  643. int rc = 0;
  644. rc = simple_read_from_buffer(ubuf, nbytes, pos,
  645. debug->debug_buffer,
  646. debug->buffer_len);
  647. return rc;
  648. }
  649. /*
  650. * fnic_stats_stats_release - Release the buffer used to store
  651. * debugfs file data
  652. * @inode: The inode pointer
  653. * @file: The file pointer that contains the buffer to release
  654. *
  655. * Description:
  656. * This routine frees the buffer that was allocated when the debugfs
  657. * file was opened.
  658. *
  659. * Returns:
  660. * This function returns zero.
  661. */
  662. static int fnic_stats_debugfs_release(struct inode *inode,
  663. struct file *file)
  664. {
  665. struct stats_debug_info *debug = file->private_data;
  666. vfree(debug->debug_buffer);
  667. kfree(debug);
  668. return 0;
  669. }
  670. static const struct file_operations fnic_stats_debugfs_fops = {
  671. .owner = THIS_MODULE,
  672. .open = fnic_stats_debugfs_open,
  673. .read = fnic_stats_debugfs_read,
  674. .release = fnic_stats_debugfs_release,
  675. };
  676. static const struct file_operations fnic_reset_debugfs_fops = {
  677. .owner = THIS_MODULE,
  678. .open = fnic_reset_stats_open,
  679. .read = fnic_reset_stats_read,
  680. .write = fnic_reset_stats_write,
  681. .release = fnic_reset_stats_release,
  682. };
  683. /*
  684. * fnic_stats_init - Initialize stats struct and create stats file per fnic
  685. *
  686. * Description:
  687. * When Debugfs is configured this routine sets up the stats file per fnic
  688. * It will create file stats and reset_stats under statistics/host# directory
  689. * to log per fnic stats.
  690. */
  691. int fnic_stats_debugfs_init(struct fnic *fnic)
  692. {
  693. int rc = -1;
  694. char name[16];
  695. snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
  696. if (!fnic_stats_debugfs_root) {
  697. printk(KERN_DEBUG "fnic_stats root doesn't exist\n");
  698. return rc;
  699. }
  700. fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
  701. fnic_stats_debugfs_root);
  702. if (!fnic->fnic_stats_debugfs_host) {
  703. printk(KERN_DEBUG "Cannot create host directory\n");
  704. return rc;
  705. }
  706. fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
  707. S_IFREG|S_IRUGO|S_IWUSR,
  708. fnic->fnic_stats_debugfs_host,
  709. fnic,
  710. &fnic_stats_debugfs_fops);
  711. if (!fnic->fnic_stats_debugfs_file) {
  712. printk(KERN_DEBUG "Cannot create host stats file\n");
  713. return rc;
  714. }
  715. fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
  716. S_IFREG|S_IRUGO|S_IWUSR,
  717. fnic->fnic_stats_debugfs_host,
  718. fnic,
  719. &fnic_reset_debugfs_fops);
  720. if (!fnic->fnic_reset_debugfs_file) {
  721. printk(KERN_DEBUG "Cannot create host stats file\n");
  722. return rc;
  723. }
  724. rc = 0;
  725. return rc;
  726. }
  727. /*
  728. * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
  729. *
  730. * Description:
  731. * When Debugfs is configured this routine removes debugfs file system
  732. * elements that are specific to fnic stats.
  733. */
  734. void fnic_stats_debugfs_remove(struct fnic *fnic)
  735. {
  736. if (!fnic)
  737. return;
  738. debugfs_remove(fnic->fnic_stats_debugfs_file);
  739. fnic->fnic_stats_debugfs_file = NULL;
  740. debugfs_remove(fnic->fnic_reset_debugfs_file);
  741. fnic->fnic_reset_debugfs_file = NULL;
  742. debugfs_remove(fnic->fnic_stats_debugfs_host);
  743. fnic->fnic_stats_debugfs_host = NULL;
  744. }