fnic_debugfs.c 22 KB

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