xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/include/bcm_ring.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * bcm_ring.h : Ring context abstraction
3*4882a593Smuzhiyun  * The ring context tracks the WRITE and READ indices where elements may be
4*4882a593Smuzhiyun  * produced and consumed respectively. All elements in the ring need to be
5*4882a593Smuzhiyun  * fixed size.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * NOTE: A ring of size N, may only hold N-1 elements.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  * Copyright (C) 1999-2017, Broadcom Corporation
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *      Unless you and Broadcom execute a separate written software license
14*4882a593Smuzhiyun  * agreement governing use of this software, this software is licensed to you
15*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 (the "GPL"),
16*4882a593Smuzhiyun  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
17*4882a593Smuzhiyun  * following added to such license:
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *      As a special exception, the copyright holders of this software give you
20*4882a593Smuzhiyun  * permission to link this software with independent modules, and to copy and
21*4882a593Smuzhiyun  * distribute the resulting executable under terms of your choice, provided that
22*4882a593Smuzhiyun  * you also meet, for each linked independent module, the terms and conditions of
23*4882a593Smuzhiyun  * the license of that module.  An independent module is a module which is not
24*4882a593Smuzhiyun  * derived from this software.  The special exception does not apply to any
25*4882a593Smuzhiyun  * modifications of the software.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  *      Notwithstanding the above, under no circumstances may you combine this
28*4882a593Smuzhiyun  * software in any way with any other Broadcom software provided under a license
29*4882a593Smuzhiyun  * other than the GPL, without Broadcom's express prior written consent.
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * <<Broadcom-WL-IPTag/Open:>>
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * $Id: bcm_ring.h 700321 2017-05-18 16:09:07Z $
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun #ifndef __bcm_ring_included__
37*4882a593Smuzhiyun #define __bcm_ring_included__
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * API Notes:
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * Ring manipulation API allows for:
42*4882a593Smuzhiyun  *  Pending operations: Often before some work can be completed, it may be
43*4882a593Smuzhiyun  *  desired that several resources are available, e.g. space for production in
44*4882a593Smuzhiyun  *  a ring. Approaches such as, #1) reserve resources one by one and return them
45*4882a593Smuzhiyun  *  if another required resource is not available, or #2) employ a two pass
46*4882a593Smuzhiyun  *  algorithm of first testing whether all resources are available, have a
47*4882a593Smuzhiyun  *  an impact on performance critical code. The approach taken here is more akin
48*4882a593Smuzhiyun  *  to approach #2, where a test for resource availability essentially also
49*4882a593Smuzhiyun  *  provides the index for production in an un-committed state.
50*4882a593Smuzhiyun  *  The same approach is taken for the consumer side.
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  *  - Pending production: Fetch the next index where a ring element may be
53*4882a593Smuzhiyun  *    produced. The caller may not commit the WRITE of the element.
54*4882a593Smuzhiyun  *  - Pending consumption: Fetch the next index where a ring element may be
55*4882a593Smuzhiyun  *    consumed. The caller may not commut the READ of the element.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  *  Producer side API:
58*4882a593Smuzhiyun  *  - bcm_ring_is_full  : Test whether ring is full
59*4882a593Smuzhiyun  *  - bcm_ring_prod     : Fetch index where an element may be produced (commit)
60*4882a593Smuzhiyun  *  - bcm_ring_prod_pend: Fetch index where an element may be produced (pending)
61*4882a593Smuzhiyun  *  - bcm_ring_prod_done: Commit a previous pending produce fetch
62*4882a593Smuzhiyun  *  - bcm_ring_prod_avail: Fetch total number free slots eligible for production
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * Consumer side API:
65*4882a593Smuzhiyun  *  - bcm_ring_is_empty : Test whether ring is empty
66*4882a593Smuzhiyun  *  - bcm_ring_cons     : Fetch index where an element may be consumed (commit)
67*4882a593Smuzhiyun  *  - bcm_ring_cons_pend: Fetch index where an element may be consumed (pending)
68*4882a593Smuzhiyun  *  - bcm_ring_cons_done: Commit a previous pending consume fetch
69*4882a593Smuzhiyun  *  - bcm_ring_cons_avail: Fetch total number elements eligible for consumption
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  *  - bcm_ring_sync_read: Sync read offset in peer ring, from local ring
72*4882a593Smuzhiyun  *  - bcm_ring_sync_write: Sync write offset in peer ring, from local ring
73*4882a593Smuzhiyun  *
74*4882a593Smuzhiyun  * +----------------------------------------------------------------------------
75*4882a593Smuzhiyun  *
76*4882a593Smuzhiyun  * Design Notes:
77*4882a593Smuzhiyun  * Following items are not tracked in a ring context (design decision)
78*4882a593Smuzhiyun  *  - width of a ring element.
79*4882a593Smuzhiyun  *  - depth of the ring.
80*4882a593Smuzhiyun  *  - base of the buffer, where the elements are stored.
81*4882a593Smuzhiyun  *  - count of number of free slots in the ring
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * Implementation Notes:
84*4882a593Smuzhiyun  *  - When BCM_RING_DEBUG is enabled, need explicit bcm_ring_init().
85*4882a593Smuzhiyun  *  - BCM_RING_EMPTY and BCM_RING_FULL are (-1)
86*4882a593Smuzhiyun  *
87*4882a593Smuzhiyun  * +----------------------------------------------------------------------------
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * Usage Notes:
90*4882a593Smuzhiyun  * An application may incarnate a ring of some fixed sized elements, by defining
91*4882a593Smuzhiyun  *  - a ring data buffer to store the ring elements.
92*4882a593Smuzhiyun  *  - depth of the ring (max number of elements managed by ring context).
93*4882a593Smuzhiyun  *    Preferrably, depth may be represented as a constant.
94*4882a593Smuzhiyun  *  - width of a ring element: to be used in pointer arithmetic with the ring's
95*4882a593Smuzhiyun  *    data buffer base and an index to fetch the ring element.
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  * Use bcm_workq_t to instantiate a pair of workq constructs, one for the
98*4882a593Smuzhiyun  * producer and the other for the consumer, both pointing to the same circular
99*4882a593Smuzhiyun  * buffer. The producer may operate on it's own local workq and flush the write
100*4882a593Smuzhiyun  * index to the consumer. Likewise the consumer may use its local workq and
101*4882a593Smuzhiyun  * flush the read index to the producer. This way we do not repeatedly access
102*4882a593Smuzhiyun  * the peer's context. The two peers may reside on different CPU cores with a
103*4882a593Smuzhiyun  * private L1 data cache.
104*4882a593Smuzhiyun  * +----------------------------------------------------------------------------
105*4882a593Smuzhiyun  *
106*4882a593Smuzhiyun  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * Copyright (C) 1999-2017, Broadcom Corporation
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  *      Unless you and Broadcom execute a separate written software license
111*4882a593Smuzhiyun  * agreement governing use of this software, this software is licensed to you
112*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 (the "GPL"),
113*4882a593Smuzhiyun  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
114*4882a593Smuzhiyun  * following added to such license:
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  *      As a special exception, the copyright holders of this software give you
117*4882a593Smuzhiyun  * permission to link this software with independent modules, and to copy and
118*4882a593Smuzhiyun  * distribute the resulting executable under terms of your choice, provided that
119*4882a593Smuzhiyun  * you also meet, for each linked independent module, the terms and conditions of
120*4882a593Smuzhiyun  * the license of that module.  An independent module is a module which is not
121*4882a593Smuzhiyun  * derived from this software.  The special exception does not apply to any
122*4882a593Smuzhiyun  * modifications of the software.
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  *      Notwithstanding the above, under no circumstances may you combine this
125*4882a593Smuzhiyun  * software in any way with any other Broadcom software provided under a license
126*4882a593Smuzhiyun  * other than the GPL, without Broadcom's express prior written consent.
127*4882a593Smuzhiyun  *
128*4882a593Smuzhiyun  * $Id: bcm_ring.h 700321 2017-05-18 16:09:07Z $
129*4882a593Smuzhiyun  *
130*4882a593Smuzhiyun  * -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
131*4882a593Smuzhiyun  * vim: set ts=4 noet sw=4 tw=80:
132*4882a593Smuzhiyun  *
133*4882a593Smuzhiyun  * +----------------------------------------------------------------------------
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun #ifdef ____cacheline_aligned
137*4882a593Smuzhiyun #define __ring_aligned                      ____cacheline_aligned
138*4882a593Smuzhiyun #else
139*4882a593Smuzhiyun #define __ring_aligned
140*4882a593Smuzhiyun #endif // endif
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /* Conditional compile for debug */
143*4882a593Smuzhiyun /* #define BCM_RING_DEBUG */
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun #define BCM_RING_EMPTY                      (-1)
146*4882a593Smuzhiyun #define BCM_RING_FULL                       (-1)
147*4882a593Smuzhiyun #define BCM_RING_NULL                       ((bcm_ring_t *)NULL)
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun #if defined(BCM_RING_DEBUG)
150*4882a593Smuzhiyun #define RING_ASSERT(exp)                    ASSERT(exp)
151*4882a593Smuzhiyun #define BCM_RING_IS_VALID(ring)             (((ring) != BCM_RING_NULL) && \
152*4882a593Smuzhiyun 	                                         ((ring)->self == (ring)))
153*4882a593Smuzhiyun #else  /* ! BCM_RING_DEBUG */
154*4882a593Smuzhiyun #define RING_ASSERT(exp)                    do {} while (0)
155*4882a593Smuzhiyun #define BCM_RING_IS_VALID(ring)             ((ring) != BCM_RING_NULL)
156*4882a593Smuzhiyun #endif /* ! BCM_RING_DEBUG */
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun #define BCM_RING_SIZE_IS_VALID(ring_size)   ((ring_size) > 0)
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /*
161*4882a593Smuzhiyun  * +----------------------------------------------------------------------------
162*4882a593Smuzhiyun  * Ring Context
163*4882a593Smuzhiyun  * +----------------------------------------------------------------------------
164*4882a593Smuzhiyun  */
165*4882a593Smuzhiyun typedef struct bcm_ring {     /* Ring context */
166*4882a593Smuzhiyun #if defined(BCM_RING_DEBUG)
167*4882a593Smuzhiyun 	struct bcm_ring *self;    /* ptr to self for IS VALID test */
168*4882a593Smuzhiyun #endif /* BCM_RING_DEBUG */
169*4882a593Smuzhiyun 	int write __ring_aligned; /* WRITE index in a circular ring */
170*4882a593Smuzhiyun 	int read  __ring_aligned; /* READ index in a circular ring */
171*4882a593Smuzhiyun } bcm_ring_t;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun static INLINE void bcm_ring_init(bcm_ring_t *ring);
174*4882a593Smuzhiyun static INLINE void bcm_ring_copy(bcm_ring_t *to, bcm_ring_t *from);
175*4882a593Smuzhiyun static INLINE bool bcm_ring_is_empty(bcm_ring_t *ring);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun static INLINE int  __bcm_ring_next_write(bcm_ring_t *ring, const int ring_size);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun static INLINE bool __bcm_ring_full(bcm_ring_t *ring, int next_write);
180*4882a593Smuzhiyun static INLINE bool bcm_ring_is_full(bcm_ring_t *ring, const int ring_size);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun static INLINE void bcm_ring_prod_done(bcm_ring_t *ring, int write);
183*4882a593Smuzhiyun static INLINE int  bcm_ring_prod_pend(bcm_ring_t *ring, int *pend_write,
184*4882a593Smuzhiyun                                       const int ring_size);
185*4882a593Smuzhiyun static INLINE int  bcm_ring_prod(bcm_ring_t *ring, const int ring_size);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun static INLINE void bcm_ring_cons_done(bcm_ring_t *ring, int read);
188*4882a593Smuzhiyun static INLINE int  bcm_ring_cons_pend(bcm_ring_t *ring, int *pend_read,
189*4882a593Smuzhiyun                                       const int ring_size);
190*4882a593Smuzhiyun static INLINE int  bcm_ring_cons(bcm_ring_t *ring, const int ring_size);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun static INLINE void bcm_ring_sync_read(bcm_ring_t *peer, const bcm_ring_t *self);
193*4882a593Smuzhiyun static INLINE void bcm_ring_sync_write(bcm_ring_t *peer, const bcm_ring_t *self);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun static INLINE int  bcm_ring_prod_avail(const bcm_ring_t *ring,
196*4882a593Smuzhiyun                                        const int ring_size);
197*4882a593Smuzhiyun static INLINE int  bcm_ring_cons_avail(const bcm_ring_t *ring,
198*4882a593Smuzhiyun                                        const int ring_size);
199*4882a593Smuzhiyun static INLINE void bcm_ring_cons_all(bcm_ring_t *ring);
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /**
202*4882a593Smuzhiyun  * bcm_ring_init - initialize a ring context.
203*4882a593Smuzhiyun  * @ring: pointer to a ring context
204*4882a593Smuzhiyun  */
205*4882a593Smuzhiyun static INLINE void
bcm_ring_init(bcm_ring_t * ring)206*4882a593Smuzhiyun bcm_ring_init(bcm_ring_t *ring)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	ASSERT(ring != (bcm_ring_t *)NULL);
209*4882a593Smuzhiyun #if defined(BCM_RING_DEBUG)
210*4882a593Smuzhiyun 	ring->self = ring;
211*4882a593Smuzhiyun #endif /* BCM_RING_DEBUG */
212*4882a593Smuzhiyun 	ring->write = 0;
213*4882a593Smuzhiyun 	ring->read = 0;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /**
217*4882a593Smuzhiyun  * bcm_ring_copy - copy construct a ring
218*4882a593Smuzhiyun  * @to: pointer to the new ring context
219*4882a593Smuzhiyun  * @from: pointer to orig ring context
220*4882a593Smuzhiyun  */
221*4882a593Smuzhiyun static INLINE void
bcm_ring_copy(bcm_ring_t * to,bcm_ring_t * from)222*4882a593Smuzhiyun bcm_ring_copy(bcm_ring_t *to, bcm_ring_t *from)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	bcm_ring_init(to);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	to->write = from->write;
227*4882a593Smuzhiyun 	to->read  = from->read;
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun /**
231*4882a593Smuzhiyun  * bcm_ring_is_empty - "Boolean" test whether ring is empty.
232*4882a593Smuzhiyun  * @ring: pointer to a ring context
233*4882a593Smuzhiyun  *
234*4882a593Smuzhiyun  * PS. does not return BCM_RING_EMPTY value.
235*4882a593Smuzhiyun  */
236*4882a593Smuzhiyun static INLINE bool
bcm_ring_is_empty(bcm_ring_t * ring)237*4882a593Smuzhiyun bcm_ring_is_empty(bcm_ring_t *ring)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring));
240*4882a593Smuzhiyun 	return (ring->read == ring->write);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun /**
244*4882a593Smuzhiyun  * __bcm_ring_next_write - determine the index where the next write may occur
245*4882a593Smuzhiyun  *                         (with wrap-around).
246*4882a593Smuzhiyun  * @ring: pointer to a ring context
247*4882a593Smuzhiyun  * @ring_size: size of the ring
248*4882a593Smuzhiyun  *
249*4882a593Smuzhiyun  * PRIVATE INTERNAL USE ONLY.
250*4882a593Smuzhiyun  */
251*4882a593Smuzhiyun static INLINE int
__bcm_ring_next_write(bcm_ring_t * ring,const int ring_size)252*4882a593Smuzhiyun __bcm_ring_next_write(bcm_ring_t *ring, const int ring_size)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
255*4882a593Smuzhiyun 	return ((ring->write + 1) % ring_size);
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun /**
259*4882a593Smuzhiyun  * __bcm_ring_full - support function for ring full test.
260*4882a593Smuzhiyun  * @ring: pointer to a ring context
261*4882a593Smuzhiyun  * @next_write: next location in ring where an element is to be produced
262*4882a593Smuzhiyun  *
263*4882a593Smuzhiyun  * PRIVATE INTERNAL USE ONLY.
264*4882a593Smuzhiyun  */
265*4882a593Smuzhiyun static INLINE bool
__bcm_ring_full(bcm_ring_t * ring,int next_write)266*4882a593Smuzhiyun __bcm_ring_full(bcm_ring_t *ring, int next_write)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	return (next_write == ring->read);
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun /**
272*4882a593Smuzhiyun  * bcm_ring_is_full - "Boolean" test whether a ring is full.
273*4882a593Smuzhiyun  * @ring: pointer to a ring context
274*4882a593Smuzhiyun  * @ring_size: size of the ring
275*4882a593Smuzhiyun  *
276*4882a593Smuzhiyun  * PS. does not return BCM_RING_FULL value.
277*4882a593Smuzhiyun  */
278*4882a593Smuzhiyun static INLINE bool
bcm_ring_is_full(bcm_ring_t * ring,const int ring_size)279*4882a593Smuzhiyun bcm_ring_is_full(bcm_ring_t *ring, const int ring_size)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun 	int next_write;
282*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
283*4882a593Smuzhiyun 	next_write = __bcm_ring_next_write(ring, ring_size);
284*4882a593Smuzhiyun 	return __bcm_ring_full(ring, next_write);
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /**
288*4882a593Smuzhiyun  * bcm_ring_prod_done - commit a previously pending index where production
289*4882a593Smuzhiyun  * was requested.
290*4882a593Smuzhiyun  * @ring: pointer to a ring context
291*4882a593Smuzhiyun  * @write: index into ring upto where production was done.
292*4882a593Smuzhiyun  * +----------------------------------------------------------------------------
293*4882a593Smuzhiyun  */
294*4882a593Smuzhiyun static INLINE void
bcm_ring_prod_done(bcm_ring_t * ring,int write)295*4882a593Smuzhiyun bcm_ring_prod_done(bcm_ring_t *ring, int write)
296*4882a593Smuzhiyun {
297*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring));
298*4882a593Smuzhiyun 	ring->write = write;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun  * bcm_ring_prod_pend - Fetch in "pend" mode, the index where an element may be
303*4882a593Smuzhiyun  * produced.
304*4882a593Smuzhiyun  * @ring: pointer to a ring context
305*4882a593Smuzhiyun  * @pend_write: next index, after the returned index
306*4882a593Smuzhiyun  * @ring_size: size of the ring
307*4882a593Smuzhiyun  */
308*4882a593Smuzhiyun static INLINE int
bcm_ring_prod_pend(bcm_ring_t * ring,int * pend_write,const int ring_size)309*4882a593Smuzhiyun bcm_ring_prod_pend(bcm_ring_t *ring, int *pend_write, const int ring_size)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	int rtn;
312*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
313*4882a593Smuzhiyun 	*pend_write = __bcm_ring_next_write(ring, ring_size);
314*4882a593Smuzhiyun 	if (__bcm_ring_full(ring, *pend_write)) {
315*4882a593Smuzhiyun 		*pend_write = BCM_RING_FULL;
316*4882a593Smuzhiyun 		rtn = BCM_RING_FULL;
317*4882a593Smuzhiyun 	} else {
318*4882a593Smuzhiyun 		/* production is not committed, caller needs to explicitly commit */
319*4882a593Smuzhiyun 		rtn = ring->write;
320*4882a593Smuzhiyun 	}
321*4882a593Smuzhiyun 	return rtn;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun /**
325*4882a593Smuzhiyun  * bcm_ring_prod - Fetch and "commit" the next index where a ring element may
326*4882a593Smuzhiyun  * be produced.
327*4882a593Smuzhiyun  * @ring: pointer to a ring context
328*4882a593Smuzhiyun  * @ring_size: size of the ring
329*4882a593Smuzhiyun  */
330*4882a593Smuzhiyun static INLINE int
bcm_ring_prod(bcm_ring_t * ring,const int ring_size)331*4882a593Smuzhiyun bcm_ring_prod(bcm_ring_t *ring, const int ring_size)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	int next_write, prod_write;
334*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	next_write = __bcm_ring_next_write(ring, ring_size);
337*4882a593Smuzhiyun 	if (__bcm_ring_full(ring, next_write)) {
338*4882a593Smuzhiyun 		prod_write = BCM_RING_FULL;
339*4882a593Smuzhiyun 	} else {
340*4882a593Smuzhiyun 		prod_write = ring->write;
341*4882a593Smuzhiyun 		bcm_ring_prod_done(ring, next_write); /* "commit" production */
342*4882a593Smuzhiyun 	}
343*4882a593Smuzhiyun 	return prod_write;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun /**
347*4882a593Smuzhiyun  * bcm_ring_cons_done - commit a previously pending read
348*4882a593Smuzhiyun  * @ring: pointer to a ring context
349*4882a593Smuzhiyun  * @read: index upto which elements have been consumed.
350*4882a593Smuzhiyun  */
351*4882a593Smuzhiyun static INLINE void
bcm_ring_cons_done(bcm_ring_t * ring,int read)352*4882a593Smuzhiyun bcm_ring_cons_done(bcm_ring_t *ring, int read)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring));
355*4882a593Smuzhiyun 	ring->read = read;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /**
359*4882a593Smuzhiyun  * bcm_ring_cons_pend - fetch in "pend" mode, the next index where a ring
360*4882a593Smuzhiyun  * element may be consumed.
361*4882a593Smuzhiyun  * @ring: pointer to a ring context
362*4882a593Smuzhiyun  * @pend_read: index into ring upto which elements may be consumed.
363*4882a593Smuzhiyun  * @ring_size: size of the ring
364*4882a593Smuzhiyun  */
365*4882a593Smuzhiyun static INLINE int
bcm_ring_cons_pend(bcm_ring_t * ring,int * pend_read,const int ring_size)366*4882a593Smuzhiyun bcm_ring_cons_pend(bcm_ring_t *ring, int *pend_read, const int ring_size)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	int rtn;
369*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
370*4882a593Smuzhiyun 	if (bcm_ring_is_empty(ring)) {
371*4882a593Smuzhiyun 		*pend_read = BCM_RING_EMPTY;
372*4882a593Smuzhiyun 		rtn = BCM_RING_EMPTY;
373*4882a593Smuzhiyun 	} else {
374*4882a593Smuzhiyun 		*pend_read = (ring->read + 1) % ring_size;
375*4882a593Smuzhiyun 		/* production is not committed, caller needs to explicitly commit */
376*4882a593Smuzhiyun 		rtn = ring->read;
377*4882a593Smuzhiyun 	}
378*4882a593Smuzhiyun 	return rtn;
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun /**
382*4882a593Smuzhiyun  * bcm_ring_cons - fetch and "commit" the next index where a ring element may
383*4882a593Smuzhiyun  * be consumed.
384*4882a593Smuzhiyun  * @ring: pointer to a ring context
385*4882a593Smuzhiyun  * @ring_size: size of the ring
386*4882a593Smuzhiyun  */
387*4882a593Smuzhiyun static INLINE int
bcm_ring_cons(bcm_ring_t * ring,const int ring_size)388*4882a593Smuzhiyun bcm_ring_cons(bcm_ring_t *ring, const int ring_size)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	int cons_read;
391*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
392*4882a593Smuzhiyun 	if (bcm_ring_is_empty(ring)) {
393*4882a593Smuzhiyun 		cons_read = BCM_RING_EMPTY;
394*4882a593Smuzhiyun 	} else {
395*4882a593Smuzhiyun 		cons_read = ring->read;
396*4882a593Smuzhiyun 		ring->read = (ring->read + 1) % ring_size; /* read is committed */
397*4882a593Smuzhiyun 	}
398*4882a593Smuzhiyun 	return cons_read;
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun /**
402*4882a593Smuzhiyun  * bcm_ring_sync_read - on consumption, update peer's read index.
403*4882a593Smuzhiyun  * @peer: pointer to peer's producer ring context
404*4882a593Smuzhiyun  * @self: pointer to consumer's ring context
405*4882a593Smuzhiyun  */
406*4882a593Smuzhiyun static INLINE void
bcm_ring_sync_read(bcm_ring_t * peer,const bcm_ring_t * self)407*4882a593Smuzhiyun bcm_ring_sync_read(bcm_ring_t *peer, const bcm_ring_t *self)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(peer));
410*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(self));
411*4882a593Smuzhiyun 	peer->read = self->read; /* flush read update to peer producer */
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun /**
415*4882a593Smuzhiyun  * bcm_ring_sync_write - on consumption, update peer's write index.
416*4882a593Smuzhiyun  * @peer: pointer to peer's consumer ring context
417*4882a593Smuzhiyun  * @self: pointer to producer's ring context
418*4882a593Smuzhiyun  */
419*4882a593Smuzhiyun static INLINE void
bcm_ring_sync_write(bcm_ring_t * peer,const bcm_ring_t * self)420*4882a593Smuzhiyun bcm_ring_sync_write(bcm_ring_t *peer, const bcm_ring_t *self)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(peer));
423*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(self));
424*4882a593Smuzhiyun 	peer->write = self->write; /* flush write update to peer consumer */
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun /**
428*4882a593Smuzhiyun  * bcm_ring_prod_avail - fetch total number of available empty slots in the
429*4882a593Smuzhiyun  * ring for production.
430*4882a593Smuzhiyun  * @ring: pointer to a ring context
431*4882a593Smuzhiyun  * @ring_size: size of the ring
432*4882a593Smuzhiyun  */
433*4882a593Smuzhiyun static INLINE int
bcm_ring_prod_avail(const bcm_ring_t * ring,const int ring_size)434*4882a593Smuzhiyun bcm_ring_prod_avail(const bcm_ring_t *ring, const int ring_size)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun 	int prod_avail;
437*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
438*4882a593Smuzhiyun 	if (ring->write >= ring->read) {
439*4882a593Smuzhiyun 		prod_avail = (ring_size - (ring->write - ring->read) - 1);
440*4882a593Smuzhiyun 	} else {
441*4882a593Smuzhiyun 		prod_avail = (ring->read - (ring->write + 1));
442*4882a593Smuzhiyun 	}
443*4882a593Smuzhiyun 	ASSERT(prod_avail < ring_size);
444*4882a593Smuzhiyun 	return prod_avail;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun /**
448*4882a593Smuzhiyun  * bcm_ring_cons_avail - fetch total number of available elements for consumption.
449*4882a593Smuzhiyun  * @ring: pointer to a ring context
450*4882a593Smuzhiyun  * @ring_size: size of the ring
451*4882a593Smuzhiyun  */
452*4882a593Smuzhiyun static INLINE int
bcm_ring_cons_avail(const bcm_ring_t * ring,const int ring_size)453*4882a593Smuzhiyun bcm_ring_cons_avail(const bcm_ring_t *ring, const int ring_size)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun 	int cons_avail;
456*4882a593Smuzhiyun 	RING_ASSERT(BCM_RING_IS_VALID(ring) && BCM_RING_SIZE_IS_VALID(ring_size));
457*4882a593Smuzhiyun 	if (ring->read == ring->write) {
458*4882a593Smuzhiyun 		cons_avail = 0;
459*4882a593Smuzhiyun 	} else if (ring->read > ring->write) {
460*4882a593Smuzhiyun 		cons_avail = ((ring_size - ring->read) + ring->write);
461*4882a593Smuzhiyun 	} else {
462*4882a593Smuzhiyun 		cons_avail = ring->write - ring->read;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 	ASSERT(cons_avail < ring_size);
465*4882a593Smuzhiyun 	return cons_avail;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun /**
469*4882a593Smuzhiyun  * bcm_ring_cons_all - set ring in state where all elements are consumed.
470*4882a593Smuzhiyun  * @ring: pointer to a ring context
471*4882a593Smuzhiyun  */
472*4882a593Smuzhiyun static INLINE void
bcm_ring_cons_all(bcm_ring_t * ring)473*4882a593Smuzhiyun bcm_ring_cons_all(bcm_ring_t *ring)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun 	ring->read = ring->write;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun /**
479*4882a593Smuzhiyun  * Work Queue
480*4882a593Smuzhiyun  * A work Queue is composed of a ring of work items, of a specified depth.
481*4882a593Smuzhiyun  * It HAS-A bcm_ring object, comprising of a RD and WR offset, to implement a
482*4882a593Smuzhiyun  * producer/consumer circular ring.
483*4882a593Smuzhiyun  */
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun struct bcm_workq {
486*4882a593Smuzhiyun 	bcm_ring_t ring;        /* Ring context abstraction */
487*4882a593Smuzhiyun 	struct bcm_workq *peer; /* Peer workq context */
488*4882a593Smuzhiyun 	void       *buffer;     /* Buffer storage for work items in workQ */
489*4882a593Smuzhiyun 	int        ring_size;   /* Depth of workQ */
490*4882a593Smuzhiyun } __ring_aligned;
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun typedef struct bcm_workq bcm_workq_t;
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun /* #define BCM_WORKQ_DEBUG */
495*4882a593Smuzhiyun #if defined(BCM_WORKQ_DEBUG)
496*4882a593Smuzhiyun #define WORKQ_ASSERT(exp)               ASSERT(exp)
497*4882a593Smuzhiyun #else  /* ! BCM_WORKQ_DEBUG */
498*4882a593Smuzhiyun #define WORKQ_ASSERT(exp)               do {} while (0)
499*4882a593Smuzhiyun #endif /* ! BCM_WORKQ_DEBUG */
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun #define WORKQ_AUDIT(workq) \
502*4882a593Smuzhiyun 	WORKQ_ASSERT((workq) != BCM_WORKQ_NULL); \
503*4882a593Smuzhiyun 	WORKQ_ASSERT(WORKQ_PEER(workq) != BCM_WORKQ_NULL); \
504*4882a593Smuzhiyun 	WORKQ_ASSERT((workq)->buffer == WORKQ_PEER(workq)->buffer); \
505*4882a593Smuzhiyun 	WORKQ_ASSERT((workq)->ring_size == WORKQ_PEER(workq)->ring_size);
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun #define BCM_WORKQ_NULL                  ((bcm_workq_t *)NULL)
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun #define WORKQ_PEER(workq)               ((workq)->peer)
510*4882a593Smuzhiyun #define WORKQ_RING(workq)               (&((workq)->ring))
511*4882a593Smuzhiyun #define WORKQ_PEER_RING(workq)          (&((workq)->peer->ring))
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun #define WORKQ_ELEMENT(__elem_type, __workq, __index) ({ \
514*4882a593Smuzhiyun 	WORKQ_ASSERT((__workq) != BCM_WORKQ_NULL); \
515*4882a593Smuzhiyun 	WORKQ_ASSERT((__index) < ((__workq)->ring_size)); \
516*4882a593Smuzhiyun 	((__elem_type *)((__workq)->buffer)) + (__index); \
517*4882a593Smuzhiyun })
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun static INLINE void bcm_workq_init(bcm_workq_t *workq, bcm_workq_t *workq_peer,
520*4882a593Smuzhiyun                                   void *buffer, int ring_size);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun static INLINE bool bcm_workq_is_empty(bcm_workq_t *workq_prod);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun static INLINE void bcm_workq_prod_sync(bcm_workq_t *workq_prod);
525*4882a593Smuzhiyun static INLINE void bcm_workq_cons_sync(bcm_workq_t *workq_cons);
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun static INLINE void bcm_workq_prod_refresh(bcm_workq_t *workq_prod);
528*4882a593Smuzhiyun static INLINE void bcm_workq_cons_refresh(bcm_workq_t *workq_cons);
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun /**
531*4882a593Smuzhiyun  * bcm_workq_init - initialize a workq
532*4882a593Smuzhiyun  * @workq: pointer to a workq context
533*4882a593Smuzhiyun  * @buffer: pointer to a pre-allocated circular buffer to serve as a ring
534*4882a593Smuzhiyun  * @ring_size: size of the ring in terms of max number of elements.
535*4882a593Smuzhiyun  */
536*4882a593Smuzhiyun static INLINE void
bcm_workq_init(bcm_workq_t * workq,bcm_workq_t * workq_peer,void * buffer,int ring_size)537*4882a593Smuzhiyun bcm_workq_init(bcm_workq_t *workq, bcm_workq_t *workq_peer,
538*4882a593Smuzhiyun                void *buffer, int ring_size)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun 	ASSERT(workq != BCM_WORKQ_NULL);
541*4882a593Smuzhiyun 	ASSERT(workq_peer != BCM_WORKQ_NULL);
542*4882a593Smuzhiyun 	ASSERT(buffer != NULL);
543*4882a593Smuzhiyun 	ASSERT(ring_size > 0);
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	WORKQ_PEER(workq) = workq_peer;
546*4882a593Smuzhiyun 	WORKQ_PEER(workq_peer) = workq;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	bcm_ring_init(WORKQ_RING(workq));
549*4882a593Smuzhiyun 	bcm_ring_init(WORKQ_RING(workq_peer));
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	workq->buffer = workq_peer->buffer = buffer;
552*4882a593Smuzhiyun 	workq->ring_size = workq_peer->ring_size = ring_size;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun /**
556*4882a593Smuzhiyun  * bcm_workq_empty - test whether there is work
557*4882a593Smuzhiyun  * @workq_prod: producer's workq
558*4882a593Smuzhiyun  */
559*4882a593Smuzhiyun static INLINE bool
bcm_workq_is_empty(bcm_workq_t * workq_prod)560*4882a593Smuzhiyun bcm_workq_is_empty(bcm_workq_t *workq_prod)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	return bcm_ring_is_empty(WORKQ_RING(workq_prod));
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun /**
566*4882a593Smuzhiyun  * bcm_workq_prod_sync - Commit the producer write index to peer workq's ring
567*4882a593Smuzhiyun  * @workq_prod: producer's workq whose write index must be synced to peer
568*4882a593Smuzhiyun  */
569*4882a593Smuzhiyun static INLINE void
bcm_workq_prod_sync(bcm_workq_t * workq_prod)570*4882a593Smuzhiyun bcm_workq_prod_sync(bcm_workq_t *workq_prod)
571*4882a593Smuzhiyun {
572*4882a593Smuzhiyun 	WORKQ_AUDIT(workq_prod);
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	/* cons::write <--- prod::write */
575*4882a593Smuzhiyun 	bcm_ring_sync_write(WORKQ_PEER_RING(workq_prod), WORKQ_RING(workq_prod));
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun /**
579*4882a593Smuzhiyun  * bcm_workq_cons_sync - Commit the consumer read index to the peer workq's ring
580*4882a593Smuzhiyun  * @workq_cons: consumer's workq whose read index must be synced to peer
581*4882a593Smuzhiyun  */
582*4882a593Smuzhiyun static INLINE void
bcm_workq_cons_sync(bcm_workq_t * workq_cons)583*4882a593Smuzhiyun bcm_workq_cons_sync(bcm_workq_t *workq_cons)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun 	WORKQ_AUDIT(workq_cons);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	/* prod::read <--- cons::read */
588*4882a593Smuzhiyun 	bcm_ring_sync_read(WORKQ_PEER_RING(workq_cons), WORKQ_RING(workq_cons));
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun /**
592*4882a593Smuzhiyun  * bcm_workq_prod_refresh - Fetch the updated consumer's read index
593*4882a593Smuzhiyun  * @workq_prod: producer's workq whose read index must be refreshed from peer
594*4882a593Smuzhiyun  */
595*4882a593Smuzhiyun static INLINE void
bcm_workq_prod_refresh(bcm_workq_t * workq_prod)596*4882a593Smuzhiyun bcm_workq_prod_refresh(bcm_workq_t *workq_prod)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun 	WORKQ_AUDIT(workq_prod);
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	/* prod::read <--- cons::read */
601*4882a593Smuzhiyun 	bcm_ring_sync_read(WORKQ_RING(workq_prod), WORKQ_PEER_RING(workq_prod));
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun /**
605*4882a593Smuzhiyun  * bcm_workq_cons_refresh - Fetch the updated producer's write index
606*4882a593Smuzhiyun  * @workq_cons: consumer's workq whose write index must be refreshed from peer
607*4882a593Smuzhiyun  */
608*4882a593Smuzhiyun static INLINE void
bcm_workq_cons_refresh(bcm_workq_t * workq_cons)609*4882a593Smuzhiyun bcm_workq_cons_refresh(bcm_workq_t *workq_cons)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	WORKQ_AUDIT(workq_cons);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	/* cons::write <--- prod::write */
614*4882a593Smuzhiyun 	bcm_ring_sync_write(WORKQ_RING(workq_cons), WORKQ_PEER_RING(workq_cons));
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun #endif /* ! __bcm_ring_h_included__ */
618