xref: /OK3568_Linux_fs/kernel/drivers/scsi/fnic/fnic_trace.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2012 Cisco Systems, Inc.  All rights reserved.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This program is free software; you may redistribute it and/or modify
5*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
6*4882a593Smuzhiyun  * the Free Software Foundation; version 2 of the License.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15*4882a593Smuzhiyun  * SOFTWARE.
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/mempool.h>
20*4882a593Smuzhiyun #include <linux/errno.h>
21*4882a593Smuzhiyun #include <linux/spinlock.h>
22*4882a593Smuzhiyun #include <linux/kallsyms.h>
23*4882a593Smuzhiyun #include <linux/time.h>
24*4882a593Smuzhiyun #include <linux/vmalloc.h>
25*4882a593Smuzhiyun #include "fnic_io.h"
26*4882a593Smuzhiyun #include "fnic.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun unsigned int trace_max_pages;
29*4882a593Smuzhiyun static int fnic_max_trace_entries;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun static unsigned long fnic_trace_buf_p;
32*4882a593Smuzhiyun static DEFINE_SPINLOCK(fnic_trace_lock);
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static fnic_trace_dbg_t fnic_trace_entries;
35*4882a593Smuzhiyun int fnic_tracing_enabled = 1;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /* static char *fnic_fc_ctlr_trace_buf_p; */
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun static int fc_trace_max_entries;
40*4882a593Smuzhiyun static unsigned long fnic_fc_ctlr_trace_buf_p;
41*4882a593Smuzhiyun static fnic_trace_dbg_t fc_trace_entries;
42*4882a593Smuzhiyun int fnic_fc_tracing_enabled = 1;
43*4882a593Smuzhiyun int fnic_fc_trace_cleared = 1;
44*4882a593Smuzhiyun static DEFINE_SPINLOCK(fnic_fc_trace_lock);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun  * fnic_trace_get_buf - Give buffer pointer to user to fill up trace information
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * Description:
51*4882a593Smuzhiyun  * This routine gets next available trace buffer entry location @wr_idx
52*4882a593Smuzhiyun  * from allocated trace buffer pages and give that memory location
53*4882a593Smuzhiyun  * to user to store the trace information.
54*4882a593Smuzhiyun  *
55*4882a593Smuzhiyun  * Return Value:
56*4882a593Smuzhiyun  * This routine returns pointer to next available trace entry
57*4882a593Smuzhiyun  * @fnic_buf_head for user to fill trace information.
58*4882a593Smuzhiyun  */
fnic_trace_get_buf(void)59*4882a593Smuzhiyun fnic_trace_data_t *fnic_trace_get_buf(void)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	unsigned long fnic_buf_head;
62*4882a593Smuzhiyun 	unsigned long flags;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	spin_lock_irqsave(&fnic_trace_lock, flags);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	/*
67*4882a593Smuzhiyun 	 * Get next available memory location for writing trace information
68*4882a593Smuzhiyun 	 * at @wr_idx and increment @wr_idx
69*4882a593Smuzhiyun 	 */
70*4882a593Smuzhiyun 	fnic_buf_head =
71*4882a593Smuzhiyun 		fnic_trace_entries.page_offset[fnic_trace_entries.wr_idx];
72*4882a593Smuzhiyun 	fnic_trace_entries.wr_idx++;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/*
75*4882a593Smuzhiyun 	 * Verify if trace buffer is full then change wd_idx to
76*4882a593Smuzhiyun 	 * start from zero
77*4882a593Smuzhiyun 	 */
78*4882a593Smuzhiyun 	if (fnic_trace_entries.wr_idx >= fnic_max_trace_entries)
79*4882a593Smuzhiyun 		fnic_trace_entries.wr_idx = 0;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/*
82*4882a593Smuzhiyun 	 * Verify if write index @wr_idx and read index @rd_idx are same then
83*4882a593Smuzhiyun 	 * increment @rd_idx to move to next entry in trace buffer
84*4882a593Smuzhiyun 	 */
85*4882a593Smuzhiyun 	if (fnic_trace_entries.wr_idx == fnic_trace_entries.rd_idx) {
86*4882a593Smuzhiyun 		fnic_trace_entries.rd_idx++;
87*4882a593Smuzhiyun 		if (fnic_trace_entries.rd_idx >= fnic_max_trace_entries)
88*4882a593Smuzhiyun 			fnic_trace_entries.rd_idx = 0;
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 	spin_unlock_irqrestore(&fnic_trace_lock, flags);
91*4882a593Smuzhiyun 	return (fnic_trace_data_t *)fnic_buf_head;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun  * fnic_get_trace_data - Copy trace buffer to a memory file
96*4882a593Smuzhiyun  * @fnic_dbgfs_t: pointer to debugfs trace buffer
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * Description:
99*4882a593Smuzhiyun  * This routine gathers the fnic trace debugfs data from the fnic_trace_data_t
100*4882a593Smuzhiyun  * buffer and dumps it to fnic_dbgfs_t. It will start at the rd_idx entry in
101*4882a593Smuzhiyun  * the log and process the log until the end of the buffer. Then it will gather
102*4882a593Smuzhiyun  * from the beginning of the log and process until the current entry @wr_idx.
103*4882a593Smuzhiyun  *
104*4882a593Smuzhiyun  * Return Value:
105*4882a593Smuzhiyun  * This routine returns the amount of bytes that were dumped into fnic_dbgfs_t
106*4882a593Smuzhiyun  */
fnic_get_trace_data(fnic_dbgfs_t * fnic_dbgfs_prt)107*4882a593Smuzhiyun int fnic_get_trace_data(fnic_dbgfs_t *fnic_dbgfs_prt)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	int rd_idx;
110*4882a593Smuzhiyun 	int wr_idx;
111*4882a593Smuzhiyun 	int len = 0;
112*4882a593Smuzhiyun 	unsigned long flags;
113*4882a593Smuzhiyun 	char str[KSYM_SYMBOL_LEN];
114*4882a593Smuzhiyun 	struct timespec64 val;
115*4882a593Smuzhiyun 	fnic_trace_data_t *tbp;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	spin_lock_irqsave(&fnic_trace_lock, flags);
118*4882a593Smuzhiyun 	rd_idx = fnic_trace_entries.rd_idx;
119*4882a593Smuzhiyun 	wr_idx = fnic_trace_entries.wr_idx;
120*4882a593Smuzhiyun 	if (wr_idx < rd_idx) {
121*4882a593Smuzhiyun 		while (1) {
122*4882a593Smuzhiyun 			/* Start from read index @rd_idx */
123*4882a593Smuzhiyun 			tbp = (fnic_trace_data_t *)
124*4882a593Smuzhiyun 				  fnic_trace_entries.page_offset[rd_idx];
125*4882a593Smuzhiyun 			if (!tbp) {
126*4882a593Smuzhiyun 				spin_unlock_irqrestore(&fnic_trace_lock, flags);
127*4882a593Smuzhiyun 				return 0;
128*4882a593Smuzhiyun 			}
129*4882a593Smuzhiyun 			/* Convert function pointer to function name */
130*4882a593Smuzhiyun 			if (sizeof(unsigned long) < 8) {
131*4882a593Smuzhiyun 				sprint_symbol(str, tbp->fnaddr.low);
132*4882a593Smuzhiyun 				jiffies_to_timespec64(tbp->timestamp.low, &val);
133*4882a593Smuzhiyun 			} else {
134*4882a593Smuzhiyun 				sprint_symbol(str, tbp->fnaddr.val);
135*4882a593Smuzhiyun 				jiffies_to_timespec64(tbp->timestamp.val, &val);
136*4882a593Smuzhiyun 			}
137*4882a593Smuzhiyun 			/*
138*4882a593Smuzhiyun 			 * Dump trace buffer entry to memory file
139*4882a593Smuzhiyun 			 * and increment read index @rd_idx
140*4882a593Smuzhiyun 			 */
141*4882a593Smuzhiyun 			len += scnprintf(fnic_dbgfs_prt->buffer + len,
142*4882a593Smuzhiyun 				  (trace_max_pages * PAGE_SIZE * 3) - len,
143*4882a593Smuzhiyun 				  "%16llu.%09lu %-50s %8x %8x %16llx %16llx "
144*4882a593Smuzhiyun 				  "%16llx %16llx %16llx\n", (u64)val.tv_sec,
145*4882a593Smuzhiyun 				  val.tv_nsec, str, tbp->host_no, tbp->tag,
146*4882a593Smuzhiyun 				  tbp->data[0], tbp->data[1], tbp->data[2],
147*4882a593Smuzhiyun 				  tbp->data[3], tbp->data[4]);
148*4882a593Smuzhiyun 			rd_idx++;
149*4882a593Smuzhiyun 			/*
150*4882a593Smuzhiyun 			 * If rd_idx is reached to maximum trace entries
151*4882a593Smuzhiyun 			 * then move rd_idx to zero
152*4882a593Smuzhiyun 			 */
153*4882a593Smuzhiyun 			if (rd_idx > (fnic_max_trace_entries-1))
154*4882a593Smuzhiyun 				rd_idx = 0;
155*4882a593Smuzhiyun 			/*
156*4882a593Smuzhiyun 			 * Continure dumpping trace buffer entries into
157*4882a593Smuzhiyun 			 * memory file till rd_idx reaches write index
158*4882a593Smuzhiyun 			 */
159*4882a593Smuzhiyun 			if (rd_idx == wr_idx)
160*4882a593Smuzhiyun 				break;
161*4882a593Smuzhiyun 		}
162*4882a593Smuzhiyun 	} else if (wr_idx > rd_idx) {
163*4882a593Smuzhiyun 		while (1) {
164*4882a593Smuzhiyun 			/* Start from read index @rd_idx */
165*4882a593Smuzhiyun 			tbp = (fnic_trace_data_t *)
166*4882a593Smuzhiyun 				  fnic_trace_entries.page_offset[rd_idx];
167*4882a593Smuzhiyun 			if (!tbp) {
168*4882a593Smuzhiyun 				spin_unlock_irqrestore(&fnic_trace_lock, flags);
169*4882a593Smuzhiyun 				return 0;
170*4882a593Smuzhiyun 			}
171*4882a593Smuzhiyun 			/* Convert function pointer to function name */
172*4882a593Smuzhiyun 			if (sizeof(unsigned long) < 8) {
173*4882a593Smuzhiyun 				sprint_symbol(str, tbp->fnaddr.low);
174*4882a593Smuzhiyun 				jiffies_to_timespec64(tbp->timestamp.low, &val);
175*4882a593Smuzhiyun 			} else {
176*4882a593Smuzhiyun 				sprint_symbol(str, tbp->fnaddr.val);
177*4882a593Smuzhiyun 				jiffies_to_timespec64(tbp->timestamp.val, &val);
178*4882a593Smuzhiyun 			}
179*4882a593Smuzhiyun 			/*
180*4882a593Smuzhiyun 			 * Dump trace buffer entry to memory file
181*4882a593Smuzhiyun 			 * and increment read index @rd_idx
182*4882a593Smuzhiyun 			 */
183*4882a593Smuzhiyun 			len += scnprintf(fnic_dbgfs_prt->buffer + len,
184*4882a593Smuzhiyun 				  (trace_max_pages * PAGE_SIZE * 3) - len,
185*4882a593Smuzhiyun 				  "%16llu.%09lu %-50s %8x %8x %16llx %16llx "
186*4882a593Smuzhiyun 				  "%16llx %16llx %16llx\n", (u64)val.tv_sec,
187*4882a593Smuzhiyun 				  val.tv_nsec, str, tbp->host_no, tbp->tag,
188*4882a593Smuzhiyun 				  tbp->data[0], tbp->data[1], tbp->data[2],
189*4882a593Smuzhiyun 				  tbp->data[3], tbp->data[4]);
190*4882a593Smuzhiyun 			rd_idx++;
191*4882a593Smuzhiyun 			/*
192*4882a593Smuzhiyun 			 * Continue dumpping trace buffer entries into
193*4882a593Smuzhiyun 			 * memory file till rd_idx reaches write index
194*4882a593Smuzhiyun 			 */
195*4882a593Smuzhiyun 			if (rd_idx == wr_idx)
196*4882a593Smuzhiyun 				break;
197*4882a593Smuzhiyun 		}
198*4882a593Smuzhiyun 	}
199*4882a593Smuzhiyun 	spin_unlock_irqrestore(&fnic_trace_lock, flags);
200*4882a593Smuzhiyun 	return len;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /*
204*4882a593Smuzhiyun  * fnic_get_stats_data - Copy fnic stats buffer to a memory file
205*4882a593Smuzhiyun  * @fnic_dbgfs_t: pointer to debugfs fnic stats buffer
206*4882a593Smuzhiyun  *
207*4882a593Smuzhiyun  * Description:
208*4882a593Smuzhiyun  * This routine gathers the fnic stats debugfs data from the fnic_stats struct
209*4882a593Smuzhiyun  * and dumps it to stats_debug_info.
210*4882a593Smuzhiyun  *
211*4882a593Smuzhiyun  * Return Value:
212*4882a593Smuzhiyun  * This routine returns the amount of bytes that were dumped into
213*4882a593Smuzhiyun  * stats_debug_info
214*4882a593Smuzhiyun  */
fnic_get_stats_data(struct stats_debug_info * debug,struct fnic_stats * stats)215*4882a593Smuzhiyun int fnic_get_stats_data(struct stats_debug_info *debug,
216*4882a593Smuzhiyun 			struct fnic_stats *stats)
217*4882a593Smuzhiyun {
218*4882a593Smuzhiyun 	int len = 0;
219*4882a593Smuzhiyun 	int buf_size = debug->buf_size;
220*4882a593Smuzhiyun 	struct timespec64 val1, val2;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	ktime_get_real_ts64(&val1);
223*4882a593Smuzhiyun 	len = scnprintf(debug->debug_buffer + len, buf_size - len,
224*4882a593Smuzhiyun 		"------------------------------------------\n"
225*4882a593Smuzhiyun 		 "\t\tTime\n"
226*4882a593Smuzhiyun 		"------------------------------------------\n");
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
229*4882a593Smuzhiyun 		"Current time :          [%lld:%ld]\n"
230*4882a593Smuzhiyun 		"Last stats reset time:  [%lld:%09ld]\n"
231*4882a593Smuzhiyun 		"Last stats read time:   [%lld:%ld]\n"
232*4882a593Smuzhiyun 		"delta since last reset: [%lld:%ld]\n"
233*4882a593Smuzhiyun 		"delta since last read:  [%lld:%ld]\n",
234*4882a593Smuzhiyun 	(s64)val1.tv_sec, val1.tv_nsec,
235*4882a593Smuzhiyun 	(s64)stats->stats_timestamps.last_reset_time.tv_sec,
236*4882a593Smuzhiyun 	stats->stats_timestamps.last_reset_time.tv_nsec,
237*4882a593Smuzhiyun 	(s64)stats->stats_timestamps.last_read_time.tv_sec,
238*4882a593Smuzhiyun 	stats->stats_timestamps.last_read_time.tv_nsec,
239*4882a593Smuzhiyun 	(s64)timespec64_sub(val1, stats->stats_timestamps.last_reset_time).tv_sec,
240*4882a593Smuzhiyun 	timespec64_sub(val1, stats->stats_timestamps.last_reset_time).tv_nsec,
241*4882a593Smuzhiyun 	(s64)timespec64_sub(val1, stats->stats_timestamps.last_read_time).tv_sec,
242*4882a593Smuzhiyun 	timespec64_sub(val1, stats->stats_timestamps.last_read_time).tv_nsec);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	stats->stats_timestamps.last_read_time = val1;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
247*4882a593Smuzhiyun 		  "------------------------------------------\n"
248*4882a593Smuzhiyun 		  "\t\tIO Statistics\n"
249*4882a593Smuzhiyun 		  "------------------------------------------\n");
250*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
251*4882a593Smuzhiyun 		  "Number of Active IOs: %lld\nMaximum Active IOs: %lld\n"
252*4882a593Smuzhiyun 		  "Number of IOs: %lld\nNumber of IO Completions: %lld\n"
253*4882a593Smuzhiyun 		  "Number of IO Failures: %lld\nNumber of IO NOT Found: %lld\n"
254*4882a593Smuzhiyun 		  "Number of Memory alloc Failures: %lld\n"
255*4882a593Smuzhiyun 		  "Number of IOREQ Null: %lld\n"
256*4882a593Smuzhiyun 		  "Number of SCSI cmd pointer Null: %lld\n"
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 		  "\nIO completion times: \n"
259*4882a593Smuzhiyun 		  "            < 10 ms : %lld\n"
260*4882a593Smuzhiyun 		  "     10 ms - 100 ms : %lld\n"
261*4882a593Smuzhiyun 		  "    100 ms - 500 ms : %lld\n"
262*4882a593Smuzhiyun 		  "    500 ms -   5 sec: %lld\n"
263*4882a593Smuzhiyun 		  "     5 sec -  10 sec: %lld\n"
264*4882a593Smuzhiyun 		  "    10 sec -  30 sec: %lld\n"
265*4882a593Smuzhiyun 		  "            > 30 sec: %lld\n",
266*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.active_ios),
267*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.max_active_ios),
268*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.num_ios),
269*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_completions),
270*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_failures),
271*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_not_found),
272*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.alloc_failures),
273*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.ioreq_null),
274*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.sc_null),
275*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_btw_0_to_10_msec),
276*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_btw_10_to_100_msec),
277*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_btw_100_to_500_msec),
278*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_btw_500_to_5000_msec),
279*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_btw_5000_to_10000_msec),
280*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_btw_10000_to_30000_msec),
281*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.io_greater_than_30000_msec));
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
284*4882a593Smuzhiyun 		  "\nCurrent Max IO time : %lld\n",
285*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->io_stats.current_max_io_time));
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
288*4882a593Smuzhiyun 		  "\n------------------------------------------\n"
289*4882a593Smuzhiyun 		  "\t\tAbort Statistics\n"
290*4882a593Smuzhiyun 		  "------------------------------------------\n");
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
293*4882a593Smuzhiyun 		  "Number of Aborts: %lld\n"
294*4882a593Smuzhiyun 		  "Number of Abort Failures: %lld\n"
295*4882a593Smuzhiyun 		  "Number of Abort Driver Timeouts: %lld\n"
296*4882a593Smuzhiyun 		  "Number of Abort FW Timeouts: %lld\n"
297*4882a593Smuzhiyun 		  "Number of Abort IO NOT Found: %lld\n"
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		  "Abort issued times: \n"
300*4882a593Smuzhiyun 		  "            < 6 sec : %lld\n"
301*4882a593Smuzhiyun 		  "     6 sec - 20 sec : %lld\n"
302*4882a593Smuzhiyun 		  "    20 sec - 30 sec : %lld\n"
303*4882a593Smuzhiyun 		  "    30 sec - 40 sec : %lld\n"
304*4882a593Smuzhiyun 		  "    40 sec - 50 sec : %lld\n"
305*4882a593Smuzhiyun 		  "    50 sec - 60 sec : %lld\n"
306*4882a593Smuzhiyun 		  "            > 60 sec: %lld\n",
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.aborts),
309*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_failures),
310*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_drv_timeouts),
311*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_fw_timeouts),
312*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_io_not_found),
313*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_0_to_6_sec),
314*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_6_to_20_sec),
315*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_20_to_30_sec),
316*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_30_to_40_sec),
317*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_40_to_50_sec),
318*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_50_to_60_sec),
319*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->abts_stats.abort_issued_greater_than_60_sec));
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
322*4882a593Smuzhiyun 		  "\n------------------------------------------\n"
323*4882a593Smuzhiyun 		  "\t\tTerminate Statistics\n"
324*4882a593Smuzhiyun 		  "------------------------------------------\n");
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
327*4882a593Smuzhiyun 		  "Number of Terminates: %lld\n"
328*4882a593Smuzhiyun 		  "Maximum Terminates: %lld\n"
329*4882a593Smuzhiyun 		  "Number of Terminate Driver Timeouts: %lld\n"
330*4882a593Smuzhiyun 		  "Number of Terminate FW Timeouts: %lld\n"
331*4882a593Smuzhiyun 		  "Number of Terminate IO NOT Found: %lld\n"
332*4882a593Smuzhiyun 		  "Number of Terminate Failures: %lld\n",
333*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->term_stats.terminates),
334*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->term_stats.max_terminates),
335*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->term_stats.terminate_drv_timeouts),
336*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->term_stats.terminate_fw_timeouts),
337*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->term_stats.terminate_io_not_found),
338*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->term_stats.terminate_failures));
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
341*4882a593Smuzhiyun 		  "\n------------------------------------------\n"
342*4882a593Smuzhiyun 		  "\t\tReset Statistics\n"
343*4882a593Smuzhiyun 		  "------------------------------------------\n");
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
346*4882a593Smuzhiyun 		  "Number of Device Resets: %lld\n"
347*4882a593Smuzhiyun 		  "Number of Device Reset Failures: %lld\n"
348*4882a593Smuzhiyun 		  "Number of Device Reset Aborts: %lld\n"
349*4882a593Smuzhiyun 		  "Number of Device Reset Timeouts: %lld\n"
350*4882a593Smuzhiyun 		  "Number of Device Reset Terminates: %lld\n"
351*4882a593Smuzhiyun 		  "Number of FW Resets: %lld\n"
352*4882a593Smuzhiyun 		  "Number of FW Reset Completions: %lld\n"
353*4882a593Smuzhiyun 		  "Number of FW Reset Failures: %lld\n"
354*4882a593Smuzhiyun 		  "Number of Fnic Reset: %lld\n"
355*4882a593Smuzhiyun 		  "Number of Fnic Reset Completions: %lld\n"
356*4882a593Smuzhiyun 		  "Number of Fnic Reset Failures: %lld\n",
357*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.device_resets),
358*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.device_reset_failures),
359*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.device_reset_aborts),
360*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.device_reset_timeouts),
361*4882a593Smuzhiyun 		  (u64)atomic64_read(
362*4882a593Smuzhiyun 			  &stats->reset_stats.device_reset_terminates),
363*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.fw_resets),
364*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.fw_reset_completions),
365*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.fw_reset_failures),
366*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.fnic_resets),
367*4882a593Smuzhiyun 		  (u64)atomic64_read(
368*4882a593Smuzhiyun 			  &stats->reset_stats.fnic_reset_completions),
369*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->reset_stats.fnic_reset_failures));
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
372*4882a593Smuzhiyun 		  "\n------------------------------------------\n"
373*4882a593Smuzhiyun 		  "\t\tFirmware Statistics\n"
374*4882a593Smuzhiyun 		  "------------------------------------------\n");
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
377*4882a593Smuzhiyun 		  "Number of Active FW Requests %lld\n"
378*4882a593Smuzhiyun 		  "Maximum FW Requests: %lld\n"
379*4882a593Smuzhiyun 		  "Number of FW out of resources: %lld\n"
380*4882a593Smuzhiyun 		  "Number of FW IO errors: %lld\n",
381*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->fw_stats.active_fw_reqs),
382*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->fw_stats.max_fw_reqs),
383*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->fw_stats.fw_out_of_resources),
384*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->fw_stats.io_fw_errs));
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
387*4882a593Smuzhiyun 		  "\n------------------------------------------\n"
388*4882a593Smuzhiyun 		  "\t\tVlan Discovery Statistics\n"
389*4882a593Smuzhiyun 		  "------------------------------------------\n");
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
392*4882a593Smuzhiyun 		  "Number of Vlan Discovery Requests Sent %lld\n"
393*4882a593Smuzhiyun 		  "Vlan Response Received with no FCF VLAN ID: %lld\n"
394*4882a593Smuzhiyun 		  "No solicitations recvd after vlan set, expiry count: %lld\n"
395*4882a593Smuzhiyun 		  "Flogi rejects count: %lld\n",
396*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->vlan_stats.vlan_disc_reqs),
397*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->vlan_stats.resp_withno_vlanID),
398*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->vlan_stats.sol_expiry_count),
399*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->vlan_stats.flogi_rejects));
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
402*4882a593Smuzhiyun 		  "\n------------------------------------------\n"
403*4882a593Smuzhiyun 		  "\t\tOther Important Statistics\n"
404*4882a593Smuzhiyun 		  "------------------------------------------\n");
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	jiffies_to_timespec64(stats->misc_stats.last_isr_time, &val1);
407*4882a593Smuzhiyun 	jiffies_to_timespec64(stats->misc_stats.last_ack_time, &val2);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
410*4882a593Smuzhiyun 		  "Last ISR time: %llu (%8llu.%09lu)\n"
411*4882a593Smuzhiyun 		  "Last ACK time: %llu (%8llu.%09lu)\n"
412*4882a593Smuzhiyun 		  "Max ISR jiffies: %llu\n"
413*4882a593Smuzhiyun 		  "Max ISR time (ms) (0 denotes < 1 ms): %llu\n"
414*4882a593Smuzhiyun 		  "Corr. work done: %llu\n"
415*4882a593Smuzhiyun 		  "Number of ISRs: %lld\n"
416*4882a593Smuzhiyun 		  "Maximum CQ Entries: %lld\n"
417*4882a593Smuzhiyun 		  "Number of ACK index out of range: %lld\n"
418*4882a593Smuzhiyun 		  "Number of data count mismatch: %lld\n"
419*4882a593Smuzhiyun 		  "Number of FCPIO Timeouts: %lld\n"
420*4882a593Smuzhiyun 		  "Number of FCPIO Aborted: %lld\n"
421*4882a593Smuzhiyun 		  "Number of SGL Invalid: %lld\n"
422*4882a593Smuzhiyun 		  "Number of Copy WQ Alloc Failures for ABTs: %lld\n"
423*4882a593Smuzhiyun 		  "Number of Copy WQ Alloc Failures for Device Reset: %lld\n"
424*4882a593Smuzhiyun 		  "Number of Copy WQ Alloc Failures for IOs: %lld\n"
425*4882a593Smuzhiyun 		  "Number of no icmnd itmf Completions: %lld\n"
426*4882a593Smuzhiyun 		  "Number of Check Conditions encountered: %lld\n"
427*4882a593Smuzhiyun 		  "Number of QUEUE Fulls: %lld\n"
428*4882a593Smuzhiyun 		  "Number of rport not ready: %lld\n"
429*4882a593Smuzhiyun 		  "Number of receive frame errors: %lld\n",
430*4882a593Smuzhiyun 		  (u64)stats->misc_stats.last_isr_time,
431*4882a593Smuzhiyun 		  (s64)val1.tv_sec, val1.tv_nsec,
432*4882a593Smuzhiyun 		  (u64)stats->misc_stats.last_ack_time,
433*4882a593Smuzhiyun 		  (s64)val2.tv_sec, val2.tv_nsec,
434*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.max_isr_jiffies),
435*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.max_isr_time_ms),
436*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.corr_work_done),
437*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.isr_count),
438*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.max_cq_entries),
439*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.ack_index_out_of_range),
440*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.data_count_mismatch),
441*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.fcpio_timeout),
442*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.fcpio_aborted),
443*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.sgl_invalid),
444*4882a593Smuzhiyun 		  (u64)atomic64_read(
445*4882a593Smuzhiyun 			  &stats->misc_stats.abts_cpwq_alloc_failures),
446*4882a593Smuzhiyun 		  (u64)atomic64_read(
447*4882a593Smuzhiyun 			  &stats->misc_stats.devrst_cpwq_alloc_failures),
448*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.io_cpwq_alloc_failures),
449*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.no_icmnd_itmf_cmpls),
450*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.check_condition),
451*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.queue_fulls),
452*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.rport_not_ready),
453*4882a593Smuzhiyun 		  (u64)atomic64_read(&stats->misc_stats.frame_errors));
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	len += scnprintf(debug->debug_buffer + len, buf_size - len,
456*4882a593Smuzhiyun 			"Firmware reported port speed: %llu\n",
457*4882a593Smuzhiyun 			(u64)atomic64_read(
458*4882a593Smuzhiyun 				&stats->misc_stats.current_port_speed));
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	return len;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun /*
465*4882a593Smuzhiyun  * fnic_trace_buf_init - Initialize fnic trace buffer logging facility
466*4882a593Smuzhiyun  *
467*4882a593Smuzhiyun  * Description:
468*4882a593Smuzhiyun  * Initialize trace buffer data structure by allocating required memory and
469*4882a593Smuzhiyun  * setting page_offset information for every trace entry by adding trace entry
470*4882a593Smuzhiyun  * length to previous page_offset value.
471*4882a593Smuzhiyun  */
fnic_trace_buf_init(void)472*4882a593Smuzhiyun int fnic_trace_buf_init(void)
473*4882a593Smuzhiyun {
474*4882a593Smuzhiyun 	unsigned long fnic_buf_head;
475*4882a593Smuzhiyun 	int i;
476*4882a593Smuzhiyun 	int err = 0;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	trace_max_pages = fnic_trace_max_pages;
479*4882a593Smuzhiyun 	fnic_max_trace_entries = (trace_max_pages * PAGE_SIZE)/
480*4882a593Smuzhiyun 					  FNIC_ENTRY_SIZE_BYTES;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	fnic_trace_buf_p = (unsigned long)vzalloc(trace_max_pages * PAGE_SIZE);
483*4882a593Smuzhiyun 	if (!fnic_trace_buf_p) {
484*4882a593Smuzhiyun 		printk(KERN_ERR PFX "Failed to allocate memory "
485*4882a593Smuzhiyun 				  "for fnic_trace_buf_p\n");
486*4882a593Smuzhiyun 		err = -ENOMEM;
487*4882a593Smuzhiyun 		goto err_fnic_trace_buf_init;
488*4882a593Smuzhiyun 	}
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	fnic_trace_entries.page_offset =
491*4882a593Smuzhiyun 		vmalloc(array_size(fnic_max_trace_entries,
492*4882a593Smuzhiyun 				   sizeof(unsigned long)));
493*4882a593Smuzhiyun 	if (!fnic_trace_entries.page_offset) {
494*4882a593Smuzhiyun 		printk(KERN_ERR PFX "Failed to allocate memory for"
495*4882a593Smuzhiyun 				  " page_offset\n");
496*4882a593Smuzhiyun 		if (fnic_trace_buf_p) {
497*4882a593Smuzhiyun 			vfree((void *)fnic_trace_buf_p);
498*4882a593Smuzhiyun 			fnic_trace_buf_p = 0;
499*4882a593Smuzhiyun 		}
500*4882a593Smuzhiyun 		err = -ENOMEM;
501*4882a593Smuzhiyun 		goto err_fnic_trace_buf_init;
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun 	memset((void *)fnic_trace_entries.page_offset, 0,
504*4882a593Smuzhiyun 		  (fnic_max_trace_entries * sizeof(unsigned long)));
505*4882a593Smuzhiyun 	fnic_trace_entries.wr_idx = fnic_trace_entries.rd_idx = 0;
506*4882a593Smuzhiyun 	fnic_buf_head = fnic_trace_buf_p;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	/*
509*4882a593Smuzhiyun 	 * Set page_offset field of fnic_trace_entries struct by
510*4882a593Smuzhiyun 	 * calculating memory location for every trace entry using
511*4882a593Smuzhiyun 	 * length of each trace entry
512*4882a593Smuzhiyun 	 */
513*4882a593Smuzhiyun 	for (i = 0; i < fnic_max_trace_entries; i++) {
514*4882a593Smuzhiyun 		fnic_trace_entries.page_offset[i] = fnic_buf_head;
515*4882a593Smuzhiyun 		fnic_buf_head += FNIC_ENTRY_SIZE_BYTES;
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 	fnic_trace_debugfs_init();
518*4882a593Smuzhiyun 	pr_info("fnic: Successfully Initialized Trace Buffer\n");
519*4882a593Smuzhiyun 	return err;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun err_fnic_trace_buf_init:
522*4882a593Smuzhiyun 	return err;
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun /*
526*4882a593Smuzhiyun  * fnic_trace_free - Free memory of fnic trace data structures.
527*4882a593Smuzhiyun  */
fnic_trace_free(void)528*4882a593Smuzhiyun void fnic_trace_free(void)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	fnic_tracing_enabled = 0;
531*4882a593Smuzhiyun 	fnic_trace_debugfs_terminate();
532*4882a593Smuzhiyun 	if (fnic_trace_entries.page_offset) {
533*4882a593Smuzhiyun 		vfree((void *)fnic_trace_entries.page_offset);
534*4882a593Smuzhiyun 		fnic_trace_entries.page_offset = NULL;
535*4882a593Smuzhiyun 	}
536*4882a593Smuzhiyun 	if (fnic_trace_buf_p) {
537*4882a593Smuzhiyun 		vfree((void *)fnic_trace_buf_p);
538*4882a593Smuzhiyun 		fnic_trace_buf_p = 0;
539*4882a593Smuzhiyun 	}
540*4882a593Smuzhiyun 	printk(KERN_INFO PFX "Successfully Freed Trace Buffer\n");
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun  * fnic_fc_ctlr_trace_buf_init -
545*4882a593Smuzhiyun  * Initialize trace buffer to log fnic control frames
546*4882a593Smuzhiyun  * Description:
547*4882a593Smuzhiyun  * Initialize trace buffer data structure by allocating
548*4882a593Smuzhiyun  * required memory for trace data as well as for Indexes.
549*4882a593Smuzhiyun  * Frame size is 256 bytes and
550*4882a593Smuzhiyun  * memory is allocated for 1024 entries of 256 bytes.
551*4882a593Smuzhiyun  * Page_offset(Index) is set to the address of trace entry
552*4882a593Smuzhiyun  * and page_offset is initialized by adding frame size
553*4882a593Smuzhiyun  * to the previous page_offset entry.
554*4882a593Smuzhiyun  */
555*4882a593Smuzhiyun 
fnic_fc_trace_init(void)556*4882a593Smuzhiyun int fnic_fc_trace_init(void)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun 	unsigned long fc_trace_buf_head;
559*4882a593Smuzhiyun 	int err = 0;
560*4882a593Smuzhiyun 	int i;
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	fc_trace_max_entries = (fnic_fc_trace_max_pages * PAGE_SIZE)/
563*4882a593Smuzhiyun 				FC_TRC_SIZE_BYTES;
564*4882a593Smuzhiyun 	fnic_fc_ctlr_trace_buf_p =
565*4882a593Smuzhiyun 		(unsigned long)vmalloc(array_size(PAGE_SIZE,
566*4882a593Smuzhiyun 						  fnic_fc_trace_max_pages));
567*4882a593Smuzhiyun 	if (!fnic_fc_ctlr_trace_buf_p) {
568*4882a593Smuzhiyun 		pr_err("fnic: Failed to allocate memory for "
569*4882a593Smuzhiyun 		       "FC Control Trace Buf\n");
570*4882a593Smuzhiyun 		err = -ENOMEM;
571*4882a593Smuzhiyun 		goto err_fnic_fc_ctlr_trace_buf_init;
572*4882a593Smuzhiyun 	}
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	memset((void *)fnic_fc_ctlr_trace_buf_p, 0,
575*4882a593Smuzhiyun 			fnic_fc_trace_max_pages * PAGE_SIZE);
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	/* Allocate memory for page offset */
578*4882a593Smuzhiyun 	fc_trace_entries.page_offset =
579*4882a593Smuzhiyun 		vmalloc(array_size(fc_trace_max_entries,
580*4882a593Smuzhiyun 				   sizeof(unsigned long)));
581*4882a593Smuzhiyun 	if (!fc_trace_entries.page_offset) {
582*4882a593Smuzhiyun 		pr_err("fnic:Failed to allocate memory for page_offset\n");
583*4882a593Smuzhiyun 		if (fnic_fc_ctlr_trace_buf_p) {
584*4882a593Smuzhiyun 			pr_err("fnic: Freeing FC Control Trace Buf\n");
585*4882a593Smuzhiyun 			vfree((void *)fnic_fc_ctlr_trace_buf_p);
586*4882a593Smuzhiyun 			fnic_fc_ctlr_trace_buf_p = 0;
587*4882a593Smuzhiyun 		}
588*4882a593Smuzhiyun 		err = -ENOMEM;
589*4882a593Smuzhiyun 		goto err_fnic_fc_ctlr_trace_buf_init;
590*4882a593Smuzhiyun 	}
591*4882a593Smuzhiyun 	memset((void *)fc_trace_entries.page_offset, 0,
592*4882a593Smuzhiyun 	       (fc_trace_max_entries * sizeof(unsigned long)));
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	fc_trace_entries.rd_idx = fc_trace_entries.wr_idx = 0;
595*4882a593Smuzhiyun 	fc_trace_buf_head = fnic_fc_ctlr_trace_buf_p;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	/*
598*4882a593Smuzhiyun 	* Set up fc_trace_entries.page_offset field with memory location
599*4882a593Smuzhiyun 	* for every trace entry
600*4882a593Smuzhiyun 	*/
601*4882a593Smuzhiyun 	for (i = 0; i < fc_trace_max_entries; i++) {
602*4882a593Smuzhiyun 		fc_trace_entries.page_offset[i] = fc_trace_buf_head;
603*4882a593Smuzhiyun 		fc_trace_buf_head += FC_TRC_SIZE_BYTES;
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 	fnic_fc_trace_debugfs_init();
606*4882a593Smuzhiyun 	pr_info("fnic: Successfully Initialized FC_CTLR Trace Buffer\n");
607*4882a593Smuzhiyun 	return err;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun err_fnic_fc_ctlr_trace_buf_init:
610*4882a593Smuzhiyun 	return err;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun /*
614*4882a593Smuzhiyun  * Fnic_fc_ctlr_trace_free - Free memory of fnic_fc_ctlr trace data structures.
615*4882a593Smuzhiyun  */
fnic_fc_trace_free(void)616*4882a593Smuzhiyun void fnic_fc_trace_free(void)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	fnic_fc_tracing_enabled = 0;
619*4882a593Smuzhiyun 	fnic_fc_trace_debugfs_terminate();
620*4882a593Smuzhiyun 	if (fc_trace_entries.page_offset) {
621*4882a593Smuzhiyun 		vfree((void *)fc_trace_entries.page_offset);
622*4882a593Smuzhiyun 		fc_trace_entries.page_offset = NULL;
623*4882a593Smuzhiyun 	}
624*4882a593Smuzhiyun 	if (fnic_fc_ctlr_trace_buf_p) {
625*4882a593Smuzhiyun 		vfree((void *)fnic_fc_ctlr_trace_buf_p);
626*4882a593Smuzhiyun 		fnic_fc_ctlr_trace_buf_p = 0;
627*4882a593Smuzhiyun 	}
628*4882a593Smuzhiyun 	pr_info("fnic:Successfully FC_CTLR Freed Trace Buffer\n");
629*4882a593Smuzhiyun }
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun /*
632*4882a593Smuzhiyun  * fnic_fc_ctlr_set_trace_data:
633*4882a593Smuzhiyun  *       Maintain rd & wr idx accordingly and set data
634*4882a593Smuzhiyun  * Passed parameters:
635*4882a593Smuzhiyun  *       host_no: host number accociated with fnic
636*4882a593Smuzhiyun  *       frame_type: send_frame, rece_frame or link event
637*4882a593Smuzhiyun  *       fc_frame: pointer to fc_frame
638*4882a593Smuzhiyun  *       frame_len: Length of the fc_frame
639*4882a593Smuzhiyun  * Description:
640*4882a593Smuzhiyun  *   This routine will get next available wr_idx and
641*4882a593Smuzhiyun  *   copy all passed trace data to the buffer pointed by wr_idx
642*4882a593Smuzhiyun  *   and increment wr_idx. It will also make sure that we dont
643*4882a593Smuzhiyun  *   overwrite the entry which we are reading and also
644*4882a593Smuzhiyun  *   wrap around if we reach the maximum entries.
645*4882a593Smuzhiyun  * Returned Value:
646*4882a593Smuzhiyun  *   It will return 0 for success or -1 for failure
647*4882a593Smuzhiyun  */
fnic_fc_trace_set_data(u32 host_no,u8 frame_type,char * frame,u32 fc_trc_frame_len)648*4882a593Smuzhiyun int fnic_fc_trace_set_data(u32 host_no, u8 frame_type,
649*4882a593Smuzhiyun 				char *frame, u32 fc_trc_frame_len)
650*4882a593Smuzhiyun {
651*4882a593Smuzhiyun 	unsigned long flags;
652*4882a593Smuzhiyun 	struct fc_trace_hdr *fc_buf;
653*4882a593Smuzhiyun 	unsigned long eth_fcoe_hdr_len;
654*4882a593Smuzhiyun 	char *fc_trace;
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (fnic_fc_tracing_enabled == 0)
657*4882a593Smuzhiyun 		return 0;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	spin_lock_irqsave(&fnic_fc_trace_lock, flags);
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	if (fnic_fc_trace_cleared == 1) {
662*4882a593Smuzhiyun 		fc_trace_entries.rd_idx = fc_trace_entries.wr_idx = 0;
663*4882a593Smuzhiyun 		pr_info("fnic: Resetting the read idx\n");
664*4882a593Smuzhiyun 		memset((void *)fnic_fc_ctlr_trace_buf_p, 0,
665*4882a593Smuzhiyun 				fnic_fc_trace_max_pages * PAGE_SIZE);
666*4882a593Smuzhiyun 		fnic_fc_trace_cleared = 0;
667*4882a593Smuzhiyun 	}
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	fc_buf = (struct fc_trace_hdr *)
670*4882a593Smuzhiyun 		fc_trace_entries.page_offset[fc_trace_entries.wr_idx];
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	fc_trace_entries.wr_idx++;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (fc_trace_entries.wr_idx >= fc_trace_max_entries)
675*4882a593Smuzhiyun 		fc_trace_entries.wr_idx = 0;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (fc_trace_entries.wr_idx == fc_trace_entries.rd_idx) {
678*4882a593Smuzhiyun 		fc_trace_entries.rd_idx++;
679*4882a593Smuzhiyun 		if (fc_trace_entries.rd_idx >= fc_trace_max_entries)
680*4882a593Smuzhiyun 			fc_trace_entries.rd_idx = 0;
681*4882a593Smuzhiyun 	}
682*4882a593Smuzhiyun 
683*4882a593Smuzhiyun 	ktime_get_real_ts64(&fc_buf->time_stamp);
684*4882a593Smuzhiyun 	fc_buf->host_no = host_no;
685*4882a593Smuzhiyun 	fc_buf->frame_type = frame_type;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	fc_trace = (char *)FC_TRACE_ADDRESS(fc_buf);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	/* During the receive path, we do not have eth hdr as well as fcoe hdr
690*4882a593Smuzhiyun 	 * at trace entry point so we will stuff 0xff just to make it generic.
691*4882a593Smuzhiyun 	 */
692*4882a593Smuzhiyun 	if (frame_type == FNIC_FC_RECV) {
693*4882a593Smuzhiyun 		eth_fcoe_hdr_len = sizeof(struct ethhdr) +
694*4882a593Smuzhiyun 					sizeof(struct fcoe_hdr);
695*4882a593Smuzhiyun 		memset((char *)fc_trace, 0xff, eth_fcoe_hdr_len);
696*4882a593Smuzhiyun 		/* Copy the rest of data frame */
697*4882a593Smuzhiyun 		memcpy((char *)(fc_trace + eth_fcoe_hdr_len), (void *)frame,
698*4882a593Smuzhiyun 		min_t(u8, fc_trc_frame_len,
699*4882a593Smuzhiyun 			(u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE
700*4882a593Smuzhiyun 						- eth_fcoe_hdr_len)));
701*4882a593Smuzhiyun 	} else {
702*4882a593Smuzhiyun 		memcpy((char *)fc_trace, (void *)frame,
703*4882a593Smuzhiyun 		min_t(u8, fc_trc_frame_len,
704*4882a593Smuzhiyun 			(u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE)));
705*4882a593Smuzhiyun 	}
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	/* Store the actual received length */
708*4882a593Smuzhiyun 	fc_buf->frame_len = fc_trc_frame_len;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
711*4882a593Smuzhiyun 	return 0;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun /*
715*4882a593Smuzhiyun  * fnic_fc_ctlr_get_trace_data: Copy trace buffer to a memory file
716*4882a593Smuzhiyun  * Passed parameter:
717*4882a593Smuzhiyun  *       @fnic_dbgfs_t: pointer to debugfs trace buffer
718*4882a593Smuzhiyun  *       rdata_flag: 1 => Unformated file
719*4882a593Smuzhiyun  *                   0 => formated file
720*4882a593Smuzhiyun  * Description:
721*4882a593Smuzhiyun  *       This routine will copy the trace data to memory file with
722*4882a593Smuzhiyun  *       proper formatting and also copy to another memory
723*4882a593Smuzhiyun  *       file without formatting for further procesing.
724*4882a593Smuzhiyun  * Retrun Value:
725*4882a593Smuzhiyun  *       Number of bytes that were dumped into fnic_dbgfs_t
726*4882a593Smuzhiyun  */
727*4882a593Smuzhiyun 
fnic_fc_trace_get_data(fnic_dbgfs_t * fnic_dbgfs_prt,u8 rdata_flag)728*4882a593Smuzhiyun int fnic_fc_trace_get_data(fnic_dbgfs_t *fnic_dbgfs_prt, u8 rdata_flag)
729*4882a593Smuzhiyun {
730*4882a593Smuzhiyun 	int rd_idx, wr_idx;
731*4882a593Smuzhiyun 	unsigned long flags;
732*4882a593Smuzhiyun 	int len = 0, j;
733*4882a593Smuzhiyun 	struct fc_trace_hdr *tdata;
734*4882a593Smuzhiyun 	char *fc_trace;
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	spin_lock_irqsave(&fnic_fc_trace_lock, flags);
737*4882a593Smuzhiyun 	if (fc_trace_entries.wr_idx == fc_trace_entries.rd_idx) {
738*4882a593Smuzhiyun 		spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
739*4882a593Smuzhiyun 		pr_info("fnic: Buffer is empty\n");
740*4882a593Smuzhiyun 		return 0;
741*4882a593Smuzhiyun 	}
742*4882a593Smuzhiyun 	rd_idx = fc_trace_entries.rd_idx;
743*4882a593Smuzhiyun 	wr_idx = fc_trace_entries.wr_idx;
744*4882a593Smuzhiyun 	if (rdata_flag == 0) {
745*4882a593Smuzhiyun 		len += scnprintf(fnic_dbgfs_prt->buffer + len,
746*4882a593Smuzhiyun 			(fnic_fc_trace_max_pages * PAGE_SIZE * 3) - len,
747*4882a593Smuzhiyun 			"Time Stamp (UTC)\t\t"
748*4882a593Smuzhiyun 			"Host No:   F Type:  len:     FCoE_FRAME:\n");
749*4882a593Smuzhiyun 	}
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	while (rd_idx != wr_idx) {
752*4882a593Smuzhiyun 		tdata = (struct fc_trace_hdr *)
753*4882a593Smuzhiyun 			fc_trace_entries.page_offset[rd_idx];
754*4882a593Smuzhiyun 		if (!tdata) {
755*4882a593Smuzhiyun 			pr_info("fnic: Rd data is NULL\n");
756*4882a593Smuzhiyun 			spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
757*4882a593Smuzhiyun 			return 0;
758*4882a593Smuzhiyun 		}
759*4882a593Smuzhiyun 		if (rdata_flag == 0) {
760*4882a593Smuzhiyun 			copy_and_format_trace_data(tdata,
761*4882a593Smuzhiyun 				fnic_dbgfs_prt, &len, rdata_flag);
762*4882a593Smuzhiyun 		} else {
763*4882a593Smuzhiyun 			fc_trace = (char *)tdata;
764*4882a593Smuzhiyun 			for (j = 0; j < FC_TRC_SIZE_BYTES; j++) {
765*4882a593Smuzhiyun 				len += scnprintf(fnic_dbgfs_prt->buffer + len,
766*4882a593Smuzhiyun 				(fnic_fc_trace_max_pages * PAGE_SIZE * 3)
767*4882a593Smuzhiyun 				- len, "%02x", fc_trace[j] & 0xff);
768*4882a593Smuzhiyun 			} /* for loop */
769*4882a593Smuzhiyun 			len += scnprintf(fnic_dbgfs_prt->buffer + len,
770*4882a593Smuzhiyun 				(fnic_fc_trace_max_pages * PAGE_SIZE * 3) - len,
771*4882a593Smuzhiyun 				"\n");
772*4882a593Smuzhiyun 		}
773*4882a593Smuzhiyun 		rd_idx++;
774*4882a593Smuzhiyun 		if (rd_idx > (fc_trace_max_entries - 1))
775*4882a593Smuzhiyun 			rd_idx = 0;
776*4882a593Smuzhiyun 	}
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
779*4882a593Smuzhiyun 	return len;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun /*
783*4882a593Smuzhiyun  * copy_and_format_trace_data: Copy formatted data to char * buffer
784*4882a593Smuzhiyun  * Passed Parameter:
785*4882a593Smuzhiyun  *      @fc_trace_hdr_t: pointer to trace data
786*4882a593Smuzhiyun  *      @fnic_dbgfs_t: pointer to debugfs trace buffer
787*4882a593Smuzhiyun  *      @orig_len: pointer to len
788*4882a593Smuzhiyun  *      rdata_flag: 0 => Formated file, 1 => Unformated file
789*4882a593Smuzhiyun  * Description:
790*4882a593Smuzhiyun  *      This routine will format and copy the passed trace data
791*4882a593Smuzhiyun  *      for formated file or unformated file accordingly.
792*4882a593Smuzhiyun  */
793*4882a593Smuzhiyun 
copy_and_format_trace_data(struct fc_trace_hdr * tdata,fnic_dbgfs_t * fnic_dbgfs_prt,int * orig_len,u8 rdata_flag)794*4882a593Smuzhiyun void copy_and_format_trace_data(struct fc_trace_hdr *tdata,
795*4882a593Smuzhiyun 				fnic_dbgfs_t *fnic_dbgfs_prt, int *orig_len,
796*4882a593Smuzhiyun 				u8 rdata_flag)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun 	struct tm tm;
799*4882a593Smuzhiyun 	int j, i = 1, len;
800*4882a593Smuzhiyun 	char *fc_trace, *fmt;
801*4882a593Smuzhiyun 	int ethhdr_len = sizeof(struct ethhdr) - 1;
802*4882a593Smuzhiyun 	int fcoehdr_len = sizeof(struct fcoe_hdr);
803*4882a593Smuzhiyun 	int fchdr_len = sizeof(struct fc_frame_header);
804*4882a593Smuzhiyun 	int max_size = fnic_fc_trace_max_pages * PAGE_SIZE * 3;
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	tdata->frame_type = tdata->frame_type & 0x7F;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	len = *orig_len;
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 	time64_to_tm(tdata->time_stamp.tv_sec, 0, &tm);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 	fmt = "%02d:%02d:%04ld %02d:%02d:%02d.%09lu ns%8x       %c%8x\t";
813*4882a593Smuzhiyun 	len += scnprintf(fnic_dbgfs_prt->buffer + len,
814*4882a593Smuzhiyun 		max_size - len,
815*4882a593Smuzhiyun 		fmt,
816*4882a593Smuzhiyun 		tm.tm_mon + 1, tm.tm_mday, tm.tm_year + 1900,
817*4882a593Smuzhiyun 		tm.tm_hour, tm.tm_min, tm.tm_sec,
818*4882a593Smuzhiyun 		tdata->time_stamp.tv_nsec, tdata->host_no,
819*4882a593Smuzhiyun 		tdata->frame_type, tdata->frame_len);
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	fc_trace = (char *)FC_TRACE_ADDRESS(tdata);
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	for (j = 0; j < min_t(u8, tdata->frame_len,
824*4882a593Smuzhiyun 		(u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE)); j++) {
825*4882a593Smuzhiyun 		if (tdata->frame_type == FNIC_FC_LE) {
826*4882a593Smuzhiyun 			len += scnprintf(fnic_dbgfs_prt->buffer + len,
827*4882a593Smuzhiyun 				max_size - len, "%c", fc_trace[j]);
828*4882a593Smuzhiyun 		} else {
829*4882a593Smuzhiyun 			len += scnprintf(fnic_dbgfs_prt->buffer + len,
830*4882a593Smuzhiyun 				max_size - len, "%02x", fc_trace[j] & 0xff);
831*4882a593Smuzhiyun 			len += scnprintf(fnic_dbgfs_prt->buffer + len,
832*4882a593Smuzhiyun 				max_size - len, " ");
833*4882a593Smuzhiyun 			if (j == ethhdr_len ||
834*4882a593Smuzhiyun 				j == ethhdr_len + fcoehdr_len ||
835*4882a593Smuzhiyun 				j == ethhdr_len + fcoehdr_len + fchdr_len ||
836*4882a593Smuzhiyun 				(i > 3 && j%fchdr_len == 0)) {
837*4882a593Smuzhiyun 				len += scnprintf(fnic_dbgfs_prt->buffer
838*4882a593Smuzhiyun 					+ len, max_size - len,
839*4882a593Smuzhiyun 					"\n\t\t\t\t\t\t\t\t");
840*4882a593Smuzhiyun 				i++;
841*4882a593Smuzhiyun 			}
842*4882a593Smuzhiyun 		} /* end of else*/
843*4882a593Smuzhiyun 	} /* End of for loop*/
844*4882a593Smuzhiyun 	len += scnprintf(fnic_dbgfs_prt->buffer + len,
845*4882a593Smuzhiyun 		max_size - len, "\n");
846*4882a593Smuzhiyun 	*orig_len = len;
847*4882a593Smuzhiyun }
848