linux_ac.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. printk(PKTLOG_TAG "%s:proc_dointvec failed\n",
  205. __func__);
  206. } else {
  207. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  208. lenp, ppos);
  209. if (ret)
  210. printk(PKTLOG_TAG "%s:proc_dointvec failed\n",
  211. __func__);
  212. }
  213. ctl->data = NULL;
  214. ctl->maxlen = 0;
  215. return ret;
  216. }
  217. static int get_pktlog_bufsize(struct ol_pktlog_dev_t *pl_dev)
  218. {
  219. return pl_dev->pl_info->buf_size;
  220. }
  221. /* sysctl procfs handler to set/get pktlog size */
  222. static int
  223. qdf_sysctl_decl(ath_sysctl_pktlog_size, ctl, write, filp, buffer, lenp, ppos)
  224. {
  225. int ret, size;
  226. ol_ath_generic_softc_handle scn;
  227. struct ol_pktlog_dev_t *pl_dev;
  228. scn = (ol_ath_generic_softc_handle) ctl->extra1;
  229. if (!scn) {
  230. printk("%s: Invalid scn context\n", __func__);
  231. ASSERT(0);
  232. return -EINVAL;
  233. }
  234. pl_dev = cds_get_pl_handle();
  235. if (!pl_dev) {
  236. printk("%s: Invalid pktlog handle\n", __func__);
  237. ASSERT(0);
  238. return -ENODEV;
  239. }
  240. ctl->data = &size;
  241. ctl->maxlen = sizeof(size);
  242. if (write) {
  243. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  244. lenp, ppos);
  245. if (ret == 0)
  246. ret = pl_dev->pl_funcs->pktlog_setsize(
  247. (struct hif_opaque_softc *)scn, size);
  248. } else {
  249. size = get_pktlog_bufsize(pl_dev);
  250. ret = QDF_SYSCTL_PROC_DOINTVEC(ctl, write, filp, buffer,
  251. lenp, ppos);
  252. }
  253. ctl->data = NULL;
  254. ctl->maxlen = 0;
  255. return ret;
  256. }
  257. /* Register sysctl table */
  258. static int pktlog_sysctl_register(struct hif_opaque_softc *scn)
  259. {
  260. struct ol_pktlog_dev_t *pl_dev = cds_get_pl_handle();
  261. struct ath_pktlog_info_lnx *pl_info_lnx;
  262. char *proc_name;
  263. if (pl_dev) {
  264. pl_info_lnx = PL_INFO_LNX(pl_dev->pl_info);
  265. proc_name = pl_dev->name;
  266. } else {
  267. pl_info_lnx = PL_INFO_LNX(g_pktlog_info);
  268. proc_name = PKTLOG_PROC_SYSTEM;
  269. }
  270. /*
  271. * Setup the sysctl table for creating the following sysctl entries:
  272. * /proc/sys/PKTLOG_PROC_DIR/<adapter>/enable for enabling/disabling
  273. * pktlog
  274. * /proc/sys/PKTLOG_PROC_DIR/<adapter>/size for changing the buffer size
  275. */
  276. memset(pl_info_lnx->sysctls, 0, sizeof(pl_info_lnx->sysctls));
  277. pl_info_lnx->sysctls[0].procname = PKTLOG_PROC_DIR;
  278. pl_info_lnx->sysctls[0].mode = PKTLOG_PROCSYS_DIR_PERM;
  279. pl_info_lnx->sysctls[0].child = &pl_info_lnx->sysctls[2];
  280. /* [1] is NULL terminator */
  281. pl_info_lnx->sysctls[2].procname = proc_name;
  282. pl_info_lnx->sysctls[2].mode = PKTLOG_PROCSYS_DIR_PERM;
  283. pl_info_lnx->sysctls[2].child = &pl_info_lnx->sysctls[4];
  284. /* [3] is NULL terminator */
  285. pl_info_lnx->sysctls[4].procname = "enable";
  286. pl_info_lnx->sysctls[4].mode = PKTLOG_PROCSYS_PERM;
  287. pl_info_lnx->sysctls[4].proc_handler = ath_sysctl_pktlog_enable;
  288. pl_info_lnx->sysctls[4].extra1 = scn;
  289. pl_info_lnx->sysctls[5].procname = "size";
  290. pl_info_lnx->sysctls[5].mode = PKTLOG_PROCSYS_PERM;
  291. pl_info_lnx->sysctls[5].proc_handler = ath_sysctl_pktlog_size;
  292. pl_info_lnx->sysctls[5].extra1 = scn;
  293. pl_info_lnx->sysctls[6].procname = "options";
  294. pl_info_lnx->sysctls[6].mode = PKTLOG_PROCSYS_PERM;
  295. pl_info_lnx->sysctls[6].proc_handler = proc_dointvec;
  296. pl_info_lnx->sysctls[6].data = &pl_info_lnx->info.options;
  297. pl_info_lnx->sysctls[6].maxlen = sizeof(pl_info_lnx->info.options);
  298. pl_info_lnx->sysctls[7].procname = "sack_thr";
  299. pl_info_lnx->sysctls[7].mode = PKTLOG_PROCSYS_PERM;
  300. pl_info_lnx->sysctls[7].proc_handler = proc_dointvec;
  301. pl_info_lnx->sysctls[7].data = &pl_info_lnx->info.sack_thr;
  302. pl_info_lnx->sysctls[7].maxlen = sizeof(pl_info_lnx->info.sack_thr);
  303. pl_info_lnx->sysctls[8].procname = "tail_length";
  304. pl_info_lnx->sysctls[8].mode = PKTLOG_PROCSYS_PERM;
  305. pl_info_lnx->sysctls[8].proc_handler = proc_dointvec;
  306. pl_info_lnx->sysctls[8].data = &pl_info_lnx->info.tail_length;
  307. pl_info_lnx->sysctls[8].maxlen = sizeof(pl_info_lnx->info.tail_length);
  308. pl_info_lnx->sysctls[9].procname = "thruput_thresh";
  309. pl_info_lnx->sysctls[9].mode = PKTLOG_PROCSYS_PERM;
  310. pl_info_lnx->sysctls[9].proc_handler = proc_dointvec;
  311. pl_info_lnx->sysctls[9].data = &pl_info_lnx->info.thruput_thresh;
  312. pl_info_lnx->sysctls[9].maxlen =
  313. sizeof(pl_info_lnx->info.thruput_thresh);
  314. pl_info_lnx->sysctls[10].procname = "phyerr_thresh";
  315. pl_info_lnx->sysctls[10].mode = PKTLOG_PROCSYS_PERM;
  316. pl_info_lnx->sysctls[10].proc_handler = proc_dointvec;
  317. pl_info_lnx->sysctls[10].data = &pl_info_lnx->info.phyerr_thresh;
  318. pl_info_lnx->sysctls[10].maxlen =
  319. sizeof(pl_info_lnx->info.phyerr_thresh);
  320. pl_info_lnx->sysctls[11].procname = "per_thresh";
  321. pl_info_lnx->sysctls[11].mode = PKTLOG_PROCSYS_PERM;
  322. pl_info_lnx->sysctls[11].proc_handler = proc_dointvec;
  323. pl_info_lnx->sysctls[11].data = &pl_info_lnx->info.per_thresh;
  324. pl_info_lnx->sysctls[11].maxlen = sizeof(pl_info_lnx->info.per_thresh);
  325. pl_info_lnx->sysctls[12].procname = "trigger_interval";
  326. pl_info_lnx->sysctls[12].mode = PKTLOG_PROCSYS_PERM;
  327. pl_info_lnx->sysctls[12].proc_handler = proc_dointvec;
  328. pl_info_lnx->sysctls[12].data = &pl_info_lnx->info.trigger_interval;
  329. pl_info_lnx->sysctls[12].maxlen =
  330. sizeof(pl_info_lnx->info.trigger_interval);
  331. /* [13] is NULL terminator */
  332. /* and register everything */
  333. /* register_sysctl_table changed from 2.6.21 onwards */
  334. pl_info_lnx->sysctl_header =
  335. register_sysctl_table(pl_info_lnx->sysctls);
  336. if (!pl_info_lnx->sysctl_header) {
  337. printk("%s: failed to register sysctls!\n", proc_name);
  338. return -EINVAL;
  339. }
  340. return 0;
  341. }
  342. /*
  343. * Initialize logging for system or adapter
  344. * Parameter scn should be NULL for system wide logging
  345. */
  346. static int pktlog_attach(struct hif_opaque_softc *scn)
  347. {
  348. struct ol_pktlog_dev_t *pl_dev;
  349. struct ath_pktlog_info_lnx *pl_info_lnx;
  350. char *proc_name;
  351. struct proc_dir_entry *proc_entry;
  352. pl_dev = cds_get_pl_handle();
  353. if (pl_dev != NULL) {
  354. pl_info_lnx = kmalloc(sizeof(*pl_info_lnx), GFP_KERNEL);
  355. if (pl_info_lnx == NULL) {
  356. printk(PKTLOG_TAG "%s:allocation failed for pl_info\n",
  357. __func__);
  358. return -ENOMEM;
  359. }
  360. pl_dev->pl_info = &pl_info_lnx->info;
  361. pl_dev->name = WLANDEV_BASENAME;
  362. proc_name = pl_dev->name;
  363. if (!pl_dev->pl_funcs)
  364. pl_dev->pl_funcs = &ol_pl_funcs;
  365. /*
  366. * Valid for both direct attach and offload architecture
  367. */
  368. pl_dev->pl_funcs->pktlog_init(scn);
  369. } else {
  370. return -EINVAL;
  371. }
  372. /*
  373. * initialize log info
  374. * might be good to move to pktlog_init
  375. */
  376. /* pl_dev->tgt_pktlog_alloced = false; */
  377. pl_dev->vendor_cmd_send = false;
  378. pl_info_lnx->proc_entry = NULL;
  379. pl_info_lnx->sysctl_header = NULL;
  380. proc_entry = proc_create_data(proc_name, PKTLOG_PROC_PERM,
  381. g_pktlog_pde, &pktlog_fops,
  382. &pl_info_lnx->info);
  383. if (proc_entry == NULL) {
  384. printk(PKTLOG_TAG "%s: create_proc_entry failed for %s\n",
  385. __func__, proc_name);
  386. goto attach_fail1;
  387. }
  388. pl_info_lnx->proc_entry = proc_entry;
  389. if (pktlog_sysctl_register(scn)) {
  390. printk(PKTLOG_TAG "%s: sysctl register failed for %s\n",
  391. __func__, proc_name);
  392. goto attach_fail2;
  393. }
  394. return 0;
  395. attach_fail2:
  396. remove_proc_entry(proc_name, g_pktlog_pde);
  397. attach_fail1:
  398. if (pl_dev)
  399. kfree(pl_dev->pl_info);
  400. return -EINVAL;
  401. }
  402. static void pktlog_sysctl_unregister(struct ol_pktlog_dev_t *pl_dev)
  403. {
  404. struct ath_pktlog_info_lnx *pl_info_lnx;
  405. if (!pl_dev) {
  406. printk("%s: Invalid pktlog context\n", __func__);
  407. ASSERT(0);
  408. return;
  409. }
  410. pl_info_lnx = (pl_dev) ? PL_INFO_LNX(pl_dev->pl_info) :
  411. PL_INFO_LNX(g_pktlog_info);
  412. if (pl_info_lnx->sysctl_header) {
  413. unregister_sysctl_table(pl_info_lnx->sysctl_header);
  414. pl_info_lnx->sysctl_header = NULL;
  415. }
  416. }
  417. static void pktlog_detach(struct ol_txrx_pdev_t *handle)
  418. {
  419. struct ol_txrx_pdev_t *txrx_pdev;
  420. struct ol_pktlog_dev_t *pl_dev;
  421. struct ath_pktlog_info *pl_info;
  422. txrx_pdev = handle;
  423. if (!txrx_pdev) {
  424. printk("%s: Invalid txrx_pdev context\n", __func__);
  425. ASSERT(0);
  426. return;
  427. }
  428. pl_dev = txrx_pdev->pl_dev;
  429. if (!pl_dev) {
  430. printk("%s: Invalid pktlog context\n", __func__);
  431. ASSERT(0);
  432. return;
  433. }
  434. pl_info = pl_dev->pl_info;
  435. remove_proc_entry(WLANDEV_BASENAME, g_pktlog_pde);
  436. pktlog_sysctl_unregister(pl_dev);
  437. pktlog_cleanup(pl_info);
  438. if (pl_info->buf) {
  439. pktlog_release_buf(txrx_pdev);
  440. pl_dev->tgt_pktlog_alloced = false;
  441. }
  442. if (pl_dev) {
  443. kfree(pl_info);
  444. pl_dev->pl_info = NULL;
  445. }
  446. }
  447. static int pktlog_open(struct inode *i, struct file *f)
  448. {
  449. PKTLOG_MOD_INC_USE_COUNT;
  450. return 0;
  451. }
  452. static int pktlog_release(struct inode *i, struct file *f)
  453. {
  454. PKTLOG_MOD_DEC_USE_COUNT;
  455. return 0;
  456. }
  457. #ifndef MIN
  458. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  459. #endif
  460. /**
  461. * pktlog_read_proc_entry() - This function is used to read data from the
  462. * proc entry into the readers buffer
  463. * @buf: Readers buffer
  464. * @nbytes: Number of bytes to read
  465. * @ppos: Offset within the drivers buffer
  466. * @pl_info: Packet log information pointer
  467. * @read_complete: Boolean value indication whether read is complete
  468. *
  469. * This function is used to read data from the proc entry into the readers
  470. * buffer. Its functionality is similar to 'pktlog_read' which does
  471. * copy to user to the user space buffer
  472. *
  473. * Return: Number of bytes read from the buffer
  474. *
  475. */
  476. ssize_t
  477. pktlog_read_proc_entry(char *buf, size_t nbytes, loff_t *ppos,
  478. struct ath_pktlog_info *pl_info, bool *read_complete)
  479. {
  480. size_t bufhdr_size;
  481. size_t count = 0, ret_val = 0;
  482. int rem_len;
  483. int start_offset, end_offset;
  484. int fold_offset, ppos_data, cur_rd_offset, cur_wr_offset;
  485. struct ath_pktlog_buf *log_buf = pl_info->buf;
  486. *read_complete = false;
  487. if (log_buf == NULL) {
  488. *read_complete = true;
  489. return 0;
  490. }
  491. if (*ppos == 0 && pl_info->log_state) {
  492. pl_info->saved_state = pl_info->log_state;
  493. pl_info->log_state = 0;
  494. }
  495. bufhdr_size = sizeof(log_buf->bufhdr);
  496. /* copy valid log entries from circular buffer into user space */
  497. rem_len = nbytes;
  498. count = 0;
  499. if (*ppos < bufhdr_size) {
  500. count = MIN((bufhdr_size - *ppos), rem_len);
  501. qdf_mem_copy(buf, ((char *)&log_buf->bufhdr) + *ppos,
  502. count);
  503. rem_len -= count;
  504. ret_val += count;
  505. }
  506. start_offset = log_buf->rd_offset;
  507. cur_wr_offset = log_buf->wr_offset;
  508. if ((rem_len == 0) || (start_offset < 0))
  509. goto rd_done;
  510. fold_offset = -1;
  511. cur_rd_offset = start_offset;
  512. /* Find the last offset and fold-offset if the buffer is folded */
  513. do {
  514. struct ath_pktlog_hdr *log_hdr;
  515. int log_data_offset;
  516. log_hdr = (struct ath_pktlog_hdr *) (log_buf->log_data +
  517. cur_rd_offset);
  518. log_data_offset = cur_rd_offset + sizeof(struct ath_pktlog_hdr);
  519. if ((fold_offset == -1)
  520. && ((pl_info->buf_size - log_data_offset)
  521. <= log_hdr->size))
  522. fold_offset = log_data_offset - 1;
  523. PKTLOG_MOV_RD_IDX(cur_rd_offset, log_buf, pl_info->buf_size);
  524. if ((fold_offset == -1) && (cur_rd_offset == 0)
  525. && (cur_rd_offset != cur_wr_offset))
  526. fold_offset = log_data_offset + log_hdr->size - 1;
  527. end_offset = log_data_offset + log_hdr->size - 1;
  528. } while (cur_rd_offset != cur_wr_offset);
  529. ppos_data = *ppos + ret_val - bufhdr_size + start_offset;
  530. if (fold_offset == -1) {
  531. if (ppos_data > end_offset)
  532. goto rd_done;
  533. count = MIN(rem_len, (end_offset - ppos_data + 1));
  534. qdf_mem_copy(buf + ret_val,
  535. log_buf->log_data + ppos_data,
  536. count);
  537. ret_val += count;
  538. rem_len -= count;
  539. } else {
  540. if (ppos_data <= fold_offset) {
  541. count = MIN(rem_len, (fold_offset - ppos_data + 1));
  542. qdf_mem_copy(buf + ret_val,
  543. log_buf->log_data + ppos_data,
  544. count);
  545. ret_val += count;
  546. rem_len -= count;
  547. }
  548. if (rem_len == 0)
  549. goto rd_done;
  550. ppos_data =
  551. *ppos + ret_val - (bufhdr_size +
  552. (fold_offset - start_offset + 1));
  553. if (ppos_data <= end_offset) {
  554. count = MIN(rem_len, (end_offset - ppos_data + 1));
  555. qdf_mem_copy(buf + ret_val,
  556. log_buf->log_data + ppos_data,
  557. count);
  558. ret_val += count;
  559. rem_len -= count;
  560. }
  561. }
  562. rd_done:
  563. if ((ret_val < nbytes) && pl_info->saved_state) {
  564. pl_info->log_state = pl_info->saved_state;
  565. pl_info->saved_state = 0;
  566. }
  567. *ppos += ret_val;
  568. if (ret_val == 0) {
  569. PKTLOG_LOCK(pl_info);
  570. /* Write pointer might have been updated during the read.
  571. * So, if some data is written into, lets not reset the pointers
  572. * We can continue to read from the offset position
  573. */
  574. if (cur_wr_offset != log_buf->wr_offset) {
  575. *read_complete = false;
  576. } else {
  577. pl_info->buf->rd_offset = -1;
  578. pl_info->buf->wr_offset = 0;
  579. pl_info->buf->bytes_written = 0;
  580. pl_info->buf->offset = PKTLOG_READ_OFFSET;
  581. *read_complete = true;
  582. }
  583. PKTLOG_UNLOCK(pl_info);
  584. }
  585. return ret_val;
  586. }
  587. static ssize_t
  588. pktlog_read(struct file *file, char *buf, size_t nbytes, loff_t *ppos)
  589. {
  590. size_t bufhdr_size;
  591. size_t count = 0, ret_val = 0;
  592. int rem_len;
  593. int start_offset, end_offset;
  594. int fold_offset, ppos_data, cur_rd_offset;
  595. struct ath_pktlog_info *pl_info;
  596. struct ath_pktlog_buf *log_buf;
  597. pl_info = (struct ath_pktlog_info *)
  598. PDE_DATA(file->f_path.dentry->d_inode);
  599. if (!pl_info)
  600. return 0;
  601. log_buf = pl_info->buf;
  602. if (log_buf == NULL)
  603. return 0;
  604. if (pl_info->log_state) {
  605. /* Read is not allowed when write is going on
  606. * When issuing cat command, ensure to send
  607. * pktlog disable command first.
  608. */
  609. return -EINVAL;
  610. }
  611. if (*ppos == 0 && pl_info->log_state) {
  612. pl_info->saved_state = pl_info->log_state;
  613. pl_info->log_state = 0;
  614. }
  615. bufhdr_size = sizeof(log_buf->bufhdr);
  616. /* copy valid log entries from circular buffer into user space */
  617. rem_len = nbytes;
  618. count = 0;
  619. if (*ppos < bufhdr_size) {
  620. count = QDF_MIN((bufhdr_size - *ppos), rem_len);
  621. if (copy_to_user(buf, ((char *)&log_buf->bufhdr) + *ppos,
  622. count))
  623. return -EFAULT;
  624. rem_len -= count;
  625. ret_val += count;
  626. }
  627. start_offset = log_buf->rd_offset;
  628. if ((rem_len == 0) || (start_offset < 0))
  629. goto rd_done;
  630. fold_offset = -1;
  631. cur_rd_offset = start_offset;
  632. /* Find the last offset and fold-offset if the buffer is folded */
  633. do {
  634. struct ath_pktlog_hdr *log_hdr;
  635. int log_data_offset;
  636. log_hdr = (struct ath_pktlog_hdr *)(log_buf->log_data +
  637. cur_rd_offset);
  638. log_data_offset = cur_rd_offset + sizeof(struct ath_pktlog_hdr);
  639. if ((fold_offset == -1)
  640. && ((pl_info->buf_size - log_data_offset)
  641. <= log_hdr->size))
  642. fold_offset = log_data_offset - 1;
  643. PKTLOG_MOV_RD_IDX(cur_rd_offset, log_buf, pl_info->buf_size);
  644. if ((fold_offset == -1) && (cur_rd_offset == 0)
  645. && (cur_rd_offset != log_buf->wr_offset))
  646. fold_offset = log_data_offset + log_hdr->size - 1;
  647. end_offset = log_data_offset + log_hdr->size - 1;
  648. } while (cur_rd_offset != log_buf->wr_offset);
  649. ppos_data = *ppos + ret_val - bufhdr_size + start_offset;
  650. if (fold_offset == -1) {
  651. if (ppos_data > end_offset)
  652. goto rd_done;
  653. count = QDF_MIN(rem_len, (end_offset - ppos_data + 1));
  654. if (copy_to_user(buf + ret_val,
  655. log_buf->log_data + ppos_data, count))
  656. return -EFAULT;
  657. ret_val += count;
  658. rem_len -= count;
  659. } else {
  660. if (ppos_data <= fold_offset) {
  661. count = QDF_MIN(rem_len, (fold_offset - ppos_data + 1));
  662. if (copy_to_user(buf + ret_val,
  663. log_buf->log_data + ppos_data, count))
  664. return -EFAULT;
  665. ret_val += count;
  666. rem_len -= count;
  667. }
  668. if (rem_len == 0)
  669. goto rd_done;
  670. ppos_data =
  671. *ppos + ret_val - (bufhdr_size +
  672. (fold_offset - start_offset + 1));
  673. if (ppos_data <= end_offset) {
  674. count = QDF_MIN(rem_len, (end_offset - ppos_data + 1));
  675. if (copy_to_user(buf + ret_val,
  676. log_buf->log_data + ppos_data, count))
  677. return -EFAULT;
  678. ret_val += count;
  679. rem_len -= count;
  680. }
  681. }
  682. rd_done:
  683. if ((ret_val < nbytes) && pl_info->saved_state) {
  684. pl_info->log_state = pl_info->saved_state;
  685. pl_info->saved_state = 0;
  686. }
  687. *ppos += ret_val;
  688. return ret_val;
  689. }
  690. #ifndef VMALLOC_VMADDR
  691. #define VMALLOC_VMADDR(x) ((unsigned long)(x))
  692. #endif
  693. /* vma operations for mapping vmalloced area to user space */
  694. static void pktlog_vopen(struct vm_area_struct *vma)
  695. {
  696. PKTLOG_MOD_INC_USE_COUNT;
  697. }
  698. static void pktlog_vclose(struct vm_area_struct *vma)
  699. {
  700. PKTLOG_MOD_DEC_USE_COUNT;
  701. }
  702. static int pktlog_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  703. {
  704. unsigned long address = (unsigned long)vmf->virtual_address;
  705. if (address == 0UL)
  706. return VM_FAULT_NOPAGE;
  707. if (vmf->pgoff > vma->vm_end)
  708. return VM_FAULT_SIGBUS;
  709. get_page(virt_to_page((void *)address));
  710. vmf->page = virt_to_page((void *)address);
  711. return 0;
  712. }
  713. static struct vm_operations_struct pktlog_vmops = {
  714. open: pktlog_vopen,
  715. close:pktlog_vclose,
  716. fault:pktlog_fault,
  717. };
  718. static int pktlog_mmap(struct file *file, struct vm_area_struct *vma)
  719. {
  720. struct ath_pktlog_info *pl_info;
  721. pl_info = (struct ath_pktlog_info *)
  722. PDE_DATA(file->f_path.dentry->d_inode);
  723. if (vma->vm_pgoff != 0) {
  724. /* Entire buffer should be mapped */
  725. return -EINVAL;
  726. }
  727. if (!pl_info->buf) {
  728. printk(PKTLOG_TAG "%s: Log buffer unavailable\n", __func__);
  729. return -ENOMEM;
  730. }
  731. vma->vm_flags |= VM_LOCKED;
  732. vma->vm_ops = &pktlog_vmops;
  733. pktlog_vopen(vma);
  734. return 0;
  735. }
  736. int pktlogmod_init(void *context)
  737. {
  738. int ret;
  739. /* create the proc directory entry */
  740. g_pktlog_pde = proc_mkdir(PKTLOG_PROC_DIR, NULL);
  741. if (g_pktlog_pde == NULL) {
  742. printk(PKTLOG_TAG "%s: proc_mkdir failed\n", __func__);
  743. return -EPERM;
  744. }
  745. /* Attach packet log */
  746. ret = pktlog_attach((struct hif_opaque_softc *)context);
  747. if (ret)
  748. goto attach_fail;
  749. return ret;
  750. attach_fail:
  751. remove_proc_entry(PKTLOG_PROC_DIR, NULL);
  752. g_pktlog_pde = NULL;
  753. return ret;
  754. }
  755. void pktlogmod_exit(struct ol_txrx_pdev_t *handle)
  756. {
  757. struct ol_pktlog_dev_t *pl_dev;
  758. pl_dev = ol_get_pl_handle(handle);
  759. if (!pl_dev || g_pktlog_pde == NULL)
  760. return;
  761. pktlog_detach(handle);
  762. /*
  763. * pdev kill needs to be implemented
  764. */
  765. remove_proc_entry(PKTLOG_PROC_DIR, NULL);
  766. }
  767. #endif