Prakash Dhavali | 7090c5f | 2015-11-02 17:55:19 -0800 | [diff] [blame] | 1 | /* |
Jeff Johnson | 6875531 | 2017-02-10 11:46:55 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2017 The Linux Foundation. All rights reserved. |
Prakash Dhavali | 7090c5f | 2015-11-02 17:55:19 -0800 | [diff] [blame] | 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 | /* |
| 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 Dhavali | 7090c5f | 2015-11-02 17:55:19 -0800 | [diff] [blame] | 37 | #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 Dhavali | 7090c5f | 2015-11-02 17:55:19 -0800 | [diff] [blame] | 43 | |
Hanumanth Reddy Pothula | 07e5f99 | 2017-11-08 19:35:19 +0530 | [diff] [blame^] | 44 | #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 Dhavali | 7090c5f | 2015-11-02 17:55:19 -0800 | [diff] [blame] | 51 | |
Hanumanth Reddy Pothula | 07e5f99 | 2017-11-08 19:35:19 +0530 | [diff] [blame^] | 52 | static struct proc_dir_entry *proc_file_driver, *proc_dir_driver; |
Prakash Dhavali | 7090c5f | 2015-11-02 17:55:19 -0800 | [diff] [blame] | 53 | |
| 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 Dhavali | 7090c5f | 2015-11-02 17:55:19 -0800 | [diff] [blame] | 63 | static 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 Dhavali | 7090c5f | 2015-11-02 17:55:19 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 72 | * hdd_driver_mem_cleanup() - Frees memory allocated for |
| 73 | * driver dump |
| 74 | * |
| 75 | * This function unallocates driver dump memory. |
| 76 | * |
| 77 | * Return: None |
| 78 | */ |
| 79 | static void hdd_driver_mem_cleanup(void) |
| 80 | { |
Jeff Johnson | 6654917 | 2017-08-28 11:53:14 -0700 | [diff] [blame] | 81 | struct hdd_context *hdd_ctx; |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 82 | |
| 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 | */ |
| 108 | static 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 Johnson | 6654917 | 2017-08-28 11:53:14 -0700 | [diff] [blame] | 113 | struct hdd_context *hdd_ctx; |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 114 | size_t no_of_bytes_read = 0; |
| 115 | |
| 116 | hdd_ctx = memdump_get_file_data(file); |
| 117 | |
Srinivas Girigowda | 87150ac | 2017-03-06 17:48:02 -0800 | [diff] [blame] | 118 | hdd_debug("Read req for size:%zu pos:%llu", count, *pos); |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 119 | status = wlan_hdd_validate_context(hdd_ctx); |
| 120 | if (status != 0) |
| 121 | return -EINVAL; |
| 122 | |
SaidiReddy Yenuga | c3a74ba | 2017-03-08 16:17:55 +0530 | [diff] [blame] | 123 | mutex_lock(&hdd_ctx->memdump_lock); |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 124 | if (*pos < 0) { |
| 125 | hdd_err("Invalid start offset for memdump read"); |
SaidiReddy Yenuga | c3a74ba | 2017-03-08 16:17:55 +0530 | [diff] [blame] | 126 | mutex_unlock(&hdd_ctx->memdump_lock); |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 127 | return -EINVAL; |
| 128 | } else if (!count || (hdd_ctx->driver_dump_size && |
| 129 | (*pos >= hdd_ctx->driver_dump_size))) { |
SaidiReddy Yenuga | c3a74ba | 2017-03-08 16:17:55 +0530 | [diff] [blame] | 130 | mutex_unlock(&hdd_ctx->memdump_lock); |
Srinivas Girigowda | 87150ac | 2017-03-06 17:48:02 -0800 | [diff] [blame] | 131 | hdd_debug("No more data to copy"); |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 132 | 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 Yenuga | c3a74ba | 2017-03-08 16:17:55 +0530 | [diff] [blame] | 142 | mutex_unlock(&hdd_ctx->memdump_lock); |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 143 | 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 Girigowda | 87150ac | 2017-03-06 17:48:02 -0800 | [diff] [blame] | 159 | hdd_debug("driver_dump_size: %d", |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 160 | 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 Yenuga | c3a74ba | 2017-03-08 16:17:55 +0530 | [diff] [blame] | 171 | mutex_unlock(&hdd_ctx->memdump_lock); |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 172 | 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 Yenuga | c3a74ba | 2017-03-08 16:17:55 +0530 | [diff] [blame] | 182 | mutex_unlock(&hdd_ctx->memdump_lock); |
| 183 | |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 184 | 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 | */ |
| 195 | static const struct file_operations driver_dump_fops = { |
| 196 | read: hdd_driver_memdump_read |
| 197 | }; |
| 198 | |
| 199 | /** |
| 200 | * hdd_driver_memdump_procfs_init() - Initialize procfs for driver memory dump |
Hanumanth Reddy Pothula | 07e5f99 | 2017-11-08 19:35:19 +0530 | [diff] [blame^] | 201 | * @hdd_ctx Pointer to hdd context |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 202 | * |
| 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 Pothula | 07e5f99 | 2017-11-08 19:35:19 +0530 | [diff] [blame^] | 208 | static int hdd_driver_memdump_procfs_init(struct hdd_context *hdd_ctx) |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 209 | { |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 210 | proc_dir_driver = proc_mkdir(PROCFS_DRIVER_DUMP_DIR, NULL); |
| 211 | if (proc_dir_driver == NULL) { |
Srinivas Girigowda | 87150ac | 2017-03-06 17:48:02 -0800 | [diff] [blame] | 212 | pr_debug("Could not initialize /proc/%s\n", |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 213 | 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 Girigowda | 87150ac | 2017-03-06 17:48:02 -0800 | [diff] [blame] | 222 | pr_debug("Could not initialize /proc/%s\n", |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 223 | 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 | */ |
| 241 | static 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 | */ |
| 258 | int hdd_driver_memdump_init(void) |
| 259 | { |
| 260 | int status; |
Hanumanth Reddy Pothula | 07e5f99 | 2017-11-08 19:35:19 +0530 | [diff] [blame^] | 261 | struct hdd_context *hdd_ctx; |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 262 | |
| 263 | if (hdd_get_conparam() == QDF_GLOBAL_FTM_MODE) { |
| 264 | hdd_err("Not initializing memdump in FTM mode"); |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
Hanumanth Reddy Pothula | 07e5f99 | 2017-11-08 19:35:19 +0530 | [diff] [blame^] | 268 | hdd_ctx = cds_get_context(QDF_MODULE_ID_HDD); |
| 269 | if (!hdd_ctx) { |
| 270 | hdd_err("Invalid HDD context"); |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | |
| 274 | mutex_init(&hdd_ctx->memdump_lock); |
| 275 | |
| 276 | status = hdd_driver_memdump_procfs_init(hdd_ctx); |
Padma, Santhosh Kumar | 9aba02f | 2016-08-11 16:30:25 +0530 | [diff] [blame] | 277 | if (status) { |
| 278 | hdd_err("Failed to create proc file"); |
| 279 | return status; |
| 280 | } |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * hdd_driver_memdump_deinit() - De initialize driver memdump feature |
| 287 | * |
| 288 | * This function removes proc file created for driver memdump feature. |
| 289 | * |
| 290 | * Return: None |
| 291 | */ |
| 292 | void hdd_driver_memdump_deinit(void) |
| 293 | { |
| 294 | if (hdd_get_conparam() == QDF_GLOBAL_FTM_MODE) { |
| 295 | hdd_err("Not deinitializing memdump in FTM mode"); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | hdd_driver_memdump_procfs_remove(); |
| 300 | |
| 301 | hdd_driver_mem_cleanup(); |
| 302 | } |