1/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16#include <linux/types.h>
17#include <linux/module.h>
18#include <linux/if_ether.h>
19#include <linux/spinlock.h>
20#include <linux/skbuff.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/err.h>
24#include <linux/jiffies.h>
25#include <net/cfg80211.h>
26
27#include <brcmu_utils.h>
28#include <brcmu_wifi.h>
29#include "core.h"
30#include "debug.h"
31#include "bus.h"
32#include "fwil.h"
33#include "fwil_types.h"
34#include "fweh.h"
35#include "fwsignal.h"
36#include "p2p.h"
37#include "cfg80211.h"
38#include "proto.h"
39
40/**
41 * DOC: Firmware Signalling
42 *
43 * Firmware can send signals to host and vice versa, which are passed in the
44 * data packets using TLV based header. This signalling layer is on top of the
45 * BDC bus protocol layer.
46 */
47
48/*
49 * single definition for firmware-driver flow control tlv's.
50 *
51 * each tlv is specified by BRCMF_FWS_TLV_DEF(name, ID, length).
52 * A length value 0 indicates variable length tlv.
53 */
54#define BRCMF_FWS_TLV_DEFLIST \
55	BRCMF_FWS_TLV_DEF(MAC_OPEN, 1, 1) \
56	BRCMF_FWS_TLV_DEF(MAC_CLOSE, 2, 1) \
57	BRCMF_FWS_TLV_DEF(MAC_REQUEST_CREDIT, 3, 2) \
58	BRCMF_FWS_TLV_DEF(TXSTATUS, 4, 4) \
59	BRCMF_FWS_TLV_DEF(PKTTAG, 5, 4) \
60	BRCMF_FWS_TLV_DEF(MACDESC_ADD,	6, 8) \
61	BRCMF_FWS_TLV_DEF(MACDESC_DEL, 7, 8) \
62	BRCMF_FWS_TLV_DEF(RSSI, 8, 1) \
63	BRCMF_FWS_TLV_DEF(INTERFACE_OPEN, 9, 1) \
64	BRCMF_FWS_TLV_DEF(INTERFACE_CLOSE, 10, 1) \
65	BRCMF_FWS_TLV_DEF(FIFO_CREDITBACK, 11, 6) \
66	BRCMF_FWS_TLV_DEF(PENDING_TRAFFIC_BMP, 12, 2) \
67	BRCMF_FWS_TLV_DEF(MAC_REQUEST_PACKET, 13, 3) \
68	BRCMF_FWS_TLV_DEF(HOST_REORDER_RXPKTS, 14, 10) \
69	BRCMF_FWS_TLV_DEF(TRANS_ID, 18, 6) \
70	BRCMF_FWS_TLV_DEF(COMP_TXSTATUS, 19, 1) \
71	BRCMF_FWS_TLV_DEF(FILLER, 255, 0)
72
73/*
74 * enum brcmf_fws_tlv_type - definition of tlv identifiers.
75 */
76#define BRCMF_FWS_TLV_DEF(name, id, len) \
77	BRCMF_FWS_TYPE_ ## name =  id,
78enum brcmf_fws_tlv_type {
79	BRCMF_FWS_TLV_DEFLIST
80	BRCMF_FWS_TYPE_INVALID
81};
82#undef BRCMF_FWS_TLV_DEF
83
84/*
85 * enum brcmf_fws_tlv_len - definition of tlv lengths.
86 */
87#define BRCMF_FWS_TLV_DEF(name, id, len) \
88	BRCMF_FWS_TYPE_ ## name ## _LEN = (len),
89enum brcmf_fws_tlv_len {
90	BRCMF_FWS_TLV_DEFLIST
91};
92#undef BRCMF_FWS_TLV_DEF
93
94#ifdef DEBUG
95/*
96 * brcmf_fws_tlv_names - array of tlv names.
97 */
98#define BRCMF_FWS_TLV_DEF(name, id, len) \
99	{ id, #name },
100static struct {
101	enum brcmf_fws_tlv_type id;
102	const char *name;
103} brcmf_fws_tlv_names[] = {
104	BRCMF_FWS_TLV_DEFLIST
105};
106#undef BRCMF_FWS_TLV_DEF
107
108
109static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
110{
111	int i;
112
113	for (i = 0; i < ARRAY_SIZE(brcmf_fws_tlv_names); i++)
114		if (brcmf_fws_tlv_names[i].id == id)
115			return brcmf_fws_tlv_names[i].name;
116
117	return "INVALID";
118}
119#else
120static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
121{
122	return "NODEBUG";
123}
124#endif /* DEBUG */
125
126/*
127 * The PKTTAG tlv has additional bytes when firmware-signalling
128 * mode has REUSESEQ flag set.
129 */
130#define BRCMF_FWS_TYPE_SEQ_LEN				2
131
132/*
133 * flags used to enable tlv signalling from firmware.
134 */
135#define BRCMF_FWS_FLAGS_RSSI_SIGNALS			0x0001
136#define BRCMF_FWS_FLAGS_XONXOFF_SIGNALS			0x0002
137#define BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS		0x0004
138#define BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE	0x0008
139#define BRCMF_FWS_FLAGS_PSQ_GENERATIONFSM_ENABLE	0x0010
140#define BRCMF_FWS_FLAGS_PSQ_ZERO_BUFFER_ENABLE		0x0020
141#define BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE		0x0040
142
143#define BRCMF_FWS_MAC_DESC_TABLE_SIZE			32
144#define BRCMF_FWS_MAC_DESC_ID_INVALID			0xff
145
146#define BRCMF_FWS_HOSTIF_FLOWSTATE_OFF			0
147#define BRCMF_FWS_HOSTIF_FLOWSTATE_ON			1
148#define BRCMF_FWS_FLOWCONTROL_HIWATER			128
149#define BRCMF_FWS_FLOWCONTROL_LOWATER			64
150
151#define BRCMF_FWS_PSQ_PREC_COUNT		((BRCMF_FWS_FIFO_COUNT + 1) * 2)
152#define BRCMF_FWS_PSQ_LEN				256
153
154#define BRCMF_FWS_HTOD_FLAG_PKTFROMHOST			0x01
155#define BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED		0x02
156
157#define BRCMF_FWS_RET_OK_NOSCHEDULE			0
158#define BRCMF_FWS_RET_OK_SCHEDULE			1
159
160#define BRCMF_FWS_MODE_REUSESEQ_SHIFT			3	/* seq reuse */
161#define BRCMF_FWS_MODE_SET_REUSESEQ(x, val)	((x) = \
162		((x) & ~(1 << BRCMF_FWS_MODE_REUSESEQ_SHIFT)) | \
163		(((val) & 1) << BRCMF_FWS_MODE_REUSESEQ_SHIFT))
164#define BRCMF_FWS_MODE_GET_REUSESEQ(x)	\
165		(((x) >> BRCMF_FWS_MODE_REUSESEQ_SHIFT) & 1)
166
167/**
168 * enum brcmf_fws_skb_state - indicates processing state of skb.
169 *
170 * @BRCMF_FWS_SKBSTATE_NEW: sk_buff is newly arrived in the driver.
171 * @BRCMF_FWS_SKBSTATE_DELAYED: sk_buff had to wait on queue.
172 * @BRCMF_FWS_SKBSTATE_SUPPRESSED: sk_buff has been suppressed by firmware.
173 * @BRCMF_FWS_SKBSTATE_TIM: allocated for TIM update info.
174 */
175enum brcmf_fws_skb_state {
176	BRCMF_FWS_SKBSTATE_NEW,
177	BRCMF_FWS_SKBSTATE_DELAYED,
178	BRCMF_FWS_SKBSTATE_SUPPRESSED,
179	BRCMF_FWS_SKBSTATE_TIM
180};
181
182/**
183 * struct brcmf_skbuff_cb - control buffer associated with skbuff.
184 *
185 * @bus_flags: 2 bytes reserved for bus specific parameters
186 * @if_flags: holds interface index and packet related flags.
187 * @htod: host to device packet identifier (used in PKTTAG tlv).
188 * @htod_seq: this 16-bit is original seq number for every suppress packet.
189 * @state: transmit state of the packet.
190 * @mac: descriptor related to destination for this packet.
191 *
192 * This information is stored in control buffer struct sk_buff::cb, which
193 * provides 48 bytes of storage so this structure should not exceed that.
194 */
195struct brcmf_skbuff_cb {
196	u16 bus_flags;
197	u16 if_flags;
198	u32 htod;
199	u16 htod_seq;
200	enum brcmf_fws_skb_state state;
201	struct brcmf_fws_mac_descriptor *mac;
202};
203
204/*
205 * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
206 */
207#define brcmf_skbcb(skb)	((struct brcmf_skbuff_cb *)((skb)->cb))
208
209/*
210 * sk_buff control if flags
211 *
212 *	b[11]  - packet sent upon firmware request.
213 *	b[10]  - packet only contains signalling data.
214 *	b[9]   - packet is a tx packet.
215 *	b[8]   - packet used requested credit
216 *	b[7]   - interface in AP mode.
217 *	b[3:0] - interface index.
218 */
219#define BRCMF_SKB_IF_FLAGS_REQUESTED_MASK	0x0800
220#define BRCMF_SKB_IF_FLAGS_REQUESTED_SHIFT	11
221#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_MASK	0x0400
222#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_SHIFT	10
223#define BRCMF_SKB_IF_FLAGS_TRANSMIT_MASK        0x0200
224#define BRCMF_SKB_IF_FLAGS_TRANSMIT_SHIFT	9
225#define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_MASK	0x0100
226#define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_SHIFT	8
227#define BRCMF_SKB_IF_FLAGS_IF_AP_MASK		0x0080
228#define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT		7
229#define BRCMF_SKB_IF_FLAGS_INDEX_MASK		0x000f
230#define BRCMF_SKB_IF_FLAGS_INDEX_SHIFT		0
231
232#define brcmf_skb_if_flags_set_field(skb, field, value) \
233	brcmu_maskset16(&(brcmf_skbcb(skb)->if_flags), \
234			BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
235			BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT, (value))
236#define brcmf_skb_if_flags_get_field(skb, field) \
237	brcmu_maskget16(brcmf_skbcb(skb)->if_flags, \
238			BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
239			BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT)
240
241/*
242 * sk_buff control packet identifier
243 *
244 * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
245 *
246 * - Generated at the host (e.g. dhd)
247 * - Seen as a generic sequence number by firmware except for the flags field.
248 *
249 * Generation	: b[31]	=> generation number for this packet [host->fw]
250 *			   OR, current generation number [fw->host]
251 * Flags	: b[30:27] => command, status flags
252 * FIFO-AC	: b[26:24] => AC-FIFO id
253 * h-slot	: b[23:8] => hanger-slot
254 * freerun	: b[7:0] => A free running counter
255 */
256#define BRCMF_SKB_HTOD_TAG_GENERATION_MASK		0x80000000
257#define BRCMF_SKB_HTOD_TAG_GENERATION_SHIFT		31
258#define BRCMF_SKB_HTOD_TAG_FLAGS_MASK			0x78000000
259#define BRCMF_SKB_HTOD_TAG_FLAGS_SHIFT			27
260#define BRCMF_SKB_HTOD_TAG_FIFO_MASK			0x07000000
261#define BRCMF_SKB_HTOD_TAG_FIFO_SHIFT			24
262#define BRCMF_SKB_HTOD_TAG_HSLOT_MASK			0x00ffff00
263#define BRCMF_SKB_HTOD_TAG_HSLOT_SHIFT			8
264#define BRCMF_SKB_HTOD_TAG_FREERUN_MASK			0x000000ff
265#define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT		0
266
267#define brcmf_skb_htod_tag_set_field(skb, field, value) \
268	brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
269			BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
270			BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
271#define brcmf_skb_htod_tag_get_field(skb, field) \
272	brcmu_maskget32(brcmf_skbcb(skb)->htod, \
273			BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
274			BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)
275
276#define BRCMF_SKB_HTOD_SEQ_FROMFW_MASK			0x2000
277#define BRCMF_SKB_HTOD_SEQ_FROMFW_SHIFT			13
278#define BRCMF_SKB_HTOD_SEQ_FROMDRV_MASK			0x1000
279#define BRCMF_SKB_HTOD_SEQ_FROMDRV_SHIFT		12
280#define BRCMF_SKB_HTOD_SEQ_NR_MASK			0x0fff
281#define BRCMF_SKB_HTOD_SEQ_NR_SHIFT			0
282
283#define brcmf_skb_htod_seq_set_field(skb, field, value) \
284	brcmu_maskset16(&(brcmf_skbcb(skb)->htod_seq), \
285			BRCMF_SKB_HTOD_SEQ_ ## field ## _MASK, \
286			BRCMF_SKB_HTOD_SEQ_ ## field ## _SHIFT, (value))
287#define brcmf_skb_htod_seq_get_field(skb, field) \
288	brcmu_maskget16(brcmf_skbcb(skb)->htod_seq, \
289			BRCMF_SKB_HTOD_SEQ_ ## field ## _MASK, \
290			BRCMF_SKB_HTOD_SEQ_ ## field ## _SHIFT)
291
292#define BRCMF_FWS_TXSTAT_GENERATION_MASK	0x80000000
293#define BRCMF_FWS_TXSTAT_GENERATION_SHIFT	31
294#define BRCMF_FWS_TXSTAT_FLAGS_MASK		0x78000000
295#define BRCMF_FWS_TXSTAT_FLAGS_SHIFT		27
296#define BRCMF_FWS_TXSTAT_FIFO_MASK		0x07000000
297#define BRCMF_FWS_TXSTAT_FIFO_SHIFT		24
298#define BRCMF_FWS_TXSTAT_HSLOT_MASK		0x00FFFF00
299#define BRCMF_FWS_TXSTAT_HSLOT_SHIFT		8
300#define BRCMF_FWS_TXSTAT_FREERUN_MASK		0x000000FF
301#define BRCMF_FWS_TXSTAT_FREERUN_SHIFT		0
302
303#define brcmf_txstatus_get_field(txs, field) \
304	brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
305			BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)
306
307/* How long to defer borrowing in jiffies */
308#define BRCMF_FWS_BORROW_DEFER_PERIOD		(HZ / 10)
309
310/**
311 * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
312 *
313 * @BRCMF_FWS_FIFO_FIRST: first fifo, ie. background.
314 * @BRCMF_FWS_FIFO_AC_BK: fifo for background traffic.
315 * @BRCMF_FWS_FIFO_AC_BE: fifo for best-effort traffic.
316 * @BRCMF_FWS_FIFO_AC_VI: fifo for video traffic.
317 * @BRCMF_FWS_FIFO_AC_VO: fifo for voice traffic.
318 * @BRCMF_FWS_FIFO_BCMC: fifo for broadcast/multicast (AP only).
319 * @BRCMF_FWS_FIFO_ATIM: fifo for ATIM (AP only).
320 * @BRCMF_FWS_FIFO_COUNT: number of fifos.
321 */
322enum brcmf_fws_fifo {
323	BRCMF_FWS_FIFO_FIRST,
324	BRCMF_FWS_FIFO_AC_BK = BRCMF_FWS_FIFO_FIRST,
325	BRCMF_FWS_FIFO_AC_BE,
326	BRCMF_FWS_FIFO_AC_VI,
327	BRCMF_FWS_FIFO_AC_VO,
328	BRCMF_FWS_FIFO_BCMC,
329	BRCMF_FWS_FIFO_ATIM,
330	BRCMF_FWS_FIFO_COUNT
331};
332
333/**
334 * enum brcmf_fws_txstatus - txstatus flag values.
335 *
336 * @BRCMF_FWS_TXSTATUS_DISCARD:
337 *	host is free to discard the packet.
338 * @BRCMF_FWS_TXSTATUS_CORE_SUPPRESS:
339 *	802.11 core suppressed the packet.
340 * @BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS:
341 *	firmware suppress the packet as device is already in PS mode.
342 * @BRCMF_FWS_TXSTATUS_FW_TOSSED:
343 *	firmware tossed the packet.
344 * @BRCMF_FWS_TXSTATUS_HOST_TOSSED:
345 *	host tossed the packet.
346 */
347enum brcmf_fws_txstatus {
348	BRCMF_FWS_TXSTATUS_DISCARD,
349	BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
350	BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
351	BRCMF_FWS_TXSTATUS_FW_TOSSED,
352	BRCMF_FWS_TXSTATUS_HOST_TOSSED
353};
354
355enum brcmf_fws_fcmode {
356	BRCMF_FWS_FCMODE_NONE,
357	BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
358	BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
359};
360
361enum brcmf_fws_mac_desc_state {
362	BRCMF_FWS_STATE_OPEN = 1,
363	BRCMF_FWS_STATE_CLOSE
364};
365
366/**
367 * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
368 *
369 * @occupied: slot is in use.
370 * @mac_handle: handle for mac entry determined by firmware.
371 * @interface_id: interface index.
372 * @state: current state.
373 * @suppressed: mac entry is suppressed.
374 * @generation: generation bit.
375 * @ac_bitmap: ac queue bitmap.
376 * @requested_credit: credits requested by firmware.
377 * @ea: ethernet address.
378 * @seq: per-node free-running sequence.
379 * @psq: power-save queue.
380 * @transit_count: packet in transit to firmware.
381 */
382struct brcmf_fws_mac_descriptor {
383	char name[16];
384	u8 occupied;
385	u8 mac_handle;
386	u8 interface_id;
387	u8 state;
388	bool suppressed;
389	u8 generation;
390	u8 ac_bitmap;
391	u8 requested_credit;
392	u8 requested_packet;
393	u8 ea[ETH_ALEN];
394	u8 seq[BRCMF_FWS_FIFO_COUNT];
395	struct pktq psq;
396	int transit_count;
397	int suppr_transit_count;
398	bool send_tim_signal;
399	u8 traffic_pending_bmp;
400	u8 traffic_lastreported_bmp;
401};
402
403#define BRCMF_FWS_HANGER_MAXITEMS	1024
404
405/**
406 * enum brcmf_fws_hanger_item_state - state of hanger item.
407 *
408 * @BRCMF_FWS_HANGER_ITEM_STATE_FREE: item is free for use.
409 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE: item is in use.
410 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED: item was suppressed.
411 */
412enum brcmf_fws_hanger_item_state {
413	BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
414	BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
415	BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
416};
417
418
419/**
420 * struct brcmf_fws_hanger_item - single entry for tx pending packet.
421 *
422 * @state: entry is either free or occupied.
423 * @pkt: packet itself.
424 */
425struct brcmf_fws_hanger_item {
426	enum brcmf_fws_hanger_item_state state;
427	struct sk_buff *pkt;
428};
429
430/**
431 * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
432 *
433 * @pushed: packets pushed to await txstatus.
434 * @popped: packets popped upon handling txstatus.
435 * @failed_to_push: packets that could not be pushed.
436 * @failed_to_pop: packets that could not be popped.
437 * @failed_slotfind: packets for which failed to find an entry.
438 * @slot_pos: last returned item index for a free entry.
439 * @items: array of hanger items.
440 */
441struct brcmf_fws_hanger {
442	u32 pushed;
443	u32 popped;
444	u32 failed_to_push;
445	u32 failed_to_pop;
446	u32 failed_slotfind;
447	u32 slot_pos;
448	struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
449};
450
451struct brcmf_fws_macdesc_table {
452	struct brcmf_fws_mac_descriptor nodes[BRCMF_FWS_MAC_DESC_TABLE_SIZE];
453	struct brcmf_fws_mac_descriptor iface[BRCMF_MAX_IFS];
454	struct brcmf_fws_mac_descriptor other;
455};
456
457struct brcmf_fws_stats {
458	u32 tlv_parse_failed;
459	u32 tlv_invalid_type;
460	u32 header_only_pkt;
461	u32 header_pulls;
462	u32 pkt2bus;
463	u32 send_pkts[5];
464	u32 requested_sent[5];
465	u32 generic_error;
466	u32 mac_update_failed;
467	u32 mac_ps_update_failed;
468	u32 if_update_failed;
469	u32 packet_request_failed;
470	u32 credit_request_failed;
471	u32 rollback_success;
472	u32 rollback_failed;
473	u32 delayq_full_error;
474	u32 supprq_full_error;
475	u32 txs_indicate;
476	u32 txs_discard;
477	u32 txs_supp_core;
478	u32 txs_supp_ps;
479	u32 txs_tossed;
480	u32 txs_host_tossed;
481	u32 bus_flow_block;
482	u32 fws_flow_block;
483};
484
485struct brcmf_fws_info {
486	struct brcmf_pub *drvr;
487	spinlock_t spinlock;
488	ulong flags;
489	struct brcmf_fws_stats stats;
490	struct brcmf_fws_hanger hanger;
491	enum brcmf_fws_fcmode fcmode;
492	bool fw_signals;
493	bool bcmc_credit_check;
494	struct brcmf_fws_macdesc_table desc;
495	struct workqueue_struct *fws_wq;
496	struct work_struct fws_dequeue_work;
497	u32 fifo_enqpkt[BRCMF_FWS_FIFO_COUNT];
498	int fifo_credit[BRCMF_FWS_FIFO_COUNT];
499	int credits_borrowed[BRCMF_FWS_FIFO_AC_VO + 1];
500	int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
501	u32 fifo_credit_map;
502	u32 fifo_delay_map;
503	unsigned long borrow_defer_timestamp;
504	bool bus_flow_blocked;
505	bool creditmap_received;
506	u8 mode;
507	bool avoid_queueing;
508};
509
510/*
511 * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
512 */
513static const int brcmf_fws_prio2fifo[] = {
514	BRCMF_FWS_FIFO_AC_BE,
515	BRCMF_FWS_FIFO_AC_BK,
516	BRCMF_FWS_FIFO_AC_BK,
517	BRCMF_FWS_FIFO_AC_BE,
518	BRCMF_FWS_FIFO_AC_VI,
519	BRCMF_FWS_FIFO_AC_VI,
520	BRCMF_FWS_FIFO_AC_VO,
521	BRCMF_FWS_FIFO_AC_VO
522};
523
524static int fcmode;
525module_param(fcmode, int, S_IRUSR);
526MODULE_PARM_DESC(fcmode, "mode of firmware signalled flow control");
527
528#define BRCMF_FWS_TLV_DEF(name, id, len) \
529	case BRCMF_FWS_TYPE_ ## name: \
530		return len;
531
532/**
533 * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
534 *
535 * @fws: firmware-signalling information.
536 * @id: identifier of the TLV.
537 *
538 * Return: the specified length for the given TLV; Otherwise -EINVAL.
539 */
540static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
541				 enum brcmf_fws_tlv_type id)
542{
543	switch (id) {
544	BRCMF_FWS_TLV_DEFLIST
545	default:
546		fws->stats.tlv_invalid_type++;
547		break;
548	}
549	return -EINVAL;
550}
551#undef BRCMF_FWS_TLV_DEF
552
553static void brcmf_fws_lock(struct brcmf_fws_info *fws)
554		__acquires(&fws->spinlock)
555{
556	spin_lock_irqsave(&fws->spinlock, fws->flags);
557}
558
559static void brcmf_fws_unlock(struct brcmf_fws_info *fws)
560		__releases(&fws->spinlock)
561{
562	spin_unlock_irqrestore(&fws->spinlock, fws->flags);
563}
564
565static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
566{
567	u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
568	return ifidx == *(int *)arg;
569}
570
571static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
572				int ifidx)
573{
574	bool (*matchfn)(struct sk_buff *, void *) = NULL;
575	struct sk_buff *skb;
576	int prec;
577
578	if (ifidx != -1)
579		matchfn = brcmf_fws_ifidx_match;
580	for (prec = 0; prec < q->num_prec; prec++) {
581		skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
582		while (skb) {
583			brcmu_pkt_buf_free_skb(skb);
584			skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
585		}
586	}
587}
588
589static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
590{
591	int i;
592
593	memset(hanger, 0, sizeof(*hanger));
594	for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
595		hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
596}
597
598static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
599{
600	u32 i;
601
602	i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
603
604	while (i != h->slot_pos) {
605		if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
606			h->slot_pos = i;
607			goto done;
608		}
609		i++;
610		if (i == BRCMF_FWS_HANGER_MAXITEMS)
611			i = 0;
612	}
613	brcmf_err("all slots occupied\n");
614	h->failed_slotfind++;
615	i = BRCMF_FWS_HANGER_MAXITEMS;
616done:
617	return i;
618}
619
620static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
621				    struct sk_buff *pkt, u32 slot_id)
622{
623	if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
624		return -ENOENT;
625
626	if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
627		brcmf_err("slot is not free\n");
628		h->failed_to_push++;
629		return -EINVAL;
630	}
631
632	h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
633	h->items[slot_id].pkt = pkt;
634	h->pushed++;
635	return 0;
636}
637
638static int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
639					  u32 slot_id, struct sk_buff **pktout,
640					  bool remove_item)
641{
642	if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
643		return -ENOENT;
644
645	if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
646		brcmf_err("entry not in use\n");
647		h->failed_to_pop++;
648		return -EINVAL;
649	}
650
651	*pktout = h->items[slot_id].pkt;
652	if (remove_item) {
653		h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
654		h->items[slot_id].pkt = NULL;
655		h->popped++;
656	}
657	return 0;
658}
659
660static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
661					    u32 slot_id)
662{
663	if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
664		return -ENOENT;
665
666	if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
667		brcmf_err("entry not in use\n");
668		return -EINVAL;
669	}
670
671	h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
672	return 0;
673}
674
675static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
676				     bool (*fn)(struct sk_buff *, void *),
677				     int ifidx)
678{
679	struct brcmf_fws_hanger *h = &fws->hanger;
680	struct sk_buff *skb;
681	int i;
682	enum brcmf_fws_hanger_item_state s;
683
684	for (i = 0; i < ARRAY_SIZE(h->items); i++) {
685		s = h->items[i].state;
686		if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE ||
687		    s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
688			skb = h->items[i].pkt;
689			if (fn == NULL || fn(skb, &ifidx)) {
690				/* suppress packets freed from psq */
691				if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE)
692					brcmu_pkt_buf_free_skb(skb);
693				h->items[i].state =
694					BRCMF_FWS_HANGER_ITEM_STATE_FREE;
695			}
696		}
697	}
698}
699
700static void brcmf_fws_macdesc_set_name(struct brcmf_fws_info *fws,
701				       struct brcmf_fws_mac_descriptor *desc)
702{
703	if (desc == &fws->desc.other)
704		strlcpy(desc->name, "MAC-OTHER", sizeof(desc->name));
705	else if (desc->mac_handle)
706		scnprintf(desc->name, sizeof(desc->name), "MAC-%d:%d",
707			  desc->mac_handle, desc->interface_id);
708	else
709		scnprintf(desc->name, sizeof(desc->name), "MACIF:%d",
710			  desc->interface_id);
711}
712
713static void brcmf_fws_macdesc_init(struct brcmf_fws_mac_descriptor *desc,
714				   u8 *addr, u8 ifidx)
715{
716	brcmf_dbg(TRACE,
717		  "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
718	desc->occupied = 1;
719	desc->state = BRCMF_FWS_STATE_OPEN;
720	desc->requested_credit = 0;
721	desc->requested_packet = 0;
722	/* depending on use may need ifp->bssidx instead */
723	desc->interface_id = ifidx;
724	desc->ac_bitmap = 0xff; /* update this when handling APSD */
725	if (addr)
726		memcpy(&desc->ea[0], addr, ETH_ALEN);
727}
728
729static
730void brcmf_fws_macdesc_deinit(struct brcmf_fws_mac_descriptor *desc)
731{
732	brcmf_dbg(TRACE,
733		  "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
734	desc->occupied = 0;
735	desc->state = BRCMF_FWS_STATE_CLOSE;
736	desc->requested_credit = 0;
737	desc->requested_packet = 0;
738}
739
740static struct brcmf_fws_mac_descriptor *
741brcmf_fws_macdesc_lookup(struct brcmf_fws_info *fws, u8 *ea)
742{
743	struct brcmf_fws_mac_descriptor *entry;
744	int i;
745
746	if (ea == NULL)
747		return ERR_PTR(-EINVAL);
748
749	entry = &fws->desc.nodes[0];
750	for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
751		if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
752			return entry;
753		entry++;
754	}
755
756	return ERR_PTR(-ENOENT);
757}
758
759static struct brcmf_fws_mac_descriptor*
760brcmf_fws_macdesc_find(struct brcmf_fws_info *fws, struct brcmf_if *ifp, u8 *da)
761{
762	struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
763	bool multicast;
764
765	multicast = is_multicast_ether_addr(da);
766
767	/* Multicast destination, STA and P2P clients get the interface entry.
768	 * STA/GC gets the Mac Entry for TDLS destinations, TDLS destinations
769	 * have their own entry.
770	 */
771	if (multicast && ifp->fws_desc) {
772		entry = ifp->fws_desc;
773		goto done;
774	}
775
776	entry = brcmf_fws_macdesc_lookup(fws, da);
777	if (IS_ERR(entry))
778		entry = ifp->fws_desc;
779
780done:
781	return entry;
782}
783
784static bool brcmf_fws_macdesc_closed(struct brcmf_fws_info *fws,
785				     struct brcmf_fws_mac_descriptor *entry,
786				     int fifo)
787{
788	struct brcmf_fws_mac_descriptor *if_entry;
789	bool closed;
790
791	/* for unique destination entries the related interface
792	 * may be closed.
793	 */
794	if (entry->mac_handle) {
795		if_entry = &fws->desc.iface[entry->interface_id];
796		if (if_entry->state == BRCMF_FWS_STATE_CLOSE)
797			return true;
798	}
799	/* an entry is closed when the state is closed and
800	 * the firmware did not request anything.
801	 */
802	closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
803		 !entry->requested_credit && !entry->requested_packet;
804
805	/* Or firmware does not allow traffic for given fifo */
806	return closed || !(entry->ac_bitmap & BIT(fifo));
807}
808
809static void brcmf_fws_macdesc_cleanup(struct brcmf_fws_info *fws,
810				      struct brcmf_fws_mac_descriptor *entry,
811				      int ifidx)
812{
813	if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
814		brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
815		entry->occupied = !!(entry->psq.len);
816	}
817}
818
819static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
820				      bool (*fn)(struct sk_buff *, void *),
821				      int ifidx)
822{
823	struct brcmf_fws_hanger_item *hi;
824	struct pktq *txq;
825	struct sk_buff *skb;
826	int prec;
827	u32 hslot;
828
829	txq = brcmf_bus_gettxq(fws->drvr->bus_if);
830	if (IS_ERR(txq)) {
831		brcmf_dbg(TRACE, "no txq to clean up\n");
832		return;
833	}
834
835	for (prec = 0; prec < txq->num_prec; prec++) {
836		skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
837		while (skb) {
838			hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
839			hi = &fws->hanger.items[hslot];
840			WARN_ON(skb != hi->pkt);
841			hi->state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
842			brcmu_pkt_buf_free_skb(skb);
843			skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
844		}
845	}
846}
847
848static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
849{
850	int i;
851	struct brcmf_fws_mac_descriptor *table;
852	bool (*matchfn)(struct sk_buff *, void *) = NULL;
853
854	if (fws == NULL)
855		return;
856
857	if (ifidx != -1)
858		matchfn = brcmf_fws_ifidx_match;
859
860	/* cleanup individual nodes */
861	table = &fws->desc.nodes[0];
862	for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
863		brcmf_fws_macdesc_cleanup(fws, &table[i], ifidx);
864
865	brcmf_fws_macdesc_cleanup(fws, &fws->desc.other, ifidx);
866	brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
867	brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
868}
869
870static u8 brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
871{
872	struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
873	u8 *wlh;
874	u16 data_offset = 0;
875	u8 fillers;
876	__le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);
877	__le16 pktseq = cpu_to_le16(brcmf_skbcb(skb)->htod_seq);
878
879	brcmf_dbg(TRACE, "enter: %s, idx=%d hslot=%d htod %X seq %X\n",
880		  entry->name, brcmf_skb_if_flags_get_field(skb, INDEX),
881		  (le32_to_cpu(pkttag) >> 8) & 0xffff,
882		  brcmf_skbcb(skb)->htod, brcmf_skbcb(skb)->htod_seq);
883	if (entry->send_tim_signal)
884		data_offset += 2 + BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
885	if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode))
886		data_offset += BRCMF_FWS_TYPE_SEQ_LEN;
887	/* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
888	data_offset += 2 + BRCMF_FWS_TYPE_PKTTAG_LEN;
889	fillers = round_up(data_offset, 4) - data_offset;
890	data_offset += fillers;
891
892	skb_push(skb, data_offset);
893	wlh = skb->data;
894
895	wlh[0] = BRCMF_FWS_TYPE_PKTTAG;
896	wlh[1] = BRCMF_FWS_TYPE_PKTTAG_LEN;
897	memcpy(&wlh[2], &pkttag, sizeof(pkttag));
898	if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode)) {
899		wlh[1] += BRCMF_FWS_TYPE_SEQ_LEN;
900		memcpy(&wlh[2 + BRCMF_FWS_TYPE_PKTTAG_LEN], &pktseq,
901		       sizeof(pktseq));
902	}
903	wlh += wlh[1] + 2;
904
905	if (entry->send_tim_signal) {
906		entry->send_tim_signal = 0;
907		wlh[0] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP;
908		wlh[1] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
909		wlh[2] = entry->mac_handle;
910		wlh[3] = entry->traffic_pending_bmp;
911		brcmf_dbg(TRACE, "adding TIM info: handle %d bmp 0x%X\n",
912			  entry->mac_handle, entry->traffic_pending_bmp);
913		wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
914		entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
915	}
916	if (fillers)
917		memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);
918
919	return (u8)(data_offset >> 2);
920}
921
922static bool brcmf_fws_tim_update(struct brcmf_fws_info *fws,
923				 struct brcmf_fws_mac_descriptor *entry,
924				 int fifo, bool send_immediately)
925{
926	struct sk_buff *skb;
927	struct brcmf_skbuff_cb *skcb;
928	s32 err;
929	u32 len;
930	u8 data_offset;
931	int ifidx;
932
933	/* check delayedQ and suppressQ in one call using bitmap */
934	if (brcmu_pktq_mlen(&entry->psq, 3 << (fifo * 2)) == 0)
935		entry->traffic_pending_bmp &= ~NBITVAL(fifo);
936	else
937		entry->traffic_pending_bmp |= NBITVAL(fifo);
938
939	entry->send_tim_signal = false;
940	if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
941		entry->send_tim_signal = true;
942	if (send_immediately && entry->send_tim_signal &&
943	    entry->state == BRCMF_FWS_STATE_CLOSE) {
944		/* create a dummy packet and sent that. The traffic          */
945		/* bitmap info will automatically be attached to that packet */
946		len = BRCMF_FWS_TYPE_PKTTAG_LEN + 2 +
947		      BRCMF_FWS_TYPE_SEQ_LEN +
948		      BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2 +
949		      4 + fws->drvr->hdrlen;
950		skb = brcmu_pkt_buf_get_skb(len);
951		if (skb == NULL)
952			return false;
953		skb_pull(skb, len);
954		skcb = brcmf_skbcb(skb);
955		skcb->mac = entry;
956		skcb->state = BRCMF_FWS_SKBSTATE_TIM;
957		skcb->htod = 0;
958		skcb->htod_seq = 0;
959		data_offset = brcmf_fws_hdrpush(fws, skb);
960		ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
961		brcmf_fws_unlock(fws);
962		err = brcmf_proto_txdata(fws->drvr, ifidx, data_offset, skb);
963		brcmf_fws_lock(fws);
964		if (err)
965			brcmu_pkt_buf_free_skb(skb);
966		return true;
967	}
968	return false;
969}
970
971static void
972brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
973			     u8 if_id)
974{
975	struct brcmf_if *ifp = fws->drvr->iflist[!if_id ? 0 : if_id + 1];
976
977	if (WARN_ON(!ifp))
978		return;
979
980	if ((ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
981	    pq->len <= BRCMF_FWS_FLOWCONTROL_LOWATER)
982		brcmf_txflowblock_if(ifp,
983				     BRCMF_NETIF_STOP_REASON_FWS_FC, false);
984	if (!(ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
985	    pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER) {
986		fws->stats.fws_flow_block++;
987		brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
988	}
989	return;
990}
991
992static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
993{
994	brcmf_dbg(CTL, "rssi %d\n", rssi);
995	return 0;
996}
997
998static
999int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
1000{
1001	struct brcmf_fws_mac_descriptor *entry, *existing;
1002	u8 mac_handle;
1003	u8 ifidx;
1004	u8 *addr;
1005
1006	mac_handle = *data++;
1007	ifidx = *data++;
1008	addr = data;
1009
1010	entry = &fws->desc.nodes[mac_handle & 0x1F];
1011	if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
1012		if (entry->occupied) {
1013			brcmf_dbg(TRACE, "deleting %s mac %pM\n",
1014				  entry->name, addr);
1015			brcmf_fws_lock(fws);
1016			brcmf_fws_macdesc_cleanup(fws, entry, -1);
1017			brcmf_fws_macdesc_deinit(entry);
1018			brcmf_fws_unlock(fws);
1019		} else
1020			fws->stats.mac_update_failed++;
1021		return 0;
1022	}
1023
1024	existing = brcmf_fws_macdesc_lookup(fws, addr);
1025	if (IS_ERR(existing)) {
1026		if (!entry->occupied) {
1027			brcmf_fws_lock(fws);
1028			entry->mac_handle = mac_handle;
1029			brcmf_fws_macdesc_init(entry, addr, ifidx);
1030			brcmf_fws_macdesc_set_name(fws, entry);
1031			brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
1032					BRCMF_FWS_PSQ_LEN);
1033			brcmf_fws_unlock(fws);
1034			brcmf_dbg(TRACE, "add %s mac %pM\n", entry->name, addr);
1035		} else {
1036			fws->stats.mac_update_failed++;
1037		}
1038	} else {
1039		if (entry != existing) {
1040			brcmf_dbg(TRACE, "copy mac %s\n", existing->name);
1041			brcmf_fws_lock(fws);
1042			memcpy(entry, existing,
1043			       offsetof(struct brcmf_fws_mac_descriptor, psq));
1044			entry->mac_handle = mac_handle;
1045			brcmf_fws_macdesc_deinit(existing);
1046			brcmf_fws_macdesc_set_name(fws, entry);
1047			brcmf_fws_unlock(fws);
1048			brcmf_dbg(TRACE, "relocate %s mac %pM\n", entry->name,
1049				  addr);
1050		} else {
1051			brcmf_dbg(TRACE, "use existing\n");
1052			WARN_ON(entry->mac_handle != mac_handle);
1053			/* TODO: what should we do here: continue, reinit, .. */
1054		}
1055	}
1056	return 0;
1057}
1058
1059static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
1060					    u8 type, u8 *data)
1061{
1062	struct brcmf_fws_mac_descriptor *entry;
1063	u8 mac_handle;
1064	int ret;
1065
1066	mac_handle = data[0];
1067	entry = &fws->desc.nodes[mac_handle & 0x1F];
1068	if (!entry->occupied) {
1069		fws->stats.mac_ps_update_failed++;
1070		return -ESRCH;
1071	}
1072	brcmf_fws_lock(fws);
1073	/* a state update should wipe old credits */
1074	entry->requested_credit = 0;
1075	entry->requested_packet = 0;
1076	if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
1077		entry->state = BRCMF_FWS_STATE_OPEN;
1078		ret = BRCMF_FWS_RET_OK_SCHEDULE;
1079	} else {
1080		entry->state = BRCMF_FWS_STATE_CLOSE;
1081		brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BK, false);
1082		brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BE, false);
1083		brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VI, false);
1084		brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VO, true);
1085		ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1086	}
1087	brcmf_fws_unlock(fws);
1088	return ret;
1089}
1090
1091static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
1092					      u8 type, u8 *data)
1093{
1094	struct brcmf_fws_mac_descriptor *entry;
1095	u8 ifidx;
1096	int ret;
1097
1098	ifidx = data[0];
1099
1100	if (ifidx >= BRCMF_MAX_IFS) {
1101		ret = -ERANGE;
1102		goto fail;
1103	}
1104
1105	entry = &fws->desc.iface[ifidx];
1106	if (!entry->occupied) {
1107		ret = -ESRCH;
1108		goto fail;
1109	}
1110
1111	brcmf_dbg(TRACE, "%s (%d): %s\n", brcmf_fws_get_tlv_name(type), type,
1112		  entry->name);
1113	brcmf_fws_lock(fws);
1114	switch (type) {
1115	case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1116		entry->state = BRCMF_FWS_STATE_OPEN;
1117		ret = BRCMF_FWS_RET_OK_SCHEDULE;
1118		break;
1119	case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1120		entry->state = BRCMF_FWS_STATE_CLOSE;
1121		ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1122		break;
1123	default:
1124		ret = -EINVAL;
1125		brcmf_fws_unlock(fws);
1126		goto fail;
1127	}
1128	brcmf_fws_unlock(fws);
1129	return ret;
1130
1131fail:
1132	fws->stats.if_update_failed++;
1133	return ret;
1134}
1135
1136static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
1137				      u8 *data)
1138{
1139	struct brcmf_fws_mac_descriptor *entry;
1140
1141	entry = &fws->desc.nodes[data[1] & 0x1F];
1142	if (!entry->occupied) {
1143		if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1144			fws->stats.credit_request_failed++;
1145		else
1146			fws->stats.packet_request_failed++;
1147		return -ESRCH;
1148	}
1149
1150	brcmf_dbg(TRACE, "%s (%d): %s cnt %d bmp %d\n",
1151		  brcmf_fws_get_tlv_name(type), type, entry->name,
1152		  data[0], data[2]);
1153	brcmf_fws_lock(fws);
1154	if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1155		entry->requested_credit = data[0];
1156	else
1157		entry->requested_packet = data[0];
1158
1159	entry->ac_bitmap = data[2];
1160	brcmf_fws_unlock(fws);
1161	return BRCMF_FWS_RET_OK_SCHEDULE;
1162}
1163
1164static void
1165brcmf_fws_macdesc_use_req_credit(struct brcmf_fws_mac_descriptor *entry,
1166				 struct sk_buff *skb)
1167{
1168	if (entry->requested_credit > 0) {
1169		entry->requested_credit--;
1170		brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1171		brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 1);
1172		if (entry->state != BRCMF_FWS_STATE_CLOSE)
1173			brcmf_err("requested credit set while mac not closed!\n");
1174	} else if (entry->requested_packet > 0) {
1175		entry->requested_packet--;
1176		brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1177		brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1178		if (entry->state != BRCMF_FWS_STATE_CLOSE)
1179			brcmf_err("requested packet set while mac not closed!\n");
1180	} else {
1181		brcmf_skb_if_flags_set_field(skb, REQUESTED, 0);
1182		brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1183	}
1184}
1185
1186static void brcmf_fws_macdesc_return_req_credit(struct sk_buff *skb)
1187{
1188	struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1189
1190	if ((brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) &&
1191	    (entry->state == BRCMF_FWS_STATE_CLOSE))
1192		entry->requested_credit++;
1193}
1194
1195static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
1196				     u8 fifo, u8 credits)
1197{
1198	int lender_ac;
1199	int *borrowed;
1200	int *fifo_credit;
1201
1202	if (!credits)
1203		return;
1204
1205	fws->fifo_credit_map |= 1 << fifo;
1206
1207	if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
1208	    (fws->credits_borrowed[0])) {
1209		for (lender_ac = BRCMF_FWS_FIFO_AC_VO; lender_ac >= 0;
1210		     lender_ac--) {
1211			borrowed = &fws->credits_borrowed[lender_ac];
1212			if (*borrowed) {
1213				fws->fifo_credit_map |= (1 << lender_ac);
1214				fifo_credit = &fws->fifo_credit[lender_ac];
1215				if (*borrowed >= credits) {
1216					*borrowed -= credits;
1217					*fifo_credit += credits;
1218					return;
1219				} else {
1220					credits -= *borrowed;
1221					*fifo_credit += *borrowed;
1222					*borrowed = 0;
1223				}
1224			}
1225		}
1226	}
1227
1228	fws->fifo_credit[fifo] += credits;
1229}
1230
1231static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
1232{
1233	/* only schedule dequeue when there are credits for delayed traffic */
1234	if ((fws->fifo_credit_map & fws->fifo_delay_map) ||
1235	    (!brcmf_fws_fc_active(fws) && fws->fifo_delay_map))
1236		queue_work(fws->fws_wq, &fws->fws_dequeue_work);
1237}
1238
1239static int brcmf_fws_enq(struct brcmf_fws_info *fws,
1240			 enum brcmf_fws_skb_state state, int fifo,
1241			 struct sk_buff *p)
1242{
1243	int prec = 2 * fifo;
1244	u32 *qfull_stat = &fws->stats.delayq_full_error;
1245	struct brcmf_fws_mac_descriptor *entry;
1246	struct pktq *pq;
1247	struct sk_buff_head *queue;
1248	struct sk_buff *p_head;
1249	struct sk_buff *p_tail;
1250	u32 fr_new;
1251	u32 fr_compare;
1252
1253	entry = brcmf_skbcb(p)->mac;
1254	if (entry == NULL) {
1255		brcmf_err("no mac descriptor found for skb %p\n", p);
1256		return -ENOENT;
1257	}
1258
1259	brcmf_dbg(DATA, "enter: fifo %d skb %p\n", fifo, p);
1260	if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1261		prec += 1;
1262		qfull_stat = &fws->stats.supprq_full_error;
1263
1264		/* Fix out of order delivery of frames. Dont assume frame    */
1265		/* can be inserted at the end, but look for correct position */
1266		pq = &entry->psq;
1267		if (pktq_full(pq) || pktq_pfull(pq, prec)) {
1268			*qfull_stat += 1;
1269			return -ENFILE;
1270		}
1271		queue = &pq->q[prec].skblist;
1272
1273		p_head = skb_peek(queue);
1274		p_tail = skb_peek_tail(queue);
1275		fr_new = brcmf_skb_htod_tag_get_field(p, FREERUN);
1276
1277		while (p_head != p_tail) {
1278			fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
1279								  FREERUN);
1280			/* be sure to handle wrap of 256 */
1281			if (((fr_new > fr_compare) &&
1282			     ((fr_new - fr_compare) < 128)) ||
1283			    ((fr_new < fr_compare) &&
1284			     ((fr_compare - fr_new) > 128)))
1285				break;
1286			p_tail = skb_queue_prev(queue, p_tail);
1287		}
1288		/* Position found. Determine what to do */
1289		if (p_tail == NULL) {
1290			/* empty list */
1291			__skb_queue_tail(queue, p);
1292		} else {
1293			fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
1294								  FREERUN);
1295			if (((fr_new > fr_compare) &&
1296			     ((fr_new - fr_compare) < 128)) ||
1297			    ((fr_new < fr_compare) &&
1298			     ((fr_compare - fr_new) > 128))) {
1299				/* After tail */
1300				__skb_queue_after(queue, p_tail, p);
1301			} else {
1302				/* Before tail */
1303				__skb_insert(p, p_tail->prev, p_tail, queue);
1304			}
1305		}
1306
1307		/* Complete the counters and statistics */
1308		pq->len++;
1309		if (pq->hi_prec < prec)
1310			pq->hi_prec = (u8) prec;
1311	} else if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
1312		*qfull_stat += 1;
1313		return -ENFILE;
1314	}
1315
1316	/* increment total enqueued packet count */
1317	fws->fifo_delay_map |= 1 << fifo;
1318	fws->fifo_enqpkt[fifo]++;
1319
1320	/* update the sk_buff state */
1321	brcmf_skbcb(p)->state = state;
1322
1323	/*
1324	 * A packet has been pushed so update traffic
1325	 * availability bitmap, if applicable
1326	 */
1327	brcmf_fws_tim_update(fws, entry, fifo, true);
1328	brcmf_fws_flow_control_check(fws, &entry->psq,
1329				     brcmf_skb_if_flags_get_field(p, INDEX));
1330	return 0;
1331}
1332
1333static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
1334{
1335	struct brcmf_fws_mac_descriptor *table;
1336	struct brcmf_fws_mac_descriptor *entry;
1337	struct sk_buff *p;
1338	int num_nodes;
1339	int node_pos;
1340	int prec_out;
1341	int pmsk;
1342	int i;
1343
1344	table = (struct brcmf_fws_mac_descriptor *)&fws->desc;
1345	num_nodes = sizeof(fws->desc) / sizeof(struct brcmf_fws_mac_descriptor);
1346	node_pos = fws->deq_node_pos[fifo];
1347
1348	for (i = 0; i < num_nodes; i++) {
1349		entry = &table[(node_pos + i) % num_nodes];
1350		if (!entry->occupied ||
1351		    brcmf_fws_macdesc_closed(fws, entry, fifo))
1352			continue;
1353
1354		if (entry->suppressed)
1355			pmsk = 2;
1356		else
1357			pmsk = 3;
1358		p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
1359		if (p == NULL) {
1360			if (entry->suppressed) {
1361				if (entry->suppr_transit_count)
1362					continue;
1363				entry->suppressed = false;
1364				p = brcmu_pktq_mdeq(&entry->psq,
1365						    1 << (fifo * 2), &prec_out);
1366			}
1367		}
1368		if  (p == NULL)
1369			continue;
1370
1371		brcmf_fws_macdesc_use_req_credit(entry, p);
1372
1373		/* move dequeue position to ensure fair round-robin */
1374		fws->deq_node_pos[fifo] = (node_pos + i + 1) % num_nodes;
1375		brcmf_fws_flow_control_check(fws, &entry->psq,
1376					     brcmf_skb_if_flags_get_field(p,
1377									  INDEX)
1378					     );
1379		/*
1380		 * A packet has been picked up, update traffic
1381		 * availability bitmap, if applicable
1382		 */
1383		brcmf_fws_tim_update(fws, entry, fifo, false);
1384
1385		/*
1386		 * decrement total enqueued fifo packets and
1387		 * clear delay bitmap if done.
1388		 */
1389		fws->fifo_enqpkt[fifo]--;
1390		if (fws->fifo_enqpkt[fifo] == 0)
1391			fws->fifo_delay_map &= ~(1 << fifo);
1392		goto done;
1393	}
1394	p = NULL;
1395done:
1396	brcmf_dbg(DATA, "exit: fifo %d skb %p\n", fifo, p);
1397	return p;
1398}
1399
1400static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
1401					 struct sk_buff *skb, u8 ifidx,
1402					 u32 genbit, u16 seq)
1403{
1404	struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1405	u32 hslot;
1406	int ret;
1407
1408	hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1409
1410	/* this packet was suppressed */
1411	if (!entry->suppressed) {
1412		entry->suppressed = true;
1413		entry->suppr_transit_count = entry->transit_count;
1414		brcmf_dbg(DATA, "suppress %s: transit %d\n",
1415			  entry->name, entry->transit_count);
1416	}
1417
1418	entry->generation = genbit;
1419
1420	brcmf_skb_htod_tag_set_field(skb, GENERATION, genbit);
1421	brcmf_skbcb(skb)->htod_seq = seq;
1422	if (brcmf_skb_htod_seq_get_field(skb, FROMFW)) {
1423		brcmf_skb_htod_seq_set_field(skb, FROMDRV, 1);
1424		brcmf_skb_htod_seq_set_field(skb, FROMFW, 0);
1425	} else {
1426		brcmf_skb_htod_seq_set_field(skb, FROMDRV, 0);
1427	}
1428	ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo, skb);
1429
1430	if (ret != 0) {
1431		/* suppress q is full drop this packet */
1432		brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb, true);
1433	} else {
1434		/* Mark suppressed to avoid a double free during wlfc cleanup */
1435		brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot);
1436	}
1437
1438	return ret;
1439}
1440
1441static int
1442brcmf_fws_txs_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
1443		      u32 genbit, u16 seq)
1444{
1445	u32 fifo;
1446	int ret;
1447	bool remove_from_hanger = true;
1448	struct sk_buff *skb;
1449	struct brcmf_skbuff_cb *skcb;
1450	struct brcmf_fws_mac_descriptor *entry = NULL;
1451	u8 ifidx;
1452
1453	brcmf_dbg(DATA, "flags %d\n", flags);
1454
1455	if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
1456		fws->stats.txs_discard++;
1457	else if (flags == BRCMF_FWS_TXSTATUS_CORE_SUPPRESS) {
1458		fws->stats.txs_supp_core++;
1459		remove_from_hanger = false;
1460	} else if (flags == BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS) {
1461		fws->stats.txs_supp_ps++;
1462		remove_from_hanger = false;
1463	} else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
1464		fws->stats.txs_tossed++;
1465	else if (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)
1466		fws->stats.txs_host_tossed++;
1467	else
1468		brcmf_err("unexpected txstatus\n");
1469
1470	ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1471				      remove_from_hanger);
1472	if (ret != 0) {
1473		brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
1474		return ret;
1475	}
1476
1477	skcb = brcmf_skbcb(skb);
1478	entry = skcb->mac;
1479	if (WARN_ON(!entry)) {
1480		brcmu_pkt_buf_free_skb(skb);
1481		return -EINVAL;
1482	}
1483	entry->transit_count--;
1484	if (entry->suppressed && entry->suppr_transit_count)
1485		entry->suppr_transit_count--;
1486
1487	brcmf_dbg(DATA, "%s flags %d htod %X seq %X\n", entry->name, flags,
1488		  skcb->htod, seq);
1489
1490	/* pick up the implicit credit from this packet */
1491	fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
1492	if ((fws->fcmode == BRCMF_FWS_FCMODE_IMPLIED_CREDIT) ||
1493	    (brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) ||
1494	    (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)) {
1495		brcmf_fws_return_credits(fws, fifo, 1);
1496		brcmf_fws_schedule_deq(fws);
1497	}
1498	brcmf_fws_macdesc_return_req_credit(skb);
1499
1500	if (brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb)) {
1501		brcmu_pkt_buf_free_skb(skb);
1502		return -EINVAL;
1503	}
1504	if (!remove_from_hanger)
1505		ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb, ifidx,
1506						    genbit, seq);
1507	if (remove_from_hanger || ret)
1508		brcmf_txfinalize(fws->drvr, skb, ifidx, true);
1509
1510	return 0;
1511}
1512
1513static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
1514					     u8 *data)
1515{
1516	int i;
1517
1518	if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
1519		brcmf_dbg(INFO, "ignored\n");
1520		return BRCMF_FWS_RET_OK_NOSCHEDULE;
1521	}
1522
1523	brcmf_dbg(DATA, "enter: data %pM\n", data);
1524	brcmf_fws_lock(fws);
1525	for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
1526		brcmf_fws_return_credits(fws, i, data[i]);
1527
1528	brcmf_dbg(DATA, "map: credit %x delay %x\n", fws->fifo_credit_map,
1529		  fws->fifo_delay_map);
1530	brcmf_fws_unlock(fws);
1531	return BRCMF_FWS_RET_OK_SCHEDULE;
1532}
1533
1534static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
1535{
1536	__le32 status_le;
1537	__le16 seq_le;
1538	u32 status;
1539	u32 hslot;
1540	u32 genbit;
1541	u8 flags;
1542	u16 seq;
1543
1544	fws->stats.txs_indicate++;
1545	memcpy(&status_le, data, sizeof(status_le));
1546	status = le32_to_cpu(status_le);
1547	flags = brcmf_txstatus_get_field(status, FLAGS);
1548	hslot = brcmf_txstatus_get_field(status, HSLOT);
1549	genbit = brcmf_txstatus_get_field(status, GENERATION);
1550	if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode)) {
1551		memcpy(&seq_le, &data[BRCMF_FWS_TYPE_PKTTAG_LEN],
1552		       sizeof(seq_le));
1553		seq = le16_to_cpu(seq_le);
1554	} else {
1555		seq = 0;
1556	}
1557
1558	brcmf_fws_lock(fws);
1559	brcmf_fws_txs_process(fws, flags, hslot, genbit, seq);
1560	brcmf_fws_unlock(fws);
1561	return BRCMF_FWS_RET_OK_NOSCHEDULE;
1562}
1563
1564static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
1565{
1566	__le32 timestamp;
1567
1568	memcpy(&timestamp, &data[2], sizeof(timestamp));
1569	brcmf_dbg(CTL, "received: seq %d, timestamp %d\n", data[1],
1570		  le32_to_cpu(timestamp));
1571	return 0;
1572}
1573
1574static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
1575				       const struct brcmf_event_msg *e,
1576				       void *data)
1577{
1578	struct brcmf_fws_info *fws = ifp->drvr->fws;
1579	int i;
1580	u8 *credits = data;
1581
1582	if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
1583		brcmf_err("event payload too small (%d)\n", e->datalen);
1584		return -EINVAL;
1585	}
1586	if (fws->creditmap_received)
1587		return 0;
1588
1589	fws->creditmap_received = true;
1590
1591	brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
1592	brcmf_fws_lock(fws);
1593	for (i = 0; i < ARRAY_SIZE(fws->fifo_credit); i++) {
1594		if (*credits)
1595			fws->fifo_credit_map |= 1 << i;
1596		else
1597			fws->fifo_credit_map &= ~(1 << i);
1598		fws->fifo_credit[i] = *credits++;
1599	}
1600	brcmf_fws_schedule_deq(fws);
1601	brcmf_fws_unlock(fws);
1602	return 0;
1603}
1604
1605static int brcmf_fws_notify_bcmc_credit_support(struct brcmf_if *ifp,
1606						const struct brcmf_event_msg *e,
1607						void *data)
1608{
1609	struct brcmf_fws_info *fws = ifp->drvr->fws;
1610
1611	brcmf_fws_lock(fws);
1612	if (fws)
1613		fws->bcmc_credit_check = true;
1614	brcmf_fws_unlock(fws);
1615	return 0;
1616}
1617
1618int brcmf_fws_hdrpull(struct brcmf_pub *drvr, int ifidx, s16 signal_len,
1619		      struct sk_buff *skb)
1620{
1621	struct brcmf_skb_reorder_data *rd;
1622	struct brcmf_fws_info *fws = drvr->fws;
1623	u8 *signal_data;
1624	s16 data_len;
1625	u8 type;
1626	u8 len;
1627	u8 *data;
1628	s32 status;
1629	s32 err;
1630
1631	brcmf_dbg(HDRS, "enter: ifidx %d, skblen %u, sig %d\n",
1632		  ifidx, skb->len, signal_len);
1633
1634	WARN_ON(signal_len > skb->len);
1635
1636	if (!signal_len)
1637		return 0;
1638	/* if flow control disabled, skip to packet data and leave */
1639	if ((!fws) || (!fws->fw_signals)) {
1640		skb_pull(skb, signal_len);
1641		return 0;
1642	}
1643
1644	fws->stats.header_pulls++;
1645	data_len = signal_len;
1646	signal_data = skb->data;
1647
1648	status = BRCMF_FWS_RET_OK_NOSCHEDULE;
1649	while (data_len > 0) {
1650		/* extract tlv info */
1651		type = signal_data[0];
1652
1653		/* FILLER type is actually not a TLV, but
1654		 * a single byte that can be skipped.
1655		 */
1656		if (type == BRCMF_FWS_TYPE_FILLER) {
1657			signal_data += 1;
1658			data_len -= 1;
1659			continue;
1660		}
1661		len = signal_data[1];
1662		data = signal_data + 2;
1663
1664		brcmf_dbg(HDRS, "tlv type=%s (%d), len=%d (%d)\n",
1665			  brcmf_fws_get_tlv_name(type), type, len,
1666			  brcmf_fws_get_tlv_len(fws, type));
1667
1668		/* abort parsing when length invalid */
1669		if (data_len < len + 2)
1670			break;
1671
1672		if (len < brcmf_fws_get_tlv_len(fws, type))
1673			break;
1674
1675		err = BRCMF_FWS_RET_OK_NOSCHEDULE;
1676		switch (type) {
1677		case BRCMF_FWS_TYPE_COMP_TXSTATUS:
1678			break;
1679		case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
1680			rd = (struct brcmf_skb_reorder_data *)skb->cb;
1681			rd->reorder = data;
1682			break;
1683		case BRCMF_FWS_TYPE_MACDESC_ADD:
1684		case BRCMF_FWS_TYPE_MACDESC_DEL:
1685			brcmf_fws_macdesc_indicate(fws, type, data);
1686			break;
1687		case BRCMF_FWS_TYPE_MAC_OPEN:
1688		case BRCMF_FWS_TYPE_MAC_CLOSE:
1689			err = brcmf_fws_macdesc_state_indicate(fws, type, data);
1690			break;
1691		case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1692		case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1693			err = brcmf_fws_interface_state_indicate(fws, type,
1694								 data);
1695			break;
1696		case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
1697		case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
1698			err = brcmf_fws_request_indicate(fws, type, data);
1699			break;
1700		case BRCMF_FWS_TYPE_TXSTATUS:
1701			brcmf_fws_txstatus_indicate(fws, data);
1702			break;
1703		case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
1704			err = brcmf_fws_fifocreditback_indicate(fws, data);
1705			break;
1706		case BRCMF_FWS_TYPE_RSSI:
1707			brcmf_fws_rssi_indicate(fws, *data);
1708			break;
1709		case BRCMF_FWS_TYPE_TRANS_ID:
1710			brcmf_fws_dbg_seqnum_check(fws, data);
1711			break;
1712		case BRCMF_FWS_TYPE_PKTTAG:
1713		case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
1714		default:
1715			fws->stats.tlv_invalid_type++;
1716			break;
1717		}
1718		if (err == BRCMF_FWS_RET_OK_SCHEDULE)
1719			status = BRCMF_FWS_RET_OK_SCHEDULE;
1720		signal_data += len + 2;
1721		data_len -= len + 2;
1722	}
1723
1724	if (data_len != 0)
1725		fws->stats.tlv_parse_failed++;
1726
1727	if (status == BRCMF_FWS_RET_OK_SCHEDULE)
1728		brcmf_fws_schedule_deq(fws);
1729
1730	/* signalling processing result does
1731	 * not affect the actual ethernet packet.
1732	 */
1733	skb_pull(skb, signal_len);
1734
1735	/* this may be a signal-only packet
1736	 */
1737	if (skb->len == 0)
1738		fws->stats.header_only_pkt++;
1739
1740	return 0;
1741}
1742
1743static u8 brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
1744				   struct sk_buff *p)
1745{
1746	struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1747	struct brcmf_fws_mac_descriptor *entry = skcb->mac;
1748	u8 flags;
1749
1750	if (skcb->state != BRCMF_FWS_SKBSTATE_SUPPRESSED)
1751		brcmf_skb_htod_tag_set_field(p, GENERATION, entry->generation);
1752	flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
1753	if (brcmf_skb_if_flags_get_field(p, REQUESTED)) {
1754		/*
1755		 * Indicate that this packet is being sent in response to an
1756		 * explicit request from the firmware side.
1757		 */
1758		flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
1759	}
1760	brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
1761	return brcmf_fws_hdrpush(fws, p);
1762}
1763
1764static void brcmf_fws_rollback_toq(struct brcmf_fws_info *fws,
1765				   struct sk_buff *skb, int fifo)
1766{
1767	struct brcmf_fws_mac_descriptor *entry;
1768	struct sk_buff *pktout;
1769	int qidx, hslot;
1770	int rc = 0;
1771
1772	entry = brcmf_skbcb(skb)->mac;
1773	if (entry->occupied) {
1774		qidx = 2 * fifo;
1775		if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
1776			qidx++;
1777
1778		pktout = brcmu_pktq_penq_head(&entry->psq, qidx, skb);
1779		if (pktout == NULL) {
1780			brcmf_err("%s queue %d full\n", entry->name, qidx);
1781			rc = -ENOSPC;
1782		}
1783	} else {
1784		brcmf_err("%s entry removed\n", entry->name);
1785		rc = -ENOENT;
1786	}
1787
1788	if (rc) {
1789		fws->stats.rollback_failed++;
1790		hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1791		brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED,
1792				      hslot, 0, 0);
1793	} else {
1794		fws->stats.rollback_success++;
1795		brcmf_fws_return_credits(fws, fifo, 1);
1796		brcmf_fws_macdesc_return_req_credit(skb);
1797	}
1798}
1799
1800static int brcmf_fws_borrow_credit(struct brcmf_fws_info *fws)
1801{
1802	int lender_ac;
1803
1804	if (time_after(fws->borrow_defer_timestamp, jiffies)) {
1805		fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
1806		return -ENAVAIL;
1807	}
1808
1809	for (lender_ac = 0; lender_ac <= BRCMF_FWS_FIFO_AC_VO; lender_ac++) {
1810		if (fws->fifo_credit[lender_ac]) {
1811			fws->credits_borrowed[lender_ac]++;
1812			fws->fifo_credit[lender_ac]--;
1813			if (fws->fifo_credit[lender_ac] == 0)
1814				fws->fifo_credit_map &= ~(1 << lender_ac);
1815			fws->fifo_credit_map |= (1 << BRCMF_FWS_FIFO_AC_BE);
1816			brcmf_dbg(DATA, "borrow credit from: %d\n", lender_ac);
1817			return 0;
1818		}
1819	}
1820	fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
1821	return -ENAVAIL;
1822}
1823
1824static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
1825				struct sk_buff *skb)
1826{
1827	struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1828	struct brcmf_fws_mac_descriptor *entry;
1829	int rc;
1830	u8 ifidx;
1831	u8 data_offset;
1832
1833	entry = skcb->mac;
1834	if (IS_ERR(entry))
1835		return PTR_ERR(entry);
1836
1837	data_offset = brcmf_fws_precommit_skb(fws, fifo, skb);
1838	entry->transit_count++;
1839	if (entry->suppressed)
1840		entry->suppr_transit_count++;
1841	ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
1842	brcmf_fws_unlock(fws);
1843	rc = brcmf_proto_txdata(fws->drvr, ifidx, data_offset, skb);
1844	brcmf_fws_lock(fws);
1845	brcmf_dbg(DATA, "%s flags %X htod %X bus_tx %d\n", entry->name,
1846		  skcb->if_flags, skcb->htod, rc);
1847	if (rc < 0) {
1848		entry->transit_count--;
1849		if (entry->suppressed)
1850			entry->suppr_transit_count--;
1851		brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb);
1852		goto rollback;
1853	}
1854
1855	fws->stats.pkt2bus++;
1856	fws->stats.send_pkts[fifo]++;
1857	if (brcmf_skb_if_flags_get_field(skb, REQUESTED))
1858		fws->stats.requested_sent[fifo]++;
1859
1860	return rc;
1861
1862rollback:
1863	brcmf_fws_rollback_toq(fws, skb, fifo);
1864	return rc;
1865}
1866
1867static int brcmf_fws_assign_htod(struct brcmf_fws_info *fws, struct sk_buff *p,
1868				  int fifo)
1869{
1870	struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1871	int rc, hslot;
1872
1873	skcb->htod = 0;
1874	skcb->htod_seq = 0;
1875	hslot = brcmf_fws_hanger_get_free_slot(&fws->hanger);
1876	brcmf_skb_htod_tag_set_field(p, HSLOT, hslot);
1877	brcmf_skb_htod_tag_set_field(p, FREERUN, skcb->mac->seq[fifo]);
1878	brcmf_skb_htod_tag_set_field(p, FIFO, fifo);
1879	rc = brcmf_fws_hanger_pushpkt(&fws->hanger, p, hslot);
1880	if (!rc)
1881		skcb->mac->seq[fifo]++;
1882	else
1883		fws->stats.generic_error++;
1884	return rc;
1885}
1886
1887int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
1888{
1889	struct brcmf_pub *drvr = ifp->drvr;
1890	struct brcmf_fws_info *fws = drvr->fws;
1891	struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1892	struct ethhdr *eh = (struct ethhdr *)(skb->data);
1893	int fifo = BRCMF_FWS_FIFO_BCMC;
1894	bool multicast = is_multicast_ether_addr(eh->h_dest);
1895	int rc = 0;
1896
1897	brcmf_dbg(DATA, "tx proto=0x%X\n", ntohs(eh->h_proto));
1898	/* determine the priority */
1899	if (!skb->priority)
1900		skb->priority = cfg80211_classify8021d(skb, NULL);
1901
1902	drvr->tx_multicast += !!multicast;
1903
1904	if (fws->avoid_queueing) {
1905		rc = brcmf_proto_txdata(drvr, ifp->ifidx, 0, skb);
1906		if (rc < 0)
1907			brcmf_txfinalize(drvr, skb, ifp->ifidx, false);
1908		return rc;
1909	}
1910
1911	/* set control buffer information */
1912	skcb->if_flags = 0;
1913	skcb->state = BRCMF_FWS_SKBSTATE_NEW;
1914	brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
1915	if (!multicast)
1916		fifo = brcmf_fws_prio2fifo[skb->priority];
1917
1918	brcmf_fws_lock(fws);
1919	if (fifo != BRCMF_FWS_FIFO_AC_BE && fifo < BRCMF_FWS_FIFO_BCMC)
1920		fws->borrow_defer_timestamp = jiffies +
1921					      BRCMF_FWS_BORROW_DEFER_PERIOD;
1922
1923	skcb->mac = brcmf_fws_macdesc_find(fws, ifp, eh->h_dest);
1924	brcmf_dbg(DATA, "%s mac %pM multi %d fifo %d\n", skcb->mac->name,
1925		  eh->h_dest, multicast, fifo);
1926	if (!brcmf_fws_assign_htod(fws, skb, fifo)) {
1927		brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
1928		brcmf_fws_schedule_deq(fws);
1929	} else {
1930		brcmf_err("drop skb: no hanger slot\n");
1931		brcmf_txfinalize(drvr, skb, ifp->ifidx, false);
1932		rc = -ENOMEM;
1933	}
1934	brcmf_fws_unlock(fws);
1935
1936	return rc;
1937}
1938
1939void brcmf_fws_reset_interface(struct brcmf_if *ifp)
1940{
1941	struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1942
1943	brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
1944	if (!entry)
1945		return;
1946
1947	brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
1948}
1949
1950void brcmf_fws_add_interface(struct brcmf_if *ifp)
1951{
1952	struct brcmf_fws_info *fws = ifp->drvr->fws;
1953	struct brcmf_fws_mac_descriptor *entry;
1954
1955	if (!ifp->ndev)
1956		return;
1957
1958	entry = &fws->desc.iface[ifp->ifidx];
1959	ifp->fws_desc = entry;
1960	brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
1961	brcmf_fws_macdesc_set_name(fws, entry);
1962	brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
1963			BRCMF_FWS_PSQ_LEN);
1964	brcmf_dbg(TRACE, "added %s\n", entry->name);
1965}
1966
1967void brcmf_fws_del_interface(struct brcmf_if *ifp)
1968{
1969	struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1970
1971	if (!entry)
1972		return;
1973
1974	brcmf_fws_lock(ifp->drvr->fws);
1975	ifp->fws_desc = NULL;
1976	brcmf_dbg(TRACE, "deleting %s\n", entry->name);
1977	brcmf_fws_macdesc_deinit(entry);
1978	brcmf_fws_cleanup(ifp->drvr->fws, ifp->ifidx);
1979	brcmf_fws_unlock(ifp->drvr->fws);
1980}
1981
1982static void brcmf_fws_dequeue_worker(struct work_struct *worker)
1983{
1984	struct brcmf_fws_info *fws;
1985	struct brcmf_pub *drvr;
1986	struct sk_buff *skb;
1987	int fifo;
1988	u32 hslot;
1989	u32 ifidx;
1990	int ret;
1991
1992	fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
1993	drvr = fws->drvr;
1994
1995	brcmf_fws_lock(fws);
1996	for (fifo = BRCMF_FWS_FIFO_BCMC; fifo >= 0 && !fws->bus_flow_blocked;
1997	     fifo--) {
1998		if (!brcmf_fws_fc_active(fws)) {
1999			while ((skb = brcmf_fws_deq(fws, fifo)) != NULL) {
2000				hslot = brcmf_skb_htod_tag_get_field(skb,
2001								     HSLOT);
2002				brcmf_fws_hanger_poppkt(&fws->hanger, hslot,
2003							&skb, true);
2004				ifidx = brcmf_skb_if_flags_get_field(skb,
2005								     INDEX);
2006				/* Use proto layer to send data frame */
2007				brcmf_fws_unlock(fws);
2008				ret = brcmf_proto_txdata(drvr, ifidx, 0, skb);
2009				brcmf_fws_lock(fws);
2010				if (ret < 0)
2011					brcmf_txfinalize(drvr, skb, ifidx,
2012							 false);
2013				if (fws->bus_flow_blocked)
2014					break;
2015			}
2016			continue;
2017		}
2018		while ((fws->fifo_credit[fifo]) || ((!fws->bcmc_credit_check) &&
2019		       (fifo == BRCMF_FWS_FIFO_BCMC))) {
2020			skb = brcmf_fws_deq(fws, fifo);
2021			if (!skb)
2022				break;
2023			fws->fifo_credit[fifo]--;
2024			if (brcmf_fws_commit_skb(fws, fifo, skb))
2025				break;
2026			if (fws->bus_flow_blocked)
2027				break;
2028		}
2029		if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
2030		    (fws->fifo_credit[fifo] == 0) &&
2031		    (!fws->bus_flow_blocked)) {
2032			while (brcmf_fws_borrow_credit(fws) == 0) {
2033				skb = brcmf_fws_deq(fws, fifo);
2034				if (!skb) {
2035					brcmf_fws_return_credits(fws, fifo, 1);
2036					break;
2037				}
2038				if (brcmf_fws_commit_skb(fws, fifo, skb))
2039					break;
2040				if (fws->bus_flow_blocked)
2041					break;
2042			}
2043		}
2044	}
2045	brcmf_fws_unlock(fws);
2046}
2047
2048#ifdef DEBUG
2049static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
2050{
2051	struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
2052	struct brcmf_fws_stats *fwstats = &bus_if->drvr->fws->stats;
2053
2054	seq_printf(seq,
2055		   "header_pulls:      %u\n"
2056		   "header_only_pkt:   %u\n"
2057		   "tlv_parse_failed:  %u\n"
2058		   "tlv_invalid_type:  %u\n"
2059		   "mac_update_fails:  %u\n"
2060		   "ps_update_fails:   %u\n"
2061		   "if_update_fails:   %u\n"
2062		   "pkt2bus:           %u\n"
2063		   "generic_error:     %u\n"
2064		   "rollback_success:  %u\n"
2065		   "rollback_failed:   %u\n"
2066		   "delayq_full:       %u\n"
2067		   "supprq_full:       %u\n"
2068		   "txs_indicate:      %u\n"
2069		   "txs_discard:       %u\n"
2070		   "txs_suppr_core:    %u\n"
2071		   "txs_suppr_ps:      %u\n"
2072		   "txs_tossed:        %u\n"
2073		   "txs_host_tossed:   %u\n"
2074		   "bus_flow_block:    %u\n"
2075		   "fws_flow_block:    %u\n"
2076		   "send_pkts:         BK:%u BE:%u VO:%u VI:%u BCMC:%u\n"
2077		   "requested_sent:    BK:%u BE:%u VO:%u VI:%u BCMC:%u\n",
2078		   fwstats->header_pulls,
2079		   fwstats->header_only_pkt,
2080		   fwstats->tlv_parse_failed,
2081		   fwstats->tlv_invalid_type,
2082		   fwstats->mac_update_failed,
2083		   fwstats->mac_ps_update_failed,
2084		   fwstats->if_update_failed,
2085		   fwstats->pkt2bus,
2086		   fwstats->generic_error,
2087		   fwstats->rollback_success,
2088		   fwstats->rollback_failed,
2089		   fwstats->delayq_full_error,
2090		   fwstats->supprq_full_error,
2091		   fwstats->txs_indicate,
2092		   fwstats->txs_discard,
2093		   fwstats->txs_supp_core,
2094		   fwstats->txs_supp_ps,
2095		   fwstats->txs_tossed,
2096		   fwstats->txs_host_tossed,
2097		   fwstats->bus_flow_block,
2098		   fwstats->fws_flow_block,
2099		   fwstats->send_pkts[0], fwstats->send_pkts[1],
2100		   fwstats->send_pkts[2], fwstats->send_pkts[3],
2101		   fwstats->send_pkts[4],
2102		   fwstats->requested_sent[0],
2103		   fwstats->requested_sent[1],
2104		   fwstats->requested_sent[2],
2105		   fwstats->requested_sent[3],
2106		   fwstats->requested_sent[4]);
2107
2108	return 0;
2109}
2110#else
2111static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
2112{
2113	return 0;
2114}
2115#endif
2116
2117int brcmf_fws_init(struct brcmf_pub *drvr)
2118{
2119	struct brcmf_fws_info *fws;
2120	u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
2121	int rc;
2122	u32 mode;
2123
2124	drvr->fws = kzalloc(sizeof(*(drvr->fws)), GFP_KERNEL);
2125	if (!drvr->fws) {
2126		rc = -ENOMEM;
2127		goto fail;
2128	}
2129
2130	fws = drvr->fws;
2131
2132	spin_lock_init(&fws->spinlock);
2133
2134	/* set linkage back */
2135	fws->drvr = drvr;
2136	fws->fcmode = fcmode;
2137
2138	if ((drvr->bus_if->always_use_fws_queue == false) &&
2139	    (fcmode == BRCMF_FWS_FCMODE_NONE)) {
2140		fws->avoid_queueing = true;
2141		brcmf_dbg(INFO, "FWS queueing will be avoided\n");
2142		return 0;
2143	}
2144
2145	fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
2146	if (fws->fws_wq == NULL) {
2147		brcmf_err("workqueue creation failed\n");
2148		rc = -EBADF;
2149		goto fail;
2150	}
2151	INIT_WORK(&fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
2152
2153	/* enable firmware signalling if fcmode active */
2154	if (fws->fcmode != BRCMF_FWS_FCMODE_NONE)
2155		tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
2156		       BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
2157		       BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE |
2158		       BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE;
2159
2160	rc = brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
2161				 brcmf_fws_notify_credit_map);
2162	if (rc < 0) {
2163		brcmf_err("register credit map handler failed\n");
2164		goto fail;
2165	}
2166	rc = brcmf_fweh_register(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT,
2167				 brcmf_fws_notify_bcmc_credit_support);
2168	if (rc < 0) {
2169		brcmf_err("register bcmc credit handler failed\n");
2170		brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
2171		goto fail;
2172	}
2173
2174	/* Setting the iovar may fail if feature is unsupported
2175	 * so leave the rc as is so driver initialization can
2176	 * continue. Set mode back to none indicating not enabled.
2177	 */
2178	fws->fw_signals = true;
2179	if (brcmf_fil_iovar_int_set(drvr->iflist[0], "tlv", tlv)) {
2180		brcmf_err("failed to set bdcv2 tlv signaling\n");
2181		fws->fcmode = BRCMF_FWS_FCMODE_NONE;
2182		fws->fw_signals = false;
2183	}
2184
2185	if (brcmf_fil_iovar_int_set(drvr->iflist[0], "ampdu_hostreorder", 1))
2186		brcmf_dbg(INFO, "enabling AMPDU host-reorder failed\n");
2187
2188	/* Enable seq number reuse, if supported */
2189	if (brcmf_fil_iovar_int_get(drvr->iflist[0], "wlfc_mode", &mode) == 0) {
2190		if (BRCMF_FWS_MODE_GET_REUSESEQ(mode)) {
2191			mode = 0;
2192			BRCMF_FWS_MODE_SET_REUSESEQ(mode, 1);
2193			if (brcmf_fil_iovar_int_set(drvr->iflist[0],
2194						    "wlfc_mode", mode) == 0) {
2195				BRCMF_FWS_MODE_SET_REUSESEQ(fws->mode, 1);
2196			}
2197		}
2198	}
2199
2200	brcmf_fws_hanger_init(&fws->hanger);
2201	brcmf_fws_macdesc_init(&fws->desc.other, NULL, 0);
2202	brcmf_fws_macdesc_set_name(fws, &fws->desc.other);
2203	brcmu_pktq_init(&fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
2204			BRCMF_FWS_PSQ_LEN);
2205
2206	/* create debugfs file for statistics */
2207	brcmf_debugfs_add_entry(drvr, "fws_stats",
2208				brcmf_debugfs_fws_stats_read);
2209
2210	brcmf_dbg(INFO, "%s bdcv2 tlv signaling [%x]\n",
2211		  fws->fw_signals ? "enabled" : "disabled", tlv);
2212	return 0;
2213
2214fail:
2215	brcmf_fws_deinit(drvr);
2216	return rc;
2217}
2218
2219void brcmf_fws_deinit(struct brcmf_pub *drvr)
2220{
2221	struct brcmf_fws_info *fws = drvr->fws;
2222
2223	if (!fws)
2224		return;
2225
2226	if (drvr->fws->fws_wq)
2227		destroy_workqueue(drvr->fws->fws_wq);
2228
2229	/* cleanup */
2230	brcmf_fws_lock(fws);
2231	brcmf_fws_cleanup(fws, -1);
2232	drvr->fws = NULL;
2233	brcmf_fws_unlock(fws);
2234
2235	/* free top structure */
2236	kfree(fws);
2237}
2238
2239bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
2240{
2241	if (!fws->creditmap_received)
2242		return false;
2243
2244	return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
2245}
2246
2247void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
2248{
2249	u32 hslot;
2250
2251	if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_TIM) {
2252		brcmu_pkt_buf_free_skb(skb);
2253		return;
2254	}
2255	brcmf_fws_lock(fws);
2256	hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
2257	brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED, hslot, 0, 0);
2258	brcmf_fws_unlock(fws);
2259}
2260
2261void brcmf_fws_bus_blocked(struct brcmf_pub *drvr, bool flow_blocked)
2262{
2263	struct brcmf_fws_info *fws = drvr->fws;
2264
2265	fws->bus_flow_blocked = flow_blocked;
2266	if (!flow_blocked)
2267		brcmf_fws_schedule_deq(fws);
2268	else
2269		fws->stats.bus_flow_block++;
2270}
2271