linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fwsignal.c
<<
>>
Prefs
   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 "core.h"
  30#include "debug.h"
  31#include "bus.h"
  32#include "fwil.h"
  33#include "fwil_types.h"
  34#include "fweh.h"
  35#include "fwsignal.h"
  36#include "p2p.h"
  37#include "cfg80211.h"
  38#include "proto.h"
  39#include "common.h"
  40
  41/**
  42 * DOC: Firmware Signalling
  43 *
  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.
  47 */
  48
  49/*
  50 * single definition for firmware-driver flow control tlv's.
  51 *
  52 * each tlv is specified by BRCMF_FWS_TLV_DEF(name, ID, length).
  53 * A length value 0 indicates variable length tlv.
  54 */
  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)
  73
  74/*
  75 * enum brcmf_fws_tlv_type - definition of tlv identifiers.
  76 */
  77#define BRCMF_FWS_TLV_DEF(name, id, len) \
  78        BRCMF_FWS_TYPE_ ## name =  id,
  79enum brcmf_fws_tlv_type {
  80        BRCMF_FWS_TLV_DEFLIST
  81        BRCMF_FWS_TYPE_INVALID
  82};
  83#undef BRCMF_FWS_TLV_DEF
  84
  85/*
  86 * enum brcmf_fws_tlv_len - definition of tlv lengths.
  87 */
  88#define BRCMF_FWS_TLV_DEF(name, id, len) \
  89        BRCMF_FWS_TYPE_ ## name ## _LEN = (len),
  90enum brcmf_fws_tlv_len {
  91        BRCMF_FWS_TLV_DEFLIST
  92};
  93#undef BRCMF_FWS_TLV_DEF
  94
  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
 101
 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
 107
 108#ifdef DEBUG
 109/*
 110 * brcmf_fws_tlv_names - array of tlv names.
 111 */
 112#define BRCMF_FWS_TLV_DEF(name, id, len) \
 113        { id, #name },
 114static struct {
 115        enum brcmf_fws_tlv_type id;
 116        const char *name;
 117} brcmf_fws_tlv_names[] = {
 118        BRCMF_FWS_TLV_DEFLIST
 119};
 120#undef BRCMF_FWS_TLV_DEF
 121
 122
 123static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
 124{
 125        int i;
 126
 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;
 130
 131        return "INVALID";
 132}
 133#else
 134static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
 135{
 136        return "NODEBUG";
 137}
 138#endif /* DEBUG */
 139
 140/*
 141 * The PKTTAG tlv has additional bytes when firmware-signalling
 142 * mode has REUSESEQ flag set.
 143 */
 144#define BRCMF_FWS_TYPE_SEQ_LEN                          2
 145
 146/*
 147 * flags used to enable tlv signalling from firmware.
 148 */
 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
 156
 157#define BRCMF_FWS_MAC_DESC_TABLE_SIZE                   32
 158#define BRCMF_FWS_MAC_DESC_ID_INVALID                   0xff
 159
 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
 164
 165#define BRCMF_FWS_PSQ_PREC_COUNT                ((BRCMF_FWS_FIFO_COUNT + 1) * 2)
 166#define BRCMF_FWS_PSQ_LEN                               256
 167
 168#define BRCMF_FWS_HTOD_FLAG_PKTFROMHOST                 0x01
 169#define BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED               0x02
 170
 171#define BRCMF_FWS_RET_OK_NOSCHEDULE                     0
 172#define BRCMF_FWS_RET_OK_SCHEDULE                       1
 173
 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)
 180
 181/**
 182 * enum brcmf_fws_skb_state - indicates processing state of skb.
 183 *
 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.
 188 */
 189enum brcmf_fws_skb_state {
 190        BRCMF_FWS_SKBSTATE_NEW,
 191        BRCMF_FWS_SKBSTATE_DELAYED,
 192        BRCMF_FWS_SKBSTATE_SUPPRESSED,
 193        BRCMF_FWS_SKBSTATE_TIM
 194};
 195
 196/**
 197 * struct brcmf_skbuff_cb - control buffer associated with skbuff.
 198 *
 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.
 205 *
 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.
 208 */
 209struct brcmf_skbuff_cb {
 210        u16 bus_flags;
 211        u16 if_flags;
 212        u32 htod;
 213        u16 htod_seq;
 214        enum brcmf_fws_skb_state state;
 215        struct brcmf_fws_mac_descriptor *mac;
 216};
 217
 218/*
 219 * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
 220 */
 221#define brcmf_skbcb(skb)        ((struct brcmf_skbuff_cb *)((skb)->cb))
 222
 223/*
 224 * sk_buff control if flags
 225 *
 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.
 232 */
 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
 245
 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)
 254
 255/*
 256 * sk_buff control packet identifier
 257 *
 258 * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
 259 *
 260 * - Generated at the host (e.g. dhd)
 261 * - Seen as a generic sequence number by firmware except for the flags field.
 262 *
 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
 269 */
 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
 280
 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)
 289
 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
 296
 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)
 305
 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
 316
 317#define brcmf_txstatus_get_field(txs, field) \
 318        brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
 319                        BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)
 320
 321/* How long to defer borrowing in jiffies */
 322#define BRCMF_FWS_BORROW_DEFER_PERIOD           (HZ / 10)
 323
 324/**
 325 * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
 326 *
 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.
 335 */
 336enum 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,
 342        BRCMF_FWS_FIFO_BCMC,
 343        BRCMF_FWS_FIFO_ATIM,
 344        BRCMF_FWS_FIFO_COUNT
 345};
 346
 347/**
 348 * enum brcmf_fws_txstatus - txstatus flag values.
 349 *
 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.
 360 */
 361enum 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
 367};
 368
 369enum brcmf_fws_fcmode {
 370        BRCMF_FWS_FCMODE_NONE,
 371        BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
 372        BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
 373};
 374
 375enum brcmf_fws_mac_desc_state {
 376        BRCMF_FWS_STATE_OPEN = 1,
 377        BRCMF_FWS_STATE_CLOSE
 378};
 379
 380/**
 381 * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
 382 *
 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.
 395 */
 396struct brcmf_fws_mac_descriptor {
 397        char name[16];
 398        u8 occupied;
 399        u8 mac_handle;
 400        u8 interface_id;
 401        u8 state;
 402        bool suppressed;
 403        u8 generation;
 404        u8 ac_bitmap;
 405        u8 requested_credit;
 406        u8 requested_packet;
 407        u8 ea[ETH_ALEN];
 408        u8 seq[BRCMF_FWS_FIFO_COUNT];
 409        struct pktq psq;
 410        int transit_count;
 411        int suppr_transit_count;
 412        bool send_tim_signal;
 413        u8 traffic_pending_bmp;
 414        u8 traffic_lastreported_bmp;
 415};
 416
 417#define BRCMF_FWS_HANGER_MAXITEMS       1024
 418
 419/**
 420 * enum brcmf_fws_hanger_item_state - state of hanger item.
 421 *
 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.
 425 */
 426enum 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
 430};
 431
 432
 433/**
 434 * struct brcmf_fws_hanger_item - single entry for tx pending packet.
 435 *
 436 * @state: entry is either free or occupied.
 437 * @pkt: packet itself.
 438 */
 439struct brcmf_fws_hanger_item {
 440        enum brcmf_fws_hanger_item_state state;
 441        struct sk_buff *pkt;
 442};
 443
 444/**
 445 * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
 446 *
 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.
 454 */
 455struct brcmf_fws_hanger {
 456        u32 pushed;
 457        u32 popped;
 458        u32 failed_to_push;
 459        u32 failed_to_pop;
 460        u32 failed_slotfind;
 461        u32 slot_pos;
 462        struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
 463};
 464
 465struct 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;
 469};
 470
 471struct brcmf_fws_stats {
 472        u32 tlv_parse_failed;
 473        u32 tlv_invalid_type;
 474        u32 header_only_pkt;
 475        u32 header_pulls;
 476        u32 pkt2bus;
 477        u32 send_pkts[5];
 478        u32 requested_sent[5];
 479        u32 generic_error;
 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;
 486        u32 rollback_failed;
 487        u32 delayq_full_error;
 488        u32 supprq_full_error;
 489        u32 txs_indicate;
 490        u32 txs_discard;
 491        u32 txs_supp_core;
 492        u32 txs_supp_ps;
 493        u32 txs_tossed;
 494        u32 txs_host_tossed;
 495        u32 bus_flow_block;
 496        u32 fws_flow_block;
 497};
 498
 499struct brcmf_fws_info {
 500        struct brcmf_pub *drvr;
 501        spinlock_t spinlock;
 502        ulong flags;
 503        struct brcmf_fws_stats stats;
 504        struct brcmf_fws_hanger hanger;
 505        enum brcmf_fws_fcmode fcmode;
 506        bool fw_signals;
 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];
 515        u32 fifo_credit_map;
 516        u32 fifo_delay_map;
 517        unsigned long borrow_defer_timestamp;
 518        bool bus_flow_blocked;
 519        bool creditmap_received;
 520        u8 mode;
 521        bool avoid_queueing;
 522};
 523
 524/*
 525 * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
 526 */
 527static 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,
 535        BRCMF_FWS_FIFO_AC_VO
 536};
 537
 538#define BRCMF_FWS_TLV_DEF(name, id, len) \
 539        case BRCMF_FWS_TYPE_ ## name: \
 540                return len;
 541
 542/**
 543 * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
 544 *
 545 * @fws: firmware-signalling information.
 546 * @id: identifier of the TLV.
 547 *
 548 * Return: the specified length for the given TLV; Otherwise -EINVAL.
 549 */
 550static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
 551                                 enum brcmf_fws_tlv_type id)
 552{
 553        switch (id) {
 554        BRCMF_FWS_TLV_DEFLIST
 555        default:
 556                fws->stats.tlv_invalid_type++;
 557                break;
 558        }
 559        return -EINVAL;
 560}
 561#undef BRCMF_FWS_TLV_DEF
 562
 563static void brcmf_fws_lock(struct brcmf_fws_info *fws)
 564                __acquires(&fws->spinlock)
 565{
 566        spin_lock_irqsave(&fws->spinlock, fws->flags);
 567}
 568
 569static void brcmf_fws_unlock(struct brcmf_fws_info *fws)
 570                __releases(&fws->spinlock)
 571{
 572        spin_unlock_irqrestore(&fws->spinlock, fws->flags);
 573}
 574
 575static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
 576{
 577        u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
 578        return ifidx == *(int *)arg;
 579}
 580
 581static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
 582                                int ifidx)
 583{
 584        bool (*matchfn)(struct sk_buff *, void *) = NULL;
 585        struct sk_buff *skb;
 586        int prec;
 587
 588        if (ifidx != -1)
 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);
 592                while (skb) {
 593                        brcmu_pkt_buf_free_skb(skb);
 594                        skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
 595                }
 596        }
 597}
 598
 599static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
 600{
 601        int i;
 602
 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;
 606}
 607
 608static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
 609{
 610        u32 i;
 611
 612        i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;
 613
 614        while (i != h->slot_pos) {
 615                if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
 616                        h->slot_pos = i;
 617                        goto done;
 618                }
 619                i++;
 620                if (i == BRCMF_FWS_HANGER_MAXITEMS)
 621                        i = 0;
 622        }
 623        brcmf_err("all slots occupied\n");
 624        h->failed_slotfind++;
 625        i = BRCMF_FWS_HANGER_MAXITEMS;
 626done:
 627        return i;
 628}
 629
 630static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
 631                                    struct sk_buff *pkt, u32 slot_id)
 632{
 633        if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
 634                return -ENOENT;
 635
 636        if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
 637                brcmf_err("slot is not free\n");
 638                h->failed_to_push++;
 639                return -EINVAL;
 640        }
 641
 642        h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
 643        h->items[slot_id].pkt = pkt;
 644        h->pushed++;
 645        return 0;
 646}
 647
 648static inline int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
 649                                          u32 slot_id, struct sk_buff **pktout,
 650                                          bool remove_item)
 651{
 652        if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
 653                return -ENOENT;
 654
 655        if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
 656                brcmf_err("entry not in use\n");
 657                h->failed_to_pop++;
 658                return -EINVAL;
 659        }
 660
 661        *pktout = h->items[slot_id].pkt;
 662        if (remove_item) {
 663                h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
 664                h->items[slot_id].pkt = NULL;
 665                h->popped++;
 666        }
 667        return 0;
 668}
 669
 670static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
 671                                            u32 slot_id)
 672{
 673        if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
 674                return -ENOENT;
 675
 676        if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
 677                brcmf_err("entry not in use\n");
 678                return -EINVAL;
 679        }
 680
 681        h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
 682        return 0;
 683}
 684
 685static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
 686                                     bool (*fn)(struct sk_buff *, void *),
 687                                     int ifidx)
 688{
 689        struct brcmf_fws_hanger *h = &fws->hanger;
 690        struct sk_buff *skb;
 691        int i;
 692        enum brcmf_fws_hanger_item_state s;
 693
 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);
 703                                h->items[i].state =
 704                                        BRCMF_FWS_HANGER_ITEM_STATE_FREE;
 705                        }
 706                }
 707        }
 708}
 709
 710static void brcmf_fws_macdesc_set_name(struct brcmf_fws_info *fws,
 711                                       struct brcmf_fws_mac_descriptor *desc)
 712{
 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);
 718        else
 719                scnprintf(desc->name, sizeof(desc->name), "MACIF:%d",
 720                          desc->interface_id);
 721}
 722
 723static void brcmf_fws_macdesc_init(struct brcmf_fws_mac_descriptor *desc,
 724                                   u8 *addr, u8 ifidx)
 725{
 726        brcmf_dbg(TRACE,
 727                  "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
 728        desc->occupied = 1;
 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 */
 735        if (addr)
 736                memcpy(&desc->ea[0], addr, ETH_ALEN);
 737}
 738
 739static
 740void brcmf_fws_macdesc_deinit(struct brcmf_fws_mac_descriptor *desc)
 741{
 742        brcmf_dbg(TRACE,
 743                  "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
 744        desc->occupied = 0;
 745        desc->state = BRCMF_FWS_STATE_CLOSE;
 746        desc->requested_credit = 0;
 747        desc->requested_packet = 0;
 748}
 749
 750static struct brcmf_fws_mac_descriptor *
 751brcmf_fws_macdesc_lookup(struct brcmf_fws_info *fws, u8 *ea)
 752{
 753        struct brcmf_fws_mac_descriptor *entry;
 754        int i;
 755
 756        if (ea == NULL)
 757                return ERR_PTR(-EINVAL);
 758
 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))
 762                        return entry;
 763                entry++;
 764        }
 765
 766        return ERR_PTR(-ENOENT);
 767}
 768
 769static struct brcmf_fws_mac_descriptor*
 770brcmf_fws_macdesc_find(struct brcmf_fws_info *fws, struct brcmf_if *ifp, u8 *da)
 771{
 772        struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
 773        bool multicast;
 774
 775        multicast = is_multicast_ether_addr(da);
 776
 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.
 780         */
 781        if (multicast && ifp->fws_desc) {
 782                entry = ifp->fws_desc;
 783                goto done;
 784        }
 785
 786        entry = brcmf_fws_macdesc_lookup(fws, da);
 787        if (IS_ERR(entry))
 788                entry = ifp->fws_desc;
 789
 790done:
 791        return entry;
 792}
 793
 794static bool brcmf_fws_macdesc_closed(struct brcmf_fws_info *fws,
 795                                     struct brcmf_fws_mac_descriptor *entry,
 796                                     int fifo)
 797{
 798        struct brcmf_fws_mac_descriptor *if_entry;
 799        bool closed;
 800
 801        /* for unique destination entries the related interface
 802         * may be closed.
 803         */
 804        if (entry->mac_handle) {
 805                if_entry = &fws->desc.iface[entry->interface_id];
 806                if (if_entry->state == BRCMF_FWS_STATE_CLOSE)
 807                        return true;
 808        }
 809        /* an entry is closed when the state is closed and
 810         * the firmware did not request anything.
 811         */
 812        closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
 813                 !entry->requested_credit && !entry->requested_packet;
 814
 815        /* Or firmware does not allow traffic for given fifo */
 816        return closed || !(entry->ac_bitmap & BIT(fifo));
 817}
 818
 819static void brcmf_fws_macdesc_cleanup(struct brcmf_fws_info *fws,
 820                                      struct brcmf_fws_mac_descriptor *entry,
 821                                      int ifidx)
 822{
 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);
 826        }
 827}
 828
 829static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
 830                                      bool (*fn)(struct sk_buff *, void *),
 831                                      int ifidx)
 832{
 833        struct brcmf_fws_hanger_item *hi;
 834        struct pktq *txq;
 835        struct sk_buff *skb;
 836        int prec;
 837        u32 hslot;
 838
 839        txq = brcmf_bus_gettxq(fws->drvr->bus_if);
 840        if (IS_ERR(txq)) {
 841                brcmf_dbg(TRACE, "no txq to clean up\n");
 842                return;
 843        }
 844
 845        for (prec = 0; prec < txq->num_prec; prec++) {
 846                skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
 847                while (skb) {
 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);
 854                }
 855        }
 856}
 857
 858static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
 859{
 860        int i;
 861        struct brcmf_fws_mac_descriptor *table;
 862        bool (*matchfn)(struct sk_buff *, void *) = NULL;
 863
 864        if (fws == NULL)
 865                return;
 866
 867        if (ifidx != -1)
 868                matchfn = brcmf_fws_ifidx_match;
 869
 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);
 874
 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);
 878}
 879
 880static u8 brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
 881{
 882        struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
 883        u8 *wlh;
 884        u16 data_offset = 0;
 885        u8 fillers;
 886        __le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);
 887        __le16 pktseq = cpu_to_le16(brcmf_skbcb(skb)->htod_seq);
 888
 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;
 901
 902        skb_push(skb, data_offset);
 903        wlh = skb->data;
 904
 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,
 911                       sizeof(pktseq));
 912        }
 913        wlh += wlh[1] + 2;
 914
 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;
 925        }
 926        if (fillers)
 927                memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);
 928
 929        return (u8)(data_offset >> 2);
 930}
 931
 932static bool brcmf_fws_tim_update(struct brcmf_fws_info *fws,
 933                                 struct brcmf_fws_mac_descriptor *entry,
 934                                 int fifo, bool send_immediately)
 935{
 936        struct sk_buff *skb;
 937        struct brcmf_skbuff_cb *skcb;
 938        s32 err;
 939        u32 len;
 940        u8 data_offset;
 941        int ifidx;
 942
 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);
 946        else
 947                entry->traffic_pending_bmp |= NBITVAL(fifo);
 948
 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);
 961                if (skb == NULL)
 962                        return false;
 963                skb_pull(skb, len);
 964                skcb = brcmf_skbcb(skb);
 965                skcb->mac = entry;
 966                skcb->state = BRCMF_FWS_SKBSTATE_TIM;
 967                skcb->htod = 0;
 968                skcb->htod_seq = 0;
 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);
 973                brcmf_fws_lock(fws);
 974                if (err)
 975                        brcmu_pkt_buf_free_skb(skb);
 976                return true;
 977        }
 978        return false;
 979}
 980
 981static void
 982brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
 983                             u8 if_id)
 984{
 985        struct brcmf_if *ifp = brcmf_get_ifp(fws->drvr, if_id);
 986
 987        if (WARN_ON(!ifp))
 988                return;
 989
 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);
 998        }
 999        return;
1000}
1001
1002static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
1003{
1004        brcmf_dbg(CTL, "rssi %d\n", rssi);
1005        return 0;
1006}
1007
1008static
1009int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
1010{
1011        struct brcmf_fws_mac_descriptor *entry, *existing;
1012        u8 mac_handle;
1013        u8 ifidx;
1014        u8 *addr;
1015
1016        mac_handle = *data++;
1017        ifidx = *data++;
1018        addr = data;
1019
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",
1024                                  entry->name, addr);
1025                        brcmf_fws_lock(fws);
1026                        brcmf_fws_macdesc_cleanup(fws, entry, -1);
1027                        brcmf_fws_macdesc_deinit(entry);
1028                        brcmf_fws_unlock(fws);
1029                } else
1030                        fws->stats.mac_update_failed++;
1031                return 0;
1032        }
1033
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,
1042                                        BRCMF_FWS_PSQ_LEN);
1043                        brcmf_fws_unlock(fws);
1044                        brcmf_dbg(TRACE, "add %s mac %pM\n", entry->name, addr);
1045                } else {
1046                        fws->stats.mac_update_failed++;
1047                }
1048        } else {
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,
1059                                  addr);
1060                } else {
1061                        brcmf_dbg(TRACE, "use existing\n");
1062                        WARN_ON(entry->mac_handle != mac_handle);
1063                        /* TODO: what should we do here: continue, reinit, .. */
1064                }
1065        }
1066        return 0;
1067}
1068
1069static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
1070                                            u8 type, u8 *data)
1071{
1072        struct brcmf_fws_mac_descriptor *entry;
1073        u8 mac_handle;
1074        int ret;
1075
1076        mac_handle = data[0];
1077        entry = &fws->desc.nodes[mac_handle & 0x1F];
1078        if (!entry->occupied) {
1079                fws->stats.mac_ps_update_failed++;
1080                return -ESRCH;
1081        }
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;
1089        } else {
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;
1096        }
1097        brcmf_fws_unlock(fws);
1098        return ret;
1099}
1100
1101static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
1102                                              u8 type, u8 *data)
1103{
1104        struct brcmf_fws_mac_descriptor *entry;
1105        u8 ifidx;
1106        int ret;
1107
1108        ifidx = data[0];
1109
1110        if (ifidx >= BRCMF_MAX_IFS) {
1111                ret = -ERANGE;
1112                goto fail;
1113        }
1114
1115        entry = &fws->desc.iface[ifidx];
1116        if (!entry->occupied) {
1117                ret = -ESRCH;
1118                goto fail;
1119        }
1120
1121        brcmf_dbg(TRACE, "%s (%d): %s\n", brcmf_fws_get_tlv_name(type), type,
1122                  entry->name);
1123        brcmf_fws_lock(fws);
1124        switch (type) {
1125        case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1126                entry->state = BRCMF_FWS_STATE_OPEN;
1127                ret = BRCMF_FWS_RET_OK_SCHEDULE;
1128                break;
1129        case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1130                entry->state = BRCMF_FWS_STATE_CLOSE;
1131                ret = BRCMF_FWS_RET_OK_NOSCHEDULE;
1132                break;
1133        default:
1134                ret = -EINVAL;
1135                brcmf_fws_unlock(fws);
1136                goto fail;
1137        }
1138        brcmf_fws_unlock(fws);
1139        return ret;
1140
1141fail:
1142        fws->stats.if_update_failed++;
1143        return ret;
1144}
1145
1146static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
1147                                      u8 *data)
1148{
1149        struct brcmf_fws_mac_descriptor *entry;
1150
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++;
1155                else
1156                        fws->stats.packet_request_failed++;
1157                return -ESRCH;
1158        }
1159
1160        brcmf_dbg(TRACE, "%s (%d): %s cnt %d bmp %d\n",
1161                  brcmf_fws_get_tlv_name(type), type, entry->name,
1162                  data[0], data[2]);
1163        brcmf_fws_lock(fws);
1164        if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
1165                entry->requested_credit = data[0];
1166        else
1167                entry->requested_packet = data[0];
1168
1169        entry->ac_bitmap = data[2];
1170        brcmf_fws_unlock(fws);
1171        return BRCMF_FWS_RET_OK_SCHEDULE;
1172}
1173
1174static void
1175brcmf_fws_macdesc_use_req_credit(struct brcmf_fws_mac_descriptor *entry,
1176                                 struct sk_buff *skb)
1177{
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");
1190        } else {
1191                brcmf_skb_if_flags_set_field(skb, REQUESTED, 0);
1192                brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1193        }
1194}
1195
1196static void brcmf_fws_macdesc_return_req_credit(struct sk_buff *skb)
1197{
1198        struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1199
1200        if ((brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) &&
1201            (entry->state == BRCMF_FWS_STATE_CLOSE))
1202                entry->requested_credit++;
1203}
1204
1205static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
1206                                     u8 fifo, u8 credits)
1207{
1208        int lender_ac;
1209        int *borrowed;
1210        int *fifo_credit;
1211
1212        if (!credits)
1213                return;
1214
1215        fws->fifo_credit_map |= 1 << fifo;
1216
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;
1220                     lender_ac--) {
1221                        borrowed = &fws->credits_borrowed[lender_ac];
1222                        if (*borrowed) {
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;
1228                                        return;
1229                                } else {
1230                                        credits -= *borrowed;
1231                                        *fifo_credit += *borrowed;
1232                                        *borrowed = 0;
1233                                }
1234                        }
1235                }
1236        }
1237
1238        fws->fifo_credit[fifo] += credits;
1239}
1240
1241static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
1242{
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);
1247}
1248
1249static int brcmf_fws_enq(struct brcmf_fws_info *fws,
1250                         enum brcmf_fws_skb_state state, int fifo,
1251                         struct sk_buff *p)
1252{
1253        int prec = 2 * fifo;
1254        u32 *qfull_stat = &fws->stats.delayq_full_error;
1255        struct brcmf_fws_mac_descriptor *entry;
1256        struct pktq *pq;
1257        struct sk_buff_head *queue;
1258        struct sk_buff *p_head;
1259        struct sk_buff *p_tail;
1260        u32 fr_new;
1261        u32 fr_compare;
1262
1263        entry = brcmf_skbcb(p)->mac;
1264        if (entry == NULL) {
1265                brcmf_err("no mac descriptor found for skb %p\n", p);
1266                return -ENOENT;
1267        }
1268
1269        brcmf_dbg(DATA, "enter: fifo %d skb %p\n", fifo, p);
1270        if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
1271                prec += 1;
1272                qfull_stat = &fws->stats.supprq_full_error;
1273
1274                /* Fix out of order delivery of frames. Dont assume frame    */
1275                /* can be inserted at the end, but look for correct position */
1276                pq = &entry->psq;
1277                if (pktq_full(pq) || pktq_pfull(pq, prec)) {
1278                        *qfull_stat += 1;
1279                        return -ENFILE;
1280                }
1281                queue = &pq->q[prec].skblist;
1282
1283                p_head = skb_peek(queue);
1284                p_tail = skb_peek_tail(queue);
1285                fr_new = brcmf_skb_htod_tag_get_field(p, FREERUN);
1286
1287                while (p_head != p_tail) {
1288                        fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
1289                                                                  FREERUN);
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)))
1295                                break;
1296                        p_tail = skb_queue_prev(queue, p_tail);
1297                }
1298                /* Position found. Determine what to do */
1299                if (p_tail == NULL) {
1300                        /* empty list */
1301                        __skb_queue_tail(queue, p);
1302                } else {
1303                        fr_compare = brcmf_skb_htod_tag_get_field(p_tail,
1304                                                                  FREERUN);
1305                        if (((fr_new > fr_compare) &&
1306                             ((fr_new - fr_compare) < 128)) ||
1307                            ((fr_new < fr_compare) &&
1308                             ((fr_compare - fr_new) > 128))) {
1309                                /* After tail */
1310                                __skb_queue_after(queue, p_tail, p);
1311                        } else {
1312                                /* Before tail */
1313                                __skb_insert(p, p_tail->prev, p_tail, queue);
1314                        }
1315                }
1316
1317                /* Complete the counters and statistics */
1318                pq->len++;
1319                if (pq->hi_prec < prec)
1320                        pq->hi_prec = (u8) prec;
1321        } else if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
1322                *qfull_stat += 1;
1323                return -ENFILE;
1324        }
1325
1326        /* increment total enqueued packet count */
1327        fws->fifo_delay_map |= 1 << fifo;
1328        fws->fifo_enqpkt[fifo]++;
1329
1330        /* update the sk_buff state */
1331        brcmf_skbcb(p)->state = state;
1332
1333        /*
1334         * A packet has been pushed so update traffic
1335         * availability bitmap, if applicable
1336         */
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));
1340        return 0;
1341}
1342
1343static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
1344{
1345        struct brcmf_fws_mac_descriptor *table;
1346        struct brcmf_fws_mac_descriptor *entry;
1347        struct sk_buff *p;
1348        int num_nodes;
1349        int node_pos;
1350        int prec_out;
1351        int pmsk;
1352        int i;
1353
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];
1357
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))
1362                        continue;
1363
1364                if (entry->suppressed)
1365                        pmsk = 2;
1366                else
1367                        pmsk = 3;
1368                p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
1369                if (p == NULL) {
1370                        if (entry->suppressed) {
1371                                if (entry->suppr_transit_count)
1372                                        continue;
1373                                entry->suppressed = false;
1374                                p = brcmu_pktq_mdeq(&entry->psq,
1375                                                    1 << (fifo * 2), &prec_out);
1376                        }
1377                }
1378                if  (p == NULL)
1379                        continue;
1380
1381                brcmf_fws_macdesc_use_req_credit(entry, p);
1382
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,
1387                                                                          INDEX)
1388                                             );
1389                /*
1390                 * A packet has been picked up, update traffic
1391                 * availability bitmap, if applicable
1392                 */
1393                brcmf_fws_tim_update(fws, entry, fifo, false);
1394
1395                /*
1396                 * decrement total enqueued fifo packets and
1397                 * clear delay bitmap if done.
1398                 */
1399                fws->fifo_enqpkt[fifo]--;
1400                if (fws->fifo_enqpkt[fifo] == 0)
1401                        fws->fifo_delay_map &= ~(1 << fifo);
1402                goto done;
1403        }
1404        p = NULL;
1405done:
1406        brcmf_dbg(DATA, "exit: fifo %d skb %p\n", fifo, p);
1407        return p;
1408}
1409
1410static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
1411                                         struct sk_buff *skb,
1412                                         u32 genbit, u16 seq)
1413{
1414        struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
1415        u32 hslot;
1416        int ret;
1417
1418        hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1419
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);
1426        }
1427
1428        entry->generation = genbit;
1429
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);
1435        } else {
1436                brcmf_skb_htod_seq_set_field(skb, FROMDRV, 0);
1437        }
1438        ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo, skb);
1439
1440        if (ret != 0) {
1441                /* suppress q is full drop this packet */
1442                brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb, true);
1443        } else {
1444                /* Mark suppressed to avoid a double free during wlfc cleanup */
1445                brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot);
1446        }
1447
1448        return ret;
1449}
1450
1451static int
1452brcmf_fws_txs_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
1453                      u32 genbit, u16 seq)
1454{
1455        u32 fifo;
1456        int ret;
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;
1462
1463        brcmf_dbg(DATA, "flags %d\n", flags);
1464
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++;
1477        else
1478                brcmf_err("unexpected txstatus\n");
1479
1480        ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1481                                      remove_from_hanger);
1482        if (ret != 0) {
1483                brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
1484                return ret;
1485        }
1486
1487        skcb = brcmf_skbcb(skb);
1488        entry = skcb->mac;
1489        if (WARN_ON(!entry)) {
1490                brcmu_pkt_buf_free_skb(skb);
1491                return -EINVAL;
1492        }
1493        entry->transit_count--;
1494        if (entry->suppressed && entry->suppr_transit_count)
1495                entry->suppr_transit_count--;
1496
1497        brcmf_dbg(DATA, "%s flags %d htod %X seq %X\n", entry->name, flags,
1498                  skcb->htod, seq);
1499
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);
1507        }
1508        brcmf_fws_macdesc_return_req_credit(skb);
1509
1510        ret = brcmf_proto_hdrpull(fws->drvr, false, skb, &ifp);
1511        if (ret) {
1512                brcmu_pkt_buf_free_skb(skb);
1513                return -EINVAL;
1514        }
1515        if (!remove_from_hanger)
1516                ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb,
1517                                                    genbit, seq);
1518        if (remove_from_hanger || ret)
1519                brcmf_txfinalize(ifp, skb, true);
1520
1521        return 0;
1522}
1523
1524static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
1525                                             u8 *data)
1526{
1527        int i;
1528
1529        if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
1530                brcmf_dbg(INFO, "ignored\n");
1531                return BRCMF_FWS_RET_OK_NOSCHEDULE;
1532        }
1533
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]);
1538
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;
1543}
1544
1545static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
1546{
1547        __le32 status_le;
1548        __le16 seq_le;
1549        u32 status;
1550        u32 hslot;
1551        u32 genbit;
1552        u8 flags;
1553        u16 seq;
1554
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],
1563                       sizeof(seq_le));
1564                seq = le16_to_cpu(seq_le);
1565        } else {
1566                seq = 0;
1567        }
1568
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;
1573}
1574
1575static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
1576{
1577        __le32 timestamp;
1578
1579        memcpy(&timestamp, &data[2], sizeof(timestamp));
1580        brcmf_dbg(CTL, "received: seq %d, timestamp %d\n", data[1],
1581                  le32_to_cpu(timestamp));
1582        return 0;
1583}
1584
1585static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
1586                                       const struct brcmf_event_msg *e,
1587                                       void *data)
1588{
1589        struct brcmf_fws_info *fws = ifp->drvr->fws;
1590        int i;
1591        u8 *credits = data;
1592
1593        if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
1594                brcmf_err("event payload too small (%d)\n", e->datalen);
1595                return -EINVAL;
1596        }
1597        if (fws->creditmap_received)
1598                return 0;
1599
1600        fws->creditmap_received = true;
1601
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++) {
1605                if (*credits)
1606                        fws->fifo_credit_map |= 1 << i;
1607                else
1608                        fws->fifo_credit_map &= ~(1 << i);
1609                fws->fifo_credit[i] = *credits++;
1610        }
1611        brcmf_fws_schedule_deq(fws);
1612        brcmf_fws_unlock(fws);
1613        return 0;
1614}
1615
1616static int brcmf_fws_notify_bcmc_credit_support(struct brcmf_if *ifp,
1617                                                const struct brcmf_event_msg *e,
1618                                                void *data)
1619{
1620        struct brcmf_fws_info *fws = ifp->drvr->fws;
1621
1622        if (fws) {
1623                brcmf_fws_lock(fws);
1624                fws->bcmc_credit_check = true;
1625                brcmf_fws_unlock(fws);
1626        }
1627        return 0;
1628}
1629
1630static void brcmf_rxreorder_get_skb_list(struct brcmf_ampdu_rx_reorder *rfi,
1631                                         u8 start, u8 end,
1632                                         struct sk_buff_head *skb_list)
1633{
1634        /* initialize return list */
1635        __skb_queue_head_init(skb_list);
1636
1637        if (rfi->pend_pkts == 0) {
1638                brcmf_dbg(INFO, "no packets in reorder queue\n");
1639                return;
1640        }
1641
1642        do {
1643                if (rfi->pktslots[start]) {
1644                        __skb_queue_tail(skb_list, rfi->pktslots[start]);
1645                        rfi->pktslots[start] = NULL;
1646                }
1647                start++;
1648                if (start > rfi->max_idx)
1649                        start = 0;
1650        } while (start != end);
1651        rfi->pend_pkts -= skb_queue_len(skb_list);
1652}
1653
1654void brcmf_fws_rxreorder(struct brcmf_if *ifp, struct sk_buff *pkt)
1655{
1656        u8 *reorder_data;
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;
1661        u8 flags;
1662        u32 buf_size;
1663
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];
1667
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);
1672                return;
1673        }
1674
1675        rfi = ifp->drvr->reorder_flows[flow_id];
1676        if (flags & BRCMF_RXREORDER_DEL_FLOW) {
1677                brcmf_dbg(INFO, "flow-%d: delete\n",
1678                          flow_id);
1679
1680                if (rfi == NULL) {
1681                        brcmf_dbg(INFO, "received flags to cleanup, but no flow (%d) yet\n",
1682                                  flow_id);
1683                        brcmf_netif_rx(ifp, pkt);
1684                        return;
1685                }
1686
1687                brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, rfi->exp_idx,
1688                                             &reorder_list);
1689                /* add the last packet */
1690                __skb_queue_tail(&reorder_list, pkt);
1691                kfree(rfi);
1692                ifp->drvr->reorder_flows[flow_id] = NULL;
1693                goto netif_rx;
1694        }
1695        /* from here on we need a flow reorder instance */
1696        if (rfi == NULL) {
1697                buf_size = sizeof(*rfi);
1698                max_idx = reorder_data[BRCMF_RXREORDER_MAXIDX_OFFSET];
1699
1700                buf_size += (max_idx + 1) * sizeof(pkt);
1701
1702                /* allocate space for flow reorder info */
1703                brcmf_dbg(INFO, "flow-%d: start, maxidx %d\n",
1704                          flow_id, max_idx);
1705                rfi = kzalloc(buf_size, GFP_ATOMIC);
1706                if (rfi == NULL) {
1707                        brcmf_err("failed to alloc buffer\n");
1708                        brcmf_netif_rx(ifp, pkt);
1709                        return;
1710                }
1711
1712                ifp->drvr->reorder_flows[flow_id] = rfi;
1713                rfi->pktslots = (struct sk_buff **)(rfi + 1);
1714                rfi->max_idx = max_idx;
1715        }
1716        if (flags & BRCMF_RXREORDER_NEW_HOLE)  {
1717                if (rfi->pend_pkts) {
1718                        brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx,
1719                                                     rfi->exp_idx,
1720                                                     &reorder_list);
1721                        WARN_ON(rfi->pend_pkts);
1722                } else {
1723                        __skb_queue_head_init(&reorder_list);
1724                }
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;
1729                rfi->pend_pkts++;
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];
1735
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;
1743                        }
1744                        rfi->pktslots[cur_idx] = pkt;
1745                        rfi->pend_pkts++;
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);
1749
1750                        /* can return now as there is no reorder
1751                         * list to process.
1752                         */
1753                        return;
1754                }
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;
1760                        }
1761                        rfi->pktslots[cur_idx] = pkt;
1762                        rfi->pend_pkts++;
1763
1764                        /* got the expected one. flush from current to expected
1765                         * and update expected
1766                         */
1767                        brcmf_dbg(DATA, "flow-%d: expected %d (%d), pending %d\n",
1768                                  flow_id, cur_idx, exp_idx, rfi->pend_pkts);
1769
1770                        rfi->cur_idx = cur_idx;
1771                        rfi->exp_idx = exp_idx;
1772
1773                        brcmf_rxreorder_get_skb_list(rfi, cur_idx, exp_idx,
1774                                                     &reorder_list);
1775                        brcmf_dbg(DATA, "flow-%d: freeing buffers %d, pending %d\n",
1776                                  flow_id, skb_queue_len(&reorder_list),
1777                                  rfi->pend_pkts);
1778                } else {
1779                        u8 end_idx;
1780
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,
1783                                  cur_idx, exp_idx);
1784                        if (flags & BRCMF_RXREORDER_FLUSH_ALL)
1785                                end_idx = rfi->exp_idx;
1786                        else
1787                                end_idx = exp_idx;
1788
1789                        /* flush pkts first */
1790                        brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, end_idx,
1791                                                     &reorder_list);
1792
1793                        if (exp_idx == ((cur_idx + 1) % (rfi->max_idx + 1))) {
1794                                __skb_queue_tail(&reorder_list, pkt);
1795                        } else {
1796                                rfi->pktslots[cur_idx] = pkt;
1797                                rfi->pend_pkts++;
1798                        }
1799                        rfi->exp_idx = exp_idx;
1800                        rfi->cur_idx = cur_idx;
1801                }
1802        } else {
1803                /* explicity window move updating the expected index */
1804                exp_idx = reorder_data[BRCMF_RXREORDER_EXPIDX_OFFSET];
1805
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;
1810                else
1811                        end_idx =  exp_idx;
1812
1813                brcmf_rxreorder_get_skb_list(rfi, rfi->exp_idx, end_idx,
1814                                             &reorder_list);
1815                __skb_queue_tail(&reorder_list, pkt);
1816                /* set the new expected idx */
1817                rfi->exp_idx = exp_idx;
1818        }
1819netif_rx:
1820        skb_queue_walk_safe(&reorder_list, pkt, pnext) {
1821                __skb_unlink(pkt, &reorder_list);
1822                brcmf_netif_rx(ifp, pkt);
1823        }
1824}
1825
1826void brcmf_fws_hdrpull(struct brcmf_if *ifp, s16 siglen, struct sk_buff *skb)
1827{
1828        struct brcmf_skb_reorder_data *rd;
1829        struct brcmf_fws_info *fws = ifp->drvr->fws;
1830        u8 *signal_data;
1831        s16 data_len;
1832        u8 type;
1833        u8 len;
1834        u8 *data;
1835        s32 status;
1836        s32 err;
1837
1838        brcmf_dbg(HDRS, "enter: ifidx %d, skblen %u, sig %d\n",
1839                  ifp->ifidx, skb->len, siglen);
1840
1841        WARN_ON(siglen > skb->len);
1842
1843        if (!siglen)
1844                return;
1845        /* if flow control disabled, skip to packet data and leave */
1846        if ((!fws) || (!fws->fw_signals)) {
1847                skb_pull(skb, siglen);
1848                return;
1849        }
1850
1851        fws->stats.header_pulls++;
1852        data_len = siglen;
1853        signal_data = skb->data;
1854
1855        status = BRCMF_FWS_RET_OK_NOSCHEDULE;
1856        while (data_len > 0) {
1857                /* extract tlv info */
1858                type = signal_data[0];
1859
1860                /* FILLER type is actually not a TLV, but
1861                 * a single byte that can be skipped.
1862                 */
1863                if (type == BRCMF_FWS_TYPE_FILLER) {
1864                        signal_data += 1;
1865                        data_len -= 1;
1866                        continue;
1867                }
1868                len = signal_data[1];
1869                data = signal_data + 2;
1870
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));
1874
1875                /* abort parsing when length invalid */
1876                if (data_len < len + 2)
1877                        break;
1878
1879                if (len < brcmf_fws_get_tlv_len(fws, type))
1880                        break;
1881
1882                err = BRCMF_FWS_RET_OK_NOSCHEDULE;
1883                switch (type) {
1884                case BRCMF_FWS_TYPE_COMP_TXSTATUS:
1885                        break;
1886                case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
1887                        rd = (struct brcmf_skb_reorder_data *)skb->cb;
1888                        rd->reorder = data;
1889                        break;
1890                case BRCMF_FWS_TYPE_MACDESC_ADD:
1891                case BRCMF_FWS_TYPE_MACDESC_DEL:
1892                        brcmf_fws_macdesc_indicate(fws, type, data);
1893                        break;
1894                case BRCMF_FWS_TYPE_MAC_OPEN:
1895                case BRCMF_FWS_TYPE_MAC_CLOSE:
1896                        err = brcmf_fws_macdesc_state_indicate(fws, type, data);
1897                        break;
1898                case BRCMF_FWS_TYPE_INTERFACE_OPEN:
1899                case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1900                        err = brcmf_fws_interface_state_indicate(fws, type,
1901                                                                 data);
1902                        break;
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);
1906                        break;
1907                case BRCMF_FWS_TYPE_TXSTATUS:
1908                        brcmf_fws_txstatus_indicate(fws, data);
1909                        break;
1910                case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
1911                        err = brcmf_fws_fifocreditback_indicate(fws, data);
1912                        break;
1913                case BRCMF_FWS_TYPE_RSSI:
1914                        brcmf_fws_rssi_indicate(fws, *data);
1915                        break;
1916                case BRCMF_FWS_TYPE_TRANS_ID:
1917                        brcmf_fws_dbg_seqnum_check(fws, data);
1918                        break;
1919                case BRCMF_FWS_TYPE_PKTTAG:
1920                case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
1921                default:
1922                        fws->stats.tlv_invalid_type++;
1923                        break;
1924                }
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;
1929        }
1930
1931        if (data_len != 0)
1932                fws->stats.tlv_parse_failed++;
1933
1934        if (status == BRCMF_FWS_RET_OK_SCHEDULE)
1935                brcmf_fws_schedule_deq(fws);
1936
1937        /* signalling processing result does
1938         * not affect the actual ethernet packet.
1939         */
1940        skb_pull(skb, siglen);
1941
1942        /* this may be a signal-only packet
1943         */
1944        if (skb->len == 0)
1945                fws->stats.header_only_pkt++;
1946}
1947
1948static u8 brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
1949                                   struct sk_buff *p)
1950{
1951        struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
1952        struct brcmf_fws_mac_descriptor *entry = skcb->mac;
1953        u8 flags;
1954
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)) {
1959                /*
1960                 * Indicate that this packet is being sent in response to an
1961                 * explicit request from the firmware side.
1962                 */
1963                flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
1964        }
1965        brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
1966        return brcmf_fws_hdrpush(fws, p);
1967}
1968
1969static void brcmf_fws_rollback_toq(struct brcmf_fws_info *fws,
1970                                   struct sk_buff *skb, int fifo)
1971{
1972        struct brcmf_fws_mac_descriptor *entry;
1973        struct sk_buff *pktout;
1974        int qidx, hslot;
1975        int rc = 0;
1976
1977        entry = brcmf_skbcb(skb)->mac;
1978        if (entry->occupied) {
1979                qidx = 2 * fifo;
1980                if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_SUPPRESSED)
1981                        qidx++;
1982
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);
1986                        rc = -ENOSPC;
1987                }
1988        } else {
1989                brcmf_err("%s entry removed\n", entry->name);
1990                rc = -ENOENT;
1991        }
1992
1993        if (rc) {
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,
1997                                      hslot, 0, 0);
1998        } else {
1999                fws->stats.rollback_success++;
2000                brcmf_fws_return_credits(fws, fifo, 1);
2001                brcmf_fws_macdesc_return_req_credit(skb);
2002        }
2003}
2004
2005static int brcmf_fws_borrow_credit(struct brcmf_fws_info *fws)
2006{
2007        int lender_ac;
2008
2009        if (time_after(fws->borrow_defer_timestamp, jiffies)) {
2010                fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
2011                return -ENAVAIL;
2012        }
2013
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);
2022                        return 0;
2023                }
2024        }
2025        fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
2026        return -ENAVAIL;
2027}
2028
2029static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
2030                                struct sk_buff *skb)
2031{
2032        struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
2033        struct brcmf_fws_mac_descriptor *entry;
2034        int rc;
2035        u8 ifidx;
2036        u8 data_offset;
2037
2038        entry = skcb->mac;
2039        if (IS_ERR(entry))
2040                return PTR_ERR(entry);
2041
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);
2052        if (rc < 0) {
2053                entry->transit_count--;
2054                if (entry->suppressed)
2055                        entry->suppr_transit_count--;
2056                (void)brcmf_proto_hdrpull(fws->drvr, false, skb, NULL);
2057                goto rollback;
2058        }
2059
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]++;
2064
2065        return rc;
2066
2067rollback:
2068        brcmf_fws_rollback_toq(fws, skb, fifo);
2069        return rc;
2070}
2071
2072static int brcmf_fws_assign_htod(struct brcmf_fws_info *fws, struct sk_buff *p,
2073                                  int fifo)
2074{
2075        struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
2076        int rc, hslot;
2077
2078        skcb->htod = 0;
2079        skcb->htod_seq = 0;
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);
2085        if (!rc)
2086                skcb->mac->seq[fifo]++;
2087        else
2088                fws->stats.generic_error++;
2089        return rc;
2090}
2091
2092int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
2093{
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);
2100        int rc = 0;
2101
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);
2106
2107        drvr->tx_multicast += !!multicast;
2108
2109        if (fws->avoid_queueing) {
2110                rc = brcmf_proto_txdata(drvr, ifp->ifidx, 0, skb);
2111                if (rc < 0)
2112                        brcmf_txfinalize(ifp, skb, false);
2113                return rc;
2114        }
2115
2116        /* set control buffer information */
2117        skcb->if_flags = 0;
2118        skcb->state = BRCMF_FWS_SKBSTATE_NEW;
2119        brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
2120        if (!multicast)
2121                fifo = brcmf_fws_prio2fifo[skb->priority];
2122
2123        brcmf_fws_lock(fws);
2124        if (fifo != BRCMF_FWS_FIFO_AC_BE && fifo < BRCMF_FWS_FIFO_BCMC)
2125                fws->borrow_defer_timestamp = jiffies +
2126                                              BRCMF_FWS_BORROW_DEFER_PERIOD;
2127
2128        skcb->mac = brcmf_fws_macdesc_find(fws, ifp, eh->h_dest);
2129        brcmf_dbg(DATA, "%s mac %pM multi %d fifo %d\n", skcb->mac->name,
2130                  eh->h_dest, multicast, fifo);
2131        if (!brcmf_fws_assign_htod(fws, skb, fifo)) {
2132                brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
2133                brcmf_fws_schedule_deq(fws);
2134        } else {
2135                brcmf_err("drop skb: no hanger slot\n");
2136                brcmf_txfinalize(ifp, skb, false);
2137                rc = -ENOMEM;
2138        }
2139        brcmf_fws_unlock(fws);
2140
2141        return rc;
2142}
2143
2144void brcmf_fws_reset_interface(struct brcmf_if *ifp)
2145{
2146        struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
2147
2148        brcmf_dbg(TRACE, "enter: bsscfgidx=%d\n", ifp->bsscfgidx);
2149        if (!entry)
2150                return;
2151
2152        brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
2153}
2154
2155void brcmf_fws_add_interface(struct brcmf_if *ifp)
2156{
2157        struct brcmf_fws_info *fws = ifp->drvr->fws;
2158        struct brcmf_fws_mac_descriptor *entry;
2159
2160        if (!ifp->ndev)
2161                return;
2162
2163        entry = &fws->desc.iface[ifp->ifidx];
2164        ifp->fws_desc = entry;
2165        brcmf_fws_macdesc_init(entry, ifp->mac_addr, ifp->ifidx);
2166        brcmf_fws_macdesc_set_name(fws, entry);
2167        brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
2168                        BRCMF_FWS_PSQ_LEN);
2169        brcmf_dbg(TRACE, "added %s\n", entry->name);
2170}
2171
2172void brcmf_fws_del_interface(struct brcmf_if *ifp)
2173{
2174        struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
2175
2176        if (!entry)
2177                return;
2178
2179        brcmf_fws_lock(ifp->drvr->fws);
2180        ifp->fws_desc = NULL;
2181        brcmf_dbg(TRACE, "deleting %s\n", entry->name);
2182        brcmf_fws_macdesc_deinit(entry);
2183        brcmf_fws_cleanup(ifp->drvr->fws, ifp->ifidx);
2184        brcmf_fws_unlock(ifp->drvr->fws);
2185}
2186
2187static void brcmf_fws_dequeue_worker(struct work_struct *worker)
2188{
2189        struct brcmf_fws_info *fws;
2190        struct brcmf_pub *drvr;
2191        struct sk_buff *skb;
2192        int fifo;
2193        u32 hslot;
2194        u32 ifidx;
2195        int ret;
2196
2197        fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);
2198        drvr = fws->drvr;
2199
2200        brcmf_fws_lock(fws);
2201        for (fifo = BRCMF_FWS_FIFO_BCMC; fifo >= 0 && !fws->bus_flow_blocked;
2202             fifo--) {
2203                if (!brcmf_fws_fc_active(fws)) {
2204                        while ((skb = brcmf_fws_deq(fws, fifo)) != NULL) {
2205                                hslot = brcmf_skb_htod_tag_get_field(skb,
2206                                                                     HSLOT);
2207                                brcmf_fws_hanger_poppkt(&fws->hanger, hslot,
2208                                                        &skb, true);
2209                                ifidx = brcmf_skb_if_flags_get_field(skb,
2210                                                                     INDEX);
2211                                /* Use proto layer to send data frame */
2212                                brcmf_fws_unlock(fws);
2213                                ret = brcmf_proto_txdata(drvr, ifidx, 0, skb);
2214                                brcmf_fws_lock(fws);
2215                                if (ret < 0)
2216                                        brcmf_txfinalize(brcmf_get_ifp(drvr,
2217                                                                       ifidx),
2218                                                         skb, false);
2219                                if (fws->bus_flow_blocked)
2220                                        break;
2221                        }
2222                        continue;
2223                }
2224                while ((fws->fifo_credit[fifo]) || ((!fws->bcmc_credit_check) &&
2225                       (fifo == BRCMF_FWS_FIFO_BCMC))) {
2226                        skb = brcmf_fws_deq(fws, fifo);
2227                        if (!skb)
2228                                break;
2229                        fws->fifo_credit[fifo]--;
2230                        if (brcmf_fws_commit_skb(fws, fifo, skb))
2231                                break;
2232                        if (fws->bus_flow_blocked)
2233                                break;
2234                }
2235                if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
2236                    (fws->fifo_credit[fifo] == 0) &&
2237                    (!fws->bus_flow_blocked)) {
2238                        while (brcmf_fws_borrow_credit(fws) == 0) {
2239                                skb = brcmf_fws_deq(fws, fifo);
2240                                if (!skb) {
2241                                        brcmf_fws_return_credits(fws, fifo, 1);
2242                                        break;
2243                                }
2244                                if (brcmf_fws_commit_skb(fws, fifo, skb))
2245                                        break;
2246                                if (fws->bus_flow_blocked)
2247                                        break;
2248                        }
2249                }
2250        }
2251        brcmf_fws_unlock(fws);
2252}
2253
2254#ifdef DEBUG
2255static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
2256{
2257        struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
2258        struct brcmf_fws_stats *fwstats = &bus_if->drvr->fws->stats;
2259
2260        seq_printf(seq,
2261                   "header_pulls:      %u\n"
2262                   "header_only_pkt:   %u\n"
2263                   "tlv_parse_failed:  %u\n"
2264                   "tlv_invalid_type:  %u\n"
2265                   "mac_update_fails:  %u\n"
2266                   "ps_update_fails:   %u\n"
2267                   "if_update_fails:   %u\n"
2268                   "pkt2bus:           %u\n"
2269                   "generic_error:     %u\n"
2270                   "rollback_success:  %u\n"
2271                   "rollback_failed:   %u\n"
2272                   "delayq_full:       %u\n"
2273                   "supprq_full:       %u\n"
2274                   "txs_indicate:      %u\n"
2275                   "txs_discard:       %u\n"
2276                   "txs_suppr_core:    %u\n"
2277                   "txs_suppr_ps:      %u\n"
2278                   "txs_tossed:        %u\n"
2279                   "txs_host_tossed:   %u\n"
2280                   "bus_flow_block:    %u\n"
2281                   "fws_flow_block:    %u\n"
2282                   "send_pkts:         BK:%u BE:%u VO:%u VI:%u BCMC:%u\n"
2283                   "requested_sent:    BK:%u BE:%u VO:%u VI:%u BCMC:%u\n",
2284                   fwstats->header_pulls,
2285                   fwstats->header_only_pkt,
2286                   fwstats->tlv_parse_failed,
2287                   fwstats->tlv_invalid_type,
2288                   fwstats->mac_update_failed,
2289                   fwstats->mac_ps_update_failed,
2290                   fwstats->if_update_failed,
2291                   fwstats->pkt2bus,
2292                   fwstats->generic_error,
2293                   fwstats->rollback_success,
2294                   fwstats->rollback_failed,
2295                   fwstats->delayq_full_error,
2296                   fwstats->supprq_full_error,
2297                   fwstats->txs_indicate,
2298                   fwstats->txs_discard,
2299                   fwstats->txs_supp_core,
2300                   fwstats->txs_supp_ps,
2301                   fwstats->txs_tossed,
2302                   fwstats->txs_host_tossed,
2303                   fwstats->bus_flow_block,
2304                   fwstats->fws_flow_block,
2305                   fwstats->send_pkts[0], fwstats->send_pkts[1],
2306                   fwstats->send_pkts[2], fwstats->send_pkts[3],
2307                   fwstats->send_pkts[4],
2308                   fwstats->requested_sent[0],
2309                   fwstats->requested_sent[1],
2310                   fwstats->requested_sent[2],
2311                   fwstats->requested_sent[3],
2312                   fwstats->requested_sent[4]);
2313
2314        return 0;
2315}
2316#else
2317static int brcmf_debugfs_fws_stats_read(struct seq_file *seq, void *data)
2318{
2319        return 0;
2320}
2321#endif
2322
2323int brcmf_fws_init(struct brcmf_pub *drvr)
2324{
2325        struct brcmf_fws_info *fws;
2326        struct brcmf_if *ifp;
2327        u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
2328        int rc;
2329        u32 mode;
2330
2331        drvr->fws = kzalloc(sizeof(*(drvr->fws)), GFP_KERNEL);
2332        if (!drvr->fws) {
2333                rc = -ENOMEM;
2334                goto fail;
2335        }
2336
2337        fws = drvr->fws;
2338
2339        spin_lock_init(&fws->spinlock);
2340
2341        /* set linkage back */
2342        fws->drvr = drvr;
2343        fws->fcmode = drvr->settings->fcmode;
2344
2345        if ((drvr->bus_if->always_use_fws_queue == false) &&
2346            (fws->fcmode == BRCMF_FWS_FCMODE_NONE)) {
2347                fws->avoid_queueing = true;
2348                brcmf_dbg(INFO, "FWS queueing will be avoided\n");
2349                return 0;
2350        }
2351
2352        fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
2353        if (fws->fws_wq == NULL) {
2354                brcmf_err("workqueue creation failed\n");
2355                rc = -EBADF;
2356                goto fail;
2357        }
2358        INIT_WORK(&fws->fws_dequeue_work, brcmf_fws_dequeue_worker);
2359
2360        /* enable firmware signalling if fcmode active */
2361        if (fws->fcmode != BRCMF_FWS_FCMODE_NONE)
2362                tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
2363                       BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
2364                       BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE |
2365                       BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE;
2366
2367        rc = brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
2368                                 brcmf_fws_notify_credit_map);
2369        if (rc < 0) {
2370                brcmf_err("register credit map handler failed\n");
2371                goto fail;
2372        }
2373        rc = brcmf_fweh_register(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT,
2374                                 brcmf_fws_notify_bcmc_credit_support);
2375        if (rc < 0) {
2376                brcmf_err("register bcmc credit handler failed\n");
2377                brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
2378                goto fail;
2379        }
2380
2381        /* Setting the iovar may fail if feature is unsupported
2382         * so leave the rc as is so driver initialization can
2383         * continue. Set mode back to none indicating not enabled.
2384         */
2385        fws->fw_signals = true;
2386        ifp = brcmf_get_ifp(drvr, 0);
2387        if (brcmf_fil_iovar_int_set(ifp, "tlv", tlv)) {
2388                brcmf_err("failed to set bdcv2 tlv signaling\n");
2389                fws->fcmode = BRCMF_FWS_FCMODE_NONE;
2390                fws->fw_signals = false;
2391        }
2392
2393        if (brcmf_fil_iovar_int_set(ifp, "ampdu_hostreorder", 1))
2394                brcmf_dbg(INFO, "enabling AMPDU host-reorder failed\n");
2395
2396        /* Enable seq number reuse, if supported */
2397        if (brcmf_fil_iovar_int_get(ifp, "wlfc_mode", &mode) == 0) {
2398                if (BRCMF_FWS_MODE_GET_REUSESEQ(mode)) {
2399                        mode = 0;
2400                        BRCMF_FWS_MODE_SET_REUSESEQ(mode, 1);
2401                        if (brcmf_fil_iovar_int_set(ifp,
2402                                                    "wlfc_mode", mode) == 0) {
2403                                BRCMF_FWS_MODE_SET_REUSESEQ(fws->mode, 1);
2404                        }
2405                }
2406        }
2407
2408        brcmf_fws_hanger_init(&fws->hanger);
2409        brcmf_fws_macdesc_init(&fws->desc.other, NULL, 0);
2410        brcmf_fws_macdesc_set_name(fws, &fws->desc.other);
2411        brcmu_pktq_init(&fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
2412                        BRCMF_FWS_PSQ_LEN);
2413
2414        /* create debugfs file for statistics */
2415        brcmf_debugfs_add_entry(drvr, "fws_stats",
2416                                brcmf_debugfs_fws_stats_read);
2417
2418        brcmf_dbg(INFO, "%s bdcv2 tlv signaling [%x]\n",
2419                  fws->fw_signals ? "enabled" : "disabled", tlv);
2420        return 0;
2421
2422fail:
2423        brcmf_fws_deinit(drvr);
2424        return rc;
2425}
2426
2427void brcmf_fws_deinit(struct brcmf_pub *drvr)
2428{
2429        struct brcmf_fws_info *fws = drvr->fws;
2430
2431        if (!fws)
2432                return;
2433
2434        if (drvr->fws->fws_wq)
2435                destroy_workqueue(drvr->fws->fws_wq);
2436
2437        /* cleanup */
2438        brcmf_fws_lock(fws);
2439        brcmf_fws_cleanup(fws, -1);
2440        drvr->fws = NULL;
2441        brcmf_fws_unlock(fws);
2442
2443        /* free top structure */
2444        kfree(fws);
2445}
2446
2447bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
2448{
2449        if (!fws->creditmap_received)
2450                return false;
2451
2452        return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
2453}
2454
2455void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
2456{
2457        u32 hslot;
2458
2459        if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_TIM) {
2460                brcmu_pkt_buf_free_skb(skb);
2461                return;
2462        }
2463        brcmf_fws_lock(fws);
2464        hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
2465        brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED, hslot, 0, 0);
2466        brcmf_fws_unlock(fws);
2467}
2468
2469void brcmf_fws_bus_blocked(struct brcmf_pub *drvr, bool flow_blocked)
2470{
2471        struct brcmf_fws_info *fws = drvr->fws;
2472        struct brcmf_if *ifp;
2473        int i;
2474
2475        if (fws->avoid_queueing) {
2476                for (i = 0; i < BRCMF_MAX_IFS; i++) {
2477                        ifp = drvr->iflist[i];
2478                        if (!ifp || !ifp->ndev)
2479                                continue;
2480                        brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FLOW,
2481                                             flow_blocked);
2482                }
2483        } else {
2484                fws->bus_flow_blocked = flow_blocked;
2485                if (!flow_blocked)
2486                        brcmf_fws_schedule_deq(fws);
2487                else
2488                        fws->stats.bus_flow_block++;
2489        }
2490}
2491