xref: /freebsd/sys/contrib/dev/broadcom/brcm80211/brcmfmac/commonring.c (revision 902136e0fe112383ec64d2ef43a446063b5e6417)
1b4c3e9b5SBjoern A. Zeeb // SPDX-License-Identifier: ISC
2b4c3e9b5SBjoern A. Zeeb /*
3b4c3e9b5SBjoern A. Zeeb  * Copyright (c) 2014 Broadcom Corporation
4b4c3e9b5SBjoern A. Zeeb  */
5b4c3e9b5SBjoern A. Zeeb 
6b4c3e9b5SBjoern A. Zeeb #include <linux/types.h>
7b4c3e9b5SBjoern A. Zeeb #include <linux/netdevice.h>
8b4c3e9b5SBjoern A. Zeeb 
9b4c3e9b5SBjoern A. Zeeb #include <brcmu_utils.h>
10b4c3e9b5SBjoern A. Zeeb #include <brcmu_wifi.h>
11b4c3e9b5SBjoern A. Zeeb 
12b4c3e9b5SBjoern A. Zeeb #include "core.h"
13b4c3e9b5SBjoern A. Zeeb #include "commonring.h"
14b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_register_cb(struct brcmf_commonring * commonring,int (* cr_ring_bell)(void * ctx),int (* cr_update_rptr)(void * ctx),int (* cr_update_wptr)(void * ctx),int (* cr_write_rptr)(void * ctx),int (* cr_write_wptr)(void * ctx),void * ctx)15b4c3e9b5SBjoern A. Zeeb void brcmf_commonring_register_cb(struct brcmf_commonring *commonring,
16b4c3e9b5SBjoern A. Zeeb 				  int (*cr_ring_bell)(void *ctx),
17b4c3e9b5SBjoern A. Zeeb 				  int (*cr_update_rptr)(void *ctx),
18b4c3e9b5SBjoern A. Zeeb 				  int (*cr_update_wptr)(void *ctx),
19b4c3e9b5SBjoern A. Zeeb 				  int (*cr_write_rptr)(void *ctx),
20b4c3e9b5SBjoern A. Zeeb 				  int (*cr_write_wptr)(void *ctx), void *ctx)
21b4c3e9b5SBjoern A. Zeeb {
22b4c3e9b5SBjoern A. Zeeb 	commonring->cr_ring_bell = cr_ring_bell;
23b4c3e9b5SBjoern A. Zeeb 	commonring->cr_update_rptr = cr_update_rptr;
24b4c3e9b5SBjoern A. Zeeb 	commonring->cr_update_wptr = cr_update_wptr;
25b4c3e9b5SBjoern A. Zeeb 	commonring->cr_write_rptr = cr_write_rptr;
26b4c3e9b5SBjoern A. Zeeb 	commonring->cr_write_wptr = cr_write_wptr;
27b4c3e9b5SBjoern A. Zeeb 	commonring->cr_ctx = ctx;
28b4c3e9b5SBjoern A. Zeeb }
29b4c3e9b5SBjoern A. Zeeb 
30b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_config(struct brcmf_commonring * commonring,u16 depth,u16 item_len,void * buf_addr)31b4c3e9b5SBjoern A. Zeeb void brcmf_commonring_config(struct brcmf_commonring *commonring, u16 depth,
32b4c3e9b5SBjoern A. Zeeb 			     u16 item_len, void *buf_addr)
33b4c3e9b5SBjoern A. Zeeb {
34b4c3e9b5SBjoern A. Zeeb 	commonring->depth = depth;
35b4c3e9b5SBjoern A. Zeeb 	commonring->item_len = item_len;
36b4c3e9b5SBjoern A. Zeeb 	commonring->buf_addr = buf_addr;
37b4c3e9b5SBjoern A. Zeeb 	if (!commonring->inited) {
38b4c3e9b5SBjoern A. Zeeb 		spin_lock_init(&commonring->lock);
39b4c3e9b5SBjoern A. Zeeb 		commonring->inited = true;
40b4c3e9b5SBjoern A. Zeeb 	}
41b4c3e9b5SBjoern A. Zeeb 	commonring->r_ptr = 0;
42b4c3e9b5SBjoern A. Zeeb 	if (commonring->cr_write_rptr)
43b4c3e9b5SBjoern A. Zeeb 		commonring->cr_write_rptr(commonring->cr_ctx);
44b4c3e9b5SBjoern A. Zeeb 	commonring->w_ptr = 0;
45b4c3e9b5SBjoern A. Zeeb 	if (commonring->cr_write_wptr)
46b4c3e9b5SBjoern A. Zeeb 		commonring->cr_write_wptr(commonring->cr_ctx);
47b4c3e9b5SBjoern A. Zeeb 	commonring->f_ptr = 0;
48b4c3e9b5SBjoern A. Zeeb }
49b4c3e9b5SBjoern A. Zeeb 
50b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_lock(struct brcmf_commonring * commonring)51b4c3e9b5SBjoern A. Zeeb void brcmf_commonring_lock(struct brcmf_commonring *commonring)
52b4c3e9b5SBjoern A. Zeeb 		__acquires(&commonring->lock)
53b4c3e9b5SBjoern A. Zeeb {
54b4c3e9b5SBjoern A. Zeeb 	unsigned long flags;
55b4c3e9b5SBjoern A. Zeeb 
56b4c3e9b5SBjoern A. Zeeb 	spin_lock_irqsave(&commonring->lock, flags);
57b4c3e9b5SBjoern A. Zeeb 	commonring->flags = flags;
58b4c3e9b5SBjoern A. Zeeb }
59b4c3e9b5SBjoern A. Zeeb 
60b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_unlock(struct brcmf_commonring * commonring)61b4c3e9b5SBjoern A. Zeeb void brcmf_commonring_unlock(struct brcmf_commonring *commonring)
62b4c3e9b5SBjoern A. Zeeb 		__releases(&commonring->lock)
63b4c3e9b5SBjoern A. Zeeb {
64b4c3e9b5SBjoern A. Zeeb 	spin_unlock_irqrestore(&commonring->lock, commonring->flags);
65b4c3e9b5SBjoern A. Zeeb }
66b4c3e9b5SBjoern A. Zeeb 
67b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_write_available(struct brcmf_commonring * commonring)68b4c3e9b5SBjoern A. Zeeb bool brcmf_commonring_write_available(struct brcmf_commonring *commonring)
69b4c3e9b5SBjoern A. Zeeb {
70b4c3e9b5SBjoern A. Zeeb 	u16 available;
71b4c3e9b5SBjoern A. Zeeb 	bool retry = true;
72b4c3e9b5SBjoern A. Zeeb 
73b4c3e9b5SBjoern A. Zeeb again:
74b4c3e9b5SBjoern A. Zeeb 	if (commonring->r_ptr <= commonring->w_ptr)
75b4c3e9b5SBjoern A. Zeeb 		available = commonring->depth - commonring->w_ptr +
76b4c3e9b5SBjoern A. Zeeb 			    commonring->r_ptr;
77b4c3e9b5SBjoern A. Zeeb 	else
78b4c3e9b5SBjoern A. Zeeb 		available = commonring->r_ptr - commonring->w_ptr;
79b4c3e9b5SBjoern A. Zeeb 
80b4c3e9b5SBjoern A. Zeeb 	if (available > 1) {
81b4c3e9b5SBjoern A. Zeeb 		if (!commonring->was_full)
82b4c3e9b5SBjoern A. Zeeb 			return true;
83b4c3e9b5SBjoern A. Zeeb 		if (available > commonring->depth / 8) {
84b4c3e9b5SBjoern A. Zeeb 			commonring->was_full = false;
85b4c3e9b5SBjoern A. Zeeb 			return true;
86b4c3e9b5SBjoern A. Zeeb 		}
87b4c3e9b5SBjoern A. Zeeb 		if (retry) {
88b4c3e9b5SBjoern A. Zeeb 			if (commonring->cr_update_rptr)
89b4c3e9b5SBjoern A. Zeeb 				commonring->cr_update_rptr(commonring->cr_ctx);
90b4c3e9b5SBjoern A. Zeeb 			retry = false;
91b4c3e9b5SBjoern A. Zeeb 			goto again;
92b4c3e9b5SBjoern A. Zeeb 		}
93b4c3e9b5SBjoern A. Zeeb 		return false;
94b4c3e9b5SBjoern A. Zeeb 	}
95b4c3e9b5SBjoern A. Zeeb 
96b4c3e9b5SBjoern A. Zeeb 	if (retry) {
97b4c3e9b5SBjoern A. Zeeb 		if (commonring->cr_update_rptr)
98b4c3e9b5SBjoern A. Zeeb 			commonring->cr_update_rptr(commonring->cr_ctx);
99b4c3e9b5SBjoern A. Zeeb 		retry = false;
100b4c3e9b5SBjoern A. Zeeb 		goto again;
101b4c3e9b5SBjoern A. Zeeb 	}
102b4c3e9b5SBjoern A. Zeeb 
103b4c3e9b5SBjoern A. Zeeb 	commonring->was_full = true;
104b4c3e9b5SBjoern A. Zeeb 	return false;
105b4c3e9b5SBjoern A. Zeeb }
106b4c3e9b5SBjoern A. Zeeb 
107b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_reserve_for_write(struct brcmf_commonring * commonring)108b4c3e9b5SBjoern A. Zeeb void *brcmf_commonring_reserve_for_write(struct brcmf_commonring *commonring)
109b4c3e9b5SBjoern A. Zeeb {
110b4c3e9b5SBjoern A. Zeeb 	void *ret_ptr;
111b4c3e9b5SBjoern A. Zeeb 	u16 available;
112b4c3e9b5SBjoern A. Zeeb 	bool retry = true;
113b4c3e9b5SBjoern A. Zeeb 
114b4c3e9b5SBjoern A. Zeeb again:
115b4c3e9b5SBjoern A. Zeeb 	if (commonring->r_ptr <= commonring->w_ptr)
116b4c3e9b5SBjoern A. Zeeb 		available = commonring->depth - commonring->w_ptr +
117b4c3e9b5SBjoern A. Zeeb 			    commonring->r_ptr;
118b4c3e9b5SBjoern A. Zeeb 	else
119b4c3e9b5SBjoern A. Zeeb 		available = commonring->r_ptr - commonring->w_ptr;
120b4c3e9b5SBjoern A. Zeeb 
121b4c3e9b5SBjoern A. Zeeb 	if (available > 1) {
122*902136e0SBjoern A. Zeeb #if defined(__linux__)
123b4c3e9b5SBjoern A. Zeeb 		ret_ptr = commonring->buf_addr +
124b4c3e9b5SBjoern A. Zeeb 			  (commonring->w_ptr * commonring->item_len);
125*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
126*902136e0SBjoern A. Zeeb 		ret_ptr = (void *)((uintptr_t)commonring->buf_addr +
127*902136e0SBjoern A. Zeeb 			  (commonring->w_ptr * commonring->item_len));
128*902136e0SBjoern A. Zeeb #endif
129b4c3e9b5SBjoern A. Zeeb 		commonring->w_ptr++;
130b4c3e9b5SBjoern A. Zeeb 		if (commonring->w_ptr == commonring->depth)
131b4c3e9b5SBjoern A. Zeeb 			commonring->w_ptr = 0;
132b4c3e9b5SBjoern A. Zeeb 		return ret_ptr;
133b4c3e9b5SBjoern A. Zeeb 	}
134b4c3e9b5SBjoern A. Zeeb 
135b4c3e9b5SBjoern A. Zeeb 	if (retry) {
136b4c3e9b5SBjoern A. Zeeb 		if (commonring->cr_update_rptr)
137b4c3e9b5SBjoern A. Zeeb 			commonring->cr_update_rptr(commonring->cr_ctx);
138b4c3e9b5SBjoern A. Zeeb 		retry = false;
139b4c3e9b5SBjoern A. Zeeb 		goto again;
140b4c3e9b5SBjoern A. Zeeb 	}
141b4c3e9b5SBjoern A. Zeeb 
142b4c3e9b5SBjoern A. Zeeb 	commonring->was_full = true;
143b4c3e9b5SBjoern A. Zeeb 	return NULL;
144b4c3e9b5SBjoern A. Zeeb }
145b4c3e9b5SBjoern A. Zeeb 
146b4c3e9b5SBjoern A. Zeeb 
147b4c3e9b5SBjoern A. Zeeb void *
brcmf_commonring_reserve_for_write_multiple(struct brcmf_commonring * commonring,u16 n_items,u16 * alloced)148b4c3e9b5SBjoern A. Zeeb brcmf_commonring_reserve_for_write_multiple(struct brcmf_commonring *commonring,
149b4c3e9b5SBjoern A. Zeeb 					    u16 n_items, u16 *alloced)
150b4c3e9b5SBjoern A. Zeeb {
151b4c3e9b5SBjoern A. Zeeb 	void *ret_ptr;
152b4c3e9b5SBjoern A. Zeeb 	u16 available;
153b4c3e9b5SBjoern A. Zeeb 	bool retry = true;
154b4c3e9b5SBjoern A. Zeeb 
155b4c3e9b5SBjoern A. Zeeb again:
156b4c3e9b5SBjoern A. Zeeb 	if (commonring->r_ptr <= commonring->w_ptr)
157b4c3e9b5SBjoern A. Zeeb 		available = commonring->depth - commonring->w_ptr +
158b4c3e9b5SBjoern A. Zeeb 			    commonring->r_ptr;
159b4c3e9b5SBjoern A. Zeeb 	else
160b4c3e9b5SBjoern A. Zeeb 		available = commonring->r_ptr - commonring->w_ptr;
161b4c3e9b5SBjoern A. Zeeb 
162b4c3e9b5SBjoern A. Zeeb 	if (available > 1) {
163*902136e0SBjoern A. Zeeb #if defined(__linux__)
164b4c3e9b5SBjoern A. Zeeb 		ret_ptr = commonring->buf_addr +
165b4c3e9b5SBjoern A. Zeeb 			  (commonring->w_ptr * commonring->item_len);
166*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
167*902136e0SBjoern A. Zeeb 		ret_ptr = (void *)((uintptr_t)commonring->buf_addr +
168*902136e0SBjoern A. Zeeb 			  (commonring->w_ptr * commonring->item_len));
169*902136e0SBjoern A. Zeeb #endif
170b4c3e9b5SBjoern A. Zeeb 		*alloced = min_t(u16, n_items, available - 1);
171b4c3e9b5SBjoern A. Zeeb 		if (*alloced + commonring->w_ptr > commonring->depth)
172b4c3e9b5SBjoern A. Zeeb 			*alloced = commonring->depth - commonring->w_ptr;
173b4c3e9b5SBjoern A. Zeeb 		commonring->w_ptr += *alloced;
174b4c3e9b5SBjoern A. Zeeb 		if (commonring->w_ptr == commonring->depth)
175b4c3e9b5SBjoern A. Zeeb 			commonring->w_ptr = 0;
176b4c3e9b5SBjoern A. Zeeb 		return ret_ptr;
177b4c3e9b5SBjoern A. Zeeb 	}
178b4c3e9b5SBjoern A. Zeeb 
179b4c3e9b5SBjoern A. Zeeb 	if (retry) {
180b4c3e9b5SBjoern A. Zeeb 		if (commonring->cr_update_rptr)
181b4c3e9b5SBjoern A. Zeeb 			commonring->cr_update_rptr(commonring->cr_ctx);
182b4c3e9b5SBjoern A. Zeeb 		retry = false;
183b4c3e9b5SBjoern A. Zeeb 		goto again;
184b4c3e9b5SBjoern A. Zeeb 	}
185b4c3e9b5SBjoern A. Zeeb 
186b4c3e9b5SBjoern A. Zeeb 	commonring->was_full = true;
187b4c3e9b5SBjoern A. Zeeb 	return NULL;
188b4c3e9b5SBjoern A. Zeeb }
189b4c3e9b5SBjoern A. Zeeb 
190b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_write_complete(struct brcmf_commonring * commonring)191b4c3e9b5SBjoern A. Zeeb int brcmf_commonring_write_complete(struct brcmf_commonring *commonring)
192b4c3e9b5SBjoern A. Zeeb {
193b4c3e9b5SBjoern A. Zeeb 	if (commonring->f_ptr > commonring->w_ptr)
194b4c3e9b5SBjoern A. Zeeb 		commonring->f_ptr = 0;
195b4c3e9b5SBjoern A. Zeeb 
196b4c3e9b5SBjoern A. Zeeb 	commonring->f_ptr = commonring->w_ptr;
197b4c3e9b5SBjoern A. Zeeb 
198b4c3e9b5SBjoern A. Zeeb 	if (commonring->cr_write_wptr)
199b4c3e9b5SBjoern A. Zeeb 		commonring->cr_write_wptr(commonring->cr_ctx);
200b4c3e9b5SBjoern A. Zeeb 	if (commonring->cr_ring_bell)
201b4c3e9b5SBjoern A. Zeeb 		return commonring->cr_ring_bell(commonring->cr_ctx);
202b4c3e9b5SBjoern A. Zeeb 
203b4c3e9b5SBjoern A. Zeeb 	return -EIO;
204b4c3e9b5SBjoern A. Zeeb }
205b4c3e9b5SBjoern A. Zeeb 
206b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_write_cancel(struct brcmf_commonring * commonring,u16 n_items)207b4c3e9b5SBjoern A. Zeeb void brcmf_commonring_write_cancel(struct brcmf_commonring *commonring,
208b4c3e9b5SBjoern A. Zeeb 				   u16 n_items)
209b4c3e9b5SBjoern A. Zeeb {
210b4c3e9b5SBjoern A. Zeeb 	if (commonring->w_ptr == 0)
211b4c3e9b5SBjoern A. Zeeb 		commonring->w_ptr = commonring->depth - n_items;
212b4c3e9b5SBjoern A. Zeeb 	else
213b4c3e9b5SBjoern A. Zeeb 		commonring->w_ptr -= n_items;
214b4c3e9b5SBjoern A. Zeeb }
215b4c3e9b5SBjoern A. Zeeb 
216b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_get_read_ptr(struct brcmf_commonring * commonring,u16 * n_items)217b4c3e9b5SBjoern A. Zeeb void *brcmf_commonring_get_read_ptr(struct brcmf_commonring *commonring,
218b4c3e9b5SBjoern A. Zeeb 				    u16 *n_items)
219b4c3e9b5SBjoern A. Zeeb {
220b4c3e9b5SBjoern A. Zeeb 	if (commonring->cr_update_wptr)
221b4c3e9b5SBjoern A. Zeeb 		commonring->cr_update_wptr(commonring->cr_ctx);
222b4c3e9b5SBjoern A. Zeeb 
223b4c3e9b5SBjoern A. Zeeb 	*n_items = (commonring->w_ptr >= commonring->r_ptr) ?
224b4c3e9b5SBjoern A. Zeeb 				(commonring->w_ptr - commonring->r_ptr) :
225b4c3e9b5SBjoern A. Zeeb 				(commonring->depth - commonring->r_ptr);
226b4c3e9b5SBjoern A. Zeeb 
227b4c3e9b5SBjoern A. Zeeb 	if (*n_items == 0)
228b4c3e9b5SBjoern A. Zeeb 		return NULL;
229b4c3e9b5SBjoern A. Zeeb 
230*902136e0SBjoern A. Zeeb #if defined(__linux__)
231b4c3e9b5SBjoern A. Zeeb 	return commonring->buf_addr +
232b4c3e9b5SBjoern A. Zeeb 	       (commonring->r_ptr * commonring->item_len);
233*902136e0SBjoern A. Zeeb #elif defined(__FreeBSD__)
234*902136e0SBjoern A. Zeeb 	return (void *)((uintptr_t)commonring->buf_addr +
235*902136e0SBjoern A. Zeeb 	       (commonring->r_ptr * commonring->item_len));
236*902136e0SBjoern A. Zeeb #endif
237b4c3e9b5SBjoern A. Zeeb }
238b4c3e9b5SBjoern A. Zeeb 
239b4c3e9b5SBjoern A. Zeeb 
brcmf_commonring_read_complete(struct brcmf_commonring * commonring,u16 n_items)240b4c3e9b5SBjoern A. Zeeb int brcmf_commonring_read_complete(struct brcmf_commonring *commonring,
241b4c3e9b5SBjoern A. Zeeb 				   u16 n_items)
242b4c3e9b5SBjoern A. Zeeb {
243b4c3e9b5SBjoern A. Zeeb 	commonring->r_ptr += n_items;
244b4c3e9b5SBjoern A. Zeeb 	if (commonring->r_ptr == commonring->depth)
245b4c3e9b5SBjoern A. Zeeb 		commonring->r_ptr = 0;
246b4c3e9b5SBjoern A. Zeeb 
247b4c3e9b5SBjoern A. Zeeb 	if (commonring->cr_write_rptr)
248b4c3e9b5SBjoern A. Zeeb 		return commonring->cr_write_rptr(commonring->cr_ctx);
249b4c3e9b5SBjoern A. Zeeb 
250b4c3e9b5SBjoern A. Zeeb 	return -EIO;
251b4c3e9b5SBjoern A. Zeeb }
252