fnic_trace.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 timespec64 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_timespec64(tbp->timestamp.low, &val);
  119. } else {
  120. sprint_symbol(str, tbp->fnaddr.val);
  121. jiffies_to_timespec64(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. "%16llu.%09lu %-50s %8x %8x %16llx %16llx "
  130. "%16llx %16llx %16llx\n", (u64)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_timespec64(tbp->timestamp.low, &val);
  161. } else {
  162. sprint_symbol(str, tbp->fnaddr.val);
  163. jiffies_to_timespec64(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. "%16llu.%09lu %-50s %8x %8x %16llx %16llx "
  172. "%16llx %16llx %16llx\n", (u64)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 timespec64 val1, val2;
  206. ktime_get_real_ts64(&val1);
  207. len = snprintf(debug->debug_buffer + len, buf_size - len,
  208. "------------------------------------------\n"
  209. "\t\tTime\n"
  210. "------------------------------------------\n");
  211. len += snprintf(debug->debug_buffer + len, buf_size - len,
  212. "Current time : [%lld:%ld]\n"
  213. "Last stats reset time: [%lld:%09ld]\n"
  214. "Last stats read time: [%lld:%ld]\n"
  215. "delta since last reset: [%lld:%ld]\n"
  216. "delta since last read: [%lld:%ld]\n",
  217. (s64)val1.tv_sec, val1.tv_nsec,
  218. (s64)stats->stats_timestamps.last_reset_time.tv_sec,
  219. stats->stats_timestamps.last_reset_time.tv_nsec,
  220. (s64)stats->stats_timestamps.last_read_time.tv_sec,
  221. stats->stats_timestamps.last_read_time.tv_nsec,
  222. (s64)timespec64_sub(val1, stats->stats_timestamps.last_reset_time).tv_sec,
  223. timespec64_sub(val1, stats->stats_timestamps.last_reset_time).tv_nsec,
  224. (s64)timespec64_sub(val1, stats->stats_timestamps.last_read_time).tv_sec,
  225. timespec64_sub(val1, stats->stats_timestamps.last_read_time).tv_nsec);
  226. stats->stats_timestamps.last_read_time = val1;
  227. len += snprintf(debug->debug_buffer + len, buf_size - len,
  228. "------------------------------------------\n"
  229. "\t\tIO Statistics\n"
  230. "------------------------------------------\n");
  231. len += snprintf(debug->debug_buffer + len, buf_size - len,
  232. "Number of Active IOs: %lld\nMaximum Active IOs: %lld\n"
  233. "Number of IOs: %lld\nNumber of IO Completions: %lld\n"
  234. "Number of IO Failures: %lld\nNumber of IO NOT Found: %lld\n"
  235. "Number of Memory alloc Failures: %lld\n"
  236. "Number of IOREQ Null: %lld\n"
  237. "Number of SCSI cmd pointer Null: %lld\n"
  238. "\nIO completion times: \n"
  239. " < 10 ms : %lld\n"
  240. " 10 ms - 100 ms : %lld\n"
  241. " 100 ms - 500 ms : %lld\n"
  242. " 500 ms - 5 sec: %lld\n"
  243. " 5 sec - 10 sec: %lld\n"
  244. " 10 sec - 30 sec: %lld\n"
  245. " > 30 sec: %lld\n",
  246. (u64)atomic64_read(&stats->io_stats.active_ios),
  247. (u64)atomic64_read(&stats->io_stats.max_active_ios),
  248. (u64)atomic64_read(&stats->io_stats.num_ios),
  249. (u64)atomic64_read(&stats->io_stats.io_completions),
  250. (u64)atomic64_read(&stats->io_stats.io_failures),
  251. (u64)atomic64_read(&stats->io_stats.io_not_found),
  252. (u64)atomic64_read(&stats->io_stats.alloc_failures),
  253. (u64)atomic64_read(&stats->io_stats.ioreq_null),
  254. (u64)atomic64_read(&stats->io_stats.sc_null),
  255. (u64)atomic64_read(&stats->io_stats.io_btw_0_to_10_msec),
  256. (u64)atomic64_read(&stats->io_stats.io_btw_10_to_100_msec),
  257. (u64)atomic64_read(&stats->io_stats.io_btw_100_to_500_msec),
  258. (u64)atomic64_read(&stats->io_stats.io_btw_500_to_5000_msec),
  259. (u64)atomic64_read(&stats->io_stats.io_btw_5000_to_10000_msec),
  260. (u64)atomic64_read(&stats->io_stats.io_btw_10000_to_30000_msec),
  261. (u64)atomic64_read(&stats->io_stats.io_greater_than_30000_msec));
  262. len += snprintf(debug->debug_buffer + len, buf_size - len,
  263. "\nCurrent Max IO time : %lld\n",
  264. (u64)atomic64_read(&stats->io_stats.current_max_io_time));
  265. len += snprintf(debug->debug_buffer + len, buf_size - len,
  266. "\n------------------------------------------\n"
  267. "\t\tAbort Statistics\n"
  268. "------------------------------------------\n");
  269. len += snprintf(debug->debug_buffer + len, buf_size - len,
  270. "Number of Aborts: %lld\n"
  271. "Number of Abort Failures: %lld\n"
  272. "Number of Abort Driver Timeouts: %lld\n"
  273. "Number of Abort FW Timeouts: %lld\n"
  274. "Number of Abort IO NOT Found: %lld\n"
  275. "Abort issued times: \n"
  276. " < 6 sec : %lld\n"
  277. " 6 sec - 20 sec : %lld\n"
  278. " 20 sec - 30 sec : %lld\n"
  279. " 30 sec - 40 sec : %lld\n"
  280. " 40 sec - 50 sec : %lld\n"
  281. " 50 sec - 60 sec : %lld\n"
  282. " > 60 sec: %lld\n",
  283. (u64)atomic64_read(&stats->abts_stats.aborts),
  284. (u64)atomic64_read(&stats->abts_stats.abort_failures),
  285. (u64)atomic64_read(&stats->abts_stats.abort_drv_timeouts),
  286. (u64)atomic64_read(&stats->abts_stats.abort_fw_timeouts),
  287. (u64)atomic64_read(&stats->abts_stats.abort_io_not_found),
  288. (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_0_to_6_sec),
  289. (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_6_to_20_sec),
  290. (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_20_to_30_sec),
  291. (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_30_to_40_sec),
  292. (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_40_to_50_sec),
  293. (u64)atomic64_read(&stats->abts_stats.abort_issued_btw_50_to_60_sec),
  294. (u64)atomic64_read(&stats->abts_stats.abort_issued_greater_than_60_sec));
  295. len += snprintf(debug->debug_buffer + len, buf_size - len,
  296. "\n------------------------------------------\n"
  297. "\t\tTerminate Statistics\n"
  298. "------------------------------------------\n");
  299. len += snprintf(debug->debug_buffer + len, buf_size - len,
  300. "Number of Terminates: %lld\n"
  301. "Maximum Terminates: %lld\n"
  302. "Number of Terminate Driver Timeouts: %lld\n"
  303. "Number of Terminate FW Timeouts: %lld\n"
  304. "Number of Terminate IO NOT Found: %lld\n"
  305. "Number of Terminate Failures: %lld\n",
  306. (u64)atomic64_read(&stats->term_stats.terminates),
  307. (u64)atomic64_read(&stats->term_stats.max_terminates),
  308. (u64)atomic64_read(&stats->term_stats.terminate_drv_timeouts),
  309. (u64)atomic64_read(&stats->term_stats.terminate_fw_timeouts),
  310. (u64)atomic64_read(&stats->term_stats.terminate_io_not_found),
  311. (u64)atomic64_read(&stats->term_stats.terminate_failures));
  312. len += snprintf(debug->debug_buffer + len, buf_size - len,
  313. "\n------------------------------------------\n"
  314. "\t\tReset Statistics\n"
  315. "------------------------------------------\n");
  316. len += snprintf(debug->debug_buffer + len, buf_size - len,
  317. "Number of Device Resets: %lld\n"
  318. "Number of Device Reset Failures: %lld\n"
  319. "Number of Device Reset Aborts: %lld\n"
  320. "Number of Device Reset Timeouts: %lld\n"
  321. "Number of Device Reset Terminates: %lld\n"
  322. "Number of FW Resets: %lld\n"
  323. "Number of FW Reset Completions: %lld\n"
  324. "Number of FW Reset Failures: %lld\n"
  325. "Number of Fnic Reset: %lld\n"
  326. "Number of Fnic Reset Completions: %lld\n"
  327. "Number of Fnic Reset Failures: %lld\n",
  328. (u64)atomic64_read(&stats->reset_stats.device_resets),
  329. (u64)atomic64_read(&stats->reset_stats.device_reset_failures),
  330. (u64)atomic64_read(&stats->reset_stats.device_reset_aborts),
  331. (u64)atomic64_read(&stats->reset_stats.device_reset_timeouts),
  332. (u64)atomic64_read(
  333. &stats->reset_stats.device_reset_terminates),
  334. (u64)atomic64_read(&stats->reset_stats.fw_resets),
  335. (u64)atomic64_read(&stats->reset_stats.fw_reset_completions),
  336. (u64)atomic64_read(&stats->reset_stats.fw_reset_failures),
  337. (u64)atomic64_read(&stats->reset_stats.fnic_resets),
  338. (u64)atomic64_read(
  339. &stats->reset_stats.fnic_reset_completions),
  340. (u64)atomic64_read(&stats->reset_stats.fnic_reset_failures));
  341. len += snprintf(debug->debug_buffer + len, buf_size - len,
  342. "\n------------------------------------------\n"
  343. "\t\tFirmware Statistics\n"
  344. "------------------------------------------\n");
  345. len += snprintf(debug->debug_buffer + len, buf_size - len,
  346. "Number of Active FW Requests %lld\n"
  347. "Maximum FW Requests: %lld\n"
  348. "Number of FW out of resources: %lld\n"
  349. "Number of FW IO errors: %lld\n",
  350. (u64)atomic64_read(&stats->fw_stats.active_fw_reqs),
  351. (u64)atomic64_read(&stats->fw_stats.max_fw_reqs),
  352. (u64)atomic64_read(&stats->fw_stats.fw_out_of_resources),
  353. (u64)atomic64_read(&stats->fw_stats.io_fw_errs));
  354. len += snprintf(debug->debug_buffer + len, buf_size - len,
  355. "\n------------------------------------------\n"
  356. "\t\tVlan Discovery Statistics\n"
  357. "------------------------------------------\n");
  358. len += snprintf(debug->debug_buffer + len, buf_size - len,
  359. "Number of Vlan Discovery Requests Sent %lld\n"
  360. "Vlan Response Received with no FCF VLAN ID: %lld\n"
  361. "No solicitations recvd after vlan set, expiry count: %lld\n"
  362. "Flogi rejects count: %lld\n",
  363. (u64)atomic64_read(&stats->vlan_stats.vlan_disc_reqs),
  364. (u64)atomic64_read(&stats->vlan_stats.resp_withno_vlanID),
  365. (u64)atomic64_read(&stats->vlan_stats.sol_expiry_count),
  366. (u64)atomic64_read(&stats->vlan_stats.flogi_rejects));
  367. len += snprintf(debug->debug_buffer + len, buf_size - len,
  368. "\n------------------------------------------\n"
  369. "\t\tOther Important Statistics\n"
  370. "------------------------------------------\n");
  371. jiffies_to_timespec64(stats->misc_stats.last_isr_time, &val1);
  372. jiffies_to_timespec64(stats->misc_stats.last_ack_time, &val2);
  373. len += snprintf(debug->debug_buffer + len, buf_size - len,
  374. "Last ISR time: %llu (%8llu.%09lu)\n"
  375. "Last ACK time: %llu (%8llu.%09lu)\n"
  376. "Number of ISRs: %lld\n"
  377. "Maximum CQ Entries: %lld\n"
  378. "Number of ACK index out of range: %lld\n"
  379. "Number of data count mismatch: %lld\n"
  380. "Number of FCPIO Timeouts: %lld\n"
  381. "Number of FCPIO Aborted: %lld\n"
  382. "Number of SGL Invalid: %lld\n"
  383. "Number of Copy WQ Alloc Failures for ABTs: %lld\n"
  384. "Number of Copy WQ Alloc Failures for Device Reset: %lld\n"
  385. "Number of Copy WQ Alloc Failures for IOs: %lld\n"
  386. "Number of no icmnd itmf Completions: %lld\n"
  387. "Number of Check Conditions encountered: %lld\n"
  388. "Number of QUEUE Fulls: %lld\n"
  389. "Number of rport not ready: %lld\n"
  390. "Number of receive frame errors: %lld\n",
  391. (u64)stats->misc_stats.last_isr_time,
  392. (s64)val1.tv_sec, val1.tv_nsec,
  393. (u64)stats->misc_stats.last_ack_time,
  394. (s64)val2.tv_sec, val2.tv_nsec,
  395. (u64)atomic64_read(&stats->misc_stats.isr_count),
  396. (u64)atomic64_read(&stats->misc_stats.max_cq_entries),
  397. (u64)atomic64_read(&stats->misc_stats.ack_index_out_of_range),
  398. (u64)atomic64_read(&stats->misc_stats.data_count_mismatch),
  399. (u64)atomic64_read(&stats->misc_stats.fcpio_timeout),
  400. (u64)atomic64_read(&stats->misc_stats.fcpio_aborted),
  401. (u64)atomic64_read(&stats->misc_stats.sgl_invalid),
  402. (u64)atomic64_read(
  403. &stats->misc_stats.abts_cpwq_alloc_failures),
  404. (u64)atomic64_read(
  405. &stats->misc_stats.devrst_cpwq_alloc_failures),
  406. (u64)atomic64_read(&stats->misc_stats.io_cpwq_alloc_failures),
  407. (u64)atomic64_read(&stats->misc_stats.no_icmnd_itmf_cmpls),
  408. (u64)atomic64_read(&stats->misc_stats.check_condition),
  409. (u64)atomic64_read(&stats->misc_stats.queue_fulls),
  410. (u64)atomic64_read(&stats->misc_stats.rport_not_ready),
  411. (u64)atomic64_read(&stats->misc_stats.frame_errors));
  412. return len;
  413. }
  414. /*
  415. * fnic_trace_buf_init - Initialize fnic trace buffer logging facility
  416. *
  417. * Description:
  418. * Initialize trace buffer data structure by allocating required memory and
  419. * setting page_offset information for every trace entry by adding trace entry
  420. * length to previous page_offset value.
  421. */
  422. int fnic_trace_buf_init(void)
  423. {
  424. unsigned long fnic_buf_head;
  425. int i;
  426. int err = 0;
  427. trace_max_pages = fnic_trace_max_pages;
  428. fnic_max_trace_entries = (trace_max_pages * PAGE_SIZE)/
  429. FNIC_ENTRY_SIZE_BYTES;
  430. fnic_trace_buf_p = (unsigned long)vmalloc((trace_max_pages * PAGE_SIZE));
  431. if (!fnic_trace_buf_p) {
  432. printk(KERN_ERR PFX "Failed to allocate memory "
  433. "for fnic_trace_buf_p\n");
  434. err = -ENOMEM;
  435. goto err_fnic_trace_buf_init;
  436. }
  437. memset((void *)fnic_trace_buf_p, 0, (trace_max_pages * PAGE_SIZE));
  438. fnic_trace_entries.page_offset =
  439. vmalloc(array_size(fnic_max_trace_entries,
  440. sizeof(unsigned long)));
  441. if (!fnic_trace_entries.page_offset) {
  442. printk(KERN_ERR PFX "Failed to allocate memory for"
  443. " page_offset\n");
  444. if (fnic_trace_buf_p) {
  445. vfree((void *)fnic_trace_buf_p);
  446. fnic_trace_buf_p = 0;
  447. }
  448. err = -ENOMEM;
  449. goto err_fnic_trace_buf_init;
  450. }
  451. memset((void *)fnic_trace_entries.page_offset, 0,
  452. (fnic_max_trace_entries * sizeof(unsigned long)));
  453. fnic_trace_entries.wr_idx = fnic_trace_entries.rd_idx = 0;
  454. fnic_buf_head = fnic_trace_buf_p;
  455. /*
  456. * Set page_offset field of fnic_trace_entries struct by
  457. * calculating memory location for every trace entry using
  458. * length of each trace entry
  459. */
  460. for (i = 0; i < fnic_max_trace_entries; i++) {
  461. fnic_trace_entries.page_offset[i] = fnic_buf_head;
  462. fnic_buf_head += FNIC_ENTRY_SIZE_BYTES;
  463. }
  464. err = fnic_trace_debugfs_init();
  465. if (err < 0) {
  466. pr_err("fnic: Failed to initialize debugfs for tracing\n");
  467. goto err_fnic_trace_debugfs_init;
  468. }
  469. pr_info("fnic: Successfully Initialized Trace Buffer\n");
  470. return err;
  471. err_fnic_trace_debugfs_init:
  472. fnic_trace_free();
  473. err_fnic_trace_buf_init:
  474. return err;
  475. }
  476. /*
  477. * fnic_trace_free - Free memory of fnic trace data structures.
  478. */
  479. void fnic_trace_free(void)
  480. {
  481. fnic_tracing_enabled = 0;
  482. fnic_trace_debugfs_terminate();
  483. if (fnic_trace_entries.page_offset) {
  484. vfree((void *)fnic_trace_entries.page_offset);
  485. fnic_trace_entries.page_offset = NULL;
  486. }
  487. if (fnic_trace_buf_p) {
  488. vfree((void *)fnic_trace_buf_p);
  489. fnic_trace_buf_p = 0;
  490. }
  491. printk(KERN_INFO PFX "Successfully Freed Trace Buffer\n");
  492. }
  493. /*
  494. * fnic_fc_ctlr_trace_buf_init -
  495. * Initialize trace buffer to log fnic control frames
  496. * Description:
  497. * Initialize trace buffer data structure by allocating
  498. * required memory for trace data as well as for Indexes.
  499. * Frame size is 256 bytes and
  500. * memory is allocated for 1024 entries of 256 bytes.
  501. * Page_offset(Index) is set to the address of trace entry
  502. * and page_offset is initialized by adding frame size
  503. * to the previous page_offset entry.
  504. */
  505. int fnic_fc_trace_init(void)
  506. {
  507. unsigned long fc_trace_buf_head;
  508. int err = 0;
  509. int i;
  510. fc_trace_max_entries = (fnic_fc_trace_max_pages * PAGE_SIZE)/
  511. FC_TRC_SIZE_BYTES;
  512. fnic_fc_ctlr_trace_buf_p =
  513. (unsigned long)vmalloc(array_size(PAGE_SIZE,
  514. fnic_fc_trace_max_pages));
  515. if (!fnic_fc_ctlr_trace_buf_p) {
  516. pr_err("fnic: Failed to allocate memory for "
  517. "FC Control Trace Buf\n");
  518. err = -ENOMEM;
  519. goto err_fnic_fc_ctlr_trace_buf_init;
  520. }
  521. memset((void *)fnic_fc_ctlr_trace_buf_p, 0,
  522. fnic_fc_trace_max_pages * PAGE_SIZE);
  523. /* Allocate memory for page offset */
  524. fc_trace_entries.page_offset =
  525. vmalloc(array_size(fc_trace_max_entries,
  526. sizeof(unsigned long)));
  527. if (!fc_trace_entries.page_offset) {
  528. pr_err("fnic:Failed to allocate memory for page_offset\n");
  529. if (fnic_fc_ctlr_trace_buf_p) {
  530. pr_err("fnic: Freeing FC Control Trace Buf\n");
  531. vfree((void *)fnic_fc_ctlr_trace_buf_p);
  532. fnic_fc_ctlr_trace_buf_p = 0;
  533. }
  534. err = -ENOMEM;
  535. goto err_fnic_fc_ctlr_trace_buf_init;
  536. }
  537. memset((void *)fc_trace_entries.page_offset, 0,
  538. (fc_trace_max_entries * sizeof(unsigned long)));
  539. fc_trace_entries.rd_idx = fc_trace_entries.wr_idx = 0;
  540. fc_trace_buf_head = fnic_fc_ctlr_trace_buf_p;
  541. /*
  542. * Set up fc_trace_entries.page_offset field with memory location
  543. * for every trace entry
  544. */
  545. for (i = 0; i < fc_trace_max_entries; i++) {
  546. fc_trace_entries.page_offset[i] = fc_trace_buf_head;
  547. fc_trace_buf_head += FC_TRC_SIZE_BYTES;
  548. }
  549. err = fnic_fc_trace_debugfs_init();
  550. if (err < 0) {
  551. pr_err("fnic: Failed to initialize FC_CTLR tracing.\n");
  552. goto err_fnic_fc_ctlr_trace_debugfs_init;
  553. }
  554. pr_info("fnic: Successfully Initialized FC_CTLR Trace Buffer\n");
  555. return err;
  556. err_fnic_fc_ctlr_trace_debugfs_init:
  557. fnic_fc_trace_free();
  558. err_fnic_fc_ctlr_trace_buf_init:
  559. return err;
  560. }
  561. /*
  562. * Fnic_fc_ctlr_trace_free - Free memory of fnic_fc_ctlr trace data structures.
  563. */
  564. void fnic_fc_trace_free(void)
  565. {
  566. fnic_fc_tracing_enabled = 0;
  567. fnic_fc_trace_debugfs_terminate();
  568. if (fc_trace_entries.page_offset) {
  569. vfree((void *)fc_trace_entries.page_offset);
  570. fc_trace_entries.page_offset = NULL;
  571. }
  572. if (fnic_fc_ctlr_trace_buf_p) {
  573. vfree((void *)fnic_fc_ctlr_trace_buf_p);
  574. fnic_fc_ctlr_trace_buf_p = 0;
  575. }
  576. pr_info("fnic:Successfully FC_CTLR Freed Trace Buffer\n");
  577. }
  578. /*
  579. * fnic_fc_ctlr_set_trace_data:
  580. * Maintain rd & wr idx accordingly and set data
  581. * Passed parameters:
  582. * host_no: host number accociated with fnic
  583. * frame_type: send_frame, rece_frame or link event
  584. * fc_frame: pointer to fc_frame
  585. * frame_len: Length of the fc_frame
  586. * Description:
  587. * This routine will get next available wr_idx and
  588. * copy all passed trace data to the buffer pointed by wr_idx
  589. * and increment wr_idx. It will also make sure that we dont
  590. * overwrite the entry which we are reading and also
  591. * wrap around if we reach the maximum entries.
  592. * Returned Value:
  593. * It will return 0 for success or -1 for failure
  594. */
  595. int fnic_fc_trace_set_data(u32 host_no, u8 frame_type,
  596. char *frame, u32 fc_trc_frame_len)
  597. {
  598. unsigned long flags;
  599. struct fc_trace_hdr *fc_buf;
  600. unsigned long eth_fcoe_hdr_len;
  601. char *fc_trace;
  602. if (fnic_fc_tracing_enabled == 0)
  603. return 0;
  604. spin_lock_irqsave(&fnic_fc_trace_lock, flags);
  605. if (fnic_fc_trace_cleared == 1) {
  606. fc_trace_entries.rd_idx = fc_trace_entries.wr_idx = 0;
  607. pr_info("fnic: Resetting the read idx\n");
  608. memset((void *)fnic_fc_ctlr_trace_buf_p, 0,
  609. fnic_fc_trace_max_pages * PAGE_SIZE);
  610. fnic_fc_trace_cleared = 0;
  611. }
  612. fc_buf = (struct fc_trace_hdr *)
  613. fc_trace_entries.page_offset[fc_trace_entries.wr_idx];
  614. fc_trace_entries.wr_idx++;
  615. if (fc_trace_entries.wr_idx >= fc_trace_max_entries)
  616. fc_trace_entries.wr_idx = 0;
  617. if (fc_trace_entries.wr_idx == fc_trace_entries.rd_idx) {
  618. fc_trace_entries.rd_idx++;
  619. if (fc_trace_entries.rd_idx >= fc_trace_max_entries)
  620. fc_trace_entries.rd_idx = 0;
  621. }
  622. ktime_get_real_ts64(&fc_buf->time_stamp);
  623. fc_buf->host_no = host_no;
  624. fc_buf->frame_type = frame_type;
  625. fc_trace = (char *)FC_TRACE_ADDRESS(fc_buf);
  626. /* During the receive path, we do not have eth hdr as well as fcoe hdr
  627. * at trace entry point so we will stuff 0xff just to make it generic.
  628. */
  629. if (frame_type == FNIC_FC_RECV) {
  630. eth_fcoe_hdr_len = sizeof(struct ethhdr) +
  631. sizeof(struct fcoe_hdr);
  632. memset((char *)fc_trace, 0xff, eth_fcoe_hdr_len);
  633. /* Copy the rest of data frame */
  634. memcpy((char *)(fc_trace + eth_fcoe_hdr_len), (void *)frame,
  635. min_t(u8, fc_trc_frame_len,
  636. (u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE
  637. - eth_fcoe_hdr_len)));
  638. } else {
  639. memcpy((char *)fc_trace, (void *)frame,
  640. min_t(u8, fc_trc_frame_len,
  641. (u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE)));
  642. }
  643. /* Store the actual received length */
  644. fc_buf->frame_len = fc_trc_frame_len;
  645. spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
  646. return 0;
  647. }
  648. /*
  649. * fnic_fc_ctlr_get_trace_data: Copy trace buffer to a memory file
  650. * Passed parameter:
  651. * @fnic_dbgfs_t: pointer to debugfs trace buffer
  652. * rdata_flag: 1 => Unformated file
  653. * 0 => formated file
  654. * Description:
  655. * This routine will copy the trace data to memory file with
  656. * proper formatting and also copy to another memory
  657. * file without formatting for further procesing.
  658. * Retrun Value:
  659. * Number of bytes that were dumped into fnic_dbgfs_t
  660. */
  661. int fnic_fc_trace_get_data(fnic_dbgfs_t *fnic_dbgfs_prt, u8 rdata_flag)
  662. {
  663. int rd_idx, wr_idx;
  664. unsigned long flags;
  665. int len = 0, j;
  666. struct fc_trace_hdr *tdata;
  667. char *fc_trace;
  668. spin_lock_irqsave(&fnic_fc_trace_lock, flags);
  669. if (fc_trace_entries.wr_idx == fc_trace_entries.rd_idx) {
  670. spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
  671. pr_info("fnic: Buffer is empty\n");
  672. return 0;
  673. }
  674. rd_idx = fc_trace_entries.rd_idx;
  675. wr_idx = fc_trace_entries.wr_idx;
  676. if (rdata_flag == 0) {
  677. len += snprintf(fnic_dbgfs_prt->buffer + len,
  678. (fnic_fc_trace_max_pages * PAGE_SIZE * 3) - len,
  679. "Time Stamp (UTC)\t\t"
  680. "Host No: F Type: len: FCoE_FRAME:\n");
  681. }
  682. while (rd_idx != wr_idx) {
  683. tdata = (struct fc_trace_hdr *)
  684. fc_trace_entries.page_offset[rd_idx];
  685. if (!tdata) {
  686. pr_info("fnic: Rd data is NULL\n");
  687. spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
  688. return 0;
  689. }
  690. if (rdata_flag == 0) {
  691. copy_and_format_trace_data(tdata,
  692. fnic_dbgfs_prt, &len, rdata_flag);
  693. } else {
  694. fc_trace = (char *)tdata;
  695. for (j = 0; j < FC_TRC_SIZE_BYTES; j++) {
  696. len += snprintf(fnic_dbgfs_prt->buffer + len,
  697. (fnic_fc_trace_max_pages * PAGE_SIZE * 3)
  698. - len, "%02x", fc_trace[j] & 0xff);
  699. } /* for loop */
  700. len += snprintf(fnic_dbgfs_prt->buffer + len,
  701. (fnic_fc_trace_max_pages * PAGE_SIZE * 3) - len,
  702. "\n");
  703. }
  704. rd_idx++;
  705. if (rd_idx > (fc_trace_max_entries - 1))
  706. rd_idx = 0;
  707. }
  708. spin_unlock_irqrestore(&fnic_fc_trace_lock, flags);
  709. return len;
  710. }
  711. /*
  712. * copy_and_format_trace_data: Copy formatted data to char * buffer
  713. * Passed Parameter:
  714. * @fc_trace_hdr_t: pointer to trace data
  715. * @fnic_dbgfs_t: pointer to debugfs trace buffer
  716. * @orig_len: pointer to len
  717. * rdata_flag: 0 => Formated file, 1 => Unformated file
  718. * Description:
  719. * This routine will format and copy the passed trace data
  720. * for formated file or unformated file accordingly.
  721. */
  722. void copy_and_format_trace_data(struct fc_trace_hdr *tdata,
  723. fnic_dbgfs_t *fnic_dbgfs_prt, int *orig_len,
  724. u8 rdata_flag)
  725. {
  726. struct tm tm;
  727. int j, i = 1, len;
  728. char *fc_trace, *fmt;
  729. int ethhdr_len = sizeof(struct ethhdr) - 1;
  730. int fcoehdr_len = sizeof(struct fcoe_hdr);
  731. int fchdr_len = sizeof(struct fc_frame_header);
  732. int max_size = fnic_fc_trace_max_pages * PAGE_SIZE * 3;
  733. tdata->frame_type = tdata->frame_type & 0x7F;
  734. len = *orig_len;
  735. time64_to_tm(tdata->time_stamp.tv_sec, 0, &tm);
  736. fmt = "%02d:%02d:%04ld %02d:%02d:%02d.%09lu ns%8x %c%8x\t";
  737. len += snprintf(fnic_dbgfs_prt->buffer + len,
  738. max_size - len,
  739. fmt,
  740. tm.tm_mon + 1, tm.tm_mday, tm.tm_year + 1900,
  741. tm.tm_hour, tm.tm_min, tm.tm_sec,
  742. tdata->time_stamp.tv_nsec, tdata->host_no,
  743. tdata->frame_type, tdata->frame_len);
  744. fc_trace = (char *)FC_TRACE_ADDRESS(tdata);
  745. for (j = 0; j < min_t(u8, tdata->frame_len,
  746. (u8)(FC_TRC_SIZE_BYTES - FC_TRC_HEADER_SIZE)); j++) {
  747. if (tdata->frame_type == FNIC_FC_LE) {
  748. len += snprintf(fnic_dbgfs_prt->buffer + len,
  749. max_size - len, "%c", fc_trace[j]);
  750. } else {
  751. len += snprintf(fnic_dbgfs_prt->buffer + len,
  752. max_size - len, "%02x", fc_trace[j] & 0xff);
  753. len += snprintf(fnic_dbgfs_prt->buffer + len,
  754. max_size - len, " ");
  755. if (j == ethhdr_len ||
  756. j == ethhdr_len + fcoehdr_len ||
  757. j == ethhdr_len + fcoehdr_len + fchdr_len ||
  758. (i > 3 && j%fchdr_len == 0)) {
  759. len += snprintf(fnic_dbgfs_prt->buffer
  760. + len, max_size - len,
  761. "\n\t\t\t\t\t\t\t\t");
  762. i++;
  763. }
  764. } /* end of else*/
  765. } /* End of for loop*/
  766. len += snprintf(fnic_dbgfs_prt->buffer + len,
  767. max_size - len, "\n");
  768. *orig_len = len;
  769. }