blob: 46f426c46404651d367d4e549dfd3ff1ab3ba5e0 [file] [log] [blame]
Prakash Dhavali7090c5f2015-11-02 17:55:19 -08001/*
Sourav Mohapatraeade62f2018-01-12 11:31:45 +05302 * Copyright (c) 2016-2018 The Linux Foundation. All rights reserved.
Prakash Dhavali7090c5f2015-11-02 17:55:19 -08003 *
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/*
23 * This file was originally distributed by Qualcomm Atheros, Inc.
24 * under proprietary terms before Copyright ownership was assigned
25 * to the Linux Foundation.
26 */
27
28/**
29 * DOC : wlan_hdd_memdump.c
30 *
31 * WLAN Host Device Driver file for dumping firmware memory
32 *
33 */
34
35#include <sme_api.h>
36#include <wlan_hdd_includes.h>
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080037#include <linux/module.h>
38#include <linux/kernel.h>
39#include <linux/version.h>
40#include <linux/proc_fs.h> /* Necessary because we use the proc fs */
41#include <linux/uaccess.h> /* for copy_to_user */
42
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080043
Hanumanth Reddy Pothula07e5f992017-11-08 19:35:19 +053044#ifdef MULTI_IF_NAME
45#define PROCFS_DRIVER_DUMP_DIR "debugdriver" MULTI_IF_NAME
46#else
47#define PROCFS_DRIVER_DUMP_DIR "debugdriver"
48#endif
49#define PROCFS_DRIVER_DUMP_NAME "driverdump"
50#define PROCFS_DRIVER_DUMP_PERM 0444
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080051
Hanumanth Reddy Pothula07e5f992017-11-08 19:35:19 +053052static struct proc_dir_entry *proc_file_driver, *proc_dir_driver;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080053
54/** memdump_get_file_data() - get data available in proc file
55 *
56 * @file - handle for the proc file.
57 *
58 * This function is used to retrieve the data passed while
59 * creating proc file entry.
60 *
61 * Return: void pointer to hdd_context
62 */
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080063static void *memdump_get_file_data(struct file *file)
64{
65 void *hdd_ctx;
66
67 hdd_ctx = PDE_DATA(file_inode(file));
68 return hdd_ctx;
69}
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080070
71/**
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +053072 * hdd_driver_mem_cleanup() - Frees memory allocated for
73 * driver dump
74 *
75 * This function unallocates driver dump memory.
76 *
77 * Return: None
78 */
79static void hdd_driver_mem_cleanup(void)
80{
Jeff Johnson66549172017-08-28 11:53:14 -070081 struct hdd_context *hdd_ctx;
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +053082
83 hdd_ctx = cds_get_context(QDF_MODULE_ID_HDD);
84 if (!hdd_ctx) {
85 hdd_err("Invalid HDD context");
86 return;
87 }
88
89 if (hdd_ctx->driver_dump_mem) {
90 qdf_mem_free(hdd_ctx->driver_dump_mem);
91 hdd_ctx->driver_dump_mem = NULL;
92 }
93}
94
95
96/**
97 * hdd_driver_memdump_read() - perform read operation in driver
98 * memory dump proc file
99 * @file - handle for the proc file.
100 * @buf - pointer to user space buffer.
101 * @count - number of bytes to be read.
102 * @pos - offset in the from buffer.
103 *
104 * This function performs read operation for the driver memory dump proc file.
105 *
106 * Return: number of bytes read on success, error code otherwise.
107 */
108static ssize_t hdd_driver_memdump_read(struct file *file, char __user *buf,
109 size_t count, loff_t *pos)
110{
111 int status;
112 QDF_STATUS qdf_status;
Jeff Johnson66549172017-08-28 11:53:14 -0700113 struct hdd_context *hdd_ctx;
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530114 size_t no_of_bytes_read = 0;
115
116 hdd_ctx = memdump_get_file_data(file);
117
Srinivas Girigowda87150ac2017-03-06 17:48:02 -0800118 hdd_debug("Read req for size:%zu pos:%llu", count, *pos);
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530119 status = wlan_hdd_validate_context(hdd_ctx);
120 if (status != 0)
121 return -EINVAL;
122
SaidiReddy Yenugac3a74ba2017-03-08 16:17:55 +0530123 mutex_lock(&hdd_ctx->memdump_lock);
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530124 if (*pos < 0) {
125 hdd_err("Invalid start offset for memdump read");
SaidiReddy Yenugac3a74ba2017-03-08 16:17:55 +0530126 mutex_unlock(&hdd_ctx->memdump_lock);
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530127 return -EINVAL;
128 } else if (!count || (hdd_ctx->driver_dump_size &&
129 (*pos >= hdd_ctx->driver_dump_size))) {
SaidiReddy Yenugac3a74ba2017-03-08 16:17:55 +0530130 mutex_unlock(&hdd_ctx->memdump_lock);
Srinivas Girigowda87150ac2017-03-06 17:48:02 -0800131 hdd_debug("No more data to copy");
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530132 return 0;
133 } else if ((*pos == 0) || (hdd_ctx->driver_dump_mem == NULL)) {
134 /*
135 * Allocate memory for Driver memory dump.
136 */
137 if (!hdd_ctx->driver_dump_mem) {
138 hdd_ctx->driver_dump_mem =
139 qdf_mem_malloc(DRIVER_MEM_DUMP_SIZE);
140 if (!hdd_ctx->driver_dump_mem) {
141 hdd_err("qdf_mem_malloc failed");
SaidiReddy Yenugac3a74ba2017-03-08 16:17:55 +0530142 mutex_unlock(&hdd_ctx->memdump_lock);
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530143 return -ENOMEM;
144 }
145 }
146
147 qdf_status = qdf_state_info_dump_all(hdd_ctx->driver_dump_mem,
148 DRIVER_MEM_DUMP_SIZE,
149 &hdd_ctx->driver_dump_size);
150 /*
151 * If qdf_status is QDF_STATUS_E_NOMEM, then memory allocated is
152 * insufficient to dump driver information. This print can give
153 * information to allocate more memory if more information from
154 * each layer is added in future.
155 */
156 if (qdf_status != QDF_STATUS_SUCCESS)
157 hdd_err("Error in dump driver information, status %d",
158 qdf_status);
Srinivas Girigowda87150ac2017-03-06 17:48:02 -0800159 hdd_debug("driver_dump_size: %d",
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530160 hdd_ctx->driver_dump_size);
161 }
162
163 if (count > hdd_ctx->driver_dump_size - *pos)
164 no_of_bytes_read = hdd_ctx->driver_dump_size - *pos;
165 else
166 no_of_bytes_read = count;
167
168 if (copy_to_user(buf, hdd_ctx->driver_dump_mem + *pos,
169 no_of_bytes_read)) {
170 hdd_err("copy to user space failed");
SaidiReddy Yenugac3a74ba2017-03-08 16:17:55 +0530171 mutex_unlock(&hdd_ctx->memdump_lock);
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530172 return -EFAULT;
173 }
174
175 /* offset(pos) should be updated here based on the copy done */
176 *pos += no_of_bytes_read;
177
178 /* Entire driver memory dump copy completed */
179 if (*pos >= hdd_ctx->driver_dump_size)
180 hdd_driver_mem_cleanup();
181
SaidiReddy Yenugac3a74ba2017-03-08 16:17:55 +0530182 mutex_unlock(&hdd_ctx->memdump_lock);
183
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530184 return no_of_bytes_read;
185}
186
187
188/**
189 * struct driver_dump_fops - file operations for driver dump feature
190 * @read - read function for driver dump operation.
191 *
192 * This structure initialize the file operation handle for memory
193 * dump feature
194 */
195static const struct file_operations driver_dump_fops = {
196read: hdd_driver_memdump_read
197};
198
199/**
200 * hdd_driver_memdump_procfs_init() - Initialize procfs for driver memory dump
Hanumanth Reddy Pothula07e5f992017-11-08 19:35:19 +0530201 * @hdd_ctx Pointer to hdd context
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530202 *
203 * This function create file under proc file system to be used later for
204 * processing driver memory dump
205 *
206 * Return: 0 on success, error code otherwise.
207 */
Hanumanth Reddy Pothula07e5f992017-11-08 19:35:19 +0530208static int hdd_driver_memdump_procfs_init(struct hdd_context *hdd_ctx)
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530209{
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530210 proc_dir_driver = proc_mkdir(PROCFS_DRIVER_DUMP_DIR, NULL);
211 if (proc_dir_driver == NULL) {
Srinivas Girigowda87150ac2017-03-06 17:48:02 -0800212 pr_debug("Could not initialize /proc/%s\n",
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530213 PROCFS_DRIVER_DUMP_DIR);
214 return -ENOMEM;
215 }
216
217 proc_file_driver = proc_create_data(PROCFS_DRIVER_DUMP_NAME,
218 PROCFS_DRIVER_DUMP_PERM, proc_dir_driver,
219 &driver_dump_fops, hdd_ctx);
220 if (proc_file_driver == NULL) {
221 remove_proc_entry(PROCFS_DRIVER_DUMP_NAME, proc_dir_driver);
Srinivas Girigowda87150ac2017-03-06 17:48:02 -0800222 pr_debug("Could not initialize /proc/%s\n",
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530223 PROCFS_DRIVER_DUMP_NAME);
224 return -ENOMEM;
225 }
226
227 pr_debug("/proc/%s/%s created\n", PROCFS_DRIVER_DUMP_DIR,
228 PROCFS_DRIVER_DUMP_NAME);
229 return 0;
230}
231
232/**
233 * hdd_driver_memdump_procfs_remove() - Remove file/dir under procfs
234 * for driver memory dump
235 *
236 * This function removes file/dir under proc file system that was
237 * processing driver memory dump
238 *
239 * Return: None
240 */
241static void hdd_driver_memdump_procfs_remove(void)
242{
243 remove_proc_entry(PROCFS_DRIVER_DUMP_NAME, proc_dir_driver);
244 pr_debug("/proc/%s/%s removed\n", PROCFS_DRIVER_DUMP_DIR,
245 PROCFS_DRIVER_DUMP_NAME);
246 remove_proc_entry(PROCFS_DRIVER_DUMP_DIR, NULL);
247 pr_debug("/proc/%s removed\n", PROCFS_DRIVER_DUMP_DIR);
248}
249
250/**
251 * hdd_driver_memdump_init() - Intialization function for driver
252 * memory dump feature
253 *
254 * This function creates proc file for driver memdump feature
255 *
256 * Return - 0 on success, error otherwise
257 */
258int hdd_driver_memdump_init(void)
259{
260 int status;
Hanumanth Reddy Pothula07e5f992017-11-08 19:35:19 +0530261 struct hdd_context *hdd_ctx;
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530262
Hanumanth Reddy Pothula07e5f992017-11-08 19:35:19 +0530263 hdd_ctx = cds_get_context(QDF_MODULE_ID_HDD);
264 if (!hdd_ctx) {
265 hdd_err("Invalid HDD context");
266 return -EINVAL;
267 }
268
269 mutex_init(&hdd_ctx->memdump_lock);
270
271 status = hdd_driver_memdump_procfs_init(hdd_ctx);
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530272 if (status) {
273 hdd_err("Failed to create proc file");
274 return status;
275 }
276
277 return 0;
278}
279
280/**
281 * hdd_driver_memdump_deinit() - De initialize driver memdump feature
282 *
283 * This function removes proc file created for driver memdump feature.
284 *
285 * Return: None
286 */
287void hdd_driver_memdump_deinit(void)
288{
Padma, Santhosh Kumar9aba02f2016-08-11 16:30:25 +0530289 hdd_driver_memdump_procfs_remove();
290
291 hdd_driver_mem_cleanup();
292}