fnic_trace.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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 <linux/time.h>
  23. #include <linux/vmalloc.h>
  24. #include "fnic_io.h"
  25. #include "fnic.h"
  26. unsigned int trace_max_pages;
  27. static int fnic_max_trace_entries;
  28. static unsigned long fnic_trace_buf_p;
  29. static DEFINE_SPINLOCK(fnic_trace_lock);
  30. static fnic_trace_dbg_t fnic_trace_entries;
  31. int fnic_tracing_enabled = 1;
  32. /* static char *fnic_fc_ctlr_trace_buf_p; */
  33. static int fc_trace_max_entries;
  34. static unsigned long fnic_fc_ctlr_trace_buf_p;
  35. static fnic_trace_dbg_t fc_trace_entries;
  36. int fnic_fc_tracing_enabled = 1;
  37. int fnic_fc_trace_cleared = 1;
  38. static DEFINE_SPINLOCK(fnic_fc_trace_lock);
  39. /*
  40. * fnic_trace_get_buf - Give buffer pointer to user to fill up trace information
  41. *
  42. * Description:
  43. * This routine gets next available trace buffer entry location @wr_idx
  44. * from allocated trace buffer pages and give that memory location
  45. * to user to store the trace information.
  46. *
  47. * Return Value:
  48. * This routine returns pointer to next available trace entry
  49. * @fnic_buf_head for user to fill trace information.
  50. */
  51. fnic_trace_data_t *fnic_trace_get_buf(void)
  52. {
  53. unsigned long fnic_buf_head;
  54. unsigned long flags;
  55. spin_lock_irqsave(&fnic_trace_lock, flags);
  56. /*
  57. * Get next available memory location for writing trace information
  58. * at @wr_idx and increment @wr_idx
  59. */
  60. fnic_buf_head =
  61. fnic_trace_entries.page_offset[fnic_trace_entries.wr_idx];
  62. fnic_trace_entries.wr_idx++;
  63. /*
  64. * Verify if trace buffer is full then change wd_idx to
  65. * start from zero
  66. */
  67. if (fnic_trace_entries.wr_idx >= fnic_max_trace_entries)
  68. fnic_trace_entries.wr_idx = 0;
  69. /*
  70. * Verify if write index @wr_idx and read index @rd_idx are same then
  71. * increment @rd_idx to move to next entry in trace buffer
  72. */
  73. if (fnic_trace_entries.wr_idx == fnic_trace_entries.rd_idx) {
  74. fnic_trace_entries.rd_idx++;
  75. if (fnic_trace_entries.rd_idx >= fnic_max_trace_entries)
  76. fnic_trace_entries.rd_idx = 0;
  77. }
  78. spin_unlock_irqrestore(&fnic_trace_lock, flags);
  79. return (fnic_trace_data_t *)fnic_buf_head;
  80. }
  81. /*
  82. * fnic_get_trace_data - Copy trace buffer to a memory file
  83. * @fnic_dbgfs_t: pointer to debugfs trace buffer
  84. *
  85. * Description:
  86. * This routine gathers the fnic trace debugfs data from the fnic_trace_data_t
  87. * buffer and dumps it to fnic_dbgfs_t. It will start at the rd_idx entry in
  88. * the log and process the log until the end of the buffer. Then it will gather
  89. * from the beginning of the log and process until the current entry @wr_idx.
  90. *
  91. * Return Value:
  92. * This routine returns the amount of bytes that were dumped into fnic_dbgfs_t
  93. */
  94. int fnic_get_trace_data(fnic_dbgfs_t *fnic_dbgfs_prt)
  95. {
  96. int rd_idx;
  97. int wr_idx;
  98. int len = 0;
  99. unsigned long flags;
  100. char str[KSYM_SYMBOL_LEN];
  101. struct timespec val;
  102. fnic_trace_data_t *tbp;
  103. spin_lock_irqsave(&fnic_trace_lock, flags);
  104. rd_idx = fnic_trace_entries.rd_idx;
  105. wr_idx = fnic_trace_entries.wr_idx;
  106. if (wr_idx < rd_idx) {
  107. while (1) {
  108. /* Start from read index @rd_idx */
  109. tbp = (fnic_trace_data_t *)
  110. fnic_trace_entries.page_offset[rd_idx];
  111. if (!tbp) {
  112. spin_unlock_irqrestore(&fnic_trace_lock, flags);
  113. return 0;
  114. }
  115. /* Convert function pointer to function name */
  116. if (sizeof(unsigned long) < 8) {
  117. sprint_symbol(str, tbp->fnaddr.low);
  118. jiffies_to_timespec(tbp->timestamp.low, &val);
  119. } else {
  120. sprint_symbol(str, tbp->fnaddr.val);
  121. jiffies_to_timespec(tbp->timestamp.val, &val);
  122. }
  123. /*
  124. * Dump trace buffer entry to memory file
  125. * and increment read index @rd_idx
  126. */
  127. len += snprintf(fnic_dbgfs_prt->buffer + len,
  128. (trace_max_pages * PAGE_SIZE * 3) - len,
  129. "%16lu.%16lu %-50s %8x %8x %16llx %16llx "
  130. "%16llx %16llx %16llx\n", val.tv_sec,
  131. val.tv_nsec, str, tbp->host_no, tbp->tag,
  132. tbp->data[0], tbp->data[1], tbp->data[2],
  133. tbp->data[3], tbp->data[4]);
  134. rd_idx++;
  135. /*
  136. * If rd_idx is reached to maximum trace entries
  137. * then move rd_idx to zero
  138. */
  139. if (rd_idx > (fnic_max_trace_entries-1))
  140. rd_idx = 0;
  141. /*
  142. * Continure dumpping trace buffer entries into
  143. * memory file till rd_idx reaches write index
  144. */
  145. if (rd_idx == wr_idx)
  146. break;
  147. }
  148. } else if (wr_idx > rd_idx) {
  149. while (1) {
  150. /* Start from read index @rd_idx */
  151. tbp = (fnic_trace_data_t *)
  152. fnic_trace_entries.page_offset[rd_idx];
  153. if (!tbp) {
  154. spin_unlock_irqrestore(&fnic_trace_lock, flags);
  155. return 0;
  156. }
  157. /* Convert function pointer to function name */
  158. if (sizeof(unsigned long) < 8) {
  159. sprint_symbol(str, tbp->fnaddr.low);
  160. jiffies_to_timespec(tbp->timestamp.low, &val);
  161. } else {
  162. sprint_symbol(str, tbp->fnaddr.val);
  163. jiffies_to_timespec(tbp->timestamp.val, &val);
  164. }
  165. /*
  166. * Dump trace buffer entry to memory file
  167. * and increment read index @rd_idx
  168. */
  169. len += snprintf(fnic_dbgfs_prt->buffer + len,
  170. (trace_max_pages * PAGE_SIZE * 3) - len,
  171. "%16lu.%16lu %-50s %8x %8x %16llx %16llx "
  172. "%16llx %16llx %16llx\n", val.tv_sec,
  173. val.tv_nsec, str, tbp->host_no, tbp->tag,
  174. tbp->data[0], tbp->data[1], tbp->data[2],
  175. tbp->data[3], tbp->data[4]);
  176. rd_idx++;
  177. /*
  178. * Continue dumpping trace buffer entries into
  179. * memory file till rd_idx reaches write index
  180. */
  181. if (rd_idx == wr_idx)
  182. break;
  183. }
  184. }
  185. spin_unlock_irqrestore(&fnic_trace_lock, flags);
  186. return len;
  187. }
  188. /*
  189. * fnic_get_stats_data - Copy fnic stats buffer to a memory file
  190. * @fnic_dbgfs_t: pointer to debugfs fnic stats buffer
  191. *
  192. * Description:
  193. * This routine gathers the fnic stats debugfs data from the fnic_stats struct
  194. * and dumps it to stats_debug_info.
  195. *
  196. * Return Value:
  197. * This routine returns the amount of bytes that were dumped into
  198. * stats_debug_info
  199. */
  200. int fnic_get_stats_data(struct stats_debug_info *debug,
  201. struct fnic_stats *stats)
  202. {
  203. int len = 0;
  204. int buf_size = debug->buf_size;
  205. struct timespec val1, val2;
  206. len = snprintf(debug->debug_buffer + len, buf_size - len,
  207. "------------------------------------------\n"
  208. "\t\tIO Statistics\n"
  209. "------------------------------------------\n");
  210. len += snprintf(debug->debug_buffer + len, buf_size - len,
  211. "Number of Active IOs: %lld\nMaximum Active IOs: %lld\n"
  212. "Number of IOs: %lld\nNumber of IO Completions: %lld\n"
  213. "Number of IO Failures: %lld\nNumber of IO NOT Found: %lld\n"
  214. "Number of Memory alloc Failures: %lld\n"
  215. "Number of IOREQ Null: %lld\n"
  216. "Number of SCSI cmd pointer Null: %lld\n",
  217. (u64)atomic64_read(&stats->io_stats.active_ios),
  218. (u64)atomic64_read(&stats->io_stats.max_active_ios),
  219. (u64)atomic64_read(&stats->io_stats.num_ios),
  220. (u64)atomic64_read(&stats->io_stats.io_completions),
  221. (u64)atomic64_read(&stats->io_stats.io_failures),
  222. (u64)atomic64_read(&stats->io_stats.io_not_found),
  223. (u64)atomic64_read(&stats->io_stats.alloc_failures),
  224. (u64)atomic64_read(&stats->io_stats.ioreq_null),
  225. (u64)atomic64_read(&stats->io_stats.sc_null));
  226. len += snprintf(debug->debug_buffer + len, buf_size - len,
  227. "\n------------------------------------------\n"
  228. "\t\tAbort Statistics\n"
  229. "------------------------------------------\n");
  230. len += snprintf(debug->debug_buffer + len, buf_size - len,
  231. "Number of Aborts: %lld\n"
  232. "Number of Abort Failures: %lld\n"
  233. "Number of Abort Driver Timeouts: %lld\n"
  234. "Number of Abort FW Timeouts: %lld\n"
  235. "Number of Abort IO NOT Found: %lld\n",
  236. (u64)atomic64_read(&stats->abts_stats.aborts),
  237. (u64)atomic64_read(&stats->abts_stats.abort_failures),
  238. (u64)atomic64_read(&stats->abts_stats.abort_drv_timeouts),
  239. (u64)atomic64_read(&stats->abts_stats.abort_fw_timeouts),
  240. (u64)atomic64_read(&stats->abts_stats.abort_io_not_found));
  241. len += snprintf(debug->debug_buffer + len, buf_size - len,
  242. "\n------------------------------------------\n"
  243. "\t\tTerminate Statistics\n"
  244. "------------------------------------------\n");
  245. len += snprintf(debug->debug_buffer + len, buf_size - len,
  246. "Number of Terminates: %lld\n"
  247. "Maximum Terminates: %lld\n"
  248. "Number of Terminate Driver Timeouts: %lld\n"
  249. "Number of Terminate FW Timeouts: %lld\n"
  250. "Number of Terminate IO NOT Found: %lld\n"
  251. "Number of Terminate Failures: %lld\n",
  252. (u64)atomic64_read(&stats->term_stats.terminates),
  253. (u64)atomic64_read(&stats->term_stats.max_terminates),
  254. (u64)atomic64_read(&stats->term_stats.terminate_drv_timeouts),
  255. (u64)atomic64_read(&stats->term_stats.terminate_fw_timeouts),
  256. (u64)atomic64_read(&stats->term_stats.terminate_io_not_found),
  257. (u64)atomic64_read(&stats->term_stats.terminate_failures));
  258. len += snprintf(debug->debug_buffer + len, buf_size - len,
  259. "\n------------------------------------------\n"
  260. "\t\tReset Statistics\n"
  261. "------------------------------------------\n");
  262. len += snprintf(debug->debug_buffer + len, buf_size - len,
  263. "Number of Device Resets: %lld\n"
  264. "Number of Device Reset Failures: %lld\n"
  265. "Number of Device Reset Aborts: %lld\n"
  266. "Number of Device Reset Timeouts: %lld\n"
  267. "Number of Device Reset Terminates: %lld\n"
  268. "Number of FW Resets: %lld\n"
  269. "Number of FW Reset Completions: %lld\n"
  270. "Number of FW Reset Failures: %lld\n"
  271. "Number of Fnic Reset: %lld\n"
  272. "Number of Fnic Reset Completions: %lld\n"
  273. "Number of Fnic Reset Failures: %lld\n",
  274. (u64)atomic64_read(&stats->reset_stats.device_resets),
  275. (u64)atomic64_read(&stats->reset_stats.device_reset_failures),
  276. (u64)atomic64_read(&stats->reset_stats.device_reset_aborts),
  277. (u64)atomic64_read(&stats->reset_stats.device_reset_timeouts),
  278. (u64)atomic64_read(
  279. &stats->reset_stats.device_reset_terminates),
  280. (u64)atomic64_read(&stats->reset_stats.fw_resets),
  281. (u64)atomic64_read(&stats->reset_stats.fw_reset_completions),
  282. (u64)atomic64_read(&stats->reset_stats.fw_reset_failures),
  283. (u64)atomic64_read(&stats->reset_stats.fnic_resets),
  284. (u64)atomic64_read(
  285. &stats->reset_stats.fnic_reset_completions),
  286. (u64)atomic64_read(&stats->reset_stats.fnic_reset_failures));
  287. len += snprintf(debug->debug_buffer + len, buf_size - len,
  288. "\n------------------------------------------\n"
  289. "\t\tFirmware Statistics\n"
  290. "------------------------------------------\n");
  291. len += snprintf(debug->debug_buffer + len, buf_size - len,
  292. "Number of Active FW Requests %lld\n"
  293. "Maximum FW Requests: %lld\n"
  294. "Number of FW out of resources: %lld\n"
  295. "Number of FW IO errors: %lld\n",
  296. (u64)atomic64_read(&stats->fw_stats.active_fw_reqs),
  297. (u64)atomic64_read(&stats->fw_stats.max_fw_reqs),
  298. (u64)atomic64_read(&stats->fw_stats.fw_out_of_resources),
  299. (u64)atomic64_read(&stats->fw_stats.io_fw_errs));
  300. len += snprintf(debug->debug_buffer + len, buf_size - len,
  301. "\n------------------------------------------\n"
  302. "\t\tVlan Discovery Statistics\n"
  303. "------------------------------------------\n");
  304. len += snprintf(debug->debug_buffer + len, buf_size - len,
  305. "Number of Vlan Discovery Requests Sent %lld\n"
  306. "Vlan Response Received with no FCF VLAN ID: %lld\n"
  307. "No solicitations recvd after vlan set, expiry count: %lld\n"
  308. "Flogi rejects count: %lld\n",
  309. (u64)atomic64_read(&stats->vlan_stats.vlan_disc_reqs),
  310. (u64)atomic64_read(&stats->vlan_stats.resp_withno_vlanID),
  311. (u64)atomic64_read(&stats->vlan_stats.sol_expiry_count),
  312. (u64)atomic64_read(&stats->vlan_stats.flogi_rejects));
  313. len += snprintf(debug->debug_buffer + len, buf_size - len,
  314. "\n------------------------------------------\n"
  315. "\t\tOther Important Statistics\n"
  316. "------------------------------------------\n");
  317. jiffies_to_timespec(stats->misc_stats.last_isr_time, &val1);
  318. jiffies_to_timespec(stats->misc_stats.last_ack_time, &val2);
  319. len += snprintf(debug->debug_buffer + len, buf_size - len,
  320. "Last ISR time: %llu (%8lu.%8lu)\n"
  321. "Last ACK time: %llu (%8lu.%8lu)\n"
  322. "Number of ISRs: %lld\n"
  323. "Maximum CQ Entries: %lld\n"
  324. "Number of ACK index out of range: %lld\n"
  325. "Number of data count mismatch: %lld\n"
  326. "Number of FCPIO Timeouts: %lld\n"
  327. "Number of FCPIO Aborted: %lld\n"
  328. "Number of SGL Invalid: %lld\n"
  329. "Number of Copy WQ Alloc Failures for ABTs: %lld\n"
  330. "Number of Copy WQ Alloc Failures for Device Reset: %lld\n"
  331. "Number of Copy WQ Alloc Failures for IOs: %lld\n"
  332. "Number of no icmnd itmf Completions: %lld\n"
  333. "Number of QUEUE Fulls: %lld\n"
  334. "Number of rport not ready: %lld\n"
  335. "Number of receive frame errors: %lld\n",
  336. (u64)stats->misc_stats.last_isr_time,
  337. val1.tv_sec, val1.tv_nsec,
  338. (u64)stats->misc_stats.last_ack_time,
  339. val2.tv_sec, val2.tv_nsec,
  340. (u64)atomic64_read(&stats->misc_stats.isr_count),
  341. (u64)atomic64_read(&stats->misc_stats.max_cq_entries),
  342. (u64)atomic64_read(&stats->misc_stats.ack_index_out_of_range),
  343. (u64)atomic64_read(&stats->misc_stats.data_count_mismatch),
  344. (u64)atomic64_read(&stats->misc_stats.fcpio_timeout),
  345. (u64)atomic64_read(&stats->misc_stats.fcpio_aborted),
  346. (u64)atomic64_read(&stats->misc_stats.sgl_invalid),
  347. (u64)atomic64_read(
  348. &stats->misc_stats.abts_cpwq_alloc_failures),
  349. (u64)atomic64_read(
  350. &stats->misc_stats.devrst_cpwq_alloc_failures),
  351. (u64)atomic64_read(&stats->misc_stats.io_cpwq_alloc_failures),
  352. (u64)atomic64_read(&stats->misc_stats.no_icmnd_itmf_cmpls),
  353. (u64)atomic64_read(&stats->misc_stats.queue_fulls),
  354. (u64)atomic64_read(&stats->misc_stats.rport_not_ready),
  355. (u64)atomic64_read(&stats->misc_stats.frame_errors));
  356. return len;
  357. }
  358. /*
  359. * fnic_trace_buf_init - Initialize fnic trace buffer logging facility
  360. *
  361. * Description:
  362. * Initialize trace buffer data structure by allocating required memory and
  363. * setting page_offset information for every trace entry by adding trace entry
  364. * length to previous page_offset value.
  365. */
  366. int fnic_trace_buf_init(void)
  367. {
  368. unsigned long fnic_buf_head;
  369. int i;
  370. int err = 0;
  371. trace_max_pages = fnic_trace_max_pages;
  372. fnic_max_trace_entries = (trace_max_pages * PAGE_SIZE)/
  373. FNIC_ENTRY_SIZE_BYTES;
  374. fnic_trace_buf_p = (unsigned long)vmalloc((trace_max_pages * PAGE_SIZE));
  375. if (!fnic_trace_buf_p) {
  376. printk(KERN_ERR PFX "Failed to allocate memory "
  377. "for fnic_trace_buf_p\n");
  378. err = -ENOMEM;
  379. goto err_fnic_trace_buf_init;
  380. }
  381. memset((void *)fnic_trace_buf_p, 0, (trace_max_pages * PAGE_SIZE));
  382. fnic_trace_entries.page_offset = vmalloc(fnic_max_trace_entries *
  383. sizeof(unsigned long));
  384. if (!fnic_trace_entries.page_offset) {
  385. printk(KERN_ERR PFX "Failed to allocate memory for"
  386. " page_offset\n");
  387. if (fnic_trace_buf_p) {
  388. vfree((void *)fnic_trace_buf_p);
  389. fnic_trace_buf_p = 0;
  390. }
  391. err = -ENOMEM;
  392. goto err_fnic_trace_buf_init;
  393. }
  394. memset((void *)fnic_trace_entries.page_offset, 0,
  395. (fnic_max_trace_entries * sizeof(unsigned long)));
  396. fnic_trace_entries.wr_idx = fnic_trace_entries.rd_idx = 0;
  397. fnic_buf_head = fnic_trace_buf_p;
  398. /*
  399. * Set page_offset field of fnic_trace_entries struct by
  400. * calculating memory location for every trace entry using
  401. * length of each trace entry
  402. */
  403. for (i = 0; i < fnic_max_trace_entries; i++) {
  404. fnic_trace_entries.page_offset[i] = fnic_buf_head;
  405. fnic_buf_head += FNIC_ENTRY_SIZE_BYTES;
  406. }
  407. err = fnic_trace_debugfs_init();
  408. if (err < 0) {
  409. pr_err("fnic: Failed to initialize debugfs for tracing\n");
  410. goto err_fnic_trace_debugfs_init;
  411. }
  412. pr_info("fnic: Successfully Initialized Trace Buffer\n");
  413. return err;
  414. err_fnic_trace_debugfs_init:
  415. fnic_trace_free();
  416. err_fnic_trace_buf_init:
  417. return err;
  418. }
  419. /*
  420. * fnic_trace_free - Free memory of fnic trace data structures.
  421. */
  422. void fnic_trace_free(void)
  423. {
  424. fnic_tracing_enabled = 0;
  425. fnic_trace_debugfs_terminate();
  426. if (fnic_trace_entries.page_offset) {
  427. vfree((void *)fnic_trace_entries.page_offset);
  428. fnic_trace_entries.page_offset = NULL;
  429. }
  430. if (fnic_trace_buf_p) {
  431. vfree((void *)fnic_trace_buf_p);
  432. fnic_trace_buf_p = 0;
  433. }
  434. printk(KERN_INFO PFX "Successfully Freed Trace Buffer\n");
  435. }
  436. /*
  437. * fnic_fc_ctlr_trace_buf_init -
  438. * Initialize trace buffer to log fnic control frames
  439. * Description:
  440. * Initialize trace buffer data structure by allocating
  441. * required memory for trace data as well as for Indexes.
  442. * Frame size is 256 bytes and
  443. * memory is allocated for 1024 entries of 256 bytes.
  444. * Page_offset(Index) is set to the address of trace entry
  445. * and page_offset is initialized by adding frame size
  446. * to the previous page_offset entry.
  447. */
  448. int fnic_fc_trace_init(void)
  449. {
  450. unsigned long fc_trace_buf_head;
  451. int err = 0;
  452. int i;
  453. fc_trace_max_entries = (fnic_fc_trace_max_pages * PAGE_SIZE)/
  454. FC_TRC_SIZE_BYTES;
  455. fnic_fc_ctlr_trace_buf_p = (unsigned long)vmalloc(
  456. fnic_fc_trace_max_pages * PAGE_SIZE);
  457. if (!fnic_fc_ctlr_trace_buf_p) {
  458. pr_err("fnic: Failed to allocate memory for "
  459. "FC Control Trace Buf\n");
  460. err = -ENOMEM;
  461. goto err_fnic_fc_ctlr_trace_buf_init;
  462. }
  463. memset((void *)fnic_fc_ctlr_trace_buf_p, 0,
  464. fnic_fc_trace_max_pages * PAGE_SIZE);
  465. /* Allocate memory for page offset */
  466. fc_trace_entries.page_offset = vmalloc(fc_trace_max_entries *
  467. sizeof(unsigned long));
  468. if (!fc_trace_entries.page_offset) {
  469. pr_err("fnic:Failed to allocate memory for page_offset\n");
  470. if (fnic_fc_ctlr_trace_buf_p) {
  471. pr_err("fnic: Freeing FC Control Trace Buf\n");
  472. vfree((void *)fnic_fc_ctlr_trace_buf_p);
  473. fnic_fc_ctlr_trace_buf_p = 0;
  474. }
  475. err = -ENOMEM;
  476. goto err_fnic_fc_ctlr_trace_buf_init;
  477. }
  478. memset((void *)fc_trace_entries.page_offset, 0,
  479. (fc_trace_max_entries * sizeof(unsigned long)));
  480. fc_trace_entries.rd_idx = fc_trace_entries.wr_idx = 0;
  481. fc_trace_buf_head = fnic_fc_ctlr_trace_buf_p;
  482. /*
  483. * Set up fc_trace_entries.page_offset field with memory location
  484. * for every trace entry
  485. */
  486. for (i = 0; i < fc_trace_max_entries; i++) {
  487. fc_trace_entries.page_offset[i] = fc_trace_buf_head;
  488. fc_trace_buf_head += FC_TRC_SIZE_BYTES;
  489. }
  490. err = fnic_fc_trace_debugfs_init();
  491. if (err < 0) {
  492. pr_err("fnic: Failed to initialize FC_CTLR tracing.\n");
  493. goto err_fnic_fc_ctlr_trace_debugfs_init;
  494. }
  495. pr_info("fnic: Successfully Initialized FC_CTLR Trace Buffer\n");
  496. return err;
  497. err_fnic_fc_ctlr_trace_debugfs_init:
  498. fnic_fc_trace_free();
  499. err_fnic_fc_ctlr_trace_buf_init:
  500. return err;
  501. }
  502. /*
  503. * Fnic_fc_ctlr_trace_free - Free memory of fnic_fc_ctlr trace data structures.
  504. */
  505. void fnic_fc_trace_free(void)
  506. {
  507. fnic_fc_tracing_enabled = 0;
  508. fnic_fc_trace_debugfs_terminate();
  509. if (fc_trace_entries.page_offset) {
  510. vfree((void *)fc_trace_entries.page_offset);
  511. fc_trace_entries.page_offset = NULL;
  512. }
  513. if (fnic_fc_ctlr_trace_buf_p) {
  514. vfree((void *)fnic_fc_ctlr_trace_buf_p);
  515. fnic_fc_ctlr_trace_buf_p = 0;
  516. }
  517. pr_info("fnic:Successfully FC_CTLR Freed Trace Buffer\n");
  518. }
  519. /*
  520. * fnic_fc_ctlr_set_trace_data:
  521. * Maintain rd & wr idx accordingly and set data
  522. * Passed parameters:
  523. * host_no: host number accociated with fnic
  524. * frame_type: send_frame, rece_frame or link event
  525. * fc_frame: pointer to fc_frame
  526. * frame_len: Length of the fc_frame
  527. * Description:
  528. * This routine will get next available wr_idx and
  529. * copy all passed trace data to the buffer pointed by wr_idx
  530. * and increment wr_idx. It will also make sure that we dont
  531. * overwrite the entry which we are reading and also
  532. * wrap around if we reach the maximum entries.
  533. * Returned Value:
  534. * It will return 0 for success or -1 for failure
  535. */
  536. int fnic_fc_trace_set_data(u32 host_no, u8 frame_type,
  537. char *frame, u32 fc_trc_frame_len)
  538. {
  539. unsigned long flags;
  540. struct fc_trace_hdr *fc_buf;
  541. unsigned long eth_fcoe_hdr_len;
  542. char *fc_trace;
  543. if (fnic_fc_tracing_enabled == 0)
  544. return 0;
  545. spin_lock_irqsave(&fnic_fc_trace_lock, flags);
  546. if (fnic_fc_trace_cleared == 1) {
  547. fc_trace_entries.rd_idx = fc_trace_entries.wr_idx = 0;
  548. pr_info("fnic: Resetting the read idx\n");
  549. memset((void *)fnic_fc_ctlr_trace_buf_p, 0,
  550. fnic_fc_trace_max_pages * PAGE_SIZE);
  551. fnic_fc_trace_cleared = 0;
  552. }
  553. fc_buf = (struct fc_trace_hdr *)
  554. fc_trace_entries.page_offset[fc_trace_entries.wr_idx];
  555. fc_trace_entries.wr_idx++;
  556. if (fc_trace_entries.wr_idx >= fc_trace_max_entries)
  557. fc_trace_entries.wr_idx = 0;
  558. if (fc_trace_entries.wr_idx == fc_trace_entries.rd_idx) {
  559. fc_trace_entries.rd_idx++;
  560. if (fc_trace_entries.rd_idx >= fc_trace_max_entries)
  561. fc_trace_entries.rd_idx = 0;
  562. }
  563. fc_buf->time_stamp = CURRENT_TIME;
  564. fc_buf->host_no = host_no;
  565. fc_buf->frame_type = frame_type;
  566. fc_trace = (char *)FC_TRACE_ADDRESS(fc_buf);
  567. /* During the receive path, we do not have eth hdr as well as fcoe hdr
  568. * at trace entry point so we will stuff 0xff just to make it generic.
  569. */
  570. if (frame_type == FNIC_FC_RECV) {
  571. eth_fcoe_hdr_len = sizeof(struct ethhdr) +
  572. sizeof(struct fcoe_hdr);
  573. memset((char *)fc_trace, 0xff, eth_fcoe_hdr_len);
  574. /* Copy the rest of data frame */
  575. memcpy((char *)(fc_trace + eth_fcoe_hdr_len), (void *)frame,
  576. min_t(u8, fc_trc_frame_len,
  577. (u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE
  578. - eth_fcoe_hdr_len)));
  579. } else {
  580. memcpy((char *)fc_trace, (void *)frame,
  581. min_t(u8, fc_trc_frame_len,
  582. (u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE)));
  583. }
  584. /* Store the actual received length */
  585. fc_buf->frame_len = fc_trc_frame_len;
  586. spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
  587. return 0;
  588. }
  589. /*
  590. * fnic_fc_ctlr_get_trace_data: Copy trace buffer to a memory file
  591. * Passed parameter:
  592. * @fnic_dbgfs_t: pointer to debugfs trace buffer
  593. * rdata_flag: 1 => Unformated file
  594. * 0 => formated file
  595. * Description:
  596. * This routine will copy the trace data to memory file with
  597. * proper formatting and also copy to another memory
  598. * file without formatting for further procesing.
  599. * Retrun Value:
  600. * Number of bytes that were dumped into fnic_dbgfs_t
  601. */
  602. int fnic_fc_trace_get_data(fnic_dbgfs_t *fnic_dbgfs_prt, u8 rdata_flag)
  603. {
  604. int rd_idx, wr_idx;
  605. unsigned long flags;
  606. int len = 0, j;
  607. struct fc_trace_hdr *tdata;
  608. char *fc_trace;
  609. spin_lock_irqsave(&fnic_fc_trace_lock, flags);
  610. if (fc_trace_entries.wr_idx == fc_trace_entries.rd_idx) {
  611. spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
  612. pr_info("fnic: Buffer is empty\n");
  613. return 0;
  614. }
  615. rd_idx = fc_trace_entries.rd_idx;
  616. wr_idx = fc_trace_entries.wr_idx;
  617. if (rdata_flag == 0) {
  618. len += snprintf(fnic_dbgfs_prt->buffer + len,
  619. (fnic_fc_trace_max_pages * PAGE_SIZE * 3) - len,
  620. "Time Stamp (UTC)\t\t"
  621. "Host No: F Type: len: FCoE_FRAME:\n");
  622. }
  623. while (rd_idx != wr_idx) {
  624. tdata = (struct fc_trace_hdr *)
  625. fc_trace_entries.page_offset[rd_idx];
  626. if (!tdata) {
  627. pr_info("fnic: Rd data is NULL\n");
  628. spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
  629. return 0;
  630. }
  631. if (rdata_flag == 0) {
  632. copy_and_format_trace_data(tdata,
  633. fnic_dbgfs_prt, &len, rdata_flag);
  634. } else {
  635. fc_trace = (char *)tdata;
  636. for (j = 0; j < FC_TRC_SIZE_BYTES; j++) {
  637. len += snprintf(fnic_dbgfs_prt->buffer + len,
  638. (fnic_fc_trace_max_pages * PAGE_SIZE * 3)
  639. - len, "%02x", fc_trace[j] & 0xff);
  640. } /* for loop */
  641. len += snprintf(fnic_dbgfs_prt->buffer + len,
  642. (fnic_fc_trace_max_pages * PAGE_SIZE * 3) - len,
  643. "\n");
  644. }
  645. rd_idx++;
  646. if (rd_idx > (fc_trace_max_entries - 1))
  647. rd_idx = 0;
  648. }
  649. spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
  650. return len;
  651. }
  652. /*
  653. * copy_and_format_trace_data: Copy formatted data to char * buffer
  654. * Passed Parameter:
  655. * @fc_trace_hdr_t: pointer to trace data
  656. * @fnic_dbgfs_t: pointer to debugfs trace buffer
  657. * @orig_len: pointer to len
  658. * rdata_flag: 0 => Formated file, 1 => Unformated file
  659. * Description:
  660. * This routine will format and copy the passed trace data
  661. * for formated file or unformated file accordingly.
  662. */
  663. void copy_and_format_trace_data(struct fc_trace_hdr *tdata,
  664. fnic_dbgfs_t *fnic_dbgfs_prt, int *orig_len,
  665. u8 rdata_flag)
  666. {
  667. struct tm tm;
  668. int j, i = 1, len;
  669. char *fc_trace, *fmt;
  670. int ethhdr_len = sizeof(struct ethhdr) - 1;
  671. int fcoehdr_len = sizeof(struct fcoe_hdr);
  672. int fchdr_len = sizeof(struct fc_frame_header);
  673. int max_size = fnic_fc_trace_max_pages * PAGE_SIZE * 3;
  674. tdata->frame_type = tdata->frame_type & 0x7F;
  675. len = *orig_len;
  676. time_to_tm(tdata->time_stamp.tv_sec, 0, &tm);
  677. fmt = "%02d:%02d:%04ld %02d:%02d:%02d.%09lu ns%8x %c%8x\t";
  678. len += snprintf(fnic_dbgfs_prt->buffer + len,
  679. max_size - len,
  680. fmt,
  681. tm.tm_mon + 1, tm.tm_mday, tm.tm_year + 1900,
  682. tm.tm_hour, tm.tm_min, tm.tm_sec,
  683. tdata->time_stamp.tv_nsec, tdata->host_no,
  684. tdata->frame_type, tdata->frame_len);
  685. fc_trace = (char *)FC_TRACE_ADDRESS(tdata);
  686. for (j = 0; j < min_t(u8, tdata->frame_len,
  687. (u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE)); j++) {
  688. if (tdata->frame_type == FNIC_FC_LE) {
  689. len += snprintf(fnic_dbgfs_prt->buffer + len,
  690. max_size - len, "%c", fc_trace[j]);
  691. } else {
  692. len += snprintf(fnic_dbgfs_prt->buffer + len,
  693. max_size - len, "%02x", fc_trace[j] & 0xff);
  694. len += snprintf(fnic_dbgfs_prt->buffer + len,
  695. max_size - len, " ");
  696. if (j == ethhdr_len ||
  697. j == ethhdr_len + fcoehdr_len ||
  698. j == ethhdr_len + fcoehdr_len + fchdr_len ||
  699. (i > 3 && j%fchdr_len == 0)) {
  700. len += snprintf(fnic_dbgfs_prt->buffer
  701. + len, max_size - len,
  702. "\n\t\t\t\t\t\t\t\t");
  703. i++;
  704. }
  705. } /* end of else*/
  706. } /* End of for loop*/
  707. len += snprintf(fnic_dbgfs_prt->buffer + len,
  708. max_size - len, "\n");
  709. *orig_len = len;
  710. }