xref: /OK3568_Linux_fs/external/rkwifibt/drivers/infineon/dhd_wlfc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * DHD PROP_TXSTATUS Module.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Portions of this code are copyright (c) 2021 Cypress Semiconductor Corporation
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 1999-2017, Broadcom Corporation
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *      Unless you and Broadcom execute a separate written software license
9*4882a593Smuzhiyun  * agreement governing use of this software, this software is licensed to you
10*4882a593Smuzhiyun  * under the terms of the GNU General Public License version 2 (the "GPL"),
11*4882a593Smuzhiyun  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12*4882a593Smuzhiyun  * following added to such license:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *      As a special exception, the copyright holders of this software give you
15*4882a593Smuzhiyun  * permission to link this software with independent modules, and to copy and
16*4882a593Smuzhiyun  * distribute the resulting executable under terms of your choice, provided that
17*4882a593Smuzhiyun  * you also meet, for each linked independent module, the terms and conditions of
18*4882a593Smuzhiyun  * the license of that module.  An independent module is a module which is not
19*4882a593Smuzhiyun  * derived from this software.  The special exception does not apply to any
20*4882a593Smuzhiyun  * modifications of the software.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  *      Notwithstanding the above, under no circumstances may you combine this
23*4882a593Smuzhiyun  * software in any way with any other Broadcom software provided under a license
24*4882a593Smuzhiyun  * other than the GPL, without Broadcom's express prior written consent.
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * <<Broadcom-WL-IPTag/Open:>>
28*4882a593Smuzhiyun  *
29*4882a593Smuzhiyun  * $Id: dhd_wlfc.c 700323 2017-05-18 16:12:11Z $
30*4882a593Smuzhiyun  *
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <typedefs.h>
34*4882a593Smuzhiyun #include <osl.h>
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #include <bcmutils.h>
37*4882a593Smuzhiyun #include <bcmendian.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include <dngl_stats.h>
40*4882a593Smuzhiyun #include <dhd.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include <dhd_bus.h>
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #include <dhd_dbg.h>
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #ifdef PROP_TXSTATUS /* a form of flow control between host and dongle */
47*4882a593Smuzhiyun #include <wlfc_proto.h>
48*4882a593Smuzhiyun #include <dhd_wlfc.h>
49*4882a593Smuzhiyun #endif // endif
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #ifdef DHDTCPACK_SUPPRESS
52*4882a593Smuzhiyun #include <dhd_ip.h>
53*4882a593Smuzhiyun #endif /* DHDTCPACK_SUPPRESS */
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * wlfc naming and lock rules:
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * 1. Private functions name like _dhd_wlfc_XXX, declared as static and avoid wlfc lock operation.
59*4882a593Smuzhiyun  * 2. Public functions name like dhd_wlfc_XXX, use wlfc lock if needed.
60*4882a593Smuzhiyun  * 3. Non-Proptxstatus module call public functions only and avoid wlfc lock operation.
61*4882a593Smuzhiyun  *
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
65*4882a593Smuzhiyun #define WLFC_THREAD_QUICK_RETRY_WAIT_MS    10      /* 10 msec */
66*4882a593Smuzhiyun #define WLFC_THREAD_RETRY_WAIT_MS          10000   /* 10 sec */
67*4882a593Smuzhiyun #endif /* defined (DHD_WLFC_THREAD) */
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun #ifdef PROP_TXSTATUS
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun #define DHD_WLFC_QMON_COMPLETE(entry)
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /** reordering related */
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
76*4882a593Smuzhiyun static void
_dhd_wlfc_thread_wakeup(dhd_pub_t * dhdp)77*4882a593Smuzhiyun _dhd_wlfc_thread_wakeup(dhd_pub_t *dhdp)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	dhdp->wlfc_thread_go = TRUE;
80*4882a593Smuzhiyun 	wake_up_interruptible(&dhdp->wlfc_wqhead);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun #endif /* DHD_WLFC_THREAD */
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun static uint16
_dhd_wlfc_adjusted_seq(void * p,uint8 current_seq)85*4882a593Smuzhiyun _dhd_wlfc_adjusted_seq(void* p, uint8 current_seq)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	uint16 seq;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	if (!p) {
90*4882a593Smuzhiyun 		return 0xffff;
91*4882a593Smuzhiyun 	}
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	seq = WL_TXSTATUS_GET_FREERUNCTR(DHD_PKTTAG_H2DTAG(PKTTAG(p)));
94*4882a593Smuzhiyun 	if (seq < current_seq) {
95*4882a593Smuzhiyun 		/* wrap around */
96*4882a593Smuzhiyun 		seq += 256;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	return seq;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /**
103*4882a593Smuzhiyun  * Enqueue a caller supplied packet on a caller supplied precedence queue, optionally reorder
104*4882a593Smuzhiyun  * suppressed packets.
105*4882a593Smuzhiyun  *    @param[in] pq       caller supplied packet queue to enqueue the packet on
106*4882a593Smuzhiyun  *    @param[in] prec     precedence of the to-be-queued packet
107*4882a593Smuzhiyun  *    @param[in] p        transmit packet to enqueue
108*4882a593Smuzhiyun  *    @param[in] qHead    if TRUE, enqueue to head instead of tail. Used to maintain d11 seq order.
109*4882a593Smuzhiyun  *    @param[in] current_seq
110*4882a593Smuzhiyun  *    @param[in] reOrder  reOrder on odd precedence (=suppress queue)
111*4882a593Smuzhiyun  */
112*4882a593Smuzhiyun static void
_dhd_wlfc_prec_enque(struct pktq * pq,int prec,void * p,bool qHead,uint8 current_seq,bool reOrder)113*4882a593Smuzhiyun _dhd_wlfc_prec_enque(struct pktq *pq, int prec, void* p, bool qHead,
114*4882a593Smuzhiyun 	uint8 current_seq, bool reOrder)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	struct pktq_prec *q;
117*4882a593Smuzhiyun 	uint16 seq, seq2;
118*4882a593Smuzhiyun 	void *p2, *p2_prev;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	if (!p)
121*4882a593Smuzhiyun 		return;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	ASSERT(prec >= 0 && prec < pq->num_prec);
124*4882a593Smuzhiyun 	ASSERT(PKTLINK(p) == NULL);		/* queueing chains not allowed */
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	ASSERT(!pktq_full(pq));
127*4882a593Smuzhiyun 	ASSERT(!pktqprec_full(pq, prec));
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	q = &pq->q[prec];
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	if (q->head == NULL) {
132*4882a593Smuzhiyun 		/* empty queue */
133*4882a593Smuzhiyun 		q->head = p;
134*4882a593Smuzhiyun 		q->tail = p;
135*4882a593Smuzhiyun 	} else {
136*4882a593Smuzhiyun 		if (reOrder && (prec & 1)) {
137*4882a593Smuzhiyun 			seq = _dhd_wlfc_adjusted_seq(p, current_seq);
138*4882a593Smuzhiyun 			p2 = qHead ? q->head : q->tail;
139*4882a593Smuzhiyun 			seq2 = _dhd_wlfc_adjusted_seq(p2, current_seq);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 			if ((qHead &&((seq+1) > seq2)) || (!qHead && ((seq2+1) > seq))) {
142*4882a593Smuzhiyun 				/* need reorder */
143*4882a593Smuzhiyun 				p2 = q->head;
144*4882a593Smuzhiyun 				p2_prev = NULL;
145*4882a593Smuzhiyun 				seq2 = _dhd_wlfc_adjusted_seq(p2, current_seq);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 				while (seq > seq2) {
148*4882a593Smuzhiyun 					p2_prev = p2;
149*4882a593Smuzhiyun 					p2 = PKTLINK(p2);
150*4882a593Smuzhiyun 					if (!p2) {
151*4882a593Smuzhiyun 						break;
152*4882a593Smuzhiyun 					}
153*4882a593Smuzhiyun 					seq2 = _dhd_wlfc_adjusted_seq(p2, current_seq);
154*4882a593Smuzhiyun 				}
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 				if (p2_prev == NULL) {
157*4882a593Smuzhiyun 					/* insert head */
158*4882a593Smuzhiyun 					PKTSETLINK(p, q->head);
159*4882a593Smuzhiyun 					q->head = p;
160*4882a593Smuzhiyun 				} else if (p2 == NULL) {
161*4882a593Smuzhiyun 					/* insert tail */
162*4882a593Smuzhiyun 					PKTSETLINK(p2_prev, p);
163*4882a593Smuzhiyun 					q->tail = p;
164*4882a593Smuzhiyun 				} else {
165*4882a593Smuzhiyun 					/* insert after p2_prev */
166*4882a593Smuzhiyun 					PKTSETLINK(p, PKTLINK(p2_prev));
167*4882a593Smuzhiyun 					PKTSETLINK(p2_prev, p);
168*4882a593Smuzhiyun 				}
169*4882a593Smuzhiyun 				goto exit;
170*4882a593Smuzhiyun 			}
171*4882a593Smuzhiyun 		}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 		if (qHead) {
174*4882a593Smuzhiyun 			PKTSETLINK(p, q->head);
175*4882a593Smuzhiyun 			q->head = p;
176*4882a593Smuzhiyun 		} else {
177*4882a593Smuzhiyun 			PKTSETLINK(q->tail, p);
178*4882a593Smuzhiyun 			q->tail = p;
179*4882a593Smuzhiyun 		}
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun exit:
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	q->n_pkts++;
185*4882a593Smuzhiyun 	pq->n_pkts_tot++;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	if (pq->hi_prec < prec)
188*4882a593Smuzhiyun 		pq->hi_prec = (uint8)prec;
189*4882a593Smuzhiyun } /* _dhd_wlfc_prec_enque */
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /**
192*4882a593Smuzhiyun  * Create a place to store all packet pointers submitted to the firmware until a status comes back,
193*4882a593Smuzhiyun  * suppress or otherwise.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * hang-er: noun, a contrivance on which things are hung, as a hook.
196*4882a593Smuzhiyun  */
197*4882a593Smuzhiyun /** @deprecated soon */
198*4882a593Smuzhiyun static void*
_dhd_wlfc_hanger_create(dhd_pub_t * dhd,int max_items)199*4882a593Smuzhiyun _dhd_wlfc_hanger_create(dhd_pub_t *dhd, int max_items)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	int i;
202*4882a593Smuzhiyun 	wlfc_hanger_t* hanger;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	/* allow only up to a specific size for now */
205*4882a593Smuzhiyun 	ASSERT(max_items == WLFC_HANGER_MAXITEMS);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if ((hanger = (wlfc_hanger_t*)DHD_OS_PREALLOC(dhd, DHD_PREALLOC_DHD_WLFC_HANGER,
208*4882a593Smuzhiyun 		WLFC_HANGER_SIZE(max_items))) == NULL) {
209*4882a593Smuzhiyun 		return NULL;
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 	memset(hanger, 0, WLFC_HANGER_SIZE(max_items));
212*4882a593Smuzhiyun 	hanger->max_items = max_items;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	for (i = 0; i < hanger->max_items; i++) {
215*4882a593Smuzhiyun 		hanger->items[i].state = WLFC_HANGER_ITEM_STATE_FREE;
216*4882a593Smuzhiyun 	}
217*4882a593Smuzhiyun 	return hanger;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /** @deprecated soon */
221*4882a593Smuzhiyun static int
_dhd_wlfc_hanger_delete(dhd_pub_t * dhd,void * hanger)222*4882a593Smuzhiyun _dhd_wlfc_hanger_delete(dhd_pub_t *dhd, void* hanger)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	wlfc_hanger_t* h = (wlfc_hanger_t*)hanger;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	if (h) {
227*4882a593Smuzhiyun 		DHD_OS_PREFREE(dhd, h, WLFC_HANGER_SIZE(h->max_items));
228*4882a593Smuzhiyun 		return BCME_OK;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 	return BCME_BADARG;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun /** @deprecated soon */
234*4882a593Smuzhiyun static uint16
_dhd_wlfc_hanger_get_free_slot(void * hanger)235*4882a593Smuzhiyun _dhd_wlfc_hanger_get_free_slot(void* hanger)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun 	uint32 i;
238*4882a593Smuzhiyun 	wlfc_hanger_t* h = (wlfc_hanger_t*)hanger;
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	if (h) {
241*4882a593Smuzhiyun 		i = h->slot_pos + 1;
242*4882a593Smuzhiyun 		if (i == h->max_items) {
243*4882a593Smuzhiyun 			i = 0;
244*4882a593Smuzhiyun 		}
245*4882a593Smuzhiyun 		while (i != h->slot_pos) {
246*4882a593Smuzhiyun 			if (h->items[i].state == WLFC_HANGER_ITEM_STATE_FREE) {
247*4882a593Smuzhiyun 				h->slot_pos = i;
248*4882a593Smuzhiyun 				return (uint16)i;
249*4882a593Smuzhiyun 			}
250*4882a593Smuzhiyun 			i++;
251*4882a593Smuzhiyun 			if (i == h->max_items)
252*4882a593Smuzhiyun 				i = 0;
253*4882a593Smuzhiyun 		}
254*4882a593Smuzhiyun 		h->failed_slotfind++;
255*4882a593Smuzhiyun 	}
256*4882a593Smuzhiyun 	return WLFC_HANGER_MAXITEMS;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun /** @deprecated soon */
260*4882a593Smuzhiyun static int
_dhd_wlfc_hanger_get_genbit(void * hanger,void * pkt,uint32 slot_id,int * gen)261*4882a593Smuzhiyun _dhd_wlfc_hanger_get_genbit(void* hanger, void* pkt, uint32 slot_id, int* gen)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun 	int rc = BCME_OK;
264*4882a593Smuzhiyun 	wlfc_hanger_t* h = (wlfc_hanger_t*)hanger;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	*gen = 0xff;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	/* this packet was not pushed at the time it went to the firmware */
269*4882a593Smuzhiyun 	if (slot_id == WLFC_HANGER_MAXITEMS)
270*4882a593Smuzhiyun 		return BCME_NOTFOUND;
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	if (h) {
273*4882a593Smuzhiyun 		if (h->items[slot_id].state != WLFC_HANGER_ITEM_STATE_FREE) {
274*4882a593Smuzhiyun 			*gen = h->items[slot_id].gen;
275*4882a593Smuzhiyun 		}
276*4882a593Smuzhiyun 		else {
277*4882a593Smuzhiyun 			DHD_ERROR(("Error: %s():%d item not used\n",
278*4882a593Smuzhiyun 				__FUNCTION__, __LINE__));
279*4882a593Smuzhiyun 			rc = BCME_NOTFOUND;
280*4882a593Smuzhiyun 		}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	} else {
283*4882a593Smuzhiyun 		rc = BCME_BADARG;
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	return rc;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun /** @deprecated soon */
290*4882a593Smuzhiyun static int
_dhd_wlfc_hanger_pushpkt(void * hanger,void * pkt,uint32 slot_id)291*4882a593Smuzhiyun _dhd_wlfc_hanger_pushpkt(void* hanger, void* pkt, uint32 slot_id)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	int rc = BCME_OK;
294*4882a593Smuzhiyun 	wlfc_hanger_t* h = (wlfc_hanger_t*)hanger;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	if (h && (slot_id < WLFC_HANGER_MAXITEMS)) {
297*4882a593Smuzhiyun 		if (h->items[slot_id].state == WLFC_HANGER_ITEM_STATE_FREE) {
298*4882a593Smuzhiyun 			h->items[slot_id].state = WLFC_HANGER_ITEM_STATE_INUSE;
299*4882a593Smuzhiyun 			h->items[slot_id].pkt = pkt;
300*4882a593Smuzhiyun 			h->items[slot_id].pkt_state = 0;
301*4882a593Smuzhiyun 			h->items[slot_id].pkt_txstatus = 0;
302*4882a593Smuzhiyun 			h->pushed++;
303*4882a593Smuzhiyun 		} else {
304*4882a593Smuzhiyun 			h->failed_to_push++;
305*4882a593Smuzhiyun 			rc = BCME_NOTFOUND;
306*4882a593Smuzhiyun 		}
307*4882a593Smuzhiyun 	} else {
308*4882a593Smuzhiyun 		rc = BCME_BADARG;
309*4882a593Smuzhiyun 	}
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	return rc;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun /** @deprecated soon */
315*4882a593Smuzhiyun static int
_dhd_wlfc_hanger_poppkt(void * hanger,uint32 slot_id,void ** pktout,bool remove_from_hanger)316*4882a593Smuzhiyun _dhd_wlfc_hanger_poppkt(void* hanger, uint32 slot_id, void** pktout, bool remove_from_hanger)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	int rc = BCME_OK;
319*4882a593Smuzhiyun 	wlfc_hanger_t* h = (wlfc_hanger_t*)hanger;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	*pktout = NULL;
322*4882a593Smuzhiyun 
323*4882a593Smuzhiyun 	/* this packet was not pushed at the time it went to the firmware */
324*4882a593Smuzhiyun 	if (slot_id == WLFC_HANGER_MAXITEMS)
325*4882a593Smuzhiyun 		return BCME_NOTFOUND;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	if (h) {
328*4882a593Smuzhiyun 		if (h->items[slot_id].state != WLFC_HANGER_ITEM_STATE_FREE) {
329*4882a593Smuzhiyun 			*pktout = h->items[slot_id].pkt;
330*4882a593Smuzhiyun 			if (remove_from_hanger) {
331*4882a593Smuzhiyun 				h->items[slot_id].state =
332*4882a593Smuzhiyun 					WLFC_HANGER_ITEM_STATE_FREE;
333*4882a593Smuzhiyun 				h->items[slot_id].pkt = NULL;
334*4882a593Smuzhiyun 				h->items[slot_id].gen = 0xff;
335*4882a593Smuzhiyun 				h->items[slot_id].identifier = 0;
336*4882a593Smuzhiyun 				h->popped++;
337*4882a593Smuzhiyun 			}
338*4882a593Smuzhiyun 		} else {
339*4882a593Smuzhiyun 			h->failed_to_pop++;
340*4882a593Smuzhiyun 			rc = BCME_NOTFOUND;
341*4882a593Smuzhiyun 		}
342*4882a593Smuzhiyun 	} else {
343*4882a593Smuzhiyun 		rc = BCME_BADARG;
344*4882a593Smuzhiyun 	}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	return rc;
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun /** @deprecated soon */
350*4882a593Smuzhiyun static int
_dhd_wlfc_hanger_mark_suppressed(void * hanger,uint32 slot_id,uint8 gen)351*4882a593Smuzhiyun _dhd_wlfc_hanger_mark_suppressed(void* hanger, uint32 slot_id, uint8 gen)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	int rc = BCME_OK;
354*4882a593Smuzhiyun 	wlfc_hanger_t* h = (wlfc_hanger_t*)hanger;
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun 	/* this packet was not pushed at the time it went to the firmware */
357*4882a593Smuzhiyun 	if (slot_id == WLFC_HANGER_MAXITEMS)
358*4882a593Smuzhiyun 		return BCME_NOTFOUND;
359*4882a593Smuzhiyun 	if (h) {
360*4882a593Smuzhiyun 		h->items[slot_id].gen = gen;
361*4882a593Smuzhiyun 		if (h->items[slot_id].state == WLFC_HANGER_ITEM_STATE_INUSE) {
362*4882a593Smuzhiyun 			h->items[slot_id].state = WLFC_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
363*4882a593Smuzhiyun 		} else {
364*4882a593Smuzhiyun 			rc = BCME_BADARG;
365*4882a593Smuzhiyun 		}
366*4882a593Smuzhiyun 	} else {
367*4882a593Smuzhiyun 		rc = BCME_BADARG;
368*4882a593Smuzhiyun 	}
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	return rc;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun /** remove reference of specific packet in hanger */
374*4882a593Smuzhiyun /** @deprecated soon */
375*4882a593Smuzhiyun static bool
_dhd_wlfc_hanger_remove_reference(wlfc_hanger_t * h,void * pkt)376*4882a593Smuzhiyun _dhd_wlfc_hanger_remove_reference(wlfc_hanger_t* h, void* pkt)
377*4882a593Smuzhiyun {
378*4882a593Smuzhiyun 	int i;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	if (!h || !pkt) {
381*4882a593Smuzhiyun 		return FALSE;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	i = WL_TXSTATUS_GET_HSLOT(DHD_PKTTAG_H2DTAG(PKTTAG(pkt)));
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	if ((i < h->max_items) && (pkt == h->items[i].pkt)) {
387*4882a593Smuzhiyun 		if (h->items[i].state == WLFC_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
388*4882a593Smuzhiyun 			h->items[i].state = WLFC_HANGER_ITEM_STATE_FREE;
389*4882a593Smuzhiyun 			h->items[i].pkt = NULL;
390*4882a593Smuzhiyun 			h->items[i].gen = 0xff;
391*4882a593Smuzhiyun 			h->items[i].identifier = 0;
392*4882a593Smuzhiyun 			return TRUE;
393*4882a593Smuzhiyun 		} else {
394*4882a593Smuzhiyun 			DHD_ERROR(("Error: %s():%d item not suppressed\n",
395*4882a593Smuzhiyun 				__FUNCTION__, __LINE__));
396*4882a593Smuzhiyun 		}
397*4882a593Smuzhiyun 	}
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	return FALSE;
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun /** afq = At Firmware Queue, queue containing packets pending in the dongle */
403*4882a593Smuzhiyun static int
_dhd_wlfc_enque_afq(athost_wl_status_info_t * ctx,void * p)404*4882a593Smuzhiyun _dhd_wlfc_enque_afq(athost_wl_status_info_t* ctx, void *p)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry;
407*4882a593Smuzhiyun 	uint16 entry_idx = WL_TXSTATUS_GET_HSLOT(DHD_PKTTAG_H2DTAG(PKTTAG(p)));
408*4882a593Smuzhiyun 	uint8 prec = DHD_PKTTAG_FIFO(PKTTAG(p));
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	if (entry_idx < WLFC_MAC_DESC_TABLE_SIZE)
411*4882a593Smuzhiyun 		entry  = &ctx->destination_entries.nodes[entry_idx];
412*4882a593Smuzhiyun 	else if (entry_idx < (WLFC_MAC_DESC_TABLE_SIZE + WLFC_MAX_IFNUM))
413*4882a593Smuzhiyun 		entry = &ctx->destination_entries.interfaces[entry_idx - WLFC_MAC_DESC_TABLE_SIZE];
414*4882a593Smuzhiyun 	else
415*4882a593Smuzhiyun 		entry = &ctx->destination_entries.other;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	pktq_penq(&entry->afq, prec, p);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	return BCME_OK;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun /** afq = At Firmware Queue, queue containing packets pending in the dongle */
423*4882a593Smuzhiyun static int
_dhd_wlfc_deque_afq(athost_wl_status_info_t * ctx,uint16 hslot,uint8 hcnt,uint8 prec,void ** pktout)424*4882a593Smuzhiyun _dhd_wlfc_deque_afq(athost_wl_status_info_t* ctx, uint16 hslot, uint8 hcnt, uint8 prec,
425*4882a593Smuzhiyun 	void **pktout)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	wlfc_mac_descriptor_t *entry;
428*4882a593Smuzhiyun 	struct pktq *pq;
429*4882a593Smuzhiyun 	struct pktq_prec *q;
430*4882a593Smuzhiyun 	void *p, *b;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	if (!ctx) {
433*4882a593Smuzhiyun 		DHD_ERROR(("%s: ctx(%p), pktout(%p)\n", __FUNCTION__, ctx, pktout));
434*4882a593Smuzhiyun 		return BCME_BADARG;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	if (pktout) {
438*4882a593Smuzhiyun 		*pktout = NULL;
439*4882a593Smuzhiyun 	}
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	ASSERT(hslot < (WLFC_MAC_DESC_TABLE_SIZE + WLFC_MAX_IFNUM + 1));
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	if (hslot < WLFC_MAC_DESC_TABLE_SIZE)
444*4882a593Smuzhiyun 		entry  = &ctx->destination_entries.nodes[hslot];
445*4882a593Smuzhiyun 	else if (hslot < (WLFC_MAC_DESC_TABLE_SIZE + WLFC_MAX_IFNUM))
446*4882a593Smuzhiyun 		entry = &ctx->destination_entries.interfaces[hslot - WLFC_MAC_DESC_TABLE_SIZE];
447*4882a593Smuzhiyun 	else
448*4882a593Smuzhiyun 		entry = &ctx->destination_entries.other;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	pq = &entry->afq;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	ASSERT(prec < pq->num_prec);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	q = &pq->q[prec];
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	b = NULL;
457*4882a593Smuzhiyun 	p = q->head;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	while (p && (hcnt != WL_TXSTATUS_GET_FREERUNCTR(DHD_PKTTAG_H2DTAG(PKTTAG(p)))))
460*4882a593Smuzhiyun 	{
461*4882a593Smuzhiyun 		b = p;
462*4882a593Smuzhiyun 		p = PKTLINK(p);
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (p == NULL) {
466*4882a593Smuzhiyun 		/* none is matched */
467*4882a593Smuzhiyun 		if (b) {
468*4882a593Smuzhiyun 			DHD_ERROR(("%s: can't find matching seq(%d)\n", __FUNCTION__, hcnt));
469*4882a593Smuzhiyun 		} else {
470*4882a593Smuzhiyun 			DHD_ERROR(("%s: queue is empty\n", __FUNCTION__));
471*4882a593Smuzhiyun 		}
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 		return BCME_ERROR;
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	bcm_pkt_validate_chk(p);
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	if (!b) {
479*4882a593Smuzhiyun 		/* head packet is matched */
480*4882a593Smuzhiyun 		if ((q->head = PKTLINK(p)) == NULL) {
481*4882a593Smuzhiyun 			q->tail = NULL;
482*4882a593Smuzhiyun 		}
483*4882a593Smuzhiyun 	} else {
484*4882a593Smuzhiyun 		/* middle packet is matched */
485*4882a593Smuzhiyun 		DHD_INFO(("%s: out of order, seq(%d), head_seq(%d)\n", __FUNCTION__, hcnt,
486*4882a593Smuzhiyun 			WL_TXSTATUS_GET_FREERUNCTR(DHD_PKTTAG_H2DTAG(PKTTAG(q->head)))));
487*4882a593Smuzhiyun 		ctx->stats.ooo_pkts[prec]++;
488*4882a593Smuzhiyun 		PKTSETLINK(b, PKTLINK(p));
489*4882a593Smuzhiyun 		if (PKTLINK(p) == NULL) {
490*4882a593Smuzhiyun 			q->tail = b;
491*4882a593Smuzhiyun 		}
492*4882a593Smuzhiyun 	}
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	q->n_pkts--;
495*4882a593Smuzhiyun 	pq->n_pkts_tot--;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun #ifdef WL_TXQ_STALL
498*4882a593Smuzhiyun 	q->dequeue_count++;
499*4882a593Smuzhiyun #endif // endif
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	PKTSETLINK(p, NULL);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	if (pktout) {
504*4882a593Smuzhiyun 		*pktout = p;
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	return BCME_OK;
508*4882a593Smuzhiyun } /* _dhd_wlfc_deque_afq */
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun /**
511*4882a593Smuzhiyun  * Flow control information piggy backs on packets, in the form of one or more TLVs. This function
512*4882a593Smuzhiyun  * pushes one or more TLVs onto a packet that is going to be sent towards the dongle.
513*4882a593Smuzhiyun  *
514*4882a593Smuzhiyun  *     @param[in]     ctx
515*4882a593Smuzhiyun  *     @param[in/out] packet
516*4882a593Smuzhiyun  *     @param[in]     tim_signal TRUE if parameter 'tim_bmp' is valid
517*4882a593Smuzhiyun  *     @param[in]     tim_bmp
518*4882a593Smuzhiyun  *     @param[in]     mac_handle
519*4882a593Smuzhiyun  *     @param[in]     htodtag
520*4882a593Smuzhiyun  *     @param[in]     htodseq d11 seqno for seqno reuse, only used if 'seq reuse' was agreed upon
521*4882a593Smuzhiyun  *                    earlier between host and firmware.
522*4882a593Smuzhiyun  *     @param[in]     skip_wlfc_hdr
523*4882a593Smuzhiyun  */
524*4882a593Smuzhiyun static int
_dhd_wlfc_pushheader(athost_wl_status_info_t * ctx,void ** packet,bool tim_signal,uint8 tim_bmp,uint8 mac_handle,uint32 htodtag,uint16 htodseq,bool skip_wlfc_hdr)525*4882a593Smuzhiyun _dhd_wlfc_pushheader(athost_wl_status_info_t* ctx, void** packet, bool tim_signal,
526*4882a593Smuzhiyun 	uint8 tim_bmp, uint8 mac_handle, uint32 htodtag, uint16 htodseq, bool skip_wlfc_hdr)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	uint32 wl_pktinfo = 0;
529*4882a593Smuzhiyun 	uint8* wlh;
530*4882a593Smuzhiyun 	uint8 dataOffset = 0;
531*4882a593Smuzhiyun 	uint8 fillers;
532*4882a593Smuzhiyun 	uint8 tim_signal_len = 0;
533*4882a593Smuzhiyun 	dhd_pub_t *dhdp = (dhd_pub_t *)ctx->dhdp;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	struct bdc_header *h;
536*4882a593Smuzhiyun 	void *p = *packet;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	if (skip_wlfc_hdr)
539*4882a593Smuzhiyun 		goto push_bdc_hdr;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (tim_signal) {
542*4882a593Smuzhiyun 		tim_signal_len = TLV_HDR_LEN + WLFC_CTL_VALUE_LEN_PENDING_TRAFFIC_BMP;
543*4882a593Smuzhiyun 	}
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	/* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
546*4882a593Smuzhiyun 	dataOffset = WLFC_CTL_VALUE_LEN_PKTTAG + TLV_HDR_LEN + tim_signal_len;
547*4882a593Smuzhiyun 	if (WLFC_GET_REUSESEQ(dhdp->wlfc_mode)) {
548*4882a593Smuzhiyun 		dataOffset += WLFC_CTL_VALUE_LEN_SEQ;
549*4882a593Smuzhiyun 	}
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	fillers = ROUNDUP(dataOffset, 4) - dataOffset;
552*4882a593Smuzhiyun 	dataOffset += fillers;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	PKTPUSH(ctx->osh, p, dataOffset);
555*4882a593Smuzhiyun 	wlh = (uint8*) PKTDATA(ctx->osh, p);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	wl_pktinfo = htol32(htodtag);
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	wlh[TLV_TAG_OFF] = WLFC_CTL_TYPE_PKTTAG;
560*4882a593Smuzhiyun 	wlh[TLV_LEN_OFF] = WLFC_CTL_VALUE_LEN_PKTTAG;
561*4882a593Smuzhiyun 	memcpy(&wlh[TLV_HDR_LEN] /* dst */, &wl_pktinfo, sizeof(uint32));
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	if (WLFC_GET_REUSESEQ(dhdp->wlfc_mode)) {
564*4882a593Smuzhiyun 		uint16 wl_seqinfo = htol16(htodseq);
565*4882a593Smuzhiyun 		wlh[TLV_LEN_OFF] += WLFC_CTL_VALUE_LEN_SEQ;
566*4882a593Smuzhiyun 		memcpy(&wlh[TLV_HDR_LEN + WLFC_CTL_VALUE_LEN_PKTTAG], &wl_seqinfo,
567*4882a593Smuzhiyun 			WLFC_CTL_VALUE_LEN_SEQ);
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	if (tim_signal_len) {
571*4882a593Smuzhiyun 		wlh[dataOffset - fillers - tim_signal_len ] =
572*4882a593Smuzhiyun 			WLFC_CTL_TYPE_PENDING_TRAFFIC_BMP;
573*4882a593Smuzhiyun 		wlh[dataOffset - fillers - tim_signal_len + 1] =
574*4882a593Smuzhiyun 			WLFC_CTL_VALUE_LEN_PENDING_TRAFFIC_BMP;
575*4882a593Smuzhiyun 		wlh[dataOffset - fillers - tim_signal_len + 2] = mac_handle;
576*4882a593Smuzhiyun 		wlh[dataOffset - fillers - tim_signal_len + 3] = tim_bmp;
577*4882a593Smuzhiyun 	}
578*4882a593Smuzhiyun 	if (fillers)
579*4882a593Smuzhiyun 		memset(&wlh[dataOffset - fillers], WLFC_CTL_TYPE_FILLER, fillers);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun push_bdc_hdr:
582*4882a593Smuzhiyun 	PKTPUSH(ctx->osh, p, BDC_HEADER_LEN);
583*4882a593Smuzhiyun 	h = (struct bdc_header *)PKTDATA(ctx->osh, p);
584*4882a593Smuzhiyun 	h->flags = (BDC_PROTO_VER << BDC_FLAG_VER_SHIFT);
585*4882a593Smuzhiyun 	if (PKTSUMNEEDED(p))
586*4882a593Smuzhiyun 		h->flags |= BDC_FLAG_SUM_NEEDED;
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun 	h->priority = (PKTPRIO(p) & BDC_PRIORITY_MASK);
589*4882a593Smuzhiyun 	h->flags2 = 0;
590*4882a593Smuzhiyun 	h->dataOffset = dataOffset >> 2;
591*4882a593Smuzhiyun 	BDC_SET_IF_IDX(h, DHD_PKTTAG_IF(PKTTAG(p)));
592*4882a593Smuzhiyun 	*packet = p;
593*4882a593Smuzhiyun 	return BCME_OK;
594*4882a593Smuzhiyun } /* _dhd_wlfc_pushheader */
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun /**
597*4882a593Smuzhiyun  * Removes (PULLs) flow control related headers from the caller supplied packet, is invoked eg
598*4882a593Smuzhiyun  * when a packet is about to be freed.
599*4882a593Smuzhiyun  */
600*4882a593Smuzhiyun static int
_dhd_wlfc_pullheader(athost_wl_status_info_t * ctx,void * pktbuf)601*4882a593Smuzhiyun _dhd_wlfc_pullheader(athost_wl_status_info_t* ctx, void* pktbuf)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun 	struct bdc_header *h;
604*4882a593Smuzhiyun 
605*4882a593Smuzhiyun 	if (PKTLEN(ctx->osh, pktbuf) < BDC_HEADER_LEN) {
606*4882a593Smuzhiyun 		DHD_ERROR(("%s: rx data too short (%d < %d)\n", __FUNCTION__,
607*4882a593Smuzhiyun 		           PKTLEN(ctx->osh, pktbuf), BDC_HEADER_LEN));
608*4882a593Smuzhiyun 		return BCME_ERROR;
609*4882a593Smuzhiyun 	}
610*4882a593Smuzhiyun 	h = (struct bdc_header *)PKTDATA(ctx->osh, pktbuf);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	/* pull BDC header */
613*4882a593Smuzhiyun 	PKTPULL(ctx->osh, pktbuf, BDC_HEADER_LEN);
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (PKTLEN(ctx->osh, pktbuf) < (uint)(h->dataOffset << 2)) {
616*4882a593Smuzhiyun 		DHD_ERROR(("%s: rx data too short (%d < %d)\n", __FUNCTION__,
617*4882a593Smuzhiyun 		           PKTLEN(ctx->osh, pktbuf), (h->dataOffset << 2)));
618*4882a593Smuzhiyun 		return BCME_ERROR;
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* pull wl-header */
622*4882a593Smuzhiyun 	PKTPULL(ctx->osh, pktbuf, (h->dataOffset << 2));
623*4882a593Smuzhiyun 	return BCME_OK;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun /**
627*4882a593Smuzhiyun  * @param[in/out] p packet
628*4882a593Smuzhiyun  */
629*4882a593Smuzhiyun static wlfc_mac_descriptor_t*
_dhd_wlfc_find_table_entry(athost_wl_status_info_t * ctx,void * p)630*4882a593Smuzhiyun _dhd_wlfc_find_table_entry(athost_wl_status_info_t* ctx, void* p)
631*4882a593Smuzhiyun {
632*4882a593Smuzhiyun 	int i;
633*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* table = ctx->destination_entries.nodes;
634*4882a593Smuzhiyun 	uint8 ifid = DHD_PKTTAG_IF(PKTTAG(p));
635*4882a593Smuzhiyun 	uint8* dstn = DHD_PKTTAG_DSTN(PKTTAG(p));
636*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry = DHD_PKTTAG_ENTRY(PKTTAG(p));
637*4882a593Smuzhiyun 	int iftype = ctx->destination_entries.interfaces[ifid].iftype;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	/* saved one exists, return it */
640*4882a593Smuzhiyun 	if (entry)
641*4882a593Smuzhiyun 		return entry;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	/* Multicast destination, STA and P2P clients get the interface entry.
644*4882a593Smuzhiyun 	 * STA/GC gets the Mac Entry for TDLS destinations, TDLS destinations
645*4882a593Smuzhiyun 	 * have their own entry.
646*4882a593Smuzhiyun 	 */
647*4882a593Smuzhiyun 	if ((iftype == WLC_E_IF_ROLE_STA || ETHER_ISMULTI(dstn) ||
648*4882a593Smuzhiyun 		iftype == WLC_E_IF_ROLE_P2P_CLIENT) &&
649*4882a593Smuzhiyun 		(ctx->destination_entries.interfaces[ifid].occupied)) {
650*4882a593Smuzhiyun 			entry = &ctx->destination_entries.interfaces[ifid];
651*4882a593Smuzhiyun 	}
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	if (entry && ETHER_ISMULTI(dstn)) {
654*4882a593Smuzhiyun 		DHD_PKTTAG_SET_ENTRY(PKTTAG(p), entry);
655*4882a593Smuzhiyun 		return entry;
656*4882a593Smuzhiyun 	}
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	for (i = 0; i < WLFC_MAC_DESC_TABLE_SIZE; i++) {
659*4882a593Smuzhiyun 		if (table[i].occupied) {
660*4882a593Smuzhiyun 			if (table[i].interface_id == ifid) {
661*4882a593Smuzhiyun 				if (!memcmp(table[i].ea, dstn, ETHER_ADDR_LEN)) {
662*4882a593Smuzhiyun 					entry = &table[i];
663*4882a593Smuzhiyun 					break;
664*4882a593Smuzhiyun 				}
665*4882a593Smuzhiyun 			}
666*4882a593Smuzhiyun 		}
667*4882a593Smuzhiyun 	}
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	if (entry == NULL)
670*4882a593Smuzhiyun 		entry = &ctx->destination_entries.other;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	DHD_PKTTAG_SET_ENTRY(PKTTAG(p), entry);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	return entry;
675*4882a593Smuzhiyun } /* _dhd_wlfc_find_table_entry */
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun /**
678*4882a593Smuzhiyun  * In case a packet must be dropped (because eg the queues are full), various tallies have to be
679*4882a593Smuzhiyun  * be updated. Called from several other functions.
680*4882a593Smuzhiyun  *     @param[in] dhdp pointer to public DHD structure
681*4882a593Smuzhiyun  *     @param[in] prec precedence of the packet
682*4882a593Smuzhiyun  *     @param[in] p    the packet to be dropped
683*4882a593Smuzhiyun  *     @param[in] bPktInQ TRUE if packet is part of a queue
684*4882a593Smuzhiyun  */
685*4882a593Smuzhiyun static int
_dhd_wlfc_prec_drop(dhd_pub_t * dhdp,int prec,void * p,bool bPktInQ)686*4882a593Smuzhiyun _dhd_wlfc_prec_drop(dhd_pub_t *dhdp, int prec, void* p, bool bPktInQ)
687*4882a593Smuzhiyun {
688*4882a593Smuzhiyun 	athost_wl_status_info_t* ctx;
689*4882a593Smuzhiyun 	void *pout = NULL;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	ASSERT(dhdp && p);
692*4882a593Smuzhiyun 	if (prec < 0 || prec >= WLFC_PSQ_PREC_COUNT) {
693*4882a593Smuzhiyun 		ASSERT(0);
694*4882a593Smuzhiyun 		return BCME_BADARG;
695*4882a593Smuzhiyun 	}
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	ctx = (athost_wl_status_info_t*)dhdp->wlfc_state;
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	if (!WLFC_GET_AFQ(dhdp->wlfc_mode) && (prec & 1)) {
700*4882a593Smuzhiyun 		/* suppressed queue, need pop from hanger */
701*4882a593Smuzhiyun 		_dhd_wlfc_hanger_poppkt(ctx->hanger, WL_TXSTATUS_GET_HSLOT(DHD_PKTTAG_H2DTAG
702*4882a593Smuzhiyun 					(PKTTAG(p))), &pout, TRUE);
703*4882a593Smuzhiyun 		ASSERT(p == pout);
704*4882a593Smuzhiyun 	}
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	if (!(prec & 1)) {
707*4882a593Smuzhiyun #ifdef DHDTCPACK_SUPPRESS
708*4882a593Smuzhiyun 		/* pkt in delayed q, so fake push BDC header for
709*4882a593Smuzhiyun 		 * dhd_tcpack_check_xmit() and dhd_txcomplete().
710*4882a593Smuzhiyun 		 */
711*4882a593Smuzhiyun 		_dhd_wlfc_pushheader(ctx, &p, FALSE, 0, 0, 0, 0, TRUE);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 		/* This packet is about to be freed, so remove it from tcp_ack_info_tbl
714*4882a593Smuzhiyun 		 * This must be one of...
715*4882a593Smuzhiyun 		 * 1. A pkt already in delayQ is evicted by another pkt with higher precedence
716*4882a593Smuzhiyun 		 * in _dhd_wlfc_prec_enq_with_drop()
717*4882a593Smuzhiyun 		 * 2. A pkt could not be enqueued to delayQ because it is full,
718*4882a593Smuzhiyun 		 * in _dhd_wlfc_enque_delayq().
719*4882a593Smuzhiyun 		 * 3. A pkt could not be enqueued to delayQ because it is full,
720*4882a593Smuzhiyun 		 * in _dhd_wlfc_rollback_packet_toq().
721*4882a593Smuzhiyun 		 */
722*4882a593Smuzhiyun 		if (dhd_tcpack_check_xmit(dhdp, p) == BCME_ERROR) {
723*4882a593Smuzhiyun 			DHD_ERROR(("%s %d: tcpack_suppress ERROR!!!"
724*4882a593Smuzhiyun 				" Stop using it\n",
725*4882a593Smuzhiyun 				__FUNCTION__, __LINE__));
726*4882a593Smuzhiyun 			dhd_tcpack_suppress_set(dhdp, TCPACK_SUP_OFF);
727*4882a593Smuzhiyun 		}
728*4882a593Smuzhiyun #endif /* DHDTCPACK_SUPPRESS */
729*4882a593Smuzhiyun 	}
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	if (bPktInQ) {
732*4882a593Smuzhiyun 		ctx->pkt_cnt_in_q[DHD_PKTTAG_IF(PKTTAG(p))][prec>>1]--;
733*4882a593Smuzhiyun 		ctx->pkt_cnt_per_ac[prec>>1]--;
734*4882a593Smuzhiyun 		ctx->pkt_cnt_in_psq--;
735*4882a593Smuzhiyun 	}
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	ctx->pkt_cnt_in_drv[DHD_PKTTAG_IF(PKTTAG(p))][DHD_PKTTAG_FIFO(PKTTAG(p))]--;
738*4882a593Smuzhiyun 	ctx->stats.pktout++;
739*4882a593Smuzhiyun 	ctx->stats.drop_pkts[prec]++;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	dhd_txcomplete(dhdp, p, FALSE);
742*4882a593Smuzhiyun 	PKTFREE(ctx->osh, p, TRUE);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	return 0;
745*4882a593Smuzhiyun } /* _dhd_wlfc_prec_drop */
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun /**
748*4882a593Smuzhiyun  * Called when eg the host handed a new packet over to the driver, or when the dongle reported
749*4882a593Smuzhiyun  * that a packet could currently not be transmitted (=suppressed). This function enqueues a transmit
750*4882a593Smuzhiyun  * packet in the host driver to be (re)transmitted at a later opportunity.
751*4882a593Smuzhiyun  *     @param[in] dhdp pointer to public DHD structure
752*4882a593Smuzhiyun  *     @param[in] qHead When TRUE, queue packet at head instead of tail, to preserve d11 sequence
753*4882a593Smuzhiyun  */
754*4882a593Smuzhiyun static bool
_dhd_wlfc_prec_enq_with_drop(dhd_pub_t * dhdp,struct pktq * pq,void * pkt,int prec,bool qHead,uint8 current_seq)755*4882a593Smuzhiyun _dhd_wlfc_prec_enq_with_drop(dhd_pub_t *dhdp, struct pktq *pq, void *pkt, int prec, bool qHead,
756*4882a593Smuzhiyun 	uint8 current_seq)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun 	void *p = NULL;
759*4882a593Smuzhiyun 	int eprec = -1;		/* precedence to evict from */
760*4882a593Smuzhiyun 	athost_wl_status_info_t* ctx;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 	ASSERT(dhdp && pq && pkt);
763*4882a593Smuzhiyun 	ASSERT(prec >= 0 && prec < pq->num_prec);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	ctx = (athost_wl_status_info_t*)dhdp->wlfc_state;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	/* Fast case, precedence queue is not full and we are also not
768*4882a593Smuzhiyun 	 * exceeding total queue length
769*4882a593Smuzhiyun 	 */
770*4882a593Smuzhiyun 	if (!pktqprec_full(pq, prec) && !pktq_full(pq)) {
771*4882a593Smuzhiyun 		goto exit;
772*4882a593Smuzhiyun 	}
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	/* Determine precedence from which to evict packet, if any */
775*4882a593Smuzhiyun 	if (pktqprec_full(pq, prec)) {
776*4882a593Smuzhiyun 		eprec = prec;
777*4882a593Smuzhiyun 	} else if (pktq_full(pq)) {
778*4882a593Smuzhiyun 		p = pktq_peek_tail(pq, &eprec);
779*4882a593Smuzhiyun 		if (!p) {
780*4882a593Smuzhiyun 			DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
781*4882a593Smuzhiyun 			return FALSE;
782*4882a593Smuzhiyun 		}
783*4882a593Smuzhiyun 		if ((eprec > prec) || (eprec < 0)) {
784*4882a593Smuzhiyun 			if (!pktqprec_empty(pq, prec)) {
785*4882a593Smuzhiyun 				eprec = prec;
786*4882a593Smuzhiyun 			} else {
787*4882a593Smuzhiyun 				return FALSE;
788*4882a593Smuzhiyun 			}
789*4882a593Smuzhiyun 		}
790*4882a593Smuzhiyun 	}
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	/* Evict if needed */
793*4882a593Smuzhiyun 	if (eprec >= 0) {
794*4882a593Smuzhiyun 		/* Detect queueing to unconfigured precedence */
795*4882a593Smuzhiyun 		ASSERT(!pktqprec_empty(pq, eprec));
796*4882a593Smuzhiyun 		/* Evict all fragmented frames */
797*4882a593Smuzhiyun 		dhd_prec_drop_pkts(dhdp, pq, eprec, _dhd_wlfc_prec_drop);
798*4882a593Smuzhiyun 	}
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun exit:
801*4882a593Smuzhiyun 	/* Enqueue */
802*4882a593Smuzhiyun 	_dhd_wlfc_prec_enque(pq, prec, pkt, qHead, current_seq,
803*4882a593Smuzhiyun 		WLFC_GET_REORDERSUPP(dhdp->wlfc_mode));
804*4882a593Smuzhiyun 	ctx->pkt_cnt_in_q[DHD_PKTTAG_IF(PKTTAG(pkt))][prec>>1]++;
805*4882a593Smuzhiyun 	ctx->pkt_cnt_per_ac[prec>>1]++;
806*4882a593Smuzhiyun 	ctx->pkt_cnt_in_psq++;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 	return TRUE;
809*4882a593Smuzhiyun } /* _dhd_wlfc_prec_enq_with_drop */
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun /**
812*4882a593Smuzhiyun  * Called during eg the 'committing' of a transmit packet from the OS layer to a lower layer, in
813*4882a593Smuzhiyun  * the event that this 'commit' failed.
814*4882a593Smuzhiyun  */
815*4882a593Smuzhiyun static int
_dhd_wlfc_rollback_packet_toq(athost_wl_status_info_t * ctx,void * p,ewlfc_packet_state_t pkt_type,uint32 hslot)816*4882a593Smuzhiyun _dhd_wlfc_rollback_packet_toq(athost_wl_status_info_t* ctx,
817*4882a593Smuzhiyun 	void* p, ewlfc_packet_state_t pkt_type, uint32 hslot)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 	/*
820*4882a593Smuzhiyun 	 * put the packet back to the head of queue
821*4882a593Smuzhiyun 	 * - suppressed packet goes back to suppress sub-queue
822*4882a593Smuzhiyun 	 * - pull out the header, if new or delayed packet
823*4882a593Smuzhiyun 	 *
824*4882a593Smuzhiyun 	 * Note: hslot is used only when header removal is done.
825*4882a593Smuzhiyun 	 */
826*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry;
827*4882a593Smuzhiyun 	int rc = BCME_OK;
828*4882a593Smuzhiyun 	int prec, fifo_id;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	entry = _dhd_wlfc_find_table_entry(ctx, p);
831*4882a593Smuzhiyun 	prec = DHD_PKTTAG_FIFO(PKTTAG(p));
832*4882a593Smuzhiyun 	fifo_id = prec << 1;
833*4882a593Smuzhiyun 	if (pkt_type == eWLFC_PKTTYPE_SUPPRESSED)
834*4882a593Smuzhiyun 		fifo_id += 1;
835*4882a593Smuzhiyun 	if (entry != NULL) {
836*4882a593Smuzhiyun 		/*
837*4882a593Smuzhiyun 		if this packet did not count against FIFO credit, it must have
838*4882a593Smuzhiyun 		taken a requested_credit from the firmware (for pspoll etc.)
839*4882a593Smuzhiyun 		*/
840*4882a593Smuzhiyun 		if ((prec != AC_COUNT) && !DHD_PKTTAG_CREDITCHECK(PKTTAG(p)))
841*4882a593Smuzhiyun 			entry->requested_credit++;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 		if (pkt_type == eWLFC_PKTTYPE_DELAYED) {
844*4882a593Smuzhiyun 			/* decrement sequence count */
845*4882a593Smuzhiyun 			WLFC_DECR_SEQCOUNT(entry, prec);
846*4882a593Smuzhiyun 			/* remove header first */
847*4882a593Smuzhiyun 			rc = _dhd_wlfc_pullheader(ctx, p);
848*4882a593Smuzhiyun 			if (rc != BCME_OK) {
849*4882a593Smuzhiyun 				DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
850*4882a593Smuzhiyun 				goto exit;
851*4882a593Smuzhiyun 			}
852*4882a593Smuzhiyun 		}
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 		if (_dhd_wlfc_prec_enq_with_drop(ctx->dhdp, &entry->psq, p, fifo_id, TRUE,
855*4882a593Smuzhiyun 			WLFC_SEQCOUNT(entry, fifo_id>>1))
856*4882a593Smuzhiyun 			== FALSE) {
857*4882a593Smuzhiyun 			/* enque failed */
858*4882a593Smuzhiyun 			DHD_ERROR(("Error: %s():%d, fifo_id(%d)\n",
859*4882a593Smuzhiyun 				__FUNCTION__, __LINE__, fifo_id));
860*4882a593Smuzhiyun 			rc = BCME_ERROR;
861*4882a593Smuzhiyun 		}
862*4882a593Smuzhiyun 	} else {
863*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
864*4882a593Smuzhiyun 		rc = BCME_ERROR;
865*4882a593Smuzhiyun 	}
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun exit:
868*4882a593Smuzhiyun 	if (rc != BCME_OK) {
869*4882a593Smuzhiyun 		ctx->stats.rollback_failed++;
870*4882a593Smuzhiyun 		_dhd_wlfc_prec_drop(ctx->dhdp, fifo_id, p, FALSE);
871*4882a593Smuzhiyun 	} else {
872*4882a593Smuzhiyun 		ctx->stats.rollback++;
873*4882a593Smuzhiyun 	}
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 	return rc;
876*4882a593Smuzhiyun } /* _dhd_wlfc_rollback_packet_toq */
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun /** Returns TRUE if host OS -> DHD flow control is allowed on the caller supplied interface */
879*4882a593Smuzhiyun static bool
_dhd_wlfc_allow_fc(athost_wl_status_info_t * ctx,uint8 ifid)880*4882a593Smuzhiyun _dhd_wlfc_allow_fc(athost_wl_status_info_t* ctx, uint8 ifid)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun 	int prec, ac_traffic = WLFC_NO_TRAFFIC;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	for (prec = 0; prec < AC_COUNT; prec++) {
885*4882a593Smuzhiyun 		if (ctx->pkt_cnt_in_drv[ifid][prec] > 0) {
886*4882a593Smuzhiyun 			if (ac_traffic == WLFC_NO_TRAFFIC)
887*4882a593Smuzhiyun 				ac_traffic = prec + 1;
888*4882a593Smuzhiyun 			else if (ac_traffic != (prec + 1))
889*4882a593Smuzhiyun 				ac_traffic = WLFC_MULTI_TRAFFIC;
890*4882a593Smuzhiyun 		}
891*4882a593Smuzhiyun 	}
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	if (ac_traffic >= 1 && ac_traffic <= AC_COUNT) {
894*4882a593Smuzhiyun 		/* single AC (BE/BK/VI/VO) in queue */
895*4882a593Smuzhiyun 		if (ctx->allow_fc) {
896*4882a593Smuzhiyun 			return TRUE;
897*4882a593Smuzhiyun 		} else {
898*4882a593Smuzhiyun 			uint32 delta;
899*4882a593Smuzhiyun 			uint32 curr_t = OSL_SYSUPTIME();
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 			if (ctx->fc_defer_timestamp == 0) {
902*4882a593Smuzhiyun 				/* first single ac scenario */
903*4882a593Smuzhiyun 				ctx->fc_defer_timestamp = curr_t;
904*4882a593Smuzhiyun 				return FALSE;
905*4882a593Smuzhiyun 			}
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 			/* single AC duration, this handles wrap around, e.g. 1 - ~0 = 2. */
908*4882a593Smuzhiyun 			delta = curr_t - ctx->fc_defer_timestamp;
909*4882a593Smuzhiyun 			if (delta >= WLFC_FC_DEFER_PERIOD_MS) {
910*4882a593Smuzhiyun 				ctx->allow_fc = TRUE;
911*4882a593Smuzhiyun 			}
912*4882a593Smuzhiyun 		}
913*4882a593Smuzhiyun 	} else {
914*4882a593Smuzhiyun 		/* multiple ACs or BCMC in queue */
915*4882a593Smuzhiyun 		ctx->allow_fc = FALSE;
916*4882a593Smuzhiyun 		ctx->fc_defer_timestamp = 0;
917*4882a593Smuzhiyun 	}
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	return ctx->allow_fc;
920*4882a593Smuzhiyun } /* _dhd_wlfc_allow_fc */
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun /**
923*4882a593Smuzhiyun  * Starts or stops the flow of transmit packets from the host OS towards the DHD, depending on
924*4882a593Smuzhiyun  * low/high watermarks.
925*4882a593Smuzhiyun  */
926*4882a593Smuzhiyun static void
_dhd_wlfc_flow_control_check(athost_wl_status_info_t * ctx,struct pktq * pq,uint8 if_id)927*4882a593Smuzhiyun _dhd_wlfc_flow_control_check(athost_wl_status_info_t* ctx, struct pktq* pq, uint8 if_id)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun 	dhd_pub_t *dhdp;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	ASSERT(ctx);
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	dhdp = (dhd_pub_t *)ctx->dhdp;
934*4882a593Smuzhiyun 	ASSERT(dhdp);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	if (dhdp->skip_fc && dhdp->skip_fc((void *)dhdp, if_id))
937*4882a593Smuzhiyun 		return;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	if ((ctx->hostif_flow_state[if_id] == OFF) && !_dhd_wlfc_allow_fc(ctx, if_id))
940*4882a593Smuzhiyun 		return;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	if ((pq->n_pkts_tot <= WLFC_FLOWCONTROL_LOWATER) && (ctx->hostif_flow_state[if_id] == ON)) {
943*4882a593Smuzhiyun 		/* start traffic */
944*4882a593Smuzhiyun 		ctx->hostif_flow_state[if_id] = OFF;
945*4882a593Smuzhiyun 		/*
946*4882a593Smuzhiyun 		WLFC_DBGMESG(("qlen:%02d, if:%02d, ->OFF, start traffic %s()\n",
947*4882a593Smuzhiyun 		pq->n_pkts_tot, if_id, __FUNCTION__));
948*4882a593Smuzhiyun 		*/
949*4882a593Smuzhiyun 		WLFC_DBGMESG(("F"));
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 		dhd_txflowcontrol(dhdp, if_id, OFF);
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 		ctx->toggle_host_if = 0;
954*4882a593Smuzhiyun 	}
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun 	if (pq->n_pkts_tot >= WLFC_FLOWCONTROL_HIWATER && ctx->hostif_flow_state[if_id] == OFF) {
957*4882a593Smuzhiyun 		/* stop traffic */
958*4882a593Smuzhiyun 		ctx->hostif_flow_state[if_id] = ON;
959*4882a593Smuzhiyun 		/*
960*4882a593Smuzhiyun 		WLFC_DBGMESG(("qlen:%02d, if:%02d, ->ON, stop traffic   %s()\n",
961*4882a593Smuzhiyun 		pq->n_pkts_tot, if_id, __FUNCTION__));
962*4882a593Smuzhiyun 		*/
963*4882a593Smuzhiyun 		WLFC_DBGMESG(("N"));
964*4882a593Smuzhiyun 
965*4882a593Smuzhiyun 		dhd_txflowcontrol(dhdp, if_id, ON);
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun 		ctx->host_ifidx = if_id;
968*4882a593Smuzhiyun 		ctx->toggle_host_if = 1;
969*4882a593Smuzhiyun 	}
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	return;
972*4882a593Smuzhiyun } /* _dhd_wlfc_flow_control_check */
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun static int
_dhd_wlfc_send_signalonly_packet(athost_wl_status_info_t * ctx,wlfc_mac_descriptor_t * entry,uint8 ta_bmp)975*4882a593Smuzhiyun _dhd_wlfc_send_signalonly_packet(athost_wl_status_info_t* ctx, wlfc_mac_descriptor_t* entry,
976*4882a593Smuzhiyun 	uint8 ta_bmp)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun 	int rc = BCME_OK;
979*4882a593Smuzhiyun 	void* p = NULL;
980*4882a593Smuzhiyun 	int dummylen = ((dhd_pub_t *)ctx->dhdp)->hdrlen+ 16;
981*4882a593Smuzhiyun 	dhd_pub_t *dhdp = (dhd_pub_t *)ctx->dhdp;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	if (dhdp->proptxstatus_txoff) {
984*4882a593Smuzhiyun 		rc = BCME_NORESOURCE;
985*4882a593Smuzhiyun 		return rc;
986*4882a593Smuzhiyun 	}
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	/* allocate a dummy packet */
989*4882a593Smuzhiyun 	p = PKTGET(ctx->osh, dummylen, TRUE);
990*4882a593Smuzhiyun 	if (p) {
991*4882a593Smuzhiyun 		PKTPULL(ctx->osh, p, dummylen);
992*4882a593Smuzhiyun 		DHD_PKTTAG_SET_H2DTAG(PKTTAG(p), 0);
993*4882a593Smuzhiyun 		_dhd_wlfc_pushheader(ctx, &p, TRUE, ta_bmp, entry->mac_handle, 0, 0, FALSE);
994*4882a593Smuzhiyun 		DHD_PKTTAG_SETSIGNALONLY(PKTTAG(p), 1);
995*4882a593Smuzhiyun 		DHD_PKTTAG_WLFCPKT_SET(PKTTAG(p), 1);
996*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
997*4882a593Smuzhiyun 		ctx->stats.signal_only_pkts_sent++;
998*4882a593Smuzhiyun #endif // endif
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun #if defined(BCMPCIE)
1001*4882a593Smuzhiyun 		rc = dhd_bus_txdata(dhdp->bus, p, ctx->host_ifidx);
1002*4882a593Smuzhiyun #else
1003*4882a593Smuzhiyun 		rc = dhd_bus_txdata(dhdp->bus, p);
1004*4882a593Smuzhiyun #endif // endif
1005*4882a593Smuzhiyun 		if (rc != BCME_OK) {
1006*4882a593Smuzhiyun 			_dhd_wlfc_pullheader(ctx, p);
1007*4882a593Smuzhiyun 			PKTFREE(ctx->osh, p, TRUE);
1008*4882a593Smuzhiyun 		}
1009*4882a593Smuzhiyun 	} else {
1010*4882a593Smuzhiyun 		DHD_ERROR(("%s: couldn't allocate new %d-byte packet\n",
1011*4882a593Smuzhiyun 		           __FUNCTION__, dummylen));
1012*4882a593Smuzhiyun 		rc = BCME_NOMEM;
1013*4882a593Smuzhiyun 		dhdp->tx_pktgetfail++;
1014*4882a593Smuzhiyun 	}
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	return rc;
1017*4882a593Smuzhiyun } /* _dhd_wlfc_send_signalonly_packet */
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun /**
1020*4882a593Smuzhiyun  * Called on eg receiving 'mac close' indication from dongle. Updates the per-MAC administration
1021*4882a593Smuzhiyun  * maintained in caller supplied parameter 'entry'.
1022*4882a593Smuzhiyun  *
1023*4882a593Smuzhiyun  *    @param[in/out] entry  administration about a remote MAC entity
1024*4882a593Smuzhiyun  *    @param[in]     prec   precedence queue for this remote MAC entitity
1025*4882a593Smuzhiyun  *
1026*4882a593Smuzhiyun  * Return value: TRUE if traffic availability changed
1027*4882a593Smuzhiyun  */
1028*4882a593Smuzhiyun static bool
_dhd_wlfc_traffic_pending_check(athost_wl_status_info_t * ctx,wlfc_mac_descriptor_t * entry,int prec)1029*4882a593Smuzhiyun _dhd_wlfc_traffic_pending_check(athost_wl_status_info_t* ctx, wlfc_mac_descriptor_t* entry,
1030*4882a593Smuzhiyun 	int prec)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun 	bool rc = FALSE;
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	if (entry->state == WLFC_STATE_CLOSE) {
1035*4882a593Smuzhiyun 		if ((pktqprec_n_pkts(&entry->psq, (prec << 1)) == 0) &&
1036*4882a593Smuzhiyun 			(pktqprec_n_pkts(&entry->psq, ((prec << 1) + 1)) == 0)) {
1037*4882a593Smuzhiyun 			/* no packets in both 'normal' and 'suspended' queues */
1038*4882a593Smuzhiyun 			if (entry->traffic_pending_bmp & NBITVAL(prec)) {
1039*4882a593Smuzhiyun 				rc = TRUE;
1040*4882a593Smuzhiyun 				entry->traffic_pending_bmp =
1041*4882a593Smuzhiyun 					entry->traffic_pending_bmp & ~ NBITVAL(prec);
1042*4882a593Smuzhiyun 			}
1043*4882a593Smuzhiyun 		} else {
1044*4882a593Smuzhiyun 			/* packets are queued in host for transmission to dongle */
1045*4882a593Smuzhiyun 			if (!(entry->traffic_pending_bmp & NBITVAL(prec))) {
1046*4882a593Smuzhiyun 				rc = TRUE;
1047*4882a593Smuzhiyun 				entry->traffic_pending_bmp =
1048*4882a593Smuzhiyun 					entry->traffic_pending_bmp | NBITVAL(prec);
1049*4882a593Smuzhiyun 			}
1050*4882a593Smuzhiyun 		}
1051*4882a593Smuzhiyun 	}
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	if (rc) {
1054*4882a593Smuzhiyun 		/* request a TIM update to firmware at the next piggyback opportunity */
1055*4882a593Smuzhiyun 		if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp) {
1056*4882a593Smuzhiyun 			entry->send_tim_signal = 1;
1057*4882a593Smuzhiyun 			_dhd_wlfc_send_signalonly_packet(ctx, entry, entry->traffic_pending_bmp);
1058*4882a593Smuzhiyun 			entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
1059*4882a593Smuzhiyun 			entry->send_tim_signal = 0;
1060*4882a593Smuzhiyun 		} else {
1061*4882a593Smuzhiyun 			rc = FALSE;
1062*4882a593Smuzhiyun 		}
1063*4882a593Smuzhiyun 	}
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	return rc;
1066*4882a593Smuzhiyun } /* _dhd_wlfc_traffic_pending_check */
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun /**
1069*4882a593Smuzhiyun  * Called on receiving a 'd11 suppressed' or 'wl suppressed' tx status from the firmware. Enqueues
1070*4882a593Smuzhiyun  * the packet to transmit to firmware again at a later opportunity.
1071*4882a593Smuzhiyun  */
1072*4882a593Smuzhiyun static int
_dhd_wlfc_enque_suppressed(athost_wl_status_info_t * ctx,int prec,void * p)1073*4882a593Smuzhiyun _dhd_wlfc_enque_suppressed(athost_wl_status_info_t* ctx, int prec, void* p)
1074*4882a593Smuzhiyun {
1075*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry;
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun 	entry = _dhd_wlfc_find_table_entry(ctx, p);
1078*4882a593Smuzhiyun 	if (entry == NULL) {
1079*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
1080*4882a593Smuzhiyun 		return BCME_NOTFOUND;
1081*4882a593Smuzhiyun 	}
1082*4882a593Smuzhiyun 	/*
1083*4882a593Smuzhiyun 	- suppressed packets go to sub_queue[2*prec + 1] AND
1084*4882a593Smuzhiyun 	- delayed packets go to sub_queue[2*prec + 0] to ensure
1085*4882a593Smuzhiyun 	order of delivery.
1086*4882a593Smuzhiyun 	*/
1087*4882a593Smuzhiyun 	if (_dhd_wlfc_prec_enq_with_drop(ctx->dhdp, &entry->psq, p, ((prec << 1) + 1), FALSE,
1088*4882a593Smuzhiyun 		WLFC_SEQCOUNT(entry, prec))
1089*4882a593Smuzhiyun 		== FALSE) {
1090*4882a593Smuzhiyun 		ctx->stats.delayq_full_error++;
1091*4882a593Smuzhiyun 		/* WLFC_DBGMESG(("Error: %s():%d\n", __FUNCTION__, __LINE__)); */
1092*4882a593Smuzhiyun 		WLFC_DBGMESG(("s"));
1093*4882a593Smuzhiyun 		return BCME_ERROR;
1094*4882a593Smuzhiyun 	}
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun 	/* A packet has been pushed, update traffic availability bitmap, if applicable */
1097*4882a593Smuzhiyun 	_dhd_wlfc_traffic_pending_check(ctx, entry, prec);
1098*4882a593Smuzhiyun 	_dhd_wlfc_flow_control_check(ctx, &entry->psq, DHD_PKTTAG_IF(PKTTAG(p)));
1099*4882a593Smuzhiyun 	return BCME_OK;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun /**
1103*4882a593Smuzhiyun  * Called when a transmit packet is about to be 'committed' from the OS layer to a lower layer
1104*4882a593Smuzhiyun  * towards the dongle (eg the DBUS layer). Updates wlfc administration. May modify packet.
1105*4882a593Smuzhiyun  *
1106*4882a593Smuzhiyun  *     @param[in/out] ctx    driver specific flow control administration
1107*4882a593Smuzhiyun  *     @param[in/out] entry  The remote MAC entity for which the packet is destined.
1108*4882a593Smuzhiyun  *     @param[in/out] packet Packet to send. This function optionally adds TLVs to the packet.
1109*4882a593Smuzhiyun  *     @param[in] header_needed True if packet is 'new' to flow control
1110*4882a593Smuzhiyun  *     @param[out] slot Handle to container in which the packet was 'parked'
1111*4882a593Smuzhiyun  */
1112*4882a593Smuzhiyun static int
_dhd_wlfc_pretx_pktprocess(athost_wl_status_info_t * ctx,wlfc_mac_descriptor_t * entry,void ** packet,int header_needed,uint32 * slot)1113*4882a593Smuzhiyun _dhd_wlfc_pretx_pktprocess(athost_wl_status_info_t* ctx,
1114*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry, void** packet, int header_needed, uint32* slot)
1115*4882a593Smuzhiyun {
1116*4882a593Smuzhiyun 	int rc = BCME_OK;
1117*4882a593Smuzhiyun 	int hslot = WLFC_HANGER_MAXITEMS;
1118*4882a593Smuzhiyun 	bool send_tim_update = FALSE;
1119*4882a593Smuzhiyun 	uint32 htod = 0;
1120*4882a593Smuzhiyun 	uint16 htodseq = 0;
1121*4882a593Smuzhiyun 	uint8 free_ctr;
1122*4882a593Smuzhiyun 	int gen = 0xff;
1123*4882a593Smuzhiyun 	dhd_pub_t *dhdp = (dhd_pub_t *)ctx->dhdp;
1124*4882a593Smuzhiyun 	void * p = *packet;
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	*slot = hslot;
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	if (entry == NULL) {
1129*4882a593Smuzhiyun 		entry = _dhd_wlfc_find_table_entry(ctx, p);
1130*4882a593Smuzhiyun 	}
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	if (entry == NULL) {
1133*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
1134*4882a593Smuzhiyun 		return BCME_ERROR;
1135*4882a593Smuzhiyun 	}
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun 	if (entry->send_tim_signal) {
1138*4882a593Smuzhiyun 		/* sends a traffic indication bitmap to the dongle */
1139*4882a593Smuzhiyun 		send_tim_update = TRUE;
1140*4882a593Smuzhiyun 		entry->send_tim_signal = 0;
1141*4882a593Smuzhiyun 		entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
1142*4882a593Smuzhiyun 	}
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun 	if (header_needed) {
1145*4882a593Smuzhiyun 		if (WLFC_GET_AFQ(dhdp->wlfc_mode)) {
1146*4882a593Smuzhiyun 			hslot = (uint)(entry - &ctx->destination_entries.nodes[0]);
1147*4882a593Smuzhiyun 		} else {
1148*4882a593Smuzhiyun 			hslot = _dhd_wlfc_hanger_get_free_slot(ctx->hanger);
1149*4882a593Smuzhiyun 		}
1150*4882a593Smuzhiyun 		gen = entry->generation;
1151*4882a593Smuzhiyun 		free_ctr = WLFC_SEQCOUNT(entry, DHD_PKTTAG_FIFO(PKTTAG(p)));
1152*4882a593Smuzhiyun 	} else {
1153*4882a593Smuzhiyun 		if (WLFC_GET_REUSESEQ(dhdp->wlfc_mode)) {
1154*4882a593Smuzhiyun 			htodseq = DHD_PKTTAG_H2DSEQ(PKTTAG(p));
1155*4882a593Smuzhiyun 		}
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 		hslot = WL_TXSTATUS_GET_HSLOT(DHD_PKTTAG_H2DTAG(PKTTAG(p)));
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 		if (WLFC_GET_REORDERSUPP(dhdp->wlfc_mode)) {
1160*4882a593Smuzhiyun 			gen = entry->generation;
1161*4882a593Smuzhiyun 		} else if (WLFC_GET_AFQ(dhdp->wlfc_mode)) {
1162*4882a593Smuzhiyun 			gen = WL_TXSTATUS_GET_GENERATION(DHD_PKTTAG_H2DTAG(PKTTAG(p)));
1163*4882a593Smuzhiyun 		} else {
1164*4882a593Smuzhiyun 			_dhd_wlfc_hanger_get_genbit(ctx->hanger, p, hslot, &gen);
1165*4882a593Smuzhiyun 		}
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 		free_ctr = WL_TXSTATUS_GET_FREERUNCTR(DHD_PKTTAG_H2DTAG(PKTTAG(p)));
1168*4882a593Smuzhiyun 		/* remove old header */
1169*4882a593Smuzhiyun 		_dhd_wlfc_pullheader(ctx, p);
1170*4882a593Smuzhiyun 	}
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	if (hslot >= WLFC_HANGER_MAXITEMS) {
1173*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():no hanger slot available\n", __FUNCTION__));
1174*4882a593Smuzhiyun 		return BCME_ERROR;
1175*4882a593Smuzhiyun 	}
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	WL_TXSTATUS_SET_FREERUNCTR(htod, free_ctr);
1178*4882a593Smuzhiyun 	WL_TXSTATUS_SET_HSLOT(htod, hslot);
1179*4882a593Smuzhiyun 	WL_TXSTATUS_SET_FIFO(htod, DHD_PKTTAG_FIFO(PKTTAG(p)));
1180*4882a593Smuzhiyun 	WL_TXSTATUS_SET_FLAGS(htod, WLFC_PKTFLAG_PKTFROMHOST);
1181*4882a593Smuzhiyun 	WL_TXSTATUS_SET_GENERATION(htod, gen);
1182*4882a593Smuzhiyun 	DHD_PKTTAG_SETPKTDIR(PKTTAG(p), 1);
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	if (!DHD_PKTTAG_CREDITCHECK(PKTTAG(p))) {
1185*4882a593Smuzhiyun 		/*
1186*4882a593Smuzhiyun 		Indicate that this packet is being sent in response to an
1187*4882a593Smuzhiyun 		explicit request from the firmware side.
1188*4882a593Smuzhiyun 		*/
1189*4882a593Smuzhiyun 		WLFC_PKTFLAG_SET_PKTREQUESTED(htod);
1190*4882a593Smuzhiyun 	} else {
1191*4882a593Smuzhiyun 		WLFC_PKTFLAG_CLR_PKTREQUESTED(htod);
1192*4882a593Smuzhiyun 	}
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	rc = _dhd_wlfc_pushheader(ctx, &p, send_tim_update,
1195*4882a593Smuzhiyun 		entry->traffic_lastreported_bmp, entry->mac_handle, htod, htodseq, FALSE);
1196*4882a593Smuzhiyun 	if (rc == BCME_OK) {
1197*4882a593Smuzhiyun 		DHD_PKTTAG_SET_H2DTAG(PKTTAG(p), htod);
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 		if (!WLFC_GET_AFQ(dhdp->wlfc_mode)) {
1200*4882a593Smuzhiyun 			wlfc_hanger_t *h = (wlfc_hanger_t*)(ctx->hanger);
1201*4882a593Smuzhiyun 			if (header_needed) {
1202*4882a593Smuzhiyun 				/*
1203*4882a593Smuzhiyun 				a new header was created for this packet.
1204*4882a593Smuzhiyun 				push to hanger slot and scrub q. Since bus
1205*4882a593Smuzhiyun 				send succeeded, increment seq number as well.
1206*4882a593Smuzhiyun 				*/
1207*4882a593Smuzhiyun 				rc = _dhd_wlfc_hanger_pushpkt(ctx->hanger, p, hslot);
1208*4882a593Smuzhiyun 				if (rc == BCME_OK) {
1209*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
1210*4882a593Smuzhiyun 					h->items[hslot].push_time =
1211*4882a593Smuzhiyun 						OSL_SYSUPTIME();
1212*4882a593Smuzhiyun #endif // endif
1213*4882a593Smuzhiyun 				} else {
1214*4882a593Smuzhiyun 					DHD_ERROR(("%s() hanger_pushpkt() failed, rc: %d\n",
1215*4882a593Smuzhiyun 						__FUNCTION__, rc));
1216*4882a593Smuzhiyun 				}
1217*4882a593Smuzhiyun 			} else {
1218*4882a593Smuzhiyun 				/* clear hanger state */
1219*4882a593Smuzhiyun 				if (((wlfc_hanger_t*)(ctx->hanger))->items[hslot].pkt != p)
1220*4882a593Smuzhiyun 					DHD_ERROR(("%s() pkt not match: cur %p, hanger pkt %p\n",
1221*4882a593Smuzhiyun 						__FUNCTION__, p, h->items[hslot].pkt));
1222*4882a593Smuzhiyun 				ASSERT(h->items[hslot].pkt == p);
1223*4882a593Smuzhiyun 				bcm_object_feature_set(h->items[hslot].pkt,
1224*4882a593Smuzhiyun 					BCM_OBJECT_FEATURE_PKT_STATE, 0);
1225*4882a593Smuzhiyun 				h->items[hslot].pkt_state = 0;
1226*4882a593Smuzhiyun 				h->items[hslot].pkt_txstatus = 0;
1227*4882a593Smuzhiyun 				h->items[hslot].state = WLFC_HANGER_ITEM_STATE_INUSE;
1228*4882a593Smuzhiyun 			}
1229*4882a593Smuzhiyun 		}
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 		if ((rc == BCME_OK) && header_needed) {
1232*4882a593Smuzhiyun 			/* increment free running sequence count */
1233*4882a593Smuzhiyun 			WLFC_INCR_SEQCOUNT(entry, DHD_PKTTAG_FIFO(PKTTAG(p)));
1234*4882a593Smuzhiyun 		}
1235*4882a593Smuzhiyun 	}
1236*4882a593Smuzhiyun 	*slot = hslot;
1237*4882a593Smuzhiyun 	*packet = p;
1238*4882a593Smuzhiyun 	return rc;
1239*4882a593Smuzhiyun } /* _dhd_wlfc_pretx_pktprocess */
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun /**
1242*4882a593Smuzhiyun  * A remote wireless mac may be temporarily 'closed' due to power management. Returns '1' if remote
1243*4882a593Smuzhiyun  * mac is in the 'open' state, otherwise '0'.
1244*4882a593Smuzhiyun  */
1245*4882a593Smuzhiyun static int
_dhd_wlfc_is_destination_open(athost_wl_status_info_t * ctx,wlfc_mac_descriptor_t * entry,int prec)1246*4882a593Smuzhiyun _dhd_wlfc_is_destination_open(athost_wl_status_info_t* ctx,
1247*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry, int prec)
1248*4882a593Smuzhiyun {
1249*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* interfaces = ctx->destination_entries.interfaces;
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 	if (entry->interface_id >= WLFC_MAX_IFNUM) {
1252*4882a593Smuzhiyun 		ASSERT(&ctx->destination_entries.other == entry);
1253*4882a593Smuzhiyun 		return 1;
1254*4882a593Smuzhiyun 	}
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	if (interfaces[entry->interface_id].iftype ==
1257*4882a593Smuzhiyun 		WLC_E_IF_ROLE_P2P_GO) {
1258*4882a593Smuzhiyun 		/* - destination interface is of type p2p GO.
1259*4882a593Smuzhiyun 		For a p2pGO interface, if the destination is OPEN but the interface is
1260*4882a593Smuzhiyun 		CLOSEd, do not send traffic. But if the dstn is CLOSEd while there is
1261*4882a593Smuzhiyun 		destination-specific-credit left send packets. This is because the
1262*4882a593Smuzhiyun 		firmware storing the destination-specific-requested packet in queue.
1263*4882a593Smuzhiyun 		*/
1264*4882a593Smuzhiyun 		if ((entry->state == WLFC_STATE_CLOSE) && (entry->requested_credit == 0) &&
1265*4882a593Smuzhiyun 			(entry->requested_packet == 0)) {
1266*4882a593Smuzhiyun 			return 0;
1267*4882a593Smuzhiyun 		}
1268*4882a593Smuzhiyun 	}
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	/* AP, p2p_go -> unicast desc entry, STA/p2p_cl -> interface desc. entry */
1271*4882a593Smuzhiyun 	if ((((entry->state == WLFC_STATE_CLOSE) ||
1272*4882a593Smuzhiyun 		(interfaces[entry->interface_id].state == WLFC_STATE_CLOSE)) &&
1273*4882a593Smuzhiyun 		(entry->requested_credit == 0) &&
1274*4882a593Smuzhiyun 		(entry->requested_packet == 0)) ||
1275*4882a593Smuzhiyun 		(!(entry->ac_bitmap & (1 << prec)))) {
1276*4882a593Smuzhiyun 		return 0;
1277*4882a593Smuzhiyun 	}
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	return 1;
1280*4882a593Smuzhiyun } /* _dhd_wlfc_is_destination_open */
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun /**
1283*4882a593Smuzhiyun  * Dequeues a suppressed or delayed packet from a queue
1284*4882a593Smuzhiyun  *    @param[in/out] ctx          Driver specific flow control administration
1285*4882a593Smuzhiyun  *    @param[in]  prec            Precedence of queue to dequeue from
1286*4882a593Smuzhiyun  *    @param[out] ac_credit_spent Boolean, returns 0 or 1
1287*4882a593Smuzhiyun  *    @param[out] needs_hdr       Boolean, returns 0 or 1
1288*4882a593Smuzhiyun  *    @param[out] entry_out       The remote MAC for which the packet is destined
1289*4882a593Smuzhiyun  *    @param[in]  only_no_credit  If TRUE, searches all entries instead of just the active ones
1290*4882a593Smuzhiyun  *
1291*4882a593Smuzhiyun  * Return value: the dequeued packet
1292*4882a593Smuzhiyun  */
1293*4882a593Smuzhiyun static void*
_dhd_wlfc_deque_delayedq(athost_wl_status_info_t * ctx,int prec,uint8 * ac_credit_spent,uint8 * needs_hdr,wlfc_mac_descriptor_t ** entry_out,bool only_no_credit)1294*4882a593Smuzhiyun _dhd_wlfc_deque_delayedq(athost_wl_status_info_t* ctx, int prec,
1295*4882a593Smuzhiyun 	uint8* ac_credit_spent, uint8* needs_hdr, wlfc_mac_descriptor_t** entry_out,
1296*4882a593Smuzhiyun 	bool only_no_credit)
1297*4882a593Smuzhiyun {
1298*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry;
1299*4882a593Smuzhiyun 	int total_entries;
1300*4882a593Smuzhiyun 	void* p = NULL;
1301*4882a593Smuzhiyun 	int i;
1302*4882a593Smuzhiyun 	uint8 credit_spent = ((prec == AC_COUNT) && !ctx->bcmc_credit_supported) ? 0 : 1;
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	*entry_out = NULL;
1305*4882a593Smuzhiyun 	/* most cases a packet will count against FIFO credit */
1306*4882a593Smuzhiyun 	*ac_credit_spent = credit_spent;
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	/* search all entries, include nodes as well as interfaces */
1309*4882a593Smuzhiyun 	if (only_no_credit) {
1310*4882a593Smuzhiyun 		total_entries = ctx->requested_entry_count;
1311*4882a593Smuzhiyun 	} else {
1312*4882a593Smuzhiyun 		total_entries = ctx->active_entry_count;
1313*4882a593Smuzhiyun 	}
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	for (i = 0; i < total_entries; i++) {
1316*4882a593Smuzhiyun 		if (only_no_credit) {
1317*4882a593Smuzhiyun 			entry = ctx->requested_entry[i];
1318*4882a593Smuzhiyun 		} else {
1319*4882a593Smuzhiyun 			entry = ctx->active_entry_head;
1320*4882a593Smuzhiyun 			/* move head to ensure fair round-robin */
1321*4882a593Smuzhiyun 			ctx->active_entry_head = ctx->active_entry_head->next;
1322*4882a593Smuzhiyun 		}
1323*4882a593Smuzhiyun 		ASSERT(entry);
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 		if (entry->occupied && _dhd_wlfc_is_destination_open(ctx, entry, prec) &&
1326*4882a593Smuzhiyun 			(entry->transit_count < WL_TXSTATUS_FREERUNCTR_MASK) &&
1327*4882a593Smuzhiyun 			(!entry->suppressed)) {
1328*4882a593Smuzhiyun 			*ac_credit_spent = credit_spent;
1329*4882a593Smuzhiyun 			if (entry->state == WLFC_STATE_CLOSE) {
1330*4882a593Smuzhiyun 				*ac_credit_spent = 0;
1331*4882a593Smuzhiyun 			}
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun 			/* higher precedence will be picked up first,
1334*4882a593Smuzhiyun 			 * i.e. suppressed packets before delayed ones
1335*4882a593Smuzhiyun 			 */
1336*4882a593Smuzhiyun 			p = pktq_pdeq(&entry->psq, PSQ_SUP_IDX(prec));
1337*4882a593Smuzhiyun 			*needs_hdr = 0;
1338*4882a593Smuzhiyun 			if (p == NULL) {
1339*4882a593Smuzhiyun 				/* De-Q from delay Q */
1340*4882a593Smuzhiyun 				p = pktq_pdeq(&entry->psq, PSQ_DLY_IDX(prec));
1341*4882a593Smuzhiyun 				*needs_hdr = 1;
1342*4882a593Smuzhiyun 			}
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun 			if (p != NULL) {
1345*4882a593Smuzhiyun 				bcm_pkt_validate_chk(p);
1346*4882a593Smuzhiyun 				/* did the packet come from suppress sub-queue? */
1347*4882a593Smuzhiyun 				if (entry->requested_credit > 0) {
1348*4882a593Smuzhiyun 					entry->requested_credit--;
1349*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
1350*4882a593Smuzhiyun 					entry->dstncredit_sent_packets++;
1351*4882a593Smuzhiyun #endif // endif
1352*4882a593Smuzhiyun 				} else if (entry->requested_packet > 0) {
1353*4882a593Smuzhiyun 					entry->requested_packet--;
1354*4882a593Smuzhiyun 					DHD_PKTTAG_SETONETIMEPKTRQST(PKTTAG(p));
1355*4882a593Smuzhiyun 				}
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun 				*entry_out = entry;
1358*4882a593Smuzhiyun 				ctx->pkt_cnt_in_q[DHD_PKTTAG_IF(PKTTAG(p))][prec]--;
1359*4882a593Smuzhiyun 				ctx->pkt_cnt_per_ac[prec]--;
1360*4882a593Smuzhiyun 				ctx->pkt_cnt_in_psq--;
1361*4882a593Smuzhiyun 				_dhd_wlfc_flow_control_check(ctx, &entry->psq,
1362*4882a593Smuzhiyun 					DHD_PKTTAG_IF(PKTTAG(p)));
1363*4882a593Smuzhiyun 				/*
1364*4882a593Smuzhiyun 				 * A packet has been picked up, update traffic availability bitmap,
1365*4882a593Smuzhiyun 				 * if applicable.
1366*4882a593Smuzhiyun 				 */
1367*4882a593Smuzhiyun 				_dhd_wlfc_traffic_pending_check(ctx, entry, prec);
1368*4882a593Smuzhiyun 				return p;
1369*4882a593Smuzhiyun 			}
1370*4882a593Smuzhiyun 		}
1371*4882a593Smuzhiyun 	}
1372*4882a593Smuzhiyun 	return NULL;
1373*4882a593Smuzhiyun } /* _dhd_wlfc_deque_delayedq */
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun /** Enqueues caller supplied packet on either a 'suppressed' or 'delayed' queue */
1376*4882a593Smuzhiyun static int
_dhd_wlfc_enque_delayq(athost_wl_status_info_t * ctx,void * pktbuf,int prec)1377*4882a593Smuzhiyun _dhd_wlfc_enque_delayq(athost_wl_status_info_t* ctx, void* pktbuf, int prec)
1378*4882a593Smuzhiyun {
1379*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry;
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	if (pktbuf != NULL) {
1382*4882a593Smuzhiyun 		entry = _dhd_wlfc_find_table_entry(ctx, pktbuf);
1383*4882a593Smuzhiyun 		if (entry == NULL) {
1384*4882a593Smuzhiyun 			DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
1385*4882a593Smuzhiyun 			return BCME_ERROR;
1386*4882a593Smuzhiyun 		}
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 		/*
1389*4882a593Smuzhiyun 		- suppressed packets go to sub_queue[2*prec + 1] AND
1390*4882a593Smuzhiyun 		- delayed packets go to sub_queue[2*prec + 0] to ensure
1391*4882a593Smuzhiyun 		order of delivery.
1392*4882a593Smuzhiyun 		*/
1393*4882a593Smuzhiyun 		if (_dhd_wlfc_prec_enq_with_drop(ctx->dhdp, &entry->psq, pktbuf, (prec << 1),
1394*4882a593Smuzhiyun 			FALSE, WLFC_SEQCOUNT(entry, prec))
1395*4882a593Smuzhiyun 			== FALSE) {
1396*4882a593Smuzhiyun 			WLFC_DBGMESG(("D"));
1397*4882a593Smuzhiyun 			ctx->stats.delayq_full_error++;
1398*4882a593Smuzhiyun 			return BCME_ERROR;
1399*4882a593Smuzhiyun 		}
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 		/* A packet has been pushed, update traffic availability bitmap, if applicable */
1402*4882a593Smuzhiyun 		_dhd_wlfc_traffic_pending_check(ctx, entry, prec);
1403*4882a593Smuzhiyun 	}
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	return BCME_OK;
1406*4882a593Smuzhiyun } /* _dhd_wlfc_enque_delayq */
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun /** Returns TRUE if caller supplied packet is destined for caller supplied interface */
_dhd_wlfc_ifpkt_fn(void * p,void * p_ifid)1409*4882a593Smuzhiyun static bool _dhd_wlfc_ifpkt_fn(void* p, void *p_ifid)
1410*4882a593Smuzhiyun {
1411*4882a593Smuzhiyun 	if (!p || !p_ifid)
1412*4882a593Smuzhiyun 		return FALSE;
1413*4882a593Smuzhiyun 
1414*4882a593Smuzhiyun 	return (DHD_PKTTAG_WLFCPKT(PKTTAG(p))&& (*((uint8 *)p_ifid) == DHD_PKTTAG_IF(PKTTAG(p))));
1415*4882a593Smuzhiyun }
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun /** Returns TRUE if caller supplied packet is destined for caller supplied remote MAC */
_dhd_wlfc_entrypkt_fn(void * p,void * entry)1418*4882a593Smuzhiyun static bool _dhd_wlfc_entrypkt_fn(void* p, void *entry)
1419*4882a593Smuzhiyun {
1420*4882a593Smuzhiyun 	if (!p || !entry)
1421*4882a593Smuzhiyun 		return FALSE;
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun 	return (DHD_PKTTAG_WLFCPKT(PKTTAG(p))&& (entry == DHD_PKTTAG_ENTRY(PKTTAG(p))));
1424*4882a593Smuzhiyun }
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun static void
_dhd_wlfc_return_implied_credit(athost_wl_status_info_t * wlfc,void * pkt)1427*4882a593Smuzhiyun _dhd_wlfc_return_implied_credit(athost_wl_status_info_t* wlfc, void* pkt)
1428*4882a593Smuzhiyun {
1429*4882a593Smuzhiyun 	dhd_pub_t *dhdp;
1430*4882a593Smuzhiyun 	bool credit_return = FALSE;
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun 	if (!wlfc || !pkt) {
1433*4882a593Smuzhiyun 		return;
1434*4882a593Smuzhiyun 	}
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 	dhdp = (dhd_pub_t *)(wlfc->dhdp);
1437*4882a593Smuzhiyun 	if (dhdp && (dhdp->proptxstatus_mode == WLFC_FCMODE_IMPLIED_CREDIT) &&
1438*4882a593Smuzhiyun 		DHD_PKTTAG_CREDITCHECK(PKTTAG(pkt))) {
1439*4882a593Smuzhiyun 		int lender, credit_returned = 0;
1440*4882a593Smuzhiyun 		uint8 fifo_id = DHD_PKTTAG_FIFO(PKTTAG(pkt));
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun 		credit_return = TRUE;
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 		/* Note that borrower is fifo_id */
1445*4882a593Smuzhiyun 		/* Return credits to highest priority lender first */
1446*4882a593Smuzhiyun 		for (lender = AC_COUNT; lender >= 0; lender--) {
1447*4882a593Smuzhiyun 			if (wlfc->credits_borrowed[fifo_id][lender] > 0) {
1448*4882a593Smuzhiyun 				wlfc->FIFO_credit[lender]++;
1449*4882a593Smuzhiyun 				wlfc->credits_borrowed[fifo_id][lender]--;
1450*4882a593Smuzhiyun 				credit_returned = 1;
1451*4882a593Smuzhiyun 				break;
1452*4882a593Smuzhiyun 			}
1453*4882a593Smuzhiyun 		}
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 		if (!credit_returned) {
1456*4882a593Smuzhiyun 			wlfc->FIFO_credit[fifo_id]++;
1457*4882a593Smuzhiyun 		}
1458*4882a593Smuzhiyun 	}
1459*4882a593Smuzhiyun 
1460*4882a593Smuzhiyun 	BCM_REFERENCE(credit_return);
1461*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
1462*4882a593Smuzhiyun 	if (credit_return) {
1463*4882a593Smuzhiyun 		_dhd_wlfc_thread_wakeup(dhdp);
1464*4882a593Smuzhiyun 	}
1465*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun 
1468*4882a593Smuzhiyun /** Removes and frees a packet from the hanger. Called during eg tx complete. */
1469*4882a593Smuzhiyun static void
_dhd_wlfc_hanger_free_pkt(athost_wl_status_info_t * wlfc,uint32 slot_id,uint8 pkt_state,int pkt_txstatus)1470*4882a593Smuzhiyun _dhd_wlfc_hanger_free_pkt(athost_wl_status_info_t* wlfc, uint32 slot_id, uint8 pkt_state,
1471*4882a593Smuzhiyun 	int pkt_txstatus)
1472*4882a593Smuzhiyun {
1473*4882a593Smuzhiyun 	wlfc_hanger_t* hanger;
1474*4882a593Smuzhiyun 	wlfc_hanger_item_t* item;
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	if (!wlfc)
1477*4882a593Smuzhiyun 		return;
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 	hanger = (wlfc_hanger_t*)wlfc->hanger;
1480*4882a593Smuzhiyun 	if (!hanger)
1481*4882a593Smuzhiyun 		return;
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun 	if (slot_id == WLFC_HANGER_MAXITEMS)
1484*4882a593Smuzhiyun 		return;
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	item = &hanger->items[slot_id];
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun 	if (item->pkt) {
1489*4882a593Smuzhiyun 		item->pkt_state |= pkt_state;
1490*4882a593Smuzhiyun 		if (pkt_txstatus != -1)
1491*4882a593Smuzhiyun 			item->pkt_txstatus = (uint8)pkt_txstatus;
1492*4882a593Smuzhiyun 		bcm_object_feature_set(item->pkt, BCM_OBJECT_FEATURE_PKT_STATE, item->pkt_state);
1493*4882a593Smuzhiyun 		if (item->pkt_state == WLFC_HANGER_PKT_STATE_COMPLETE) {
1494*4882a593Smuzhiyun 			void *p = NULL;
1495*4882a593Smuzhiyun 			void *pkt = item->pkt;
1496*4882a593Smuzhiyun 			uint8 old_state = item->state;
1497*4882a593Smuzhiyun 			int ret = _dhd_wlfc_hanger_poppkt(wlfc->hanger, slot_id, &p, TRUE);
1498*4882a593Smuzhiyun 			BCM_REFERENCE(ret);
1499*4882a593Smuzhiyun 			BCM_REFERENCE(pkt);
1500*4882a593Smuzhiyun 			ASSERT((ret == BCME_OK) && p && (pkt == p));
1501*4882a593Smuzhiyun 			if (old_state == WLFC_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
1502*4882a593Smuzhiyun 				printf("ERROR: free a suppressed pkt %p state %d pkt_state %d\n",
1503*4882a593Smuzhiyun 					pkt, old_state, item->pkt_state);
1504*4882a593Smuzhiyun 			}
1505*4882a593Smuzhiyun 			ASSERT(old_state != WLFC_HANGER_ITEM_STATE_INUSE_SUPPRESSED);
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 			/* free packet */
1508*4882a593Smuzhiyun 			wlfc->pkt_cnt_in_drv[DHD_PKTTAG_IF(PKTTAG(p))]
1509*4882a593Smuzhiyun 				[DHD_PKTTAG_FIFO(PKTTAG(p))]--;
1510*4882a593Smuzhiyun 			wlfc->stats.pktout++;
1511*4882a593Smuzhiyun 			dhd_txcomplete((dhd_pub_t *)wlfc->dhdp, p, item->pkt_txstatus);
1512*4882a593Smuzhiyun 			PKTFREE(wlfc->osh, p, TRUE);
1513*4882a593Smuzhiyun 		}
1514*4882a593Smuzhiyun 	} else {
1515*4882a593Smuzhiyun 		/* free slot */
1516*4882a593Smuzhiyun 		if (item->state == WLFC_HANGER_ITEM_STATE_FREE)
1517*4882a593Smuzhiyun 			DHD_ERROR(("Error: %s():%d Multiple TXSTATUS or BUSRETURNED: %d (%d)\n",
1518*4882a593Smuzhiyun 			    __FUNCTION__, __LINE__, item->pkt_state, pkt_state));
1519*4882a593Smuzhiyun 		item->state = WLFC_HANGER_ITEM_STATE_FREE;
1520*4882a593Smuzhiyun 	}
1521*4882a593Smuzhiyun } /* _dhd_wlfc_hanger_free_pkt */
1522*4882a593Smuzhiyun 
1523*4882a593Smuzhiyun /** Called during eg detach() */
1524*4882a593Smuzhiyun static void
_dhd_wlfc_pktq_flush(athost_wl_status_info_t * ctx,struct pktq * pq,bool dir,f_processpkt_t fn,void * arg,q_type_t q_type)1525*4882a593Smuzhiyun _dhd_wlfc_pktq_flush(athost_wl_status_info_t* ctx, struct pktq *pq,
1526*4882a593Smuzhiyun 	bool dir, f_processpkt_t fn, void *arg, q_type_t q_type)
1527*4882a593Smuzhiyun {
1528*4882a593Smuzhiyun 	int prec;
1529*4882a593Smuzhiyun 	dhd_pub_t *dhdp = (dhd_pub_t *)ctx->dhdp;
1530*4882a593Smuzhiyun 
1531*4882a593Smuzhiyun 	ASSERT(dhdp);
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun 	/* Optimize flush, if pktq len = 0, just return.
1534*4882a593Smuzhiyun 	 * pktq len of 0 means pktq's prec q's are all empty.
1535*4882a593Smuzhiyun 	 */
1536*4882a593Smuzhiyun 	if (pq->n_pkts_tot == 0) {
1537*4882a593Smuzhiyun 		return;
1538*4882a593Smuzhiyun 	}
1539*4882a593Smuzhiyun 
1540*4882a593Smuzhiyun 	for (prec = 0; prec < pq->num_prec; prec++) {
1541*4882a593Smuzhiyun 		struct pktq_prec *q;
1542*4882a593Smuzhiyun 		void *p, *prev = NULL;
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun 		q = &pq->q[prec];
1545*4882a593Smuzhiyun 		p = q->head;
1546*4882a593Smuzhiyun 		while (p) {
1547*4882a593Smuzhiyun 			bcm_pkt_validate_chk(p);
1548*4882a593Smuzhiyun 			if (fn == NULL || (*fn)(p, arg)) {
1549*4882a593Smuzhiyun 				bool head = (p == q->head);
1550*4882a593Smuzhiyun 				if (head)
1551*4882a593Smuzhiyun 					q->head = PKTLINK(p);
1552*4882a593Smuzhiyun 				else
1553*4882a593Smuzhiyun 					PKTSETLINK(prev, PKTLINK(p));
1554*4882a593Smuzhiyun 				if (q_type == Q_TYPE_PSQ) {
1555*4882a593Smuzhiyun 					if (!WLFC_GET_AFQ(dhdp->wlfc_mode) && (prec & 1)) {
1556*4882a593Smuzhiyun 						_dhd_wlfc_hanger_remove_reference(ctx->hanger, p);
1557*4882a593Smuzhiyun 					}
1558*4882a593Smuzhiyun 					ctx->pkt_cnt_in_q[DHD_PKTTAG_IF(PKTTAG(p))][prec>>1]--;
1559*4882a593Smuzhiyun 					ctx->pkt_cnt_per_ac[prec>>1]--;
1560*4882a593Smuzhiyun 					ctx->pkt_cnt_in_psq--;
1561*4882a593Smuzhiyun 					ctx->stats.cleanup_psq_cnt++;
1562*4882a593Smuzhiyun 					if (!(prec & 1)) {
1563*4882a593Smuzhiyun 						/* pkt in delayed q, so fake push BDC header for
1564*4882a593Smuzhiyun 						 * dhd_tcpack_check_xmit() and dhd_txcomplete().
1565*4882a593Smuzhiyun 						 */
1566*4882a593Smuzhiyun 						_dhd_wlfc_pushheader(ctx, &p, FALSE, 0, 0,
1567*4882a593Smuzhiyun 							0, 0, TRUE);
1568*4882a593Smuzhiyun #ifdef DHDTCPACK_SUPPRESS
1569*4882a593Smuzhiyun 						if (dhd_tcpack_check_xmit(dhdp, p) == BCME_ERROR) {
1570*4882a593Smuzhiyun 							DHD_ERROR(("%s %d: tcpack_suppress ERROR!!!"
1571*4882a593Smuzhiyun 								" Stop using it\n",
1572*4882a593Smuzhiyun 								__FUNCTION__, __LINE__));
1573*4882a593Smuzhiyun 							dhd_tcpack_suppress_set(dhdp,
1574*4882a593Smuzhiyun 								TCPACK_SUP_OFF);
1575*4882a593Smuzhiyun 						}
1576*4882a593Smuzhiyun #endif /* DHDTCPACK_SUPPRESS */
1577*4882a593Smuzhiyun 					}
1578*4882a593Smuzhiyun 				} else if (q_type == Q_TYPE_AFQ) {
1579*4882a593Smuzhiyun 					wlfc_mac_descriptor_t* entry =
1580*4882a593Smuzhiyun 						_dhd_wlfc_find_table_entry(ctx, p);
1581*4882a593Smuzhiyun 					if (entry->transit_count)
1582*4882a593Smuzhiyun 						entry->transit_count--;
1583*4882a593Smuzhiyun 					if (entry->suppr_transit_count) {
1584*4882a593Smuzhiyun 						entry->suppr_transit_count--;
1585*4882a593Smuzhiyun 						if (entry->suppressed &&
1586*4882a593Smuzhiyun 							(!entry->onbus_pkts_count) &&
1587*4882a593Smuzhiyun 							(!entry->suppr_transit_count))
1588*4882a593Smuzhiyun 							entry->suppressed = FALSE;
1589*4882a593Smuzhiyun 					}
1590*4882a593Smuzhiyun 					_dhd_wlfc_return_implied_credit(ctx, p);
1591*4882a593Smuzhiyun 					ctx->stats.cleanup_fw_cnt++;
1592*4882a593Smuzhiyun 				}
1593*4882a593Smuzhiyun 				PKTSETLINK(p, NULL);
1594*4882a593Smuzhiyun 				if (dir) {
1595*4882a593Smuzhiyun 					ctx->pkt_cnt_in_drv[DHD_PKTTAG_IF(PKTTAG(p))][prec>>1]--;
1596*4882a593Smuzhiyun 					ctx->stats.pktout++;
1597*4882a593Smuzhiyun 					dhd_txcomplete(dhdp, p, FALSE);
1598*4882a593Smuzhiyun 				}
1599*4882a593Smuzhiyun 				PKTFREE(ctx->osh, p, dir);
1600*4882a593Smuzhiyun 
1601*4882a593Smuzhiyun 				q->n_pkts--;
1602*4882a593Smuzhiyun 				pq->n_pkts_tot--;
1603*4882a593Smuzhiyun #ifdef WL_TXQ_STALL
1604*4882a593Smuzhiyun 				q->dequeue_count++;
1605*4882a593Smuzhiyun #endif // endif
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 				p = (head ? q->head : PKTLINK(prev));
1608*4882a593Smuzhiyun 			} else {
1609*4882a593Smuzhiyun 				prev = p;
1610*4882a593Smuzhiyun 				p = PKTLINK(p);
1611*4882a593Smuzhiyun 			}
1612*4882a593Smuzhiyun 		}
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun 		if (q->head == NULL) {
1615*4882a593Smuzhiyun 			ASSERT(q->n_pkts == 0);
1616*4882a593Smuzhiyun 			q->tail = NULL;
1617*4882a593Smuzhiyun 		}
1618*4882a593Smuzhiyun 
1619*4882a593Smuzhiyun 	}
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 	if (fn == NULL)
1622*4882a593Smuzhiyun 		ASSERT(pq->n_pkts_tot == 0);
1623*4882a593Smuzhiyun } /* _dhd_wlfc_pktq_flush */
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun /** !BCMDBUS specific function. Dequeues a packet from the caller supplied queue. */
1626*4882a593Smuzhiyun static void*
_dhd_wlfc_pktq_pdeq_with_fn(struct pktq * pq,int prec,f_processpkt_t fn,void * arg)1627*4882a593Smuzhiyun _dhd_wlfc_pktq_pdeq_with_fn(struct pktq *pq, int prec, f_processpkt_t fn, void *arg)
1628*4882a593Smuzhiyun {
1629*4882a593Smuzhiyun 	struct pktq_prec *q;
1630*4882a593Smuzhiyun 	void *p, *prev = NULL;
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun 	ASSERT(prec >= 0 && prec < pq->num_prec);
1633*4882a593Smuzhiyun 
1634*4882a593Smuzhiyun 	q = &pq->q[prec];
1635*4882a593Smuzhiyun 	p = q->head;
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun 	while (p) {
1638*4882a593Smuzhiyun 		if (fn == NULL || (*fn)(p, arg)) {
1639*4882a593Smuzhiyun 			break;
1640*4882a593Smuzhiyun 		} else {
1641*4882a593Smuzhiyun 			prev = p;
1642*4882a593Smuzhiyun 			p = PKTLINK(p);
1643*4882a593Smuzhiyun 		}
1644*4882a593Smuzhiyun 	}
1645*4882a593Smuzhiyun 	if (p == NULL)
1646*4882a593Smuzhiyun 		return NULL;
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	bcm_pkt_validate_chk(p);
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun 	if (prev == NULL) {
1651*4882a593Smuzhiyun 		if ((q->head = PKTLINK(p)) == NULL) {
1652*4882a593Smuzhiyun 			q->tail = NULL;
1653*4882a593Smuzhiyun 		}
1654*4882a593Smuzhiyun 	} else {
1655*4882a593Smuzhiyun 		PKTSETLINK(prev, PKTLINK(p));
1656*4882a593Smuzhiyun 		if (q->tail == p) {
1657*4882a593Smuzhiyun 			q->tail = prev;
1658*4882a593Smuzhiyun 		}
1659*4882a593Smuzhiyun 	}
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 	q->n_pkts--;
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun 	pq->n_pkts_tot--;
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun #ifdef WL_TXQ_STALL
1666*4882a593Smuzhiyun 	q->dequeue_count++;
1667*4882a593Smuzhiyun #endif // endif
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 	PKTSETLINK(p, NULL);
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun 	return p;
1672*4882a593Smuzhiyun }
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun /** !BCMDBUS specific function */
1675*4882a593Smuzhiyun static void
_dhd_wlfc_cleanup_txq(dhd_pub_t * dhd,f_processpkt_t fn,void * arg)1676*4882a593Smuzhiyun _dhd_wlfc_cleanup_txq(dhd_pub_t *dhd, f_processpkt_t fn, void *arg)
1677*4882a593Smuzhiyun {
1678*4882a593Smuzhiyun 	int prec;
1679*4882a593Smuzhiyun 	void *pkt = NULL, *head = NULL, *tail = NULL;
1680*4882a593Smuzhiyun 	struct pktq *txq = (struct pktq *)dhd_bus_txq(dhd->bus);
1681*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
1682*4882a593Smuzhiyun 	wlfc_hanger_t* h = (wlfc_hanger_t*)wlfc->hanger;
1683*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry;
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	dhd_os_sdlock_txq(dhd);
1686*4882a593Smuzhiyun 	for (prec = 0; prec < txq->num_prec; prec++) {
1687*4882a593Smuzhiyun 		while ((pkt = _dhd_wlfc_pktq_pdeq_with_fn(txq, prec, fn, arg))) {
1688*4882a593Smuzhiyun #ifdef DHDTCPACK_SUPPRESS
1689*4882a593Smuzhiyun 			if (dhd_tcpack_check_xmit(dhd, pkt) == BCME_ERROR) {
1690*4882a593Smuzhiyun 				DHD_ERROR(("%s %d: tcpack_suppress ERROR!!! Stop using it\n",
1691*4882a593Smuzhiyun 					__FUNCTION__, __LINE__));
1692*4882a593Smuzhiyun 				dhd_tcpack_suppress_set(dhd, TCPACK_SUP_OFF);
1693*4882a593Smuzhiyun 			}
1694*4882a593Smuzhiyun #endif /* DHDTCPACK_SUPPRESS */
1695*4882a593Smuzhiyun 			if (!head) {
1696*4882a593Smuzhiyun 				head = pkt;
1697*4882a593Smuzhiyun 			}
1698*4882a593Smuzhiyun 			if (tail) {
1699*4882a593Smuzhiyun 				PKTSETLINK(tail, pkt);
1700*4882a593Smuzhiyun 			}
1701*4882a593Smuzhiyun 			tail = pkt;
1702*4882a593Smuzhiyun 		}
1703*4882a593Smuzhiyun 	}
1704*4882a593Smuzhiyun 	dhd_os_sdunlock_txq(dhd);
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun 	while ((pkt = head)) {
1707*4882a593Smuzhiyun 		head = PKTLINK(pkt);
1708*4882a593Smuzhiyun 		PKTSETLINK(pkt, NULL);
1709*4882a593Smuzhiyun 		entry = _dhd_wlfc_find_table_entry(wlfc, pkt);
1710*4882a593Smuzhiyun 
1711*4882a593Smuzhiyun 		if (!WLFC_GET_AFQ(dhd->wlfc_mode) &&
1712*4882a593Smuzhiyun 			!_dhd_wlfc_hanger_remove_reference(h, pkt)) {
1713*4882a593Smuzhiyun 			DHD_ERROR(("%s: can't find pkt(%p) in hanger, free it anyway\n",
1714*4882a593Smuzhiyun 				__FUNCTION__, pkt));
1715*4882a593Smuzhiyun 		}
1716*4882a593Smuzhiyun 		if (entry->transit_count)
1717*4882a593Smuzhiyun 			entry->transit_count--;
1718*4882a593Smuzhiyun 		if (entry->suppr_transit_count) {
1719*4882a593Smuzhiyun 			entry->suppr_transit_count--;
1720*4882a593Smuzhiyun 			if (entry->suppressed &&
1721*4882a593Smuzhiyun 				(!entry->onbus_pkts_count) &&
1722*4882a593Smuzhiyun 				(!entry->suppr_transit_count))
1723*4882a593Smuzhiyun 				entry->suppressed = FALSE;
1724*4882a593Smuzhiyun 		}
1725*4882a593Smuzhiyun 		_dhd_wlfc_return_implied_credit(wlfc, pkt);
1726*4882a593Smuzhiyun 		wlfc->pkt_cnt_in_drv[DHD_PKTTAG_IF(PKTTAG(pkt))][DHD_PKTTAG_FIFO(PKTTAG(pkt))]--;
1727*4882a593Smuzhiyun 		wlfc->stats.pktout++;
1728*4882a593Smuzhiyun 		wlfc->stats.cleanup_txq_cnt++;
1729*4882a593Smuzhiyun 		dhd_txcomplete(dhd, pkt, FALSE);
1730*4882a593Smuzhiyun 		PKTFREE(wlfc->osh, pkt, TRUE);
1731*4882a593Smuzhiyun 	}
1732*4882a593Smuzhiyun } /* _dhd_wlfc_cleanup_txq */
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun /** called during eg detach */
1735*4882a593Smuzhiyun void
_dhd_wlfc_cleanup(dhd_pub_t * dhd,f_processpkt_t fn,void * arg)1736*4882a593Smuzhiyun _dhd_wlfc_cleanup(dhd_pub_t *dhd, f_processpkt_t fn, void *arg)
1737*4882a593Smuzhiyun {
1738*4882a593Smuzhiyun 	int i;
1739*4882a593Smuzhiyun 	int total_entries;
1740*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
1741*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* table;
1742*4882a593Smuzhiyun 	wlfc_hanger_t* h = (wlfc_hanger_t*)wlfc->hanger;
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun 	wlfc->stats.cleanup_txq_cnt = 0;
1745*4882a593Smuzhiyun 	wlfc->stats.cleanup_psq_cnt = 0;
1746*4882a593Smuzhiyun 	wlfc->stats.cleanup_fw_cnt = 0;
1747*4882a593Smuzhiyun 
1748*4882a593Smuzhiyun 	/*
1749*4882a593Smuzhiyun 	*  flush sequence should be txq -> psq -> hanger/afq, hanger has to be last one
1750*4882a593Smuzhiyun 	*/
1751*4882a593Smuzhiyun 	/* flush bus->txq */
1752*4882a593Smuzhiyun 	_dhd_wlfc_cleanup_txq(dhd, fn, arg);
1753*4882a593Smuzhiyun 
1754*4882a593Smuzhiyun 	/* flush psq, search all entries, include nodes as well as interfaces */
1755*4882a593Smuzhiyun 	total_entries = sizeof(wlfc->destination_entries)/sizeof(wlfc_mac_descriptor_t);
1756*4882a593Smuzhiyun 	table = (wlfc_mac_descriptor_t*)&wlfc->destination_entries;
1757*4882a593Smuzhiyun 
1758*4882a593Smuzhiyun 	for (i = 0; i < total_entries; i++) {
1759*4882a593Smuzhiyun 		if (table[i].occupied) {
1760*4882a593Smuzhiyun 			/* release packets held in PSQ (both delayed and suppressed) */
1761*4882a593Smuzhiyun 			if (table[i].psq.n_pkts_tot) {
1762*4882a593Smuzhiyun 				WLFC_DBGMESG(("%s(): PSQ[%d].len = %d\n",
1763*4882a593Smuzhiyun 					__FUNCTION__, i, table[i].psq.n_pkts_tot));
1764*4882a593Smuzhiyun 				_dhd_wlfc_pktq_flush(wlfc, &table[i].psq, TRUE,
1765*4882a593Smuzhiyun 					fn, arg, Q_TYPE_PSQ);
1766*4882a593Smuzhiyun 			}
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun 			/* free packets held in AFQ */
1769*4882a593Smuzhiyun 			if (WLFC_GET_AFQ(dhd->wlfc_mode) && (table[i].afq.n_pkts_tot)) {
1770*4882a593Smuzhiyun 				_dhd_wlfc_pktq_flush(wlfc, &table[i].afq, TRUE,
1771*4882a593Smuzhiyun 					fn, arg, Q_TYPE_AFQ);
1772*4882a593Smuzhiyun 			}
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 			if ((fn == NULL) && (&table[i] != &wlfc->destination_entries.other)) {
1775*4882a593Smuzhiyun 				table[i].occupied = 0;
1776*4882a593Smuzhiyun 				if (table[i].transit_count || table[i].suppr_transit_count) {
1777*4882a593Smuzhiyun 					DHD_ERROR(("%s: table[%d] transit(%d), suppr_tansit(%d)\n",
1778*4882a593Smuzhiyun 						__FUNCTION__, i,
1779*4882a593Smuzhiyun 						table[i].transit_count,
1780*4882a593Smuzhiyun 						table[i].suppr_transit_count));
1781*4882a593Smuzhiyun 				}
1782*4882a593Smuzhiyun 			}
1783*4882a593Smuzhiyun 		}
1784*4882a593Smuzhiyun 	}
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 	/*
1787*4882a593Smuzhiyun 		. flush remained pkt in hanger queue, not in bus->txq nor psq.
1788*4882a593Smuzhiyun 		. the remained pkt was successfully downloaded to dongle already.
1789*4882a593Smuzhiyun 		. hanger slot state cannot be set to free until receive txstatus update.
1790*4882a593Smuzhiyun 	*/
1791*4882a593Smuzhiyun 	if (!WLFC_GET_AFQ(dhd->wlfc_mode)) {
1792*4882a593Smuzhiyun 		for (i = 0; i < h->max_items; i++) {
1793*4882a593Smuzhiyun 			if ((h->items[i].state == WLFC_HANGER_ITEM_STATE_INUSE) ||
1794*4882a593Smuzhiyun 				(h->items[i].state == WLFC_HANGER_ITEM_STATE_INUSE_SUPPRESSED)) {
1795*4882a593Smuzhiyun 				if (fn == NULL || (*fn)(h->items[i].pkt, arg)) {
1796*4882a593Smuzhiyun 					h->items[i].state = WLFC_HANGER_ITEM_STATE_FLUSHED;
1797*4882a593Smuzhiyun 				}
1798*4882a593Smuzhiyun 			}
1799*4882a593Smuzhiyun 		}
1800*4882a593Smuzhiyun 	}
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 	return;
1803*4882a593Smuzhiyun } /* _dhd_wlfc_cleanup */
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun /** Called after eg the dongle signalled a new remote MAC that it connected with to the DHD */
1806*4882a593Smuzhiyun static int
_dhd_wlfc_mac_entry_update(athost_wl_status_info_t * ctx,wlfc_mac_descriptor_t * entry,uint8 action,uint8 ifid,uint8 iftype,uint8 * ea,f_processpkt_t fn,void * arg)1807*4882a593Smuzhiyun _dhd_wlfc_mac_entry_update(athost_wl_status_info_t* ctx, wlfc_mac_descriptor_t* entry,
1808*4882a593Smuzhiyun 	uint8 action, uint8 ifid, uint8 iftype, uint8* ea,
1809*4882a593Smuzhiyun 	f_processpkt_t fn, void *arg)
1810*4882a593Smuzhiyun {
1811*4882a593Smuzhiyun 	int rc = BCME_OK;
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun 	if ((action == eWLFC_MAC_ENTRY_ACTION_ADD) || (action == eWLFC_MAC_ENTRY_ACTION_UPDATE)) {
1814*4882a593Smuzhiyun 		entry->occupied = 1;
1815*4882a593Smuzhiyun 		entry->state = WLFC_STATE_OPEN;
1816*4882a593Smuzhiyun 		entry->requested_credit = 0;
1817*4882a593Smuzhiyun 		entry->interface_id = ifid;
1818*4882a593Smuzhiyun 		entry->iftype = iftype;
1819*4882a593Smuzhiyun 		entry->ac_bitmap = 0xff; /* update this when handling APSD */
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun 		/* for an interface entry we may not care about the MAC address */
1822*4882a593Smuzhiyun 		if (ea != NULL)
1823*4882a593Smuzhiyun 			memcpy(&entry->ea[0], ea, ETHER_ADDR_LEN);
1824*4882a593Smuzhiyun 
1825*4882a593Smuzhiyun 		if (action == eWLFC_MAC_ENTRY_ACTION_ADD) {
1826*4882a593Smuzhiyun 			entry->suppressed = FALSE;
1827*4882a593Smuzhiyun 			entry->transit_count = 0;
1828*4882a593Smuzhiyun 			entry->suppr_transit_count = 0;
1829*4882a593Smuzhiyun 			entry->onbus_pkts_count = 0;
1830*4882a593Smuzhiyun 		}
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 		if (action == eWLFC_MAC_ENTRY_ACTION_ADD) {
1833*4882a593Smuzhiyun 			dhd_pub_t *dhdp = (dhd_pub_t *)(ctx->dhdp);
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 			pktq_init(&entry->psq, WLFC_PSQ_PREC_COUNT, WLFC_PSQ_LEN);
1836*4882a593Smuzhiyun 			_dhd_wlfc_flow_control_check(ctx, &entry->psq, ifid);
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 			if (WLFC_GET_AFQ(dhdp->wlfc_mode)) {
1839*4882a593Smuzhiyun 				pktq_init(&entry->afq, WLFC_AFQ_PREC_COUNT, WLFC_PSQ_LEN);
1840*4882a593Smuzhiyun 			}
1841*4882a593Smuzhiyun 
1842*4882a593Smuzhiyun 			if (entry->next == NULL) {
1843*4882a593Smuzhiyun 				/* not linked to anywhere, add to tail */
1844*4882a593Smuzhiyun 				if (ctx->active_entry_head) {
1845*4882a593Smuzhiyun 					entry->prev = ctx->active_entry_head->prev;
1846*4882a593Smuzhiyun 					ctx->active_entry_head->prev->next = entry;
1847*4882a593Smuzhiyun 					ctx->active_entry_head->prev = entry;
1848*4882a593Smuzhiyun 					entry->next = ctx->active_entry_head;
1849*4882a593Smuzhiyun 				} else {
1850*4882a593Smuzhiyun 					ASSERT(ctx->active_entry_count == 0);
1851*4882a593Smuzhiyun 					entry->prev = entry->next = entry;
1852*4882a593Smuzhiyun 					ctx->active_entry_head = entry;
1853*4882a593Smuzhiyun 				}
1854*4882a593Smuzhiyun 				ctx->active_entry_count++;
1855*4882a593Smuzhiyun 			} else {
1856*4882a593Smuzhiyun 				DHD_ERROR(("%s():%d, entry(%d)\n", __FUNCTION__, __LINE__,
1857*4882a593Smuzhiyun 					(int)(entry - &ctx->destination_entries.nodes[0])));
1858*4882a593Smuzhiyun 			}
1859*4882a593Smuzhiyun 		}
1860*4882a593Smuzhiyun 	} else if (action == eWLFC_MAC_ENTRY_ACTION_DEL) {
1861*4882a593Smuzhiyun 		/* When the entry is deleted, the packets that are queued in the entry must be
1862*4882a593Smuzhiyun 		   cleanup. The cleanup action should be before the occupied is set as 0.
1863*4882a593Smuzhiyun 		*/
1864*4882a593Smuzhiyun 		_dhd_wlfc_cleanup(ctx->dhdp, fn, arg);
1865*4882a593Smuzhiyun 		_dhd_wlfc_flow_control_check(ctx, &entry->psq, ifid);
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 		entry->occupied = 0;
1868*4882a593Smuzhiyun 		entry->state = WLFC_STATE_CLOSE;
1869*4882a593Smuzhiyun 		memset(&entry->ea[0], 0, ETHER_ADDR_LEN);
1870*4882a593Smuzhiyun 
1871*4882a593Smuzhiyun 		if (entry->next) {
1872*4882a593Smuzhiyun 			/* not floating, remove from Q */
1873*4882a593Smuzhiyun 			if (ctx->active_entry_count <= 1) {
1874*4882a593Smuzhiyun 				/* last item */
1875*4882a593Smuzhiyun 				ctx->active_entry_head = NULL;
1876*4882a593Smuzhiyun 				ctx->active_entry_count = 0;
1877*4882a593Smuzhiyun 			} else {
1878*4882a593Smuzhiyun 				entry->prev->next = entry->next;
1879*4882a593Smuzhiyun 				entry->next->prev = entry->prev;
1880*4882a593Smuzhiyun 				if (entry == ctx->active_entry_head) {
1881*4882a593Smuzhiyun 					ctx->active_entry_head = entry->next;
1882*4882a593Smuzhiyun 				}
1883*4882a593Smuzhiyun 				ctx->active_entry_count--;
1884*4882a593Smuzhiyun 			}
1885*4882a593Smuzhiyun 			entry->next = entry->prev = NULL;
1886*4882a593Smuzhiyun 		} else {
1887*4882a593Smuzhiyun 			DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
1888*4882a593Smuzhiyun 		}
1889*4882a593Smuzhiyun 	}
1890*4882a593Smuzhiyun 	return rc;
1891*4882a593Smuzhiyun } /* _dhd_wlfc_mac_entry_update */
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun #ifdef LIMIT_BORROW
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun /** LIMIT_BORROW specific function */
1896*4882a593Smuzhiyun static int
_dhd_wlfc_borrow_credit(athost_wl_status_info_t * ctx,int highest_lender_ac,int borrower_ac,bool bBorrowAll)1897*4882a593Smuzhiyun _dhd_wlfc_borrow_credit(athost_wl_status_info_t* ctx, int highest_lender_ac, int borrower_ac,
1898*4882a593Smuzhiyun 	bool bBorrowAll)
1899*4882a593Smuzhiyun {
1900*4882a593Smuzhiyun 	int lender_ac, borrow_limit = 0;
1901*4882a593Smuzhiyun 	int rc = -1;
1902*4882a593Smuzhiyun 
1903*4882a593Smuzhiyun 	if (ctx == NULL) {
1904*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
1905*4882a593Smuzhiyun 		return -1;
1906*4882a593Smuzhiyun 	}
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun 	/* Borrow from lowest priority available AC (including BC/MC credits) */
1909*4882a593Smuzhiyun 	for (lender_ac = 0; lender_ac <= highest_lender_ac; lender_ac++) {
1910*4882a593Smuzhiyun 		if (!bBorrowAll) {
1911*4882a593Smuzhiyun 			borrow_limit = ctx->Init_FIFO_credit[lender_ac]/WLFC_BORROW_LIMIT_RATIO;
1912*4882a593Smuzhiyun 		} else {
1913*4882a593Smuzhiyun 			borrow_limit = 0;
1914*4882a593Smuzhiyun 		}
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun 		if (ctx->FIFO_credit[lender_ac] > borrow_limit) {
1917*4882a593Smuzhiyun 			ctx->credits_borrowed[borrower_ac][lender_ac]++;
1918*4882a593Smuzhiyun 			ctx->FIFO_credit[lender_ac]--;
1919*4882a593Smuzhiyun 			rc = lender_ac;
1920*4882a593Smuzhiyun 			break;
1921*4882a593Smuzhiyun 		}
1922*4882a593Smuzhiyun 	}
1923*4882a593Smuzhiyun 
1924*4882a593Smuzhiyun 	return rc;
1925*4882a593Smuzhiyun }
1926*4882a593Smuzhiyun 
1927*4882a593Smuzhiyun /** LIMIT_BORROW specific function */
_dhd_wlfc_return_credit(athost_wl_status_info_t * ctx,int lender_ac,int borrower_ac)1928*4882a593Smuzhiyun static int _dhd_wlfc_return_credit(athost_wl_status_info_t* ctx, int lender_ac, int borrower_ac)
1929*4882a593Smuzhiyun {
1930*4882a593Smuzhiyun 	if ((ctx == NULL) || (lender_ac < 0) || (lender_ac > AC_COUNT) ||
1931*4882a593Smuzhiyun 		(borrower_ac < 0) || (borrower_ac > AC_COUNT)) {
1932*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d, ctx(%p), lender_ac(%d), borrower_ac(%d)\n",
1933*4882a593Smuzhiyun 			__FUNCTION__, __LINE__, ctx, lender_ac, borrower_ac));
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun 		return BCME_BADARG;
1936*4882a593Smuzhiyun 	}
1937*4882a593Smuzhiyun 
1938*4882a593Smuzhiyun 	ctx->credits_borrowed[borrower_ac][lender_ac]--;
1939*4882a593Smuzhiyun 	ctx->FIFO_credit[lender_ac]++;
1940*4882a593Smuzhiyun 
1941*4882a593Smuzhiyun 	return BCME_OK;
1942*4882a593Smuzhiyun }
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun #endif /* LIMIT_BORROW */
1945*4882a593Smuzhiyun 
1946*4882a593Smuzhiyun /**
1947*4882a593Smuzhiyun  * Called on an interface event (WLC_E_IF) indicated by firmware.
1948*4882a593Smuzhiyun  *     @param action : eg eWLFC_MAC_ENTRY_ACTION_UPDATE or eWLFC_MAC_ENTRY_ACTION_ADD
1949*4882a593Smuzhiyun  */
1950*4882a593Smuzhiyun static int
_dhd_wlfc_interface_entry_update(void * state,uint8 action,uint8 ifid,uint8 iftype,uint8 * ea)1951*4882a593Smuzhiyun _dhd_wlfc_interface_entry_update(void* state,
1952*4882a593Smuzhiyun 	uint8 action, uint8 ifid, uint8 iftype, uint8* ea)
1953*4882a593Smuzhiyun {
1954*4882a593Smuzhiyun 	athost_wl_status_info_t* ctx = (athost_wl_status_info_t*)state;
1955*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry;
1956*4882a593Smuzhiyun 
1957*4882a593Smuzhiyun 	if (ifid >= WLFC_MAX_IFNUM)
1958*4882a593Smuzhiyun 		return BCME_BADARG;
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 	entry = &ctx->destination_entries.interfaces[ifid];
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	return _dhd_wlfc_mac_entry_update(ctx, entry, action, ifid, iftype, ea,
1963*4882a593Smuzhiyun 		_dhd_wlfc_ifpkt_fn, &ifid);
1964*4882a593Smuzhiyun }
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun /**
1967*4882a593Smuzhiyun  * Called eg on receiving a WLC_E_BCMC_CREDIT_SUPPORT event from the dongle (broadcast/multicast
1968*4882a593Smuzhiyun  * specific)
1969*4882a593Smuzhiyun  */
1970*4882a593Smuzhiyun static int
_dhd_wlfc_BCMCCredit_support_update(void * state)1971*4882a593Smuzhiyun _dhd_wlfc_BCMCCredit_support_update(void* state)
1972*4882a593Smuzhiyun {
1973*4882a593Smuzhiyun 	athost_wl_status_info_t* ctx = (athost_wl_status_info_t*)state;
1974*4882a593Smuzhiyun 
1975*4882a593Smuzhiyun 	ctx->bcmc_credit_supported = TRUE;
1976*4882a593Smuzhiyun 	return BCME_OK;
1977*4882a593Smuzhiyun }
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun /** Called eg on receiving a WLC_E_FIFO_CREDIT_MAP event from the dongle */
1980*4882a593Smuzhiyun static int
_dhd_wlfc_FIFOcreditmap_update(void * state,uint8 * credits)1981*4882a593Smuzhiyun _dhd_wlfc_FIFOcreditmap_update(void* state, uint8* credits)
1982*4882a593Smuzhiyun {
1983*4882a593Smuzhiyun 	athost_wl_status_info_t* ctx = (athost_wl_status_info_t*)state;
1984*4882a593Smuzhiyun 	int i;
1985*4882a593Smuzhiyun 
1986*4882a593Smuzhiyun 	for (i = 0; i <= 4; i++) {
1987*4882a593Smuzhiyun 		if (ctx->Init_FIFO_credit[i] != ctx->FIFO_credit[i]) {
1988*4882a593Smuzhiyun 			DHD_ERROR(("%s: credit[i] is not returned, (%d %d)\n",
1989*4882a593Smuzhiyun 				__FUNCTION__, ctx->Init_FIFO_credit[i], ctx->FIFO_credit[i]));
1990*4882a593Smuzhiyun 		}
1991*4882a593Smuzhiyun 	}
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 	/* update the AC FIFO credit map */
1994*4882a593Smuzhiyun 	ctx->FIFO_credit[0] += (credits[0] - ctx->Init_FIFO_credit[0]);
1995*4882a593Smuzhiyun 	ctx->FIFO_credit[1] += (credits[1] - ctx->Init_FIFO_credit[1]);
1996*4882a593Smuzhiyun 	ctx->FIFO_credit[2] += (credits[2] - ctx->Init_FIFO_credit[2]);
1997*4882a593Smuzhiyun 	ctx->FIFO_credit[3] += (credits[3] - ctx->Init_FIFO_credit[3]);
1998*4882a593Smuzhiyun 	ctx->FIFO_credit[4] += (credits[4] - ctx->Init_FIFO_credit[4]);
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 	ctx->Init_FIFO_credit[0] = credits[0];
2001*4882a593Smuzhiyun 	ctx->Init_FIFO_credit[1] = credits[1];
2002*4882a593Smuzhiyun 	ctx->Init_FIFO_credit[2] = credits[2];
2003*4882a593Smuzhiyun 	ctx->Init_FIFO_credit[3] = credits[3];
2004*4882a593Smuzhiyun 	ctx->Init_FIFO_credit[4] = credits[4];
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun 	/* credit for ATIM FIFO is not used yet. */
2007*4882a593Smuzhiyun 	ctx->Init_FIFO_credit[5] = ctx->FIFO_credit[5] = 0;
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	return BCME_OK;
2010*4882a593Smuzhiyun }
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun /**
2013*4882a593Smuzhiyun  * Called during committing of a transmit packet from the OS DHD layer to the next layer towards
2014*4882a593Smuzhiyun  * the dongle (eg the DBUS layer). All transmit packets flow via this function to the next layer.
2015*4882a593Smuzhiyun  *
2016*4882a593Smuzhiyun  *     @param[in/out] ctx      Driver specific flow control administration
2017*4882a593Smuzhiyun  *     @param[in] ac           Access Category (QoS) of called supplied packet
2018*4882a593Smuzhiyun  *     @param[in] commit_info  Contains eg the packet to send
2019*4882a593Smuzhiyun  *     @param[in] fcommit      Function pointer to transmit function of next software layer
2020*4882a593Smuzhiyun  *     @param[in] commit_ctx   Opaque context used when calling next layer
2021*4882a593Smuzhiyun  */
2022*4882a593Smuzhiyun static int
_dhd_wlfc_handle_packet_commit(athost_wl_status_info_t * ctx,int ac,dhd_wlfc_commit_info_t * commit_info,f_commitpkt_t fcommit,void * commit_ctx)2023*4882a593Smuzhiyun _dhd_wlfc_handle_packet_commit(athost_wl_status_info_t* ctx, int ac,
2024*4882a593Smuzhiyun     dhd_wlfc_commit_info_t *commit_info, f_commitpkt_t fcommit, void* commit_ctx)
2025*4882a593Smuzhiyun {
2026*4882a593Smuzhiyun 	uint32 hslot;
2027*4882a593Smuzhiyun 	int	rc;
2028*4882a593Smuzhiyun 	dhd_pub_t *dhdp = (dhd_pub_t *)(ctx->dhdp);
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun 	/*
2031*4882a593Smuzhiyun 		if ac_fifo_credit_spent = 0
2032*4882a593Smuzhiyun 
2033*4882a593Smuzhiyun 		This packet will not count against the FIFO credit.
2034*4882a593Smuzhiyun 		To ensure the txstatus corresponding to this packet
2035*4882a593Smuzhiyun 		does not provide an implied credit (default behavior)
2036*4882a593Smuzhiyun 		mark the packet accordingly.
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 		if ac_fifo_credit_spent = 1
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun 		This is a normal packet and it counts against the FIFO
2041*4882a593Smuzhiyun 		credit count.
2042*4882a593Smuzhiyun 	*/
2043*4882a593Smuzhiyun 	DHD_PKTTAG_SETCREDITCHECK(PKTTAG(commit_info->p), commit_info->ac_fifo_credit_spent);
2044*4882a593Smuzhiyun 	rc = _dhd_wlfc_pretx_pktprocess(ctx, commit_info->mac_entry, &commit_info->p,
2045*4882a593Smuzhiyun 	     commit_info->needs_hdr, &hslot);
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun 	if (rc == BCME_OK) {
2048*4882a593Smuzhiyun 		rc = fcommit(commit_ctx, commit_info->p);
2049*4882a593Smuzhiyun 		if (rc == BCME_OK) {
2050*4882a593Smuzhiyun 			uint8 gen = WL_TXSTATUS_GET_GENERATION(
2051*4882a593Smuzhiyun 				DHD_PKTTAG_H2DTAG(PKTTAG(commit_info->p)));
2052*4882a593Smuzhiyun 			ctx->stats.pkt2bus++;
2053*4882a593Smuzhiyun 			if (commit_info->ac_fifo_credit_spent || (ac == AC_COUNT)) {
2054*4882a593Smuzhiyun 				ctx->stats.send_pkts[ac]++;
2055*4882a593Smuzhiyun 				WLFC_HOST_FIFO_CREDIT_INC_SENTCTRS(ctx, ac);
2056*4882a593Smuzhiyun 			}
2057*4882a593Smuzhiyun 
2058*4882a593Smuzhiyun 			if (gen != commit_info->mac_entry->generation) {
2059*4882a593Smuzhiyun 				/* will be suppressed back by design */
2060*4882a593Smuzhiyun 				if (!commit_info->mac_entry->suppressed) {
2061*4882a593Smuzhiyun 					commit_info->mac_entry->suppressed = TRUE;
2062*4882a593Smuzhiyun 				}
2063*4882a593Smuzhiyun 				commit_info->mac_entry->suppr_transit_count++;
2064*4882a593Smuzhiyun 			}
2065*4882a593Smuzhiyun 			commit_info->mac_entry->transit_count++;
2066*4882a593Smuzhiyun 			commit_info->mac_entry->onbus_pkts_count++;
2067*4882a593Smuzhiyun 		} else if (commit_info->needs_hdr) {
2068*4882a593Smuzhiyun 			if (!WLFC_GET_AFQ(dhdp->wlfc_mode)) {
2069*4882a593Smuzhiyun 				void *pout = NULL;
2070*4882a593Smuzhiyun 				/* pop hanger for delayed packet */
2071*4882a593Smuzhiyun 				_dhd_wlfc_hanger_poppkt(ctx->hanger, WL_TXSTATUS_GET_HSLOT(
2072*4882a593Smuzhiyun 					DHD_PKTTAG_H2DTAG(PKTTAG(commit_info->p))), &pout, TRUE);
2073*4882a593Smuzhiyun 				ASSERT(commit_info->p == pout);
2074*4882a593Smuzhiyun 			}
2075*4882a593Smuzhiyun 		}
2076*4882a593Smuzhiyun 	} else {
2077*4882a593Smuzhiyun 		ctx->stats.generic_error++;
2078*4882a593Smuzhiyun 	}
2079*4882a593Smuzhiyun 
2080*4882a593Smuzhiyun 	if (rc != BCME_OK) {
2081*4882a593Smuzhiyun 		/*
2082*4882a593Smuzhiyun 		   pretx pkt process or bus commit has failed, rollback.
2083*4882a593Smuzhiyun 		   - remove wl-header for a delayed packet
2084*4882a593Smuzhiyun 		   - save wl-header header for suppressed packets
2085*4882a593Smuzhiyun 		   - reset credit check flag
2086*4882a593Smuzhiyun 		*/
2087*4882a593Smuzhiyun 		_dhd_wlfc_rollback_packet_toq(ctx, commit_info->p, commit_info->pkt_type, hslot);
2088*4882a593Smuzhiyun 		DHD_PKTTAG_SETCREDITCHECK(PKTTAG(commit_info->p), 0);
2089*4882a593Smuzhiyun 	}
2090*4882a593Smuzhiyun 
2091*4882a593Smuzhiyun 	return rc;
2092*4882a593Smuzhiyun } /* _dhd_wlfc_handle_packet_commit */
2093*4882a593Smuzhiyun 
2094*4882a593Smuzhiyun /** Returns remote MAC descriptor for caller supplied MAC address */
2095*4882a593Smuzhiyun static uint8
_dhd_wlfc_find_mac_desc_id_from_mac(dhd_pub_t * dhdp,uint8 * ea)2096*4882a593Smuzhiyun _dhd_wlfc_find_mac_desc_id_from_mac(dhd_pub_t *dhdp, uint8 *ea)
2097*4882a593Smuzhiyun {
2098*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* table =
2099*4882a593Smuzhiyun 		((athost_wl_status_info_t*)dhdp->wlfc_state)->destination_entries.nodes;
2100*4882a593Smuzhiyun 	uint8 table_index;
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	if (ea != NULL) {
2103*4882a593Smuzhiyun 		for (table_index = 0; table_index < WLFC_MAC_DESC_TABLE_SIZE; table_index++) {
2104*4882a593Smuzhiyun 			if ((memcmp(ea, &table[table_index].ea[0], ETHER_ADDR_LEN) == 0) &&
2105*4882a593Smuzhiyun 				table[table_index].occupied)
2106*4882a593Smuzhiyun 				return table_index;
2107*4882a593Smuzhiyun 		}
2108*4882a593Smuzhiyun 	}
2109*4882a593Smuzhiyun 	return WLFC_MAC_DESC_ID_INVALID;
2110*4882a593Smuzhiyun }
2111*4882a593Smuzhiyun 
2112*4882a593Smuzhiyun /**
2113*4882a593Smuzhiyun  * Called when the host receives a WLFC_CTL_TYPE_TXSTATUS event from the dongle, indicating the
2114*4882a593Smuzhiyun  * status of a frame that the dongle attempted to transmit over the wireless medium.
2115*4882a593Smuzhiyun  */
2116*4882a593Smuzhiyun static int
dhd_wlfc_suppressed_acked_update(dhd_pub_t * dhd,uint16 hslot,uint8 prec,uint8 hcnt)2117*4882a593Smuzhiyun dhd_wlfc_suppressed_acked_update(dhd_pub_t *dhd, uint16 hslot, uint8 prec, uint8 hcnt)
2118*4882a593Smuzhiyun {
2119*4882a593Smuzhiyun 	athost_wl_status_info_t* ctx;
2120*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry = NULL;
2121*4882a593Smuzhiyun 	struct pktq *pq;
2122*4882a593Smuzhiyun 	struct pktq_prec *q;
2123*4882a593Smuzhiyun 	void *p, *b;
2124*4882a593Smuzhiyun 
2125*4882a593Smuzhiyun 	if (!dhd) {
2126*4882a593Smuzhiyun 		DHD_ERROR(("%s: dhd(%p)\n", __FUNCTION__, dhd));
2127*4882a593Smuzhiyun 		return BCME_BADARG;
2128*4882a593Smuzhiyun 	}
2129*4882a593Smuzhiyun 	ctx = (athost_wl_status_info_t*)dhd->wlfc_state;
2130*4882a593Smuzhiyun 	if (!ctx) {
2131*4882a593Smuzhiyun 		DHD_ERROR(("%s: ctx(%p)\n", __FUNCTION__, ctx));
2132*4882a593Smuzhiyun 		return BCME_ERROR;
2133*4882a593Smuzhiyun 	}
2134*4882a593Smuzhiyun 
2135*4882a593Smuzhiyun 	ASSERT(hslot < (WLFC_MAC_DESC_TABLE_SIZE + WLFC_MAX_IFNUM + 1));
2136*4882a593Smuzhiyun 
2137*4882a593Smuzhiyun 	if (hslot < WLFC_MAC_DESC_TABLE_SIZE)
2138*4882a593Smuzhiyun 		entry  = &ctx->destination_entries.nodes[hslot];
2139*4882a593Smuzhiyun 	else if (hslot < (WLFC_MAC_DESC_TABLE_SIZE + WLFC_MAX_IFNUM))
2140*4882a593Smuzhiyun 		entry = &ctx->destination_entries.interfaces[hslot - WLFC_MAC_DESC_TABLE_SIZE];
2141*4882a593Smuzhiyun 	else
2142*4882a593Smuzhiyun 		entry = &ctx->destination_entries.other;
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun 	pq = &entry->psq;
2145*4882a593Smuzhiyun 
2146*4882a593Smuzhiyun 	ASSERT(((prec << 1) + 1) < pq->num_prec);
2147*4882a593Smuzhiyun 
2148*4882a593Smuzhiyun 	q = &pq->q[((prec << 1) + 1)];
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	b = NULL;
2151*4882a593Smuzhiyun 	p = q->head;
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun 	while (p && (hcnt != WL_TXSTATUS_GET_FREERUNCTR(DHD_PKTTAG_H2DTAG(PKTTAG(p))))) {
2154*4882a593Smuzhiyun 		b = p;
2155*4882a593Smuzhiyun 		p = PKTLINK(p);
2156*4882a593Smuzhiyun 	}
2157*4882a593Smuzhiyun 
2158*4882a593Smuzhiyun 	if (p == NULL) {
2159*4882a593Smuzhiyun 		/* none is matched */
2160*4882a593Smuzhiyun 		if (b) {
2161*4882a593Smuzhiyun 			DHD_ERROR(("%s: can't find matching seq(%d)\n", __FUNCTION__, hcnt));
2162*4882a593Smuzhiyun 		} else {
2163*4882a593Smuzhiyun 			DHD_ERROR(("%s: queue is empty\n", __FUNCTION__));
2164*4882a593Smuzhiyun 		}
2165*4882a593Smuzhiyun 
2166*4882a593Smuzhiyun 		return BCME_ERROR;
2167*4882a593Smuzhiyun 	}
2168*4882a593Smuzhiyun 
2169*4882a593Smuzhiyun 	if (!b) {
2170*4882a593Smuzhiyun 		/* head packet is matched */
2171*4882a593Smuzhiyun 		if ((q->head = PKTLINK(p)) == NULL) {
2172*4882a593Smuzhiyun 			q->tail = NULL;
2173*4882a593Smuzhiyun 		}
2174*4882a593Smuzhiyun 	} else {
2175*4882a593Smuzhiyun 		/* middle packet is matched */
2176*4882a593Smuzhiyun 		PKTSETLINK(b, PKTLINK(p));
2177*4882a593Smuzhiyun 		if (PKTLINK(p) == NULL) {
2178*4882a593Smuzhiyun 			q->tail = b;
2179*4882a593Smuzhiyun 		}
2180*4882a593Smuzhiyun 	}
2181*4882a593Smuzhiyun 
2182*4882a593Smuzhiyun 	q->n_pkts--;
2183*4882a593Smuzhiyun 	pq->n_pkts_tot--;
2184*4882a593Smuzhiyun 
2185*4882a593Smuzhiyun #ifdef WL_TXQ_STALL
2186*4882a593Smuzhiyun 	q->dequeue_count++;
2187*4882a593Smuzhiyun #endif // endif
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 	ctx->pkt_cnt_in_q[DHD_PKTTAG_IF(PKTTAG(p))][prec]--;
2190*4882a593Smuzhiyun 	ctx->pkt_cnt_per_ac[prec]--;
2191*4882a593Smuzhiyun 
2192*4882a593Smuzhiyun 	PKTSETLINK(p, NULL);
2193*4882a593Smuzhiyun 
2194*4882a593Smuzhiyun 	if (WLFC_GET_AFQ(dhd->wlfc_mode)) {
2195*4882a593Smuzhiyun 		_dhd_wlfc_enque_afq(ctx, p);
2196*4882a593Smuzhiyun 	} else {
2197*4882a593Smuzhiyun 		_dhd_wlfc_hanger_pushpkt(ctx->hanger, p, hslot);
2198*4882a593Smuzhiyun 	}
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun 	entry->transit_count++;
2201*4882a593Smuzhiyun 
2202*4882a593Smuzhiyun 	return BCME_OK;
2203*4882a593Smuzhiyun }
2204*4882a593Smuzhiyun 
2205*4882a593Smuzhiyun static int
_dhd_wlfc_compressed_txstatus_update(dhd_pub_t * dhd,uint8 * pkt_info,uint8 len,void ** p_mac)2206*4882a593Smuzhiyun _dhd_wlfc_compressed_txstatus_update(dhd_pub_t *dhd, uint8* pkt_info, uint8 len, void** p_mac)
2207*4882a593Smuzhiyun {
2208*4882a593Smuzhiyun 	uint8 status_flag_ori, status_flag;
2209*4882a593Smuzhiyun 	uint32 status;
2210*4882a593Smuzhiyun 	int ret = BCME_OK;
2211*4882a593Smuzhiyun 	int remove_from_hanger_ori, remove_from_hanger = 1;
2212*4882a593Smuzhiyun 	void* pktbuf = NULL;
2213*4882a593Smuzhiyun 	uint8 fifo_id = 0, gen = 0, count = 0, hcnt;
2214*4882a593Smuzhiyun 	uint16 hslot;
2215*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry = NULL;
2216*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2217*4882a593Smuzhiyun 	uint16 seq = 0, seq_fromfw = 0, seq_num = 0;
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	memcpy(&status, pkt_info, sizeof(uint32));
2220*4882a593Smuzhiyun 	status = ltoh32(status);
2221*4882a593Smuzhiyun 	status_flag = WL_TXSTATUS_GET_FLAGS(status);
2222*4882a593Smuzhiyun 	hcnt = WL_TXSTATUS_GET_FREERUNCTR(status);
2223*4882a593Smuzhiyun 	hslot = WL_TXSTATUS_GET_HSLOT(status);
2224*4882a593Smuzhiyun 	fifo_id = WL_TXSTATUS_GET_FIFO(status);
2225*4882a593Smuzhiyun 	gen = WL_TXSTATUS_GET_GENERATION(status);
2226*4882a593Smuzhiyun 
2227*4882a593Smuzhiyun 	if (WLFC_GET_REUSESEQ(dhd->wlfc_mode)) {
2228*4882a593Smuzhiyun 		memcpy(&seq, pkt_info + WLFC_CTL_VALUE_LEN_TXSTATUS, WLFC_CTL_VALUE_LEN_SEQ);
2229*4882a593Smuzhiyun 		seq = ltoh16(seq);
2230*4882a593Smuzhiyun 		seq_fromfw = GET_WL_HAS_ASSIGNED_SEQ(seq);
2231*4882a593Smuzhiyun 		seq_num = WL_SEQ_GET_NUM(seq);
2232*4882a593Smuzhiyun 	}
2233*4882a593Smuzhiyun 
2234*4882a593Smuzhiyun 	wlfc->stats.txstatus_in += len;
2235*4882a593Smuzhiyun 
2236*4882a593Smuzhiyun 	if (status_flag == WLFC_CTL_PKTFLAG_DISCARD) {
2237*4882a593Smuzhiyun 		wlfc->stats.pkt_freed += len;
2238*4882a593Smuzhiyun 	} else if (status_flag == WLFC_CTL_PKTFLAG_DISCARD_NOACK) {
2239*4882a593Smuzhiyun 		wlfc->stats.pkt_freed += len;
2240*4882a593Smuzhiyun 	} else if (status_flag == WLFC_CTL_PKTFLAG_D11SUPPRESS) {
2241*4882a593Smuzhiyun 		wlfc->stats.d11_suppress += len;
2242*4882a593Smuzhiyun 		remove_from_hanger = 0;
2243*4882a593Smuzhiyun 	} else if (status_flag == WLFC_CTL_PKTFLAG_WLSUPPRESS) {
2244*4882a593Smuzhiyun 		wlfc->stats.wl_suppress += len;
2245*4882a593Smuzhiyun 		remove_from_hanger = 0;
2246*4882a593Smuzhiyun 	} else if (status_flag == WLFC_CTL_PKTFLAG_TOSSED_BYWLC) {
2247*4882a593Smuzhiyun 		wlfc->stats.wlc_tossed_pkts += len;
2248*4882a593Smuzhiyun 	} else if (status_flag == WLFC_CTL_PKTFLAG_SUPPRESS_ACKED) {
2249*4882a593Smuzhiyun 		wlfc->stats.pkt_freed += len;
2250*4882a593Smuzhiyun 	} else if (status_flag == WLFC_CTL_PKTFLAG_EXPIRED) {
2251*4882a593Smuzhiyun 		wlfc->stats.pkt_exptime += len;
2252*4882a593Smuzhiyun 	} else if (status_flag == WLFC_CTL_PKTFLAG_DROPPED) {
2253*4882a593Smuzhiyun 		wlfc->stats.pkt_dropped += len;
2254*4882a593Smuzhiyun 	}
2255*4882a593Smuzhiyun 
2256*4882a593Smuzhiyun 	if (dhd->proptxstatus_txstatus_ignore) {
2257*4882a593Smuzhiyun 		if (!remove_from_hanger) {
2258*4882a593Smuzhiyun 			DHD_ERROR(("suppress txstatus: %d\n", status_flag));
2259*4882a593Smuzhiyun 		}
2260*4882a593Smuzhiyun 		return BCME_OK;
2261*4882a593Smuzhiyun 	}
2262*4882a593Smuzhiyun 
2263*4882a593Smuzhiyun 	status_flag_ori = status_flag;
2264*4882a593Smuzhiyun 	remove_from_hanger_ori = remove_from_hanger;
2265*4882a593Smuzhiyun 
2266*4882a593Smuzhiyun 	while (count < len) {
2267*4882a593Smuzhiyun 		if (status_flag == WLFC_CTL_PKTFLAG_SUPPRESS_ACKED) {
2268*4882a593Smuzhiyun 			dhd_wlfc_suppressed_acked_update(dhd, hslot, fifo_id, hcnt);
2269*4882a593Smuzhiyun 		}
2270*4882a593Smuzhiyun 		if (WLFC_GET_AFQ(dhd->wlfc_mode)) {
2271*4882a593Smuzhiyun 			ret = _dhd_wlfc_deque_afq(wlfc, hslot, hcnt, fifo_id, &pktbuf);
2272*4882a593Smuzhiyun 		} else {
2273*4882a593Smuzhiyun 			status_flag = status_flag_ori;
2274*4882a593Smuzhiyun 			remove_from_hanger = remove_from_hanger_ori;
2275*4882a593Smuzhiyun 			ret = _dhd_wlfc_hanger_poppkt(wlfc->hanger, hslot, &pktbuf, FALSE);
2276*4882a593Smuzhiyun 			if (!pktbuf) {
2277*4882a593Smuzhiyun 				_dhd_wlfc_hanger_free_pkt(wlfc, hslot,
2278*4882a593Smuzhiyun 					WLFC_HANGER_PKT_STATE_TXSTATUS, -1);
2279*4882a593Smuzhiyun 				goto cont;
2280*4882a593Smuzhiyun 			} else {
2281*4882a593Smuzhiyun 				wlfc_hanger_t* h = (wlfc_hanger_t*)wlfc->hanger;
2282*4882a593Smuzhiyun 				if (h->items[hslot].state == WLFC_HANGER_ITEM_STATE_FLUSHED) {
2283*4882a593Smuzhiyun 					status_flag = WLFC_CTL_PKTFLAG_DISCARD;
2284*4882a593Smuzhiyun 					remove_from_hanger = 1;
2285*4882a593Smuzhiyun 				}
2286*4882a593Smuzhiyun 			}
2287*4882a593Smuzhiyun 		}
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun 		if ((ret != BCME_OK) || !pktbuf) {
2290*4882a593Smuzhiyun 			goto cont;
2291*4882a593Smuzhiyun 		}
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 		bcm_pkt_validate_chk(pktbuf);
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 		/* set fifo_id to correct value because not all FW does that */
2296*4882a593Smuzhiyun 		fifo_id = DHD_PKTTAG_FIFO(PKTTAG(pktbuf));
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun 		entry = _dhd_wlfc_find_table_entry(wlfc, pktbuf);
2299*4882a593Smuzhiyun 
2300*4882a593Smuzhiyun 		if (!remove_from_hanger) {
2301*4882a593Smuzhiyun 			/* this packet was suppressed */
2302*4882a593Smuzhiyun 			if (!entry->suppressed || (entry->generation != gen)) {
2303*4882a593Smuzhiyun 				if (!entry->suppressed) {
2304*4882a593Smuzhiyun 					entry->suppr_transit_count = entry->transit_count;
2305*4882a593Smuzhiyun 					if (p_mac) {
2306*4882a593Smuzhiyun 						*p_mac = entry;
2307*4882a593Smuzhiyun 					}
2308*4882a593Smuzhiyun 				} else {
2309*4882a593Smuzhiyun 					DHD_ERROR(("gen(%d), entry->generation(%d)\n",
2310*4882a593Smuzhiyun 						gen, entry->generation));
2311*4882a593Smuzhiyun 				}
2312*4882a593Smuzhiyun 				entry->suppressed = TRUE;
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun 			}
2315*4882a593Smuzhiyun 			entry->generation = gen;
2316*4882a593Smuzhiyun 		}
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
2319*4882a593Smuzhiyun 		if (!WLFC_GET_AFQ(dhd->wlfc_mode))
2320*4882a593Smuzhiyun 		{
2321*4882a593Smuzhiyun 			uint32 new_t = OSL_SYSUPTIME();
2322*4882a593Smuzhiyun 			uint32 old_t;
2323*4882a593Smuzhiyun 			uint32 delta;
2324*4882a593Smuzhiyun 			old_t = ((wlfc_hanger_t*)(wlfc->hanger))->items[hslot].push_time;
2325*4882a593Smuzhiyun 
2326*4882a593Smuzhiyun 			wlfc->stats.latency_sample_count++;
2327*4882a593Smuzhiyun 			if (new_t > old_t)
2328*4882a593Smuzhiyun 				delta = new_t - old_t;
2329*4882a593Smuzhiyun 			else
2330*4882a593Smuzhiyun 				delta = 0xffffffff + new_t - old_t;
2331*4882a593Smuzhiyun 			wlfc->stats.total_status_latency += delta;
2332*4882a593Smuzhiyun 			wlfc->stats.latency_most_recent = delta;
2333*4882a593Smuzhiyun 
2334*4882a593Smuzhiyun 			wlfc->stats.deltas[wlfc->stats.idx_delta++] = delta;
2335*4882a593Smuzhiyun 			if (wlfc->stats.idx_delta == sizeof(wlfc->stats.deltas)/sizeof(uint32))
2336*4882a593Smuzhiyun 				wlfc->stats.idx_delta = 0;
2337*4882a593Smuzhiyun 		}
2338*4882a593Smuzhiyun #endif /* PROP_TXSTATUS_DEBUG */
2339*4882a593Smuzhiyun 
2340*4882a593Smuzhiyun 		/* pick up the implicit credit from this packet */
2341*4882a593Smuzhiyun 		if (DHD_PKTTAG_CREDITCHECK(PKTTAG(pktbuf))) {
2342*4882a593Smuzhiyun 			_dhd_wlfc_return_implied_credit(wlfc, pktbuf);
2343*4882a593Smuzhiyun 		} else {
2344*4882a593Smuzhiyun 			/*
2345*4882a593Smuzhiyun 			if this packet did not count against FIFO credit, it must have
2346*4882a593Smuzhiyun 			taken a requested_credit from the destination entry (for pspoll etc.)
2347*4882a593Smuzhiyun 			*/
2348*4882a593Smuzhiyun 			if (!DHD_PKTTAG_ONETIMEPKTRQST(PKTTAG(pktbuf))) {
2349*4882a593Smuzhiyun 				entry->requested_credit++;
2350*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
2351*4882a593Smuzhiyun 				_dhd_wlfc_thread_wakeup(dhd);
2352*4882a593Smuzhiyun #endif /* DHD_WLFC_THREAD */
2353*4882a593Smuzhiyun 			}
2354*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
2355*4882a593Smuzhiyun 			entry->dstncredit_acks++;
2356*4882a593Smuzhiyun #endif // endif
2357*4882a593Smuzhiyun 		}
2358*4882a593Smuzhiyun 
2359*4882a593Smuzhiyun 		if ((status_flag == WLFC_CTL_PKTFLAG_D11SUPPRESS) ||
2360*4882a593Smuzhiyun 			(status_flag == WLFC_CTL_PKTFLAG_WLSUPPRESS)) {
2361*4882a593Smuzhiyun 			/* save generation bit inside packet */
2362*4882a593Smuzhiyun 			WL_TXSTATUS_SET_GENERATION(DHD_PKTTAG_H2DTAG(PKTTAG(pktbuf)), gen);
2363*4882a593Smuzhiyun 
2364*4882a593Smuzhiyun 			if (WLFC_GET_REUSESEQ(dhd->wlfc_mode)) {
2365*4882a593Smuzhiyun 				WL_SEQ_SET_REUSE(DHD_PKTTAG_H2DSEQ(PKTTAG(pktbuf)), seq_fromfw);
2366*4882a593Smuzhiyun 				WL_SEQ_SET_NUM(DHD_PKTTAG_H2DSEQ(PKTTAG(pktbuf)), seq_num);
2367*4882a593Smuzhiyun 			}
2368*4882a593Smuzhiyun 
2369*4882a593Smuzhiyun 			ret = _dhd_wlfc_enque_suppressed(wlfc, fifo_id, pktbuf);
2370*4882a593Smuzhiyun 			if (ret != BCME_OK) {
2371*4882a593Smuzhiyun 				/* delay q is full, drop this packet */
2372*4882a593Smuzhiyun 				DHD_WLFC_QMON_COMPLETE(entry);
2373*4882a593Smuzhiyun 				_dhd_wlfc_prec_drop(dhd, (fifo_id << 1) + 1, pktbuf, FALSE);
2374*4882a593Smuzhiyun 			} else {
2375*4882a593Smuzhiyun 				if (!WLFC_GET_AFQ(dhd->wlfc_mode)) {
2376*4882a593Smuzhiyun 					/* Mark suppressed to avoid a double free
2377*4882a593Smuzhiyun 					during wlfc cleanup
2378*4882a593Smuzhiyun 					*/
2379*4882a593Smuzhiyun 					_dhd_wlfc_hanger_mark_suppressed(wlfc->hanger, hslot, gen);
2380*4882a593Smuzhiyun 				}
2381*4882a593Smuzhiyun 			}
2382*4882a593Smuzhiyun 		} else {
2383*4882a593Smuzhiyun 
2384*4882a593Smuzhiyun 			DHD_WLFC_QMON_COMPLETE(entry);
2385*4882a593Smuzhiyun 
2386*4882a593Smuzhiyun 			if (!WLFC_GET_AFQ(dhd->wlfc_mode)) {
2387*4882a593Smuzhiyun 				_dhd_wlfc_hanger_free_pkt(wlfc, hslot,
2388*4882a593Smuzhiyun 					WLFC_HANGER_PKT_STATE_TXSTATUS, TRUE);
2389*4882a593Smuzhiyun 			} else {
2390*4882a593Smuzhiyun 				dhd_txcomplete(dhd, pktbuf, TRUE);
2391*4882a593Smuzhiyun 				wlfc->pkt_cnt_in_drv[DHD_PKTTAG_IF(PKTTAG(pktbuf))]
2392*4882a593Smuzhiyun 					[DHD_PKTTAG_FIFO(PKTTAG(pktbuf))]--;
2393*4882a593Smuzhiyun 				wlfc->stats.pktout++;
2394*4882a593Smuzhiyun 				/* free the packet */
2395*4882a593Smuzhiyun 				PKTFREE(wlfc->osh, pktbuf, TRUE);
2396*4882a593Smuzhiyun 			}
2397*4882a593Smuzhiyun 		}
2398*4882a593Smuzhiyun 		/* pkt back from firmware side */
2399*4882a593Smuzhiyun 		if (entry->transit_count)
2400*4882a593Smuzhiyun 			entry->transit_count--;
2401*4882a593Smuzhiyun 		if (entry->suppr_transit_count) {
2402*4882a593Smuzhiyun 			entry->suppr_transit_count--;
2403*4882a593Smuzhiyun 			if (entry->suppressed &&
2404*4882a593Smuzhiyun 				(!entry->onbus_pkts_count) &&
2405*4882a593Smuzhiyun 				(!entry->suppr_transit_count))
2406*4882a593Smuzhiyun 				entry->suppressed = FALSE;
2407*4882a593Smuzhiyun 		}
2408*4882a593Smuzhiyun 
2409*4882a593Smuzhiyun cont:
2410*4882a593Smuzhiyun 		hcnt = (hcnt + 1) & WL_TXSTATUS_FREERUNCTR_MASK;
2411*4882a593Smuzhiyun 		if (!WLFC_GET_AFQ(dhd->wlfc_mode)) {
2412*4882a593Smuzhiyun 			hslot = (hslot + 1) & WL_TXSTATUS_HSLOT_MASK;
2413*4882a593Smuzhiyun 		}
2414*4882a593Smuzhiyun 
2415*4882a593Smuzhiyun 		if (WLFC_GET_REUSESEQ(dhd->wlfc_mode) && seq_fromfw) {
2416*4882a593Smuzhiyun 			seq_num = (seq_num + 1) & WL_SEQ_NUM_MASK;
2417*4882a593Smuzhiyun 		}
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun 		count++;
2420*4882a593Smuzhiyun 	}
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun 	return BCME_OK;
2423*4882a593Smuzhiyun } /* _dhd_wlfc_compressed_txstatus_update */
2424*4882a593Smuzhiyun 
2425*4882a593Smuzhiyun /**
2426*4882a593Smuzhiyun  * Called when eg host receives a 'WLFC_CTL_TYPE_FIFO_CREDITBACK' event from the dongle.
2427*4882a593Smuzhiyun  *    @param[in] credits caller supplied credit that will be added to the host credit.
2428*4882a593Smuzhiyun  */
2429*4882a593Smuzhiyun static int
_dhd_wlfc_fifocreditback_indicate(dhd_pub_t * dhd,uint8 * credits)2430*4882a593Smuzhiyun _dhd_wlfc_fifocreditback_indicate(dhd_pub_t *dhd, uint8* credits)
2431*4882a593Smuzhiyun {
2432*4882a593Smuzhiyun 	int i;
2433*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2434*4882a593Smuzhiyun 	for (i = 0; i < WLFC_CTL_VALUE_LEN_FIFO_CREDITBACK; i++) {
2435*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
2436*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_back[i] += credits[i];
2437*4882a593Smuzhiyun #endif // endif
2438*4882a593Smuzhiyun 
2439*4882a593Smuzhiyun 		/* update FIFO credits */
2440*4882a593Smuzhiyun 		if (dhd->proptxstatus_mode == WLFC_FCMODE_EXPLICIT_CREDIT)
2441*4882a593Smuzhiyun 		{
2442*4882a593Smuzhiyun 			int lender; /* Note that borrower is i */
2443*4882a593Smuzhiyun 
2444*4882a593Smuzhiyun 			/* Return credits to highest priority lender first */
2445*4882a593Smuzhiyun 			for (lender = AC_COUNT; (lender >= 0) && (credits[i] > 0); lender--) {
2446*4882a593Smuzhiyun 				if (wlfc->credits_borrowed[i][lender] > 0) {
2447*4882a593Smuzhiyun 					if (credits[i] >= wlfc->credits_borrowed[i][lender]) {
2448*4882a593Smuzhiyun 						credits[i] -=
2449*4882a593Smuzhiyun 							(uint8)wlfc->credits_borrowed[i][lender];
2450*4882a593Smuzhiyun 						wlfc->FIFO_credit[lender] +=
2451*4882a593Smuzhiyun 						    wlfc->credits_borrowed[i][lender];
2452*4882a593Smuzhiyun 						wlfc->credits_borrowed[i][lender] = 0;
2453*4882a593Smuzhiyun 					} else {
2454*4882a593Smuzhiyun 						wlfc->credits_borrowed[i][lender] -= credits[i];
2455*4882a593Smuzhiyun 						wlfc->FIFO_credit[lender] += credits[i];
2456*4882a593Smuzhiyun 						credits[i] = 0;
2457*4882a593Smuzhiyun 					}
2458*4882a593Smuzhiyun 				}
2459*4882a593Smuzhiyun 			}
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun 			/* If we have more credits left over, these must belong to the AC */
2462*4882a593Smuzhiyun 			if (credits[i] > 0) {
2463*4882a593Smuzhiyun 				wlfc->FIFO_credit[i] += credits[i];
2464*4882a593Smuzhiyun 			}
2465*4882a593Smuzhiyun 
2466*4882a593Smuzhiyun 			if (wlfc->FIFO_credit[i] > wlfc->Init_FIFO_credit[i]) {
2467*4882a593Smuzhiyun 				wlfc->FIFO_credit[i] = wlfc->Init_FIFO_credit[i];
2468*4882a593Smuzhiyun 			}
2469*4882a593Smuzhiyun 		}
2470*4882a593Smuzhiyun 	}
2471*4882a593Smuzhiyun 
2472*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
2473*4882a593Smuzhiyun 	_dhd_wlfc_thread_wakeup(dhd);
2474*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 	return BCME_OK;
2477*4882a593Smuzhiyun } /* _dhd_wlfc_fifocreditback_indicate */
2478*4882a593Smuzhiyun 
2479*4882a593Smuzhiyun /** !BCMDBUS specific function */
2480*4882a593Smuzhiyun static void
_dhd_wlfc_suppress_txq(dhd_pub_t * dhd,f_processpkt_t fn,void * arg)2481*4882a593Smuzhiyun _dhd_wlfc_suppress_txq(dhd_pub_t *dhd, f_processpkt_t fn, void *arg)
2482*4882a593Smuzhiyun {
2483*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2484*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* entry;
2485*4882a593Smuzhiyun 	int prec;
2486*4882a593Smuzhiyun 	void *pkt = NULL, *head = NULL, *tail = NULL;
2487*4882a593Smuzhiyun 	struct pktq *txq = (struct pktq *)dhd_bus_txq(dhd->bus);
2488*4882a593Smuzhiyun 	uint8	results[WLFC_CTL_VALUE_LEN_TXSTATUS+WLFC_CTL_VALUE_LEN_SEQ];
2489*4882a593Smuzhiyun 	uint8 credits[WLFC_CTL_VALUE_LEN_FIFO_CREDITBACK] = {0};
2490*4882a593Smuzhiyun 	uint32 htod = 0;
2491*4882a593Smuzhiyun 	uint16 htodseq = 0;
2492*4882a593Smuzhiyun 	bool bCreditUpdate = FALSE;
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun 	dhd_os_sdlock_txq(dhd);
2495*4882a593Smuzhiyun 	for (prec = 0; prec < txq->num_prec; prec++) {
2496*4882a593Smuzhiyun 		while ((pkt = _dhd_wlfc_pktq_pdeq_with_fn(txq, prec, fn, arg))) {
2497*4882a593Smuzhiyun 			if (!head) {
2498*4882a593Smuzhiyun 				head = pkt;
2499*4882a593Smuzhiyun 			}
2500*4882a593Smuzhiyun 			if (tail) {
2501*4882a593Smuzhiyun 				PKTSETLINK(tail, pkt);
2502*4882a593Smuzhiyun 			}
2503*4882a593Smuzhiyun 			tail = pkt;
2504*4882a593Smuzhiyun 		}
2505*4882a593Smuzhiyun 	}
2506*4882a593Smuzhiyun 	dhd_os_sdunlock_txq(dhd);
2507*4882a593Smuzhiyun 
2508*4882a593Smuzhiyun 	while ((pkt = head)) {
2509*4882a593Smuzhiyun 		head = PKTLINK(pkt);
2510*4882a593Smuzhiyun 		PKTSETLINK(pkt, NULL);
2511*4882a593Smuzhiyun 
2512*4882a593Smuzhiyun 		entry = _dhd_wlfc_find_table_entry(wlfc, pkt);
2513*4882a593Smuzhiyun 		if (!entry) {
2514*4882a593Smuzhiyun 			PKTFREE(dhd->osh, pkt, TRUE);
2515*4882a593Smuzhiyun 			continue;
2516*4882a593Smuzhiyun 		}
2517*4882a593Smuzhiyun 		if (entry->onbus_pkts_count > 0) {
2518*4882a593Smuzhiyun 			entry->onbus_pkts_count--;
2519*4882a593Smuzhiyun 		}
2520*4882a593Smuzhiyun 		if (entry->suppressed &&
2521*4882a593Smuzhiyun 				(!entry->onbus_pkts_count) &&
2522*4882a593Smuzhiyun 				(!entry->suppr_transit_count)) {
2523*4882a593Smuzhiyun 			entry->suppressed = FALSE;
2524*4882a593Smuzhiyun 		}
2525*4882a593Smuzhiyun 		/* fake a suppression txstatus */
2526*4882a593Smuzhiyun 		htod = DHD_PKTTAG_H2DTAG(PKTTAG(pkt));
2527*4882a593Smuzhiyun 		WL_TXSTATUS_SET_FLAGS(htod, WLFC_CTL_PKTFLAG_WLSUPPRESS);
2528*4882a593Smuzhiyun 		WL_TXSTATUS_SET_GENERATION(htod, entry->generation);
2529*4882a593Smuzhiyun 		htod = htol32(htod);
2530*4882a593Smuzhiyun 		memcpy(results, &htod, WLFC_CTL_VALUE_LEN_TXSTATUS);
2531*4882a593Smuzhiyun 		if (WLFC_GET_REUSESEQ(dhd->wlfc_mode)) {
2532*4882a593Smuzhiyun 			htodseq = DHD_PKTTAG_H2DSEQ(PKTTAG(pkt));
2533*4882a593Smuzhiyun 			if (IS_WL_TO_REUSE_SEQ(htodseq)) {
2534*4882a593Smuzhiyun 				SET_WL_HAS_ASSIGNED_SEQ(htodseq);
2535*4882a593Smuzhiyun 				RESET_WL_TO_REUSE_SEQ(htodseq);
2536*4882a593Smuzhiyun 			}
2537*4882a593Smuzhiyun 			htodseq = htol16(htodseq);
2538*4882a593Smuzhiyun 			memcpy(results + WLFC_CTL_VALUE_LEN_TXSTATUS, &htodseq,
2539*4882a593Smuzhiyun 				WLFC_CTL_VALUE_LEN_SEQ);
2540*4882a593Smuzhiyun 		}
2541*4882a593Smuzhiyun 		if (WLFC_GET_AFQ(dhd->wlfc_mode)) {
2542*4882a593Smuzhiyun 			_dhd_wlfc_enque_afq(wlfc, pkt);
2543*4882a593Smuzhiyun 		}
2544*4882a593Smuzhiyun 		_dhd_wlfc_compressed_txstatus_update(dhd, results, 1, NULL);
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 		/* fake a fifo credit back */
2547*4882a593Smuzhiyun 		if (DHD_PKTTAG_CREDITCHECK(PKTTAG(pkt))) {
2548*4882a593Smuzhiyun 			credits[DHD_PKTTAG_FIFO(PKTTAG(pkt))]++;
2549*4882a593Smuzhiyun 			bCreditUpdate = TRUE;
2550*4882a593Smuzhiyun 		}
2551*4882a593Smuzhiyun 	}
2552*4882a593Smuzhiyun 
2553*4882a593Smuzhiyun 	if (bCreditUpdate) {
2554*4882a593Smuzhiyun 		_dhd_wlfc_fifocreditback_indicate(dhd, credits);
2555*4882a593Smuzhiyun 	}
2556*4882a593Smuzhiyun } /* _dhd_wlfc_suppress_txq */
2557*4882a593Smuzhiyun 
2558*4882a593Smuzhiyun static int
_dhd_wlfc_dbg_senum_check(dhd_pub_t * dhd,uint8 * value)2559*4882a593Smuzhiyun _dhd_wlfc_dbg_senum_check(dhd_pub_t *dhd, uint8 *value)
2560*4882a593Smuzhiyun {
2561*4882a593Smuzhiyun 	uint32 timestamp;
2562*4882a593Smuzhiyun 
2563*4882a593Smuzhiyun 	(void)dhd;
2564*4882a593Smuzhiyun 
2565*4882a593Smuzhiyun 	bcopy(&value[2], &timestamp, sizeof(uint32));
2566*4882a593Smuzhiyun 	timestamp = ltoh32(timestamp);
2567*4882a593Smuzhiyun 	DHD_INFO(("RXPKT: SEQ: %d, timestamp %d\n", value[1], timestamp));
2568*4882a593Smuzhiyun 	return BCME_OK;
2569*4882a593Smuzhiyun }
2570*4882a593Smuzhiyun 
2571*4882a593Smuzhiyun static int
_dhd_wlfc_rssi_indicate(dhd_pub_t * dhd,uint8 * rssi)2572*4882a593Smuzhiyun _dhd_wlfc_rssi_indicate(dhd_pub_t *dhd, uint8* rssi)
2573*4882a593Smuzhiyun {
2574*4882a593Smuzhiyun 	(void)dhd;
2575*4882a593Smuzhiyun 	(void)rssi;
2576*4882a593Smuzhiyun 	return BCME_OK;
2577*4882a593Smuzhiyun }
2578*4882a593Smuzhiyun 
2579*4882a593Smuzhiyun static void
_dhd_wlfc_add_requested_entry(athost_wl_status_info_t * wlfc,wlfc_mac_descriptor_t * entry)2580*4882a593Smuzhiyun _dhd_wlfc_add_requested_entry(athost_wl_status_info_t* wlfc, wlfc_mac_descriptor_t* entry)
2581*4882a593Smuzhiyun {
2582*4882a593Smuzhiyun 	int i;
2583*4882a593Smuzhiyun 
2584*4882a593Smuzhiyun 	if (!wlfc || !entry) {
2585*4882a593Smuzhiyun 		return;
2586*4882a593Smuzhiyun 	}
2587*4882a593Smuzhiyun 
2588*4882a593Smuzhiyun 	for (i = 0; i < wlfc->requested_entry_count; i++) {
2589*4882a593Smuzhiyun 		if (entry == wlfc->requested_entry[i]) {
2590*4882a593Smuzhiyun 			break;
2591*4882a593Smuzhiyun 		}
2592*4882a593Smuzhiyun 	}
2593*4882a593Smuzhiyun 
2594*4882a593Smuzhiyun 	if (i == wlfc->requested_entry_count) {
2595*4882a593Smuzhiyun 		/* no match entry found */
2596*4882a593Smuzhiyun 		ASSERT(wlfc->requested_entry_count <= (WLFC_MAC_DESC_TABLE_SIZE-1));
2597*4882a593Smuzhiyun 		wlfc->requested_entry[wlfc->requested_entry_count++] = entry;
2598*4882a593Smuzhiyun 	}
2599*4882a593Smuzhiyun }
2600*4882a593Smuzhiyun 
2601*4882a593Smuzhiyun /** called on eg receiving 'mac open' event from the dongle. */
2602*4882a593Smuzhiyun static void
_dhd_wlfc_remove_requested_entry(athost_wl_status_info_t * wlfc,wlfc_mac_descriptor_t * entry)2603*4882a593Smuzhiyun _dhd_wlfc_remove_requested_entry(athost_wl_status_info_t* wlfc, wlfc_mac_descriptor_t* entry)
2604*4882a593Smuzhiyun {
2605*4882a593Smuzhiyun 	int i;
2606*4882a593Smuzhiyun 
2607*4882a593Smuzhiyun 	if (!wlfc || !entry) {
2608*4882a593Smuzhiyun 		return;
2609*4882a593Smuzhiyun 	}
2610*4882a593Smuzhiyun 
2611*4882a593Smuzhiyun 	for (i = 0; i < wlfc->requested_entry_count; i++) {
2612*4882a593Smuzhiyun 		if (entry == wlfc->requested_entry[i]) {
2613*4882a593Smuzhiyun 			break;
2614*4882a593Smuzhiyun 		}
2615*4882a593Smuzhiyun 	}
2616*4882a593Smuzhiyun 
2617*4882a593Smuzhiyun 	if (i < wlfc->requested_entry_count) {
2618*4882a593Smuzhiyun 		/* found */
2619*4882a593Smuzhiyun 		ASSERT(wlfc->requested_entry_count > 0);
2620*4882a593Smuzhiyun 		wlfc->requested_entry_count--;
2621*4882a593Smuzhiyun 		if (i != wlfc->requested_entry_count) {
2622*4882a593Smuzhiyun 			wlfc->requested_entry[i] =
2623*4882a593Smuzhiyun 				wlfc->requested_entry[wlfc->requested_entry_count];
2624*4882a593Smuzhiyun 		}
2625*4882a593Smuzhiyun 		wlfc->requested_entry[wlfc->requested_entry_count] = NULL;
2626*4882a593Smuzhiyun 	}
2627*4882a593Smuzhiyun }
2628*4882a593Smuzhiyun 
2629*4882a593Smuzhiyun /** called on eg receiving a WLFC_CTL_TYPE_MACDESC_ADD TLV from the dongle */
2630*4882a593Smuzhiyun static int
_dhd_wlfc_mac_table_update(dhd_pub_t * dhd,uint8 * value,uint8 type)2631*4882a593Smuzhiyun _dhd_wlfc_mac_table_update(dhd_pub_t *dhd, uint8* value, uint8 type)
2632*4882a593Smuzhiyun {
2633*4882a593Smuzhiyun 	int rc;
2634*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2635*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* table;
2636*4882a593Smuzhiyun 	uint8 existing_index;
2637*4882a593Smuzhiyun 	uint8 table_index;
2638*4882a593Smuzhiyun 	uint8 ifid;
2639*4882a593Smuzhiyun 	uint8* ea;
2640*4882a593Smuzhiyun 
2641*4882a593Smuzhiyun 	WLFC_DBGMESG(("%s(), mac ["MACDBG"],%s,idx:%d,id:0x%02x\n",
2642*4882a593Smuzhiyun 		__FUNCTION__, MAC2STRDBG(&value[2]),
2643*4882a593Smuzhiyun 		((type == WLFC_CTL_TYPE_MACDESC_ADD) ? "ADD":"DEL"),
2644*4882a593Smuzhiyun 		WLFC_MAC_DESC_GET_LOOKUP_INDEX(value[0]), value[0]));
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 	table = wlfc->destination_entries.nodes;
2647*4882a593Smuzhiyun 	table_index = WLFC_MAC_DESC_GET_LOOKUP_INDEX(value[0]);
2648*4882a593Smuzhiyun 	ifid = value[1];
2649*4882a593Smuzhiyun 	ea = &value[2];
2650*4882a593Smuzhiyun 
2651*4882a593Smuzhiyun 	_dhd_wlfc_remove_requested_entry(wlfc, &table[table_index]);
2652*4882a593Smuzhiyun 	if (type == WLFC_CTL_TYPE_MACDESC_ADD) {
2653*4882a593Smuzhiyun 		existing_index = _dhd_wlfc_find_mac_desc_id_from_mac(dhd, &value[2]);
2654*4882a593Smuzhiyun 		if ((existing_index != WLFC_MAC_DESC_ID_INVALID) &&
2655*4882a593Smuzhiyun 			(existing_index != table_index) && table[existing_index].occupied) {
2656*4882a593Smuzhiyun 			/*
2657*4882a593Smuzhiyun 			there is an existing different entry, free the old one
2658*4882a593Smuzhiyun 			and move it to new index if necessary.
2659*4882a593Smuzhiyun 			*/
2660*4882a593Smuzhiyun 			rc = _dhd_wlfc_mac_entry_update(wlfc, &table[existing_index],
2661*4882a593Smuzhiyun 				eWLFC_MAC_ENTRY_ACTION_DEL, table[existing_index].interface_id,
2662*4882a593Smuzhiyun 				table[existing_index].iftype, NULL, _dhd_wlfc_entrypkt_fn,
2663*4882a593Smuzhiyun 				&table[existing_index]);
2664*4882a593Smuzhiyun 		}
2665*4882a593Smuzhiyun 
2666*4882a593Smuzhiyun 		if (!table[table_index].occupied) {
2667*4882a593Smuzhiyun 			/* this new MAC entry does not exist, create one */
2668*4882a593Smuzhiyun 			table[table_index].mac_handle = value[0];
2669*4882a593Smuzhiyun 			rc = _dhd_wlfc_mac_entry_update(wlfc, &table[table_index],
2670*4882a593Smuzhiyun 				eWLFC_MAC_ENTRY_ACTION_ADD, ifid,
2671*4882a593Smuzhiyun 				wlfc->destination_entries.interfaces[ifid].iftype,
2672*4882a593Smuzhiyun 				ea, NULL, NULL);
2673*4882a593Smuzhiyun 		} else {
2674*4882a593Smuzhiyun 			/* the space should have been empty, but it's not */
2675*4882a593Smuzhiyun 			wlfc->stats.mac_update_failed++;
2676*4882a593Smuzhiyun 		}
2677*4882a593Smuzhiyun 	}
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 	if (type == WLFC_CTL_TYPE_MACDESC_DEL) {
2680*4882a593Smuzhiyun 		if (table[table_index].occupied) {
2681*4882a593Smuzhiyun 				rc = _dhd_wlfc_mac_entry_update(wlfc, &table[table_index],
2682*4882a593Smuzhiyun 					eWLFC_MAC_ENTRY_ACTION_DEL, ifid,
2683*4882a593Smuzhiyun 					wlfc->destination_entries.interfaces[ifid].iftype,
2684*4882a593Smuzhiyun 					ea, _dhd_wlfc_entrypkt_fn, &table[table_index]);
2685*4882a593Smuzhiyun 		} else {
2686*4882a593Smuzhiyun 			/* the space should have been occupied, but it's not */
2687*4882a593Smuzhiyun 			wlfc->stats.mac_update_failed++;
2688*4882a593Smuzhiyun 		}
2689*4882a593Smuzhiyun 	}
2690*4882a593Smuzhiyun 	BCM_REFERENCE(rc);
2691*4882a593Smuzhiyun 	return BCME_OK;
2692*4882a593Smuzhiyun } /* _dhd_wlfc_mac_table_update */
2693*4882a593Smuzhiyun 
2694*4882a593Smuzhiyun /** Called on a 'mac open' or 'mac close' event indicated by the dongle */
2695*4882a593Smuzhiyun static int
_dhd_wlfc_psmode_update(dhd_pub_t * dhd,uint8 * value,uint8 type)2696*4882a593Smuzhiyun _dhd_wlfc_psmode_update(dhd_pub_t *dhd, uint8* value, uint8 type)
2697*4882a593Smuzhiyun {
2698*4882a593Smuzhiyun 	/* Handle PS on/off indication */
2699*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2700*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* table;
2701*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* desc; /* a table maps from mac handle to mac descriptor */
2702*4882a593Smuzhiyun 	uint8 mac_handle = value[0];
2703*4882a593Smuzhiyun 	int i;
2704*4882a593Smuzhiyun 
2705*4882a593Smuzhiyun 	table = wlfc->destination_entries.nodes;
2706*4882a593Smuzhiyun 	desc = &table[WLFC_MAC_DESC_GET_LOOKUP_INDEX(mac_handle)];
2707*4882a593Smuzhiyun 	if (desc->occupied) {
2708*4882a593Smuzhiyun 		if (type == WLFC_CTL_TYPE_MAC_OPEN) {
2709*4882a593Smuzhiyun 			desc->state = WLFC_STATE_OPEN;
2710*4882a593Smuzhiyun 			desc->ac_bitmap = 0xff;
2711*4882a593Smuzhiyun 			DHD_WLFC_CTRINC_MAC_OPEN(desc);
2712*4882a593Smuzhiyun 			desc->requested_credit = 0;
2713*4882a593Smuzhiyun 			desc->requested_packet = 0;
2714*4882a593Smuzhiyun 			_dhd_wlfc_remove_requested_entry(wlfc, desc);
2715*4882a593Smuzhiyun 		} else {
2716*4882a593Smuzhiyun 			desc->state = WLFC_STATE_CLOSE;
2717*4882a593Smuzhiyun 			DHD_WLFC_CTRINC_MAC_CLOSE(desc);
2718*4882a593Smuzhiyun 			/* Indicate to firmware if there is any traffic pending. */
2719*4882a593Smuzhiyun 			for (i = 0; i < AC_COUNT; i++) {
2720*4882a593Smuzhiyun 				_dhd_wlfc_traffic_pending_check(wlfc, desc, i);
2721*4882a593Smuzhiyun 			}
2722*4882a593Smuzhiyun 		}
2723*4882a593Smuzhiyun 	} else {
2724*4882a593Smuzhiyun 		wlfc->stats.psmode_update_failed++;
2725*4882a593Smuzhiyun 	}
2726*4882a593Smuzhiyun 
2727*4882a593Smuzhiyun 	return BCME_OK;
2728*4882a593Smuzhiyun } /* _dhd_wlfc_psmode_update */
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun /** called upon receiving 'interface open' or 'interface close' event from the dongle */
2731*4882a593Smuzhiyun static int
_dhd_wlfc_interface_update(dhd_pub_t * dhd,uint8 * value,uint8 type)2732*4882a593Smuzhiyun _dhd_wlfc_interface_update(dhd_pub_t *dhd, uint8* value, uint8 type)
2733*4882a593Smuzhiyun {
2734*4882a593Smuzhiyun 	/* Handle PS on/off indication */
2735*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2736*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* table;
2737*4882a593Smuzhiyun 	uint8 if_id = value[0];
2738*4882a593Smuzhiyun 
2739*4882a593Smuzhiyun 	if (if_id < WLFC_MAX_IFNUM) {
2740*4882a593Smuzhiyun 		table = wlfc->destination_entries.interfaces;
2741*4882a593Smuzhiyun 		if (table[if_id].occupied) {
2742*4882a593Smuzhiyun 			if (type == WLFC_CTL_TYPE_INTERFACE_OPEN) {
2743*4882a593Smuzhiyun 				table[if_id].state = WLFC_STATE_OPEN;
2744*4882a593Smuzhiyun 				/* WLFC_DBGMESG(("INTERFACE[%d] OPEN\n", if_id)); */
2745*4882a593Smuzhiyun 			} else {
2746*4882a593Smuzhiyun 				table[if_id].state = WLFC_STATE_CLOSE;
2747*4882a593Smuzhiyun 				/* WLFC_DBGMESG(("INTERFACE[%d] CLOSE\n", if_id)); */
2748*4882a593Smuzhiyun 			}
2749*4882a593Smuzhiyun 			return BCME_OK;
2750*4882a593Smuzhiyun 		}
2751*4882a593Smuzhiyun 	}
2752*4882a593Smuzhiyun 	wlfc->stats.interface_update_failed++;
2753*4882a593Smuzhiyun 
2754*4882a593Smuzhiyun 	return BCME_OK;
2755*4882a593Smuzhiyun }
2756*4882a593Smuzhiyun 
2757*4882a593Smuzhiyun /** Called on receiving a WLFC_CTL_TYPE_MAC_REQUEST_CREDIT TLV from the dongle */
2758*4882a593Smuzhiyun static int
_dhd_wlfc_credit_request(dhd_pub_t * dhd,uint8 * value)2759*4882a593Smuzhiyun _dhd_wlfc_credit_request(dhd_pub_t *dhd, uint8* value)
2760*4882a593Smuzhiyun {
2761*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2762*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* table;
2763*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* desc;
2764*4882a593Smuzhiyun 	uint8 mac_handle;
2765*4882a593Smuzhiyun 	uint8 credit;
2766*4882a593Smuzhiyun 
2767*4882a593Smuzhiyun 	table = wlfc->destination_entries.nodes;
2768*4882a593Smuzhiyun 	mac_handle = value[1];
2769*4882a593Smuzhiyun 	credit = value[0];
2770*4882a593Smuzhiyun 
2771*4882a593Smuzhiyun 	desc = &table[WLFC_MAC_DESC_GET_LOOKUP_INDEX(mac_handle)];
2772*4882a593Smuzhiyun 	if (desc->occupied) {
2773*4882a593Smuzhiyun 		desc->requested_credit = credit;
2774*4882a593Smuzhiyun 
2775*4882a593Smuzhiyun 		desc->ac_bitmap = value[2] & (~(1<<AC_COUNT));
2776*4882a593Smuzhiyun 		_dhd_wlfc_add_requested_entry(wlfc, desc);
2777*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
2778*4882a593Smuzhiyun 		if (credit) {
2779*4882a593Smuzhiyun 			_dhd_wlfc_thread_wakeup(dhd);
2780*4882a593Smuzhiyun 		}
2781*4882a593Smuzhiyun #endif /* DHD_WLFC_THREAD */
2782*4882a593Smuzhiyun 	} else {
2783*4882a593Smuzhiyun 		wlfc->stats.credit_request_failed++;
2784*4882a593Smuzhiyun 	}
2785*4882a593Smuzhiyun 
2786*4882a593Smuzhiyun 	return BCME_OK;
2787*4882a593Smuzhiyun }
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun /** Called on receiving a WLFC_CTL_TYPE_MAC_REQUEST_PACKET TLV from the dongle */
2790*4882a593Smuzhiyun static int
_dhd_wlfc_packet_request(dhd_pub_t * dhd,uint8 * value)2791*4882a593Smuzhiyun _dhd_wlfc_packet_request(dhd_pub_t *dhd, uint8* value)
2792*4882a593Smuzhiyun {
2793*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2794*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* table;
2795*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* desc;
2796*4882a593Smuzhiyun 	uint8 mac_handle;
2797*4882a593Smuzhiyun 	uint8 packet_count;
2798*4882a593Smuzhiyun 
2799*4882a593Smuzhiyun 	table = wlfc->destination_entries.nodes;
2800*4882a593Smuzhiyun 	mac_handle = value[1];
2801*4882a593Smuzhiyun 	packet_count = value[0];
2802*4882a593Smuzhiyun 
2803*4882a593Smuzhiyun 	desc = &table[WLFC_MAC_DESC_GET_LOOKUP_INDEX(mac_handle)];
2804*4882a593Smuzhiyun 	if (desc->occupied) {
2805*4882a593Smuzhiyun 		desc->requested_packet = packet_count;
2806*4882a593Smuzhiyun 
2807*4882a593Smuzhiyun 		desc->ac_bitmap = value[2] & (~(1<<AC_COUNT));
2808*4882a593Smuzhiyun 		_dhd_wlfc_add_requested_entry(wlfc, desc);
2809*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
2810*4882a593Smuzhiyun 		if (packet_count) {
2811*4882a593Smuzhiyun 			_dhd_wlfc_thread_wakeup(dhd);
2812*4882a593Smuzhiyun 		}
2813*4882a593Smuzhiyun #endif /* DHD_WLFC_THREAD */
2814*4882a593Smuzhiyun 	} else {
2815*4882a593Smuzhiyun 		wlfc->stats.packet_request_failed++;
2816*4882a593Smuzhiyun 	}
2817*4882a593Smuzhiyun 
2818*4882a593Smuzhiyun 	return BCME_OK;
2819*4882a593Smuzhiyun }
2820*4882a593Smuzhiyun 
2821*4882a593Smuzhiyun /** Called when host receives a WLFC_CTL_TYPE_HOST_REORDER_RXPKTS TLV from the dongle */
2822*4882a593Smuzhiyun static void
_dhd_wlfc_reorderinfo_indicate(uint8 * val,uint8 len,uchar * info_buf,uint * info_len)2823*4882a593Smuzhiyun _dhd_wlfc_reorderinfo_indicate(uint8 *val, uint8 len, uchar *info_buf, uint *info_len)
2824*4882a593Smuzhiyun {
2825*4882a593Smuzhiyun 	if (info_len) {
2826*4882a593Smuzhiyun 		/* Check copy length to avoid buffer overrun. In case of length exceeding
2827*4882a593Smuzhiyun 		*  WLHOST_REORDERDATA_TOTLEN, return failure instead sending incomplete result
2828*4882a593Smuzhiyun 		*  of length WLHOST_REORDERDATA_TOTLEN
2829*4882a593Smuzhiyun 		*/
2830*4882a593Smuzhiyun 		if ((info_buf) && (len <= WLHOST_REORDERDATA_TOTLEN)) {
2831*4882a593Smuzhiyun 			bcopy(val, info_buf, len);
2832*4882a593Smuzhiyun 			*info_len = len;
2833*4882a593Smuzhiyun 		} else {
2834*4882a593Smuzhiyun 			*info_len = 0;
2835*4882a593Smuzhiyun 		}
2836*4882a593Smuzhiyun 	}
2837*4882a593Smuzhiyun }
2838*4882a593Smuzhiyun 
2839*4882a593Smuzhiyun /*
2840*4882a593Smuzhiyun  * public functions
2841*4882a593Smuzhiyun  */
2842*4882a593Smuzhiyun 
dhd_wlfc_is_supported(dhd_pub_t * dhd)2843*4882a593Smuzhiyun bool dhd_wlfc_is_supported(dhd_pub_t *dhd)
2844*4882a593Smuzhiyun {
2845*4882a593Smuzhiyun 	bool rc = TRUE;
2846*4882a593Smuzhiyun 
2847*4882a593Smuzhiyun 	if (dhd == NULL) {
2848*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
2849*4882a593Smuzhiyun 		return FALSE;
2850*4882a593Smuzhiyun 	}
2851*4882a593Smuzhiyun 
2852*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
2853*4882a593Smuzhiyun 
2854*4882a593Smuzhiyun 	if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
2855*4882a593Smuzhiyun 		rc =  FALSE;
2856*4882a593Smuzhiyun 	}
2857*4882a593Smuzhiyun 
2858*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
2859*4882a593Smuzhiyun 
2860*4882a593Smuzhiyun 	return rc;
2861*4882a593Smuzhiyun }
2862*4882a593Smuzhiyun 
dhd_wlfc_enable(dhd_pub_t * dhd)2863*4882a593Smuzhiyun int dhd_wlfc_enable(dhd_pub_t *dhd)
2864*4882a593Smuzhiyun {
2865*4882a593Smuzhiyun 	int i, rc = BCME_OK;
2866*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc;
2867*4882a593Smuzhiyun 
2868*4882a593Smuzhiyun 	if (dhd == NULL) {
2869*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
2870*4882a593Smuzhiyun 		return BCME_BADARG;
2871*4882a593Smuzhiyun 	}
2872*4882a593Smuzhiyun 
2873*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
2874*4882a593Smuzhiyun 
2875*4882a593Smuzhiyun 	if (!dhd->wlfc_enabled || dhd->wlfc_state) {
2876*4882a593Smuzhiyun 		rc = BCME_OK;
2877*4882a593Smuzhiyun 		goto exit;
2878*4882a593Smuzhiyun 	}
2879*4882a593Smuzhiyun 
2880*4882a593Smuzhiyun 	/* allocate space to track txstatus propagated from firmware */
2881*4882a593Smuzhiyun 	dhd->wlfc_state = DHD_OS_PREALLOC(dhd, DHD_PREALLOC_DHD_WLFC_INFO,
2882*4882a593Smuzhiyun 		sizeof(athost_wl_status_info_t));
2883*4882a593Smuzhiyun 	if (dhd->wlfc_state == NULL) {
2884*4882a593Smuzhiyun 		rc = BCME_NOMEM;
2885*4882a593Smuzhiyun 		goto exit;
2886*4882a593Smuzhiyun 	}
2887*4882a593Smuzhiyun 
2888*4882a593Smuzhiyun 	/* initialize state space */
2889*4882a593Smuzhiyun 	wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
2890*4882a593Smuzhiyun 	memset(wlfc, 0, sizeof(athost_wl_status_info_t));
2891*4882a593Smuzhiyun 
2892*4882a593Smuzhiyun 	/* remember osh & dhdp */
2893*4882a593Smuzhiyun 	wlfc->osh = dhd->osh;
2894*4882a593Smuzhiyun 	wlfc->dhdp = dhd;
2895*4882a593Smuzhiyun 
2896*4882a593Smuzhiyun 	if (!WLFC_GET_AFQ(dhd->wlfc_mode)) {
2897*4882a593Smuzhiyun 		wlfc->hanger = _dhd_wlfc_hanger_create(dhd, WLFC_HANGER_MAXITEMS);
2898*4882a593Smuzhiyun 		if (wlfc->hanger == NULL) {
2899*4882a593Smuzhiyun 			DHD_OS_PREFREE(dhd, dhd->wlfc_state,
2900*4882a593Smuzhiyun 				sizeof(athost_wl_status_info_t));
2901*4882a593Smuzhiyun 			dhd->wlfc_state = NULL;
2902*4882a593Smuzhiyun 			rc = BCME_NOMEM;
2903*4882a593Smuzhiyun 			goto exit;
2904*4882a593Smuzhiyun 		}
2905*4882a593Smuzhiyun 	}
2906*4882a593Smuzhiyun 
2907*4882a593Smuzhiyun 	dhd->proptxstatus_mode = WLFC_FCMODE_EXPLICIT_CREDIT;
2908*4882a593Smuzhiyun 	/* default to check rx pkt */
2909*4882a593Smuzhiyun 	dhd->wlfc_rxpkt_chk = TRUE;
2910*4882a593Smuzhiyun 	if (dhd->op_mode & DHD_FLAG_IBSS_MODE) {
2911*4882a593Smuzhiyun 		dhd->wlfc_rxpkt_chk = FALSE;
2912*4882a593Smuzhiyun 	}
2913*4882a593Smuzhiyun 
2914*4882a593Smuzhiyun 	/* initialize all interfaces to accept traffic */
2915*4882a593Smuzhiyun 	for (i = 0; i < WLFC_MAX_IFNUM; i++) {
2916*4882a593Smuzhiyun 		wlfc->hostif_flow_state[i] = OFF;
2917*4882a593Smuzhiyun 	}
2918*4882a593Smuzhiyun 
2919*4882a593Smuzhiyun 	_dhd_wlfc_mac_entry_update(wlfc, &wlfc->destination_entries.other,
2920*4882a593Smuzhiyun 		eWLFC_MAC_ENTRY_ACTION_ADD, 0xff, 0, NULL, NULL, NULL);
2921*4882a593Smuzhiyun 
2922*4882a593Smuzhiyun 	wlfc->allow_credit_borrow = 0;
2923*4882a593Smuzhiyun 	wlfc->single_ac = 0;
2924*4882a593Smuzhiyun 	wlfc->single_ac_timestamp = 0;
2925*4882a593Smuzhiyun 
2926*4882a593Smuzhiyun exit:
2927*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
2928*4882a593Smuzhiyun 
2929*4882a593Smuzhiyun 	return rc;
2930*4882a593Smuzhiyun } /* dhd_wlfc_enable */
2931*4882a593Smuzhiyun 
2932*4882a593Smuzhiyun #ifdef SUPPORT_P2P_GO_PS
2933*4882a593Smuzhiyun 
2934*4882a593Smuzhiyun /**
2935*4882a593Smuzhiyun  * Called when the host platform enters a lower power mode, eg right before a system hibernate.
2936*4882a593Smuzhiyun  * SUPPORT_P2P_GO_PS specific function.
2937*4882a593Smuzhiyun  */
2938*4882a593Smuzhiyun int
dhd_wlfc_suspend(dhd_pub_t * dhd)2939*4882a593Smuzhiyun dhd_wlfc_suspend(dhd_pub_t *dhd)
2940*4882a593Smuzhiyun {
2941*4882a593Smuzhiyun 	uint32 tlv = 0;
2942*4882a593Smuzhiyun 
2943*4882a593Smuzhiyun 	DHD_TRACE(("%s: masking wlfc events\n", __FUNCTION__));
2944*4882a593Smuzhiyun 	if (!dhd->wlfc_enabled)
2945*4882a593Smuzhiyun 		return -1;
2946*4882a593Smuzhiyun 
2947*4882a593Smuzhiyun 	if (!dhd_wl_ioctl_get_intiovar(dhd, "tlv", &tlv, WLC_GET_VAR, FALSE, 0))
2948*4882a593Smuzhiyun 		return -1;
2949*4882a593Smuzhiyun 	if ((tlv & (WLFC_FLAGS_RSSI_SIGNALS | WLFC_FLAGS_XONXOFF_SIGNALS)) == 0)
2950*4882a593Smuzhiyun 		return 0;
2951*4882a593Smuzhiyun 	tlv &= ~(WLFC_FLAGS_RSSI_SIGNALS | WLFC_FLAGS_XONXOFF_SIGNALS);
2952*4882a593Smuzhiyun 	if (!dhd_wl_ioctl_set_intiovar(dhd, "tlv", tlv, WLC_SET_VAR, TRUE, 0))
2953*4882a593Smuzhiyun 		return -1;
2954*4882a593Smuzhiyun 
2955*4882a593Smuzhiyun 	return 0;
2956*4882a593Smuzhiyun }
2957*4882a593Smuzhiyun 
2958*4882a593Smuzhiyun /**
2959*4882a593Smuzhiyun  * Called when the host platform resumes from a power management operation, eg resume after a
2960*4882a593Smuzhiyun  * system hibernate. SUPPORT_P2P_GO_PS specific function.
2961*4882a593Smuzhiyun  */
2962*4882a593Smuzhiyun int
dhd_wlfc_resume(dhd_pub_t * dhd)2963*4882a593Smuzhiyun dhd_wlfc_resume(dhd_pub_t *dhd)
2964*4882a593Smuzhiyun {
2965*4882a593Smuzhiyun 	uint32 tlv = 0;
2966*4882a593Smuzhiyun 
2967*4882a593Smuzhiyun 	DHD_TRACE(("%s: unmasking wlfc events\n", __FUNCTION__));
2968*4882a593Smuzhiyun 	if (!dhd->wlfc_enabled)
2969*4882a593Smuzhiyun 		return -1;
2970*4882a593Smuzhiyun 
2971*4882a593Smuzhiyun 	if (!dhd_wl_ioctl_get_intiovar(dhd, "tlv", &tlv, WLC_GET_VAR, FALSE, 0))
2972*4882a593Smuzhiyun 		return -1;
2973*4882a593Smuzhiyun 	if ((tlv & (WLFC_FLAGS_RSSI_SIGNALS | WLFC_FLAGS_XONXOFF_SIGNALS)) ==
2974*4882a593Smuzhiyun 		(WLFC_FLAGS_RSSI_SIGNALS | WLFC_FLAGS_XONXOFF_SIGNALS))
2975*4882a593Smuzhiyun 		return 0;
2976*4882a593Smuzhiyun 	tlv |= (WLFC_FLAGS_RSSI_SIGNALS | WLFC_FLAGS_XONXOFF_SIGNALS);
2977*4882a593Smuzhiyun 	if (!dhd_wl_ioctl_set_intiovar(dhd, "tlv", tlv, WLC_SET_VAR, TRUE, 0))
2978*4882a593Smuzhiyun 		return -1;
2979*4882a593Smuzhiyun 
2980*4882a593Smuzhiyun 	return 0;
2981*4882a593Smuzhiyun }
2982*4882a593Smuzhiyun 
2983*4882a593Smuzhiyun #endif /* SUPPORT_P2P_GO_PS */
2984*4882a593Smuzhiyun 
2985*4882a593Smuzhiyun /** A flow control header was received from firmware, containing one or more TLVs */
2986*4882a593Smuzhiyun int
dhd_wlfc_parse_header_info(dhd_pub_t * dhd,void * pktbuf,int tlv_hdr_len,uchar * reorder_info_buf,uint * reorder_info_len)2987*4882a593Smuzhiyun dhd_wlfc_parse_header_info(dhd_pub_t *dhd, void* pktbuf, int tlv_hdr_len, uchar *reorder_info_buf,
2988*4882a593Smuzhiyun 	uint *reorder_info_len)
2989*4882a593Smuzhiyun {
2990*4882a593Smuzhiyun 	uint8 type, len;
2991*4882a593Smuzhiyun 	uint8* value;
2992*4882a593Smuzhiyun 	uint8* tmpbuf;
2993*4882a593Smuzhiyun 	uint16 remainder = (uint16)tlv_hdr_len;
2994*4882a593Smuzhiyun 	uint16 processed = 0;
2995*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc = NULL;
2996*4882a593Smuzhiyun 	void* entry;
2997*4882a593Smuzhiyun 
2998*4882a593Smuzhiyun 	if ((dhd == NULL) || (pktbuf == NULL)) {
2999*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3000*4882a593Smuzhiyun 		return BCME_BADARG;
3001*4882a593Smuzhiyun 	}
3002*4882a593Smuzhiyun 
3003*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3004*4882a593Smuzhiyun 
3005*4882a593Smuzhiyun 	if (dhd->proptxstatus_mode != WLFC_ONLY_AMPDU_HOSTREORDER) {
3006*4882a593Smuzhiyun 		if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3007*4882a593Smuzhiyun 			dhd_os_wlfc_unblock(dhd);
3008*4882a593Smuzhiyun 			return WLFC_UNSUPPORTED;
3009*4882a593Smuzhiyun 		}
3010*4882a593Smuzhiyun 		wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
3011*4882a593Smuzhiyun 	}
3012*4882a593Smuzhiyun 
3013*4882a593Smuzhiyun 	tmpbuf = (uint8*)PKTDATA(dhd->osh, pktbuf);
3014*4882a593Smuzhiyun 
3015*4882a593Smuzhiyun 	if (remainder) {
3016*4882a593Smuzhiyun 		while ((processed < (WLFC_MAX_PENDING_DATALEN * 2)) && (remainder > 0)) {
3017*4882a593Smuzhiyun 			type = tmpbuf[processed];
3018*4882a593Smuzhiyun 			if (type == WLFC_CTL_TYPE_FILLER) {
3019*4882a593Smuzhiyun 				remainder -= 1;
3020*4882a593Smuzhiyun 				processed += 1;
3021*4882a593Smuzhiyun 				continue;
3022*4882a593Smuzhiyun 			}
3023*4882a593Smuzhiyun 
3024*4882a593Smuzhiyun 			len  = tmpbuf[processed + 1];
3025*4882a593Smuzhiyun 			value = &tmpbuf[processed + 2];
3026*4882a593Smuzhiyun 
3027*4882a593Smuzhiyun 			if (remainder < (2 + len))
3028*4882a593Smuzhiyun 				break;
3029*4882a593Smuzhiyun 
3030*4882a593Smuzhiyun 			remainder -= 2 + len;
3031*4882a593Smuzhiyun 			processed += 2 + len;
3032*4882a593Smuzhiyun 			entry = NULL;
3033*4882a593Smuzhiyun 
3034*4882a593Smuzhiyun 			DHD_INFO(("%s():%d type %d remainder %d processed %d\n",
3035*4882a593Smuzhiyun 				__FUNCTION__, __LINE__, type, remainder, processed));
3036*4882a593Smuzhiyun 
3037*4882a593Smuzhiyun 			if (type == WLFC_CTL_TYPE_HOST_REORDER_RXPKTS)
3038*4882a593Smuzhiyun 				_dhd_wlfc_reorderinfo_indicate(value, len, reorder_info_buf,
3039*4882a593Smuzhiyun 					reorder_info_len);
3040*4882a593Smuzhiyun 
3041*4882a593Smuzhiyun 			if (wlfc == NULL) {
3042*4882a593Smuzhiyun 				ASSERT(dhd->proptxstatus_mode == WLFC_ONLY_AMPDU_HOSTREORDER);
3043*4882a593Smuzhiyun 
3044*4882a593Smuzhiyun 				if (type != WLFC_CTL_TYPE_HOST_REORDER_RXPKTS &&
3045*4882a593Smuzhiyun 					type != WLFC_CTL_TYPE_TRANS_ID)
3046*4882a593Smuzhiyun 					DHD_INFO(("%s():%d dhd->wlfc_state is NULL yet!"
3047*4882a593Smuzhiyun 					" type %d remainder %d processed %d\n",
3048*4882a593Smuzhiyun 					__FUNCTION__, __LINE__, type, remainder, processed));
3049*4882a593Smuzhiyun 				continue;
3050*4882a593Smuzhiyun 			}
3051*4882a593Smuzhiyun 
3052*4882a593Smuzhiyun 			if (type == WLFC_CTL_TYPE_TXSTATUS) {
3053*4882a593Smuzhiyun 				_dhd_wlfc_compressed_txstatus_update(dhd, value, 1, &entry);
3054*4882a593Smuzhiyun 			} else if (type == WLFC_CTL_TYPE_COMP_TXSTATUS) {
3055*4882a593Smuzhiyun 				uint8 compcnt_offset = WLFC_CTL_VALUE_LEN_TXSTATUS;
3056*4882a593Smuzhiyun 
3057*4882a593Smuzhiyun 				if (WLFC_GET_REUSESEQ(dhd->wlfc_mode)) {
3058*4882a593Smuzhiyun 					compcnt_offset += WLFC_CTL_VALUE_LEN_SEQ;
3059*4882a593Smuzhiyun 				}
3060*4882a593Smuzhiyun 				_dhd_wlfc_compressed_txstatus_update(dhd, value,
3061*4882a593Smuzhiyun 					value[compcnt_offset], &entry);
3062*4882a593Smuzhiyun 			} else if (type == WLFC_CTL_TYPE_FIFO_CREDITBACK) {
3063*4882a593Smuzhiyun 				_dhd_wlfc_fifocreditback_indicate(dhd, value);
3064*4882a593Smuzhiyun 			} else if (type == WLFC_CTL_TYPE_RSSI) {
3065*4882a593Smuzhiyun 				_dhd_wlfc_rssi_indicate(dhd, value);
3066*4882a593Smuzhiyun 			} else if (type == WLFC_CTL_TYPE_MAC_REQUEST_CREDIT) {
3067*4882a593Smuzhiyun 				_dhd_wlfc_credit_request(dhd, value);
3068*4882a593Smuzhiyun 			} else if (type == WLFC_CTL_TYPE_MAC_REQUEST_PACKET) {
3069*4882a593Smuzhiyun 				_dhd_wlfc_packet_request(dhd, value);
3070*4882a593Smuzhiyun 			} else if ((type == WLFC_CTL_TYPE_MAC_OPEN) ||
3071*4882a593Smuzhiyun 				(type == WLFC_CTL_TYPE_MAC_CLOSE)) {
3072*4882a593Smuzhiyun 				_dhd_wlfc_psmode_update(dhd, value, type);
3073*4882a593Smuzhiyun 			} else if ((type == WLFC_CTL_TYPE_MACDESC_ADD) ||
3074*4882a593Smuzhiyun 				(type == WLFC_CTL_TYPE_MACDESC_DEL)) {
3075*4882a593Smuzhiyun 				_dhd_wlfc_mac_table_update(dhd, value, type);
3076*4882a593Smuzhiyun 			} else if (type == WLFC_CTL_TYPE_TRANS_ID) {
3077*4882a593Smuzhiyun 				_dhd_wlfc_dbg_senum_check(dhd, value);
3078*4882a593Smuzhiyun 			} else if ((type == WLFC_CTL_TYPE_INTERFACE_OPEN) ||
3079*4882a593Smuzhiyun 				(type == WLFC_CTL_TYPE_INTERFACE_CLOSE)) {
3080*4882a593Smuzhiyun 				_dhd_wlfc_interface_update(dhd, value, type);
3081*4882a593Smuzhiyun 			}
3082*4882a593Smuzhiyun 
3083*4882a593Smuzhiyun 			if (entry && WLFC_GET_REORDERSUPP(dhd->wlfc_mode)) {
3084*4882a593Smuzhiyun 				/* suppress all packets for this mac entry from bus->txq */
3085*4882a593Smuzhiyun 				_dhd_wlfc_suppress_txq(dhd, _dhd_wlfc_entrypkt_fn, entry);
3086*4882a593Smuzhiyun 			}
3087*4882a593Smuzhiyun 		} /* while */
3088*4882a593Smuzhiyun 
3089*4882a593Smuzhiyun 		if (remainder != 0 && wlfc) {
3090*4882a593Smuzhiyun 			/* trouble..., something is not right */
3091*4882a593Smuzhiyun 			wlfc->stats.tlv_parse_failed++;
3092*4882a593Smuzhiyun 		}
3093*4882a593Smuzhiyun 	} /* if */
3094*4882a593Smuzhiyun 
3095*4882a593Smuzhiyun 	if (wlfc)
3096*4882a593Smuzhiyun 		wlfc->stats.dhd_hdrpulls++;
3097*4882a593Smuzhiyun 
3098*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3099*4882a593Smuzhiyun 	return BCME_OK;
3100*4882a593Smuzhiyun }
3101*4882a593Smuzhiyun 
3102*4882a593Smuzhiyun KERNEL_THREAD_RETURN_TYPE
dhd_wlfc_transfer_packets(void * data)3103*4882a593Smuzhiyun dhd_wlfc_transfer_packets(void *data)
3104*4882a593Smuzhiyun {
3105*4882a593Smuzhiyun 	dhd_pub_t *dhdp = (dhd_pub_t *)data;
3106*4882a593Smuzhiyun 	int ac, single_ac = 0, rc = BCME_OK;
3107*4882a593Smuzhiyun 	dhd_wlfc_commit_info_t  commit_info;
3108*4882a593Smuzhiyun 	athost_wl_status_info_t* ctx;
3109*4882a593Smuzhiyun 	int bus_retry_count = 0;
3110*4882a593Smuzhiyun 	int pkt_send = 0;
3111*4882a593Smuzhiyun 	int pkt_send_per_ac = 0;
3112*4882a593Smuzhiyun 
3113*4882a593Smuzhiyun 	uint8 tx_map = 0; /* packets (send + in queue), Bitmask for 4 ACs + BC/MC */
3114*4882a593Smuzhiyun 	uint8 rx_map = 0; /* received packets, Bitmask for 4 ACs + BC/MC */
3115*4882a593Smuzhiyun 	uint8 packets_map = 0; /* packets in queue, Bitmask for 4 ACs + BC/MC */
3116*4882a593Smuzhiyun 	bool no_credit = FALSE;
3117*4882a593Smuzhiyun 
3118*4882a593Smuzhiyun 	int lender;
3119*4882a593Smuzhiyun 
3120*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
3121*4882a593Smuzhiyun 	/* wait till someone wakeup me up, will change it at running time */
3122*4882a593Smuzhiyun 	int wait_msec = msecs_to_jiffies(0xFFFFFFFF);
3123*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
3124*4882a593Smuzhiyun 
3125*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
3126*4882a593Smuzhiyun 	while (1) {
3127*4882a593Smuzhiyun 		bus_retry_count = 0;
3128*4882a593Smuzhiyun 		pkt_send = 0;
3129*4882a593Smuzhiyun 		tx_map = 0;
3130*4882a593Smuzhiyun 		rx_map = 0;
3131*4882a593Smuzhiyun 		packets_map = 0;
3132*4882a593Smuzhiyun 		wait_msec = wait_event_interruptible_timeout(dhdp->wlfc_wqhead,
3133*4882a593Smuzhiyun 			dhdp->wlfc_thread_go, wait_msec);
3134*4882a593Smuzhiyun 		if (kthread_should_stop()) {
3135*4882a593Smuzhiyun 			break;
3136*4882a593Smuzhiyun 		}
3137*4882a593Smuzhiyun 		dhdp->wlfc_thread_go = FALSE;
3138*4882a593Smuzhiyun 
3139*4882a593Smuzhiyun 		dhd_os_wlfc_block(dhdp);
3140*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
3141*4882a593Smuzhiyun 		ctx = (athost_wl_status_info_t*)dhdp->wlfc_state;
3142*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
3143*4882a593Smuzhiyun 		if (!ctx)
3144*4882a593Smuzhiyun 			goto exit;
3145*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
3146*4882a593Smuzhiyun 
3147*4882a593Smuzhiyun 	memset(&commit_info, 0, sizeof(commit_info));
3148*4882a593Smuzhiyun 
3149*4882a593Smuzhiyun 	/*
3150*4882a593Smuzhiyun 	Commit packets for regular AC traffic. Higher priority first.
3151*4882a593Smuzhiyun 	First, use up FIFO credits available to each AC. Based on distribution
3152*4882a593Smuzhiyun 	and credits left, borrow from other ACs as applicable
3153*4882a593Smuzhiyun 
3154*4882a593Smuzhiyun 	-NOTE:
3155*4882a593Smuzhiyun 	If the bus between the host and firmware is overwhelmed by the
3156*4882a593Smuzhiyun 	traffic from host, it is possible that higher priority traffic
3157*4882a593Smuzhiyun 	starves the lower priority queue. If that occurs often, we may
3158*4882a593Smuzhiyun 	have to employ weighted round-robin or ucode scheme to avoid
3159*4882a593Smuzhiyun 	low priority packet starvation.
3160*4882a593Smuzhiyun 	*/
3161*4882a593Smuzhiyun 
3162*4882a593Smuzhiyun 	for (ac = AC_COUNT; ac >= 0; ac--) {
3163*4882a593Smuzhiyun 		if (dhdp->wlfc_rxpkt_chk) {
3164*4882a593Smuzhiyun 			/* check rx packet */
3165*4882a593Smuzhiyun 			uint32 curr_t = OSL_SYSUPTIME(), delta;
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun 			delta = curr_t - ctx->rx_timestamp[ac];
3168*4882a593Smuzhiyun 			if (delta < WLFC_RX_DETECTION_THRESHOLD_MS) {
3169*4882a593Smuzhiyun 				rx_map |= (1 << ac);
3170*4882a593Smuzhiyun 			}
3171*4882a593Smuzhiyun 		}
3172*4882a593Smuzhiyun 
3173*4882a593Smuzhiyun 		if (ctx->pkt_cnt_per_ac[ac] == 0) {
3174*4882a593Smuzhiyun 			continue;
3175*4882a593Smuzhiyun 		}
3176*4882a593Smuzhiyun 
3177*4882a593Smuzhiyun 		tx_map |= (1 << ac);
3178*4882a593Smuzhiyun 		single_ac = ac + 1;
3179*4882a593Smuzhiyun 		pkt_send_per_ac = 0;
3180*4882a593Smuzhiyun 		while ((FALSE == dhdp->proptxstatus_txoff) &&
3181*4882a593Smuzhiyun 				(pkt_send_per_ac < WLFC_PACKET_BOUND)) {
3182*4882a593Smuzhiyun 			/* packets from delayQ with less priority are fresh and
3183*4882a593Smuzhiyun 			 * they'd need header and have no MAC entry
3184*4882a593Smuzhiyun 			 */
3185*4882a593Smuzhiyun 			no_credit = (ctx->FIFO_credit[ac] < 1);
3186*4882a593Smuzhiyun 			if (dhdp->proptxstatus_credit_ignore ||
3187*4882a593Smuzhiyun 				((ac == AC_COUNT) && !ctx->bcmc_credit_supported)) {
3188*4882a593Smuzhiyun 				no_credit = FALSE;
3189*4882a593Smuzhiyun 			}
3190*4882a593Smuzhiyun 
3191*4882a593Smuzhiyun 			lender = -1;
3192*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3193*4882a593Smuzhiyun 			if (no_credit && (ac < AC_COUNT) && (tx_map >= rx_map) &&
3194*4882a593Smuzhiyun 				dhdp->wlfc_borrow_allowed) {
3195*4882a593Smuzhiyun 				/* try borrow from lower priority */
3196*4882a593Smuzhiyun 				lender = _dhd_wlfc_borrow_credit(ctx, ac - 1, ac, FALSE);
3197*4882a593Smuzhiyun 				if (lender != -1) {
3198*4882a593Smuzhiyun 					no_credit = FALSE;
3199*4882a593Smuzhiyun 				}
3200*4882a593Smuzhiyun 			}
3201*4882a593Smuzhiyun #endif // endif
3202*4882a593Smuzhiyun 			commit_info.needs_hdr = 1;
3203*4882a593Smuzhiyun 			commit_info.mac_entry = NULL;
3204*4882a593Smuzhiyun 			commit_info.p = _dhd_wlfc_deque_delayedq(ctx, ac,
3205*4882a593Smuzhiyun 				&(commit_info.ac_fifo_credit_spent),
3206*4882a593Smuzhiyun 				&(commit_info.needs_hdr),
3207*4882a593Smuzhiyun 				&(commit_info.mac_entry),
3208*4882a593Smuzhiyun 				no_credit);
3209*4882a593Smuzhiyun 			commit_info.pkt_type = (commit_info.needs_hdr) ? eWLFC_PKTTYPE_DELAYED :
3210*4882a593Smuzhiyun 				eWLFC_PKTTYPE_SUPPRESSED;
3211*4882a593Smuzhiyun 
3212*4882a593Smuzhiyun 			if (commit_info.p == NULL) {
3213*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3214*4882a593Smuzhiyun 				if (lender != -1 && dhdp->wlfc_borrow_allowed) {
3215*4882a593Smuzhiyun 					_dhd_wlfc_return_credit(ctx, lender, ac);
3216*4882a593Smuzhiyun 				}
3217*4882a593Smuzhiyun #endif // endif
3218*4882a593Smuzhiyun 				break;
3219*4882a593Smuzhiyun 			}
3220*4882a593Smuzhiyun 
3221*4882a593Smuzhiyun 			if (!dhdp->proptxstatus_credit_ignore && (lender == -1)) {
3222*4882a593Smuzhiyun 				ASSERT(ctx->FIFO_credit[ac] >= commit_info.ac_fifo_credit_spent);
3223*4882a593Smuzhiyun 			}
3224*4882a593Smuzhiyun 			/* here we can ensure have credit or no credit needed */
3225*4882a593Smuzhiyun 			rc = _dhd_wlfc_handle_packet_commit(ctx, ac, &commit_info,
3226*4882a593Smuzhiyun 				ctx->fcommit, ctx->commit_ctx);
3227*4882a593Smuzhiyun 
3228*4882a593Smuzhiyun 			/* Bus commits may fail (e.g. flow control); abort after retries */
3229*4882a593Smuzhiyun 			if (rc == BCME_OK) {
3230*4882a593Smuzhiyun 				pkt_send++;
3231*4882a593Smuzhiyun 				pkt_send_per_ac++;
3232*4882a593Smuzhiyun 				if (commit_info.ac_fifo_credit_spent && (lender == -1)) {
3233*4882a593Smuzhiyun 					ctx->FIFO_credit[ac]--;
3234*4882a593Smuzhiyun 				}
3235*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3236*4882a593Smuzhiyun 				else if (!commit_info.ac_fifo_credit_spent && (lender != -1) &&
3237*4882a593Smuzhiyun 					dhdp->wlfc_borrow_allowed) {
3238*4882a593Smuzhiyun 					_dhd_wlfc_return_credit(ctx, lender, ac);
3239*4882a593Smuzhiyun 				}
3240*4882a593Smuzhiyun #endif // endif
3241*4882a593Smuzhiyun 			} else {
3242*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3243*4882a593Smuzhiyun 				if (lender != -1 && dhdp->wlfc_borrow_allowed) {
3244*4882a593Smuzhiyun 					_dhd_wlfc_return_credit(ctx, lender, ac);
3245*4882a593Smuzhiyun 				}
3246*4882a593Smuzhiyun #endif // endif
3247*4882a593Smuzhiyun 				bus_retry_count++;
3248*4882a593Smuzhiyun 				if (bus_retry_count >= BUS_RETRIES) {
3249*4882a593Smuzhiyun 					DHD_ERROR(("%s: bus error %d\n", __FUNCTION__, rc));
3250*4882a593Smuzhiyun 					goto exit;
3251*4882a593Smuzhiyun 				}
3252*4882a593Smuzhiyun 			}
3253*4882a593Smuzhiyun 		}
3254*4882a593Smuzhiyun 
3255*4882a593Smuzhiyun 		if (ctx->pkt_cnt_per_ac[ac]) {
3256*4882a593Smuzhiyun 			packets_map |= (1 << ac);
3257*4882a593Smuzhiyun 		}
3258*4882a593Smuzhiyun 	}
3259*4882a593Smuzhiyun 
3260*4882a593Smuzhiyun 	if ((tx_map == 0) || dhdp->proptxstatus_credit_ignore) {
3261*4882a593Smuzhiyun 		/* nothing send out or remain in queue */
3262*4882a593Smuzhiyun 		rc = BCME_OK;
3263*4882a593Smuzhiyun 		goto exit;
3264*4882a593Smuzhiyun 	}
3265*4882a593Smuzhiyun 
3266*4882a593Smuzhiyun 	if (((tx_map & (tx_map - 1)) == 0) && (tx_map >= rx_map)) {
3267*4882a593Smuzhiyun 		/* only one tx ac exist and no higher rx ac */
3268*4882a593Smuzhiyun 		if ((single_ac == ctx->single_ac) && ctx->allow_credit_borrow) {
3269*4882a593Smuzhiyun 			ac = single_ac - 1;
3270*4882a593Smuzhiyun 		} else {
3271*4882a593Smuzhiyun 			uint32 delta;
3272*4882a593Smuzhiyun 			uint32 curr_t = OSL_SYSUPTIME();
3273*4882a593Smuzhiyun 
3274*4882a593Smuzhiyun 			if (single_ac != ctx->single_ac) {
3275*4882a593Smuzhiyun 				/* new single ac traffic (first single ac or different single ac) */
3276*4882a593Smuzhiyun 				ctx->allow_credit_borrow = 0;
3277*4882a593Smuzhiyun 				ctx->single_ac_timestamp = curr_t;
3278*4882a593Smuzhiyun 				ctx->single_ac = (uint8)single_ac;
3279*4882a593Smuzhiyun 				rc = BCME_OK;
3280*4882a593Smuzhiyun 				goto exit;
3281*4882a593Smuzhiyun 			}
3282*4882a593Smuzhiyun 			/* same ac traffic, check if it lasts enough time */
3283*4882a593Smuzhiyun 			delta = curr_t - ctx->single_ac_timestamp;
3284*4882a593Smuzhiyun 
3285*4882a593Smuzhiyun 			if (delta >= WLFC_BORROW_DEFER_PERIOD_MS) {
3286*4882a593Smuzhiyun 				/* wait enough time, can borrow now */
3287*4882a593Smuzhiyun 				ctx->allow_credit_borrow = 1;
3288*4882a593Smuzhiyun 				ac = single_ac - 1;
3289*4882a593Smuzhiyun 			} else {
3290*4882a593Smuzhiyun 				rc = BCME_OK;
3291*4882a593Smuzhiyun 				goto exit;
3292*4882a593Smuzhiyun 			}
3293*4882a593Smuzhiyun 		}
3294*4882a593Smuzhiyun 	} else {
3295*4882a593Smuzhiyun 		/* If we have multiple AC traffic, turn off borrowing, mark time and bail out */
3296*4882a593Smuzhiyun 		ctx->allow_credit_borrow = 0;
3297*4882a593Smuzhiyun 		ctx->single_ac_timestamp = 0;
3298*4882a593Smuzhiyun 		ctx->single_ac = 0;
3299*4882a593Smuzhiyun 		rc = BCME_OK;
3300*4882a593Smuzhiyun 		goto exit;
3301*4882a593Smuzhiyun 	}
3302*4882a593Smuzhiyun 
3303*4882a593Smuzhiyun 	if (packets_map == 0) {
3304*4882a593Smuzhiyun 		/* nothing to send, skip borrow */
3305*4882a593Smuzhiyun 		rc = BCME_OK;
3306*4882a593Smuzhiyun 		goto exit;
3307*4882a593Smuzhiyun 	}
3308*4882a593Smuzhiyun 
3309*4882a593Smuzhiyun 	/* At this point, borrow all credits only for ac */
3310*4882a593Smuzhiyun 	while (FALSE == dhdp->proptxstatus_txoff) {
3311*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3312*4882a593Smuzhiyun 		if (dhdp->wlfc_borrow_allowed) {
3313*4882a593Smuzhiyun 			if ((lender = _dhd_wlfc_borrow_credit(ctx, AC_COUNT, ac, TRUE)) == -1) {
3314*4882a593Smuzhiyun 				break;
3315*4882a593Smuzhiyun 			}
3316*4882a593Smuzhiyun 		}
3317*4882a593Smuzhiyun 		else
3318*4882a593Smuzhiyun 			break;
3319*4882a593Smuzhiyun #endif // endif
3320*4882a593Smuzhiyun 		commit_info.p = _dhd_wlfc_deque_delayedq(ctx, ac,
3321*4882a593Smuzhiyun 			&(commit_info.ac_fifo_credit_spent),
3322*4882a593Smuzhiyun 			&(commit_info.needs_hdr),
3323*4882a593Smuzhiyun 			&(commit_info.mac_entry),
3324*4882a593Smuzhiyun 			FALSE);
3325*4882a593Smuzhiyun 		if (commit_info.p == NULL) {
3326*4882a593Smuzhiyun 			/* before borrow only one ac exists and now this only ac is empty */
3327*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3328*4882a593Smuzhiyun 			_dhd_wlfc_return_credit(ctx, lender, ac);
3329*4882a593Smuzhiyun #endif // endif
3330*4882a593Smuzhiyun 			break;
3331*4882a593Smuzhiyun 		}
3332*4882a593Smuzhiyun 
3333*4882a593Smuzhiyun 		commit_info.pkt_type = (commit_info.needs_hdr) ? eWLFC_PKTTYPE_DELAYED :
3334*4882a593Smuzhiyun 			eWLFC_PKTTYPE_SUPPRESSED;
3335*4882a593Smuzhiyun 
3336*4882a593Smuzhiyun 		rc = _dhd_wlfc_handle_packet_commit(ctx, ac, &commit_info,
3337*4882a593Smuzhiyun 		     ctx->fcommit, ctx->commit_ctx);
3338*4882a593Smuzhiyun 
3339*4882a593Smuzhiyun 		/* Bus commits may fail (e.g. flow control); abort after retries */
3340*4882a593Smuzhiyun 		if (rc == BCME_OK) {
3341*4882a593Smuzhiyun 			pkt_send++;
3342*4882a593Smuzhiyun 			if (commit_info.ac_fifo_credit_spent) {
3343*4882a593Smuzhiyun #ifndef LIMIT_BORROW
3344*4882a593Smuzhiyun 				ctx->FIFO_credit[ac]--;
3345*4882a593Smuzhiyun #endif // endif
3346*4882a593Smuzhiyun 			} else {
3347*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3348*4882a593Smuzhiyun 				_dhd_wlfc_return_credit(ctx, lender, ac);
3349*4882a593Smuzhiyun #endif // endif
3350*4882a593Smuzhiyun 			}
3351*4882a593Smuzhiyun 		} else {
3352*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3353*4882a593Smuzhiyun 			_dhd_wlfc_return_credit(ctx, lender, ac);
3354*4882a593Smuzhiyun #endif // endif
3355*4882a593Smuzhiyun 			bus_retry_count++;
3356*4882a593Smuzhiyun 			if (bus_retry_count >= BUS_RETRIES) {
3357*4882a593Smuzhiyun 				DHD_ERROR(("%s: bus error %d\n", __FUNCTION__, rc));
3358*4882a593Smuzhiyun 				goto exit;
3359*4882a593Smuzhiyun 			}
3360*4882a593Smuzhiyun 		}
3361*4882a593Smuzhiyun 	}
3362*4882a593Smuzhiyun 
3363*4882a593Smuzhiyun 	BCM_REFERENCE(pkt_send);
3364*4882a593Smuzhiyun 
3365*4882a593Smuzhiyun exit:
3366*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
3367*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhdp);
3368*4882a593Smuzhiyun 		if (ctx && ctx->pkt_cnt_in_psq && pkt_send) {
3369*4882a593Smuzhiyun 			wait_msec = msecs_to_jiffies(WLFC_THREAD_QUICK_RETRY_WAIT_MS);
3370*4882a593Smuzhiyun 		} else {
3371*4882a593Smuzhiyun 			wait_msec = msecs_to_jiffies(WLFC_THREAD_RETRY_WAIT_MS);
3372*4882a593Smuzhiyun 		}
3373*4882a593Smuzhiyun 	}
3374*4882a593Smuzhiyun 	return 0;
3375*4882a593Smuzhiyun #else
3376*4882a593Smuzhiyun 	return rc;
3377*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
3378*4882a593Smuzhiyun }
3379*4882a593Smuzhiyun 
3380*4882a593Smuzhiyun /**
3381*4882a593Smuzhiyun  * Enqueues a transmit packet in the next layer towards the dongle, eg the DBUS layer. Called by
3382*4882a593Smuzhiyun  * eg dhd_sendpkt().
3383*4882a593Smuzhiyun  *     @param[in] dhdp                  Pointer to public DHD structure
3384*4882a593Smuzhiyun  *     @param[in] fcommit               Pointer to transmit function of next layer
3385*4882a593Smuzhiyun  *     @param[in] commit_ctx            Opaque context used when calling next layer
3386*4882a593Smuzhiyun  *     @param[in] pktbuf                Packet to send
3387*4882a593Smuzhiyun  *     @param[in] need_toggle_host_if   If TRUE, resets flag ctx->toggle_host_if
3388*4882a593Smuzhiyun  */
3389*4882a593Smuzhiyun int
dhd_wlfc_commit_packets(dhd_pub_t * dhdp,f_commitpkt_t fcommit,void * commit_ctx,void * pktbuf,bool need_toggle_host_if)3390*4882a593Smuzhiyun dhd_wlfc_commit_packets(dhd_pub_t *dhdp, f_commitpkt_t fcommit, void* commit_ctx, void *pktbuf,
3391*4882a593Smuzhiyun 	bool need_toggle_host_if)
3392*4882a593Smuzhiyun {
3393*4882a593Smuzhiyun 	int rc = BCME_OK;
3394*4882a593Smuzhiyun 	athost_wl_status_info_t* ctx;
3395*4882a593Smuzhiyun 
3396*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
3397*4882a593Smuzhiyun 	if (!pktbuf)
3398*4882a593Smuzhiyun 		return BCME_OK;
3399*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
3400*4882a593Smuzhiyun 
3401*4882a593Smuzhiyun 	if ((dhdp == NULL) || (fcommit == NULL)) {
3402*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3403*4882a593Smuzhiyun 		return BCME_BADARG;
3404*4882a593Smuzhiyun 	}
3405*4882a593Smuzhiyun 
3406*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhdp);
3407*4882a593Smuzhiyun 
3408*4882a593Smuzhiyun 	if (!dhdp->wlfc_state || (dhdp->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3409*4882a593Smuzhiyun 		if (pktbuf) {
3410*4882a593Smuzhiyun 			DHD_PKTTAG_WLFCPKT_SET(PKTTAG(pktbuf), 0);
3411*4882a593Smuzhiyun 		}
3412*4882a593Smuzhiyun 		rc =  WLFC_UNSUPPORTED;
3413*4882a593Smuzhiyun 		goto exit;
3414*4882a593Smuzhiyun 	}
3415*4882a593Smuzhiyun 
3416*4882a593Smuzhiyun 	ctx = (athost_wl_status_info_t*)dhdp->wlfc_state;
3417*4882a593Smuzhiyun 
3418*4882a593Smuzhiyun 	if (dhdp->proptxstatus_module_ignore) {
3419*4882a593Smuzhiyun 		if (pktbuf) {
3420*4882a593Smuzhiyun 			uint32 htod = 0;
3421*4882a593Smuzhiyun 			WL_TXSTATUS_SET_FLAGS(htod, WLFC_PKTFLAG_PKTFROMHOST);
3422*4882a593Smuzhiyun 			_dhd_wlfc_pushheader(ctx, &pktbuf, FALSE, 0, 0, htod, 0, FALSE);
3423*4882a593Smuzhiyun 			if (fcommit(commit_ctx, pktbuf)) {
3424*4882a593Smuzhiyun 				/* free it if failed, otherwise do it in tx complete cb */
3425*4882a593Smuzhiyun 				PKTFREE(ctx->osh, pktbuf, TRUE);
3426*4882a593Smuzhiyun 			}
3427*4882a593Smuzhiyun 			rc = BCME_OK;
3428*4882a593Smuzhiyun 		}
3429*4882a593Smuzhiyun 		goto exit;
3430*4882a593Smuzhiyun 	}
3431*4882a593Smuzhiyun 
3432*4882a593Smuzhiyun 	if (pktbuf) {
3433*4882a593Smuzhiyun 		int ac = DHD_PKTTAG_FIFO(PKTTAG(pktbuf));
3434*4882a593Smuzhiyun 		ASSERT(ac <= AC_COUNT);
3435*4882a593Smuzhiyun 		DHD_PKTTAG_WLFCPKT_SET(PKTTAG(pktbuf), 1);
3436*4882a593Smuzhiyun 		/* en-queue the packets to respective queue. */
3437*4882a593Smuzhiyun 		rc = _dhd_wlfc_enque_delayq(ctx, pktbuf, ac);
3438*4882a593Smuzhiyun 		if (rc) {
3439*4882a593Smuzhiyun 			_dhd_wlfc_prec_drop(ctx->dhdp, (ac << 1), pktbuf, FALSE);
3440*4882a593Smuzhiyun 		} else {
3441*4882a593Smuzhiyun 			ctx->stats.pktin++;
3442*4882a593Smuzhiyun 			ctx->pkt_cnt_in_drv[DHD_PKTTAG_IF(PKTTAG(pktbuf))][ac]++;
3443*4882a593Smuzhiyun 		}
3444*4882a593Smuzhiyun 	}
3445*4882a593Smuzhiyun 
3446*4882a593Smuzhiyun 	if (!ctx->fcommit) {
3447*4882a593Smuzhiyun 		ctx->fcommit = fcommit;
3448*4882a593Smuzhiyun 	} else {
3449*4882a593Smuzhiyun 		ASSERT(ctx->fcommit == fcommit);
3450*4882a593Smuzhiyun 	}
3451*4882a593Smuzhiyun 	if (!ctx->commit_ctx) {
3452*4882a593Smuzhiyun 		ctx->commit_ctx = commit_ctx;
3453*4882a593Smuzhiyun 	} else {
3454*4882a593Smuzhiyun 		ASSERT(ctx->commit_ctx == commit_ctx);
3455*4882a593Smuzhiyun 	}
3456*4882a593Smuzhiyun 
3457*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
3458*4882a593Smuzhiyun 	_dhd_wlfc_thread_wakeup(dhdp);
3459*4882a593Smuzhiyun #else
3460*4882a593Smuzhiyun 	dhd_wlfc_transfer_packets(dhdp);
3461*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
3462*4882a593Smuzhiyun 
3463*4882a593Smuzhiyun exit:
3464*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhdp);
3465*4882a593Smuzhiyun 	return rc;
3466*4882a593Smuzhiyun } /* dhd_wlfc_commit_packets */
3467*4882a593Smuzhiyun 
3468*4882a593Smuzhiyun /**
3469*4882a593Smuzhiyun  * Called when the (lower) DBUS layer indicates completion (succesfull or not) of a transmit packet
3470*4882a593Smuzhiyun  */
3471*4882a593Smuzhiyun int
dhd_wlfc_txcomplete(dhd_pub_t * dhd,void * txp,bool success)3472*4882a593Smuzhiyun dhd_wlfc_txcomplete(dhd_pub_t *dhd, void *txp, bool success)
3473*4882a593Smuzhiyun {
3474*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc;
3475*4882a593Smuzhiyun 	wlfc_mac_descriptor_t *entry;
3476*4882a593Smuzhiyun 	void* pout = NULL;
3477*4882a593Smuzhiyun 	int rtn = BCME_OK;
3478*4882a593Smuzhiyun 	if ((dhd == NULL) || (txp == NULL)) {
3479*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3480*4882a593Smuzhiyun 		return BCME_BADARG;
3481*4882a593Smuzhiyun 	}
3482*4882a593Smuzhiyun 
3483*4882a593Smuzhiyun 	bcm_pkt_validate_chk(txp);
3484*4882a593Smuzhiyun 
3485*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3486*4882a593Smuzhiyun 
3487*4882a593Smuzhiyun 	if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3488*4882a593Smuzhiyun 		rtn = WLFC_UNSUPPORTED;
3489*4882a593Smuzhiyun 		goto EXIT;
3490*4882a593Smuzhiyun 	}
3491*4882a593Smuzhiyun 
3492*4882a593Smuzhiyun 	wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
3493*4882a593Smuzhiyun 	if (DHD_PKTTAG_SIGNALONLY(PKTTAG(txp))) {
3494*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
3495*4882a593Smuzhiyun 		wlfc->stats.signal_only_pkts_freed++;
3496*4882a593Smuzhiyun #endif // endif
3497*4882a593Smuzhiyun 		/* is this a signal-only packet? */
3498*4882a593Smuzhiyun 		_dhd_wlfc_pullheader(wlfc, txp);
3499*4882a593Smuzhiyun 		PKTFREE(wlfc->osh, txp, TRUE);
3500*4882a593Smuzhiyun 		goto EXIT;
3501*4882a593Smuzhiyun 	}
3502*4882a593Smuzhiyun 
3503*4882a593Smuzhiyun 	entry = _dhd_wlfc_find_table_entry(wlfc, txp);
3504*4882a593Smuzhiyun 	ASSERT(entry);
3505*4882a593Smuzhiyun 
3506*4882a593Smuzhiyun 	if (!success || dhd->proptxstatus_txstatus_ignore) {
3507*4882a593Smuzhiyun 		WLFC_DBGMESG(("At: %s():%d, bus_complete() failure for %p, htod_tag:0x%08x\n",
3508*4882a593Smuzhiyun 			__FUNCTION__, __LINE__, txp, DHD_PKTTAG_H2DTAG(PKTTAG(txp))));
3509*4882a593Smuzhiyun 		if (!WLFC_GET_AFQ(dhd->wlfc_mode)) {
3510*4882a593Smuzhiyun 			_dhd_wlfc_hanger_poppkt(wlfc->hanger, WL_TXSTATUS_GET_HSLOT(
3511*4882a593Smuzhiyun 				DHD_PKTTAG_H2DTAG(PKTTAG(txp))), &pout, TRUE);
3512*4882a593Smuzhiyun 			ASSERT(txp == pout);
3513*4882a593Smuzhiyun 		}
3514*4882a593Smuzhiyun 
3515*4882a593Smuzhiyun 		/* indicate failure and free the packet */
3516*4882a593Smuzhiyun 		dhd_txcomplete(dhd, txp, success);
3517*4882a593Smuzhiyun 
3518*4882a593Smuzhiyun 		/* return the credit, if necessary */
3519*4882a593Smuzhiyun 		_dhd_wlfc_return_implied_credit(wlfc, txp);
3520*4882a593Smuzhiyun 
3521*4882a593Smuzhiyun 		if (entry->transit_count)
3522*4882a593Smuzhiyun 			entry->transit_count--;
3523*4882a593Smuzhiyun 		if (entry->suppr_transit_count)
3524*4882a593Smuzhiyun 			entry->suppr_transit_count--;
3525*4882a593Smuzhiyun 		wlfc->pkt_cnt_in_drv[DHD_PKTTAG_IF(PKTTAG(txp))][DHD_PKTTAG_FIFO(PKTTAG(txp))]--;
3526*4882a593Smuzhiyun 		wlfc->stats.pktout++;
3527*4882a593Smuzhiyun 		PKTFREE(wlfc->osh, txp, TRUE);
3528*4882a593Smuzhiyun 	} else {
3529*4882a593Smuzhiyun 		/* bus confirmed pkt went to firmware side */
3530*4882a593Smuzhiyun 		if (WLFC_GET_AFQ(dhd->wlfc_mode)) {
3531*4882a593Smuzhiyun 			_dhd_wlfc_enque_afq(wlfc, txp);
3532*4882a593Smuzhiyun 		} else {
3533*4882a593Smuzhiyun 			int hslot = WL_TXSTATUS_GET_HSLOT(DHD_PKTTAG_H2DTAG(PKTTAG(txp)));
3534*4882a593Smuzhiyun 			_dhd_wlfc_hanger_free_pkt(wlfc, hslot,
3535*4882a593Smuzhiyun 				WLFC_HANGER_PKT_STATE_BUSRETURNED, -1);
3536*4882a593Smuzhiyun 		}
3537*4882a593Smuzhiyun 	}
3538*4882a593Smuzhiyun 
3539*4882a593Smuzhiyun 	ASSERT(entry->onbus_pkts_count > 0);
3540*4882a593Smuzhiyun 	if (entry->onbus_pkts_count > 0)
3541*4882a593Smuzhiyun 		entry->onbus_pkts_count--;
3542*4882a593Smuzhiyun 	if (entry->suppressed &&
3543*4882a593Smuzhiyun 		(!entry->onbus_pkts_count) &&
3544*4882a593Smuzhiyun 		(!entry->suppr_transit_count))
3545*4882a593Smuzhiyun 		entry->suppressed = FALSE;
3546*4882a593Smuzhiyun EXIT:
3547*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3548*4882a593Smuzhiyun 	return rtn;
3549*4882a593Smuzhiyun } /* dhd_wlfc_txcomplete */
3550*4882a593Smuzhiyun 
3551*4882a593Smuzhiyun int
dhd_wlfc_init(dhd_pub_t * dhd)3552*4882a593Smuzhiyun dhd_wlfc_init(dhd_pub_t *dhd)
3553*4882a593Smuzhiyun {
3554*4882a593Smuzhiyun 	/* enable all signals & indicate host proptxstatus logic is active */
3555*4882a593Smuzhiyun 	uint32 tlv, mode, fw_caps;
3556*4882a593Smuzhiyun 	int ret = 0;
3557*4882a593Smuzhiyun 
3558*4882a593Smuzhiyun 	if (dhd == NULL) {
3559*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3560*4882a593Smuzhiyun 		return BCME_BADARG;
3561*4882a593Smuzhiyun 	}
3562*4882a593Smuzhiyun 
3563*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3564*4882a593Smuzhiyun 	if (dhd->wlfc_enabled) {
3565*4882a593Smuzhiyun 		DHD_INFO(("%s():%d, Already enabled!\n", __FUNCTION__, __LINE__));
3566*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
3567*4882a593Smuzhiyun 		return BCME_OK;
3568*4882a593Smuzhiyun 	}
3569*4882a593Smuzhiyun 	dhd->wlfc_enabled = TRUE;
3570*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3571*4882a593Smuzhiyun 
3572*4882a593Smuzhiyun 	tlv = WLFC_FLAGS_RSSI_SIGNALS |
3573*4882a593Smuzhiyun 		WLFC_FLAGS_XONXOFF_SIGNALS |
3574*4882a593Smuzhiyun 		WLFC_FLAGS_CREDIT_STATUS_SIGNALS |
3575*4882a593Smuzhiyun 		WLFC_FLAGS_HOST_PROPTXSTATUS_ACTIVE |
3576*4882a593Smuzhiyun 		WLFC_FLAGS_HOST_RXRERODER_ACTIVE;
3577*4882a593Smuzhiyun 
3578*4882a593Smuzhiyun 	/*
3579*4882a593Smuzhiyun 	try to enable/disable signaling by sending "tlv" iovar. if that fails,
3580*4882a593Smuzhiyun 	fallback to no flow control? Print a message for now.
3581*4882a593Smuzhiyun 	*/
3582*4882a593Smuzhiyun 
3583*4882a593Smuzhiyun 	/* enable proptxtstatus signaling by default */
3584*4882a593Smuzhiyun 	if (!dhd_wl_ioctl_set_intiovar(dhd, "tlv", tlv, WLC_SET_VAR, TRUE, 0)) {
3585*4882a593Smuzhiyun 		/*
3586*4882a593Smuzhiyun 		Leaving the message for now, it should be removed after a while; once
3587*4882a593Smuzhiyun 		the tlv situation is stable.
3588*4882a593Smuzhiyun 		*/
3589*4882a593Smuzhiyun 		DHD_INFO(("dhd_wlfc_init(): successfully %s bdcv2 tlv signaling, %d\n",
3590*4882a593Smuzhiyun 			dhd->wlfc_enabled?"enabled":"disabled", tlv));
3591*4882a593Smuzhiyun 	}
3592*4882a593Smuzhiyun 
3593*4882a593Smuzhiyun 	mode = 0;
3594*4882a593Smuzhiyun 
3595*4882a593Smuzhiyun 	/* query caps */
3596*4882a593Smuzhiyun 	ret = dhd_wl_ioctl_get_intiovar(dhd, "wlfc_mode", &fw_caps, WLC_GET_VAR, FALSE, 0);
3597*4882a593Smuzhiyun 
3598*4882a593Smuzhiyun 	if (!ret) {
3599*4882a593Smuzhiyun 		DHD_INFO(("%s: query wlfc_mode succeed, fw_caps=0x%x\n", __FUNCTION__, fw_caps));
3600*4882a593Smuzhiyun 
3601*4882a593Smuzhiyun 		if (WLFC_IS_OLD_DEF(fw_caps)) {
3602*4882a593Smuzhiyun 			/* enable proptxtstatus v2 by default */
3603*4882a593Smuzhiyun 			mode = WLFC_MODE_AFQ;
3604*4882a593Smuzhiyun 		} else {
3605*4882a593Smuzhiyun 			WLFC_SET_AFQ(mode, WLFC_GET_AFQ(fw_caps));
3606*4882a593Smuzhiyun 			WLFC_SET_REORDERSUPP(mode, WLFC_GET_REORDERSUPP(fw_caps));
3607*4882a593Smuzhiyun 		}
3608*4882a593Smuzhiyun 		ret = dhd_wl_ioctl_set_intiovar(dhd, "wlfc_mode", mode, WLC_SET_VAR, TRUE, 0);
3609*4882a593Smuzhiyun 	}
3610*4882a593Smuzhiyun 
3611*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3612*4882a593Smuzhiyun 
3613*4882a593Smuzhiyun 	dhd->wlfc_mode = 0;
3614*4882a593Smuzhiyun 	if (ret >= 0) {
3615*4882a593Smuzhiyun 		if (WLFC_IS_OLD_DEF(mode)) {
3616*4882a593Smuzhiyun 			WLFC_SET_AFQ(dhd->wlfc_mode, (mode == WLFC_MODE_AFQ));
3617*4882a593Smuzhiyun 		} else {
3618*4882a593Smuzhiyun 			dhd->wlfc_mode = mode;
3619*4882a593Smuzhiyun 		}
3620*4882a593Smuzhiyun 	}
3621*4882a593Smuzhiyun 
3622*4882a593Smuzhiyun 	DHD_INFO(("dhd_wlfc_init(): wlfc_mode=0x%x, ret=%d\n", dhd->wlfc_mode, ret));
3623*4882a593Smuzhiyun #ifdef LIMIT_BORROW
3624*4882a593Smuzhiyun 	dhd->wlfc_borrow_allowed = TRUE;
3625*4882a593Smuzhiyun #endif // endif
3626*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3627*4882a593Smuzhiyun 
3628*4882a593Smuzhiyun 	if (dhd->plat_init)
3629*4882a593Smuzhiyun 		dhd->plat_init((void *)dhd);
3630*4882a593Smuzhiyun 
3631*4882a593Smuzhiyun 	return BCME_OK;
3632*4882a593Smuzhiyun } /* dhd_wlfc_init */
3633*4882a593Smuzhiyun 
3634*4882a593Smuzhiyun /** AMPDU host reorder specific function */
3635*4882a593Smuzhiyun int
dhd_wlfc_hostreorder_init(dhd_pub_t * dhd)3636*4882a593Smuzhiyun dhd_wlfc_hostreorder_init(dhd_pub_t *dhd)
3637*4882a593Smuzhiyun {
3638*4882a593Smuzhiyun 	/* enable only ampdu hostreorder here */
3639*4882a593Smuzhiyun 	uint32 tlv;
3640*4882a593Smuzhiyun 
3641*4882a593Smuzhiyun 	if (dhd == NULL) {
3642*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3643*4882a593Smuzhiyun 		return BCME_BADARG;
3644*4882a593Smuzhiyun 	}
3645*4882a593Smuzhiyun 
3646*4882a593Smuzhiyun 	DHD_TRACE(("%s():%d Enter\n", __FUNCTION__, __LINE__));
3647*4882a593Smuzhiyun 
3648*4882a593Smuzhiyun 	tlv = WLFC_FLAGS_HOST_RXRERODER_ACTIVE;
3649*4882a593Smuzhiyun 
3650*4882a593Smuzhiyun 	/* enable proptxtstatus signaling by default */
3651*4882a593Smuzhiyun 	if (dhd_wl_ioctl_set_intiovar(dhd, "tlv", tlv, WLC_SET_VAR, TRUE, 0)) {
3652*4882a593Smuzhiyun 		DHD_ERROR(("%s(): failed to enable/disable bdcv2 tlv signaling\n",
3653*4882a593Smuzhiyun 			__FUNCTION__));
3654*4882a593Smuzhiyun 	} else {
3655*4882a593Smuzhiyun 		/*
3656*4882a593Smuzhiyun 		Leaving the message for now, it should be removed after a while; once
3657*4882a593Smuzhiyun 		the tlv situation is stable.
3658*4882a593Smuzhiyun 		*/
3659*4882a593Smuzhiyun 		DHD_ERROR(("%s(): successful bdcv2 tlv signaling, %d\n",
3660*4882a593Smuzhiyun 			__FUNCTION__, tlv));
3661*4882a593Smuzhiyun 	}
3662*4882a593Smuzhiyun 
3663*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3664*4882a593Smuzhiyun 	dhd->proptxstatus_mode = WLFC_ONLY_AMPDU_HOSTREORDER;
3665*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3666*4882a593Smuzhiyun 
3667*4882a593Smuzhiyun 	return BCME_OK;
3668*4882a593Smuzhiyun }
3669*4882a593Smuzhiyun 
3670*4882a593Smuzhiyun int
dhd_wlfc_cleanup_txq(dhd_pub_t * dhd,f_processpkt_t fn,void * arg)3671*4882a593Smuzhiyun dhd_wlfc_cleanup_txq(dhd_pub_t *dhd, f_processpkt_t fn, void *arg)
3672*4882a593Smuzhiyun {
3673*4882a593Smuzhiyun 	if (dhd == NULL) {
3674*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3675*4882a593Smuzhiyun 		return BCME_BADARG;
3676*4882a593Smuzhiyun 	}
3677*4882a593Smuzhiyun 
3678*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3679*4882a593Smuzhiyun 
3680*4882a593Smuzhiyun 	if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3681*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
3682*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
3683*4882a593Smuzhiyun 	}
3684*4882a593Smuzhiyun 
3685*4882a593Smuzhiyun 	_dhd_wlfc_cleanup_txq(dhd, fn, arg);
3686*4882a593Smuzhiyun 
3687*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3688*4882a593Smuzhiyun 
3689*4882a593Smuzhiyun 	return BCME_OK;
3690*4882a593Smuzhiyun }
3691*4882a593Smuzhiyun 
3692*4882a593Smuzhiyun /** release all packet resources */
3693*4882a593Smuzhiyun int
dhd_wlfc_cleanup(dhd_pub_t * dhd,f_processpkt_t fn,void * arg)3694*4882a593Smuzhiyun dhd_wlfc_cleanup(dhd_pub_t *dhd, f_processpkt_t fn, void *arg)
3695*4882a593Smuzhiyun {
3696*4882a593Smuzhiyun 	if (dhd == NULL) {
3697*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3698*4882a593Smuzhiyun 		return BCME_BADARG;
3699*4882a593Smuzhiyun 	}
3700*4882a593Smuzhiyun 
3701*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3702*4882a593Smuzhiyun 
3703*4882a593Smuzhiyun 	if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3704*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
3705*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
3706*4882a593Smuzhiyun 	}
3707*4882a593Smuzhiyun 
3708*4882a593Smuzhiyun 	_dhd_wlfc_cleanup(dhd, fn, arg);
3709*4882a593Smuzhiyun 
3710*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3711*4882a593Smuzhiyun 
3712*4882a593Smuzhiyun 	return BCME_OK;
3713*4882a593Smuzhiyun }
3714*4882a593Smuzhiyun 
3715*4882a593Smuzhiyun int
dhd_wlfc_deinit(dhd_pub_t * dhd)3716*4882a593Smuzhiyun dhd_wlfc_deinit(dhd_pub_t *dhd)
3717*4882a593Smuzhiyun {
3718*4882a593Smuzhiyun 	/* cleanup all psq related resources */
3719*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc;
3720*4882a593Smuzhiyun 	uint32 tlv = 0;
3721*4882a593Smuzhiyun 	uint32 hostreorder = 0;
3722*4882a593Smuzhiyun 
3723*4882a593Smuzhiyun 	if (dhd == NULL) {
3724*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3725*4882a593Smuzhiyun 		return BCME_BADARG;
3726*4882a593Smuzhiyun 	}
3727*4882a593Smuzhiyun 
3728*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3729*4882a593Smuzhiyun 	if (!dhd->wlfc_enabled) {
3730*4882a593Smuzhiyun 		DHD_ERROR(("%s():%d, Already disabled!\n", __FUNCTION__, __LINE__));
3731*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
3732*4882a593Smuzhiyun 		return BCME_OK;
3733*4882a593Smuzhiyun 	}
3734*4882a593Smuzhiyun 
3735*4882a593Smuzhiyun 	dhd->wlfc_enabled = FALSE;
3736*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3737*4882a593Smuzhiyun 
3738*4882a593Smuzhiyun 	/* query ampdu hostreorder */
3739*4882a593Smuzhiyun 	(void) dhd_wl_ioctl_get_intiovar(dhd, "ampdu_hostreorder",
3740*4882a593Smuzhiyun 		&hostreorder, WLC_GET_VAR, FALSE, 0);
3741*4882a593Smuzhiyun 
3742*4882a593Smuzhiyun 	if (hostreorder) {
3743*4882a593Smuzhiyun 		tlv = WLFC_FLAGS_HOST_RXRERODER_ACTIVE;
3744*4882a593Smuzhiyun 		DHD_ERROR(("%s():%d, maintain HOST RXRERODER flag in tvl\n",
3745*4882a593Smuzhiyun 			__FUNCTION__, __LINE__));
3746*4882a593Smuzhiyun 	}
3747*4882a593Smuzhiyun 
3748*4882a593Smuzhiyun 	/* Disable proptxtstatus signaling for deinit */
3749*4882a593Smuzhiyun 	(void) dhd_wl_ioctl_set_intiovar(dhd, "tlv", tlv, WLC_SET_VAR, TRUE, 0);
3750*4882a593Smuzhiyun 
3751*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
3752*4882a593Smuzhiyun 
3753*4882a593Smuzhiyun 	if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3754*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
3755*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
3756*4882a593Smuzhiyun 	}
3757*4882a593Smuzhiyun 
3758*4882a593Smuzhiyun 	wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
3759*4882a593Smuzhiyun 
3760*4882a593Smuzhiyun 	_dhd_wlfc_cleanup(dhd, NULL, NULL);
3761*4882a593Smuzhiyun 
3762*4882a593Smuzhiyun 	if (!WLFC_GET_AFQ(dhd->wlfc_mode)) {
3763*4882a593Smuzhiyun 		int i;
3764*4882a593Smuzhiyun 		wlfc_hanger_t* h = (wlfc_hanger_t*)wlfc->hanger;
3765*4882a593Smuzhiyun 		for (i = 0; i < h->max_items; i++) {
3766*4882a593Smuzhiyun 			if (h->items[i].state != WLFC_HANGER_ITEM_STATE_FREE) {
3767*4882a593Smuzhiyun 				_dhd_wlfc_hanger_free_pkt(wlfc, i,
3768*4882a593Smuzhiyun 					WLFC_HANGER_PKT_STATE_COMPLETE, TRUE);
3769*4882a593Smuzhiyun 			}
3770*4882a593Smuzhiyun 		}
3771*4882a593Smuzhiyun 
3772*4882a593Smuzhiyun 		/* delete hanger */
3773*4882a593Smuzhiyun 		_dhd_wlfc_hanger_delete(dhd, h);
3774*4882a593Smuzhiyun 	}
3775*4882a593Smuzhiyun 
3776*4882a593Smuzhiyun 	/* free top structure */
3777*4882a593Smuzhiyun 	DHD_OS_PREFREE(dhd, dhd->wlfc_state,
3778*4882a593Smuzhiyun 		sizeof(athost_wl_status_info_t));
3779*4882a593Smuzhiyun 	dhd->wlfc_state = NULL;
3780*4882a593Smuzhiyun 	dhd->proptxstatus_mode = hostreorder ?
3781*4882a593Smuzhiyun 		WLFC_ONLY_AMPDU_HOSTREORDER : WLFC_FCMODE_NONE;
3782*4882a593Smuzhiyun 
3783*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
3784*4882a593Smuzhiyun 
3785*4882a593Smuzhiyun 	if (dhd->plat_deinit)
3786*4882a593Smuzhiyun 		dhd->plat_deinit((void *)dhd);
3787*4882a593Smuzhiyun 	return BCME_OK;
3788*4882a593Smuzhiyun } /* dhd_wlfc_init */
3789*4882a593Smuzhiyun 
3790*4882a593Smuzhiyun /**
3791*4882a593Smuzhiyun  * Called on an interface event (WLC_E_IF) indicated by firmware
3792*4882a593Smuzhiyun  *     @param[in] dhdp   Pointer to public DHD structure
3793*4882a593Smuzhiyun  *     @param[in] action eg eWLFC_MAC_ENTRY_ACTION_UPDATE or eWLFC_MAC_ENTRY_ACTION_ADD
3794*4882a593Smuzhiyun  */
dhd_wlfc_interface_event(dhd_pub_t * dhdp,uint8 action,uint8 ifid,uint8 iftype,uint8 * ea)3795*4882a593Smuzhiyun int dhd_wlfc_interface_event(dhd_pub_t *dhdp, uint8 action, uint8 ifid, uint8 iftype, uint8* ea)
3796*4882a593Smuzhiyun {
3797*4882a593Smuzhiyun 	int rc;
3798*4882a593Smuzhiyun 
3799*4882a593Smuzhiyun 	if (dhdp == NULL) {
3800*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3801*4882a593Smuzhiyun 		return BCME_BADARG;
3802*4882a593Smuzhiyun 	}
3803*4882a593Smuzhiyun 
3804*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhdp);
3805*4882a593Smuzhiyun 
3806*4882a593Smuzhiyun 	if (!dhdp->wlfc_state || (dhdp->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3807*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhdp);
3808*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
3809*4882a593Smuzhiyun 	}
3810*4882a593Smuzhiyun 
3811*4882a593Smuzhiyun 	rc = _dhd_wlfc_interface_entry_update(dhdp->wlfc_state, action, ifid, iftype, ea);
3812*4882a593Smuzhiyun 
3813*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhdp);
3814*4882a593Smuzhiyun 	return rc;
3815*4882a593Smuzhiyun }
3816*4882a593Smuzhiyun 
3817*4882a593Smuzhiyun /** Called eg on receiving a WLC_E_FIFO_CREDIT_MAP event from the dongle */
dhd_wlfc_FIFOcreditmap_event(dhd_pub_t * dhdp,uint8 * event_data)3818*4882a593Smuzhiyun int dhd_wlfc_FIFOcreditmap_event(dhd_pub_t *dhdp, uint8* event_data)
3819*4882a593Smuzhiyun {
3820*4882a593Smuzhiyun 	int rc;
3821*4882a593Smuzhiyun 
3822*4882a593Smuzhiyun 	if (dhdp == NULL) {
3823*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3824*4882a593Smuzhiyun 		return BCME_BADARG;
3825*4882a593Smuzhiyun 	}
3826*4882a593Smuzhiyun 
3827*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhdp);
3828*4882a593Smuzhiyun 
3829*4882a593Smuzhiyun 	if (!dhdp->wlfc_state || (dhdp->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3830*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhdp);
3831*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
3832*4882a593Smuzhiyun 	}
3833*4882a593Smuzhiyun 
3834*4882a593Smuzhiyun 	rc = _dhd_wlfc_FIFOcreditmap_update(dhdp->wlfc_state, event_data);
3835*4882a593Smuzhiyun 
3836*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhdp);
3837*4882a593Smuzhiyun 
3838*4882a593Smuzhiyun 	return rc;
3839*4882a593Smuzhiyun }
3840*4882a593Smuzhiyun #ifdef LIMIT_BORROW
dhd_wlfc_disable_credit_borrow_event(dhd_pub_t * dhdp,uint8 * event_data)3841*4882a593Smuzhiyun int dhd_wlfc_disable_credit_borrow_event(dhd_pub_t *dhdp, uint8* event_data)
3842*4882a593Smuzhiyun {
3843*4882a593Smuzhiyun 	if (dhdp == NULL || event_data == NULL) {
3844*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3845*4882a593Smuzhiyun 		return BCME_BADARG;
3846*4882a593Smuzhiyun 	}
3847*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhdp);
3848*4882a593Smuzhiyun 	dhdp->wlfc_borrow_allowed = (bool)(*(uint32 *)event_data);
3849*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhdp);
3850*4882a593Smuzhiyun 
3851*4882a593Smuzhiyun 	return BCME_OK;
3852*4882a593Smuzhiyun }
3853*4882a593Smuzhiyun #endif /* LIMIT_BORROW */
3854*4882a593Smuzhiyun 
3855*4882a593Smuzhiyun /**
3856*4882a593Smuzhiyun  * Called eg on receiving a WLC_E_BCMC_CREDIT_SUPPORT event from the dongle (broadcast/multicast
3857*4882a593Smuzhiyun  * specific)
3858*4882a593Smuzhiyun  */
dhd_wlfc_BCMCCredit_support_event(dhd_pub_t * dhdp)3859*4882a593Smuzhiyun int dhd_wlfc_BCMCCredit_support_event(dhd_pub_t *dhdp)
3860*4882a593Smuzhiyun {
3861*4882a593Smuzhiyun 	int rc;
3862*4882a593Smuzhiyun 
3863*4882a593Smuzhiyun 	if (dhdp == NULL) {
3864*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3865*4882a593Smuzhiyun 		return BCME_BADARG;
3866*4882a593Smuzhiyun 	}
3867*4882a593Smuzhiyun 
3868*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhdp);
3869*4882a593Smuzhiyun 
3870*4882a593Smuzhiyun 	if (!dhdp->wlfc_state || (dhdp->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3871*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhdp);
3872*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
3873*4882a593Smuzhiyun 	}
3874*4882a593Smuzhiyun 
3875*4882a593Smuzhiyun 	rc = _dhd_wlfc_BCMCCredit_support_update(dhdp->wlfc_state);
3876*4882a593Smuzhiyun 
3877*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhdp);
3878*4882a593Smuzhiyun 	return rc;
3879*4882a593Smuzhiyun }
3880*4882a593Smuzhiyun 
3881*4882a593Smuzhiyun /** debug specific function */
3882*4882a593Smuzhiyun int
dhd_wlfc_dump(dhd_pub_t * dhdp,struct bcmstrbuf * strbuf)3883*4882a593Smuzhiyun dhd_wlfc_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
3884*4882a593Smuzhiyun {
3885*4882a593Smuzhiyun 	int i;
3886*4882a593Smuzhiyun 	uint8* ea;
3887*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc;
3888*4882a593Smuzhiyun 	wlfc_hanger_t* h;
3889*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* mac_table;
3890*4882a593Smuzhiyun 	wlfc_mac_descriptor_t* interfaces;
3891*4882a593Smuzhiyun 	char* iftypes[] = {"STA", "AP", "WDS", "p2pGO", "p2pCL"};
3892*4882a593Smuzhiyun 
3893*4882a593Smuzhiyun 	if (!dhdp || !strbuf) {
3894*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
3895*4882a593Smuzhiyun 		return BCME_BADARG;
3896*4882a593Smuzhiyun 	}
3897*4882a593Smuzhiyun 
3898*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhdp);
3899*4882a593Smuzhiyun 
3900*4882a593Smuzhiyun 	if (!dhdp->wlfc_state || (dhdp->proptxstatus_mode == WLFC_FCMODE_NONE)) {
3901*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhdp);
3902*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
3903*4882a593Smuzhiyun 	}
3904*4882a593Smuzhiyun 
3905*4882a593Smuzhiyun 	wlfc = (athost_wl_status_info_t*)dhdp->wlfc_state;
3906*4882a593Smuzhiyun 
3907*4882a593Smuzhiyun 	h = (wlfc_hanger_t*)wlfc->hanger;
3908*4882a593Smuzhiyun 	if (h == NULL) {
3909*4882a593Smuzhiyun 		bcm_bprintf(strbuf, "wlfc-hanger not initialized yet\n");
3910*4882a593Smuzhiyun 	}
3911*4882a593Smuzhiyun 
3912*4882a593Smuzhiyun 	mac_table = wlfc->destination_entries.nodes;
3913*4882a593Smuzhiyun 	interfaces = wlfc->destination_entries.interfaces;
3914*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "---- wlfc stats ----\n");
3915*4882a593Smuzhiyun 
3916*4882a593Smuzhiyun 	if (!WLFC_GET_AFQ(dhdp->wlfc_mode)) {
3917*4882a593Smuzhiyun 		h = (wlfc_hanger_t*)wlfc->hanger;
3918*4882a593Smuzhiyun 		if (h == NULL) {
3919*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "wlfc-hanger not initialized yet\n");
3920*4882a593Smuzhiyun 		} else {
3921*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "wlfc hanger (pushed,popped,f_push,"
3922*4882a593Smuzhiyun 				"f_pop,f_slot, pending) = (%d,%d,%d,%d,%d,%d)\n",
3923*4882a593Smuzhiyun 				h->pushed,
3924*4882a593Smuzhiyun 				h->popped,
3925*4882a593Smuzhiyun 				h->failed_to_push,
3926*4882a593Smuzhiyun 				h->failed_to_pop,
3927*4882a593Smuzhiyun 				h->failed_slotfind,
3928*4882a593Smuzhiyun 				(h->pushed - h->popped));
3929*4882a593Smuzhiyun 		}
3930*4882a593Smuzhiyun 	}
3931*4882a593Smuzhiyun 
3932*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "wlfc fail(tlv,credit_rqst,mac_update,psmode_update), "
3933*4882a593Smuzhiyun 		"(dq_full,rollback_fail) = (%d,%d,%d,%d), (%d,%d)\n",
3934*4882a593Smuzhiyun 		wlfc->stats.tlv_parse_failed,
3935*4882a593Smuzhiyun 		wlfc->stats.credit_request_failed,
3936*4882a593Smuzhiyun 		wlfc->stats.mac_update_failed,
3937*4882a593Smuzhiyun 		wlfc->stats.psmode_update_failed,
3938*4882a593Smuzhiyun 		wlfc->stats.delayq_full_error,
3939*4882a593Smuzhiyun 		wlfc->stats.rollback_failed);
3940*4882a593Smuzhiyun 
3941*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "PKTS (init_credit,credit,sent,drop_d,drop_s,outoforder) "
3942*4882a593Smuzhiyun 		"(AC0[%d,%d,%d,%d,%d,%d],AC1[%d,%d,%d,%d,%d,%d],AC2[%d,%d,%d,%d,%d,%d],"
3943*4882a593Smuzhiyun 		"AC3[%d,%d,%d,%d,%d,%d],BC_MC[%d,%d,%d,%d,%d,%d])\n",
3944*4882a593Smuzhiyun 		wlfc->Init_FIFO_credit[0], wlfc->FIFO_credit[0], wlfc->stats.send_pkts[0],
3945*4882a593Smuzhiyun 		wlfc->stats.drop_pkts[0], wlfc->stats.drop_pkts[1], wlfc->stats.ooo_pkts[0],
3946*4882a593Smuzhiyun 		wlfc->Init_FIFO_credit[1], wlfc->FIFO_credit[1], wlfc->stats.send_pkts[1],
3947*4882a593Smuzhiyun 		wlfc->stats.drop_pkts[2], wlfc->stats.drop_pkts[3], wlfc->stats.ooo_pkts[1],
3948*4882a593Smuzhiyun 		wlfc->Init_FIFO_credit[2], wlfc->FIFO_credit[2], wlfc->stats.send_pkts[2],
3949*4882a593Smuzhiyun 		wlfc->stats.drop_pkts[4], wlfc->stats.drop_pkts[5], wlfc->stats.ooo_pkts[2],
3950*4882a593Smuzhiyun 		wlfc->Init_FIFO_credit[3], wlfc->FIFO_credit[3], wlfc->stats.send_pkts[3],
3951*4882a593Smuzhiyun 		wlfc->stats.drop_pkts[6], wlfc->stats.drop_pkts[7], wlfc->stats.ooo_pkts[3],
3952*4882a593Smuzhiyun 		wlfc->Init_FIFO_credit[4], wlfc->FIFO_credit[4], wlfc->stats.send_pkts[4],
3953*4882a593Smuzhiyun 		wlfc->stats.drop_pkts[8], wlfc->stats.drop_pkts[9], wlfc->stats.ooo_pkts[4]);
3954*4882a593Smuzhiyun 
3955*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "\n");
3956*4882a593Smuzhiyun 	for (i = 0; i < WLFC_MAX_IFNUM; i++) {
3957*4882a593Smuzhiyun 		if (interfaces[i].occupied) {
3958*4882a593Smuzhiyun 			char* iftype_desc;
3959*4882a593Smuzhiyun 
3960*4882a593Smuzhiyun 			if (interfaces[i].iftype > WLC_E_IF_ROLE_P2P_CLIENT)
3961*4882a593Smuzhiyun 				iftype_desc = "<Unknown";
3962*4882a593Smuzhiyun 			else
3963*4882a593Smuzhiyun 				iftype_desc = iftypes[interfaces[i].iftype];
3964*4882a593Smuzhiyun 
3965*4882a593Smuzhiyun 			ea = interfaces[i].ea;
3966*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "INTERFACE[%d].ea = "
3967*4882a593Smuzhiyun 				"["MACDBG"], if:%d, type: %s "
3968*4882a593Smuzhiyun 				"netif_flow_control:%s\n", i,
3969*4882a593Smuzhiyun 				MAC2STRDBG(ea), interfaces[i].interface_id,
3970*4882a593Smuzhiyun 				iftype_desc, ((wlfc->hostif_flow_state[i] == OFF)
3971*4882a593Smuzhiyun 				? " OFF":" ON"));
3972*4882a593Smuzhiyun 
3973*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "INTERFACE[%d].PSQ(len,state,credit),"
3974*4882a593Smuzhiyun 				"(trans,supp_trans,onbus)"
3975*4882a593Smuzhiyun 				"= (%d,%s,%d),(%d,%d,%d)\n",
3976*4882a593Smuzhiyun 				i,
3977*4882a593Smuzhiyun 				interfaces[i].psq.n_pkts_tot,
3978*4882a593Smuzhiyun 				((interfaces[i].state ==
3979*4882a593Smuzhiyun 				WLFC_STATE_OPEN) ? "OPEN":"CLOSE"),
3980*4882a593Smuzhiyun 				interfaces[i].requested_credit,
3981*4882a593Smuzhiyun 				interfaces[i].transit_count,
3982*4882a593Smuzhiyun 				interfaces[i].suppr_transit_count,
3983*4882a593Smuzhiyun 				interfaces[i].onbus_pkts_count);
3984*4882a593Smuzhiyun 
3985*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "INTERFACE[%d].PSQ"
3986*4882a593Smuzhiyun 				"(delay0,sup0,afq0),(delay1,sup1,afq1),(delay2,sup2,afq2),"
3987*4882a593Smuzhiyun 				"(delay3,sup3,afq3),(delay4,sup4,afq4) = (%d,%d,%d),"
3988*4882a593Smuzhiyun 				"(%d,%d,%d),(%d,%d,%d),(%d,%d,%d),(%d,%d,%d)\n",
3989*4882a593Smuzhiyun 				i,
3990*4882a593Smuzhiyun 				interfaces[i].psq.q[0].n_pkts,
3991*4882a593Smuzhiyun 				interfaces[i].psq.q[1].n_pkts,
3992*4882a593Smuzhiyun 				interfaces[i].afq.q[0].n_pkts,
3993*4882a593Smuzhiyun 				interfaces[i].psq.q[2].n_pkts,
3994*4882a593Smuzhiyun 				interfaces[i].psq.q[3].n_pkts,
3995*4882a593Smuzhiyun 				interfaces[i].afq.q[1].n_pkts,
3996*4882a593Smuzhiyun 				interfaces[i].psq.q[4].n_pkts,
3997*4882a593Smuzhiyun 				interfaces[i].psq.q[5].n_pkts,
3998*4882a593Smuzhiyun 				interfaces[i].afq.q[2].n_pkts,
3999*4882a593Smuzhiyun 				interfaces[i].psq.q[6].n_pkts,
4000*4882a593Smuzhiyun 				interfaces[i].psq.q[7].n_pkts,
4001*4882a593Smuzhiyun 				interfaces[i].afq.q[3].n_pkts,
4002*4882a593Smuzhiyun 				interfaces[i].psq.q[8].n_pkts,
4003*4882a593Smuzhiyun 				interfaces[i].psq.q[9].n_pkts,
4004*4882a593Smuzhiyun 				interfaces[i].afq.q[4].n_pkts);
4005*4882a593Smuzhiyun 		}
4006*4882a593Smuzhiyun 	}
4007*4882a593Smuzhiyun 
4008*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "\n");
4009*4882a593Smuzhiyun 	for (i = 0; i < WLFC_MAC_DESC_TABLE_SIZE; i++) {
4010*4882a593Smuzhiyun 		if (mac_table[i].occupied) {
4011*4882a593Smuzhiyun 			ea = mac_table[i].ea;
4012*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "MAC_table[%d].ea = "
4013*4882a593Smuzhiyun 				"["MACDBG"], if:%d \n", i,
4014*4882a593Smuzhiyun 				MAC2STRDBG(ea),	mac_table[i].interface_id);
4015*4882a593Smuzhiyun 
4016*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "MAC_table[%d].PSQ(len,state,credit),"
4017*4882a593Smuzhiyun 				"(trans,supp_trans,onbus)"
4018*4882a593Smuzhiyun 				"= (%d,%s,%d),(%d,%d,%d)\n",
4019*4882a593Smuzhiyun 				i,
4020*4882a593Smuzhiyun 				mac_table[i].psq.n_pkts_tot,
4021*4882a593Smuzhiyun 				((mac_table[i].state ==
4022*4882a593Smuzhiyun 				WLFC_STATE_OPEN) ? " OPEN":"CLOSE"),
4023*4882a593Smuzhiyun 				mac_table[i].requested_credit,
4024*4882a593Smuzhiyun 				mac_table[i].transit_count,
4025*4882a593Smuzhiyun 				mac_table[i].suppr_transit_count,
4026*4882a593Smuzhiyun 				mac_table[i].onbus_pkts_count);
4027*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
4028*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "MAC_table[%d]: (opened, closed) = (%d, %d)\n",
4029*4882a593Smuzhiyun 				i, mac_table[i].opened_ct, mac_table[i].closed_ct);
4030*4882a593Smuzhiyun #endif // endif
4031*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "MAC_table[%d].PSQ"
4032*4882a593Smuzhiyun 				"(delay0,sup0,afq0),(delay1,sup1,afq1),(delay2,sup2,afq2),"
4033*4882a593Smuzhiyun 				"(delay3,sup3,afq3),(delay4,sup4,afq4) =(%d,%d,%d),"
4034*4882a593Smuzhiyun 				"(%d,%d,%d),(%d,%d,%d),(%d,%d,%d),(%d,%d,%d)\n",
4035*4882a593Smuzhiyun 				i,
4036*4882a593Smuzhiyun 				mac_table[i].psq.q[0].n_pkts,
4037*4882a593Smuzhiyun 				mac_table[i].psq.q[1].n_pkts,
4038*4882a593Smuzhiyun 				mac_table[i].afq.q[0].n_pkts,
4039*4882a593Smuzhiyun 				mac_table[i].psq.q[2].n_pkts,
4040*4882a593Smuzhiyun 				mac_table[i].psq.q[3].n_pkts,
4041*4882a593Smuzhiyun 				mac_table[i].afq.q[1].n_pkts,
4042*4882a593Smuzhiyun 				mac_table[i].psq.q[4].n_pkts,
4043*4882a593Smuzhiyun 				mac_table[i].psq.q[5].n_pkts,
4044*4882a593Smuzhiyun 				mac_table[i].afq.q[2].n_pkts,
4045*4882a593Smuzhiyun 				mac_table[i].psq.q[6].n_pkts,
4046*4882a593Smuzhiyun 				mac_table[i].psq.q[7].n_pkts,
4047*4882a593Smuzhiyun 				mac_table[i].afq.q[3].n_pkts,
4048*4882a593Smuzhiyun 				mac_table[i].psq.q[8].n_pkts,
4049*4882a593Smuzhiyun 				mac_table[i].psq.q[9].n_pkts,
4050*4882a593Smuzhiyun 				mac_table[i].afq.q[4].n_pkts);
4051*4882a593Smuzhiyun 
4052*4882a593Smuzhiyun 		}
4053*4882a593Smuzhiyun 	}
4054*4882a593Smuzhiyun 
4055*4882a593Smuzhiyun #ifdef PROP_TXSTATUS_DEBUG
4056*4882a593Smuzhiyun 	{
4057*4882a593Smuzhiyun 		int avg;
4058*4882a593Smuzhiyun 		int moving_avg = 0;
4059*4882a593Smuzhiyun 		int moving_samples;
4060*4882a593Smuzhiyun 
4061*4882a593Smuzhiyun 		if (wlfc->stats.latency_sample_count) {
4062*4882a593Smuzhiyun 			moving_samples = sizeof(wlfc->stats.deltas)/sizeof(uint32);
4063*4882a593Smuzhiyun 
4064*4882a593Smuzhiyun 			for (i = 0; i < moving_samples; i++)
4065*4882a593Smuzhiyun 				moving_avg += wlfc->stats.deltas[i];
4066*4882a593Smuzhiyun 			moving_avg /= moving_samples;
4067*4882a593Smuzhiyun 
4068*4882a593Smuzhiyun 			avg = (100 * wlfc->stats.total_status_latency) /
4069*4882a593Smuzhiyun 				wlfc->stats.latency_sample_count;
4070*4882a593Smuzhiyun 			bcm_bprintf(strbuf, "txstatus latency (average, last, moving[%d]) = "
4071*4882a593Smuzhiyun 				"(%d.%d, %03d, %03d)\n",
4072*4882a593Smuzhiyun 				moving_samples, avg/100, (avg - (avg/100)*100),
4073*4882a593Smuzhiyun 				wlfc->stats.latency_most_recent,
4074*4882a593Smuzhiyun 				moving_avg);
4075*4882a593Smuzhiyun 		}
4076*4882a593Smuzhiyun 	}
4077*4882a593Smuzhiyun 
4078*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "wlfc- fifo[0-5] credit stats: sent = (%d,%d,%d,%d,%d,%d), "
4079*4882a593Smuzhiyun 		"back = (%d,%d,%d,%d,%d,%d)\n",
4080*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_sent[0],
4081*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_sent[1],
4082*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_sent[2],
4083*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_sent[3],
4084*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_sent[4],
4085*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_sent[5],
4086*4882a593Smuzhiyun 
4087*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_back[0],
4088*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_back[1],
4089*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_back[2],
4090*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_back[3],
4091*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_back[4],
4092*4882a593Smuzhiyun 		wlfc->stats.fifo_credits_back[5]);
4093*4882a593Smuzhiyun 	{
4094*4882a593Smuzhiyun 		uint32 fifo_cr_sent = 0;
4095*4882a593Smuzhiyun 		uint32 fifo_cr_acked = 0;
4096*4882a593Smuzhiyun 		uint32 request_cr_sent = 0;
4097*4882a593Smuzhiyun 		uint32 request_cr_ack = 0;
4098*4882a593Smuzhiyun 		uint32 bc_mc_cr_ack = 0;
4099*4882a593Smuzhiyun 
4100*4882a593Smuzhiyun 		for (i = 0; i < sizeof(wlfc->stats.fifo_credits_sent)/sizeof(uint32); i++) {
4101*4882a593Smuzhiyun 			fifo_cr_sent += wlfc->stats.fifo_credits_sent[i];
4102*4882a593Smuzhiyun 		}
4103*4882a593Smuzhiyun 
4104*4882a593Smuzhiyun 		for (i = 0; i < sizeof(wlfc->stats.fifo_credits_back)/sizeof(uint32); i++) {
4105*4882a593Smuzhiyun 			fifo_cr_acked += wlfc->stats.fifo_credits_back[i];
4106*4882a593Smuzhiyun 		}
4107*4882a593Smuzhiyun 
4108*4882a593Smuzhiyun 		for (i = 0; i < WLFC_MAC_DESC_TABLE_SIZE; i++) {
4109*4882a593Smuzhiyun 			if (wlfc->destination_entries.nodes[i].occupied) {
4110*4882a593Smuzhiyun 				request_cr_sent +=
4111*4882a593Smuzhiyun 					wlfc->destination_entries.nodes[i].dstncredit_sent_packets;
4112*4882a593Smuzhiyun 			}
4113*4882a593Smuzhiyun 		}
4114*4882a593Smuzhiyun 		for (i = 0; i < WLFC_MAX_IFNUM; i++) {
4115*4882a593Smuzhiyun 			if (wlfc->destination_entries.interfaces[i].occupied) {
4116*4882a593Smuzhiyun 				request_cr_sent +=
4117*4882a593Smuzhiyun 				wlfc->destination_entries.interfaces[i].dstncredit_sent_packets;
4118*4882a593Smuzhiyun 			}
4119*4882a593Smuzhiyun 		}
4120*4882a593Smuzhiyun 		for (i = 0; i < WLFC_MAC_DESC_TABLE_SIZE; i++) {
4121*4882a593Smuzhiyun 			if (wlfc->destination_entries.nodes[i].occupied) {
4122*4882a593Smuzhiyun 				request_cr_ack +=
4123*4882a593Smuzhiyun 					wlfc->destination_entries.nodes[i].dstncredit_acks;
4124*4882a593Smuzhiyun 			}
4125*4882a593Smuzhiyun 		}
4126*4882a593Smuzhiyun 		for (i = 0; i < WLFC_MAX_IFNUM; i++) {
4127*4882a593Smuzhiyun 			if (wlfc->destination_entries.interfaces[i].occupied) {
4128*4882a593Smuzhiyun 				request_cr_ack +=
4129*4882a593Smuzhiyun 					wlfc->destination_entries.interfaces[i].dstncredit_acks;
4130*4882a593Smuzhiyun 			}
4131*4882a593Smuzhiyun 		}
4132*4882a593Smuzhiyun 		bcm_bprintf(strbuf, "wlfc- (sent, status) => pq(%d,%d), vq(%d,%d),"
4133*4882a593Smuzhiyun 			"other:%d, bc_mc:%d, signal-only, (sent,freed): (%d,%d)",
4134*4882a593Smuzhiyun 			fifo_cr_sent, fifo_cr_acked,
4135*4882a593Smuzhiyun 			request_cr_sent, request_cr_ack,
4136*4882a593Smuzhiyun 			wlfc->destination_entries.other.dstncredit_acks,
4137*4882a593Smuzhiyun 			bc_mc_cr_ack,
4138*4882a593Smuzhiyun 			wlfc->stats.signal_only_pkts_sent, wlfc->stats.signal_only_pkts_freed);
4139*4882a593Smuzhiyun 	}
4140*4882a593Smuzhiyun #endif /* PROP_TXSTATUS_DEBUG */
4141*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "\n");
4142*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "wlfc- pkt((in,2bus,txstats,hdrpull,out),"
4143*4882a593Smuzhiyun 		"(dropped,hdr_only,wlc_tossed,wlc_dropped,wlc_exptime)"
4144*4882a593Smuzhiyun 		"(freed,free_err,rollback)) = "
4145*4882a593Smuzhiyun 		"((%d,%d,%d,%d,%d),(%d,%d,%d,%d,%d),(%d,%d,%d))\n",
4146*4882a593Smuzhiyun 		wlfc->stats.pktin,
4147*4882a593Smuzhiyun 		wlfc->stats.pkt2bus,
4148*4882a593Smuzhiyun 		wlfc->stats.txstatus_in,
4149*4882a593Smuzhiyun 		wlfc->stats.dhd_hdrpulls,
4150*4882a593Smuzhiyun 		wlfc->stats.pktout,
4151*4882a593Smuzhiyun 
4152*4882a593Smuzhiyun 		wlfc->stats.pktdropped,
4153*4882a593Smuzhiyun 		wlfc->stats.wlfc_header_only_pkt,
4154*4882a593Smuzhiyun 		wlfc->stats.wlc_tossed_pkts,
4155*4882a593Smuzhiyun 		wlfc->stats.pkt_dropped,
4156*4882a593Smuzhiyun 		wlfc->stats.pkt_exptime,
4157*4882a593Smuzhiyun 
4158*4882a593Smuzhiyun 		wlfc->stats.pkt_freed,
4159*4882a593Smuzhiyun 		wlfc->stats.pkt_free_err, wlfc->stats.rollback);
4160*4882a593Smuzhiyun 
4161*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "wlfc- suppress((d11,wlc,err),enq(d11,wl,hq,mac?),retx(d11,wlc,hq)) = "
4162*4882a593Smuzhiyun 		"((%d,%d,%d),(%d,%d,%d,%d),(%d,%d,%d))\n",
4163*4882a593Smuzhiyun 		wlfc->stats.d11_suppress,
4164*4882a593Smuzhiyun 		wlfc->stats.wl_suppress,
4165*4882a593Smuzhiyun 		wlfc->stats.bad_suppress,
4166*4882a593Smuzhiyun 
4167*4882a593Smuzhiyun 		wlfc->stats.psq_d11sup_enq,
4168*4882a593Smuzhiyun 		wlfc->stats.psq_wlsup_enq,
4169*4882a593Smuzhiyun 		wlfc->stats.psq_hostq_enq,
4170*4882a593Smuzhiyun 		wlfc->stats.mac_handle_notfound,
4171*4882a593Smuzhiyun 
4172*4882a593Smuzhiyun 		wlfc->stats.psq_d11sup_retx,
4173*4882a593Smuzhiyun 		wlfc->stats.psq_wlsup_retx,
4174*4882a593Smuzhiyun 		wlfc->stats.psq_hostq_retx);
4175*4882a593Smuzhiyun 
4176*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "wlfc- cleanup(txq,psq,fw) = (%d,%d,%d)\n",
4177*4882a593Smuzhiyun 		wlfc->stats.cleanup_txq_cnt,
4178*4882a593Smuzhiyun 		wlfc->stats.cleanup_psq_cnt,
4179*4882a593Smuzhiyun 		wlfc->stats.cleanup_fw_cnt);
4180*4882a593Smuzhiyun 
4181*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "wlfc- generic error: %d\n", wlfc->stats.generic_error);
4182*4882a593Smuzhiyun 
4183*4882a593Smuzhiyun 	for (i = 0; i < WLFC_MAX_IFNUM; i++) {
4184*4882a593Smuzhiyun 		bcm_bprintf(strbuf, "wlfc- if[%d], pkt_cnt_in_q/AC[0-4] = (%d,%d,%d,%d,%d)\n", i,
4185*4882a593Smuzhiyun 			wlfc->pkt_cnt_in_q[i][0],
4186*4882a593Smuzhiyun 			wlfc->pkt_cnt_in_q[i][1],
4187*4882a593Smuzhiyun 			wlfc->pkt_cnt_in_q[i][2],
4188*4882a593Smuzhiyun 			wlfc->pkt_cnt_in_q[i][3],
4189*4882a593Smuzhiyun 			wlfc->pkt_cnt_in_q[i][4]);
4190*4882a593Smuzhiyun 	}
4191*4882a593Smuzhiyun 	bcm_bprintf(strbuf, "\n");
4192*4882a593Smuzhiyun 
4193*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhdp);
4194*4882a593Smuzhiyun 	return BCME_OK;
4195*4882a593Smuzhiyun } /* dhd_wlfc_dump */
4196*4882a593Smuzhiyun 
dhd_wlfc_clear_counts(dhd_pub_t * dhd)4197*4882a593Smuzhiyun int dhd_wlfc_clear_counts(dhd_pub_t *dhd)
4198*4882a593Smuzhiyun {
4199*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc;
4200*4882a593Smuzhiyun 	wlfc_hanger_t* hanger;
4201*4882a593Smuzhiyun 
4202*4882a593Smuzhiyun 	if (dhd == NULL) {
4203*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4204*4882a593Smuzhiyun 		return BCME_BADARG;
4205*4882a593Smuzhiyun 	}
4206*4882a593Smuzhiyun 
4207*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4208*4882a593Smuzhiyun 
4209*4882a593Smuzhiyun 	if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
4210*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
4211*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
4212*4882a593Smuzhiyun 	}
4213*4882a593Smuzhiyun 
4214*4882a593Smuzhiyun 	wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
4215*4882a593Smuzhiyun 
4216*4882a593Smuzhiyun 	memset(&wlfc->stats, 0, sizeof(athost_wl_stat_counters_t));
4217*4882a593Smuzhiyun 
4218*4882a593Smuzhiyun 	if (!WLFC_GET_AFQ(dhd->wlfc_mode)) {
4219*4882a593Smuzhiyun 		hanger = (wlfc_hanger_t*)wlfc->hanger;
4220*4882a593Smuzhiyun 
4221*4882a593Smuzhiyun 		hanger->pushed = 0;
4222*4882a593Smuzhiyun 		hanger->popped = 0;
4223*4882a593Smuzhiyun 		hanger->failed_slotfind = 0;
4224*4882a593Smuzhiyun 		hanger->failed_to_pop = 0;
4225*4882a593Smuzhiyun 		hanger->failed_to_push = 0;
4226*4882a593Smuzhiyun 	}
4227*4882a593Smuzhiyun 
4228*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4229*4882a593Smuzhiyun 
4230*4882a593Smuzhiyun 	return BCME_OK;
4231*4882a593Smuzhiyun }
4232*4882a593Smuzhiyun 
4233*4882a593Smuzhiyun /** returns TRUE if flow control is enabled */
dhd_wlfc_get_enable(dhd_pub_t * dhd,bool * val)4234*4882a593Smuzhiyun int dhd_wlfc_get_enable(dhd_pub_t *dhd, bool *val)
4235*4882a593Smuzhiyun {
4236*4882a593Smuzhiyun 	if (!dhd || !val) {
4237*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4238*4882a593Smuzhiyun 		return BCME_BADARG;
4239*4882a593Smuzhiyun 	}
4240*4882a593Smuzhiyun 
4241*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4242*4882a593Smuzhiyun 
4243*4882a593Smuzhiyun 	*val = dhd->wlfc_enabled;
4244*4882a593Smuzhiyun 
4245*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4246*4882a593Smuzhiyun 
4247*4882a593Smuzhiyun 	return BCME_OK;
4248*4882a593Smuzhiyun }
4249*4882a593Smuzhiyun 
4250*4882a593Smuzhiyun /** Called via an IOVAR */
dhd_wlfc_get_mode(dhd_pub_t * dhd,int * val)4251*4882a593Smuzhiyun int dhd_wlfc_get_mode(dhd_pub_t *dhd, int *val)
4252*4882a593Smuzhiyun {
4253*4882a593Smuzhiyun 	if (!dhd || !val) {
4254*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4255*4882a593Smuzhiyun 		return BCME_BADARG;
4256*4882a593Smuzhiyun 	}
4257*4882a593Smuzhiyun 
4258*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4259*4882a593Smuzhiyun 
4260*4882a593Smuzhiyun 	*val = dhd->wlfc_state ? dhd->proptxstatus_mode : 0;
4261*4882a593Smuzhiyun 
4262*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4263*4882a593Smuzhiyun 
4264*4882a593Smuzhiyun 	return BCME_OK;
4265*4882a593Smuzhiyun }
4266*4882a593Smuzhiyun 
4267*4882a593Smuzhiyun /** Called via an IOVAR */
dhd_wlfc_set_mode(dhd_pub_t * dhd,int val)4268*4882a593Smuzhiyun int dhd_wlfc_set_mode(dhd_pub_t *dhd, int val)
4269*4882a593Smuzhiyun {
4270*4882a593Smuzhiyun 	if (!dhd) {
4271*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4272*4882a593Smuzhiyun 		return BCME_BADARG;
4273*4882a593Smuzhiyun 	}
4274*4882a593Smuzhiyun 
4275*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4276*4882a593Smuzhiyun 
4277*4882a593Smuzhiyun 	if (dhd->wlfc_state) {
4278*4882a593Smuzhiyun 		dhd->proptxstatus_mode = val & 0xff;
4279*4882a593Smuzhiyun 	}
4280*4882a593Smuzhiyun 
4281*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4282*4882a593Smuzhiyun 
4283*4882a593Smuzhiyun 	return BCME_OK;
4284*4882a593Smuzhiyun }
4285*4882a593Smuzhiyun 
4286*4882a593Smuzhiyun /** Called when rx frame is received from the dongle */
dhd_wlfc_is_header_only_pkt(dhd_pub_t * dhd,void * pktbuf)4287*4882a593Smuzhiyun bool dhd_wlfc_is_header_only_pkt(dhd_pub_t * dhd, void *pktbuf)
4288*4882a593Smuzhiyun {
4289*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc;
4290*4882a593Smuzhiyun 	bool rc = FALSE;
4291*4882a593Smuzhiyun 
4292*4882a593Smuzhiyun 	if (dhd == NULL) {
4293*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4294*4882a593Smuzhiyun 		return FALSE;
4295*4882a593Smuzhiyun 	}
4296*4882a593Smuzhiyun 
4297*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4298*4882a593Smuzhiyun 
4299*4882a593Smuzhiyun 	if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
4300*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
4301*4882a593Smuzhiyun 		return FALSE;
4302*4882a593Smuzhiyun 	}
4303*4882a593Smuzhiyun 
4304*4882a593Smuzhiyun 	wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
4305*4882a593Smuzhiyun 
4306*4882a593Smuzhiyun 	if (PKTLEN(wlfc->osh, pktbuf) == 0) {
4307*4882a593Smuzhiyun 		wlfc->stats.wlfc_header_only_pkt++;
4308*4882a593Smuzhiyun 		rc = TRUE;
4309*4882a593Smuzhiyun 	}
4310*4882a593Smuzhiyun 
4311*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4312*4882a593Smuzhiyun 
4313*4882a593Smuzhiyun 	return rc;
4314*4882a593Smuzhiyun }
4315*4882a593Smuzhiyun 
dhd_wlfc_flowcontrol(dhd_pub_t * dhdp,bool state,bool bAcquireLock)4316*4882a593Smuzhiyun int dhd_wlfc_flowcontrol(dhd_pub_t *dhdp, bool state, bool bAcquireLock)
4317*4882a593Smuzhiyun {
4318*4882a593Smuzhiyun 	if (dhdp == NULL) {
4319*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4320*4882a593Smuzhiyun 		return BCME_BADARG;
4321*4882a593Smuzhiyun 	}
4322*4882a593Smuzhiyun 
4323*4882a593Smuzhiyun 	if (bAcquireLock) {
4324*4882a593Smuzhiyun 		dhd_os_wlfc_block(dhdp);
4325*4882a593Smuzhiyun 	}
4326*4882a593Smuzhiyun 
4327*4882a593Smuzhiyun 	if (!dhdp->wlfc_state || (dhdp->proptxstatus_mode == WLFC_FCMODE_NONE) ||
4328*4882a593Smuzhiyun 		dhdp->proptxstatus_module_ignore) {
4329*4882a593Smuzhiyun 		if (bAcquireLock) {
4330*4882a593Smuzhiyun 			dhd_os_wlfc_unblock(dhdp);
4331*4882a593Smuzhiyun 		}
4332*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
4333*4882a593Smuzhiyun 	}
4334*4882a593Smuzhiyun 
4335*4882a593Smuzhiyun 	if (state != dhdp->proptxstatus_txoff) {
4336*4882a593Smuzhiyun 		dhdp->proptxstatus_txoff = state;
4337*4882a593Smuzhiyun 	}
4338*4882a593Smuzhiyun 
4339*4882a593Smuzhiyun 	if (bAcquireLock) {
4340*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhdp);
4341*4882a593Smuzhiyun 	}
4342*4882a593Smuzhiyun 
4343*4882a593Smuzhiyun 	return BCME_OK;
4344*4882a593Smuzhiyun }
4345*4882a593Smuzhiyun 
4346*4882a593Smuzhiyun /** Called when eg an rx frame is received from the dongle */
dhd_wlfc_save_rxpath_ac_time(dhd_pub_t * dhd,uint8 prio)4347*4882a593Smuzhiyun int dhd_wlfc_save_rxpath_ac_time(dhd_pub_t * dhd, uint8 prio)
4348*4882a593Smuzhiyun {
4349*4882a593Smuzhiyun 	athost_wl_status_info_t* wlfc;
4350*4882a593Smuzhiyun 	int rx_path_ac = -1;
4351*4882a593Smuzhiyun 
4352*4882a593Smuzhiyun 	if ((dhd == NULL) || (prio >= NUMPRIO)) {
4353*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4354*4882a593Smuzhiyun 		return BCME_BADARG;
4355*4882a593Smuzhiyun 	}
4356*4882a593Smuzhiyun 
4357*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4358*4882a593Smuzhiyun 
4359*4882a593Smuzhiyun 	if (!dhd->wlfc_rxpkt_chk) {
4360*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
4361*4882a593Smuzhiyun 		return BCME_OK;
4362*4882a593Smuzhiyun 	}
4363*4882a593Smuzhiyun 
4364*4882a593Smuzhiyun 	if (!dhd->wlfc_state || (dhd->proptxstatus_mode == WLFC_FCMODE_NONE)) {
4365*4882a593Smuzhiyun 		dhd_os_wlfc_unblock(dhd);
4366*4882a593Smuzhiyun 		return WLFC_UNSUPPORTED;
4367*4882a593Smuzhiyun 	}
4368*4882a593Smuzhiyun 
4369*4882a593Smuzhiyun 	wlfc = (athost_wl_status_info_t*)dhd->wlfc_state;
4370*4882a593Smuzhiyun 
4371*4882a593Smuzhiyun 	rx_path_ac = prio2fifo[prio];
4372*4882a593Smuzhiyun 	wlfc->rx_timestamp[rx_path_ac] = OSL_SYSUPTIME();
4373*4882a593Smuzhiyun 
4374*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4375*4882a593Smuzhiyun 
4376*4882a593Smuzhiyun 	return BCME_OK;
4377*4882a593Smuzhiyun }
4378*4882a593Smuzhiyun 
4379*4882a593Smuzhiyun /** called via an IOVAR */
dhd_wlfc_get_module_ignore(dhd_pub_t * dhd,int * val)4380*4882a593Smuzhiyun int dhd_wlfc_get_module_ignore(dhd_pub_t *dhd, int *val)
4381*4882a593Smuzhiyun {
4382*4882a593Smuzhiyun 	if (!dhd || !val) {
4383*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4384*4882a593Smuzhiyun 		return BCME_BADARG;
4385*4882a593Smuzhiyun 	}
4386*4882a593Smuzhiyun 
4387*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4388*4882a593Smuzhiyun 
4389*4882a593Smuzhiyun 	*val = dhd->proptxstatus_module_ignore;
4390*4882a593Smuzhiyun 
4391*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4392*4882a593Smuzhiyun 
4393*4882a593Smuzhiyun 	return BCME_OK;
4394*4882a593Smuzhiyun }
4395*4882a593Smuzhiyun 
4396*4882a593Smuzhiyun /** called via an IOVAR */
dhd_wlfc_set_module_ignore(dhd_pub_t * dhd,int val)4397*4882a593Smuzhiyun int dhd_wlfc_set_module_ignore(dhd_pub_t *dhd, int val)
4398*4882a593Smuzhiyun {
4399*4882a593Smuzhiyun 	uint32 tlv = 0;
4400*4882a593Smuzhiyun 	bool bChanged = FALSE;
4401*4882a593Smuzhiyun 
4402*4882a593Smuzhiyun 	if (!dhd) {
4403*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4404*4882a593Smuzhiyun 		return BCME_BADARG;
4405*4882a593Smuzhiyun 	}
4406*4882a593Smuzhiyun 
4407*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4408*4882a593Smuzhiyun 
4409*4882a593Smuzhiyun 	if ((bool)val != dhd->proptxstatus_module_ignore) {
4410*4882a593Smuzhiyun 		dhd->proptxstatus_module_ignore = (val != 0);
4411*4882a593Smuzhiyun 		/* force txstatus_ignore sync with proptxstatus_module_ignore */
4412*4882a593Smuzhiyun 		dhd->proptxstatus_txstatus_ignore = dhd->proptxstatus_module_ignore;
4413*4882a593Smuzhiyun 		if (FALSE == dhd->proptxstatus_module_ignore) {
4414*4882a593Smuzhiyun 			tlv = WLFC_FLAGS_RSSI_SIGNALS |
4415*4882a593Smuzhiyun 				WLFC_FLAGS_XONXOFF_SIGNALS |
4416*4882a593Smuzhiyun 				WLFC_FLAGS_CREDIT_STATUS_SIGNALS |
4417*4882a593Smuzhiyun 				WLFC_FLAGS_HOST_PROPTXSTATUS_ACTIVE;
4418*4882a593Smuzhiyun 		}
4419*4882a593Smuzhiyun 		/* always enable host reorder */
4420*4882a593Smuzhiyun 		tlv |= WLFC_FLAGS_HOST_RXRERODER_ACTIVE;
4421*4882a593Smuzhiyun 		bChanged = TRUE;
4422*4882a593Smuzhiyun 	}
4423*4882a593Smuzhiyun 
4424*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4425*4882a593Smuzhiyun 
4426*4882a593Smuzhiyun 	if (bChanged) {
4427*4882a593Smuzhiyun 		/* select enable proptxtstatus signaling */
4428*4882a593Smuzhiyun 		if (dhd_wl_ioctl_set_intiovar(dhd, "tlv", tlv, WLC_SET_VAR, TRUE, 0)) {
4429*4882a593Smuzhiyun 			DHD_ERROR(("%s: failed to set bdcv2 tlv signaling to 0x%x\n",
4430*4882a593Smuzhiyun 				__FUNCTION__, tlv));
4431*4882a593Smuzhiyun 		} else {
4432*4882a593Smuzhiyun 			DHD_ERROR(("%s: successfully set bdcv2 tlv signaling to 0x%x\n",
4433*4882a593Smuzhiyun 				__FUNCTION__, tlv));
4434*4882a593Smuzhiyun 		}
4435*4882a593Smuzhiyun 	}
4436*4882a593Smuzhiyun 
4437*4882a593Smuzhiyun #if defined(DHD_WLFC_THREAD)
4438*4882a593Smuzhiyun 	_dhd_wlfc_thread_wakeup(dhd);
4439*4882a593Smuzhiyun #endif /* defined(DHD_WLFC_THREAD) */
4440*4882a593Smuzhiyun 
4441*4882a593Smuzhiyun 	return BCME_OK;
4442*4882a593Smuzhiyun }
4443*4882a593Smuzhiyun 
4444*4882a593Smuzhiyun /** called via an IOVAR */
dhd_wlfc_get_credit_ignore(dhd_pub_t * dhd,int * val)4445*4882a593Smuzhiyun int dhd_wlfc_get_credit_ignore(dhd_pub_t *dhd, int *val)
4446*4882a593Smuzhiyun {
4447*4882a593Smuzhiyun 	if (!dhd || !val) {
4448*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4449*4882a593Smuzhiyun 		return BCME_BADARG;
4450*4882a593Smuzhiyun 	}
4451*4882a593Smuzhiyun 
4452*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4453*4882a593Smuzhiyun 
4454*4882a593Smuzhiyun 	*val = dhd->proptxstatus_credit_ignore;
4455*4882a593Smuzhiyun 
4456*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4457*4882a593Smuzhiyun 
4458*4882a593Smuzhiyun 	return BCME_OK;
4459*4882a593Smuzhiyun }
4460*4882a593Smuzhiyun 
4461*4882a593Smuzhiyun /** called via an IOVAR */
dhd_wlfc_set_credit_ignore(dhd_pub_t * dhd,int val)4462*4882a593Smuzhiyun int dhd_wlfc_set_credit_ignore(dhd_pub_t *dhd, int val)
4463*4882a593Smuzhiyun {
4464*4882a593Smuzhiyun 	if (!dhd) {
4465*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4466*4882a593Smuzhiyun 		return BCME_BADARG;
4467*4882a593Smuzhiyun 	}
4468*4882a593Smuzhiyun 
4469*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4470*4882a593Smuzhiyun 
4471*4882a593Smuzhiyun 	dhd->proptxstatus_credit_ignore = (val != 0);
4472*4882a593Smuzhiyun 
4473*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4474*4882a593Smuzhiyun 
4475*4882a593Smuzhiyun 	return BCME_OK;
4476*4882a593Smuzhiyun }
4477*4882a593Smuzhiyun 
4478*4882a593Smuzhiyun /** called via an IOVAR */
dhd_wlfc_get_txstatus_ignore(dhd_pub_t * dhd,int * val)4479*4882a593Smuzhiyun int dhd_wlfc_get_txstatus_ignore(dhd_pub_t *dhd, int *val)
4480*4882a593Smuzhiyun {
4481*4882a593Smuzhiyun 	if (!dhd || !val) {
4482*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4483*4882a593Smuzhiyun 		return BCME_BADARG;
4484*4882a593Smuzhiyun 	}
4485*4882a593Smuzhiyun 
4486*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4487*4882a593Smuzhiyun 
4488*4882a593Smuzhiyun 	*val = dhd->proptxstatus_txstatus_ignore;
4489*4882a593Smuzhiyun 
4490*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4491*4882a593Smuzhiyun 
4492*4882a593Smuzhiyun 	return BCME_OK;
4493*4882a593Smuzhiyun }
4494*4882a593Smuzhiyun 
4495*4882a593Smuzhiyun /** called via an IOVAR */
dhd_wlfc_set_txstatus_ignore(dhd_pub_t * dhd,int val)4496*4882a593Smuzhiyun int dhd_wlfc_set_txstatus_ignore(dhd_pub_t *dhd, int val)
4497*4882a593Smuzhiyun {
4498*4882a593Smuzhiyun 	if (!dhd) {
4499*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4500*4882a593Smuzhiyun 		return BCME_BADARG;
4501*4882a593Smuzhiyun 	}
4502*4882a593Smuzhiyun 
4503*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4504*4882a593Smuzhiyun 
4505*4882a593Smuzhiyun 	dhd->proptxstatus_txstatus_ignore = (val != 0);
4506*4882a593Smuzhiyun 
4507*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4508*4882a593Smuzhiyun 
4509*4882a593Smuzhiyun 	return BCME_OK;
4510*4882a593Smuzhiyun }
4511*4882a593Smuzhiyun 
4512*4882a593Smuzhiyun /** called via an IOVAR */
dhd_wlfc_get_rxpkt_chk(dhd_pub_t * dhd,int * val)4513*4882a593Smuzhiyun int dhd_wlfc_get_rxpkt_chk(dhd_pub_t *dhd, int *val)
4514*4882a593Smuzhiyun {
4515*4882a593Smuzhiyun 	if (!dhd || !val) {
4516*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4517*4882a593Smuzhiyun 		return BCME_BADARG;
4518*4882a593Smuzhiyun 	}
4519*4882a593Smuzhiyun 
4520*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4521*4882a593Smuzhiyun 
4522*4882a593Smuzhiyun 	*val = dhd->wlfc_rxpkt_chk;
4523*4882a593Smuzhiyun 
4524*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4525*4882a593Smuzhiyun 
4526*4882a593Smuzhiyun 	return BCME_OK;
4527*4882a593Smuzhiyun }
4528*4882a593Smuzhiyun 
4529*4882a593Smuzhiyun /** called via an IOVAR */
dhd_wlfc_set_rxpkt_chk(dhd_pub_t * dhd,int val)4530*4882a593Smuzhiyun int dhd_wlfc_set_rxpkt_chk(dhd_pub_t *dhd, int val)
4531*4882a593Smuzhiyun {
4532*4882a593Smuzhiyun 	if (!dhd) {
4533*4882a593Smuzhiyun 		DHD_ERROR(("Error: %s():%d\n", __FUNCTION__, __LINE__));
4534*4882a593Smuzhiyun 		return BCME_BADARG;
4535*4882a593Smuzhiyun 	}
4536*4882a593Smuzhiyun 
4537*4882a593Smuzhiyun 	dhd_os_wlfc_block(dhd);
4538*4882a593Smuzhiyun 
4539*4882a593Smuzhiyun 	dhd->wlfc_rxpkt_chk = (val != 0);
4540*4882a593Smuzhiyun 
4541*4882a593Smuzhiyun 	dhd_os_wlfc_unblock(dhd);
4542*4882a593Smuzhiyun 
4543*4882a593Smuzhiyun 	return BCME_OK;
4544*4882a593Smuzhiyun }
4545*4882a593Smuzhiyun 
4546*4882a593Smuzhiyun #endif /* PROP_TXSTATUS */
4547