blob: 29b1f24c2d0f92c86a55dade5e0e61a943004608 [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>
Arend van Spriel1aac1e92013-04-23 12:53:15 +020024#include <linux/jiffies.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 Sprielae664602013-06-18 13:29:24 +0200144#define BRCMF_FWS_PSQ_PREC_COUNT ((BRCMF_FWS_FIFO_COUNT + 1) * 2)
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200145#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 Sprielc7773fc2013-06-06 13:49:32 +0200159 * @BRCMF_FWS_SKBSTATE_TIM: allocated for TIM update info.
Arend van Sprielebb93882013-04-03 12:40:35 +0200160 */
161enum brcmf_fws_skb_state {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200162 BRCMF_FWS_SKBSTATE_NEW,
163 BRCMF_FWS_SKBSTATE_DELAYED,
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200164 BRCMF_FWS_SKBSTATE_SUPPRESSED,
165 BRCMF_FWS_SKBSTATE_TIM
Arend van Sprielebb93882013-04-03 12:40:35 +0200166};
167
168/**
169 * struct brcmf_skbuff_cb - control buffer associated with skbuff.
170 *
171 * @if_flags: holds interface index and packet related flags.
Arend van Sprielebb93882013-04-03 12:40:35 +0200172 * @htod: host to device packet identifier (used in PKTTAG tlv).
Arend van Sprielebb93882013-04-03 12:40:35 +0200173 * @state: transmit state of the packet.
174 * @mac: descriptor related to destination for this packet.
175 *
176 * This information is stored in control buffer struct sk_buff::cb, which
177 * provides 48 bytes of storage so this structure should not exceed that.
178 */
179struct brcmf_skbuff_cb {
180 u16 if_flags;
Arend van Sprielebb93882013-04-03 12:40:35 +0200181 u32 htod;
Arend van Sprielebb93882013-04-03 12:40:35 +0200182 enum brcmf_fws_skb_state state;
183 struct brcmf_fws_mac_descriptor *mac;
184};
185
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200186/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200187 * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
188 */
189#define brcmf_skbcb(skb) ((struct brcmf_skbuff_cb *)((skb)->cb))
190
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200191/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200192 * sk_buff control if flags
193 *
Hante Meulemanfa587d42013-06-06 13:18:03 +0200194 * b[11] - packet sent upon firmware request.
Arend van Sprielebb93882013-04-03 12:40:35 +0200195 * b[10] - packet only contains signalling data.
196 * b[9] - packet is a tx packet.
Hante Meulemanfa587d42013-06-06 13:18:03 +0200197 * b[8] - packet used requested credit
Arend van Sprielebb93882013-04-03 12:40:35 +0200198 * b[7] - interface in AP mode.
Arend van Sprielebb93882013-04-03 12:40:35 +0200199 * b[3:0] - interface index.
200 */
201#define BRCMF_SKB_IF_FLAGS_REQUESTED_MASK 0x0800
202#define BRCMF_SKB_IF_FLAGS_REQUESTED_SHIFT 11
203#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_MASK 0x0400
204#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_SHIFT 10
205#define BRCMF_SKB_IF_FLAGS_TRANSMIT_MASK 0x0200
206#define BRCMF_SKB_IF_FLAGS_TRANSMIT_SHIFT 9
Hante Meulemanfa587d42013-06-06 13:18:03 +0200207#define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_MASK 0x0100
208#define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_SHIFT 8
Arend van Sprielebb93882013-04-03 12:40:35 +0200209#define BRCMF_SKB_IF_FLAGS_IF_AP_MASK 0x0080
210#define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT 7
Arend van Sprielebb93882013-04-03 12:40:35 +0200211#define BRCMF_SKB_IF_FLAGS_INDEX_MASK 0x000f
212#define BRCMF_SKB_IF_FLAGS_INDEX_SHIFT 0
213
214#define brcmf_skb_if_flags_set_field(skb, field, value) \
215 brcmu_maskset16(&(brcmf_skbcb(skb)->if_flags), \
216 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
217 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT, (value))
218#define brcmf_skb_if_flags_get_field(skb, field) \
219 brcmu_maskget16(brcmf_skbcb(skb)->if_flags, \
220 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
221 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT)
222
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200223/*
Arend van Sprielebb93882013-04-03 12:40:35 +0200224 * sk_buff control packet identifier
225 *
226 * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
227 *
228 * - Generated at the host (e.g. dhd)
229 * - Seen as a generic sequence number by firmware except for the flags field.
230 *
231 * Generation : b[31] => generation number for this packet [host->fw]
232 * OR, current generation number [fw->host]
233 * Flags : b[30:27] => command, status flags
234 * FIFO-AC : b[26:24] => AC-FIFO id
235 * h-slot : b[23:8] => hanger-slot
236 * freerun : b[7:0] => A free running counter
237 */
238#define BRCMF_SKB_HTOD_TAG_GENERATION_MASK 0x80000000
239#define BRCMF_SKB_HTOD_TAG_GENERATION_SHIFT 31
240#define BRCMF_SKB_HTOD_TAG_FLAGS_MASK 0x78000000
241#define BRCMF_SKB_HTOD_TAG_FLAGS_SHIFT 27
242#define BRCMF_SKB_HTOD_TAG_FIFO_MASK 0x07000000
243#define BRCMF_SKB_HTOD_TAG_FIFO_SHIFT 24
244#define BRCMF_SKB_HTOD_TAG_HSLOT_MASK 0x00ffff00
245#define BRCMF_SKB_HTOD_TAG_HSLOT_SHIFT 8
246#define BRCMF_SKB_HTOD_TAG_FREERUN_MASK 0x000000ff
Hante Meulemanbe4910a2013-06-06 13:17:53 +0200247#define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT 0
Arend van Sprielebb93882013-04-03 12:40:35 +0200248
249#define brcmf_skb_htod_tag_set_field(skb, field, value) \
Arend van Spriele2432b62013-04-03 12:40:38 +0200250 brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
Arend van Sprielebb93882013-04-03 12:40:35 +0200251 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
252 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
253#define brcmf_skb_htod_tag_get_field(skb, field) \
Arend van Spriele2432b62013-04-03 12:40:38 +0200254 brcmu_maskget32(brcmf_skbcb(skb)->htod, \
Arend van Sprielebb93882013-04-03 12:40:35 +0200255 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
256 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)
257
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200258#define BRCMF_FWS_TXSTAT_GENERATION_MASK 0x80000000
259#define BRCMF_FWS_TXSTAT_GENERATION_SHIFT 31
260#define BRCMF_FWS_TXSTAT_FLAGS_MASK 0x78000000
261#define BRCMF_FWS_TXSTAT_FLAGS_SHIFT 27
262#define BRCMF_FWS_TXSTAT_FIFO_MASK 0x07000000
263#define BRCMF_FWS_TXSTAT_FIFO_SHIFT 24
264#define BRCMF_FWS_TXSTAT_HSLOT_MASK 0x00FFFF00
265#define BRCMF_FWS_TXSTAT_HSLOT_SHIFT 8
266#define BRCMF_FWS_TXSTAT_PKTID_MASK 0x00FFFFFF
267#define BRCMF_FWS_TXSTAT_PKTID_SHIFT 0
268
269#define brcmf_txstatus_get_field(txs, field) \
270 brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
271 BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)
272
Arend van Spriel1aac1e92013-04-23 12:53:15 +0200273/* How long to defer borrowing in jiffies */
274#define BRCMF_FWS_BORROW_DEFER_PERIOD (HZ / 10)
275
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200276/**
277 * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
278 *
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200279 * @BRCMF_FWS_FIFO_FIRST: first fifo, ie. background.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200280 * @BRCMF_FWS_FIFO_AC_BK: fifo for background traffic.
281 * @BRCMF_FWS_FIFO_AC_BE: fifo for best-effort traffic.
282 * @BRCMF_FWS_FIFO_AC_VI: fifo for video traffic.
283 * @BRCMF_FWS_FIFO_AC_VO: fifo for voice traffic.
284 * @BRCMF_FWS_FIFO_BCMC: fifo for broadcast/multicast (AP only).
285 * @BRCMF_FWS_FIFO_ATIM: fifo for ATIM (AP only).
286 * @BRCMF_FWS_FIFO_COUNT: number of fifos.
287 */
288enum brcmf_fws_fifo {
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200289 BRCMF_FWS_FIFO_FIRST,
290 BRCMF_FWS_FIFO_AC_BK = BRCMF_FWS_FIFO_FIRST,
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200291 BRCMF_FWS_FIFO_AC_BE,
292 BRCMF_FWS_FIFO_AC_VI,
293 BRCMF_FWS_FIFO_AC_VO,
294 BRCMF_FWS_FIFO_BCMC,
295 BRCMF_FWS_FIFO_ATIM,
296 BRCMF_FWS_FIFO_COUNT
297};
298
299/**
300 * enum brcmf_fws_txstatus - txstatus flag values.
301 *
302 * @BRCMF_FWS_TXSTATUS_DISCARD:
303 * host is free to discard the packet.
304 * @BRCMF_FWS_TXSTATUS_CORE_SUPPRESS:
305 * 802.11 core suppressed the packet.
306 * @BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS:
307 * firmware suppress the packet as device is already in PS mode.
308 * @BRCMF_FWS_TXSTATUS_FW_TOSSED:
309 * firmware tossed the packet.
Hante Meulemanfa587d42013-06-06 13:18:03 +0200310 * @BRCMF_FWS_TXSTATUS_HOST_TOSSED:
311 * host tossed the packet.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200312 */
313enum brcmf_fws_txstatus {
314 BRCMF_FWS_TXSTATUS_DISCARD,
315 BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
316 BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
Hante Meulemanfa587d42013-06-06 13:18:03 +0200317 BRCMF_FWS_TXSTATUS_FW_TOSSED,
318 BRCMF_FWS_TXSTATUS_HOST_TOSSED
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200319};
320
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200321enum brcmf_fws_fcmode {
322 BRCMF_FWS_FCMODE_NONE,
323 BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
324 BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
325};
326
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200327enum brcmf_fws_mac_desc_state {
328 BRCMF_FWS_STATE_OPEN = 1,
329 BRCMF_FWS_STATE_CLOSE
330};
331
Arend van Sprielebb93882013-04-03 12:40:35 +0200332/**
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200333 * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
334 *
335 * @occupied: slot is in use.
Arend van Sprielce814c12013-04-03 12:40:31 +0200336 * @mac_handle: handle for mac entry determined by firmware.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200337 * @interface_id: interface index.
338 * @state: current state.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200339 * @suppressed: mac entry is suppressed.
340 * @generation: generation bit.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200341 * @ac_bitmap: ac queue bitmap.
342 * @requested_credit: credits requested by firmware.
343 * @ea: ethernet address.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200344 * @seq: per-node free-running sequence.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200345 * @psq: power-save queue.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200346 * @transit_count: packet in transit to firmware.
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200347 */
348struct brcmf_fws_mac_descriptor {
Arend van Spriel8071fd62013-06-06 13:17:51 +0200349 char name[16];
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200350 u8 occupied;
Arend van Sprielce814c12013-04-03 12:40:31 +0200351 u8 mac_handle;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200352 u8 interface_id;
353 u8 state;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200354 bool suppressed;
355 u8 generation;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200356 u8 ac_bitmap;
357 u8 requested_credit;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200358 u8 requested_packet;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200359 u8 ea[ETH_ALEN];
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200360 u8 seq[BRCMF_FWS_FIFO_COUNT];
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200361 struct pktq psq;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200362 int transit_count;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200363 int suppr_transit_count;
364 bool send_tim_signal;
365 u8 traffic_pending_bmp;
366 u8 traffic_lastreported_bmp;
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200367};
368
Arend van Spriel69712802013-04-03 12:40:37 +0200369#define BRCMF_FWS_HANGER_MAXITEMS 1024
370
371/**
372 * enum brcmf_fws_hanger_item_state - state of hanger item.
373 *
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200374 * @BRCMF_FWS_HANGER_ITEM_STATE_FREE: item is free for use.
375 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE: item is in use.
376 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED: item was suppressed.
Arend van Spriel69712802013-04-03 12:40:37 +0200377 */
378enum brcmf_fws_hanger_item_state {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200379 BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
380 BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
381 BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
Arend van Spriel69712802013-04-03 12:40:37 +0200382};
383
384
385/**
386 * struct brcmf_fws_hanger_item - single entry for tx pending packet.
387 *
388 * @state: entry is either free or occupied.
Arend van Spriel69712802013-04-03 12:40:37 +0200389 * @pkt: packet itself.
390 */
391struct brcmf_fws_hanger_item {
392 enum brcmf_fws_hanger_item_state state;
Arend van Spriel69712802013-04-03 12:40:37 +0200393 struct sk_buff *pkt;
394};
395
396/**
397 * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
398 *
Arend van Spriel69712802013-04-03 12:40:37 +0200399 * @pushed: packets pushed to await txstatus.
400 * @popped: packets popped upon handling txstatus.
401 * @failed_to_push: packets that could not be pushed.
402 * @failed_to_pop: packets that could not be popped.
403 * @failed_slotfind: packets for which failed to find an entry.
404 * @slot_pos: last returned item index for a free entry.
405 * @items: array of hanger items.
406 */
407struct brcmf_fws_hanger {
408 u32 pushed;
409 u32 popped;
410 u32 failed_to_push;
411 u32 failed_to_pop;
412 u32 failed_slotfind;
413 u32 slot_pos;
414 struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
415};
416
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200417struct brcmf_fws_macdesc_table {
418 struct brcmf_fws_mac_descriptor nodes[BRCMF_FWS_MAC_DESC_TABLE_SIZE];
419 struct brcmf_fws_mac_descriptor iface[BRCMF_MAX_IFS];
420 struct brcmf_fws_mac_descriptor other;
421};
422
Arend van Spriel349e7102013-03-03 12:45:28 +0100423struct brcmf_fws_info {
424 struct brcmf_pub *drvr;
425 struct brcmf_fws_stats stats;
Arend van Spriel69712802013-04-03 12:40:37 +0200426 struct brcmf_fws_hanger hanger;
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200427 enum brcmf_fws_fcmode fcmode;
Hante Meuleman7d747032013-06-18 13:29:22 +0200428 bool bcmc_credit_check;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200429 struct brcmf_fws_macdesc_table desc;
430 struct workqueue_struct *fws_wq;
431 struct work_struct fws_dequeue_work;
432 u32 fifo_enqpkt[BRCMF_FWS_FIFO_COUNT];
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200433 int fifo_credit[BRCMF_FWS_FIFO_COUNT];
Arend van Spriel1aac1e92013-04-23 12:53:15 +0200434 int credits_borrowed[BRCMF_FWS_FIFO_AC_VO + 1];
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200435 int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
436 u32 fifo_credit_map;
437 u32 fifo_delay_map;
Arend van Spriel1aac1e92013-04-23 12:53:15 +0200438 unsigned long borrow_defer_timestamp;
Hante Meulemandf50f752013-06-06 13:17:48 +0200439 bool bus_flow_blocked;
Hante Meuleman0d24b0e2013-06-06 13:17:58 +0200440 bool creditmap_received;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200441};
442
443/*
444 * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
445 */
446static const int brcmf_fws_prio2fifo[] = {
447 BRCMF_FWS_FIFO_AC_BE,
448 BRCMF_FWS_FIFO_AC_BK,
449 BRCMF_FWS_FIFO_AC_BK,
450 BRCMF_FWS_FIFO_AC_BE,
451 BRCMF_FWS_FIFO_AC_VI,
452 BRCMF_FWS_FIFO_AC_VI,
453 BRCMF_FWS_FIFO_AC_VO,
454 BRCMF_FWS_FIFO_AC_VO
Arend van Spriel349e7102013-03-03 12:45:28 +0100455};
456
Arend van Sprielc7f34a62013-04-03 12:40:39 +0200457static int fcmode;
458module_param(fcmode, int, S_IRUSR);
459MODULE_PARM_DESC(fcmode, "mode of firmware signalled flow control");
460
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200461#define BRCMF_FWS_TLV_DEF(name, id, len) \
462 case BRCMF_FWS_TYPE_ ## name: \
463 return len;
464
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200465/**
466 * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
467 *
468 * @fws: firmware-signalling information.
469 * @id: identifier of the TLV.
470 *
471 * Return: the specified length for the given TLV; Otherwise -EINVAL.
472 */
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200473static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
474 enum brcmf_fws_tlv_type id)
475{
476 switch (id) {
477 BRCMF_FWS_TLV_DEFLIST
478 default:
Arend van Sprielbb8c8062013-04-03 12:40:30 +0200479 fws->stats.tlv_invalid_type++;
480 break;
481 }
482 return -EINVAL;
483}
484#undef BRCMF_FWS_TLV_DEF
485
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200486static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
487{
488 u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
489 return ifidx == *(int *)arg;
490}
491
492static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
493 int ifidx)
494{
495 bool (*matchfn)(struct sk_buff *, void *) = NULL;
496 struct sk_buff *skb;
497 int prec;
498
499 if (ifidx != -1)
500 matchfn = brcmf_fws_ifidx_match;
501 for (prec = 0; prec < q->num_prec; prec++) {
502 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
503 while (skb) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200504 brcmu_pkt_buf_free_skb(skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200505 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
506 }
507 }
508}
509
Arend van Spriel69712802013-04-03 12:40:37 +0200510static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
511{
512 int i;
513
Arend van Spriel69712802013-04-03 12:40:37 +0200514 memset(hanger, 0, sizeof(*hanger));
515 for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200516 hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
Arend van Spriel69712802013-04-03 12:40:37 +0200517}
518
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200519static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
Arend van Spriel69712802013-04-03 12:40:37 +0200520{
521 u32 i;
522
Arend van Spriel69712802013-04-03 12:40:37 +0200523 i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
524
525 while (i != h->slot_pos) {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200526 if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
Arend van Spriel69712802013-04-03 12:40:37 +0200527 h->slot_pos = i;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200528 goto done;
Arend van Spriel69712802013-04-03 12:40:37 +0200529 }
530 i++;
531 if (i == BRCMF_FWS_HANGER_MAXITEMS)
532 i = 0;
533 }
534 brcmf_err("all slots occupied\n");
535 h->failed_slotfind++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200536 i = BRCMF_FWS_HANGER_MAXITEMS;
537done:
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200538 return i;
Arend van Spriel69712802013-04-03 12:40:37 +0200539}
540
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200541static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
Hante Meulemanbe4910a2013-06-06 13:17:53 +0200542 struct sk_buff *pkt, u32 slot_id)
Arend van Spriel69712802013-04-03 12:40:37 +0200543{
Arend van Spriel69712802013-04-03 12:40:37 +0200544 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
545 return -ENOENT;
546
547 if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
548 brcmf_err("slot is not free\n");
549 h->failed_to_push++;
550 return -EINVAL;
551 }
552
553 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
554 h->items[slot_id].pkt = pkt;
Arend van Spriel69712802013-04-03 12:40:37 +0200555 h->pushed++;
556 return 0;
557}
558
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200559static int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
Arend van Spriel69712802013-04-03 12:40:37 +0200560 u32 slot_id, struct sk_buff **pktout,
561 bool remove_item)
562{
Arend van Spriel69712802013-04-03 12:40:37 +0200563 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
564 return -ENOENT;
565
566 if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
567 brcmf_err("entry not in use\n");
568 h->failed_to_pop++;
569 return -EINVAL;
570 }
571
572 *pktout = h->items[slot_id].pkt;
573 if (remove_item) {
574 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
575 h->items[slot_id].pkt = NULL;
Arend van Spriel69712802013-04-03 12:40:37 +0200576 h->popped++;
577 }
578 return 0;
579}
580
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200581static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
Hante Meulemanbe4910a2013-06-06 13:17:53 +0200582 u32 slot_id)
Arend van Spriel69712802013-04-03 12:40:37 +0200583{
Arend van Spriel69712802013-04-03 12:40:37 +0200584 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
585 return -ENOENT;
586
Arend van Spriel5631bec2013-06-06 13:18:07 +0200587 if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
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 Spriel84bcc0c2013-04-03 12:40:50 +0200596static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
Arend van Spriel69712802013-04-03 12:40:37 +0200597 bool (*fn)(struct sk_buff *, void *),
598 int ifidx)
599{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200600 struct brcmf_fws_hanger *h = &fws->hanger;
Arend van Spriel69712802013-04-03 12:40:37 +0200601 struct sk_buff *skb;
602 int i;
603 enum brcmf_fws_hanger_item_state s;
604
Arend van Spriel69712802013-04-03 12:40:37 +0200605 for (i = 0; i < ARRAY_SIZE(h->items); i++) {
606 s = h->items[i].state;
607 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE ||
608 s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
609 skb = h->items[i].pkt;
610 if (fn == NULL || fn(skb, &ifidx)) {
611 /* suppress packets freed from psq */
612 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE)
613 brcmu_pkt_buf_free_skb(skb);
614 h->items[i].state =
615 BRCMF_FWS_HANGER_ITEM_STATE_FREE;
616 }
617 }
618 }
619}
620
Arend van Spriel8071fd62013-06-06 13:17:51 +0200621static void brcmf_fws_macdesc_set_name(struct brcmf_fws_info *fws,
622 struct brcmf_fws_mac_descriptor *desc)
623{
624 if (desc == &fws->desc.other)
625 strlcpy(desc->name, "MAC-OTHER", sizeof(desc->name));
626 else if (desc->mac_handle)
627 scnprintf(desc->name, sizeof(desc->name), "MAC-%d:%d",
628 desc->mac_handle, desc->interface_id);
629 else
630 scnprintf(desc->name, sizeof(desc->name), "MACIF:%d",
631 desc->interface_id);
632}
633
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200634static void brcmf_fws_macdesc_init(struct brcmf_fws_mac_descriptor *desc,
635 u8 *addr, u8 ifidx)
Arend van Sprielce814c12013-04-03 12:40:31 +0200636{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200637 brcmf_dbg(TRACE,
638 "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
Arend van Sprielce814c12013-04-03 12:40:31 +0200639 desc->occupied = 1;
640 desc->state = BRCMF_FWS_STATE_OPEN;
641 desc->requested_credit = 0;
Hante Meulemanfa587d42013-06-06 13:18:03 +0200642 desc->requested_packet = 0;
Arend van Sprielce814c12013-04-03 12:40:31 +0200643 /* depending on use may need ifp->bssidx instead */
644 desc->interface_id = ifidx;
645 desc->ac_bitmap = 0xff; /* update this when handling APSD */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200646 if (addr)
647 memcpy(&desc->ea[0], addr, ETH_ALEN);
Arend van Sprielce814c12013-04-03 12:40:31 +0200648}
649
650static
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200651void brcmf_fws_macdesc_deinit(struct brcmf_fws_mac_descriptor *desc)
Arend van Sprielce814c12013-04-03 12:40:31 +0200652{
653 brcmf_dbg(TRACE,
654 "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
655 desc->occupied = 0;
656 desc->state = BRCMF_FWS_STATE_CLOSE;
657 desc->requested_credit = 0;
Hante Meulemanfa587d42013-06-06 13:18:03 +0200658 desc->requested_packet = 0;
Arend van Sprielce814c12013-04-03 12:40:31 +0200659}
660
661static struct brcmf_fws_mac_descriptor *
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200662brcmf_fws_macdesc_lookup(struct brcmf_fws_info *fws, u8 *ea)
Arend van Sprielce814c12013-04-03 12:40:31 +0200663{
664 struct brcmf_fws_mac_descriptor *entry;
665 int i;
666
Arend van Sprielce814c12013-04-03 12:40:31 +0200667 if (ea == NULL)
668 return ERR_PTR(-EINVAL);
669
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200670 entry = &fws->desc.nodes[0];
671 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
Arend van Sprielce814c12013-04-03 12:40:31 +0200672 if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
673 return entry;
674 entry++;
675 }
676
677 return ERR_PTR(-ENOENT);
678}
679
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200680static struct brcmf_fws_mac_descriptor*
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200681brcmf_fws_macdesc_find(struct brcmf_fws_info *fws, struct brcmf_if *ifp, u8 *da)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200682{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200683 struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200684 bool multicast;
Arend van Sprielf97a7f02013-04-11 13:28:58 +0200685
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200686 multicast = is_multicast_ether_addr(da);
Arend van Spriela7965fb2013-04-11 17:08:37 +0200687
Hante Meuleman5cd51c22013-06-06 13:17:56 +0200688 /* Multicast destination, STA and P2P clients get the interface entry.
689 * STA/GC gets the Mac Entry for TDLS destinations, TDLS destinations
690 * have their own entry.
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200691 */
Hante Meuleman5cd51c22013-06-06 13:17:56 +0200692 if (multicast && ifp->fws_desc) {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200693 entry = ifp->fws_desc;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200694 goto done;
Hante Meuleman5cd51c22013-06-06 13:17:56 +0200695 }
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200696
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200697 entry = brcmf_fws_macdesc_lookup(fws, da);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200698 if (IS_ERR(entry))
Hante Meuleman5cd51c22013-06-06 13:17:56 +0200699 entry = ifp->fws_desc;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200700
701done:
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200702 return entry;
703}
704
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200705static bool brcmf_fws_macdesc_closed(struct brcmf_fws_info *fws,
706 struct brcmf_fws_mac_descriptor *entry,
707 int fifo)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200708{
Arend van Sprielb75c1a32013-04-17 21:25:53 +0200709 struct brcmf_fws_mac_descriptor *if_entry;
Arend van Spriel9fc60222013-04-11 17:12:04 +0200710 bool closed;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200711
Arend van Sprielb75c1a32013-04-17 21:25:53 +0200712 /* for unique destination entries the related interface
713 * may be closed.
714 */
715 if (entry->mac_handle) {
716 if_entry = &fws->desc.iface[entry->interface_id];
717 if (if_entry->state == BRCMF_FWS_STATE_CLOSE)
718 return true;
719 }
Arend van Spriel9fc60222013-04-11 17:12:04 +0200720 /* an entry is closed when the state is closed and
721 * the firmware did not request anything.
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200722 */
Arend van Spriel9fc60222013-04-11 17:12:04 +0200723 closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
724 !entry->requested_credit && !entry->requested_packet;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200725
Arend van Spriel9fc60222013-04-11 17:12:04 +0200726 /* Or firmware does not allow traffic for given fifo */
727 return closed || !(entry->ac_bitmap & BIT(fifo));
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200728}
729
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200730static void brcmf_fws_macdesc_cleanup(struct brcmf_fws_info *fws,
731 struct brcmf_fws_mac_descriptor *entry,
732 int ifidx)
Arend van Sprielfba14002013-04-03 12:40:33 +0200733{
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200734 if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +0200735 brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
Arend van Sprielfba14002013-04-03 12:40:33 +0200736 entry->occupied = !!(entry->psq.len);
737 }
738}
739
Arend van Spriele2432b62013-04-03 12:40:38 +0200740static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
741 bool (*fn)(struct sk_buff *, void *),
742 int ifidx)
743{
744 struct brcmf_fws_hanger_item *hi;
745 struct pktq *txq;
746 struct sk_buff *skb;
747 int prec;
748 u32 hslot;
749
Arend van Spriele2432b62013-04-03 12:40:38 +0200750 txq = brcmf_bus_gettxq(fws->drvr->bus_if);
751 if (IS_ERR(txq)) {
752 brcmf_dbg(TRACE, "no txq to clean up\n");
753 return;
754 }
755
756 for (prec = 0; prec < txq->num_prec; prec++) {
757 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
758 while (skb) {
759 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
760 hi = &fws->hanger.items[hslot];
761 WARN_ON(skb != hi->pkt);
762 hi->state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
763 brcmu_pkt_buf_free_skb(skb);
764 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
765 }
766 }
767}
768
Arend van Sprielfba14002013-04-03 12:40:33 +0200769static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
770{
771 int i;
772 struct brcmf_fws_mac_descriptor *table;
Arend van Spriela3e993c2013-04-03 12:40:36 +0200773 bool (*matchfn)(struct sk_buff *, void *) = NULL;
Arend van Sprielfba14002013-04-03 12:40:33 +0200774
Arend van Sprielfba14002013-04-03 12:40:33 +0200775 if (fws == NULL)
776 return;
777
Arend van Spriela3e993c2013-04-03 12:40:36 +0200778 if (ifidx != -1)
779 matchfn = brcmf_fws_ifidx_match;
780
Arend van Sprielfba14002013-04-03 12:40:33 +0200781 /* cleanup individual nodes */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200782 table = &fws->desc.nodes[0];
783 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200784 brcmf_fws_macdesc_cleanup(fws, &table[i], ifidx);
Arend van Sprielfba14002013-04-03 12:40:33 +0200785
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200786 brcmf_fws_macdesc_cleanup(fws, &fws->desc.other, ifidx);
Arend van Spriele2432b62013-04-03 12:40:38 +0200787 brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200788 brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
789}
790
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200791static int brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200792{
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200793 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
794 u8 *wlh;
795 u16 data_offset = 0;
796 u8 fillers;
797 __le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);
798
Arend van Spriel80898a12013-06-26 14:20:20 +0200799 brcmf_dbg(TRACE, "enter: %s, idx=%d pkttag=0x%08X, hslot=%d\n",
800 entry->name, brcmf_skb_if_flags_get_field(skb, INDEX),
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200801 le32_to_cpu(pkttag), (le32_to_cpu(pkttag) >> 8) & 0xffff);
802 if (entry->send_tim_signal)
803 data_offset += 2 + BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
804
805 /* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
806 data_offset += 2 + BRCMF_FWS_TYPE_PKTTAG_LEN;
807 fillers = round_up(data_offset, 4) - data_offset;
808 data_offset += fillers;
809
810 skb_push(skb, data_offset);
811 wlh = skb->data;
812
813 wlh[0] = BRCMF_FWS_TYPE_PKTTAG;
814 wlh[1] = BRCMF_FWS_TYPE_PKTTAG_LEN;
815 memcpy(&wlh[2], &pkttag, sizeof(pkttag));
816 wlh += BRCMF_FWS_TYPE_PKTTAG_LEN + 2;
817
818 if (entry->send_tim_signal) {
819 entry->send_tim_signal = 0;
820 wlh[0] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP;
821 wlh[1] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
822 wlh[2] = entry->mac_handle;
823 wlh[3] = entry->traffic_pending_bmp;
Arend van Spriel80898a12013-06-26 14:20:20 +0200824 brcmf_dbg(TRACE, "adding TIM info: handle %d bmp 0x%X\n",
825 entry->mac_handle, entry->traffic_pending_bmp);
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200826 wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
827 entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200828 }
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200829 if (fillers)
830 memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);
831
832 brcmf_proto_hdrpush(fws->drvr, brcmf_skb_if_flags_get_field(skb, INDEX),
833 data_offset >> 2, skb);
834 return 0;
835}
836
837static bool brcmf_fws_tim_update(struct brcmf_fws_info *fws,
838 struct brcmf_fws_mac_descriptor *entry,
Arend van Spriel4e89dfc2013-06-18 13:29:23 +0200839 int fifo, bool send_immediately)
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200840{
841 struct sk_buff *skb;
842 struct brcmf_bus *bus;
843 struct brcmf_skbuff_cb *skcb;
844 s32 err;
845 u32 len;
846
847 /* check delayedQ and suppressQ in one call using bitmap */
Arend van Spriel4e89dfc2013-06-18 13:29:23 +0200848 if (brcmu_pktq_mlen(&entry->psq, 3 << (fifo * 2)) == 0)
849 entry->traffic_pending_bmp &= ~NBITVAL(fifo);
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200850 else
Arend van Spriel4e89dfc2013-06-18 13:29:23 +0200851 entry->traffic_pending_bmp |= NBITVAL(fifo);
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200852
853 entry->send_tim_signal = false;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200854 if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
855 entry->send_tim_signal = true;
Arend van Sprielc7773fc2013-06-06 13:49:32 +0200856 if (send_immediately && entry->send_tim_signal &&
857 entry->state == BRCMF_FWS_STATE_CLOSE) {
858 /* create a dummy packet and sent that. The traffic */
859 /* bitmap info will automatically be attached to that packet */
860 len = BRCMF_FWS_TYPE_PKTTAG_LEN + 2 +
861 BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2 +
862 4 + fws->drvr->hdrlen;
863 skb = brcmu_pkt_buf_get_skb(len);
864 if (skb == NULL)
865 return false;
866 skb_pull(skb, len);
867 skcb = brcmf_skbcb(skb);
868 skcb->mac = entry;
869 skcb->state = BRCMF_FWS_SKBSTATE_TIM;
870 bus = fws->drvr->bus_if;
871 err = brcmf_fws_hdrpush(fws, skb);
872 if (err == 0)
873 err = brcmf_bus_txdata(bus, skb);
874 if (err)
875 brcmu_pkt_buf_free_skb(skb);
876 return true;
877 }
878 return false;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200879}
880
881static void
882brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
883 u8 if_id)
884{
Hante Meuleman402e3ba2013-06-06 13:17:57 +0200885 struct brcmf_if *ifp = fws->drvr->iflist[!if_id ? 0 : if_id + 1];
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200886
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200887 if (WARN_ON(!ifp))
888 return;
889
890 if ((ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
891 pq->len <= BRCMF_FWS_FLOWCONTROL_LOWATER)
892 brcmf_txflowblock_if(ifp,
893 BRCMF_NETIF_STOP_REASON_FWS_FC, false);
894 if (!(ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
Arend van Spriel5631bec2013-06-06 13:18:07 +0200895 pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER) {
896 fws->stats.fws_flow_block++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200897 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
Arend van Spriel5631bec2013-06-06 13:18:07 +0200898 }
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200899 return;
Arend van Sprielfba14002013-04-03 12:40:33 +0200900}
901
Arend van Spriel349e7102013-03-03 12:45:28 +0100902static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
903{
904 brcmf_dbg(CTL, "rssi %d\n", rssi);
905 return 0;
906}
907
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200908/* using macro so sparse checking does not complain
909 * about locking imbalance.
910 */
911#define brcmf_fws_lock(drvr, flags) \
912do { \
913 flags = 0; \
914 spin_lock_irqsave(&((drvr)->fws_spinlock), (flags)); \
915} while (0)
916
917/* using macro so sparse checking does not complain
918 * about locking imbalance.
919 */
920#define brcmf_fws_unlock(drvr, flags) \
921 spin_unlock_irqrestore(&((drvr)->fws_spinlock), (flags))
922
Arend van Sprielce814c12013-04-03 12:40:31 +0200923static
924int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
925{
926 struct brcmf_fws_mac_descriptor *entry, *existing;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200927 ulong flags;
Arend van Sprielce814c12013-04-03 12:40:31 +0200928 u8 mac_handle;
929 u8 ifidx;
930 u8 *addr;
931
932 mac_handle = *data++;
933 ifidx = *data++;
934 addr = data;
935
Arend van Spriel84bcc0c2013-04-03 12:40:50 +0200936 entry = &fws->desc.nodes[mac_handle & 0x1F];
Arend van Sprielce814c12013-04-03 12:40:31 +0200937 if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
Arend van Spriel9a83f1e2013-04-23 12:53:13 +0200938 if (entry->occupied) {
Arend van Spriel8071fd62013-06-06 13:17:51 +0200939 brcmf_dbg(TRACE, "deleting %s mac %pM\n",
940 entry->name, addr);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200941 brcmf_fws_lock(fws->drvr, flags);
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200942 brcmf_fws_macdesc_cleanup(fws, entry, -1);
943 brcmf_fws_macdesc_deinit(entry);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200944 brcmf_fws_unlock(fws->drvr, flags);
Arend van Spriel9a83f1e2013-04-23 12:53:13 +0200945 } else
Arend van Sprielce814c12013-04-03 12:40:31 +0200946 fws->stats.mac_update_failed++;
Arend van Sprielce814c12013-04-03 12:40:31 +0200947 return 0;
948 }
949
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200950 existing = brcmf_fws_macdesc_lookup(fws, addr);
Arend van Sprielce814c12013-04-03 12:40:31 +0200951 if (IS_ERR(existing)) {
952 if (!entry->occupied) {
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200953 brcmf_fws_lock(fws->drvr, flags);
Arend van Sprielce814c12013-04-03 12:40:31 +0200954 entry->mac_handle = mac_handle;
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200955 brcmf_fws_macdesc_init(entry, addr, ifidx);
Arend van Spriel8071fd62013-06-06 13:17:51 +0200956 brcmf_fws_macdesc_set_name(fws, entry);
Arend van Sprielce814c12013-04-03 12:40:31 +0200957 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
958 BRCMF_FWS_PSQ_LEN);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200959 brcmf_fws_unlock(fws->drvr, flags);
Arend van Spriel8071fd62013-06-06 13:17:51 +0200960 brcmf_dbg(TRACE, "add %s mac %pM\n", entry->name, addr);
Arend van Sprielce814c12013-04-03 12:40:31 +0200961 } else {
962 fws->stats.mac_update_failed++;
963 }
964 } else {
965 if (entry != existing) {
Arend van Spriel8071fd62013-06-06 13:17:51 +0200966 brcmf_dbg(TRACE, "copy mac %s\n", existing->name);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200967 brcmf_fws_lock(fws->drvr, flags);
Arend van Sprielce814c12013-04-03 12:40:31 +0200968 memcpy(entry, existing,
969 offsetof(struct brcmf_fws_mac_descriptor, psq));
970 entry->mac_handle = mac_handle;
Arend van Spriel1ab083a2013-06-18 13:29:25 +0200971 brcmf_fws_macdesc_deinit(existing);
Arend van Spriel8071fd62013-06-06 13:17:51 +0200972 brcmf_fws_macdesc_set_name(fws, entry);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200973 brcmf_fws_unlock(fws->drvr, flags);
Arend van Spriel8071fd62013-06-06 13:17:51 +0200974 brcmf_dbg(TRACE, "relocate %s mac %pM\n", entry->name,
975 addr);
Arend van Sprielce814c12013-04-03 12:40:31 +0200976 } else {
977 brcmf_dbg(TRACE, "use existing\n");
978 WARN_ON(entry->mac_handle != mac_handle);
979 /* TODO: what should we do here: continue, reinit, .. */
980 }
981 }
982 return 0;
983}
984
Arend van Spriel33753d42013-04-05 10:57:40 +0200985static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
986 u8 type, u8 *data)
987{
988 struct brcmf_fws_mac_descriptor *entry;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200989 ulong flags;
Arend van Spriel33753d42013-04-05 10:57:40 +0200990 u8 mac_handle;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200991 int ret;
Arend van Spriel33753d42013-04-05 10:57:40 +0200992
993 mac_handle = data[0];
994 entry = &fws->desc.nodes[mac_handle & 0x1F];
995 if (!entry->occupied) {
996 fws->stats.mac_ps_update_failed++;
997 return -ESRCH;
998 }
Arend van Sprielc6a681ab2013-06-26 14:20:21 +0200999 brcmf_fws_lock(fws->drvr, flags);
Arend van Sprielc7773fc2013-06-06 13:49:32 +02001000 /* a state update should wipe old credits */
Arend van Spriel33753d42013-04-05 10:57:40 +02001001 entry->requested_credit = 0;
Arend van Sprielc7773fc2013-06-06 13:49:32 +02001002 entry->requested_packet = 0;
Arend van Spriel33753d42013-04-05 10:57:40 +02001003 if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
1004 entry->state = BRCMF_FWS_STATE_OPEN;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001005 ret = BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel33753d42013-04-05 10:57:40 +02001006 } else {
1007 entry->state = BRCMF_FWS_STATE_CLOSE;
Arend van Sprielc7773fc2013-06-06 13:49:32 +02001008 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BK, false);
1009 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BE, false);
1010 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VI, false);
1011 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VO, true);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001012 ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel33753d42013-04-05 10:57:40 +02001013 }
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001014 brcmf_fws_unlock(fws->drvr, flags);
1015 return ret;
Arend van Spriel33753d42013-04-05 10:57:40 +02001016}
1017
1018static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
1019 u8 type, u8 *data)
1020{
1021 struct brcmf_fws_mac_descriptor *entry;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001022 ulong flags;
Arend van Spriel33753d42013-04-05 10:57:40 +02001023 u8 ifidx;
1024 int ret;
1025
1026 ifidx = data[0];
1027
Arend van Spriel33753d42013-04-05 10:57:40 +02001028 if (ifidx >= BRCMF_MAX_IFS) {
1029 ret = -ERANGE;
1030 goto fail;
1031 }
1032
1033 entry = &fws->desc.iface[ifidx];
1034 if (!entry->occupied) {
1035 ret = -ESRCH;
1036 goto fail;
1037 }
1038
Arend van Spriel8071fd62013-06-06 13:17:51 +02001039 brcmf_dbg(TRACE, "%s (%d): %s\n", brcmf_fws_get_tlv_name(type), type,
1040 entry->name);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001041 brcmf_fws_lock(fws->drvr, flags);
Arend van Spriel33753d42013-04-05 10:57:40 +02001042 switch (type) {
1043 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1044 entry->state = BRCMF_FWS_STATE_OPEN;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001045 ret = BRCMF_FWS_RET_OK_SCHEDULE;
1046 break;
Arend van Spriel33753d42013-04-05 10:57:40 +02001047 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1048 entry->state = BRCMF_FWS_STATE_CLOSE;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001049 ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1050 break;
Arend van Spriel33753d42013-04-05 10:57:40 +02001051 default:
1052 ret = -EINVAL;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001053 brcmf_fws_unlock(fws->drvr, flags);
1054 goto fail;
Arend van Spriel33753d42013-04-05 10:57:40 +02001055 }
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001056 brcmf_fws_unlock(fws->drvr, flags);
1057 return ret;
1058
Arend van Spriel33753d42013-04-05 10:57:40 +02001059fail:
1060 fws->stats.if_update_failed++;
1061 return ret;
1062}
1063
Arend van Spriel5f38b682013-04-05 10:57:41 +02001064static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
1065 u8 *data)
1066{
1067 struct brcmf_fws_mac_descriptor *entry;
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001068 ulong flags;
Arend van Spriel5f38b682013-04-05 10:57:41 +02001069
1070 entry = &fws->desc.nodes[data[1] & 0x1F];
1071 if (!entry->occupied) {
1072 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1073 fws->stats.credit_request_failed++;
1074 else
1075 fws->stats.packet_request_failed++;
1076 return -ESRCH;
1077 }
1078
Arend van Spriel8071fd62013-06-06 13:17:51 +02001079 brcmf_dbg(TRACE, "%s (%d): %s cnt %d bmp %d\n",
1080 brcmf_fws_get_tlv_name(type), type, entry->name,
1081 data[0], data[2]);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001082 brcmf_fws_lock(fws->drvr, flags);
Arend van Spriel5f38b682013-04-05 10:57:41 +02001083 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1084 entry->requested_credit = data[0];
1085 else
1086 entry->requested_packet = data[0];
1087
1088 entry->ac_bitmap = data[2];
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001089 brcmf_fws_unlock(fws->drvr, flags);
Arend van Spriela5e9d802013-04-17 21:25:54 +02001090 return BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel5f38b682013-04-05 10:57:41 +02001091}
1092
Hante Meulemanfa587d42013-06-06 13:18:03 +02001093static void
1094brcmf_fws_macdesc_use_req_credit(struct brcmf_fws_mac_descriptor *entry,
1095 struct sk_buff *skb)
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001096{
Hante Meulemanfa587d42013-06-06 13:18:03 +02001097 if (entry->requested_credit > 0) {
1098 entry->requested_credit--;
1099 brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1100 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 1);
1101 if (entry->state != BRCMF_FWS_STATE_CLOSE)
1102 brcmf_err("requested credit set while mac not closed!\n");
1103 } else if (entry->requested_packet > 0) {
1104 entry->requested_packet--;
1105 brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1106 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1107 if (entry->state != BRCMF_FWS_STATE_CLOSE)
1108 brcmf_err("requested packet set while mac not closed!\n");
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001109 } else {
Hante Meulemanfa587d42013-06-06 13:18:03 +02001110 brcmf_skb_if_flags_set_field(skb, REQUESTED, 0);
1111 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001112 }
Hante Meulemanfa587d42013-06-06 13:18:03 +02001113}
1114
1115static void brcmf_fws_macdesc_return_req_credit(struct sk_buff *skb)
1116{
1117 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1118
1119 if ((brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) &&
1120 (entry->state == BRCMF_FWS_STATE_CLOSE))
1121 entry->requested_credit++;
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001122}
1123
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001124static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
1125 u8 fifo, u8 credits)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001126{
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001127 int lender_ac;
1128 int *borrowed;
1129 int *fifo_credit;
1130
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001131 if (!credits)
1132 return;
1133
Hante Meulemanafc3bbf2013-06-06 13:17:54 +02001134 fws->fifo_credit_map |= 1 << fifo;
1135
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001136 if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
1137 (fws->credits_borrowed[0])) {
1138 for (lender_ac = BRCMF_FWS_FIFO_AC_VO; lender_ac >= 0;
1139 lender_ac--) {
1140 borrowed = &fws->credits_borrowed[lender_ac];
1141 if (*borrowed) {
1142 fws->fifo_credit_map |= (1 << lender_ac);
1143 fifo_credit = &fws->fifo_credit[lender_ac];
1144 if (*borrowed >= credits) {
1145 *borrowed -= credits;
1146 *fifo_credit += credits;
1147 return;
1148 } else {
1149 credits -= *borrowed;
1150 *fifo_credit += *borrowed;
1151 *borrowed = 0;
1152 }
1153 }
1154 }
1155 }
1156
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001157 fws->fifo_credit[fifo] += credits;
1158}
1159
1160static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
1161{
1162 /* only schedule dequeue when there are credits for delayed traffic */
1163 if (fws->fifo_credit_map & fws->fifo_delay_map)
1164 queue_work(fws->fws_wq, &fws->fws_dequeue_work);
1165}
1166
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001167static int brcmf_fws_enq(struct brcmf_fws_info *fws,
1168 enum brcmf_fws_skb_state state, int fifo,
1169 struct sk_buff *p)
1170{
1171 int prec = 2 * fifo;
1172 u32 *qfull_stat = &fws->stats.delayq_full_error;
1173
1174 struct brcmf_fws_mac_descriptor *entry;
1175
1176 entry = brcmf_skbcb(p)->mac;
1177 if (entry == NULL) {
1178 brcmf_err("no mac descriptor found for skb %p\n", p);
1179 return -ENOENT;
1180 }
1181
Arend van Spriel8071fd62013-06-06 13:17:51 +02001182 brcmf_dbg(DATA, "enter: fifo %d skb %p\n", fifo, p);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001183 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1184 prec += 1;
1185 qfull_stat = &fws->stats.supprq_full_error;
1186 }
1187
1188 if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
1189 *qfull_stat += 1;
1190 return -ENFILE;
1191 }
1192
1193 /* increment total enqueued packet count */
1194 fws->fifo_delay_map |= 1 << fifo;
1195 fws->fifo_enqpkt[fifo]++;
1196
1197 /* update the sk_buff state */
1198 brcmf_skbcb(p)->state = state;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001199
1200 /*
1201 * A packet has been pushed so update traffic
1202 * availability bitmap, if applicable
1203 */
Arend van Sprielc7773fc2013-06-06 13:49:32 +02001204 brcmf_fws_tim_update(fws, entry, fifo, true);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001205 brcmf_fws_flow_control_check(fws, &entry->psq,
1206 brcmf_skb_if_flags_get_field(p, INDEX));
1207 return 0;
1208}
1209
1210static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
1211{
1212 struct brcmf_fws_mac_descriptor *table;
1213 struct brcmf_fws_mac_descriptor *entry;
1214 struct sk_buff *p;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001215 int num_nodes;
1216 int node_pos;
1217 int prec_out;
Arend van Spriel07529d22013-04-17 21:25:49 +02001218 int pmsk;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001219 int i;
1220
1221 table = (struct brcmf_fws_mac_descriptor *)&fws->desc;
1222 num_nodes = sizeof(fws->desc) / sizeof(struct brcmf_fws_mac_descriptor);
1223 node_pos = fws->deq_node_pos[fifo];
1224
1225 for (i = 0; i < num_nodes; i++) {
1226 entry = &table[(node_pos + i) % num_nodes];
Arend van Sprielb75c1a32013-04-17 21:25:53 +02001227 if (!entry->occupied ||
Arend van Spriel1ab083a2013-06-18 13:29:25 +02001228 brcmf_fws_macdesc_closed(fws, entry, fifo))
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001229 continue;
1230
1231 if (entry->suppressed)
1232 pmsk = 2;
Arend van Spriel07529d22013-04-17 21:25:49 +02001233 else
1234 pmsk = 3;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001235 p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
1236 if (p == NULL) {
1237 if (entry->suppressed) {
Hante Meuleman8c5140f2013-06-06 13:18:01 +02001238 if (entry->suppr_transit_count)
1239 continue;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001240 entry->suppressed = false;
1241 p = brcmu_pktq_mdeq(&entry->psq,
1242 1 << (fifo * 2), &prec_out);
1243 }
1244 }
1245 if (p == NULL)
1246 continue;
1247
Hante Meulemanfa587d42013-06-06 13:18:03 +02001248 brcmf_fws_macdesc_use_req_credit(entry, p);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001249
1250 /* move dequeue position to ensure fair round-robin */
1251 fws->deq_node_pos[fifo] = (node_pos + i + 1) % num_nodes;
1252 brcmf_fws_flow_control_check(fws, &entry->psq,
1253 brcmf_skb_if_flags_get_field(p,
1254 INDEX)
1255 );
1256 /*
1257 * A packet has been picked up, update traffic
1258 * availability bitmap, if applicable
1259 */
Arend van Sprielc7773fc2013-06-06 13:49:32 +02001260 brcmf_fws_tim_update(fws, entry, fifo, false);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001261
1262 /*
1263 * decrement total enqueued fifo packets and
1264 * clear delay bitmap if done.
1265 */
1266 fws->fifo_enqpkt[fifo]--;
1267 if (fws->fifo_enqpkt[fifo] == 0)
1268 fws->fifo_delay_map &= ~(1 << fifo);
1269 goto done;
1270 }
1271 p = NULL;
1272done:
Arend van Spriel8071fd62013-06-06 13:17:51 +02001273 brcmf_dbg(DATA, "exit: fifo %d skb %p\n", fifo, p);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001274 return p;
1275}
1276
1277static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
1278 struct sk_buff *skb, u32 genbit)
1279{
1280 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1281 u32 hslot;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001282 int ret;
Hante Meuleman2747e5f2013-06-06 13:17:59 +02001283 u8 ifidx;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001284
1285 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1286
1287 /* this packet was suppressed */
Hante Meulemanbe4910a2013-06-06 13:17:53 +02001288 if (!entry->suppressed) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001289 entry->suppressed = true;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001290 entry->suppr_transit_count = entry->transit_count;
Hante Meuleman8c5140f2013-06-06 13:18:01 +02001291 brcmf_dbg(DATA, "suppress %s: transit %d\n",
1292 entry->name, entry->transit_count);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001293 }
1294
1295 entry->generation = genbit;
1296
Hante Meuleman2747e5f2013-06-06 13:17:59 +02001297 ret = brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb);
1298 if (ret == 0)
1299 ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo,
1300 skb);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001301 if (ret != 0) {
Hante Meuleman2747e5f2013-06-06 13:17:59 +02001302 /* suppress q is full or hdrpull failed, drop this packet */
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001303 brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1304 true);
1305 } else {
1306 /*
1307 * Mark suppressed to avoid a double free during
1308 * wlfc cleanup
1309 */
Hante Meulemanbe4910a2013-06-06 13:17:53 +02001310 brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001311 }
1312
1313 return ret;
1314}
1315
1316static int
Arend van Spriel8071fd62013-06-06 13:17:51 +02001317brcmf_fws_txs_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001318 u32 genbit)
1319{
1320 u32 fifo;
1321 int ret;
1322 bool remove_from_hanger = true;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001323 struct sk_buff *skb;
Arend van Spriel8071fd62013-06-06 13:17:51 +02001324 struct brcmf_skbuff_cb *skcb;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001325 struct brcmf_fws_mac_descriptor *entry = NULL;
1326
Arend van Spriel8071fd62013-06-06 13:17:51 +02001327 brcmf_dbg(DATA, "flags %d\n", flags);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001328
1329 if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
1330 fws->stats.txs_discard++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001331 else if (flags == BRCMF_FWS_TXSTATUS_CORE_SUPPRESS) {
1332 fws->stats.txs_supp_core++;
1333 remove_from_hanger = false;
1334 } else if (flags == BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS) {
1335 fws->stats.txs_supp_ps++;
1336 remove_from_hanger = false;
Arend van Spriel5631bec2013-06-06 13:18:07 +02001337 } else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001338 fws->stats.txs_tossed++;
Arend van Spriel5631bec2013-06-06 13:18:07 +02001339 else if (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)
1340 fws->stats.txs_host_tossed++;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001341 else
1342 brcmf_err("unexpected txstatus\n");
1343
1344 ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001345 remove_from_hanger);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001346 if (ret != 0) {
1347 brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
1348 return ret;
1349 }
1350
Arend van Spriel8071fd62013-06-06 13:17:51 +02001351 skcb = brcmf_skbcb(skb);
1352 entry = skcb->mac;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001353 if (WARN_ON(!entry)) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001354 brcmu_pkt_buf_free_skb(skb);
1355 return -EINVAL;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001356 }
Hante Meuleman8c5140f2013-06-06 13:18:01 +02001357 entry->transit_count--;
1358 if (entry->suppressed && entry->suppr_transit_count)
1359 entry->suppr_transit_count--;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001360
Arend van Spriel8071fd62013-06-06 13:17:51 +02001361 brcmf_dbg(DATA, "%s flags %X htod %X\n", entry->name, skcb->if_flags,
1362 skcb->htod);
1363
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001364 /* pick up the implicit credit from this packet */
1365 fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
Hante Meulemanfa587d42013-06-06 13:18:03 +02001366 if ((fws->fcmode == BRCMF_FWS_FCMODE_IMPLIED_CREDIT) ||
1367 (brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) ||
1368 (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)) {
1369 brcmf_fws_return_credits(fws, fifo, 1);
1370 brcmf_fws_schedule_deq(fws);
1371 }
1372 brcmf_fws_macdesc_return_req_credit(skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001373
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001374 if (!remove_from_hanger)
1375 ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb, genbit);
1376
Hante Meuleman8c5140f2013-06-06 13:18:01 +02001377 if (remove_from_hanger || ret)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001378 brcmf_txfinalize(fws->drvr, skb, true);
Hante Meuleman8c5140f2013-06-06 13:18:01 +02001379
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001380 return 0;
1381}
1382
1383static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
1384 u8 *data)
1385{
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001386 ulong flags;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001387 int i;
1388
1389 if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
1390 brcmf_dbg(INFO, "ignored\n");
Arend van Spriela5e9d802013-04-17 21:25:54 +02001391 return BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001392 }
1393
Arend van Spriel8071fd62013-06-06 13:17:51 +02001394 brcmf_dbg(DATA, "enter: data %pM\n", data);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001395 brcmf_fws_lock(fws->drvr, flags);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001396 for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
1397 brcmf_fws_return_credits(fws, i, data[i]);
1398
Arend van Spriel8071fd62013-06-06 13:17:51 +02001399 brcmf_dbg(DATA, "map: credit %x delay %x\n", fws->fifo_credit_map,
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001400 fws->fifo_delay_map);
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001401 brcmf_fws_unlock(fws->drvr, flags);
Arend van Spriela5e9d802013-04-17 21:25:54 +02001402 return BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001403}
1404
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001405static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
1406{
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001407 ulong lflags;
Arend van Spriela2ffc562013-04-05 10:57:39 +02001408 __le32 status_le;
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001409 u32 status;
1410 u32 hslot;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001411 u32 genbit;
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001412 u8 flags;
1413
1414 fws->stats.txs_indicate++;
Arend van Spriela2ffc562013-04-05 10:57:39 +02001415 memcpy(&status_le, data, sizeof(status_le));
1416 status = le32_to_cpu(status_le);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001417 flags = brcmf_txstatus_get_field(status, FLAGS);
1418 hslot = brcmf_txstatus_get_field(status, HSLOT);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001419 genbit = brcmf_txstatus_get_field(status, GENERATION);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001420
Arend van Sprielc6a681ab2013-06-26 14:20:21 +02001421 brcmf_fws_lock(fws->drvr, lflags);
1422 brcmf_fws_txs_process(fws, flags, hslot, genbit);
1423 brcmf_fws_unlock(fws->drvr, lflags);
1424 return BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001425}
1426
Arend van Spriel349e7102013-03-03 12:45:28 +01001427static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
1428{
1429 __le32 timestamp;
1430
1431 memcpy(&timestamp, &data[2], sizeof(timestamp));
Arend van Spriel8071fd62013-06-06 13:17:51 +02001432 brcmf_dbg(CTL, "received: seq %d, timestamp %d\n", data[1],
Arend van Spriel349e7102013-03-03 12:45:28 +01001433 le32_to_cpu(timestamp));
1434 return 0;
1435}
1436
Arend van Spriel43fa6352013-04-03 12:40:32 +02001437static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
1438 const struct brcmf_event_msg *e,
1439 void *data)
1440{
1441 struct brcmf_fws_info *fws = ifp->drvr->fws;
1442 int i;
1443 ulong flags;
1444 u8 *credits = data;
1445
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001446 if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
1447 brcmf_err("event payload too small (%d)\n", e->datalen);
1448 return -EINVAL;
1449 }
Hante Meuleman0d24b0e2013-06-06 13:17:58 +02001450 if (fws->creditmap_received)
1451 return 0;
1452
1453 fws->creditmap_received = true;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001454
1455 brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001456 brcmf_fws_lock(ifp->drvr, flags);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001457 for (i = 0; i < ARRAY_SIZE(fws->fifo_credit); i++) {
1458 if (*credits)
1459 fws->fifo_credit_map |= 1 << i;
1460 else
1461 fws->fifo_credit_map &= ~(1 << i);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001462 fws->fifo_credit[i] = *credits++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001463 }
1464 brcmf_fws_schedule_deq(fws);
Arend van Spriel43fa6352013-04-03 12:40:32 +02001465 brcmf_fws_unlock(ifp->drvr, flags);
1466 return 0;
1467}
1468
Hante Meuleman7d747032013-06-18 13:29:22 +02001469static int brcmf_fws_notify_bcmc_credit_support(struct brcmf_if *ifp,
1470 const struct brcmf_event_msg *e,
1471 void *data)
1472{
1473 struct brcmf_fws_info *fws = ifp->drvr->fws;
1474 ulong flags;
1475
1476 brcmf_fws_lock(ifp->drvr, flags);
1477 if (fws)
1478 fws->bcmc_credit_check = true;
1479 brcmf_fws_unlock(ifp->drvr, flags);
1480 return 0;
1481}
1482
Arend van Spriel349e7102013-03-03 12:45:28 +01001483int brcmf_fws_hdrpull(struct brcmf_pub *drvr, int ifidx, s16 signal_len,
1484 struct sk_buff *skb)
1485{
1486 struct brcmf_fws_info *fws = drvr->fws;
Arend van Spriel349e7102013-03-03 12:45:28 +01001487 u8 *signal_data;
1488 s16 data_len;
1489 u8 type;
1490 u8 len;
1491 u8 *data;
Arend van Spriela5e9d802013-04-17 21:25:54 +02001492 s32 status;
1493 s32 err;
Arend van Spriel349e7102013-03-03 12:45:28 +01001494
Arend van Spriel8071fd62013-06-06 13:17:51 +02001495 brcmf_dbg(HDRS, "enter: ifidx %d, skblen %u, sig %d\n",
Arend van Spriel349e7102013-03-03 12:45:28 +01001496 ifidx, skb->len, signal_len);
1497
1498 WARN_ON(signal_len > skb->len);
1499
1500 /* if flow control disabled, skip to packet data and leave */
1501 if (!signal_len || !drvr->fw_signals) {
1502 skb_pull(skb, signal_len);
1503 return 0;
1504 }
1505
Arend van Spriel349e7102013-03-03 12:45:28 +01001506 fws->stats.header_pulls++;
1507 data_len = signal_len;
1508 signal_data = skb->data;
1509
Arend van Spriela5e9d802013-04-17 21:25:54 +02001510 status = BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel349e7102013-03-03 12:45:28 +01001511 while (data_len > 0) {
1512 /* extract tlv info */
1513 type = signal_data[0];
1514
1515 /* FILLER type is actually not a TLV, but
1516 * a single byte that can be skipped.
1517 */
1518 if (type == BRCMF_FWS_TYPE_FILLER) {
1519 signal_data += 1;
1520 data_len -= 1;
1521 continue;
1522 }
1523 len = signal_data[1];
1524 data = signal_data + 2;
1525
Arend van Spriel8071fd62013-06-06 13:17:51 +02001526 brcmf_dbg(HDRS, "tlv type=%s (%d), len=%d (%d)\n",
1527 brcmf_fws_get_tlv_name(type), type, len,
1528 brcmf_fws_get_tlv_len(fws, type));
Arend van Spriel2af15582013-04-03 12:40:42 +02001529
Arend van Spriel349e7102013-03-03 12:45:28 +01001530 /* abort parsing when length invalid */
1531 if (data_len < len + 2)
1532 break;
1533
Arend van Sprielf55d94a2013-06-06 13:17:46 +02001534 if (len < brcmf_fws_get_tlv_len(fws, type))
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001535 break;
1536
Arend van Spriela5e9d802013-04-17 21:25:54 +02001537 err = BRCMF_FWS_RET_OK_NOSCHEDULE;
Arend van Spriel349e7102013-03-03 12:45:28 +01001538 switch (type) {
Arend van Spriel349e7102013-03-03 12:45:28 +01001539 case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001540 case BRCMF_FWS_TYPE_COMP_TXSTATUS:
Arend van Sprielce814c12013-04-03 12:40:31 +02001541 break;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001542 case BRCMF_FWS_TYPE_MACDESC_ADD:
1543 case BRCMF_FWS_TYPE_MACDESC_DEL:
Arend van Sprielce814c12013-04-03 12:40:31 +02001544 brcmf_fws_macdesc_indicate(fws, type, data);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001545 break;
Arend van Spriel33753d42013-04-05 10:57:40 +02001546 case BRCMF_FWS_TYPE_MAC_OPEN:
1547 case BRCMF_FWS_TYPE_MAC_CLOSE:
Arend van Spriela5e9d802013-04-17 21:25:54 +02001548 err = brcmf_fws_macdesc_state_indicate(fws, type, data);
Arend van Spriel33753d42013-04-05 10:57:40 +02001549 break;
1550 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1551 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
Arend van Spriela5e9d802013-04-17 21:25:54 +02001552 err = brcmf_fws_interface_state_indicate(fws, type,
1553 data);
Arend van Spriel33753d42013-04-05 10:57:40 +02001554 break;
Arend van Spriel5f38b682013-04-05 10:57:41 +02001555 case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
1556 case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
Arend van Spriela5e9d802013-04-17 21:25:54 +02001557 err = brcmf_fws_request_indicate(fws, type, data);
Arend van Spriel5f38b682013-04-05 10:57:41 +02001558 break;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001559 case BRCMF_FWS_TYPE_TXSTATUS:
1560 brcmf_fws_txstatus_indicate(fws, data);
1561 break;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001562 case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
Arend van Spriela5e9d802013-04-17 21:25:54 +02001563 err = brcmf_fws_fifocreditback_indicate(fws, data);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001564 break;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001565 case BRCMF_FWS_TYPE_RSSI:
1566 brcmf_fws_rssi_indicate(fws, *data);
Arend van Spriel349e7102013-03-03 12:45:28 +01001567 break;
1568 case BRCMF_FWS_TYPE_TRANS_ID:
Arend van Spriel349e7102013-03-03 12:45:28 +01001569 brcmf_fws_dbg_seqnum_check(fws, data);
1570 break;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001571 case BRCMF_FWS_TYPE_PKTTAG:
1572 case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
Arend van Spriel349e7102013-03-03 12:45:28 +01001573 default:
1574 fws->stats.tlv_invalid_type++;
1575 break;
1576 }
Arend van Spriela5e9d802013-04-17 21:25:54 +02001577 if (err == BRCMF_FWS_RET_OK_SCHEDULE)
1578 status = BRCMF_FWS_RET_OK_SCHEDULE;
Arend van Spriel349e7102013-03-03 12:45:28 +01001579 signal_data += len + 2;
1580 data_len -= len + 2;
1581 }
1582
1583 if (data_len != 0)
1584 fws->stats.tlv_parse_failed++;
1585
Arend van Spriela5e9d802013-04-17 21:25:54 +02001586 if (status == BRCMF_FWS_RET_OK_SCHEDULE)
1587 brcmf_fws_schedule_deq(fws);
1588
Arend van Spriel349e7102013-03-03 12:45:28 +01001589 /* signalling processing result does
1590 * not affect the actual ethernet packet.
1591 */
1592 skb_pull(skb, signal_len);
1593
1594 /* this may be a signal-only packet
1595 */
1596 if (skb->len == 0)
1597 fws->stats.header_only_pkt++;
1598
Arend van Spriel349e7102013-03-03 12:45:28 +01001599 return 0;
1600}
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001601
Arend van Spriel456d0682013-06-26 14:35:10 +02001602static void brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001603 struct sk_buff *p)
1604{
1605 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1606 struct brcmf_fws_mac_descriptor *entry = skcb->mac;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001607 u8 flags;
1608
Hante Meulemanbe4910a2013-06-06 13:17:53 +02001609 brcmf_skb_if_flags_set_field(p, TRANSMIT, 1);
Hante Meulemanbe4910a2013-06-06 13:17:53 +02001610 brcmf_skb_htod_tag_set_field(p, GENERATION, entry->generation);
1611 flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
Hante Meulemanfa587d42013-06-06 13:18:03 +02001612 if (brcmf_skb_if_flags_get_field(p, REQUESTED)) {
Hante Meulemanbe4910a2013-06-06 13:17:53 +02001613 /*
1614 * Indicate that this packet is being sent in response to an
1615 * explicit request from the firmware side.
1616 */
1617 flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
1618 }
1619 brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
Arend van Spriel456d0682013-06-26 14:35:10 +02001620 brcmf_fws_hdrpush(fws, p);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001621}
1622
Arend van Spriel456d0682013-06-26 14:35:10 +02001623static void brcmf_fws_rollback_toq(struct brcmf_fws_info *fws,
1624 struct sk_buff *skb, int fifo)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001625{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001626 struct brcmf_fws_mac_descriptor *entry;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001627 struct sk_buff *pktout;
Arend van Spriel456d0682013-06-26 14:35:10 +02001628 int qidx, hslot;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001629 int rc = 0;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001630
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001631 entry = brcmf_skbcb(skb)->mac;
Arend van Spriel456d0682013-06-26 14:35:10 +02001632 if (entry->occupied) {
1633 qidx = 2 * fifo;
1634 if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
1635 qidx++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001636
Arend van Spriel456d0682013-06-26 14:35:10 +02001637 pktout = brcmu_pktq_penq_head(&entry->psq, qidx, skb);
1638 if (pktout == NULL) {
1639 brcmf_err("%s queue %d full\n", entry->name, qidx);
1640 rc = -ENOSPC;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001641 }
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001642 } else {
Arend van Spriel456d0682013-06-26 14:35:10 +02001643 brcmf_err("%s entry removed\n", entry->name);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001644 rc = -ENOENT;
1645 }
1646
Arend van Spriel67994fa2013-04-17 21:25:56 +02001647 if (rc) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001648 fws->stats.rollback_failed++;
Arend van Spriel456d0682013-06-26 14:35:10 +02001649 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
Hante Meulemanfa587d42013-06-06 13:18:03 +02001650 brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED,
1651 hslot, 0);
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001652 } else {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001653 fws->stats.rollback_success++;
Hante Meulemanfa587d42013-06-06 13:18:03 +02001654 brcmf_fws_return_credits(fws, fifo, 1);
1655 brcmf_fws_macdesc_return_req_credit(skb);
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001656 }
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001657}
1658
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001659static int brcmf_fws_borrow_credit(struct brcmf_fws_info *fws)
1660{
1661 int lender_ac;
1662
Hante Meulemanafc3bbf2013-06-06 13:17:54 +02001663 if (time_after(fws->borrow_defer_timestamp, jiffies)) {
1664 fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001665 return -ENAVAIL;
Hante Meulemanafc3bbf2013-06-06 13:17:54 +02001666 }
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001667
1668 for (lender_ac = 0; lender_ac <= BRCMF_FWS_FIFO_AC_VO; lender_ac++) {
1669 if (fws->fifo_credit[lender_ac]) {
1670 fws->credits_borrowed[lender_ac]++;
1671 fws->fifo_credit[lender_ac]--;
1672 if (fws->fifo_credit[lender_ac] == 0)
1673 fws->fifo_credit_map &= ~(1 << lender_ac);
Hante Meulemanafc3bbf2013-06-06 13:17:54 +02001674 fws->fifo_credit_map |= (1 << BRCMF_FWS_FIFO_AC_BE);
Arend van Spriel8071fd62013-06-06 13:17:51 +02001675 brcmf_dbg(DATA, "borrow credit from: %d\n", lender_ac);
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001676 return 0;
1677 }
1678 }
Hante Meulemanafc3bbf2013-06-06 13:17:54 +02001679 fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001680 return -ENAVAIL;
1681}
1682
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001683static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
1684 struct sk_buff *skb)
1685{
1686 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1687 struct brcmf_fws_mac_descriptor *entry;
1688 struct brcmf_bus *bus = fws->drvr->bus_if;
1689 int rc;
Hante Meuleman2747e5f2013-06-06 13:17:59 +02001690 u8 ifidx;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001691
1692 entry = skcb->mac;
1693 if (IS_ERR(entry))
1694 return PTR_ERR(entry);
1695
Arend van Spriel456d0682013-06-26 14:35:10 +02001696 brcmf_fws_precommit_skb(fws, fifo, skb);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001697 rc = brcmf_bus_txdata(bus, skb);
Arend van Spriel456d0682013-06-26 14:35:10 +02001698 brcmf_dbg(DATA, "%s flags %X htod %X bus_tx %d\n", entry->name,
1699 skcb->if_flags, skcb->htod, rc);
Hante Meuleman2747e5f2013-06-06 13:17:59 +02001700 if (rc < 0) {
1701 brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001702 goto rollback;
Hante Meuleman2747e5f2013-06-06 13:17:59 +02001703 }
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001704
Hante Meuleman8c5140f2013-06-06 13:18:01 +02001705 entry->transit_count++;
1706 if (entry->suppressed)
1707 entry->suppr_transit_count++;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001708 fws->stats.pkt2bus++;
Arend van Sprielcf3a6872013-06-06 13:18:02 +02001709 fws->stats.send_pkts[fifo]++;
Hante Meulemanfa587d42013-06-06 13:18:03 +02001710 if (brcmf_skb_if_flags_get_field(skb, REQUESTED))
1711 fws->stats.requested_sent[fifo]++;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001712
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001713 return rc;
1714
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001715rollback:
Arend van Spriel51f6dd92013-06-06 13:17:47 +02001716 brcmf_fws_rollback_toq(fws, skb, fifo);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001717 return rc;
1718}
1719
Arend van Spriel456d0682013-06-26 14:35:10 +02001720static int brcmf_fws_assign_htod(struct brcmf_fws_info *fws, struct sk_buff *p,
1721 int fifo)
1722{
1723 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1724 int rc, hslot;
1725
1726 hslot = brcmf_fws_hanger_get_free_slot(&fws->hanger);
1727 brcmf_skb_htod_tag_set_field(p, HSLOT, hslot);
1728 brcmf_skb_htod_tag_set_field(p, FREERUN, skcb->mac->seq[fifo]);
1729 brcmf_skb_htod_tag_set_field(p, FIFO, fifo);
1730 rc = brcmf_fws_hanger_pushpkt(&fws->hanger, p, hslot);
1731 if (!rc)
1732 skcb->mac->seq[fifo]++;
1733 else
1734 fws->stats.generic_error++;
1735 return rc;
1736}
1737
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001738int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
1739{
1740 struct brcmf_pub *drvr = ifp->drvr;
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001741 struct brcmf_fws_info *fws = drvr->fws;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001742 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1743 struct ethhdr *eh = (struct ethhdr *)(skb->data);
1744 ulong flags;
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001745 int fifo = BRCMF_FWS_FIFO_BCMC;
1746 bool multicast = is_multicast_ether_addr(eh->h_dest);
Arend van Spriel28a905b2013-07-22 20:31:22 +02001747 bool pae = eh->h_proto == htons(ETH_P_PAE);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001748
1749 /* determine the priority */
1750 if (!skb->priority)
1751 skb->priority = cfg80211_classify8021d(skb);
1752
1753 drvr->tx_multicast += !!multicast;
Arend van Spriel28a905b2013-07-22 20:31:22 +02001754 if (pae)
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001755 atomic_inc(&ifp->pend_8021x_cnt);
1756
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001757 if (!brcmf_fws_fc_active(fws)) {
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001758 /* 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 Spriel3edc1cf2013-04-03 12:40:40 +02001767 skcb->state = BRCMF_FWS_SKBSTATE_NEW;
Arend van Spriel31ed07d2013-04-11 13:29:00 +02001768 brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001769 if (!multicast)
1770 fifo = brcmf_fws_prio2fifo[skb->priority];
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001771
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001772 brcmf_fws_lock(drvr, flags);
Arend van Spriel20863742013-06-26 14:20:14 +02001773 if (fifo != BRCMF_FWS_FIFO_AC_BE && fifo < BRCMF_FWS_FIFO_BCMC)
1774 fws->borrow_defer_timestamp = jiffies +
1775 BRCMF_FWS_BORROW_DEFER_PERIOD;
Arend van Spriel456d0682013-06-26 14:35:10 +02001776
1777 skcb->mac = brcmf_fws_macdesc_find(fws, ifp, eh->h_dest);
1778 brcmf_dbg(DATA, "%s mac %pM multi %d fifo %d\n", skcb->mac->name,
1779 eh->h_dest, multicast, fifo);
1780 if (!brcmf_fws_assign_htod(fws, skb, fifo)) {
1781 brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
1782 brcmf_fws_schedule_deq(fws);
1783 } else {
1784 brcmf_err("drop skb: no hanger slot\n");
Arend van Spriel28a905b2013-07-22 20:31:22 +02001785 if (pae) {
1786 atomic_dec(&ifp->pend_8021x_cnt);
1787 if (waitqueue_active(&ifp->pend_8021x_wait))
1788 wake_up(&ifp->pend_8021x_wait);
1789 }
Arend van Spriel456d0682013-06-26 14:35:10 +02001790 brcmu_pkt_buf_free_skb(skb);
1791 }
Arend van Spriel3edc1cf2013-04-03 12:40:40 +02001792 brcmf_fws_unlock(drvr, flags);
1793 return 0;
1794}
1795
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001796void brcmf_fws_reset_interface(struct brcmf_if *ifp)
1797{
1798 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1799
1800 brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
1801 if (!entry)
1802 return;
1803
Arend van Spriel1ab083a2013-06-18 13:29:25 +02001804 brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001805}
1806
1807void brcmf_fws_add_interface(struct brcmf_if *ifp)
1808{
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001809 struct brcmf_fws_info *fws = ifp->drvr->fws;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001810 struct brcmf_fws_mac_descriptor *entry;
1811
Arend van Spriel01b8e7d2013-04-05 10:57:51 +02001812 if (!ifp->ndev || !ifp->drvr->fw_signals)
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001813 return;
1814
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001815 entry = &fws->desc.iface[ifp->ifidx];
1816 ifp->fws_desc = entry;
Arend van Spriel1ab083a2013-06-18 13:29:25 +02001817 brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
Arend van Spriel8071fd62013-06-06 13:17:51 +02001818 brcmf_fws_macdesc_set_name(fws, entry);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001819 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
1820 BRCMF_FWS_PSQ_LEN);
Arend van Spriel8071fd62013-06-06 13:17:51 +02001821 brcmf_dbg(TRACE, "added %s\n", entry->name);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001822}
1823
1824void brcmf_fws_del_interface(struct brcmf_if *ifp)
1825{
1826 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
Arend van Spriel6d421e52013-04-17 21:25:55 +02001827 ulong flags;
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001828
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001829 if (!entry)
1830 return;
1831
Arend van Spriel6d421e52013-04-17 21:25:55 +02001832 brcmf_fws_lock(ifp->drvr, flags);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001833 ifp->fws_desc = NULL;
Arend van Spriel8071fd62013-06-06 13:17:51 +02001834 brcmf_dbg(TRACE, "deleting %s\n", entry->name);
Arend van Spriel1ab083a2013-06-18 13:29:25 +02001835 brcmf_fws_macdesc_deinit(entry);
Arend van Spriela3e993c2013-04-03 12:40:36 +02001836 brcmf_fws_cleanup(ifp->drvr->fws, ifp->ifidx);
Arend van Spriel6d421e52013-04-17 21:25:55 +02001837 brcmf_fws_unlock(ifp->drvr, flags);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001838}
1839
1840static void brcmf_fws_dequeue_worker(struct work_struct *worker)
1841{
1842 struct brcmf_fws_info *fws;
1843 struct sk_buff *skb;
1844 ulong flags;
1845 int fifo;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001846
1847 fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
1848
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001849 brcmf_fws_lock(fws->drvr, flags);
Arend van Spriel2a5d7b02013-06-26 14:20:15 +02001850 for (fifo = BRCMF_FWS_FIFO_BCMC; fifo >= 0 && !fws->bus_flow_blocked;
Hante Meulemandf50f752013-06-06 13:17:48 +02001851 fifo--) {
Hante Meuleman7d747032013-06-18 13:29:22 +02001852 while ((fws->fifo_credit[fifo]) || ((!fws->bcmc_credit_check) &&
1853 (fifo == BRCMF_FWS_FIFO_BCMC))) {
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001854 skb = brcmf_fws_deq(fws, fifo);
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001855 if (!skb)
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001856 break;
Hante Meulemanfa587d42013-06-06 13:18:03 +02001857 fws->fifo_credit[fifo]--;
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001858 if (brcmf_fws_commit_skb(fws, fifo, skb))
1859 break;
Hante Meulemandf50f752013-06-06 13:17:48 +02001860 if (fws->bus_flow_blocked)
1861 break;
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001862 }
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001863 if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
Arend van Spriel289ec1c2013-06-06 13:17:50 +02001864 (fws->fifo_credit[fifo] == 0) &&
Hante Meulemandf50f752013-06-06 13:17:48 +02001865 (!fws->bus_flow_blocked)) {
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001866 while (brcmf_fws_borrow_credit(fws) == 0) {
1867 skb = brcmf_fws_deq(fws, fifo);
1868 if (!skb) {
1869 brcmf_fws_return_credits(fws, fifo, 1);
1870 break;
1871 }
Hante Meulemaneb2410c2013-06-06 13:18:00 +02001872 if (brcmf_fws_commit_skb(fws, fifo, skb))
1873 break;
Hante Meulemandf50f752013-06-06 13:17:48 +02001874 if (fws->bus_flow_blocked)
1875 break;
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001876 }
Arend van Spriel1aac1e92013-04-23 12:53:15 +02001877 }
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001878 }
1879 brcmf_fws_unlock(fws->drvr, flags);
Arend van Sprielbb8c8062013-04-03 12:40:30 +02001880}
Arend van Sprielc7f34a62013-04-03 12:40:39 +02001881
Arend van Sprield9337412013-04-03 12:40:47 +02001882int brcmf_fws_init(struct brcmf_pub *drvr)
1883{
Arend van Spriel290fb762013-04-03 12:40:49 +02001884 u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
Arend van Sprield9337412013-04-03 12:40:47 +02001885 int rc;
1886
Arend van Spriel47829f42013-04-03 12:40:48 +02001887 if (!drvr->fw_signals)
1888 return 0;
1889
Arend van Sprield9337412013-04-03 12:40:47 +02001890 spin_lock_init(&drvr->fws_spinlock);
1891
1892 drvr->fws = kzalloc(sizeof(*(drvr->fws)), GFP_KERNEL);
1893 if (!drvr->fws) {
1894 rc = -ENOMEM;
1895 goto fail;
1896 }
1897
1898 /* set linkage back */
1899 drvr->fws->drvr = drvr;
1900 drvr->fws->fcmode = fcmode;
1901
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001902 drvr->fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
1903 if (drvr->fws->fws_wq == NULL) {
1904 brcmf_err("workqueue creation failed\n");
1905 rc = -EBADF;
1906 goto fail;
1907 }
1908 INIT_WORK(&drvr->fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
1909
Arend van Spriel290fb762013-04-03 12:40:49 +02001910 /* enable firmware signalling if fcmode active */
1911 if (drvr->fws->fcmode != BRCMF_FWS_FCMODE_NONE)
1912 tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
Arend van Spriel33753d42013-04-05 10:57:40 +02001913 BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
1914 BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE;
Arend van Spriel290fb762013-04-03 12:40:49 +02001915
Arend van Spriela786b382013-04-23 12:53:14 +02001916 rc = brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
1917 brcmf_fws_notify_credit_map);
Arend van Sprield9337412013-04-03 12:40:47 +02001918 if (rc < 0) {
Arend van Spriela786b382013-04-23 12:53:14 +02001919 brcmf_err("register credit map handler failed\n");
Arend van Sprield9337412013-04-03 12:40:47 +02001920 goto fail;
1921 }
Hante Meuleman7d747032013-06-18 13:29:22 +02001922 rc = brcmf_fweh_register(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT,
1923 brcmf_fws_notify_bcmc_credit_support);
1924 if (rc < 0) {
1925 brcmf_err("register bcmc credit handler failed\n");
1926 brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
1927 goto fail;
1928 }
Arend van Sprield9337412013-04-03 12:40:47 +02001929
Arend van Spriela786b382013-04-23 12:53:14 +02001930 /* setting the iovar may fail if feature is unsupported
1931 * so leave the rc as is so driver initialization can
1932 * continue.
1933 */
1934 if (brcmf_fil_iovar_int_set(drvr->iflist[0], "tlv", tlv)) {
1935 brcmf_err("failed to set bdcv2 tlv signaling\n");
1936 goto fail_event;
Arend van Sprield9337412013-04-03 12:40:47 +02001937 }
1938
1939 brcmf_fws_hanger_init(&drvr->fws->hanger);
Arend van Spriel1ab083a2013-06-18 13:29:25 +02001940 brcmf_fws_macdesc_init(&drvr->fws->desc.other, NULL, 0);
Arend van Spriel8071fd62013-06-06 13:17:51 +02001941 brcmf_fws_macdesc_set_name(drvr->fws, &drvr->fws->desc.other);
Arend van Spriel84bcc0c2013-04-03 12:40:50 +02001942 brcmu_pktq_init(&drvr->fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
1943 BRCMF_FWS_PSQ_LEN);
Arend van Sprield9337412013-04-03 12:40:47 +02001944
1945 /* create debugfs file for statistics */
1946 brcmf_debugfs_create_fws_stats(drvr, &drvr->fws->stats);
1947
Arend van Spriel8071fd62013-06-06 13:17:51 +02001948 brcmf_dbg(INFO, "%s bdcv2 tlv signaling [%x]\n",
Arend van Sprield9337412013-04-03 12:40:47 +02001949 drvr->fw_signals ? "enabled" : "disabled", tlv);
1950 return 0;
1951
Arend van Spriela786b382013-04-23 12:53:14 +02001952fail_event:
Hante Meuleman7d747032013-06-18 13:29:22 +02001953 brcmf_fweh_unregister(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT);
Arend van Spriela786b382013-04-23 12:53:14 +02001954 brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
Arend van Sprield9337412013-04-03 12:40:47 +02001955fail:
Arend van Sprield9337412013-04-03 12:40:47 +02001956 brcmf_fws_deinit(drvr);
1957 return rc;
1958}
1959
1960void brcmf_fws_deinit(struct brcmf_pub *drvr)
1961{
1962 struct brcmf_fws_info *fws = drvr->fws;
1963 ulong flags;
1964
Arend van Spriel47829f42013-04-03 12:40:48 +02001965 if (!fws)
1966 return;
1967
Arend van Spriela786b382013-04-23 12:53:14 +02001968 /* disable firmware signalling entirely
1969 * to avoid using the workqueue.
1970 */
1971 drvr->fw_signals = false;
1972
1973 if (drvr->fws->fws_wq)
1974 destroy_workqueue(drvr->fws->fws_wq);
1975
Arend van Sprield9337412013-04-03 12:40:47 +02001976 /* cleanup */
1977 brcmf_fws_lock(drvr, flags);
1978 brcmf_fws_cleanup(fws, -1);
1979 drvr->fws = NULL;
1980 brcmf_fws_unlock(drvr, flags);
1981
1982 /* free top structure */
1983 kfree(fws);
1984}
1985
Arend van Sprielc7f34a62013-04-03 12:40:39 +02001986bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
1987{
1988 if (!fws)
1989 return false;
1990
Arend van Sprielc7f34a62013-04-03 12:40:39 +02001991 return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
1992}
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001993
1994void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
1995{
1996 ulong flags;
Hante Meulemanfa587d42013-06-06 13:18:03 +02001997 u32 hslot;
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02001998
Arend van Sprielc7773fc2013-06-06 13:49:32 +02001999 if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_TIM) {
2000 brcmu_pkt_buf_free_skb(skb);
2001 return;
2002 }
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02002003 brcmf_fws_lock(fws->drvr, flags);
Hante Meulemanfa587d42013-06-06 13:18:03 +02002004 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
2005 brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED, hslot, 0);
Arend van Spriel0f8b5cc2013-04-03 12:40:41 +02002006 brcmf_fws_unlock(fws->drvr, flags);
2007}
Hante Meulemandf50f752013-06-06 13:17:48 +02002008
2009void brcmf_fws_bus_blocked(struct brcmf_pub *drvr, bool flow_blocked)
2010{
2011 struct brcmf_fws_info *fws = drvr->fws;
2012
2013 fws->bus_flow_blocked = flow_blocked;
2014 if (!flow_blocked)
2015 brcmf_fws_schedule_deq(fws);
Arend van Spriel5631bec2013-06-06 13:18:07 +02002016 else
2017 fws->stats.bus_flow_block++;
Hante Meulemandf50f752013-06-06 13:17:48 +02002018}