wlan_logging_sock_svc.c 43 KB

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