linux_ac.c 27 KB

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