fnic_trace.c 26 KB

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