linux_ac.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. pktlog_cleanup(pl_info);
  440. if (pl_info->buf) {
  441. pktlog_release_buf(txrx_pdev);
  442. pl_dev->tgt_pktlog_alloced = false;
  443. }
  444. if (pl_dev) {
  445. kfree(pl_info);
  446. pl_dev->pl_info = NULL;
  447. }
  448. }
  449. static int pktlog_open(struct inode *i, struct file *f)
  450. {
  451. PKTLOG_MOD_INC_USE_COUNT;
  452. return 0;
  453. }
  454. static int pktlog_release(struct inode *i, struct file *f)
  455. {
  456. PKTLOG_MOD_DEC_USE_COUNT;
  457. return 0;
  458. }
  459. #ifndef MIN
  460. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  461. #endif
  462. /**
  463. * pktlog_read_proc_entry() - This function is used to read data from the
  464. * proc entry into the readers buffer
  465. * @buf: Readers buffer
  466. * @nbytes: Number of bytes to read
  467. * @ppos: Offset within the drivers buffer
  468. * @pl_info: Packet log information pointer
  469. * @read_complete: Boolean value indication whether read is complete
  470. *
  471. * This function is used to read data from the proc entry into the readers
  472. * buffer. Its functionality is similar to 'pktlog_read' which does
  473. * copy to user to the user space buffer
  474. *
  475. * Return: Number of bytes read from the buffer
  476. *
  477. */
  478. ssize_t
  479. pktlog_read_proc_entry(char *buf, size_t nbytes, loff_t *ppos,
  480. struct ath_pktlog_info *pl_info, bool *read_complete)
  481. {
  482. size_t bufhdr_size;
  483. size_t count = 0, ret_val = 0;
  484. int rem_len;
  485. int start_offset, end_offset;
  486. int fold_offset, ppos_data, cur_rd_offset, cur_wr_offset;
  487. struct ath_pktlog_buf *log_buf = pl_info->buf;
  488. *read_complete = false;
  489. if (log_buf == NULL) {
  490. *read_complete = true;
  491. return 0;
  492. }
  493. if (*ppos == 0 && pl_info->log_state) {
  494. pl_info->saved_state = pl_info->log_state;
  495. pl_info->log_state = 0;
  496. }
  497. bufhdr_size = sizeof(log_buf->bufhdr);
  498. /* copy valid log entries from circular buffer into user space */
  499. rem_len = nbytes;
  500. count = 0;
  501. if (*ppos < bufhdr_size) {
  502. count = MIN((bufhdr_size - *ppos), rem_len);
  503. qdf_mem_copy(buf, ((char *)&log_buf->bufhdr) + *ppos,
  504. count);
  505. rem_len -= count;
  506. ret_val += count;
  507. }
  508. start_offset = log_buf->rd_offset;
  509. cur_wr_offset = log_buf->wr_offset;
  510. if ((rem_len == 0) || (start_offset < 0))
  511. goto rd_done;
  512. fold_offset = -1;
  513. cur_rd_offset = start_offset;
  514. /* Find the last offset and fold-offset if the buffer is folded */
  515. do {
  516. struct ath_pktlog_hdr *log_hdr;
  517. int log_data_offset;
  518. log_hdr = (struct ath_pktlog_hdr *) (log_buf->log_data +
  519. cur_rd_offset);
  520. log_data_offset = cur_rd_offset + sizeof(struct ath_pktlog_hdr);
  521. if ((fold_offset == -1)
  522. && ((pl_info->buf_size - log_data_offset)
  523. <= log_hdr->size))
  524. fold_offset = log_data_offset - 1;
  525. PKTLOG_MOV_RD_IDX(cur_rd_offset, log_buf, pl_info->buf_size);
  526. if ((fold_offset == -1) && (cur_rd_offset == 0)
  527. && (cur_rd_offset != cur_wr_offset))
  528. fold_offset = log_data_offset + log_hdr->size - 1;
  529. end_offset = log_data_offset + log_hdr->size - 1;
  530. } while (cur_rd_offset != cur_wr_offset);
  531. ppos_data = *ppos + ret_val - bufhdr_size + start_offset;
  532. if (fold_offset == -1) {
  533. if (ppos_data > end_offset)
  534. goto rd_done;
  535. count = MIN(rem_len, (end_offset - ppos_data + 1));
  536. qdf_mem_copy(buf + ret_val,
  537. log_buf->log_data + ppos_data,
  538. count);
  539. ret_val += count;
  540. rem_len -= count;
  541. } else {
  542. if (ppos_data <= fold_offset) {
  543. count = MIN(rem_len, (fold_offset - ppos_data + 1));
  544. qdf_mem_copy(buf + ret_val,
  545. log_buf->log_data + ppos_data,
  546. count);
  547. ret_val += count;
  548. rem_len -= count;
  549. }
  550. if (rem_len == 0)
  551. goto rd_done;
  552. ppos_data =
  553. *ppos + ret_val - (bufhdr_size +
  554. (fold_offset - start_offset + 1));
  555. if (ppos_data <= end_offset) {
  556. count = MIN(rem_len, (end_offset - ppos_data + 1));
  557. qdf_mem_copy(buf + ret_val,
  558. log_buf->log_data + ppos_data,
  559. count);
  560. ret_val += count;
  561. rem_len -= count;
  562. }
  563. }
  564. rd_done:
  565. if ((ret_val < nbytes) && pl_info->saved_state) {
  566. pl_info->log_state = pl_info->saved_state;
  567. pl_info->saved_state = 0;
  568. }
  569. *ppos += ret_val;
  570. if (ret_val == 0) {
  571. PKTLOG_LOCK(pl_info);
  572. /* Write pointer might have been updated during the read.
  573. * So, if some data is written into, lets not reset the pointers
  574. * We can continue to read from the offset position
  575. */
  576. if (cur_wr_offset != log_buf->wr_offset) {
  577. *read_complete = false;
  578. } else {
  579. pl_info->buf->rd_offset = -1;
  580. pl_info->buf->wr_offset = 0;
  581. pl_info->buf->bytes_written = 0;
  582. pl_info->buf->offset = PKTLOG_READ_OFFSET;
  583. *read_complete = true;
  584. }
  585. PKTLOG_UNLOCK(pl_info);
  586. }
  587. return ret_val;
  588. }
  589. static ssize_t
  590. pktlog_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
  591. {
  592. size_t bufhdr_size;
  593. size_t count = 0, ret_val = 0;
  594. int rem_len;
  595. int start_offset, end_offset;
  596. int fold_offset, ppos_data, cur_rd_offset;
  597. struct ath_pktlog_info *pl_info;
  598. struct ath_pktlog_buf *log_buf;
  599. pl_info = (struct ath_pktlog_info *)
  600. PDE_DATA(file->f_path.dentry->d_inode);
  601. if (!pl_info)
  602. return 0;
  603. log_buf = pl_info->buf;
  604. if (log_buf == NULL)
  605. return 0;
  606. if (pl_info->log_state) {
  607. /* Read is not allowed when write is going on
  608. * When issuing cat command, ensure to send
  609. * pktlog disable command first.
  610. */
  611. return -EINVAL;
  612. }
  613. if (*ppos == 0 && pl_info->log_state) {
  614. pl_info->saved_state = pl_info->log_state;
  615. pl_info->log_state = 0;
  616. }
  617. bufhdr_size = sizeof(log_buf->bufhdr);
  618. /* copy valid log entries from circular buffer into user space */
  619. rem_len = nbytes;
  620. count = 0;
  621. if (*ppos < bufhdr_size) {
  622. count = QDF_MIN((bufhdr_size - *ppos), rem_len);
  623. if (copy_to_user(buf, ((char *)&log_buf->bufhdr) + *ppos,
  624. count))
  625. return -EFAULT;
  626. rem_len -= count;
  627. ret_val += count;
  628. }
  629. start_offset = log_buf->rd_offset;
  630. if ((rem_len == 0) || (start_offset < 0))
  631. goto rd_done;
  632. fold_offset = -1;
  633. cur_rd_offset = start_offset;
  634. /* Find the last offset and fold-offset if the buffer is folded */
  635. do {
  636. struct ath_pktlog_hdr *log_hdr;
  637. int log_data_offset;
  638. log_hdr = (struct ath_pktlog_hdr *)(log_buf->log_data +
  639. cur_rd_offset);
  640. log_data_offset = cur_rd_offset + sizeof(struct ath_pktlog_hdr);
  641. if ((fold_offset == -1)
  642. && ((pl_info->buf_size - log_data_offset)
  643. <= log_hdr->size))
  644. fold_offset = log_data_offset - 1;
  645. PKTLOG_MOV_RD_IDX(cur_rd_offset, log_buf, pl_info->buf_size);
  646. if ((fold_offset == -1) && (cur_rd_offset == 0)
  647. && (cur_rd_offset != log_buf->wr_offset))
  648. fold_offset = log_data_offset + log_hdr->size - 1;
  649. end_offset = log_data_offset + log_hdr->size - 1;
  650. } while (cur_rd_offset != log_buf->wr_offset);
  651. ppos_data = *ppos + ret_val - bufhdr_size + start_offset;
  652. if (fold_offset == -1) {
  653. if (ppos_data > end_offset)
  654. goto rd_done;
  655. count = QDF_MIN(rem_len, (end_offset - ppos_data + 1));
  656. if (copy_to_user(buf + ret_val,
  657. log_buf->log_data + ppos_data, count))
  658. return -EFAULT;
  659. ret_val += count;
  660. rem_len -= count;
  661. } else {
  662. if (ppos_data <= fold_offset) {
  663. count = QDF_MIN(rem_len, (fold_offset - ppos_data + 1));
  664. if (copy_to_user(buf + ret_val,
  665. log_buf->log_data + ppos_data, count))
  666. return -EFAULT;
  667. ret_val += count;
  668. rem_len -= count;
  669. }
  670. if (rem_len == 0)
  671. goto rd_done;
  672. ppos_data =
  673. *ppos + ret_val - (bufhdr_size +
  674. (fold_offset - start_offset + 1));
  675. if (ppos_data <= end_offset) {
  676. count = QDF_MIN(rem_len, (end_offset - ppos_data + 1));
  677. if (copy_to_user(buf + ret_val,
  678. log_buf->log_data + ppos_data, count))
  679. return -EFAULT;
  680. ret_val += count;
  681. rem_len -= count;
  682. }
  683. }
  684. rd_done:
  685. if ((ret_val < nbytes) && pl_info->saved_state) {
  686. pl_info->log_state = pl_info->saved_state;
  687. pl_info->saved_state = 0;
  688. }
  689. *ppos += ret_val;
  690. return ret_val;
  691. }
  692. #ifndef VMALLOC_VMADDR
  693. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  694. #endif
  695. /* vma operations for mapping vmalloced area to user space */
  696. static void pktlog_vopen(struct vm_area_struct *vma)
  697. {
  698. PKTLOG_MOD_INC_USE_COUNT;
  699. }
  700. static void pktlog_vclose(struct vm_area_struct *vma)
  701. {
  702. PKTLOG_MOD_DEC_USE_COUNT;
  703. }
  704. static int pktlog_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  705. {
  706. unsigned long address = (unsigned long)vmf->virtual_address;
  707. if (address == 0UL)
  708. return VM_FAULT_NOPAGE;
  709. if (vmf->pgoff > vma->vm_end)
  710. return VM_FAULT_SIGBUS;
  711. get_page(virt_to_page((void *)address));
  712. vmf->page = virt_to_page((void *)address);
  713. return 0;
  714. }
  715. static struct vm_operations_struct pktlog_vmops = {
  716. open: pktlog_vopen,
  717. close:pktlog_vclose,
  718. fault:pktlog_fault,
  719. };
  720. static int pktlog_mmap(struct file *file, struct vm_area_struct *vma)
  721. {
  722. struct ath_pktlog_info *pl_info;
  723. pl_info = (struct ath_pktlog_info *)
  724. PDE_DATA(file->f_path.dentry->d_inode);
  725. if (vma->vm_pgoff != 0) {
  726. /* Entire buffer should be mapped */
  727. return -EINVAL;
  728. }
  729. if (!pl_info->buf) {
  730. printk(PKTLOG_TAG "%s: Log buffer unavailable\n", __func__);
  731. return -ENOMEM;
  732. }
  733. vma->vm_flags |= VM_LOCKED;
  734. vma->vm_ops = &pktlog_vmops;
  735. pktlog_vopen(vma);
  736. return 0;
  737. }
  738. int pktlogmod_init(void *context)
  739. {
  740. int ret;
  741. /* create the proc directory entry */
  742. g_pktlog_pde = proc_mkdir(PKTLOG_PROC_DIR, NULL);
  743. if (g_pktlog_pde == NULL) {
  744. printk(PKTLOG_TAG "%s: proc_mkdir failed\n", __func__);
  745. return -EPERM;
  746. }
  747. /* Attach packet log */
  748. ret = pktlog_attach((struct hif_opaque_softc *)context);
  749. if (ret)
  750. goto attach_fail;
  751. return ret;
  752. attach_fail:
  753. remove_proc_entry(PKTLOG_PROC_DIR, NULL);
  754. g_pktlog_pde = NULL;
  755. return ret;
  756. }
  757. void pktlogmod_exit(struct ol_txrx_pdev_t *handle)
  758. {
  759. struct ol_pktlog_dev_t *pl_dev;
  760. pl_dev = ol_get_pl_handle(handle);
  761. if (!pl_dev || g_pktlog_pde == NULL)
  762. return;
  763. pktlog_detach(handle);
  764. /*
  765. * pdev kill needs to be implemented
  766. */
  767. remove_proc_entry(PKTLOG_PROC_DIR, NULL);
  768. }
  769. #endif