Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / wireless / broadcom / brcm80211 / brcmfmac / fwsignal.c
blob1de8497d92b8a9506d93b7b7b23cc250ae18065c
1 /*
2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <linux/types.h>
17 #include <linux/module.h>
18 #include <linux/if_ether.h>
19 #include <linux/spinlock.h>
20 #include <linux/skbuff.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/err.h>
24 #include <linux/jiffies.h>
25 #include <net/cfg80211.h>
27 #include <brcmu_utils.h>
28 #include <brcmu_wifi.h>
29 #include "core.h"
30 #include "debug.h"
31 #include "bus.h"
32 #include "fwil.h"
33 #include "fwil_types.h"
34 #include "fweh.h"
35 #include "fwsignal.h"
36 #include "p2p.h"
37 #include "cfg80211.h"
38 #include "proto.h"
39 #include "bcdc.h"
40 #include "common.h"
42 /**
43 * DOC: Firmware Signalling
45 * Firmware can send signals to host and vice versa, which are passed in the
46 * data packets using TLV based header. This signalling layer is on top of the
47 * BDC bus protocol layer.
51 * single definition for firmware-driver flow control tlv's.
53 * each tlv is specified by BRCMF_FWS_TLV_DEF(name, ID, length).
54 * A length value 0 indicates variable length tlv.
56 #define BRCMF_FWS_TLV_DEFLIST \
57 BRCMF_FWS_TLV_DEF(MAC_OPEN, 1, 1) \
58 BRCMF_FWS_TLV_DEF(MAC_CLOSE, 2, 1) \
59 BRCMF_FWS_TLV_DEF(MAC_REQUEST_CREDIT, 3, 2) \
60 BRCMF_FWS_TLV_DEF(TXSTATUS, 4, 4) \
61 BRCMF_FWS_TLV_DEF(PKTTAG, 5, 4) \
62 BRCMF_FWS_TLV_DEF(MACDESC_ADD, 6, 8) \
63 BRCMF_FWS_TLV_DEF(MACDESC_DEL, 7, 8) \
64 BRCMF_FWS_TLV_DEF(RSSI, 8, 1) \
65 BRCMF_FWS_TLV_DEF(INTERFACE_OPEN, 9, 1) \
66 BRCMF_FWS_TLV_DEF(INTERFACE_CLOSE, 10, 1) \
67 BRCMF_FWS_TLV_DEF(FIFO_CREDITBACK, 11, 6) \
68 BRCMF_FWS_TLV_DEF(PENDING_TRAFFIC_BMP, 12, 2) \
69 BRCMF_FWS_TLV_DEF(MAC_REQUEST_PACKET, 13, 3) \
70 BRCMF_FWS_TLV_DEF(HOST_REORDER_RXPKTS, 14, 10) \
71 BRCMF_FWS_TLV_DEF(TRANS_ID, 18, 6) \
72 BRCMF_FWS_TLV_DEF(COMP_TXSTATUS, 19, 1) \
73 BRCMF_FWS_TLV_DEF(FILLER, 255, 0)
76 * enum brcmf_fws_tlv_type - definition of tlv identifiers.
78 #define BRCMF_FWS_TLV_DEF(name, id, len) \
79 BRCMF_FWS_TYPE_ ## name = id,
80 enum brcmf_fws_tlv_type {
81 BRCMF_FWS_TLV_DEFLIST
82 BRCMF_FWS_TYPE_INVALID
84 #undef BRCMF_FWS_TLV_DEF
87 * enum brcmf_fws_tlv_len - definition of tlv lengths.
89 #define BRCMF_FWS_TLV_DEF(name, id, len) \
90 BRCMF_FWS_TYPE_ ## name ## _LEN = (len),
91 enum brcmf_fws_tlv_len {
92 BRCMF_FWS_TLV_DEFLIST
94 #undef BRCMF_FWS_TLV_DEF
96 /* AMPDU rx reordering definitions */
97 #define BRCMF_RXREORDER_FLOWID_OFFSET 0
98 #define BRCMF_RXREORDER_MAXIDX_OFFSET 2
99 #define BRCMF_RXREORDER_FLAGS_OFFSET 4
100 #define BRCMF_RXREORDER_CURIDX_OFFSET 6
101 #define BRCMF_RXREORDER_EXPIDX_OFFSET 8
103 #define BRCMF_RXREORDER_DEL_FLOW 0x01
104 #define BRCMF_RXREORDER_FLUSH_ALL 0x02
105 #define BRCMF_RXREORDER_CURIDX_VALID 0x04
106 #define BRCMF_RXREORDER_EXPIDX_VALID 0x08
107 #define BRCMF_RXREORDER_NEW_HOLE 0x10
109 #ifdef DEBUG
111 * brcmf_fws_tlv_names - array of tlv names.
113 #define BRCMF_FWS_TLV_DEF(name, id, len) \
114 { id, #name },
115 static struct {
116 enum brcmf_fws_tlv_type id;
117 const char *name;
118 } brcmf_fws_tlv_names[] = {
119 BRCMF_FWS_TLV_DEFLIST
121 #undef BRCMF_FWS_TLV_DEF
124 static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
126 int i;
128 for (i = 0; i < ARRAY_SIZE(brcmf_fws_tlv_names); i++)
129 if (brcmf_fws_tlv_names[i].id == id)
130 return brcmf_fws_tlv_names[i].name;
132 return "INVALID";
134 #else
135 static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
137 return "NODEBUG";
139 #endif /* DEBUG */
142 * The PKTTAG tlv has additional bytes when firmware-signalling
143 * mode has REUSESEQ flag set.
145 #define BRCMF_FWS_TYPE_SEQ_LEN 2
148 * flags used to enable tlv signalling from firmware.
150 #define BRCMF_FWS_FLAGS_RSSI_SIGNALS 0x0001
151 #define BRCMF_FWS_FLAGS_XONXOFF_SIGNALS 0x0002
152 #define BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS 0x0004
153 #define BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE 0x0008
154 #define BRCMF_FWS_FLAGS_PSQ_GENERATIONFSM_ENABLE 0x0010
155 #define BRCMF_FWS_FLAGS_PSQ_ZERO_BUFFER_ENABLE 0x0020
156 #define BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE 0x0040
158 #define BRCMF_FWS_MAC_DESC_TABLE_SIZE 32
159 #define BRCMF_FWS_MAC_DESC_ID_INVALID 0xff
161 #define BRCMF_FWS_HOSTIF_FLOWSTATE_OFF 0
162 #define BRCMF_FWS_HOSTIF_FLOWSTATE_ON 1
163 #define BRCMF_FWS_FLOWCONTROL_HIWATER 128
164 #define BRCMF_FWS_FLOWCONTROL_LOWATER 64
166 #define BRCMF_FWS_PSQ_PREC_COUNT ((BRCMF_FWS_FIFO_COUNT + 1) * 2)
167 #define BRCMF_FWS_PSQ_LEN 256
169 #define BRCMF_FWS_HTOD_FLAG_PKTFROMHOST 0x01
170 #define BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED 0x02
172 #define BRCMF_FWS_RET_OK_NOSCHEDULE 0
173 #define BRCMF_FWS_RET_OK_SCHEDULE 1
175 #define BRCMF_FWS_MODE_REUSESEQ_SHIFT 3 /* seq reuse */
176 #define BRCMF_FWS_MODE_SET_REUSESEQ(x, val) ((x) = \
177 ((x) & ~(1 << BRCMF_FWS_MODE_REUSESEQ_SHIFT)) | \
178 (((val) & 1) << BRCMF_FWS_MODE_REUSESEQ_SHIFT))
179 #define BRCMF_FWS_MODE_GET_REUSESEQ(x) \
180 (((x) >> BRCMF_FWS_MODE_REUSESEQ_SHIFT) & 1)
183 * enum brcmf_fws_skb_state - indicates processing state of skb.
185 * @BRCMF_FWS_SKBSTATE_NEW: sk_buff is newly arrived in the driver.
186 * @BRCMF_FWS_SKBSTATE_DELAYED: sk_buff had to wait on queue.
187 * @BRCMF_FWS_SKBSTATE_SUPPRESSED: sk_buff has been suppressed by firmware.
188 * @BRCMF_FWS_SKBSTATE_TIM: allocated for TIM update info.
190 enum brcmf_fws_skb_state {
191 BRCMF_FWS_SKBSTATE_NEW,
192 BRCMF_FWS_SKBSTATE_DELAYED,
193 BRCMF_FWS_SKBSTATE_SUPPRESSED,
194 BRCMF_FWS_SKBSTATE_TIM
198 * struct brcmf_skbuff_cb - control buffer associated with skbuff.
200 * @bus_flags: 2 bytes reserved for bus specific parameters
201 * @if_flags: holds interface index and packet related flags.
202 * @htod: host to device packet identifier (used in PKTTAG tlv).
203 * @htod_seq: this 16-bit is original seq number for every suppress packet.
204 * @state: transmit state of the packet.
205 * @mac: descriptor related to destination for this packet.
207 * This information is stored in control buffer struct sk_buff::cb, which
208 * provides 48 bytes of storage so this structure should not exceed that.
210 struct brcmf_skbuff_cb {
211 u16 bus_flags;
212 u16 if_flags;
213 u32 htod;
214 u16 htod_seq;
215 enum brcmf_fws_skb_state state;
216 struct brcmf_fws_mac_descriptor *mac;
220 * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
222 #define brcmf_skbcb(skb) ((struct brcmf_skbuff_cb *)((skb)->cb))
225 * sk_buff control if flags
227 * b[11] - packet sent upon firmware request.
228 * b[10] - packet only contains signalling data.
229 * b[9] - packet is a tx packet.
230 * b[8] - packet used requested credit
231 * b[7] - interface in AP mode.
232 * b[3:0] - interface index.
234 #define BRCMF_SKB_IF_FLAGS_REQUESTED_MASK 0x0800
235 #define BRCMF_SKB_IF_FLAGS_REQUESTED_SHIFT 11
236 #define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_MASK 0x0400
237 #define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_SHIFT 10
238 #define BRCMF_SKB_IF_FLAGS_TRANSMIT_MASK 0x0200
239 #define BRCMF_SKB_IF_FLAGS_TRANSMIT_SHIFT 9
240 #define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_MASK 0x0100
241 #define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_SHIFT 8
242 #define BRCMF_SKB_IF_FLAGS_IF_AP_MASK 0x0080
243 #define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT 7
244 #define BRCMF_SKB_IF_FLAGS_INDEX_MASK 0x000f
245 #define BRCMF_SKB_IF_FLAGS_INDEX_SHIFT 0
247 #define brcmf_skb_if_flags_set_field(skb, field, value) \
248 brcmu_maskset16(&(brcmf_skbcb(skb)->if_flags), \
249 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
250 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT, (value))
251 #define brcmf_skb_if_flags_get_field(skb, field) \
252 brcmu_maskget16(brcmf_skbcb(skb)->if_flags, \
253 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
254 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT)
257 * sk_buff control packet identifier
259 * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
261 * - Generated at the host (e.g. dhd)
262 * - Seen as a generic sequence number by firmware except for the flags field.
264 * Generation : b[31] => generation number for this packet [host->fw]
265 * OR, current generation number [fw->host]
266 * Flags : b[30:27] => command, status flags
267 * FIFO-AC : b[26:24] => AC-FIFO id
268 * h-slot : b[23:8] => hanger-slot
269 * freerun : b[7:0] => A free running counter
271 #define BRCMF_SKB_HTOD_TAG_GENERATION_MASK 0x80000000
272 #define BRCMF_SKB_HTOD_TAG_GENERATION_SHIFT 31
273 #define BRCMF_SKB_HTOD_TAG_FLAGS_MASK 0x78000000
274 #define BRCMF_SKB_HTOD_TAG_FLAGS_SHIFT 27
275 #define BRCMF_SKB_HTOD_TAG_FIFO_MASK 0x07000000
276 #define BRCMF_SKB_HTOD_TAG_FIFO_SHIFT 24
277 #define BRCMF_SKB_HTOD_TAG_HSLOT_MASK 0x00ffff00
278 #define BRCMF_SKB_HTOD_TAG_HSLOT_SHIFT 8
279 #define BRCMF_SKB_HTOD_TAG_FREERUN_MASK 0x000000ff
280 #define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT 0
282 #define brcmf_skb_htod_tag_set_field(skb, field, value) \
283 brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
284 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
285 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
286 #define brcmf_skb_htod_tag_get_field(skb, field) \
287 brcmu_maskget32(brcmf_skbcb(skb)->htod, \
288 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
289 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)
291 #define BRCMF_SKB_HTOD_SEQ_FROMFW_MASK 0x2000
292 #define BRCMF_SKB_HTOD_SEQ_FROMFW_SHIFT 13
293 #define BRCMF_SKB_HTOD_SEQ_FROMDRV_MASK 0x1000
294 #define BRCMF_SKB_HTOD_SEQ_FROMDRV_SHIFT 12
295 #define BRCMF_SKB_HTOD_SEQ_NR_MASK 0x0fff
296 #define BRCMF_SKB_HTOD_SEQ_NR_SHIFT 0
298 #define brcmf_skb_htod_seq_set_field(skb, field, value) \
299 brcmu_maskset16(&(brcmf_skbcb(skb)->htod_seq), \
300 BRCMF_SKB_HTOD_SEQ_ ## field ## _MASK, \
301 BRCMF_SKB_HTOD_SEQ_ ## field ## _SHIFT, (value))
302 #define brcmf_skb_htod_seq_get_field(skb, field) \
303 brcmu_maskget16(brcmf_skbcb(skb)->htod_seq, \
304 BRCMF_SKB_HTOD_SEQ_ ## field ## _MASK, \
305 BRCMF_SKB_HTOD_SEQ_ ## field ## _SHIFT)
307 #define BRCMF_FWS_TXSTAT_GENERATION_MASK 0x80000000
308 #define BRCMF_FWS_TXSTAT_GENERATION_SHIFT 31
309 #define BRCMF_FWS_TXSTAT_FLAGS_MASK 0x78000000
310 #define BRCMF_FWS_TXSTAT_FLAGS_SHIFT 27
311 #define BRCMF_FWS_TXSTAT_FIFO_MASK 0x07000000
312 #define BRCMF_FWS_TXSTAT_FIFO_SHIFT 24
313 #define BRCMF_FWS_TXSTAT_HSLOT_MASK 0x00FFFF00
314 #define BRCMF_FWS_TXSTAT_HSLOT_SHIFT 8
315 #define BRCMF_FWS_TXSTAT_FREERUN_MASK 0x000000FF
316 #define BRCMF_FWS_TXSTAT_FREERUN_SHIFT 0
318 #define brcmf_txstatus_get_field(txs, field) \
319 brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
320 BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)
322 /* How long to defer borrowing in jiffies */
323 #define BRCMF_FWS_BORROW_DEFER_PERIOD (HZ / 10)
326 * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
328 * @BRCMF_FWS_FIFO_FIRST: first fifo, ie. background.
329 * @BRCMF_FWS_FIFO_AC_BK: fifo for background traffic.
330 * @BRCMF_FWS_FIFO_AC_BE: fifo for best-effort traffic.
331 * @BRCMF_FWS_FIFO_AC_VI: fifo for video traffic.
332 * @BRCMF_FWS_FIFO_AC_VO: fifo for voice traffic.
333 * @BRCMF_FWS_FIFO_BCMC: fifo for broadcast/multicast (AP only).
334 * @BRCMF_FWS_FIFO_ATIM: fifo for ATIM (AP only).
335 * @BRCMF_FWS_FIFO_COUNT: number of fifos.
337 enum brcmf_fws_fifo {
338 BRCMF_FWS_FIFO_FIRST,
339 BRCMF_FWS_FIFO_AC_BK = BRCMF_FWS_FIFO_FIRST,
340 BRCMF_FWS_FIFO_AC_BE,
341 BRCMF_FWS_FIFO_AC_VI,
342 BRCMF_FWS_FIFO_AC_VO,
343 BRCMF_FWS_FIFO_BCMC,
344 BRCMF_FWS_FIFO_ATIM,
345 BRCMF_FWS_FIFO_COUNT
349 * enum brcmf_fws_txstatus - txstatus flag values.
351 * @BRCMF_FWS_TXSTATUS_DISCARD:
352 * host is free to discard the packet.
353 * @BRCMF_FWS_TXSTATUS_CORE_SUPPRESS:
354 * 802.11 core suppressed the packet.
355 * @BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS:
356 * firmware suppress the packet as device is already in PS mode.
357 * @BRCMF_FWS_TXSTATUS_FW_TOSSED:
358 * firmware tossed the packet.
359 * @BRCMF_FWS_TXSTATUS_HOST_TOSSED:
360 * host tossed the packet.
362 enum brcmf_fws_txstatus {
363 BRCMF_FWS_TXSTATUS_DISCARD,
364 BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
365 BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
366 BRCMF_FWS_TXSTATUS_FW_TOSSED,
367 BRCMF_FWS_TXSTATUS_HOST_TOSSED
370 enum brcmf_fws_fcmode {
371 BRCMF_FWS_FCMODE_NONE,
372 BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
373 BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
376 enum brcmf_fws_mac_desc_state {
377 BRCMF_FWS_STATE_OPEN = 1,
378 BRCMF_FWS_STATE_CLOSE
382 * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
384 * @occupied: slot is in use.
385 * @mac_handle: handle for mac entry determined by firmware.
386 * @interface_id: interface index.
387 * @state: current state.
388 * @suppressed: mac entry is suppressed.
389 * @generation: generation bit.
390 * @ac_bitmap: ac queue bitmap.
391 * @requested_credit: credits requested by firmware.
392 * @ea: ethernet address.
393 * @seq: per-node free-running sequence.
394 * @psq: power-save queue.
395 * @transit_count: packet in transit to firmware.
397 struct brcmf_fws_mac_descriptor {
398 char name[16];
399 u8 occupied;
400 u8 mac_handle;
401 u8 interface_id;
402 u8 state;
403 bool suppressed;
404 u8 generation;
405 u8 ac_bitmap;
406 u8 requested_credit;
407 u8 requested_packet;
408 u8 ea[ETH_ALEN];
409 u8 seq[BRCMF_FWS_FIFO_COUNT];
410 struct pktq psq;
411 int transit_count;
412 int suppr_transit_count;
413 bool send_tim_signal;
414 u8 traffic_pending_bmp;
415 u8 traffic_lastreported_bmp;
418 #define BRCMF_FWS_HANGER_MAXITEMS 1024
421 * enum brcmf_fws_hanger_item_state - state of hanger item.
423 * @BRCMF_FWS_HANGER_ITEM_STATE_FREE: item is free for use.
424 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE: item is in use.
425 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED: item was suppressed.
427 enum brcmf_fws_hanger_item_state {
428 BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
429 BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
430 BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
435 * struct brcmf_fws_hanger_item - single entry for tx pending packet.
437 * @state: entry is either free or occupied.
438 * @pkt: packet itself.
440 struct brcmf_fws_hanger_item {
441 enum brcmf_fws_hanger_item_state state;
442 struct sk_buff *pkt;
446 * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
448 * @pushed: packets pushed to await txstatus.
449 * @popped: packets popped upon handling txstatus.
450 * @failed_to_push: packets that could not be pushed.
451 * @failed_to_pop: packets that could not be popped.
452 * @failed_slotfind: packets for which failed to find an entry.
453 * @slot_pos: last returned item index for a free entry.
454 * @items: array of hanger items.
456 struct brcmf_fws_hanger {
457 u32 pushed;
458 u32 popped;
459 u32 failed_to_push;
460 u32 failed_to_pop;
461 u32 failed_slotfind;
462 u32 slot_pos;
463 struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
466 struct brcmf_fws_macdesc_table {
467 struct brcmf_fws_mac_descriptor nodes[BRCMF_FWS_MAC_DESC_TABLE_SIZE];
468 struct brcmf_fws_mac_descriptor iface[BRCMF_MAX_IFS];
469 struct brcmf_fws_mac_descriptor other;
472 struct brcmf_fws_stats {
473 u32 tlv_parse_failed;
474 u32 tlv_invalid_type;
475 u32 header_only_pkt;
476 u32 header_pulls;
477 u32 pkt2bus;
478 u32 send_pkts[5];
479 u32 requested_sent[5];
480 u32 generic_error;
481 u32 mac_update_failed;
482 u32 mac_ps_update_failed;
483 u32 if_update_failed;
484 u32 packet_request_failed;
485 u32 credit_request_failed;
486 u32 rollback_success;
487 u32 rollback_failed;
488 u32 delayq_full_error;
489 u32 supprq_full_error;
490 u32 txs_indicate;
491 u32 txs_discard;
492 u32 txs_supp_core;
493 u32 txs_supp_ps;
494 u32 txs_tossed;
495 u32 txs_host_tossed;
496 u32 bus_flow_block;
497 u32 fws_flow_block;
500 struct brcmf_fws_info {
501 struct brcmf_pub *drvr;
502 spinlock_t spinlock;
503 ulong flags;
504 struct brcmf_fws_stats stats;
505 struct brcmf_fws_hanger hanger;
506 enum brcmf_fws_fcmode fcmode;
507 bool fw_signals;
508 bool bcmc_credit_check;
509 struct brcmf_fws_macdesc_table desc;
510 struct workqueue_struct *fws_wq;
511 struct work_struct fws_dequeue_work;
512 u32 fifo_enqpkt[BRCMF_FWS_FIFO_COUNT];
513 int fifo_credit[BRCMF_FWS_FIFO_COUNT];
514 int credits_borrowed[BRCMF_FWS_FIFO_AC_VO + 1];
515 int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
516 u32 fifo_credit_map;
517 u32 fifo_delay_map;
518 unsigned long borrow_defer_timestamp;
519 bool bus_flow_blocked;
520 bool creditmap_received;
521 u8 mode;
522 bool avoid_queueing;
526 * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
528 static const int brcmf_fws_prio2fifo[] = {
529 BRCMF_FWS_FIFO_AC_BE,
530 BRCMF_FWS_FIFO_AC_BK,
531 BRCMF_FWS_FIFO_AC_BK,
532 BRCMF_FWS_FIFO_AC_BE,
533 BRCMF_FWS_FIFO_AC_VI,
534 BRCMF_FWS_FIFO_AC_VI,
535 BRCMF_FWS_FIFO_AC_VO,
536 BRCMF_FWS_FIFO_AC_VO
539 #define BRCMF_FWS_TLV_DEF(name, id, len) \
540 case BRCMF_FWS_TYPE_ ## name: \
541 return len;
544 * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
546 * @fws: firmware-signalling information.
547 * @id: identifier of the TLV.
549 * Return: the specified length for the given TLV; Otherwise -EINVAL.
551 static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
552 enum brcmf_fws_tlv_type id)
554 switch (id) {
555 BRCMF_FWS_TLV_DEFLIST
556 default:
557 fws->stats.tlv_invalid_type++;
558 break;
560 return -EINVAL;
562 #undef BRCMF_FWS_TLV_DEF
564 static void brcmf_fws_lock(struct brcmf_fws_info *fws)
565 __acquires(&fws->spinlock)
567 spin_lock_irqsave(&fws->spinlock, fws->flags);
570 static void brcmf_fws_unlock(struct brcmf_fws_info *fws)
571 __releases(&fws->spinlock)
573 spin_unlock_irqrestore(&fws->spinlock, fws->flags);
576 static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
578 u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
579 return ifidx == *(int *)arg;
582 static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
584 int i;
586 memset(hanger, 0, sizeof(*hanger));
587 for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
588 hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
591 static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
593 u32 i;
595 i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
597 while (i != h->slot_pos) {
598 if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
599 h->slot_pos = i;
600 goto done;
602 i++;
603 if (i == BRCMF_FWS_HANGER_MAXITEMS)
604 i = 0;
606 brcmf_err("all slots occupied\n");
607 h->failed_slotfind++;
608 i = BRCMF_FWS_HANGER_MAXITEMS;
609 done:
610 return i;
613 static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
614 struct sk_buff *pkt, u32 slot_id)
616 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
617 return -ENOENT;
619 if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
620 brcmf_err("slot is not free\n");
621 h->failed_to_push++;
622 return -EINVAL;
625 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
626 h->items[slot_id].pkt = pkt;
627 h->pushed++;
628 return 0;
631 static inline int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
632 u32 slot_id, struct sk_buff **pktout,
633 bool remove_item)
635 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
636 return -ENOENT;
638 if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
639 brcmf_err("entry not in use\n");
640 h->failed_to_pop++;
641 return -EINVAL;
644 *pktout = h->items[slot_id].pkt;
645 if (remove_item) {
646 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
647 h->items[slot_id].pkt = NULL;
648 h->popped++;
650 return 0;
653 static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
654 int ifidx)
656 bool (*matchfn)(struct sk_buff *, void *) = NULL;
657 struct sk_buff *skb;
658 int prec;
659 u32 hslot;
661 if (ifidx != -1)
662 matchfn = brcmf_fws_ifidx_match;
663 for (prec = 0; prec < q->num_prec; prec++) {
664 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
665 while (skb) {
666 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
667 brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
668 true);
669 brcmu_pkt_buf_free_skb(skb);
670 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
675 static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
676 u32 slot_id)
678 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
679 return -ENOENT;
681 if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
682 brcmf_err("entry not in use\n");
683 return -EINVAL;
686 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
687 return 0;
690 static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
691 bool (*fn)(struct sk_buff *, void *),
692 int ifidx)
694 struct brcmf_fws_hanger *h = &fws->hanger;
695 struct sk_buff *skb;
696 int i;
697 enum brcmf_fws_hanger_item_state s;
699 for (i = 0; i < ARRAY_SIZE(h->items); i++) {
700 s = h->items[i].state;
701 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE ||
702 s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
703 skb = h->items[i].pkt;
704 if (fn == NULL || fn(skb, &ifidx)) {
705 /* suppress packets freed from psq */
706 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE)
707 brcmu_pkt_buf_free_skb(skb);
708 h->items[i].state =
709 BRCMF_FWS_HANGER_ITEM_STATE_FREE;
715 static void brcmf_fws_macdesc_set_name(struct brcmf_fws_info *fws,
716 struct brcmf_fws_mac_descriptor *desc)
718 if (desc == &fws->desc.other)
719 strlcpy(desc->name, "MAC-OTHER", sizeof(desc->name));
720 else if (desc->mac_handle)
721 scnprintf(desc->name, sizeof(desc->name), "MAC-%d:%d",
722 desc->mac_handle, desc->interface_id);
723 else
724 scnprintf(desc->name, sizeof(desc->name), "MACIF:%d",
725 desc->interface_id);
728 static void brcmf_fws_macdesc_init(struct brcmf_fws_mac_descriptor *desc,
729 u8 *addr, u8 ifidx)
731 brcmf_dbg(TRACE,
732 "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
733 desc->occupied = 1;
734 desc->state = BRCMF_FWS_STATE_OPEN;
735 desc->requested_credit = 0;
736 desc->requested_packet = 0;
737 /* depending on use may need ifp->bsscfgidx instead */
738 desc->interface_id = ifidx;
739 desc->ac_bitmap = 0xff; /* update this when handling APSD */
740 if (addr)
741 memcpy(&desc->ea[0], addr, ETH_ALEN);
744 static
745 void brcmf_fws_macdesc_deinit(struct brcmf_fws_mac_descriptor *desc)
747 brcmf_dbg(TRACE,
748 "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
749 desc->occupied = 0;
750 desc->state = BRCMF_FWS_STATE_CLOSE;
751 desc->requested_credit = 0;
752 desc->requested_packet = 0;
755 static struct brcmf_fws_mac_descriptor *
756 brcmf_fws_macdesc_lookup(struct brcmf_fws_info *fws, u8 *ea)
758 struct brcmf_fws_mac_descriptor *entry;
759 int i;
761 if (ea == NULL)
762 return ERR_PTR(-EINVAL);
764 entry = &fws->desc.nodes[0];
765 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
766 if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
767 return entry;
768 entry++;
771 return ERR_PTR(-ENOENT);
774 static struct brcmf_fws_mac_descriptor*
775 brcmf_fws_macdesc_find(struct brcmf_fws_info *fws, struct brcmf_if *ifp, u8 *da)
777 struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
778 bool multicast;
780 multicast = is_multicast_ether_addr(da);
782 /* Multicast destination, STA and P2P clients get the interface entry.
783 * STA/GC gets the Mac Entry for TDLS destinations, TDLS destinations
784 * have their own entry.
786 if (multicast && ifp->fws_desc) {
787 entry = ifp->fws_desc;
788 goto done;
791 entry = brcmf_fws_macdesc_lookup(fws, da);
792 if (IS_ERR(entry))
793 entry = ifp->fws_desc;
795 done:
796 return entry;
799 static bool brcmf_fws_macdesc_closed(struct brcmf_fws_info *fws,
800 struct brcmf_fws_mac_descriptor *entry,
801 int fifo)
803 struct brcmf_fws_mac_descriptor *if_entry;
804 bool closed;
806 /* for unique destination entries the related interface
807 * may be closed.
809 if (entry->mac_handle) {
810 if_entry = &fws->desc.iface[entry->interface_id];
811 if (if_entry->state == BRCMF_FWS_STATE_CLOSE)
812 return true;
814 /* an entry is closed when the state is closed and
815 * the firmware did not request anything.
817 closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
818 !entry->requested_credit && !entry->requested_packet;
820 /* Or firmware does not allow traffic for given fifo */
821 return closed || !(entry->ac_bitmap & BIT(fifo));
824 static void brcmf_fws_macdesc_cleanup(struct brcmf_fws_info *fws,
825 struct brcmf_fws_mac_descriptor *entry,
826 int ifidx)
828 if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
829 brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
830 entry->occupied = !!(entry->psq.len);
834 static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
835 bool (*fn)(struct sk_buff *, void *),
836 int ifidx)
838 struct brcmf_fws_hanger_item *hi;
839 struct pktq *txq;
840 struct sk_buff *skb;
841 int prec;
842 u32 hslot;
844 txq = brcmf_bus_gettxq(fws->drvr->bus_if);
845 if (IS_ERR(txq)) {
846 brcmf_dbg(TRACE, "no txq to clean up\n");
847 return;
850 for (prec = 0; prec < txq->num_prec; prec++) {
851 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
852 while (skb) {
853 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
854 hi = &fws->hanger.items[hslot];
855 WARN_ON(skb != hi->pkt);
856 hi->state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
857 brcmu_pkt_buf_free_skb(skb);
858 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
863 static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
865 int i;
866 struct brcmf_fws_mac_descriptor *table;
867 bool (*matchfn)(struct sk_buff *, void *) = NULL;
869 if (fws == NULL)
870 return;
872 if (ifidx != -1)
873 matchfn = brcmf_fws_ifidx_match;
875 /* cleanup individual nodes */
876 table = &fws->desc.nodes[0];
877 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
878 brcmf_fws_macdesc_cleanup(fws, &table[i], ifidx);
880 brcmf_fws_macdesc_cleanup(fws, &fws->desc.other, ifidx);
881 brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
882 brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
885 static u8 brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
887 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
888 u8 *wlh;
889 u16 data_offset = 0;
890 u8 fillers;
891 __le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);
892 __le16 pktseq = cpu_to_le16(brcmf_skbcb(skb)->htod_seq);
894 brcmf_dbg(TRACE, "enter: %s, idx=%d hslot=%d htod %X seq %X\n",
895 entry->name, brcmf_skb_if_flags_get_field(skb, INDEX),
896 (le32_to_cpu(pkttag) >> 8) & 0xffff,
897 brcmf_skbcb(skb)->htod, brcmf_skbcb(skb)->htod_seq);
898 if (entry->send_tim_signal)
899 data_offset += 2 + BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
900 if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode))
901 data_offset += BRCMF_FWS_TYPE_SEQ_LEN;
902 /* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
903 data_offset += 2 + BRCMF_FWS_TYPE_PKTTAG_LEN;
904 fillers = round_up(data_offset, 4) - data_offset;
905 data_offset += fillers;
907 skb_push(skb, data_offset);
908 wlh = skb->data;
910 wlh[0] = BRCMF_FWS_TYPE_PKTTAG;
911 wlh[1] = BRCMF_FWS_TYPE_PKTTAG_LEN;
912 memcpy(&wlh[2], &pkttag, sizeof(pkttag));
913 if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode)) {
914 wlh[1] += BRCMF_FWS_TYPE_SEQ_LEN;
915 memcpy(&wlh[2 + BRCMF_FWS_TYPE_PKTTAG_LEN], &pktseq,
916 sizeof(pktseq));
918 wlh += wlh[1] + 2;
920 if (entry->send_tim_signal) {
921 entry->send_tim_signal = 0;
922 wlh[0] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP;
923 wlh[1] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
924 wlh[2] = entry->mac_handle;
925 wlh[3] = entry->traffic_pending_bmp;
926 brcmf_dbg(TRACE, "adding TIM info: handle %d bmp 0x%X\n",
927 entry->mac_handle, entry->traffic_pending_bmp);
928 wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
929 entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
931 if (fillers)
932 memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);
934 return (u8)(data_offset >> 2);
937 static bool brcmf_fws_tim_update(struct brcmf_fws_info *fws,
938 struct brcmf_fws_mac_descriptor *entry,
939 int fifo, bool send_immediately)
941 struct sk_buff *skb;
942 struct brcmf_skbuff_cb *skcb;
943 s32 err;
944 u32 len;
945 u8 data_offset;
946 int ifidx;
948 /* check delayedQ and suppressQ in one call using bitmap */
949 if (brcmu_pktq_mlen(&entry->psq, 3 << (fifo * 2)) == 0)
950 entry->traffic_pending_bmp &= ~NBITVAL(fifo);
951 else
952 entry->traffic_pending_bmp |= NBITVAL(fifo);
954 entry->send_tim_signal = false;
955 if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
956 entry->send_tim_signal = true;
957 if (send_immediately && entry->send_tim_signal &&
958 entry->state == BRCMF_FWS_STATE_CLOSE) {
959 /* create a dummy packet and sent that. The traffic */
960 /* bitmap info will automatically be attached to that packet */
961 len = BRCMF_FWS_TYPE_PKTTAG_LEN + 2 +
962 BRCMF_FWS_TYPE_SEQ_LEN +
963 BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2 +
964 4 + fws->drvr->hdrlen;
965 skb = brcmu_pkt_buf_get_skb(len);
966 if (skb == NULL)
967 return false;
968 skb_pull(skb, len);
969 skcb = brcmf_skbcb(skb);
970 skcb->mac = entry;
971 skcb->state = BRCMF_FWS_SKBSTATE_TIM;
972 skcb->htod = 0;
973 skcb->htod_seq = 0;
974 data_offset = brcmf_fws_hdrpush(fws, skb);
975 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
976 brcmf_fws_unlock(fws);
977 err = brcmf_proto_txdata(fws->drvr, ifidx, data_offset, skb);
978 brcmf_fws_lock(fws);
979 if (err)
980 brcmu_pkt_buf_free_skb(skb);
981 return true;
983 return false;
986 static void
987 brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
988 u8 if_id)
990 struct brcmf_if *ifp = brcmf_get_ifp(fws->drvr, if_id);
992 if (WARN_ON(!ifp))
993 return;
995 if ((ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
996 pq->len <= BRCMF_FWS_FLOWCONTROL_LOWATER)
997 brcmf_txflowblock_if(ifp,
998 BRCMF_NETIF_STOP_REASON_FWS_FC, false);
999 if (!(ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
1000 pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER) {
1001 fws->stats.fws_flow_block++;
1002 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
1004 return;
1007 static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
1009 brcmf_dbg(CTL, "rssi %d\n", rssi);
1010 return 0;
1013 static
1014 int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
1016 struct brcmf_fws_mac_descriptor *entry, *existing;
1017 u8 mac_handle;
1018 u8 ifidx;
1019 u8 *addr;
1021 mac_handle = *data++;
1022 ifidx = *data++;
1023 addr = data;
1025 entry = &fws->desc.nodes[mac_handle & 0x1F];
1026 if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
1027 if (entry->occupied) {
1028 brcmf_dbg(TRACE, "deleting %s mac %pM\n",
1029 entry->name, addr);
1030 brcmf_fws_lock(fws);
1031 brcmf_fws_macdesc_cleanup(fws, entry, -1);
1032 brcmf_fws_macdesc_deinit(entry);
1033 brcmf_fws_unlock(fws);
1034 } else
1035 fws->stats.mac_update_failed++;
1036 return 0;
1039 existing = brcmf_fws_macdesc_lookup(fws, addr);
1040 if (IS_ERR(existing)) {
1041 if (!entry->occupied) {
1042 brcmf_fws_lock(fws);
1043 entry->mac_handle = mac_handle;
1044 brcmf_fws_macdesc_init(entry, addr, ifidx);
1045 brcmf_fws_macdesc_set_name(fws, entry);
1046 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
1047 BRCMF_FWS_PSQ_LEN);
1048 brcmf_fws_unlock(fws);
1049 brcmf_dbg(TRACE, "add %s mac %pM\n", entry->name, addr);
1050 } else {
1051 fws->stats.mac_update_failed++;
1053 } else {
1054 if (entry != existing) {
1055 brcmf_dbg(TRACE, "copy mac %s\n", existing->name);
1056 brcmf_fws_lock(fws);
1057 memcpy(entry, existing,
1058 offsetof(struct brcmf_fws_mac_descriptor, psq));
1059 entry->mac_handle = mac_handle;
1060 brcmf_fws_macdesc_deinit(existing);
1061 brcmf_fws_macdesc_set_name(fws, entry);
1062 brcmf_fws_unlock(fws);
1063 brcmf_dbg(TRACE, "relocate %s mac %pM\n", entry->name,
1064 addr);
1065 } else {
1066 brcmf_dbg(TRACE, "use existing\n");
1067 WARN_ON(entry->mac_handle != mac_handle);
1068 /* TODO: what should we do here: continue, reinit, .. */
1071 return 0;
1074 static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
1075 u8 type, u8 *data)
1077 struct brcmf_fws_mac_descriptor *entry;
1078 u8 mac_handle;
1079 int ret;
1081 mac_handle = data[0];
1082 entry = &fws->desc.nodes[mac_handle & 0x1F];
1083 if (!entry->occupied) {
1084 fws->stats.mac_ps_update_failed++;
1085 return -ESRCH;
1087 brcmf_fws_lock(fws);
1088 /* a state update should wipe old credits */
1089 entry->requested_credit = 0;
1090 entry->requested_packet = 0;
1091 if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
1092 entry->state = BRCMF_FWS_STATE_OPEN;
1093 ret = BRCMF_FWS_RET_OK_SCHEDULE;
1094 } else {
1095 entry->state = BRCMF_FWS_STATE_CLOSE;
1096 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BK, false);
1097 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BE, false);
1098 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VI, false);
1099 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VO, true);
1100 ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1102 brcmf_fws_unlock(fws);
1103 return ret;
1106 static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
1107 u8 type, u8 *data)
1109 struct brcmf_fws_mac_descriptor *entry;
1110 u8 ifidx;
1111 int ret;
1113 ifidx = data[0];
1115 if (ifidx >= BRCMF_MAX_IFS) {
1116 ret = -ERANGE;
1117 goto fail;
1120 entry = &fws->desc.iface[ifidx];
1121 if (!entry->occupied) {
1122 ret = -ESRCH;
1123 goto fail;
1126 brcmf_dbg(TRACE, "%s (%d): %s\n", brcmf_fws_get_tlv_name(type), type,
1127 entry->name);
1128 brcmf_fws_lock(fws);
1129 switch (type) {
1130 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1131 entry->state = BRCMF_FWS_STATE_OPEN;
1132 ret = BRCMF_FWS_RET_OK_SCHEDULE;
1133 break;
1134 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1135 entry->state = BRCMF_FWS_STATE_CLOSE;
1136 ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1137 break;
1138 default:
1139 ret = -EINVAL;
1140 brcmf_fws_unlock(fws);
1141 goto fail;
1143 brcmf_fws_unlock(fws);
1144 return ret;
1146 fail:
1147 fws->stats.if_update_failed++;
1148 return ret;
1151 static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
1152 u8 *data)
1154 struct brcmf_fws_mac_descriptor *entry;
1156 entry = &fws->desc.nodes[data[1] & 0x1F];
1157 if (!entry->occupied) {
1158 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1159 fws->stats.credit_request_failed++;
1160 else
1161 fws->stats.packet_request_failed++;
1162 return -ESRCH;
1165 brcmf_dbg(TRACE, "%s (%d): %s cnt %d bmp %d\n",
1166 brcmf_fws_get_tlv_name(type), type, entry->name,
1167 data[0], data[2]);
1168 brcmf_fws_lock(fws);
1169 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1170 entry->requested_credit = data[0];
1171 else
1172 entry->requested_packet = data[0];
1174 entry->ac_bitmap = data[2];
1175 brcmf_fws_unlock(fws);
1176 return BRCMF_FWS_RET_OK_SCHEDULE;
1179 static void
1180 brcmf_fws_macdesc_use_req_credit(struct brcmf_fws_mac_descriptor *entry,
1181 struct sk_buff *skb)
1183 if (entry->requested_credit > 0) {
1184 entry->requested_credit--;
1185 brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1186 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 1);
1187 if (entry->state != BRCMF_FWS_STATE_CLOSE)
1188 brcmf_err("requested credit set while mac not closed!\n");
1189 } else if (entry->requested_packet > 0) {
1190 entry->requested_packet--;
1191 brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1192 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1193 if (entry->state != BRCMF_FWS_STATE_CLOSE)
1194 brcmf_err("requested packet set while mac not closed!\n");
1195 } else {
1196 brcmf_skb_if_flags_set_field(skb, REQUESTED, 0);
1197 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1201 static void brcmf_fws_macdesc_return_req_credit(struct sk_buff *skb)
1203 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1205 if ((brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) &&
1206 (entry->state == BRCMF_FWS_STATE_CLOSE))
1207 entry->requested_credit++;
1210 static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
1211 u8 fifo, u8 credits)
1213 int lender_ac;
1214 int *borrowed;
1215 int *fifo_credit;
1217 if (!credits)
1218 return;
1220 fws->fifo_credit_map |= 1 << fifo;
1222 if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
1223 (fws->credits_borrowed[0])) {
1224 for (lender_ac = BRCMF_FWS_FIFO_AC_VO; lender_ac >= 0;
1225 lender_ac--) {
1226 borrowed = &fws->credits_borrowed[lender_ac];
1227 if (*borrowed) {
1228 fws->fifo_credit_map |= (1 << lender_ac);
1229 fifo_credit = &fws->fifo_credit[lender_ac];
1230 if (*borrowed >= credits) {
1231 *borrowed -= credits;
1232 *fifo_credit += credits;
1233 return;
1234 } else {
1235 credits -= *borrowed;
1236 *fifo_credit += *borrowed;
1237 *borrowed = 0;
1243 fws->fifo_credit[fifo] += credits;
1246 static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
1248 /* only schedule dequeue when there are credits for delayed traffic */
1249 if ((fws->fifo_credit_map & fws->fifo_delay_map) ||
1250 (!brcmf_fws_fc_active(fws) && fws->fifo_delay_map))
1251 queue_work(fws->fws_wq, &fws->fws_dequeue_work);
1254 static int brcmf_fws_enq(struct brcmf_fws_info *fws,
1255 enum brcmf_fws_skb_state state, int fifo,
1256 struct sk_buff *p)
1258 int prec = 2 * fifo;
1259 u32 *qfull_stat = &fws->stats.delayq_full_error;
1260 struct brcmf_fws_mac_descriptor *entry;
1261 struct pktq *pq;
1262 struct sk_buff_head *queue;
1263 struct sk_buff *p_head;
1264 struct sk_buff *p_tail;
1265 u32 fr_new;
1266 u32 fr_compare;
1268 entry = brcmf_skbcb(p)->mac;
1269 if (entry == NULL) {
1270 brcmf_err("no mac descriptor found for skb %p\n", p);
1271 return -ENOENT;
1274 brcmf_dbg(DATA, "enter: fifo %d skb %p\n", fifo, p);
1275 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1276 prec += 1;
1277 qfull_stat = &fws->stats.supprq_full_error;
1279 /* Fix out of order delivery of frames. Dont assume frame */
1280 /* can be inserted at the end, but look for correct position */
1281 pq = &entry->psq;
1282 if (pktq_full(pq) || pktq_pfull(pq, prec)) {
1283 *qfull_stat += 1;
1284 return -ENFILE;
1286 queue = &pq->q[prec].skblist;
1288 p_head = skb_peek(queue);
1289 p_tail = skb_peek_tail(queue);
1290 fr_new = brcmf_skb_htod_tag_get_field(p, FREERUN);
1292 while (p_head != p_tail) {
1293 fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
1294 FREERUN);
1295 /* be sure to handle wrap of 256 */
1296 if (((fr_new > fr_compare) &&
1297 ((fr_new - fr_compare) < 128)) ||
1298 ((fr_new < fr_compare) &&
1299 ((fr_compare - fr_new) > 128)))
1300 break;
1301 p_tail = skb_queue_prev(queue, p_tail);
1303 /* Position found. Determine what to do */
1304 if (p_tail == NULL) {
1305 /* empty list */
1306 __skb_queue_tail(queue, p);
1307 } else {
1308 fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
1309 FREERUN);
1310 if (((fr_new > fr_compare) &&
1311 ((fr_new - fr_compare) < 128)) ||
1312 ((fr_new < fr_compare) &&
1313 ((fr_compare - fr_new) > 128))) {
1314 /* After tail */
1315 __skb_queue_after(queue, p_tail, p);
1316 } else {
1317 /* Before tail */
1318 __skb_insert(p, p_tail->prev, p_tail, queue);
1322 /* Complete the counters and statistics */
1323 pq->len++;
1324 if (pq->hi_prec < prec)
1325 pq->hi_prec = (u8) prec;
1326 } else if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
1327 *qfull_stat += 1;
1328 return -ENFILE;
1331 /* increment total enqueued packet count */
1332 fws->fifo_delay_map |= 1 << fifo;
1333 fws->fifo_enqpkt[fifo]++;
1335 /* update the sk_buff state */
1336 brcmf_skbcb(p)->state = state;
1339 * A packet has been pushed so update traffic
1340 * availability bitmap, if applicable
1342 brcmf_fws_tim_update(fws, entry, fifo, true);
1343 brcmf_fws_flow_control_check(fws, &entry->psq,
1344 brcmf_skb_if_flags_get_field(p, INDEX));
1345 return 0;
1348 static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
1350 struct brcmf_fws_mac_descriptor *table;
1351 struct brcmf_fws_mac_descriptor *entry;
1352 struct sk_buff *p;
1353 int num_nodes;
1354 int node_pos;
1355 int prec_out;
1356 int pmsk;
1357 int i;
1359 table = (struct brcmf_fws_mac_descriptor *)&fws->desc;
1360 num_nodes = sizeof(fws->desc) / sizeof(struct brcmf_fws_mac_descriptor);
1361 node_pos = fws->deq_node_pos[fifo];
1363 for (i = 0; i < num_nodes; i++) {
1364 entry = &table[(node_pos + i) % num_nodes];
1365 if (!entry->occupied ||
1366 brcmf_fws_macdesc_closed(fws, entry, fifo))
1367 continue;
1369 if (entry->suppressed)
1370 pmsk = 2;
1371 else
1372 pmsk = 3;
1373 p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
1374 if (p == NULL) {
1375 if (entry->suppressed) {
1376 if (entry->suppr_transit_count)
1377 continue;
1378 entry->suppressed = false;
1379 p = brcmu_pktq_mdeq(&entry->psq,
1380 1 << (fifo * 2), &prec_out);
1383 if (p == NULL)
1384 continue;
1386 brcmf_fws_macdesc_use_req_credit(entry, p);
1388 /* move dequeue position to ensure fair round-robin */
1389 fws->deq_node_pos[fifo] = (node_pos + i + 1) % num_nodes;
1390 brcmf_fws_flow_control_check(fws, &entry->psq,
1391 brcmf_skb_if_flags_get_field(p,
1392 INDEX)
1395 * A packet has been picked up, update traffic
1396 * availability bitmap, if applicable
1398 brcmf_fws_tim_update(fws, entry, fifo, false);
1401 * decrement total enqueued fifo packets and
1402 * clear delay bitmap if done.
1404 fws->fifo_enqpkt[fifo]--;
1405 if (fws->fifo_enqpkt[fifo] == 0)
1406 fws->fifo_delay_map &= ~(1 << fifo);
1407 goto done;
1409 p = NULL;
1410 done:
1411 brcmf_dbg(DATA, "exit: fifo %d skb %p\n", fifo, p);
1412 return p;
1415 static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
1416 struct sk_buff *skb,
1417 u32 genbit, u16 seq)
1419 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1420 u32 hslot;
1421 int ret;
1423 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1425 /* this packet was suppressed */
1426 if (!entry->suppressed) {
1427 entry->suppressed = true;
1428 entry->suppr_transit_count = entry->transit_count;
1429 brcmf_dbg(DATA, "suppress %s: transit %d\n",
1430 entry->name, entry->transit_count);
1433 entry->generation = genbit;
1435 brcmf_skb_htod_tag_set_field(skb, GENERATION, genbit);
1436 brcmf_skbcb(skb)->htod_seq = seq;
1437 if (brcmf_skb_htod_seq_get_field(skb, FROMFW)) {
1438 brcmf_skb_htod_seq_set_field(skb, FROMDRV, 1);
1439 brcmf_skb_htod_seq_set_field(skb, FROMFW, 0);
1440 } else {
1441 brcmf_skb_htod_seq_set_field(skb, FROMDRV, 0);
1443 ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo, skb);
1445 if (ret != 0) {
1446 /* suppress q is full drop this packet */
1447 brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb, true);
1448 } else {
1449 /* Mark suppressed to avoid a double free during wlfc cleanup */
1450 brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot);
1453 return ret;
1456 static int
1457 brcmf_fws_txs_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
1458 u32 genbit, u16 seq)
1460 u32 fifo;
1461 int ret;
1462 bool remove_from_hanger = true;
1463 struct sk_buff *skb;
1464 struct brcmf_skbuff_cb *skcb;
1465 struct brcmf_fws_mac_descriptor *entry = NULL;
1466 struct brcmf_if *ifp;
1468 brcmf_dbg(DATA, "flags %d\n", flags);
1470 if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
1471 fws->stats.txs_discard++;
1472 else if (flags == BRCMF_FWS_TXSTATUS_CORE_SUPPRESS) {
1473 fws->stats.txs_supp_core++;
1474 remove_from_hanger = false;
1475 } else if (flags == BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS) {
1476 fws->stats.txs_supp_ps++;
1477 remove_from_hanger = false;
1478 } else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
1479 fws->stats.txs_tossed++;
1480 else if (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)
1481 fws->stats.txs_host_tossed++;
1482 else
1483 brcmf_err("unexpected txstatus\n");
1485 ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1486 remove_from_hanger);
1487 if (ret != 0) {
1488 brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
1489 return ret;
1492 skcb = brcmf_skbcb(skb);
1493 entry = skcb->mac;
1494 if (WARN_ON(!entry)) {
1495 brcmu_pkt_buf_free_skb(skb);
1496 return -EINVAL;
1498 entry->transit_count--;
1499 if (entry->suppressed && entry->suppr_transit_count)
1500 entry->suppr_transit_count--;
1502 brcmf_dbg(DATA, "%s flags %d htod %X seq %X\n", entry->name, flags,
1503 skcb->htod, seq);
1505 /* pick up the implicit credit from this packet */
1506 fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
1507 if ((fws->fcmode == BRCMF_FWS_FCMODE_IMPLIED_CREDIT) ||
1508 (brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) ||
1509 (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)) {
1510 brcmf_fws_return_credits(fws, fifo, 1);
1511 brcmf_fws_schedule_deq(fws);
1513 brcmf_fws_macdesc_return_req_credit(skb);
1515 ret = brcmf_proto_hdrpull(fws->drvr, false, skb, &ifp);
1516 if (ret) {
1517 brcmu_pkt_buf_free_skb(skb);
1518 return -EINVAL;
1520 if (!remove_from_hanger)
1521 ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb,
1522 genbit, seq);
1523 if (remove_from_hanger || ret)
1524 brcmf_txfinalize(ifp, skb, true);
1526 return 0;
1529 static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
1530 u8 *data)
1532 int i;
1534 if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
1535 brcmf_dbg(INFO, "ignored\n");
1536 return BRCMF_FWS_RET_OK_NOSCHEDULE;
1539 brcmf_dbg(DATA, "enter: data %pM\n", data);
1540 brcmf_fws_lock(fws);
1541 for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
1542 brcmf_fws_return_credits(fws, i, data[i]);
1544 brcmf_dbg(DATA, "map: credit %x delay %x\n", fws->fifo_credit_map,
1545 fws->fifo_delay_map);
1546 brcmf_fws_unlock(fws);
1547 return BRCMF_FWS_RET_OK_SCHEDULE;
1550 static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
1552 __le32 status_le;
1553 __le16 seq_le;
1554 u32 status;
1555 u32 hslot;
1556 u32 genbit;
1557 u8 flags;
1558 u16 seq;
1560 fws->stats.txs_indicate++;
1561 memcpy(&status_le, data, sizeof(status_le));
1562 status = le32_to_cpu(status_le);
1563 flags = brcmf_txstatus_get_field(status, FLAGS);
1564 hslot = brcmf_txstatus_get_field(status, HSLOT);
1565 genbit = brcmf_txstatus_get_field(status, GENERATION);
1566 if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode)) {
1567 memcpy(&seq_le, &data[BRCMF_FWS_TYPE_PKTTAG_LEN],
1568 sizeof(seq_le));
1569 seq = le16_to_cpu(seq_le);
1570 } else {
1571 seq = 0;
1574 brcmf_fws_lock(fws);
1575 brcmf_fws_txs_process(fws, flags, hslot, genbit, seq);
1576 brcmf_fws_unlock(fws);
1577 return BRCMF_FWS_RET_OK_NOSCHEDULE;
1580 static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
1582 __le32 timestamp;
1584 memcpy(&timestamp, &data[2], sizeof(timestamp));
1585 brcmf_dbg(CTL, "received: seq %d, timestamp %d\n", data[1],
1586 le32_to_cpu(timestamp));
1587 return 0;
1590 static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
1591 const struct brcmf_event_msg *e,
1592 void *data)
1594 struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
1595 int i;
1596 u8 *credits = data;
1598 if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
1599 brcmf_err("event payload too small (%d)\n", e->datalen);
1600 return -EINVAL;
1602 if (fws->creditmap_received)
1603 return 0;
1605 fws->creditmap_received = true;
1607 brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
1608 brcmf_fws_lock(fws);
1609 for (i = 0; i < ARRAY_SIZE(fws->fifo_credit); i++) {
1610 if (*credits)
1611 fws->fifo_credit_map |= 1 << i;
1612 else
1613 fws->fifo_credit_map &= ~(1 << i);
1614 fws->fifo_credit[i] = *credits++;
1616 brcmf_fws_schedule_deq(fws);
1617 brcmf_fws_unlock(fws);
1618 return 0;
1621 static int brcmf_fws_notify_bcmc_credit_support(struct brcmf_if *ifp,
1622 const struct brcmf_event_msg *e,
1623 void *data)
1625 struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
1627 if (fws) {
1628 brcmf_fws_lock(fws);
1629 fws->bcmc_credit_check = true;
1630 brcmf_fws_unlock(fws);
1632 return 0;
1635 static void brcmf_rxreorder_get_skb_list(struct brcmf_ampdu_rx_reorder *rfi,
1636 u8 start, u8 end,
1637 struct sk_buff_head *skb_list)
1639 /* initialize return list */
1640 __skb_queue_head_init(skb_list);
1642 if (rfi->pend_pkts == 0) {
1643 brcmf_dbg(INFO, "no packets in reorder queue\n");
1644 return;
1647 do {
1648 if (rfi->pktslots[start]) {
1649 __skb_queue_tail(skb_list, rfi->pktslots[start]);
1650 rfi->pktslots[start] = NULL;
1652 start++;
1653 if (start > rfi->max_idx)
1654 start = 0;
1655 } while (start != end);
1656 rfi->pend_pkts -= skb_queue_len(skb_list);
1659 void brcmf_fws_rxreorder(struct brcmf_if *ifp, struct sk_buff *pkt)
1661 u8 *reorder_data;
1662 u8 flow_id, max_idx, cur_idx, exp_idx, end_idx;
1663 struct brcmf_ampdu_rx_reorder *rfi;
1664 struct sk_buff_head reorder_list;
1665 struct sk_buff *pnext;
1666 u8 flags;
1667 u32 buf_size;
1669 reorder_data = ((struct brcmf_skb_reorder_data *)pkt->cb)->reorder;
1670 flow_id = reorder_data[BRCMF_RXREORDER_FLOWID_OFFSET];
1671 flags = reorder_data[BRCMF_RXREORDER_FLAGS_OFFSET];
1673 /* validate flags and flow id */
1674 if (flags == 0xFF) {
1675 brcmf_err("invalid flags...so ignore this packet\n");
1676 brcmf_netif_rx(ifp, pkt);
1677 return;
1680 rfi = ifp->drvr->reorder_flows[flow_id];
1681 if (flags & BRCMF_RXREORDER_DEL_FLOW) {
1682 brcmf_dbg(INFO, "flow-%d: delete\n",
1683 flow_id);
1685 if (rfi == NULL) {
1686 brcmf_dbg(INFO, "received flags to cleanup, but no flow (%d) yet\n",
1687 flow_id);
1688 brcmf_netif_rx(ifp, pkt);
1689 return;
1692 brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, rfi->exp_idx,
1693 &reorder_list);
1694 /* add the last packet */
1695 __skb_queue_tail(&reorder_list, pkt);
1696 kfree(rfi);
1697 ifp->drvr->reorder_flows[flow_id] = NULL;
1698 goto netif_rx;
1700 /* from here on we need a flow reorder instance */
1701 if (rfi == NULL) {
1702 buf_size = sizeof(*rfi);
1703 max_idx = reorder_data[BRCMF_RXREORDER_MAXIDX_OFFSET];
1705 buf_size += (max_idx + 1) * sizeof(pkt);
1707 /* allocate space for flow reorder info */
1708 brcmf_dbg(INFO, "flow-%d: start, maxidx %d\n",
1709 flow_id, max_idx);
1710 rfi = kzalloc(buf_size, GFP_ATOMIC);
1711 if (rfi == NULL) {
1712 brcmf_err("failed to alloc buffer\n");
1713 brcmf_netif_rx(ifp, pkt);
1714 return;
1717 ifp->drvr->reorder_flows[flow_id] = rfi;
1718 rfi->pktslots = (struct sk_buff **)(rfi + 1);
1719 rfi->max_idx = max_idx;
1721 if (flags & BRCMF_RXREORDER_NEW_HOLE) {
1722 if (rfi->pend_pkts) {
1723 brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx,
1724 rfi->exp_idx,
1725 &reorder_list);
1726 WARN_ON(rfi->pend_pkts);
1727 } else {
1728 __skb_queue_head_init(&reorder_list);
1730 rfi->cur_idx = reorder_data[BRCMF_RXREORDER_CURIDX_OFFSET];
1731 rfi->exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
1732 rfi->max_idx = reorder_data[BRCMF_RXREORDER_MAXIDX_OFFSET];
1733 rfi->pktslots[rfi->cur_idx] = pkt;
1734 rfi->pend_pkts++;
1735 brcmf_dbg(DATA, "flow-%d: new hole %d (%d), pending %d\n",
1736 flow_id, rfi->cur_idx, rfi->exp_idx, rfi->pend_pkts);
1737 } else if (flags & BRCMF_RXREORDER_CURIDX_VALID) {
1738 cur_idx = reorder_data[BRCMF_RXREORDER_CURIDX_OFFSET];
1739 exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
1741 if ((exp_idx == rfi->exp_idx) && (cur_idx != rfi->exp_idx)) {
1742 /* still in the current hole */
1743 /* enqueue the current on the buffer chain */
1744 if (rfi->pktslots[cur_idx] != NULL) {
1745 brcmf_dbg(INFO, "HOLE: ERROR buffer pending..free it\n");
1746 brcmu_pkt_buf_free_skb(rfi->pktslots[cur_idx]);
1747 rfi->pktslots[cur_idx] = NULL;
1749 rfi->pktslots[cur_idx] = pkt;
1750 rfi->pend_pkts++;
1751 rfi->cur_idx = cur_idx;
1752 brcmf_dbg(DATA, "flow-%d: store pkt %d (%d), pending %d\n",
1753 flow_id, cur_idx, exp_idx, rfi->pend_pkts);
1755 /* can return now as there is no reorder
1756 * list to process.
1758 return;
1760 if (rfi->exp_idx == cur_idx) {
1761 if (rfi->pktslots[cur_idx] != NULL) {
1762 brcmf_dbg(INFO, "error buffer pending..free it\n");
1763 brcmu_pkt_buf_free_skb(rfi->pktslots[cur_idx]);
1764 rfi->pktslots[cur_idx] = NULL;
1766 rfi->pktslots[cur_idx] = pkt;
1767 rfi->pend_pkts++;
1769 /* got the expected one. flush from current to expected
1770 * and update expected
1772 brcmf_dbg(DATA, "flow-%d: expected %d (%d), pending %d\n",
1773 flow_id, cur_idx, exp_idx, rfi->pend_pkts);
1775 rfi->cur_idx = cur_idx;
1776 rfi->exp_idx = exp_idx;
1778 brcmf_rxreorder_get_skb_list(rfi, cur_idx, exp_idx,
1779 &reorder_list);
1780 brcmf_dbg(DATA, "flow-%d: freeing buffers %d, pending %d\n",
1781 flow_id, skb_queue_len(&reorder_list),
1782 rfi->pend_pkts);
1783 } else {
1784 u8 end_idx;
1786 brcmf_dbg(DATA, "flow-%d (0x%x): both moved, old %d/%d, new %d/%d\n",
1787 flow_id, flags, rfi->cur_idx, rfi->exp_idx,
1788 cur_idx, exp_idx);
1789 if (flags & BRCMF_RXREORDER_FLUSH_ALL)
1790 end_idx = rfi->exp_idx;
1791 else
1792 end_idx = exp_idx;
1794 /* flush pkts first */
1795 brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, end_idx,
1796 &reorder_list);
1798 if (exp_idx == ((cur_idx + 1) % (rfi->max_idx + 1))) {
1799 __skb_queue_tail(&reorder_list, pkt);
1800 } else {
1801 rfi->pktslots[cur_idx] = pkt;
1802 rfi->pend_pkts++;
1804 rfi->exp_idx = exp_idx;
1805 rfi->cur_idx = cur_idx;
1807 } else {
1808 /* explicity window move updating the expected index */
1809 exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
1811 brcmf_dbg(DATA, "flow-%d (0x%x): change expected: %d -> %d\n",
1812 flow_id, flags, rfi->exp_idx, exp_idx);
1813 if (flags & BRCMF_RXREORDER_FLUSH_ALL)
1814 end_idx = rfi->exp_idx;
1815 else
1816 end_idx = exp_idx;
1818 brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, end_idx,
1819 &reorder_list);
1820 __skb_queue_tail(&reorder_list, pkt);
1821 /* set the new expected idx */
1822 rfi->exp_idx = exp_idx;
1824 netif_rx:
1825 skb_queue_walk_safe(&reorder_list, pkt, pnext) {
1826 __skb_unlink(pkt, &reorder_list);
1827 brcmf_netif_rx(ifp, pkt);
1831 void brcmf_fws_hdrpull(struct brcmf_if *ifp, s16 siglen, struct sk_buff *skb)
1833 struct brcmf_skb_reorder_data *rd;
1834 struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
1835 u8 *signal_data;
1836 s16 data_len;
1837 u8 type;
1838 u8 len;
1839 u8 *data;
1840 s32 status;
1841 s32 err;
1843 brcmf_dbg(HDRS, "enter: ifidx %d, skblen %u, sig %d\n",
1844 ifp->ifidx, skb->len, siglen);
1846 WARN_ON(siglen > skb->len);
1848 if (!siglen)
1849 return;
1850 /* if flow control disabled, skip to packet data and leave */
1851 if ((!fws) || (!fws->fw_signals)) {
1852 skb_pull(skb, siglen);
1853 return;
1856 fws->stats.header_pulls++;
1857 data_len = siglen;
1858 signal_data = skb->data;
1860 status = BRCMF_FWS_RET_OK_NOSCHEDULE;
1861 while (data_len > 0) {
1862 /* extract tlv info */
1863 type = signal_data[0];
1865 /* FILLER type is actually not a TLV, but
1866 * a single byte that can be skipped.
1868 if (type == BRCMF_FWS_TYPE_FILLER) {
1869 signal_data += 1;
1870 data_len -= 1;
1871 continue;
1873 len = signal_data[1];
1874 data = signal_data + 2;
1876 brcmf_dbg(HDRS, "tlv type=%s (%d), len=%d (%d)\n",
1877 brcmf_fws_get_tlv_name(type), type, len,
1878 brcmf_fws_get_tlv_len(fws, type));
1880 /* abort parsing when length invalid */
1881 if (data_len < len + 2)
1882 break;
1884 if (len < brcmf_fws_get_tlv_len(fws, type))
1885 break;
1887 err = BRCMF_FWS_RET_OK_NOSCHEDULE;
1888 switch (type) {
1889 case BRCMF_FWS_TYPE_COMP_TXSTATUS:
1890 break;
1891 case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
1892 rd = (struct brcmf_skb_reorder_data *)skb->cb;
1893 rd->reorder = data;
1894 break;
1895 case BRCMF_FWS_TYPE_MACDESC_ADD:
1896 case BRCMF_FWS_TYPE_MACDESC_DEL:
1897 brcmf_fws_macdesc_indicate(fws, type, data);
1898 break;
1899 case BRCMF_FWS_TYPE_MAC_OPEN:
1900 case BRCMF_FWS_TYPE_MAC_CLOSE:
1901 err = brcmf_fws_macdesc_state_indicate(fws, type, data);
1902 break;
1903 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1904 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1905 err = brcmf_fws_interface_state_indicate(fws, type,
1906 data);
1907 break;
1908 case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
1909 case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
1910 err = brcmf_fws_request_indicate(fws, type, data);
1911 break;
1912 case BRCMF_FWS_TYPE_TXSTATUS:
1913 brcmf_fws_txstatus_indicate(fws, data);
1914 break;
1915 case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
1916 err = brcmf_fws_fifocreditback_indicate(fws, data);
1917 break;
1918 case BRCMF_FWS_TYPE_RSSI:
1919 brcmf_fws_rssi_indicate(fws, *data);
1920 break;
1921 case BRCMF_FWS_TYPE_TRANS_ID:
1922 brcmf_fws_dbg_seqnum_check(fws, data);
1923 break;
1924 case BRCMF_FWS_TYPE_PKTTAG:
1925 case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
1926 default:
1927 fws->stats.tlv_invalid_type++;
1928 break;
1930 if (err == BRCMF_FWS_RET_OK_SCHEDULE)
1931 status = BRCMF_FWS_RET_OK_SCHEDULE;
1932 signal_data += len + 2;
1933 data_len -= len + 2;
1936 if (data_len != 0)
1937 fws->stats.tlv_parse_failed++;
1939 if (status == BRCMF_FWS_RET_OK_SCHEDULE)
1940 brcmf_fws_schedule_deq(fws);
1942 /* signalling processing result does
1943 * not affect the actual ethernet packet.
1945 skb_pull(skb, siglen);
1947 /* this may be a signal-only packet
1949 if (skb->len == 0)
1950 fws->stats.header_only_pkt++;
1953 static u8 brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
1954 struct sk_buff *p)
1956 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1957 struct brcmf_fws_mac_descriptor *entry = skcb->mac;
1958 u8 flags;
1960 if (skcb->state != BRCMF_FWS_SKBSTATE_SUPPRESSED)
1961 brcmf_skb_htod_tag_set_field(p, GENERATION, entry->generation);
1962 flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
1963 if (brcmf_skb_if_flags_get_field(p, REQUESTED)) {
1965 * Indicate that this packet is being sent in response to an
1966 * explicit request from the firmware side.
1968 flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
1970 brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
1971 return brcmf_fws_hdrpush(fws, p);
1974 static void brcmf_fws_rollback_toq(struct brcmf_fws_info *fws,
1975 struct sk_buff *skb, int fifo)
1977 struct brcmf_fws_mac_descriptor *entry;
1978 struct sk_buff *pktout;
1979 int qidx, hslot;
1980 int rc = 0;
1982 entry = brcmf_skbcb(skb)->mac;
1983 if (entry->occupied) {
1984 qidx = 2 * fifo;
1985 if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
1986 qidx++;
1988 pktout = brcmu_pktq_penq_head(&entry->psq, qidx, skb);
1989 if (pktout == NULL) {
1990 brcmf_err("%s queue %d full\n", entry->name, qidx);
1991 rc = -ENOSPC;
1993 } else {
1994 brcmf_err("%s entry removed\n", entry->name);
1995 rc = -ENOENT;
1998 if (rc) {
1999 fws->stats.rollback_failed++;
2000 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
2001 brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED,
2002 hslot, 0, 0);
2003 } else {
2004 fws->stats.rollback_success++;
2005 brcmf_fws_return_credits(fws, fifo, 1);
2006 brcmf_fws_macdesc_return_req_credit(skb);
2010 static int brcmf_fws_borrow_credit(struct brcmf_fws_info *fws)
2012 int lender_ac;
2014 if (time_after(fws->borrow_defer_timestamp, jiffies)) {
2015 fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
2016 return -ENAVAIL;
2019 for (lender_ac = 0; lender_ac <= BRCMF_FWS_FIFO_AC_VO; lender_ac++) {
2020 if (fws->fifo_credit[lender_ac]) {
2021 fws->credits_borrowed[lender_ac]++;
2022 fws->fifo_credit[lender_ac]--;
2023 if (fws->fifo_credit[lender_ac] == 0)
2024 fws->fifo_credit_map &= ~(1 << lender_ac);
2025 fws->fifo_credit_map |= (1 << BRCMF_FWS_FIFO_AC_BE);
2026 brcmf_dbg(DATA, "borrow credit from: %d\n", lender_ac);
2027 return 0;
2030 fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
2031 return -ENAVAIL;
2034 static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
2035 struct sk_buff *skb)
2037 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
2038 struct brcmf_fws_mac_descriptor *entry;
2039 int rc;
2040 u8 ifidx;
2041 u8 data_offset;
2043 entry = skcb->mac;
2044 if (IS_ERR(entry))
2045 return PTR_ERR(entry);
2047 data_offset = brcmf_fws_precommit_skb(fws, fifo, skb);
2048 entry->transit_count++;
2049 if (entry->suppressed)
2050 entry->suppr_transit_count++;
2051 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
2052 brcmf_fws_unlock(fws);
2053 rc = brcmf_proto_txdata(fws->drvr, ifidx, data_offset, skb);
2054 brcmf_fws_lock(fws);
2055 brcmf_dbg(DATA, "%s flags %X htod %X bus_tx %d\n", entry->name,
2056 skcb->if_flags, skcb->htod, rc);
2057 if (rc < 0) {
2058 entry->transit_count--;
2059 if (entry->suppressed)
2060 entry->suppr_transit_count--;
2061 (void)brcmf_proto_hdrpull(fws->drvr, false, skb, NULL);
2062 goto rollback;
2065 fws->stats.pkt2bus++;
2066 fws->stats.send_pkts[fifo]++;
2067 if (brcmf_skb_if_flags_get_field(skb, REQUESTED))
2068 fws->stats.requested_sent[fifo]++;
2070 return rc;
2072 rollback:
2073 brcmf_fws_rollback_toq(fws, skb, fifo);
2074 return rc;
2077 static int brcmf_fws_assign_htod(struct brcmf_fws_info *fws, struct sk_buff *p,
2078 int fifo)
2080 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
2081 int rc, hslot;
2083 skcb->htod = 0;
2084 skcb->htod_seq = 0;
2085 hslot = brcmf_fws_hanger_get_free_slot(&fws->hanger);
2086 brcmf_skb_htod_tag_set_field(p, HSLOT, hslot);
2087 brcmf_skb_htod_tag_set_field(p, FREERUN, skcb->mac->seq[fifo]);
2088 brcmf_skb_htod_tag_set_field(p, FIFO, fifo);
2089 rc = brcmf_fws_hanger_pushpkt(&fws->hanger, p, hslot);
2090 if (!rc)
2091 skcb->mac->seq[fifo]++;
2092 else
2093 fws->stats.generic_error++;
2094 return rc;
2097 int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
2099 struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
2100 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
2101 struct ethhdr *eh = (struct ethhdr *)(skb->data);
2102 int fifo = BRCMF_FWS_FIFO_BCMC;
2103 bool multicast = is_multicast_ether_addr(eh->h_dest);
2104 int rc = 0;
2106 brcmf_dbg(DATA, "tx proto=0x%X\n", ntohs(eh->h_proto));
2108 /* set control buffer information */
2109 skcb->if_flags = 0;
2110 skcb->state = BRCMF_FWS_SKBSTATE_NEW;
2111 brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
2112 if (!multicast)
2113 fifo = brcmf_fws_prio2fifo[skb->priority];
2115 brcmf_fws_lock(fws);
2116 if (fifo != BRCMF_FWS_FIFO_AC_BE && fifo < BRCMF_FWS_FIFO_BCMC)
2117 fws->borrow_defer_timestamp = jiffies +
2118 BRCMF_FWS_BORROW_DEFER_PERIOD;
2120 skcb->mac = brcmf_fws_macdesc_find(fws, ifp, eh->h_dest);
2121 brcmf_dbg(DATA, "%s mac %pM multi %d fifo %d\n", skcb->mac->name,
2122 eh->h_dest, multicast, fifo);
2123 if (!brcmf_fws_assign_htod(fws, skb, fifo)) {
2124 brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
2125 brcmf_fws_schedule_deq(fws);
2126 } else {
2127 brcmf_err("drop skb: no hanger slot\n");
2128 brcmf_txfinalize(ifp, skb, false);
2129 rc = -ENOMEM;
2131 brcmf_fws_unlock(fws);
2133 return rc;
2136 void brcmf_fws_reset_interface(struct brcmf_if *ifp)
2138 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
2140 brcmf_dbg(TRACE, "enter: bsscfgidx=%d\n", ifp->bsscfgidx);
2141 if (!entry)
2142 return;
2144 brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
2147 void brcmf_fws_add_interface(struct brcmf_if *ifp)
2149 struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
2150 struct brcmf_fws_mac_descriptor *entry;
2152 if (!ifp->ndev || !brcmf_fws_queue_skbs(fws))
2153 return;
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);
2164 void brcmf_fws_del_interface(struct brcmf_if *ifp)
2166 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
2167 struct brcmf_fws_info *fws = drvr_to_fws(ifp->drvr);
2169 if (!entry)
2170 return;
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);
2182 static void brcmf_fws_dequeue_worker(struct work_struct *worker)
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;
2192 fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
2193 drvr = fws->drvr;
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;
2217 continue;
2219 while ((fws->fifo_credit[fifo]) || ((!fws->bcmc_credit_check) &&
2220 (fifo == BRCMF_FWS_FIFO_BCMC))) {
2221 skb = brcmf_fws_deq(fws, fifo);
2222 if (!skb)
2223 break;
2224 fws->fifo_credit[fifo]--;
2225 if (brcmf_fws_commit_skb(fws, fifo, skb))
2226 break;
2227 if (fws->bus_flow_blocked)
2228 break;
2230 if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
2231 (fws->fifo_credit[fifo] == 0) &&
2232 (!fws->bus_flow_blocked)) {
2233 while (brcmf_fws_borrow_credit(fws) == 0) {
2234 skb = brcmf_fws_deq(fws, fifo);
2235 if (!skb) {
2236 brcmf_fws_return_credits(fws, fifo, 1);
2237 break;
2239 if (brcmf_fws_commit_skb(fws, fifo, skb))
2240 break;
2241 if (fws->bus_flow_blocked)
2242 break;
2246 brcmf_fws_unlock(fws);
2249 #ifdef DEBUG
2250 static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
2252 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
2253 struct brcmf_fws_stats *fwstats = &(drvr_to_fws(bus_if->drvr)->stats);
2255 seq_printf(seq,
2256 "header_pulls: %u\n"
2257 "header_only_pkt: %u\n"
2258 "tlv_parse_failed: %u\n"
2259 "tlv_invalid_type: %u\n"
2260 "mac_update_fails: %u\n"
2261 "ps_update_fails: %u\n"
2262 "if_update_fails: %u\n"
2263 "pkt2bus: %u\n"
2264 "generic_error: %u\n"
2265 "rollback_success: %u\n"
2266 "rollback_failed: %u\n"
2267 "delayq_full: %u\n"
2268 "supprq_full: %u\n"
2269 "txs_indicate: %u\n"
2270 "txs_discard: %u\n"
2271 "txs_suppr_core: %u\n"
2272 "txs_suppr_ps: %u\n"
2273 "txs_tossed: %u\n"
2274 "txs_host_tossed: %u\n"
2275 "bus_flow_block: %u\n"
2276 "fws_flow_block: %u\n"
2277 "send_pkts: BK:%u BE:%u VO:%u VI:%u BCMC:%u\n"
2278 "requested_sent: BK:%u BE:%u VO:%u VI:%u BCMC:%u\n",
2279 fwstats->header_pulls,
2280 fwstats->header_only_pkt,
2281 fwstats->tlv_parse_failed,
2282 fwstats->tlv_invalid_type,
2283 fwstats->mac_update_failed,
2284 fwstats->mac_ps_update_failed,
2285 fwstats->if_update_failed,
2286 fwstats->pkt2bus,
2287 fwstats->generic_error,
2288 fwstats->rollback_success,
2289 fwstats->rollback_failed,
2290 fwstats->delayq_full_error,
2291 fwstats->supprq_full_error,
2292 fwstats->txs_indicate,
2293 fwstats->txs_discard,
2294 fwstats->txs_supp_core,
2295 fwstats->txs_supp_ps,
2296 fwstats->txs_tossed,
2297 fwstats->txs_host_tossed,
2298 fwstats->bus_flow_block,
2299 fwstats->fws_flow_block,
2300 fwstats->send_pkts[0], fwstats->send_pkts[1],
2301 fwstats->send_pkts[2], fwstats->send_pkts[3],
2302 fwstats->send_pkts[4],
2303 fwstats->requested_sent[0],
2304 fwstats->requested_sent[1],
2305 fwstats->requested_sent[2],
2306 fwstats->requested_sent[3],
2307 fwstats->requested_sent[4]);
2309 return 0;
2311 #else
2312 static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
2314 return 0;
2316 #endif
2318 struct brcmf_fws_info *brcmf_fws_attach(struct brcmf_pub *drvr)
2320 struct brcmf_fws_info *fws;
2321 struct brcmf_if *ifp;
2322 u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
2323 int rc;
2324 u32 mode;
2326 fws = kzalloc(sizeof(*fws), GFP_KERNEL);
2327 if (!fws) {
2328 rc = -ENOMEM;
2329 goto fail;
2332 spin_lock_init(&fws->spinlock);
2334 /* store drvr reference */
2335 fws->drvr = drvr;
2336 fws->fcmode = drvr->settings->fcmode;
2338 if ((drvr->bus_if->always_use_fws_queue == false) &&
2339 (fws->fcmode == BRCMF_FWS_FCMODE_NONE)) {
2340 fws->avoid_queueing = true;
2341 brcmf_dbg(INFO, "FWS queueing will be avoided\n");
2342 return fws;
2345 fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
2346 if (fws->fws_wq == NULL) {
2347 brcmf_err("workqueue creation failed\n");
2348 rc = -EBADF;
2349 goto fail;
2351 INIT_WORK(&fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
2353 /* enable firmware signalling if fcmode active */
2354 if (fws->fcmode != BRCMF_FWS_FCMODE_NONE)
2355 tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
2356 BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
2357 BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE |
2358 BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE;
2360 rc = brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
2361 brcmf_fws_notify_credit_map);
2362 if (rc < 0) {
2363 brcmf_err("register credit map handler failed\n");
2364 goto fail;
2366 rc = brcmf_fweh_register(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT,
2367 brcmf_fws_notify_bcmc_credit_support);
2368 if (rc < 0) {
2369 brcmf_err("register bcmc credit handler failed\n");
2370 brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
2371 goto fail;
2374 /* Setting the iovar may fail if feature is unsupported
2375 * so leave the rc as is so driver initialization can
2376 * continue. Set mode back to none indicating not enabled.
2378 fws->fw_signals = true;
2379 ifp = brcmf_get_ifp(drvr, 0);
2380 if (brcmf_fil_iovar_int_set(ifp, "tlv", tlv)) {
2381 brcmf_err("failed to set bdcv2 tlv signaling\n");
2382 fws->fcmode = BRCMF_FWS_FCMODE_NONE;
2383 fws->fw_signals = false;
2386 if (brcmf_fil_iovar_int_set(ifp, "ampdu_hostreorder", 1))
2387 brcmf_dbg(INFO, "enabling AMPDU host-reorder failed\n");
2389 /* Enable seq number reuse, if supported */
2390 if (brcmf_fil_iovar_int_get(ifp, "wlfc_mode", &mode) == 0) {
2391 if (BRCMF_FWS_MODE_GET_REUSESEQ(mode)) {
2392 mode = 0;
2393 BRCMF_FWS_MODE_SET_REUSESEQ(mode, 1);
2394 if (brcmf_fil_iovar_int_set(ifp,
2395 "wlfc_mode", mode) == 0) {
2396 BRCMF_FWS_MODE_SET_REUSESEQ(fws->mode, 1);
2401 brcmf_fws_hanger_init(&fws->hanger);
2402 brcmf_fws_macdesc_init(&fws->desc.other, NULL, 0);
2403 brcmf_fws_macdesc_set_name(fws, &fws->desc.other);
2404 brcmf_dbg(INFO, "added %s\n", fws->desc.other.name);
2405 brcmu_pktq_init(&fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
2406 BRCMF_FWS_PSQ_LEN);
2408 brcmf_dbg(INFO, "%s bdcv2 tlv signaling [%x]\n",
2409 fws->fw_signals ? "enabled" : "disabled", tlv);
2410 return fws;
2412 fail:
2413 brcmf_fws_detach_pre_delif(fws);
2414 brcmf_fws_detach_post_delif(fws);
2415 return ERR_PTR(rc);
2418 void brcmf_fws_detach_pre_delif(struct brcmf_fws_info *fws)
2420 if (!fws)
2421 return;
2422 if (fws->fws_wq) {
2423 destroy_workqueue(fws->fws_wq);
2424 fws->fws_wq = NULL;
2428 void brcmf_fws_detach_post_delif(struct brcmf_fws_info *fws)
2430 if (!fws)
2431 return;
2433 /* cleanup */
2434 brcmf_fws_lock(fws);
2435 brcmf_fws_cleanup(fws, -1);
2436 brcmf_fws_unlock(fws);
2438 /* free top structure */
2439 kfree(fws);
2442 void brcmf_fws_debugfs_create(struct brcmf_pub *drvr)
2444 /* create debugfs file for statistics */
2445 brcmf_debugfs_add_entry(drvr, "fws_stats",
2446 brcmf_debugfs_fws_stats_read);
2449 bool brcmf_fws_queue_skbs(struct brcmf_fws_info *fws)
2451 return !fws->avoid_queueing;
2454 bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
2456 if (!fws->creditmap_received)
2457 return false;
2459 return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
2462 void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
2464 u32 hslot;
2466 if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_TIM) {
2467 brcmu_pkt_buf_free_skb(skb);
2468 return;
2470 brcmf_fws_lock(fws);
2471 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
2472 brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED, hslot, 0, 0);
2473 brcmf_fws_unlock(fws);
2476 void brcmf_fws_bus_blocked(struct brcmf_pub *drvr, bool flow_blocked)
2478 struct brcmf_fws_info *fws = drvr_to_fws(drvr);
2479 struct brcmf_if *ifp;
2480 int i;
2482 if (fws->avoid_queueing) {
2483 for (i = 0; i < BRCMF_MAX_IFS; i++) {
2484 ifp = drvr->iflist[i];
2485 if (!ifp || !ifp->ndev)
2486 continue;
2487 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FLOW,
2488 flow_blocked);
2490 } else {
2491 fws->bus_flow_blocked = flow_blocked;
2492 if (!flow_blocked)
2493 brcmf_fws_schedule_deq(fws);
2494 else
2495 fws->stats.bus_flow_block++;