linux_ac.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. ol_txrx_pdev_handle pdev_txrx_handle;
  117. pdev_txrx_handle = cds_get_context(QDF_MODULE_ID_TXRX);
  118. if (!pdev_txrx_handle || !pdev_txrx_handle->pl_dev) {
  119. printk(PKTLOG_TAG
  120. "%s: Unable to allocate buffer "
  121. "scn or scn->pdev_txrx_handle->pl_dev is null\n",
  122. __func__);
  123. return -EINVAL;
  124. }
  125. pl_info = pdev_txrx_handle->pl_dev->pl_info;
  126. page_cnt = (sizeof(*(pl_info->buf)) + pl_info->buf_size) / PAGE_SIZE;
  127. pl_info->buf = vmalloc((page_cnt + 2) * PAGE_SIZE);
  128. if (pl_info->buf == NULL) {
  129. printk(PKTLOG_TAG
  130. "%s: Unable to allocate buffer "
  131. "(%d pages)\n", __func__, page_cnt);
  132. return -ENOMEM;
  133. }
  134. pl_info->buf = (struct ath_pktlog_buf *)
  135. (((unsigned long)(pl_info->buf) + PAGE_SIZE - 1)
  136. & PAGE_MASK);
  137. for (vaddr = (unsigned long)(pl_info->buf);
  138. vaddr < ((unsigned long)(pl_info->buf) + (page_cnt * PAGE_SIZE));
  139. vaddr += PAGE_SIZE) {
  140. vpg = vmalloc_to_page((const void *)vaddr);
  141. SetPageReserved(vpg);
  142. }
  143. return 0;
  144. }
  145. void pktlog_release_buf(ol_txrx_pdev_handle pdev_txrx_handle)
  146. {
  147. unsigned long page_cnt;
  148. unsigned long vaddr;
  149. struct page *vpg;
  150. struct ath_pktlog_info *pl_info;
  151. if (!pdev_txrx_handle || !pdev_txrx_handle->pl_dev) {
  152. printk(PKTLOG_TAG
  153. "%s: Unable to allocate buffer"
  154. "scn or scn->pdev_txrx_handle->pl_dev is null\n",
  155. __func__);
  156. return;
  157. }
  158. pl_info = pdev_txrx_handle->pl_dev->pl_info;
  159. page_cnt = ((sizeof(*(pl_info->buf)) + pl_info->buf_size) /
  160. PAGE_SIZE) + 1;
  161. for (vaddr = (unsigned long)(pl_info->buf);
  162. vaddr < (unsigned long)(pl_info->buf) + (page_cnt * PAGE_SIZE);
  163. vaddr += PAGE_SIZE) {
  164. vpg = vmalloc_to_page((const void *)vaddr);
  165. ClearPageReserved(vpg);
  166. }
  167. vfree(pl_info->buf);
  168. pl_info->buf = NULL;
  169. }
  170. static void pktlog_cleanup(struct ath_pktlog_info *pl_info)
  171. {
  172. pl_info->log_state = 0;
  173. PKTLOG_LOCK_DESTROY(pl_info);
  174. }
  175. /* sysctl procfs handler to enable pktlog */
  176. static int
  177. qdf_sysctl_decl(ath_sysctl_pktlog_enable, ctl, write, filp, buffer, lenp, ppos)
  178. {
  179. int ret, enable;
  180. ol_ath_generic_softc_handle scn;
  181. struct ol_pktlog_dev_t *pl_dev;
  182. scn = (ol_ath_generic_softc_handle) ctl->extra1;
  183. if (!scn) {
  184. printk("%s: Invalid scn context\n", __func__);
  185. ASSERT(0);
  186. return -EINVAL;
  187. }
  188. pl_dev = cds_get_pl_handle();
  189. if (!pl_dev) {
  190. printk("%s: Invalid pktlog context\n", __func__);
  191. ASSERT(0);
  192. return -ENODEV;
  193. }
  194. ctl->data = &enable;
  195. ctl->maxlen = sizeof(enable);
  196. if (write) {
  197. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  198. lenp, ppos);
  199. if (ret == 0)
  200. ret = pl_dev->pl_funcs->pktlog_enable(
  201. (struct hif_opaque_softc *)scn, enable,
  202. cds_is_packet_log_enabled(), 0, 1);
  203. else
  204. QDF_TRACE(QDF_MODULE_ID_SYS, QDF_TRACE_LEVEL_DEBUG,
  205. "Line:%d %s:proc_dointvec failed reason %d",
  206. __LINE__, __func__, ret);
  207. } else {
  208. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  209. lenp, ppos);
  210. if (ret)
  211. QDF_TRACE(QDF_MODULE_ID_SYS, QDF_TRACE_LEVEL_DEBUG,
  212. "Line:%d %s:proc_dointvec failed reason %d",
  213. __LINE__, __func__, ret);
  214. }
  215. ctl->data = NULL;
  216. ctl->maxlen = 0;
  217. return ret;
  218. }
  219. static int get_pktlog_bufsize(struct ol_pktlog_dev_t *pl_dev)
  220. {
  221. return pl_dev->pl_info->buf_size;
  222. }
  223. /* sysctl procfs handler to set/get pktlog size */
  224. static int
  225. qdf_sysctl_decl(ath_sysctl_pktlog_size, ctl, write, filp, buffer, lenp, ppos)
  226. {
  227. int ret, size;
  228. ol_ath_generic_softc_handle scn;
  229. struct ol_pktlog_dev_t *pl_dev;
  230. scn = (ol_ath_generic_softc_handle) ctl->extra1;
  231. if (!scn) {
  232. printk("%s: Invalid scn context\n", __func__);
  233. ASSERT(0);
  234. return -EINVAL;
  235. }
  236. pl_dev = cds_get_pl_handle();
  237. if (!pl_dev) {
  238. printk("%s: Invalid pktlog handle\n", __func__);
  239. ASSERT(0);
  240. return -ENODEV;
  241. }
  242. ctl->data = &size;
  243. ctl->maxlen = sizeof(size);
  244. if (write) {
  245. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  246. lenp, ppos);
  247. if (ret == 0)
  248. ret = pl_dev->pl_funcs->pktlog_setsize(
  249. (struct hif_opaque_softc *)scn, size);
  250. } else {
  251. size = get_pktlog_bufsize(pl_dev);
  252. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  253. lenp, ppos);
  254. }
  255. ctl->data = NULL;
  256. ctl->maxlen = 0;
  257. return ret;
  258. }
  259. /* Register sysctl table */
  260. static int pktlog_sysctl_register(struct hif_opaque_softc *scn)
  261. {
  262. struct ol_pktlog_dev_t *pl_dev = cds_get_pl_handle();
  263. struct ath_pktlog_info_lnx *pl_info_lnx;
  264. char *proc_name;
  265. if (pl_dev) {
  266. pl_info_lnx = PL_INFO_LNX(pl_dev->pl_info);
  267. proc_name = pl_dev->name;
  268. } else {
  269. pl_info_lnx = PL_INFO_LNX(g_pktlog_info);
  270. proc_name = PKTLOG_PROC_SYSTEM;
  271. }
  272. /*
  273. * Setup the sysctl table for creating the following sysctl entries:
  274. * /proc/sys/PKTLOG_PROC_DIR/<adapter>/enable for enabling/disabling
  275. * pktlog
  276. * /proc/sys/PKTLOG_PROC_DIR/<adapter>/size for changing the buffer size
  277. */
  278. memset(pl_info_lnx->sysctls, 0, sizeof(pl_info_lnx->sysctls));
  279. pl_info_lnx->sysctls[0].procname = PKTLOG_PROC_DIR;
  280. pl_info_lnx->sysctls[0].mode = PKTLOG_PROCSYS_DIR_PERM;
  281. pl_info_lnx->sysctls[0].child = &pl_info_lnx->sysctls[2];
  282. /* [1] is NULL terminator */
  283. pl_info_lnx->sysctls[2].procname = proc_name;
  284. pl_info_lnx->sysctls[2].mode = PKTLOG_PROCSYS_DIR_PERM;
  285. pl_info_lnx->sysctls[2].child = &pl_info_lnx->sysctls[4];
  286. /* [3] is NULL terminator */
  287. pl_info_lnx->sysctls[4].procname = "enable";
  288. pl_info_lnx->sysctls[4].mode = PKTLOG_PROCSYS_PERM;
  289. pl_info_lnx->sysctls[4].proc_handler = ath_sysctl_pktlog_enable;
  290. pl_info_lnx->sysctls[4].extra1 = scn;
  291. pl_info_lnx->sysctls[5].procname = "size";
  292. pl_info_lnx->sysctls[5].mode = PKTLOG_PROCSYS_PERM;
  293. pl_info_lnx->sysctls[5].proc_handler = ath_sysctl_pktlog_size;
  294. pl_info_lnx->sysctls[5].extra1 = scn;
  295. pl_info_lnx->sysctls[6].procname = "options";
  296. pl_info_lnx->sysctls[6].mode = PKTLOG_PROCSYS_PERM;
  297. pl_info_lnx->sysctls[6].proc_handler = proc_dointvec;
  298. pl_info_lnx->sysctls[6].data = &pl_info_lnx->info.options;
  299. pl_info_lnx->sysctls[6].maxlen = sizeof(pl_info_lnx->info.options);
  300. pl_info_lnx->sysctls[7].procname = "sack_thr";
  301. pl_info_lnx->sysctls[7].mode = PKTLOG_PROCSYS_PERM;
  302. pl_info_lnx->sysctls[7].proc_handler = proc_dointvec;
  303. pl_info_lnx->sysctls[7].data = &pl_info_lnx->info.sack_thr;
  304. pl_info_lnx->sysctls[7].maxlen = sizeof(pl_info_lnx->info.sack_thr);
  305. pl_info_lnx->sysctls[8].procname = "tail_length";
  306. pl_info_lnx->sysctls[8].mode = PKTLOG_PROCSYS_PERM;
  307. pl_info_lnx->sysctls[8].proc_handler = proc_dointvec;
  308. pl_info_lnx->sysctls[8].data = &pl_info_lnx->info.tail_length;
  309. pl_info_lnx->sysctls[8].maxlen = sizeof(pl_info_lnx->info.tail_length);
  310. pl_info_lnx->sysctls[9].procname = "thruput_thresh";
  311. pl_info_lnx->sysctls[9].mode = PKTLOG_PROCSYS_PERM;
  312. pl_info_lnx->sysctls[9].proc_handler = proc_dointvec;
  313. pl_info_lnx->sysctls[9].data = &pl_info_lnx->info.thruput_thresh;
  314. pl_info_lnx->sysctls[9].maxlen =
  315. sizeof(pl_info_lnx->info.thruput_thresh);
  316. pl_info_lnx->sysctls[10].procname = "phyerr_thresh";
  317. pl_info_lnx->sysctls[10].mode = PKTLOG_PROCSYS_PERM;
  318. pl_info_lnx->sysctls[10].proc_handler = proc_dointvec;
  319. pl_info_lnx->sysctls[10].data = &pl_info_lnx->info.phyerr_thresh;
  320. pl_info_lnx->sysctls[10].maxlen =
  321. sizeof(pl_info_lnx->info.phyerr_thresh);
  322. pl_info_lnx->sysctls[11].procname = "per_thresh";
  323. pl_info_lnx->sysctls[11].mode = PKTLOG_PROCSYS_PERM;
  324. pl_info_lnx->sysctls[11].proc_handler = proc_dointvec;
  325. pl_info_lnx->sysctls[11].data = &pl_info_lnx->info.per_thresh;
  326. pl_info_lnx->sysctls[11].maxlen = sizeof(pl_info_lnx->info.per_thresh);
  327. pl_info_lnx->sysctls[12].procname = "trigger_interval";
  328. pl_info_lnx->sysctls[12].mode = PKTLOG_PROCSYS_PERM;
  329. pl_info_lnx->sysctls[12].proc_handler = proc_dointvec;
  330. pl_info_lnx->sysctls[12].data = &pl_info_lnx->info.trigger_interval;
  331. pl_info_lnx->sysctls[12].maxlen =
  332. sizeof(pl_info_lnx->info.trigger_interval);
  333. /* [13] is NULL terminator */
  334. /* and register everything */
  335. /* register_sysctl_table changed from 2.6.21 onwards */
  336. pl_info_lnx->sysctl_header =
  337. register_sysctl_table(pl_info_lnx->sysctls);
  338. if (!pl_info_lnx->sysctl_header) {
  339. printk("%s: failed to register sysctls!\n", proc_name);
  340. return -EINVAL;
  341. }
  342. return 0;
  343. }
  344. /*
  345. * Initialize logging for system or adapter
  346. * Parameter scn should be NULL for system wide logging
  347. */
  348. static int pktlog_attach(struct hif_opaque_softc *scn)
  349. {
  350. struct ol_pktlog_dev_t *pl_dev;
  351. struct ath_pktlog_info_lnx *pl_info_lnx;
  352. char *proc_name;
  353. struct proc_dir_entry *proc_entry;
  354. pl_dev = cds_get_pl_handle();
  355. if (pl_dev != NULL) {
  356. pl_info_lnx = kmalloc(sizeof(*pl_info_lnx), GFP_KERNEL);
  357. if (pl_info_lnx == NULL) {
  358. printk(PKTLOG_TAG "%s:allocation failed for pl_info\n",
  359. __func__);
  360. return -ENOMEM;
  361. }
  362. pl_dev->pl_info = &pl_info_lnx->info;
  363. pl_dev->name = WLANDEV_BASENAME;
  364. proc_name = pl_dev->name;
  365. if (!pl_dev->pl_funcs)
  366. pl_dev->pl_funcs = &ol_pl_funcs;
  367. /*
  368. * Valid for both direct attach and offload architecture
  369. */
  370. pl_dev->pl_funcs->pktlog_init(scn);
  371. } else {
  372. return -EINVAL;
  373. }
  374. /*
  375. * initialize log info
  376. * might be good to move to pktlog_init
  377. */
  378. /* pl_dev->tgt_pktlog_alloced = false; */
  379. pl_dev->vendor_cmd_send = false;
  380. pl_info_lnx->proc_entry = NULL;
  381. pl_info_lnx->sysctl_header = NULL;
  382. proc_entry = proc_create_data(proc_name, PKTLOG_PROC_PERM,
  383. g_pktlog_pde, &pktlog_fops,
  384. &pl_info_lnx->info);
  385. if (proc_entry == NULL) {
  386. printk(PKTLOG_TAG "%s: create_proc_entry failed for %s\n",
  387. __func__, proc_name);
  388. goto attach_fail1;
  389. }
  390. pl_info_lnx->proc_entry = proc_entry;
  391. if (pktlog_sysctl_register(scn)) {
  392. printk(PKTLOG_TAG "%s: sysctl register failed for %s\n",
  393. __func__, proc_name);
  394. goto attach_fail2;
  395. }
  396. return 0;
  397. attach_fail2:
  398. remove_proc_entry(proc_name, g_pktlog_pde);
  399. attach_fail1:
  400. if (pl_dev)
  401. kfree(pl_dev->pl_info);
  402. return -EINVAL;
  403. }
  404. static void pktlog_sysctl_unregister(struct ol_pktlog_dev_t *pl_dev)
  405. {
  406. struct ath_pktlog_info_lnx *pl_info_lnx;
  407. if (!pl_dev) {
  408. printk("%s: Invalid pktlog context\n", __func__);
  409. ASSERT(0);
  410. return;
  411. }
  412. pl_info_lnx = (pl_dev) ? PL_INFO_LNX(pl_dev->pl_info) :
  413. PL_INFO_LNX(g_pktlog_info);
  414. if (pl_info_lnx->sysctl_header) {
  415. unregister_sysctl_table(pl_info_lnx->sysctl_header);
  416. pl_info_lnx->sysctl_header = NULL;
  417. }
  418. }
  419. static void pktlog_detach(struct ol_txrx_pdev_t *handle)
  420. {
  421. struct ol_txrx_pdev_t *txrx_pdev;
  422. struct ol_pktlog_dev_t *pl_dev;
  423. struct ath_pktlog_info *pl_info;
  424. txrx_pdev = handle;
  425. if (!txrx_pdev) {
  426. printk("%s: Invalid txrx_pdev context\n", __func__);
  427. ASSERT(0);
  428. return;
  429. }
  430. pl_dev = txrx_pdev->pl_dev;
  431. if (!pl_dev) {
  432. printk("%s: Invalid pktlog context\n", __func__);
  433. ASSERT(0);
  434. return;
  435. }
  436. pl_info = pl_dev->pl_info;
  437. remove_proc_entry(WLANDEV_BASENAME, g_pktlog_pde);
  438. pktlog_sysctl_unregister(pl_dev);
  439. spin_lock_bh(&pl_info->log_lock);
  440. if (pl_info->buf) {
  441. pktlog_release_buf(txrx_pdev);
  442. pl_dev->tgt_pktlog_alloced = false;
  443. }
  444. spin_unlock_bh(&pl_info->log_lock);
  445. pktlog_cleanup(pl_info);
  446. if (pl_dev) {
  447. kfree(pl_info);
  448. pl_dev->pl_info = NULL;
  449. }
  450. }
  451. static int pktlog_open(struct inode *i, struct file *f)
  452. {
  453. struct hif_opaque_softc *scn;
  454. struct ol_pktlog_dev_t *pl_dev;
  455. struct ath_pktlog_info *pl_info;
  456. int ret = 0;
  457. PKTLOG_MOD_INC_USE_COUNT;
  458. pl_info = (struct ath_pktlog_info *)
  459. PDE_DATA(f->f_path.dentry->d_inode);
  460. if (!pl_info) {
  461. pr_err("%s: pl_info NULL", __func__);
  462. return -EINVAL;
  463. }
  464. if (pl_info->curr_pkt_state != PKTLOG_OPR_NOT_IN_PROGRESS) {
  465. pr_info("%s: plinfo state (%d) != PKTLOG_OPR_NOT_IN_PROGRESS",
  466. __func__, pl_info->curr_pkt_state);
  467. return -EBUSY;
  468. }
  469. pl_info->curr_pkt_state = PKTLOG_OPR_IN_PROGRESS_READ_START;
  470. scn = cds_get_context(QDF_MODULE_ID_HIF);
  471. if (!scn) {
  472. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  473. qdf_print("%s: Invalid scn context\n", __func__);
  474. ASSERT(0);
  475. return -EINVAL;
  476. }
  477. pl_dev = cds_get_pl_handle();
  478. if (!pl_dev) {
  479. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  480. qdf_print("%s: Invalid pktlog handle\n", __func__);
  481. ASSERT(0);
  482. return -ENODEV;
  483. }
  484. pl_info->init_saved_state = pl_info->log_state;
  485. if (!pl_info->log_state) {
  486. /* Pktlog is already disabled.
  487. * Proceed to read directly.
  488. */
  489. pl_info->curr_pkt_state =
  490. PKTLOG_OPR_IN_PROGRESS_READ_START_PKTLOG_DISABLED;
  491. return ret;
  492. }
  493. /* Disbable the pktlog internally. */
  494. ret = pl_dev->pl_funcs->pktlog_disable(scn);
  495. pl_info->log_state = 0;
  496. pl_info->curr_pkt_state =
  497. PKTLOG_OPR_IN_PROGRESS_READ_START_PKTLOG_DISABLED;
  498. return ret;
  499. }
  500. static int pktlog_release(struct inode *i, struct file *f)
  501. {
  502. struct hif_opaque_softc *scn;
  503. struct ol_pktlog_dev_t *pl_dev;
  504. struct ath_pktlog_info *pl_info;
  505. int ret = 0;
  506. PKTLOG_MOD_DEC_USE_COUNT;
  507. pl_info = (struct ath_pktlog_info *)
  508. PDE_DATA(f->f_path.dentry->d_inode);
  509. if (!pl_info)
  510. return -EINVAL;
  511. scn = cds_get_context(QDF_MODULE_ID_HIF);
  512. if (!scn) {
  513. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  514. qdf_print("%s: Invalid scn context\n", __func__);
  515. ASSERT(0);
  516. return -EINVAL;
  517. }
  518. pl_dev = cds_get_pl_handle();
  519. if (!pl_dev) {
  520. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  521. qdf_print("%s: Invalid pktlog handle\n", __func__);
  522. ASSERT(0);
  523. return -ENODEV;
  524. }
  525. pl_info->curr_pkt_state = PKTLOG_OPR_IN_PROGRESS_READ_COMPLETE;
  526. /*clear pktlog buffer.*/
  527. pktlog_clearbuff(scn, true);
  528. pl_info->log_state = pl_info->init_saved_state;
  529. pl_info->init_saved_state = 0;
  530. /*Enable pktlog again*/
  531. ret = pl_dev->pl_funcs->pktlog_enable(
  532. (struct hif_opaque_softc *)scn, pl_info->log_state,
  533. cds_is_packet_log_enabled(), 0, 1);
  534. if (ret != 0)
  535. pr_warn("%s: pktlog cannot be enabled. ret value %d\n",
  536. __func__, ret);
  537. pl_info->curr_pkt_state = PKTLOG_OPR_NOT_IN_PROGRESS;
  538. return ret;
  539. }
  540. #ifndef MIN
  541. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  542. #endif
  543. /**
  544. * pktlog_read_proc_entry() - This function is used to read data from the
  545. * proc entry into the readers buffer
  546. * @buf: Readers buffer
  547. * @nbytes: Number of bytes to read
  548. * @ppos: Offset within the drivers buffer
  549. * @pl_info: Packet log information pointer
  550. * @read_complete: Boolean value indication whether read is complete
  551. *
  552. * This function is used to read data from the proc entry into the readers
  553. * buffer. Its functionality is similar to 'pktlog_read' which does
  554. * copy to user to the user space buffer
  555. *
  556. * Return: Number of bytes read from the buffer
  557. *
  558. */
  559. ssize_t
  560. pktlog_read_proc_entry(char *buf, size_t nbytes, loff_t *ppos,
  561. struct ath_pktlog_info *pl_info, bool *read_complete)
  562. {
  563. size_t bufhdr_size;
  564. size_t count = 0, ret_val = 0;
  565. int rem_len;
  566. int start_offset, end_offset;
  567. int fold_offset, ppos_data, cur_rd_offset, cur_wr_offset;
  568. struct ath_pktlog_buf *log_buf;
  569. spin_lock_bh(&pl_info->log_lock);
  570. log_buf = pl_info->buf;
  571. *read_complete = false;
  572. if (log_buf == NULL) {
  573. *read_complete = true;
  574. spin_unlock_bh(&pl_info->log_lock);
  575. return 0;
  576. }
  577. if (*ppos == 0 && pl_info->log_state) {
  578. pl_info->saved_state = pl_info->log_state;
  579. pl_info->log_state = 0;
  580. }
  581. bufhdr_size = sizeof(log_buf->bufhdr);
  582. /* copy valid log entries from circular buffer into user space */
  583. rem_len = nbytes;
  584. count = 0;
  585. if (*ppos < bufhdr_size) {
  586. count = MIN((bufhdr_size - *ppos), rem_len);
  587. qdf_mem_copy(buf, ((char *)&log_buf->bufhdr) + *ppos,
  588. count);
  589. rem_len -= count;
  590. ret_val += count;
  591. }
  592. start_offset = log_buf->rd_offset;
  593. cur_wr_offset = log_buf->wr_offset;
  594. if ((rem_len == 0) || (start_offset < 0))
  595. goto rd_done;
  596. fold_offset = -1;
  597. cur_rd_offset = start_offset;
  598. /* Find the last offset and fold-offset if the buffer is folded */
  599. do {
  600. struct ath_pktlog_hdr *log_hdr;
  601. int log_data_offset;
  602. log_hdr = (struct ath_pktlog_hdr *) (log_buf->log_data +
  603. cur_rd_offset);
  604. log_data_offset = cur_rd_offset + sizeof(struct ath_pktlog_hdr);
  605. if ((fold_offset == -1)
  606. && ((pl_info->buf_size - log_data_offset)
  607. <= log_hdr->size))
  608. fold_offset = log_data_offset - 1;
  609. PKTLOG_MOV_RD_IDX(cur_rd_offset, log_buf, pl_info->buf_size);
  610. if ((fold_offset == -1) && (cur_rd_offset == 0)
  611. && (cur_rd_offset != cur_wr_offset))
  612. fold_offset = log_data_offset + log_hdr->size - 1;
  613. end_offset = log_data_offset + log_hdr->size - 1;
  614. } while (cur_rd_offset != cur_wr_offset);
  615. ppos_data = *ppos + ret_val - bufhdr_size + start_offset;
  616. if (fold_offset == -1) {
  617. if (ppos_data > end_offset)
  618. goto rd_done;
  619. count = MIN(rem_len, (end_offset - ppos_data + 1));
  620. qdf_mem_copy(buf + ret_val,
  621. log_buf->log_data + ppos_data,
  622. count);
  623. ret_val += count;
  624. rem_len -= count;
  625. } else {
  626. if (ppos_data <= fold_offset) {
  627. count = MIN(rem_len, (fold_offset - ppos_data + 1));
  628. qdf_mem_copy(buf + ret_val,
  629. log_buf->log_data + ppos_data,
  630. count);
  631. ret_val += count;
  632. rem_len -= count;
  633. }
  634. if (rem_len == 0)
  635. goto rd_done;
  636. ppos_data =
  637. *ppos + ret_val - (bufhdr_size +
  638. (fold_offset - start_offset + 1));
  639. if (ppos_data <= end_offset) {
  640. count = MIN(rem_len, (end_offset - ppos_data + 1));
  641. qdf_mem_copy(buf + ret_val,
  642. log_buf->log_data + ppos_data,
  643. count);
  644. ret_val += count;
  645. rem_len -= count;
  646. }
  647. }
  648. rd_done:
  649. if ((ret_val < nbytes) && pl_info->saved_state) {
  650. pl_info->log_state = pl_info->saved_state;
  651. pl_info->saved_state = 0;
  652. }
  653. *ppos += ret_val;
  654. if (ret_val == 0) {
  655. /* Write pointer might have been updated during the read.
  656. * So, if some data is written into, lets not reset the pointers
  657. * We can continue to read from the offset position
  658. */
  659. if (cur_wr_offset != log_buf->wr_offset) {
  660. *read_complete = false;
  661. } else {
  662. pl_info->buf->rd_offset = -1;
  663. pl_info->buf->wr_offset = 0;
  664. pl_info->buf->bytes_written = 0;
  665. pl_info->buf->offset = PKTLOG_READ_OFFSET;
  666. *read_complete = true;
  667. }
  668. }
  669. spin_unlock_bh(&pl_info->log_lock);
  670. return ret_val;
  671. }
  672. static ssize_t
  673. pktlog_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
  674. {
  675. size_t bufhdr_size;
  676. size_t count = 0, ret_val = 0;
  677. int rem_len;
  678. int start_offset, end_offset;
  679. int fold_offset, ppos_data, cur_rd_offset;
  680. struct ath_pktlog_info *pl_info;
  681. struct ath_pktlog_buf *log_buf;
  682. pl_info = (struct ath_pktlog_info *)
  683. PDE_DATA(file->f_path.dentry->d_inode);
  684. if (!pl_info)
  685. return 0;
  686. spin_lock_bh(&pl_info->log_lock);
  687. log_buf = pl_info->buf;
  688. if (log_buf == NULL) {
  689. spin_unlock_bh(&pl_info->log_lock);
  690. return 0;
  691. }
  692. if (pl_info->log_state) {
  693. /* Read is not allowed when write is going on
  694. * When issuing cat command, ensure to send
  695. * pktlog disable command first.
  696. */
  697. spin_unlock_bh(&pl_info->log_lock);
  698. return -EINVAL;
  699. }
  700. if (*ppos == 0 && pl_info->log_state) {
  701. pl_info->saved_state = pl_info->log_state;
  702. pl_info->log_state = 0;
  703. }
  704. bufhdr_size = sizeof(log_buf->bufhdr);
  705. /* copy valid log entries from circular buffer into user space */
  706. rem_len = nbytes;
  707. count = 0;
  708. if (*ppos < bufhdr_size) {
  709. count = QDF_MIN((bufhdr_size - *ppos), rem_len);
  710. spin_unlock_bh(&pl_info->log_lock);
  711. if (copy_to_user(buf, ((char *)&log_buf->bufhdr) + *ppos,
  712. count))
  713. return -EFAULT;
  714. rem_len -= count;
  715. ret_val += count;
  716. spin_lock_bh(&pl_info->log_lock);
  717. }
  718. start_offset = log_buf->rd_offset;
  719. if ((rem_len == 0) || (start_offset < 0))
  720. goto rd_done;
  721. fold_offset = -1;
  722. cur_rd_offset = start_offset;
  723. /* Find the last offset and fold-offset if the buffer is folded */
  724. do {
  725. struct ath_pktlog_hdr *log_hdr;
  726. int log_data_offset;
  727. log_hdr = (struct ath_pktlog_hdr *)(log_buf->log_data +
  728. cur_rd_offset);
  729. log_data_offset = cur_rd_offset + sizeof(struct ath_pktlog_hdr);
  730. if ((fold_offset == -1)
  731. && ((pl_info->buf_size - log_data_offset)
  732. <= log_hdr->size))
  733. fold_offset = log_data_offset - 1;
  734. PKTLOG_MOV_RD_IDX(cur_rd_offset, log_buf, pl_info->buf_size);
  735. if ((fold_offset == -1) && (cur_rd_offset == 0)
  736. && (cur_rd_offset != log_buf->wr_offset))
  737. fold_offset = log_data_offset + log_hdr->size - 1;
  738. end_offset = log_data_offset + log_hdr->size - 1;
  739. } while (cur_rd_offset != log_buf->wr_offset);
  740. ppos_data = *ppos + ret_val - bufhdr_size + start_offset;
  741. if (fold_offset == -1) {
  742. if (ppos_data > end_offset)
  743. goto rd_done;
  744. count = QDF_MIN(rem_len, (end_offset - ppos_data + 1));
  745. spin_unlock_bh(&pl_info->log_lock);
  746. if (copy_to_user(buf + ret_val,
  747. log_buf->log_data + ppos_data, count))
  748. return -EFAULT;
  749. ret_val += count;
  750. rem_len -= count;
  751. spin_lock_bh(&pl_info->log_lock);
  752. } else {
  753. if (ppos_data <= fold_offset) {
  754. count = QDF_MIN(rem_len, (fold_offset - ppos_data + 1));
  755. spin_unlock_bh(&pl_info->log_lock);
  756. if (copy_to_user(buf + ret_val,
  757. log_buf->log_data + ppos_data, count))
  758. return -EFAULT;
  759. ret_val += count;
  760. rem_len -= count;
  761. spin_lock_bh(&pl_info->log_lock);
  762. }
  763. if (rem_len == 0)
  764. goto rd_done;
  765. ppos_data =
  766. *ppos + ret_val - (bufhdr_size +
  767. (fold_offset - start_offset + 1));
  768. if (ppos_data <= end_offset) {
  769. count = QDF_MIN(rem_len, (end_offset - ppos_data + 1));
  770. spin_unlock_bh(&pl_info->log_lock);
  771. if (copy_to_user(buf + ret_val,
  772. log_buf->log_data + ppos_data, count))
  773. return -EFAULT;
  774. ret_val += count;
  775. rem_len -= count;
  776. spin_lock_bh(&pl_info->log_lock);
  777. }
  778. }
  779. rd_done:
  780. if ((ret_val < nbytes) && pl_info->saved_state) {
  781. pl_info->log_state = pl_info->saved_state;
  782. pl_info->saved_state = 0;
  783. }
  784. *ppos += ret_val;
  785. spin_unlock_bh(&pl_info->log_lock);
  786. return ret_val;
  787. }
  788. #ifndef VMALLOC_VMADDR
  789. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  790. #endif
  791. /* vma operations for mapping vmalloced area to user space */
  792. static void pktlog_vopen(struct vm_area_struct *vma)
  793. {
  794. PKTLOG_MOD_INC_USE_COUNT;
  795. }
  796. static void pktlog_vclose(struct vm_area_struct *vma)
  797. {
  798. PKTLOG_MOD_DEC_USE_COUNT;
  799. }
  800. static int pktlog_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  801. {
  802. unsigned long address = (unsigned long)vmf->virtual_address;
  803. if (address == 0UL)
  804. return VM_FAULT_NOPAGE;
  805. if (vmf->pgoff > vma->vm_end)
  806. return VM_FAULT_SIGBUS;
  807. get_page(virt_to_page((void *)address));
  808. vmf->page = virt_to_page((void *)address);
  809. return 0;
  810. }
  811. static struct vm_operations_struct pktlog_vmops = {
  812. open: pktlog_vopen,
  813. close:pktlog_vclose,
  814. fault:pktlog_fault,
  815. };
  816. static int pktlog_mmap(struct file *file, struct vm_area_struct *vma)
  817. {
  818. struct ath_pktlog_info *pl_info;
  819. pl_info = (struct ath_pktlog_info *)
  820. PDE_DATA(file->f_path.dentry->d_inode);
  821. if (vma->vm_pgoff != 0) {
  822. /* Entire buffer should be mapped */
  823. return -EINVAL;
  824. }
  825. if (!pl_info->buf) {
  826. printk(PKTLOG_TAG "%s: Log buffer unavailable\n", __func__);
  827. return -ENOMEM;
  828. }
  829. vma->vm_flags |= VM_LOCKED;
  830. vma->vm_ops = &pktlog_vmops;
  831. pktlog_vopen(vma);
  832. return 0;
  833. }
  834. int pktlogmod_init(void *context)
  835. {
  836. int ret;
  837. /* create the proc directory entry */
  838. g_pktlog_pde = proc_mkdir(PKTLOG_PROC_DIR, NULL);
  839. if (g_pktlog_pde == NULL) {
  840. printk(PKTLOG_TAG "%s: proc_mkdir failed\n", __func__);
  841. return -EPERM;
  842. }
  843. /* Attach packet log */
  844. ret = pktlog_attach((struct hif_opaque_softc *)context);
  845. if (ret)
  846. goto attach_fail;
  847. return ret;
  848. attach_fail:
  849. remove_proc_entry(PKTLOG_PROC_DIR, NULL);
  850. g_pktlog_pde = NULL;
  851. return ret;
  852. }
  853. void pktlogmod_exit(struct ol_txrx_pdev_t *handle)
  854. {
  855. struct ol_pktlog_dev_t *pl_dev;
  856. pl_dev = ol_get_pl_handle(handle);
  857. if (!pl_dev || g_pktlog_pde == NULL)
  858. return;
  859. pktlog_detach(handle);
  860. /*
  861. * pdev kill needs to be implemented
  862. */
  863. remove_proc_entry(PKTLOG_PROC_DIR, NULL);
  864. }
  865. #endif