1 /** @file circularbuf.c
3 * PCIe host driver and dongle firmware need to communicate with each other. The mechanism consists
4 * of multiple circular buffers located in (DMA'able) host memory. A circular buffer is either used
5 * for host -> dongle (h2d) or dongle -> host communication. Both host driver and firmware make use
6 * of this source file. This source file contains functions to manage such a set of circular
7 * buffers, but does not contain the code to read or write the data itself into the buffers. It
8 * leaves that up to the software layer that uses this file, which can be implemented either using
9 * pio or DMA transfers. It also leaves the format of the data that is written and read to a higher
10 * layer. Typically the data is in the form of so-called 'message buffers'.
12 * $Copyright Open Broadcom Corporation$
14 * $Id: circularbuf.c 467150 2014-04-02 17:30:43Z $
17 #include <circularbuf.h>
18 #include <bcmmsgbuf.h>
21 #define CIRCULARBUF_READ_SPACE_AT_END(x) \
22 ((x->w_ptr >= x->rp_ptr) ? (x->w_ptr - x->rp_ptr) : (x->e_ptr - x->rp_ptr))
24 #define CIRCULARBUF_READ_SPACE_AVAIL(x) \
25 (((CIRCULARBUF_READ_SPACE_AT_END(x) == 0) && (x->w_ptr < x->rp_ptr)) ? \
26 x->w_ptr : CIRCULARBUF_READ_SPACE_AT_END(x))
28 int cbuf_msg_level = CBUF_ERROR_VAL | CBUF_TRACE_VAL | CBUF_INFORM_VAL;
30 /* #define CBUF_DEBUG */
32 #define CBUF_DEBUG_CHECK(x) x
34 #define CBUF_DEBUG_CHECK(x)
35 #endif /* CBUF_DEBUG */
38 * -----------------------------------------------------------------------------
39 * Function : circularbuf_init
43 * Input Args : buf_base_addr: address of DMA'able host memory provided by caller
48 * -----------------------------------------------------------------------------
51 circularbuf_init(circularbuf_t *handle, void *buf_base_addr, uint16 total_buf_len)
53 handle->buf_addr = buf_base_addr;
55 handle->depth = handle->e_ptr = HTOL32(total_buf_len);
57 /* Initialize Read and Write pointers */
58 handle->w_ptr = handle->r_ptr = handle->wp_ptr = handle->rp_ptr = HTOL32(0);
59 handle->mb_ring_bell = NULL;
60 handle->mb_ctx = NULL;
66 * When an item is added to the circular buffer by the producing party, the consuming party has to
67 * be notified by means of a 'door bell' or 'ring'. This function allows the caller to register a
68 * 'ring' function that will be called when a 'write complete' occurs.
71 circularbuf_register_cb(circularbuf_t *handle, mb_ring_t mb_ring_func, void *ctx)
73 handle->mb_ring_bell = mb_ring_func;
79 circularbuf_check_sanity(circularbuf_t *handle)
81 if ((handle->e_ptr > handle->depth) ||
82 (handle->r_ptr > handle->e_ptr) ||
83 (handle->rp_ptr > handle->e_ptr) ||
84 (handle->w_ptr > handle->e_ptr))
86 printf("%s:%d: Pointers are corrupted.\n", __FUNCTION__, __LINE__);
87 circularbuf_debug_print(handle);
92 #endif /* CBUF_DEBUG */
95 * -----------------------------------------------------------------------------
96 * Function : circularbuf_reserve_for_write
99 * This function reserves N bytes for write in the circular buffer. The circularbuf
100 * implementation will only reserve space in the circular buffer and return
101 * the pointer to the address where the new data can be written.
102 * The actual write implementation (bcopy/dma) is outside the scope of
103 * circularbuf implementation.
106 * size - No. of bytes to reserve for write
109 * void * : Pointer to the reserved location. This is the address
110 * that will be used for write (dma/bcopy)
112 * -----------------------------------------------------------------------------
115 circularbuf_reserve_for_write(circularbuf_t *handle, uint16 size)
118 void *ret_ptr = NULL;
120 CBUF_DEBUG_CHECK(circularbuf_check_sanity(handle));
121 ASSERT(size < handle->depth);
123 if (handle->wp_ptr >= handle->r_ptr)
124 avail_space = handle->depth - handle->wp_ptr;
126 avail_space = handle->r_ptr - handle->wp_ptr;
128 ASSERT(avail_space <= handle->depth);
129 if (avail_space > size)
131 /* Great. We have enough space. */
132 ret_ptr = CIRCULARBUF_START(handle) + handle->wp_ptr;
135 * We need to update the wp_ptr for the next guy to write.
137 * Please Note : We are not updating the write pointer here. This can be
138 * done only after write is complete (In case of DMA, we can only schedule
139 * the DMA. Actual completion will be known only on DMA complete interrupt).
141 handle->wp_ptr += size;
146 * If there is no available space, we should check if there is some space left
147 * in the beginning of the circular buffer. Wrap-around case, where there is
148 * not enough space in the end of the circular buffer. But, there might be
149 * room in the beginning of the buffer.
151 if (handle->wp_ptr >= handle->r_ptr)
153 avail_space = handle->r_ptr;
154 if (avail_space > size)
156 /* OK. There is room in the beginning. Let's go ahead and use that.
157 * But, before that, we have left a hole at the end of the circular
158 * buffer as that was not sufficient to accomodate the requested
159 * size. Let's make sure this is updated in the circularbuf structure
160 * so that consumer does not use the hole.
162 handle->e_ptr = handle->wp_ptr;
163 handle->wp_ptr = size;
165 return CIRCULARBUF_START(handle);
169 /* We have tried enough to accomodate the new packet. There is no room for now. */
174 * -----------------------------------------------------------------------------
175 * Function : circularbuf_write_complete
178 * This function has to be called by the producer end of circularbuf to indicate to
179 * the circularbuf layer that data has been written and the write pointer can be
180 * updated. In the process, if there was a doorbell callback registered, that
181 * function would also be invoked as to notify the consuming party.
184 * dest_addr : Address where the data was written. This would be the
185 * same address that was reserved earlier.
186 * bytes_written : Length of data written
188 * -----------------------------------------------------------------------------
191 circularbuf_write_complete(circularbuf_t *handle, uint16 bytes_written)
193 CBUF_DEBUG_CHECK(circularbuf_check_sanity(handle));
195 /* Update the write pointer */
196 if ((handle->w_ptr + bytes_written) >= handle->depth) {
197 OSL_CACHE_FLUSH((void *) CIRCULARBUF_START(handle), bytes_written);
198 handle->w_ptr = bytes_written;
200 OSL_CACHE_FLUSH((void *) (CIRCULARBUF_START(handle) + handle->w_ptr),
202 handle->w_ptr += bytes_written;
205 /* And ring the door bell (mail box interrupt) to indicate to the peer that
206 * message is available for consumption.
208 if (handle->mb_ring_bell)
209 handle->mb_ring_bell(handle->mb_ctx);
213 * -----------------------------------------------------------------------------
214 * Function : circularbuf_get_read_ptr
217 * This function will be called by the consumer of circularbuf for reading data from
218 * the circular buffer. This will typically be invoked when the consumer gets a
219 * doorbell interrupt.
220 * Please note that the function only returns the pointer (and length) from
221 * where the data can be read. Actual read implementation is up to the
222 * consumer. It could be a bcopy or dma.
225 * void * : Address from where the data can be read.
226 * available_len : Length of data available for read.
228 * -----------------------------------------------------------------------------
231 circularbuf_get_read_ptr(circularbuf_t *handle, uint16 *available_len)
235 CBUF_DEBUG_CHECK(circularbuf_check_sanity(handle));
237 /* First check if there is any data available in the circular buffer */
238 *available_len = CIRCULARBUF_READ_SPACE_AVAIL(handle);
239 if (*available_len == 0)
243 * Although there might be data in the circular buffer for read, in
244 * cases of write wrap-around and read still in the end of the circular
245 * buffer, we might have to wrap around the read pending pointer also.
247 if (CIRCULARBUF_READ_SPACE_AT_END(handle) == 0)
250 ret_addr = CIRCULARBUF_START(handle) + handle->rp_ptr;
253 * Please note that we do not update the read pointer here. Only
254 * read pending pointer is updated, so that next reader knows where
256 * read pointer can only be updated when the read is complete.
258 handle->rp_ptr = (uint16)(ret_addr - CIRCULARBUF_START(handle) + *available_len);
260 ASSERT(*available_len <= handle->depth);
262 OSL_CACHE_INV((void *) ret_addr, *available_len);
268 * -----------------------------------------------------------------------------
269 * Function : circularbuf_read_complete
271 * This function has to be called by the consumer end of circularbuf to indicate
272 * that data has been consumed and the read pointer can be updated, so the producing side
273 * can can use the freed space for new entries.
277 * bytes_read : No. of bytes consumed by the consumer. This has to match
278 * the length returned by circularbuf_get_read_ptr
281 * CIRCULARBUF_SUCCESS : Otherwise
283 * -----------------------------------------------------------------------------
285 circularbuf_ret_t BCMFASTPATH
286 circularbuf_read_complete(circularbuf_t *handle, uint16 bytes_read)
288 CBUF_DEBUG_CHECK(circularbuf_check_sanity(handle));
289 ASSERT(bytes_read < handle->depth);
291 /* Update the read pointer */
292 if ((handle->w_ptr < handle->e_ptr) && (handle->r_ptr + bytes_read) > handle->e_ptr)
293 handle->r_ptr = bytes_read;
295 handle->r_ptr += bytes_read;
297 return CIRCULARBUF_SUCCESS;
301 * -----------------------------------------------------------------------------
302 * Function : circularbuf_revert_rp_ptr
305 * The rp_ptr update during circularbuf_get_read_ptr() is done to reflect the amount of data
306 * that is sent out to be read by the consumer. But the consumer may not always read the
307 * entire data. In such a case, the rp_ptr needs to be reverted back by 'left' bytes, where
308 * 'left' is the no. of bytes left unread.
311 * bytes : The no. of bytes left unread by the consumer
313 * -----------------------------------------------------------------------------
316 circularbuf_revert_rp_ptr(circularbuf_t *handle, uint16 bytes)
318 CBUF_DEBUG_CHECK(circularbuf_check_sanity(handle));
319 ASSERT(bytes < handle->depth);
321 handle->rp_ptr -= bytes;
323 return CIRCULARBUF_SUCCESS;