1 /* Copyright (c) 2014 Broadcom Corporation
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * 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 ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <linux/types.h>
17 #include <linux/netdevice.h>
19 #include <brcmu_utils.h>
20 #include <brcmu_wifi.h>
23 #include "commonring.h"
26 /* dma flushing needs implementation for mips and arm platforms. Should
27 * be put in util. Note, this is not real flushing. It is virtual non
28 * cached memory. Only write buffers should have to be drained. Though
29 * this may be different depending on platform......
32 #define brcmf_dma_flush(addr, len)
33 #define brcmf_dma_invalidate_cache(addr, len)
36 void brcmf_commonring_register_cb(struct brcmf_commonring *commonring,
37 int (*cr_ring_bell)(void *ctx),
38 int (*cr_update_rptr)(void *ctx),
39 int (*cr_update_wptr)(void *ctx),
40 int (*cr_write_rptr)(void *ctx),
41 int (*cr_write_wptr)(void *ctx), void *ctx)
43 commonring->cr_ring_bell = cr_ring_bell;
44 commonring->cr_update_rptr = cr_update_rptr;
45 commonring->cr_update_wptr = cr_update_wptr;
46 commonring->cr_write_rptr = cr_write_rptr;
47 commonring->cr_write_wptr = cr_write_wptr;
48 commonring->cr_ctx = ctx;
52 void brcmf_commonring_config(struct brcmf_commonring *commonring, u16 depth,
53 u16 item_len, void *buf_addr)
55 commonring->depth = depth;
56 commonring->item_len = item_len;
57 commonring->buf_addr = buf_addr;
58 if (!commonring->inited) {
59 spin_lock_init(&commonring->lock);
60 commonring->inited = true;
62 commonring->r_ptr = 0;
63 if (commonring->cr_write_rptr)
64 commonring->cr_write_rptr(commonring->cr_ctx);
65 commonring->w_ptr = 0;
66 if (commonring->cr_write_wptr)
67 commonring->cr_write_wptr(commonring->cr_ctx);
68 commonring->f_ptr = 0;
72 void brcmf_commonring_lock(struct brcmf_commonring *commonring)
73 __acquires(&commonring->lock)
77 spin_lock_irqsave(&commonring->lock, flags);
78 commonring->flags = flags;
82 void brcmf_commonring_unlock(struct brcmf_commonring *commonring)
83 __releases(&commonring->lock)
85 spin_unlock_irqrestore(&commonring->lock, commonring->flags);
89 bool brcmf_commonring_write_available(struct brcmf_commonring *commonring)
95 if (commonring->r_ptr <= commonring->w_ptr)
96 available = commonring->depth - commonring->w_ptr +
99 available = commonring->r_ptr - commonring->w_ptr;
102 if (!commonring->was_full)
104 if (available > commonring->depth / 8) {
105 commonring->was_full = false;
109 if (commonring->cr_update_rptr)
110 commonring->cr_update_rptr(commonring->cr_ctx);
118 if (commonring->cr_update_rptr)
119 commonring->cr_update_rptr(commonring->cr_ctx);
124 commonring->was_full = true;
129 void *brcmf_commonring_reserve_for_write(struct brcmf_commonring *commonring)
136 if (commonring->r_ptr <= commonring->w_ptr)
137 available = commonring->depth - commonring->w_ptr +
140 available = commonring->r_ptr - commonring->w_ptr;
143 ret_ptr = commonring->buf_addr +
144 (commonring->w_ptr * commonring->item_len);
146 if (commonring->w_ptr == commonring->depth)
147 commonring->w_ptr = 0;
152 if (commonring->cr_update_rptr)
153 commonring->cr_update_rptr(commonring->cr_ctx);
158 commonring->was_full = true;
164 brcmf_commonring_reserve_for_write_multiple(struct brcmf_commonring *commonring,
165 u16 n_items, u16 *alloced)
172 if (commonring->r_ptr <= commonring->w_ptr)
173 available = commonring->depth - commonring->w_ptr +
176 available = commonring->r_ptr - commonring->w_ptr;
179 ret_ptr = commonring->buf_addr +
180 (commonring->w_ptr * commonring->item_len);
181 *alloced = min_t(u16, n_items, available - 1);
182 if (*alloced + commonring->w_ptr > commonring->depth)
183 *alloced = commonring->depth - commonring->w_ptr;
184 commonring->w_ptr += *alloced;
185 if (commonring->w_ptr == commonring->depth)
186 commonring->w_ptr = 0;
191 if (commonring->cr_update_rptr)
192 commonring->cr_update_rptr(commonring->cr_ctx);
197 commonring->was_full = true;
202 int brcmf_commonring_write_complete(struct brcmf_commonring *commonring)
206 address = commonring->buf_addr;
207 address += (commonring->f_ptr * commonring->item_len);
208 if (commonring->f_ptr > commonring->w_ptr) {
209 brcmf_dma_flush(address,
210 (commonring->depth - commonring->f_ptr) *
211 commonring->item_len);
212 address = commonring->buf_addr;
213 commonring->f_ptr = 0;
215 brcmf_dma_flush(address, (commonring->w_ptr - commonring->f_ptr) *
216 commonring->item_len);
218 commonring->f_ptr = commonring->w_ptr;
220 if (commonring->cr_write_wptr)
221 commonring->cr_write_wptr(commonring->cr_ctx);
222 if (commonring->cr_ring_bell)
223 return commonring->cr_ring_bell(commonring->cr_ctx);
229 void brcmf_commonring_write_cancel(struct brcmf_commonring *commonring,
232 if (commonring->w_ptr == 0)
233 commonring->w_ptr = commonring->depth - n_items;
235 commonring->w_ptr -= n_items;
239 void *brcmf_commonring_get_read_ptr(struct brcmf_commonring *commonring,
244 if (commonring->cr_update_wptr)
245 commonring->cr_update_wptr(commonring->cr_ctx);
247 *n_items = (commonring->w_ptr >= commonring->r_ptr) ?
248 (commonring->w_ptr - commonring->r_ptr) :
249 (commonring->depth - commonring->r_ptr);
254 ret_addr = commonring->buf_addr +
255 (commonring->r_ptr * commonring->item_len);
257 commonring->r_ptr += *n_items;
258 if (commonring->r_ptr == commonring->depth)
259 commonring->r_ptr = 0;
261 brcmf_dma_invalidate_cache(ret_addr, *n_ items * commonring->item_len);
267 int brcmf_commonring_read_complete(struct brcmf_commonring *commonring)
269 if (commonring->cr_write_rptr)
270 return commonring->cr_write_rptr(commonring->cr_ctx);