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