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