fnic_trace.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/mempool.h>
  19. #include <linux/errno.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/kallsyms.h>
  22. #include "fnic_io.h"
  23. #include "fnic.h"
  24. unsigned int trace_max_pages;
  25. static int fnic_max_trace_entries;
  26. static unsigned long fnic_trace_buf_p;
  27. static DEFINE_SPINLOCK(fnic_trace_lock);
  28. static fnic_trace_dbg_t fnic_trace_entries;
  29. int fnic_tracing_enabled = 1;
  30. /*
  31. * fnic_trace_get_buf - Give buffer pointer to user to fill up trace information
  32. *
  33. * Description:
  34. * This routine gets next available trace buffer entry location @wr_idx
  35. * from allocated trace buffer pages and give that memory location
  36. * to user to store the trace information.
  37. *
  38. * Return Value:
  39. * This routine returns pointer to next available trace entry
  40. * @fnic_buf_head for user to fill trace information.
  41. */
  42. fnic_trace_data_t *fnic_trace_get_buf(void)
  43. {
  44. unsigned long fnic_buf_head;
  45. unsigned long flags;
  46. spin_lock_irqsave(&fnic_trace_lock, flags);
  47. /*
  48. * Get next available memory location for writing trace information
  49. * at @wr_idx and increment @wr_idx
  50. */
  51. fnic_buf_head =
  52. fnic_trace_entries.page_offset[fnic_trace_entries.wr_idx];
  53. fnic_trace_entries.wr_idx++;
  54. /*
  55. * Verify if trace buffer is full then change wd_idx to
  56. * start from zero
  57. */
  58. if (fnic_trace_entries.wr_idx >= fnic_max_trace_entries)
  59. fnic_trace_entries.wr_idx = 0;
  60. /*
  61. * Verify if write index @wr_idx and read index @rd_idx are same then
  62. * increment @rd_idx to move to next entry in trace buffer
  63. */
  64. if (fnic_trace_entries.wr_idx == fnic_trace_entries.rd_idx) {
  65. fnic_trace_entries.rd_idx++;
  66. if (fnic_trace_entries.rd_idx >= fnic_max_trace_entries)
  67. fnic_trace_entries.rd_idx = 0;
  68. }
  69. spin_unlock_irqrestore(&fnic_trace_lock, flags);
  70. return (fnic_trace_data_t *)fnic_buf_head;
  71. }
  72. /*
  73. * fnic_get_trace_data - Copy trace buffer to a memory file
  74. * @fnic_dbgfs_t: pointer to debugfs trace buffer
  75. *
  76. * Description:
  77. * This routine gathers the fnic trace debugfs data from the fnic_trace_data_t
  78. * buffer and dumps it to fnic_dbgfs_t. It will start at the rd_idx entry in
  79. * the log and process the log until the end of the buffer. Then it will gather
  80. * from the beginning of the log and process until the current entry @wr_idx.
  81. *
  82. * Return Value:
  83. * This routine returns the amount of bytes that were dumped into fnic_dbgfs_t
  84. */
  85. int fnic_get_trace_data(fnic_dbgfs_t *fnic_dbgfs_prt)
  86. {
  87. int rd_idx;
  88. int wr_idx;
  89. int len = 0;
  90. unsigned long flags;
  91. char str[KSYM_SYMBOL_LEN];
  92. struct timespec val;
  93. fnic_trace_data_t *tbp;
  94. spin_lock_irqsave(&fnic_trace_lock, flags);
  95. rd_idx = fnic_trace_entries.rd_idx;
  96. wr_idx = fnic_trace_entries.wr_idx;
  97. if (wr_idx < rd_idx) {
  98. while (1) {
  99. /* Start from read index @rd_idx */
  100. tbp = (fnic_trace_data_t *)
  101. fnic_trace_entries.page_offset[rd_idx];
  102. if (!tbp) {
  103. spin_unlock_irqrestore(&fnic_trace_lock, flags);
  104. return 0;
  105. }
  106. /* Convert function pointer to function name */
  107. if (sizeof(unsigned long) < 8) {
  108. sprint_symbol(str, tbp->fnaddr.low);
  109. jiffies_to_timespec(tbp->timestamp.low, &val);
  110. } else {
  111. sprint_symbol(str, tbp->fnaddr.val);
  112. jiffies_to_timespec(tbp->timestamp.val, &val);
  113. }
  114. /*
  115. * Dump trace buffer entry to memory file
  116. * and increment read index @rd_idx
  117. */
  118. len += snprintf(fnic_dbgfs_prt->buffer + len,
  119. (trace_max_pages * PAGE_SIZE * 3) - len,
  120. "%16lu.%16lu %-50s %8x %8x %16llx %16llx "
  121. "%16llx %16llx %16llx\n", val.tv_sec,
  122. val.tv_nsec, str, tbp->host_no, tbp->tag,
  123. tbp->data[0], tbp->data[1], tbp->data[2],
  124. tbp->data[3], tbp->data[4]);
  125. rd_idx++;
  126. /*
  127. * If rd_idx is reached to maximum trace entries
  128. * then move rd_idx to zero
  129. */
  130. if (rd_idx > (fnic_max_trace_entries-1))
  131. rd_idx = 0;
  132. /*
  133. * Continure dumpping trace buffer entries into
  134. * memory file till rd_idx reaches write index
  135. */
  136. if (rd_idx == wr_idx)
  137. break;
  138. }
  139. } else if (wr_idx > rd_idx) {
  140. while (1) {
  141. /* Start from read index @rd_idx */
  142. tbp = (fnic_trace_data_t *)
  143. fnic_trace_entries.page_offset[rd_idx];
  144. if (!tbp) {
  145. spin_unlock_irqrestore(&fnic_trace_lock, flags);
  146. return 0;
  147. }
  148. /* Convert function pointer to function name */
  149. if (sizeof(unsigned long) < 8) {
  150. sprint_symbol(str, tbp->fnaddr.low);
  151. jiffies_to_timespec(tbp->timestamp.low, &val);
  152. } else {
  153. sprint_symbol(str, tbp->fnaddr.val);
  154. jiffies_to_timespec(tbp->timestamp.val, &val);
  155. }
  156. /*
  157. * Dump trace buffer entry to memory file
  158. * and increment read index @rd_idx
  159. */
  160. len += snprintf(fnic_dbgfs_prt->buffer + len,
  161. (trace_max_pages * PAGE_SIZE * 3) - len,
  162. "%16lu.%16lu %-50s %8x %8x %16llx %16llx "
  163. "%16llx %16llx %16llx\n", val.tv_sec,
  164. val.tv_nsec, str, tbp->host_no, tbp->tag,
  165. tbp->data[0], tbp->data[1], tbp->data[2],
  166. tbp->data[3], tbp->data[4]);
  167. rd_idx++;
  168. /*
  169. * Continue dumpping trace buffer entries into
  170. * memory file till rd_idx reaches write index
  171. */
  172. if (rd_idx == wr_idx)
  173. break;
  174. }
  175. }
  176. spin_unlock_irqrestore(&fnic_trace_lock, flags);
  177. return len;
  178. }
  179. /*
  180. * fnic_trace_buf_init - Initialize fnic trace buffer logging facility
  181. *
  182. * Description:
  183. * Initialize trace buffer data structure by allocating required memory and
  184. * setting page_offset information for every trace entry by adding trace entry
  185. * length to previous page_offset value.
  186. */
  187. int fnic_trace_buf_init(void)
  188. {
  189. unsigned long fnic_buf_head;
  190. int i;
  191. int err = 0;
  192. trace_max_pages = fnic_trace_max_pages;
  193. fnic_max_trace_entries = (trace_max_pages * PAGE_SIZE)/
  194. FNIC_ENTRY_SIZE_BYTES;
  195. fnic_trace_buf_p = (unsigned long)vmalloc((trace_max_pages * PAGE_SIZE));
  196. if (!fnic_trace_buf_p) {
  197. printk(KERN_ERR PFX "Failed to allocate memory "
  198. "for fnic_trace_buf_p\n");
  199. err = -ENOMEM;
  200. goto err_fnic_trace_buf_init;
  201. }
  202. memset((void *)fnic_trace_buf_p, 0, (trace_max_pages * PAGE_SIZE));
  203. fnic_trace_entries.page_offset = vmalloc(fnic_max_trace_entries *
  204. sizeof(unsigned long));
  205. if (!fnic_trace_entries.page_offset) {
  206. printk(KERN_ERR PFX "Failed to allocate memory for"
  207. " page_offset\n");
  208. if (fnic_trace_buf_p) {
  209. vfree((void *)fnic_trace_buf_p);
  210. fnic_trace_buf_p = 0;
  211. }
  212. err = -ENOMEM;
  213. goto err_fnic_trace_buf_init;
  214. }
  215. memset((void *)fnic_trace_entries.page_offset, 0,
  216. (fnic_max_trace_entries * sizeof(unsigned long)));
  217. fnic_trace_entries.wr_idx = fnic_trace_entries.rd_idx = 0;
  218. fnic_buf_head = fnic_trace_buf_p;
  219. /*
  220. * Set page_offset field of fnic_trace_entries struct by
  221. * calculating memory location for every trace entry using
  222. * length of each trace entry
  223. */
  224. for (i = 0; i < fnic_max_trace_entries; i++) {
  225. fnic_trace_entries.page_offset[i] = fnic_buf_head;
  226. fnic_buf_head += FNIC_ENTRY_SIZE_BYTES;
  227. }
  228. err = fnic_trace_debugfs_init();
  229. if (err < 0) {
  230. printk(KERN_ERR PFX "Failed to initialize debugfs for tracing\n");
  231. goto err_fnic_trace_debugfs_init;
  232. }
  233. printk(KERN_INFO PFX "Successfully Initialized Trace Buffer\n");
  234. return err;
  235. err_fnic_trace_debugfs_init:
  236. fnic_trace_free();
  237. err_fnic_trace_buf_init:
  238. return err;
  239. }
  240. /*
  241. * fnic_trace_free - Free memory of fnic trace data structures.
  242. */
  243. void fnic_trace_free(void)
  244. {
  245. fnic_tracing_enabled = 0;
  246. fnic_trace_debugfs_terminate();
  247. if (fnic_trace_entries.page_offset) {
  248. vfree((void *)fnic_trace_entries.page_offset);
  249. fnic_trace_entries.page_offset = NULL;
  250. }
  251. if (fnic_trace_buf_p) {
  252. vfree((void *)fnic_trace_buf_p);
  253. fnic_trace_buf_p = 0;
  254. }
  255. printk(KERN_INFO PFX "Successfully Freed Trace Buffer\n");
  256. }