blob: 9dae8fcea2c5f4ab5e75819571d6bf8892905f4d [file] [log] [blame]
Arend van Spriel349e7102013-03-03 12:45:28 +01001/*
2 * Copyright (c) 2010 Broadcom Corporation
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16#include <linux/types.h>
Arend van Sprielc7f34a62013-04-03 12:40:39 +020017#include <linux/module.h>
Arend van Spriel349e7102013-03-03 12:45:28 +010018#include <linux/if_ether.h>
19#include <linux/spinlock.h>
20#include <linux/skbuff.h>
21#include <linux/netdevice.h>
Arend van Spriel3edc1cf2013-04-03 12:40:40 +020022#include <linux/etherdevice.h>
Arend van Spriel349e7102013-03-03 12:45:28 +010023#include <linux/err.h>
24#include <uapi/linux/nl80211.h>
Arend van Spriel3edc1cf2013-04-03 12:40:40 +020025#include <net/cfg80211.h>
Arend van Spriel349e7102013-03-03 12:45:28 +010026
27#include <brcmu_utils.h>
28#include <brcmu_wifi.h>
29#include "dhd.h"
Arend van Spriel3edc1cf2013-04-03 12:40:40 +020030#include "dhd_proto.h"
Arend van Spriel349e7102013-03-03 12:45:28 +010031#include "dhd_dbg.h"
Arend van Spriele2432b62013-04-03 12:40:38 +020032#include "dhd_bus.h"
Arend van Spriel349e7102013-03-03 12:45:28 +010033#include "fwil.h"
Arend van Spriela7965fb2013-04-11 17:08:37 +020034#include "fwil_types.h"
Arend van Spriel349e7102013-03-03 12:45:28 +010035#include "fweh.h"
36#include "fwsignal.h"
Arend van Spriela7965fb2013-04-11 17:08:37 +020037#include "p2p.h"
38#include "wl_cfg80211.h"
Arend van Spriel349e7102013-03-03 12:45:28 +010039
40/**
41 * DOC: Firmware Signalling
42 *
43 * Firmware can send signals to host and vice versa, which are passed in the
44 * data packets using TLV based header. This signalling layer is on top of the
45 * BDC bus protocol layer.
46 */
47
48/*
49 * single definition for firmware-driver flow control tlv's.
50 *
51 * each tlv is specified by BRCMF_FWS_TLV_DEF(name, ID, length).
52 * A length value 0 indicates variable length tlv.
53 */
54#define BRCMF_FWS_TLV_DEFLIST \
55 BRCMF_FWS_TLV_DEF(MAC_OPEN, 1, 1) \
56 BRCMF_FWS_TLV_DEF(MAC_CLOSE, 2, 1) \
57 BRCMF_FWS_TLV_DEF(MAC_REQUEST_CREDIT, 3, 2) \
58 BRCMF_FWS_TLV_DEF(TXSTATUS, 4, 4) \
59 BRCMF_FWS_TLV_DEF(PKTTAG, 5, 4) \
60 BRCMF_FWS_TLV_DEF(MACDESC_ADD, 6, 8) \
61 BRCMF_FWS_TLV_DEF(MACDESC_DEL, 7, 8) \
62 BRCMF_FWS_TLV_DEF(RSSI, 8, 1) \
63 BRCMF_FWS_TLV_DEF(INTERFACE_OPEN, 9, 1) \
64 BRCMF_FWS_TLV_DEF(INTERFACE_CLOSE, 10, 1) \
Arend van Spriel94607232013-04-03 12:40:46 +020065 BRCMF_FWS_TLV_DEF(FIFO_CREDITBACK, 11, 6) \
Arend van Spriel349e7102013-03-03 12:45:28 +010066 BRCMF_FWS_TLV_DEF(PENDING_TRAFFIC_BMP, 12, 2) \
67 BRCMF_FWS_TLV_DEF(MAC_REQUEST_PACKET, 13, 3) \
68 BRCMF_FWS_TLV_DEF(HOST_REORDER_RXPKTS, 14, 10) \
69 BRCMF_FWS_TLV_DEF(TRANS_ID, 18, 6) \
70 BRCMF_FWS_TLV_DEF(COMP_TXSTATUS, 19, 1) \
71 BRCMF_FWS_TLV_DEF(FILLER, 255, 0)
72
Arend van Spriel3edc1cf2013-04-03 12:40:40 +020073/*
Arend van Spriel349e7102013-03-03 12:45:28 +010074 * enum brcmf_fws_tlv_type - definition of tlv identifiers.
75 */
76#define BRCMF_FWS_TLV_DEF(name, id, len) \
77 BRCMF_FWS_TYPE_ ## name = id,
78enum brcmf_fws_tlv_type {
79 BRCMF_FWS_TLV_DEFLIST
80 BRCMF_FWS_TYPE_INVALID
81};
82#undef BRCMF_FWS_TLV_DEF
83
Arend van Spriel3edc1cf2013-04-03 12:40:40 +020084/*
85 * enum brcmf_fws_tlv_len - definition of tlv lengths.
86 */
87#define BRCMF_FWS_TLV_DEF(name, id, len) \
88 BRCMF_FWS_TYPE_ ## name ## _LEN = (len),
89enum brcmf_fws_tlv_len {
90 BRCMF_FWS_TLV_DEFLIST
91};
92#undef BRCMF_FWS_TLV_DEF
93
Arend van Spriel349e7102013-03-03 12:45:28 +010094#ifdef DEBUG
Arend van Spriel3edc1cf2013-04-03 12:40:40 +020095/*
Arend van Spriel349e7102013-03-03 12:45:28 +010096 * brcmf_fws_tlv_names - array of tlv names.
97 */
98#define BRCMF_FWS_TLV_DEF(name, id, len) \
99 { id, #name },
100static struct {
101 enum brcmf_fws_tlv_type id;
102 const char *name;
103} brcmf_fws_tlv_names[] = {
104 BRCMF_FWS_TLV_DEFLIST
105};
106#undef BRCMF_FWS_TLV_DEF
107
108static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
109{
110 int i;
111
112 for (i = 0; i < ARRAY_SIZE(brcmf_fws_tlv_names); i++)
113 if (brcmf_fws_tlv_names[i].id == id)
114 return brcmf_fws_tlv_names[i].name;
115
116 return "INVALID";
117}
118#else
119static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
120{
121 return "NODEBUG";
122}
123#endif /* DEBUG */
124
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200125/*
Arend van Spriel349e7102013-03-03 12:45:28 +0100126 * flags used to enable tlv signalling from firmware.
127 */
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200128#define BRCMF_FWS_FLAGS_RSSI_SIGNALS 0x0001
129#define BRCMF_FWS_FLAGS_XONXOFF_SIGNALS 0x0002
130#define BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS 0x0004
131#define BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE 0x0008
132#define BRCMF_FWS_FLAGS_PSQ_GENERATIONFSM_ENABLE 0x0010
133#define BRCMF_FWS_FLAGS_PSQ_ZERO_BUFFER_ENABLE 0x0020
134#define BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE 0x0040
Arend van Spriel349e7102013-03-03 12:45:28 +0100135
Arend van Spriel349e7102013-03-03 12:45:28 +0100136#define BRCMF_FWS_MAC_DESC_TABLE_SIZE 32
Arend van Spriel349e7102013-03-03 12:45:28 +0100137#define BRCMF_FWS_MAC_DESC_ID_INVALID 0xff
138
139#define BRCMF_FWS_HOSTIF_FLOWSTATE_OFF 0
140#define BRCMF_FWS_HOSTIF_FLOWSTATE_ON 1
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200141#define BRCMF_FWS_FLOWCONTROL_HIWATER 128
142#define BRCMF_FWS_FLOWCONTROL_LOWATER 64
Arend van Spriel349e7102013-03-03 12:45:28 +0100143
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200144#define BRCMF_FWS_PSQ_PREC_COUNT ((NL80211_NUM_ACS + 1) * 2)
145#define BRCMF_FWS_PSQ_LEN 256
146
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200147#define BRCMF_FWS_HTOD_FLAG_PKTFROMHOST 0x01
148#define BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED 0x02
149
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200150/**
Arend van Sprielebb93882013-04-03 12:40:35 +0200151 * enum brcmf_fws_skb_state - indicates processing state of skb.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200152 *
153 * @BRCMF_FWS_SKBSTATE_NEW: sk_buff is newly arrived in the driver.
154 * @BRCMF_FWS_SKBSTATE_DELAYED: sk_buff had to wait on queue.
155 * @BRCMF_FWS_SKBSTATE_SUPPRESSED: sk_buff has been suppressed by firmware.
Arend van Sprielebb93882013-04-03 12:40:35 +0200156 */
157enum brcmf_fws_skb_state {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200158 BRCMF_FWS_SKBSTATE_NEW,
159 BRCMF_FWS_SKBSTATE_DELAYED,
160 BRCMF_FWS_SKBSTATE_SUPPRESSED
Arend van Sprielebb93882013-04-03 12:40:35 +0200161};
162
163/**
164 * struct brcmf_skbuff_cb - control buffer associated with skbuff.
165 *
166 * @if_flags: holds interface index and packet related flags.
Arend van Sprielebb93882013-04-03 12:40:35 +0200167 * @htod: host to device packet identifier (used in PKTTAG tlv).
Arend van Sprielebb93882013-04-03 12:40:35 +0200168 * @state: transmit state of the packet.
169 * @mac: descriptor related to destination for this packet.
170 *
171 * This information is stored in control buffer struct sk_buff::cb, which
172 * provides 48 bytes of storage so this structure should not exceed that.
173 */
174struct brcmf_skbuff_cb {
175 u16 if_flags;
Arend van Sprielebb93882013-04-03 12:40:35 +0200176 u32 htod;
Arend van Sprielebb93882013-04-03 12:40:35 +0200177 enum brcmf_fws_skb_state state;
178 struct brcmf_fws_mac_descriptor *mac;
179};
180
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200181/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200182 * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
183 */
184#define brcmf_skbcb(skb) ((struct brcmf_skbuff_cb *)((skb)->cb))
185
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200186/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200187 * sk_buff control if flags
188 *
189 * b[11] - packet sent upon firmware request.
190 * b[10] - packet only contains signalling data.
191 * b[9] - packet is a tx packet.
192 * b[8] - packet uses FIFO credit (non-pspoll).
193 * b[7] - interface in AP mode.
194 * b[6:4] - AC FIFO number.
195 * b[3:0] - interface index.
196 */
197#define BRCMF_SKB_IF_FLAGS_REQUESTED_MASK 0x0800
198#define BRCMF_SKB_IF_FLAGS_REQUESTED_SHIFT 11
199#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_MASK 0x0400
200#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_SHIFT 10
201#define BRCMF_SKB_IF_FLAGS_TRANSMIT_MASK 0x0200
202#define BRCMF_SKB_IF_FLAGS_TRANSMIT_SHIFT 9
203#define BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK 0x0100
204#define BRCMF_SKB_IF_FLAGS_CREDITCHECK_SHIFT 8
205#define BRCMF_SKB_IF_FLAGS_IF_AP_MASK 0x0080
206#define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT 7
207#define BRCMF_SKB_IF_FLAGS_FIFO_MASK 0x0070
208#define BRCMF_SKB_IF_FLAGS_FIFO_SHIFT 4
209#define BRCMF_SKB_IF_FLAGS_INDEX_MASK 0x000f
210#define BRCMF_SKB_IF_FLAGS_INDEX_SHIFT 0
211
212#define brcmf_skb_if_flags_set_field(skb, field, value) \
213 brcmu_maskset16(&(brcmf_skbcb(skb)->if_flags), \
214 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
215 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT, (value))
216#define brcmf_skb_if_flags_get_field(skb, field) \
217 brcmu_maskget16(brcmf_skbcb(skb)->if_flags, \
218 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
219 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT)
220
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200221/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200222 * sk_buff control packet identifier
223 *
224 * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
225 *
226 * - Generated at the host (e.g. dhd)
227 * - Seen as a generic sequence number by firmware except for the flags field.
228 *
229 * Generation : b[31] => generation number for this packet [host->fw]
230 * OR, current generation number [fw->host]
231 * Flags : b[30:27] => command, status flags
232 * FIFO-AC : b[26:24] => AC-FIFO id
233 * h-slot : b[23:8] => hanger-slot
234 * freerun : b[7:0] => A free running counter
235 */
236#define BRCMF_SKB_HTOD_TAG_GENERATION_MASK 0x80000000
237#define BRCMF_SKB_HTOD_TAG_GENERATION_SHIFT 31
238#define BRCMF_SKB_HTOD_TAG_FLAGS_MASK 0x78000000
239#define BRCMF_SKB_HTOD_TAG_FLAGS_SHIFT 27
240#define BRCMF_SKB_HTOD_TAG_FIFO_MASK 0x07000000
241#define BRCMF_SKB_HTOD_TAG_FIFO_SHIFT 24
242#define BRCMF_SKB_HTOD_TAG_HSLOT_MASK 0x00ffff00
243#define BRCMF_SKB_HTOD_TAG_HSLOT_SHIFT 8
244#define BRCMF_SKB_HTOD_TAG_FREERUN_MASK 0x000000ff
245#define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT 0
246
247#define brcmf_skb_htod_tag_set_field(skb, field, value) \
Arend van Spriele2432b62013-04-03 12:40:38 +0200248 brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
Arend van Sprielebb93882013-04-03 12:40:35 +0200249 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
250 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
251#define brcmf_skb_htod_tag_get_field(skb, field) \
Arend van Spriele2432b62013-04-03 12:40:38 +0200252 brcmu_maskget32(brcmf_skbcb(skb)->htod, \
Arend van Sprielebb93882013-04-03 12:40:35 +0200253 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
254 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)
255
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200256#define BRCMF_FWS_TXSTAT_GENERATION_MASK 0x80000000
257#define BRCMF_FWS_TXSTAT_GENERATION_SHIFT 31
258#define BRCMF_FWS_TXSTAT_FLAGS_MASK 0x78000000
259#define BRCMF_FWS_TXSTAT_FLAGS_SHIFT 27
260#define BRCMF_FWS_TXSTAT_FIFO_MASK 0x07000000
261#define BRCMF_FWS_TXSTAT_FIFO_SHIFT 24
262#define BRCMF_FWS_TXSTAT_HSLOT_MASK 0x00FFFF00
263#define BRCMF_FWS_TXSTAT_HSLOT_SHIFT 8
264#define BRCMF_FWS_TXSTAT_PKTID_MASK 0x00FFFFFF
265#define BRCMF_FWS_TXSTAT_PKTID_SHIFT 0
266
267#define brcmf_txstatus_get_field(txs, field) \
268 brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
269 BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)
270
271/**
272 * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
273 *
274 * @BRCMF_FWS_FIFO_AC_BK: fifo for background traffic.
275 * @BRCMF_FWS_FIFO_AC_BE: fifo for best-effort traffic.
276 * @BRCMF_FWS_FIFO_AC_VI: fifo for video traffic.
277 * @BRCMF_FWS_FIFO_AC_VO: fifo for voice traffic.
278 * @BRCMF_FWS_FIFO_BCMC: fifo for broadcast/multicast (AP only).
279 * @BRCMF_FWS_FIFO_ATIM: fifo for ATIM (AP only).
280 * @BRCMF_FWS_FIFO_COUNT: number of fifos.
281 */
282enum brcmf_fws_fifo {
283 BRCMF_FWS_FIFO_AC_BK,
284 BRCMF_FWS_FIFO_AC_BE,
285 BRCMF_FWS_FIFO_AC_VI,
286 BRCMF_FWS_FIFO_AC_VO,
287 BRCMF_FWS_FIFO_BCMC,
288 BRCMF_FWS_FIFO_ATIM,
289 BRCMF_FWS_FIFO_COUNT
290};
291
292/**
293 * enum brcmf_fws_txstatus - txstatus flag values.
294 *
295 * @BRCMF_FWS_TXSTATUS_DISCARD:
296 * host is free to discard the packet.
297 * @BRCMF_FWS_TXSTATUS_CORE_SUPPRESS:
298 * 802.11 core suppressed the packet.
299 * @BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS:
300 * firmware suppress the packet as device is already in PS mode.
301 * @BRCMF_FWS_TXSTATUS_FW_TOSSED:
302 * firmware tossed the packet.
303 */
304enum brcmf_fws_txstatus {
305 BRCMF_FWS_TXSTATUS_DISCARD,
306 BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
307 BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
308 BRCMF_FWS_TXSTATUS_FW_TOSSED
309};
310
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200311enum brcmf_fws_fcmode {
312 BRCMF_FWS_FCMODE_NONE,
313 BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
314 BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
315};
316
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200317enum brcmf_fws_mac_desc_state {
318 BRCMF_FWS_STATE_OPEN = 1,
319 BRCMF_FWS_STATE_CLOSE
320};
321
Arend van Sprielebb93882013-04-03 12:40:35 +0200322/**
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200323 * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
324 *
325 * @occupied: slot is in use.
Arend van Sprielce814c12013-04-03 12:40:31 +0200326 * @mac_handle: handle for mac entry determined by firmware.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200327 * @interface_id: interface index.
328 * @state: current state.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200329 * @suppressed: mac entry is suppressed.
330 * @generation: generation bit.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200331 * @ac_bitmap: ac queue bitmap.
332 * @requested_credit: credits requested by firmware.
333 * @ea: ethernet address.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200334 * @seq: per-node free-running sequence.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200335 * @psq: power-save queue.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200336 * @transit_count: packet in transit to firmware.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200337 */
338struct brcmf_fws_mac_descriptor {
339 u8 occupied;
Arend van Sprielce814c12013-04-03 12:40:31 +0200340 u8 mac_handle;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200341 u8 interface_id;
342 u8 state;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200343 bool suppressed;
344 u8 generation;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200345 u8 ac_bitmap;
346 u8 requested_credit;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200347 u8 requested_packet;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200348 u8 ea[ETH_ALEN];
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200349 u8 seq[BRCMF_FWS_FIFO_COUNT];
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200350 struct pktq psq;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200351 int transit_count;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200352 int suppress_count;
353 int suppr_transit_count;
354 bool send_tim_signal;
355 u8 traffic_pending_bmp;
356 u8 traffic_lastreported_bmp;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200357};
358
Arend van Spriel69712802013-04-03 12:40:37 +0200359#define BRCMF_FWS_HANGER_MAXITEMS 1024
360
361/**
362 * enum brcmf_fws_hanger_item_state - state of hanger item.
363 *
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200364 * @BRCMF_FWS_HANGER_ITEM_STATE_FREE: item is free for use.
365 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE: item is in use.
366 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED: item was suppressed.
Arend van Spriel69712802013-04-03 12:40:37 +0200367 */
368enum brcmf_fws_hanger_item_state {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200369 BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
370 BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
371 BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
Arend van Spriel69712802013-04-03 12:40:37 +0200372};
373
374
375/**
376 * struct brcmf_fws_hanger_item - single entry for tx pending packet.
377 *
378 * @state: entry is either free or occupied.
379 * @gen: generation.
Arend van Spriel69712802013-04-03 12:40:37 +0200380 * @pkt: packet itself.
381 */
382struct brcmf_fws_hanger_item {
383 enum brcmf_fws_hanger_item_state state;
384 u8 gen;
Arend van Spriel69712802013-04-03 12:40:37 +0200385 struct sk_buff *pkt;
386};
387
388/**
389 * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
390 *
Arend van Spriel69712802013-04-03 12:40:37 +0200391 * @pushed: packets pushed to await txstatus.
392 * @popped: packets popped upon handling txstatus.
393 * @failed_to_push: packets that could not be pushed.
394 * @failed_to_pop: packets that could not be popped.
395 * @failed_slotfind: packets for which failed to find an entry.
396 * @slot_pos: last returned item index for a free entry.
397 * @items: array of hanger items.
398 */
399struct brcmf_fws_hanger {
400 u32 pushed;
401 u32 popped;
402 u32 failed_to_push;
403 u32 failed_to_pop;
404 u32 failed_slotfind;
405 u32 slot_pos;
406 struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
407};
408
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200409struct brcmf_fws_macdesc_table {
410 struct brcmf_fws_mac_descriptor nodes[BRCMF_FWS_MAC_DESC_TABLE_SIZE];
411 struct brcmf_fws_mac_descriptor iface[BRCMF_MAX_IFS];
412 struct brcmf_fws_mac_descriptor other;
413};
414
Arend van Spriel349e7102013-03-03 12:45:28 +0100415struct brcmf_fws_info {
416 struct brcmf_pub *drvr;
417 struct brcmf_fws_stats stats;
Arend van Spriel69712802013-04-03 12:40:37 +0200418 struct brcmf_fws_hanger hanger;
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200419 enum brcmf_fws_fcmode fcmode;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200420 struct brcmf_fws_macdesc_table desc;
421 struct workqueue_struct *fws_wq;
422 struct work_struct fws_dequeue_work;
423 u32 fifo_enqpkt[BRCMF_FWS_FIFO_COUNT];
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200424 int fifo_credit[BRCMF_FWS_FIFO_COUNT];
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200425 int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
426 u32 fifo_credit_map;
427 u32 fifo_delay_map;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200428};
429
430/*
431 * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
432 */
433static const int brcmf_fws_prio2fifo[] = {
434 BRCMF_FWS_FIFO_AC_BE,
435 BRCMF_FWS_FIFO_AC_BK,
436 BRCMF_FWS_FIFO_AC_BK,
437 BRCMF_FWS_FIFO_AC_BE,
438 BRCMF_FWS_FIFO_AC_VI,
439 BRCMF_FWS_FIFO_AC_VI,
440 BRCMF_FWS_FIFO_AC_VO,
441 BRCMF_FWS_FIFO_AC_VO
Arend van Spriel349e7102013-03-03 12:45:28 +0100442};
443
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200444static int fcmode;
445module_param(fcmode, int, S_IRUSR);
446MODULE_PARM_DESC(fcmode, "mode of firmware signalled flow control");
447
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200448#define BRCMF_FWS_TLV_DEF(name, id, len) \
449 case BRCMF_FWS_TYPE_ ## name: \
450 return len;
451
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200452/**
453 * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
454 *
455 * @fws: firmware-signalling information.
456 * @id: identifier of the TLV.
457 *
458 * Return: the specified length for the given TLV; Otherwise -EINVAL.
459 */
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200460static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
461 enum brcmf_fws_tlv_type id)
462{
463 switch (id) {
464 BRCMF_FWS_TLV_DEFLIST
465 default:
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200466 fws->stats.tlv_invalid_type++;
467 break;
468 }
469 return -EINVAL;
470}
471#undef BRCMF_FWS_TLV_DEF
472
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200473static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
474{
475 u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
476 return ifidx == *(int *)arg;
477}
478
479static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
480 int ifidx)
481{
482 bool (*matchfn)(struct sk_buff *, void *) = NULL;
483 struct sk_buff *skb;
484 int prec;
485
486 if (ifidx != -1)
487 matchfn = brcmf_fws_ifidx_match;
488 for (prec = 0; prec < q->num_prec; prec++) {
489 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
490 while (skb) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200491 brcmu_pkt_buf_free_skb(skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200492 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
493 }
494 }
495}
496
Arend van Spriel69712802013-04-03 12:40:37 +0200497static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
498{
499 int i;
500
501 brcmf_dbg(TRACE, "enter\n");
502 memset(hanger, 0, sizeof(*hanger));
503 for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200504 hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
Arend van Spriel69712802013-04-03 12:40:37 +0200505}
506
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200507static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
Arend van Spriel69712802013-04-03 12:40:37 +0200508{
509 u32 i;
510
511 brcmf_dbg(TRACE, "enter\n");
512 i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
513
514 while (i != h->slot_pos) {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200515 if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
Arend van Spriel69712802013-04-03 12:40:37 +0200516 h->slot_pos = i;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200517 goto done;
Arend van Spriel69712802013-04-03 12:40:37 +0200518 }
519 i++;
520 if (i == BRCMF_FWS_HANGER_MAXITEMS)
521 i = 0;
522 }
523 brcmf_err("all slots occupied\n");
524 h->failed_slotfind++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200525 i = BRCMF_FWS_HANGER_MAXITEMS;
526done:
527 brcmf_dbg(TRACE, "exit: %d\n", i);
528 return i;
Arend van Spriel69712802013-04-03 12:40:37 +0200529}
530
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200531static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
Arend van Spriel69712802013-04-03 12:40:37 +0200532 struct sk_buff *pkt, u32 slot_id)
533{
534 brcmf_dbg(TRACE, "enter\n");
535 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
536 return -ENOENT;
537
538 if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
539 brcmf_err("slot is not free\n");
540 h->failed_to_push++;
541 return -EINVAL;
542 }
543
544 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
545 h->items[slot_id].pkt = pkt;
Arend van Spriel69712802013-04-03 12:40:37 +0200546 h->pushed++;
547 return 0;
548}
549
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200550static int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
Arend van Spriel69712802013-04-03 12:40:37 +0200551 u32 slot_id, struct sk_buff **pktout,
552 bool remove_item)
553{
554 brcmf_dbg(TRACE, "enter\n");
555 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
556 return -ENOENT;
557
558 if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
559 brcmf_err("entry not in use\n");
560 h->failed_to_pop++;
561 return -EINVAL;
562 }
563
564 *pktout = h->items[slot_id].pkt;
565 if (remove_item) {
566 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
567 h->items[slot_id].pkt = NULL;
Arend van Spriel69712802013-04-03 12:40:37 +0200568 h->items[slot_id].gen = 0xff;
569 h->popped++;
570 }
571 return 0;
572}
573
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200574static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
Arend van Spriel69712802013-04-03 12:40:37 +0200575 u32 slot_id, u8 gen)
576{
577 brcmf_dbg(TRACE, "enter\n");
578
579 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
580 return -ENOENT;
581
582 h->items[slot_id].gen = gen;
583
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200584 if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_INUSE) {
Arend van Spriel69712802013-04-03 12:40:37 +0200585 brcmf_err("entry not in use\n");
586 return -EINVAL;
587 }
588
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200589 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
Arend van Spriel69712802013-04-03 12:40:37 +0200590 return 0;
591}
592
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200593static int brcmf_fws_hanger_get_genbit(struct brcmf_fws_hanger *hanger,
Arend van Spriel69712802013-04-03 12:40:37 +0200594 struct sk_buff *pkt, u32 slot_id,
595 int *gen)
596{
597 brcmf_dbg(TRACE, "enter\n");
598 *gen = 0xff;
599
600 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
601 return -ENOENT;
602
603 if (hanger->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
604 brcmf_err("slot not in use\n");
605 return -EINVAL;
606 }
607
608 *gen = hanger->items[slot_id].gen;
609 return 0;
610}
611
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200612static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
Arend van Spriel69712802013-04-03 12:40:37 +0200613 bool (*fn)(struct sk_buff *, void *),
614 int ifidx)
615{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200616 struct brcmf_fws_hanger *h = &fws->hanger;
Arend van Spriel69712802013-04-03 12:40:37 +0200617 struct sk_buff *skb;
618 int i;
619 enum brcmf_fws_hanger_item_state s;
620
621 brcmf_dbg(TRACE, "enter: ifidx=%d\n", ifidx);
622 for (i = 0; i < ARRAY_SIZE(h->items); i++) {
623 s = h->items[i].state;
624 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE ||
625 s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
626 skb = h->items[i].pkt;
627 if (fn == NULL || fn(skb, &ifidx)) {
628 /* suppress packets freed from psq */
629 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE)
630 brcmu_pkt_buf_free_skb(skb);
631 h->items[i].state =
632 BRCMF_FWS_HANGER_ITEM_STATE_FREE;
633 }
634 }
635 }
636}
637
Arend van Sprielce814c12013-04-03 12:40:31 +0200638static void brcmf_fws_init_mac_descriptor(struct brcmf_fws_mac_descriptor *desc,
639 u8 *addr, u8 ifidx)
640{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200641 brcmf_dbg(TRACE,
642 "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
Arend van Sprielce814c12013-04-03 12:40:31 +0200643 desc->occupied = 1;
644 desc->state = BRCMF_FWS_STATE_OPEN;
645 desc->requested_credit = 0;
646 /* depending on use may need ifp->bssidx instead */
647 desc->interface_id = ifidx;
648 desc->ac_bitmap = 0xff; /* update this when handling APSD */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200649 if (addr)
650 memcpy(&desc->ea[0], addr, ETH_ALEN);
Arend van Sprielce814c12013-04-03 12:40:31 +0200651}
652
653static
654void brcmf_fws_clear_mac_descriptor(struct brcmf_fws_mac_descriptor *desc)
655{
656 brcmf_dbg(TRACE,
657 "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
658 desc->occupied = 0;
659 desc->state = BRCMF_FWS_STATE_CLOSE;
660 desc->requested_credit = 0;
661}
662
663static struct brcmf_fws_mac_descriptor *
664brcmf_fws_mac_descriptor_lookup(struct brcmf_fws_info *fws, u8 *ea)
665{
666 struct brcmf_fws_mac_descriptor *entry;
667 int i;
668
669 brcmf_dbg(TRACE, "enter: ea=%pM\n", ea);
670 if (ea == NULL)
671 return ERR_PTR(-EINVAL);
672
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200673 entry = &fws->desc.nodes[0];
674 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
Arend van Sprielce814c12013-04-03 12:40:31 +0200675 if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
676 return entry;
677 entry++;
678 }
679
680 return ERR_PTR(-ENOENT);
681}
682
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200683static struct brcmf_fws_mac_descriptor*
Arend van Sprielf97a7f02013-04-11 13:28:58 +0200684brcmf_fws_find_mac_desc(struct brcmf_fws_info *fws, struct brcmf_if *ifp,
685 u8 *da)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200686{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200687 struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200688 bool multicast;
Arend van Spriela7965fb2013-04-11 17:08:37 +0200689 enum nl80211_iftype iftype;
Arend van Sprielf97a7f02013-04-11 13:28:58 +0200690
691 brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200692
693 multicast = is_multicast_ether_addr(da);
Arend van Spriela7965fb2013-04-11 17:08:37 +0200694 iftype = brcmf_cfg80211_get_iftype(ifp);
695
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200696 /* Multicast destination and P2P clients get the interface entry.
697 * STA gets the interface entry if there is no exact match. For
698 * example, TDLS destinations have their own entry.
699 */
700 entry = NULL;
Arend van Spriela7965fb2013-04-11 17:08:37 +0200701 if ((multicast || iftype == NL80211_IFTYPE_STATION ||
702 iftype == NL80211_IFTYPE_P2P_CLIENT) && ifp->fws_desc)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200703 entry = ifp->fws_desc;
704
Arend van Spriela7965fb2013-04-11 17:08:37 +0200705 if (entry != NULL && iftype != NL80211_IFTYPE_STATION)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200706 goto done;
707
708 entry = brcmf_fws_mac_descriptor_lookup(fws, da);
709 if (IS_ERR(entry))
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200710 entry = &fws->desc.other;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200711
712done:
713 brcmf_dbg(TRACE, "exit: entry=%p\n", entry);
714 return entry;
715}
716
Arend van Spriel9fc60222013-04-11 17:12:04 +0200717static bool brcmf_fws_mac_desc_closed(struct brcmf_fws_mac_descriptor *entry,
718 int fifo)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200719{
Arend van Spriel9fc60222013-04-11 17:12:04 +0200720 bool closed;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200721
Arend van Spriel9fc60222013-04-11 17:12:04 +0200722 /* an entry is closed when the state is closed and
723 * the firmware did not request anything.
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200724 */
Arend van Spriel9fc60222013-04-11 17:12:04 +0200725 closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
726 !entry->requested_credit && !entry->requested_packet;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200727
Arend van Spriel9fc60222013-04-11 17:12:04 +0200728 /* Or firmware does not allow traffic for given fifo */
729 return closed || !(entry->ac_bitmap & BIT(fifo));
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200730}
731
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200732static void brcmf_fws_mac_desc_cleanup(struct brcmf_fws_info *fws,
733 struct brcmf_fws_mac_descriptor *entry,
Arend van Sprielfba14002013-04-03 12:40:33 +0200734 int ifidx)
735{
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200736 brcmf_dbg(TRACE, "enter: entry=(ea=%pM, ifid=%d), ifidx=%d\n",
Arend van Sprielfba14002013-04-03 12:40:33 +0200737 entry->ea, entry->interface_id, ifidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200738 if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
739 brcmf_dbg(TRACE, "flush psq: ifidx=%d, qlen=%d\n",
Arend van Sprielfba14002013-04-03 12:40:33 +0200740 ifidx, entry->psq.len);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200741 brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
Arend van Sprielfba14002013-04-03 12:40:33 +0200742 entry->occupied = !!(entry->psq.len);
743 }
744}
745
Arend van Spriele2432b62013-04-03 12:40:38 +0200746static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
747 bool (*fn)(struct sk_buff *, void *),
748 int ifidx)
749{
750 struct brcmf_fws_hanger_item *hi;
751 struct pktq *txq;
752 struct sk_buff *skb;
753 int prec;
754 u32 hslot;
755
756 brcmf_dbg(TRACE, "enter: ifidx=%d\n", ifidx);
757 txq = brcmf_bus_gettxq(fws->drvr->bus_if);
758 if (IS_ERR(txq)) {
759 brcmf_dbg(TRACE, "no txq to clean up\n");
760 return;
761 }
762
763 for (prec = 0; prec < txq->num_prec; prec++) {
764 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
765 while (skb) {
766 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
767 hi = &fws->hanger.items[hslot];
768 WARN_ON(skb != hi->pkt);
769 hi->state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
770 brcmu_pkt_buf_free_skb(skb);
771 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
772 }
773 }
774}
775
Arend van Sprielfba14002013-04-03 12:40:33 +0200776static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
777{
778 int i;
779 struct brcmf_fws_mac_descriptor *table;
Arend van Spriela3e993c2013-04-03 12:40:36 +0200780 bool (*matchfn)(struct sk_buff *, void *) = NULL;
Arend van Sprielfba14002013-04-03 12:40:33 +0200781
782 brcmf_dbg(TRACE, "enter: ifidx=%d\n", ifidx);
783 if (fws == NULL)
784 return;
785
Arend van Spriela3e993c2013-04-03 12:40:36 +0200786 if (ifidx != -1)
787 matchfn = brcmf_fws_ifidx_match;
788
Arend van Sprielfba14002013-04-03 12:40:33 +0200789 /* cleanup individual nodes */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200790 table = &fws->desc.nodes[0];
791 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200792 brcmf_fws_mac_desc_cleanup(fws, &table[i], ifidx);
Arend van Sprielfba14002013-04-03 12:40:33 +0200793
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200794 brcmf_fws_mac_desc_cleanup(fws, &fws->desc.other, ifidx);
Arend van Spriele2432b62013-04-03 12:40:38 +0200795 brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200796 brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
797}
798
799static void brcmf_fws_tim_update(struct brcmf_fws_info *ctx,
800 struct brcmf_fws_mac_descriptor *entry,
801 int prec)
802{
803 brcmf_dbg(TRACE, "enter: ea=%pM\n", entry->ea);
804 if (entry->state == BRCMF_FWS_STATE_CLOSE) {
805 /* check delayedQ and suppressQ in one call using bitmap */
806 if (brcmu_pktq_mlen(&entry->psq, 3 << (prec * 2)) == 0)
807 entry->traffic_pending_bmp =
808 entry->traffic_pending_bmp & ~NBITVAL(prec);
809 else
810 entry->traffic_pending_bmp =
811 entry->traffic_pending_bmp | NBITVAL(prec);
812 }
813 /* request a TIM update to firmware at the next piggyback opportunity */
814 if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
815 entry->send_tim_signal = true;
816}
817
818static void
819brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
820 u8 if_id)
821{
822 struct brcmf_if *ifp = fws->drvr->iflist[if_id];
823
824 brcmf_dbg(TRACE,
825 "enter: bssidx=%d, ifidx=%d\n", ifp->bssidx, ifp->ifidx);
826 if (WARN_ON(!ifp))
827 return;
828
829 if ((ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
830 pq->len <= BRCMF_FWS_FLOWCONTROL_LOWATER)
831 brcmf_txflowblock_if(ifp,
832 BRCMF_NETIF_STOP_REASON_FWS_FC, false);
833 if (!(ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
834 pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER)
835 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
836 return;
Arend van Sprielfba14002013-04-03 12:40:33 +0200837}
838
Arend van Spriel349e7102013-03-03 12:45:28 +0100839static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
840{
841 brcmf_dbg(CTL, "rssi %d\n", rssi);
842 return 0;
843}
844
Arend van Sprielce814c12013-04-03 12:40:31 +0200845static
846int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
847{
848 struct brcmf_fws_mac_descriptor *entry, *existing;
849 u8 mac_handle;
850 u8 ifidx;
851 u8 *addr;
852
853 mac_handle = *data++;
854 ifidx = *data++;
855 addr = data;
856
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200857 entry = &fws->desc.nodes[mac_handle & 0x1F];
Arend van Sprielce814c12013-04-03 12:40:31 +0200858 if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
859 brcmf_dbg(TRACE, "deleting mac %pM idx %d\n", addr, ifidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200860 if (entry->occupied)
861 brcmf_fws_clear_mac_descriptor(entry);
862 else
Arend van Sprielce814c12013-04-03 12:40:31 +0200863 fws->stats.mac_update_failed++;
Arend van Sprielce814c12013-04-03 12:40:31 +0200864 return 0;
865 }
866
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200867 brcmf_dbg(TRACE,
868 "add mac %pM handle %u idx %d\n", addr, mac_handle, ifidx);
Arend van Sprielce814c12013-04-03 12:40:31 +0200869 existing = brcmf_fws_mac_descriptor_lookup(fws, addr);
870 if (IS_ERR(existing)) {
871 if (!entry->occupied) {
872 entry->mac_handle = mac_handle;
873 brcmf_fws_init_mac_descriptor(entry, addr, ifidx);
874 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
875 BRCMF_FWS_PSQ_LEN);
876 } else {
877 fws->stats.mac_update_failed++;
878 }
879 } else {
880 if (entry != existing) {
881 brcmf_dbg(TRACE, "relocate mac\n");
882 memcpy(entry, existing,
883 offsetof(struct brcmf_fws_mac_descriptor, psq));
884 entry->mac_handle = mac_handle;
885 brcmf_fws_clear_mac_descriptor(existing);
886 } else {
887 brcmf_dbg(TRACE, "use existing\n");
888 WARN_ON(entry->mac_handle != mac_handle);
889 /* TODO: what should we do here: continue, reinit, .. */
890 }
891 }
892 return 0;
893}
894
Arend van Spriel33753d42013-04-05 10:57:40 +0200895static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
896 u8 type, u8 *data)
897{
898 struct brcmf_fws_mac_descriptor *entry;
899 u8 mac_handle;
900 int i;
901
902 mac_handle = data[0];
903 entry = &fws->desc.nodes[mac_handle & 0x1F];
904 if (!entry->occupied) {
905 fws->stats.mac_ps_update_failed++;
906 return -ESRCH;
907 }
908
909 /* a state update should wipe old credits? */
910 entry->requested_credit = 0;
911 if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
912 entry->state = BRCMF_FWS_STATE_OPEN;
913 } else {
914 entry->state = BRCMF_FWS_STATE_CLOSE;
915 for (i = BRCMF_FWS_FIFO_AC_BE; i < NL80211_NUM_ACS; i++)
916 brcmf_fws_tim_update(fws, entry, i);
917 }
918 return 0;
919}
920
921static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
922 u8 type, u8 *data)
923{
924 struct brcmf_fws_mac_descriptor *entry;
925 u8 ifidx;
926 int ret;
927
928 ifidx = data[0];
929
930 brcmf_dbg(TRACE, "enter: ifidx=%d\n", ifidx);
931 if (ifidx >= BRCMF_MAX_IFS) {
932 ret = -ERANGE;
933 goto fail;
934 }
935
936 entry = &fws->desc.iface[ifidx];
937 if (!entry->occupied) {
938 ret = -ESRCH;
939 goto fail;
940 }
941
942 switch (type) {
943 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
944 entry->state = BRCMF_FWS_STATE_OPEN;
945 return 0;
946 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
947 entry->state = BRCMF_FWS_STATE_CLOSE;
948 return 0;
949 default:
950 ret = -EINVAL;
951 break;
952 }
953fail:
954 fws->stats.if_update_failed++;
955 return ret;
956}
957
Arend van Spriel5f38b682013-04-05 10:57:41 +0200958static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
959 u8 *data)
960{
961 struct brcmf_fws_mac_descriptor *entry;
962
963 entry = &fws->desc.nodes[data[1] & 0x1F];
964 if (!entry->occupied) {
965 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
966 fws->stats.credit_request_failed++;
967 else
968 fws->stats.packet_request_failed++;
969 return -ESRCH;
970 }
971
972 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
973 entry->requested_credit = data[0];
974 else
975 entry->requested_packet = data[0];
976
977 entry->ac_bitmap = data[2];
978 return 0;
979}
980
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200981static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
982 u8 fifo, u8 credits)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200983{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200984 if (!credits)
985 return;
986
987 fws->fifo_credit_map |= 1 << fifo;
988 fws->fifo_credit[fifo] += credits;
989}
990
991static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
992{
993 /* only schedule dequeue when there are credits for delayed traffic */
994 if (fws->fifo_credit_map & fws->fifo_delay_map)
995 queue_work(fws->fws_wq, &fws->fws_dequeue_work);
996}
997
998static void brcmf_skb_pick_up_credit(struct brcmf_fws_info *fws, int fifo,
999 struct sk_buff *p)
1000{
1001 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(p)->mac;
1002
1003 if (brcmf_skbcb(p)->if_flags & BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK) {
1004 if (fws->fcmode != BRCMF_FWS_FCMODE_IMPLIED_CREDIT)
1005 return;
1006 brcmf_fws_return_credits(fws, fifo, 1);
1007 } else {
1008 /*
1009 * if this packet did not count against FIFO credit, it
1010 * must have taken a requested_credit from the destination
1011 * entry (for pspoll etc.)
1012 */
1013 if (!brcmf_skb_if_flags_get_field(p, REQUESTED))
1014 entry->requested_credit++;
1015 }
1016 brcmf_fws_schedule_deq(fws);
1017}
1018
1019static int brcmf_fws_enq(struct brcmf_fws_info *fws,
1020 enum brcmf_fws_skb_state state, int fifo,
1021 struct sk_buff *p)
1022{
1023 int prec = 2 * fifo;
1024 u32 *qfull_stat = &fws->stats.delayq_full_error;
1025
1026 struct brcmf_fws_mac_descriptor *entry;
1027
1028 entry = brcmf_skbcb(p)->mac;
1029 if (entry == NULL) {
1030 brcmf_err("no mac descriptor found for skb %p\n", p);
1031 return -ENOENT;
1032 }
1033
1034 brcmf_dbg(TRACE, "enter: ea=%pM, qlen=%d\n", entry->ea, entry->psq.len);
1035 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1036 prec += 1;
1037 qfull_stat = &fws->stats.supprq_full_error;
1038 }
1039
1040 if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
1041 *qfull_stat += 1;
1042 return -ENFILE;
1043 }
1044
1045 /* increment total enqueued packet count */
1046 fws->fifo_delay_map |= 1 << fifo;
1047 fws->fifo_enqpkt[fifo]++;
1048
1049 /* update the sk_buff state */
1050 brcmf_skbcb(p)->state = state;
1051 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
1052 entry->suppress_count++;
1053
1054 /*
1055 * A packet has been pushed so update traffic
1056 * availability bitmap, if applicable
1057 */
1058 brcmf_fws_tim_update(fws, entry, fifo);
1059 brcmf_fws_flow_control_check(fws, &entry->psq,
1060 brcmf_skb_if_flags_get_field(p, INDEX));
1061 return 0;
1062}
1063
1064static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
1065{
1066 struct brcmf_fws_mac_descriptor *table;
1067 struct brcmf_fws_mac_descriptor *entry;
1068 struct sk_buff *p;
1069 int use_credit = 1;
1070 int num_nodes;
1071 int node_pos;
1072 int prec_out;
Arend van Spriel07529d22013-04-17 21:25:49 +02001073 int pmsk;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001074 int i;
1075
1076 table = (struct brcmf_fws_mac_descriptor *)&fws->desc;
1077 num_nodes = sizeof(fws->desc) / sizeof(struct brcmf_fws_mac_descriptor);
1078 node_pos = fws->deq_node_pos[fifo];
1079
1080 for (i = 0; i < num_nodes; i++) {
1081 entry = &table[(node_pos + i) % num_nodes];
Arend van Spriel9fc60222013-04-11 17:12:04 +02001082 if (!entry->occupied || brcmf_fws_mac_desc_closed(entry, fifo))
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001083 continue;
1084
1085 if (entry->suppressed)
1086 pmsk = 2;
Arend van Spriel07529d22013-04-17 21:25:49 +02001087 else
1088 pmsk = 3;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001089 p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
1090 if (p == NULL) {
1091 if (entry->suppressed) {
1092 if (entry->suppr_transit_count >
1093 entry->suppress_count)
1094 return NULL;
1095 entry->suppressed = false;
1096 p = brcmu_pktq_mdeq(&entry->psq,
1097 1 << (fifo * 2), &prec_out);
1098 }
1099 }
1100 if (p == NULL)
1101 continue;
1102
1103 /* did the packet come from suppress sub-queue? */
1104 if (entry->requested_credit > 0) {
1105 entry->requested_credit--;
1106 /*
1107 * if the packet was pulled out while destination is in
1108 * closed state but had a non-zero packets requested,
1109 * then this should not count against the FIFO credit.
1110 * That is due to the fact that the firmware will
1111 * most likely hold onto this packet until a suitable
1112 * time later to push it to the appropriate AC FIFO.
1113 */
1114 if (entry->state == BRCMF_FWS_STATE_CLOSE)
1115 use_credit = 0;
1116 } else if (entry->requested_packet > 0) {
1117 entry->requested_packet--;
1118 brcmf_skb_if_flags_set_field(p, REQUESTED, 1);
1119 if (entry->state == BRCMF_FWS_STATE_CLOSE)
1120 use_credit = 0;
1121 }
1122 brcmf_skb_if_flags_set_field(p, CREDITCHECK, use_credit);
1123
1124 /* move dequeue position to ensure fair round-robin */
1125 fws->deq_node_pos[fifo] = (node_pos + i + 1) % num_nodes;
1126 brcmf_fws_flow_control_check(fws, &entry->psq,
1127 brcmf_skb_if_flags_get_field(p,
1128 INDEX)
1129 );
1130 /*
1131 * A packet has been picked up, update traffic
1132 * availability bitmap, if applicable
1133 */
1134 brcmf_fws_tim_update(fws, entry, fifo);
1135
1136 /*
1137 * decrement total enqueued fifo packets and
1138 * clear delay bitmap if done.
1139 */
1140 fws->fifo_enqpkt[fifo]--;
1141 if (fws->fifo_enqpkt[fifo] == 0)
1142 fws->fifo_delay_map &= ~(1 << fifo);
1143 goto done;
1144 }
1145 p = NULL;
1146done:
1147 brcmf_dbg(TRACE, "exit: fifo %d skb %p\n", fifo, p);
1148 return p;
1149}
1150
1151static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
1152 struct sk_buff *skb, u32 genbit)
1153{
1154 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1155 u32 hslot;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001156 int ret;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001157
1158 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1159
1160 /* this packet was suppressed */
1161 if (!entry->suppressed || entry->generation != genbit) {
1162 entry->suppressed = true;
1163 entry->suppress_count = brcmu_pktq_mlen(&entry->psq,
1164 1 << (fifo * 2 + 1));
1165 entry->suppr_transit_count = entry->transit_count;
1166 }
1167
1168 entry->generation = genbit;
1169
1170 ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo, skb);
1171 if (ret != 0) {
1172 /* suppress q is full, drop this packet */
1173 brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1174 true);
1175 } else {
1176 /*
1177 * Mark suppressed to avoid a double free during
1178 * wlfc cleanup
1179 */
1180 brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot,
1181 genbit);
1182 entry->suppress_count++;
1183 }
1184
1185 return ret;
1186}
1187
1188static int
1189brcmf_fws_txstatus_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
1190 u32 genbit)
1191{
1192 u32 fifo;
1193 int ret;
1194 bool remove_from_hanger = true;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001195 struct sk_buff *skb;
1196 struct brcmf_fws_mac_descriptor *entry = NULL;
1197
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001198 brcmf_dbg(TRACE, "status: flags=0x%X, hslot=%d\n",
1199 flags, hslot);
1200
1201 if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
1202 fws->stats.txs_discard++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001203 else if (flags == BRCMF_FWS_TXSTATUS_CORE_SUPPRESS) {
1204 fws->stats.txs_supp_core++;
1205 remove_from_hanger = false;
1206 } else if (flags == BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS) {
1207 fws->stats.txs_supp_ps++;
1208 remove_from_hanger = false;
1209 } else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001210 fws->stats.txs_tossed++;
1211 else
1212 brcmf_err("unexpected txstatus\n");
1213
1214 ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001215 remove_from_hanger);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001216 if (ret != 0) {
1217 brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
1218 return ret;
1219 }
1220
1221 entry = brcmf_skbcb(skb)->mac;
1222 if (WARN_ON(!entry)) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001223 brcmu_pkt_buf_free_skb(skb);
1224 return -EINVAL;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001225 }
1226
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001227 /* pick up the implicit credit from this packet */
1228 fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
1229 brcmf_skb_pick_up_credit(fws, fifo, skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001230
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001231 if (!remove_from_hanger)
1232 ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb, genbit);
1233
1234 if (remove_from_hanger || ret) {
1235 entry->transit_count--;
1236 if (entry->suppressed)
1237 entry->suppr_transit_count--;
1238
1239 brcmf_txfinalize(fws->drvr, skb, true);
1240 }
1241 return 0;
1242}
1243
1244static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
1245 u8 *data)
1246{
1247 int i;
1248
1249 if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
1250 brcmf_dbg(INFO, "ignored\n");
1251 return 0;
1252 }
1253
1254 brcmf_dbg(TRACE, "enter: data %pM\n", data);
1255 for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
1256 brcmf_fws_return_credits(fws, i, data[i]);
1257
1258 brcmf_dbg(INFO, "map: credit %x delay %x\n", fws->fifo_credit_map,
1259 fws->fifo_delay_map);
1260 brcmf_fws_schedule_deq(fws);
1261 return 0;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001262}
1263
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001264static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
1265{
Arend van Spriela2ffc562013-04-05 10:57:39 +02001266 __le32 status_le;
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001267 u32 status;
1268 u32 hslot;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001269 u32 genbit;
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001270 u8 flags;
1271
1272 fws->stats.txs_indicate++;
Arend van Spriela2ffc562013-04-05 10:57:39 +02001273 memcpy(&status_le, data, sizeof(status_le));
1274 status = le32_to_cpu(status_le);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001275 flags = brcmf_txstatus_get_field(status, FLAGS);
1276 hslot = brcmf_txstatus_get_field(status, HSLOT);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001277 genbit = brcmf_txstatus_get_field(status, GENERATION);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001278
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001279 return brcmf_fws_txstatus_process(fws, flags, hslot, genbit);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001280}
1281
Arend van Spriel349e7102013-03-03 12:45:28 +01001282static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
1283{
1284 __le32 timestamp;
1285
1286 memcpy(&timestamp, &data[2], sizeof(timestamp));
1287 brcmf_dbg(INFO, "received: seq %d, timestamp %d\n", data[1],
1288 le32_to_cpu(timestamp));
1289 return 0;
1290}
1291
1292/* using macro so sparse checking does not complain
1293 * about locking imbalance.
1294 */
1295#define brcmf_fws_lock(drvr, flags) \
1296do { \
1297 flags = 0; \
1298 spin_lock_irqsave(&((drvr)->fws_spinlock), (flags)); \
1299} while (0)
1300
1301/* using macro so sparse checking does not complain
1302 * about locking imbalance.
1303 */
1304#define brcmf_fws_unlock(drvr, flags) \
1305 spin_unlock_irqrestore(&((drvr)->fws_spinlock), (flags))
1306
Arend van Spriel43fa6352013-04-03 12:40:32 +02001307static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
1308 const struct brcmf_event_msg *e,
1309 void *data)
1310{
1311 struct brcmf_fws_info *fws = ifp->drvr->fws;
1312 int i;
1313 ulong flags;
1314 u8 *credits = data;
1315
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001316 if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
1317 brcmf_err("event payload too small (%d)\n", e->datalen);
1318 return -EINVAL;
1319 }
1320
1321 brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001322 brcmf_fws_lock(ifp->drvr, flags);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001323 for (i = 0; i < ARRAY_SIZE(fws->fifo_credit); i++) {
1324 if (*credits)
1325 fws->fifo_credit_map |= 1 << i;
1326 else
1327 fws->fifo_credit_map &= ~(1 << i);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001328 fws->fifo_credit[i] = *credits++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001329 }
1330 brcmf_fws_schedule_deq(fws);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001331 brcmf_fws_unlock(ifp->drvr, flags);
1332 return 0;
1333}
1334
Arend van Spriel349e7102013-03-03 12:45:28 +01001335int brcmf_fws_hdrpull(struct brcmf_pub *drvr, int ifidx, s16 signal_len,
1336 struct sk_buff *skb)
1337{
1338 struct brcmf_fws_info *fws = drvr->fws;
1339 ulong flags;
1340 u8 *signal_data;
1341 s16 data_len;
1342 u8 type;
1343 u8 len;
1344 u8 *data;
1345
1346 brcmf_dbg(TRACE, "enter: ifidx %d, skblen %u, sig %d\n",
1347 ifidx, skb->len, signal_len);
1348
1349 WARN_ON(signal_len > skb->len);
1350
1351 /* if flow control disabled, skip to packet data and leave */
1352 if (!signal_len || !drvr->fw_signals) {
1353 skb_pull(skb, signal_len);
1354 return 0;
1355 }
1356
1357 /* lock during tlv parsing */
1358 brcmf_fws_lock(drvr, flags);
1359
1360 fws->stats.header_pulls++;
1361 data_len = signal_len;
1362 signal_data = skb->data;
1363
1364 while (data_len > 0) {
1365 /* extract tlv info */
1366 type = signal_data[0];
1367
1368 /* FILLER type is actually not a TLV, but
1369 * a single byte that can be skipped.
1370 */
1371 if (type == BRCMF_FWS_TYPE_FILLER) {
1372 signal_data += 1;
1373 data_len -= 1;
1374 continue;
1375 }
1376 len = signal_data[1];
1377 data = signal_data + 2;
1378
Arend van Spriel2af15582013-04-03 12:40:42 +02001379 brcmf_dbg(INFO, "tlv type=%d (%s), len=%d, data[0]=%d\n", type,
1380 brcmf_fws_get_tlv_name(type), len, *data);
1381
Arend van Spriel349e7102013-03-03 12:45:28 +01001382 /* abort parsing when length invalid */
1383 if (data_len < len + 2)
1384 break;
1385
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001386 if (len != brcmf_fws_get_tlv_len(fws, type))
1387 break;
1388
Arend van Spriel349e7102013-03-03 12:45:28 +01001389 switch (type) {
Arend van Spriel349e7102013-03-03 12:45:28 +01001390 case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001391 case BRCMF_FWS_TYPE_COMP_TXSTATUS:
Arend van Sprielce814c12013-04-03 12:40:31 +02001392 break;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001393 case BRCMF_FWS_TYPE_MACDESC_ADD:
1394 case BRCMF_FWS_TYPE_MACDESC_DEL:
Arend van Sprielce814c12013-04-03 12:40:31 +02001395 brcmf_fws_macdesc_indicate(fws, type, data);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001396 break;
Arend van Spriel33753d42013-04-05 10:57:40 +02001397 case BRCMF_FWS_TYPE_MAC_OPEN:
1398 case BRCMF_FWS_TYPE_MAC_CLOSE:
1399 brcmf_fws_macdesc_state_indicate(fws, type, data);
1400 break;
1401 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1402 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1403 brcmf_fws_interface_state_indicate(fws, type, data);
1404 break;
Arend van Spriel5f38b682013-04-05 10:57:41 +02001405 case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
1406 case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
1407 brcmf_fws_request_indicate(fws, type, data);
1408 break;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001409 case BRCMF_FWS_TYPE_TXSTATUS:
1410 brcmf_fws_txstatus_indicate(fws, data);
1411 break;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001412 case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
1413 brcmf_fws_fifocreditback_indicate(fws, data);
1414 break;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001415 case BRCMF_FWS_TYPE_RSSI:
1416 brcmf_fws_rssi_indicate(fws, *data);
Arend van Spriel349e7102013-03-03 12:45:28 +01001417 break;
1418 case BRCMF_FWS_TYPE_TRANS_ID:
Arend van Spriel349e7102013-03-03 12:45:28 +01001419 brcmf_fws_dbg_seqnum_check(fws, data);
1420 break;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001421 case BRCMF_FWS_TYPE_PKTTAG:
1422 case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
Arend van Spriel349e7102013-03-03 12:45:28 +01001423 default:
1424 fws->stats.tlv_invalid_type++;
1425 break;
1426 }
1427
1428 signal_data += len + 2;
1429 data_len -= len + 2;
1430 }
1431
1432 if (data_len != 0)
1433 fws->stats.tlv_parse_failed++;
1434
1435 /* signalling processing result does
1436 * not affect the actual ethernet packet.
1437 */
1438 skb_pull(skb, signal_len);
1439
1440 /* this may be a signal-only packet
1441 */
1442 if (skb->len == 0)
1443 fws->stats.header_only_pkt++;
1444
1445 brcmf_fws_unlock(drvr, flags);
1446 return 0;
1447}
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001448
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001449static int brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
1450{
1451 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1452 u8 *wlh;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001453 u16 data_offset = 0;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001454 u8 fillers;
1455 __le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);
1456
1457 brcmf_dbg(TRACE, "enter: ea=%pM, ifidx=%u, pkttag=0x%08X\n",
1458 entry->ea, entry->interface_id, le32_to_cpu(pkttag));
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001459 if (entry->send_tim_signal)
1460 data_offset += 2 + BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
1461
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001462 /* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001463 data_offset += 2 + BRCMF_FWS_TYPE_PKTTAG_LEN;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001464 fillers = round_up(data_offset, 4) - data_offset;
1465 data_offset += fillers;
1466
1467 skb_push(skb, data_offset);
1468 wlh = skb->data;
1469
1470 wlh[0] = BRCMF_FWS_TYPE_PKTTAG;
1471 wlh[1] = BRCMF_FWS_TYPE_PKTTAG_LEN;
1472 memcpy(&wlh[2], &pkttag, sizeof(pkttag));
1473 wlh += BRCMF_FWS_TYPE_PKTTAG_LEN + 2;
1474
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001475 if (entry->send_tim_signal) {
1476 entry->send_tim_signal = 0;
1477 wlh[0] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP;
1478 wlh[1] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
1479 wlh[2] = entry->mac_handle;
1480 wlh[3] = entry->traffic_pending_bmp;
1481 wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
1482 entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
1483 }
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001484 if (fillers)
1485 memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);
1486
1487 brcmf_proto_hdrpush(fws->drvr, brcmf_skb_if_flags_get_field(skb, INDEX),
1488 data_offset >> 2, skb);
1489 return 0;
1490}
1491
1492static int brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
1493 struct sk_buff *p)
1494{
1495 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1496 struct brcmf_fws_mac_descriptor *entry = skcb->mac;
1497 int rc = 0;
1498 bool header_needed;
1499 int hslot = BRCMF_FWS_HANGER_MAXITEMS;
1500 u8 free_ctr;
1501 u8 ifidx;
1502 u8 flags;
1503
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001504 header_needed = skcb->state != BRCMF_FWS_SKBSTATE_SUPPRESSED;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001505
1506 if (header_needed) {
1507 /* obtaining free slot may fail, but that will be caught
1508 * by the hanger push. This assures the packet has a BDC
1509 * header upon return.
1510 */
1511 hslot = brcmf_fws_hanger_get_free_slot(&fws->hanger);
1512 free_ctr = entry->seq[fifo];
1513 brcmf_skb_htod_tag_set_field(p, HSLOT, hslot);
1514 brcmf_skb_htod_tag_set_field(p, FREERUN, free_ctr);
1515 brcmf_skb_htod_tag_set_field(p, GENERATION, 1);
1516 entry->transit_count++;
1517 }
1518 brcmf_skb_if_flags_set_field(p, TRANSMIT, 1);
1519 brcmf_skb_htod_tag_set_field(p, FIFO, fifo);
1520
1521 flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
1522 if (!(skcb->if_flags & BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK)) {
1523 /*
1524 Indicate that this packet is being sent in response to an
1525 explicit request from the firmware side.
1526 */
1527 flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
1528 }
1529 brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
1530 if (header_needed) {
1531 brcmf_fws_hdrpush(fws, p);
1532 rc = brcmf_fws_hanger_pushpkt(&fws->hanger, p, hslot);
1533 if (rc)
1534 brcmf_err("hanger push failed: rc=%d\n", rc);
1535 } else {
1536 int gen;
1537
1538 /* remove old header */
1539 rc = brcmf_proto_hdrpull(fws->drvr, false, &ifidx, p);
1540 if (rc == 0) {
1541 hslot = brcmf_skb_htod_tag_get_field(p, HSLOT);
1542 brcmf_fws_hanger_get_genbit(&fws->hanger, p,
1543 hslot, &gen);
1544 brcmf_skb_htod_tag_set_field(p, GENERATION, gen);
1545
1546 /* push new header */
1547 brcmf_fws_hdrpush(fws, p);
1548 }
1549 }
1550
1551 return rc;
1552}
1553
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001554static int
1555brcmf_fws_rollback_toq(struct brcmf_fws_info *fws, struct sk_buff *skb)
1556{
1557 /*
1558 put the packet back to the head of queue
1559
1560 - suppressed packet goes back to suppress sub-queue
1561 - pull out the header, if new or delayed packet
1562
1563 Note: hslot is used only when header removal is done.
1564 */
1565 struct brcmf_fws_mac_descriptor *entry;
1566 enum brcmf_fws_skb_state state;
1567 struct sk_buff *pktout;
1568 int rc = 0;
1569 int fifo;
1570 int hslot;
1571 u8 ifidx;
1572
1573 fifo = brcmf_skb_if_flags_get_field(skb, FIFO);
1574 state = brcmf_skbcb(skb)->state;
1575 entry = brcmf_skbcb(skb)->mac;
1576
1577 if (entry != NULL) {
1578 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1579 /* wl-header is saved for suppressed packets */
1580 pktout = brcmu_pktq_penq_head(&entry->psq, 2 * fifo + 1,
1581 skb);
1582 if (pktout == NULL) {
1583 brcmf_err("suppress queue full\n");
1584 rc = -ENOSPC;
1585 }
1586 } else {
1587 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1588
1589 /* remove header first */
1590 rc = brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb);
1591 if (rc) {
1592 brcmf_err("header removal failed\n");
1593 /* free the hanger slot */
1594 brcmf_fws_hanger_poppkt(&fws->hanger, hslot,
1595 &pktout, true);
1596 brcmf_txfinalize(fws->drvr, skb, false);
1597 rc = -EINVAL;
1598 goto fail;
1599 }
1600
1601 /* delay-q packets are going to delay-q */
1602 pktout = brcmu_pktq_penq_head(&entry->psq,
1603 2 * fifo, skb);
1604 if (pktout == NULL) {
1605 brcmf_err("delay queue full\n");
1606 rc = -ENOSPC;
1607 }
1608
1609 /* free the hanger slot */
1610 brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &pktout,
1611 true);
1612
1613 /* decrement sequence count */
1614 entry->seq[fifo]--;
1615 }
1616 /*
1617 if this packet did not count against FIFO credit, it must have
1618 taken a requested_credit from the firmware (for pspoll etc.)
1619 */
1620 if (!(brcmf_skbcb(skb)->if_flags &
1621 BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK))
1622 entry->requested_credit++;
1623 } else {
1624 brcmf_err("no mac entry linked\n");
1625 rc = -ENOENT;
1626 }
1627
1628
1629fail:
1630 if (rc)
1631 fws->stats.rollback_failed++;
1632 else
1633 fws->stats.rollback_success++;
1634 return rc;
1635}
1636
1637static int brcmf_fws_consume_credit(struct brcmf_fws_info *fws, int fifo,
1638 struct sk_buff *skb)
1639{
1640 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1641 int *credit = &fws->fifo_credit[fifo];
1642 int use_credit = 1;
1643
1644 brcmf_dbg(TRACE, "enter: ac=%d, credits=%d\n", fifo, *credit);
1645
1646 if (entry->requested_credit > 0) {
1647 /*
1648 * if the packet was pulled out while destination is in
1649 * closed state but had a non-zero packets requested,
1650 * then this should not count against the FIFO credit.
1651 * That is due to the fact that the firmware will
1652 * most likely hold onto this packet until a suitable
1653 * time later to push it to the appropriate AC FIFO.
1654 */
1655 entry->requested_credit--;
1656 if (entry->state == BRCMF_FWS_STATE_CLOSE)
1657 use_credit = 0;
1658 } else if (entry->requested_packet > 0) {
1659 entry->requested_packet--;
1660 brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1661 if (entry->state == BRCMF_FWS_STATE_CLOSE)
1662 use_credit = 0;
1663 }
1664 brcmf_skb_if_flags_set_field(skb, CREDITCHECK, use_credit);
1665 if (!use_credit) {
1666 brcmf_dbg(TRACE, "exit: no creditcheck set\n");
1667 return 0;
1668 }
1669
1670 if (!(*credit)) {
1671 brcmf_dbg(TRACE, "exit: credits depleted\n");
1672 return -ENAVAIL;
1673 }
1674 (*credit)--;
1675 if (!(*credit))
1676 fws->fifo_credit_map &= ~(1 << fifo);
1677 brcmf_dbg(TRACE, "exit: ac=%d, credits=%d\n", fifo, *credit);
1678 return 0;
1679}
1680
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001681static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
1682 struct sk_buff *skb)
1683{
1684 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1685 struct brcmf_fws_mac_descriptor *entry;
1686 struct brcmf_bus *bus = fws->drvr->bus_if;
1687 int rc;
1688
1689 entry = skcb->mac;
1690 if (IS_ERR(entry))
1691 return PTR_ERR(entry);
1692
1693 rc = brcmf_fws_precommit_skb(fws, fifo, skb);
1694 if (rc < 0) {
1695 fws->stats.generic_error++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001696 goto rollback;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001697 }
1698
1699 rc = brcmf_bus_txdata(bus, skb);
1700 if (rc < 0)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001701 goto rollback;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001702
1703 entry->seq[fifo]++;
1704 fws->stats.pkt2bus++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001705 if (brcmf_skbcb(skb)->if_flags & BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK) {
1706 fws->stats.send_pkts[fifo]++;
1707 fws->stats.fifo_credits_sent[fifo]++;
1708 }
1709
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001710 return rc;
1711
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001712rollback:
1713 rc = brcmf_fws_rollback_toq(fws, skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001714 return rc;
1715}
1716
1717int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
1718{
1719 struct brcmf_pub *drvr = ifp->drvr;
1720 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1721 struct ethhdr *eh = (struct ethhdr *)(skb->data);
1722 ulong flags;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001723 int fifo = BRCMF_FWS_FIFO_BCMC;
1724 bool multicast = is_multicast_ether_addr(eh->h_dest);
1725
1726 /* determine the priority */
1727 if (!skb->priority)
1728 skb->priority = cfg80211_classify8021d(skb);
1729
1730 drvr->tx_multicast += !!multicast;
1731 if (ntohs(eh->h_proto) == ETH_P_PAE)
1732 atomic_inc(&ifp->pend_8021x_cnt);
1733
1734 if (!brcmf_fws_fc_active(drvr->fws)) {
1735 /* If the protocol uses a data header, apply it */
Arend van Spriel31ed07d2013-04-11 13:29:00 +02001736 brcmf_proto_hdrpush(drvr, ifp->ifidx, 0, skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001737
1738 /* Use bus module to send data frame */
1739 return brcmf_bus_txdata(drvr->bus_if, skb);
1740 }
1741
1742 /* set control buffer information */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001743 skcb->if_flags = 0;
Arend van Sprielf97a7f02013-04-11 13:28:58 +02001744 skcb->mac = brcmf_fws_find_mac_desc(drvr->fws, ifp, eh->h_dest);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001745 skcb->state = BRCMF_FWS_SKBSTATE_NEW;
Arend van Spriel31ed07d2013-04-11 13:29:00 +02001746 brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001747 if (!multicast)
1748 fifo = brcmf_fws_prio2fifo[skb->priority];
1749 brcmf_skb_if_flags_set_field(skb, FIFO, fifo);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001750
1751 brcmf_dbg(TRACE, "ea=%pM, multi=%d, fifo=%d\n", eh->h_dest,
1752 multicast, fifo);
1753
1754 brcmf_fws_lock(drvr, flags);
Arend van Spriel9fc60222013-04-11 17:12:04 +02001755 if (skcb->mac->suppressed ||
1756 brcmf_fws_mac_desc_closed(skcb->mac, fifo) ||
1757 brcmu_pktq_mlen(&skcb->mac->psq, 3 << (fifo * 2)) ||
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001758 (!multicast &&
1759 brcmf_fws_consume_credit(drvr->fws, fifo, skb) < 0)) {
1760 /* enqueue the packet in delayQ */
1761 drvr->fws->fifo_delay_map |= 1 << fifo;
1762 brcmf_fws_enq(drvr->fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
1763 } else {
1764 brcmf_fws_commit_skb(drvr->fws, fifo, skb);
1765 }
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001766 brcmf_fws_unlock(drvr, flags);
1767 return 0;
1768}
1769
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001770void brcmf_fws_reset_interface(struct brcmf_if *ifp)
1771{
1772 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1773
1774 brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
1775 if (!entry)
1776 return;
1777
Arend van Sprielce814c12013-04-03 12:40:31 +02001778 brcmf_fws_init_mac_descriptor(entry, ifp->mac_addr, ifp->ifidx);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001779}
1780
1781void brcmf_fws_add_interface(struct brcmf_if *ifp)
1782{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001783 struct brcmf_fws_info *fws = ifp->drvr->fws;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001784 struct brcmf_fws_mac_descriptor *entry;
1785
1786 brcmf_dbg(TRACE, "enter: idx=%d, mac=%pM\n",
1787 ifp->bssidx, ifp->mac_addr);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02001788 if (!ifp->ndev || !ifp->drvr->fw_signals)
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001789 return;
1790
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001791 entry = &fws->desc.iface[ifp->ifidx];
1792 ifp->fws_desc = entry;
1793 brcmf_fws_init_mac_descriptor(entry, ifp->mac_addr, ifp->ifidx);
1794 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
1795 BRCMF_FWS_PSQ_LEN);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001796}
1797
1798void brcmf_fws_del_interface(struct brcmf_if *ifp)
1799{
1800 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1801
1802 brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
1803 if (!entry)
1804 return;
1805
1806 ifp->fws_desc = NULL;
Arend van Sprielce814c12013-04-03 12:40:31 +02001807 brcmf_fws_clear_mac_descriptor(entry);
Arend van Spriela3e993c2013-04-03 12:40:36 +02001808 brcmf_fws_cleanup(ifp->drvr->fws, ifp->ifidx);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001809}
1810
1811static void brcmf_fws_dequeue_worker(struct work_struct *worker)
1812{
1813 struct brcmf_fws_info *fws;
1814 struct sk_buff *skb;
1815 ulong flags;
1816 int fifo;
1817 int credit;
1818
1819 fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
1820
1821 brcmf_dbg(TRACE, "enter: fws=%p\n", fws);
1822 brcmf_fws_lock(fws->drvr, flags);
1823 for (fifo = NL80211_NUM_ACS; fifo >= 0; fifo--) {
1824 brcmf_dbg(TRACE, "fifo %d credit %d\n", fifo,
1825 fws->fifo_credit[fifo]);
1826 for (credit = 0; credit < fws->fifo_credit[fifo]; /* nop */) {
1827 skb = brcmf_fws_deq(fws, fifo);
1828 if (!skb)
1829 break;
1830 if (!brcmf_fws_commit_skb(fws, fifo, skb) &&
1831 brcmf_skbcb(skb)->if_flags &
1832 BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK)
1833 credit++;
1834 }
1835 fws->fifo_credit[fifo] -= credit;
1836 }
1837 brcmf_fws_unlock(fws->drvr, flags);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001838}
Arend van Sprielc7f34a62013-04-03 12:40:39 +02001839
Arend van Sprield9337412013-04-03 12:40:47 +02001840int brcmf_fws_init(struct brcmf_pub *drvr)
1841{
Arend van Spriel290fb762013-04-03 12:40:49 +02001842 u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
Arend van Sprield9337412013-04-03 12:40:47 +02001843 int rc;
1844
Arend van Spriel47829f42013-04-03 12:40:48 +02001845 if (!drvr->fw_signals)
1846 return 0;
1847
Arend van Sprield9337412013-04-03 12:40:47 +02001848 spin_lock_init(&drvr->fws_spinlock);
1849
1850 drvr->fws = kzalloc(sizeof(*(drvr->fws)), GFP_KERNEL);
1851 if (!drvr->fws) {
1852 rc = -ENOMEM;
1853 goto fail;
1854 }
1855
1856 /* set linkage back */
1857 drvr->fws->drvr = drvr;
1858 drvr->fws->fcmode = fcmode;
1859
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001860 drvr->fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
1861 if (drvr->fws->fws_wq == NULL) {
1862 brcmf_err("workqueue creation failed\n");
1863 rc = -EBADF;
1864 goto fail;
1865 }
1866 INIT_WORK(&drvr->fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
1867
Arend van Spriel290fb762013-04-03 12:40:49 +02001868 /* enable firmware signalling if fcmode active */
1869 if (drvr->fws->fcmode != BRCMF_FWS_FCMODE_NONE)
1870 tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
Arend van Spriel33753d42013-04-05 10:57:40 +02001871 BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
1872 BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE;
Arend van Spriel290fb762013-04-03 12:40:49 +02001873
Arend van Sprield9337412013-04-03 12:40:47 +02001874 rc = brcmf_fil_iovar_int_set(drvr->iflist[0], "tlv", tlv);
1875 if (rc < 0) {
1876 brcmf_err("failed to set bdcv2 tlv signaling\n");
1877 goto fail;
1878 }
1879
1880 if (brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
1881 brcmf_fws_notify_credit_map)) {
1882 brcmf_err("register credit map handler failed\n");
1883 goto fail;
1884 }
1885
1886 brcmf_fws_hanger_init(&drvr->fws->hanger);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001887 brcmf_fws_init_mac_descriptor(&drvr->fws->desc.other, NULL, 0);
1888 brcmu_pktq_init(&drvr->fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
1889 BRCMF_FWS_PSQ_LEN);
Arend van Sprield9337412013-04-03 12:40:47 +02001890
1891 /* create debugfs file for statistics */
1892 brcmf_debugfs_create_fws_stats(drvr, &drvr->fws->stats);
1893
1894 /* TODO: remove upon feature delivery */
1895 brcmf_err("%s bdcv2 tlv signaling [%x]\n",
1896 drvr->fw_signals ? "enabled" : "disabled", tlv);
1897 return 0;
1898
1899fail:
1900 /* disable flow control entirely */
1901 drvr->fw_signals = false;
1902 brcmf_fws_deinit(drvr);
1903 return rc;
1904}
1905
1906void brcmf_fws_deinit(struct brcmf_pub *drvr)
1907{
1908 struct brcmf_fws_info *fws = drvr->fws;
1909 ulong flags;
1910
Arend van Spriel47829f42013-04-03 12:40:48 +02001911 if (!fws)
1912 return;
1913
Arend van Sprield9337412013-04-03 12:40:47 +02001914 /* cleanup */
1915 brcmf_fws_lock(drvr, flags);
1916 brcmf_fws_cleanup(fws, -1);
1917 drvr->fws = NULL;
1918 brcmf_fws_unlock(drvr, flags);
1919
1920 /* free top structure */
1921 kfree(fws);
1922}
1923
Arend van Sprielc7f34a62013-04-03 12:40:39 +02001924bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
1925{
1926 if (!fws)
1927 return false;
1928
1929 brcmf_dbg(TRACE, "enter: mode=%d\n", fws->fcmode);
1930 return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
1931}
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001932
1933void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
1934{
1935 ulong flags;
1936
1937 brcmf_fws_lock(fws->drvr, flags);
1938 brcmf_fws_txstatus_process(fws, BRCMF_FWS_TXSTATUS_FW_TOSSED,
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001939 brcmf_skb_htod_tag_get_field(skb, HSLOT), 0);
1940 /* the packet never reached firmware so reclaim credit */
1941 if (fws->fcmode == BRCMF_FWS_FCMODE_EXPLICIT_CREDIT &&
1942 brcmf_skbcb(skb)->if_flags & BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK) {
1943 brcmf_fws_return_credits(fws,
1944 brcmf_skb_htod_tag_get_field(skb,
1945 FIFO),
1946 1);
1947 brcmf_fws_schedule_deq(fws);
1948 }
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001949 brcmf_fws_unlock(fws->drvr, flags);
1950}