1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2014 Cisco Systems, Inc. All rights reserved.
4 #include <linux/module.h>
5 #include <linux/mempool.h>
6 #include <linux/errno.h>
7 #include <linux/vmalloc.h>
13 * snic_get_trc_buf : Allocates a trace record and returns.
15 struct snic_trc_data *
16 snic_get_trc_buf(void)
18 struct snic_trc *trc = &snic_glob->trc;
19 struct snic_trc_data *td = NULL;
22 spin_lock_irqsave(&trc->lock, flags);
23 td = &trc->buf[trc->wr_idx];
26 if (trc->wr_idx == trc->max_idx)
29 if (trc->wr_idx != trc->rd_idx) {
30 spin_unlock_irqrestore(&trc->lock, flags);
36 if (trc->rd_idx == trc->max_idx)
39 td->ts = 0; /* Marker for checking the record, for complete data*/
40 spin_unlock_irqrestore(&trc->lock, flags);
45 } /* end of snic_get_trc_buf */
48 * snic_fmt_trc_data : Formats trace data for printing.
51 snic_fmt_trc_data(struct snic_trc_data *td, char *buf, int buf_sz)
54 struct timespec64 tmspec;
56 jiffies_to_timespec64(td->ts, &tmspec);
58 len += snprintf(buf, buf_sz,
59 "%llu.%09lu %-25s %3d %4x %16llx %16llx %16llx %16llx %16llx\n",
65 td->data[0], td->data[1], td->data[2], td->data[3],
69 } /* end of snic_fmt_trc_data */
72 * snic_get_trc_data : Returns a formatted trace buffer.
75 snic_get_trc_data(char *buf, int buf_sz)
77 struct snic_trc_data *td = NULL;
78 struct snic_trc *trc = &snic_glob->trc;
81 spin_lock_irqsave(&trc->lock, flags);
82 if (trc->rd_idx == trc->wr_idx) {
83 spin_unlock_irqrestore(&trc->lock, flags);
87 td = &trc->buf[trc->rd_idx];
90 /* write in progress. */
91 spin_unlock_irqrestore(&trc->lock, flags);
97 if (trc->rd_idx == trc->max_idx)
99 spin_unlock_irqrestore(&trc->lock, flags);
101 return snic_fmt_trc_data(td, buf, buf_sz);
102 } /* end of snic_get_trc_data */
105 * snic_trc_init() : Configures Trace Functionality for snic.
110 struct snic_trc *trc = &snic_glob->trc;
112 int tbuf_sz = 0, ret;
114 tbuf_sz = (snic_trace_max_pages * PAGE_SIZE);
115 tbuf = vzalloc(tbuf_sz);
117 SNIC_ERR("Failed to Allocate Trace Buffer Size. %d\n", tbuf_sz);
118 SNIC_ERR("Trace Facility not enabled.\n");
124 trc->buf = (struct snic_trc_data *) tbuf;
125 spin_lock_init(&trc->lock);
127 snic_trc_debugfs_init();
129 trc->max_idx = (tbuf_sz / SNIC_TRC_ENTRY_SZ);
130 trc->rd_idx = trc->wr_idx = 0;
132 SNIC_INFO("Trace Facility Enabled.\n Trace Buffer SZ %lu Pages.\n",
133 tbuf_sz / PAGE_SIZE);
137 } /* end of snic_trc_init */
140 * snic_trc_free : Releases the trace buffer and disables the tracing.
145 struct snic_trc *trc = &snic_glob->trc;
148 snic_trc_debugfs_term();
155 SNIC_INFO("Trace Facility Disabled.\n");
156 } /* end of snic_trc_free */