fwsignal.c 55.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2010 Broadcom Corporation
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <linux/types.h>
17
#include <linux/module.h>
18 19 20 21
#include <linux/if_ether.h>
#include <linux/spinlock.h>
#include <linux/skbuff.h>
#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 28 29

#include <brcmu_utils.h>
#include <brcmu_wifi.h>
#include "dhd.h"
30
#include "dhd_proto.h"
31
#include "dhd_dbg.h"
32
#include "dhd_bus.h"
33
#include "fwil.h"
34
#include "fwil_types.h"
35 36
#include "fweh.h"
#include "fwsignal.h"
37 38
#include "p2p.h"
#include "wl_cfg80211.h"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

/**
 * DOC: Firmware Signalling
 *
 * Firmware can send signals to host and vice versa, which are passed in the
 * data packets using TLV based header. This signalling layer is on top of the
 * BDC bus protocol layer.
 */

/*
 * single definition for firmware-driver flow control tlv's.
 *
 * each tlv is specified by BRCMF_FWS_TLV_DEF(name, ID, length).
 * A length value 0 indicates variable length tlv.
 */
#define BRCMF_FWS_TLV_DEFLIST \
	BRCMF_FWS_TLV_DEF(MAC_OPEN, 1, 1) \
	BRCMF_FWS_TLV_DEF(MAC_CLOSE, 2, 1) \
	BRCMF_FWS_TLV_DEF(MAC_REQUEST_CREDIT, 3, 2) \
	BRCMF_FWS_TLV_DEF(TXSTATUS, 4, 4) \
	BRCMF_FWS_TLV_DEF(PKTTAG, 5, 4) \
	BRCMF_FWS_TLV_DEF(MACDESC_ADD,	6, 8) \
	BRCMF_FWS_TLV_DEF(MACDESC_DEL, 7, 8) \
	BRCMF_FWS_TLV_DEF(RSSI, 8, 1) \
	BRCMF_FWS_TLV_DEF(INTERFACE_OPEN, 9, 1) \
	BRCMF_FWS_TLV_DEF(INTERFACE_CLOSE, 10, 1) \
65
	BRCMF_FWS_TLV_DEF(FIFO_CREDITBACK, 11, 6) \
66 67 68 69 70 71 72
	BRCMF_FWS_TLV_DEF(PENDING_TRAFFIC_BMP, 12, 2) \
	BRCMF_FWS_TLV_DEF(MAC_REQUEST_PACKET, 13, 3) \
	BRCMF_FWS_TLV_DEF(HOST_REORDER_RXPKTS, 14, 10) \
	BRCMF_FWS_TLV_DEF(TRANS_ID, 18, 6) \
	BRCMF_FWS_TLV_DEF(COMP_TXSTATUS, 19, 1) \
	BRCMF_FWS_TLV_DEF(FILLER, 255, 0)

73
/*
74 75 76 77 78 79 80 81 82 83
 * enum brcmf_fws_tlv_type - definition of tlv identifiers.
 */
#define BRCMF_FWS_TLV_DEF(name, id, len) \
	BRCMF_FWS_TYPE_ ## name =  id,
enum brcmf_fws_tlv_type {
	BRCMF_FWS_TLV_DEFLIST
	BRCMF_FWS_TYPE_INVALID
};
#undef BRCMF_FWS_TLV_DEF

84 85 86 87 88 89 90 91 92 93
/*
 * enum brcmf_fws_tlv_len - definition of tlv lengths.
 */
#define BRCMF_FWS_TLV_DEF(name, id, len) \
	BRCMF_FWS_TYPE_ ## name ## _LEN = (len),
enum brcmf_fws_tlv_len {
	BRCMF_FWS_TLV_DEFLIST
};
#undef BRCMF_FWS_TLV_DEF

94
#ifdef DEBUG
95
/*
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
 * brcmf_fws_tlv_names - array of tlv names.
 */
#define BRCMF_FWS_TLV_DEF(name, id, len) \
	{ id, #name },
static struct {
	enum brcmf_fws_tlv_type id;
	const char *name;
} brcmf_fws_tlv_names[] = {
	BRCMF_FWS_TLV_DEFLIST
};
#undef BRCMF_FWS_TLV_DEF

static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(brcmf_fws_tlv_names); i++)
		if (brcmf_fws_tlv_names[i].id == id)
			return brcmf_fws_tlv_names[i].name;

	return "INVALID";
}
#else
static const char *brcmf_fws_get_tlv_name(enum brcmf_fws_tlv_type id)
{
	return "NODEBUG";
}
#endif /* DEBUG */

125
/*
126 127
 * flags used to enable tlv signalling from firmware.
 */
128 129 130 131 132 133 134 135
#define BRCMF_FWS_FLAGS_RSSI_SIGNALS			0x0001
#define BRCMF_FWS_FLAGS_XONXOFF_SIGNALS			0x0002
#define BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS		0x0004
#define BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE	0x0008
#define BRCMF_FWS_FLAGS_PSQ_GENERATIONFSM_ENABLE	0x0010
#define BRCMF_FWS_FLAGS_PSQ_ZERO_BUFFER_ENABLE		0x0020
#define BRCMF_FWS_FLAGS_HOST_RXREORDER_ACTIVE		0x0040

136 137 138 139 140
#define BRCMF_FWS_MAC_DESC_TABLE_SIZE			32
#define BRCMF_FWS_MAC_DESC_ID_INVALID			0xff

#define BRCMF_FWS_HOSTIF_FLOWSTATE_OFF			0
#define BRCMF_FWS_HOSTIF_FLOWSTATE_ON			1
141 142
#define BRCMF_FWS_FLOWCONTROL_HIWATER			128
#define BRCMF_FWS_FLOWCONTROL_LOWATER			64
143

144
#define BRCMF_FWS_PSQ_PREC_COUNT		((BRCMF_FWS_FIFO_COUNT + 1) * 2)
145 146
#define BRCMF_FWS_PSQ_LEN				256

147 148 149
#define BRCMF_FWS_HTOD_FLAG_PKTFROMHOST			0x01
#define BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED		0x02

150 151 152
#define BRCMF_FWS_RET_OK_NOSCHEDULE	0
#define BRCMF_FWS_RET_OK_SCHEDULE	1

153 154
/**
 * enum brcmf_fws_skb_state - indicates processing state of skb.
155 156 157 158
 *
 * @BRCMF_FWS_SKBSTATE_NEW: sk_buff is newly arrived in the driver.
 * @BRCMF_FWS_SKBSTATE_DELAYED: sk_buff had to wait on queue.
 * @BRCMF_FWS_SKBSTATE_SUPPRESSED: sk_buff has been suppressed by firmware.
159
 * @BRCMF_FWS_SKBSTATE_TIM: allocated for TIM update info.
160 161
 */
enum brcmf_fws_skb_state {
162 163
	BRCMF_FWS_SKBSTATE_NEW,
	BRCMF_FWS_SKBSTATE_DELAYED,
164 165
	BRCMF_FWS_SKBSTATE_SUPPRESSED,
	BRCMF_FWS_SKBSTATE_TIM
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
};

/**
 * struct brcmf_skbuff_cb - control buffer associated with skbuff.
 *
 * @if_flags: holds interface index and packet related flags.
 * @htod: host to device packet identifier (used in PKTTAG tlv).
 * @state: transmit state of the packet.
 * @mac: descriptor related to destination for this packet.
 *
 * This information is stored in control buffer struct sk_buff::cb, which
 * provides 48 bytes of storage so this structure should not exceed that.
 */
struct brcmf_skbuff_cb {
	u16 if_flags;
	u32 htod;
	enum brcmf_fws_skb_state state;
	struct brcmf_fws_mac_descriptor *mac;
};

186
/*
187 188 189 190
 * macro casting skbuff control buffer to struct brcmf_skbuff_cb.
 */
#define brcmf_skbcb(skb)	((struct brcmf_skbuff_cb *)((skb)->cb))

191
/*
192 193
 * sk_buff control if flags
 *
194
 *	b[11]  - packet sent upon firmware request.
195 196
 *	b[10]  - packet only contains signalling data.
 *	b[9]   - packet is a tx packet.
197
 *	b[8]   - packet used requested credit
198 199 200 201 202 203 204 205 206
 *	b[7]   - interface in AP mode.
 *	b[3:0] - interface index.
 */
#define BRCMF_SKB_IF_FLAGS_REQUESTED_MASK	0x0800
#define BRCMF_SKB_IF_FLAGS_REQUESTED_SHIFT	11
#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_MASK	0x0400
#define BRCMF_SKB_IF_FLAGS_SIGNAL_ONLY_SHIFT	10
#define BRCMF_SKB_IF_FLAGS_TRANSMIT_MASK        0x0200
#define BRCMF_SKB_IF_FLAGS_TRANSMIT_SHIFT	9
207 208
#define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_MASK	0x0100
#define BRCMF_SKB_IF_FLAGS_REQ_CREDIT_SHIFT	8
209 210 211 212 213 214 215 216 217 218 219 220 221 222
#define BRCMF_SKB_IF_FLAGS_IF_AP_MASK		0x0080
#define BRCMF_SKB_IF_FLAGS_IF_AP_SHIFT		7
#define BRCMF_SKB_IF_FLAGS_INDEX_MASK		0x000f
#define BRCMF_SKB_IF_FLAGS_INDEX_SHIFT		0

#define brcmf_skb_if_flags_set_field(skb, field, value) \
	brcmu_maskset16(&(brcmf_skbcb(skb)->if_flags), \
			BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
			BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT, (value))
#define brcmf_skb_if_flags_get_field(skb, field) \
	brcmu_maskget16(brcmf_skbcb(skb)->if_flags, \
			BRCMF_SKB_IF_FLAGS_ ## field ## _MASK, \
			BRCMF_SKB_IF_FLAGS_ ## field ## _SHIFT)

223
/*
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
 * sk_buff control packet identifier
 *
 * 32-bit packet identifier used in PKTTAG tlv from host to dongle.
 *
 * - Generated at the host (e.g. dhd)
 * - Seen as a generic sequence number by firmware except for the flags field.
 *
 * Generation	: b[31]	=> generation number for this packet [host->fw]
 *			   OR, current generation number [fw->host]
 * Flags	: b[30:27] => command, status flags
 * FIFO-AC	: b[26:24] => AC-FIFO id
 * h-slot	: b[23:8] => hanger-slot
 * freerun	: b[7:0] => A free running counter
 */
#define BRCMF_SKB_HTOD_TAG_GENERATION_MASK		0x80000000
#define BRCMF_SKB_HTOD_TAG_GENERATION_SHIFT		31
#define BRCMF_SKB_HTOD_TAG_FLAGS_MASK			0x78000000
#define BRCMF_SKB_HTOD_TAG_FLAGS_SHIFT			27
#define BRCMF_SKB_HTOD_TAG_FIFO_MASK			0x07000000
#define BRCMF_SKB_HTOD_TAG_FIFO_SHIFT			24
#define BRCMF_SKB_HTOD_TAG_HSLOT_MASK			0x00ffff00
#define BRCMF_SKB_HTOD_TAG_HSLOT_SHIFT			8
#define BRCMF_SKB_HTOD_TAG_FREERUN_MASK			0x000000ff
247
#define BRCMF_SKB_HTOD_TAG_FREERUN_SHIFT		0
248 249

#define brcmf_skb_htod_tag_set_field(skb, field, value) \
250
	brcmu_maskset32(&(brcmf_skbcb(skb)->htod), \
251 252 253
			BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
			BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT, (value))
#define brcmf_skb_htod_tag_get_field(skb, field) \
254
	brcmu_maskget32(brcmf_skbcb(skb)->htod, \
255 256 257
			BRCMF_SKB_HTOD_TAG_ ## field ## _MASK, \
			BRCMF_SKB_HTOD_TAG_ ## field ## _SHIFT)

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
#define BRCMF_FWS_TXSTAT_GENERATION_MASK	0x80000000
#define BRCMF_FWS_TXSTAT_GENERATION_SHIFT	31
#define BRCMF_FWS_TXSTAT_FLAGS_MASK		0x78000000
#define BRCMF_FWS_TXSTAT_FLAGS_SHIFT		27
#define BRCMF_FWS_TXSTAT_FIFO_MASK		0x07000000
#define BRCMF_FWS_TXSTAT_FIFO_SHIFT		24
#define BRCMF_FWS_TXSTAT_HSLOT_MASK		0x00FFFF00
#define BRCMF_FWS_TXSTAT_HSLOT_SHIFT		8
#define BRCMF_FWS_TXSTAT_PKTID_MASK		0x00FFFFFF
#define BRCMF_FWS_TXSTAT_PKTID_SHIFT		0

#define brcmf_txstatus_get_field(txs, field) \
	brcmu_maskget32(txs, BRCMF_FWS_TXSTAT_ ## field ## _MASK, \
			BRCMF_FWS_TXSTAT_ ## field ## _SHIFT)

273 274 275
/* How long to defer borrowing in jiffies */
#define BRCMF_FWS_BORROW_DEFER_PERIOD		(HZ / 10)

276 277 278
/**
 * enum brcmf_fws_fifo - fifo indices used by dongle firmware.
 *
279
 * @BRCMF_FWS_FIFO_FIRST: first fifo, ie. background.
280 281 282 283 284 285 286 287 288
 * @BRCMF_FWS_FIFO_AC_BK: fifo for background traffic.
 * @BRCMF_FWS_FIFO_AC_BE: fifo for best-effort traffic.
 * @BRCMF_FWS_FIFO_AC_VI: fifo for video traffic.
 * @BRCMF_FWS_FIFO_AC_VO: fifo for voice traffic.
 * @BRCMF_FWS_FIFO_BCMC: fifo for broadcast/multicast (AP only).
 * @BRCMF_FWS_FIFO_ATIM: fifo for ATIM (AP only).
 * @BRCMF_FWS_FIFO_COUNT: number of fifos.
 */
enum brcmf_fws_fifo {
289 290
	BRCMF_FWS_FIFO_FIRST,
	BRCMF_FWS_FIFO_AC_BK = BRCMF_FWS_FIFO_FIRST,
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	BRCMF_FWS_FIFO_AC_BE,
	BRCMF_FWS_FIFO_AC_VI,
	BRCMF_FWS_FIFO_AC_VO,
	BRCMF_FWS_FIFO_BCMC,
	BRCMF_FWS_FIFO_ATIM,
	BRCMF_FWS_FIFO_COUNT
};

/**
 * enum brcmf_fws_txstatus - txstatus flag values.
 *
 * @BRCMF_FWS_TXSTATUS_DISCARD:
 *	host is free to discard the packet.
 * @BRCMF_FWS_TXSTATUS_CORE_SUPPRESS:
 *	802.11 core suppressed the packet.
 * @BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS:
 *	firmware suppress the packet as device is already in PS mode.
 * @BRCMF_FWS_TXSTATUS_FW_TOSSED:
 *	firmware tossed the packet.
310 311
 * @BRCMF_FWS_TXSTATUS_HOST_TOSSED:
 *	host tossed the packet.
312 313 314 315 316
 */
enum brcmf_fws_txstatus {
	BRCMF_FWS_TXSTATUS_DISCARD,
	BRCMF_FWS_TXSTATUS_CORE_SUPPRESS,
	BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS,
317 318
	BRCMF_FWS_TXSTATUS_FW_TOSSED,
	BRCMF_FWS_TXSTATUS_HOST_TOSSED
319 320
};

321 322 323 324 325 326
enum brcmf_fws_fcmode {
	BRCMF_FWS_FCMODE_NONE,
	BRCMF_FWS_FCMODE_IMPLIED_CREDIT,
	BRCMF_FWS_FCMODE_EXPLICIT_CREDIT
};

327 328 329 330 331
enum brcmf_fws_mac_desc_state {
	BRCMF_FWS_STATE_OPEN = 1,
	BRCMF_FWS_STATE_CLOSE
};

332 333 334 335
/**
 * struct brcmf_fws_mac_descriptor - firmware signalling data per node/interface
 *
 * @occupied: slot is in use.
336
 * @mac_handle: handle for mac entry determined by firmware.
337 338
 * @interface_id: interface index.
 * @state: current state.
339 340
 * @suppressed: mac entry is suppressed.
 * @generation: generation bit.
341 342 343
 * @ac_bitmap: ac queue bitmap.
 * @requested_credit: credits requested by firmware.
 * @ea: ethernet address.
344
 * @seq: per-node free-running sequence.
345
 * @psq: power-save queue.
346
 * @transit_count: packet in transit to firmware.
347 348
 */
struct brcmf_fws_mac_descriptor {
349
	char name[16];
350
	u8 occupied;
351
	u8 mac_handle;
352 353
	u8 interface_id;
	u8 state;
354 355
	bool suppressed;
	u8 generation;
356 357
	u8 ac_bitmap;
	u8 requested_credit;
358
	u8 requested_packet;
359
	u8 ea[ETH_ALEN];
360
	u8 seq[BRCMF_FWS_FIFO_COUNT];
361
	struct pktq psq;
362
	int transit_count;
363 364 365 366
	int suppr_transit_count;
	bool send_tim_signal;
	u8 traffic_pending_bmp;
	u8 traffic_lastreported_bmp;
367 368
};

369 370 371 372 373
#define BRCMF_FWS_HANGER_MAXITEMS	1024

/**
 * enum brcmf_fws_hanger_item_state - state of hanger item.
 *
374 375 376
 * @BRCMF_FWS_HANGER_ITEM_STATE_FREE: item is free for use.
 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE: item is in use.
 * @BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED: item was suppressed.
377 378
 */
enum brcmf_fws_hanger_item_state {
379 380 381
	BRCMF_FWS_HANGER_ITEM_STATE_FREE = 1,
	BRCMF_FWS_HANGER_ITEM_STATE_INUSE,
	BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
};


/**
 * struct brcmf_fws_hanger_item - single entry for tx pending packet.
 *
 * @state: entry is either free or occupied.
 * @pkt: packet itself.
 */
struct brcmf_fws_hanger_item {
	enum brcmf_fws_hanger_item_state state;
	struct sk_buff *pkt;
};

/**
 * struct brcmf_fws_hanger - holds packets awaiting firmware txstatus.
 *
 * @pushed: packets pushed to await txstatus.
 * @popped: packets popped upon handling txstatus.
 * @failed_to_push: packets that could not be pushed.
 * @failed_to_pop: packets that could not be popped.
 * @failed_slotfind: packets for which failed to find an entry.
 * @slot_pos: last returned item index for a free entry.
 * @items: array of hanger items.
 */
struct brcmf_fws_hanger {
	u32 pushed;
	u32 popped;
	u32 failed_to_push;
	u32 failed_to_pop;
	u32 failed_slotfind;
	u32 slot_pos;
	struct brcmf_fws_hanger_item items[BRCMF_FWS_HANGER_MAXITEMS];
};

417 418 419 420 421 422
struct brcmf_fws_macdesc_table {
	struct brcmf_fws_mac_descriptor nodes[BRCMF_FWS_MAC_DESC_TABLE_SIZE];
	struct brcmf_fws_mac_descriptor iface[BRCMF_MAX_IFS];
	struct brcmf_fws_mac_descriptor other;
};

423 424 425
struct brcmf_fws_info {
	struct brcmf_pub *drvr;
	struct brcmf_fws_stats stats;
426
	struct brcmf_fws_hanger hanger;
427
	enum brcmf_fws_fcmode fcmode;
428
	bool bcmc_credit_check;
429 430 431 432
	struct brcmf_fws_macdesc_table desc;
	struct workqueue_struct *fws_wq;
	struct work_struct fws_dequeue_work;
	u32 fifo_enqpkt[BRCMF_FWS_FIFO_COUNT];
433
	int fifo_credit[BRCMF_FWS_FIFO_COUNT];
434
	int credits_borrowed[BRCMF_FWS_FIFO_AC_VO + 1];
435 436 437
	int deq_node_pos[BRCMF_FWS_FIFO_COUNT];
	u32 fifo_credit_map;
	u32 fifo_delay_map;
438
	unsigned long borrow_defer_timestamp;
439
	bool bus_flow_blocked;
440
	bool creditmap_received;
441 442 443 444 445 446 447 448 449 450 451 452 453 454
};

/*
 * brcmf_fws_prio2fifo - mapping from 802.1d priority to firmware fifo index.
 */
static const int brcmf_fws_prio2fifo[] = {
	BRCMF_FWS_FIFO_AC_BE,
	BRCMF_FWS_FIFO_AC_BK,
	BRCMF_FWS_FIFO_AC_BK,
	BRCMF_FWS_FIFO_AC_BE,
	BRCMF_FWS_FIFO_AC_VI,
	BRCMF_FWS_FIFO_AC_VI,
	BRCMF_FWS_FIFO_AC_VO,
	BRCMF_FWS_FIFO_AC_VO
455 456
};

457 458 459 460
static int fcmode;
module_param(fcmode, int, S_IRUSR);
MODULE_PARM_DESC(fcmode, "mode of firmware signalled flow control");

461 462 463 464
#define BRCMF_FWS_TLV_DEF(name, id, len) \
	case BRCMF_FWS_TYPE_ ## name: \
		return len;

465 466 467 468 469 470 471 472
/**
 * brcmf_fws_get_tlv_len() - returns defined length for given tlv id.
 *
 * @fws: firmware-signalling information.
 * @id: identifier of the TLV.
 *
 * Return: the specified length for the given TLV; Otherwise -EINVAL.
 */
473 474 475 476 477 478 479 480 481 482 483 484 485
static int brcmf_fws_get_tlv_len(struct brcmf_fws_info *fws,
				 enum brcmf_fws_tlv_type id)
{
	switch (id) {
	BRCMF_FWS_TLV_DEFLIST
	default:
		fws->stats.tlv_invalid_type++;
		break;
	}
	return -EINVAL;
}
#undef BRCMF_FWS_TLV_DEF

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
static bool brcmf_fws_ifidx_match(struct sk_buff *skb, void *arg)
{
	u32 ifidx = brcmf_skb_if_flags_get_field(skb, INDEX);
	return ifidx == *(int *)arg;
}

static void brcmf_fws_psq_flush(struct brcmf_fws_info *fws, struct pktq *q,
				int ifidx)
{
	bool (*matchfn)(struct sk_buff *, void *) = NULL;
	struct sk_buff *skb;
	int prec;

	if (ifidx != -1)
		matchfn = brcmf_fws_ifidx_match;
	for (prec = 0; prec < q->num_prec; prec++) {
		skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
		while (skb) {
504
			brcmu_pkt_buf_free_skb(skb);
505 506 507 508 509
			skb = brcmu_pktq_pdeq_match(q, prec, matchfn, &ifidx);
		}
	}
}

510 511 512 513 514 515
static void brcmf_fws_hanger_init(struct brcmf_fws_hanger *hanger)
{
	int i;

	memset(hanger, 0, sizeof(*hanger));
	for (i = 0; i < ARRAY_SIZE(hanger->items); i++)
516
		hanger->items[i].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
517 518
}

519
static u32 brcmf_fws_hanger_get_free_slot(struct brcmf_fws_hanger *h)
520 521 522 523 524 525
{
	u32 i;

	i = (h->slot_pos + 1) % BRCMF_FWS_HANGER_MAXITEMS;

	while (i != h->slot_pos) {
526
		if (h->items[i].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
527
			h->slot_pos = i;
528
			goto done;
529 530 531 532 533 534 535
		}
		i++;
		if (i == BRCMF_FWS_HANGER_MAXITEMS)
			i = 0;
	}
	brcmf_err("all slots occupied\n");
	h->failed_slotfind++;
536 537 538
	i = BRCMF_FWS_HANGER_MAXITEMS;
done:
	return i;
539 540
}

541
static int brcmf_fws_hanger_pushpkt(struct brcmf_fws_hanger *h,
542
				    struct sk_buff *pkt, u32 slot_id)
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
{
	if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
		return -ENOENT;

	if (h->items[slot_id].state != BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
		brcmf_err("slot is not free\n");
		h->failed_to_push++;
		return -EINVAL;
	}

	h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE;
	h->items[slot_id].pkt = pkt;
	h->pushed++;
	return 0;
}

559
static int brcmf_fws_hanger_poppkt(struct brcmf_fws_hanger *h,
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
					  u32 slot_id, struct sk_buff **pktout,
					  bool remove_item)
{
	if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
		return -ENOENT;

	if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
		brcmf_err("entry not in use\n");
		h->failed_to_pop++;
		return -EINVAL;
	}

	*pktout = h->items[slot_id].pkt;
	if (remove_item) {
		h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
		h->items[slot_id].pkt = NULL;
		h->popped++;
	}
	return 0;
}

581
static int brcmf_fws_hanger_mark_suppressed(struct brcmf_fws_hanger *h,
582
					    u32 slot_id)
583 584 585 586
{
	if (slot_id >= BRCMF_FWS_HANGER_MAXITEMS)
		return -ENOENT;

587
	if (h->items[slot_id].state == BRCMF_FWS_HANGER_ITEM_STATE_FREE) {
588 589 590 591
		brcmf_err("entry not in use\n");
		return -EINVAL;
	}

592
	h->items[slot_id].state = BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED;
593 594 595
	return 0;
}

596
static void brcmf_fws_hanger_cleanup(struct brcmf_fws_info *fws,
597 598 599
				     bool (*fn)(struct sk_buff *, void *),
				     int ifidx)
{
600
	struct brcmf_fws_hanger *h = &fws->hanger;
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	struct sk_buff *skb;
	int i;
	enum brcmf_fws_hanger_item_state s;

	for (i = 0; i < ARRAY_SIZE(h->items); i++) {
		s = h->items[i].state;
		if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE ||
		    s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE_SUPPRESSED) {
			skb = h->items[i].pkt;
			if (fn == NULL || fn(skb, &ifidx)) {
				/* suppress packets freed from psq */
				if (s == BRCMF_FWS_HANGER_ITEM_STATE_INUSE)
					brcmu_pkt_buf_free_skb(skb);
				h->items[i].state =
					BRCMF_FWS_HANGER_ITEM_STATE_FREE;
			}
		}
	}
}

621 622 623 624 625 626 627 628 629 630 631 632 633
static void brcmf_fws_macdesc_set_name(struct brcmf_fws_info *fws,
				       struct brcmf_fws_mac_descriptor *desc)
{
	if (desc == &fws->desc.other)
		strlcpy(desc->name, "MAC-OTHER", sizeof(desc->name));
	else if (desc->mac_handle)
		scnprintf(desc->name, sizeof(desc->name), "MAC-%d:%d",
			  desc->mac_handle, desc->interface_id);
	else
		scnprintf(desc->name, sizeof(desc->name), "MACIF:%d",
			  desc->interface_id);
}

634 635 636
static void brcmf_fws_init_mac_descriptor(struct brcmf_fws_mac_descriptor *desc,
					  u8 *addr, u8 ifidx)
{
637 638
	brcmf_dbg(TRACE,
		  "enter: desc %p ea=%pM, ifidx=%u\n", desc, addr, ifidx);
639 640 641
	desc->occupied = 1;
	desc->state = BRCMF_FWS_STATE_OPEN;
	desc->requested_credit = 0;
642
	desc->requested_packet = 0;
643 644 645
	/* depending on use may need ifp->bssidx instead */
	desc->interface_id = ifidx;
	desc->ac_bitmap = 0xff; /* update this when handling APSD */
646 647
	if (addr)
		memcpy(&desc->ea[0], addr, ETH_ALEN);
648 649 650 651 652 653 654 655 656 657
}

static
void brcmf_fws_clear_mac_descriptor(struct brcmf_fws_mac_descriptor *desc)
{
	brcmf_dbg(TRACE,
		  "enter: ea=%pM, ifidx=%u\n", desc->ea, desc->interface_id);
	desc->occupied = 0;
	desc->state = BRCMF_FWS_STATE_CLOSE;
	desc->requested_credit = 0;
658
	desc->requested_packet = 0;
659 660 661 662 663 664 665 666 667 668 669
}

static struct brcmf_fws_mac_descriptor *
brcmf_fws_mac_descriptor_lookup(struct brcmf_fws_info *fws, u8 *ea)
{
	struct brcmf_fws_mac_descriptor *entry;
	int i;

	if (ea == NULL)
		return ERR_PTR(-EINVAL);

670 671
	entry = &fws->desc.nodes[0];
	for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++) {
672 673 674 675 676 677 678 679
		if (entry->occupied && !memcmp(entry->ea, ea, ETH_ALEN))
			return entry;
		entry++;
	}

	return ERR_PTR(-ENOENT);
}

680
static struct brcmf_fws_mac_descriptor*
681 682
brcmf_fws_find_mac_desc(struct brcmf_fws_info *fws, struct brcmf_if *ifp,
			u8 *da)
683
{
684
	struct brcmf_fws_mac_descriptor *entry = &fws->desc.other;
685 686
	bool multicast;

687
	multicast = is_multicast_ether_addr(da);
688

689 690 691
	/* Multicast destination, STA and P2P clients get the interface entry.
	 * STA/GC gets the Mac Entry for TDLS destinations, TDLS destinations
	 * have their own entry.
692
	 */
693
	if (multicast && ifp->fws_desc) {
694 695
		entry = ifp->fws_desc;
		goto done;
696
	}
697 698 699

	entry = brcmf_fws_mac_descriptor_lookup(fws, da);
	if (IS_ERR(entry))
700
		entry = ifp->fws_desc;
701 702 703 704 705

done:
	return entry;
}

706 707
static bool brcmf_fws_mac_desc_closed(struct brcmf_fws_info *fws,
				      struct brcmf_fws_mac_descriptor *entry,
708
				      int fifo)
709
{
710
	struct brcmf_fws_mac_descriptor *if_entry;
711
	bool closed;
712

713 714 715 716 717 718 719 720
	/* for unique destination entries the related interface
	 * may be closed.
	 */
	if (entry->mac_handle) {
		if_entry = &fws->desc.iface[entry->interface_id];
		if (if_entry->state == BRCMF_FWS_STATE_CLOSE)
			return true;
	}
721 722
	/* an entry is closed when the state is closed and
	 * the firmware did not request anything.
723
	 */
724 725
	closed = entry->state == BRCMF_FWS_STATE_CLOSE &&
		 !entry->requested_credit && !entry->requested_packet;
726

727 728
	/* Or firmware does not allow traffic for given fifo */
	return closed || !(entry->ac_bitmap & BIT(fifo));
729 730
}

731 732
static void brcmf_fws_mac_desc_cleanup(struct brcmf_fws_info *fws,
				       struct brcmf_fws_mac_descriptor *entry,
733 734
				       int ifidx)
{
735 736
	if (entry->occupied && (ifidx == -1 || ifidx == entry->interface_id)) {
		brcmf_fws_psq_flush(fws, &entry->psq, ifidx);
737 738 739 740
		entry->occupied = !!(entry->psq.len);
	}
}

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
static void brcmf_fws_bus_txq_cleanup(struct brcmf_fws_info *fws,
				      bool (*fn)(struct sk_buff *, void *),
				      int ifidx)
{
	struct brcmf_fws_hanger_item *hi;
	struct pktq *txq;
	struct sk_buff *skb;
	int prec;
	u32 hslot;

	txq = brcmf_bus_gettxq(fws->drvr->bus_if);
	if (IS_ERR(txq)) {
		brcmf_dbg(TRACE, "no txq to clean up\n");
		return;
	}

	for (prec = 0; prec < txq->num_prec; prec++) {
		skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
		while (skb) {
			hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
			hi = &fws->hanger.items[hslot];
			WARN_ON(skb != hi->pkt);
			hi->state = BRCMF_FWS_HANGER_ITEM_STATE_FREE;
			brcmu_pkt_buf_free_skb(skb);
			skb = brcmu_pktq_pdeq_match(txq, prec, fn, &ifidx);
		}
	}
}

770 771 772 773
static void brcmf_fws_cleanup(struct brcmf_fws_info *fws, int ifidx)
{
	int i;
	struct brcmf_fws_mac_descriptor *table;
774
	bool (*matchfn)(struct sk_buff *, void *) = NULL;
775 776 777 778

	if (fws == NULL)
		return;

779 780 781
	if (ifidx != -1)
		matchfn = brcmf_fws_ifidx_match;

782
	/* cleanup individual nodes */
783 784
	table = &fws->desc.nodes[0];
	for (i = 0; i < ARRAY_SIZE(fws->desc.nodes); i++)
785
		brcmf_fws_mac_desc_cleanup(fws, &table[i], ifidx);
786

787
	brcmf_fws_mac_desc_cleanup(fws, &fws->desc.other, ifidx);
788
	brcmf_fws_bus_txq_cleanup(fws, matchfn, ifidx);
789 790 791
	brcmf_fws_hanger_cleanup(fws, matchfn, ifidx);
}

792
static int brcmf_fws_hdrpush(struct brcmf_fws_info *fws, struct sk_buff *skb)
793
{
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
	struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
	u8 *wlh;
	u16 data_offset = 0;
	u8 fillers;
	__le32 pkttag = cpu_to_le32(brcmf_skbcb(skb)->htod);

	brcmf_dbg(TRACE, "enter: ea=%pM, ifidx=%u (%u), pkttag=0x%08X, hslot=%d\n",
		  entry->ea, entry->interface_id,
		  brcmf_skb_if_flags_get_field(skb, INDEX),
		  le32_to_cpu(pkttag), (le32_to_cpu(pkttag) >> 8) & 0xffff);
	if (entry->send_tim_signal)
		data_offset += 2 + BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;

	/* +2 is for Type[1] and Len[1] in TLV, plus TIM signal */
	data_offset += 2 + BRCMF_FWS_TYPE_PKTTAG_LEN;
	fillers = round_up(data_offset, 4) - data_offset;
	data_offset += fillers;

	skb_push(skb, data_offset);
	wlh = skb->data;

	wlh[0] = BRCMF_FWS_TYPE_PKTTAG;
	wlh[1] = BRCMF_FWS_TYPE_PKTTAG_LEN;
	memcpy(&wlh[2], &pkttag, sizeof(pkttag));
	wlh += BRCMF_FWS_TYPE_PKTTAG_LEN + 2;

	if (entry->send_tim_signal) {
		entry->send_tim_signal = 0;
		wlh[0] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP;
		wlh[1] = BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN;
		wlh[2] = entry->mac_handle;
		wlh[3] = entry->traffic_pending_bmp;
		brcmf_dbg(TRACE, "adding TIM info: %02X:%02X:%02X:%02X\n",
			  wlh[0], wlh[1], wlh[2], wlh[3]);
		wlh += BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2;
		entry->traffic_lastreported_bmp = entry->traffic_pending_bmp;
830
	}
831 832 833 834 835 836 837 838 839 840
	if (fillers)
		memset(wlh, BRCMF_FWS_TYPE_FILLER, fillers);

	brcmf_proto_hdrpush(fws->drvr, brcmf_skb_if_flags_get_field(skb, INDEX),
			    data_offset >> 2, skb);
	return 0;
}

static bool brcmf_fws_tim_update(struct brcmf_fws_info *fws,
				 struct brcmf_fws_mac_descriptor *entry,
841
				 int fifo, bool send_immediately)
842 843 844 845 846 847 848 849
{
	struct sk_buff *skb;
	struct brcmf_bus *bus;
	struct brcmf_skbuff_cb *skcb;
	s32 err;
	u32 len;

	/* check delayedQ and suppressQ in one call using bitmap */
850 851
	if (brcmu_pktq_mlen(&entry->psq, 3 << (fifo * 2)) == 0)
		entry->traffic_pending_bmp &= ~NBITVAL(fifo);
852
	else
853
		entry->traffic_pending_bmp |= NBITVAL(fifo);
854 855

	entry->send_tim_signal = false;
856 857
	if (entry->traffic_lastreported_bmp != entry->traffic_pending_bmp)
		entry->send_tim_signal = true;
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
	if (send_immediately && entry->send_tim_signal &&
	    entry->state == BRCMF_FWS_STATE_CLOSE) {
		/* create a dummy packet and sent that. The traffic          */
		/* bitmap info will automatically be attached to that packet */
		len = BRCMF_FWS_TYPE_PKTTAG_LEN + 2 +
		      BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP_LEN + 2 +
		      4 + fws->drvr->hdrlen;
		skb = brcmu_pkt_buf_get_skb(len);
		if (skb == NULL)
			return false;
		skb_pull(skb, len);
		skcb = brcmf_skbcb(skb);
		skcb->mac = entry;
		skcb->state = BRCMF_FWS_SKBSTATE_TIM;
		bus = fws->drvr->bus_if;
		err = brcmf_fws_hdrpush(fws, skb);
		if (err == 0)
			err = brcmf_bus_txdata(bus, skb);
		if (err)
			brcmu_pkt_buf_free_skb(skb);
		return true;
	}
	return false;
881 882 883 884 885 886
}

static void
brcmf_fws_flow_control_check(struct brcmf_fws_info *fws, struct pktq *pq,
			     u8 if_id)
{
887
	struct brcmf_if *ifp = fws->drvr->iflist[!if_id ? 0 : if_id + 1];
888 889 890 891 892 893 894 895 896

	if (WARN_ON(!ifp))
		return;

	if ((ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
	    pq->len <= BRCMF_FWS_FLOWCONTROL_LOWATER)
		brcmf_txflowblock_if(ifp,
				     BRCMF_NETIF_STOP_REASON_FWS_FC, false);
	if (!(ifp->netif_stop & BRCMF_NETIF_STOP_REASON_FWS_FC) &&
897 898
	    pq->len >= BRCMF_FWS_FLOWCONTROL_HIWATER) {
		fws->stats.fws_flow_block++;
899
		brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FWS_FC, true);
900
	}
901
	return;
902 903
}

904 905 906 907 908 909
static int brcmf_fws_rssi_indicate(struct brcmf_fws_info *fws, s8 rssi)
{
	brcmf_dbg(CTL, "rssi %d\n", rssi);
	return 0;
}

910 911 912 913 914 915 916 917 918 919 920 921
static
int brcmf_fws_macdesc_indicate(struct brcmf_fws_info *fws, u8 type, u8 *data)
{
	struct brcmf_fws_mac_descriptor *entry, *existing;
	u8 mac_handle;
	u8 ifidx;
	u8 *addr;

	mac_handle = *data++;
	ifidx = *data++;
	addr = data;

922
	entry = &fws->desc.nodes[mac_handle & 0x1F];
923
	if (type == BRCMF_FWS_TYPE_MACDESC_DEL) {
924
		if (entry->occupied) {
925 926
			brcmf_dbg(TRACE, "deleting %s mac %pM\n",
				  entry->name, addr);
927
			brcmf_fws_mac_desc_cleanup(fws, entry, -1);
928
			brcmf_fws_clear_mac_descriptor(entry);
929
		} else
930 931 932 933 934 935 936 937 938
			fws->stats.mac_update_failed++;
		return 0;
	}

	existing = brcmf_fws_mac_descriptor_lookup(fws, addr);
	if (IS_ERR(existing)) {
		if (!entry->occupied) {
			entry->mac_handle = mac_handle;
			brcmf_fws_init_mac_descriptor(entry, addr, ifidx);
939
			brcmf_fws_macdesc_set_name(fws, entry);
940 941
			brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
					BRCMF_FWS_PSQ_LEN);
942
			brcmf_dbg(TRACE, "add %s mac %pM\n", entry->name, addr);
943 944 945 946 947
		} else {
			fws->stats.mac_update_failed++;
		}
	} else {
		if (entry != existing) {
948
			brcmf_dbg(TRACE, "copy mac %s\n", existing->name);
949 950 951 952
			memcpy(entry, existing,
			       offsetof(struct brcmf_fws_mac_descriptor, psq));
			entry->mac_handle = mac_handle;
			brcmf_fws_clear_mac_descriptor(existing);
953 954 955
			brcmf_fws_macdesc_set_name(fws, entry);
			brcmf_dbg(TRACE, "relocate %s mac %pM\n", entry->name,
				  addr);
956 957 958 959 960 961 962 963 964
		} else {
			brcmf_dbg(TRACE, "use existing\n");
			WARN_ON(entry->mac_handle != mac_handle);
			/* TODO: what should we do here: continue, reinit, .. */
		}
	}
	return 0;
}

965 966 967 968 969 970 971 972 973 974 975 976
static int brcmf_fws_macdesc_state_indicate(struct brcmf_fws_info *fws,
					    u8 type, u8 *data)
{
	struct brcmf_fws_mac_descriptor *entry;
	u8 mac_handle;

	mac_handle = data[0];
	entry = &fws->desc.nodes[mac_handle & 0x1F];
	if (!entry->occupied) {
		fws->stats.mac_ps_update_failed++;
		return -ESRCH;
	}
977
	/* a state update should wipe old credits */
978
	entry->requested_credit = 0;
979
	entry->requested_packet = 0;
980 981
	if (type == BRCMF_FWS_TYPE_MAC_OPEN) {
		entry->state = BRCMF_FWS_STATE_OPEN;
982
		return BRCMF_FWS_RET_OK_SCHEDULE;
983 984
	} else {
		entry->state = BRCMF_FWS_STATE_CLOSE;
985 986 987 988
		brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BK, false);
		brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_BE, false);
		brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VI, false);
		brcmf_fws_tim_update(fws, entry, BRCMF_FWS_FIFO_AC_VO, true);
989
	}
990
	return BRCMF_FWS_RET_OK_NOSCHEDULE;
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
}

static int brcmf_fws_interface_state_indicate(struct brcmf_fws_info *fws,
					      u8 type, u8 *data)
{
	struct brcmf_fws_mac_descriptor *entry;
	u8 ifidx;
	int ret;

	ifidx = data[0];

	if (ifidx >= BRCMF_MAX_IFS) {
		ret = -ERANGE;
		goto fail;
	}

	entry = &fws->desc.iface[ifidx];
	if (!entry->occupied) {
		ret = -ESRCH;
		goto fail;
	}

1013 1014
	brcmf_dbg(TRACE, "%s (%d): %s\n", brcmf_fws_get_tlv_name(type), type,
		  entry->name);
1015 1016 1017
	switch (type) {
	case BRCMF_FWS_TYPE_INTERFACE_OPEN:
		entry->state = BRCMF_FWS_STATE_OPEN;
1018
		return BRCMF_FWS_RET_OK_SCHEDULE;
1019 1020
	case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
		entry->state = BRCMF_FWS_STATE_CLOSE;
1021
		return BRCMF_FWS_RET_OK_NOSCHEDULE;
1022 1023 1024 1025 1026 1027 1028 1029 1030
	default:
		ret = -EINVAL;
		break;
	}
fail:
	fws->stats.if_update_failed++;
	return ret;
}

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
static int brcmf_fws_request_indicate(struct brcmf_fws_info *fws, u8 type,
				      u8 *data)
{
	struct brcmf_fws_mac_descriptor *entry;

	entry = &fws->desc.nodes[data[1] & 0x1F];
	if (!entry->occupied) {
		if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
			fws->stats.credit_request_failed++;
		else
			fws->stats.packet_request_failed++;
		return -ESRCH;
	}

1045 1046 1047
	brcmf_dbg(TRACE, "%s (%d): %s cnt %d bmp %d\n",
		  brcmf_fws_get_tlv_name(type), type, entry->name,
		  data[0], data[2]);
1048 1049 1050 1051 1052 1053
	if (type == BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT)
		entry->requested_credit = data[0];
	else
		entry->requested_packet = data[0];

	entry->ac_bitmap = data[2];
1054
	return BRCMF_FWS_RET_OK_SCHEDULE;
1055 1056
}

1057 1058 1059
static void
brcmf_fws_macdesc_use_req_credit(struct brcmf_fws_mac_descriptor *entry,
				 struct sk_buff *skb)
1060
{
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	if (entry->requested_credit > 0) {
		entry->requested_credit--;
		brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
		brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 1);
		if (entry->state != BRCMF_FWS_STATE_CLOSE)
			brcmf_err("requested credit set while mac not closed!\n");
	} else if (entry->requested_packet > 0) {
		entry->requested_packet--;
		brcmf_skb_if_flags_set_field(skb, REQUESTED, 1);
		brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
		if (entry->state != BRCMF_FWS_STATE_CLOSE)
			brcmf_err("requested packet set while mac not closed!\n");
1073
	} else {
1074 1075
		brcmf_skb_if_flags_set_field(skb, REQUESTED, 0);
		brcmf_skb_if_flags_set_field(skb, REQ_CREDIT, 0);
1076
	}
1077 1078 1079 1080 1081 1082 1083 1084 1085
}

static void brcmf_fws_macdesc_return_req_credit(struct sk_buff *skb)
{
	struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;

	if ((brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) &&
	    (entry->state == BRCMF_FWS_STATE_CLOSE))
		entry->requested_credit++;
1086 1087
}

1088 1089 1090
static void brcmf_fws_return_credits(struct brcmf_fws_info *fws,
				     u8 fifo, u8 credits)
{
1091 1092 1093 1094
	int lender_ac;
	int *borrowed;
	int *fifo_credit;

1095 1096 1097
	if (!credits)
		return;

1098 1099
	fws->fifo_credit_map |= 1 << fifo;

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
	if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
	    (fws->credits_borrowed[0])) {
		for (lender_ac = BRCMF_FWS_FIFO_AC_VO; lender_ac >= 0;
		     lender_ac--) {
			borrowed = &fws->credits_borrowed[lender_ac];
			if (*borrowed) {
				fws->fifo_credit_map |= (1 << lender_ac);
				fifo_credit = &fws->fifo_credit[lender_ac];
				if (*borrowed >= credits) {
					*borrowed -= credits;
					*fifo_credit += credits;
					return;
				} else {
					credits -= *borrowed;
					*fifo_credit += *borrowed;
					*borrowed = 0;
				}
			}
		}
	}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
	fws->fifo_credit[fifo] += credits;
}

static void brcmf_fws_schedule_deq(struct brcmf_fws_info *fws)
{
	/* only schedule dequeue when there are credits for delayed traffic */
	if (fws->fifo_credit_map & fws->fifo_delay_map)
		queue_work(fws->fws_wq, &fws->fws_dequeue_work);
}

static int brcmf_fws_enq(struct brcmf_fws_info *fws,
			 enum brcmf_fws_skb_state state, int fifo,
			 struct sk_buff *p)
{
	int prec = 2 * fifo;
	u32 *qfull_stat = &fws->stats.delayq_full_error;

	struct brcmf_fws_mac_descriptor *entry;

	entry = brcmf_skbcb(p)->mac;
	if (entry == NULL) {
		brcmf_err("no mac descriptor found for skb %p\n", p);
		return -ENOENT;
	}

1146
	brcmf_dbg(DATA, "enter: fifo %d skb %p\n", fifo, p);
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
		prec += 1;
		qfull_stat = &fws->stats.supprq_full_error;
	}

	if (brcmu_pktq_penq(&entry->psq, prec, p) == NULL) {
		*qfull_stat += 1;
		return -ENFILE;
	}

	/* increment total enqueued packet count */
	fws->fifo_delay_map |= 1 << fifo;
	fws->fifo_enqpkt[fifo]++;

	/* update the sk_buff state */
	brcmf_skbcb(p)->state = state;

	/*
	 * A packet has been pushed so update traffic
	 * availability bitmap, if applicable
	 */
1168
	brcmf_fws_tim_update(fws, entry, fifo, true);
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
	brcmf_fws_flow_control_check(fws, &entry->psq,
				     brcmf_skb_if_flags_get_field(p, INDEX));
	return 0;
}

static struct sk_buff *brcmf_fws_deq(struct brcmf_fws_info *fws, int fifo)
{
	struct brcmf_fws_mac_descriptor *table;
	struct brcmf_fws_mac_descriptor *entry;
	struct sk_buff *p;
	int num_nodes;
	int node_pos;
	int prec_out;
1182
	int pmsk;
1183 1184 1185 1186 1187 1188 1189 1190
	int i;

	table = (struct brcmf_fws_mac_descriptor *)&fws->desc;
	num_nodes = sizeof(fws->desc) / sizeof(struct brcmf_fws_mac_descriptor);
	node_pos = fws->deq_node_pos[fifo];

	for (i = 0; i < num_nodes; i++) {
		entry = &table[(node_pos + i) % num_nodes];
1191 1192
		if (!entry->occupied ||
		    brcmf_fws_mac_desc_closed(fws, entry, fifo))
1193 1194 1195 1196
			continue;

		if (entry->suppressed)
			pmsk = 2;
1197 1198
		else
			pmsk = 3;
1199 1200 1201
		p = brcmu_pktq_mdeq(&entry->psq, pmsk << (fifo * 2), &prec_out);
		if (p == NULL) {
			if (entry->suppressed) {
1202 1203
				if (entry->suppr_transit_count)
					continue;
1204 1205 1206 1207 1208 1209 1210 1211
				entry->suppressed = false;
				p = brcmu_pktq_mdeq(&entry->psq,
						    1 << (fifo * 2), &prec_out);
			}
		}
		if  (p == NULL)
			continue;

1212
		brcmf_fws_macdesc_use_req_credit(entry, p);
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

		/* move dequeue position to ensure fair round-robin */
		fws->deq_node_pos[fifo] = (node_pos + i + 1) % num_nodes;
		brcmf_fws_flow_control_check(fws, &entry->psq,
					     brcmf_skb_if_flags_get_field(p,
									  INDEX)
					     );
		/*
		 * A packet has been picked up, update traffic
		 * availability bitmap, if applicable
		 */
1224
		brcmf_fws_tim_update(fws, entry, fifo, false);
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

		/*
		 * decrement total enqueued fifo packets and
		 * clear delay bitmap if done.
		 */
		fws->fifo_enqpkt[fifo]--;
		if (fws->fifo_enqpkt[fifo] == 0)
			fws->fifo_delay_map &= ~(1 << fifo);
		goto done;
	}
	p = NULL;
done:
1237
	brcmf_dbg(DATA, "exit: fifo %d skb %p\n", fifo, p);
1238 1239 1240 1241 1242 1243 1244 1245 1246
	return p;
}

static int brcmf_fws_txstatus_suppressed(struct brcmf_fws_info *fws, int fifo,
					 struct sk_buff *skb, u32 genbit)
{
	struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
	u32 hslot;
	int ret;
1247
	u8 ifidx;
1248 1249 1250 1251

	hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);

	/* this packet was suppressed */
1252
	if (!entry->suppressed) {
1253 1254
		entry->suppressed = true;
		entry->suppr_transit_count = entry->transit_count;
1255 1256
		brcmf_dbg(DATA, "suppress %s: transit %d\n",
			  entry->name, entry->transit_count);
1257 1258 1259 1260
	}

	entry->generation = genbit;

1261 1262 1263 1264
	ret = brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb);
	if (ret == 0)
		ret = brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_SUPPRESSED, fifo,
				    skb);
1265
	if (ret != 0) {
1266
		/* suppress q is full or hdrpull failed, drop this packet */
1267 1268 1269 1270 1271 1272 1273
		brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
					true);
	} else {
		/*
		 * Mark suppressed to avoid a double free during
		 * wlfc cleanup
		 */
1274
		brcmf_fws_hanger_mark_suppressed(&fws->hanger, hslot);
1275 1276 1277 1278 1279
	}

	return ret;
}

1280
static int
1281
brcmf_fws_txs_process(struct brcmf_fws_info *fws, u8 flags, u32 hslot,
1282
			   u32 genbit)
1283
{
1284
	u32 fifo;
1285
	int ret;
1286
	bool remove_from_hanger = true;
1287
	struct sk_buff *skb;
1288
	struct brcmf_skbuff_cb *skcb;
1289 1290
	struct brcmf_fws_mac_descriptor *entry = NULL;

1291
	brcmf_dbg(DATA, "flags %d\n", flags);
1292 1293 1294

	if (flags == BRCMF_FWS_TXSTATUS_DISCARD)
		fws->stats.txs_discard++;
1295 1296 1297 1298 1299 1300
	else if (flags == BRCMF_FWS_TXSTATUS_CORE_SUPPRESS) {
		fws->stats.txs_supp_core++;
		remove_from_hanger = false;
	} else if (flags == BRCMF_FWS_TXSTATUS_FW_PS_SUPPRESS) {
		fws->stats.txs_supp_ps++;
		remove_from_hanger = false;
1301
	} else if (flags == BRCMF_FWS_TXSTATUS_FW_TOSSED)
1302
		fws->stats.txs_tossed++;
1303 1304
	else if (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)
		fws->stats.txs_host_tossed++;
1305 1306 1307 1308
	else
		brcmf_err("unexpected txstatus\n");

	ret = brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &skb,
1309
				      remove_from_hanger);
1310 1311 1312 1313 1314
	if (ret != 0) {
		brcmf_err("no packet in hanger slot: hslot=%d\n", hslot);
		return ret;
	}

1315 1316
	skcb = brcmf_skbcb(skb);
	entry = skcb->mac;
1317
	if (WARN_ON(!entry)) {
1318 1319
		brcmu_pkt_buf_free_skb(skb);
		return -EINVAL;
1320
	}
1321 1322 1323
	entry->transit_count--;
	if (entry->suppressed && entry->suppr_transit_count)
		entry->suppr_transit_count--;
1324

1325 1326 1327
	brcmf_dbg(DATA, "%s flags %X htod %X\n", entry->name, skcb->if_flags,
		  skcb->htod);

1328 1329
	/* pick up the implicit credit from this packet */
	fifo = brcmf_skb_htod_tag_get_field(skb, FIFO);
1330 1331 1332 1333 1334 1335 1336
	if ((fws->fcmode == BRCMF_FWS_FCMODE_IMPLIED_CREDIT) ||
	    (brcmf_skb_if_flags_get_field(skb, REQ_CREDIT)) ||
	    (flags == BRCMF_FWS_TXSTATUS_HOST_TOSSED)) {
		brcmf_fws_return_credits(fws, fifo, 1);
		brcmf_fws_schedule_deq(fws);
	}
	brcmf_fws_macdesc_return_req_credit(skb);
1337

1338 1339 1340
	if (!remove_from_hanger)
		ret = brcmf_fws_txstatus_suppressed(fws, fifo, skb, genbit);

1341
	if (remove_from_hanger || ret)
1342
		brcmf_txfinalize(fws->drvr, skb, true);
1343

1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	return 0;
}

static int brcmf_fws_fifocreditback_indicate(struct brcmf_fws_info *fws,
					     u8 *data)
{
	int i;

	if (fws->fcmode != BRCMF_FWS_FCMODE_EXPLICIT_CREDIT) {
		brcmf_dbg(INFO, "ignored\n");
1354
		return BRCMF_FWS_RET_OK_NOSCHEDULE;
1355 1356
	}

1357
	brcmf_dbg(DATA, "enter: data %pM\n", data);
1358 1359 1360
	for (i = 0; i < BRCMF_FWS_FIFO_COUNT; i++)
		brcmf_fws_return_credits(fws, i, data[i]);

1361
	brcmf_dbg(DATA, "map: credit %x delay %x\n", fws->fifo_credit_map,
1362
		  fws->fifo_delay_map);
1363
	return BRCMF_FWS_RET_OK_SCHEDULE;
1364 1365
}

1366 1367
static int brcmf_fws_txstatus_indicate(struct brcmf_fws_info *fws, u8 *data)
{
1368
	__le32 status_le;
1369 1370
	u32 status;
	u32 hslot;
1371
	u32 genbit;
1372 1373 1374
	u8 flags;

	fws->stats.txs_indicate++;
1375 1376
	memcpy(&status_le, data, sizeof(status_le));
	status = le32_to_cpu(status_le);
1377 1378
	flags = brcmf_txstatus_get_field(status, FLAGS);
	hslot = brcmf_txstatus_get_field(status, HSLOT);
1379
	genbit = brcmf_txstatus_get_field(status, GENERATION);
1380

1381
	return brcmf_fws_txs_process(fws, flags, hslot, genbit);
1382 1383
}

1384 1385 1386 1387 1388
static int brcmf_fws_dbg_seqnum_check(struct brcmf_fws_info *fws, u8 *data)
{
	__le32 timestamp;

	memcpy(&timestamp, &data[2], sizeof(timestamp));
1389
	brcmf_dbg(CTL, "received: seq %d, timestamp %d\n", data[1],
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
		  le32_to_cpu(timestamp));
	return 0;
}

/* using macro so sparse checking does not complain
 * about locking imbalance.
 */
#define brcmf_fws_lock(drvr, flags)				\
do {								\
	flags = 0;						\
	spin_lock_irqsave(&((drvr)->fws_spinlock), (flags));	\
} while (0)

/* using macro so sparse checking does not complain
 * about locking imbalance.
 */
#define brcmf_fws_unlock(drvr, flags) \
	spin_unlock_irqrestore(&((drvr)->fws_spinlock), (flags))

1409 1410 1411 1412 1413 1414 1415 1416 1417
static int brcmf_fws_notify_credit_map(struct brcmf_if *ifp,
				       const struct brcmf_event_msg *e,
				       void *data)
{
	struct brcmf_fws_info *fws = ifp->drvr->fws;
	int i;
	ulong flags;
	u8 *credits = data;

1418 1419 1420 1421
	if (e->datalen < BRCMF_FWS_FIFO_COUNT) {
		brcmf_err("event payload too small (%d)\n", e->datalen);
		return -EINVAL;
	}
1422 1423 1424 1425
	if (fws->creditmap_received)
		return 0;

	fws->creditmap_received = true;
1426 1427

	brcmf_dbg(TRACE, "enter: credits %pM\n", credits);
1428
	brcmf_fws_lock(ifp->drvr, flags);
1429 1430 1431 1432 1433
	for (i = 0; i < ARRAY_SIZE(fws->fifo_credit); i++) {
		if (*credits)
			fws->fifo_credit_map |= 1 << i;
		else
			fws->fifo_credit_map &= ~(1 << i);
1434
		fws->fifo_credit[i] = *credits++;
1435 1436
	}
	brcmf_fws_schedule_deq(fws);
1437 1438 1439 1440
	brcmf_fws_unlock(ifp->drvr, flags);
	return 0;
}

1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
static int brcmf_fws_notify_bcmc_credit_support(struct brcmf_if *ifp,
						const struct brcmf_event_msg *e,
						void *data)
{
	struct brcmf_fws_info *fws = ifp->drvr->fws;
	ulong flags;

	brcmf_fws_lock(ifp->drvr, flags);
	if (fws)
		fws->bcmc_credit_check = true;
	brcmf_fws_unlock(ifp->drvr, flags);
	return 0;
}

1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
int brcmf_fws_hdrpull(struct brcmf_pub *drvr, int ifidx, s16 signal_len,
		      struct sk_buff *skb)
{
	struct brcmf_fws_info *fws = drvr->fws;
	ulong flags;
	u8 *signal_data;
	s16 data_len;
	u8 type;
	u8 len;
	u8 *data;
1465 1466
	s32 status;
	s32 err;
1467

1468
	brcmf_dbg(HDRS, "enter: ifidx %d, skblen %u, sig %d\n",
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
		  ifidx, skb->len, signal_len);

	WARN_ON(signal_len > skb->len);

	/* if flow control disabled, skip to packet data and leave */
	if (!signal_len || !drvr->fw_signals) {
		skb_pull(skb, signal_len);
		return 0;
	}

	/* lock during tlv parsing */
	brcmf_fws_lock(drvr, flags);

	fws->stats.header_pulls++;
	data_len = signal_len;
	signal_data = skb->data;

1486
	status = BRCMF_FWS_RET_OK_NOSCHEDULE;
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
	while (data_len > 0) {
		/* extract tlv info */
		type = signal_data[0];

		/* FILLER type is actually not a TLV, but
		 * a single byte that can be skipped.
		 */
		if (type == BRCMF_FWS_TYPE_FILLER) {
			signal_data += 1;
			data_len -= 1;
			continue;
		}
		len = signal_data[1];
		data = signal_data + 2;

1502 1503 1504
		brcmf_dbg(HDRS, "tlv type=%s (%d), len=%d (%d)\n",
			  brcmf_fws_get_tlv_name(type), type, len,
			  brcmf_fws_get_tlv_len(fws, type));
1505

1506 1507 1508 1509
		/* abort parsing when length invalid */
		if (data_len < len + 2)
			break;

1510
		if (len < brcmf_fws_get_tlv_len(fws, type))
1511 1512
			break;

1513
		err = BRCMF_FWS_RET_OK_NOSCHEDULE;
1514 1515
		switch (type) {
		case BRCMF_FWS_TYPE_HOST_REORDER_RXPKTS:
1516
		case BRCMF_FWS_TYPE_COMP_TXSTATUS:
1517
			break;
1518 1519
		case BRCMF_FWS_TYPE_MACDESC_ADD:
		case BRCMF_FWS_TYPE_MACDESC_DEL:
1520
			brcmf_fws_macdesc_indicate(fws, type, data);
1521
			break;
1522 1523
		case BRCMF_FWS_TYPE_MAC_OPEN:
		case BRCMF_FWS_TYPE_MAC_CLOSE:
1524
			err = brcmf_fws_macdesc_state_indicate(fws, type, data);
1525 1526 1527
			break;
		case BRCMF_FWS_TYPE_INTERFACE_OPEN:
		case BRCMF_FWS_TYPE_INTERFACE_CLOSE:
1528 1529
			err = brcmf_fws_interface_state_indicate(fws, type,
								 data);
1530
			break;
1531 1532
		case BRCMF_FWS_TYPE_MAC_REQUEST_CREDIT:
		case BRCMF_FWS_TYPE_MAC_REQUEST_PACKET:
1533
			err = brcmf_fws_request_indicate(fws, type, data);
1534
			break;
1535 1536 1537
		case BRCMF_FWS_TYPE_TXSTATUS:
			brcmf_fws_txstatus_indicate(fws, data);
			break;
1538
		case BRCMF_FWS_TYPE_FIFO_CREDITBACK:
1539
			err = brcmf_fws_fifocreditback_indicate(fws, data);
1540
			break;
1541 1542
		case BRCMF_FWS_TYPE_RSSI:
			brcmf_fws_rssi_indicate(fws, *data);
1543 1544 1545 1546
			break;
		case BRCMF_FWS_TYPE_TRANS_ID:
			brcmf_fws_dbg_seqnum_check(fws, data);
			break;
1547 1548
		case BRCMF_FWS_TYPE_PKTTAG:
		case BRCMF_FWS_TYPE_PENDING_TRAFFIC_BMP:
1549 1550 1551 1552
		default:
			fws->stats.tlv_invalid_type++;
			break;
		}
1553 1554
		if (err == BRCMF_FWS_RET_OK_SCHEDULE)
			status = BRCMF_FWS_RET_OK_SCHEDULE;
1555 1556 1557 1558 1559 1560 1561
		signal_data += len + 2;
		data_len -= len + 2;
	}

	if (data_len != 0)
		fws->stats.tlv_parse_failed++;

1562 1563 1564
	if (status == BRCMF_FWS_RET_OK_SCHEDULE)
		brcmf_fws_schedule_deq(fws);

1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	/* signalling processing result does
	 * not affect the actual ethernet packet.
	 */
	skb_pull(skb, signal_len);

	/* this may be a signal-only packet
	 */
	if (skb->len == 0)
		fws->stats.header_only_pkt++;

	brcmf_fws_unlock(drvr, flags);
	return 0;
}
1578

1579 1580 1581 1582 1583 1584
static int brcmf_fws_precommit_skb(struct brcmf_fws_info *fws, int fifo,
				   struct sk_buff *p)
{
	struct brcmf_skbuff_cb *skcb = brcmf_skbcb(p);
	struct brcmf_fws_mac_descriptor *entry = skcb->mac;
	int rc = 0;
1585
	bool first_time;
1586 1587 1588 1589
	int hslot = BRCMF_FWS_HANGER_MAXITEMS;
	u8 free_ctr;
	u8 flags;

1590
	first_time = skcb->state != BRCMF_FWS_SKBSTATE_SUPPRESSED;
1591

1592 1593 1594 1595
	brcmf_skb_if_flags_set_field(p, TRANSMIT, 1);
	brcmf_skb_htod_tag_set_field(p, FIFO, fifo);
	brcmf_skb_htod_tag_set_field(p, GENERATION, entry->generation);
	flags = BRCMF_FWS_HTOD_FLAG_PKTFROMHOST;
1596
	if (brcmf_skb_if_flags_get_field(p, REQUESTED)) {
1597 1598 1599 1600 1601 1602 1603 1604
		/*
		 * Indicate that this packet is being sent in response to an
		 * explicit request from the firmware side.
		 */
		flags |= BRCMF_FWS_HTOD_FLAG_PKT_REQUESTED;
	}
	brcmf_skb_htod_tag_set_field(p, FLAGS, flags);
	if (first_time) {
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
		/* obtaining free slot may fail, but that will be caught
		 * by the hanger push. This assures the packet has a BDC
		 * header upon return.
		 */
		hslot = brcmf_fws_hanger_get_free_slot(&fws->hanger);
		free_ctr = entry->seq[fifo];
		brcmf_skb_htod_tag_set_field(p, HSLOT, hslot);
		brcmf_skb_htod_tag_set_field(p, FREERUN, free_ctr);
		rc = brcmf_fws_hanger_pushpkt(&fws->hanger, p, hslot);
		if (rc)
			brcmf_err("hanger push failed: rc=%d\n", rc);
	}

1618 1619 1620
	if (rc == 0)
		brcmf_fws_hdrpush(fws, p);

1621 1622 1623
	return rc;
}

1624
static void
1625 1626
brcmf_fws_rollback_toq(struct brcmf_fws_info *fws,
		       struct sk_buff *skb, int fifo)
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
{
	/*
	put the packet back to the head of queue

	- suppressed packet goes back to suppress sub-queue
	- pull out the header, if new or delayed packet

	Note: hslot is used only when header removal is done.
	*/
	struct brcmf_fws_mac_descriptor *entry;
	enum brcmf_fws_skb_state state;
	struct sk_buff *pktout;
	int rc = 0;
	int hslot;

	state = brcmf_skbcb(skb)->state;
	entry = brcmf_skbcb(skb)->mac;
1644
	hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675

	if (entry != NULL) {
		if (state == BRCMF_FWS_SKBSTATE_SUPPRESSED) {
			/* wl-header is saved for suppressed packets */
			pktout = brcmu_pktq_penq_head(&entry->psq, 2 * fifo + 1,
						      skb);
			if (pktout == NULL) {
				brcmf_err("suppress queue full\n");
				rc = -ENOSPC;
			}
		} else {
			/* delay-q packets are going to delay-q */
			pktout = brcmu_pktq_penq_head(&entry->psq,
						      2 * fifo, skb);
			if (pktout == NULL) {
				brcmf_err("delay queue full\n");
				rc = -ENOSPC;
			}

			/* free the hanger slot */
			brcmf_fws_hanger_poppkt(&fws->hanger, hslot, &pktout,
						true);

			/* decrement sequence count */
			entry->seq[fifo]--;
		}
	} else {
		brcmf_err("no mac entry linked\n");
		rc = -ENOENT;
	}

1676
	if (rc) {
1677
		fws->stats.rollback_failed++;
1678 1679
		brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED,
				      hslot, 0);
1680
	} else {
1681
		fws->stats.rollback_success++;
1682 1683
		brcmf_fws_return_credits(fws, fifo, 1);
		brcmf_fws_macdesc_return_req_credit(skb);
1684
	}
1685 1686
}

1687 1688 1689 1690
static int brcmf_fws_borrow_credit(struct brcmf_fws_info *fws)
{
	int lender_ac;

1691 1692
	if (time_after(fws->borrow_defer_timestamp, jiffies)) {
		fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
1693
		return -ENAVAIL;
1694
	}
1695 1696 1697 1698 1699 1700 1701

	for (lender_ac = 0; lender_ac <= BRCMF_FWS_FIFO_AC_VO; lender_ac++) {
		if (fws->fifo_credit[lender_ac]) {
			fws->credits_borrowed[lender_ac]++;
			fws->fifo_credit[lender_ac]--;
			if (fws->fifo_credit[lender_ac] == 0)
				fws->fifo_credit_map &= ~(1 << lender_ac);
1702
			fws->fifo_credit_map |= (1 << BRCMF_FWS_FIFO_AC_BE);
1703
			brcmf_dbg(DATA, "borrow credit from: %d\n", lender_ac);
1704 1705 1706
			return 0;
		}
	}
1707
	fws->fifo_credit_map &= ~(1 << BRCMF_FWS_FIFO_AC_BE);
1708 1709 1710
	return -ENAVAIL;
}

1711 1712 1713 1714 1715 1716
static int brcmf_fws_consume_credit(struct brcmf_fws_info *fws, int fifo,
				    struct sk_buff *skb)
{
	struct brcmf_fws_mac_descriptor *entry = brcmf_skbcb(skb)->mac;
	int *credit = &fws->fifo_credit[fifo];

1717 1718 1719 1720
	if (fifo != BRCMF_FWS_FIFO_AC_BE)
		fws->borrow_defer_timestamp = jiffies +
					      BRCMF_FWS_BORROW_DEFER_PERIOD;

1721
	if (!(*credit)) {
1722
		/* Try to borrow a credit from other queue */
1723 1724 1725 1726 1727 1728 1729 1730 1731
		if (fifo != BRCMF_FWS_FIFO_AC_BE ||
		    (brcmf_fws_borrow_credit(fws) != 0)) {
			brcmf_dbg(DATA, "ac=%d, credits depleted\n", fifo);
			return -ENAVAIL;
		}
	} else {
		(*credit)--;
		if (!(*credit))
			fws->fifo_credit_map &= ~(1 << fifo);
1732
	}
1733 1734 1735 1736 1737 1738

	brcmf_fws_macdesc_use_req_credit(entry, skb);

	brcmf_dbg(DATA, "ac=%d, credits=%02d:%02d:%02d:%02d\n", fifo,
		  fws->fifo_credit[0], fws->fifo_credit[1],
		  fws->fifo_credit[2], fws->fifo_credit[3]);
1739 1740 1741
	return 0;
}

1742 1743 1744 1745 1746 1747 1748
static int brcmf_fws_commit_skb(struct brcmf_fws_info *fws, int fifo,
				struct sk_buff *skb)
{
	struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
	struct brcmf_fws_mac_descriptor *entry;
	struct brcmf_bus *bus = fws->drvr->bus_if;
	int rc;
1749
	u8 ifidx;
1750 1751 1752 1753 1754 1755 1756 1757

	entry = skcb->mac;
	if (IS_ERR(entry))
		return PTR_ERR(entry);

	rc = brcmf_fws_precommit_skb(fws, fifo, skb);
	if (rc < 0) {
		fws->stats.generic_error++;
1758
		goto rollback;
1759 1760
	}

1761 1762
	brcmf_dbg(DATA, "%s flags %X htod %X\n", entry->name, skcb->if_flags,
		  skcb->htod);
1763
	rc = brcmf_bus_txdata(bus, skb);
1764 1765
	if (rc < 0) {
		brcmf_proto_hdrpull(fws->drvr, false, &ifidx, skb);
1766
		goto rollback;
1767
	}
1768

1769 1770 1771
	entry->transit_count++;
	if (entry->suppressed)
		entry->suppr_transit_count++;
1772 1773
	entry->seq[fifo]++;
	fws->stats.pkt2bus++;
1774
	fws->stats.send_pkts[fifo]++;
1775 1776
	if (brcmf_skb_if_flags_get_field(skb, REQUESTED))
		fws->stats.requested_sent[fifo]++;
1777

1778 1779
	return rc;

1780
rollback:
1781
	brcmf_fws_rollback_toq(fws, skb, fifo);
1782 1783 1784 1785 1786 1787
	return rc;
}

int brcmf_fws_process_skb(struct brcmf_if *ifp, struct sk_buff *skb)
{
	struct brcmf_pub *drvr = ifp->drvr;
1788
	struct brcmf_fws_info *fws = drvr->fws;
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
	struct brcmf_skbuff_cb *skcb = brcmf_skbcb(skb);
	struct ethhdr *eh = (struct ethhdr *)(skb->data);
	ulong flags;
	int fifo = BRCMF_FWS_FIFO_BCMC;
	bool multicast = is_multicast_ether_addr(eh->h_dest);

	/* determine the priority */
	if (!skb->priority)
		skb->priority = cfg80211_classify8021d(skb);

	drvr->tx_multicast += !!multicast;
	if (ntohs(eh->h_proto) == ETH_P_PAE)
		atomic_inc(&ifp->pend_8021x_cnt);

1803
	if (!brcmf_fws_fc_active(fws)) {
1804
		/* If the protocol uses a data header, apply it */
1805
		brcmf_proto_hdrpush(drvr, ifp->ifidx, 0, skb);
1806 1807 1808 1809 1810 1811

		/* Use bus module to send data frame */
		return brcmf_bus_txdata(drvr->bus_if, skb);
	}

	/* set control buffer information */
1812
	skcb->if_flags = 0;
1813
	skcb->mac = brcmf_fws_find_mac_desc(fws, ifp, eh->h_dest);
1814
	skcb->state = BRCMF_FWS_SKBSTATE_NEW;
1815
	brcmf_skb_if_flags_set_field(skb, INDEX, ifp->ifidx);
1816 1817 1818
	if (!multicast)
		fifo = brcmf_fws_prio2fifo[skb->priority];

1819 1820
	brcmf_dbg(DATA, "%s mac %pM multi %d fifo %d\n", skcb->mac->name,
		  eh->h_dest, multicast, fifo);
1821 1822

	brcmf_fws_lock(drvr, flags);
1823 1824 1825 1826 1827 1828
	/* multicast credit support is conditional, setting
	 * flag to false to assure credit is consumed below.
	 */
	if (fws->bcmc_credit_check)
		multicast = false;

1829
	if (skcb->mac->suppressed ||
1830
	    fws->bus_flow_blocked ||
1831
	    brcmf_fws_mac_desc_closed(fws, skcb->mac, fifo) ||
1832
	    brcmu_pktq_mlen(&skcb->mac->psq, 3 << (fifo * 2)) ||
1833
	    (!multicast &&
1834
	     brcmf_fws_consume_credit(fws, fifo, skb) < 0)) {
1835 1836
		/* enqueue the packet in delayQ */
		drvr->fws->fifo_delay_map |= 1 << fifo;
1837
		brcmf_fws_enq(fws, BRCMF_FWS_SKBSTATE_DELAYED, fifo, skb);
1838
	} else {
1839
		brcmf_fws_commit_skb(fws, fifo, skb);
1840
	}
1841 1842 1843 1844
	brcmf_fws_unlock(drvr, flags);
	return 0;
}

1845 1846 1847 1848 1849 1850 1851 1852
void brcmf_fws_reset_interface(struct brcmf_if *ifp)
{
	struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;

	brcmf_dbg(TRACE, "enter: idx=%d\n", ifp->bssidx);
	if (!entry)
		return;

1853
	brcmf_fws_init_mac_descriptor(entry, ifp->mac_addr, ifp->ifidx);
1854 1855 1856 1857
}

void brcmf_fws_add_interface(struct brcmf_if *ifp)
{
1858
	struct brcmf_fws_info *fws = ifp->drvr->fws;
1859 1860
	struct brcmf_fws_mac_descriptor *entry;

1861
	if (!ifp->ndev || !ifp->drvr->fw_signals)
1862 1863
		return;

1864 1865 1866
	entry = &fws->desc.iface[ifp->ifidx];
	ifp->fws_desc = entry;
	brcmf_fws_init_mac_descriptor(entry, ifp->mac_addr, ifp->ifidx);
1867
	brcmf_fws_macdesc_set_name(fws, entry);
1868 1869
	brcmu_pktq_init(&entry->psq, BRCMF_FWS_PSQ_PREC_COUNT,
			BRCMF_FWS_PSQ_LEN);
1870
	brcmf_dbg(TRACE, "added %s\n", entry->name);
1871 1872 1873 1874 1875
}

void brcmf_fws_del_interface(struct brcmf_if *ifp)
{
	struct brcmf_fws_mac_descriptor *entry = ifp->fws_desc;
1876
	ulong flags;
1877 1878 1879 1880

	if (!entry)
		return;

1881
	brcmf_fws_lock(ifp->drvr, flags);
1882
	ifp->fws_desc = NULL;
1883
	brcmf_dbg(TRACE, "deleting %s\n", entry->name);
1884
	brcmf_fws_clear_mac_descriptor(entry);
1885
	brcmf_fws_cleanup(ifp->drvr->fws, ifp->ifidx);
1886
	brcmf_fws_unlock(ifp->drvr, flags);
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
}

static void brcmf_fws_dequeue_worker(struct work_struct *worker)
{
	struct brcmf_fws_info *fws;
	struct sk_buff *skb;
	ulong flags;
	int fifo;

	fws = container_of(worker, struct brcmf_fws_info, fws_dequeue_work);

	brcmf_fws_lock(fws->drvr, flags);
1899 1900
	for (fifo = NL80211_NUM_ACS; fifo >= 0 && !fws->bus_flow_blocked;
	     fifo--) {
1901 1902
		while ((fws->fifo_credit[fifo]) || ((!fws->bcmc_credit_check) &&
		       (fifo == BRCMF_FWS_FIFO_BCMC))) {
1903
			skb = brcmf_fws_deq(fws, fifo);
1904
			if (!skb)
1905
				break;
1906
			fws->fifo_credit[fifo]--;
1907 1908
			if (brcmf_fws_commit_skb(fws, fifo, skb))
				break;
1909 1910
			if (fws->bus_flow_blocked)
				break;
1911
		}
1912
		if ((fifo == BRCMF_FWS_FIFO_AC_BE) &&
1913
		    (fws->fifo_credit[fifo] == 0) &&
1914
		    (!fws->bus_flow_blocked)) {
1915 1916 1917 1918 1919 1920
			while (brcmf_fws_borrow_credit(fws) == 0) {
				skb = brcmf_fws_deq(fws, fifo);
				if (!skb) {
					brcmf_fws_return_credits(fws, fifo, 1);
					break;
				}
1921 1922
				if (brcmf_fws_commit_skb(fws, fifo, skb))
					break;
1923 1924
				if (fws->bus_flow_blocked)
					break;
1925 1926
			}
		}
1927 1928
	}
	brcmf_fws_unlock(fws->drvr, flags);
1929
}
1930

1931 1932
int brcmf_fws_init(struct brcmf_pub *drvr)
{
1933
	u32 tlv = BRCMF_FWS_FLAGS_RSSI_SIGNALS;
1934 1935
	int rc;

1936 1937 1938
	if (!drvr->fw_signals)
		return 0;

1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
	spin_lock_init(&drvr->fws_spinlock);

	drvr->fws = kzalloc(sizeof(*(drvr->fws)), GFP_KERNEL);
	if (!drvr->fws) {
		rc = -ENOMEM;
		goto fail;
	}

	/* set linkage back */
	drvr->fws->drvr = drvr;
	drvr->fws->fcmode = fcmode;

1951 1952 1953 1954 1955 1956 1957 1958
	drvr->fws->fws_wq = create_singlethread_workqueue("brcmf_fws_wq");
	if (drvr->fws->fws_wq == NULL) {
		brcmf_err("workqueue creation failed\n");
		rc = -EBADF;
		goto fail;
	}
	INIT_WORK(&drvr->fws->fws_dequeue_work, brcmf_fws_dequeue_worker);

1959 1960 1961
	/* enable firmware signalling if fcmode active */
	if (drvr->fws->fcmode != BRCMF_FWS_FCMODE_NONE)
		tlv |= BRCMF_FWS_FLAGS_XONXOFF_SIGNALS |
1962 1963
		       BRCMF_FWS_FLAGS_CREDIT_STATUS_SIGNALS |
		       BRCMF_FWS_FLAGS_HOST_PROPTXSTATUS_ACTIVE;
1964

1965 1966
	rc = brcmf_fweh_register(drvr, BRCMF_E_FIFO_CREDIT_MAP,
				 brcmf_fws_notify_credit_map);
1967
	if (rc < 0) {
1968
		brcmf_err("register credit map handler failed\n");
1969 1970
		goto fail;
	}
1971 1972 1973 1974 1975 1976 1977
	rc = brcmf_fweh_register(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT,
				 brcmf_fws_notify_bcmc_credit_support);
	if (rc < 0) {
		brcmf_err("register bcmc credit handler failed\n");
		brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
		goto fail;
	}
1978

1979 1980 1981 1982 1983 1984 1985
	/* setting the iovar may fail if feature is unsupported
	 * so leave the rc as is so driver initialization can
	 * continue.
	 */
	if (brcmf_fil_iovar_int_set(drvr->iflist[0], "tlv", tlv)) {
		brcmf_err("failed to set bdcv2 tlv signaling\n");
		goto fail_event;
1986 1987 1988
	}

	brcmf_fws_hanger_init(&drvr->fws->hanger);
1989
	brcmf_fws_init_mac_descriptor(&drvr->fws->desc.other, NULL, 0);
1990
	brcmf_fws_macdesc_set_name(drvr->fws, &drvr->fws->desc.other);
1991 1992
	brcmu_pktq_init(&drvr->fws->desc.other.psq, BRCMF_FWS_PSQ_PREC_COUNT,
			BRCMF_FWS_PSQ_LEN);
1993 1994 1995 1996

	/* create debugfs file for statistics */
	brcmf_debugfs_create_fws_stats(drvr, &drvr->fws->stats);

1997
	brcmf_dbg(INFO, "%s bdcv2 tlv signaling [%x]\n",
1998 1999 2000
		  drvr->fw_signals ? "enabled" : "disabled", tlv);
	return 0;

2001
fail_event:
2002
	brcmf_fweh_unregister(drvr, BRCMF_E_BCMC_CREDIT_SUPPORT);
2003
	brcmf_fweh_unregister(drvr, BRCMF_E_FIFO_CREDIT_MAP);
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
fail:
	brcmf_fws_deinit(drvr);
	return rc;
}

void brcmf_fws_deinit(struct brcmf_pub *drvr)
{
	struct brcmf_fws_info *fws = drvr->fws;
	ulong flags;

2014 2015 2016
	if (!fws)
		return;

2017 2018 2019 2020 2021 2022 2023 2024
	/* disable firmware signalling entirely
	 * to avoid using the workqueue.
	 */
	drvr->fw_signals = false;

	if (drvr->fws->fws_wq)
		destroy_workqueue(drvr->fws->fws_wq);

2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
	/* cleanup */
	brcmf_fws_lock(drvr, flags);
	brcmf_fws_cleanup(fws, -1);
	drvr->fws = NULL;
	brcmf_fws_unlock(drvr, flags);

	/* free top structure */
	kfree(fws);
}

2035 2036 2037 2038 2039 2040 2041
bool brcmf_fws_fc_active(struct brcmf_fws_info *fws)
{
	if (!fws)
		return false;

	return fws->fcmode != BRCMF_FWS_FCMODE_NONE;
}
2042 2043 2044 2045

void brcmf_fws_bustxfail(struct brcmf_fws_info *fws, struct sk_buff *skb)
{
	ulong flags;
2046
	u32 hslot;
2047

2048 2049 2050 2051
	if (brcmf_skbcb(skb)->state == BRCMF_FWS_SKBSTATE_TIM) {
		brcmu_pkt_buf_free_skb(skb);
		return;
	}
2052
	brcmf_fws_lock(fws->drvr, flags);
2053 2054
	hslot = brcmf_skb_htod_tag_get_field(skb, HSLOT);
	brcmf_fws_txs_process(fws, BRCMF_FWS_TXSTATUS_HOST_TOSSED, hslot, 0);
2055 2056
	brcmf_fws_unlock(fws->drvr, flags);
}
2057 2058 2059 2060 2061 2062 2063 2064

void brcmf_fws_bus_blocked(struct brcmf_pub *drvr, bool flow_blocked)
{
	struct brcmf_fws_info *fws = drvr->fws;

	fws->bus_flow_blocked = flow_blocked;
	if (!flow_blocked)
		brcmf_fws_schedule_deq(fws);
2065 2066
	else
		fws->stats.bus_flow_block++;
2067
}