snic_trc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2014 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/vmalloc.h>
  21. #include "snic_io.h"
  22. #include "snic.h"
  23. /*
  24. * snic_get_trc_buf : Allocates a trace record and returns.
  25. */
  26. struct snic_trc_data *
  27. snic_get_trc_buf(void)
  28. {
  29. struct snic_trc *trc = &snic_glob->trc;
  30. struct snic_trc_data *td = NULL;
  31. unsigned long flags;
  32. spin_lock_irqsave(&trc->lock, flags);
  33. td = &trc->buf[trc->wr_idx];
  34. trc->wr_idx++;
  35. if (trc->wr_idx == trc->max_idx)
  36. trc->wr_idx = 0;
  37. if (trc->wr_idx != trc->rd_idx) {
  38. spin_unlock_irqrestore(&trc->lock, flags);
  39. goto end;
  40. }
  41. trc->rd_idx++;
  42. if (trc->rd_idx == trc->max_idx)
  43. trc->rd_idx = 0;
  44. td->ts = 0; /* Marker for checking the record, for complete data*/
  45. spin_unlock_irqrestore(&trc->lock, flags);
  46. end:
  47. return td;
  48. } /* end of snic_get_trc_buf */
  49. /*
  50. * snic_fmt_trc_data : Formats trace data for printing.
  51. */
  52. static int
  53. snic_fmt_trc_data(struct snic_trc_data *td, char *buf, int buf_sz)
  54. {
  55. int len = 0;
  56. struct timespec tmspec;
  57. jiffies_to_timespec(td->ts, &tmspec);
  58. len += snprintf(buf, buf_sz,
  59. "%lu.%10lu %-25s %3d %4x %16llx %16llx %16llx %16llx %16llx\n",
  60. tmspec.tv_sec,
  61. tmspec.tv_nsec,
  62. td->fn,
  63. td->hno,
  64. td->tag,
  65. td->data[0], td->data[1], td->data[2], td->data[3],
  66. td->data[4]);
  67. return len;
  68. } /* end of snic_fmt_trc_data */
  69. /*
  70. * snic_get_trc_data : Returns a formatted trace buffer.
  71. */
  72. int
  73. snic_get_trc_data(char *buf, int buf_sz)
  74. {
  75. struct snic_trc_data *td = NULL;
  76. struct snic_trc *trc = &snic_glob->trc;
  77. unsigned long flags;
  78. spin_lock_irqsave(&trc->lock, flags);
  79. if (trc->rd_idx == trc->wr_idx) {
  80. spin_unlock_irqrestore(&trc->lock, flags);
  81. return -1;
  82. }
  83. td = &trc->buf[trc->rd_idx];
  84. if (td->ts == 0) {
  85. /* write in progress. */
  86. spin_unlock_irqrestore(&trc->lock, flags);
  87. return -1;
  88. }
  89. trc->rd_idx++;
  90. if (trc->rd_idx == trc->max_idx)
  91. trc->rd_idx = 0;
  92. spin_unlock_irqrestore(&trc->lock, flags);
  93. return snic_fmt_trc_data(td, buf, buf_sz);
  94. } /* end of snic_get_trc_data */
  95. /*
  96. * snic_trc_init() : Configures Trace Functionality for snic.
  97. */
  98. int
  99. snic_trc_init(void)
  100. {
  101. struct snic_trc *trc = &snic_glob->trc;
  102. void *tbuf = NULL;
  103. int tbuf_sz = 0, ret;
  104. tbuf_sz = (snic_trace_max_pages * PAGE_SIZE);
  105. tbuf = vmalloc(tbuf_sz);
  106. if (!tbuf) {
  107. SNIC_ERR("Failed to Allocate Trace Buffer Size. %d\n", tbuf_sz);
  108. SNIC_ERR("Trace Facility not enabled.\n");
  109. ret = -ENOMEM;
  110. return ret;
  111. }
  112. memset(tbuf, 0, tbuf_sz);
  113. trc->buf = (struct snic_trc_data *) tbuf;
  114. spin_lock_init(&trc->lock);
  115. ret = snic_trc_debugfs_init();
  116. if (ret) {
  117. SNIC_ERR("Failed to create Debugfs Files.\n");
  118. goto error;
  119. }
  120. trc->max_idx = (tbuf_sz / SNIC_TRC_ENTRY_SZ);
  121. trc->rd_idx = trc->wr_idx = 0;
  122. trc->enable = 1;
  123. SNIC_INFO("Trace Facility Enabled.\n Trace Buffer SZ %lu Pages.\n",
  124. tbuf_sz / PAGE_SIZE);
  125. ret = 0;
  126. return ret;
  127. error:
  128. snic_trc_free();
  129. return ret;
  130. } /* end of snic_trc_init */
  131. /*
  132. * snic_trc_free : Releases the trace buffer and disables the tracing.
  133. */
  134. void
  135. snic_trc_free(void)
  136. {
  137. struct snic_trc *trc = &snic_glob->trc;
  138. trc->enable = 0;
  139. snic_trc_debugfs_term();
  140. if (trc->buf) {
  141. vfree(trc->buf);
  142. trc->buf = NULL;
  143. }
  144. SNIC_INFO("Trace Facility Disabled.\n");
  145. } /* end of snic_trc_free */