Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Gaurao Chaudhari | 89833c9 | 2020-01-10 14:58:26 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 2020, The Linux Foundation. All rights reserved. |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 4 | * Permission to use, copy, modify, and/or distribute this software for |
| 5 | * any purpose with or without fee is hereby granted, provided that the |
| 6 | * above copyright notice and this permission notice appear in all copies. |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 13 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | ************************************************************************** |
| 15 | */ |
| 16 | /* |
| 17 | * nss_log.c |
| 18 | * NSS FW debug logger retrieval from DDR (memory) |
| 19 | * |
| 20 | */ |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/miscdevice.h> |
| 29 | #include <linux/posix-timers.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/time.h> |
| 32 | #include <linux/platform_device.h> |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 33 | #include <linux/device.h> |
| 34 | #include <nss_hal.h> |
| 35 | #include "nss_core.h" |
| 36 | #include "nss_log.h" |
| 37 | |
| 38 | /* |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 39 | * Private data for each device file open instance |
| 40 | */ |
| 41 | struct nss_log_data { |
| 42 | void *load_mem; /* Pointer to struct nss_log_descriptor - descriptor data */ |
| 43 | dma_addr_t dma_addr; /* Handle to DMA */ |
| 44 | uint32_t last_entry; /* Last known sampled entry (or index) */ |
| 45 | uint32_t nentries; /* Caches the total number of entries of log buffer */ |
| 46 | int nss_id; /* NSS Core id being used */ |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 47 | struct nss_ctx_instance *nss_ctx; |
| 48 | /* NSS ctx instance */ |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 51 | struct nss_log_ring_buffer_addr nss_rbe[NSS_MAX_CORES]; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 52 | |
| 53 | static DEFINE_MUTEX(nss_log_mutex); |
| 54 | static wait_queue_head_t nss_log_wq; |
| 55 | static nss_log_msg_callback_t nss_debug_interface_cb; |
| 56 | static void *nss_debug_interface_app_data = NULL; |
| 57 | |
| 58 | static wait_queue_head_t msg_wq; |
| 59 | enum nss_cmn_response msg_response; |
| 60 | static bool msg_event; |
| 61 | |
| 62 | /* |
| 63 | * nss_log_llseek() |
| 64 | * Seek operation. |
| 65 | */ |
| 66 | static loff_t nss_log_llseek(struct file *file, loff_t offset, int origin) |
| 67 | { |
| 68 | struct nss_log_data *data = file->private_data; |
| 69 | |
| 70 | switch (origin) { |
| 71 | case SEEK_SET: |
| 72 | break; |
| 73 | case SEEK_CUR: |
| 74 | offset += file->f_pos; |
| 75 | break; |
| 76 | case SEEK_END: |
| 77 | offset = ((data->nentries * sizeof(struct nss_log_entry)) + sizeof(struct nss_log_descriptor)) - offset; |
| 78 | break; |
| 79 | default: |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | return (offset >= 0) ? (file->f_pos = offset) : -EINVAL; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * nss_log_open() |
| 88 | * Open operation for our device. We let as many instance run together |
| 89 | */ |
| 90 | static int nss_log_open(struct inode *inode, struct file *filp) |
| 91 | { |
| 92 | struct nss_log_data *data = NULL; |
| 93 | struct nss_top_instance *nss_top; |
| 94 | struct nss_ctx_instance *nss_ctx; |
| 95 | int nss_id; |
| 96 | |
| 97 | /* |
| 98 | * i_private is passed to us by debug_fs_create() |
| 99 | */ |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 100 | nss_id = (int)(nss_ptr_t)inode->i_private; |
Suman Ghosh | 9f7b370 | 2018-09-21 19:51:40 +0530 | [diff] [blame] | 101 | if (nss_id < 0 || nss_id >= nss_top_main.num_nss) { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 102 | nss_warning("nss_id is not valid :%d\n", nss_id); |
| 103 | return -ENODEV; |
| 104 | } |
| 105 | |
| 106 | nss_top = &nss_top_main; |
| 107 | nss_ctx = &nss_top->nss[nss_id]; |
| 108 | |
| 109 | data = kzalloc(sizeof(struct nss_log_data), GFP_KERNEL); |
| 110 | if (!data) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 111 | nss_warning("%px: Failed to allocate memory for log_data", nss_ctx); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 112 | return -ENOMEM; |
| 113 | } |
| 114 | |
| 115 | mutex_lock(&nss_log_mutex); |
| 116 | if (!nss_rbe[nss_id].addr) { |
| 117 | mutex_unlock(&nss_log_mutex); |
| 118 | kfree(data); |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 119 | nss_warning("%px: Ring buffer not configured yet for nss_id:%d", nss_ctx, nss_id); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 120 | return -EIO; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Actual ring buffer. |
| 125 | */ |
| 126 | data->load_mem = nss_rbe[nss_id].addr; |
| 127 | data->last_entry = 0; |
| 128 | data->nentries = nss_rbe[nss_id].nentries; |
| 129 | data->dma_addr = nss_rbe[nss_id].dma_addr; |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 130 | data->nss_ctx = nss_ctx; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * Increment the reference count so that we don't free |
| 134 | * the memory |
| 135 | */ |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 136 | nss_rbe[nss_id].ref_cnt++; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 137 | data->nss_id = nss_id; |
| 138 | filp->private_data = data; |
| 139 | mutex_unlock(&nss_log_mutex); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * nss_log_release() |
| 146 | * release gets called when close() is called on the file |
| 147 | * descriptor. We unmap the IO region. |
| 148 | */ |
| 149 | static int nss_log_release(struct inode *inode, struct file *filp) |
| 150 | { |
| 151 | struct nss_log_data *data = filp->private_data; |
| 152 | |
| 153 | if (!data) { |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | |
| 157 | mutex_lock(&nss_log_mutex); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 158 | nss_rbe[data->nss_id].ref_cnt--; |
| 159 | BUG_ON(nss_rbe[data->nss_id].ref_cnt < 0); |
| 160 | if (!nss_rbe[data->nss_id].ref_cnt) { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 161 | wake_up(&nss_log_wq); |
| 162 | } |
| 163 | mutex_unlock(&nss_log_mutex); |
| 164 | kfree(data); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* |
Saurabh Misra | 6d42da7 | 2015-03-05 14:57:01 -0800 | [diff] [blame] | 169 | * nss_log_current_entry() |
| 170 | * Reads current entry index from NSS log descriptor. |
| 171 | */ |
| 172 | static uint32_t nss_log_current_entry(struct nss_log_descriptor *desc) |
| 173 | { |
| 174 | rmb(); |
| 175 | return desc->current_entry; |
| 176 | } |
| 177 | |
| 178 | /* |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 179 | * nss_log_read() |
| 180 | * Read operation lets command like cat and tail read our memory log buffer data. |
| 181 | */ |
| 182 | static ssize_t nss_log_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) |
| 183 | { |
| 184 | struct nss_log_data *data = filp->private_data; |
| 185 | struct nss_log_descriptor *desc; |
| 186 | size_t bytes = 0; |
| 187 | size_t b; |
| 188 | struct nss_log_entry *rb; |
| 189 | uint32_t entry; |
| 190 | uint32_t offset, index; |
| 191 | char msg[NSS_LOG_OUTPUT_LINE_SIZE]; |
| 192 | |
| 193 | if (!data) { |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | desc = data->load_mem; |
| 198 | if (!desc) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 199 | nss_warning("%px: load_mem is NULL", data); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 200 | return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * If buffer is too small to fit even one entry. |
| 205 | */ |
| 206 | if (size < NSS_LOG_OUTPUT_LINE_SIZE) { |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Get the current index |
| 212 | */ |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 213 | dma_sync_single_for_cpu(data->nss_ctx->dev, data->dma_addr, sizeof(struct nss_log_descriptor), DMA_FROM_DEVICE); |
| 214 | |
Saurabh Misra | 6d42da7 | 2015-03-05 14:57:01 -0800 | [diff] [blame] | 215 | entry = nss_log_current_entry(desc); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 216 | |
| 217 | /* |
| 218 | * If the current and last sampled indexes are same then bail out. |
| 219 | */ |
| 220 | if (unlikely(data->last_entry == entry)) { |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * If this is the first read (after open) on our device file. |
| 226 | */ |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 227 | if (unlikely(!(*ppos))) { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 228 | /* |
| 229 | * If log buffer has rolled over. Almost all the time |
| 230 | * it will be true. |
| 231 | */ |
| 232 | if (likely(entry > data->nentries)) { |
| 233 | /* |
| 234 | * Determine how much we can stuff in one |
| 235 | * buffer passed to us and accordingly |
| 236 | * reduce our index. |
| 237 | */ |
| 238 | data->last_entry = entry - data->nentries; |
| 239 | } else { |
| 240 | data->last_entry = 0; |
| 241 | } |
| 242 | } else if (unlikely(entry > data->nentries && ((entry - data->nentries) > data->last_entry))) { |
| 243 | /* |
| 244 | * If FW is producing debug buffer at a pace faster than |
| 245 | * we can consume, then we restrict our iteration. |
| 246 | */ |
| 247 | data->last_entry = entry - data->nentries; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Iterate over indexes. |
| 252 | */ |
| 253 | while (entry > data->last_entry) { |
| 254 | index = offset = (data->last_entry % data->nentries); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 255 | offset = (offset * sizeof(struct nss_log_entry)) |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 256 | + offsetof(struct nss_log_descriptor, log_ring_buffer); |
| 257 | |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 258 | dma_sync_single_for_cpu(data->nss_ctx->dev, data->dma_addr + offset, |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 259 | sizeof(struct nss_log_entry), DMA_FROM_DEVICE); |
| 260 | rb = &desc->log_ring_buffer[index]; |
| 261 | |
Gaurao Chaudhari | 89833c9 | 2020-01-10 14:58:26 -0800 | [diff] [blame] | 262 | b = scnprintf(msg, sizeof(msg), NSS_LOG_LINE_FORMAT, |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 263 | rb->thread_num, rb->timestamp, rb->message); |
| 264 | |
| 265 | data->last_entry++; |
| 266 | |
| 267 | /* |
| 268 | * Copy to user buffer and if we fail then we return |
| 269 | * failure. |
| 270 | */ |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 271 | if (copy_to_user(buf + bytes, msg, b)) { |
| 272 | return -EFAULT; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 275 | bytes += b; |
| 276 | |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 277 | /* |
| 278 | * If we ran out of space in the buffer. |
| 279 | */ |
| 280 | if ((bytes + NSS_LOG_OUTPUT_LINE_SIZE) >= size) |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | if (bytes > 0) |
| 285 | *ppos = bytes; |
| 286 | |
| 287 | return bytes; |
| 288 | } |
| 289 | |
| 290 | struct file_operations nss_logs_core_ops = { |
| 291 | .owner = THIS_MODULE, |
| 292 | .open = nss_log_open, |
| 293 | .read = nss_log_read, |
| 294 | .release = nss_log_release, |
| 295 | .llseek = nss_log_llseek, |
| 296 | }; |
| 297 | |
| 298 | /* |
| 299 | * nss_debug_interface_set_callback() |
| 300 | * Sets the callback |
| 301 | */ |
| 302 | void nss_debug_interface_set_callback(nss_log_msg_callback_t cb, void *app_data) |
| 303 | { |
| 304 | nss_debug_interface_cb = cb; |
| 305 | nss_debug_interface_app_data = app_data; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * nss_debug_interface_event() |
| 310 | * Received an event from NSS FW |
| 311 | */ |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 312 | static void nss_debug_interface_event(void *app_data, struct nss_log_debug_interface_msg *nim) |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 313 | { |
| 314 | struct nss_cmn_msg *ncm = (struct nss_cmn_msg *)nim; |
| 315 | |
| 316 | msg_response = ncm->response; |
| 317 | msg_event = true; |
| 318 | wake_up(&msg_wq); |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * nss_debug_interface_handler() |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 323 | * handle NSS -> HLOS messages for debug interfaces |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 324 | */ |
| 325 | static void nss_debug_interface_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 326 | { |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 327 | struct nss_log_debug_interface_msg *ntm = (struct nss_log_debug_interface_msg *)ncm; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 328 | nss_log_msg_callback_t cb; |
| 329 | |
| 330 | BUG_ON(ncm->interface != NSS_DEBUG_INTERFACE); |
| 331 | |
| 332 | /* |
| 333 | * Is this a valid request/response packet? |
| 334 | */ |
| 335 | if (ncm->type > NSS_DEBUG_INTERFACE_TYPE_MAX) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 336 | nss_warning("%px: received invalid message %d for CAPWAP interface", nss_ctx, ncm->type); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 337 | return; |
| 338 | } |
| 339 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 340 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_log_debug_interface_msg)) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 341 | nss_warning("%px: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 346 | |
| 347 | /* |
| 348 | * Update the callback and app_data for NOTIFY messages. |
| 349 | */ |
Suruchi Agarwal | e4ad24a | 2018-06-11 12:03:46 +0530 | [diff] [blame] | 350 | if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 351 | ncm->cb = (nss_ptr_t)nss_debug_interface_cb; |
| 352 | ncm->app_data = (nss_ptr_t)nss_debug_interface_app_data; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Do we have a callback |
| 357 | */ |
| 358 | if (!ncm->cb) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 359 | nss_trace("%px: cb is null for interface %d", nss_ctx, ncm->interface); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 360 | return; |
| 361 | } |
| 362 | |
| 363 | cb = (nss_log_msg_callback_t)ncm->cb; |
| 364 | cb((void *)ncm->app_data, ntm); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * nss_debug_interface_tx() |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 369 | * Transmit a debug interface message to NSS FW |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 370 | */ |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 371 | static nss_tx_status_t nss_debug_interface_tx(struct nss_ctx_instance *nss_ctx, struct nss_log_debug_interface_msg *msg) |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 372 | { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 373 | struct nss_cmn_msg *ncm = &msg->cm; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 374 | |
| 375 | /* |
| 376 | * Sanity check the message |
| 377 | */ |
| 378 | if (ncm->interface != NSS_DEBUG_INTERFACE) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 379 | nss_warning("%px: tx request for another interface: %d", nss_ctx, ncm->interface); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 380 | return NSS_TX_FAILURE; |
| 381 | } |
| 382 | |
| 383 | if (ncm->type > NSS_DEBUG_INTERFACE_TYPE_MAX) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 384 | nss_warning("%px: message type out of range: %d", nss_ctx, ncm->type); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 385 | return NSS_TX_FAILURE; |
| 386 | } |
| 387 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 388 | return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* |
| 392 | * nss_debug_log_buffer_alloc() |
| 393 | * Allocates and Initializes log buffer for the use in NSS FW (logging) |
| 394 | */ |
| 395 | bool nss_debug_log_buffer_alloc(uint8_t nss_id, uint32_t nentry) |
| 396 | { |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 397 | struct nss_log_debug_interface_msg msg; |
| 398 | struct nss_log_debug_memory_msg *dbg; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 399 | struct nss_top_instance *nss_top; |
| 400 | struct nss_ctx_instance *nss_ctx; |
| 401 | dma_addr_t dma_addr; |
| 402 | uint32_t size; |
| 403 | void *addr = NULL; |
| 404 | nss_tx_status_t status; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 405 | |
Suman Ghosh | 9f7b370 | 2018-09-21 19:51:40 +0530 | [diff] [blame] | 406 | if (nss_id >= nss_top_main.num_nss) { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | |
| 410 | nss_top = &nss_top_main; |
| 411 | nss_ctx = &nss_top->nss[nss_id]; |
| 412 | |
| 413 | if (nss_ctx->state != NSS_CORE_STATE_INITIALIZED) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 414 | nss_warning("%px: NSS Core:%d is not initialized yet\n", nss_ctx, nss_id); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 415 | return false; |
| 416 | } |
| 417 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 418 | size = sizeof(struct nss_log_descriptor) + (sizeof(struct nss_log_entry) * nentry); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 419 | addr = kmalloc(size, GFP_ATOMIC); |
| 420 | if (!addr) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 421 | nss_warning("%px: Failed to allocate memory for logging (size:%d)\n", nss_ctx, size); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 422 | return false; |
| 423 | } |
| 424 | |
| 425 | memset(addr, 0, size); |
Stephen Wang | efd3851 | 2017-01-24 14:01:02 -0800 | [diff] [blame] | 426 | dma_addr = (uint32_t)dma_map_single(nss_ctx->dev, addr, size, DMA_FROM_DEVICE); |
| 427 | if (unlikely(dma_mapping_error(nss_ctx->dev, dma_addr))) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 428 | nss_warning("%px: Failed to map address in DMA", nss_ctx); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 429 | kfree(addr); |
| 430 | return false; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* |
| 434 | * If we already have ring buffer associated with nss_id, then |
| 435 | * we must wait before we attach a new ring buffer. |
| 436 | */ |
| 437 | mutex_lock(&nss_log_mutex); |
| 438 | if (nss_rbe[nss_id].addr) { |
| 439 | mutex_unlock(&nss_log_mutex); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 440 | |
| 441 | /* |
| 442 | * Someone is using the current logbuffer. Wait until ref count become 0. |
| 443 | * We have to return mutex here, because the current user requires it to |
| 444 | * release the reference. |
| 445 | */ |
| 446 | if (!wait_event_timeout(nss_log_wq, !nss_rbe[nss_id].ref_cnt, 5 * HZ)) { |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 447 | nss_warning("%px: Timeout waiting for refcnt to become 0\n", nss_ctx); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 448 | goto fail; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | mutex_lock(&nss_log_mutex); |
| 452 | if (!nss_rbe[nss_id].addr) { |
| 453 | mutex_unlock(&nss_log_mutex); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 454 | goto fail; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 455 | } |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 456 | if (nss_rbe[nss_id].ref_cnt > 0) { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 457 | mutex_unlock(&nss_log_mutex); |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 458 | nss_warning("%px: Some other thread is contending..opting out\n", nss_ctx); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 459 | goto fail; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 460 | } |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 463 | memset(&msg, 0, sizeof(struct nss_log_debug_interface_msg)); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 464 | nss_cmn_msg_init(&msg.cm, NSS_DEBUG_INTERFACE, NSS_DEBUG_INTERFACE_TYPE_LOG_BUF_INIT, |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 465 | sizeof(struct nss_log_debug_memory_msg), nss_debug_interface_event, NULL); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 466 | |
| 467 | dbg = &msg.msg.addr; |
| 468 | dbg->nentry = nentry; |
| 469 | dbg->version = NSS_DEBUG_LOG_VERSION; |
Saurabh Misra | 6d42da7 | 2015-03-05 14:57:01 -0800 | [diff] [blame] | 470 | dbg->phy_addr = dma_addr; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 471 | |
| 472 | msg_event = false; |
| 473 | status = nss_debug_interface_tx(nss_ctx, &msg); |
| 474 | if (status != NSS_TX_SUCCESS) { |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 475 | mutex_unlock(&nss_log_mutex); |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 476 | nss_warning("%px: Failed to send message to debug interface:%d\n", nss_ctx, status); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 477 | goto fail; |
| 478 | } |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 479 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 480 | /* |
| 481 | * Wait for 5 seconds since this is a critical operation. |
| 482 | * Mutex is not unlocked here because we do not want someone to acquire the mutex and use the logbuffer |
| 483 | * while we are waiting message from NSS. |
| 484 | */ |
| 485 | if (!wait_event_timeout(msg_wq, msg_event, 5 * HZ)) { |
| 486 | mutex_unlock(&nss_log_mutex); |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 487 | nss_warning("%px: Timeout send message to debug interface\n", nss_ctx); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 488 | goto fail; |
| 489 | } |
| 490 | |
| 491 | if (msg_response != NSS_CMN_RESPONSE_ACK) { |
| 492 | mutex_unlock(&nss_log_mutex); |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 493 | nss_warning("%px: Response error for send message to debug interface:%d\n", nss_ctx, msg_response); |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 494 | goto fail; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* |
| 498 | * If we had to free the previous allocation for ring buffer. |
| 499 | */ |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 500 | if (nss_rbe[nss_id].addr) { |
| 501 | uint32_t old_size; |
| 502 | old_size = sizeof(struct nss_log_descriptor) + |
| 503 | (sizeof(struct nss_log_entry) * nss_rbe[nss_id].nentries); |
| 504 | dma_unmap_single(nss_ctx->dev, nss_rbe[nss_id].dma_addr, old_size, DMA_FROM_DEVICE); |
| 505 | kfree(nss_rbe[nss_id].addr); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 508 | nss_rbe[nss_id].addr = addr; |
| 509 | nss_rbe[nss_id].nentries = nentry; |
| 510 | nss_rbe[nss_id].ref_cnt = 0; |
| 511 | nss_rbe[nss_id].dma_addr = dma_addr; |
| 512 | mutex_unlock(&nss_log_mutex); |
| 513 | wake_up(&nss_log_wq); |
| 514 | return true; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 515 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 516 | fail: |
Kyle Swenson | dd7b296 | 2021-03-16 13:46:32 -0600 | [diff] [blame] | 517 | dma_unmap_single(nss_ctx->dev, dma_addr, size, DMA_FROM_DEVICE); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 518 | kfree(addr); |
| 519 | wake_up(&nss_log_wq); |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * nss_logbuffer_handler() |
| 525 | * Enable NSS debug output |
| 526 | */ |
Stephen Wang | 52e6d34 | 2016-03-29 15:02:33 -0700 | [diff] [blame] | 527 | int nss_logbuffer_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos) |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 528 | { |
| 529 | int ret; |
Thomas Wu | cf21508 | 2017-08-02 14:23:37 -0700 | [diff] [blame] | 530 | int core_status; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 531 | int i; |
| 532 | |
| 533 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 534 | if (ret) { |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | if (!write) { |
| 539 | return ret; |
| 540 | } |
| 541 | |
| 542 | if (nss_ctl_logbuf < 32) { |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 543 | nss_warning("Invalid NSS FW logbuffer size:%d (must be > 32)\n", nss_ctl_logbuf); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 544 | nss_ctl_logbuf = 0; |
| 545 | return ret; |
| 546 | } |
| 547 | |
Suman Ghosh | 9f7b370 | 2018-09-21 19:51:40 +0530 | [diff] [blame] | 548 | for (i = 0; i < nss_top_main.num_nss; i++) { |
Thomas Wu | cf21508 | 2017-08-02 14:23:37 -0700 | [diff] [blame] | 549 | /* |
| 550 | * Register the callback handler and allocate the debug log buffers |
| 551 | */ |
| 552 | core_status = nss_core_register_handler(&nss_top_main.nss[i], NSS_DEBUG_INTERFACE, nss_debug_interface_handler, NULL); |
| 553 | if (core_status != NSS_CORE_STATUS_SUCCESS) { |
| 554 | nss_warning("NSS logbuffer init failed with register handler:%d\n", core_status); |
| 555 | } |
| 556 | |
Cemil Coskun | 5f51db5 | 2018-05-07 17:15:37 -0700 | [diff] [blame] | 557 | if (!nss_debug_log_buffer_alloc(i, nss_ctl_logbuf)) { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 558 | nss_warning("%d: Failed to set debug log buffer on NSS core", i); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | /* |
| 566 | * nss_log_init() |
| 567 | * Initializes NSS FW logs retrieval logic from /sys |
| 568 | */ |
| 569 | void nss_log_init(void) |
| 570 | { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 571 | int i; |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 572 | struct dentry *logs_dentry; |
| 573 | struct dentry *core_log_dentry; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 574 | |
| 575 | memset(nss_rbe, 0, sizeof(nss_rbe)); |
| 576 | init_waitqueue_head(&nss_log_wq); |
| 577 | init_waitqueue_head(&msg_wq); |
| 578 | |
| 579 | /* |
| 580 | * Create directory for obtaining NSS FW logs from each core |
| 581 | */ |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 582 | logs_dentry = debugfs_create_dir("logs", nss_top_main.top_dentry); |
| 583 | if (unlikely(!logs_dentry)) { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 584 | nss_warning("Failed to create qca-nss-drv/logs directory in debugfs"); |
| 585 | return; |
| 586 | } |
| 587 | |
Suman Ghosh | 9f7b370 | 2018-09-21 19:51:40 +0530 | [diff] [blame] | 588 | for (i = 0; i < nss_top_main.num_nss; i++) { |
Naresh Kumar Mehta | 9ff70e4 | 2019-01-03 14:45:53 +0530 | [diff] [blame] | 589 | char file[16]; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 590 | extern struct file_operations nss_logs_core_ops; |
| 591 | |
| 592 | snprintf(file, sizeof(file), "core%d", i); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 593 | core_log_dentry = debugfs_create_file(file, 0400, |
| 594 | logs_dentry, (void *)(nss_ptr_t)i, &nss_logs_core_ops); |
| 595 | if (unlikely(!core_log_dentry)) { |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 596 | nss_warning("Failed to create qca-nss-drv/logs/%s file in debugfs", file); |
| 597 | return; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | nss_debug_interface_set_callback(nss_debug_interface_event, NULL); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 602 | } |