blob: 66cdc45add4e9c7851c549eed417fafcbb073a43 [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 Spriela5e9d802013-04-17 21:25:54 +0200150#define BRCMF_FWS_RET_OK_NOSCHEDULE 0
151#define BRCMF_FWS_RET_OK_SCHEDULE 1
152
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200153/**
Arend van Sprielebb93882013-04-03 12:40:35 +0200154 * enum brcmf_fws_skb_state - indicates processing state of skb.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200155 *
156 * @BRCMF_FWS_SKBSTATE_NEW: sk_buff is newly arrived in the driver.
157 * @BRCMF_FWS_SKBSTATE_DELAYED: sk_buff had to wait on queue.
158 * @BRCMF_FWS_SKBSTATE_SUPPRESSED: sk_buff has been suppressed by firmware.
Arend van Sprielebb93882013-04-03 12:40:35 +0200159 */
160enum brcmf_fws_skb_state {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200161 BRCMF_FWS_SKBSTATE_NEW,
162 BRCMF_FWS_SKBSTATE_DELAYED,
163 BRCMF_FWS_SKBSTATE_SUPPRESSED
Arend van Sprielebb93882013-04-03 12:40:35 +0200164};
165
166/**
167 * struct brcmf_skbuff_cb - control buffer associated with skbuff.
168 *
169 * @if_flags: holds interface index and packet related flags.
Arend van Sprielebb93882013-04-03 12:40:35 +0200170 * @htod: host to device packet identifier (used in PKTTAG tlv).
Arend van Sprielebb93882013-04-03 12:40:35 +0200171 * @state: transmit state of the packet.
172 * @mac: descriptor related to destination for this packet.
173 *
174 * This information is stored in control buffer struct sk_buff::cb, which
175 * provides 48 bytes of storage so this structure should not exceed that.
176 */
177struct brcmf_skbuff_cb {
178 u16 if_flags;
Arend van Sprielebb93882013-04-03 12:40:35 +0200179 u32 htod;
Arend van Sprielebb93882013-04-03 12:40:35 +0200180 enum brcmf_fws_skb_state state;
181 struct brcmf_fws_mac_descriptor *mac;
182};
183
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200184/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200185 * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
186 */
187#define brcmf_skbcb(skb) ((struct brcmf_skbuff_cb *)((skb)->cb))
188
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200189/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200190 * sk_buff control if flags
191 *
192 * b[11] - packet sent upon firmware request.
193 * b[10] - packet only contains signalling data.
194 * b[9] - packet is a tx packet.
195 * b[8] - packet uses FIFO credit (non-pspoll).
196 * b[7] - interface in AP mode.
197 * b[6:4] - AC FIFO number.
198 * b[3:0] - interface index.
199 */
200#define BRCMF_SKB_IF_FLAGS_REQUESTED_MASK 0x0800
201#define BRCMF_SKB_IF_FLAGS_REQUESTED_SHIFT 11
202#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_MASK 0x0400
203#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_SHIFT 10
204#define BRCMF_SKB_IF_FLAGS_TRANSMIT_MASK 0x0200
205#define BRCMF_SKB_IF_FLAGS_TRANSMIT_SHIFT 9
206#define BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK 0x0100
207#define BRCMF_SKB_IF_FLAGS_CREDITCHECK_SHIFT 8
208#define BRCMF_SKB_IF_FLAGS_IF_AP_MASK 0x0080
209#define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT 7
210#define BRCMF_SKB_IF_FLAGS_FIFO_MASK 0x0070
211#define BRCMF_SKB_IF_FLAGS_FIFO_SHIFT 4
212#define BRCMF_SKB_IF_FLAGS_INDEX_MASK 0x000f
213#define BRCMF_SKB_IF_FLAGS_INDEX_SHIFT 0
214
215#define brcmf_skb_if_flags_set_field(skb, field, value) \
216 brcmu_maskset16(&(brcmf_skbcb(skb)->if_flags), \
217 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
218 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT, (value))
219#define brcmf_skb_if_flags_get_field(skb, field) \
220 brcmu_maskget16(brcmf_skbcb(skb)->if_flags, \
221 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
222 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT)
223
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200224/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200225 * sk_buff control packet identifier
226 *
227 * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
228 *
229 * - Generated at the host (e.g. dhd)
230 * - Seen as a generic sequence number by firmware except for the flags field.
231 *
232 * Generation : b[31] => generation number for this packet [host->fw]
233 * OR, current generation number [fw->host]
234 * Flags : b[30:27] => command, status flags
235 * FIFO-AC : b[26:24] => AC-FIFO id
236 * h-slot : b[23:8] => hanger-slot
237 * freerun : b[7:0] => A free running counter
238 */
239#define BRCMF_SKB_HTOD_TAG_GENERATION_MASK 0x80000000
240#define BRCMF_SKB_HTOD_TAG_GENERATION_SHIFT 31
241#define BRCMF_SKB_HTOD_TAG_FLAGS_MASK 0x78000000
242#define BRCMF_SKB_HTOD_TAG_FLAGS_SHIFT 27
243#define BRCMF_SKB_HTOD_TAG_FIFO_MASK 0x07000000
244#define BRCMF_SKB_HTOD_TAG_FIFO_SHIFT 24
245#define BRCMF_SKB_HTOD_TAG_HSLOT_MASK 0x00ffff00
246#define BRCMF_SKB_HTOD_TAG_HSLOT_SHIFT 8
247#define BRCMF_SKB_HTOD_TAG_FREERUN_MASK 0x000000ff
248#define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT 0
249
250#define brcmf_skb_htod_tag_set_field(skb, field, value) \
Arend van Spriele2432b62013-04-03 12:40:38 +0200251 brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
Arend van Sprielebb93882013-04-03 12:40:35 +0200252 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
253 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
254#define brcmf_skb_htod_tag_get_field(skb, field) \
Arend van Spriele2432b62013-04-03 12:40:38 +0200255 brcmu_maskget32(brcmf_skbcb(skb)->htod, \
Arend van Sprielebb93882013-04-03 12:40:35 +0200256 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
257 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)
258
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200259#define BRCMF_FWS_TXSTAT_GENERATION_MASK 0x80000000
260#define BRCMF_FWS_TXSTAT_GENERATION_SHIFT 31
261#define BRCMF_FWS_TXSTAT_FLAGS_MASK 0x78000000
262#define BRCMF_FWS_TXSTAT_FLAGS_SHIFT 27
263#define BRCMF_FWS_TXSTAT_FIFO_MASK 0x07000000
264#define BRCMF_FWS_TXSTAT_FIFO_SHIFT 24
265#define BRCMF_FWS_TXSTAT_HSLOT_MASK 0x00FFFF00
266#define BRCMF_FWS_TXSTAT_HSLOT_SHIFT 8
267#define BRCMF_FWS_TXSTAT_PKTID_MASK 0x00FFFFFF
268#define BRCMF_FWS_TXSTAT_PKTID_SHIFT 0
269
270#define brcmf_txstatus_get_field(txs, field) \
271 brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
272 BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)
273
274/**
275 * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
276 *
277 * @BRCMF_FWS_FIFO_AC_BK: fifo for background traffic.
278 * @BRCMF_FWS_FIFO_AC_BE: fifo for best-effort traffic.
279 * @BRCMF_FWS_FIFO_AC_VI: fifo for video traffic.
280 * @BRCMF_FWS_FIFO_AC_VO: fifo for voice traffic.
281 * @BRCMF_FWS_FIFO_BCMC: fifo for broadcast/multicast (AP only).
282 * @BRCMF_FWS_FIFO_ATIM: fifo for ATIM (AP only).
283 * @BRCMF_FWS_FIFO_COUNT: number of fifos.
284 */
285enum brcmf_fws_fifo {
286 BRCMF_FWS_FIFO_AC_BK,
287 BRCMF_FWS_FIFO_AC_BE,
288 BRCMF_FWS_FIFO_AC_VI,
289 BRCMF_FWS_FIFO_AC_VO,
290 BRCMF_FWS_FIFO_BCMC,
291 BRCMF_FWS_FIFO_ATIM,
292 BRCMF_FWS_FIFO_COUNT
293};
294
295/**
296 * enum brcmf_fws_txstatus - txstatus flag values.
297 *
298 * @BRCMF_FWS_TXSTATUS_DISCARD:
299 * host is free to discard the packet.
300 * @BRCMF_FWS_TXSTATUS_CORE_SUPPRESS:
301 * 802.11 core suppressed the packet.
302 * @BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS:
303 * firmware suppress the packet as device is already in PS mode.
304 * @BRCMF_FWS_TXSTATUS_FW_TOSSED:
305 * firmware tossed the packet.
306 */
307enum brcmf_fws_txstatus {
308 BRCMF_FWS_TXSTATUS_DISCARD,
309 BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
310 BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
311 BRCMF_FWS_TXSTATUS_FW_TOSSED
312};
313
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200314enum brcmf_fws_fcmode {
315 BRCMF_FWS_FCMODE_NONE,
316 BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
317 BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
318};
319
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200320enum brcmf_fws_mac_desc_state {
321 BRCMF_FWS_STATE_OPEN = 1,
322 BRCMF_FWS_STATE_CLOSE
323};
324
Arend van Sprielebb93882013-04-03 12:40:35 +0200325/**
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200326 * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
327 *
328 * @occupied: slot is in use.
Arend van Sprielce814c12013-04-03 12:40:31 +0200329 * @mac_handle: handle for mac entry determined by firmware.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200330 * @interface_id: interface index.
331 * @state: current state.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200332 * @suppressed: mac entry is suppressed.
333 * @generation: generation bit.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200334 * @ac_bitmap: ac queue bitmap.
335 * @requested_credit: credits requested by firmware.
336 * @ea: ethernet address.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200337 * @seq: per-node free-running sequence.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200338 * @psq: power-save queue.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200339 * @transit_count: packet in transit to firmware.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200340 */
341struct brcmf_fws_mac_descriptor {
342 u8 occupied;
Arend van Sprielce814c12013-04-03 12:40:31 +0200343 u8 mac_handle;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200344 u8 interface_id;
345 u8 state;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200346 bool suppressed;
347 u8 generation;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200348 u8 ac_bitmap;
349 u8 requested_credit;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200350 u8 requested_packet;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200351 u8 ea[ETH_ALEN];
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200352 u8 seq[BRCMF_FWS_FIFO_COUNT];
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200353 struct pktq psq;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200354 int transit_count;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200355 int suppress_count;
356 int suppr_transit_count;
357 bool send_tim_signal;
358 u8 traffic_pending_bmp;
359 u8 traffic_lastreported_bmp;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200360};
361
Arend van Spriel69712802013-04-03 12:40:37 +0200362#define BRCMF_FWS_HANGER_MAXITEMS 1024
363
364/**
365 * enum brcmf_fws_hanger_item_state - state of hanger item.
366 *
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200367 * @BRCMF_FWS_HANGER_ITEM_STATE_FREE: item is free for use.
368 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE: item is in use.
369 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED: item was suppressed.
Arend van Spriel69712802013-04-03 12:40:37 +0200370 */
371enum brcmf_fws_hanger_item_state {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200372 BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
373 BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
374 BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
Arend van Spriel69712802013-04-03 12:40:37 +0200375};
376
377
378/**
379 * struct brcmf_fws_hanger_item - single entry for tx pending packet.
380 *
381 * @state: entry is either free or occupied.
382 * @gen: generation.
Arend van Spriel69712802013-04-03 12:40:37 +0200383 * @pkt: packet itself.
384 */
385struct brcmf_fws_hanger_item {
386 enum brcmf_fws_hanger_item_state state;
387 u8 gen;
Arend van Spriel69712802013-04-03 12:40:37 +0200388 struct sk_buff *pkt;
389};
390
391/**
392 * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
393 *
Arend van Spriel69712802013-04-03 12:40:37 +0200394 * @pushed: packets pushed to await txstatus.
395 * @popped: packets popped upon handling txstatus.
396 * @failed_to_push: packets that could not be pushed.
397 * @failed_to_pop: packets that could not be popped.
398 * @failed_slotfind: packets for which failed to find an entry.
399 * @slot_pos: last returned item index for a free entry.
400 * @items: array of hanger items.
401 */
402struct brcmf_fws_hanger {
403 u32 pushed;
404 u32 popped;
405 u32 failed_to_push;
406 u32 failed_to_pop;
407 u32 failed_slotfind;
408 u32 slot_pos;
409 struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
410};
411
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200412struct brcmf_fws_macdesc_table {
413 struct brcmf_fws_mac_descriptor nodes[BRCMF_FWS_MAC_DESC_TABLE_SIZE];
414 struct brcmf_fws_mac_descriptor iface[BRCMF_MAX_IFS];
415 struct brcmf_fws_mac_descriptor other;
416};
417
Arend van Spriel349e7102013-03-03 12:45:28 +0100418struct brcmf_fws_info {
419 struct brcmf_pub *drvr;
420 struct brcmf_fws_stats stats;
Arend van Spriel69712802013-04-03 12:40:37 +0200421 struct brcmf_fws_hanger hanger;
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200422 enum brcmf_fws_fcmode fcmode;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200423 struct brcmf_fws_macdesc_table desc;
424 struct workqueue_struct *fws_wq;
425 struct work_struct fws_dequeue_work;
426 u32 fifo_enqpkt[BRCMF_FWS_FIFO_COUNT];
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200427 int fifo_credit[BRCMF_FWS_FIFO_COUNT];
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200428 int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
429 u32 fifo_credit_map;
430 u32 fifo_delay_map;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200431};
432
433/*
434 * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
435 */
436static const int brcmf_fws_prio2fifo[] = {
437 BRCMF_FWS_FIFO_AC_BE,
438 BRCMF_FWS_FIFO_AC_BK,
439 BRCMF_FWS_FIFO_AC_BK,
440 BRCMF_FWS_FIFO_AC_BE,
441 BRCMF_FWS_FIFO_AC_VI,
442 BRCMF_FWS_FIFO_AC_VI,
443 BRCMF_FWS_FIFO_AC_VO,
444 BRCMF_FWS_FIFO_AC_VO
Arend van Spriel349e7102013-03-03 12:45:28 +0100445};
446
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200447static int fcmode;
448module_param(fcmode, int, S_IRUSR);
449MODULE_PARM_DESC(fcmode, "mode of firmware signalled flow control");
450
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200451#define BRCMF_FWS_TLV_DEF(name, id, len) \
452 case BRCMF_FWS_TYPE_ ## name: \
453 return len;
454
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200455/**
456 * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
457 *
458 * @fws: firmware-signalling information.
459 * @id: identifier of the TLV.
460 *
461 * Return: the specified length for the given TLV; Otherwise -EINVAL.
462 */
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200463static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
464 enum brcmf_fws_tlv_type id)
465{
466 switch (id) {
467 BRCMF_FWS_TLV_DEFLIST
468 default:
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200469 fws->stats.tlv_invalid_type++;
470 break;
471 }
472 return -EINVAL;
473}
474#undef BRCMF_FWS_TLV_DEF
475
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200476static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
477{
478 u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
479 return ifidx == *(int *)arg;
480}
481
482static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
483 int ifidx)
484{
485 bool (*matchfn)(struct sk_buff *, void *) = NULL;
486 struct sk_buff *skb;
487 int prec;
488
489 if (ifidx != -1)
490 matchfn = brcmf_fws_ifidx_match;
491 for (prec = 0; prec < q->num_prec; prec++) {
492 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
493 while (skb) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200494 brcmu_pkt_buf_free_skb(skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200495 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
496 }
497 }
498}
499
Arend van Spriel69712802013-04-03 12:40:37 +0200500static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
501{
502 int i;
503
504 brcmf_dbg(TRACE, "enter\n");
505 memset(hanger, 0, sizeof(*hanger));
506 for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200507 hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
Arend van Spriel69712802013-04-03 12:40:37 +0200508}
509
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200510static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
Arend van Spriel69712802013-04-03 12:40:37 +0200511{
512 u32 i;
513
514 brcmf_dbg(TRACE, "enter\n");
515 i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
516
517 while (i != h->slot_pos) {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200518 if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
Arend van Spriel69712802013-04-03 12:40:37 +0200519 h->slot_pos = i;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200520 goto done;
Arend van Spriel69712802013-04-03 12:40:37 +0200521 }
522 i++;
523 if (i == BRCMF_FWS_HANGER_MAXITEMS)
524 i = 0;
525 }
526 brcmf_err("all slots occupied\n");
527 h->failed_slotfind++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200528 i = BRCMF_FWS_HANGER_MAXITEMS;
529done:
530 brcmf_dbg(TRACE, "exit: %d\n", i);
531 return i;
Arend van Spriel69712802013-04-03 12:40:37 +0200532}
533
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200534static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
Arend van Spriel69712802013-04-03 12:40:37 +0200535 struct sk_buff *pkt, u32 slot_id)
536{
537 brcmf_dbg(TRACE, "enter\n");
538 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
539 return -ENOENT;
540
541 if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
542 brcmf_err("slot is not free\n");
543 h->failed_to_push++;
544 return -EINVAL;
545 }
546
547 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
548 h->items[slot_id].pkt = pkt;
Arend van Spriel69712802013-04-03 12:40:37 +0200549 h->pushed++;
550 return 0;
551}
552
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200553static int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
Arend van Spriel69712802013-04-03 12:40:37 +0200554 u32 slot_id, struct sk_buff **pktout,
555 bool remove_item)
556{
557 brcmf_dbg(TRACE, "enter\n");
558 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
559 return -ENOENT;
560
561 if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
562 brcmf_err("entry not in use\n");
563 h->failed_to_pop++;
564 return -EINVAL;
565 }
566
567 *pktout = h->items[slot_id].pkt;
568 if (remove_item) {
569 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
570 h->items[slot_id].pkt = NULL;
Arend van Spriel69712802013-04-03 12:40:37 +0200571 h->items[slot_id].gen = 0xff;
572 h->popped++;
573 }
574 return 0;
575}
576
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200577static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
Arend van Spriel69712802013-04-03 12:40:37 +0200578 u32 slot_id, u8 gen)
579{
580 brcmf_dbg(TRACE, "enter\n");
581
582 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
583 return -ENOENT;
584
585 h->items[slot_id].gen = gen;
586
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200587 if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_INUSE) {
Arend van Spriel69712802013-04-03 12:40:37 +0200588 brcmf_err("entry not in use\n");
589 return -EINVAL;
590 }
591
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200592 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
Arend van Spriel69712802013-04-03 12:40:37 +0200593 return 0;
594}
595
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200596static int brcmf_fws_hanger_get_genbit(struct brcmf_fws_hanger *hanger,
Arend van Spriel69712802013-04-03 12:40:37 +0200597 struct sk_buff *pkt, u32 slot_id,
598 int *gen)
599{
600 brcmf_dbg(TRACE, "enter\n");
601 *gen = 0xff;
602
603 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
604 return -ENOENT;
605
606 if (hanger->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
607 brcmf_err("slot not in use\n");
608 return -EINVAL;
609 }
610
611 *gen = hanger->items[slot_id].gen;
612 return 0;
613}
614
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200615static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
Arend van Spriel69712802013-04-03 12:40:37 +0200616 bool (*fn)(struct sk_buff *, void *),
617 int ifidx)
618{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200619 struct brcmf_fws_hanger *h = &fws->hanger;
Arend van Spriel69712802013-04-03 12:40:37 +0200620 struct sk_buff *skb;
621 int i;
622 enum brcmf_fws_hanger_item_state s;
623
624 brcmf_dbg(TRACE, "enter: ifidx=%d\n", ifidx);
625 for (i = 0; i < ARRAY_SIZE(h->items); i++) {
626 s = h->items[i].state;
627 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE ||
628 s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
629 skb = h->items[i].pkt;
630 if (fn == NULL || fn(skb, &ifidx)) {
631 /* suppress packets freed from psq */
632 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE)
633 brcmu_pkt_buf_free_skb(skb);
634 h->items[i].state =
635 BRCMF_FWS_HANGER_ITEM_STATE_FREE;
636 }
637 }
638 }
639}
640
Arend van Sprielce814c12013-04-03 12:40:31 +0200641static void brcmf_fws_init_mac_descriptor(struct brcmf_fws_mac_descriptor *desc,
642 u8 *addr, u8 ifidx)
643{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200644 brcmf_dbg(TRACE,
645 "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
Arend van Sprielce814c12013-04-03 12:40:31 +0200646 desc->occupied = 1;
647 desc->state = BRCMF_FWS_STATE_OPEN;
648 desc->requested_credit = 0;
649 /* depending on use may need ifp->bssidx instead */
650 desc->interface_id = ifidx;
651 desc->ac_bitmap = 0xff; /* update this when handling APSD */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200652 if (addr)
653 memcpy(&desc->ea[0], addr, ETH_ALEN);
Arend van Sprielce814c12013-04-03 12:40:31 +0200654}
655
656static
657void brcmf_fws_clear_mac_descriptor(struct brcmf_fws_mac_descriptor *desc)
658{
659 brcmf_dbg(TRACE,
660 "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
661 desc->occupied = 0;
662 desc->state = BRCMF_FWS_STATE_CLOSE;
663 desc->requested_credit = 0;
664}
665
666static struct brcmf_fws_mac_descriptor *
667brcmf_fws_mac_descriptor_lookup(struct brcmf_fws_info *fws, u8 *ea)
668{
669 struct brcmf_fws_mac_descriptor *entry;
670 int i;
671
672 brcmf_dbg(TRACE, "enter: ea=%pM\n", ea);
673 if (ea == NULL)
674 return ERR_PTR(-EINVAL);
675
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200676 entry = &fws->desc.nodes[0];
677 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
Arend van Sprielce814c12013-04-03 12:40:31 +0200678 if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
679 return entry;
680 entry++;
681 }
682
683 return ERR_PTR(-ENOENT);
684}
685
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200686static struct brcmf_fws_mac_descriptor*
Arend van Sprielf97a7f02013-04-11 13:28:58 +0200687brcmf_fws_find_mac_desc(struct brcmf_fws_info *fws, struct brcmf_if *ifp,
688 u8 *da)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200689{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200690 struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200691 bool multicast;
Arend van Spriela7965fb2013-04-11 17:08:37 +0200692 enum nl80211_iftype iftype;
Arend van Sprielf97a7f02013-04-11 13:28:58 +0200693
694 brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200695
696 multicast = is_multicast_ether_addr(da);
Arend van Spriela7965fb2013-04-11 17:08:37 +0200697 iftype = brcmf_cfg80211_get_iftype(ifp);
698
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200699 /* Multicast destination and P2P clients get the interface entry.
700 * STA gets the interface entry if there is no exact match. For
701 * example, TDLS destinations have their own entry.
702 */
703 entry = NULL;
Arend van Spriela7965fb2013-04-11 17:08:37 +0200704 if ((multicast || iftype == NL80211_IFTYPE_STATION ||
705 iftype == NL80211_IFTYPE_P2P_CLIENT) && ifp->fws_desc)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200706 entry = ifp->fws_desc;
707
Arend van Spriela7965fb2013-04-11 17:08:37 +0200708 if (entry != NULL && iftype != NL80211_IFTYPE_STATION)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200709 goto done;
710
711 entry = brcmf_fws_mac_descriptor_lookup(fws, da);
712 if (IS_ERR(entry))
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200713 entry = &fws->desc.other;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200714
715done:
716 brcmf_dbg(TRACE, "exit: entry=%p\n", entry);
717 return entry;
718}
719
Arend van Sprielb75c1a32013-04-17 21:25:53 +0200720static bool brcmf_fws_mac_desc_closed(struct brcmf_fws_info *fws,
721 struct brcmf_fws_mac_descriptor *entry,
Arend van Spriel9fc60222013-04-11 17:12:04 +0200722 int fifo)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200723{
Arend van Sprielb75c1a32013-04-17 21:25:53 +0200724 struct brcmf_fws_mac_descriptor *if_entry;
Arend van Spriel9fc60222013-04-11 17:12:04 +0200725 bool closed;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200726
Arend van Sprielb75c1a32013-04-17 21:25:53 +0200727 /* for unique destination entries the related interface
728 * may be closed.
729 */
730 if (entry->mac_handle) {
731 if_entry = &fws->desc.iface[entry->interface_id];
732 if (if_entry->state == BRCMF_FWS_STATE_CLOSE)
733 return true;
734 }
Arend van Spriel9fc60222013-04-11 17:12:04 +0200735 /* an entry is closed when the state is closed and
736 * the firmware did not request anything.
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200737 */
Arend van Spriel9fc60222013-04-11 17:12:04 +0200738 closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
739 !entry->requested_credit && !entry->requested_packet;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200740
Arend van Spriel9fc60222013-04-11 17:12:04 +0200741 /* Or firmware does not allow traffic for given fifo */
742 return closed || !(entry->ac_bitmap & BIT(fifo));
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200743}
744
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200745static void brcmf_fws_mac_desc_cleanup(struct brcmf_fws_info *fws,
746 struct brcmf_fws_mac_descriptor *entry,
Arend van Sprielfba14002013-04-03 12:40:33 +0200747 int ifidx)
748{
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200749 brcmf_dbg(TRACE, "enter: entry=(ea=%pM, ifid=%d), ifidx=%d\n",
Arend van Sprielfba14002013-04-03 12:40:33 +0200750 entry->ea, entry->interface_id, ifidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200751 if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
752 brcmf_dbg(TRACE, "flush psq: ifidx=%d, qlen=%d\n",
Arend van Sprielfba14002013-04-03 12:40:33 +0200753 ifidx, entry->psq.len);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200754 brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
Arend van Sprielfba14002013-04-03 12:40:33 +0200755 entry->occupied = !!(entry->psq.len);
756 }
757}
758
Arend van Spriele2432b62013-04-03 12:40:38 +0200759static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
760 bool (*fn)(struct sk_buff *, void *),
761 int ifidx)
762{
763 struct brcmf_fws_hanger_item *hi;
764 struct pktq *txq;
765 struct sk_buff *skb;
766 int prec;
767 u32 hslot;
768
769 brcmf_dbg(TRACE, "enter: ifidx=%d\n", ifidx);
770 txq = brcmf_bus_gettxq(fws->drvr->bus_if);
771 if (IS_ERR(txq)) {
772 brcmf_dbg(TRACE, "no txq to clean up\n");
773 return;
774 }
775
776 for (prec = 0; prec < txq->num_prec; prec++) {
777 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
778 while (skb) {
779 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
780 hi = &fws->hanger.items[hslot];
781 WARN_ON(skb != hi->pkt);
782 hi->state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
783 brcmu_pkt_buf_free_skb(skb);
784 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
785 }
786 }
787}
788
Arend van Sprielfba14002013-04-03 12:40:33 +0200789static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
790{
791 int i;
792 struct brcmf_fws_mac_descriptor *table;
Arend van Spriela3e993c2013-04-03 12:40:36 +0200793 bool (*matchfn)(struct sk_buff *, void *) = NULL;
Arend van Sprielfba14002013-04-03 12:40:33 +0200794
795 brcmf_dbg(TRACE, "enter: ifidx=%d\n", ifidx);
796 if (fws == NULL)
797 return;
798
Arend van Spriela3e993c2013-04-03 12:40:36 +0200799 if (ifidx != -1)
800 matchfn = brcmf_fws_ifidx_match;
801
Arend van Sprielfba14002013-04-03 12:40:33 +0200802 /* cleanup individual nodes */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200803 table = &fws->desc.nodes[0];
804 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200805 brcmf_fws_mac_desc_cleanup(fws, &table[i], ifidx);
Arend van Sprielfba14002013-04-03 12:40:33 +0200806
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200807 brcmf_fws_mac_desc_cleanup(fws, &fws->desc.other, ifidx);
Arend van Spriele2432b62013-04-03 12:40:38 +0200808 brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200809 brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
810}
811
812static void brcmf_fws_tim_update(struct brcmf_fws_info *ctx,
813 struct brcmf_fws_mac_descriptor *entry,
814 int prec)
815{
816 brcmf_dbg(TRACE, "enter: ea=%pM\n", entry->ea);
817 if (entry->state == BRCMF_FWS_STATE_CLOSE) {
818 /* check delayedQ and suppressQ in one call using bitmap */
819 if (brcmu_pktq_mlen(&entry->psq, 3 << (prec * 2)) == 0)
820 entry->traffic_pending_bmp =
821 entry->traffic_pending_bmp & ~NBITVAL(prec);
822 else
823 entry->traffic_pending_bmp =
824 entry->traffic_pending_bmp | NBITVAL(prec);
825 }
826 /* request a TIM update to firmware at the next piggyback opportunity */
827 if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
828 entry->send_tim_signal = true;
829}
830
831static void
832brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
833 u8 if_id)
834{
835 struct brcmf_if *ifp = fws->drvr->iflist[if_id];
836
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200837 if (WARN_ON(!ifp))
838 return;
839
Wei Yongjun16a1f732013-04-19 10:14:31 +0800840 brcmf_dbg(TRACE,
841 "enter: bssidx=%d, ifidx=%d\n", ifp->bssidx, ifp->ifidx);
842
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200843 if ((ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
844 pq->len <= BRCMF_FWS_FLOWCONTROL_LOWATER)
845 brcmf_txflowblock_if(ifp,
846 BRCMF_NETIF_STOP_REASON_FWS_FC, false);
847 if (!(ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
848 pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER)
849 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
850 return;
Arend van Sprielfba14002013-04-03 12:40:33 +0200851}
852
Arend van Spriel349e7102013-03-03 12:45:28 +0100853static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
854{
855 brcmf_dbg(CTL, "rssi %d\n", rssi);
856 return 0;
857}
858
Arend van Sprielce814c12013-04-03 12:40:31 +0200859static
860int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
861{
862 struct brcmf_fws_mac_descriptor *entry, *existing;
863 u8 mac_handle;
864 u8 ifidx;
865 u8 *addr;
866
867 mac_handle = *data++;
868 ifidx = *data++;
869 addr = data;
870
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200871 entry = &fws->desc.nodes[mac_handle & 0x1F];
Arend van Sprielce814c12013-04-03 12:40:31 +0200872 if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
873 brcmf_dbg(TRACE, "deleting mac %pM idx %d\n", addr, ifidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200874 if (entry->occupied)
875 brcmf_fws_clear_mac_descriptor(entry);
876 else
Arend van Sprielce814c12013-04-03 12:40:31 +0200877 fws->stats.mac_update_failed++;
Arend van Sprielce814c12013-04-03 12:40:31 +0200878 return 0;
879 }
880
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200881 brcmf_dbg(TRACE,
882 "add mac %pM handle %u idx %d\n", addr, mac_handle, ifidx);
Arend van Sprielce814c12013-04-03 12:40:31 +0200883 existing = brcmf_fws_mac_descriptor_lookup(fws, addr);
884 if (IS_ERR(existing)) {
885 if (!entry->occupied) {
886 entry->mac_handle = mac_handle;
887 brcmf_fws_init_mac_descriptor(entry, addr, ifidx);
888 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
889 BRCMF_FWS_PSQ_LEN);
890 } else {
891 fws->stats.mac_update_failed++;
892 }
893 } else {
894 if (entry != existing) {
895 brcmf_dbg(TRACE, "relocate mac\n");
896 memcpy(entry, existing,
897 offsetof(struct brcmf_fws_mac_descriptor, psq));
898 entry->mac_handle = mac_handle;
899 brcmf_fws_clear_mac_descriptor(existing);
900 } else {
901 brcmf_dbg(TRACE, "use existing\n");
902 WARN_ON(entry->mac_handle != mac_handle);
903 /* TODO: what should we do here: continue, reinit, .. */
904 }
905 }
906 return 0;
907}
908
Arend van Spriel33753d42013-04-05 10:57:40 +0200909static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
910 u8 type, u8 *data)
911{
912 struct brcmf_fws_mac_descriptor *entry;
913 u8 mac_handle;
914 int i;
915
916 mac_handle = data[0];
917 entry = &fws->desc.nodes[mac_handle & 0x1F];
918 if (!entry->occupied) {
919 fws->stats.mac_ps_update_failed++;
920 return -ESRCH;
921 }
922
923 /* a state update should wipe old credits? */
924 entry->requested_credit = 0;
925 if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
926 entry->state = BRCMF_FWS_STATE_OPEN;
Arend van Spriela5e9d802013-04-17 21:25:54 +0200927 return BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel33753d42013-04-05 10:57:40 +0200928 } else {
929 entry->state = BRCMF_FWS_STATE_CLOSE;
930 for (i = BRCMF_FWS_FIFO_AC_BE; i < NL80211_NUM_ACS; i++)
931 brcmf_fws_tim_update(fws, entry, i);
932 }
Arend van Spriela5e9d802013-04-17 21:25:54 +0200933 return BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel33753d42013-04-05 10:57:40 +0200934}
935
936static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
937 u8 type, u8 *data)
938{
939 struct brcmf_fws_mac_descriptor *entry;
940 u8 ifidx;
941 int ret;
942
943 ifidx = data[0];
944
945 brcmf_dbg(TRACE, "enter: ifidx=%d\n", ifidx);
946 if (ifidx >= BRCMF_MAX_IFS) {
947 ret = -ERANGE;
948 goto fail;
949 }
950
951 entry = &fws->desc.iface[ifidx];
952 if (!entry->occupied) {
953 ret = -ESRCH;
954 goto fail;
955 }
956
957 switch (type) {
958 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
959 entry->state = BRCMF_FWS_STATE_OPEN;
Arend van Spriela5e9d802013-04-17 21:25:54 +0200960 return BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel33753d42013-04-05 10:57:40 +0200961 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
962 entry->state = BRCMF_FWS_STATE_CLOSE;
Arend van Spriela5e9d802013-04-17 21:25:54 +0200963 return BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel33753d42013-04-05 10:57:40 +0200964 default:
965 ret = -EINVAL;
966 break;
967 }
968fail:
969 fws->stats.if_update_failed++;
970 return ret;
971}
972
Arend van Spriel5f38b682013-04-05 10:57:41 +0200973static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
974 u8 *data)
975{
976 struct brcmf_fws_mac_descriptor *entry;
977
978 entry = &fws->desc.nodes[data[1] & 0x1F];
979 if (!entry->occupied) {
980 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
981 fws->stats.credit_request_failed++;
982 else
983 fws->stats.packet_request_failed++;
984 return -ESRCH;
985 }
986
987 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
988 entry->requested_credit = data[0];
989 else
990 entry->requested_packet = data[0];
991
992 entry->ac_bitmap = data[2];
Arend van Spriela5e9d802013-04-17 21:25:54 +0200993 return BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel5f38b682013-04-05 10:57:41 +0200994}
995
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200996static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
997 u8 fifo, u8 credits)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200998{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200999 if (!credits)
1000 return;
1001
1002 fws->fifo_credit_map |= 1 << fifo;
1003 fws->fifo_credit[fifo] += credits;
1004}
1005
1006static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
1007{
1008 /* only schedule dequeue when there are credits for delayed traffic */
1009 if (fws->fifo_credit_map & fws->fifo_delay_map)
1010 queue_work(fws->fws_wq, &fws->fws_dequeue_work);
1011}
1012
1013static void brcmf_skb_pick_up_credit(struct brcmf_fws_info *fws, int fifo,
1014 struct sk_buff *p)
1015{
1016 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(p)->mac;
1017
1018 if (brcmf_skbcb(p)->if_flags & BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK) {
1019 if (fws->fcmode != BRCMF_FWS_FCMODE_IMPLIED_CREDIT)
1020 return;
1021 brcmf_fws_return_credits(fws, fifo, 1);
1022 } else {
1023 /*
1024 * if this packet did not count against FIFO credit, it
1025 * must have taken a requested_credit from the destination
1026 * entry (for pspoll etc.)
1027 */
1028 if (!brcmf_skb_if_flags_get_field(p, REQUESTED))
1029 entry->requested_credit++;
1030 }
1031 brcmf_fws_schedule_deq(fws);
1032}
1033
1034static int brcmf_fws_enq(struct brcmf_fws_info *fws,
1035 enum brcmf_fws_skb_state state, int fifo,
1036 struct sk_buff *p)
1037{
1038 int prec = 2 * fifo;
1039 u32 *qfull_stat = &fws->stats.delayq_full_error;
1040
1041 struct brcmf_fws_mac_descriptor *entry;
1042
1043 entry = brcmf_skbcb(p)->mac;
1044 if (entry == NULL) {
1045 brcmf_err("no mac descriptor found for skb %p\n", p);
1046 return -ENOENT;
1047 }
1048
1049 brcmf_dbg(TRACE, "enter: ea=%pM, qlen=%d\n", entry->ea, entry->psq.len);
1050 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1051 prec += 1;
1052 qfull_stat = &fws->stats.supprq_full_error;
1053 }
1054
1055 if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
1056 *qfull_stat += 1;
1057 return -ENFILE;
1058 }
1059
1060 /* increment total enqueued packet count */
1061 fws->fifo_delay_map |= 1 << fifo;
1062 fws->fifo_enqpkt[fifo]++;
1063
1064 /* update the sk_buff state */
1065 brcmf_skbcb(p)->state = state;
1066 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
1067 entry->suppress_count++;
1068
1069 /*
1070 * A packet has been pushed so update traffic
1071 * availability bitmap, if applicable
1072 */
1073 brcmf_fws_tim_update(fws, entry, fifo);
1074 brcmf_fws_flow_control_check(fws, &entry->psq,
1075 brcmf_skb_if_flags_get_field(p, INDEX));
1076 return 0;
1077}
1078
1079static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
1080{
1081 struct brcmf_fws_mac_descriptor *table;
1082 struct brcmf_fws_mac_descriptor *entry;
1083 struct sk_buff *p;
1084 int use_credit = 1;
1085 int num_nodes;
1086 int node_pos;
1087 int prec_out;
Arend van Spriel07529d22013-04-17 21:25:49 +02001088 int pmsk;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001089 int i;
1090
1091 table = (struct brcmf_fws_mac_descriptor *)&fws->desc;
1092 num_nodes = sizeof(fws->desc) / sizeof(struct brcmf_fws_mac_descriptor);
1093 node_pos = fws->deq_node_pos[fifo];
1094
1095 for (i = 0; i < num_nodes; i++) {
1096 entry = &table[(node_pos + i) % num_nodes];
Arend van Sprielb75c1a32013-04-17 21:25:53 +02001097 if (!entry->occupied ||
1098 brcmf_fws_mac_desc_closed(fws, entry, fifo))
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001099 continue;
1100
1101 if (entry->suppressed)
1102 pmsk = 2;
Arend van Spriel07529d22013-04-17 21:25:49 +02001103 else
1104 pmsk = 3;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001105 p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
1106 if (p == NULL) {
1107 if (entry->suppressed) {
1108 if (entry->suppr_transit_count >
1109 entry->suppress_count)
1110 return NULL;
1111 entry->suppressed = false;
1112 p = brcmu_pktq_mdeq(&entry->psq,
1113 1 << (fifo * 2), &prec_out);
1114 }
1115 }
1116 if (p == NULL)
1117 continue;
1118
1119 /* did the packet come from suppress sub-queue? */
1120 if (entry->requested_credit > 0) {
1121 entry->requested_credit--;
1122 /*
1123 * if the packet was pulled out while destination is in
1124 * closed state but had a non-zero packets requested,
1125 * then this should not count against the FIFO credit.
1126 * That is due to the fact that the firmware will
1127 * most likely hold onto this packet until a suitable
1128 * time later to push it to the appropriate AC FIFO.
1129 */
1130 if (entry->state == BRCMF_FWS_STATE_CLOSE)
1131 use_credit = 0;
1132 } else if (entry->requested_packet > 0) {
1133 entry->requested_packet--;
1134 brcmf_skb_if_flags_set_field(p, REQUESTED, 1);
1135 if (entry->state == BRCMF_FWS_STATE_CLOSE)
1136 use_credit = 0;
1137 }
1138 brcmf_skb_if_flags_set_field(p, CREDITCHECK, use_credit);
1139
1140 /* move dequeue position to ensure fair round-robin */
1141 fws->deq_node_pos[fifo] = (node_pos + i + 1) % num_nodes;
1142 brcmf_fws_flow_control_check(fws, &entry->psq,
1143 brcmf_skb_if_flags_get_field(p,
1144 INDEX)
1145 );
1146 /*
1147 * A packet has been picked up, update traffic
1148 * availability bitmap, if applicable
1149 */
1150 brcmf_fws_tim_update(fws, entry, fifo);
1151
1152 /*
1153 * decrement total enqueued fifo packets and
1154 * clear delay bitmap if done.
1155 */
1156 fws->fifo_enqpkt[fifo]--;
1157 if (fws->fifo_enqpkt[fifo] == 0)
1158 fws->fifo_delay_map &= ~(1 << fifo);
1159 goto done;
1160 }
1161 p = NULL;
1162done:
1163 brcmf_dbg(TRACE, "exit: fifo %d skb %p\n", fifo, p);
1164 return p;
1165}
1166
1167static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
1168 struct sk_buff *skb, u32 genbit)
1169{
1170 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1171 u32 hslot;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001172 int ret;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001173
1174 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1175
1176 /* this packet was suppressed */
1177 if (!entry->suppressed || entry->generation != genbit) {
1178 entry->suppressed = true;
1179 entry->suppress_count = brcmu_pktq_mlen(&entry->psq,
1180 1 << (fifo * 2 + 1));
1181 entry->suppr_transit_count = entry->transit_count;
1182 }
1183
1184 entry->generation = genbit;
1185
1186 ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo, skb);
1187 if (ret != 0) {
1188 /* suppress q is full, drop this packet */
1189 brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1190 true);
1191 } else {
1192 /*
1193 * Mark suppressed to avoid a double free during
1194 * wlfc cleanup
1195 */
1196 brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot,
1197 genbit);
1198 entry->suppress_count++;
1199 }
1200
1201 return ret;
1202}
1203
1204static int
1205brcmf_fws_txstatus_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
1206 u32 genbit)
1207{
1208 u32 fifo;
1209 int ret;
1210 bool remove_from_hanger = true;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001211 struct sk_buff *skb;
1212 struct brcmf_fws_mac_descriptor *entry = NULL;
1213
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001214 brcmf_dbg(TRACE, "status: flags=0x%X, hslot=%d\n",
1215 flags, hslot);
1216
1217 if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
1218 fws->stats.txs_discard++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001219 else if (flags == BRCMF_FWS_TXSTATUS_CORE_SUPPRESS) {
1220 fws->stats.txs_supp_core++;
1221 remove_from_hanger = false;
1222 } else if (flags == BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS) {
1223 fws->stats.txs_supp_ps++;
1224 remove_from_hanger = false;
1225 } else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001226 fws->stats.txs_tossed++;
1227 else
1228 brcmf_err("unexpected txstatus\n");
1229
1230 ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001231 remove_from_hanger);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001232 if (ret != 0) {
1233 brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
1234 return ret;
1235 }
1236
1237 entry = brcmf_skbcb(skb)->mac;
1238 if (WARN_ON(!entry)) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001239 brcmu_pkt_buf_free_skb(skb);
1240 return -EINVAL;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001241 }
1242
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001243 /* pick up the implicit credit from this packet */
1244 fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
1245 brcmf_skb_pick_up_credit(fws, fifo, skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001246
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001247 if (!remove_from_hanger)
1248 ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb, genbit);
1249
1250 if (remove_from_hanger || ret) {
1251 entry->transit_count--;
1252 if (entry->suppressed)
1253 entry->suppr_transit_count--;
1254
1255 brcmf_txfinalize(fws->drvr, skb, true);
1256 }
1257 return 0;
1258}
1259
1260static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
1261 u8 *data)
1262{
1263 int i;
1264
1265 if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
1266 brcmf_dbg(INFO, "ignored\n");
Arend van Spriela5e9d802013-04-17 21:25:54 +02001267 return BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001268 }
1269
1270 brcmf_dbg(TRACE, "enter: data %pM\n", data);
1271 for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
1272 brcmf_fws_return_credits(fws, i, data[i]);
1273
1274 brcmf_dbg(INFO, "map: credit %x delay %x\n", fws->fifo_credit_map,
1275 fws->fifo_delay_map);
Arend van Spriela5e9d802013-04-17 21:25:54 +02001276 return BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001277}
1278
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001279static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
1280{
Arend van Spriela2ffc562013-04-05 10:57:39 +02001281 __le32 status_le;
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001282 u32 status;
1283 u32 hslot;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001284 u32 genbit;
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001285 u8 flags;
1286
1287 fws->stats.txs_indicate++;
Arend van Spriela2ffc562013-04-05 10:57:39 +02001288 memcpy(&status_le, data, sizeof(status_le));
1289 status = le32_to_cpu(status_le);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001290 flags = brcmf_txstatus_get_field(status, FLAGS);
1291 hslot = brcmf_txstatus_get_field(status, HSLOT);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001292 genbit = brcmf_txstatus_get_field(status, GENERATION);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001293
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001294 return brcmf_fws_txstatus_process(fws, flags, hslot, genbit);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001295}
1296
Arend van Spriel349e7102013-03-03 12:45:28 +01001297static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
1298{
1299 __le32 timestamp;
1300
1301 memcpy(&timestamp, &data[2], sizeof(timestamp));
1302 brcmf_dbg(INFO, "received: seq %d, timestamp %d\n", data[1],
1303 le32_to_cpu(timestamp));
1304 return 0;
1305}
1306
1307/* using macro so sparse checking does not complain
1308 * about locking imbalance.
1309 */
1310#define brcmf_fws_lock(drvr, flags) \
1311do { \
1312 flags = 0; \
1313 spin_lock_irqsave(&((drvr)->fws_spinlock), (flags)); \
1314} while (0)
1315
1316/* using macro so sparse checking does not complain
1317 * about locking imbalance.
1318 */
1319#define brcmf_fws_unlock(drvr, flags) \
1320 spin_unlock_irqrestore(&((drvr)->fws_spinlock), (flags))
1321
Arend van Spriel43fa6352013-04-03 12:40:32 +02001322static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
1323 const struct brcmf_event_msg *e,
1324 void *data)
1325{
1326 struct brcmf_fws_info *fws = ifp->drvr->fws;
1327 int i;
1328 ulong flags;
1329 u8 *credits = data;
1330
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001331 if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
1332 brcmf_err("event payload too small (%d)\n", e->datalen);
1333 return -EINVAL;
1334 }
1335
1336 brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001337 brcmf_fws_lock(ifp->drvr, flags);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001338 for (i = 0; i < ARRAY_SIZE(fws->fifo_credit); i++) {
1339 if (*credits)
1340 fws->fifo_credit_map |= 1 << i;
1341 else
1342 fws->fifo_credit_map &= ~(1 << i);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001343 fws->fifo_credit[i] = *credits++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001344 }
1345 brcmf_fws_schedule_deq(fws);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001346 brcmf_fws_unlock(ifp->drvr, flags);
1347 return 0;
1348}
1349
Arend van Spriel349e7102013-03-03 12:45:28 +01001350int brcmf_fws_hdrpull(struct brcmf_pub *drvr, int ifidx, s16 signal_len,
1351 struct sk_buff *skb)
1352{
1353 struct brcmf_fws_info *fws = drvr->fws;
1354 ulong flags;
1355 u8 *signal_data;
1356 s16 data_len;
1357 u8 type;
1358 u8 len;
1359 u8 *data;
Arend van Spriela5e9d802013-04-17 21:25:54 +02001360 s32 status;
1361 s32 err;
Arend van Spriel349e7102013-03-03 12:45:28 +01001362
1363 brcmf_dbg(TRACE, "enter: ifidx %d, skblen %u, sig %d\n",
1364 ifidx, skb->len, signal_len);
1365
1366 WARN_ON(signal_len > skb->len);
1367
1368 /* if flow control disabled, skip to packet data and leave */
1369 if (!signal_len || !drvr->fw_signals) {
1370 skb_pull(skb, signal_len);
1371 return 0;
1372 }
1373
1374 /* lock during tlv parsing */
1375 brcmf_fws_lock(drvr, flags);
1376
1377 fws->stats.header_pulls++;
1378 data_len = signal_len;
1379 signal_data = skb->data;
1380
Arend van Spriela5e9d802013-04-17 21:25:54 +02001381 status = BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel349e7102013-03-03 12:45:28 +01001382 while (data_len > 0) {
1383 /* extract tlv info */
1384 type = signal_data[0];
1385
1386 /* FILLER type is actually not a TLV, but
1387 * a single byte that can be skipped.
1388 */
1389 if (type == BRCMF_FWS_TYPE_FILLER) {
1390 signal_data += 1;
1391 data_len -= 1;
1392 continue;
1393 }
1394 len = signal_data[1];
1395 data = signal_data + 2;
1396
Arend van Spriel2af15582013-04-03 12:40:42 +02001397 brcmf_dbg(INFO, "tlv type=%d (%s), len=%d, data[0]=%d\n", type,
1398 brcmf_fws_get_tlv_name(type), len, *data);
1399
Arend van Spriel349e7102013-03-03 12:45:28 +01001400 /* abort parsing when length invalid */
1401 if (data_len < len + 2)
1402 break;
1403
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001404 if (len != brcmf_fws_get_tlv_len(fws, type))
1405 break;
1406
Arend van Spriela5e9d802013-04-17 21:25:54 +02001407 err = BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel349e7102013-03-03 12:45:28 +01001408 switch (type) {
Arend van Spriel349e7102013-03-03 12:45:28 +01001409 case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001410 case BRCMF_FWS_TYPE_COMP_TXSTATUS:
Arend van Sprielce814c12013-04-03 12:40:31 +02001411 break;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001412 case BRCMF_FWS_TYPE_MACDESC_ADD:
1413 case BRCMF_FWS_TYPE_MACDESC_DEL:
Arend van Sprielce814c12013-04-03 12:40:31 +02001414 brcmf_fws_macdesc_indicate(fws, type, data);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001415 break;
Arend van Spriel33753d42013-04-05 10:57:40 +02001416 case BRCMF_FWS_TYPE_MAC_OPEN:
1417 case BRCMF_FWS_TYPE_MAC_CLOSE:
Arend van Spriela5e9d802013-04-17 21:25:54 +02001418 err = brcmf_fws_macdesc_state_indicate(fws, type, data);
Arend van Spriel33753d42013-04-05 10:57:40 +02001419 break;
1420 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1421 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
Arend van Spriela5e9d802013-04-17 21:25:54 +02001422 err = brcmf_fws_interface_state_indicate(fws, type,
1423 data);
Arend van Spriel33753d42013-04-05 10:57:40 +02001424 break;
Arend van Spriel5f38b682013-04-05 10:57:41 +02001425 case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
1426 case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
Arend van Spriela5e9d802013-04-17 21:25:54 +02001427 err = brcmf_fws_request_indicate(fws, type, data);
Arend van Spriel5f38b682013-04-05 10:57:41 +02001428 break;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001429 case BRCMF_FWS_TYPE_TXSTATUS:
1430 brcmf_fws_txstatus_indicate(fws, data);
1431 break;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001432 case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
Arend van Spriela5e9d802013-04-17 21:25:54 +02001433 err = brcmf_fws_fifocreditback_indicate(fws, data);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001434 break;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001435 case BRCMF_FWS_TYPE_RSSI:
1436 brcmf_fws_rssi_indicate(fws, *data);
Arend van Spriel349e7102013-03-03 12:45:28 +01001437 break;
1438 case BRCMF_FWS_TYPE_TRANS_ID:
Arend van Spriel349e7102013-03-03 12:45:28 +01001439 brcmf_fws_dbg_seqnum_check(fws, data);
1440 break;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001441 case BRCMF_FWS_TYPE_PKTTAG:
1442 case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
Arend van Spriel349e7102013-03-03 12:45:28 +01001443 default:
1444 fws->stats.tlv_invalid_type++;
1445 break;
1446 }
Arend van Spriela5e9d802013-04-17 21:25:54 +02001447 if (err == BRCMF_FWS_RET_OK_SCHEDULE)
1448 status = BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel349e7102013-03-03 12:45:28 +01001449 signal_data += len + 2;
1450 data_len -= len + 2;
1451 }
1452
1453 if (data_len != 0)
1454 fws->stats.tlv_parse_failed++;
1455
Arend van Spriela5e9d802013-04-17 21:25:54 +02001456 if (status == BRCMF_FWS_RET_OK_SCHEDULE)
1457 brcmf_fws_schedule_deq(fws);
1458
Arend van Spriel349e7102013-03-03 12:45:28 +01001459 /* signalling processing result does
1460 * not affect the actual ethernet packet.
1461 */
1462 skb_pull(skb, signal_len);
1463
1464 /* this may be a signal-only packet
1465 */
1466 if (skb->len == 0)
1467 fws->stats.header_only_pkt++;
1468
1469 brcmf_fws_unlock(drvr, flags);
1470 return 0;
1471}
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001472
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001473static int brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
1474{
1475 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1476 u8 *wlh;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001477 u16 data_offset = 0;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001478 u8 fillers;
1479 __le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);
1480
1481 brcmf_dbg(TRACE, "enter: ea=%pM, ifidx=%u, pkttag=0x%08X\n",
1482 entry->ea, entry->interface_id, le32_to_cpu(pkttag));
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001483 if (entry->send_tim_signal)
1484 data_offset += 2 + BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
1485
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001486 /* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001487 data_offset += 2 + BRCMF_FWS_TYPE_PKTTAG_LEN;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001488 fillers = round_up(data_offset, 4) - data_offset;
1489 data_offset += fillers;
1490
1491 skb_push(skb, data_offset);
1492 wlh = skb->data;
1493
1494 wlh[0] = BRCMF_FWS_TYPE_PKTTAG;
1495 wlh[1] = BRCMF_FWS_TYPE_PKTTAG_LEN;
1496 memcpy(&wlh[2], &pkttag, sizeof(pkttag));
1497 wlh += BRCMF_FWS_TYPE_PKTTAG_LEN + 2;
1498
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001499 if (entry->send_tim_signal) {
1500 entry->send_tim_signal = 0;
1501 wlh[0] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP;
1502 wlh[1] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
1503 wlh[2] = entry->mac_handle;
1504 wlh[3] = entry->traffic_pending_bmp;
1505 wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
1506 entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
1507 }
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001508 if (fillers)
1509 memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);
1510
1511 brcmf_proto_hdrpush(fws->drvr, brcmf_skb_if_flags_get_field(skb, INDEX),
1512 data_offset >> 2, skb);
1513 return 0;
1514}
1515
1516static int brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
1517 struct sk_buff *p)
1518{
1519 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1520 struct brcmf_fws_mac_descriptor *entry = skcb->mac;
1521 int rc = 0;
1522 bool header_needed;
1523 int hslot = BRCMF_FWS_HANGER_MAXITEMS;
1524 u8 free_ctr;
1525 u8 ifidx;
1526 u8 flags;
1527
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001528 header_needed = skcb->state != BRCMF_FWS_SKBSTATE_SUPPRESSED;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001529
1530 if (header_needed) {
1531 /* obtaining free slot may fail, but that will be caught
1532 * by the hanger push. This assures the packet has a BDC
1533 * header upon return.
1534 */
1535 hslot = brcmf_fws_hanger_get_free_slot(&fws->hanger);
1536 free_ctr = entry->seq[fifo];
1537 brcmf_skb_htod_tag_set_field(p, HSLOT, hslot);
1538 brcmf_skb_htod_tag_set_field(p, FREERUN, free_ctr);
1539 brcmf_skb_htod_tag_set_field(p, GENERATION, 1);
1540 entry->transit_count++;
1541 }
1542 brcmf_skb_if_flags_set_field(p, TRANSMIT, 1);
1543 brcmf_skb_htod_tag_set_field(p, FIFO, fifo);
1544
1545 flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
1546 if (!(skcb->if_flags & BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK)) {
1547 /*
1548 Indicate that this packet is being sent in response to an
1549 explicit request from the firmware side.
1550 */
1551 flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
1552 }
1553 brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
1554 if (header_needed) {
1555 brcmf_fws_hdrpush(fws, p);
1556 rc = brcmf_fws_hanger_pushpkt(&fws->hanger, p, hslot);
1557 if (rc)
1558 brcmf_err("hanger push failed: rc=%d\n", rc);
1559 } else {
1560 int gen;
1561
1562 /* remove old header */
1563 rc = brcmf_proto_hdrpull(fws->drvr, false, &ifidx, p);
1564 if (rc == 0) {
1565 hslot = brcmf_skb_htod_tag_get_field(p, HSLOT);
1566 brcmf_fws_hanger_get_genbit(&fws->hanger, p,
1567 hslot, &gen);
1568 brcmf_skb_htod_tag_set_field(p, GENERATION, gen);
1569
1570 /* push new header */
1571 brcmf_fws_hdrpush(fws, p);
1572 }
1573 }
1574
1575 return rc;
1576}
1577
Arend van Spriel88f656f2013-04-17 21:25:57 +02001578static void
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001579brcmf_fws_rollback_toq(struct brcmf_fws_info *fws, struct sk_buff *skb)
1580{
1581 /*
1582 put the packet back to the head of queue
1583
1584 - suppressed packet goes back to suppress sub-queue
1585 - pull out the header, if new or delayed packet
1586
1587 Note: hslot is used only when header removal is done.
1588 */
1589 struct brcmf_fws_mac_descriptor *entry;
1590 enum brcmf_fws_skb_state state;
1591 struct sk_buff *pktout;
1592 int rc = 0;
1593 int fifo;
1594 int hslot;
1595 u8 ifidx;
1596
1597 fifo = brcmf_skb_if_flags_get_field(skb, FIFO);
1598 state = brcmf_skbcb(skb)->state;
1599 entry = brcmf_skbcb(skb)->mac;
1600
1601 if (entry != NULL) {
1602 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1603 /* wl-header is saved for suppressed packets */
1604 pktout = brcmu_pktq_penq_head(&entry->psq, 2 * fifo + 1,
1605 skb);
1606 if (pktout == NULL) {
1607 brcmf_err("suppress queue full\n");
1608 rc = -ENOSPC;
1609 }
1610 } else {
1611 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1612
1613 /* remove header first */
1614 rc = brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb);
1615 if (rc) {
1616 brcmf_err("header removal failed\n");
1617 /* free the hanger slot */
1618 brcmf_fws_hanger_poppkt(&fws->hanger, hslot,
1619 &pktout, true);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001620 rc = -EINVAL;
1621 goto fail;
1622 }
1623
1624 /* delay-q packets are going to delay-q */
1625 pktout = brcmu_pktq_penq_head(&entry->psq,
1626 2 * fifo, skb);
1627 if (pktout == NULL) {
1628 brcmf_err("delay queue full\n");
1629 rc = -ENOSPC;
1630 }
1631
1632 /* free the hanger slot */
1633 brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &pktout,
1634 true);
1635
1636 /* decrement sequence count */
1637 entry->seq[fifo]--;
1638 }
1639 /*
1640 if this packet did not count against FIFO credit, it must have
1641 taken a requested_credit from the firmware (for pspoll etc.)
1642 */
1643 if (!(brcmf_skbcb(skb)->if_flags &
1644 BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK))
1645 entry->requested_credit++;
1646 } else {
1647 brcmf_err("no mac entry linked\n");
1648 rc = -ENOENT;
1649 }
1650
1651
1652fail:
Arend van Spriel67994fa2013-04-17 21:25:56 +02001653 if (rc) {
1654 brcmf_txfinalize(fws->drvr, skb, false);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001655 fws->stats.rollback_failed++;
Arend van Spriel67994fa2013-04-17 21:25:56 +02001656 } else
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001657 fws->stats.rollback_success++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001658}
1659
1660static int brcmf_fws_consume_credit(struct brcmf_fws_info *fws, int fifo,
1661 struct sk_buff *skb)
1662{
1663 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1664 int *credit = &fws->fifo_credit[fifo];
1665 int use_credit = 1;
1666
1667 brcmf_dbg(TRACE, "enter: ac=%d, credits=%d\n", fifo, *credit);
1668
1669 if (entry->requested_credit > 0) {
1670 /*
1671 * if the packet was pulled out while destination is in
1672 * closed state but had a non-zero packets requested,
1673 * then this should not count against the FIFO credit.
1674 * That is due to the fact that the firmware will
1675 * most likely hold onto this packet until a suitable
1676 * time later to push it to the appropriate AC FIFO.
1677 */
1678 entry->requested_credit--;
1679 if (entry->state == BRCMF_FWS_STATE_CLOSE)
1680 use_credit = 0;
1681 } else if (entry->requested_packet > 0) {
1682 entry->requested_packet--;
1683 brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1684 if (entry->state == BRCMF_FWS_STATE_CLOSE)
1685 use_credit = 0;
1686 }
1687 brcmf_skb_if_flags_set_field(skb, CREDITCHECK, use_credit);
1688 if (!use_credit) {
1689 brcmf_dbg(TRACE, "exit: no creditcheck set\n");
1690 return 0;
1691 }
1692
1693 if (!(*credit)) {
1694 brcmf_dbg(TRACE, "exit: credits depleted\n");
1695 return -ENAVAIL;
1696 }
1697 (*credit)--;
1698 if (!(*credit))
1699 fws->fifo_credit_map &= ~(1 << fifo);
1700 brcmf_dbg(TRACE, "exit: ac=%d, credits=%d\n", fifo, *credit);
1701 return 0;
1702}
1703
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001704static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
1705 struct sk_buff *skb)
1706{
1707 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1708 struct brcmf_fws_mac_descriptor *entry;
1709 struct brcmf_bus *bus = fws->drvr->bus_if;
1710 int rc;
1711
1712 entry = skcb->mac;
1713 if (IS_ERR(entry))
1714 return PTR_ERR(entry);
1715
1716 rc = brcmf_fws_precommit_skb(fws, fifo, skb);
1717 if (rc < 0) {
1718 fws->stats.generic_error++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001719 goto rollback;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001720 }
1721
1722 rc = brcmf_bus_txdata(bus, skb);
1723 if (rc < 0)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001724 goto rollback;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001725
1726 entry->seq[fifo]++;
1727 fws->stats.pkt2bus++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001728 if (brcmf_skbcb(skb)->if_flags & BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK) {
1729 fws->stats.send_pkts[fifo]++;
1730 fws->stats.fifo_credits_sent[fifo]++;
1731 }
1732
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001733 return rc;
1734
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001735rollback:
Arend van Spriel88f656f2013-04-17 21:25:57 +02001736 brcmf_fws_rollback_toq(fws, skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001737 return rc;
1738}
1739
1740int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
1741{
1742 struct brcmf_pub *drvr = ifp->drvr;
1743 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1744 struct ethhdr *eh = (struct ethhdr *)(skb->data);
1745 ulong flags;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001746 int fifo = BRCMF_FWS_FIFO_BCMC;
1747 bool multicast = is_multicast_ether_addr(eh->h_dest);
1748
1749 /* determine the priority */
1750 if (!skb->priority)
1751 skb->priority = cfg80211_classify8021d(skb);
1752
1753 drvr->tx_multicast += !!multicast;
1754 if (ntohs(eh->h_proto) == ETH_P_PAE)
1755 atomic_inc(&ifp->pend_8021x_cnt);
1756
1757 if (!brcmf_fws_fc_active(drvr->fws)) {
1758 /* If the protocol uses a data header, apply it */
Arend van Spriel31ed07d2013-04-11 13:29:00 +02001759 brcmf_proto_hdrpush(drvr, ifp->ifidx, 0, skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001760
1761 /* Use bus module to send data frame */
1762 return brcmf_bus_txdata(drvr->bus_if, skb);
1763 }
1764
1765 /* set control buffer information */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001766 skcb->if_flags = 0;
Arend van Sprielf97a7f02013-04-11 13:28:58 +02001767 skcb->mac = brcmf_fws_find_mac_desc(drvr->fws, ifp, eh->h_dest);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001768 skcb->state = BRCMF_FWS_SKBSTATE_NEW;
Arend van Spriel31ed07d2013-04-11 13:29:00 +02001769 brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001770 if (!multicast)
1771 fifo = brcmf_fws_prio2fifo[skb->priority];
1772 brcmf_skb_if_flags_set_field(skb, FIFO, fifo);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001773
1774 brcmf_dbg(TRACE, "ea=%pM, multi=%d, fifo=%d\n", eh->h_dest,
1775 multicast, fifo);
1776
1777 brcmf_fws_lock(drvr, flags);
Arend van Spriel9fc60222013-04-11 17:12:04 +02001778 if (skcb->mac->suppressed ||
Arend van Sprielb75c1a32013-04-17 21:25:53 +02001779 brcmf_fws_mac_desc_closed(drvr->fws, skcb->mac, fifo) ||
Arend van Spriel9fc60222013-04-11 17:12:04 +02001780 brcmu_pktq_mlen(&skcb->mac->psq, 3 << (fifo * 2)) ||
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001781 (!multicast &&
1782 brcmf_fws_consume_credit(drvr->fws, fifo, skb) < 0)) {
1783 /* enqueue the packet in delayQ */
1784 drvr->fws->fifo_delay_map |= 1 << fifo;
1785 brcmf_fws_enq(drvr->fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
1786 } else {
1787 brcmf_fws_commit_skb(drvr->fws, fifo, skb);
1788 }
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001789 brcmf_fws_unlock(drvr, flags);
1790 return 0;
1791}
1792
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001793void brcmf_fws_reset_interface(struct brcmf_if *ifp)
1794{
1795 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1796
1797 brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
1798 if (!entry)
1799 return;
1800
Arend van Sprielce814c12013-04-03 12:40:31 +02001801 brcmf_fws_init_mac_descriptor(entry, ifp->mac_addr, ifp->ifidx);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001802}
1803
1804void brcmf_fws_add_interface(struct brcmf_if *ifp)
1805{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001806 struct brcmf_fws_info *fws = ifp->drvr->fws;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001807 struct brcmf_fws_mac_descriptor *entry;
1808
1809 brcmf_dbg(TRACE, "enter: idx=%d, mac=%pM\n",
1810 ifp->bssidx, ifp->mac_addr);
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02001811 if (!ifp->ndev || !ifp->drvr->fw_signals)
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001812 return;
1813
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001814 entry = &fws->desc.iface[ifp->ifidx];
1815 ifp->fws_desc = entry;
1816 brcmf_fws_init_mac_descriptor(entry, ifp->mac_addr, ifp->ifidx);
1817 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
1818 BRCMF_FWS_PSQ_LEN);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001819}
1820
1821void brcmf_fws_del_interface(struct brcmf_if *ifp)
1822{
1823 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
Arend van Spriel6d421e52013-04-17 21:25:55 +02001824 ulong flags;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001825
1826 brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
1827 if (!entry)
1828 return;
1829
Arend van Spriel6d421e52013-04-17 21:25:55 +02001830 brcmf_fws_lock(ifp->drvr, flags);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001831 ifp->fws_desc = NULL;
Arend van Sprielce814c12013-04-03 12:40:31 +02001832 brcmf_fws_clear_mac_descriptor(entry);
Arend van Spriela3e993c2013-04-03 12:40:36 +02001833 brcmf_fws_cleanup(ifp->drvr->fws, ifp->ifidx);
Arend van Spriel6d421e52013-04-17 21:25:55 +02001834 brcmf_fws_unlock(ifp->drvr, flags);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001835}
1836
1837static void brcmf_fws_dequeue_worker(struct work_struct *worker)
1838{
1839 struct brcmf_fws_info *fws;
1840 struct sk_buff *skb;
1841 ulong flags;
1842 int fifo;
1843 int credit;
1844
1845 fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
1846
1847 brcmf_dbg(TRACE, "enter: fws=%p\n", fws);
1848 brcmf_fws_lock(fws->drvr, flags);
1849 for (fifo = NL80211_NUM_ACS; fifo >= 0; fifo--) {
1850 brcmf_dbg(TRACE, "fifo %d credit %d\n", fifo,
1851 fws->fifo_credit[fifo]);
1852 for (credit = 0; credit < fws->fifo_credit[fifo]; /* nop */) {
1853 skb = brcmf_fws_deq(fws, fifo);
Arend van Spriel047445c2013-04-17 21:25:58 +02001854 if (!skb || brcmf_fws_commit_skb(fws, fifo, skb))
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001855 break;
Arend van Spriel047445c2013-04-17 21:25:58 +02001856 if (brcmf_skbcb(skb)->if_flags &
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001857 BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK)
1858 credit++;
1859 }
1860 fws->fifo_credit[fifo] -= credit;
1861 }
1862 brcmf_fws_unlock(fws->drvr, flags);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001863}
Arend van Sprielc7f34a62013-04-03 12:40:39 +02001864
Arend van Sprield9337412013-04-03 12:40:47 +02001865int brcmf_fws_init(struct brcmf_pub *drvr)
1866{
Arend van Spriel290fb762013-04-03 12:40:49 +02001867 u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
Arend van Sprield9337412013-04-03 12:40:47 +02001868 int rc;
1869
Arend van Spriel47829f42013-04-03 12:40:48 +02001870 if (!drvr->fw_signals)
1871 return 0;
1872
Arend van Sprield9337412013-04-03 12:40:47 +02001873 spin_lock_init(&drvr->fws_spinlock);
1874
1875 drvr->fws = kzalloc(sizeof(*(drvr->fws)), GFP_KERNEL);
1876 if (!drvr->fws) {
1877 rc = -ENOMEM;
1878 goto fail;
1879 }
1880
1881 /* set linkage back */
1882 drvr->fws->drvr = drvr;
1883 drvr->fws->fcmode = fcmode;
1884
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001885 drvr->fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
1886 if (drvr->fws->fws_wq == NULL) {
1887 brcmf_err("workqueue creation failed\n");
1888 rc = -EBADF;
1889 goto fail;
1890 }
1891 INIT_WORK(&drvr->fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
1892
Arend van Spriel290fb762013-04-03 12:40:49 +02001893 /* enable firmware signalling if fcmode active */
1894 if (drvr->fws->fcmode != BRCMF_FWS_FCMODE_NONE)
1895 tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
Arend van Spriel33753d42013-04-05 10:57:40 +02001896 BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
1897 BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE;
Arend van Spriel290fb762013-04-03 12:40:49 +02001898
Arend van Sprield9337412013-04-03 12:40:47 +02001899 rc = brcmf_fil_iovar_int_set(drvr->iflist[0], "tlv", tlv);
1900 if (rc < 0) {
1901 brcmf_err("failed to set bdcv2 tlv signaling\n");
1902 goto fail;
1903 }
1904
1905 if (brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
1906 brcmf_fws_notify_credit_map)) {
1907 brcmf_err("register credit map handler failed\n");
1908 goto fail;
1909 }
1910
1911 brcmf_fws_hanger_init(&drvr->fws->hanger);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001912 brcmf_fws_init_mac_descriptor(&drvr->fws->desc.other, NULL, 0);
1913 brcmu_pktq_init(&drvr->fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
1914 BRCMF_FWS_PSQ_LEN);
Arend van Sprield9337412013-04-03 12:40:47 +02001915
1916 /* create debugfs file for statistics */
1917 brcmf_debugfs_create_fws_stats(drvr, &drvr->fws->stats);
1918
1919 /* TODO: remove upon feature delivery */
1920 brcmf_err("%s bdcv2 tlv signaling [%x]\n",
1921 drvr->fw_signals ? "enabled" : "disabled", tlv);
1922 return 0;
1923
1924fail:
1925 /* disable flow control entirely */
1926 drvr->fw_signals = false;
1927 brcmf_fws_deinit(drvr);
1928 return rc;
1929}
1930
1931void brcmf_fws_deinit(struct brcmf_pub *drvr)
1932{
1933 struct brcmf_fws_info *fws = drvr->fws;
1934 ulong flags;
1935
Arend van Spriel47829f42013-04-03 12:40:48 +02001936 if (!fws)
1937 return;
1938
Arend van Sprield9337412013-04-03 12:40:47 +02001939 /* cleanup */
1940 brcmf_fws_lock(drvr, flags);
1941 brcmf_fws_cleanup(fws, -1);
1942 drvr->fws = NULL;
1943 brcmf_fws_unlock(drvr, flags);
1944
1945 /* free top structure */
1946 kfree(fws);
1947}
1948
Arend van Sprielc7f34a62013-04-03 12:40:39 +02001949bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
1950{
1951 if (!fws)
1952 return false;
1953
1954 brcmf_dbg(TRACE, "enter: mode=%d\n", fws->fcmode);
1955 return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
1956}
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001957
1958void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
1959{
1960 ulong flags;
1961
1962 brcmf_fws_lock(fws->drvr, flags);
1963 brcmf_fws_txstatus_process(fws, BRCMF_FWS_TXSTATUS_FW_TOSSED,
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001964 brcmf_skb_htod_tag_get_field(skb, HSLOT), 0);
1965 /* the packet never reached firmware so reclaim credit */
1966 if (fws->fcmode == BRCMF_FWS_FCMODE_EXPLICIT_CREDIT &&
1967 brcmf_skbcb(skb)->if_flags & BRCMF_SKB_IF_FLAGS_CREDITCHECK_MASK) {
1968 brcmf_fws_return_credits(fws,
1969 brcmf_skb_htod_tag_get_field(skb,
1970 FIFO),
1971 1);
1972 brcmf_fws_schedule_deq(fws);
1973 }
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001974 brcmf_fws_unlock(fws->drvr, flags);
1975}