blob: bfdca657564101686045a5668d43d5ba7120349f [file] [log] [blame]
Saurabh Misra96998db2014-07-10 12:15:48 -07001/*
2 **************************************************************************
Gaurao Chaudhari89833c92020-01-10 14:58:26 -08003 * Copyright (c) 2014-2018, 2020, The Linux Foundation. All rights reserved.
Saurabh Misra96998db2014-07-10 12:15:48 -07004 * 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 Misra96998db2014-07-10 12:15:48 -070033#include <linux/device.h>
34#include <nss_hal.h>
35#include "nss_core.h"
36#include "nss_log.h"
37
38/*
Saurabh Misra96998db2014-07-10 12:15:48 -070039 * Private data for each device file open instance
40 */
41struct 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 Swensondd7b2962021-03-16 13:46:32 -060047 struct nss_ctx_instance *nss_ctx;
48 /* NSS ctx instance */
Saurabh Misra96998db2014-07-10 12:15:48 -070049};
50
Cemil Coskun5f51db52018-05-07 17:15:37 -070051struct nss_log_ring_buffer_addr nss_rbe[NSS_MAX_CORES];
Saurabh Misra96998db2014-07-10 12:15:48 -070052
53static DEFINE_MUTEX(nss_log_mutex);
54static wait_queue_head_t nss_log_wq;
55static nss_log_msg_callback_t nss_debug_interface_cb;
56static void *nss_debug_interface_app_data = NULL;
57
58static wait_queue_head_t msg_wq;
59enum nss_cmn_response msg_response;
60static bool msg_event;
61
62/*
63 * nss_log_llseek()
64 * Seek operation.
65 */
66static 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 */
90static 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 Wangaed46332016-12-12 17:29:03 -0800100 nss_id = (int)(nss_ptr_t)inode->i_private;
Suman Ghosh9f7b3702018-09-21 19:51:40 +0530101 if (nss_id < 0 || nss_id >= nss_top_main.num_nss) {
Saurabh Misra96998db2014-07-10 12:15:48 -0700102 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 Swensondd7b2962021-03-16 13:46:32 -0600111 nss_warning("%px: Failed to allocate memory for log_data", nss_ctx);
Saurabh Misra96998db2014-07-10 12:15:48 -0700112 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 Swensondd7b2962021-03-16 13:46:32 -0600119 nss_warning("%px: Ring buffer not configured yet for nss_id:%d", nss_ctx, nss_id);
Saurabh Misra96998db2014-07-10 12:15:48 -0700120 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 Swensondd7b2962021-03-16 13:46:32 -0600130 data->nss_ctx = nss_ctx;
Saurabh Misra96998db2014-07-10 12:15:48 -0700131
132 /*
133 * Increment the reference count so that we don't free
134 * the memory
135 */
Cemil Coskun5f51db52018-05-07 17:15:37 -0700136 nss_rbe[nss_id].ref_cnt++;
Saurabh Misra96998db2014-07-10 12:15:48 -0700137 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 */
149static 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 Coskun5f51db52018-05-07 17:15:37 -0700158 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 Misra96998db2014-07-10 12:15:48 -0700161 wake_up(&nss_log_wq);
162 }
163 mutex_unlock(&nss_log_mutex);
164 kfree(data);
165 return 0;
166}
167
168/*
Saurabh Misra6d42da72015-03-05 14:57:01 -0800169 * nss_log_current_entry()
170 * Reads current entry index from NSS log descriptor.
171 */
172static uint32_t nss_log_current_entry(struct nss_log_descriptor *desc)
173{
174 rmb();
175 return desc->current_entry;
176}
177
178/*
Saurabh Misra96998db2014-07-10 12:15:48 -0700179 * nss_log_read()
180 * Read operation lets command like cat and tail read our memory log buffer data.
181 */
182static 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 Swensondd7b2962021-03-16 13:46:32 -0600199 nss_warning("%px: load_mem is NULL", data);
Saurabh Misra96998db2014-07-10 12:15:48 -0700200 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 Swensondd7b2962021-03-16 13:46:32 -0600213 dma_sync_single_for_cpu(data->nss_ctx->dev, data->dma_addr, sizeof(struct nss_log_descriptor), DMA_FROM_DEVICE);
214
Saurabh Misra6d42da72015-03-05 14:57:01 -0800215 entry = nss_log_current_entry(desc);
Saurabh Misra96998db2014-07-10 12:15:48 -0700216
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 Coskun5f51db52018-05-07 17:15:37 -0700227 if (unlikely(!(*ppos))) {
Saurabh Misra96998db2014-07-10 12:15:48 -0700228 /*
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 Coskun5f51db52018-05-07 17:15:37 -0700255 offset = (offset * sizeof(struct nss_log_entry))
Saurabh Misra96998db2014-07-10 12:15:48 -0700256 + offsetof(struct nss_log_descriptor, log_ring_buffer);
257
Kyle Swensondd7b2962021-03-16 13:46:32 -0600258 dma_sync_single_for_cpu(data->nss_ctx->dev, data->dma_addr + offset,
Saurabh Misra96998db2014-07-10 12:15:48 -0700259 sizeof(struct nss_log_entry), DMA_FROM_DEVICE);
260 rb = &desc->log_ring_buffer[index];
261
Gaurao Chaudhari89833c92020-01-10 14:58:26 -0800262 b = scnprintf(msg, sizeof(msg), NSS_LOG_LINE_FORMAT,
Saurabh Misra96998db2014-07-10 12:15:48 -0700263 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 Coskun5f51db52018-05-07 17:15:37 -0700271 if (copy_to_user(buf + bytes, msg, b)) {
272 return -EFAULT;
Saurabh Misra96998db2014-07-10 12:15:48 -0700273 }
274
Cemil Coskun5f51db52018-05-07 17:15:37 -0700275 bytes += b;
276
Saurabh Misra96998db2014-07-10 12:15:48 -0700277 /*
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
290struct 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 */
302void 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 Coskun5f51db52018-05-07 17:15:37 -0700312static void nss_debug_interface_event(void *app_data, struct nss_log_debug_interface_msg *nim)
Saurabh Misra96998db2014-07-10 12:15:48 -0700313{
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 Coskun5f51db52018-05-07 17:15:37 -0700323 * handle NSS -> HLOS messages for debug interfaces
Saurabh Misra96998db2014-07-10 12:15:48 -0700324 */
325static void nss_debug_interface_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
326{
Cemil Coskun5f51db52018-05-07 17:15:37 -0700327 struct nss_log_debug_interface_msg *ntm = (struct nss_log_debug_interface_msg *)ncm;
Saurabh Misra96998db2014-07-10 12:15:48 -0700328 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 Swensondd7b2962021-03-16 13:46:32 -0600336 nss_warning("%px: received invalid message %d for CAPWAP interface", nss_ctx, ncm->type);
Saurabh Misra96998db2014-07-10 12:15:48 -0700337 return;
338 }
339
Cemil Coskun5f51db52018-05-07 17:15:37 -0700340 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_log_debug_interface_msg)) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600341 nss_warning("%px: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Saurabh Misra96998db2014-07-10 12:15:48 -0700342 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 Agarwale4ad24a2018-06-11 12:03:46 +0530350 if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -0800351 ncm->cb = (nss_ptr_t)nss_debug_interface_cb;
352 ncm->app_data = (nss_ptr_t)nss_debug_interface_app_data;
Saurabh Misra96998db2014-07-10 12:15:48 -0700353 }
354
355 /*
356 * Do we have a callback
357 */
358 if (!ncm->cb) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600359 nss_trace("%px: cb is null for interface %d", nss_ctx, ncm->interface);
Saurabh Misra96998db2014-07-10 12:15:48 -0700360 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 Coskun5f51db52018-05-07 17:15:37 -0700369 * Transmit a debug interface message to NSS FW
Saurabh Misra96998db2014-07-10 12:15:48 -0700370 */
Cemil Coskun5f51db52018-05-07 17:15:37 -0700371static nss_tx_status_t nss_debug_interface_tx(struct nss_ctx_instance *nss_ctx, struct nss_log_debug_interface_msg *msg)
Saurabh Misra96998db2014-07-10 12:15:48 -0700372{
Saurabh Misra96998db2014-07-10 12:15:48 -0700373 struct nss_cmn_msg *ncm = &msg->cm;
Saurabh Misra96998db2014-07-10 12:15:48 -0700374
375 /*
376 * Sanity check the message
377 */
378 if (ncm->interface != NSS_DEBUG_INTERFACE) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600379 nss_warning("%px: tx request for another interface: %d", nss_ctx, ncm->interface);
Saurabh Misra96998db2014-07-10 12:15:48 -0700380 return NSS_TX_FAILURE;
381 }
382
383 if (ncm->type > NSS_DEBUG_INTERFACE_TYPE_MAX) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600384 nss_warning("%px: message type out of range: %d", nss_ctx, ncm->type);
Saurabh Misra96998db2014-07-10 12:15:48 -0700385 return NSS_TX_FAILURE;
386 }
387
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700388 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Saurabh Misra96998db2014-07-10 12:15:48 -0700389}
390
391/*
392 * nss_debug_log_buffer_alloc()
393 * Allocates and Initializes log buffer for the use in NSS FW (logging)
394 */
395bool nss_debug_log_buffer_alloc(uint8_t nss_id, uint32_t nentry)
396{
Cemil Coskun5f51db52018-05-07 17:15:37 -0700397 struct nss_log_debug_interface_msg msg;
398 struct nss_log_debug_memory_msg *dbg;
Saurabh Misra96998db2014-07-10 12:15:48 -0700399 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 Misra96998db2014-07-10 12:15:48 -0700405
Suman Ghosh9f7b3702018-09-21 19:51:40 +0530406 if (nss_id >= nss_top_main.num_nss) {
Saurabh Misra96998db2014-07-10 12:15:48 -0700407 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 Swensondd7b2962021-03-16 13:46:32 -0600414 nss_warning("%px: NSS Core:%d is not initialized yet\n", nss_ctx, nss_id);
Saurabh Misra96998db2014-07-10 12:15:48 -0700415 return false;
416 }
417
Cemil Coskun5f51db52018-05-07 17:15:37 -0700418 size = sizeof(struct nss_log_descriptor) + (sizeof(struct nss_log_entry) * nentry);
Saurabh Misra96998db2014-07-10 12:15:48 -0700419 addr = kmalloc(size, GFP_ATOMIC);
420 if (!addr) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600421 nss_warning("%px: Failed to allocate memory for logging (size:%d)\n", nss_ctx, size);
Saurabh Misra96998db2014-07-10 12:15:48 -0700422 return false;
423 }
424
425 memset(addr, 0, size);
Stephen Wangefd38512017-01-24 14:01:02 -0800426 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 Swensondd7b2962021-03-16 13:46:32 -0600428 nss_warning("%px: Failed to map address in DMA", nss_ctx);
Cemil Coskun5f51db52018-05-07 17:15:37 -0700429 kfree(addr);
430 return false;
Saurabh Misra96998db2014-07-10 12:15:48 -0700431 }
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 Coskun5f51db52018-05-07 17:15:37 -0700440
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 Swensondd7b2962021-03-16 13:46:32 -0600447 nss_warning("%px: Timeout waiting for refcnt to become 0\n", nss_ctx);
Cemil Coskun5f51db52018-05-07 17:15:37 -0700448 goto fail;
Saurabh Misra96998db2014-07-10 12:15:48 -0700449 }
450
451 mutex_lock(&nss_log_mutex);
452 if (!nss_rbe[nss_id].addr) {
453 mutex_unlock(&nss_log_mutex);
Cemil Coskun5f51db52018-05-07 17:15:37 -0700454 goto fail;
Saurabh Misra96998db2014-07-10 12:15:48 -0700455 }
Cemil Coskun5f51db52018-05-07 17:15:37 -0700456 if (nss_rbe[nss_id].ref_cnt > 0) {
Saurabh Misra96998db2014-07-10 12:15:48 -0700457 mutex_unlock(&nss_log_mutex);
Kyle Swensondd7b2962021-03-16 13:46:32 -0600458 nss_warning("%px: Some other thread is contending..opting out\n", nss_ctx);
Cemil Coskun5f51db52018-05-07 17:15:37 -0700459 goto fail;
Saurabh Misra96998db2014-07-10 12:15:48 -0700460 }
Saurabh Misra96998db2014-07-10 12:15:48 -0700461 }
462
Cemil Coskun5f51db52018-05-07 17:15:37 -0700463 memset(&msg, 0, sizeof(struct nss_log_debug_interface_msg));
Saurabh Misra96998db2014-07-10 12:15:48 -0700464 nss_cmn_msg_init(&msg.cm, NSS_DEBUG_INTERFACE, NSS_DEBUG_INTERFACE_TYPE_LOG_BUF_INIT,
Cemil Coskun5f51db52018-05-07 17:15:37 -0700465 sizeof(struct nss_log_debug_memory_msg), nss_debug_interface_event, NULL);
Saurabh Misra96998db2014-07-10 12:15:48 -0700466
467 dbg = &msg.msg.addr;
468 dbg->nentry = nentry;
469 dbg->version = NSS_DEBUG_LOG_VERSION;
Saurabh Misra6d42da72015-03-05 14:57:01 -0800470 dbg->phy_addr = dma_addr;
Saurabh Misra96998db2014-07-10 12:15:48 -0700471
472 msg_event = false;
473 status = nss_debug_interface_tx(nss_ctx, &msg);
474 if (status != NSS_TX_SUCCESS) {
Cemil Coskun5f51db52018-05-07 17:15:37 -0700475 mutex_unlock(&nss_log_mutex);
Kyle Swensondd7b2962021-03-16 13:46:32 -0600476 nss_warning("%px: Failed to send message to debug interface:%d\n", nss_ctx, status);
Cemil Coskun5f51db52018-05-07 17:15:37 -0700477 goto fail;
478 }
Saurabh Misra96998db2014-07-10 12:15:48 -0700479
Cemil Coskun5f51db52018-05-07 17:15:37 -0700480 /*
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 Swensondd7b2962021-03-16 13:46:32 -0600487 nss_warning("%px: Timeout send message to debug interface\n", nss_ctx);
Cemil Coskun5f51db52018-05-07 17:15:37 -0700488 goto fail;
489 }
490
491 if (msg_response != NSS_CMN_RESPONSE_ACK) {
492 mutex_unlock(&nss_log_mutex);
Kyle Swensondd7b2962021-03-16 13:46:32 -0600493 nss_warning("%px: Response error for send message to debug interface:%d\n", nss_ctx, msg_response);
Cemil Coskun5f51db52018-05-07 17:15:37 -0700494 goto fail;
Saurabh Misra96998db2014-07-10 12:15:48 -0700495 }
496
497 /*
498 * If we had to free the previous allocation for ring buffer.
499 */
Cemil Coskun5f51db52018-05-07 17:15:37 -0700500 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 Misra96998db2014-07-10 12:15:48 -0700506 }
507
Cemil Coskun5f51db52018-05-07 17:15:37 -0700508 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 Misra96998db2014-07-10 12:15:48 -0700515
Cemil Coskun5f51db52018-05-07 17:15:37 -0700516fail:
Kyle Swensondd7b2962021-03-16 13:46:32 -0600517 dma_unmap_single(nss_ctx->dev, dma_addr, size, DMA_FROM_DEVICE);
Saurabh Misra96998db2014-07-10 12:15:48 -0700518 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 Wang52e6d342016-03-29 15:02:33 -0700527int nss_logbuffer_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
Saurabh Misra96998db2014-07-10 12:15:48 -0700528{
529 int ret;
Thomas Wucf215082017-08-02 14:23:37 -0700530 int core_status;
Saurabh Misra96998db2014-07-10 12:15:48 -0700531 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 Coskun5f51db52018-05-07 17:15:37 -0700543 nss_warning("Invalid NSS FW logbuffer size:%d (must be > 32)\n", nss_ctl_logbuf);
Saurabh Misra96998db2014-07-10 12:15:48 -0700544 nss_ctl_logbuf = 0;
545 return ret;
546 }
547
Suman Ghosh9f7b3702018-09-21 19:51:40 +0530548 for (i = 0; i < nss_top_main.num_nss; i++) {
Thomas Wucf215082017-08-02 14:23:37 -0700549 /*
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 Coskun5f51db52018-05-07 17:15:37 -0700557 if (!nss_debug_log_buffer_alloc(i, nss_ctl_logbuf)) {
Saurabh Misra96998db2014-07-10 12:15:48 -0700558 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 */
569void nss_log_init(void)
570{
Saurabh Misra96998db2014-07-10 12:15:48 -0700571 int i;
Yu Huang8c107082017-07-24 14:58:26 -0700572 struct dentry *logs_dentry;
573 struct dentry *core_log_dentry;
Saurabh Misra96998db2014-07-10 12:15:48 -0700574
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 Huang8c107082017-07-24 14:58:26 -0700582 logs_dentry = debugfs_create_dir("logs", nss_top_main.top_dentry);
583 if (unlikely(!logs_dentry)) {
Saurabh Misra96998db2014-07-10 12:15:48 -0700584 nss_warning("Failed to create qca-nss-drv/logs directory in debugfs");
585 return;
586 }
587
Suman Ghosh9f7b3702018-09-21 19:51:40 +0530588 for (i = 0; i < nss_top_main.num_nss; i++) {
Naresh Kumar Mehta9ff70e42019-01-03 14:45:53 +0530589 char file[16];
Saurabh Misra96998db2014-07-10 12:15:48 -0700590 extern struct file_operations nss_logs_core_ops;
591
592 snprintf(file, sizeof(file), "core%d", i);
Yu Huang8c107082017-07-24 14:58:26 -0700593 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 Misra96998db2014-07-10 12:15:48 -0700596 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 Misra96998db2014-07-10 12:15:48 -0700602}