Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / net / wireless / brcm80211 / brcmfmac / fwsignal.c
1 /*
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>
17 #include <linux/module.h>
18 #include <linux/if_ether.h>
19 #include <linux/spinlock.h>
20 #include <linux/skbuff.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/err.h>
24 #include <linux/jiffies.h>
25 #include <net/cfg80211.h>
26
27 #include <brcmu_utils.h>
28 #include <brcmu_wifi.h>
29 #include "dhd.h"
30 #include "dhd_proto.h"
31 #include "dhd_dbg.h"
32 #include "dhd_bus.h"
33 #include "fwil.h"
34 #include "fwil_types.h"
35 #include "fweh.h"
36 #include "fwsignal.h"
37 #include "p2p.h"
38 #include "wl_cfg80211.h"
39
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) \
65         BRCMF_FWS_TLV_DEF(FIFO_CREDITBACK, 11, 6) \
66         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
73 /*
74  * 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,
78 enum brcmf_fws_tlv_type {
79         BRCMF_FWS_TLV_DEFLIST
80         BRCMF_FWS_TYPE_INVALID
81 };
82 #undef BRCMF_FWS_TLV_DEF
83
84 /*
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),
89 enum brcmf_fws_tlv_len {
90         BRCMF_FWS_TLV_DEFLIST
91 };
92 #undef BRCMF_FWS_TLV_DEF
93
94 #ifdef DEBUG
95 /*
96  * brcmf_fws_tlv_names - array of tlv names.
97  */
98 #define BRCMF_FWS_TLV_DEF(name, id, len) \
99         { id, #name },
100 static 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
108 static 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
119 static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
120 {
121         return "NODEBUG";
122 }
123 #endif /* DEBUG */
124
125 /*
126  * flags used to enable tlv signalling from firmware.
127  */
128 #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
135
136 #define BRCMF_FWS_MAC_DESC_TABLE_SIZE                   32
137 #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
141 #define BRCMF_FWS_FLOWCONTROL_HIWATER                   128
142 #define BRCMF_FWS_FLOWCONTROL_LOWATER                   64
143
144 #define BRCMF_FWS_PSQ_PREC_COUNT                ((BRCMF_FWS_FIFO_COUNT + 1) * 2)
145 #define BRCMF_FWS_PSQ_LEN                               256
146
147 #define BRCMF_FWS_HTOD_FLAG_PKTFROMHOST                 0x01
148 #define BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED               0x02
149
150 #define BRCMF_FWS_RET_OK_NOSCHEDULE     0
151 #define BRCMF_FWS_RET_OK_SCHEDULE       1
152
153 /**
154  * enum brcmf_fws_skb_state - indicates processing state of skb.
155  *
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.
159  * @BRCMF_FWS_SKBSTATE_TIM: allocated for TIM update info.
160  */
161 enum brcmf_fws_skb_state {
162         BRCMF_FWS_SKBSTATE_NEW,
163         BRCMF_FWS_SKBSTATE_DELAYED,
164         BRCMF_FWS_SKBSTATE_SUPPRESSED,
165         BRCMF_FWS_SKBSTATE_TIM
166 };
167
168 /**
169  * struct brcmf_skbuff_cb - control buffer associated with skbuff.
170  *
171  * @if_flags: holds interface index and packet related flags.
172  * @htod: host to device packet identifier (used in PKTTAG tlv).
173  * @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  */
179 struct brcmf_skbuff_cb {
180         u16 if_flags;
181         u32 htod;
182         enum brcmf_fws_skb_state state;
183         struct brcmf_fws_mac_descriptor *mac;
184 };
185
186 /*
187  * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
188  */
189 #define brcmf_skbcb(skb)        ((struct brcmf_skbuff_cb *)((skb)->cb))
190
191 /*
192  * sk_buff control if flags
193  *
194  *      b[11]  - packet sent upon firmware request.
195  *      b[10]  - packet only contains signalling data.
196  *      b[9]   - packet is a tx packet.
197  *      b[8]   - packet used requested credit
198  *      b[7]   - interface in AP mode.
199  *      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
207 #define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_MASK      0x0100
208 #define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_SHIFT     8
209 #define BRCMF_SKB_IF_FLAGS_IF_AP_MASK           0x0080
210 #define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT          7
211 #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
223 /*
224  * 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
247 #define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT                0
248
249 #define brcmf_skb_htod_tag_set_field(skb, field, value) \
250         brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
251                         BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
252                         BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
253 #define brcmf_skb_htod_tag_get_field(skb, field) \
254         brcmu_maskget32(brcmf_skbcb(skb)->htod, \
255                         BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
256                         BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)
257
258 #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
273 /* How long to defer borrowing in jiffies */
274 #define BRCMF_FWS_BORROW_DEFER_PERIOD           (HZ / 10)
275
276 /**
277  * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
278  *
279  * @BRCMF_FWS_FIFO_FIRST: first fifo, ie. background.
280  * @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  */
288 enum brcmf_fws_fifo {
289         BRCMF_FWS_FIFO_FIRST,
290         BRCMF_FWS_FIFO_AC_BK = BRCMF_FWS_FIFO_FIRST,
291         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.
310  * @BRCMF_FWS_TXSTATUS_HOST_TOSSED:
311  *      host tossed the packet.
312  */
313 enum brcmf_fws_txstatus {
314         BRCMF_FWS_TXSTATUS_DISCARD,
315         BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
316         BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
317         BRCMF_FWS_TXSTATUS_FW_TOSSED,
318         BRCMF_FWS_TXSTATUS_HOST_TOSSED
319 };
320
321 enum brcmf_fws_fcmode {
322         BRCMF_FWS_FCMODE_NONE,
323         BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
324         BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
325 };
326
327 enum brcmf_fws_mac_desc_state {
328         BRCMF_FWS_STATE_OPEN = 1,
329         BRCMF_FWS_STATE_CLOSE
330 };
331
332 /**
333  * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
334  *
335  * @occupied: slot is in use.
336  * @mac_handle: handle for mac entry determined by firmware.
337  * @interface_id: interface index.
338  * @state: current state.
339  * @suppressed: mac entry is suppressed.
340  * @generation: generation bit.
341  * @ac_bitmap: ac queue bitmap.
342  * @requested_credit: credits requested by firmware.
343  * @ea: ethernet address.
344  * @seq: per-node free-running sequence.
345  * @psq: power-save queue.
346  * @transit_count: packet in transit to firmware.
347  */
348 struct brcmf_fws_mac_descriptor {
349         char name[16];
350         u8 occupied;
351         u8 mac_handle;
352         u8 interface_id;
353         u8 state;
354         bool suppressed;
355         u8 generation;
356         u8 ac_bitmap;
357         u8 requested_credit;
358         u8 requested_packet;
359         u8 ea[ETH_ALEN];
360         u8 seq[BRCMF_FWS_FIFO_COUNT];
361         struct pktq psq;
362         int transit_count;
363         int suppr_transit_count;
364         bool send_tim_signal;
365         u8 traffic_pending_bmp;
366         u8 traffic_lastreported_bmp;
367 };
368
369 #define BRCMF_FWS_HANGER_MAXITEMS       1024
370
371 /**
372  * enum brcmf_fws_hanger_item_state - state of hanger item.
373  *
374  * @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.
377  */
378 enum brcmf_fws_hanger_item_state {
379         BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
380         BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
381         BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
382 };
383
384
385 /**
386  * struct brcmf_fws_hanger_item - single entry for tx pending packet.
387  *
388  * @state: entry is either free or occupied.
389  * @pkt: packet itself.
390  */
391 struct brcmf_fws_hanger_item {
392         enum brcmf_fws_hanger_item_state state;
393         struct sk_buff *pkt;
394 };
395
396 /**
397  * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
398  *
399  * @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  */
407 struct 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
417 struct 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
423 struct brcmf_fws_info {
424         struct brcmf_pub *drvr;
425         struct brcmf_fws_stats stats;
426         struct brcmf_fws_hanger hanger;
427         enum brcmf_fws_fcmode fcmode;
428         bool bcmc_credit_check;
429         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];
433         int fifo_credit[BRCMF_FWS_FIFO_COUNT];
434         int credits_borrowed[BRCMF_FWS_FIFO_AC_VO + 1];
435         int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
436         u32 fifo_credit_map;
437         u32 fifo_delay_map;
438         unsigned long borrow_defer_timestamp;
439         bool bus_flow_blocked;
440         bool creditmap_received;
441 };
442
443 /*
444  * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
445  */
446 static 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
455 };
456
457 static int fcmode;
458 module_param(fcmode, int, S_IRUSR);
459 MODULE_PARM_DESC(fcmode, "mode of firmware signalled flow control");
460
461 #define BRCMF_FWS_TLV_DEF(name, id, len) \
462         case BRCMF_FWS_TYPE_ ## name: \
463                 return len;
464
465 /**
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  */
473 static 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:
479                 fws->stats.tlv_invalid_type++;
480                 break;
481         }
482         return -EINVAL;
483 }
484 #undef BRCMF_FWS_TLV_DEF
485
486 static 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
492 static 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) {
504                         brcmu_pkt_buf_free_skb(skb);
505                         skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
506                 }
507         }
508 }
509
510 static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
511 {
512         int i;
513
514         memset(hanger, 0, sizeof(*hanger));
515         for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
516                 hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
517 }
518
519 static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
520 {
521         u32 i;
522
523         i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
524
525         while (i != h->slot_pos) {
526                 if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
527                         h->slot_pos = i;
528                         goto done;
529                 }
530                 i++;
531                 if (i == BRCMF_FWS_HANGER_MAXITEMS)
532                         i = 0;
533         }
534         brcmf_err("all slots occupied\n");
535         h->failed_slotfind++;
536         i = BRCMF_FWS_HANGER_MAXITEMS;
537 done:
538         return i;
539 }
540
541 static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
542                                     struct sk_buff *pkt, u32 slot_id)
543 {
544         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;
555         h->pushed++;
556         return 0;
557 }
558
559 static int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
560                                           u32 slot_id, struct sk_buff **pktout,
561                                           bool remove_item)
562 {
563         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;
576                 h->popped++;
577         }
578         return 0;
579 }
580
581 static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
582                                             u32 slot_id)
583 {
584         if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
585                 return -ENOENT;
586
587         if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
588                 brcmf_err("entry not in use\n");
589                 return -EINVAL;
590         }
591
592         h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
593         return 0;
594 }
595
596 static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
597                                      bool (*fn)(struct sk_buff *, void *),
598                                      int ifidx)
599 {
600         struct brcmf_fws_hanger *h = &fws->hanger;
601         struct sk_buff *skb;
602         int i;
603         enum brcmf_fws_hanger_item_state s;
604
605         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
621 static 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
634 static void brcmf_fws_macdesc_init(struct brcmf_fws_mac_descriptor *desc,
635                                    u8 *addr, u8 ifidx)
636 {
637         brcmf_dbg(TRACE,
638                   "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
639         desc->occupied = 1;
640         desc->state = BRCMF_FWS_STATE_OPEN;
641         desc->requested_credit = 0;
642         desc->requested_packet = 0;
643         /* depending on use may need ifp->bssidx instead */
644         desc->interface_id = ifidx;
645         desc->ac_bitmap = 0xff; /* update this when handling APSD */
646         if (addr)
647                 memcpy(&desc->ea[0], addr, ETH_ALEN);
648 }
649
650 static
651 void brcmf_fws_macdesc_deinit(struct brcmf_fws_mac_descriptor *desc)
652 {
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;
658         desc->requested_packet = 0;
659 }
660
661 static struct brcmf_fws_mac_descriptor *
662 brcmf_fws_macdesc_lookup(struct brcmf_fws_info *fws, u8 *ea)
663 {
664         struct brcmf_fws_mac_descriptor *entry;
665         int i;
666
667         if (ea == NULL)
668                 return ERR_PTR(-EINVAL);
669
670         entry = &fws->desc.nodes[0];
671         for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
672                 if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
673                         return entry;
674                 entry++;
675         }
676
677         return ERR_PTR(-ENOENT);
678 }
679
680 static struct brcmf_fws_mac_descriptor*
681 brcmf_fws_macdesc_find(struct brcmf_fws_info *fws, struct brcmf_if *ifp, u8 *da)
682 {
683         struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
684         bool multicast;
685
686         multicast = is_multicast_ether_addr(da);
687
688         /* 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.
691          */
692         if (multicast && ifp->fws_desc) {
693                 entry = ifp->fws_desc;
694                 goto done;
695         }
696
697         entry = brcmf_fws_macdesc_lookup(fws, da);
698         if (IS_ERR(entry))
699                 entry = ifp->fws_desc;
700
701 done:
702         return entry;
703 }
704
705 static bool brcmf_fws_macdesc_closed(struct brcmf_fws_info *fws,
706                                      struct brcmf_fws_mac_descriptor *entry,
707                                      int fifo)
708 {
709         struct brcmf_fws_mac_descriptor *if_entry;
710         bool closed;
711
712         /* 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         }
720         /* an entry is closed when the state is closed and
721          * the firmware did not request anything.
722          */
723         closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
724                  !entry->requested_credit && !entry->requested_packet;
725
726         /* Or firmware does not allow traffic for given fifo */
727         return closed || !(entry->ac_bitmap & BIT(fifo));
728 }
729
730 static void brcmf_fws_macdesc_cleanup(struct brcmf_fws_info *fws,
731                                       struct brcmf_fws_mac_descriptor *entry,
732                                       int ifidx)
733 {
734         if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
735                 brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
736                 entry->occupied = !!(entry->psq.len);
737         }
738 }
739
740 static 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
750         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
769 static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
770 {
771         int i;
772         struct brcmf_fws_mac_descriptor *table;
773         bool (*matchfn)(struct sk_buff *, void *) = NULL;
774
775         if (fws == NULL)
776                 return;
777
778         if (ifidx != -1)
779                 matchfn = brcmf_fws_ifidx_match;
780
781         /* cleanup individual nodes */
782         table = &fws->desc.nodes[0];
783         for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
784                 brcmf_fws_macdesc_cleanup(fws, &table[i], ifidx);
785
786         brcmf_fws_macdesc_cleanup(fws, &fws->desc.other, ifidx);
787         brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
788         brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
789 }
790
791 static int brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
792 {
793         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
799         brcmf_dbg(TRACE, "enter: %s, idx=%d pkttag=0x%08X, hslot=%d\n",
800                   entry->name, brcmf_skb_if_flags_get_field(skb, INDEX),
801                   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;
824                 brcmf_dbg(TRACE, "adding TIM info: handle %d bmp 0x%X\n",
825                           entry->mac_handle, entry->traffic_pending_bmp);
826                 wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
827                 entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
828         }
829         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
837 static bool brcmf_fws_tim_update(struct brcmf_fws_info *fws,
838                                  struct brcmf_fws_mac_descriptor *entry,
839                                  int fifo, bool send_immediately)
840 {
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 */
848         if (brcmu_pktq_mlen(&entry->psq, 3 << (fifo * 2)) == 0)
849                 entry->traffic_pending_bmp &= ~NBITVAL(fifo);
850         else
851                 entry->traffic_pending_bmp |= NBITVAL(fifo);
852
853         entry->send_tim_signal = false;
854         if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
855                 entry->send_tim_signal = true;
856         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;
879 }
880
881 static void
882 brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
883                              u8 if_id)
884 {
885         struct brcmf_if *ifp = fws->drvr->iflist[!if_id ? 0 : if_id + 1];
886
887         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) &&
895             pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER) {
896                 fws->stats.fws_flow_block++;
897                 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
898         }
899         return;
900 }
901
902 static 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
908 /* using macro so sparse checking does not complain
909  * about locking imbalance.
910  */
911 #define brcmf_fws_lock(drvr, flags)                             \
912 do {                                                            \
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
923 static
924 int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
925 {
926         struct brcmf_fws_mac_descriptor *entry, *existing;
927         ulong flags;
928         u8 mac_handle;
929         u8 ifidx;
930         u8 *addr;
931
932         mac_handle = *data++;
933         ifidx = *data++;
934         addr = data;
935
936         entry = &fws->desc.nodes[mac_handle & 0x1F];
937         if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
938                 if (entry->occupied) {
939                         brcmf_dbg(TRACE, "deleting %s mac %pM\n",
940                                   entry->name, addr);
941                         brcmf_fws_lock(fws->drvr, flags);
942                         brcmf_fws_macdesc_cleanup(fws, entry, -1);
943                         brcmf_fws_macdesc_deinit(entry);
944                         brcmf_fws_unlock(fws->drvr, flags);
945                 } else
946                         fws->stats.mac_update_failed++;
947                 return 0;
948         }
949
950         existing = brcmf_fws_macdesc_lookup(fws, addr);
951         if (IS_ERR(existing)) {
952                 if (!entry->occupied) {
953                         brcmf_fws_lock(fws->drvr, flags);
954                         entry->mac_handle = mac_handle;
955                         brcmf_fws_macdesc_init(entry, addr, ifidx);
956                         brcmf_fws_macdesc_set_name(fws, entry);
957                         brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
958                                         BRCMF_FWS_PSQ_LEN);
959                         brcmf_fws_unlock(fws->drvr, flags);
960                         brcmf_dbg(TRACE, "add %s mac %pM\n", entry->name, addr);
961                 } else {
962                         fws->stats.mac_update_failed++;
963                 }
964         } else {
965                 if (entry != existing) {
966                         brcmf_dbg(TRACE, "copy mac %s\n", existing->name);
967                         brcmf_fws_lock(fws->drvr, flags);
968                         memcpy(entry, existing,
969                                offsetof(struct brcmf_fws_mac_descriptor, psq));
970                         entry->mac_handle = mac_handle;
971                         brcmf_fws_macdesc_deinit(existing);
972                         brcmf_fws_macdesc_set_name(fws, entry);
973                         brcmf_fws_unlock(fws->drvr, flags);
974                         brcmf_dbg(TRACE, "relocate %s mac %pM\n", entry->name,
975                                   addr);
976                 } 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
985 static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
986                                             u8 type, u8 *data)
987 {
988         struct brcmf_fws_mac_descriptor *entry;
989         ulong flags;
990         u8 mac_handle;
991         int ret;
992
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         }
999         brcmf_fws_lock(fws->drvr, flags);
1000         /* a state update should wipe old credits */
1001         entry->requested_credit = 0;
1002         entry->requested_packet = 0;
1003         if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
1004                 entry->state = BRCMF_FWS_STATE_OPEN;
1005                 ret = BRCMF_FWS_RET_OK_SCHEDULE;
1006         } else {
1007                 entry->state = BRCMF_FWS_STATE_CLOSE;
1008                 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);
1012                 ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1013         }
1014         brcmf_fws_unlock(fws->drvr, flags);
1015         return ret;
1016 }
1017
1018 static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
1019                                               u8 type, u8 *data)
1020 {
1021         struct brcmf_fws_mac_descriptor *entry;
1022         ulong flags;
1023         u8 ifidx;
1024         int ret;
1025
1026         ifidx = data[0];
1027
1028         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
1039         brcmf_dbg(TRACE, "%s (%d): %s\n", brcmf_fws_get_tlv_name(type), type,
1040                   entry->name);
1041         brcmf_fws_lock(fws->drvr, flags);
1042         switch (type) {
1043         case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1044                 entry->state = BRCMF_FWS_STATE_OPEN;
1045                 ret = BRCMF_FWS_RET_OK_SCHEDULE;
1046                 break;
1047         case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1048                 entry->state = BRCMF_FWS_STATE_CLOSE;
1049                 ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1050                 break;
1051         default:
1052                 ret = -EINVAL;
1053                 brcmf_fws_unlock(fws->drvr, flags);
1054                 goto fail;
1055         }
1056         brcmf_fws_unlock(fws->drvr, flags);
1057         return ret;
1058
1059 fail:
1060         fws->stats.if_update_failed++;
1061         return ret;
1062 }
1063
1064 static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
1065                                       u8 *data)
1066 {
1067         struct brcmf_fws_mac_descriptor *entry;
1068         ulong flags;
1069
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
1079         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]);
1082         brcmf_fws_lock(fws->drvr, flags);
1083         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];
1089         brcmf_fws_unlock(fws->drvr, flags);
1090         return BRCMF_FWS_RET_OK_SCHEDULE;
1091 }
1092
1093 static void
1094 brcmf_fws_macdesc_use_req_credit(struct brcmf_fws_mac_descriptor *entry,
1095                                  struct sk_buff *skb)
1096 {
1097         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");
1109         } else {
1110                 brcmf_skb_if_flags_set_field(skb, REQUESTED, 0);
1111                 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1112         }
1113 }
1114
1115 static 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++;
1122 }
1123
1124 static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
1125                                      u8 fifo, u8 credits)
1126 {
1127         int lender_ac;
1128         int *borrowed;
1129         int *fifo_credit;
1130
1131         if (!credits)
1132                 return;
1133
1134         fws->fifo_credit_map |= 1 << fifo;
1135
1136         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
1157         fws->fifo_credit[fifo] += credits;
1158 }
1159
1160 static 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
1167 static 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
1182         brcmf_dbg(DATA, "enter: fifo %d skb %p\n", fifo, p);
1183         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;
1199
1200         /*
1201          * A packet has been pushed so update traffic
1202          * availability bitmap, if applicable
1203          */
1204         brcmf_fws_tim_update(fws, entry, fifo, true);
1205         brcmf_fws_flow_control_check(fws, &entry->psq,
1206                                      brcmf_skb_if_flags_get_field(p, INDEX));
1207         return 0;
1208 }
1209
1210 static 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;
1215         int num_nodes;
1216         int node_pos;
1217         int prec_out;
1218         int pmsk;
1219         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];
1227                 if (!entry->occupied ||
1228                     brcmf_fws_macdesc_closed(fws, entry, fifo))
1229                         continue;
1230
1231                 if (entry->suppressed)
1232                         pmsk = 2;
1233                 else
1234                         pmsk = 3;
1235                 p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
1236                 if (p == NULL) {
1237                         if (entry->suppressed) {
1238                                 if (entry->suppr_transit_count)
1239                                         continue;
1240                                 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
1248                 brcmf_fws_macdesc_use_req_credit(entry, p);
1249
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                  */
1260                 brcmf_fws_tim_update(fws, entry, fifo, false);
1261
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;
1272 done:
1273         brcmf_dbg(DATA, "exit: fifo %d skb %p\n", fifo, p);
1274         return p;
1275 }
1276
1277 static 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;
1282         int ret;
1283         u8 ifidx;
1284
1285         hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1286
1287         /* this packet was suppressed */
1288         if (!entry->suppressed) {
1289                 entry->suppressed = true;
1290                 entry->suppr_transit_count = entry->transit_count;
1291                 brcmf_dbg(DATA, "suppress %s: transit %d\n",
1292                           entry->name, entry->transit_count);
1293         }
1294
1295         entry->generation = genbit;
1296
1297         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);
1301         if (ret != 0) {
1302                 /* suppress q is full or hdrpull failed, drop this packet */
1303                 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                  */
1310                 brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot);
1311         }
1312
1313         return ret;
1314 }
1315
1316 static int
1317 brcmf_fws_txs_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
1318                            u32 genbit)
1319 {
1320         u32 fifo;
1321         int ret;
1322         bool remove_from_hanger = true;
1323         struct sk_buff *skb;
1324         struct brcmf_skbuff_cb *skcb;
1325         struct brcmf_fws_mac_descriptor *entry = NULL;
1326
1327         brcmf_dbg(DATA, "flags %d\n", flags);
1328
1329         if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
1330                 fws->stats.txs_discard++;
1331         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;
1337         } else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
1338                 fws->stats.txs_tossed++;
1339         else if (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)
1340                 fws->stats.txs_host_tossed++;
1341         else
1342                 brcmf_err("unexpected txstatus\n");
1343
1344         ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1345                                       remove_from_hanger);
1346         if (ret != 0) {
1347                 brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
1348                 return ret;
1349         }
1350
1351         skcb = brcmf_skbcb(skb);
1352         entry = skcb->mac;
1353         if (WARN_ON(!entry)) {
1354                 brcmu_pkt_buf_free_skb(skb);
1355                 return -EINVAL;
1356         }
1357         entry->transit_count--;
1358         if (entry->suppressed && entry->suppr_transit_count)
1359                 entry->suppr_transit_count--;
1360
1361         brcmf_dbg(DATA, "%s flags %X htod %X\n", entry->name, skcb->if_flags,
1362                   skcb->htod);
1363
1364         /* pick up the implicit credit from this packet */
1365         fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
1366         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);
1373
1374         if (!remove_from_hanger)
1375                 ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb, genbit);
1376
1377         if (remove_from_hanger || ret)
1378                 brcmf_txfinalize(fws->drvr, skb, true);
1379
1380         return 0;
1381 }
1382
1383 static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
1384                                              u8 *data)
1385 {
1386         ulong flags;
1387         int i;
1388
1389         if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
1390                 brcmf_dbg(INFO, "ignored\n");
1391                 return BRCMF_FWS_RET_OK_NOSCHEDULE;
1392         }
1393
1394         brcmf_dbg(DATA, "enter: data %pM\n", data);
1395         brcmf_fws_lock(fws->drvr, flags);
1396         for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
1397                 brcmf_fws_return_credits(fws, i, data[i]);
1398
1399         brcmf_dbg(DATA, "map: credit %x delay %x\n", fws->fifo_credit_map,
1400                   fws->fifo_delay_map);
1401         brcmf_fws_unlock(fws->drvr, flags);
1402         return BRCMF_FWS_RET_OK_SCHEDULE;
1403 }
1404
1405 static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
1406 {
1407         ulong lflags;
1408         __le32 status_le;
1409         u32 status;
1410         u32 hslot;
1411         u32 genbit;
1412         u8 flags;
1413
1414         fws->stats.txs_indicate++;
1415         memcpy(&status_le, data, sizeof(status_le));
1416         status = le32_to_cpu(status_le);
1417         flags = brcmf_txstatus_get_field(status, FLAGS);
1418         hslot = brcmf_txstatus_get_field(status, HSLOT);
1419         genbit = brcmf_txstatus_get_field(status, GENERATION);
1420
1421         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;
1425 }
1426
1427 static 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));
1432         brcmf_dbg(CTL, "received: seq %d, timestamp %d\n", data[1],
1433                   le32_to_cpu(timestamp));
1434         return 0;
1435 }
1436
1437 static 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
1446         if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
1447                 brcmf_err("event payload too small (%d)\n", e->datalen);
1448                 return -EINVAL;
1449         }
1450         if (fws->creditmap_received)
1451                 return 0;
1452
1453         fws->creditmap_received = true;
1454
1455         brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
1456         brcmf_fws_lock(ifp->drvr, flags);
1457         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);
1462                 fws->fifo_credit[i] = *credits++;
1463         }
1464         brcmf_fws_schedule_deq(fws);
1465         brcmf_fws_unlock(ifp->drvr, flags);
1466         return 0;
1467 }
1468
1469 static 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
1483 int 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;
1487         u8 *signal_data;
1488         s16 data_len;
1489         u8 type;
1490         u8 len;
1491         u8 *data;
1492         s32 status;
1493         s32 err;
1494
1495         brcmf_dbg(HDRS, "enter: ifidx %d, skblen %u, sig %d\n",
1496                   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
1506         fws->stats.header_pulls++;
1507         data_len = signal_len;
1508         signal_data = skb->data;
1509
1510         status = BRCMF_FWS_RET_OK_NOSCHEDULE;
1511         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
1526                 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));
1529
1530                 /* abort parsing when length invalid */
1531                 if (data_len < len + 2)
1532                         break;
1533
1534                 if (len < brcmf_fws_get_tlv_len(fws, type))
1535                         break;
1536
1537                 err = BRCMF_FWS_RET_OK_NOSCHEDULE;
1538                 switch (type) {
1539                 case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
1540                 case BRCMF_FWS_TYPE_COMP_TXSTATUS:
1541                         break;
1542                 case BRCMF_FWS_TYPE_MACDESC_ADD:
1543                 case BRCMF_FWS_TYPE_MACDESC_DEL:
1544                         brcmf_fws_macdesc_indicate(fws, type, data);
1545                         break;
1546                 case BRCMF_FWS_TYPE_MAC_OPEN:
1547                 case BRCMF_FWS_TYPE_MAC_CLOSE:
1548                         err = brcmf_fws_macdesc_state_indicate(fws, type, data);
1549                         break;
1550                 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1551                 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1552                         err = brcmf_fws_interface_state_indicate(fws, type,
1553                                                                  data);
1554                         break;
1555                 case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
1556                 case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
1557                         err = brcmf_fws_request_indicate(fws, type, data);
1558                         break;
1559                 case BRCMF_FWS_TYPE_TXSTATUS:
1560                         brcmf_fws_txstatus_indicate(fws, data);
1561                         break;
1562                 case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
1563                         err = brcmf_fws_fifocreditback_indicate(fws, data);
1564                         break;
1565                 case BRCMF_FWS_TYPE_RSSI:
1566                         brcmf_fws_rssi_indicate(fws, *data);
1567                         break;
1568                 case BRCMF_FWS_TYPE_TRANS_ID:
1569                         brcmf_fws_dbg_seqnum_check(fws, data);
1570                         break;
1571                 case BRCMF_FWS_TYPE_PKTTAG:
1572                 case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
1573                 default:
1574                         fws->stats.tlv_invalid_type++;
1575                         break;
1576                 }
1577                 if (err == BRCMF_FWS_RET_OK_SCHEDULE)
1578                         status = BRCMF_FWS_RET_OK_SCHEDULE;
1579                 signal_data += len + 2;
1580                 data_len -= len + 2;
1581         }
1582
1583         if (data_len != 0)
1584                 fws->stats.tlv_parse_failed++;
1585
1586         if (status == BRCMF_FWS_RET_OK_SCHEDULE)
1587                 brcmf_fws_schedule_deq(fws);
1588
1589         /* 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
1599         return 0;
1600 }
1601
1602 static void brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
1603                                    struct sk_buff *p)
1604 {
1605         struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1606         struct brcmf_fws_mac_descriptor *entry = skcb->mac;
1607         u8 flags;
1608
1609         brcmf_skb_if_flags_set_field(p, TRANSMIT, 1);
1610         brcmf_skb_htod_tag_set_field(p, GENERATION, entry->generation);
1611         flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
1612         if (brcmf_skb_if_flags_get_field(p, REQUESTED)) {
1613                 /*
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);
1620         brcmf_fws_hdrpush(fws, p);
1621 }
1622
1623 static void brcmf_fws_rollback_toq(struct brcmf_fws_info *fws,
1624                                    struct sk_buff *skb, int fifo)
1625 {
1626         struct brcmf_fws_mac_descriptor *entry;
1627         struct sk_buff *pktout;
1628         int qidx, hslot;
1629         int rc = 0;
1630
1631         entry = brcmf_skbcb(skb)->mac;
1632         if (entry->occupied) {
1633                 qidx = 2 * fifo;
1634                 if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
1635                         qidx++;
1636
1637                 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;
1641                 }
1642         } else {
1643                 brcmf_err("%s entry removed\n", entry->name);
1644                 rc = -ENOENT;
1645         }
1646
1647         if (rc) {
1648                 fws->stats.rollback_failed++;
1649                 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1650                 brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED,
1651                                       hslot, 0);
1652         } else {
1653                 fws->stats.rollback_success++;
1654                 brcmf_fws_return_credits(fws, fifo, 1);
1655                 brcmf_fws_macdesc_return_req_credit(skb);
1656         }
1657 }
1658
1659 static int brcmf_fws_borrow_credit(struct brcmf_fws_info *fws)
1660 {
1661         int lender_ac;
1662
1663         if (time_after(fws->borrow_defer_timestamp, jiffies)) {
1664                 fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
1665                 return -ENAVAIL;
1666         }
1667
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);
1674                         fws->fifo_credit_map |= (1 << BRCMF_FWS_FIFO_AC_BE);
1675                         brcmf_dbg(DATA, "borrow credit from: %d\n", lender_ac);
1676                         return 0;
1677                 }
1678         }
1679         fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
1680         return -ENAVAIL;
1681 }
1682
1683 static 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;
1690         u8 ifidx;
1691
1692         entry = skcb->mac;
1693         if (IS_ERR(entry))
1694                 return PTR_ERR(entry);
1695
1696         brcmf_fws_precommit_skb(fws, fifo, skb);
1697         rc = brcmf_bus_txdata(bus, skb);
1698         brcmf_dbg(DATA, "%s flags %X htod %X bus_tx %d\n", entry->name,
1699                   skcb->if_flags, skcb->htod, rc);
1700         if (rc < 0) {
1701                 brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb);
1702                 goto rollback;
1703         }
1704
1705         entry->transit_count++;
1706         if (entry->suppressed)
1707                 entry->suppr_transit_count++;
1708         fws->stats.pkt2bus++;
1709         fws->stats.send_pkts[fifo]++;
1710         if (brcmf_skb_if_flags_get_field(skb, REQUESTED))
1711                 fws->stats.requested_sent[fifo]++;
1712
1713         return rc;
1714
1715 rollback:
1716         brcmf_fws_rollback_toq(fws, skb, fifo);
1717         return rc;
1718 }
1719
1720 static 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
1738 int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
1739 {
1740         struct brcmf_pub *drvr = ifp->drvr;
1741         struct brcmf_fws_info *fws = drvr->fws;
1742         struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
1743         struct ethhdr *eh = (struct ethhdr *)(skb->data);
1744         ulong flags;
1745         int fifo = BRCMF_FWS_FIFO_BCMC;
1746         bool multicast = is_multicast_ether_addr(eh->h_dest);
1747
1748         /* determine the priority */
1749         if (!skb->priority)
1750                 skb->priority = cfg80211_classify8021d(skb);
1751
1752         drvr->tx_multicast += !!multicast;
1753         if (ntohs(eh->h_proto) == ETH_P_PAE)
1754                 atomic_inc(&ifp->pend_8021x_cnt);
1755
1756         if (!brcmf_fws_fc_active(fws)) {
1757                 /* If the protocol uses a data header, apply it */
1758                 brcmf_proto_hdrpush(drvr, ifp->ifidx, 0, skb);
1759
1760                 /* Use bus module to send data frame */
1761                 return brcmf_bus_txdata(drvr->bus_if, skb);
1762         }
1763
1764         /* set control buffer information */
1765         skcb->if_flags = 0;
1766         skcb->state = BRCMF_FWS_SKBSTATE_NEW;
1767         brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
1768         if (!multicast)
1769                 fifo = brcmf_fws_prio2fifo[skb->priority];
1770
1771         brcmf_fws_lock(drvr, flags);
1772         if (fifo != BRCMF_FWS_FIFO_AC_BE && fifo < BRCMF_FWS_FIFO_BCMC)
1773                 fws->borrow_defer_timestamp = jiffies +
1774                                               BRCMF_FWS_BORROW_DEFER_PERIOD;
1775
1776         skcb->mac = brcmf_fws_macdesc_find(fws, ifp, eh->h_dest);
1777         brcmf_dbg(DATA, "%s mac %pM multi %d fifo %d\n", skcb->mac->name,
1778                   eh->h_dest, multicast, fifo);
1779         if (!brcmf_fws_assign_htod(fws, skb, fifo)) {
1780                 brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
1781                 brcmf_fws_schedule_deq(fws);
1782         } else {
1783                 brcmf_err("drop skb: no hanger slot\n");
1784                 brcmu_pkt_buf_free_skb(skb);
1785         }
1786         brcmf_fws_unlock(drvr, flags);
1787         return 0;
1788 }
1789
1790 void brcmf_fws_reset_interface(struct brcmf_if *ifp)
1791 {
1792         struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1793
1794         brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
1795         if (!entry)
1796                 return;
1797
1798         brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
1799 }
1800
1801 void brcmf_fws_add_interface(struct brcmf_if *ifp)
1802 {
1803         struct brcmf_fws_info *fws = ifp->drvr->fws;
1804         struct brcmf_fws_mac_descriptor *entry;
1805
1806         if (!ifp->ndev || !ifp->drvr->fw_signals)
1807                 return;
1808
1809         entry = &fws->desc.iface[ifp->ifidx];
1810         ifp->fws_desc = entry;
1811         brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
1812         brcmf_fws_macdesc_set_name(fws, entry);
1813         brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
1814                         BRCMF_FWS_PSQ_LEN);
1815         brcmf_dbg(TRACE, "added %s\n", entry->name);
1816 }
1817
1818 void brcmf_fws_del_interface(struct brcmf_if *ifp)
1819 {
1820         struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1821         ulong flags;
1822
1823         if (!entry)
1824                 return;
1825
1826         brcmf_fws_lock(ifp->drvr, flags);
1827         ifp->fws_desc = NULL;
1828         brcmf_dbg(TRACE, "deleting %s\n", entry->name);
1829         brcmf_fws_macdesc_deinit(entry);
1830         brcmf_fws_cleanup(ifp->drvr->fws, ifp->ifidx);
1831         brcmf_fws_unlock(ifp->drvr, flags);
1832 }
1833
1834 static void brcmf_fws_dequeue_worker(struct work_struct *worker)
1835 {
1836         struct brcmf_fws_info *fws;
1837         struct sk_buff *skb;
1838         ulong flags;
1839         int fifo;
1840
1841         fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
1842
1843         brcmf_fws_lock(fws->drvr, flags);
1844         for (fifo = BRCMF_FWS_FIFO_BCMC; fifo >= 0 && !fws->bus_flow_blocked;
1845              fifo--) {
1846                 while ((fws->fifo_credit[fifo]) || ((!fws->bcmc_credit_check) &&
1847                        (fifo == BRCMF_FWS_FIFO_BCMC))) {
1848                         skb = brcmf_fws_deq(fws, fifo);
1849                         if (!skb)
1850                                 break;
1851                         fws->fifo_credit[fifo]--;
1852                         if (brcmf_fws_commit_skb(fws, fifo, skb))
1853                                 break;
1854                         if (fws->bus_flow_blocked)
1855                                 break;
1856                 }
1857                 if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
1858                     (fws->fifo_credit[fifo] == 0) &&
1859                     (!fws->bus_flow_blocked)) {
1860                         while (brcmf_fws_borrow_credit(fws) == 0) {
1861                                 skb = brcmf_fws_deq(fws, fifo);
1862                                 if (!skb) {
1863                                         brcmf_fws_return_credits(fws, fifo, 1);
1864                                         break;
1865                                 }
1866                                 if (brcmf_fws_commit_skb(fws, fifo, skb))
1867                                         break;
1868                                 if (fws->bus_flow_blocked)
1869                                         break;
1870                         }
1871                 }
1872         }
1873         brcmf_fws_unlock(fws->drvr, flags);
1874 }
1875
1876 int brcmf_fws_init(struct brcmf_pub *drvr)
1877 {
1878         u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
1879         int rc;
1880
1881         if (!drvr->fw_signals)
1882                 return 0;
1883
1884         spin_lock_init(&drvr->fws_spinlock);
1885
1886         drvr->fws = kzalloc(sizeof(*(drvr->fws)), GFP_KERNEL);
1887         if (!drvr->fws) {
1888                 rc = -ENOMEM;
1889                 goto fail;
1890         }
1891
1892         /* set linkage back */
1893         drvr->fws->drvr = drvr;
1894         drvr->fws->fcmode = fcmode;
1895
1896         drvr->fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
1897         if (drvr->fws->fws_wq == NULL) {
1898                 brcmf_err("workqueue creation failed\n");
1899                 rc = -EBADF;
1900                 goto fail;
1901         }
1902         INIT_WORK(&drvr->fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
1903
1904         /* enable firmware signalling if fcmode active */
1905         if (drvr->fws->fcmode != BRCMF_FWS_FCMODE_NONE)
1906                 tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
1907                        BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
1908                        BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE;
1909
1910         rc = brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
1911                                  brcmf_fws_notify_credit_map);
1912         if (rc < 0) {
1913                 brcmf_err("register credit map handler failed\n");
1914                 goto fail;
1915         }
1916         rc = brcmf_fweh_register(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT,
1917                                  brcmf_fws_notify_bcmc_credit_support);
1918         if (rc < 0) {
1919                 brcmf_err("register bcmc credit handler failed\n");
1920                 brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
1921                 goto fail;
1922         }
1923
1924         /* setting the iovar may fail if feature is unsupported
1925          * so leave the rc as is so driver initialization can
1926          * continue.
1927          */
1928         if (brcmf_fil_iovar_int_set(drvr->iflist[0], "tlv", tlv)) {
1929                 brcmf_err("failed to set bdcv2 tlv signaling\n");
1930                 goto fail_event;
1931         }
1932
1933         brcmf_fws_hanger_init(&drvr->fws->hanger);
1934         brcmf_fws_macdesc_init(&drvr->fws->desc.other, NULL, 0);
1935         brcmf_fws_macdesc_set_name(drvr->fws, &drvr->fws->desc.other);
1936         brcmu_pktq_init(&drvr->fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
1937                         BRCMF_FWS_PSQ_LEN);
1938
1939         /* create debugfs file for statistics */
1940         brcmf_debugfs_create_fws_stats(drvr, &drvr->fws->stats);
1941
1942         brcmf_dbg(INFO, "%s bdcv2 tlv signaling [%x]\n",
1943                   drvr->fw_signals ? "enabled" : "disabled", tlv);
1944         return 0;
1945
1946 fail_event:
1947         brcmf_fweh_unregister(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT);
1948         brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
1949 fail:
1950         brcmf_fws_deinit(drvr);
1951         return rc;
1952 }
1953
1954 void brcmf_fws_deinit(struct brcmf_pub *drvr)
1955 {
1956         struct brcmf_fws_info *fws = drvr->fws;
1957         ulong flags;
1958
1959         if (!fws)
1960                 return;
1961
1962         /* disable firmware signalling entirely
1963          * to avoid using the workqueue.
1964          */
1965         drvr->fw_signals = false;
1966
1967         if (drvr->fws->fws_wq)
1968                 destroy_workqueue(drvr->fws->fws_wq);
1969
1970         /* cleanup */
1971         brcmf_fws_lock(drvr, flags);
1972         brcmf_fws_cleanup(fws, -1);
1973         drvr->fws = NULL;
1974         brcmf_fws_unlock(drvr, flags);
1975
1976         /* free top structure */
1977         kfree(fws);
1978 }
1979
1980 bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
1981 {
1982         if (!fws)
1983                 return false;
1984
1985         return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
1986 }
1987
1988 void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
1989 {
1990         ulong flags;
1991         u32 hslot;
1992
1993         if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_TIM) {
1994                 brcmu_pkt_buf_free_skb(skb);
1995                 return;
1996         }
1997         brcmf_fws_lock(fws->drvr, flags);
1998         hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1999         brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED, hslot, 0);
2000         brcmf_fws_unlock(fws->drvr, flags);
2001 }
2002
2003 void brcmf_fws_bus_blocked(struct brcmf_pub *drvr, bool flow_blocked)
2004 {
2005         struct brcmf_fws_info *fws = drvr->fws;
2006
2007         fws->bus_flow_blocked = flow_blocked;
2008         if (!flow_blocked)
2009                 brcmf_fws_schedule_deq(fws);
2010         else
2011                 fws->stats.bus_flow_block++;
2012 }