linux_ac.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /*
  2. * Copyright (c) 2012-2017 The Linux Foundation. All rights reserved.
  3. *
  4. * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
  5. *
  6. *
  7. * Permission to use, copy, modify, and/or distribute this software for
  8. * any purpose with or without fee is hereby granted, provided that the
  9. * above copyright notice and this permission notice appear in all
  10. * copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  13. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  15. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  16. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  17. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  18. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  19. * PERFORMANCE OF THIS SOFTWARE.
  20. */
  21. /*
  22. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. #ifndef REMOVE_PKT_LOG
  27. #ifndef EXPORT_SYMTAB
  28. #define EXPORT_SYMTAB
  29. #endif
  30. #ifndef __KERNEL__
  31. #define __KERNEL__
  32. #endif
  33. /*
  34. * Linux specific implementation of Pktlogs for 802.11ac
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/module.h>
  39. #include <linux/vmalloc.h>
  40. #include <linux/proc_fs.h>
  41. #include <pktlog_ac_i.h>
  42. #include <pktlog_ac_fmt.h>
  43. #include <pktlog_ac.h>
  44. #include "i_host_diag_core_log.h"
  45. #include "host_diag_core_log.h"
  46. #include "ani_global.h"
  47. #define PKTLOG_TAG "ATH_PKTLOG"
  48. #define PKTLOG_DEVNAME_SIZE 32
  49. #define MAX_WLANDEV 1
  50. #ifdef MULTI_IF_NAME
  51. #define PKTLOG_PROC_DIR "ath_pktlog" MULTI_IF_NAME
  52. #else
  53. #define PKTLOG_PROC_DIR "ath_pktlog"
  54. #endif
  55. /* Permissions for creating proc entries */
  56. #define PKTLOG_PROC_PERM 0444
  57. #define PKTLOG_PROCSYS_DIR_PERM 0555
  58. #define PKTLOG_PROCSYS_PERM 0644
  59. #ifndef __MOD_INC_USE_COUNT
  60. #define PKTLOG_MOD_INC_USE_COUNT do { \
  61. if (!try_module_get(THIS_MODULE)) { \
  62. printk(KERN_WARNING "try_module_get failed\n"); \
  63. } } while (0)
  64. #define PKTLOG_MOD_DEC_USE_COUNT module_put(THIS_MODULE)
  65. #else
  66. #define PKTLOG_MOD_INC_USE_COUNT MOD_INC_USE_COUNT
  67. #define PKTLOG_MOD_DEC_USE_COUNT MOD_DEC_USE_COUNT
  68. #endif
  69. static struct ath_pktlog_info *g_pktlog_info;
  70. static struct proc_dir_entry *g_pktlog_pde;
  71. static int pktlog_attach(struct hif_opaque_softc *sc);
  72. static void pktlog_detach(struct ol_txrx_pdev_t *handle);
  73. static int pktlog_open(struct inode *i, struct file *f);
  74. static int pktlog_release(struct inode *i, struct file *f);
  75. static int pktlog_mmap(struct file *f, struct vm_area_struct *vma);
  76. static ssize_t pktlog_read(struct file *file, char *buf, size_t nbytes,
  77. loff_t *ppos);
  78. static struct file_operations pktlog_fops = {
  79. open: pktlog_open,
  80. release:pktlog_release,
  81. mmap : pktlog_mmap,
  82. read : pktlog_read,
  83. };
  84. /*
  85. * Linux implementation of helper functions
  86. */
  87. static struct ol_pktlog_dev_t *cds_get_pl_handle(void)
  88. {
  89. ol_txrx_pdev_handle pdev_txrx_handle;
  90. pdev_txrx_handle = cds_get_context(QDF_MODULE_ID_TXRX);
  91. if (!pdev_txrx_handle) {
  92. QDF_ASSERT(0);
  93. return NULL;
  94. }
  95. return pdev_txrx_handle->pl_dev;
  96. }
  97. static struct ol_pktlog_dev_t *ol_get_pl_handle(
  98. ol_txrx_pdev_handle pdev_txrx_handle)
  99. {
  100. if (!pdev_txrx_handle)
  101. return NULL;
  102. return pdev_txrx_handle->pl_dev;
  103. }
  104. void pktlog_disable_adapter_logging(struct hif_opaque_softc *scn)
  105. {
  106. struct ol_pktlog_dev_t *pl_dev = cds_get_pl_handle();
  107. if (pl_dev)
  108. pl_dev->pl_info->log_state = 0;
  109. }
  110. int pktlog_alloc_buf(struct hif_opaque_softc *scn)
  111. {
  112. uint32_t page_cnt;
  113. unsigned long vaddr;
  114. struct page *vpg;
  115. struct ath_pktlog_info *pl_info;
  116. struct ath_pktlog_buf *buffer;
  117. ol_txrx_pdev_handle pdev_txrx_handle;
  118. pdev_txrx_handle = cds_get_context(QDF_MODULE_ID_TXRX);
  119. if (!pdev_txrx_handle || !pdev_txrx_handle->pl_dev) {
  120. printk(PKTLOG_TAG
  121. "%s: Unable to allocate buffer "
  122. "scn or scn->pdev_txrx_handle->pl_dev is null\n",
  123. __func__);
  124. return -EINVAL;
  125. }
  126. pl_info = pdev_txrx_handle->pl_dev->pl_info;
  127. page_cnt = (sizeof(*(pl_info->buf)) + pl_info->buf_size) / PAGE_SIZE;
  128. spin_lock_bh(&pl_info->log_lock);
  129. if (pl_info->buf != NULL) {
  130. printk(PKTLOG_TAG "Buffer is already in use\n");
  131. spin_unlock_bh(&pl_info->log_lock);
  132. return -EINVAL;
  133. }
  134. spin_unlock_bh(&pl_info->log_lock);
  135. buffer = vmalloc((page_cnt + 2) * PAGE_SIZE);
  136. if (buffer == NULL) {
  137. printk(PKTLOG_TAG
  138. "%s: Unable to allocate buffer "
  139. "(%d pages)\n", __func__, page_cnt);
  140. return -ENOMEM;
  141. }
  142. buffer = (struct ath_pktlog_buf *)
  143. (((unsigned long)(buffer) + PAGE_SIZE - 1)
  144. & PAGE_MASK);
  145. for (vaddr = (unsigned long)(buffer);
  146. vaddr < ((unsigned long)(buffer) + (page_cnt * PAGE_SIZE));
  147. vaddr += PAGE_SIZE) {
  148. vpg = vmalloc_to_page((const void *)vaddr);
  149. SetPageReserved(vpg);
  150. }
  151. spin_lock_bh(&pl_info->log_lock);
  152. if (pl_info->buf != NULL)
  153. pktlog_release_buf(pdev_txrx_handle);
  154. pl_info->buf = buffer;
  155. spin_unlock_bh(&pl_info->log_lock);
  156. return 0;
  157. }
  158. void pktlog_release_buf(ol_txrx_pdev_handle pdev_txrx_handle)
  159. {
  160. unsigned long page_cnt;
  161. unsigned long vaddr;
  162. struct page *vpg;
  163. struct ath_pktlog_info *pl_info;
  164. if (!pdev_txrx_handle || !pdev_txrx_handle->pl_dev) {
  165. printk(PKTLOG_TAG
  166. "%s: Unable to allocate buffer"
  167. "scn or scn->pdev_txrx_handle->pl_dev is null\n",
  168. __func__);
  169. return;
  170. }
  171. pl_info = pdev_txrx_handle->pl_dev->pl_info;
  172. page_cnt = ((sizeof(*(pl_info->buf)) + pl_info->buf_size) /
  173. PAGE_SIZE) + 1;
  174. for (vaddr = (unsigned long)(pl_info->buf);
  175. vaddr < (unsigned long)(pl_info->buf) + (page_cnt * PAGE_SIZE);
  176. vaddr += PAGE_SIZE) {
  177. vpg = vmalloc_to_page((const void *)vaddr);
  178. ClearPageReserved(vpg);
  179. }
  180. vfree(pl_info->buf);
  181. pl_info->buf = NULL;
  182. }
  183. static void pktlog_cleanup(struct ath_pktlog_info *pl_info)
  184. {
  185. pl_info->log_state = 0;
  186. PKTLOG_LOCK_DESTROY(pl_info);
  187. mutex_destroy(&pl_info->pktlog_mutex);
  188. }
  189. /* sysctl procfs handler to enable pktlog */
  190. static int
  191. qdf_sysctl_decl(ath_sysctl_pktlog_enable, ctl, write, filp, buffer, lenp, ppos)
  192. {
  193. int ret, enable;
  194. ol_ath_generic_softc_handle scn;
  195. struct ol_pktlog_dev_t *pl_dev;
  196. scn = (ol_ath_generic_softc_handle) ctl->extra1;
  197. if (!scn) {
  198. printk("%s: Invalid scn context\n", __func__);
  199. ASSERT(0);
  200. return -EINVAL;
  201. }
  202. pl_dev = cds_get_pl_handle();
  203. if (!pl_dev) {
  204. printk("%s: Invalid pktlog context\n", __func__);
  205. ASSERT(0);
  206. return -ENODEV;
  207. }
  208. ctl->data = &enable;
  209. ctl->maxlen = sizeof(enable);
  210. if (write) {
  211. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  212. lenp, ppos);
  213. if (ret == 0)
  214. ret = pl_dev->pl_funcs->pktlog_enable(
  215. (struct hif_opaque_softc *)scn, enable,
  216. cds_is_packet_log_enabled(), 0, 1);
  217. else
  218. QDF_TRACE(QDF_MODULE_ID_SYS, QDF_TRACE_LEVEL_DEBUG,
  219. "Line:%d %s:proc_dointvec failed reason %d",
  220. __LINE__, __func__, ret);
  221. } else {
  222. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  223. lenp, ppos);
  224. if (ret)
  225. QDF_TRACE(QDF_MODULE_ID_SYS, QDF_TRACE_LEVEL_DEBUG,
  226. "Line:%d %s:proc_dointvec failed reason %d",
  227. __LINE__, __func__, ret);
  228. }
  229. ctl->data = NULL;
  230. ctl->maxlen = 0;
  231. return ret;
  232. }
  233. static int get_pktlog_bufsize(struct ol_pktlog_dev_t *pl_dev)
  234. {
  235. return pl_dev->pl_info->buf_size;
  236. }
  237. /* sysctl procfs handler to set/get pktlog size */
  238. static int
  239. qdf_sysctl_decl(ath_sysctl_pktlog_size, ctl, write, filp, buffer, lenp, ppos)
  240. {
  241. int ret, size;
  242. ol_ath_generic_softc_handle scn;
  243. struct ol_pktlog_dev_t *pl_dev;
  244. scn = (ol_ath_generic_softc_handle) ctl->extra1;
  245. if (!scn) {
  246. printk("%s: Invalid scn context\n", __func__);
  247. ASSERT(0);
  248. return -EINVAL;
  249. }
  250. pl_dev = cds_get_pl_handle();
  251. if (!pl_dev) {
  252. printk("%s: Invalid pktlog handle\n", __func__);
  253. ASSERT(0);
  254. return -ENODEV;
  255. }
  256. ctl->data = &size;
  257. ctl->maxlen = sizeof(size);
  258. if (write) {
  259. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  260. lenp, ppos);
  261. if (ret == 0)
  262. ret = pl_dev->pl_funcs->pktlog_setsize(
  263. (struct hif_opaque_softc *)scn, size);
  264. } else {
  265. size = get_pktlog_bufsize(pl_dev);
  266. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  267. lenp, ppos);
  268. }
  269. ctl->data = NULL;
  270. ctl->maxlen = 0;
  271. return ret;
  272. }
  273. /* Register sysctl table */
  274. static int pktlog_sysctl_register(struct hif_opaque_softc *scn)
  275. {
  276. struct ol_pktlog_dev_t *pl_dev = cds_get_pl_handle();
  277. struct ath_pktlog_info_lnx *pl_info_lnx;
  278. char *proc_name;
  279. if (pl_dev) {
  280. pl_info_lnx = PL_INFO_LNX(pl_dev->pl_info);
  281. proc_name = pl_dev->name;
  282. } else {
  283. pl_info_lnx = PL_INFO_LNX(g_pktlog_info);
  284. proc_name = PKTLOG_PROC_SYSTEM;
  285. }
  286. /*
  287. * Setup the sysctl table for creating the following sysctl entries:
  288. * /proc/sys/PKTLOG_PROC_DIR/<adapter>/enable for enabling/disabling
  289. * pktlog
  290. * /proc/sys/PKTLOG_PROC_DIR/<adapter>/size for changing the buffer size
  291. */
  292. memset(pl_info_lnx->sysctls, 0, sizeof(pl_info_lnx->sysctls));
  293. pl_info_lnx->sysctls[0].procname = PKTLOG_PROC_DIR;
  294. pl_info_lnx->sysctls[0].mode = PKTLOG_PROCSYS_DIR_PERM;
  295. pl_info_lnx->sysctls[0].child = &pl_info_lnx->sysctls[2];
  296. /* [1] is NULL terminator */
  297. pl_info_lnx->sysctls[2].procname = proc_name;
  298. pl_info_lnx->sysctls[2].mode = PKTLOG_PROCSYS_DIR_PERM;
  299. pl_info_lnx->sysctls[2].child = &pl_info_lnx->sysctls[4];
  300. /* [3] is NULL terminator */
  301. pl_info_lnx->sysctls[4].procname = "enable";
  302. pl_info_lnx->sysctls[4].mode = PKTLOG_PROCSYS_PERM;
  303. pl_info_lnx->sysctls[4].proc_handler = ath_sysctl_pktlog_enable;
  304. pl_info_lnx->sysctls[4].extra1 = scn;
  305. pl_info_lnx->sysctls[5].procname = "size";
  306. pl_info_lnx->sysctls[5].mode = PKTLOG_PROCSYS_PERM;
  307. pl_info_lnx->sysctls[5].proc_handler = ath_sysctl_pktlog_size;
  308. pl_info_lnx->sysctls[5].extra1 = scn;
  309. pl_info_lnx->sysctls[6].procname = "options";
  310. pl_info_lnx->sysctls[6].mode = PKTLOG_PROCSYS_PERM;
  311. pl_info_lnx->sysctls[6].proc_handler = proc_dointvec;
  312. pl_info_lnx->sysctls[6].data = &pl_info_lnx->info.options;
  313. pl_info_lnx->sysctls[6].maxlen = sizeof(pl_info_lnx->info.options);
  314. pl_info_lnx->sysctls[7].procname = "sack_thr";
  315. pl_info_lnx->sysctls[7].mode = PKTLOG_PROCSYS_PERM;
  316. pl_info_lnx->sysctls[7].proc_handler = proc_dointvec;
  317. pl_info_lnx->sysctls[7].data = &pl_info_lnx->info.sack_thr;
  318. pl_info_lnx->sysctls[7].maxlen = sizeof(pl_info_lnx->info.sack_thr);
  319. pl_info_lnx->sysctls[8].procname = "tail_length";
  320. pl_info_lnx->sysctls[8].mode = PKTLOG_PROCSYS_PERM;
  321. pl_info_lnx->sysctls[8].proc_handler = proc_dointvec;
  322. pl_info_lnx->sysctls[8].data = &pl_info_lnx->info.tail_length;
  323. pl_info_lnx->sysctls[8].maxlen = sizeof(pl_info_lnx->info.tail_length);
  324. pl_info_lnx->sysctls[9].procname = "thruput_thresh";
  325. pl_info_lnx->sysctls[9].mode = PKTLOG_PROCSYS_PERM;
  326. pl_info_lnx->sysctls[9].proc_handler = proc_dointvec;
  327. pl_info_lnx->sysctls[9].data = &pl_info_lnx->info.thruput_thresh;
  328. pl_info_lnx->sysctls[9].maxlen =
  329. sizeof(pl_info_lnx->info.thruput_thresh);
  330. pl_info_lnx->sysctls[10].procname = "phyerr_thresh";
  331. pl_info_lnx->sysctls[10].mode = PKTLOG_PROCSYS_PERM;
  332. pl_info_lnx->sysctls[10].proc_handler = proc_dointvec;
  333. pl_info_lnx->sysctls[10].data = &pl_info_lnx->info.phyerr_thresh;
  334. pl_info_lnx->sysctls[10].maxlen =
  335. sizeof(pl_info_lnx->info.phyerr_thresh);
  336. pl_info_lnx->sysctls[11].procname = "per_thresh";
  337. pl_info_lnx->sysctls[11].mode = PKTLOG_PROCSYS_PERM;
  338. pl_info_lnx->sysctls[11].proc_handler = proc_dointvec;
  339. pl_info_lnx->sysctls[11].data = &pl_info_lnx->info.per_thresh;
  340. pl_info_lnx->sysctls[11].maxlen = sizeof(pl_info_lnx->info.per_thresh);
  341. pl_info_lnx->sysctls[12].procname = "trigger_interval";
  342. pl_info_lnx->sysctls[12].mode = PKTLOG_PROCSYS_PERM;
  343. pl_info_lnx->sysctls[12].proc_handler = proc_dointvec;
  344. pl_info_lnx->sysctls[12].data = &pl_info_lnx->info.trigger_interval;
  345. pl_info_lnx->sysctls[12].maxlen =
  346. sizeof(pl_info_lnx->info.trigger_interval);
  347. /* [13] is NULL terminator */
  348. /* and register everything */
  349. /* register_sysctl_table changed from 2.6.21 onwards */
  350. pl_info_lnx->sysctl_header =
  351. register_sysctl_table(pl_info_lnx->sysctls);
  352. if (!pl_info_lnx->sysctl_header) {
  353. printk("%s: failed to register sysctls!\n", proc_name);
  354. return -EINVAL;
  355. }
  356. return 0;
  357. }
  358. /*
  359. * Initialize logging for system or adapter
  360. * Parameter scn should be NULL for system wide logging
  361. */
  362. static int pktlog_attach(struct hif_opaque_softc *scn)
  363. {
  364. struct ol_pktlog_dev_t *pl_dev;
  365. struct ath_pktlog_info_lnx *pl_info_lnx;
  366. char *proc_name;
  367. struct proc_dir_entry *proc_entry;
  368. pl_dev = cds_get_pl_handle();
  369. if (pl_dev != NULL) {
  370. pl_info_lnx = kmalloc(sizeof(*pl_info_lnx), GFP_KERNEL);
  371. if (pl_info_lnx == NULL) {
  372. printk(PKTLOG_TAG "%s:allocation failed for pl_info\n",
  373. __func__);
  374. return -ENOMEM;
  375. }
  376. pl_dev->pl_info = &pl_info_lnx->info;
  377. pl_dev->name = WLANDEV_BASENAME;
  378. proc_name = pl_dev->name;
  379. if (!pl_dev->pl_funcs)
  380. pl_dev->pl_funcs = &ol_pl_funcs;
  381. /*
  382. * Valid for both direct attach and offload architecture
  383. */
  384. pl_dev->pl_funcs->pktlog_init(scn);
  385. } else {
  386. return -EINVAL;
  387. }
  388. /*
  389. * initialize log info
  390. * might be good to move to pktlog_init
  391. */
  392. /* pl_dev->tgt_pktlog_alloced = false; */
  393. pl_dev->vendor_cmd_send = false;
  394. pl_info_lnx->proc_entry = NULL;
  395. pl_info_lnx->sysctl_header = NULL;
  396. proc_entry = proc_create_data(proc_name, PKTLOG_PROC_PERM,
  397. g_pktlog_pde, &pktlog_fops,
  398. &pl_info_lnx->info);
  399. if (proc_entry == NULL) {
  400. printk(PKTLOG_TAG "%s: create_proc_entry failed for %s\n",
  401. __func__, proc_name);
  402. goto attach_fail1;
  403. }
  404. pl_info_lnx->proc_entry = proc_entry;
  405. if (pktlog_sysctl_register(scn)) {
  406. printk(PKTLOG_TAG "%s: sysctl register failed for %s\n",
  407. __func__, proc_name);
  408. goto attach_fail2;
  409. }
  410. return 0;
  411. attach_fail2:
  412. remove_proc_entry(proc_name, g_pktlog_pde);
  413. attach_fail1:
  414. if (pl_dev)
  415. kfree(pl_dev->pl_info);
  416. return -EINVAL;
  417. }
  418. static void pktlog_sysctl_unregister(struct ol_pktlog_dev_t *pl_dev)
  419. {
  420. struct ath_pktlog_info_lnx *pl_info_lnx;
  421. if (!pl_dev) {
  422. printk("%s: Invalid pktlog context\n", __func__);
  423. ASSERT(0);
  424. return;
  425. }
  426. pl_info_lnx = (pl_dev) ? PL_INFO_LNX(pl_dev->pl_info) :
  427. PL_INFO_LNX(g_pktlog_info);
  428. if (pl_info_lnx->sysctl_header) {
  429. unregister_sysctl_table(pl_info_lnx->sysctl_header);
  430. pl_info_lnx->sysctl_header = NULL;
  431. }
  432. }
  433. static void pktlog_detach(struct ol_txrx_pdev_t *handle)
  434. {
  435. struct ol_txrx_pdev_t *txrx_pdev;
  436. struct ol_pktlog_dev_t *pl_dev;
  437. struct ath_pktlog_info *pl_info;
  438. txrx_pdev = handle;
  439. if (!txrx_pdev) {
  440. printk("%s: Invalid txrx_pdev context\n", __func__);
  441. ASSERT(0);
  442. return;
  443. }
  444. pl_dev = txrx_pdev->pl_dev;
  445. if (!pl_dev) {
  446. printk("%s: Invalid pktlog context\n", __func__);
  447. ASSERT(0);
  448. return;
  449. }
  450. pl_info = pl_dev->pl_info;
  451. remove_proc_entry(WLANDEV_BASENAME, g_pktlog_pde);
  452. pktlog_sysctl_unregister(pl_dev);
  453. spin_lock_bh(&pl_info->log_lock);
  454. if (pl_info->buf) {
  455. pktlog_release_buf(txrx_pdev);
  456. pl_dev->tgt_pktlog_alloced = false;
  457. }
  458. spin_unlock_bh(&pl_info->log_lock);
  459. pktlog_cleanup(pl_info);
  460. if (pl_dev) {
  461. kfree(pl_info);
  462. pl_dev->pl_info = NULL;
  463. }
  464. }
  465. static int __pktlog_open(struct inode *i, struct file *f)
  466. {
  467. struct hif_opaque_softc *scn;
  468. struct ol_pktlog_dev_t *pl_dev;
  469. struct ath_pktlog_info *pl_info;
  470. int ret = 0;
  471. PKTLOG_MOD_INC_USE_COUNT;
  472. pl_info = (struct ath_pktlog_info *)
  473. PDE_DATA(f->f_path.dentry->d_inode);
  474. if (!pl_info) {
  475. pr_err("%s: pl_info NULL", __func__);
  476. return -EINVAL;
  477. }
  478. if (pl_info->curr_pkt_state != PKTLOG_OPR_NOT_IN_PROGRESS) {
  479. pr_info("%s: plinfo state (%d) != PKTLOG_OPR_NOT_IN_PROGRESS",
  480. __func__, pl_info->curr_pkt_state);
  481. return -EBUSY;
  482. }
  483. if (cds_is_load_or_unload_in_progress() || cds_is_driver_recovering()) {
  484. pr_info("%s: Load/Unload or recovery is in progress", __func__);
  485. return -EAGAIN;
  486. }
  487. pl_info->curr_pkt_state = PKTLOG_OPR_IN_PROGRESS_READ_START;
  488. scn = cds_get_context(QDF_MODULE_ID_HIF);
  489. if (!scn) {
  490. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  491. qdf_print("%s: Invalid scn context\n", __func__);
  492. ASSERT(0);
  493. return -EINVAL;
  494. }
  495. pl_dev = cds_get_pl_handle();
  496. if (!pl_dev) {
  497. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  498. qdf_print("%s: Invalid pktlog handle\n", __func__);
  499. ASSERT(0);
  500. return -ENODEV;
  501. }
  502. pl_info->init_saved_state = pl_info->log_state;
  503. if (!pl_info->log_state) {
  504. /* Pktlog is already disabled.
  505. * Proceed to read directly.
  506. */
  507. pl_info->curr_pkt_state =
  508. PKTLOG_OPR_IN_PROGRESS_READ_START_PKTLOG_DISABLED;
  509. return ret;
  510. }
  511. /* Disbable the pktlog internally. */
  512. ret = pl_dev->pl_funcs->pktlog_disable(scn);
  513. pl_info->log_state = 0;
  514. pl_info->curr_pkt_state =
  515. PKTLOG_OPR_IN_PROGRESS_READ_START_PKTLOG_DISABLED;
  516. return ret;
  517. }
  518. static int pktlog_open(struct inode *i, struct file *f)
  519. {
  520. int ret;
  521. cds_ssr_protect(__func__);
  522. ret = __pktlog_open(i, f);
  523. cds_ssr_unprotect(__func__);
  524. return ret;
  525. }
  526. static int __pktlog_release(struct inode *i, struct file *f)
  527. {
  528. struct hif_opaque_softc *scn;
  529. struct ol_pktlog_dev_t *pl_dev;
  530. struct ath_pktlog_info *pl_info;
  531. int ret = 0;
  532. PKTLOG_MOD_DEC_USE_COUNT;
  533. pl_info = (struct ath_pktlog_info *)
  534. PDE_DATA(f->f_path.dentry->d_inode);
  535. if (!pl_info)
  536. return -EINVAL;
  537. if (cds_is_load_or_unload_in_progress() || cds_is_driver_recovering()) {
  538. pr_info("%s: Load/Unload or recovery is in progress", __func__);
  539. return -EAGAIN;
  540. }
  541. scn = cds_get_context(QDF_MODULE_ID_HIF);
  542. if (!scn) {
  543. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  544. qdf_print("%s: Invalid scn context\n", __func__);
  545. ASSERT(0);
  546. return -EINVAL;
  547. }
  548. pl_dev = cds_get_pl_handle();
  549. if (!pl_dev) {
  550. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  551. qdf_print("%s: Invalid pktlog handle\n", __func__);
  552. ASSERT(0);
  553. return -ENODEV;
  554. }
  555. pl_info->curr_pkt_state = PKTLOG_OPR_IN_PROGRESS_READ_COMPLETE;
  556. /*clear pktlog buffer.*/
  557. pktlog_clearbuff(scn, true);
  558. pl_info->log_state = pl_info->init_saved_state;
  559. pl_info->init_saved_state = 0;
  560. /*Enable pktlog again*/
  561. ret = pl_dev->pl_funcs->pktlog_enable(
  562. (struct hif_opaque_softc *)scn, pl_info->log_state,
  563. cds_is_packet_log_enabled(), 0, 1);
  564. if (ret != 0)
  565. pr_warn("%s: pktlog cannot be enabled. ret value %d\n",
  566. __func__, ret);
  567. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  568. return ret;
  569. }
  570. static int pktlog_release(struct inode *i, struct file *f)
  571. {
  572. int ret;
  573. cds_ssr_protect(__func__);
  574. ret = __pktlog_release(i, f);
  575. cds_ssr_unprotect(__func__);
  576. return ret;
  577. }
  578. #ifndef MIN
  579. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  580. #endif
  581. /**
  582. * pktlog_read_proc_entry() - This function is used to read data from the
  583. * proc entry into the readers buffer
  584. * @buf: Readers buffer
  585. * @nbytes: Number of bytes to read
  586. * @ppos: Offset within the drivers buffer
  587. * @pl_info: Packet log information pointer
  588. * @read_complete: Boolean value indication whether read is complete
  589. *
  590. * This function is used to read data from the proc entry into the readers
  591. * buffer. Its functionality is similar to 'pktlog_read' which does
  592. * copy to user to the user space buffer
  593. *
  594. * Return: Number of bytes read from the buffer
  595. *
  596. */
  597. ssize_t
  598. pktlog_read_proc_entry(char *buf, size_t nbytes, loff_t *ppos,
  599. struct ath_pktlog_info *pl_info, bool *read_complete)
  600. {
  601. size_t bufhdr_size;
  602. size_t count = 0, ret_val = 0;
  603. int rem_len;
  604. int start_offset, end_offset;
  605. int fold_offset, ppos_data, cur_rd_offset, cur_wr_offset;
  606. struct ath_pktlog_buf *log_buf;
  607. spin_lock_bh(&pl_info->log_lock);
  608. log_buf = pl_info->buf;
  609. *read_complete = false;
  610. if (log_buf == NULL) {
  611. *read_complete = true;
  612. spin_unlock_bh(&pl_info->log_lock);
  613. return 0;
  614. }
  615. if (*ppos == 0 && pl_info->log_state) {
  616. pl_info->saved_state = pl_info->log_state;
  617. pl_info->log_state = 0;
  618. }
  619. bufhdr_size = sizeof(log_buf->bufhdr);
  620. /* copy valid log entries from circular buffer into user space */
  621. rem_len = nbytes;
  622. count = 0;
  623. if (*ppos < bufhdr_size) {
  624. count = MIN((bufhdr_size - *ppos), rem_len);
  625. qdf_mem_copy(buf, ((char *)&log_buf->bufhdr) + *ppos,
  626. count);
  627. rem_len -= count;
  628. ret_val += count;
  629. }
  630. start_offset = log_buf->rd_offset;
  631. cur_wr_offset = log_buf->wr_offset;
  632. if ((rem_len == 0) || (start_offset < 0))
  633. goto rd_done;
  634. fold_offset = -1;
  635. cur_rd_offset = start_offset;
  636. /* Find the last offset and fold-offset if the buffer is folded */
  637. do {
  638. struct ath_pktlog_hdr *log_hdr;
  639. int log_data_offset;
  640. log_hdr = (struct ath_pktlog_hdr *) (log_buf->log_data +
  641. cur_rd_offset);
  642. log_data_offset = cur_rd_offset + sizeof(struct ath_pktlog_hdr);
  643. if ((fold_offset == -1)
  644. && ((pl_info->buf_size - log_data_offset)
  645. <= log_hdr->size))
  646. fold_offset = log_data_offset - 1;
  647. PKTLOG_MOV_RD_IDX(cur_rd_offset, log_buf, pl_info->buf_size);
  648. if ((fold_offset == -1) && (cur_rd_offset == 0)
  649. && (cur_rd_offset != cur_wr_offset))
  650. fold_offset = log_data_offset + log_hdr->size - 1;
  651. end_offset = log_data_offset + log_hdr->size - 1;
  652. } while (cur_rd_offset != cur_wr_offset);
  653. ppos_data = *ppos + ret_val - bufhdr_size + start_offset;
  654. if (fold_offset == -1) {
  655. if (ppos_data > end_offset)
  656. goto rd_done;
  657. count = MIN(rem_len, (end_offset - ppos_data + 1));
  658. qdf_mem_copy(buf + ret_val,
  659. log_buf->log_data + ppos_data,
  660. count);
  661. ret_val += count;
  662. rem_len -= count;
  663. } else {
  664. if (ppos_data <= fold_offset) {
  665. count = MIN(rem_len, (fold_offset - ppos_data + 1));
  666. qdf_mem_copy(buf + ret_val,
  667. log_buf->log_data + ppos_data,
  668. count);
  669. ret_val += count;
  670. rem_len -= count;
  671. }
  672. if (rem_len == 0)
  673. goto rd_done;
  674. ppos_data =
  675. *ppos + ret_val - (bufhdr_size +
  676. (fold_offset - start_offset + 1));
  677. if (ppos_data <= end_offset) {
  678. count = MIN(rem_len, (end_offset - ppos_data + 1));
  679. qdf_mem_copy(buf + ret_val,
  680. log_buf->log_data + ppos_data,
  681. count);
  682. ret_val += count;
  683. rem_len -= count;
  684. }
  685. }
  686. rd_done:
  687. if ((ret_val < nbytes) && pl_info->saved_state) {
  688. pl_info->log_state = pl_info->saved_state;
  689. pl_info->saved_state = 0;
  690. }
  691. *ppos += ret_val;
  692. if (ret_val == 0) {
  693. /* Write pointer might have been updated during the read.
  694. * So, if some data is written into, lets not reset the pointers
  695. * We can continue to read from the offset position
  696. */
  697. if (cur_wr_offset != log_buf->wr_offset) {
  698. *read_complete = false;
  699. } else {
  700. pl_info->buf->rd_offset = -1;
  701. pl_info->buf->wr_offset = 0;
  702. pl_info->buf->bytes_written = 0;
  703. pl_info->buf->offset = PKTLOG_READ_OFFSET;
  704. *read_complete = true;
  705. }
  706. }
  707. spin_unlock_bh(&pl_info->log_lock);
  708. return ret_val;
  709. }
  710. static ssize_t
  711. __pktlog_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
  712. {
  713. size_t bufhdr_size;
  714. size_t count = 0, ret_val = 0;
  715. int rem_len;
  716. int start_offset, end_offset;
  717. int fold_offset, ppos_data, cur_rd_offset;
  718. struct ath_pktlog_info *pl_info;
  719. struct ath_pktlog_buf *log_buf;
  720. if (cds_is_load_or_unload_in_progress() || cds_is_driver_recovering()) {
  721. pr_info("%s: Load/Unload or recovery is in progress", __func__);
  722. return -EAGAIN;
  723. }
  724. pl_info = (struct ath_pktlog_info *)
  725. PDE_DATA(file->f_path.dentry->d_inode);
  726. if (!pl_info)
  727. return 0;
  728. spin_lock_bh(&pl_info->log_lock);
  729. log_buf = pl_info->buf;
  730. if (log_buf == NULL) {
  731. spin_unlock_bh(&pl_info->log_lock);
  732. return 0;
  733. }
  734. if (pl_info->log_state) {
  735. /* Read is not allowed when write is going on
  736. * When issuing cat command, ensure to send
  737. * pktlog disable command first.
  738. */
  739. spin_unlock_bh(&pl_info->log_lock);
  740. return -EINVAL;
  741. }
  742. if (*ppos == 0 && pl_info->log_state) {
  743. pl_info->saved_state = pl_info->log_state;
  744. pl_info->log_state = 0;
  745. }
  746. bufhdr_size = sizeof(log_buf->bufhdr);
  747. /* copy valid log entries from circular buffer into user space */
  748. rem_len = nbytes;
  749. count = 0;
  750. if (*ppos < bufhdr_size) {
  751. count = QDF_MIN((bufhdr_size - *ppos), rem_len);
  752. spin_unlock_bh(&pl_info->log_lock);
  753. if (copy_to_user(buf, ((char *)&log_buf->bufhdr) + *ppos,
  754. count))
  755. return -EFAULT;
  756. rem_len -= count;
  757. ret_val += count;
  758. spin_lock_bh(&pl_info->log_lock);
  759. }
  760. start_offset = log_buf->rd_offset;
  761. if ((rem_len == 0) || (start_offset < 0))
  762. goto rd_done;
  763. fold_offset = -1;
  764. cur_rd_offset = start_offset;
  765. /* Find the last offset and fold-offset if the buffer is folded */
  766. do {
  767. struct ath_pktlog_hdr *log_hdr;
  768. int log_data_offset;
  769. log_hdr = (struct ath_pktlog_hdr *)(log_buf->log_data +
  770. cur_rd_offset);
  771. log_data_offset = cur_rd_offset + sizeof(struct ath_pktlog_hdr);
  772. if ((fold_offset == -1)
  773. && ((pl_info->buf_size - log_data_offset)
  774. <= log_hdr->size))
  775. fold_offset = log_data_offset - 1;
  776. PKTLOG_MOV_RD_IDX(cur_rd_offset, log_buf, pl_info->buf_size);
  777. if ((fold_offset == -1) && (cur_rd_offset == 0)
  778. && (cur_rd_offset != log_buf->wr_offset))
  779. fold_offset = log_data_offset + log_hdr->size - 1;
  780. end_offset = log_data_offset + log_hdr->size - 1;
  781. } while (cur_rd_offset != log_buf->wr_offset);
  782. ppos_data = *ppos + ret_val - bufhdr_size + start_offset;
  783. if (fold_offset == -1) {
  784. if (ppos_data > end_offset)
  785. goto rd_done;
  786. count = QDF_MIN(rem_len, (end_offset - ppos_data + 1));
  787. spin_unlock_bh(&pl_info->log_lock);
  788. if (copy_to_user(buf + ret_val,
  789. log_buf->log_data + ppos_data, count))
  790. return -EFAULT;
  791. ret_val += count;
  792. rem_len -= count;
  793. spin_lock_bh(&pl_info->log_lock);
  794. } else {
  795. if (ppos_data <= fold_offset) {
  796. count = QDF_MIN(rem_len, (fold_offset - ppos_data + 1));
  797. spin_unlock_bh(&pl_info->log_lock);
  798. if (copy_to_user(buf + ret_val,
  799. log_buf->log_data + ppos_data, count))
  800. return -EFAULT;
  801. ret_val += count;
  802. rem_len -= count;
  803. spin_lock_bh(&pl_info->log_lock);
  804. }
  805. if (rem_len == 0)
  806. goto rd_done;
  807. ppos_data =
  808. *ppos + ret_val - (bufhdr_size +
  809. (fold_offset - start_offset + 1));
  810. if (ppos_data <= end_offset) {
  811. count = QDF_MIN(rem_len, (end_offset - ppos_data + 1));
  812. spin_unlock_bh(&pl_info->log_lock);
  813. if (copy_to_user(buf + ret_val,
  814. log_buf->log_data + ppos_data, count))
  815. return -EFAULT;
  816. ret_val += count;
  817. rem_len -= count;
  818. spin_lock_bh(&pl_info->log_lock);
  819. }
  820. }
  821. rd_done:
  822. if ((ret_val < nbytes) && pl_info->saved_state) {
  823. pl_info->log_state = pl_info->saved_state;
  824. pl_info->saved_state = 0;
  825. }
  826. *ppos += ret_val;
  827. spin_unlock_bh(&pl_info->log_lock);
  828. return ret_val;
  829. }
  830. static ssize_t
  831. pktlog_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
  832. {
  833. size_t ret;
  834. struct ath_pktlog_info *pl_info;
  835. pl_info = (struct ath_pktlog_info *)
  836. PDE_DATA(file->f_path.dentry->d_inode);
  837. if (!pl_info)
  838. return 0;
  839. cds_ssr_protect(__func__);
  840. mutex_lock(&pl_info->pktlog_mutex);
  841. ret = __pktlog_read(file, buf, nbytes, ppos);
  842. mutex_unlock(&pl_info->pktlog_mutex);
  843. cds_ssr_unprotect(__func__);
  844. return ret;
  845. }
  846. #ifndef VMALLOC_VMADDR
  847. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  848. #endif
  849. /* vma operations for mapping vmalloced area to user space */
  850. static void pktlog_vopen(struct vm_area_struct *vma)
  851. {
  852. PKTLOG_MOD_INC_USE_COUNT;
  853. }
  854. static void pktlog_vclose(struct vm_area_struct *vma)
  855. {
  856. PKTLOG_MOD_DEC_USE_COUNT;
  857. }
  858. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
  859. static inline unsigned long pktlog_get_fault_address(struct vm_fault *vmf)
  860. {
  861. return (unsigned long)vmf->virtual_address;
  862. }
  863. #else
  864. static inline unsigned long pktlog_get_fault_address(struct vm_fault *vmf)
  865. {
  866. return vmf->address;
  867. }
  868. #endif /* KERNEL_VERSION(4, 10, 0) */
  869. static int
  870. pktlog_fault_handler(struct vm_area_struct *vma, struct vm_fault *vmf)
  871. {
  872. unsigned long address = pktlog_get_fault_address(vmf);
  873. if (address == 0UL)
  874. return VM_FAULT_NOPAGE;
  875. if (vmf->pgoff > vma->vm_end)
  876. return VM_FAULT_SIGBUS;
  877. get_page(virt_to_page((void *)address));
  878. vmf->page = virt_to_page((void *)address);
  879. return 0;
  880. }
  881. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 10, 0)
  882. static int pktlog_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  883. {
  884. return pktlog_fault_handler(vma, vmf);
  885. }
  886. #else
  887. static int pktlog_fault(struct vm_fault *vmf)
  888. {
  889. return pktlog_fault_handler(vmf->vma, vmf);
  890. }
  891. #endif
  892. static struct vm_operations_struct pktlog_vmops = {
  893. open: pktlog_vopen,
  894. close:pktlog_vclose,
  895. fault:pktlog_fault,
  896. };
  897. static int __pktlog_mmap(struct file *file, struct vm_area_struct *vma)
  898. {
  899. struct ath_pktlog_info *pl_info;
  900. pl_info = (struct ath_pktlog_info *)
  901. PDE_DATA(file->f_path.dentry->d_inode);
  902. if (vma->vm_pgoff != 0) {
  903. /* Entire buffer should be mapped */
  904. return -EINVAL;
  905. }
  906. if (!pl_info->buf) {
  907. printk(PKTLOG_TAG "%s: Log buffer unavailable\n", __func__);
  908. return -ENOMEM;
  909. }
  910. if (cds_is_load_or_unload_in_progress() || cds_is_driver_recovering()) {
  911. pr_info("%s: Load/Unload or recovery is in progress", __func__);
  912. return -EAGAIN;
  913. }
  914. vma->vm_flags |= VM_LOCKED;
  915. vma->vm_ops = &pktlog_vmops;
  916. pktlog_vopen(vma);
  917. return 0;
  918. }
  919. static int pktlog_mmap(struct file *file, struct vm_area_struct *vma)
  920. {
  921. int ret;
  922. cds_ssr_protect(__func__);
  923. ret = __pktlog_mmap(file, vma);
  924. cds_ssr_unprotect(__func__);
  925. return ret;
  926. }
  927. int pktlogmod_init(void *context)
  928. {
  929. int ret;
  930. /* create the proc directory entry */
  931. g_pktlog_pde = proc_mkdir(PKTLOG_PROC_DIR, NULL);
  932. if (g_pktlog_pde == NULL) {
  933. printk(PKTLOG_TAG "%s: proc_mkdir failed\n", __func__);
  934. return -EPERM;
  935. }
  936. /* Attach packet log */
  937. ret = pktlog_attach((struct hif_opaque_softc *)context);
  938. if (ret)
  939. goto attach_fail;
  940. return ret;
  941. attach_fail:
  942. remove_proc_entry(PKTLOG_PROC_DIR, NULL);
  943. g_pktlog_pde = NULL;
  944. return ret;
  945. }
  946. void pktlogmod_exit(struct ol_txrx_pdev_t *handle)
  947. {
  948. struct ol_pktlog_dev_t *pl_dev;
  949. pl_dev = ol_get_pl_handle(handle);
  950. if (!pl_dev || g_pktlog_pde == NULL)
  951. return;
  952. pktlog_detach(handle);
  953. /*
  954. * pdev kill needs to be implemented
  955. */
  956. remove_proc_entry(PKTLOG_PROC_DIR, NULL);
  957. }
  958. #endif