wlan_logging_sock_svc.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /*
  2. * Copyright (c) 2014-2020 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. int wlan_logging_sock_init_svc(void)
  915. {
  916. int i = 0, j, pkt_stats_size;
  917. unsigned long irq_flag;
  918. flush_timer_init();
  919. spin_lock_init(&gwlan_logging.spin_lock);
  920. spin_lock_init(&gwlan_logging.pkt_stats_lock);
  921. gwlan_logging.console_log_levels = 0;
  922. gwlan_logging.num_buf = MAX_LOGMSG_COUNT;
  923. gwlan_logging.buffer_length = MAX_LOGMSG_LENGTH;
  924. if (allocate_log_msg_buffer() != QDF_STATUS_SUCCESS) {
  925. qdf_err("Could not allocate memory for log_msg");
  926. return -ENOMEM;
  927. }
  928. spin_lock_irqsave(&gwlan_logging.spin_lock, irq_flag);
  929. INIT_LIST_HEAD(&gwlan_logging.free_list);
  930. INIT_LIST_HEAD(&gwlan_logging.filled_list);
  931. INIT_LIST_HEAD(&gwlan_logging.panic_list);
  932. for (i = 0; i < gwlan_logging.num_buf; i++) {
  933. list_add(&gplog_msg[i].node, &gwlan_logging.free_list);
  934. gplog_msg[i].index = i;
  935. }
  936. gwlan_logging.pcur_node = (struct log_msg *)
  937. (gwlan_logging.free_list.next);
  938. list_del_init(gwlan_logging.free_list.next);
  939. spin_unlock_irqrestore(&gwlan_logging.spin_lock, irq_flag);
  940. /* Initialize the pktStats data structure here */
  941. pkt_stats_size = sizeof(struct pkt_stats_msg);
  942. gpkt_stats_buffers = vmalloc(MAX_PKTSTATS_BUFF * pkt_stats_size);
  943. if (!gpkt_stats_buffers) {
  944. qdf_err("Could not allocate memory for Pkt stats");
  945. goto err1;
  946. }
  947. qdf_mem_zero(gpkt_stats_buffers,
  948. MAX_PKTSTATS_BUFF * pkt_stats_size);
  949. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  950. gwlan_logging.pkt_stats_msg_idx = 0;
  951. INIT_LIST_HEAD(&gwlan_logging.pkt_stat_free_list);
  952. INIT_LIST_HEAD(&gwlan_logging.pkt_stat_filled_list);
  953. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  954. for (i = 0; i < MAX_PKTSTATS_BUFF; i++) {
  955. gpkt_stats_buffers[i].skb = dev_alloc_skb(MAX_PKTSTATS_LENGTH);
  956. if (!gpkt_stats_buffers[i].skb) {
  957. qdf_err("Memory alloc failed for skb");
  958. /* free previously allocated skb and return */
  959. for (j = 0; j < i ; j++)
  960. dev_kfree_skb(gpkt_stats_buffers[j].skb);
  961. goto err2;
  962. }
  963. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  964. list_add(&gpkt_stats_buffers[i].node,
  965. &gwlan_logging.pkt_stat_free_list);
  966. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  967. }
  968. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  969. gwlan_logging.pkt_stats_pcur_node = (struct pkt_stats_msg *)
  970. (gwlan_logging.pkt_stat_free_list.next);
  971. list_del_init(gwlan_logging.pkt_stat_free_list.next);
  972. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  973. /* Pkt Stats intialization done */
  974. init_waitqueue_head(&gwlan_logging.wait_queue);
  975. gwlan_logging.exit = false;
  976. clear_bit(HOST_LOG_DRIVER_MSG, &gwlan_logging.eventFlag);
  977. clear_bit(HOST_LOG_PER_PKT_STATS, &gwlan_logging.eventFlag);
  978. clear_bit(HOST_LOG_FW_FLUSH_COMPLETE, &gwlan_logging.eventFlag);
  979. init_completion(&gwlan_logging.shutdown_comp);
  980. gwlan_logging.thread = kthread_create(wlan_logging_thread, NULL,
  981. "wlan_logging_thread");
  982. if (IS_ERR(gwlan_logging.thread)) {
  983. qdf_err("Could not Create LogMsg Thread Controller");
  984. goto err3;
  985. }
  986. wake_up_process(gwlan_logging.thread);
  987. gwlan_logging.is_active = true;
  988. gwlan_logging.is_flush_complete = false;
  989. return 0;
  990. err3:
  991. for (i = 0; i < MAX_PKTSTATS_BUFF; i++) {
  992. if (gpkt_stats_buffers[i].skb)
  993. dev_kfree_skb(gpkt_stats_buffers[i].skb);
  994. }
  995. err2:
  996. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  997. gwlan_logging.pkt_stats_pcur_node = NULL;
  998. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  999. vfree(gpkt_stats_buffers);
  1000. gpkt_stats_buffers = NULL;
  1001. err1:
  1002. spin_lock_irqsave(&gwlan_logging.spin_lock, irq_flag);
  1003. gwlan_logging.pcur_node = NULL;
  1004. spin_unlock_irqrestore(&gwlan_logging.spin_lock, irq_flag);
  1005. free_log_msg_buffer();
  1006. return -ENOMEM;
  1007. }
  1008. static void flush_timer_deinit(void)
  1009. {
  1010. gwlan_logging.is_flush_timer_initialized = false;
  1011. qdf_spin_lock(&gwlan_logging.flush_timer_lock);
  1012. qdf_timer_stop(&gwlan_logging.flush_timer);
  1013. qdf_timer_free(&gwlan_logging.flush_timer);
  1014. qdf_spin_unlock(&gwlan_logging.flush_timer_lock);
  1015. qdf_spinlock_destroy(&gwlan_logging.flush_timer_lock);
  1016. }
  1017. int wlan_logging_sock_deinit_svc(void)
  1018. {
  1019. unsigned long irq_flag;
  1020. int i;
  1021. if (!gwlan_logging.pcur_node)
  1022. return 0;
  1023. INIT_COMPLETION(gwlan_logging.shutdown_comp);
  1024. gwlan_logging.exit = true;
  1025. gwlan_logging.is_active = false;
  1026. #if defined(FEATURE_FW_LOG_PARSING) || defined(FEATURE_WLAN_DIAG_SUPPORT)
  1027. cds_set_multicast_logging(0);
  1028. #endif
  1029. gwlan_logging.is_flush_complete = false;
  1030. clear_bit(HOST_LOG_DRIVER_MSG, &gwlan_logging.eventFlag);
  1031. clear_bit(HOST_LOG_PER_PKT_STATS, &gwlan_logging.eventFlag);
  1032. clear_bit(HOST_LOG_FW_FLUSH_COMPLETE, &gwlan_logging.eventFlag);
  1033. wake_up_interruptible(&gwlan_logging.wait_queue);
  1034. wait_for_completion(&gwlan_logging.shutdown_comp);
  1035. spin_lock_irqsave(&gwlan_logging.spin_lock, irq_flag);
  1036. gwlan_logging.pcur_node = NULL;
  1037. spin_unlock_irqrestore(&gwlan_logging.spin_lock, irq_flag);
  1038. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, irq_flag);
  1039. gwlan_logging.pkt_stats_pcur_node = NULL;
  1040. gwlan_logging.pkt_stats_msg_idx = 0;
  1041. gwlan_logging.pkt_stat_drop_cnt = 0;
  1042. for (i = 0; i < MAX_PKTSTATS_BUFF; i++) {
  1043. if (gpkt_stats_buffers[i].skb)
  1044. dev_kfree_skb(gpkt_stats_buffers[i].skb);
  1045. }
  1046. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, irq_flag);
  1047. vfree(gpkt_stats_buffers);
  1048. gpkt_stats_buffers = NULL;
  1049. free_log_msg_buffer();
  1050. flush_timer_deinit();
  1051. return 0;
  1052. }
  1053. /**
  1054. * wlan_logging_set_per_pkt_stats() - This function triggers per packet logging
  1055. *
  1056. * This function is used to send signal to the logger thread for logging per
  1057. * packet stats
  1058. *
  1059. * Return: None
  1060. *
  1061. */
  1062. void wlan_logging_set_per_pkt_stats(void)
  1063. {
  1064. if (gwlan_logging.is_active == false)
  1065. return;
  1066. set_bit(HOST_LOG_PER_PKT_STATS, &gwlan_logging.eventFlag);
  1067. wake_up_interruptible(&gwlan_logging.wait_queue);
  1068. }
  1069. /*
  1070. * wlan_logging_set_fw_flush_complete() - FW log flush completion
  1071. *
  1072. * This function is used to send signal to the logger thread to indicate
  1073. * that the flushing of FW logs is complete by the FW
  1074. *
  1075. * Return: None
  1076. *
  1077. */
  1078. void wlan_logging_set_fw_flush_complete(void)
  1079. {
  1080. if (!gwlan_logging.is_active)
  1081. return;
  1082. set_bit(HOST_LOG_FW_FLUSH_COMPLETE, &gwlan_logging.eventFlag);
  1083. wake_up_interruptible(&gwlan_logging.wait_queue);
  1084. }
  1085. /**
  1086. * wlan_flush_host_logs_for_fatal() - Flush host logs
  1087. *
  1088. * This function is used to send signal to the logger thread to
  1089. * Flush the host logs
  1090. *
  1091. * Return: None
  1092. */
  1093. void wlan_flush_host_logs_for_fatal(void)
  1094. {
  1095. unsigned long flags;
  1096. if (gwlan_logging.flush_timer_period == 0)
  1097. qdf_info("Flush all host logs Setting HOST_LOG_POST_MAS");
  1098. spin_lock_irqsave(&gwlan_logging.spin_lock, flags);
  1099. wlan_queue_logmsg_for_app();
  1100. spin_unlock_irqrestore(&gwlan_logging.spin_lock, flags);
  1101. set_bit(HOST_LOG_DRIVER_MSG, &gwlan_logging.eventFlag);
  1102. wake_up_interruptible(&gwlan_logging.wait_queue);
  1103. }
  1104. #ifdef FEATURE_PKTLOG
  1105. #ifndef REMOVE_PKT_LOG
  1106. static uint8_t gtx_count;
  1107. static uint8_t grx_count;
  1108. /**
  1109. * wlan_get_pkt_stats_free_node() - Get the free node for pkt stats
  1110. *
  1111. * This function is used to get the free node for pkt stats from
  1112. * free list/filles list
  1113. *
  1114. * Return: int
  1115. *
  1116. */
  1117. static int wlan_get_pkt_stats_free_node(void)
  1118. {
  1119. int ret = 0;
  1120. list_add_tail(&gwlan_logging.pkt_stats_pcur_node->node,
  1121. &gwlan_logging.pkt_stat_filled_list);
  1122. if (!list_empty(&gwlan_logging.pkt_stat_free_list)) {
  1123. /* Get buffer from free list */
  1124. gwlan_logging.pkt_stats_pcur_node =
  1125. (struct pkt_stats_msg *)(gwlan_logging.pkt_stat_free_list.next);
  1126. list_del_init(gwlan_logging.pkt_stat_free_list.next);
  1127. } else if (!list_empty(&gwlan_logging.pkt_stat_filled_list)) {
  1128. /* Get buffer from filled list. This condition will drop the
  1129. * packet from being indicated to app
  1130. */
  1131. gwlan_logging.pkt_stats_pcur_node =
  1132. (struct pkt_stats_msg *)
  1133. (gwlan_logging.pkt_stat_filled_list.next);
  1134. ++gwlan_logging.pkt_stat_drop_cnt;
  1135. /* print every 64th drop count */
  1136. if (
  1137. cds_is_multicast_logging() &&
  1138. (!(gwlan_logging.pkt_stat_drop_cnt % 0x40))) {
  1139. qdf_err("drop_count = %u",
  1140. gwlan_logging.pkt_stat_drop_cnt);
  1141. }
  1142. list_del_init(gwlan_logging.pkt_stat_filled_list.next);
  1143. ret = 1;
  1144. }
  1145. /* Reset the skb values, essential if dequeued from filled list */
  1146. skb_trim(gwlan_logging.pkt_stats_pcur_node->skb, 0);
  1147. return ret;
  1148. }
  1149. /**
  1150. * wlan_pkt_stats_to_logger_thread() - Add the pkt stats to SKB
  1151. * @pl_hdr: Pointer to pl_hdr
  1152. * @pkt_dump: Pointer to pkt_dump
  1153. * @data: Pointer to data
  1154. *
  1155. * This function adds the pktstats hdr and data to current
  1156. * skb node of free list.
  1157. *
  1158. * Return: None
  1159. */
  1160. void wlan_pkt_stats_to_logger_thread(void *pl_hdr, void *pkt_dump, void *data)
  1161. {
  1162. struct ath_pktlog_hdr *pktlog_hdr;
  1163. struct packet_dump *pkt_stats_dump;
  1164. int total_stats_len = 0;
  1165. bool wake_up_thread = false;
  1166. unsigned long flags;
  1167. struct sk_buff *ptr;
  1168. int hdr_size;
  1169. pktlog_hdr = (struct ath_pktlog_hdr *)pl_hdr;
  1170. if (!pktlog_hdr) {
  1171. qdf_err("Invalid pkt_stats_header");
  1172. return;
  1173. }
  1174. pkt_stats_dump = (struct packet_dump *)pkt_dump;
  1175. total_stats_len = sizeof(struct ath_pktlog_hdr) +
  1176. pktlog_hdr->size;
  1177. spin_lock_irqsave(&gwlan_logging.pkt_stats_lock, flags);
  1178. if (!gwlan_logging.pkt_stats_pcur_node) {
  1179. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, flags);
  1180. return;
  1181. }
  1182. /* Check if we can accommodate more log into current node/buffer */
  1183. hdr_size = sizeof(struct host_log_pktlog_info) +
  1184. sizeof(tAniNlHdr);
  1185. if ((total_stats_len + hdr_size) >=
  1186. skb_tailroom(gwlan_logging.pkt_stats_pcur_node->skb)) {
  1187. wake_up_thread = true;
  1188. wlan_get_pkt_stats_free_node();
  1189. }
  1190. ptr = gwlan_logging.pkt_stats_pcur_node->skb;
  1191. qdf_mem_copy(skb_put(ptr,
  1192. sizeof(struct ath_pktlog_hdr)),
  1193. pktlog_hdr,
  1194. sizeof(struct ath_pktlog_hdr));
  1195. if (pkt_stats_dump) {
  1196. qdf_mem_copy(skb_put(ptr,
  1197. sizeof(struct packet_dump)),
  1198. pkt_stats_dump,
  1199. sizeof(struct packet_dump));
  1200. pktlog_hdr->size -= sizeof(struct packet_dump);
  1201. }
  1202. if (data)
  1203. qdf_mem_copy(skb_put(ptr,
  1204. pktlog_hdr->size),
  1205. data, pktlog_hdr->size);
  1206. if (pkt_stats_dump && pkt_stats_dump->type == STOP_MONITOR) {
  1207. wake_up_thread = true;
  1208. wlan_get_pkt_stats_free_node();
  1209. }
  1210. spin_unlock_irqrestore(&gwlan_logging.pkt_stats_lock, flags);
  1211. /* Wakeup logger thread */
  1212. if (true == wake_up_thread) {
  1213. set_bit(HOST_LOG_PER_PKT_STATS, &gwlan_logging.eventFlag);
  1214. wake_up_interruptible(&gwlan_logging.wait_queue);
  1215. }
  1216. }
  1217. /**
  1218. * driver_hal_status_map() - maps driver to hal
  1219. * status
  1220. * @status: status to be mapped
  1221. *
  1222. * This function is used to map driver to hal status
  1223. *
  1224. * Return: None
  1225. *
  1226. */
  1227. static void driver_hal_status_map(uint8_t *status)
  1228. {
  1229. switch (*status) {
  1230. case tx_status_ok:
  1231. *status = TX_PKT_FATE_ACKED;
  1232. break;
  1233. case tx_status_discard:
  1234. *status = TX_PKT_FATE_DRV_DROP_OTHER;
  1235. break;
  1236. case tx_status_no_ack:
  1237. *status = TX_PKT_FATE_SENT;
  1238. break;
  1239. case tx_status_download_fail:
  1240. *status = TX_PKT_FATE_FW_QUEUED;
  1241. break;
  1242. default:
  1243. *status = TX_PKT_FATE_DRV_DROP_OTHER;
  1244. break;
  1245. }
  1246. }
  1247. /*
  1248. * send_packetdump() - send packet dump
  1249. * @soc: soc handle
  1250. * @vdev_id: ID of the virtual device handle
  1251. * @netbuf: netbuf
  1252. * @status: status of tx packet
  1253. * @type: type of packet
  1254. *
  1255. * This function is used to send packet dump to HAL layer
  1256. * using wlan_pkt_stats_to_logger_thread
  1257. *
  1258. * Return: None
  1259. *
  1260. */
  1261. static void send_packetdump(ol_txrx_soc_handle soc,
  1262. uint8_t vdev_id, qdf_nbuf_t netbuf,
  1263. uint8_t status, uint8_t type)
  1264. {
  1265. struct ath_pktlog_hdr pktlog_hdr = {0};
  1266. struct packet_dump pd_hdr = {0};
  1267. if (!netbuf) {
  1268. qdf_err("Invalid netbuf");
  1269. return;
  1270. }
  1271. /* Send packet dump only for STA interface */
  1272. if (wlan_op_mode_sta != cdp_get_opmode(soc, vdev_id))
  1273. return;
  1274. #if defined(HELIUMPLUS)
  1275. pktlog_hdr.flags |= PKTLOG_HDR_SIZE_16;
  1276. #endif
  1277. pktlog_hdr.log_type = PKTLOG_TYPE_PKT_DUMP;
  1278. pktlog_hdr.size = sizeof(pd_hdr) + netbuf->len;
  1279. pd_hdr.status = status;
  1280. pd_hdr.type = type;
  1281. pd_hdr.driver_ts = qdf_get_monotonic_boottime();
  1282. if ((type == TX_MGMT_PKT) || (type == TX_DATA_PKT))
  1283. gtx_count++;
  1284. else if ((type == RX_MGMT_PKT) || (type == RX_DATA_PKT))
  1285. grx_count++;
  1286. wlan_pkt_stats_to_logger_thread(&pktlog_hdr, &pd_hdr, netbuf->data);
  1287. }
  1288. /*
  1289. * send_packetdump_monitor() - sends start/stop packet dump indication
  1290. * @type: type of packet
  1291. *
  1292. * This function is used to indicate HAL layer to start/stop monitoring
  1293. * of packets
  1294. *
  1295. * Return: None
  1296. *
  1297. */
  1298. static void send_packetdump_monitor(uint8_t type)
  1299. {
  1300. struct ath_pktlog_hdr pktlog_hdr = {0};
  1301. struct packet_dump pd_hdr = {0};
  1302. #if defined(HELIUMPLUS)
  1303. pktlog_hdr.flags |= PKTLOG_HDR_SIZE_16;
  1304. #endif
  1305. pktlog_hdr.log_type = PKTLOG_TYPE_PKT_DUMP;
  1306. pktlog_hdr.size = sizeof(pd_hdr);
  1307. pd_hdr.type = type;
  1308. LOGGING_TRACE(QDF_TRACE_LEVEL_DEBUG,
  1309. "fate Tx-Rx %s: type: %d", __func__, type);
  1310. wlan_pkt_stats_to_logger_thread(&pktlog_hdr, &pd_hdr, NULL);
  1311. }
  1312. void wlan_deregister_txrx_packetdump(uint8_t pdev_id)
  1313. {
  1314. void *soc = cds_get_context(QDF_MODULE_ID_SOC);
  1315. if (!soc)
  1316. return;
  1317. if (gtx_count || grx_count) {
  1318. cdp_deregister_packetdump_cb(soc, pdev_id);
  1319. wma_deregister_packetdump_callback();
  1320. send_packetdump_monitor(STOP_MONITOR);
  1321. csr_packetdump_timer_stop();
  1322. gtx_count = 0;
  1323. grx_count = 0;
  1324. } else
  1325. LOGGING_TRACE(QDF_TRACE_LEVEL_DEBUG,
  1326. "%s: deregistered packetdump already", __func__);
  1327. }
  1328. /*
  1329. * check_txrx_packetdump_count() - function to check
  1330. * tx/rx packet dump global counts
  1331. * @pdev_id: datapath pdev identifier
  1332. *
  1333. * This function is used to check global counts of tx/rx
  1334. * packet dump functionality.
  1335. *
  1336. * Return: 1 if either gtx_count or grx_count reached 32
  1337. * 0 otherwise
  1338. *
  1339. */
  1340. static bool check_txrx_packetdump_count(uint8_t pdev_id)
  1341. {
  1342. if (gtx_count == MAX_NUM_PKT_LOG ||
  1343. grx_count == MAX_NUM_PKT_LOG) {
  1344. LOGGING_TRACE(QDF_TRACE_LEVEL_DEBUG,
  1345. "%s gtx_count: %d grx_count: %d deregister packetdump",
  1346. __func__, gtx_count, grx_count);
  1347. wlan_deregister_txrx_packetdump(pdev_id);
  1348. return 1;
  1349. }
  1350. return 0;
  1351. }
  1352. /*
  1353. * tx_packetdump_cb() - tx packet dump callback
  1354. * @soc: soc handle
  1355. * @pdev_id: datapath pdev id
  1356. * @vdev_id: vdev id
  1357. * @netbuf: netbuf
  1358. * @status: status of tx packet
  1359. * @type: packet type
  1360. *
  1361. * This function is used to send tx packet dump to HAL layer
  1362. * and deregister packet dump callbacks
  1363. *
  1364. * Return: None
  1365. *
  1366. */
  1367. static void tx_packetdump_cb(ol_txrx_soc_handle soc,
  1368. uint8_t pdev_id, uint8_t vdev_id,
  1369. qdf_nbuf_t netbuf,
  1370. uint8_t status, uint8_t type)
  1371. {
  1372. bool temp;
  1373. if (!soc)
  1374. return;
  1375. temp = check_txrx_packetdump_count(pdev_id);
  1376. if (temp)
  1377. return;
  1378. driver_hal_status_map(&status);
  1379. send_packetdump(soc, vdev_id, netbuf, status, type);
  1380. }
  1381. /*
  1382. * rx_packetdump_cb() - rx packet dump callback
  1383. * @soc: soc handle
  1384. * @pdev_id: datapath pdev id
  1385. * @vdev_id: vdev id
  1386. * @netbuf: netbuf
  1387. * @status: status of rx packet
  1388. * @type: packet type
  1389. *
  1390. * This function is used to send rx packet dump to HAL layer
  1391. * and deregister packet dump callbacks
  1392. *
  1393. * Return: None
  1394. *
  1395. */
  1396. static void rx_packetdump_cb(ol_txrx_soc_handle soc,
  1397. uint8_t pdev_id, uint8_t vdev_id,
  1398. qdf_nbuf_t netbuf,
  1399. uint8_t status, uint8_t type)
  1400. {
  1401. bool temp;
  1402. if (!soc)
  1403. return;
  1404. temp = check_txrx_packetdump_count(pdev_id);
  1405. if (temp)
  1406. return;
  1407. send_packetdump(soc, vdev_id, netbuf, status, type);
  1408. }
  1409. void wlan_register_txrx_packetdump(uint8_t pdev_id)
  1410. {
  1411. void *soc = cds_get_context(QDF_MODULE_ID_SOC);
  1412. if (!soc)
  1413. return;
  1414. cdp_register_packetdump_cb(soc, pdev_id,
  1415. tx_packetdump_cb, rx_packetdump_cb);
  1416. wma_register_packetdump_callback(tx_packetdump_cb,
  1417. rx_packetdump_cb);
  1418. send_packetdump_monitor(START_MONITOR);
  1419. gtx_count = 0;
  1420. grx_count = 0;
  1421. csr_packetdump_timer_start();
  1422. }
  1423. #endif /* REMOVE_PKT_LOG */
  1424. #endif /* FEATURE_PKTLOG */
  1425. #endif /* WLAN_LOGGING_SOCK_SVC_ENABLE */