2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <linux/types.h>
17 #include <linux/module.h>
18 #include <linux/if_ether.h>
19 #include <linux/spinlock.h>
20 #include <linux/skbuff.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23 #include <linux/err.h>
24 #include <linux/jiffies.h>
25 #include <net/cfg80211.h>
27 #include <brcmu_utils.h>
28 #include <brcmu_wifi.h>
33 #include "fwil_types.h"
42 * DOC: Firmware Signalling
44 * Firmware can send signals to host and vice versa, which are passed in the
45 * data packets using TLV based header. This signalling layer is on top of the
46 * BDC bus protocol layer.
50 * single definition for firmware-driver flow control tlv's.
52 * each tlv is specified by BRCMF_FWS_TLV_DEF(name, ID, length).
53 * A length value 0 indicates variable length tlv.
55 #define BRCMF_FWS_TLV_DEFLIST \
56 BRCMF_FWS_TLV_DEF(MAC_OPEN, 1, 1) \
57 BRCMF_FWS_TLV_DEF(MAC_CLOSE, 2, 1) \
58 BRCMF_FWS_TLV_DEF(MAC_REQUEST_CREDIT, 3, 2) \
59 BRCMF_FWS_TLV_DEF(TXSTATUS, 4, 4) \
60 BRCMF_FWS_TLV_DEF(PKTTAG, 5, 4) \
61 BRCMF_FWS_TLV_DEF(MACDESC_ADD, 6, 8) \
62 BRCMF_FWS_TLV_DEF(MACDESC_DEL, 7, 8) \
63 BRCMF_FWS_TLV_DEF(RSSI, 8, 1) \
64 BRCMF_FWS_TLV_DEF(INTERFACE_OPEN, 9, 1) \
65 BRCMF_FWS_TLV_DEF(INTERFACE_CLOSE, 10, 1) \
66 BRCMF_FWS_TLV_DEF(FIFO_CREDITBACK, 11, 6) \
67 BRCMF_FWS_TLV_DEF(PENDING_TRAFFIC_BMP, 12, 2) \
68 BRCMF_FWS_TLV_DEF(MAC_REQUEST_PACKET, 13, 3) \
69 BRCMF_FWS_TLV_DEF(HOST_REORDER_RXPKTS, 14, 10) \
70 BRCMF_FWS_TLV_DEF(TRANS_ID, 18, 6) \
71 BRCMF_FWS_TLV_DEF(COMP_TXSTATUS, 19, 1) \
72 BRCMF_FWS_TLV_DEF(FILLER, 255, 0)
75 * enum brcmf_fws_tlv_type - definition of tlv identifiers.
77 #define BRCMF_FWS_TLV_DEF(name, id, len) \
78 BRCMF_FWS_TYPE_ ## name = id,
79 enum brcmf_fws_tlv_type {
81 BRCMF_FWS_TYPE_INVALID
83 #undef BRCMF_FWS_TLV_DEF
86 * enum brcmf_fws_tlv_len - definition of tlv lengths.
88 #define BRCMF_FWS_TLV_DEF(name, id, len) \
89 BRCMF_FWS_TYPE_ ## name ## _LEN = (len),
90 enum brcmf_fws_tlv_len {
93 #undef BRCMF_FWS_TLV_DEF
95 /* AMPDU rx reordering definitions */
96 #define BRCMF_RXREORDER_FLOWID_OFFSET 0
97 #define BRCMF_RXREORDER_MAXIDX_OFFSET 2
98 #define BRCMF_RXREORDER_FLAGS_OFFSET 4
99 #define BRCMF_RXREORDER_CURIDX_OFFSET 6
100 #define BRCMF_RXREORDER_EXPIDX_OFFSET 8
102 #define BRCMF_RXREORDER_DEL_FLOW 0x01
103 #define BRCMF_RXREORDER_FLUSH_ALL 0x02
104 #define BRCMF_RXREORDER_CURIDX_VALID 0x04
105 #define BRCMF_RXREORDER_EXPIDX_VALID 0x08
106 #define BRCMF_RXREORDER_NEW_HOLE 0x10
110 * brcmf_fws_tlv_names - array of tlv names.
112 #define BRCMF_FWS_TLV_DEF(name, id, len) \
115 enum brcmf_fws_tlv_type id;
117 } brcmf_fws_tlv_names[] = {
118 BRCMF_FWS_TLV_DEFLIST
120 #undef BRCMF_FWS_TLV_DEF
123 static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
127 for (i = 0; i < ARRAY_SIZE(brcmf_fws_tlv_names); i++)
128 if (brcmf_fws_tlv_names[i].id == id)
129 return brcmf_fws_tlv_names[i].name;
134 static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
141 * The PKTTAG tlv has additional bytes when firmware-signalling
142 * mode has REUSESEQ flag set.
144 #define BRCMF_FWS_TYPE_SEQ_LEN 2
147 * flags used to enable tlv signalling from firmware.
149 #define BRCMF_FWS_FLAGS_RSSI_SIGNALS 0x0001
150 #define BRCMF_FWS_FLAGS_XONXOFF_SIGNALS 0x0002
151 #define BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS 0x0004
152 #define BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE 0x0008
153 #define BRCMF_FWS_FLAGS_PSQ_GENERATIONFSM_ENABLE 0x0010
154 #define BRCMF_FWS_FLAGS_PSQ_ZERO_BUFFER_ENABLE 0x0020
155 #define BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE 0x0040
157 #define BRCMF_FWS_MAC_DESC_TABLE_SIZE 32
158 #define BRCMF_FWS_MAC_DESC_ID_INVALID 0xff
160 #define BRCMF_FWS_HOSTIF_FLOWSTATE_OFF 0
161 #define BRCMF_FWS_HOSTIF_FLOWSTATE_ON 1
162 #define BRCMF_FWS_FLOWCONTROL_HIWATER 128
163 #define BRCMF_FWS_FLOWCONTROL_LOWATER 64
165 #define BRCMF_FWS_PSQ_PREC_COUNT ((BRCMF_FWS_FIFO_COUNT + 1) * 2)
166 #define BRCMF_FWS_PSQ_LEN 256
168 #define BRCMF_FWS_HTOD_FLAG_PKTFROMHOST 0x01
169 #define BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED 0x02
171 #define BRCMF_FWS_RET_OK_NOSCHEDULE 0
172 #define BRCMF_FWS_RET_OK_SCHEDULE 1
174 #define BRCMF_FWS_MODE_REUSESEQ_SHIFT 3 /* seq reuse */
175 #define BRCMF_FWS_MODE_SET_REUSESEQ(x, val) ((x) = \
176 ((x) & ~(1 << BRCMF_FWS_MODE_REUSESEQ_SHIFT)) | \
177 (((val) & 1) << BRCMF_FWS_MODE_REUSESEQ_SHIFT))
178 #define BRCMF_FWS_MODE_GET_REUSESEQ(x) \
179 (((x) >> BRCMF_FWS_MODE_REUSESEQ_SHIFT) & 1)
182 * enum brcmf_fws_skb_state - indicates processing state of skb.
184 * @BRCMF_FWS_SKBSTATE_NEW: sk_buff is newly arrived in the driver.
185 * @BRCMF_FWS_SKBSTATE_DELAYED: sk_buff had to wait on queue.
186 * @BRCMF_FWS_SKBSTATE_SUPPRESSED: sk_buff has been suppressed by firmware.
187 * @BRCMF_FWS_SKBSTATE_TIM: allocated for TIM update info.
189 enum brcmf_fws_skb_state {
190 BRCMF_FWS_SKBSTATE_NEW,
191 BRCMF_FWS_SKBSTATE_DELAYED,
192 BRCMF_FWS_SKBSTATE_SUPPRESSED,
193 BRCMF_FWS_SKBSTATE_TIM
197 * struct brcmf_skbuff_cb - control buffer associated with skbuff.
199 * @bus_flags: 2 bytes reserved for bus specific parameters
200 * @if_flags: holds interface index and packet related flags.
201 * @htod: host to device packet identifier (used in PKTTAG tlv).
202 * @htod_seq: this 16-bit is original seq number for every suppress packet.
203 * @state: transmit state of the packet.
204 * @mac: descriptor related to destination for this packet.
206 * This information is stored in control buffer struct sk_buff::cb, which
207 * provides 48 bytes of storage so this structure should not exceed that.
209 struct brcmf_skbuff_cb {
214 enum brcmf_fws_skb_state state;
215 struct brcmf_fws_mac_descriptor *mac;
219 * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
221 #define brcmf_skbcb(skb) ((struct brcmf_skbuff_cb *)((skb)->cb))
224 * sk_buff control if flags
226 * b[11] - packet sent upon firmware request.
227 * b[10] - packet only contains signalling data.
228 * b[9] - packet is a tx packet.
229 * b[8] - packet used requested credit
230 * b[7] - interface in AP mode.
231 * b[3:0] - interface index.
233 #define BRCMF_SKB_IF_FLAGS_REQUESTED_MASK 0x0800
234 #define BRCMF_SKB_IF_FLAGS_REQUESTED_SHIFT 11
235 #define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_MASK 0x0400
236 #define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_SHIFT 10
237 #define BRCMF_SKB_IF_FLAGS_TRANSMIT_MASK 0x0200
238 #define BRCMF_SKB_IF_FLAGS_TRANSMIT_SHIFT 9
239 #define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_MASK 0x0100
240 #define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_SHIFT 8
241 #define BRCMF_SKB_IF_FLAGS_IF_AP_MASK 0x0080
242 #define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT 7
243 #define BRCMF_SKB_IF_FLAGS_INDEX_MASK 0x000f
244 #define BRCMF_SKB_IF_FLAGS_INDEX_SHIFT 0
246 #define brcmf_skb_if_flags_set_field(skb, field, value) \
247 brcmu_maskset16(&(brcmf_skbcb(skb)->if_flags), \
248 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
249 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT, (value))
250 #define brcmf_skb_if_flags_get_field(skb, field) \
251 brcmu_maskget16(brcmf_skbcb(skb)->if_flags, \
252 BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
253 BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT)
256 * sk_buff control packet identifier
258 * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
260 * - Generated at the host (e.g. dhd)
261 * - Seen as a generic sequence number by firmware except for the flags field.
263 * Generation : b[31] => generation number for this packet [host->fw]
264 * OR, current generation number [fw->host]
265 * Flags : b[30:27] => command, status flags
266 * FIFO-AC : b[26:24] => AC-FIFO id
267 * h-slot : b[23:8] => hanger-slot
268 * freerun : b[7:0] => A free running counter
270 #define BRCMF_SKB_HTOD_TAG_GENERATION_MASK 0x80000000
271 #define BRCMF_SKB_HTOD_TAG_GENERATION_SHIFT 31
272 #define BRCMF_SKB_HTOD_TAG_FLAGS_MASK 0x78000000
273 #define BRCMF_SKB_HTOD_TAG_FLAGS_SHIFT 27
274 #define BRCMF_SKB_HTOD_TAG_FIFO_MASK 0x07000000
275 #define BRCMF_SKB_HTOD_TAG_FIFO_SHIFT 24
276 #define BRCMF_SKB_HTOD_TAG_HSLOT_MASK 0x00ffff00
277 #define BRCMF_SKB_HTOD_TAG_HSLOT_SHIFT 8
278 #define BRCMF_SKB_HTOD_TAG_FREERUN_MASK 0x000000ff
279 #define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT 0
281 #define brcmf_skb_htod_tag_set_field(skb, field, value) \
282 brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
283 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
284 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
285 #define brcmf_skb_htod_tag_get_field(skb, field) \
286 brcmu_maskget32(brcmf_skbcb(skb)->htod, \
287 BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
288 BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)
290 #define BRCMF_SKB_HTOD_SEQ_FROMFW_MASK 0x2000
291 #define BRCMF_SKB_HTOD_SEQ_FROMFW_SHIFT 13
292 #define BRCMF_SKB_HTOD_SEQ_FROMDRV_MASK 0x1000
293 #define BRCMF_SKB_HTOD_SEQ_FROMDRV_SHIFT 12
294 #define BRCMF_SKB_HTOD_SEQ_NR_MASK 0x0fff
295 #define BRCMF_SKB_HTOD_SEQ_NR_SHIFT 0
297 #define brcmf_skb_htod_seq_set_field(skb, field, value) \
298 brcmu_maskset16(&(brcmf_skbcb(skb)->htod_seq), \
299 BRCMF_SKB_HTOD_SEQ_ ## field ## _MASK, \
300 BRCMF_SKB_HTOD_SEQ_ ## field ## _SHIFT, (value))
301 #define brcmf_skb_htod_seq_get_field(skb, field) \
302 brcmu_maskget16(brcmf_skbcb(skb)->htod_seq, \
303 BRCMF_SKB_HTOD_SEQ_ ## field ## _MASK, \
304 BRCMF_SKB_HTOD_SEQ_ ## field ## _SHIFT)
306 #define BRCMF_FWS_TXSTAT_GENERATION_MASK 0x80000000
307 #define BRCMF_FWS_TXSTAT_GENERATION_SHIFT 31
308 #define BRCMF_FWS_TXSTAT_FLAGS_MASK 0x78000000
309 #define BRCMF_FWS_TXSTAT_FLAGS_SHIFT 27
310 #define BRCMF_FWS_TXSTAT_FIFO_MASK 0x07000000
311 #define BRCMF_FWS_TXSTAT_FIFO_SHIFT 24
312 #define BRCMF_FWS_TXSTAT_HSLOT_MASK 0x00FFFF00
313 #define BRCMF_FWS_TXSTAT_HSLOT_SHIFT 8
314 #define BRCMF_FWS_TXSTAT_FREERUN_MASK 0x000000FF
315 #define BRCMF_FWS_TXSTAT_FREERUN_SHIFT 0
317 #define brcmf_txstatus_get_field(txs, field) \
318 brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
319 BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)
321 /* How long to defer borrowing in jiffies */
322 #define BRCMF_FWS_BORROW_DEFER_PERIOD (HZ / 10)
325 * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
327 * @BRCMF_FWS_FIFO_FIRST: first fifo, ie. background.
328 * @BRCMF_FWS_FIFO_AC_BK: fifo for background traffic.
329 * @BRCMF_FWS_FIFO_AC_BE: fifo for best-effort traffic.
330 * @BRCMF_FWS_FIFO_AC_VI: fifo for video traffic.
331 * @BRCMF_FWS_FIFO_AC_VO: fifo for voice traffic.
332 * @BRCMF_FWS_FIFO_BCMC: fifo for broadcast/multicast (AP only).
333 * @BRCMF_FWS_FIFO_ATIM: fifo for ATIM (AP only).
334 * @BRCMF_FWS_FIFO_COUNT: number of fifos.
336 enum brcmf_fws_fifo {
337 BRCMF_FWS_FIFO_FIRST,
338 BRCMF_FWS_FIFO_AC_BK = BRCMF_FWS_FIFO_FIRST,
339 BRCMF_FWS_FIFO_AC_BE,
340 BRCMF_FWS_FIFO_AC_VI,
341 BRCMF_FWS_FIFO_AC_VO,
348 * enum brcmf_fws_txstatus - txstatus flag values.
350 * @BRCMF_FWS_TXSTATUS_DISCARD:
351 * host is free to discard the packet.
352 * @BRCMF_FWS_TXSTATUS_CORE_SUPPRESS:
353 * 802.11 core suppressed the packet.
354 * @BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS:
355 * firmware suppress the packet as device is already in PS mode.
356 * @BRCMF_FWS_TXSTATUS_FW_TOSSED:
357 * firmware tossed the packet.
358 * @BRCMF_FWS_TXSTATUS_HOST_TOSSED:
359 * host tossed the packet.
361 enum brcmf_fws_txstatus {
362 BRCMF_FWS_TXSTATUS_DISCARD,
363 BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
364 BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
365 BRCMF_FWS_TXSTATUS_FW_TOSSED,
366 BRCMF_FWS_TXSTATUS_HOST_TOSSED
369 enum brcmf_fws_fcmode {
370 BRCMF_FWS_FCMODE_NONE,
371 BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
372 BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
375 enum brcmf_fws_mac_desc_state {
376 BRCMF_FWS_STATE_OPEN = 1,
377 BRCMF_FWS_STATE_CLOSE
381 * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
383 * @occupied: slot is in use.
384 * @mac_handle: handle for mac entry determined by firmware.
385 * @interface_id: interface index.
386 * @state: current state.
387 * @suppressed: mac entry is suppressed.
388 * @generation: generation bit.
389 * @ac_bitmap: ac queue bitmap.
390 * @requested_credit: credits requested by firmware.
391 * @ea: ethernet address.
392 * @seq: per-node free-running sequence.
393 * @psq: power-save queue.
394 * @transit_count: packet in transit to firmware.
396 struct brcmf_fws_mac_descriptor {
408 u8 seq[BRCMF_FWS_FIFO_COUNT];
411 int suppr_transit_count;
412 bool send_tim_signal;
413 u8 traffic_pending_bmp;
414 u8 traffic_lastreported_bmp;
417 #define BRCMF_FWS_HANGER_MAXITEMS 1024
420 * enum brcmf_fws_hanger_item_state - state of hanger item.
422 * @BRCMF_FWS_HANGER_ITEM_STATE_FREE: item is free for use.
423 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE: item is in use.
424 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED: item was suppressed.
426 enum brcmf_fws_hanger_item_state {
427 BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
428 BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
429 BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
434 * struct brcmf_fws_hanger_item - single entry for tx pending packet.
436 * @state: entry is either free or occupied.
437 * @pkt: packet itself.
439 struct brcmf_fws_hanger_item {
440 enum brcmf_fws_hanger_item_state state;
445 * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
447 * @pushed: packets pushed to await txstatus.
448 * @popped: packets popped upon handling txstatus.
449 * @failed_to_push: packets that could not be pushed.
450 * @failed_to_pop: packets that could not be popped.
451 * @failed_slotfind: packets for which failed to find an entry.
452 * @slot_pos: last returned item index for a free entry.
453 * @items: array of hanger items.
455 struct brcmf_fws_hanger {
462 struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
465 struct brcmf_fws_macdesc_table {
466 struct brcmf_fws_mac_descriptor nodes[BRCMF_FWS_MAC_DESC_TABLE_SIZE];
467 struct brcmf_fws_mac_descriptor iface[BRCMF_MAX_IFS];
468 struct brcmf_fws_mac_descriptor other;
471 struct brcmf_fws_stats {
472 u32 tlv_parse_failed;
473 u32 tlv_invalid_type;
478 u32 requested_sent[5];
480 u32 mac_update_failed;
481 u32 mac_ps_update_failed;
482 u32 if_update_failed;
483 u32 packet_request_failed;
484 u32 credit_request_failed;
485 u32 rollback_success;
487 u32 delayq_full_error;
488 u32 supprq_full_error;
499 struct brcmf_fws_info {
500 struct brcmf_pub *drvr;
503 struct brcmf_fws_stats stats;
504 struct brcmf_fws_hanger hanger;
505 enum brcmf_fws_fcmode fcmode;
507 bool bcmc_credit_check;
508 struct brcmf_fws_macdesc_table desc;
509 struct workqueue_struct *fws_wq;
510 struct work_struct fws_dequeue_work;
511 u32 fifo_enqpkt[BRCMF_FWS_FIFO_COUNT];
512 int fifo_credit[BRCMF_FWS_FIFO_COUNT];
513 int credits_borrowed[BRCMF_FWS_FIFO_AC_VO + 1];
514 int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
517 unsigned long borrow_defer_timestamp;
518 bool bus_flow_blocked;
519 bool creditmap_received;
525 * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
527 static const int brcmf_fws_prio2fifo[] = {
528 BRCMF_FWS_FIFO_AC_BE,
529 BRCMF_FWS_FIFO_AC_BK,
530 BRCMF_FWS_FIFO_AC_BK,
531 BRCMF_FWS_FIFO_AC_BE,
532 BRCMF_FWS_FIFO_AC_VI,
533 BRCMF_FWS_FIFO_AC_VI,
534 BRCMF_FWS_FIFO_AC_VO,
538 #define BRCMF_FWS_TLV_DEF(name, id, len) \
539 case BRCMF_FWS_TYPE_ ## name: \
543 * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
545 * @fws: firmware-signalling information.
546 * @id: identifier of the TLV.
548 * Return: the specified length for the given TLV; Otherwise -EINVAL.
550 static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
551 enum brcmf_fws_tlv_type id)
554 BRCMF_FWS_TLV_DEFLIST
556 fws->stats.tlv_invalid_type++;
561 #undef BRCMF_FWS_TLV_DEF
563 static void brcmf_fws_lock(struct brcmf_fws_info *fws)
564 __acquires(&fws->spinlock)
566 spin_lock_irqsave(&fws->spinlock, fws->flags);
569 static void brcmf_fws_unlock(struct brcmf_fws_info *fws)
570 __releases(&fws->spinlock)
572 spin_unlock_irqrestore(&fws->spinlock, fws->flags);
575 static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
577 u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
578 return ifidx == *(int *)arg;
581 static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
584 bool (*matchfn)(struct sk_buff *, void *) = NULL;
589 matchfn = brcmf_fws_ifidx_match;
590 for (prec = 0; prec < q->num_prec; prec++) {
591 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
593 brcmu_pkt_buf_free_skb(skb);
594 skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
599 static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
603 memset(hanger, 0, sizeof(*hanger));
604 for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
605 hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
608 static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
612 i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
614 while (i != h->slot_pos) {
615 if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
620 if (i == BRCMF_FWS_HANGER_MAXITEMS)
623 brcmf_err("all slots occupied\n");
624 h->failed_slotfind++;
625 i = BRCMF_FWS_HANGER_MAXITEMS;
630 static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
631 struct sk_buff *pkt, u32 slot_id)
633 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
636 if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
637 brcmf_err("slot is not free\n");
642 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
643 h->items[slot_id].pkt = pkt;
648 static inline int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
649 u32 slot_id, struct sk_buff **pktout,
652 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
655 if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
656 brcmf_err("entry not in use\n");
661 *pktout = h->items[slot_id].pkt;
663 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
664 h->items[slot_id].pkt = NULL;
670 static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
673 if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
676 if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
677 brcmf_err("entry not in use\n");
681 h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
685 static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
686 bool (*fn)(struct sk_buff *, void *),
689 struct brcmf_fws_hanger *h = &fws->hanger;
692 enum brcmf_fws_hanger_item_state s;
694 for (i = 0; i < ARRAY_SIZE(h->items); i++) {
695 s = h->items[i].state;
696 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE ||
697 s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
698 skb = h->items[i].pkt;
699 if (fn == NULL || fn(skb, &ifidx)) {
700 /* suppress packets freed from psq */
701 if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE)
702 brcmu_pkt_buf_free_skb(skb);
704 BRCMF_FWS_HANGER_ITEM_STATE_FREE;
710 static void brcmf_fws_macdesc_set_name(struct brcmf_fws_info *fws,
711 struct brcmf_fws_mac_descriptor *desc)
713 if (desc == &fws->desc.other)
714 strlcpy(desc->name, "MAC-OTHER", sizeof(desc->name));
715 else if (desc->mac_handle)
716 scnprintf(desc->name, sizeof(desc->name), "MAC-%d:%d",
717 desc->mac_handle, desc->interface_id);
719 scnprintf(desc->name, sizeof(desc->name), "MACIF:%d",
723 static void brcmf_fws_macdesc_init(struct brcmf_fws_mac_descriptor *desc,
727 "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
729 desc->state = BRCMF_FWS_STATE_OPEN;
730 desc->requested_credit = 0;
731 desc->requested_packet = 0;
732 /* depending on use may need ifp->bsscfgidx instead */
733 desc->interface_id = ifidx;
734 desc->ac_bitmap = 0xff; /* update this when handling APSD */
736 memcpy(&desc->ea[0], addr, ETH_ALEN);
740 void brcmf_fws_macdesc_deinit(struct brcmf_fws_mac_descriptor *desc)
743 "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
745 desc->state = BRCMF_FWS_STATE_CLOSE;
746 desc->requested_credit = 0;
747 desc->requested_packet = 0;
750 static struct brcmf_fws_mac_descriptor *
751 brcmf_fws_macdesc_lookup(struct brcmf_fws_info *fws, u8 *ea)
753 struct brcmf_fws_mac_descriptor *entry;
757 return ERR_PTR(-EINVAL);
759 entry = &fws->desc.nodes[0];
760 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
761 if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
766 return ERR_PTR(-ENOENT);
769 static struct brcmf_fws_mac_descriptor*
770 brcmf_fws_macdesc_find(struct brcmf_fws_info *fws, struct brcmf_if *ifp, u8 *da)
772 struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
775 multicast = is_multicast_ether_addr(da);
777 /* Multicast destination, STA and P2P clients get the interface entry.
778 * STA/GC gets the Mac Entry for TDLS destinations, TDLS destinations
779 * have their own entry.
781 if (multicast && ifp->fws_desc) {
782 entry = ifp->fws_desc;
786 entry = brcmf_fws_macdesc_lookup(fws, da);
788 entry = ifp->fws_desc;
794 static bool brcmf_fws_macdesc_closed(struct brcmf_fws_info *fws,
795 struct brcmf_fws_mac_descriptor *entry,
798 struct brcmf_fws_mac_descriptor *if_entry;
801 /* for unique destination entries the related interface
804 if (entry->mac_handle) {
805 if_entry = &fws->desc.iface[entry->interface_id];
806 if (if_entry->state == BRCMF_FWS_STATE_CLOSE)
809 /* an entry is closed when the state is closed and
810 * the firmware did not request anything.
812 closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
813 !entry->requested_credit && !entry->requested_packet;
815 /* Or firmware does not allow traffic for given fifo */
816 return closed || !(entry->ac_bitmap & BIT(fifo));
819 static void brcmf_fws_macdesc_cleanup(struct brcmf_fws_info *fws,
820 struct brcmf_fws_mac_descriptor *entry,
823 if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
824 brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
825 entry->occupied = !!(entry->psq.len);
829 static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
830 bool (*fn)(struct sk_buff *, void *),
833 struct brcmf_fws_hanger_item *hi;
839 txq = brcmf_bus_gettxq(fws->drvr->bus_if);
841 brcmf_dbg(TRACE, "no txq to clean up\n");
845 for (prec = 0; prec < txq->num_prec; prec++) {
846 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
848 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
849 hi = &fws->hanger.items[hslot];
850 WARN_ON(skb != hi->pkt);
851 hi->state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
852 brcmu_pkt_buf_free_skb(skb);
853 skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
858 static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
861 struct brcmf_fws_mac_descriptor *table;
862 bool (*matchfn)(struct sk_buff *, void *) = NULL;
868 matchfn = brcmf_fws_ifidx_match;
870 /* cleanup individual nodes */
871 table = &fws->desc.nodes[0];
872 for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
873 brcmf_fws_macdesc_cleanup(fws, &table[i], ifidx);
875 brcmf_fws_macdesc_cleanup(fws, &fws->desc.other, ifidx);
876 brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
877 brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
880 static u8 brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
882 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
886 __le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);
887 __le16 pktseq = cpu_to_le16(brcmf_skbcb(skb)->htod_seq);
889 brcmf_dbg(TRACE, "enter: %s, idx=%d hslot=%d htod %X seq %X\n",
890 entry->name, brcmf_skb_if_flags_get_field(skb, INDEX),
891 (le32_to_cpu(pkttag) >> 8) & 0xffff,
892 brcmf_skbcb(skb)->htod, brcmf_skbcb(skb)->htod_seq);
893 if (entry->send_tim_signal)
894 data_offset += 2 + BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
895 if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode))
896 data_offset += BRCMF_FWS_TYPE_SEQ_LEN;
897 /* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
898 data_offset += 2 + BRCMF_FWS_TYPE_PKTTAG_LEN;
899 fillers = round_up(data_offset, 4) - data_offset;
900 data_offset += fillers;
902 skb_push(skb, data_offset);
905 wlh[0] = BRCMF_FWS_TYPE_PKTTAG;
906 wlh[1] = BRCMF_FWS_TYPE_PKTTAG_LEN;
907 memcpy(&wlh[2], &pkttag, sizeof(pkttag));
908 if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode)) {
909 wlh[1] += BRCMF_FWS_TYPE_SEQ_LEN;
910 memcpy(&wlh[2 + BRCMF_FWS_TYPE_PKTTAG_LEN], &pktseq,
915 if (entry->send_tim_signal) {
916 entry->send_tim_signal = 0;
917 wlh[0] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP;
918 wlh[1] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
919 wlh[2] = entry->mac_handle;
920 wlh[3] = entry->traffic_pending_bmp;
921 brcmf_dbg(TRACE, "adding TIM info: handle %d bmp 0x%X\n",
922 entry->mac_handle, entry->traffic_pending_bmp);
923 wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
924 entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
927 memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);
929 return (u8)(data_offset >> 2);
932 static bool brcmf_fws_tim_update(struct brcmf_fws_info *fws,
933 struct brcmf_fws_mac_descriptor *entry,
934 int fifo, bool send_immediately)
937 struct brcmf_skbuff_cb *skcb;
943 /* check delayedQ and suppressQ in one call using bitmap */
944 if (brcmu_pktq_mlen(&entry->psq, 3 << (fifo * 2)) == 0)
945 entry->traffic_pending_bmp &= ~NBITVAL(fifo);
947 entry->traffic_pending_bmp |= NBITVAL(fifo);
949 entry->send_tim_signal = false;
950 if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
951 entry->send_tim_signal = true;
952 if (send_immediately && entry->send_tim_signal &&
953 entry->state == BRCMF_FWS_STATE_CLOSE) {
954 /* create a dummy packet and sent that. The traffic */
955 /* bitmap info will automatically be attached to that packet */
956 len = BRCMF_FWS_TYPE_PKTTAG_LEN + 2 +
957 BRCMF_FWS_TYPE_SEQ_LEN +
958 BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2 +
959 4 + fws->drvr->hdrlen;
960 skb = brcmu_pkt_buf_get_skb(len);
964 skcb = brcmf_skbcb(skb);
966 skcb->state = BRCMF_FWS_SKBSTATE_TIM;
969 data_offset = brcmf_fws_hdrpush(fws, skb);
970 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
971 brcmf_fws_unlock(fws);
972 err = brcmf_proto_txdata(fws->drvr, ifidx, data_offset, skb);
975 brcmu_pkt_buf_free_skb(skb);
982 brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
985 struct brcmf_if *ifp = brcmf_get_ifp(fws->drvr, if_id);
990 if ((ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
991 pq->len <= BRCMF_FWS_FLOWCONTROL_LOWATER)
992 brcmf_txflowblock_if(ifp,
993 BRCMF_NETIF_STOP_REASON_FWS_FC, false);
994 if (!(ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
995 pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER) {
996 fws->stats.fws_flow_block++;
997 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
1002 static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
1004 brcmf_dbg(CTL, "rssi %d\n", rssi);
1009 int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
1011 struct brcmf_fws_mac_descriptor *entry, *existing;
1016 mac_handle = *data++;
1020 entry = &fws->desc.nodes[mac_handle & 0x1F];
1021 if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
1022 if (entry->occupied) {
1023 brcmf_dbg(TRACE, "deleting %s mac %pM\n",
1025 brcmf_fws_lock(fws);
1026 brcmf_fws_macdesc_cleanup(fws, entry, -1);
1027 brcmf_fws_macdesc_deinit(entry);
1028 brcmf_fws_unlock(fws);
1030 fws->stats.mac_update_failed++;
1034 existing = brcmf_fws_macdesc_lookup(fws, addr);
1035 if (IS_ERR(existing)) {
1036 if (!entry->occupied) {
1037 brcmf_fws_lock(fws);
1038 entry->mac_handle = mac_handle;
1039 brcmf_fws_macdesc_init(entry, addr, ifidx);
1040 brcmf_fws_macdesc_set_name(fws, entry);
1041 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
1043 brcmf_fws_unlock(fws);
1044 brcmf_dbg(TRACE, "add %s mac %pM\n", entry->name, addr);
1046 fws->stats.mac_update_failed++;
1049 if (entry != existing) {
1050 brcmf_dbg(TRACE, "copy mac %s\n", existing->name);
1051 brcmf_fws_lock(fws);
1052 memcpy(entry, existing,
1053 offsetof(struct brcmf_fws_mac_descriptor, psq));
1054 entry->mac_handle = mac_handle;
1055 brcmf_fws_macdesc_deinit(existing);
1056 brcmf_fws_macdesc_set_name(fws, entry);
1057 brcmf_fws_unlock(fws);
1058 brcmf_dbg(TRACE, "relocate %s mac %pM\n", entry->name,
1061 brcmf_dbg(TRACE, "use existing\n");
1062 WARN_ON(entry->mac_handle != mac_handle);
1063 /* TODO: what should we do here: continue, reinit, .. */
1069 static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
1072 struct brcmf_fws_mac_descriptor *entry;
1076 mac_handle = data[0];
1077 entry = &fws->desc.nodes[mac_handle & 0x1F];
1078 if (!entry->occupied) {
1079 fws->stats.mac_ps_update_failed++;
1082 brcmf_fws_lock(fws);
1083 /* a state update should wipe old credits */
1084 entry->requested_credit = 0;
1085 entry->requested_packet = 0;
1086 if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
1087 entry->state = BRCMF_FWS_STATE_OPEN;
1088 ret = BRCMF_FWS_RET_OK_SCHEDULE;
1090 entry->state = BRCMF_FWS_STATE_CLOSE;
1091 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BK, false);
1092 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BE, false);
1093 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VI, false);
1094 brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VO, true);
1095 ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1097 brcmf_fws_unlock(fws);
1101 static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
1104 struct brcmf_fws_mac_descriptor *entry;
1110 if (ifidx >= BRCMF_MAX_IFS) {
1115 entry = &fws->desc.iface[ifidx];
1116 if (!entry->occupied) {
1121 brcmf_dbg(TRACE, "%s (%d): %s\n", brcmf_fws_get_tlv_name(type), type,
1123 brcmf_fws_lock(fws);
1125 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1126 entry->state = BRCMF_FWS_STATE_OPEN;
1127 ret = BRCMF_FWS_RET_OK_SCHEDULE;
1129 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1130 entry->state = BRCMF_FWS_STATE_CLOSE;
1131 ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1135 brcmf_fws_unlock(fws);
1138 brcmf_fws_unlock(fws);
1142 fws->stats.if_update_failed++;
1146 static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
1149 struct brcmf_fws_mac_descriptor *entry;
1151 entry = &fws->desc.nodes[data[1] & 0x1F];
1152 if (!entry->occupied) {
1153 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1154 fws->stats.credit_request_failed++;
1156 fws->stats.packet_request_failed++;
1160 brcmf_dbg(TRACE, "%s (%d): %s cnt %d bmp %d\n",
1161 brcmf_fws_get_tlv_name(type), type, entry->name,
1163 brcmf_fws_lock(fws);
1164 if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1165 entry->requested_credit = data[0];
1167 entry->requested_packet = data[0];
1169 entry->ac_bitmap = data[2];
1170 brcmf_fws_unlock(fws);
1171 return BRCMF_FWS_RET_OK_SCHEDULE;
1175 brcmf_fws_macdesc_use_req_credit(struct brcmf_fws_mac_descriptor *entry,
1176 struct sk_buff *skb)
1178 if (entry->requested_credit > 0) {
1179 entry->requested_credit--;
1180 brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1181 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 1);
1182 if (entry->state != BRCMF_FWS_STATE_CLOSE)
1183 brcmf_err("requested credit set while mac not closed!\n");
1184 } else if (entry->requested_packet > 0) {
1185 entry->requested_packet--;
1186 brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
1187 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1188 if (entry->state != BRCMF_FWS_STATE_CLOSE)
1189 brcmf_err("requested packet set while mac not closed!\n");
1191 brcmf_skb_if_flags_set_field(skb, REQUESTED, 0);
1192 brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1196 static void brcmf_fws_macdesc_return_req_credit(struct sk_buff *skb)
1198 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1200 if ((brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) &&
1201 (entry->state == BRCMF_FWS_STATE_CLOSE))
1202 entry->requested_credit++;
1205 static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
1206 u8 fifo, u8 credits)
1215 fws->fifo_credit_map |= 1 << fifo;
1217 if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
1218 (fws->credits_borrowed[0])) {
1219 for (lender_ac = BRCMF_FWS_FIFO_AC_VO; lender_ac >= 0;
1221 borrowed = &fws->credits_borrowed[lender_ac];
1223 fws->fifo_credit_map |= (1 << lender_ac);
1224 fifo_credit = &fws->fifo_credit[lender_ac];
1225 if (*borrowed >= credits) {
1226 *borrowed -= credits;
1227 *fifo_credit += credits;
1230 credits -= *borrowed;
1231 *fifo_credit += *borrowed;
1238 fws->fifo_credit[fifo] += credits;
1241 static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
1243 /* only schedule dequeue when there are credits for delayed traffic */
1244 if ((fws->fifo_credit_map & fws->fifo_delay_map) ||
1245 (!brcmf_fws_fc_active(fws) && fws->fifo_delay_map))
1246 queue_work(fws->fws_wq, &fws->fws_dequeue_work);
1249 static int brcmf_fws_enq(struct brcmf_fws_info *fws,
1250 enum brcmf_fws_skb_state state, int fifo,
1253 int prec = 2 * fifo;
1254 u32 *qfull_stat = &fws->stats.delayq_full_error;
1255 struct brcmf_fws_mac_descriptor *entry;
1257 struct sk_buff_head *queue;
1258 struct sk_buff *p_head;
1259 struct sk_buff *p_tail;
1263 entry = brcmf_skbcb(p)->mac;
1264 if (entry == NULL) {
1265 brcmf_err("no mac descriptor found for skb %p\n", p);
1269 brcmf_dbg(DATA, "enter: fifo %d skb %p\n", fifo, p);
1270 if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1272 qfull_stat = &fws->stats.supprq_full_error;
1274 /* Fix out of order delivery of frames. Dont assume frame */
1275 /* can be inserted at the end, but look for correct position */
1277 if (pktq_full(pq) || pktq_pfull(pq, prec)) {
1281 queue = &pq->q[prec].skblist;
1283 p_head = skb_peek(queue);
1284 p_tail = skb_peek_tail(queue);
1285 fr_new = brcmf_skb_htod_tag_get_field(p, FREERUN);
1287 while (p_head != p_tail) {
1288 fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
1290 /* be sure to handle wrap of 256 */
1291 if (((fr_new > fr_compare) &&
1292 ((fr_new - fr_compare) < 128)) ||
1293 ((fr_new < fr_compare) &&
1294 ((fr_compare - fr_new) > 128)))
1296 p_tail = skb_queue_prev(queue, p_tail);
1298 /* Position found. Determine what to do */
1299 if (p_tail == NULL) {
1301 __skb_queue_tail(queue, p);
1303 fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
1305 if (((fr_new > fr_compare) &&
1306 ((fr_new - fr_compare) < 128)) ||
1307 ((fr_new < fr_compare) &&
1308 ((fr_compare - fr_new) > 128))) {
1310 __skb_queue_after(queue, p_tail, p);
1313 __skb_insert(p, p_tail->prev, p_tail, queue);
1317 /* Complete the counters and statistics */
1319 if (pq->hi_prec < prec)
1320 pq->hi_prec = (u8) prec;
1321 } else if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
1326 /* increment total enqueued packet count */
1327 fws->fifo_delay_map |= 1 << fifo;
1328 fws->fifo_enqpkt[fifo]++;
1330 /* update the sk_buff state */
1331 brcmf_skbcb(p)->state = state;
1334 * A packet has been pushed so update traffic
1335 * availability bitmap, if applicable
1337 brcmf_fws_tim_update(fws, entry, fifo, true);
1338 brcmf_fws_flow_control_check(fws, &entry->psq,
1339 brcmf_skb_if_flags_get_field(p, INDEX));
1343 static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
1345 struct brcmf_fws_mac_descriptor *table;
1346 struct brcmf_fws_mac_descriptor *entry;
1354 table = (struct brcmf_fws_mac_descriptor *)&fws->desc;
1355 num_nodes = sizeof(fws->desc) / sizeof(struct brcmf_fws_mac_descriptor);
1356 node_pos = fws->deq_node_pos[fifo];
1358 for (i = 0; i < num_nodes; i++) {
1359 entry = &table[(node_pos + i) % num_nodes];
1360 if (!entry->occupied ||
1361 brcmf_fws_macdesc_closed(fws, entry, fifo))
1364 if (entry->suppressed)
1368 p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
1370 if (entry->suppressed) {
1371 if (entry->suppr_transit_count)
1373 entry->suppressed = false;
1374 p = brcmu_pktq_mdeq(&entry->psq,
1375 1 << (fifo * 2), &prec_out);
1381 brcmf_fws_macdesc_use_req_credit(entry, p);
1383 /* move dequeue position to ensure fair round-robin */
1384 fws->deq_node_pos[fifo] = (node_pos + i + 1) % num_nodes;
1385 brcmf_fws_flow_control_check(fws, &entry->psq,
1386 brcmf_skb_if_flags_get_field(p,
1390 * A packet has been picked up, update traffic
1391 * availability bitmap, if applicable
1393 brcmf_fws_tim_update(fws, entry, fifo, false);
1396 * decrement total enqueued fifo packets and
1397 * clear delay bitmap if done.
1399 fws->fifo_enqpkt[fifo]--;
1400 if (fws->fifo_enqpkt[fifo] == 0)
1401 fws->fifo_delay_map &= ~(1 << fifo);
1406 brcmf_dbg(DATA, "exit: fifo %d skb %p\n", fifo, p);
1410 static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
1411 struct sk_buff *skb,
1412 u32 genbit, u16 seq)
1414 struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1418 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1420 /* this packet was suppressed */
1421 if (!entry->suppressed) {
1422 entry->suppressed = true;
1423 entry->suppr_transit_count = entry->transit_count;
1424 brcmf_dbg(DATA, "suppress %s: transit %d\n",
1425 entry->name, entry->transit_count);
1428 entry->generation = genbit;
1430 brcmf_skb_htod_tag_set_field(skb, GENERATION, genbit);
1431 brcmf_skbcb(skb)->htod_seq = seq;
1432 if (brcmf_skb_htod_seq_get_field(skb, FROMFW)) {
1433 brcmf_skb_htod_seq_set_field(skb, FROMDRV, 1);
1434 brcmf_skb_htod_seq_set_field(skb, FROMFW, 0);
1436 brcmf_skb_htod_seq_set_field(skb, FROMDRV, 0);
1438 ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo, skb);
1441 /* suppress q is full drop this packet */
1442 brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb, true);
1444 /* Mark suppressed to avoid a double free during wlfc cleanup */
1445 brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot);
1452 brcmf_fws_txs_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
1453 u32 genbit, u16 seq)
1457 bool remove_from_hanger = true;
1458 struct sk_buff *skb;
1459 struct brcmf_skbuff_cb *skcb;
1460 struct brcmf_fws_mac_descriptor *entry = NULL;
1461 struct brcmf_if *ifp;
1463 brcmf_dbg(DATA, "flags %d\n", flags);
1465 if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
1466 fws->stats.txs_discard++;
1467 else if (flags == BRCMF_FWS_TXSTATUS_CORE_SUPPRESS) {
1468 fws->stats.txs_supp_core++;
1469 remove_from_hanger = false;
1470 } else if (flags == BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS) {
1471 fws->stats.txs_supp_ps++;
1472 remove_from_hanger = false;
1473 } else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
1474 fws->stats.txs_tossed++;
1475 else if (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)
1476 fws->stats.txs_host_tossed++;
1478 brcmf_err("unexpected txstatus\n");
1480 ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1481 remove_from_hanger);
1483 brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
1487 skcb = brcmf_skbcb(skb);
1489 if (WARN_ON(!entry)) {
1490 brcmu_pkt_buf_free_skb(skb);
1493 entry->transit_count--;
1494 if (entry->suppressed && entry->suppr_transit_count)
1495 entry->suppr_transit_count--;
1497 brcmf_dbg(DATA, "%s flags %d htod %X seq %X\n", entry->name, flags,
1500 /* pick up the implicit credit from this packet */
1501 fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
1502 if ((fws->fcmode == BRCMF_FWS_FCMODE_IMPLIED_CREDIT) ||
1503 (brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) ||
1504 (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)) {
1505 brcmf_fws_return_credits(fws, fifo, 1);
1506 brcmf_fws_schedule_deq(fws);
1508 brcmf_fws_macdesc_return_req_credit(skb);
1510 ret = brcmf_proto_hdrpull(fws->drvr, false, skb, &ifp);
1512 brcmu_pkt_buf_free_skb(skb);
1515 if (!remove_from_hanger)
1516 ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb,
1518 if (remove_from_hanger || ret)
1519 brcmf_txfinalize(ifp, skb, true);
1524 static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
1529 if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
1530 brcmf_dbg(INFO, "ignored\n");
1531 return BRCMF_FWS_RET_OK_NOSCHEDULE;
1534 brcmf_dbg(DATA, "enter: data %pM\n", data);
1535 brcmf_fws_lock(fws);
1536 for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
1537 brcmf_fws_return_credits(fws, i, data[i]);
1539 brcmf_dbg(DATA, "map: credit %x delay %x\n", fws->fifo_credit_map,
1540 fws->fifo_delay_map);
1541 brcmf_fws_unlock(fws);
1542 return BRCMF_FWS_RET_OK_SCHEDULE;
1545 static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
1555 fws->stats.txs_indicate++;
1556 memcpy(&status_le, data, sizeof(status_le));
1557 status = le32_to_cpu(status_le);
1558 flags = brcmf_txstatus_get_field(status, FLAGS);
1559 hslot = brcmf_txstatus_get_field(status, HSLOT);
1560 genbit = brcmf_txstatus_get_field(status, GENERATION);
1561 if (BRCMF_FWS_MODE_GET_REUSESEQ(fws->mode)) {
1562 memcpy(&seq_le, &data[BRCMF_FWS_TYPE_PKTTAG_LEN],
1564 seq = le16_to_cpu(seq_le);
1569 brcmf_fws_lock(fws);
1570 brcmf_fws_txs_process(fws, flags, hslot, genbit, seq);
1571 brcmf_fws_unlock(fws);
1572 return BRCMF_FWS_RET_OK_NOSCHEDULE;
1575 static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
1579 memcpy(×tamp, &data[2], sizeof(timestamp));
1580 brcmf_dbg(CTL, "received: seq %d, timestamp %d\n", data[1],
1581 le32_to_cpu(timestamp));
1585 static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
1586 const struct brcmf_event_msg *e,
1589 struct brcmf_fws_info *fws = ifp->drvr->fws;
1593 if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
1594 brcmf_err("event payload too small (%d)\n", e->datalen);
1597 if (fws->creditmap_received)
1600 fws->creditmap_received = true;
1602 brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
1603 brcmf_fws_lock(fws);
1604 for (i = 0; i < ARRAY_SIZE(fws->fifo_credit); i++) {
1606 fws->fifo_credit_map |= 1 << i;
1608 fws->fifo_credit_map &= ~(1 << i);
1609 fws->fifo_credit[i] = *credits++;
1611 brcmf_fws_schedule_deq(fws);
1612 brcmf_fws_unlock(fws);
1616 static int brcmf_fws_notify_bcmc_credit_support(struct brcmf_if *ifp,
1617 const struct brcmf_event_msg *e,
1620 struct brcmf_fws_info *fws = ifp->drvr->fws;
1623 brcmf_fws_lock(fws);
1624 fws->bcmc_credit_check = true;
1625 brcmf_fws_unlock(fws);
1630 static void brcmf_rxreorder_get_skb_list(struct brcmf_ampdu_rx_reorder *rfi,
1632 struct sk_buff_head *skb_list)
1634 /* initialize return list */
1635 __skb_queue_head_init(skb_list);
1637 if (rfi->pend_pkts == 0) {
1638 brcmf_dbg(INFO, "no packets in reorder queue\n");
1643 if (rfi->pktslots[start]) {
1644 __skb_queue_tail(skb_list, rfi->pktslots[start]);
1645 rfi->pktslots[start] = NULL;
1648 if (start > rfi->max_idx)
1650 } while (start != end);
1651 rfi->pend_pkts -= skb_queue_len(skb_list);
1654 void brcmf_fws_rxreorder(struct brcmf_if *ifp, struct sk_buff *pkt)
1657 u8 flow_id, max_idx, cur_idx, exp_idx, end_idx;
1658 struct brcmf_ampdu_rx_reorder *rfi;
1659 struct sk_buff_head reorder_list;
1660 struct sk_buff *pnext;
1664 reorder_data = ((struct brcmf_skb_reorder_data *)pkt->cb)->reorder;
1665 flow_id = reorder_data[BRCMF_RXREORDER_FLOWID_OFFSET];
1666 flags = reorder_data[BRCMF_RXREORDER_FLAGS_OFFSET];
1668 /* validate flags and flow id */
1669 if (flags == 0xFF) {
1670 brcmf_err("invalid flags...so ignore this packet\n");
1671 brcmf_netif_rx(ifp, pkt);
1675 rfi = ifp->drvr->reorder_flows[flow_id];
1676 if (flags & BRCMF_RXREORDER_DEL_FLOW) {
1677 brcmf_dbg(INFO, "flow-%d: delete\n",
1681 brcmf_dbg(INFO, "received flags to cleanup, but no flow (%d) yet\n",
1683 brcmf_netif_rx(ifp, pkt);
1687 brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, rfi->exp_idx,
1689 /* add the last packet */
1690 __skb_queue_tail(&reorder_list, pkt);
1692 ifp->drvr->reorder_flows[flow_id] = NULL;
1695 /* from here on we need a flow reorder instance */
1697 buf_size = sizeof(*rfi);
1698 max_idx = reorder_data[BRCMF_RXREORDER_MAXIDX_OFFSET];
1700 buf_size += (max_idx + 1) * sizeof(pkt);
1702 /* allocate space for flow reorder info */
1703 brcmf_dbg(INFO, "flow-%d: start, maxidx %d\n",
1705 rfi = kzalloc(buf_size, GFP_ATOMIC);
1707 brcmf_err("failed to alloc buffer\n");
1708 brcmf_netif_rx(ifp, pkt);
1712 ifp->drvr->reorder_flows[flow_id] = rfi;
1713 rfi->pktslots = (struct sk_buff **)(rfi + 1);
1714 rfi->max_idx = max_idx;
1716 if (flags & BRCMF_RXREORDER_NEW_HOLE) {
1717 if (rfi->pend_pkts) {
1718 brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx,
1721 WARN_ON(rfi->pend_pkts);
1723 __skb_queue_head_init(&reorder_list);
1725 rfi->cur_idx = reorder_data[BRCMF_RXREORDER_CURIDX_OFFSET];
1726 rfi->exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
1727 rfi->max_idx = reorder_data[BRCMF_RXREORDER_MAXIDX_OFFSET];
1728 rfi->pktslots[rfi->cur_idx] = pkt;
1730 brcmf_dbg(DATA, "flow-%d: new hole %d (%d), pending %d\n",
1731 flow_id, rfi->cur_idx, rfi->exp_idx, rfi->pend_pkts);
1732 } else if (flags & BRCMF_RXREORDER_CURIDX_VALID) {
1733 cur_idx = reorder_data[BRCMF_RXREORDER_CURIDX_OFFSET];
1734 exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
1736 if ((exp_idx == rfi->exp_idx) && (cur_idx != rfi->exp_idx)) {
1737 /* still in the current hole */
1738 /* enqueue the current on the buffer chain */
1739 if (rfi->pktslots[cur_idx] != NULL) {
1740 brcmf_dbg(INFO, "HOLE: ERROR buffer pending..free it\n");
1741 brcmu_pkt_buf_free_skb(rfi->pktslots[cur_idx]);
1742 rfi->pktslots[cur_idx] = NULL;
1744 rfi->pktslots[cur_idx] = pkt;
1746 rfi->cur_idx = cur_idx;
1747 brcmf_dbg(DATA, "flow-%d: store pkt %d (%d), pending %d\n",
1748 flow_id, cur_idx, exp_idx, rfi->pend_pkts);
1750 /* can return now as there is no reorder
1755 if (rfi->exp_idx == cur_idx) {
1756 if (rfi->pktslots[cur_idx] != NULL) {
1757 brcmf_dbg(INFO, "error buffer pending..free it\n");
1758 brcmu_pkt_buf_free_skb(rfi->pktslots[cur_idx]);
1759 rfi->pktslots[cur_idx] = NULL;
1761 rfi->pktslots[cur_idx] = pkt;
1764 /* got the expected one. flush from current to expected
1765 * and update expected
1767 brcmf_dbg(DATA, "flow-%d: expected %d (%d), pending %d\n",
1768 flow_id, cur_idx, exp_idx, rfi->pend_pkts);
1770 rfi->cur_idx = cur_idx;
1771 rfi->exp_idx = exp_idx;
1773 brcmf_rxreorder_get_skb_list(rfi, cur_idx, exp_idx,
1775 brcmf_dbg(DATA, "flow-%d: freeing buffers %d, pending %d\n",
1776 flow_id, skb_queue_len(&reorder_list),
1781 brcmf_dbg(DATA, "flow-%d (0x%x): both moved, old %d/%d, new %d/%d\n",
1782 flow_id, flags, rfi->cur_idx, rfi->exp_idx,
1784 if (flags & BRCMF_RXREORDER_FLUSH_ALL)
1785 end_idx = rfi->exp_idx;
1789 /* flush pkts first */
1790 brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, end_idx,
1793 if (exp_idx == ((cur_idx + 1) % (rfi->max_idx + 1))) {
1794 __skb_queue_tail(&reorder_list, pkt);
1796 rfi->pktslots[cur_idx] = pkt;
1799 rfi->exp_idx = exp_idx;
1800 rfi->cur_idx = cur_idx;
1803 /* explicity window move updating the expected index */
1804 exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
1806 brcmf_dbg(DATA, "flow-%d (0x%x): change expected: %d -> %d\n",
1807 flow_id, flags, rfi->exp_idx, exp_idx);
1808 if (flags & BRCMF_RXREORDER_FLUSH_ALL)
1809 end_idx = rfi->exp_idx;
1813 brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, end_idx,
1815 __skb_queue_tail(&reorder_list, pkt);
1816 /* set the new expected idx */
1817 rfi->exp_idx = exp_idx;
1820 skb_queue_walk_safe(&reorder_list, pkt, pnext) {
1821 __skb_unlink(pkt, &reorder_list);
1822 brcmf_netif_rx(ifp, pkt);
1826 void brcmf_fws_hdrpull(struct brcmf_if *ifp, s16 siglen, struct sk_buff *skb)
1828 struct brcmf_skb_reorder_data *rd;
1829 struct brcmf_fws_info *fws = ifp->drvr->fws;
1838 brcmf_dbg(HDRS, "enter: ifidx %d, skblen %u, sig %d\n",
1839 ifp->ifidx, skb->len, siglen);
1841 WARN_ON(siglen > skb->len);
1845 /* if flow control disabled, skip to packet data and leave */
1846 if ((!fws) || (!fws->fw_signals)) {
1847 skb_pull(skb, siglen);
1851 fws->stats.header_pulls++;
1853 signal_data = skb->data;
1855 status = BRCMF_FWS_RET_OK_NOSCHEDULE;
1856 while (data_len > 0) {
1857 /* extract tlv info */
1858 type = signal_data[0];
1860 /* FILLER type is actually not a TLV, but
1861 * a single byte that can be skipped.
1863 if (type == BRCMF_FWS_TYPE_FILLER) {
1868 len = signal_data[1];
1869 data = signal_data + 2;
1871 brcmf_dbg(HDRS, "tlv type=%s (%d), len=%d (%d)\n",
1872 brcmf_fws_get_tlv_name(type), type, len,
1873 brcmf_fws_get_tlv_len(fws, type));
1875 /* abort parsing when length invalid */
1876 if (data_len < len + 2)
1879 if (len < brcmf_fws_get_tlv_len(fws, type))
1882 err = BRCMF_FWS_RET_OK_NOSCHEDULE;
1884 case BRCMF_FWS_TYPE_COMP_TXSTATUS:
1886 case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
1887 rd = (struct brcmf_skb_reorder_data *)skb->cb;
1890 case BRCMF_FWS_TYPE_MACDESC_ADD:
1891 case BRCMF_FWS_TYPE_MACDESC_DEL:
1892 brcmf_fws_macdesc_indicate(fws, type, data);
1894 case BRCMF_FWS_TYPE_MAC_OPEN:
1895 case BRCMF_FWS_TYPE_MAC_CLOSE:
1896 err = brcmf_fws_macdesc_state_indicate(fws, type, data);
1898 case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1899 case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1900 err = brcmf_fws_interface_state_indicate(fws, type,
1903 case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
1904 case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
1905 err = brcmf_fws_request_indicate(fws, type, data);
1907 case BRCMF_FWS_TYPE_TXSTATUS:
1908 brcmf_fws_txstatus_indicate(fws, data);
1910 case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
1911 err = brcmf_fws_fifocreditback_indicate(fws, data);
1913 case BRCMF_FWS_TYPE_RSSI:
1914 brcmf_fws_rssi_indicate(fws, *data);
1916 case BRCMF_FWS_TYPE_TRANS_ID:
1917 brcmf_fws_dbg_seqnum_check(fws, data);
1919 case BRCMF_FWS_TYPE_PKTTAG:
1920 case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
1922 fws->stats.tlv_invalid_type++;
1925 if (err == BRCMF_FWS_RET_OK_SCHEDULE)
1926 status = BRCMF_FWS_RET_OK_SCHEDULE;
1927 signal_data += len + 2;
1928 data_len -= len + 2;
1932 fws->stats.tlv_parse_failed++;
1934 if (status == BRCMF_FWS_RET_OK_SCHEDULE)
1935 brcmf_fws_schedule_deq(fws);
1937 /* signalling processing result does
1938 * not affect the actual ethernet packet.
1940 skb_pull(skb, siglen);
1942 /* this may be a signal-only packet
1945 fws->stats.header_only_pkt++;
1948 static u8 brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
1951 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1952 struct brcmf_fws_mac_descriptor *entry = skcb->mac;
1955 if (skcb->state != BRCMF_FWS_SKBSTATE_SUPPRESSED)
1956 brcmf_skb_htod_tag_set_field(p, GENERATION, entry->generation);
1957 flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
1958 if (brcmf_skb_if_flags_get_field(p, REQUESTED)) {
1960 * Indicate that this packet is being sent in response to an
1961 * explicit request from the firmware side.
1963 flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
1965 brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
1966 return brcmf_fws_hdrpush(fws, p);
1969 static void brcmf_fws_rollback_toq(struct brcmf_fws_info *fws,
1970 struct sk_buff *skb, int fifo)
1972 struct brcmf_fws_mac_descriptor *entry;
1973 struct sk_buff *pktout;
1977 entry = brcmf_skbcb(skb)->mac;
1978 if (entry->occupied) {
1980 if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
1983 pktout = brcmu_pktq_penq_head(&entry->psq, qidx, skb);
1984 if (pktout == NULL) {
1985 brcmf_err("%s queue %d full\n", entry->name, qidx);
1989 brcmf_err("%s entry removed\n", entry->name);
1994 fws->stats.rollback_failed++;
1995 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1996 brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED,
1999 fws->stats.rollback_success++;
2000 brcmf_fws_return_credits(fws, fifo, 1);
2001 brcmf_fws_macdesc_return_req_credit(skb);
2005 static int brcmf_fws_borrow_credit(struct brcmf_fws_info *fws)
2009 if (time_after(fws->borrow_defer_timestamp, jiffies)) {
2010 fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
2014 for (lender_ac = 0; lender_ac <= BRCMF_FWS_FIFO_AC_VO; lender_ac++) {
2015 if (fws->fifo_credit[lender_ac]) {
2016 fws->credits_borrowed[lender_ac]++;
2017 fws->fifo_credit[lender_ac]--;
2018 if (fws->fifo_credit[lender_ac] == 0)
2019 fws->fifo_credit_map &= ~(1 << lender_ac);
2020 fws->fifo_credit_map |= (1 << BRCMF_FWS_FIFO_AC_BE);
2021 brcmf_dbg(DATA, "borrow credit from: %d\n", lender_ac);
2025 fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
2029 static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
2030 struct sk_buff *skb)
2032 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
2033 struct brcmf_fws_mac_descriptor *entry;
2040 return PTR_ERR(entry);
2042 data_offset = brcmf_fws_precommit_skb(fws, fifo, skb);
2043 entry->transit_count++;
2044 if (entry->suppressed)
2045 entry->suppr_transit_count++;
2046 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
2047 brcmf_fws_unlock(fws);
2048 rc = brcmf_proto_txdata(fws->drvr, ifidx, data_offset, skb);
2049 brcmf_fws_lock(fws);
2050 brcmf_dbg(DATA, "%s flags %X htod %X bus_tx %d\n", entry->name,
2051 skcb->if_flags, skcb->htod, rc);
2053 entry->transit_count--;
2054 if (entry->suppressed)
2055 entry->suppr_transit_count--;
2056 (void)brcmf_proto_hdrpull(fws->drvr, false, skb, NULL);
2060 fws->stats.pkt2bus++;
2061 fws->stats.send_pkts[fifo]++;
2062 if (brcmf_skb_if_flags_get_field(skb, REQUESTED))
2063 fws->stats.requested_sent[fifo]++;
2068 brcmf_fws_rollback_toq(fws, skb, fifo);
2072 static int brcmf_fws_assign_htod(struct brcmf_fws_info *fws, struct sk_buff *p,
2075 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
2080 hslot = brcmf_fws_hanger_get_free_slot(&fws->hanger);
2081 brcmf_skb_htod_tag_set_field(p, HSLOT, hslot);
2082 brcmf_skb_htod_tag_set_field(p, FREERUN, skcb->mac->seq[fifo]);
2083 brcmf_skb_htod_tag_set_field(p, FIFO, fifo);
2084 rc = brcmf_fws_hanger_pushpkt(&fws->hanger, p, hslot);
2086 skcb->mac->seq[fifo]++;
2088 fws->stats.generic_error++;
2092 int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
2094 struct brcmf_pub *drvr = ifp->drvr;
2095 struct brcmf_fws_info *fws = drvr->fws;
2096 struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
2097 struct ethhdr *eh = (struct ethhdr *)(skb->data);
2098 int fifo = BRCMF_FWS_FIFO_BCMC;
2099 bool multicast = is_multicast_ether_addr(eh->h_dest);
2102 brcmf_dbg(DATA, "tx proto=0x%X\n", ntohs(eh->h_proto));
2103 /* determine the priority */
2104 if ((skb->priority == 0) || (skb->priority > 7))
2105 skb->priority = cfg80211_classify8021d(skb, NULL);
2107 if (fws->avoid_queueing) {
2108 rc = brcmf_proto_txdata(drvr, ifp->ifidx, 0, skb);
2110 brcmf_txfinalize(ifp, skb, false);
2114 /* set control buffer information */
2116 skcb->state = BRCMF_FWS_SKBSTATE_NEW;
2117 brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
2119 fifo = brcmf_fws_prio2fifo[skb->priority];
2121 brcmf_fws_lock(fws);
2122 if (fifo != BRCMF_FWS_FIFO_AC_BE && fifo < BRCMF_FWS_FIFO_BCMC)
2123 fws->borrow_defer_timestamp = jiffies +
2124 BRCMF_FWS_BORROW_DEFER_PERIOD;
2126 skcb->mac = brcmf_fws_macdesc_find(fws, ifp, eh->h_dest);
2127 brcmf_dbg(DATA, "%s mac %pM multi %d fifo %d\n", skcb->mac->name,
2128 eh->h_dest, multicast, fifo);
2129 if (!brcmf_fws_assign_htod(fws, skb, fifo)) {
2130 brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
2131 brcmf_fws_schedule_deq(fws);
2133 brcmf_err("drop skb: no hanger slot\n");
2134 brcmf_txfinalize(ifp, skb, false);
2137 brcmf_fws_unlock(fws);
2142 void brcmf_fws_reset_interface(struct brcmf_if *ifp)
2144 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
2146 brcmf_dbg(TRACE, "enter: bsscfgidx=%d\n", ifp->bsscfgidx);
2150 brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
2153 void brcmf_fws_add_interface(struct brcmf_if *ifp)
2155 struct brcmf_fws_info *fws = ifp->drvr->fws;
2156 struct brcmf_fws_mac_descriptor *entry;
2161 entry = &fws->desc.iface[ifp->ifidx];
2162 ifp->fws_desc = entry;
2163 brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
2164 brcmf_fws_macdesc_set_name(fws, entry);
2165 brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
2167 brcmf_dbg(TRACE, "added %s\n", entry->name);
2170 void brcmf_fws_del_interface(struct brcmf_if *ifp)
2172 struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
2177 brcmf_fws_lock(ifp->drvr->fws);
2178 ifp->fws_desc = NULL;
2179 brcmf_dbg(TRACE, "deleting %s\n", entry->name);
2180 brcmf_fws_macdesc_deinit(entry);
2181 brcmf_fws_cleanup(ifp->drvr->fws, ifp->ifidx);
2182 brcmf_fws_unlock(ifp->drvr->fws);
2185 static void brcmf_fws_dequeue_worker(struct work_struct *worker)
2187 struct brcmf_fws_info *fws;
2188 struct brcmf_pub *drvr;
2189 struct sk_buff *skb;
2195 fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
2198 brcmf_fws_lock(fws);
2199 for (fifo = BRCMF_FWS_FIFO_BCMC; fifo >= 0 && !fws->bus_flow_blocked;
2201 if (!brcmf_fws_fc_active(fws)) {
2202 while ((skb = brcmf_fws_deq(fws, fifo)) != NULL) {
2203 hslot = brcmf_skb_htod_tag_get_field(skb,
2205 brcmf_fws_hanger_poppkt(&fws->hanger, hslot,
2207 ifidx = brcmf_skb_if_flags_get_field(skb,
2209 /* Use proto layer to send data frame */
2210 brcmf_fws_unlock(fws);
2211 ret = brcmf_proto_txdata(drvr, ifidx, 0, skb);
2212 brcmf_fws_lock(fws);
2214 brcmf_txfinalize(brcmf_get_ifp(drvr,
2217 if (fws->bus_flow_blocked)
2222 while ((fws->fifo_credit[fifo]) || ((!fws->bcmc_credit_check) &&
2223 (fifo == BRCMF_FWS_FIFO_BCMC))) {
2224 skb = brcmf_fws_deq(fws, fifo);
2227 fws->fifo_credit[fifo]--;
2228 if (brcmf_fws_commit_skb(fws, fifo, skb))
2230 if (fws->bus_flow_blocked)
2233 if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
2234 (fws->fifo_credit[fifo] == 0) &&
2235 (!fws->bus_flow_blocked)) {
2236 while (brcmf_fws_borrow_credit(fws) == 0) {
2237 skb = brcmf_fws_deq(fws, fifo);
2239 brcmf_fws_return_credits(fws, fifo, 1);
2242 if (brcmf_fws_commit_skb(fws, fifo, skb))
2244 if (fws->bus_flow_blocked)
2249 brcmf_fws_unlock(fws);
2253 static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
2255 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
2256 struct brcmf_fws_stats *fwstats = &bus_if->drvr->fws->stats;
2259 "header_pulls: %u\n"
2260 "header_only_pkt: %u\n"
2261 "tlv_parse_failed: %u\n"
2262 "tlv_invalid_type: %u\n"
2263 "mac_update_fails: %u\n"
2264 "ps_update_fails: %u\n"
2265 "if_update_fails: %u\n"
2267 "generic_error: %u\n"
2268 "rollback_success: %u\n"
2269 "rollback_failed: %u\n"
2272 "txs_indicate: %u\n"
2274 "txs_suppr_core: %u\n"
2275 "txs_suppr_ps: %u\n"
2277 "txs_host_tossed: %u\n"
2278 "bus_flow_block: %u\n"
2279 "fws_flow_block: %u\n"
2280 "send_pkts: BK:%u BE:%u VO:%u VI:%u BCMC:%u\n"
2281 "requested_sent: BK:%u BE:%u VO:%u VI:%u BCMC:%u\n",
2282 fwstats->header_pulls,
2283 fwstats->header_only_pkt,
2284 fwstats->tlv_parse_failed,
2285 fwstats->tlv_invalid_type,
2286 fwstats->mac_update_failed,
2287 fwstats->mac_ps_update_failed,
2288 fwstats->if_update_failed,
2290 fwstats->generic_error,
2291 fwstats->rollback_success,
2292 fwstats->rollback_failed,
2293 fwstats->delayq_full_error,
2294 fwstats->supprq_full_error,
2295 fwstats->txs_indicate,
2296 fwstats->txs_discard,
2297 fwstats->txs_supp_core,
2298 fwstats->txs_supp_ps,
2299 fwstats->txs_tossed,
2300 fwstats->txs_host_tossed,
2301 fwstats->bus_flow_block,
2302 fwstats->fws_flow_block,
2303 fwstats->send_pkts[0], fwstats->send_pkts[1],
2304 fwstats->send_pkts[2], fwstats->send_pkts[3],
2305 fwstats->send_pkts[4],
2306 fwstats->requested_sent[0],
2307 fwstats->requested_sent[1],
2308 fwstats->requested_sent[2],
2309 fwstats->requested_sent[3],
2310 fwstats->requested_sent[4]);
2315 static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
2321 int brcmf_fws_init(struct brcmf_pub *drvr)
2323 struct brcmf_fws_info *fws;
2324 struct brcmf_if *ifp;
2325 u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
2329 drvr->fws = kzalloc(sizeof(*(drvr->fws)), GFP_KERNEL);
2337 spin_lock_init(&fws->spinlock);
2339 /* set linkage back */
2341 fws->fcmode = drvr->settings->fcmode;
2343 if ((drvr->bus_if->always_use_fws_queue == false) &&
2344 (fws->fcmode == BRCMF_FWS_FCMODE_NONE)) {
2345 fws->avoid_queueing = true;
2346 brcmf_dbg(INFO, "FWS queueing will be avoided\n");
2350 fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
2351 if (fws->fws_wq == NULL) {
2352 brcmf_err("workqueue creation failed\n");
2356 INIT_WORK(&fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
2358 /* enable firmware signalling if fcmode active */
2359 if (fws->fcmode != BRCMF_FWS_FCMODE_NONE)
2360 tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
2361 BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
2362 BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE |
2363 BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE;
2365 rc = brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
2366 brcmf_fws_notify_credit_map);
2368 brcmf_err("register credit map handler failed\n");
2371 rc = brcmf_fweh_register(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT,
2372 brcmf_fws_notify_bcmc_credit_support);
2374 brcmf_err("register bcmc credit handler failed\n");
2375 brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
2379 /* Setting the iovar may fail if feature is unsupported
2380 * so leave the rc as is so driver initialization can
2381 * continue. Set mode back to none indicating not enabled.
2383 fws->fw_signals = true;
2384 ifp = brcmf_get_ifp(drvr, 0);
2385 if (brcmf_fil_iovar_int_set(ifp, "tlv", tlv)) {
2386 brcmf_err("failed to set bdcv2 tlv signaling\n");
2387 fws->fcmode = BRCMF_FWS_FCMODE_NONE;
2388 fws->fw_signals = false;
2391 if (brcmf_fil_iovar_int_set(ifp, "ampdu_hostreorder", 1))
2392 brcmf_dbg(INFO, "enabling AMPDU host-reorder failed\n");
2394 /* Enable seq number reuse, if supported */
2395 if (brcmf_fil_iovar_int_get(ifp, "wlfc_mode", &mode) == 0) {
2396 if (BRCMF_FWS_MODE_GET_REUSESEQ(mode)) {
2398 BRCMF_FWS_MODE_SET_REUSESEQ(mode, 1);
2399 if (brcmf_fil_iovar_int_set(ifp,
2400 "wlfc_mode", mode) == 0) {
2401 BRCMF_FWS_MODE_SET_REUSESEQ(fws->mode, 1);
2406 brcmf_fws_hanger_init(&fws->hanger);
2407 brcmf_fws_macdesc_init(&fws->desc.other, NULL, 0);
2408 brcmf_fws_macdesc_set_name(fws, &fws->desc.other);
2409 brcmu_pktq_init(&fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
2412 /* create debugfs file for statistics */
2413 brcmf_debugfs_add_entry(drvr, "fws_stats",
2414 brcmf_debugfs_fws_stats_read);
2416 brcmf_dbg(INFO, "%s bdcv2 tlv signaling [%x]\n",
2417 fws->fw_signals ? "enabled" : "disabled", tlv);
2421 brcmf_fws_deinit(drvr);
2425 void brcmf_fws_deinit(struct brcmf_pub *drvr)
2427 struct brcmf_fws_info *fws = drvr->fws;
2432 if (drvr->fws->fws_wq)
2433 destroy_workqueue(drvr->fws->fws_wq);
2436 brcmf_fws_lock(fws);
2437 brcmf_fws_cleanup(fws, -1);
2439 brcmf_fws_unlock(fws);
2441 /* free top structure */
2445 bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
2447 if (!fws->creditmap_received)
2450 return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
2453 void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
2457 if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_TIM) {
2458 brcmu_pkt_buf_free_skb(skb);
2461 brcmf_fws_lock(fws);
2462 hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
2463 brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED, hslot, 0, 0);
2464 brcmf_fws_unlock(fws);
2467 void brcmf_fws_bus_blocked(struct brcmf_pub *drvr, bool flow_blocked)
2469 struct brcmf_fws_info *fws = drvr->fws;
2470 struct brcmf_if *ifp;
2473 if (fws->avoid_queueing) {
2474 for (i = 0; i < BRCMF_MAX_IFS; i++) {
2475 ifp = drvr->iflist[i];
2476 if (!ifp || !ifp->ndev)
2478 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FLOW,
2482 fws->bus_flow_blocked = flow_blocked;
2484 brcmf_fws_schedule_deq(fws);
2486 fws->stats.bus_flow_block++;