linux_ac.c 26 KB

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