Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Saurabh Misra | 6d42da7 | 2015-03-05 14:57:01 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2015, 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 | /* |
| 39 | * Gives us important data from NSS platform data |
| 40 | */ |
| 41 | extern struct nss_top_instance nss_top_main; |
| 42 | |
| 43 | /* |
| 44 | * Private data for each device file open instance |
| 45 | */ |
| 46 | struct nss_log_data { |
| 47 | void *load_mem; /* Pointer to struct nss_log_descriptor - descriptor data */ |
| 48 | dma_addr_t dma_addr; /* Handle to DMA */ |
| 49 | uint32_t last_entry; /* Last known sampled entry (or index) */ |
| 50 | uint32_t nentries; /* Caches the total number of entries of log buffer */ |
| 51 | int nss_id; /* NSS Core id being used */ |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * Saves the ring buffer address for logging per NSS core |
| 56 | */ |
| 57 | struct nss_ring_buffer_addr { |
| 58 | void *addr; /* Pointer to struct nss_log_descriptor */ |
| 59 | dma_addr_t dma_addr; /* DMA Handle */ |
| 60 | uint32_t nentries; /* Number of entries in the ring buffer */ |
| 61 | int refcnt; /* Reference count */ |
| 62 | }; |
| 63 | |
| 64 | static struct nss_ring_buffer_addr nss_rbe[NSS_MAX_CORES]; |
| 65 | |
| 66 | static DEFINE_MUTEX(nss_log_mutex); |
| 67 | static wait_queue_head_t nss_log_wq; |
| 68 | static nss_log_msg_callback_t nss_debug_interface_cb; |
| 69 | static void *nss_debug_interface_app_data = NULL; |
| 70 | |
| 71 | static wait_queue_head_t msg_wq; |
| 72 | enum nss_cmn_response msg_response; |
| 73 | static bool msg_event; |
| 74 | |
| 75 | /* |
| 76 | * nss_log_llseek() |
| 77 | * Seek operation. |
| 78 | */ |
| 79 | static loff_t nss_log_llseek(struct file *file, loff_t offset, int origin) |
| 80 | { |
| 81 | struct nss_log_data *data = file->private_data; |
| 82 | |
| 83 | switch (origin) { |
| 84 | case SEEK_SET: |
| 85 | break; |
| 86 | case SEEK_CUR: |
| 87 | offset += file->f_pos; |
| 88 | break; |
| 89 | case SEEK_END: |
| 90 | offset = ((data->nentries * sizeof(struct nss_log_entry)) + sizeof(struct nss_log_descriptor)) - offset; |
| 91 | break; |
| 92 | default: |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | return (offset >= 0) ? (file->f_pos = offset) : -EINVAL; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * nss_log_open() |
| 101 | * Open operation for our device. We let as many instance run together |
| 102 | */ |
| 103 | static int nss_log_open(struct inode *inode, struct file *filp) |
| 104 | { |
| 105 | struct nss_log_data *data = NULL; |
| 106 | struct nss_top_instance *nss_top; |
| 107 | struct nss_ctx_instance *nss_ctx; |
| 108 | int nss_id; |
| 109 | |
| 110 | /* |
| 111 | * i_private is passed to us by debug_fs_create() |
| 112 | */ |
| 113 | nss_id = (int)inode->i_private; |
| 114 | if (nss_id < 0 || nss_id >= NSS_MAX_CORES) { |
| 115 | nss_warning("nss_id is not valid :%d\n", nss_id); |
| 116 | return -ENODEV; |
| 117 | } |
| 118 | |
| 119 | nss_top = &nss_top_main; |
| 120 | nss_ctx = &nss_top->nss[nss_id]; |
| 121 | |
| 122 | data = kzalloc(sizeof(struct nss_log_data), GFP_KERNEL); |
| 123 | if (!data) { |
| 124 | nss_warning("%p: Failed to allocate memory for log_data", nss_ctx); |
| 125 | return -ENOMEM; |
| 126 | } |
| 127 | |
| 128 | mutex_lock(&nss_log_mutex); |
| 129 | if (!nss_rbe[nss_id].addr) { |
| 130 | mutex_unlock(&nss_log_mutex); |
| 131 | kfree(data); |
| 132 | nss_warning("%p: Ring buffer not configured yet for nss_id:%d", nss_ctx, nss_id); |
| 133 | return -EIO; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Actual ring buffer. |
| 138 | */ |
| 139 | data->load_mem = nss_rbe[nss_id].addr; |
| 140 | data->last_entry = 0; |
| 141 | data->nentries = nss_rbe[nss_id].nentries; |
| 142 | data->dma_addr = nss_rbe[nss_id].dma_addr; |
| 143 | |
| 144 | /* |
| 145 | * Increment the reference count so that we don't free |
| 146 | * the memory |
| 147 | */ |
| 148 | nss_rbe[nss_id].refcnt++; |
| 149 | data->nss_id = nss_id; |
| 150 | filp->private_data = data; |
| 151 | mutex_unlock(&nss_log_mutex); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * nss_log_release() |
| 158 | * release gets called when close() is called on the file |
| 159 | * descriptor. We unmap the IO region. |
| 160 | */ |
| 161 | static int nss_log_release(struct inode *inode, struct file *filp) |
| 162 | { |
| 163 | struct nss_log_data *data = filp->private_data; |
| 164 | |
| 165 | if (!data) { |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | mutex_lock(&nss_log_mutex); |
| 170 | nss_rbe[data->nss_id].refcnt--; |
| 171 | BUG_ON(nss_rbe[data->nss_id].refcnt < 0); |
| 172 | if (nss_rbe[data->nss_id].refcnt == 0) { |
| 173 | wake_up(&nss_log_wq); |
| 174 | } |
| 175 | mutex_unlock(&nss_log_mutex); |
| 176 | kfree(data); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /* |
Saurabh Misra | 6d42da7 | 2015-03-05 14:57:01 -0800 | [diff] [blame] | 181 | * nss_log_current_entry() |
| 182 | * Reads current entry index from NSS log descriptor. |
| 183 | */ |
| 184 | static uint32_t nss_log_current_entry(struct nss_log_descriptor *desc) |
| 185 | { |
| 186 | rmb(); |
| 187 | return desc->current_entry; |
| 188 | } |
| 189 | |
| 190 | /* |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 191 | * nss_log_read() |
| 192 | * Read operation lets command like cat and tail read our memory log buffer data. |
| 193 | */ |
| 194 | static ssize_t nss_log_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) |
| 195 | { |
| 196 | struct nss_log_data *data = filp->private_data; |
| 197 | struct nss_log_descriptor *desc; |
| 198 | size_t bytes = 0; |
| 199 | size_t b; |
| 200 | struct nss_log_entry *rb; |
| 201 | uint32_t entry; |
| 202 | uint32_t offset, index; |
| 203 | char msg[NSS_LOG_OUTPUT_LINE_SIZE]; |
| 204 | |
| 205 | if (!data) { |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | |
| 209 | desc = data->load_mem; |
| 210 | if (!desc) { |
| 211 | nss_warning("%p: load_mem is NULL", data); |
| 212 | return -EINVAL; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * If buffer is too small to fit even one entry. |
| 217 | */ |
| 218 | if (size < NSS_LOG_OUTPUT_LINE_SIZE) { |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | * Get the current index |
| 224 | */ |
| 225 | dma_sync_single_for_cpu(NULL, data->dma_addr, sizeof (struct nss_log_descriptor), DMA_FROM_DEVICE); |
Saurabh Misra | 6d42da7 | 2015-03-05 14:57:01 -0800 | [diff] [blame] | 226 | entry = nss_log_current_entry(desc); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * If the current and last sampled indexes are same then bail out. |
| 230 | */ |
| 231 | if (unlikely(data->last_entry == entry)) { |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /* |
| 236 | * If this is the first read (after open) on our device file. |
| 237 | */ |
| 238 | if (unlikely(*ppos == 0)) { |
| 239 | /* |
| 240 | * If log buffer has rolled over. Almost all the time |
| 241 | * it will be true. |
| 242 | */ |
| 243 | if (likely(entry > data->nentries)) { |
| 244 | /* |
| 245 | * Determine how much we can stuff in one |
| 246 | * buffer passed to us and accordingly |
| 247 | * reduce our index. |
| 248 | */ |
| 249 | data->last_entry = entry - data->nentries; |
| 250 | } else { |
| 251 | data->last_entry = 0; |
| 252 | } |
| 253 | } else if (unlikely(entry > data->nentries && ((entry - data->nentries) > data->last_entry))) { |
| 254 | /* |
| 255 | * If FW is producing debug buffer at a pace faster than |
| 256 | * we can consume, then we restrict our iteration. |
| 257 | */ |
| 258 | data->last_entry = entry - data->nentries; |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Iterate over indexes. |
| 263 | */ |
| 264 | while (entry > data->last_entry) { |
| 265 | index = offset = (data->last_entry % data->nentries); |
| 266 | offset = (offset * sizeof (struct nss_log_entry)) |
| 267 | + offsetof(struct nss_log_descriptor, log_ring_buffer); |
| 268 | |
| 269 | dma_sync_single_for_cpu(NULL, data->dma_addr + offset, |
| 270 | sizeof(struct nss_log_entry), DMA_FROM_DEVICE); |
| 271 | rb = &desc->log_ring_buffer[index]; |
| 272 | |
| 273 | b = snprintf(msg, sizeof(msg), NSS_LOG_LINE_FORMAT, |
| 274 | rb->thread_num, rb->timestamp, rb->message); |
| 275 | |
| 276 | data->last_entry++; |
| 277 | |
| 278 | /* |
| 279 | * Copy to user buffer and if we fail then we return |
| 280 | * failure. |
| 281 | */ |
| 282 | if (copy_to_user(buf + bytes, msg, b) == 0) { |
| 283 | bytes += b; |
| 284 | } else { |
| 285 | bytes = -EFAULT; |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * If we ran out of space in the buffer. |
| 291 | */ |
| 292 | if ((bytes + NSS_LOG_OUTPUT_LINE_SIZE) >= size) |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | if (bytes > 0) |
| 297 | *ppos = bytes; |
| 298 | |
| 299 | return bytes; |
| 300 | } |
| 301 | |
| 302 | struct file_operations nss_logs_core_ops = { |
| 303 | .owner = THIS_MODULE, |
| 304 | .open = nss_log_open, |
| 305 | .read = nss_log_read, |
| 306 | .release = nss_log_release, |
| 307 | .llseek = nss_log_llseek, |
| 308 | }; |
| 309 | |
| 310 | /* |
| 311 | * nss_debug_interface_set_callback() |
| 312 | * Sets the callback |
| 313 | */ |
| 314 | void nss_debug_interface_set_callback(nss_log_msg_callback_t cb, void *app_data) |
| 315 | { |
| 316 | nss_debug_interface_cb = cb; |
| 317 | nss_debug_interface_app_data = app_data; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * nss_debug_interface_event() |
| 322 | * Received an event from NSS FW |
| 323 | */ |
| 324 | static void nss_debug_interface_event(void *app_data, struct nss_debug_interface_msg *nim) |
| 325 | { |
| 326 | struct nss_cmn_msg *ncm = (struct nss_cmn_msg *)nim; |
| 327 | |
| 328 | msg_response = ncm->response; |
| 329 | msg_event = true; |
| 330 | wake_up(&msg_wq); |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * nss_debug_interface_handler() |
| 335 | * handle NSS -> HLOS messages for debug interfaces |
| 336 | */ |
| 337 | static void nss_debug_interface_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 338 | { |
| 339 | struct nss_debug_interface_msg *ntm = (struct nss_debug_interface_msg *)ncm; |
| 340 | nss_log_msg_callback_t cb; |
| 341 | |
| 342 | BUG_ON(ncm->interface != NSS_DEBUG_INTERFACE); |
| 343 | |
| 344 | /* |
| 345 | * Is this a valid request/response packet? |
| 346 | */ |
| 347 | if (ncm->type > NSS_DEBUG_INTERFACE_TYPE_MAX) { |
| 348 | nss_warning("%p: received invalid message %d for CAPWAP interface", nss_ctx, ncm->type); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | if (ncm->len > sizeof(struct nss_debug_interface_msg)) { |
| 353 | nss_warning("%p: Length of message is greater than required: %d", nss_ctx, ncm->interface); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 358 | |
| 359 | /* |
| 360 | * Update the callback and app_data for NOTIFY messages. |
| 361 | */ |
| 362 | if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) { |
| 363 | ncm->cb = (uint32_t)nss_debug_interface_cb; |
| 364 | ncm->app_data = (uint32_t)nss_debug_interface_app_data; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Do we have a callback |
| 369 | */ |
| 370 | if (!ncm->cb) { |
| 371 | nss_trace("%p: cb is null for interface %d", nss_ctx, ncm->interface); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | cb = (nss_log_msg_callback_t)ncm->cb; |
| 376 | cb((void *)ncm->app_data, ntm); |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | * nss_debug_interface_tx() |
| 381 | * Transmit a debug interface message to NSS FW |
| 382 | */ |
| 383 | static nss_tx_status_t nss_debug_interface_tx(struct nss_ctx_instance *nss_ctx, struct nss_debug_interface_msg *msg) |
| 384 | { |
| 385 | struct nss_debug_interface_msg *nm; |
| 386 | struct nss_cmn_msg *ncm = &msg->cm; |
| 387 | struct sk_buff *nbuf; |
| 388 | int32_t status; |
| 389 | |
| 390 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 391 | nss_warning("%p: debug if msg dropped as core not ready", nss_ctx); |
| 392 | return NSS_TX_FAILURE_NOT_READY; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Sanity check the message |
| 397 | */ |
| 398 | if (ncm->interface != NSS_DEBUG_INTERFACE) { |
| 399 | nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface); |
| 400 | return NSS_TX_FAILURE; |
| 401 | } |
| 402 | |
| 403 | if (ncm->type > NSS_DEBUG_INTERFACE_TYPE_MAX) { |
| 404 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 405 | return NSS_TX_FAILURE; |
| 406 | } |
| 407 | |
| 408 | if (ncm->len > sizeof(struct nss_debug_interface_msg)) { |
| 409 | nss_warning("%p: message length is invalid: %d", nss_ctx, ncm->len); |
| 410 | return NSS_TX_FAILURE; |
| 411 | } |
| 412 | |
| 413 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
| 414 | if (unlikely(!nbuf)) { |
Sundarajan Srinivasan | 62fee7e | 2015-01-22 11:13:10 -0800 | [diff] [blame^] | 415 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 416 | nss_warning("%p: msg dropped as command allocation failed", nss_ctx); |
| 417 | return NSS_TX_FAILURE; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Copy the message to our skb |
| 422 | */ |
| 423 | nm = (struct nss_debug_interface_msg *)skb_put(nbuf, sizeof(struct nss_debug_interface_msg)); |
| 424 | memcpy(nm, msg, sizeof(struct nss_debug_interface_msg)); |
| 425 | |
| 426 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 427 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 428 | dev_kfree_skb_any(nbuf); |
| 429 | nss_warning("%p: Unable to enqueue 'debug if message' \n", nss_ctx); |
| 430 | return NSS_TX_FAILURE; |
| 431 | } |
| 432 | |
| 433 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit, |
| 434 | NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 435 | |
| 436 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 437 | return NSS_TX_SUCCESS; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * nss_debug_log_buffer_alloc() |
| 442 | * Allocates and Initializes log buffer for the use in NSS FW (logging) |
| 443 | */ |
| 444 | bool nss_debug_log_buffer_alloc(uint8_t nss_id, uint32_t nentry) |
| 445 | { |
| 446 | struct nss_ring_buffer_addr old_rbe; |
| 447 | struct nss_debug_interface_msg msg; |
| 448 | struct nss_debug_log_memory_msg *dbg; |
| 449 | struct nss_top_instance *nss_top; |
| 450 | struct nss_ctx_instance *nss_ctx; |
| 451 | dma_addr_t dma_addr; |
| 452 | uint32_t size; |
| 453 | void *addr = NULL; |
| 454 | nss_tx_status_t status; |
| 455 | bool err = false; |
| 456 | bool old_state = false; |
| 457 | |
| 458 | if (nss_id > NSS_MAX_CORES) { |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | nss_top = &nss_top_main; |
| 463 | nss_ctx = &nss_top->nss[nss_id]; |
| 464 | |
| 465 | if (nss_ctx->state != NSS_CORE_STATE_INITIALIZED) { |
| 466 | nss_warning("%p: NSS Core:%d is not initialized yet\n", nss_ctx, nss_id); |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | memset(&msg, 0, sizeof(struct nss_debug_interface_msg)); |
| 471 | |
| 472 | size = sizeof (struct nss_log_descriptor) + (sizeof (struct nss_log_entry) * nentry); |
| 473 | addr = kmalloc(size, GFP_ATOMIC); |
| 474 | if (!addr) { |
| 475 | nss_warning("%p: Failed to allocate memory for logging (size:%d)\n", nss_ctx, size); |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | memset(addr, 0, size); |
| 480 | dma_addr = (uint32_t)dma_map_single(NULL, addr, size, DMA_FROM_DEVICE); |
| 481 | if (unlikely(dma_mapping_error(NULL, dma_addr))) { |
| 482 | nss_warning("%p: Failed to map address in DMA", nss_ctx); |
| 483 | goto fail2; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * If we already have ring buffer associated with nss_id, then |
| 488 | * we must wait before we attach a new ring buffer. |
| 489 | */ |
| 490 | mutex_lock(&nss_log_mutex); |
| 491 | if (nss_rbe[nss_id].addr) { |
| 492 | mutex_unlock(&nss_log_mutex); |
| 493 | if (!wait_event_timeout(nss_log_wq, nss_rbe[nss_id].refcnt == 0, 5 * HZ)) { |
| 494 | nss_warning("%p: Timeout waiting for refcnt to become 0\n", nss_ctx); |
| 495 | goto fail1; |
| 496 | } |
| 497 | |
| 498 | mutex_lock(&nss_log_mutex); |
| 499 | if (!nss_rbe[nss_id].addr) { |
| 500 | mutex_unlock(&nss_log_mutex); |
| 501 | goto fail1; |
| 502 | } |
| 503 | if (nss_rbe[nss_id].refcnt > 0) { |
| 504 | mutex_unlock(&nss_log_mutex); |
| 505 | nss_warning("%p: Some other thread is condenting..opting out\n", nss_ctx); |
| 506 | goto fail1; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Save the original dma buffer. In case we fail down the line, we will |
| 511 | * restore the state. Otherwise, old_state will be freed once we get |
| 512 | * ACK from NSS FW. |
| 513 | */ |
| 514 | old_state = true; |
| 515 | memcpy(&old_rbe, &nss_rbe[nss_id], sizeof (struct nss_ring_buffer_addr)); |
| 516 | } |
| 517 | |
| 518 | nss_rbe[nss_id].addr = addr; |
| 519 | nss_rbe[nss_id].nentries = nentry; |
| 520 | nss_rbe[nss_id].refcnt = 1; /* Block other threads till we are done */ |
| 521 | nss_rbe[nss_id].dma_addr = dma_addr; |
| 522 | mutex_unlock(&nss_log_mutex); |
| 523 | |
| 524 | memset(&msg, 0, sizeof (struct nss_debug_interface_msg)); |
| 525 | nss_cmn_msg_init(&msg.cm, NSS_DEBUG_INTERFACE, NSS_DEBUG_INTERFACE_TYPE_LOG_BUF_INIT, |
| 526 | sizeof(struct nss_debug_log_memory_msg), nss_debug_interface_event, NULL); |
| 527 | |
| 528 | dbg = &msg.msg.addr; |
| 529 | dbg->nentry = nentry; |
| 530 | dbg->version = NSS_DEBUG_LOG_VERSION; |
Saurabh Misra | 6d42da7 | 2015-03-05 14:57:01 -0800 | [diff] [blame] | 531 | dbg->phy_addr = dma_addr; |
Saurabh Misra | 96998db | 2014-07-10 12:15:48 -0700 | [diff] [blame] | 532 | |
| 533 | msg_event = false; |
| 534 | status = nss_debug_interface_tx(nss_ctx, &msg); |
| 535 | if (status != NSS_TX_SUCCESS) { |
| 536 | nss_warning("%p: Failed to send message to debug interface:%d\n", nss_ctx, status); |
| 537 | err = true; |
| 538 | } else { |
| 539 | int r; |
| 540 | |
| 541 | /* |
| 542 | * Wait for 5 seconds since this is a critical operation. |
| 543 | */ |
| 544 | r = wait_event_timeout(msg_wq, msg_event == true, 5 * HZ); |
| 545 | if (r == 0) { |
| 546 | nss_warning("%p: Timeout send message to debug interface\n", nss_ctx); |
| 547 | err = true; |
| 548 | } else if (msg_response != NSS_CMN_RESPONSE_ACK) { |
| 549 | nss_warning("%p: Response error for send message to debug interface:%d\n", nss_ctx, msg_response); |
| 550 | err = true; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * If we had to free the previous allocation for ring buffer. |
| 556 | */ |
| 557 | if (old_state == true) { |
| 558 | /* |
| 559 | * If we didn't fail, then we must unmap and free previous dma buffer |
| 560 | */ |
| 561 | if (err == false) { |
| 562 | uint32_t old_size; |
| 563 | |
| 564 | old_size = sizeof (struct nss_log_descriptor) + |
| 565 | (sizeof (struct nss_log_entry) * old_rbe.nentries); |
| 566 | dma_unmap_single(NULL, old_rbe.dma_addr, old_size, DMA_FROM_DEVICE); |
| 567 | kfree(old_rbe.addr); |
| 568 | } else { |
| 569 | /* |
| 570 | * Restore the original dma buffer since we failed somewhere. |
| 571 | */ |
| 572 | mutex_lock(&nss_log_mutex); |
| 573 | memcpy(&nss_rbe[nss_id], &old_rbe, sizeof (struct nss_ring_buffer_addr)); |
| 574 | mutex_unlock(&nss_log_mutex); |
| 575 | wake_up(&nss_log_wq); |
| 576 | } |
| 577 | } else { |
| 578 | /* |
| 579 | * There was no logbuffer allocated from host side. |
| 580 | */ |
| 581 | |
| 582 | /* |
| 583 | * If there was error, then we need to reset back. Note that we are |
| 584 | * still holding refcnt. |
| 585 | */ |
| 586 | if (err == true) { |
| 587 | mutex_lock(&nss_log_mutex); |
| 588 | nss_rbe[nss_id].addr = NULL; |
| 589 | nss_rbe[nss_id].nentries = 0; |
| 590 | nss_rbe[nss_id].refcnt = 0; |
| 591 | nss_rbe[nss_id].dma_addr = 0; |
| 592 | mutex_unlock(&nss_log_mutex); |
| 593 | wake_up(&nss_log_wq); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (err == false) { |
| 598 | mutex_lock(&nss_log_mutex); |
| 599 | nss_rbe[nss_id].refcnt--; /* we are done */ |
| 600 | mutex_unlock(&nss_log_mutex); |
| 601 | wake_up(&nss_log_wq); |
| 602 | return true; |
| 603 | } |
| 604 | |
| 605 | fail1: |
| 606 | if (addr) { |
| 607 | dma_unmap_single(NULL, dma_addr, size, DMA_FROM_DEVICE); |
| 608 | } |
| 609 | fail2: |
| 610 | kfree(addr); |
| 611 | wake_up(&nss_log_wq); |
| 612 | return false; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | * nss_logbuffer_handler() |
| 617 | * Enable NSS debug output |
| 618 | */ |
| 619 | int nss_logbuffer_handler(ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos) |
| 620 | { |
| 621 | int ret; |
| 622 | int i; |
| 623 | |
| 624 | ret = proc_dointvec(ctl, write, buffer, lenp, ppos); |
| 625 | if (ret) { |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | if (!write) { |
| 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | if (nss_ctl_logbuf < 32) { |
| 634 | printk("Invalid NSS FW logbuffer size:%d (must be > 32)\n", nss_ctl_logbuf); |
| 635 | nss_ctl_logbuf = 0; |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | for (i = 0; i < NSS_MAX_CORES; i++) { |
| 640 | if (nss_debug_log_buffer_alloc(i, nss_ctl_logbuf) == false) { |
| 641 | nss_warning("%d: Failed to set debug log buffer on NSS core", i); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | return ret; |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * nss_log_init() |
| 650 | * Initializes NSS FW logs retrieval logic from /sys |
| 651 | */ |
| 652 | void nss_log_init(void) |
| 653 | { |
| 654 | int core_status; |
| 655 | int i; |
| 656 | |
| 657 | memset(nss_rbe, 0, sizeof(nss_rbe)); |
| 658 | init_waitqueue_head(&nss_log_wq); |
| 659 | init_waitqueue_head(&msg_wq); |
| 660 | |
| 661 | /* |
| 662 | * Create directory for obtaining NSS FW logs from each core |
| 663 | */ |
| 664 | nss_top_main.logs_dentry = debugfs_create_dir("logs", nss_top_main.top_dentry); |
| 665 | if (unlikely(!nss_top_main.logs_dentry)) { |
| 666 | nss_warning("Failed to create qca-nss-drv/logs directory in debugfs"); |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | for (i = 0; i < NSS_MAX_CORES; i++) { |
| 671 | char file[10]; |
| 672 | extern struct file_operations nss_logs_core_ops; |
| 673 | |
| 674 | snprintf(file, sizeof(file), "core%d", i); |
| 675 | nss_top_main.core_log_dentry = debugfs_create_file(file, 0400, |
| 676 | nss_top_main.logs_dentry, (void *)i, &nss_logs_core_ops); |
| 677 | if (unlikely(!nss_top_main.core_log_dentry)) { |
| 678 | nss_warning("Failed to create qca-nss-drv/logs/%s file in debugfs", file); |
| 679 | return; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | nss_debug_interface_set_callback(nss_debug_interface_event, NULL); |
| 684 | core_status = nss_core_register_handler(NSS_DEBUG_INTERFACE, nss_debug_interface_handler, NULL); |
| 685 | if (core_status != NSS_CORE_STATUS_SUCCESS) { |
| 686 | nss_warning("NSS logbuffer init failed with register handler:%d\n", core_status); |
| 687 | } |
| 688 | } |