19de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* Copyright (c) 2015, The Linux Foundation. All rights reserved. 29de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * 39de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * Redistribution and use in source and binary forms, with or without 49de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * modification, are permitted provided that the following conditions are 59de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * met: 69de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * * Redistributions of source code must retain the above copyright 79de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * notice, this list of conditions and the following disclaimer. 89de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * * Redistributions in binary form must reproduce the above 99de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * copyright notice, this list of conditions and the following 109de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * disclaimer in the documentation and/or other materials provided 119de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * with the distribution. 129de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * * Neither the name of The Linux Foundation nor the names of its 139de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * contributors may be used to endorse or promote products derived 149de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * from this software without specific prior written permission. 159de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * 169de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 179de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 189de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 199de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 209de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 219de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 229de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 239de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 249de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 259de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 269de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 279de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik */ 289de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 299de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik#ifndef __RING_BUFFER_H 309de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik#define __RING_BUFFER_H 319de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 329de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* Ring buffer status codes */ 339de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaikenum rb_status { 349de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik RB_SUCCESS = 0, 359de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik RB_FAILURE = 1, 36eb3d1bfb7ba3935f3d5d31fc25a826760584e474Amarnath Hullur Subramanyam RB_FULL = 2, 37eb3d1bfb7ba3935f3d5d31fc25a826760584e474Amarnath Hullur Subramanyam RB_RETRY = 3, 389de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik}; 399de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 409de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaikstruct rb_stats { 419de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik u32 total_bytes_written; 429de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik u32 total_bytes_read; 439de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik u32 cur_valid_bytes; 449de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik unsigned int max_num_bufs; 459de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik size_t each_buf_size; 469de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik}; 479de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 489de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaiktypedef void (*threshold_call_back) (void *cb_ctx); 499de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 509de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* intiitalizes the ring buffer and returns the context to it */ 519de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaikvoid * ring_buffer_init(size_t size_of_buf, int num_bufs); 529de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 539de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* Frees up the mem allocated for this ring buffer operation */ 549de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaikvoid ring_buffer_deinit(void *ctx); 559de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 569de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* Writes writes length of bytes from buf to ring buffer */ 5757db12bad7921f7e4efa148f1a3a8ed37e92d5d4Amarnath Hullur Subramanyamenum rb_status rb_write(void *ctx, u8 *buf, size_t length, int overwrite, 5857db12bad7921f7e4efa148f1a3a8ed37e92d5d4Amarnath Hullur Subramanyam size_t record_length); 599de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 609de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* Tries to read max_length of bytes from ring buffer to buf 619de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * and returns actual length of bytes read from ring buffer 629de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik */ 639de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaiksize_t rb_read(void *ctx, u8 *buf, size_t max_length); 649de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 659de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* A buffer with possible maximum of bytes that can be read 669de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * from a single buffer of ring buffer 679de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * Ring buffer module looses the ownership of the buffer returned by this api, 689de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * which means the caller has to make sure to free the buffer returned. 699de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik */ 709de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaiku8 *rb_get_read_buf(void *ctx, size_t *length); 719de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 729de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* calls callback whenever ring_buffer reaches percent percentage of it'ss 739de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik * full size 749de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik */ 759de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaikvoid rb_config_threshold(void *ctx, 769de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik unsigned int num_min_bytes, 779de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik threshold_call_back callback, 789de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik void *cb_ctx); 799de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 809de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik/* Get the current status of ring buffer */ 819de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaikvoid rb_get_stats(void *ctx, struct rb_stats *rbs); 829de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik 839de438de8f6b21a0355e9f891133e0f18d026878Subhani Shaik#endif /* __RING_BUFFER_H */ 84