snic_trc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. #include <linux/module.h>
  4. #include <linux/mempool.h>
  5. #include <linux/errno.h>
  6. #include <linux/vmalloc.h>
  7. #include "snic_io.h"
  8. #include "snic.h"
  9. /*
  10. * snic_get_trc_buf : Allocates a trace record and returns.
  11. */
  12. struct snic_trc_data *
  13. snic_get_trc_buf(void)
  14. {
  15. struct snic_trc *trc = &snic_glob->trc;
  16. struct snic_trc_data *td = NULL;
  17. unsigned long flags;
  18. spin_lock_irqsave(&trc->lock, flags);
  19. td = &trc->buf[trc->wr_idx];
  20. trc->wr_idx++;
  21. if (trc->wr_idx == trc->max_idx)
  22. trc->wr_idx = 0;
  23. if (trc->wr_idx != trc->rd_idx) {
  24. spin_unlock_irqrestore(&trc->lock, flags);
  25. goto end;
  26. }
  27. trc->rd_idx++;
  28. if (trc->rd_idx == trc->max_idx)
  29. trc->rd_idx = 0;
  30. td->ts = 0; /* Marker for checking the record, for complete data*/
  31. spin_unlock_irqrestore(&trc->lock, flags);
  32. end:
  33. return td;
  34. } /* end of snic_get_trc_buf */
  35. /*
  36. * snic_fmt_trc_data : Formats trace data for printing.
  37. */
  38. static int
  39. snic_fmt_trc_data(struct snic_trc_data *td, char *buf, int buf_sz)
  40. {
  41. int len = 0;
  42. struct timespec64 tmspec;
  43. jiffies_to_timespec64(td->ts, &tmspec);
  44. len += snprintf(buf, buf_sz,
  45. "%llu.%09lu %-25s %3d %4x %16llx %16llx %16llx %16llx %16llx\n",
  46. tmspec.tv_sec,
  47. tmspec.tv_nsec,
  48. td->fn,
  49. td->hno,
  50. td->tag,
  51. td->data[0], td->data[1], td->data[2], td->data[3],
  52. td->data[4]);
  53. return len;
  54. } /* end of snic_fmt_trc_data */
  55. /*
  56. * snic_get_trc_data : Returns a formatted trace buffer.
  57. */
  58. int
  59. snic_get_trc_data(char *buf, int buf_sz)
  60. {
  61. struct snic_trc_data *td = NULL;
  62. struct snic_trc *trc = &snic_glob->trc;
  63. unsigned long flags;
  64. spin_lock_irqsave(&trc->lock, flags);
  65. if (trc->rd_idx == trc->wr_idx) {
  66. spin_unlock_irqrestore(&trc->lock, flags);
  67. return -1;
  68. }
  69. td = &trc->buf[trc->rd_idx];
  70. if (td->ts == 0) {
  71. /* write in progress. */
  72. spin_unlock_irqrestore(&trc->lock, flags);
  73. return -1;
  74. }
  75. trc->rd_idx++;
  76. if (trc->rd_idx == trc->max_idx)
  77. trc->rd_idx = 0;
  78. spin_unlock_irqrestore(&trc->lock, flags);
  79. return snic_fmt_trc_data(td, buf, buf_sz);
  80. } /* end of snic_get_trc_data */
  81. /*
  82. * snic_trc_init() : Configures Trace Functionality for snic.
  83. */
  84. int
  85. snic_trc_init(void)
  86. {
  87. struct snic_trc *trc = &snic_glob->trc;
  88. void *tbuf = NULL;
  89. int tbuf_sz = 0, ret;
  90. tbuf_sz = (snic_trace_max_pages * PAGE_SIZE);
  91. tbuf = vzalloc(tbuf_sz);
  92. if (!tbuf) {
  93. SNIC_ERR("Failed to Allocate Trace Buffer Size. %d\n", tbuf_sz);
  94. SNIC_ERR("Trace Facility not enabled.\n");
  95. ret = -ENOMEM;
  96. return ret;
  97. }
  98. trc->buf = (struct snic_trc_data *) tbuf;
  99. spin_lock_init(&trc->lock);
  100. snic_trc_debugfs_init();
  101. trc->max_idx = (tbuf_sz / SNIC_TRC_ENTRY_SZ);
  102. trc->rd_idx = trc->wr_idx = 0;
  103. trc->enable = true;
  104. SNIC_INFO("Trace Facility Enabled.\n Trace Buffer SZ %lu Pages.\n",
  105. tbuf_sz / PAGE_SIZE);
  106. ret = 0;
  107. return ret;
  108. } /* end of snic_trc_init */
  109. /*
  110. * snic_trc_free : Releases the trace buffer and disables the tracing.
  111. */
  112. void
  113. snic_trc_free(void)
  114. {
  115. struct snic_trc *trc = &snic_glob->trc;
  116. trc->enable = false;
  117. snic_trc_debugfs_term();
  118. if (trc->buf) {
  119. vfree(trc->buf);
  120. trc->buf = NULL;
  121. }
  122. SNIC_INFO("Trace Facility Disabled.\n");
  123. } /* end of snic_trc_free */