wlan_logging_sock_svc.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * Copyright (c) 2014-2019 The Linux Foundation. All rights reserved.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for
  5. * any purpose with or without fee is hereby granted, provided that the
  6. * above copyright notice and this permission notice appear in all
  7. * copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  10. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  11. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  12. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  16. * PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /******************************************************************************
  19. * wlan_logging_sock_svc.c
  20. *
  21. ******************************************************************************/
  22. #ifdef WLAN_LOGGING_SOCK_SVC_ENABLE
  23. #include <linux/vmalloc.h>
  24. #ifdef CONFIG_MCL
  25. #include <cds_api.h>
  26. #include <host_diag_core_event.h>
  27. #include "cds_utils.h"
  28. #include "csr_api.h"
  29. #include "wma.h"
  30. #include "ol_txrx_api.h"
  31. #include "pktlog_ac.h"
  32. #include <cdp_txrx_misc.h>
  33. #endif
  34. #include <wlan_logging_sock_svc.h>
  35. #include <linux/kthread.h>
  36. #include <qdf_time.h>
  37. #include <qdf_trace.h>
  38. #include <qdf_mc_timer.h>
  39. #include <qdf_timer.h>
  40. #include <qdf_lock.h>
  41. #include <wlan_ptt_sock_svc.h>
  42. #include <host_diag_core_event.h>
  43. #include "host_diag_core_log.h"
  44. #ifdef CNSS_GENL
  45. #include <net/cnss_nl.h>
  46. #endif
  47. #define MAX_NUM_PKT_LOG 32
  48. /**
  49. * struct tx_status - tx status
  50. * @tx_status_ok: successfully sent + acked
  51. * @tx_status_discard: discard - not sent (congestion control)
  52. * @tx_status_no_ack: no_ack - sent, but no ack
  53. * @tx_status_download_fail: download_fail -
  54. * the host could not deliver the tx frame to the target
  55. * @tx_status_peer_del: peer_del - tx completion for
  56. * alreay deleted peer used for HL case
  57. *
  58. * This enum has tx status types
  59. */
  60. enum tx_status {
  61. tx_status_ok,
  62. tx_status_discard,
  63. tx_status_no_ack,
  64. tx_status_download_fail,
  65. tx_status_peer_del,
  66. };
  67. #define LOGGING_TRACE(level, args ...) \
  68. QDF_TRACE(QDF_MODULE_ID_HDD, level, ## args)
  69. /* Global variables */
  70. #define ANI_NL_MSG_LOG_TYPE 89
  71. #define ANI_NL_MSG_READY_IND_TYPE 90
  72. #ifndef MAX_LOGMSG_COUNT
  73. #define MAX_LOGMSG_COUNT 256
  74. #endif
  75. #define MAX_LOGMSG_LENGTH 2048
  76. #define MAX_SKBMSG_LENGTH 4096
  77. #define MAX_PKTSTATS_LENGTH 2048
  78. #define MAX_PKTSTATS_BUFF 16
  79. #define HOST_LOG_DRIVER_MSG 0x001
  80. #define HOST_LOG_PER_PKT_STATS 0x002
  81. #define HOST_LOG_FW_FLUSH_COMPLETE 0x003
  82. #define DIAG_TYPE_LOGS 1
  83. #define PTT_MSG_DIAG_CMDS_TYPE 0x5050
  84. struct log_msg {
  85. struct list_head node;
  86. unsigned int radio;
  87. unsigned int index;
  88. /* indicates the current filled log length in logbuf */
  89. unsigned int filled_length;
  90. /*
  91. * Buf to hold the log msg
  92. * tAniHdr + log
  93. */
  94. char logbuf[MAX_LOGMSG_LENGTH];
  95. };
  96. /**
  97. * struct packet_dump - This data structure contains the
  98. * Tx/Rx packet stats
  99. * @status: Status
  100. * @type: Type
  101. * @driver_ts: driver timestamp
  102. * @fw_ts: fw timestamp
  103. */
  104. struct packet_dump {
  105. unsigned char status;
  106. unsigned char type;
  107. uint32_t driver_ts;
  108. uint16_t fw_ts;
  109. } __attribute__((__packed__));
  110. /**
  111. * struct pkt_stats_msg - This data structure contains the
  112. * pkt stats node for link list
  113. * @node: LinkList node
  114. * @node: Pointer to skb
  115. */
  116. struct pkt_stats_msg {
  117. struct list_head node;
  118. struct sk_buff *skb;
  119. };
  120. #define MAX_FLUSH_TIMER_PERIOD_VALUE 3600000 /* maximum of 1 hour (in ms) */
  121. struct wlan_logging {
  122. /* Log Fatal and ERROR to console */
  123. bool log_to_console;
  124. /* Number of buffers to be used for logging */
  125. uint32_t num_buf;
  126. uint32_t buffer_length;
  127. /* Lock to synchronize access to shared logging resource */
  128. spinlock_t spin_lock;
  129. /* Holds the free node which can be used for filling logs */
  130. struct list_head free_list;
  131. /* Holds the filled nodes which needs to be indicated to APP */
  132. struct list_head filled_list;
  133. /* Wait queue for Logger thread */
  134. wait_queue_head_t wait_queue;
  135. /* Logger thread */
  136. struct task_struct *thread;
  137. /* Logging thread sets this variable on exit */
  138. struct completion shutdown_comp;
  139. /* Indicates to logger thread to exit */
  140. bool exit;
  141. /* Holds number of dropped logs */
  142. unsigned int drop_count;
  143. /* current logbuf to which the log will be filled to */
  144. struct log_msg *pcur_node;
  145. /* Event flag used for wakeup and post indication*/
  146. unsigned long eventFlag;
  147. /* Indicates logger thread is activated */
  148. bool is_active;
  149. /* Flush completion check */
  150. bool is_flush_complete;
  151. /* paramaters for pkt stats */
  152. struct list_head pkt_stat_free_list;
  153. struct list_head pkt_stat_filled_list;
  154. struct pkt_stats_msg *pkt_stats_pcur_node;
  155. unsigned int pkt_stat_drop_cnt;
  156. spinlock_t pkt_stats_lock;
  157. unsigned int pkt_stats_msg_idx;
  158. qdf_timer_t flush_timer;
  159. bool is_flush_timer_initialized;
  160. uint32_t flush_timer_period;
  161. qdf_spinlock_t flush_timer_lock;
  162. };
  163. static struct wlan_logging gwlan_logging;
  164. static struct pkt_stats_msg *gpkt_stats_buffers;
  165. #ifdef WLAN_LOGGING_BUFFERS_DYNAMICALLY
  166. static struct log_msg *gplog_msg;
  167. static inline QDF_STATUS allocate_log_msg_buffer(void)
  168. {
  169. gplog_msg = vzalloc(MAX_LOGMSG_COUNT * sizeof(*gplog_msg));
  170. return gplog_msg ? QDF_STATUS_SUCCESS : QDF_STATUS_E_NOMEM;
  171. }
  172. static inline void free_log_msg_buffer(void)
  173. {
  174. vfree(gplog_msg);
  175. gplog_msg = NULL;
  176. }
  177. #else
  178. static struct log_msg gplog_msg[MAX_LOGMSG_COUNT];
  179. static inline QDF_STATUS allocate_log_msg_buffer(void)
  180. {
  181. return QDF_STATUS_SUCCESS;
  182. }
  183. static inline void free_log_msg_buffer(void)
  184. { }
  185. #endif
  186. /* Need to call this with spin_lock acquired */
  187. static int wlan_queue_logmsg_for_app(void)
  188. {
  189. char *ptr;
  190. int ret = 0;
  191. ptr = &gwlan_logging.pcur_node->logbuf[sizeof(tAniHdr)];
  192. ptr[gwlan_logging.pcur_node->filled_length] = '\0';
  193. *(unsigned short *)(gwlan_logging.pcur_node->logbuf) =
  194. ANI_NL_MSG_LOG_TYPE;
  195. *(unsigned short *)(gwlan_logging.pcur_node->logbuf + 2) =
  196. gwlan_logging.pcur_node->filled_length;
  197. list_add_tail(&gwlan_logging.pcur_node->node,
  198. &gwlan_logging.filled_list);
  199. if (!list_empty(&gwlan_logging.free_list)) {
  200. /* Get buffer from free list */
  201. gwlan_logging.pcur_node =
  202. (struct log_msg *)(gwlan_logging.free_list.next);
  203. list_del_init(gwlan_logging.free_list.next);
  204. } else if (!list_empty(&gwlan_logging.filled_list)) {
  205. /* Get buffer from filled list */
  206. /* This condition will drop the packet from being
  207. * indicated to app
  208. */
  209. gwlan_logging.pcur_node =
  210. (struct log_msg *)(gwlan_logging.filled_list.next);
  211. ++gwlan_logging.drop_count;
  212. list_del_init(gwlan_logging.filled_list.next);
  213. ret = 1;
  214. }
  215. /* Reset the current node values */
  216. gwlan_logging.pcur_node->filled_length = 0;
  217. return ret;
  218. }
  219. static const char *current_process_name(void)
  220. {
  221. if (in_irq())
  222. return "irq";
  223. if (in_softirq())
  224. return "soft_irq";
  225. return current->comm;
  226. }
  227. /**
  228. * wlan_add_user_log_time_stamp() - populate firmware and kernel timestamps
  229. * @tbuf: Pointer to time stamp buffer
  230. * @tbuf_sz: Time buffer size
  231. * @ts: Time stamp value
  232. *
  233. * For adrastea time stamp is QTIMER raw tick which will be used by cnss_diag
  234. * to convert it into user visible time stamp. In adrstea FW also uses QTIMER
  235. * raw ticks which is needed to synchronize host and fw log time stamps
  236. *
  237. * Also add logcat timestamp so that driver logs and
  238. * logcat logs can be co-related
  239. *
  240. * For discrete solution e.g rome use system tick and convert it into
  241. * seconds.milli seconds
  242. *
  243. * Return: number of characters written in target buffer not including
  244. * trailing '/0'
  245. */
  246. static int wlan_add_user_log_time_stamp(char *tbuf, size_t tbuf_sz, uint64_t ts)
  247. {
  248. char time_buf[20];
  249. qdf_get_time_of_the_day_in_hr_min_sec_usec(time_buf, sizeof(time_buf));
  250. return scnprintf(tbuf, tbuf_sz, "[%.16s][0x%llx]%s",
  251. current_process_name(), ts, time_buf);
  252. }
  253. #ifdef WLAN_MAX_LOGS_PER_SEC
  254. static qdf_time_t __log_window_end_ticks;
  255. static qdf_atomic_t __log_window_count;
  256. /**
  257. * assert_on_excessive_logging() - Check for and panic on excessive logging
  258. *
  259. * Track logging count using a quasi-tumbling window, 1 second long. If the max
  260. * logging count for a given window is exceeded, panic.
  261. *
  262. * Return: None
  263. */
  264. static void assert_on_excessive_logging(void)
  265. {
  266. qdf_time_t now = qdf_system_ticks();
  267. /*
  268. * If 'now' is more recent than the end of the window, reset.
  269. *
  270. * Note: This is not thread safe, and can result in more than one reset.
  271. * For our purposes, this is fine.
  272. */
  273. if (qdf_system_time_after(now, __log_window_end_ticks)) {
  274. __log_window_end_ticks = now + qdf_system_ticks_per_sec;
  275. qdf_atomic_set(&__log_window_count, 0);
  276. }
  277. /* this _is_ thread safe, and results in at most one panic */
  278. if (qdf_atomic_inc_return(&__log_window_count) == WLAN_MAX_LOGS_PER_SEC)
  279. QDF_DEBUG_PANIC("Exceeded %d logs per second",
  280. WLAN_MAX_LOGS_PER_SEC);
  281. }
  282. #else
  283. static inline void assert_on_excessive_logging(void) { }
  284. #endif /* WLAN_MAX_LOGS_PER_SEC */
  285. static inline void
  286. log_to_console(QDF_TRACE_LEVEL level, const char *timestamp, const char *msg)
  287. {
  288. switch (level) {
  289. case QDF_TRACE_LEVEL_FATAL:
  290. pr_alert("%s %s\n", timestamp, msg);
  291. assert_on_excessive_logging();
  292. break;
  293. case QDF_TRACE_LEVEL_ERROR:
  294. pr_err("%s %s\n", timestamp, msg);
  295. assert_on_excessive_logging();
  296. break;
  297. case QDF_TRACE_LEVEL_WARN:
  298. pr_warn("%s %s\n", timestamp, msg);
  299. assert_on_excessive_logging();
  300. break;
  301. case QDF_TRACE_LEVEL_INFO:
  302. pr_info("%s %s\n", timestamp, msg);
  303. assert_on_excessive_logging();
  304. break;
  305. case QDF_TRACE_LEVEL_INFO_HIGH:
  306. case QDF_TRACE_LEVEL_INFO_MED:
  307. case QDF_TRACE_LEVEL_INFO_LOW:
  308. case QDF_TRACE_LEVEL_DEBUG:
  309. default:
  310. /* these levels should not be logged to console */
  311. break;
  312. }
  313. }
  314. int wlan_log_to_user(QDF_TRACE_LEVEL log_level, char *to_be_sent, int length)
  315. {
  316. char *ptr;
  317. char tbuf[60];
  318. int tlen;
  319. int total_log_len;
  320. unsigned int *pfilled_length;
  321. bool wake_up_thread = false;
  322. unsigned long flags;
  323. uint64_t ts;
  324. /* Add the current time stamp */
  325. ts = qdf_get_log_timestamp();
  326. tlen = wlan_add_user_log_time_stamp(tbuf, sizeof(tbuf), ts);
  327. /* if logging isn't up yet, just dump to dmesg */
  328. if (!gwlan_logging.is_active) {
  329. log_to_console(log_level, tbuf, to_be_sent);
  330. return 0;
  331. }
  332. /* 1+1 indicate '\n'+'\0' */
  333. total_log_len = length + tlen + 1 + 1;
  334. spin_lock_irqsave(&gwlan_logging.spin_lock, flags);
  335. /* wlan logging svc resources are not yet initialized */
  336. if (!gwlan_logging.pcur_node) {
  337. spin_unlock_irqrestore(&gwlan_logging.spin_lock, flags);
  338. return -EIO;
  339. }
  340. pfilled_length = &gwlan_logging.pcur_node->filled_length;
  341. /* Check if we can accommodate more log into current node/buffer */
  342. if ((MAX_LOGMSG_LENGTH - (*pfilled_length +
  343. sizeof(tAniNlHdr))) < total_log_len) {
  344. wake_up_thread = true;
  345. wlan_queue_logmsg_for_app();
  346. pfilled_length = &gwlan_logging.pcur_node->filled_length;
  347. }
  348. ptr = &gwlan_logging.pcur_node->logbuf[sizeof(tAniHdr)];
  349. if (unlikely(MAX_LOGMSG_LENGTH < (sizeof(tAniNlHdr) + total_log_len))) {
  350. /*
  351. * Assumption here is that we receive logs which is less than
  352. * MAX_LOGMSG_LENGTH, where we can accommodate the
  353. * tAniNlHdr + [context][timestamp] + log
  354. * If log length is over MAX_LOGMSG_LENGTH,
  355. * the overflow part will be discarded.
  356. */
  357. length = MAX_LOGMSG_LENGTH - sizeof(tAniNlHdr) - tlen - 2;
  358. /*
  359. * QDF_ASSERT if complete log was not accommodated into
  360. * the available buffer.
  361. */
  362. QDF_ASSERT(0);
  363. }
  364. memcpy(&ptr[*pfilled_length], tbuf, tlen);
  365. memcpy(&ptr[*pfilled_length + tlen], to_be_sent, length);
  366. *pfilled_length += tlen + length;
  367. ptr[*pfilled_length] = '\n';
  368. *pfilled_length += 1;
  369. spin_unlock_irqrestore(&gwlan_logging.spin_lock, flags);
  370. /* Wakeup logger thread */
  371. if (wake_up_thread) {
  372. set_bit(HOST_LOG_DRIVER_MSG, &gwlan_logging.eventFlag);
  373. wake_up_interruptible(&gwlan_logging.wait_queue);
  374. }
  375. if (gwlan_logging.log_to_console)
  376. log_to_console(log_level, tbuf, to_be_sent);
  377. return 0;
  378. }
  379. /**
  380. * nl_srv_bcast_host_logs() - Wrapper to send bcast msgs to host logs mcast grp
  381. * @skb: sk buffer pointer
  382. *
  383. * Sends the bcast message to host logs multicast group with generic nl socket
  384. * if CNSS_GENL is enabled. Else, use the legacy netlink socket to send.
  385. *
  386. * Return: zero on success, error code otherwise
  387. */
  388. #ifdef CNSS_GENL
  389. static int nl_srv_bcast_host_logs(struct sk_buff *skb)
  390. {
  391. return nl_srv_bcast(skb, CLD80211_MCGRP_HOST_LOGS, ANI_NL_MSG_LOG);
  392. }
  393. #else
  394. static int nl_srv_bcast_host_logs(struct sk_buff *skb)
  395. {
  396. return nl_srv_bcast(skb);
  397. }
  398. #endif
  399. #ifndef REMOVE_PKT_LOG
  400. /**
  401. * pkt_stats_fill_headers() - This function adds headers to skb
  402. * @skb: skb to which headers need to be added
  403. *
  404. * Return: 0 on success or Errno on failure
  405. */
  406. static int pkt_stats_fill_headers(struct sk_buff *skb)
  407. {
  408. struct host_log_pktlog_info cds_pktlog;
  409. int cds_pkt_size = sizeof(struct host_log_pktlog_info);
  410. tAniNlHdr msg_header;
  411. int extra_header_len, nl_payload_len;
  412. static int nlmsg_seq;
  413. int diag_type;
  414. qdf_mem_zero(&cds_pktlog, cds_pkt_size);
  415. cds_pktlog.version = VERSION_LOG_WLAN_PKT_LOG_INFO_C;
  416. cds_pktlog.buf_len = skb->len;
  417. cds_pktlog.seq_no = gwlan_logging.pkt_stats_msg_idx++;
  418. host_diag_log_set_code(&cds_pktlog, LOG_WLAN_PKT_LOG_INFO_C);
  419. host_diag_log_set_length(&cds_pktlog.log_hdr, skb->len +
  420. cds_pkt_size);
  421. if (unlikely(skb_headroom(skb) < cds_pkt_size)) {
  422. pr_err("VPKT [%d]: Insufficient headroom, head[%pK], data[%pK], req[%zu]",
  423. __LINE__, skb->head, skb->data, sizeof(msg_header));
  424. return -EIO;
  425. }
  426. qdf_mem_copy(skb_push(skb, cds_pkt_size),
  427. &cds_pktlog, cds_pkt_size);
  428. if (unlikely(skb_headroom(skb) < sizeof(int))) {
  429. pr_err("VPKT [%d]: Insufficient headroom, head[%pK], data[%pK], req[%zu]",
  430. __LINE__, skb->head, skb->data, sizeof(int));
  431. return -EIO;
  432. }
  433. diag_type = DIAG_TYPE_LOGS;
  434. qdf_mem_copy(skb_push(skb, sizeof(int)), &diag_type, sizeof(int));
  435. extra_header_len = sizeof(msg_header.radio) + sizeof(tAniHdr) +
  436. sizeof(struct nlmsghdr);
  437. nl_payload_len = extra_header_len + skb->len;
  438. msg_header.nlh.nlmsg_type = ANI_NL_MSG_PUMAC;
  439. msg_header.nlh.nlmsg_len = nl_payload_len;
  440. msg_header.nlh.nlmsg_flags = NLM_F_REQUEST;
  441. msg_header.nlh.nlmsg_pid = 0;
  442. msg_header.nlh.nlmsg_seq = nlmsg_seq++;
  443. msg_header.radio = 0;
  444. msg_header.wmsg.type = PTT_MSG_DIAG_CMDS_TYPE;
  445. msg_header.wmsg.length = cpu_to_be16(skb->len);
  446. if (unlikely(skb_headroom(skb) < sizeof(msg_header))) {
  447. pr_err("VPKT [%d]: Insufficient headroom, head[%pK], data[%pK], req[%zu]",
  448. __LINE__, skb->head, skb->data, sizeof(msg_header));
  449. return -EIO;
  450. }
  451. qdf_mem_copy(skb_push(skb, sizeof(msg_header)), &msg_header,
  452. sizeof(msg_header));
  453. return 0;
  454. }
  455. /**
  456. * nl_srv_bcast_diag() - Wrapper to send bcast msgs to diag events mcast grp
  457. * @skb: sk buffer pointer
  458. *
  459. * Sends the bcast message to diag events multicast group with generic nl socket
  460. * if CNSS_GENL is enabled. Else, use the legacy netlink socket to send.
  461. *
  462. * Return: zero on success, error code otherwise
  463. */
  464. static int nl_srv_bcast_diag(struct sk_buff *skb)
  465. {
  466. #ifdef CNSS_GENL
  467. return nl_srv_bcast(skb, CLD80211_MCGRP_DIAG_EVENTS, ANI_NL_MSG_PUMAC);
  468. #else
  469. return nl_srv_bcast(skb);
  470. #endif
  471. }
  472. /**
  473. * pktlog_send_per_pkt_stats_to_user() - This function is used to send the per
  474. * packet statistics to the user
  475. *
  476. * This function is used to send the per packet statistics to the user
  477. *
  478. * Return: Success if the message is posted to user
  479. */
  480. static int pktlog_send_per_pkt_stats_to_user(void)
  481. {
  482. int ret = -1;
  483. struct pkt_stats_msg *pstats_msg;
  484. unsigned long flags;
  485. struct sk_buff *skb_new = NULL;
  486. static int rate_limit;
  487. bool free_old_skb = false;
  488. while (!list_empty(&gwlan_logging.pkt_stat_filled_list)
  489. && !gwlan_logging.exit) {
  490. skb_new = dev_alloc_skb(MAX_SKBMSG_LENGTH);
  491. if (!skb_new) {
  492. if (!rate_limit) {
  493. pr_err("%s: dev_alloc_skb() failed for msg size[%d] drop count = %u\n",
  494. __func__, MAX_SKBMSG_LENGTH,
  495. gwlan_logging.drop_count);
  496. }
  497. rate_limit = 1;
  498. ret = -ENOMEM;
  499. break;
  500. }
  501. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, flags);
  502. pstats_msg = (struct pkt_stats_msg *)
  503. (gwlan_logging.pkt_stat_filled_list.next);
  504. list_del_init(gwlan_logging.pkt_stat_filled_list.next);
  505. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, flags);
  506. ret = pkt_stats_fill_headers(pstats_msg->skb);
  507. if (ret < 0) {
  508. pr_err("%s failed to fill headers %d\n", __func__, ret);
  509. free_old_skb = true;
  510. goto err;
  511. }
  512. ret = nl_srv_bcast_diag(pstats_msg->skb);
  513. if (ret < 0) {
  514. pr_info("%s: Send Failed %d drop_count = %u\n",
  515. __func__, ret,
  516. ++gwlan_logging.pkt_stat_drop_cnt);
  517. } else {
  518. ret = 0;
  519. }
  520. err:
  521. /*
  522. * Free old skb in case or error before assigning new skb
  523. * to the free list.
  524. */
  525. if (free_old_skb)
  526. dev_kfree_skb(pstats_msg->skb);
  527. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, flags);
  528. pstats_msg->skb = skb_new;
  529. list_add_tail(&pstats_msg->node,
  530. &gwlan_logging.pkt_stat_free_list);
  531. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, flags);
  532. ret = 0;
  533. }
  534. return ret;
  535. }
  536. #else
  537. static inline
  538. int pktlog_send_per_pkt_stats_to_user(void)
  539. {
  540. return 0;
  541. }
  542. #endif
  543. static int send_filled_buffers_to_user(void)
  544. {
  545. int ret = -1;
  546. struct log_msg *plog_msg;
  547. int payload_len;
  548. int tot_msg_len;
  549. tAniNlHdr *wnl;
  550. struct sk_buff *skb = NULL;
  551. struct nlmsghdr *nlh;
  552. static int nlmsg_seq;
  553. unsigned long flags;
  554. static int rate_limit;
  555. while (!list_empty(&gwlan_logging.filled_list)
  556. && !gwlan_logging.exit) {
  557. skb = dev_alloc_skb(MAX_LOGMSG_LENGTH);
  558. if (!skb) {
  559. if (!rate_limit) {
  560. pr_err
  561. ("%s: dev_alloc_skb() failed for msg size[%d] drop count = %u\n",
  562. __func__, MAX_LOGMSG_LENGTH,
  563. gwlan_logging.drop_count);
  564. }
  565. rate_limit = 1;
  566. ret = -ENOMEM;
  567. break;
  568. }
  569. rate_limit = 0;
  570. spin_lock_irqsave(&gwlan_logging.spin_lock, flags);
  571. plog_msg = (struct log_msg *)
  572. (gwlan_logging.filled_list.next);
  573. list_del_init(gwlan_logging.filled_list.next);
  574. spin_unlock_irqrestore(&gwlan_logging.spin_lock, flags);
  575. /* 4 extra bytes for the radio idx */
  576. payload_len = plog_msg->filled_length +
  577. sizeof(wnl->radio) + sizeof(tAniHdr);
  578. tot_msg_len = NLMSG_SPACE(payload_len);
  579. nlh = nlmsg_put(skb, 0, nlmsg_seq++,
  580. ANI_NL_MSG_LOG, payload_len, NLM_F_REQUEST);
  581. if (!nlh) {
  582. spin_lock_irqsave(&gwlan_logging.spin_lock, flags);
  583. list_add_tail(&plog_msg->node,
  584. &gwlan_logging.free_list);
  585. spin_unlock_irqrestore(&gwlan_logging.spin_lock, flags);
  586. pr_err("%s: drop_count = %u\n", __func__,
  587. ++gwlan_logging.drop_count);
  588. pr_err("%s: nlmsg_put() failed for msg size[%d]\n",
  589. __func__, tot_msg_len);
  590. dev_kfree_skb(skb);
  591. skb = NULL;
  592. ret = -EINVAL;
  593. continue;
  594. }
  595. wnl = (tAniNlHdr *) nlh;
  596. wnl->radio = plog_msg->radio;
  597. memcpy(&wnl->wmsg, plog_msg->logbuf,
  598. plog_msg->filled_length + sizeof(tAniHdr));
  599. spin_lock_irqsave(&gwlan_logging.spin_lock, flags);
  600. list_add_tail(&plog_msg->node, &gwlan_logging.free_list);
  601. spin_unlock_irqrestore(&gwlan_logging.spin_lock, flags);
  602. ret = nl_srv_bcast_host_logs(skb);
  603. /* print every 64th drop count */
  604. if (ret < 0 && (!(gwlan_logging.drop_count % 0x40))) {
  605. pr_err("%s: Send Failed %d drop_count = %u\n",
  606. __func__, ret, ++gwlan_logging.drop_count);
  607. }
  608. }
  609. return ret;
  610. }
  611. #ifdef FEATURE_WLAN_DIAG_SUPPORT
  612. /**
  613. * wlan_report_log_completion() - Report bug report completion to userspace
  614. * @is_fatal: Type of event, fatal or not
  615. * @indicator: Source of bug report, framework/host/firmware
  616. * @reason_code: Reason for triggering bug report
  617. * @ring_id: Ring id of logging entities
  618. *
  619. * This function is used to report the bug report completion to userspace
  620. *
  621. * Return: None
  622. */
  623. void wlan_report_log_completion(uint32_t is_fatal,
  624. uint32_t indicator,
  625. uint32_t reason_code,
  626. uint8_t ring_id)
  627. {
  628. WLAN_HOST_DIAG_EVENT_DEF(wlan_diag_event,
  629. struct host_event_wlan_log_complete);
  630. wlan_diag_event.is_fatal = is_fatal;
  631. wlan_diag_event.indicator = indicator;
  632. wlan_diag_event.reason_code = reason_code;
  633. wlan_diag_event.reserved = ring_id;
  634. WLAN_HOST_DIAG_EVENT_REPORT(&wlan_diag_event, EVENT_WLAN_LOG_COMPLETE);
  635. }
  636. #endif
  637. #ifdef FEATURE_WLAN_DIAG_SUPPORT
  638. /**
  639. * send_flush_completion_to_user() - Indicate flush completion to the user
  640. * @ring_id: Ring id of logging entities
  641. *
  642. * This function is used to send the flush completion message to user space
  643. *
  644. * Return: None
  645. */
  646. static void send_flush_completion_to_user(uint8_t ring_id)
  647. {
  648. uint32_t is_fatal, indicator, reason_code;
  649. bool recovery_needed;
  650. cds_get_and_reset_log_completion(&is_fatal,
  651. &indicator, &reason_code, &recovery_needed);
  652. /* Error on purpose, so that it will get logged in the kmsg */
  653. LOGGING_TRACE(QDF_TRACE_LEVEL_DEBUG,
  654. "%s: Sending flush done to userspace reason code %d",
  655. __func__, reason_code);
  656. wlan_report_log_completion(is_fatal, indicator, reason_code, ring_id);
  657. if (recovery_needed)
  658. cds_trigger_recovery(QDF_REASON_UNSPECIFIED);
  659. }
  660. #endif
  661. static void setup_flush_timer(void)
  662. {
  663. qdf_spin_lock(&gwlan_logging.flush_timer_lock);
  664. if (!gwlan_logging.is_flush_timer_initialized ||
  665. (gwlan_logging.flush_timer_period == 0)) {
  666. qdf_spin_unlock(&gwlan_logging.flush_timer_lock);
  667. return;
  668. }
  669. qdf_timer_mod(&gwlan_logging.flush_timer,
  670. gwlan_logging.flush_timer_period);
  671. qdf_spin_unlock(&gwlan_logging.flush_timer_lock);
  672. }
  673. /**
  674. * wlan_logging_thread() - The WLAN Logger thread
  675. * @Arg - pointer to the HDD context
  676. *
  677. * This thread logs log message to App registered for the logs.
  678. */
  679. static int wlan_logging_thread(void *Arg)
  680. {
  681. int ret_wait_status = 0;
  682. int ret = 0;
  683. unsigned long flags;
  684. while (!gwlan_logging.exit) {
  685. setup_flush_timer();
  686. ret_wait_status =
  687. wait_event_interruptible(gwlan_logging.wait_queue,
  688. (!list_empty
  689. (&gwlan_logging.filled_list)
  690. || test_bit(
  691. HOST_LOG_DRIVER_MSG,
  692. &gwlan_logging.eventFlag)
  693. || test_bit(
  694. HOST_LOG_PER_PKT_STATS,
  695. &gwlan_logging.eventFlag)
  696. || test_bit(
  697. HOST_LOG_FW_FLUSH_COMPLETE,
  698. &gwlan_logging.eventFlag)
  699. || gwlan_logging.exit));
  700. if (ret_wait_status == -ERESTARTSYS) {
  701. pr_err
  702. ("%s: wait_event_interruptible returned -ERESTARTSYS",
  703. __func__);
  704. break;
  705. }
  706. if (gwlan_logging.exit)
  707. break;
  708. if (test_and_clear_bit(HOST_LOG_DRIVER_MSG,
  709. &gwlan_logging.eventFlag)) {
  710. ret = send_filled_buffers_to_user();
  711. if (-ENOMEM == ret)
  712. msleep(200);
  713. #ifdef FEATURE_WLAN_DIAG_SUPPORT
  714. if (WLAN_LOG_INDICATOR_HOST_ONLY ==
  715. cds_get_log_indicator()) {
  716. send_flush_completion_to_user(
  717. RING_ID_DRIVER_DEBUG);
  718. }
  719. #endif
  720. }
  721. if (test_and_clear_bit(HOST_LOG_PER_PKT_STATS,
  722. &gwlan_logging.eventFlag)) {
  723. ret = pktlog_send_per_pkt_stats_to_user();
  724. if (-ENOMEM == ret)
  725. msleep(200);
  726. }
  727. if (test_and_clear_bit(HOST_LOG_FW_FLUSH_COMPLETE,
  728. &gwlan_logging.eventFlag)) {
  729. /* Flush bit could have been set while we were mid
  730. * way in the logging thread. So, need to check other
  731. * buffers like log messages, per packet stats again
  732. * to flush any residual data in them
  733. */
  734. if (gwlan_logging.is_flush_complete == true) {
  735. gwlan_logging.is_flush_complete = false;
  736. #ifdef FEATURE_WLAN_DIAG_SUPPORT
  737. send_flush_completion_to_user(
  738. RING_ID_DRIVER_DEBUG);
  739. #endif
  740. } else {
  741. gwlan_logging.is_flush_complete = true;
  742. /* Flush all current host logs*/
  743. spin_lock_irqsave(&gwlan_logging.spin_lock,
  744. flags);
  745. wlan_queue_logmsg_for_app();
  746. spin_unlock_irqrestore(&gwlan_logging.spin_lock,
  747. flags);
  748. set_bit(HOST_LOG_DRIVER_MSG,
  749. &gwlan_logging.eventFlag);
  750. set_bit(HOST_LOG_PER_PKT_STATS,
  751. &gwlan_logging.eventFlag);
  752. set_bit(HOST_LOG_FW_FLUSH_COMPLETE,
  753. &gwlan_logging.eventFlag);
  754. wake_up_interruptible(
  755. &gwlan_logging.wait_queue);
  756. }
  757. }
  758. }
  759. complete_and_exit(&gwlan_logging.shutdown_comp, 0);
  760. return 0;
  761. }
  762. void wlan_logging_set_active(bool active)
  763. {
  764. gwlan_logging.is_active = active;
  765. }
  766. void wlan_logging_set_log_to_console(bool log_to_console)
  767. {
  768. gwlan_logging.log_to_console = log_to_console;
  769. }
  770. static void flush_log_buffers_timer(void *dummy)
  771. {
  772. wlan_flush_host_logs_for_fatal();
  773. }
  774. int wlan_logging_set_flush_timer(uint32_t milliseconds)
  775. {
  776. if (milliseconds > MAX_FLUSH_TIMER_PERIOD_VALUE) {
  777. QDF_TRACE_ERROR(QDF_MODULE_ID_QDF,
  778. "ERROR! value should be (0 - %d)\n",
  779. MAX_FLUSH_TIMER_PERIOD_VALUE);
  780. return -EINVAL;
  781. }
  782. if (!gwlan_logging.is_active) {
  783. QDF_TRACE_ERROR(QDF_MODULE_ID_QDF,
  784. "WLAN-Logging not active");
  785. return -EINVAL;
  786. }
  787. qdf_spin_lock(&gwlan_logging.flush_timer_lock);
  788. if (!gwlan_logging.is_flush_timer_initialized) {
  789. qdf_spin_unlock(&gwlan_logging.flush_timer_lock);
  790. return -EINVAL;
  791. }
  792. gwlan_logging.flush_timer_period = milliseconds;
  793. if (milliseconds) {
  794. qdf_timer_mod(&gwlan_logging.flush_timer,
  795. gwlan_logging.flush_timer_period);
  796. }
  797. qdf_spin_unlock(&gwlan_logging.flush_timer_lock);
  798. return 0;
  799. }
  800. static void flush_timer_init(void)
  801. {
  802. qdf_spinlock_create(&gwlan_logging.flush_timer_lock);
  803. qdf_timer_init(NULL, &gwlan_logging.flush_timer,
  804. flush_log_buffers_timer, NULL,
  805. QDF_TIMER_TYPE_SW);
  806. gwlan_logging.is_flush_timer_initialized = true;
  807. gwlan_logging.flush_timer_period = 0;
  808. }
  809. int wlan_logging_sock_init_svc(void)
  810. {
  811. int i = 0, j, pkt_stats_size;
  812. unsigned long irq_flag;
  813. flush_timer_init();
  814. spin_lock_init(&gwlan_logging.spin_lock);
  815. spin_lock_init(&gwlan_logging.pkt_stats_lock);
  816. gwlan_logging.log_to_console = false;
  817. gwlan_logging.num_buf = MAX_LOGMSG_COUNT;
  818. gwlan_logging.buffer_length = MAX_LOGMSG_LENGTH;
  819. if (allocate_log_msg_buffer() != QDF_STATUS_SUCCESS) {
  820. pr_err("%s: Could not allocate memory for log_msg\n",
  821. __func__);
  822. return -ENOMEM;
  823. }
  824. spin_lock_irqsave(&gwlan_logging.spin_lock, irq_flag);
  825. INIT_LIST_HEAD(&gwlan_logging.free_list);
  826. INIT_LIST_HEAD(&gwlan_logging.filled_list);
  827. for (i = 0; i < gwlan_logging.num_buf; i++) {
  828. list_add(&gplog_msg[i].node, &gwlan_logging.free_list);
  829. gplog_msg[i].index = i;
  830. }
  831. gwlan_logging.pcur_node = (struct log_msg *)
  832. (gwlan_logging.free_list.next);
  833. list_del_init(gwlan_logging.free_list.next);
  834. spin_unlock_irqrestore(&gwlan_logging.spin_lock, irq_flag);
  835. /* Initialize the pktStats data structure here */
  836. pkt_stats_size = sizeof(struct pkt_stats_msg);
  837. gpkt_stats_buffers = vmalloc(MAX_PKTSTATS_BUFF * pkt_stats_size);
  838. if (!gpkt_stats_buffers) {
  839. pr_err("%s: Could not allocate memory for Pkt stats\n",
  840. __func__);
  841. goto err1;
  842. }
  843. qdf_mem_zero(gpkt_stats_buffers,
  844. MAX_PKTSTATS_BUFF * pkt_stats_size);
  845. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  846. gwlan_logging.pkt_stats_msg_idx = 0;
  847. INIT_LIST_HEAD(&gwlan_logging.pkt_stat_free_list);
  848. INIT_LIST_HEAD(&gwlan_logging.pkt_stat_filled_list);
  849. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  850. for (i = 0; i < MAX_PKTSTATS_BUFF; i++) {
  851. gpkt_stats_buffers[i].skb = dev_alloc_skb(MAX_PKTSTATS_LENGTH);
  852. if (!gpkt_stats_buffers[i].skb) {
  853. pr_err("%s: Memory alloc failed for skb", __func__);
  854. /* free previously allocated skb and return */
  855. for (j = 0; j < i ; j++)
  856. dev_kfree_skb(gpkt_stats_buffers[j].skb);
  857. goto err2;
  858. }
  859. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  860. list_add(&gpkt_stats_buffers[i].node,
  861. &gwlan_logging.pkt_stat_free_list);
  862. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  863. }
  864. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  865. gwlan_logging.pkt_stats_pcur_node = (struct pkt_stats_msg *)
  866. (gwlan_logging.pkt_stat_free_list.next);
  867. list_del_init(gwlan_logging.pkt_stat_free_list.next);
  868. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  869. /* Pkt Stats intialization done */
  870. init_waitqueue_head(&gwlan_logging.wait_queue);
  871. gwlan_logging.exit = false;
  872. clear_bit(HOST_LOG_DRIVER_MSG, &gwlan_logging.eventFlag);
  873. clear_bit(HOST_LOG_PER_PKT_STATS, &gwlan_logging.eventFlag);
  874. clear_bit(HOST_LOG_FW_FLUSH_COMPLETE, &gwlan_logging.eventFlag);
  875. init_completion(&gwlan_logging.shutdown_comp);
  876. gwlan_logging.thread = kthread_create(wlan_logging_thread, NULL,
  877. "wlan_logging_thread");
  878. if (IS_ERR(gwlan_logging.thread)) {
  879. pr_err("%s: Could not Create LogMsg Thread Controller",
  880. __func__);
  881. goto err3;
  882. }
  883. wake_up_process(gwlan_logging.thread);
  884. gwlan_logging.is_active = true;
  885. gwlan_logging.is_flush_complete = false;
  886. return 0;
  887. err3:
  888. for (i = 0; i < MAX_PKTSTATS_BUFF; i++) {
  889. if (gpkt_stats_buffers[i].skb)
  890. dev_kfree_skb(gpkt_stats_buffers[i].skb);
  891. }
  892. err2:
  893. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  894. gwlan_logging.pkt_stats_pcur_node = NULL;
  895. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  896. vfree(gpkt_stats_buffers);
  897. gpkt_stats_buffers = NULL;
  898. err1:
  899. spin_lock_irqsave(&gwlan_logging.spin_lock, irq_flag);
  900. gwlan_logging.pcur_node = NULL;
  901. spin_unlock_irqrestore(&gwlan_logging.spin_lock, irq_flag);
  902. free_log_msg_buffer();
  903. return -ENOMEM;
  904. }
  905. static void flush_timer_deinit(void)
  906. {
  907. gwlan_logging.is_flush_timer_initialized = false;
  908. qdf_spin_lock(&gwlan_logging.flush_timer_lock);
  909. qdf_timer_stop(&gwlan_logging.flush_timer);
  910. qdf_timer_free(&gwlan_logging.flush_timer);
  911. qdf_spin_unlock(&gwlan_logging.flush_timer_lock);
  912. qdf_spinlock_destroy(&gwlan_logging.flush_timer_lock);
  913. }
  914. int wlan_logging_sock_deinit_svc(void)
  915. {
  916. unsigned long irq_flag;
  917. int i;
  918. if (!gwlan_logging.pcur_node)
  919. return 0;
  920. #ifdef CONFIG_MCL
  921. INIT_COMPLETION(gwlan_logging.shutdown_comp);
  922. #endif
  923. gwlan_logging.exit = true;
  924. gwlan_logging.is_active = false;
  925. #ifdef CONFIG_MCL
  926. cds_set_multicast_logging(0);
  927. #endif
  928. gwlan_logging.is_flush_complete = false;
  929. clear_bit(HOST_LOG_DRIVER_MSG, &gwlan_logging.eventFlag);
  930. clear_bit(HOST_LOG_PER_PKT_STATS, &gwlan_logging.eventFlag);
  931. clear_bit(HOST_LOG_FW_FLUSH_COMPLETE, &gwlan_logging.eventFlag);
  932. wake_up_interruptible(&gwlan_logging.wait_queue);
  933. wait_for_completion(&gwlan_logging.shutdown_comp);
  934. spin_lock_irqsave(&gwlan_logging.spin_lock, irq_flag);
  935. gwlan_logging.pcur_node = NULL;
  936. spin_unlock_irqrestore(&gwlan_logging.spin_lock, irq_flag);
  937. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  938. gwlan_logging.pkt_stats_pcur_node = NULL;
  939. gwlan_logging.pkt_stats_msg_idx = 0;
  940. gwlan_logging.pkt_stat_drop_cnt = 0;
  941. for (i = 0; i < MAX_PKTSTATS_BUFF; i++) {
  942. if (gpkt_stats_buffers[i].skb)
  943. dev_kfree_skb(gpkt_stats_buffers[i].skb);
  944. }
  945. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  946. vfree(gpkt_stats_buffers);
  947. gpkt_stats_buffers = NULL;
  948. free_log_msg_buffer();
  949. flush_timer_deinit();
  950. return 0;
  951. }
  952. /**
  953. * wlan_logging_set_per_pkt_stats() - This function triggers per packet logging
  954. *
  955. * This function is used to send signal to the logger thread for logging per
  956. * packet stats
  957. *
  958. * Return: None
  959. *
  960. */
  961. void wlan_logging_set_per_pkt_stats(void)
  962. {
  963. if (gwlan_logging.is_active == false)
  964. return;
  965. set_bit(HOST_LOG_PER_PKT_STATS, &gwlan_logging.eventFlag);
  966. wake_up_interruptible(&gwlan_logging.wait_queue);
  967. }
  968. /*
  969. * wlan_logging_set_fw_flush_complete() - FW log flush completion
  970. *
  971. * This function is used to send signal to the logger thread to indicate
  972. * that the flushing of FW logs is complete by the FW
  973. *
  974. * Return: None
  975. *
  976. */
  977. void wlan_logging_set_fw_flush_complete(void)
  978. {
  979. if (gwlan_logging.is_active == false
  980. #ifdef CONFIG_MCL
  981. || !cds_is_fatal_event_enabled()
  982. #endif
  983. )
  984. return;
  985. set_bit(HOST_LOG_FW_FLUSH_COMPLETE, &gwlan_logging.eventFlag);
  986. wake_up_interruptible(&gwlan_logging.wait_queue);
  987. }
  988. /**
  989. * wlan_flush_host_logs_for_fatal() - Flush host logs
  990. *
  991. * This function is used to send signal to the logger thread to
  992. * Flush the host logs
  993. *
  994. * Return: None
  995. */
  996. void wlan_flush_host_logs_for_fatal(void)
  997. {
  998. unsigned long flags;
  999. #ifdef CONFIG_MCL
  1000. if (cds_is_log_report_in_progress()) {
  1001. #endif
  1002. if (gwlan_logging.flush_timer_period == 0)
  1003. pr_info("%s:flush all host logs Setting HOST_LOG_POST_MASK\n",
  1004. __func__);
  1005. spin_lock_irqsave(&gwlan_logging.spin_lock, flags);
  1006. wlan_queue_logmsg_for_app();
  1007. spin_unlock_irqrestore(&gwlan_logging.spin_lock, flags);
  1008. set_bit(HOST_LOG_DRIVER_MSG, &gwlan_logging.eventFlag);
  1009. wake_up_interruptible(&gwlan_logging.wait_queue);
  1010. #ifdef CONFIG_MCL
  1011. }
  1012. #endif
  1013. }
  1014. #ifdef FEATURE_PKTLOG
  1015. #ifndef REMOVE_PKT_LOG
  1016. static uint8_t gtx_count;
  1017. static uint8_t grx_count;
  1018. /**
  1019. * wlan_get_pkt_stats_free_node() - Get the free node for pkt stats
  1020. *
  1021. * This function is used to get the free node for pkt stats from
  1022. * free list/filles list
  1023. *
  1024. * Return: int
  1025. *
  1026. */
  1027. static int wlan_get_pkt_stats_free_node(void)
  1028. {
  1029. int ret = 0;
  1030. list_add_tail(&gwlan_logging.pkt_stats_pcur_node->node,
  1031. &gwlan_logging.pkt_stat_filled_list);
  1032. if (!list_empty(&gwlan_logging.pkt_stat_free_list)) {
  1033. /* Get buffer from free list */
  1034. gwlan_logging.pkt_stats_pcur_node =
  1035. (struct pkt_stats_msg *)(gwlan_logging.pkt_stat_free_list.next);
  1036. list_del_init(gwlan_logging.pkt_stat_free_list.next);
  1037. } else if (!list_empty(&gwlan_logging.pkt_stat_filled_list)) {
  1038. /* Get buffer from filled list. This condition will drop the
  1039. * packet from being indicated to app
  1040. */
  1041. gwlan_logging.pkt_stats_pcur_node =
  1042. (struct pkt_stats_msg *)
  1043. (gwlan_logging.pkt_stat_filled_list.next);
  1044. ++gwlan_logging.pkt_stat_drop_cnt;
  1045. /* print every 64th drop count */
  1046. if (
  1047. cds_is_multicast_logging() &&
  1048. (!(gwlan_logging.pkt_stat_drop_cnt % 0x40))) {
  1049. pr_err("%s: drop_count = %u\n",
  1050. __func__, gwlan_logging.pkt_stat_drop_cnt);
  1051. }
  1052. list_del_init(gwlan_logging.pkt_stat_filled_list.next);
  1053. ret = 1;
  1054. }
  1055. /* Reset the skb values, essential if dequeued from filled list */
  1056. skb_trim(gwlan_logging.pkt_stats_pcur_node->skb, 0);
  1057. return ret;
  1058. }
  1059. /**
  1060. * wlan_pkt_stats_to_logger_thread() - Add the pkt stats to SKB
  1061. * @pl_hdr: Pointer to pl_hdr
  1062. * @pkt_dump: Pointer to pkt_dump
  1063. * @data: Pointer to data
  1064. *
  1065. * This function adds the pktstats hdr and data to current
  1066. * skb node of free list.
  1067. *
  1068. * Return: None
  1069. */
  1070. void wlan_pkt_stats_to_logger_thread(void *pl_hdr, void *pkt_dump, void *data)
  1071. {
  1072. struct ath_pktlog_hdr *pktlog_hdr;
  1073. struct packet_dump *pkt_stats_dump;
  1074. int total_stats_len = 0;
  1075. bool wake_up_thread = false;
  1076. unsigned long flags;
  1077. struct sk_buff *ptr;
  1078. int hdr_size;
  1079. pktlog_hdr = (struct ath_pktlog_hdr *)pl_hdr;
  1080. if (!pktlog_hdr) {
  1081. pr_err("%s : Invalid pkt_stats_header\n", __func__);
  1082. return;
  1083. }
  1084. pkt_stats_dump = (struct packet_dump *)pkt_dump;
  1085. total_stats_len = sizeof(struct ath_pktlog_hdr) +
  1086. pktlog_hdr->size;
  1087. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, flags);
  1088. if (!gwlan_logging.pkt_stats_pcur_node) {
  1089. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, flags);
  1090. return;
  1091. }
  1092. /* Check if we can accommodate more log into current node/buffer */
  1093. hdr_size = sizeof(struct host_log_pktlog_info) +
  1094. sizeof(tAniNlHdr);
  1095. if ((total_stats_len + hdr_size) >=
  1096. skb_tailroom(gwlan_logging.pkt_stats_pcur_node->skb)) {
  1097. wake_up_thread = true;
  1098. wlan_get_pkt_stats_free_node();
  1099. }
  1100. ptr = gwlan_logging.pkt_stats_pcur_node->skb;
  1101. qdf_mem_copy(skb_put(ptr,
  1102. sizeof(struct ath_pktlog_hdr)),
  1103. pktlog_hdr,
  1104. sizeof(struct ath_pktlog_hdr));
  1105. if (pkt_stats_dump) {
  1106. qdf_mem_copy(skb_put(ptr,
  1107. sizeof(struct packet_dump)),
  1108. pkt_stats_dump,
  1109. sizeof(struct packet_dump));
  1110. pktlog_hdr->size -= sizeof(struct packet_dump);
  1111. }
  1112. if (data)
  1113. qdf_mem_copy(skb_put(ptr,
  1114. pktlog_hdr->size),
  1115. data, pktlog_hdr->size);
  1116. if (pkt_stats_dump && pkt_stats_dump->type == STOP_MONITOR) {
  1117. wake_up_thread = true;
  1118. wlan_get_pkt_stats_free_node();
  1119. }
  1120. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, flags);
  1121. /* Wakeup logger thread */
  1122. if (true == wake_up_thread) {
  1123. set_bit(HOST_LOG_PER_PKT_STATS, &gwlan_logging.eventFlag);
  1124. wake_up_interruptible(&gwlan_logging.wait_queue);
  1125. }
  1126. }
  1127. /**
  1128. * driver_hal_status_map() - maps driver to hal
  1129. * status
  1130. * @status: status to be mapped
  1131. *
  1132. * This function is used to map driver to hal status
  1133. *
  1134. * Return: None
  1135. *
  1136. */
  1137. static void driver_hal_status_map(uint8_t *status)
  1138. {
  1139. switch (*status) {
  1140. case tx_status_ok:
  1141. *status = TX_PKT_FATE_ACKED;
  1142. break;
  1143. case tx_status_discard:
  1144. *status = TX_PKT_FATE_DRV_DROP_OTHER;
  1145. break;
  1146. case tx_status_no_ack:
  1147. *status = TX_PKT_FATE_SENT;
  1148. break;
  1149. case tx_status_download_fail:
  1150. *status = TX_PKT_FATE_FW_QUEUED;
  1151. break;
  1152. default:
  1153. *status = TX_PKT_FATE_DRV_DROP_OTHER;
  1154. break;
  1155. }
  1156. }
  1157. /*
  1158. * send_packetdump() - send packet dump
  1159. * @soc: soc handle
  1160. * @vdev: vdev handle
  1161. * @netbuf: netbuf
  1162. * @status: status of tx packet
  1163. * @type: type of packet
  1164. *
  1165. * This function is used to send packet dump to HAL layer
  1166. * using wlan_pkt_stats_to_logger_thread
  1167. *
  1168. * Return: None
  1169. *
  1170. */
  1171. static void send_packetdump(ol_txrx_soc_handle soc,
  1172. struct cdp_vdev *vdev, qdf_nbuf_t netbuf,
  1173. uint8_t status, uint8_t type)
  1174. {
  1175. struct ath_pktlog_hdr pktlog_hdr = {0};
  1176. struct packet_dump pd_hdr = {0};
  1177. if (!netbuf) {
  1178. pr_err("%s: Invalid netbuf.\n", __func__);
  1179. return;
  1180. }
  1181. /* Send packet dump only for STA interface */
  1182. if (wlan_op_mode_sta != cdp_get_opmode(soc, vdev))
  1183. return;
  1184. #if defined(HELIUMPLUS)
  1185. pktlog_hdr.flags |= PKTLOG_HDR_SIZE_16;
  1186. #endif
  1187. pktlog_hdr.log_type = PKTLOG_TYPE_PKT_DUMP;
  1188. pktlog_hdr.size = sizeof(pd_hdr) + netbuf->len;
  1189. pd_hdr.status = status;
  1190. pd_hdr.type = type;
  1191. pd_hdr.driver_ts = qdf_get_monotonic_boottime();
  1192. if ((type == TX_MGMT_PKT) || (type == TX_DATA_PKT))
  1193. gtx_count++;
  1194. else if ((type == RX_MGMT_PKT) || (type == RX_DATA_PKT))
  1195. grx_count++;
  1196. wlan_pkt_stats_to_logger_thread(&pktlog_hdr, &pd_hdr, netbuf->data);
  1197. }
  1198. /*
  1199. * send_packetdump_monitor() - sends start/stop packet dump indication
  1200. * @type: type of packet
  1201. *
  1202. * This function is used to indicate HAL layer to start/stop monitoring
  1203. * of packets
  1204. *
  1205. * Return: None
  1206. *
  1207. */
  1208. static void send_packetdump_monitor(uint8_t type)
  1209. {
  1210. struct ath_pktlog_hdr pktlog_hdr = {0};
  1211. struct packet_dump pd_hdr = {0};
  1212. #if defined(HELIUMPLUS)
  1213. pktlog_hdr.flags |= PKTLOG_HDR_SIZE_16;
  1214. #endif
  1215. pktlog_hdr.log_type = PKTLOG_TYPE_PKT_DUMP;
  1216. pktlog_hdr.size = sizeof(pd_hdr);
  1217. pd_hdr.type = type;
  1218. LOGGING_TRACE(QDF_TRACE_LEVEL_DEBUG,
  1219. "fate Tx-Rx %s: type: %d", __func__, type);
  1220. wlan_pkt_stats_to_logger_thread(&pktlog_hdr, &pd_hdr, NULL);
  1221. }
  1222. /**
  1223. * wlan_deregister_txrx_packetdump() - tx/rx packet dump
  1224. * deregistration
  1225. *
  1226. * This function is used to deregister tx/rx packet dump callbacks
  1227. * with ol, pe and htt layers
  1228. *
  1229. * Return: None
  1230. *
  1231. */
  1232. void wlan_deregister_txrx_packetdump(void)
  1233. {
  1234. void *soc = cds_get_context(QDF_MODULE_ID_SOC);
  1235. if (!soc)
  1236. return;
  1237. if (gtx_count || grx_count) {
  1238. cdp_deregister_packetdump_cb(soc);
  1239. wma_deregister_packetdump_callback();
  1240. send_packetdump_monitor(STOP_MONITOR);
  1241. csr_packetdump_timer_stop();
  1242. gtx_count = 0;
  1243. grx_count = 0;
  1244. } else
  1245. LOGGING_TRACE(QDF_TRACE_LEVEL_DEBUG,
  1246. "%s: deregistered packetdump already", __func__);
  1247. }
  1248. /*
  1249. * check_txrx_packetdump_count() - function to check
  1250. * tx/rx packet dump global counts
  1251. *
  1252. * This function is used to check global counts of tx/rx
  1253. * packet dump functionality.
  1254. *
  1255. * Return: 1 if either gtx_count or grx_count reached 32
  1256. * 0 otherwise
  1257. *
  1258. */
  1259. static bool check_txrx_packetdump_count(void)
  1260. {
  1261. if (gtx_count == MAX_NUM_PKT_LOG ||
  1262. grx_count == MAX_NUM_PKT_LOG) {
  1263. LOGGING_TRACE(QDF_TRACE_LEVEL_DEBUG,
  1264. "%s gtx_count: %d grx_count: %d deregister packetdump",
  1265. __func__, gtx_count, grx_count);
  1266. wlan_deregister_txrx_packetdump();
  1267. return 1;
  1268. }
  1269. return 0;
  1270. }
  1271. /*
  1272. * tx_packetdump_cb() - tx packet dump callback
  1273. * @soc: soc handle
  1274. * @vdev: vdev handle
  1275. * @netbuf: netbuf
  1276. * @status: status of tx packet
  1277. * @type: packet type
  1278. *
  1279. * This function is used to send tx packet dump to HAL layer
  1280. * and deregister packet dump callbacks
  1281. *
  1282. * Return: None
  1283. *
  1284. */
  1285. static void tx_packetdump_cb(ol_txrx_soc_handle soc,
  1286. struct cdp_vdev *vdev, qdf_nbuf_t netbuf,
  1287. uint8_t status, uint8_t type)
  1288. {
  1289. bool temp;
  1290. if (!soc || !vdev)
  1291. return;
  1292. temp = check_txrx_packetdump_count();
  1293. if (temp)
  1294. return;
  1295. driver_hal_status_map(&status);
  1296. send_packetdump(soc, vdev, netbuf, status, type);
  1297. }
  1298. /*
  1299. * rx_packetdump_cb() - rx packet dump callback
  1300. * @soc: soc handle
  1301. * @vdev: vdev handle
  1302. * @netbuf: netbuf
  1303. * @status: status of rx packet
  1304. * @type: packet type
  1305. *
  1306. * This function is used to send rx packet dump to HAL layer
  1307. * and deregister packet dump callbacks
  1308. *
  1309. * Return: None
  1310. *
  1311. */
  1312. static void rx_packetdump_cb(ol_txrx_soc_handle soc,
  1313. struct cdp_vdev *vdev, qdf_nbuf_t netbuf,
  1314. uint8_t status, uint8_t type)
  1315. {
  1316. bool temp;
  1317. if (!soc || !vdev)
  1318. return;
  1319. temp = check_txrx_packetdump_count();
  1320. if (temp)
  1321. return;
  1322. send_packetdump(soc, vdev, netbuf, status, type);
  1323. }
  1324. /**
  1325. * wlan_register_txrx_packetdump() - tx/rx packet dump
  1326. * registration
  1327. *
  1328. * This function is used to register tx/rx packet dump callbacks
  1329. * with ol, pe and htt layers
  1330. *
  1331. * Return: None
  1332. *
  1333. */
  1334. void wlan_register_txrx_packetdump(void)
  1335. {
  1336. void *soc = cds_get_context(QDF_MODULE_ID_SOC);
  1337. if (!soc)
  1338. return;
  1339. cdp_register_packetdump_cb(soc, tx_packetdump_cb, rx_packetdump_cb);
  1340. wma_register_packetdump_callback(tx_packetdump_cb,
  1341. rx_packetdump_cb);
  1342. send_packetdump_monitor(START_MONITOR);
  1343. gtx_count = 0;
  1344. grx_count = 0;
  1345. }
  1346. #endif /* REMOVE_PKT_LOG */
  1347. #endif /* FEATURE_PKTLOG */
  1348. #endif /* WLAN_LOGGING_SOCK_SVC_ENABLE */