linux_ac.c 27 KB

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