File Index
Files and directories
Sign in
FILE

mwl8k.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/marvell/mwl8k.c C
   1/*
   2 * drivers/net/wireless/mwl8k.c
   3 * Driver for Marvell TOPDOG 802.11 Wireless cards
   4 *
   5 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
   6 *
   7 * This file is licensed under the terms of the GNU General Public
   8 * License version 2.  This program is licensed "as is" without any
   9 * warranty of any kind, whether express or implied.
  10 */
  11
  12#include <linux/interrupt.h>
  13#include <linux/module.h>
  14#include <linux/kernel.h>
  15#include <linux/sched.h>
  16#include <linux/spinlock.h>
  17#include <linux/list.h>
  18#include <linux/pci.h>
  19#include <linux/delay.h>
  20#include <linux/completion.h>
  21#include <linux/etherdevice.h>
  22#include <linux/slab.h>
  23#include <net/mac80211.h>
  24#include <linux/moduleparam.h>
  25#include <linux/firmware.h>
  26#include <linux/workqueue.h>
  27
  28#define MWL8K_DESC	"Marvell TOPDOG(R) 802.11 Wireless Network Driver"
  29#define MWL8K_NAME	KBUILD_MODNAME
  30#define MWL8K_VERSION	"0.13"
  31
  32/* Module parameters */
  33static bool ap_mode_default;
  34module_param(ap_mode_default, bool, 0);
  35MODULE_PARM_DESC(ap_mode_default,
  36		 "Set to 1 to make ap mode the default instead of sta mode");
  37
  38/* Register definitions */
  39#define MWL8K_HIU_GEN_PTR			0x00000c10
  40#define  MWL8K_MODE_STA				 0x0000005a
  41#define  MWL8K_MODE_AP				 0x000000a5
  42#define MWL8K_HIU_INT_CODE			0x00000c14
  43#define  MWL8K_FWSTA_READY			 0xf0f1f2f4
  44#define  MWL8K_FWAP_READY			 0xf1f2f4a5
  45#define  MWL8K_INT_CODE_CMD_FINISHED		 0x00000005
  46#define MWL8K_HIU_SCRATCH			0x00000c40
  47
  48/* Host->device communications */
  49#define MWL8K_HIU_H2A_INTERRUPT_EVENTS		0x00000c18
  50#define MWL8K_HIU_H2A_INTERRUPT_STATUS		0x00000c1c
  51#define MWL8K_HIU_H2A_INTERRUPT_MASK		0x00000c20
  52#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL	0x00000c24
  53#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK	0x00000c28
  54#define  MWL8K_H2A_INT_DUMMY			 (1 << 20)
  55#define  MWL8K_H2A_INT_RESET			 (1 << 15)
  56#define  MWL8K_H2A_INT_DOORBELL			 (1 << 1)
  57#define  MWL8K_H2A_INT_PPA_READY		 (1 << 0)
  58
  59/* Device->host communications */
  60#define MWL8K_HIU_A2H_INTERRUPT_EVENTS		0x00000c2c
  61#define MWL8K_HIU_A2H_INTERRUPT_STATUS		0x00000c30
  62#define MWL8K_HIU_A2H_INTERRUPT_MASK		0x00000c34
  63#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL	0x00000c38
  64#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK	0x00000c3c
  65#define  MWL8K_A2H_INT_DUMMY			 (1 << 20)
  66#define  MWL8K_A2H_INT_BA_WATCHDOG		 (1 << 14)
  67#define  MWL8K_A2H_INT_CHNL_SWITCHED		 (1 << 11)
  68#define  MWL8K_A2H_INT_QUEUE_EMPTY		 (1 << 10)
  69#define  MWL8K_A2H_INT_RADAR_DETECT		 (1 << 7)
  70#define  MWL8K_A2H_INT_RADIO_ON			 (1 << 6)
  71#define  MWL8K_A2H_INT_RADIO_OFF		 (1 << 5)
  72#define  MWL8K_A2H_INT_MAC_EVENT		 (1 << 3)
  73#define  MWL8K_A2H_INT_OPC_DONE			 (1 << 2)
  74#define  MWL8K_A2H_INT_RX_READY			 (1 << 1)
  75#define  MWL8K_A2H_INT_TX_DONE			 (1 << 0)
  76
  77/* HW micro second timer register
  78 * located at offset 0xA600. This
  79 * will be used to timestamp tx
  80 * packets.
  81 */
  82
  83#define	MWL8K_HW_TIMER_REGISTER			0x0000a600
  84#define BBU_RXRDY_CNT_REG			0x0000a860
  85#define NOK_CCA_CNT_REG				0x0000a6a0
  86#define BBU_AVG_NOISE_VAL			0x67
  87
  88#define MWL8K_A2H_EVENTS	(MWL8K_A2H_INT_DUMMY | \
  89				 MWL8K_A2H_INT_CHNL_SWITCHED | \
  90				 MWL8K_A2H_INT_QUEUE_EMPTY | \
  91				 MWL8K_A2H_INT_RADAR_DETECT | \
  92				 MWL8K_A2H_INT_RADIO_ON | \
  93				 MWL8K_A2H_INT_RADIO_OFF | \
  94				 MWL8K_A2H_INT_MAC_EVENT | \
  95				 MWL8K_A2H_INT_OPC_DONE | \
  96				 MWL8K_A2H_INT_RX_READY | \
  97				 MWL8K_A2H_INT_TX_DONE | \
  98				 MWL8K_A2H_INT_BA_WATCHDOG)
  99
 100#define MWL8K_RX_QUEUES		1
 101#define MWL8K_TX_WMM_QUEUES	4
 102#define MWL8K_MAX_AMPDU_QUEUES	8
 103#define MWL8K_MAX_TX_QUEUES	(MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
 104#define mwl8k_tx_queues(priv)	(MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
 105
 106/* txpriorities are mapped with hw queues.
 107 * Each hw queue has a txpriority.
 108 */
 109#define TOTAL_HW_TX_QUEUES	8
 110
 111/* Each HW queue can have one AMPDU stream.
 112 * But, because one of the hw queue is reserved,
 113 * maximum AMPDU queues that can be created are
 114 * one short of total tx queues.
 115 */
 116#define MWL8K_NUM_AMPDU_STREAMS	(TOTAL_HW_TX_QUEUES - 1)
 117
 118#define MWL8K_NUM_CHANS 18
 119
 120struct rxd_ops {
 121	int rxd_size;
 122	void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
 123	void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
 124	int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
 125			   __le16 *qos, s8 *noise);
 126};
 127
 128struct mwl8k_device_info {
 129	char *part_name;
 130	char *helper_image;
 131	char *fw_image_sta;
 132	char *fw_image_ap;
 133	struct rxd_ops *ap_rxd_ops;
 134	u32 fw_api_ap;
 135};
 136
 137struct mwl8k_rx_queue {
 138	int rxd_count;
 139
 140	/* hw receives here */
 141	int head;
 142
 143	/* refill descs here */
 144	int tail;
 145
 146	void *rxd;
 147	dma_addr_t rxd_dma;
 148	struct {
 149		struct sk_buff *skb;
 150		DEFINE_DMA_UNMAP_ADDR(dma);
 151	} *buf;
 152};
 153
 154struct mwl8k_tx_queue {
 155	/* hw transmits here */
 156	int head;
 157
 158	/* sw appends here */
 159	int tail;
 160
 161	unsigned int len;
 162	struct mwl8k_tx_desc *txd;
 163	dma_addr_t txd_dma;
 164	struct sk_buff **skb;
 165};
 166
 167enum {
 168	AMPDU_NO_STREAM,
 169	AMPDU_STREAM_NEW,
 170	AMPDU_STREAM_IN_PROGRESS,
 171	AMPDU_STREAM_ACTIVE,
 172};
 173
 174struct mwl8k_ampdu_stream {
 175	struct ieee80211_sta *sta;
 176	u8 tid;
 177	u8 state;
 178	u8 idx;
 179};
 180
 181struct mwl8k_priv {
 182	struct ieee80211_hw *hw;
 183	struct pci_dev *pdev;
 184	int irq;
 185
 186	struct mwl8k_device_info *device_info;
 187
 188	void __iomem *sram;
 189	void __iomem *regs;
 190
 191	/* firmware */
 192	const struct firmware *fw_helper;
 193	const struct firmware *fw_ucode;
 194
 195	/* hardware/firmware parameters */
 196	bool ap_fw;
 197	struct rxd_ops *rxd_ops;
 198	struct ieee80211_supported_band band_24;
 199	struct ieee80211_channel channels_24[14];
 200	struct ieee80211_rate rates_24[13];
 201	struct ieee80211_supported_band band_50;
 202	struct ieee80211_channel channels_50[9];
 203	struct ieee80211_rate rates_50[8];
 204	u32 ap_macids_supported;
 205	u32 sta_macids_supported;
 206
 207	/* Ampdu stream information */
 208	u8 num_ampdu_queues;
 209	spinlock_t stream_lock;
 210	struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
 211	struct work_struct watchdog_ba_handle;
 212
 213	/* firmware access */
 214	struct mutex fw_mutex;
 215	struct task_struct *fw_mutex_owner;
 216	struct task_struct *hw_restart_owner;
 217	int fw_mutex_depth;
 218	struct completion *hostcmd_wait;
 219
 220	atomic_t watchdog_event_pending;
 221
 222	/* lock held over TX and TX reap */
 223	spinlock_t tx_lock;
 224
 225	/* TX quiesce completion, protected by fw_mutex and tx_lock */
 226	struct completion *tx_wait;
 227
 228	/* List of interfaces.  */
 229	u32 macids_used;
 230	struct list_head vif_list;
 231
 232	/* power management status cookie from firmware */
 233	u32 *cookie;
 234	dma_addr_t cookie_dma;
 235
 236	u16 num_mcaddrs;
 237	u8 hw_rev;
 238	u32 fw_rev;
 239	u32 caps;
 240
 241	/*
 242	 * Running count of TX packets in flight, to avoid
 243	 * iterating over the transmit rings each time.
 244	 */
 245	int pending_tx_pkts;
 246
 247	struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
 248	struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
 249	u32 txq_offset[MWL8K_MAX_TX_QUEUES];
 250
 251	bool radio_on;
 252	bool radio_short_preamble;
 253	bool sniffer_enabled;
 254	bool wmm_enabled;
 255
 256	/* XXX need to convert this to handle multiple interfaces */
 257	bool capture_beacon;
 258	u8 capture_bssid[ETH_ALEN];
 259	struct sk_buff *beacon_skb;
 260
 261	/*
 262	 * This FJ worker has to be global as it is scheduled from the
 263	 * RX handler.  At this point we don't know which interface it
 264	 * belongs to until the list of bssids waiting to complete join
 265	 * is checked.
 266	 */
 267	struct work_struct finalize_join_worker;
 268
 269	/* Tasklet to perform TX reclaim.  */
 270	struct tasklet_struct poll_tx_task;
 271
 272	/* Tasklet to perform RX.  */
 273	struct tasklet_struct poll_rx_task;
 274
 275	/* Most recently reported noise in dBm */
 276	s8 noise;
 277
 278	/*
 279	 * preserve the queue configurations so they can be restored if/when
 280	 * the firmware image is swapped.
 281	 */
 282	struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
 283
 284	/* To perform the task of reloading the firmware */
 285	struct work_struct fw_reload;
 286	bool hw_restart_in_progress;
 287
 288	/* async firmware loading state */
 289	unsigned fw_state;
 290	char *fw_pref;
 291	char *fw_alt;
 292	bool is_8764;
 293	struct completion firmware_loading_complete;
 294
 295	/* bitmap of running BSSes */
 296	u32 running_bsses;
 297
 298	/* ACS related */
 299	bool sw_scan_start;
 300	struct ieee80211_channel *acs_chan;
 301	unsigned long channel_time;
 302	struct survey_info survey[MWL8K_NUM_CHANS];
 303};
 304
 305#define MAX_WEP_KEY_LEN         13
 306#define NUM_WEP_KEYS            4
 307
 308/* Per interface specific private data */
 309struct mwl8k_vif {
 310	struct list_head list;
 311	struct ieee80211_vif *vif;
 312
 313	/* Firmware macid for this vif.  */
 314	int macid;
 315
 316	/* Non AMPDU sequence number assigned by driver.  */
 317	u16 seqno;
 318
 319	/* Saved WEP keys */
 320	struct {
 321		u8 enabled;
 322		u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
 323	} wep_key_conf[NUM_WEP_KEYS];
 324
 325	/* BSSID */
 326	u8 bssid[ETH_ALEN];
 327
 328	/* A flag to indicate is HW crypto is enabled for this bssid */
 329	bool is_hw_crypto_enabled;
 330};
 331#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
 332#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
 333
 334struct tx_traffic_info {
 335	u32 start_time;
 336	u32 pkts;
 337};
 338
 339#define MWL8K_MAX_TID 8
 340struct mwl8k_sta {
 341	/* Index into station database. Returned by UPDATE_STADB.  */
 342	u8 peer_id;
 343	u8 is_ampdu_allowed;
 344	struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
 345};
 346#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
 347
 348static const struct ieee80211_channel mwl8k_channels_24[] = {
 349	{ .band = NL80211_BAND_2GHZ, .center_freq = 2412, .hw_value = 1, },
 350	{ .band = NL80211_BAND_2GHZ, .center_freq = 2417, .hw_value = 2, },
 351	{ .band = NL80211_BAND_2GHZ, .center_freq = 2422, .hw_value = 3, },
 352	{ .band = NL80211_BAND_2GHZ, .center_freq = 2427, .hw_value = 4, },
 353	{ .band = NL80211_BAND_2GHZ, .center_freq = 2432, .hw_value = 5, },
 354	{ .band = NL80211_BAND_2GHZ, .center_freq = 2437, .hw_value = 6, },
 355	{ .band = NL80211_BAND_2GHZ, .center_freq = 2442, .hw_value = 7, },
 356	{ .band = NL80211_BAND_2GHZ, .center_freq = 2447, .hw_value = 8, },
 357	{ .band = NL80211_BAND_2GHZ, .center_freq = 2452, .hw_value = 9, },
 358	{ .band = NL80211_BAND_2GHZ, .center_freq = 2457, .hw_value = 10, },
 359	{ .band = NL80211_BAND_2GHZ, .center_freq = 2462, .hw_value = 11, },
 360	{ .band = NL80211_BAND_2GHZ, .center_freq = 2467, .hw_value = 12, },
 361	{ .band = NL80211_BAND_2GHZ, .center_freq = 2472, .hw_value = 13, },
 362	{ .band = NL80211_BAND_2GHZ, .center_freq = 2484, .hw_value = 14, },
 363};
 364
 365static const struct ieee80211_rate mwl8k_rates_24[] = {
 366	{ .bitrate = 10, .hw_value = 2, },
 367	{ .bitrate = 20, .hw_value = 4, },
 368	{ .bitrate = 55, .hw_value = 11, },
 369	{ .bitrate = 110, .hw_value = 22, },
 370	{ .bitrate = 220, .hw_value = 44, },
 371	{ .bitrate = 60, .hw_value = 12, },
 372	{ .bitrate = 90, .hw_value = 18, },
 373	{ .bitrate = 120, .hw_value = 24, },
 374	{ .bitrate = 180, .hw_value = 36, },
 375	{ .bitrate = 240, .hw_value = 48, },
 376	{ .bitrate = 360, .hw_value = 72, },
 377	{ .bitrate = 480, .hw_value = 96, },
 378	{ .bitrate = 540, .hw_value = 108, },
 379};
 380
 381static const struct ieee80211_channel mwl8k_channels_50[] = {
 382	{ .band = NL80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
 383	{ .band = NL80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
 384	{ .band = NL80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
 385	{ .band = NL80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
 386	{ .band = NL80211_BAND_5GHZ, .center_freq = 5745, .hw_value = 149, },
 387	{ .band = NL80211_BAND_5GHZ, .center_freq = 5765, .hw_value = 153, },
 388	{ .band = NL80211_BAND_5GHZ, .center_freq = 5785, .hw_value = 157, },
 389	{ .band = NL80211_BAND_5GHZ, .center_freq = 5805, .hw_value = 161, },
 390	{ .band = NL80211_BAND_5GHZ, .center_freq = 5825, .hw_value = 165, },
 391};
 392
 393static const struct ieee80211_rate mwl8k_rates_50[] = {
 394	{ .bitrate = 60, .hw_value = 12, },
 395	{ .bitrate = 90, .hw_value = 18, },
 396	{ .bitrate = 120, .hw_value = 24, },
 397	{ .bitrate = 180, .hw_value = 36, },
 398	{ .bitrate = 240, .hw_value = 48, },
 399	{ .bitrate = 360, .hw_value = 72, },
 400	{ .bitrate = 480, .hw_value = 96, },
 401	{ .bitrate = 540, .hw_value = 108, },
 402};
 403
 404/* Set or get info from Firmware */
 405#define MWL8K_CMD_GET			0x0000
 406#define MWL8K_CMD_SET			0x0001
 407#define MWL8K_CMD_SET_LIST		0x0002
 408
 409/* Firmware command codes */
 410#define MWL8K_CMD_CODE_DNLD		0x0001
 411#define MWL8K_CMD_GET_HW_SPEC		0x0003
 412#define MWL8K_CMD_SET_HW_SPEC		0x0004
 413#define MWL8K_CMD_MAC_MULTICAST_ADR	0x0010
 414#define MWL8K_CMD_GET_STAT		0x0014
 415#define MWL8K_CMD_BBP_REG_ACCESS	0x001a
 416#define MWL8K_CMD_RADIO_CONTROL		0x001c
 417#define MWL8K_CMD_RF_TX_POWER		0x001e
 418#define MWL8K_CMD_TX_POWER		0x001f
 419#define MWL8K_CMD_RF_ANTENNA		0x0020
 420#define MWL8K_CMD_SET_BEACON		0x0100		/* per-vif */
 421#define MWL8K_CMD_SET_PRE_SCAN		0x0107
 422#define MWL8K_CMD_SET_POST_SCAN		0x0108
 423#define MWL8K_CMD_SET_RF_CHANNEL	0x010a
 424#define MWL8K_CMD_SET_AID		0x010d
 425#define MWL8K_CMD_SET_RATE		0x0110
 426#define MWL8K_CMD_SET_FINALIZE_JOIN	0x0111
 427#define MWL8K_CMD_RTS_THRESHOLD		0x0113
 428#define MWL8K_CMD_SET_SLOT		0x0114
 429#define MWL8K_CMD_SET_EDCA_PARAMS	0x0115
 430#define MWL8K_CMD_SET_WMM_MODE		0x0123
 431#define MWL8K_CMD_MIMO_CONFIG		0x0125
 432#define MWL8K_CMD_USE_FIXED_RATE	0x0126
 433#define MWL8K_CMD_ENABLE_SNIFFER	0x0150
 434#define MWL8K_CMD_SET_MAC_ADDR		0x0202		/* per-vif */
 435#define MWL8K_CMD_SET_RATEADAPT_MODE	0x0203
 436#define MWL8K_CMD_GET_WATCHDOG_BITMAP	0x0205
 437#define MWL8K_CMD_DEL_MAC_ADDR		0x0206		/* per-vif */
 438#define MWL8K_CMD_BSS_START		0x1100		/* per-vif */
 439#define MWL8K_CMD_SET_NEW_STN		0x1111		/* per-vif */
 440#define MWL8K_CMD_UPDATE_ENCRYPTION	0x1122		/* per-vif */
 441#define MWL8K_CMD_UPDATE_STADB		0x1123
 442#define MWL8K_CMD_BASTREAM		0x1125
 443
 444#define MWL8K_LEGACY_5G_RATE_OFFSET \
 445	(ARRAY_SIZE(mwl8k_rates_24) - ARRAY_SIZE(mwl8k_rates_50))
 446
 447static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
 448{
 449	u16 command = le16_to_cpu(cmd);
 450
 451#define MWL8K_CMDNAME(x)	case MWL8K_CMD_##x: do {\
 452					snprintf(buf, bufsize, "%s", #x);\
 453					return buf;\
 454					} while (0)
 455	switch (command & ~0x8000) {
 456		MWL8K_CMDNAME(CODE_DNLD);
 457		MWL8K_CMDNAME(GET_HW_SPEC);
 458		MWL8K_CMDNAME(SET_HW_SPEC);
 459		MWL8K_CMDNAME(MAC_MULTICAST_ADR);
 460		MWL8K_CMDNAME(GET_STAT);
 461		MWL8K_CMDNAME(RADIO_CONTROL);
 462		MWL8K_CMDNAME(RF_TX_POWER);
 463		MWL8K_CMDNAME(TX_POWER);
 464		MWL8K_CMDNAME(RF_ANTENNA);
 465		MWL8K_CMDNAME(SET_BEACON);
 466		MWL8K_CMDNAME(SET_PRE_SCAN);
 467		MWL8K_CMDNAME(SET_POST_SCAN);
 468		MWL8K_CMDNAME(SET_RF_CHANNEL);
 469		MWL8K_CMDNAME(SET_AID);
 470		MWL8K_CMDNAME(SET_RATE);
 471		MWL8K_CMDNAME(SET_FINALIZE_JOIN);
 472		MWL8K_CMDNAME(RTS_THRESHOLD);
 473		MWL8K_CMDNAME(SET_SLOT);
 474		MWL8K_CMDNAME(SET_EDCA_PARAMS);
 475		MWL8K_CMDNAME(SET_WMM_MODE);
 476		MWL8K_CMDNAME(MIMO_CONFIG);
 477		MWL8K_CMDNAME(USE_FIXED_RATE);
 478		MWL8K_CMDNAME(ENABLE_SNIFFER);
 479		MWL8K_CMDNAME(SET_MAC_ADDR);
 480		MWL8K_CMDNAME(SET_RATEADAPT_MODE);
 481		MWL8K_CMDNAME(BSS_START);
 482		MWL8K_CMDNAME(SET_NEW_STN);
 483		MWL8K_CMDNAME(UPDATE_ENCRYPTION);
 484		MWL8K_CMDNAME(UPDATE_STADB);
 485		MWL8K_CMDNAME(BASTREAM);
 486		MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
 487	default:
 488		snprintf(buf, bufsize, "0x%x", cmd);
 489	}
 490#undef MWL8K_CMDNAME
 491
 492	return buf;
 493}
 494
 495/* Hardware and firmware reset */
 496static void mwl8k_hw_reset(struct mwl8k_priv *priv)
 497{
 498	iowrite32(MWL8K_H2A_INT_RESET,
 499		priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
 500	iowrite32(MWL8K_H2A_INT_RESET,
 501		priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
 502	msleep(20);
 503}
 504
 505/* Release fw image */
 506static void mwl8k_release_fw(const struct firmware **fw)
 507{
 508	if (*fw == NULL)
 509		return;
 510	release_firmware(*fw);
 511	*fw = NULL;
 512}
 513
 514static void mwl8k_release_firmware(struct mwl8k_priv *priv)
 515{
 516	mwl8k_release_fw(&priv->fw_ucode);
 517	mwl8k_release_fw(&priv->fw_helper);
 518}
 519
 520/* states for asynchronous f/w loading */
 521static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
 522enum {
 523	FW_STATE_INIT = 0,
 524	FW_STATE_LOADING_PREF,
 525	FW_STATE_LOADING_ALT,
 526	FW_STATE_ERROR,
 527};
 528
 529/* Request fw image */
 530static int mwl8k_request_fw(struct mwl8k_priv *priv,
 531			    const char *fname, const struct firmware **fw,
 532			    bool nowait)
 533{
 534	/* release current image */
 535	if (*fw != NULL)
 536		mwl8k_release_fw(fw);
 537
 538	if (nowait)
 539		return request_firmware_nowait(THIS_MODULE, 1, fname,
 540					       &priv->pdev->dev, GFP_KERNEL,
 541					       priv, mwl8k_fw_state_machine);
 542	else
 543		return request_firmware(fw, fname, &priv->pdev->dev);
 544}
 545
 546static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
 547				  bool nowait)
 548{
 549	struct mwl8k_device_info *di = priv->device_info;
 550	int rc;
 551
 552	if (di->helper_image != NULL) {
 553		if (nowait)
 554			rc = mwl8k_request_fw(priv, di->helper_image,
 555					      &priv->fw_helper, true);
 556		else
 557			rc = mwl8k_request_fw(priv, di->helper_image,
 558					      &priv->fw_helper, false);
 559		if (rc)
 560			printk(KERN_ERR "%s: Error requesting helper fw %s\n",
 561			       pci_name(priv->pdev), di->helper_image);
 562
 563		if (rc || nowait)
 564			return rc;
 565	}
 566
 567	if (nowait) {
 568		/*
 569		 * if we get here, no helper image is needed.  Skip the
 570		 * FW_STATE_INIT state.
 571		 */
 572		priv->fw_state = FW_STATE_LOADING_PREF;
 573		rc = mwl8k_request_fw(priv, fw_image,
 574				      &priv->fw_ucode,
 575				      true);
 576	} else
 577		rc = mwl8k_request_fw(priv, fw_image,
 578				      &priv->fw_ucode, false);
 579	if (rc) {
 580		printk(KERN_ERR "%s: Error requesting firmware file %s\n",
 581		       pci_name(priv->pdev), fw_image);
 582		mwl8k_release_fw(&priv->fw_helper);
 583		return rc;
 584	}
 585
 586	return 0;
 587}
 588
 589struct mwl8k_cmd_pkt {
 590	/* New members MUST be added within the __struct_group() macro below. */
 591	__struct_group(mwl8k_cmd_pkt_hdr, hdr, __packed,
 592		__le16	code;
 593		__le16	length;
 594		__u8	seq_num;
 595		__u8	macid;
 596		__le16	result;
 597	);
 598	char payload[];
 599} __packed;
 600static_assert(offsetof(struct mwl8k_cmd_pkt, payload) == sizeof(struct mwl8k_cmd_pkt_hdr),
 601	      "struct member likely outside of __struct_group()");
 602
 603/*
 604 * Firmware loading.
 605 */
 606static int
 607mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
 608{
 609	void __iomem *regs = priv->regs;
 610	dma_addr_t dma_addr;
 611	int loops;
 612
 613	dma_addr = dma_map_single(&priv->pdev->dev, data, length,
 614				  DMA_TO_DEVICE);
 615	if (dma_mapping_error(&priv->pdev->dev, dma_addr))
 616		return -ENOMEM;
 617
 618	iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
 619	iowrite32(0, regs + MWL8K_HIU_INT_CODE);
 620	iowrite32(MWL8K_H2A_INT_DOORBELL,
 621		regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
 622	iowrite32(MWL8K_H2A_INT_DUMMY,
 623		regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
 624
 625	loops = 1000;
 626	do {
 627		u32 int_code;
 628		if (priv->is_8764) {
 629			int_code = ioread32(regs +
 630					    MWL8K_HIU_H2A_INTERRUPT_STATUS);
 631			if (int_code == 0)
 632				break;
 633		} else {
 634			int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
 635			if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
 636				iowrite32(0, regs + MWL8K_HIU_INT_CODE);
 637				break;
 638			}
 639		}
 640		cond_resched();
 641		udelay(1);
 642	} while (--loops);
 643
 644	dma_unmap_single(&priv->pdev->dev, dma_addr, length, DMA_TO_DEVICE);
 645
 646	return loops ? 0 : -ETIMEDOUT;
 647}
 648
 649static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
 650				const u8 *data, size_t length)
 651{
 652	struct mwl8k_cmd_pkt *cmd;
 653	int done;
 654	int rc = 0;
 655
 656	cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
 657	if (cmd == NULL)
 658		return -ENOMEM;
 659
 660	cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
 661	cmd->seq_num = 0;
 662	cmd->macid = 0;
 663	cmd->result = 0;
 664
 665	done = 0;
 666	while (length) {
 667		int block_size = length > 256 ? 256 : length;
 668
 669		memcpy(cmd->payload, data + done, block_size);
 670		cmd->length = cpu_to_le16(block_size);
 671
 672		rc = mwl8k_send_fw_load_cmd(priv, cmd,
 673						sizeof(*cmd) + block_size);
 674		if (rc)
 675			break;
 676
 677		done += block_size;
 678		length -= block_size;
 679	}
 680
 681	if (!rc) {
 682		cmd->length = 0;
 683		rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
 684	}
 685
 686	kfree(cmd);
 687
 688	return rc;
 689}
 690
 691static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
 692				const u8 *data, size_t length)
 693{
 694	unsigned char *buffer;
 695	int may_continue, rc = 0;
 696	u32 done, prev_block_size;
 697
 698	buffer = kmalloc(1024, GFP_KERNEL);
 699	if (buffer == NULL)
 700		return -ENOMEM;
 701
 702	done = 0;
 703	prev_block_size = 0;
 704	may_continue = 1000;
 705	while (may_continue > 0) {
 706		u32 block_size;
 707
 708		block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
 709		if (block_size & 1) {
 710			block_size &= ~1;
 711			may_continue--;
 712		} else {
 713			done += prev_block_size;
 714			length -= prev_block_size;
 715		}
 716
 717		if (block_size > 1024 || block_size > length) {
 718			rc = -EOVERFLOW;
 719			break;
 720		}
 721
 722		if (length == 0) {
 723			rc = 0;
 724			break;
 725		}
 726
 727		if (block_size == 0) {
 728			rc = -EPROTO;
 729			may_continue--;
 730			udelay(1);
 731			continue;
 732		}
 733
 734		prev_block_size = block_size;
 735		memcpy(buffer, data + done, block_size);
 736
 737		rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
 738		if (rc)
 739			break;
 740	}
 741
 742	if (!rc && length != 0)
 743		rc = -EREMOTEIO;
 744
 745	kfree(buffer);
 746
 747	return rc;
 748}
 749
 750static int mwl8k_load_firmware(struct ieee80211_hw *hw)
 751{
 752	struct mwl8k_priv *priv = hw->priv;
 753	const struct firmware *fw = priv->fw_ucode;
 754	int rc;
 755	int loops;
 756
 757	if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
 758		const struct firmware *helper = priv->fw_helper;
 759
 760		if (helper == NULL) {
 761			printk(KERN_ERR "%s: helper image needed but none "
 762			       "given\n", pci_name(priv->pdev));
 763			return -EINVAL;
 764		}
 765
 766		rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
 767		if (rc) {
 768			printk(KERN_ERR "%s: unable to load firmware "
 769			       "helper image\n", pci_name(priv->pdev));
 770			return rc;
 771		}
 772		msleep(20);
 773
 774		rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
 775	} else {
 776		if (priv->is_8764)
 777			rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
 778		else
 779			rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
 780	}
 781
 782	if (rc) {
 783		printk(KERN_ERR "%s: unable to load firmware image\n",
 784		       pci_name(priv->pdev));
 785		return rc;
 786	}
 787
 788	iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
 789
 790	loops = 500000;
 791	do {
 792		u32 ready_code;
 793
 794		ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
 795		if (ready_code == MWL8K_FWAP_READY) {
 796			priv->ap_fw = true;
 797			break;
 798		} else if (ready_code == MWL8K_FWSTA_READY) {
 799			priv->ap_fw = false;
 800			break;
 801		}
 802
 803		cond_resched();
 804		udelay(1);
 805	} while (--loops);
 806
 807	return loops ? 0 : -ETIMEDOUT;
 808}
 809
 810
 811/* DMA header used by firmware and hardware.  */
 812struct mwl8k_dma_data {
 813	__le16 fwlen;
 814	struct ieee80211_hdr wh;
 815	char data[];
 816} __packed __aligned(2);
 817
 818/* Routines to add/remove DMA header from skb.  */
 819static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
 820{
 821	struct mwl8k_dma_data *tr;
 822	int hdrlen;
 823
 824	tr = (struct mwl8k_dma_data *)skb->data;
 825	hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
 826
 827	if (hdrlen != sizeof(tr->wh)) {
 828		if (ieee80211_is_data_qos(tr->wh.frame_control)) {
 829			memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
 830			*((__le16 *)(tr->data - 2)) = qos;
 831		} else {
 832			memmove(tr->data - hdrlen, &tr->wh, hdrlen);
 833		}
 834	}
 835
 836	if (hdrlen != sizeof(*tr))
 837		skb_pull(skb, sizeof(*tr) - hdrlen);
 838}
 839
 840#define REDUCED_TX_HEADROOM	8
 841
 842static void
 843mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
 844						int head_pad, int tail_pad)
 845{
 846	struct ieee80211_hdr *wh;
 847	int hdrlen;
 848	int reqd_hdrlen;
 849	struct mwl8k_dma_data *tr;
 850
 851	/*
 852	 * Add a firmware DMA header; the firmware requires that we
 853	 * present a 2-byte payload length followed by a 4-address
 854	 * header (without QoS field), followed (optionally) by any
 855	 * WEP/ExtIV header (but only filled in for CCMP).
 856	 */
 857	wh = (struct ieee80211_hdr *)skb->data;
 858
 859	hdrlen = ieee80211_hdrlen(wh->frame_control);
 860
 861	/*
 862	 * Check if skb_resize is required because of
 863	 * tx_headroom adjustment.
 864	 */
 865	if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
 866						+ REDUCED_TX_HEADROOM))) {
 867		if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
 868
 869			wiphy_err(priv->hw->wiphy,
 870					"Failed to reallocate TX buffer\n");
 871			return;
 872		}
 873		skb->truesize += REDUCED_TX_HEADROOM;
 874	}
 875
 876	reqd_hdrlen = sizeof(*tr) + head_pad;
 877
 878	if (hdrlen != reqd_hdrlen)
 879		skb_push(skb, reqd_hdrlen - hdrlen);
 880
 881	if (ieee80211_is_data_qos(wh->frame_control))
 882		hdrlen -= IEEE80211_QOS_CTL_LEN;
 883
 884	tr = (struct mwl8k_dma_data *)skb->data;
 885	if (wh != &tr->wh)
 886		memmove(&tr->wh, wh, hdrlen);
 887	if (hdrlen != sizeof(tr->wh))
 888		memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
 889
 890	/*
 891	 * Firmware length is the length of the fully formed "802.11
 892	 * payload".  That is, everything except for the 802.11 header.
 893	 * This includes all crypto material including the MIC.
 894	 */
 895	tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
 896}
 897
 898static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
 899		struct sk_buff *skb)
 900{
 901	struct ieee80211_hdr *wh;
 902	struct ieee80211_tx_info *tx_info;
 903	struct ieee80211_key_conf *key_conf;
 904	int data_pad;
 905	int head_pad = 0;
 906
 907	wh = (struct ieee80211_hdr *)skb->data;
 908
 909	tx_info = IEEE80211_SKB_CB(skb);
 910
 911	key_conf = NULL;
 912	if (ieee80211_is_data(wh->frame_control))
 913		key_conf = tx_info->control.hw_key;
 914
 915	/*
 916	 * Make sure the packet header is in the DMA header format (4-address
 917	 * without QoS), and add head & tail padding when HW crypto is enabled.
 918	 *
 919	 * We have the following trailer padding requirements:
 920	 * - WEP: 4 trailer bytes (ICV)
 921	 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
 922	 * - CCMP: 8 trailer bytes (MIC)
 923	 */
 924	data_pad = 0;
 925	if (key_conf != NULL) {
 926		head_pad = key_conf->iv_len;
 927		switch (key_conf->cipher) {
 928		case WLAN_CIPHER_SUITE_WEP40:
 929		case WLAN_CIPHER_SUITE_WEP104:
 930			data_pad = 4;
 931			break;
 932		case WLAN_CIPHER_SUITE_TKIP:
 933			data_pad = 12;
 934			break;
 935		case WLAN_CIPHER_SUITE_CCMP:
 936			data_pad = 8;
 937			break;
 938		}
 939	}
 940	mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
 941}
 942
 943/*
 944 * Packet reception for 88w8366/88w8764 AP firmware.
 945 */
 946struct mwl8k_rxd_ap {
 947	__le16 pkt_len;
 948	__u8 sq2;
 949	__u8 rate;
 950	__le32 pkt_phys_addr;
 951	__le32 next_rxd_phys_addr;
 952	__le16 qos_control;
 953	__le16 htsig2;
 954	__le32 hw_rssi_info;
 955	__le32 hw_noise_floor_info;
 956	__u8 noise_floor;
 957	__u8 pad0[3];
 958	__u8 rssi;
 959	__u8 rx_status;
 960	__u8 channel;
 961	__u8 rx_ctrl;
 962} __packed;
 963
 964#define MWL8K_AP_RATE_INFO_MCS_FORMAT		0x80
 965#define MWL8K_AP_RATE_INFO_40MHZ		0x40
 966#define MWL8K_AP_RATE_INFO_RATEID(x)		((x) & 0x3f)
 967
 968#define MWL8K_AP_RX_CTRL_OWNED_BY_HOST		0x80
 969
 970/* 8366/8764 AP rx_status bits */
 971#define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK		0x80
 972#define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR		0xFF
 973#define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR		0x02
 974#define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR		0x04
 975#define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR		0x08
 976
 977static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
 978{
 979	struct mwl8k_rxd_ap *rxd = _rxd;
 980
 981	rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
 982	rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
 983}
 984
 985static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
 986{
 987	struct mwl8k_rxd_ap *rxd = _rxd;
 988
 989	rxd->pkt_len = cpu_to_le16(len);
 990	rxd->pkt_phys_addr = cpu_to_le32(addr);
 991	wmb();
 992	rxd->rx_ctrl = 0;
 993}
 994
 995static int
 996mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
 997		     __le16 *qos, s8 *noise)
 998{
 999	struct mwl8k_rxd_ap *rxd = _rxd;
1000
1001	if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
1002		return -1;
1003	rmb();
1004
1005	memset(status, 0, sizeof(*status));
1006
1007	status->signal = -rxd->rssi;
1008	*noise = -rxd->noise_floor;
1009
1010	if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
1011		status->encoding = RX_ENC_HT;
1012		if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
1013			status->bw = RATE_INFO_BW_40;
1014		status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
1015	} else {
1016		int i;
1017
1018		for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
1019			if (mwl8k_rates_24[i].hw_value == rxd->rate) {
1020				status->rate_idx = i;
1021				break;
1022			}
1023		}
1024	}
1025
1026	if (rxd->channel > 14) {
1027		status->band = NL80211_BAND_5GHZ;
1028		if (!(status->encoding == RX_ENC_HT) &&
1029		    status->rate_idx >= MWL8K_LEGACY_5G_RATE_OFFSET)
1030			status->rate_idx -= MWL8K_LEGACY_5G_RATE_OFFSET;
1031	} else {
1032		status->band = NL80211_BAND_2GHZ;
1033	}
1034	status->freq = ieee80211_channel_to_frequency(rxd->channel,
1035						      status->band);
1036
1037	*qos = rxd->qos_control;
1038
1039	if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
1040	    (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
1041	    (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
1042		status->flag |= RX_FLAG_MMIC_ERROR;
1043
1044	return le16_to_cpu(rxd->pkt_len);
1045}
1046
1047static struct rxd_ops rxd_ap_ops = {
1048	.rxd_size	= sizeof(struct mwl8k_rxd_ap),
1049	.rxd_init	= mwl8k_rxd_ap_init,
1050	.rxd_refill	= mwl8k_rxd_ap_refill,
1051	.rxd_process	= mwl8k_rxd_ap_process,
1052};
1053
1054/*
1055 * Packet reception for STA firmware.
1056 */
1057struct mwl8k_rxd_sta {
1058	__le16 pkt_len;
1059	__u8 link_quality;
1060	__u8 noise_level;
1061	__le32 pkt_phys_addr;
1062	__le32 next_rxd_phys_addr;
1063	__le16 qos_control;
1064	__le16 rate_info;
1065	__le32 pad0[4];
1066	__u8 rssi;
1067	__u8 channel;
1068	__le16 pad1;
1069	__u8 rx_ctrl;
1070	__u8 rx_status;
1071	__u8 pad2[2];
1072} __packed;
1073
1074#define MWL8K_STA_RATE_INFO_SHORTPRE		0x8000
1075#define MWL8K_STA_RATE_INFO_ANTSELECT(x)	(((x) >> 11) & 0x3)
1076#define MWL8K_STA_RATE_INFO_RATEID(x)		(((x) >> 3) & 0x3f)
1077#define MWL8K_STA_RATE_INFO_40MHZ		0x0004
1078#define MWL8K_STA_RATE_INFO_SHORTGI		0x0002
1079#define MWL8K_STA_RATE_INFO_MCS_FORMAT		0x0001
1080
1081#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST		0x02
1082#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR		0x04
1083/* ICV=0 or MIC=1 */
1084#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE		0x08
1085/* Key is uploaded only in failure case */
1086#define MWL8K_STA_RX_CTRL_KEY_INDEX			0x30
1087
1088static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
1089{
1090	struct mwl8k_rxd_sta *rxd = _rxd;
1091
1092	rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
1093	rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
1094}
1095
1096static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
1097{
1098	struct mwl8k_rxd_sta *rxd = _rxd;
1099
1100	rxd->pkt_len = cpu_to_le16(len);
1101	rxd->pkt_phys_addr = cpu_to_le32(addr);
1102	wmb();
1103	rxd->rx_ctrl = 0;
1104}
1105
1106static int
1107mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
1108		       __le16 *qos, s8 *noise)
1109{
1110	struct mwl8k_rxd_sta *rxd = _rxd;
1111	u16 rate_info;
1112
1113	if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
1114		return -1;
1115	rmb();
1116
1117	rate_info = le16_to_cpu(rxd->rate_info);
1118
1119	memset(status, 0, sizeof(*status));
1120
1121	status->signal = -rxd->rssi;
1122	*noise = -rxd->noise_level;
1123	status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1124	status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
1125
1126	if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
1127		status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
1128	if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
1129		status->bw = RATE_INFO_BW_40;
1130	if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
1131		status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1132	if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
1133		status->encoding = RX_ENC_HT;
1134
1135	if (rxd->channel > 14) {
1136		status->band = NL80211_BAND_5GHZ;
1137		if (!(status->encoding == RX_ENC_HT) &&
1138		    status->rate_idx >= MWL8K_LEGACY_5G_RATE_OFFSET)
1139			status->rate_idx -= MWL8K_LEGACY_5G_RATE_OFFSET;
1140	} else {
1141		status->band = NL80211_BAND_2GHZ;
1142	}
1143	status->freq = ieee80211_channel_to_frequency(rxd->channel,
1144						      status->band);
1145
1146	*qos = rxd->qos_control;
1147	if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1148	    (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1149		status->flag |= RX_FLAG_MMIC_ERROR;
1150
1151	return le16_to_cpu(rxd->pkt_len);
1152}
1153
1154static struct rxd_ops rxd_sta_ops = {
1155	.rxd_size	= sizeof(struct mwl8k_rxd_sta),
1156	.rxd_init	= mwl8k_rxd_sta_init,
1157	.rxd_refill	= mwl8k_rxd_sta_refill,
1158	.rxd_process	= mwl8k_rxd_sta_process,
1159};
1160
1161
1162#define MWL8K_RX_DESCS		256
1163#define MWL8K_RX_MAXSZ		3800
1164
1165static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1166{
1167	struct mwl8k_priv *priv = hw->priv;
1168	struct mwl8k_rx_queue *rxq = priv->rxq + index;
1169	int size;
1170	int i;
1171
1172	rxq->rxd_count = 0;
1173	rxq->head = 0;
1174	rxq->tail = 0;
1175
1176	size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
1177
1178	rxq->rxd = dma_alloc_coherent(&priv->pdev->dev, size, &rxq->rxd_dma,
1179				      GFP_KERNEL);
1180	if (rxq->rxd == NULL) {
1181		wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
1182		return -ENOMEM;
1183	}
1184
1185	rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
1186	if (rxq->buf == NULL) {
1187		dma_free_coherent(&priv->pdev->dev, size, rxq->rxd,
1188				  rxq->rxd_dma);
1189		return -ENOMEM;
1190	}
1191
1192	for (i = 0; i < MWL8K_RX_DESCS; i++) {
1193		int desc_size;
1194		void *rxd;
1195		int nexti;
1196		dma_addr_t next_dma_addr;
1197
1198		desc_size = priv->rxd_ops->rxd_size;
1199		rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
1200
1201		nexti = i + 1;
1202		if (nexti == MWL8K_RX_DESCS)
1203			nexti = 0;
1204		next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1205
1206		priv->rxd_ops->rxd_init(rxd, next_dma_addr);
1207	}
1208
1209	return 0;
1210}
1211
1212static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1213{
1214	struct mwl8k_priv *priv = hw->priv;
1215	struct mwl8k_rx_queue *rxq = priv->rxq + index;
1216	int refilled = 0;
1217
1218	while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
1219		struct sk_buff *skb;
1220		dma_addr_t addr;
1221		int rx;
1222		void *rxd;
1223
1224		skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1225		if (skb == NULL)
1226			break;
1227
1228		addr = dma_map_single(&priv->pdev->dev, skb->data,
1229				      MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
1230		if (dma_mapping_error(&priv->pdev->dev, addr)) {
1231			kfree_skb(skb);
1232			break;
1233		}
1234
1235		rxq->rxd_count++;
1236		rx = rxq->tail++;
1237		if (rxq->tail == MWL8K_RX_DESCS)
1238			rxq->tail = 0;
1239		rxq->buf[rx].skb = skb;
1240		dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
1241
1242		rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1243		priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
1244
1245		refilled++;
1246	}
1247
1248	return refilled;
1249}
1250
1251/* Must be called only when the card's reception is completely halted */
1252static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1253{
1254	struct mwl8k_priv *priv = hw->priv;
1255	struct mwl8k_rx_queue *rxq = priv->rxq + index;
1256	int i;
1257
1258	if (rxq->rxd == NULL)
1259		return;
1260
1261	for (i = 0; i < MWL8K_RX_DESCS; i++) {
1262		if (rxq->buf[i].skb != NULL) {
1263			dma_unmap_single(&priv->pdev->dev,
1264					 dma_unmap_addr(&rxq->buf[i], dma),
1265					 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
1266			dma_unmap_addr_set(&rxq->buf[i], dma, 0);
1267
1268			kfree_skb(rxq->buf[i].skb);
1269			rxq->buf[i].skb = NULL;
1270		}
1271	}
1272
1273	kfree(rxq->buf);
1274	rxq->buf = NULL;
1275
1276	dma_free_coherent(&priv->pdev->dev,
1277			  MWL8K_RX_DESCS * priv->rxd_ops->rxd_size, rxq->rxd,
1278			  rxq->rxd_dma);
1279	rxq->rxd = NULL;
1280}
1281
1282
1283/*
1284 * Scan a list of BSSIDs to process for finalize join.
1285 * Allows for extension to process multiple BSSIDs.
1286 */
1287static inline int
1288mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1289{
1290	return priv->capture_beacon &&
1291		ieee80211_is_beacon(wh->frame_control) &&
1292		ether_addr_equal_64bits(wh->addr3, priv->capture_bssid);
1293}
1294
1295static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1296				     struct sk_buff *skb)
1297{
1298	struct mwl8k_priv *priv = hw->priv;
1299
1300	priv->capture_beacon = false;
1301	eth_zero_addr(priv->capture_bssid);
1302
1303	/*
1304	 * Use GFP_ATOMIC as rxq_process is called from
1305	 * the primary interrupt handler, memory allocation call
1306	 * must not sleep.
1307	 */
1308	priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1309	if (priv->beacon_skb != NULL)
1310		ieee80211_queue_work(hw, &priv->finalize_join_worker);
1311}
1312
1313static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1314						   u8 *bssid)
1315{
1316	struct mwl8k_vif *mwl8k_vif;
1317
1318	list_for_each_entry(mwl8k_vif,
1319			    vif_list, list) {
1320		if (memcmp(bssid, mwl8k_vif->bssid,
1321			   ETH_ALEN) == 0)
1322			return mwl8k_vif;
1323	}
1324
1325	return NULL;
1326}
1327
1328static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1329{
1330	struct mwl8k_priv *priv = hw->priv;
1331	struct mwl8k_vif *mwl8k_vif = NULL;
1332	struct mwl8k_rx_queue *rxq = priv->rxq + index;
1333	int processed;
1334
1335	processed = 0;
1336	while (rxq->rxd_count && limit--) {
1337		struct sk_buff *skb;
1338		void *rxd;
1339		int pkt_len;
1340		struct ieee80211_rx_status status;
1341		struct ieee80211_hdr *wh;
1342		__le16 qos;
1343
1344		skb = rxq->buf[rxq->head].skb;
1345		if (skb == NULL)
1346			break;
1347
1348		rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1349
1350		pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1351							&priv->noise);
1352		if (pkt_len < 0)
1353			break;
1354
1355		rxq->buf[rxq->head].skb = NULL;
1356
1357		dma_unmap_single(&priv->pdev->dev,
1358				 dma_unmap_addr(&rxq->buf[rxq->head], dma),
1359				 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
1360		dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
1361
1362		rxq->head++;
1363		if (rxq->head == MWL8K_RX_DESCS)
1364			rxq->head = 0;
1365
1366		rxq->rxd_count--;
1367
1368		wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1369
1370		/*
1371		 * Check for a pending join operation.  Save a
1372		 * copy of the beacon and schedule a tasklet to
1373		 * send a FINALIZE_JOIN command to the firmware.
1374		 */
1375		if (mwl8k_capture_bssid(priv, (void *)skb->data))
1376			mwl8k_save_beacon(hw, skb);
1377
1378		if (ieee80211_has_protected(wh->frame_control)) {
1379
1380			/* Check if hw crypto has been enabled for
1381			 * this bss. If yes, set the status flags
1382			 * accordingly
1383			 */
1384			mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1385								wh->addr1);
1386
1387			if (mwl8k_vif != NULL &&
1388			    mwl8k_vif->is_hw_crypto_enabled) {
1389				/*
1390				 * When MMIC ERROR is encountered
1391				 * by the firmware, payload is
1392				 * dropped and only 32 bytes of
1393				 * mwl8k Firmware header is sent
1394				 * to the host.
1395				 *
1396				 * We need to add four bytes of
1397				 * key information.  In it
1398				 * MAC80211 expects keyidx set to
1399				 * 0 for triggering Counter
1400				 * Measure of MMIC failure.
1401				 */
1402				if (status.flag & RX_FLAG_MMIC_ERROR) {
1403					struct mwl8k_dma_data *tr;
1404					tr = (struct mwl8k_dma_data *)skb->data;
1405					memset((void *)&(tr->data), 0, 4);
1406					pkt_len += 4;
1407				}
1408
1409				if (!ieee80211_is_auth(wh->frame_control))
1410					status.flag |= RX_FLAG_IV_STRIPPED |
1411						       RX_FLAG_DECRYPTED |
1412						       RX_FLAG_MMIC_STRIPPED;
1413			}
1414		}
1415
1416		skb_put(skb, pkt_len);
1417		mwl8k_remove_dma_header(skb, qos);
1418		memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1419		ieee80211_rx_irqsafe(hw, skb);
1420
1421		processed++;
1422	}
1423
1424	return processed;
1425}
1426
1427
1428/*
1429 * Packet transmission.
1430 */
1431
1432#define MWL8K_TXD_STATUS_OK			0x00000001
1433#define MWL8K_TXD_STATUS_OK_RETRY		0x00000002
1434#define MWL8K_TXD_STATUS_OK_MORE_RETRY		0x00000004
1435#define MWL8K_TXD_STATUS_MULTICAST_TX		0x00000008
1436#define MWL8K_TXD_STATUS_FW_OWNED		0x80000000
1437
1438#define MWL8K_QOS_QLEN_UNSPEC			0xff00
1439#define MWL8K_QOS_ACK_POLICY_MASK		0x0060
1440#define MWL8K_QOS_ACK_POLICY_NORMAL		0x0000
1441#define MWL8K_QOS_ACK_POLICY_BLOCKACK		0x0060
1442#define MWL8K_QOS_EOSP				0x0010
1443
1444struct mwl8k_tx_desc {
1445	__le32 status;
1446	__u8 data_rate;
1447	__u8 tx_priority;
1448	__le16 qos_control;
1449	__le32 pkt_phys_addr;
1450	__le16 pkt_len;
1451	__u8 dest_MAC_addr[ETH_ALEN];
1452	__le32 next_txd_phys_addr;
1453	__le32 timestamp;
1454	__le16 rate_info;
1455	__u8 peer_id;
1456	__u8 tx_frag_cnt;
1457} __packed;
1458
1459#define MWL8K_TX_DESCS		128
1460
1461static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1462{
1463	struct mwl8k_priv *priv = hw->priv;
1464	struct mwl8k_tx_queue *txq = priv->txq + index;
1465	int size;
1466	int i;
1467
1468	txq->len = 0;
1469	txq->head = 0;
1470	txq->tail = 0;
1471
1472	size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1473
1474	txq->txd = dma_alloc_coherent(&priv->pdev->dev, size, &txq->txd_dma,
1475				      GFP_KERNEL);
1476	if (txq->txd == NULL) {
1477		wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
1478		return -ENOMEM;
1479	}
1480
1481	txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
1482	if (txq->skb == NULL) {
1483		dma_free_coherent(&priv->pdev->dev, size, txq->txd,
1484				  txq->txd_dma);
1485		txq->txd = NULL;
1486		return -ENOMEM;
1487	}
1488
1489	for (i = 0; i < MWL8K_TX_DESCS; i++) {
1490		struct mwl8k_tx_desc *tx_desc;
1491		int nexti;
1492
1493		tx_desc = txq->txd + i;
1494		nexti = (i + 1) % MWL8K_TX_DESCS;
1495
1496		tx_desc->status = 0;
1497		tx_desc->next_txd_phys_addr =
1498			cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
1499	}
1500
1501	return 0;
1502}
1503
1504static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1505{
1506	iowrite32(MWL8K_H2A_INT_PPA_READY,
1507		priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1508	iowrite32(MWL8K_H2A_INT_DUMMY,
1509		priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1510	ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1511}
1512
1513static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
1514{
1515	struct mwl8k_priv *priv = hw->priv;
1516	int i;
1517
1518	for (i = 0; i < mwl8k_tx_queues(priv); i++) {
1519		struct mwl8k_tx_queue *txq = priv->txq + i;
1520		int fw_owned = 0;
1521		int drv_owned = 0;
1522		int unused = 0;
1523		int desc;
1524
1525		for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
1526			struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1527			u32 status;
1528
1529			status = le32_to_cpu(tx_desc->status);
1530			if (status & MWL8K_TXD_STATUS_FW_OWNED)
1531				fw_owned++;
1532			else
1533				drv_owned++;
1534
1535			if (tx_desc->pkt_len == 0)
1536				unused++;
1537		}
1538
1539		wiphy_err(hw->wiphy,
1540			  "txq[%d] len=%d head=%d tail=%d "
1541			  "fw_owned=%d drv_owned=%d unused=%d\n",
1542			  i,
1543			  txq->len, txq->head, txq->tail,
1544			  fw_owned, drv_owned, unused);
1545	}
1546}
1547
1548/*
1549 * Must be called with priv->fw_mutex held and tx queues stopped.
1550 */
1551#define MWL8K_TX_WAIT_TIMEOUT_MS	5000
1552
1553static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
1554{
1555	struct mwl8k_priv *priv = hw->priv;
1556	DECLARE_COMPLETION_ONSTACK(tx_wait);
1557	int retry;
1558	int rc;
1559
1560	might_sleep();
1561
1562	/* Since fw restart is in progress, allow only the firmware
1563	 * commands from the restart code and block the other
1564	 * commands since they are going to fail in any case since
1565	 * the firmware has crashed
1566	 */
1567	if (priv->hw_restart_in_progress) {
1568		if (priv->hw_restart_owner == current)
1569			return 0;
1570		else
1571			return -EBUSY;
1572	}
1573
1574	if (atomic_read(&priv->watchdog_event_pending))
1575		return 0;
1576
1577	/*
1578	 * The TX queues are stopped at this point, so this test
1579	 * doesn't need to take ->tx_lock.
1580	 */
1581	if (!priv->pending_tx_pkts)
1582		return 0;
1583
1584	retry = 1;
1585	rc = 0;
1586
1587	spin_lock_bh(&priv->tx_lock);
1588	priv->tx_wait = &tx_wait;
1589	while (!rc) {
1590		int oldcount;
1591		unsigned long timeout;
1592
1593		oldcount = priv->pending_tx_pkts;
1594
1595		spin_unlock_bh(&priv->tx_lock);
1596		timeout = wait_for_completion_timeout(&tx_wait,
1597			    msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1598
1599		if (atomic_read(&priv->watchdog_event_pending)) {
1600			spin_lock_bh(&priv->tx_lock);
1601			priv->tx_wait = NULL;
1602			spin_unlock_bh(&priv->tx_lock);
1603			return 0;
1604		}
1605
1606		spin_lock_bh(&priv->tx_lock);
1607
1608		if (timeout || !priv->pending_tx_pkts) {
1609			WARN_ON(priv->pending_tx_pkts);
1610			if (retry)
1611				wiphy_notice(hw->wiphy, "tx rings drained\n");
1612			break;
1613		}
1614
1615		if (retry) {
1616			mwl8k_tx_start(priv);
1617			retry = 0;
1618			continue;
1619		}
1620
1621		if (priv->pending_tx_pkts < oldcount) {
1622			wiphy_notice(hw->wiphy,
1623				     "waiting for tx rings to drain (%d -> %d pkts)\n",
1624				     oldcount, priv->pending_tx_pkts);
1625			retry = 1;
1626			continue;
1627		}
1628
1629		priv->tx_wait = NULL;
1630
1631		wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1632			  MWL8K_TX_WAIT_TIMEOUT_MS);
1633		mwl8k_dump_tx_rings(hw);
1634		priv->hw_restart_in_progress = true;
1635		ieee80211_queue_work(hw, &priv->fw_reload);
1636
1637		rc = -ETIMEDOUT;
1638	}
1639	priv->tx_wait = NULL;
1640	spin_unlock_bh(&priv->tx_lock);
1641
1642	return rc;
1643}
1644
1645#define MWL8K_TXD_SUCCESS(status)				\
1646	((status) & (MWL8K_TXD_STATUS_OK |			\
1647		     MWL8K_TXD_STATUS_OK_RETRY |		\
1648		     MWL8K_TXD_STATUS_OK_MORE_RETRY))
1649
1650static int mwl8k_tid_queue_mapping(u8 tid)
1651{
1652	BUG_ON(tid > 7);
1653
1654	switch (tid) {
1655	case 0:
1656	case 3:
1657		return IEEE80211_AC_BE;
1658	case 1:
1659	case 2:
1660		return IEEE80211_AC_BK;
1661	case 4:
1662	case 5:
1663		return IEEE80211_AC_VI;
1664	case 6:
1665	case 7:
1666		return IEEE80211_AC_VO;
1667	default:
1668		return -1;
1669	}
1670}
1671
1672/* The firmware will fill in the rate information
1673 * for each packet that gets queued in the hardware
1674 * and these macros will interpret that info.
1675 */
1676
1677#define RI_FORMAT(a)		  (a & 0x0001)
1678#define RI_RATE_ID_MCS(a)	 ((a & 0x01f8) >> 3)
1679
1680static int
1681mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
1682{
1683	struct mwl8k_priv *priv = hw->priv;
1684	struct mwl8k_tx_queue *txq = priv->txq + index;
1685	int processed;
1686
1687	processed = 0;
1688	while (txq->len > 0 && limit--) {
1689		int tx;
1690		struct mwl8k_tx_desc *tx_desc;
1691		unsigned long addr;
1692		int size;
1693		struct sk_buff *skb;
1694		struct ieee80211_tx_info *info;
1695		u32 status;
1696		struct ieee80211_sta *sta;
1697		struct mwl8k_sta *sta_info = NULL;
1698		u16 rate_info;
1699		struct ieee80211_hdr *wh;
1700
1701		tx = txq->head;
1702		tx_desc = txq->txd + tx;
1703
1704		status = le32_to_cpu(tx_desc->status);
1705
1706		if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1707			if (!force)
1708				break;
1709			tx_desc->status &=
1710				~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1711		}
1712
1713		txq->head = (tx + 1) % MWL8K_TX_DESCS;
1714		BUG_ON(txq->len == 0);
1715		txq->len--;
1716		priv->pending_tx_pkts--;
1717
1718		addr = le32_to_cpu(tx_desc->pkt_phys_addr);
1719		size = le16_to_cpu(tx_desc->pkt_len);
1720		skb = txq->skb[tx];
1721		txq->skb[tx] = NULL;
1722
1723		BUG_ON(skb == NULL);
1724		dma_unmap_single(&priv->pdev->dev, addr, size, DMA_TO_DEVICE);
1725
1726		mwl8k_remove_dma_header(skb, tx_desc->qos_control);
1727
1728		wh = (struct ieee80211_hdr *) skb->data;
1729
1730		/* Mark descriptor as unused */
1731		tx_desc->pkt_phys_addr = 0;
1732		tx_desc->pkt_len = 0;
1733
1734		info = IEEE80211_SKB_CB(skb);
1735		if (ieee80211_is_data(wh->frame_control)) {
1736			rcu_read_lock();
1737			sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
1738							   wh->addr2);
1739			if (sta) {
1740				sta_info = MWL8K_STA(sta);
1741				BUG_ON(sta_info == NULL);
1742				rate_info = le16_to_cpu(tx_desc->rate_info);
1743				/* If rate is < 6.5 Mpbs for an ht station
1744				 * do not form an ampdu. If the station is a
1745				 * legacy station (format = 0), do not form an
1746				 * ampdu
1747				 */
1748				if (RI_RATE_ID_MCS(rate_info) < 1 ||
1749				    RI_FORMAT(rate_info) == 0) {
1750					sta_info->is_ampdu_allowed = false;
1751				} else {
1752					sta_info->is_ampdu_allowed = true;
1753				}
1754			}
1755			rcu_read_unlock();
1756		}
1757
1758		ieee80211_tx_info_clear_status(info);
1759
1760		/* Rate control is happening in the firmware.
1761		 * Ensure no tx rate is being reported.
1762		 */
1763		info->status.rates[0].idx = -1;
1764		info->status.rates[0].count = 1;
1765
1766		if (MWL8K_TXD_SUCCESS(status))
1767			info->flags |= IEEE80211_TX_STAT_ACK;
1768
1769		ieee80211_tx_status_irqsafe(hw, skb);
1770
1771		processed++;
1772	}
1773
1774	return processed;
1775}
1776
1777/* must be called only when the card's transmit is completely halted */
1778static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1779{
1780	struct mwl8k_priv *priv = hw->priv;
1781	struct mwl8k_tx_queue *txq = priv->txq + index;
1782
1783	if (txq->txd == NULL)
1784		return;
1785
1786	mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
1787
1788	kfree(txq->skb);
1789	txq->skb = NULL;
1790
1791	dma_free_coherent(&priv->pdev->dev,
1792			  MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
1793			  txq->txd, txq->txd_dma);
1794	txq->txd = NULL;
1795}
1796
1797/* caller must hold priv->stream_lock when calling the stream functions */
1798static struct mwl8k_ampdu_stream *
1799mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1800{
1801	struct mwl8k_ampdu_stream *stream;
1802	struct mwl8k_priv *priv = hw->priv;
1803	int i;
1804
1805	for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
1806		stream = &priv->ampdu[i];
1807		if (stream->state == AMPDU_NO_STREAM) {
1808			stream->sta = sta;
1809			stream->state = AMPDU_STREAM_NEW;
1810			stream->tid = tid;
1811			stream->idx = i;
1812			wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1813				    sta->addr, tid);
1814			return stream;
1815		}
1816	}
1817	return NULL;
1818}
1819
1820static int
1821mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1822{
1823	int ret;
1824
1825	/* if the stream has already been started, don't start it again */
1826	if (stream->state != AMPDU_STREAM_NEW)
1827		return 0;
1828	ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1829	if (ret)
1830		wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1831			    "%d\n", stream->sta->addr, stream->tid, ret);
1832	else
1833		wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1834			    stream->sta->addr, stream->tid);
1835	return ret;
1836}
1837
1838static void
1839mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1840{
1841	wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1842		    stream->tid);
1843	memset(stream, 0, sizeof(*stream));
1844}
1845
1846static struct mwl8k_ampdu_stream *
1847mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1848{
1849	struct mwl8k_priv *priv = hw->priv;
1850	int i;
1851
1852	for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
1853		struct mwl8k_ampdu_stream *stream;
1854		stream = &priv->ampdu[i];
1855		if (stream->state == AMPDU_NO_STREAM)
1856			continue;
1857		if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1858		    stream->tid == tid)
1859			return stream;
1860	}
1861	return NULL;
1862}
1863
1864#define MWL8K_AMPDU_PACKET_THRESHOLD 64
1865static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
1866{
1867	struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1868	struct tx_traffic_info *tx_stats;
1869
1870	BUG_ON(tid >= MWL8K_MAX_TID);
1871	tx_stats = &sta_info->tx_stats[tid];
1872
1873	return sta_info->is_ampdu_allowed &&
1874		tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
1875}
1876
1877static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
1878{
1879	struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1880	struct tx_traffic_info *tx_stats;
1881
1882	BUG_ON(tid >= MWL8K_MAX_TID);
1883	tx_stats = &sta_info->tx_stats[tid];
1884
1885	if (tx_stats->start_time == 0)
1886		tx_stats->start_time = jiffies;
1887
1888	/* reset the packet count after each second elapses.  If the number of
1889	 * packets ever exceeds the ampdu_min_traffic threshold, we will allow
1890	 * an ampdu stream to be started.
1891	 */
1892	if (time_after(jiffies, (unsigned long)tx_stats->start_time + HZ)) {
1893		tx_stats->pkts = 0;
1894		tx_stats->start_time = 0;
1895	} else
1896		tx_stats->pkts++;
1897}
1898
1899/* The hardware ampdu queues start from 5.
1900 * txpriorities for ampdu queues are
1901 * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
1902 * and queue 3 is lowest (queue 4 is reserved)
1903 */
1904#define BA_QUEUE		5
1905
1906static void
1907mwl8k_txq_xmit(struct ieee80211_hw *hw,
1908	       int index,
1909	       struct ieee80211_sta *sta,
1910	       struct sk_buff *skb)
1911{
1912	struct mwl8k_priv *priv = hw->priv;
1913	struct ieee80211_tx_info *tx_info;
1914	struct mwl8k_vif *mwl8k_vif;
1915	struct ieee80211_hdr *wh;
1916	struct mwl8k_tx_queue *txq;
1917	struct mwl8k_tx_desc *tx;
1918	dma_addr_t dma;
1919	u32 txstatus;
1920	u8 txdatarate;
1921	u16 qos;
1922	int txpriority;
1923	u8 tid = 0;
1924	struct mwl8k_ampdu_stream *stream = NULL;
1925	bool start_ba_session = false;
1926	bool mgmtframe = false;
1927	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1928	bool eapol_frame = false;
1929
1930	wh = (struct ieee80211_hdr *)skb->data;
1931	if (ieee80211_is_data_qos(wh->frame_control))
1932		qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1933	else
1934		qos = 0;
1935
1936	if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1937		eapol_frame = true;
1938
1939	if (ieee80211_is_mgmt(wh->frame_control))
1940		mgmtframe = true;
1941
1942	if (priv->ap_fw)
1943		mwl8k_encapsulate_tx_frame(priv, skb);
1944	else
1945		mwl8k_add_dma_header(priv, skb, 0, 0);
1946
1947	wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1948
1949	tx_info = IEEE80211_SKB_CB(skb);
1950	mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
1951
1952	if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1953		wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1954		wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1955		mwl8k_vif->seqno += 0x10;
1956	}
1957
1958	/* Setup firmware control bit fields for each frame type.  */
1959	txstatus = 0;
1960	txdatarate = 0;
1961	if (ieee80211_is_mgmt(wh->frame_control) ||
1962	    ieee80211_is_ctl(wh->frame_control)) {
1963		txdatarate = 0;
1964		qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
1965	} else if (ieee80211_is_data(wh->frame_control)) {
1966		txdatarate = 1;
1967		if (is_multicast_ether_addr(wh->addr1))
1968			txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1969
1970		qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
1971		if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1972			qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
1973		else
1974			qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
1975	}
1976
1977	/* Queue ADDBA request in the respective data queue.  While setting up
1978	 * the ampdu stream, mac80211 queues further packets for that
1979	 * particular ra/tid pair.  However, packets piled up in the hardware
1980	 * for that ra/tid pair will still go out. ADDBA request and the
1981	 * related data packets going out from different queues asynchronously
1982	 * will cause a shift in the receiver window which might result in
1983	 * ampdu packets getting dropped at the receiver after the stream has
1984	 * been setup.
1985	 */
1986	if (unlikely(ieee80211_is_action(wh->frame_control) &&
1987	    mgmt->u.action.category == WLAN_CATEGORY_BACK &&
1988	    mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
1989	    priv->ap_fw)) {
1990		u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1991		tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1992		index = mwl8k_tid_queue_mapping(tid);
1993	}
1994
1995	txpriority = index;
1996
1997	if (priv->ap_fw && sta && sta->deflink.ht_cap.ht_supported && !eapol_frame &&
1998	    ieee80211_is_data_qos(wh->frame_control)) {
1999		tid = qos & 0xf;
2000		mwl8k_tx_count_packet(sta, tid);
2001		spin_lock(&priv->stream_lock);
2002		stream = mwl8k_lookup_stream(hw, sta->addr, tid);
2003		if (stream != NULL) {
2004			if (stream->state == AMPDU_STREAM_ACTIVE) {
2005				WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
2006				txpriority = (BA_QUEUE + stream->idx) %
2007					     TOTAL_HW_TX_QUEUES;
2008				if (stream->idx <= 1)
2009					index = stream->idx +
2010						MWL8K_TX_WMM_QUEUES;
2011
2012			} else if (stream->state == AMPDU_STREAM_NEW) {
2013				/* We get here if the driver sends us packets
2014				 * after we've initiated a stream, but before
2015				 * our ampdu_action routine has been called
2016				 * with IEEE80211_AMPDU_TX_START to get the SSN
2017				 * for the ADDBA request.  So this packet can
2018				 * go out with no risk of sequence number
2019				 * mismatch.  No special handling is required.
2020				 */
2021			} else {
2022				/* Drop packets that would go out after the
2023				 * ADDBA request was sent but before the ADDBA
2024				 * response is received.  If we don't do this,
2025				 * the recipient would probably receive it
2026				 * after the ADDBA request with SSN 0.  This
2027				 * will cause the recipient's BA receive window
2028				 * to shift, which would cause the subsequent
2029				 * packets in the BA stream to be discarded.
2030				 * mac80211 queues our packets for us in this
2031				 * case, so this is really just a safety check.
2032				 */
2033				wiphy_warn(hw->wiphy,
2034					   "Cannot send packet while ADDBA "
2035					   "dialog is underway.\n");
2036				spin_unlock(&priv->stream_lock);
2037				dev_kfree_skb(skb);
2038				return;
2039			}
2040		} else {
2041			/* Defer calling mwl8k_start_stream so that the current
2042			 * skb can go out before the ADDBA request.  This
2043			 * prevents sequence number mismatch at the recepient
2044			 * as described above.
2045			 */
2046			if (mwl8k_ampdu_allowed(sta, tid)) {
2047				stream = mwl8k_add_stream(hw, sta, tid);
2048				if (stream != NULL)
2049					start_ba_session = true;
2050			}
2051		}
2052		spin_unlock(&priv->stream_lock);
2053	} else {
2054		qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
2055		qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
2056	}
2057
2058	dma = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
2059			     DMA_TO_DEVICE);
2060
2061	if (dma_mapping_error(&priv->pdev->dev, dma)) {
2062		wiphy_debug(hw->wiphy,
2063			    "failed to dma map skb, dropping TX frame.\n");
2064		if (start_ba_session) {
2065			spin_lock(&priv->stream_lock);
2066			mwl8k_remove_stream(hw, stream);
2067			spin_unlock(&priv->stream_lock);
2068		}
2069		dev_kfree_skb(skb);
2070		return;
2071	}
2072
2073	spin_lock_bh(&priv->tx_lock);
2074
2075	txq = priv->txq + index;
2076
2077	/* Mgmt frames that go out frequently are probe
2078	 * responses. Other mgmt frames got out relatively
2079	 * infrequently. Hence reserve 2 buffers so that
2080	 * other mgmt frames do not get dropped due to an
2081	 * already queued probe response in one of the
2082	 * reserved buffers.
2083	 */
2084
2085	if (txq->len >= MWL8K_TX_DESCS - 2) {
2086		if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
2087			if (start_ba_session) {
2088				spin_lock(&priv->stream_lock);
2089				mwl8k_remove_stream(hw, stream);
2090				spin_unlock(&priv->stream_lock);
2091			}
2092			mwl8k_tx_start(priv);
2093			spin_unlock_bh(&priv->tx_lock);
2094			dma_unmap_single(&priv->pdev->dev, dma, skb->len,
2095					 DMA_TO_DEVICE);
2096			dev_kfree_skb(skb);
2097			return;
2098		}
2099	}
2100
2101	BUG_ON(txq->skb[txq->tail] != NULL);
2102	txq->skb[txq->tail] = skb;
2103
2104	tx = txq->txd + txq->tail;
2105	tx->data_rate = txdatarate;
2106	tx->tx_priority = txpriority;
2107	tx->qos_control = cpu_to_le16(qos);
2108	tx->pkt_phys_addr = cpu_to_le32(dma);
2109	tx->pkt_len = cpu_to_le16(skb->len);
2110	tx->rate_info = 0;
2111	if (!priv->ap_fw && sta != NULL)
2112		tx->peer_id = MWL8K_STA(sta)->peer_id;
2113	else
2114		tx->peer_id = 0;
2115
2116	if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
2117		tx->timestamp = cpu_to_le32(ioread32(priv->regs +
2118						MWL8K_HW_TIMER_REGISTER));
2119	else
2120		tx->timestamp = 0;
2121
2122	wmb();
2123	tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
2124
2125	txq->len++;
2126	priv->pending_tx_pkts++;
2127
2128	txq->tail++;
2129	if (txq->tail == MWL8K_TX_DESCS)
2130		txq->tail = 0;
2131
2132	mwl8k_tx_start(priv);
2133
2134	spin_unlock_bh(&priv->tx_lock);
2135
2136	/* Initiate the ampdu session here */
2137	if (start_ba_session) {
2138		spin_lock(&priv->stream_lock);
2139		if (mwl8k_start_stream(hw, stream))
2140			mwl8k_remove_stream(hw, stream);
2141		spin_unlock(&priv->stream_lock);
2142	}
2143}
2144
2145
2146/*
2147 * Firmware access.
2148 *
2149 * We have the following requirements for issuing firmware commands:
2150 * - Some commands require that the packet transmit path is idle when
2151 *   the command is issued.  (For simplicity, we'll just quiesce the
2152 *   transmit path for every command.)
2153 * - There are certain sequences of commands that need to be issued to
2154 *   the hardware sequentially, with no other intervening commands.
2155 *
2156 * This leads to an implementation of a "firmware lock" as a mutex that
2157 * can be taken recursively, and which is taken by both the low-level
2158 * command submission function (mwl8k_post_cmd) as well as any users of
2159 * that function that require issuing of an atomic sequence of commands,
2160 * and quiesces the transmit path whenever it's taken.
2161 */
2162static int mwl8k_fw_lock(struct ieee80211_hw *hw)
2163{
2164	struct mwl8k_priv *priv = hw->priv;
2165
2166	if (priv->fw_mutex_owner != current) {
2167		int rc;
2168
2169		mutex_lock(&priv->fw_mutex);
2170		ieee80211_stop_queues(hw);
2171
2172		rc = mwl8k_tx_wait_empty(hw);
2173		if (rc) {
2174			if (!priv->hw_restart_in_progress)
2175				ieee80211_wake_queues(hw);
2176
2177			mutex_unlock(&priv->fw_mutex);
2178
2179			return rc;
2180		}
2181
2182		priv->fw_mutex_owner = current;
2183	}
2184
2185	priv->fw_mutex_depth++;
2186
2187	return 0;
2188}
2189
2190static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
2191{
2192	struct mwl8k_priv *priv = hw->priv;
2193
2194	if (!--priv->fw_mutex_depth) {
2195		if (!priv->hw_restart_in_progress)
2196			ieee80211_wake_queues(hw);
2197
2198		priv->fw_mutex_owner = NULL;
2199		mutex_unlock(&priv->fw_mutex);
2200	}
2201}
2202
2203static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
2204			       u32 bitmap);
2205
2206/*
2207 * Command processing.
2208 */
2209
2210/* Timeout firmware commands after 10s */
2211#define MWL8K_CMD_TIMEOUT_MS	10000
2212
2213static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt_hdr *cmd)
2214{
2215	DECLARE_COMPLETION_ONSTACK(cmd_wait);
2216	struct mwl8k_priv *priv = hw->priv;
2217	void __iomem *regs = priv->regs;
2218	dma_addr_t dma_addr;
2219	unsigned int dma_size;
2220	int rc;
2221	unsigned long time_left = 0;
2222	u8 buf[32];
2223	u32 bitmap = 0;
2224
2225	wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
2226		  mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
2227
2228	/* Before posting firmware commands that could change the hardware
2229	 * characteristics, make sure that all BSSes are stopped temporary.
2230	 * Enable these stopped BSSes after completion of the commands
2231	 */
2232
2233	rc = mwl8k_fw_lock(hw);
2234	if (rc)
2235		return rc;
2236
2237	if (priv->ap_fw && priv->running_bsses) {
2238		switch (le16_to_cpu(cmd->code)) {
2239		case MWL8K_CMD_SET_RF_CHANNEL:
2240		case MWL8K_CMD_RADIO_CONTROL:
2241		case MWL8K_CMD_RF_TX_POWER:
2242		case MWL8K_CMD_TX_POWER:
2243		case MWL8K_CMD_RF_ANTENNA:
2244		case MWL8K_CMD_RTS_THRESHOLD:
2245		case MWL8K_CMD_MIMO_CONFIG:
2246			bitmap = priv->running_bsses;
2247			mwl8k_enable_bsses(hw, false, bitmap);
2248			break;
2249		}
2250	}
2251
2252	cmd->result = (__force __le16) 0xffff;
2253	dma_size = le16_to_cpu(cmd->length);
2254	dma_addr = dma_map_single(&priv->pdev->dev, cmd, dma_size,
2255				  DMA_BIDIRECTIONAL);
2256	if (dma_mapping_error(&priv->pdev->dev, dma_addr)) {
2257		rc = -ENOMEM;
2258		goto exit;
2259	}
2260
2261	priv->hostcmd_wait = &cmd_wait;
2262	iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
2263	iowrite32(MWL8K_H2A_INT_DOORBELL,
2264		regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2265	iowrite32(MWL8K_H2A_INT_DUMMY,
2266		regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2267
2268	time_left = wait_for_completion_timeout(&cmd_wait,
2269						msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
2270
2271	priv->hostcmd_wait = NULL;
2272
2273
2274	dma_unmap_single(&priv->pdev->dev, dma_addr, dma_size,
2275			 DMA_BIDIRECTIONAL);
2276
2277	if (!time_left) {
2278		wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
2279			  mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2280			  MWL8K_CMD_TIMEOUT_MS);
2281		rc = -ETIMEDOUT;
2282	} else {
2283		int ms;
2284
2285		ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(time_left);
2286
2287		rc = cmd->result ? -EINVAL : 0;
2288		if (rc)
2289			wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
2290				  mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2291				  le16_to_cpu(cmd->result));
2292		else if (ms > 2000)
2293			wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
2294				     mwl8k_cmd_name(cmd->code,
2295						    buf, sizeof(buf)),
2296				     ms);
2297	}
2298
2299exit:
2300	if (bitmap)
2301		mwl8k_enable_bsses(hw, true, bitmap);
2302
2303	mwl8k_fw_unlock(hw);
2304
2305	return rc;
2306}
2307
2308static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
2309				 struct ieee80211_vif *vif,
2310				 struct mwl8k_cmd_pkt_hdr *cmd)
2311{
2312	if (vif != NULL)
2313		cmd->macid = MWL8K_VIF(vif)->macid;
2314	return mwl8k_post_cmd(hw, cmd);
2315}
2316
2317/*
2318 * Setup code shared between STA and AP firmware images.
2319 */
2320static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2321{
2322	struct mwl8k_priv *priv = hw->priv;
2323
2324	BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2325	memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2326
2327	BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2328	memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2329
2330	priv->band_24.band = NL80211_BAND_2GHZ;
2331	priv->band_24.channels = priv->channels_24;
2332	priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2333	priv->band_24.bitrates = priv->rates_24;
2334	priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2335
2336	hw->wiphy->bands[NL80211_BAND_2GHZ] = &priv->band_24;
2337}
2338
2339static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2340{
2341	struct mwl8k_priv *priv = hw->priv;
2342
2343	BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2344	memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2345
2346	BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2347	memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2348
2349	priv->band_50.band = NL80211_BAND_5GHZ;
2350	priv->band_50.channels = priv->channels_50;
2351	priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2352	priv->band_50.bitrates = priv->rates_50;
2353	priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2354
2355	hw->wiphy->bands[NL80211_BAND_5GHZ] = &priv->band_50;
2356}
2357
2358/*
2359 * CMD_GET_HW_SPEC (STA version).
2360 */
2361struct mwl8k_cmd_get_hw_spec_sta {
2362	struct mwl8k_cmd_pkt_hdr header;
2363	__u8 hw_rev;
2364	__u8 host_interface;
2365	__le16 num_mcaddrs;
2366	__u8 perm_addr[ETH_ALEN];
2367	__le16 region_code;
2368	__le32 fw_rev;
2369	__le32 ps_cookie;
2370	__le32 caps;
2371	__u8 mcs_bitmap[16];
2372	__le32 rx_queue_ptr;
2373	__le32 num_tx_queues;
2374	__le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
2375	__le32 caps2;
2376	__le32 num_tx_desc_per_queue;
2377	__le32 total_rxd;
2378} __packed;
2379
2380#define MWL8K_CAP_MAX_AMSDU		0x20000000
2381#define MWL8K_CAP_GREENFIELD		0x08000000
2382#define MWL8K_CAP_AMPDU			0x04000000
2383#define MWL8K_CAP_RX_STBC		0x01000000
2384#define MWL8K_CAP_TX_STBC		0x00800000
2385#define MWL8K_CAP_SHORTGI_40MHZ		0x00400000
2386#define MWL8K_CAP_SHORTGI_20MHZ		0x00200000
2387#define MWL8K_CAP_RX_ANTENNA_MASK	0x000e0000
2388#define MWL8K_CAP_TX_ANTENNA_MASK	0x0001c000
2389#define MWL8K_CAP_DELAY_BA		0x00003000
2390#define MWL8K_CAP_MIMO			0x00000200
2391#define MWL8K_CAP_40MHZ			0x00000100
2392#define MWL8K_CAP_BAND_MASK		0x00000007
2393#define MWL8K_CAP_5GHZ			0x00000004
2394#define MWL8K_CAP_2GHZ4			0x00000001
2395
2396static void
2397mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2398		  struct ieee80211_supported_band *band, u32 cap)
2399{
2400	int rx_streams;
2401	int tx_streams;
2402
2403	band->ht_cap.ht_supported = 1;
2404
2405	if (cap & MWL8K_CAP_MAX_AMSDU)
2406		band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
2407	if (cap & MWL8K_CAP_GREENFIELD)
2408		band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
2409	if (cap & MWL8K_CAP_AMPDU) {
2410		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2411		band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2412		band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2413	}
2414	if (cap & MWL8K_CAP_RX_STBC)
2415		band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
2416	if (cap & MWL8K_CAP_TX_STBC)
2417		band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
2418	if (cap & MWL8K_CAP_SHORTGI_40MHZ)
2419		band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
2420	if (cap & MWL8K_CAP_SHORTGI_20MHZ)
2421		band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
2422	if (cap & MWL8K_CAP_DELAY_BA)
2423		band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
2424	if (cap & MWL8K_CAP_40MHZ)
2425		band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2426
2427	rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2428	tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2429
2430	band->ht_cap.mcs.rx_mask[0] = 0xff;
2431	if (rx_streams >= 2)
2432		band->ht_cap.mcs.rx_mask[1] = 0xff;
2433	if (rx_streams >= 3)
2434		band->ht_cap.mcs.rx_mask[2] = 0xff;
2435	band->ht_cap.mcs.rx_mask[4] = 0x01;
2436	band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2437
2438	if (rx_streams != tx_streams) {
2439		band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2440		band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
2441				IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2442	}
2443}
2444
2445static void
2446mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2447{
2448	struct mwl8k_priv *priv = hw->priv;
2449
2450	if (priv->caps)
2451		return;
2452
2453	if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2454		mwl8k_setup_2ghz_band(hw);
2455		if (caps & MWL8K_CAP_MIMO)
2456			mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2457	}
2458
2459	if (caps & MWL8K_CAP_5GHZ) {
2460		mwl8k_setup_5ghz_band(hw);
2461		if (caps & MWL8K_CAP_MIMO)
2462			mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2463	}
2464
2465	priv->caps = caps;
2466}
2467
2468static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
2469{
2470	struct mwl8k_priv *priv = hw->priv;
2471	struct mwl8k_cmd_get_hw_spec_sta *cmd;
2472	int rc;
2473	int i;
2474
2475	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2476	if (cmd == NULL)
2477		return -ENOMEM;
2478
2479	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2480	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2481
2482	memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2483	cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2484	cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2485	cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2486	for (i = 0; i < mwl8k_tx_queues(priv); i++)
2487		cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
2488	cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2489	cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2490
2491	rc = mwl8k_post_cmd(hw, &cmd->header);
2492
2493	if (!rc) {
2494		SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2495		priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2496		priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2497		priv->hw_rev = cmd->hw_rev;
2498		mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
2499		priv->ap_macids_supported = 0x00000000;
2500		priv->sta_macids_supported = 0x00000001;
2501	}
2502
2503	kfree(cmd);
2504	return rc;
2505}
2506
2507/*
2508 * CMD_GET_HW_SPEC (AP version).
2509 */
2510struct mwl8k_cmd_get_hw_spec_ap {
2511	struct mwl8k_cmd_pkt_hdr header;
2512	__u8 hw_rev;
2513	__u8 host_interface;
2514	__le16 num_wcb;
2515	__le16 num_mcaddrs;
2516	__u8 perm_addr[ETH_ALEN];
2517	__le16 region_code;
2518	__le16 num_antenna;
2519	__le32 fw_rev;
2520	__le32 wcbbase0;
2521	__le32 rxwrptr;
2522	__le32 rxrdptr;
2523	__le32 ps_cookie;
2524	__le32 wcbbase1;
2525	__le32 wcbbase2;
2526	__le32 wcbbase3;
2527	__le32 fw_api_version;
2528	__le32 caps;
2529	__le32 num_of_ampdu_queues;
2530	__le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
2531} __packed;
2532
2533static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2534{
2535	struct mwl8k_priv *priv = hw->priv;
2536	struct mwl8k_cmd_get_hw_spec_ap *cmd;
2537	int rc, i;
2538	u32 api_version;
2539
2540	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2541	if (cmd == NULL)
2542		return -ENOMEM;
2543
2544	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2545	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2546
2547	memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2548	cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2549
2550	rc = mwl8k_post_cmd(hw, &cmd->header);
2551
2552	if (!rc) {
2553		int off;
2554
2555		api_version = le32_to_cpu(cmd->fw_api_version);
2556		if (priv->device_info->fw_api_ap != api_version) {
2557			printk(KERN_ERR "%s: Unsupported fw API version for %s."
2558			       "  Expected %d got %d.\n", MWL8K_NAME,
2559			       priv->device_info->part_name,
2560			       priv->device_info->fw_api_ap,
2561			       api_version);
2562			rc = -EINVAL;
2563			goto done;
2564		}
2565		SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2566		priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2567		priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2568		priv->hw_rev = cmd->hw_rev;
2569		mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
2570		priv->ap_macids_supported = 0x000000ff;
2571		priv->sta_macids_supported = 0x00000100;
2572		priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2573		if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2574			wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2575				   " but we only support %d.\n",
2576				   priv->num_ampdu_queues,
2577				   MWL8K_MAX_AMPDU_QUEUES);
2578			priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2579		}
2580		off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
2581		iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
2582
2583		off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
2584		iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
2585
2586		priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2587		priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2588		priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2589		priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
2590
2591		for (i = 0; i < priv->num_ampdu_queues; i++)
2592			priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
2593				le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
2594	}
2595
2596done:
2597	kfree(cmd);
2598	return rc;
2599}
2600
2601/*
2602 * CMD_SET_HW_SPEC.
2603 */
2604struct mwl8k_cmd_set_hw_spec {
2605	struct mwl8k_cmd_pkt_hdr header;
2606	__u8 hw_rev;
2607	__u8 host_interface;
2608	__le16 num_mcaddrs;
2609	__u8 perm_addr[ETH_ALEN];
2610	__le16 region_code;
2611	__le32 fw_rev;
2612	__le32 ps_cookie;
2613	__le32 caps;
2614	__le32 rx_queue_ptr;
2615	__le32 num_tx_queues;
2616	__le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
2617	__le32 flags;
2618	__le32 num_tx_desc_per_queue;
2619	__le32 total_rxd;
2620} __packed;
2621
2622/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2623 * packets to expire 500 ms after the timestamp in the tx descriptor.  That is,
2624 * the packets that are queued for more than 500ms, will be dropped in the
2625 * hardware. This helps minimizing the issues caused due to head-of-line
2626 * blocking where a slow client can hog the bandwidth and affect traffic to a
2627 * faster client.
2628 */
2629#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY	0x00000400
2630#define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR	0x00000200
2631#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT		0x00000080
2632#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP	0x00000020
2633#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON		0x00000010
2634
2635static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2636{
2637	struct mwl8k_priv *priv = hw->priv;
2638	struct mwl8k_cmd_set_hw_spec *cmd;
2639	int rc;
2640	int i;
2641
2642	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2643	if (cmd == NULL)
2644		return -ENOMEM;
2645
2646	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2647	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2648
2649	cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2650	cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2651	cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2652
2653	/*
2654	 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2655	 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2656	 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2657	 * priority is interpreted the right way in firmware.
2658	 */
2659	for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2660		int j = mwl8k_tx_queues(priv) - 1 - i;
2661		cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2662	}
2663
2664	cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2665				 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2666				 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
2667				 MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
2668				 MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
2669	cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2670	cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2671
2672	rc = mwl8k_post_cmd(hw, &cmd->header);
2673	kfree(cmd);
2674
2675	return rc;
2676}
2677
2678/*
2679 * CMD_MAC_MULTICAST_ADR.
2680 */
2681struct mwl8k_cmd_mac_multicast_adr {
2682	struct mwl8k_cmd_pkt_hdr header;
2683	__le16 action;
2684	__le16 numaddr;
2685	__u8 addr[][ETH_ALEN];
2686};
2687
2688#define MWL8K_ENABLE_RX_DIRECTED	0x0001
2689#define MWL8K_ENABLE_RX_MULTICAST	0x0002
2690#define MWL8K_ENABLE_RX_ALL_MULTICAST	0x0004
2691#define MWL8K_ENABLE_RX_BROADCAST	0x0008
2692
2693static struct mwl8k_cmd_pkt_hdr *
2694__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
2695			      struct netdev_hw_addr_list *mc_list)
2696{
2697	struct mwl8k_priv *priv = hw->priv;
2698	struct mwl8k_cmd_mac_multicast_adr *cmd;
2699	int size;
2700	int mc_count = 0;
2701
2702	if (mc_list)
2703		mc_count = netdev_hw_addr_list_count(mc_list);
2704
2705	if (allmulti || mc_count > priv->num_mcaddrs) {
2706		allmulti = 1;
2707		mc_count = 0;
2708	}
2709
2710	size = sizeof(*cmd) + mc_count * ETH_ALEN;
2711
2712	cmd = kzalloc(size, GFP_ATOMIC);
2713	if (cmd == NULL)
2714		return NULL;
2715
2716	cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2717	cmd->header.length = cpu_to_le16(size);
2718	cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2719				  MWL8K_ENABLE_RX_BROADCAST);
2720
2721	if (allmulti) {
2722		cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2723	} else if (mc_count) {
2724		struct netdev_hw_addr *ha;
2725		int i = 0;
2726
2727		cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2728		cmd->numaddr = cpu_to_le16(mc_count);
2729		netdev_hw_addr_list_for_each(ha, mc_list) {
2730			memcpy(cmd->addr[i++], ha->addr, ETH_ALEN);
2731		}
2732	}
2733
2734	return &cmd->header;
2735}
2736
2737/*
2738 * CMD_GET_STAT.
2739 */
2740struct mwl8k_cmd_get_stat {
2741	struct mwl8k_cmd_pkt_hdr header;
2742	__le32 stats[64];
2743} __packed;
2744
2745#define MWL8K_STAT_ACK_FAILURE	9
2746#define MWL8K_STAT_RTS_FAILURE	12
2747#define MWL8K_STAT_FCS_ERROR	24
2748#define MWL8K_STAT_RTS_SUCCESS	11
2749
2750static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2751			      struct ieee80211_low_level_stats *stats)
2752{
2753	struct mwl8k_cmd_get_stat *cmd;
2754	int rc;
2755
2756	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2757	if (cmd == NULL)
2758		return -ENOMEM;
2759
2760	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2761	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2762
2763	rc = mwl8k_post_cmd(hw, &cmd->header);
2764	if (!rc) {
2765		stats->dot11ACKFailureCount =
2766			le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2767		stats->dot11RTSFailureCount =
2768			le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2769		stats->dot11FCSErrorCount =
2770			le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2771		stats->dot11RTSSuccessCount =
2772			le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2773	}
2774	kfree(cmd);
2775
2776	return rc;
2777}
2778
2779/*
2780 * CMD_RADIO_CONTROL.
2781 */
2782struct mwl8k_cmd_radio_control {
2783	struct mwl8k_cmd_pkt_hdr header;
2784	__le16 action;
2785	__le16 control;
2786	__le16 radio_on;
2787} __packed;
2788
2789static int
2790mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
2791{
2792	struct mwl8k_priv *priv = hw->priv;
2793	struct mwl8k_cmd_radio_control *cmd;
2794	int rc;
2795
2796	if (enable == priv->radio_on && !force)
2797		return 0;
2798
2799	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2800	if (cmd == NULL)
2801		return -ENOMEM;
2802
2803	cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2804	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2805	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2806	cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
2807	cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2808
2809	rc = mwl8k_post_cmd(hw, &cmd->header);
2810	kfree(cmd);
2811
2812	if (!rc)
2813		priv->radio_on = enable;
2814
2815	return rc;
2816}
2817
2818static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
2819{
2820	return mwl8k_cmd_radio_control(hw, 0, 0);
2821}
2822
2823static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
2824{
2825	return mwl8k_cmd_radio_control(hw, 1, 0);
2826}
2827
2828static int
2829mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2830{
2831	struct mwl8k_priv *priv = hw->priv;
2832
2833	priv->radio_short_preamble = short_preamble;
2834
2835	return mwl8k_cmd_radio_control(hw, 1, 1);
2836}
2837
2838/*
2839 * CMD_RF_TX_POWER.
2840 */
2841#define MWL8K_RF_TX_POWER_LEVEL_TOTAL	8
2842
2843struct mwl8k_cmd_rf_tx_power {
2844	struct mwl8k_cmd_pkt_hdr header;
2845	__le16 action;
2846	__le16 support_level;
2847	__le16 current_level;
2848	__le16 reserved;
2849	__le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
2850} __packed;
2851
2852static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
2853{
2854	struct mwl8k_cmd_rf_tx_power *cmd;
2855	int rc;
2856
2857	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2858	if (cmd == NULL)
2859		return -ENOMEM;
2860
2861	cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2862	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2863	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2864	cmd->support_level = cpu_to_le16(dBm);
2865
2866	rc = mwl8k_post_cmd(hw, &cmd->header);
2867	kfree(cmd);
2868
2869	return rc;
2870}
2871
2872/*
2873 * CMD_TX_POWER.
2874 */
2875#define MWL8K_TX_POWER_LEVEL_TOTAL      12
2876
2877struct mwl8k_cmd_tx_power {
2878	struct mwl8k_cmd_pkt_hdr header;
2879	__le16 action;
2880	__le16 band;
2881	__le16 channel;
2882	__le16 bw;
2883	__le16 sub_ch;
2884	__le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2885} __packed;
2886
2887static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2888				     struct ieee80211_conf *conf,
2889				     unsigned short pwr)
2890{
2891	struct ieee80211_channel *channel = conf->chandef.chan;
2892	enum nl80211_channel_type channel_type =
2893		cfg80211_get_chandef_type(&conf->chandef);
2894	struct mwl8k_cmd_tx_power *cmd;
2895	int rc;
2896	int i;
2897
2898	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2899	if (cmd == NULL)
2900		return -ENOMEM;
2901
2902	cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2903	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2904	cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2905
2906	if (channel->band == NL80211_BAND_2GHZ)
2907		cmd->band = cpu_to_le16(0x1);
2908	else if (channel->band == NL80211_BAND_5GHZ)
2909		cmd->band = cpu_to_le16(0x4);
2910
2911	cmd->channel = cpu_to_le16(channel->hw_value);
2912
2913	if (channel_type == NL80211_CHAN_NO_HT ||
2914	    channel_type == NL80211_CHAN_HT20) {
2915		cmd->bw = cpu_to_le16(0x2);
2916	} else {
2917		cmd->bw = cpu_to_le16(0x4);
2918		if (channel_type == NL80211_CHAN_HT40MINUS)
2919			cmd->sub_ch = cpu_to_le16(0x3);
2920		else if (channel_type == NL80211_CHAN_HT40PLUS)
2921			cmd->sub_ch = cpu_to_le16(0x1);
2922	}
2923
2924	for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2925		cmd->power_level_list[i] = cpu_to_le16(pwr);
2926
2927	rc = mwl8k_post_cmd(hw, &cmd->header);
2928	kfree(cmd);
2929
2930	return rc;
2931}
2932
2933/*
2934 * CMD_RF_ANTENNA.
2935 */
2936struct mwl8k_cmd_rf_antenna {
2937	struct mwl8k_cmd_pkt_hdr header;
2938	__le16 antenna;
2939	__le16 mode;
2940} __packed;
2941
2942#define MWL8K_RF_ANTENNA_RX		1
2943#define MWL8K_RF_ANTENNA_TX		2
2944
2945static int
2946mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2947{
2948	struct mwl8k_cmd_rf_antenna *cmd;
2949	int rc;
2950
2951	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2952	if (cmd == NULL)
2953		return -ENOMEM;
2954
2955	cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2956	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2957	cmd->antenna = cpu_to_le16(antenna);
2958	cmd->mode = cpu_to_le16(mask);
2959
2960	rc = mwl8k_post_cmd(hw, &cmd->header);
2961	kfree(cmd);
2962
2963	return rc;
2964}
2965
2966/*
2967 * CMD_SET_BEACON.
2968 */
2969
2970static bool mwl8k_beacon_has_ds_params(const u8 *buf, int len)
2971{
2972	const struct ieee80211_mgmt *mgmt = (const void *)buf;
2973	int ies_len;
2974
2975	if (len <= offsetof(struct ieee80211_mgmt, u.beacon.variable))
2976		return false;
2977
2978	ies_len = len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
2979
2980	return cfg80211_find_ie(WLAN_EID_DS_PARAMS, mgmt->u.beacon.variable,
2981				ies_len) != NULL;
2982}
2983
2984static void mwl8k_beacon_copy_inject_ds_params(struct ieee80211_hw *hw,
2985					       u8 *buf_dst, const u8 *buf_src,
2986					       int src_len)
2987{
2988	const struct ieee80211_mgmt *mgmt = (const void *)buf_src;
2989	static const u8 before_ds_params[] = {
2990		WLAN_EID_SSID,
2991		WLAN_EID_SUPP_RATES,
2992	};
2993	const u8 *ies;
2994	int hdr_len, left, offs, pos;
2995
2996	ies = mgmt->u.beacon.variable;
2997	hdr_len = offsetof(struct ieee80211_mgmt, u.beacon.variable);
2998
2999	offs = ieee80211_ie_split(ies, src_len - hdr_len, before_ds_params,
3000				  ARRAY_SIZE(before_ds_params), 0);
3001
3002	pos = hdr_len + offs;
3003	left = src_len - pos;
3004
3005	memcpy(buf_dst, buf_src, pos);
3006
3007	/* Inject a DSSS Parameter Set after SSID + Supp Rates */
3008	buf_dst[pos + 0] = WLAN_EID_DS_PARAMS;
3009	buf_dst[pos + 1] = 1;
3010	buf_dst[pos + 2] = hw->conf.chandef.chan->hw_value;
3011
3012	memcpy(buf_dst + pos + 3, buf_src + pos, left);
3013}
3014struct mwl8k_cmd_set_beacon {
3015	struct mwl8k_cmd_pkt_hdr header;
3016	__le16 beacon_len;
3017	__u8 beacon[];
3018};
3019
3020static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
3021				struct ieee80211_vif *vif, u8 *beacon, int len)
3022{
3023	bool ds_params_present = mwl8k_beacon_has_ds_params(beacon, len);
3024	struct mwl8k_cmd_set_beacon *cmd;
3025	int rc, final_len = len;
3026
3027	if (!ds_params_present) {
3028		/*
3029		 * mwl8k firmware requires a DS Params IE with the current
3030		 * channel in AP beacons. If mac80211/hostapd does not
3031		 * include it, inject one here. IE ID + length + channel
3032		 * number = 3 bytes.
3033		 */
3034		final_len += 3;
3035	}
3036
3037	cmd = kzalloc(sizeof(*cmd) + final_len, GFP_KERNEL);
3038	if (cmd == NULL)
3039		return -ENOMEM;
3040
3041	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
3042	cmd->header.length = cpu_to_le16(sizeof(*cmd) + final_len);
3043	cmd->beacon_len = cpu_to_le16(final_len);
3044
3045	if (ds_params_present)
3046		memcpy(cmd->beacon, beacon, len);
3047	else
3048		mwl8k_beacon_copy_inject_ds_params(hw, cmd->beacon, beacon,
3049						   len);
3050
3051	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3052	kfree(cmd);
3053
3054	return rc;
3055}
3056
3057/*
3058 * CMD_SET_PRE_SCAN.
3059 */
3060struct mwl8k_cmd_set_pre_scan {
3061	struct mwl8k_cmd_pkt_hdr header;
3062} __packed;
3063
3064static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
3065{
3066	struct mwl8k_cmd_set_pre_scan *cmd;
3067	int rc;
3068
3069	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3070	if (cmd == NULL)
3071		return -ENOMEM;
3072
3073	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
3074	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3075
3076	rc = mwl8k_post_cmd(hw, &cmd->header);
3077	kfree(cmd);
3078
3079	return rc;
3080}
3081
3082/*
3083 * CMD_BBP_REG_ACCESS.
3084 */
3085struct mwl8k_cmd_bbp_reg_access {
3086	struct mwl8k_cmd_pkt_hdr header;
3087	__le16 action;
3088	__le16 offset;
3089	u8 value;
3090	u8 rsrv[3];
3091} __packed;
3092
3093static int
3094mwl8k_cmd_bbp_reg_access(struct ieee80211_hw *hw,
3095			 u16 action,
3096			 u16 offset,
3097			 u8 *value)
3098{
3099	struct mwl8k_cmd_bbp_reg_access *cmd;
3100	int rc;
3101
3102	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3103	if (cmd == NULL)
3104		return -ENOMEM;
3105
3106	cmd->header.code = cpu_to_le16(MWL8K_CMD_BBP_REG_ACCESS);
3107	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3108	cmd->action = cpu_to_le16(action);
3109	cmd->offset = cpu_to_le16(offset);
3110
3111	rc = mwl8k_post_cmd(hw, &cmd->header);
3112
3113	if (!rc)
3114		*value = cmd->value;
3115	else
3116		*value = 0;
3117
3118	kfree(cmd);
3119
3120	return rc;
3121}
3122
3123/*
3124 * CMD_SET_POST_SCAN.
3125 */
3126struct mwl8k_cmd_set_post_scan {
3127	struct mwl8k_cmd_pkt_hdr header;
3128	__le32 isibss;
3129	__u8 bssid[ETH_ALEN];
3130} __packed;
3131
3132static int
3133mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
3134{
3135	struct mwl8k_cmd_set_post_scan *cmd;
3136	int rc;
3137
3138	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3139	if (cmd == NULL)
3140		return -ENOMEM;
3141
3142	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
3143	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3144	cmd->isibss = 0;
3145	memcpy(cmd->bssid, mac, ETH_ALEN);
3146
3147	rc = mwl8k_post_cmd(hw, &cmd->header);
3148	kfree(cmd);
3149
3150	return rc;
3151}
3152
3153static int freq_to_idx(struct mwl8k_priv *priv, int freq)
3154{
3155	struct ieee80211_supported_band *sband;
3156	int band, ch, idx = 0;
3157
3158	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3159		sband = priv->hw->wiphy->bands[band];
3160		if (!sband)
3161			continue;
3162
3163		for (ch = 0; ch < sband->n_channels; ch++, idx++)
3164			if (sband->channels[ch].center_freq == freq)
3165				goto exit;
3166	}
3167
3168exit:
3169	return idx;
3170}
3171
3172static void mwl8k_update_survey(struct mwl8k_priv *priv,
3173				struct ieee80211_channel *channel)
3174{
3175	u32 cca_cnt, rx_rdy;
3176	s8 nf = 0, idx;
3177	struct survey_info *survey;
3178
3179	idx = freq_to_idx(priv, priv->acs_chan->center_freq);
3180	if (idx >= MWL8K_NUM_CHANS) {
3181		wiphy_err(priv->hw->wiphy, "Failed to update survey\n");
3182		return;
3183	}
3184
3185	survey = &priv->survey[idx];
3186
3187	cca_cnt = ioread32(priv->regs + NOK_CCA_CNT_REG);
3188	cca_cnt /= 1000; /* uSecs to mSecs */
3189	survey->time_busy = (u64) cca_cnt;
3190
3191	rx_rdy = ioread32(priv->regs + BBU_RXRDY_CNT_REG);
3192	rx_rdy /= 1000; /* uSecs to mSecs */
3193	survey->time_rx = (u64) rx_rdy;
3194
3195	priv->channel_time = jiffies - priv->channel_time;
3196	survey->time = jiffies_to_msecs(priv->channel_time);
3197
3198	survey->channel = channel;
3199
3200	mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &nf);
3201
3202	/* Make sure sign is negative else ACS  at hostapd fails */
3203	survey->noise = nf * -1;
3204
3205	survey->filled = SURVEY_INFO_NOISE_DBM |
3206			 SURVEY_INFO_TIME |
3207			 SURVEY_INFO_TIME_BUSY |
3208			 SURVEY_INFO_TIME_RX;
3209}
3210
3211/*
3212 * CMD_SET_RF_CHANNEL.
3213 */
3214struct mwl8k_cmd_set_rf_channel {
3215	struct mwl8k_cmd_pkt_hdr header;
3216	__le16 action;
3217	__u8 current_channel;
3218	__le32 channel_flags;
3219} __packed;
3220
3221static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
3222				    struct ieee80211_conf *conf)
3223{
3224	struct ieee80211_channel *channel = conf->chandef.chan;
3225	enum nl80211_channel_type channel_type =
3226		cfg80211_get_chandef_type(&conf->chandef);
3227	struct mwl8k_cmd_set_rf_channel *cmd;
3228	struct mwl8k_priv *priv = hw->priv;
3229	int rc;
3230
3231	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3232	if (cmd == NULL)
3233		return -ENOMEM;
3234
3235	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
3236	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3237	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3238	cmd->current_channel = channel->hw_value;
3239
3240	if (channel->band == NL80211_BAND_2GHZ)
3241		cmd->channel_flags |= cpu_to_le32(0x00000001);
3242	else if (channel->band == NL80211_BAND_5GHZ)
3243		cmd->channel_flags |= cpu_to_le32(0x00000004);
3244
3245	if (!priv->sw_scan_start) {
3246		if (channel_type == NL80211_CHAN_NO_HT ||
3247		    channel_type == NL80211_CHAN_HT20)
3248			cmd->channel_flags |= cpu_to_le32(0x00000080);
3249		else if (channel_type == NL80211_CHAN_HT40MINUS)
3250			cmd->channel_flags |= cpu_to_le32(0x000001900);
3251		else if (channel_type == NL80211_CHAN_HT40PLUS)
3252			cmd->channel_flags |= cpu_to_le32(0x000000900);
3253	} else {
3254		cmd->channel_flags |= cpu_to_le32(0x00000080);
3255	}
3256
3257	if (priv->sw_scan_start) {
3258		/* Store current channel stats
3259		 * before switching to newer one.
3260		 * This will be processed only for AP fw.
3261		 */
3262		if (priv->channel_time != 0)
3263			mwl8k_update_survey(priv, priv->acs_chan);
3264
3265		priv->channel_time = jiffies;
3266		priv->acs_chan =  channel;
3267	}
3268
3269	rc = mwl8k_post_cmd(hw, &cmd->header);
3270	kfree(cmd);
3271
3272	return rc;
3273}
3274
3275/*
3276 * CMD_SET_AID.
3277 */
3278#define MWL8K_FRAME_PROT_DISABLED			0x00
3279#define MWL8K_FRAME_PROT_11G				0x07
3280#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY		0x02
3281#define MWL8K_FRAME_PROT_11N_HT_ALL			0x06
3282
3283struct mwl8k_cmd_update_set_aid {
3284	struct mwl8k_cmd_pkt_hdr header;
3285	__le16	aid;
3286
3287	 /* AP's MAC address (BSSID) */
3288	__u8	bssid[ETH_ALEN];
3289	__le16	protection_mode;
3290	__u8	supp_rates[14];
3291} __packed;
3292
3293static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
3294{
3295	int i;
3296	int j;
3297
3298	/*
3299	 * Clear nonstandard rate 4.
3300	 */
3301	mask &= 0x1fef;
3302
3303	for (i = 0, j = 0; i < 13; i++) {
3304		if (mask & (1 << i))
3305			rates[j++] = mwl8k_rates_24[i].hw_value;
3306	}
3307}
3308
3309static int
3310mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
3311		  struct ieee80211_vif *vif, u32 legacy_rate_mask)
3312{
3313	struct mwl8k_cmd_update_set_aid *cmd;
3314	u16 prot_mode;
3315	int rc;
3316
3317	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3318	if (cmd == NULL)
3319		return -ENOMEM;
3320
3321	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
3322	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3323	cmd->aid = cpu_to_le16(vif->cfg.aid);
3324	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
3325
3326	if (vif->bss_conf.use_cts_prot) {
3327		prot_mode = MWL8K_FRAME_PROT_11G;
3328	} else {
3329		switch (vif->bss_conf.ht_operation_mode &
3330			IEEE80211_HT_OP_MODE_PROTECTION) {
3331		case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
3332			prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
3333			break;
3334		case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
3335			prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
3336			break;
3337		default:
3338			prot_mode = MWL8K_FRAME_PROT_DISABLED;
3339			break;
3340		}
3341	}
3342	cmd->protection_mode = cpu_to_le16(prot_mode);
3343
3344	legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
3345
3346	rc = mwl8k_post_cmd(hw, &cmd->header);
3347	kfree(cmd);
3348
3349	return rc;
3350}
3351
3352/*
3353 * CMD_SET_RATE.
3354 */
3355struct mwl8k_cmd_set_rate {
3356	struct mwl8k_cmd_pkt_hdr header;
3357	__u8	legacy_rates[14];
3358
3359	/* Bitmap for supported MCS codes.  */
3360	__u8	mcs_set[16];
3361	__u8	reserved[16];
3362} __packed;
3363
3364static int
3365mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3366		   u32 legacy_rate_mask, u8 *mcs_rates)
3367{
3368	struct mwl8k_cmd_set_rate *cmd;
3369	int rc;
3370
3371	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3372	if (cmd == NULL)
3373		return -ENOMEM;
3374
3375	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
3376	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3377	legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
3378	memcpy(cmd->mcs_set, mcs_rates, 16);
3379
3380	rc = mwl8k_post_cmd(hw, &cmd->header);
3381	kfree(cmd);
3382
3383	return rc;
3384}
3385
3386/*
3387 * CMD_FINALIZE_JOIN.
3388 */
3389#define MWL8K_FJ_BEACON_MAXLEN	128
3390
3391struct mwl8k_cmd_finalize_join {
3392	struct mwl8k_cmd_pkt_hdr header;
3393	__le32 sleep_interval;	/* Number of beacon periods to sleep */
3394	__u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
3395} __packed;
3396
3397static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
3398				   int framelen, int dtim)
3399{
3400	struct mwl8k_cmd_finalize_join *cmd;
3401	struct ieee80211_mgmt *payload = frame;
3402	int payload_len;
3403	int rc;
3404
3405	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3406	if (cmd == NULL)
3407		return -ENOMEM;
3408
3409	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
3410	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3411	cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
3412
3413	payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
3414	if (payload_len < 0)
3415		payload_len = 0;
3416	else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
3417		payload_len = MWL8K_FJ_BEACON_MAXLEN;
3418
3419	memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
3420
3421	rc = mwl8k_post_cmd(hw, &cmd->header);
3422	kfree(cmd);
3423
3424	return rc;
3425}
3426
3427/*
3428 * CMD_SET_RTS_THRESHOLD.
3429 */
3430struct mwl8k_cmd_set_rts_threshold {
3431	struct mwl8k_cmd_pkt_hdr header;
3432	__le16 action;
3433	__le16 threshold;
3434} __packed;
3435
3436static int
3437mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
3438			    int rts_thresh)
3439{
3440	struct mwl8k_cmd_set_rts_threshold *cmd;
3441	int rc;
3442
3443	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3444	if (cmd == NULL)
3445		return -ENOMEM;
3446
3447	cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3448	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3449	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3450	cmd->threshold = cpu_to_le16(rts_thresh);
3451
3452	rc = mwl8k_post_cmd(hw, &cmd->header);
3453	kfree(cmd);
3454
3455	return rc;
3456}
3457
3458/*
3459 * CMD_SET_SLOT.
3460 */
3461struct mwl8k_cmd_set_slot {
3462	struct mwl8k_cmd_pkt_hdr header;
3463	__le16 action;
3464	__u8 short_slot;
3465} __packed;
3466
3467static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
3468{
3469	struct mwl8k_cmd_set_slot *cmd;
3470	int rc;
3471
3472	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3473	if (cmd == NULL)
3474		return -ENOMEM;
3475
3476	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3477	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3478	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3479	cmd->short_slot = short_slot_time;
3480
3481	rc = mwl8k_post_cmd(hw, &cmd->header);
3482	kfree(cmd);
3483
3484	return rc;
3485}
3486
3487/*
3488 * CMD_SET_EDCA_PARAMS.
3489 */
3490struct mwl8k_cmd_set_edca_params {
3491	struct mwl8k_cmd_pkt_hdr header;
3492
3493	/* See MWL8K_SET_EDCA_XXX below */
3494	__le16 action;
3495
3496	/* TX opportunity in units of 32 us */
3497	__le16 txop;
3498
3499	union {
3500		struct {
3501			/* Log exponent of max contention period: 0...15 */
3502			__le32 log_cw_max;
3503
3504			/* Log exponent of min contention period: 0...15 */
3505			__le32 log_cw_min;
3506
3507			/* Adaptive interframe spacing in units of 32us */
3508			__u8 aifs;
3509
3510			/* TX queue to configure */
3511			__u8 txq;
3512		} ap;
3513		struct {
3514			/* Log exponent of max contention period: 0...15 */
3515			__u8 log_cw_max;
3516
3517			/* Log exponent of min contention period: 0...15 */
3518			__u8 log_cw_min;
3519
3520			/* Adaptive interframe spacing in units of 32us */
3521			__u8 aifs;
3522
3523			/* TX queue to configure */
3524			__u8 txq;
3525		} sta;
3526	};
3527} __packed;
3528
3529#define MWL8K_SET_EDCA_CW	0x01
3530#define MWL8K_SET_EDCA_TXOP	0x02
3531#define MWL8K_SET_EDCA_AIFS	0x04
3532
3533#define MWL8K_SET_EDCA_ALL	(MWL8K_SET_EDCA_CW | \
3534				 MWL8K_SET_EDCA_TXOP | \
3535				 MWL8K_SET_EDCA_AIFS)
3536
3537static int
3538mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3539			  __u16 cw_min, __u16 cw_max,
3540			  __u8 aifs, __u16 txop)
3541{
3542	struct mwl8k_priv *priv = hw->priv;
3543	struct mwl8k_cmd_set_edca_params *cmd;
3544	int rc;
3545
3546	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3547	if (cmd == NULL)
3548		return -ENOMEM;
3549
3550	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3551	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3552	cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3553	cmd->txop = cpu_to_le16(txop);
3554	if (priv->ap_fw) {
3555		cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3556		cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3557		cmd->ap.aifs = aifs;
3558		cmd->ap.txq = qnum;
3559	} else {
3560		cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3561		cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3562		cmd->sta.aifs = aifs;
3563		cmd->sta.txq = qnum;
3564	}
3565
3566	rc = mwl8k_post_cmd(hw, &cmd->header);
3567	kfree(cmd);
3568
3569	return rc;
3570}
3571
3572/*
3573 * CMD_SET_WMM_MODE.
3574 */
3575struct mwl8k_cmd_set_wmm_mode {
3576	struct mwl8k_cmd_pkt_hdr header;
3577	__le16 action;
3578} __packed;
3579
3580static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
3581{
3582	struct mwl8k_priv *priv = hw->priv;
3583	struct mwl8k_cmd_set_wmm_mode *cmd;
3584	int rc;
3585
3586	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3587	if (cmd == NULL)
3588		return -ENOMEM;
3589
3590	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
3591	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3592	cmd->action = cpu_to_le16(!!enable);
3593
3594	rc = mwl8k_post_cmd(hw, &cmd->header);
3595	kfree(cmd);
3596
3597	if (!rc)
3598		priv->wmm_enabled = enable;
3599
3600	return rc;
3601}
3602
3603/*
3604 * CMD_MIMO_CONFIG.
3605 */
3606struct mwl8k_cmd_mimo_config {
3607	struct mwl8k_cmd_pkt_hdr header;
3608	__le32 action;
3609	__u8 rx_antenna_map;
3610	__u8 tx_antenna_map;
3611} __packed;
3612
3613static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
3614{
3615	struct mwl8k_cmd_mimo_config *cmd;
3616	int rc;
3617
3618	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3619	if (cmd == NULL)
3620		return -ENOMEM;
3621
3622	cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
3623	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3624	cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3625	cmd->rx_antenna_map = rx;
3626	cmd->tx_antenna_map = tx;
3627
3628	rc = mwl8k_post_cmd(hw, &cmd->header);
3629	kfree(cmd);
3630
3631	return rc;
3632}
3633
3634/*
3635 * CMD_USE_FIXED_RATE (STA version).
3636 */
3637struct mwl8k_cmd_use_fixed_rate_sta {
3638	struct mwl8k_cmd_pkt_hdr header;
3639	__le32 action;
3640	__le32 allow_rate_drop;
3641	__le32 num_rates;
3642	struct {
3643		__le32 is_ht_rate;
3644		__le32 enable_retry;
3645		__le32 rate;
3646		__le32 retry_count;
3647	} rate_entry[8];
3648	__le32 rate_type;
3649	__le32 reserved1;
3650	__le32 reserved2;
3651} __packed;
3652
3653#define MWL8K_USE_AUTO_RATE	0x0002
3654#define MWL8K_UCAST_RATE	0
3655
3656static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
3657{
3658	struct mwl8k_cmd_use_fixed_rate_sta *cmd;
3659	int rc;
3660
3661	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3662	if (cmd == NULL)
3663		return -ENOMEM;
3664
3665	cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3666	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3667	cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3668	cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
3669
3670	rc = mwl8k_post_cmd(hw, &cmd->header);
3671	kfree(cmd);
3672
3673	return rc;
3674}
3675
3676/*
3677 * CMD_USE_FIXED_RATE (AP version).
3678 */
3679struct mwl8k_cmd_use_fixed_rate_ap {
3680	struct mwl8k_cmd_pkt_hdr header;
3681	__le32 action;
3682	__le32 allow_rate_drop;
3683	__le32 num_rates;
3684	struct mwl8k_rate_entry_ap {
3685		__le32 is_ht_rate;
3686		__le32 enable_retry;
3687		__le32 rate;
3688		__le32 retry_count;
3689	} rate_entry[4];
3690	u8 multicast_rate;
3691	u8 multicast_rate_type;
3692	u8 management_rate;
3693} __packed;
3694
3695static int
3696mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3697{
3698	struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3699	int rc;
3700
3701	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3702	if (cmd == NULL)
3703		return -ENOMEM;
3704
3705	cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3706	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3707	cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3708	cmd->multicast_rate = mcast;
3709	cmd->management_rate = mgmt;
3710
3711	rc = mwl8k_post_cmd(hw, &cmd->header);
3712	kfree(cmd);
3713
3714	return rc;
3715}
3716
3717/*
3718 * CMD_ENABLE_SNIFFER.
3719 */
3720struct mwl8k_cmd_enable_sniffer {
3721	struct mwl8k_cmd_pkt_hdr header;
3722	__le32 action;
3723} __packed;
3724
3725static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3726{
3727	struct mwl8k_cmd_enable_sniffer *cmd;
3728	int rc;
3729
3730	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3731	if (cmd == NULL)
3732		return -ENOMEM;
3733
3734	cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3735	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3736	cmd->action = cpu_to_le32(!!enable);
3737
3738	rc = mwl8k_post_cmd(hw, &cmd->header);
3739	kfree(cmd);
3740
3741	return rc;
3742}
3743
3744struct mwl8k_cmd_update_mac_addr {
3745	struct mwl8k_cmd_pkt_hdr header;
3746	union {
3747		struct {
3748			__le16 mac_type;
3749			__u8 mac_addr[ETH_ALEN];
3750		} mbss;
3751		__u8 mac_addr[ETH_ALEN];
3752	};
3753} __packed;
3754
3755#define MWL8K_MAC_TYPE_PRIMARY_CLIENT		0
3756#define MWL8K_MAC_TYPE_SECONDARY_CLIENT		1
3757#define MWL8K_MAC_TYPE_PRIMARY_AP		2
3758#define MWL8K_MAC_TYPE_SECONDARY_AP		3
3759
3760static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3761				  struct ieee80211_vif *vif, u8 *mac, bool set)
3762{
3763	struct mwl8k_priv *priv = hw->priv;
3764	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3765	struct mwl8k_cmd_update_mac_addr *cmd;
3766	int mac_type;
3767	int rc;
3768
3769	mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3770	if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3771		if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3772			if (priv->ap_fw)
3773				mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3774			else
3775				mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3776		else
3777			mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3778	} else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3779		if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3780			mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3781		else
3782			mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3783	}
3784
3785	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3786	if (cmd == NULL)
3787		return -ENOMEM;
3788
3789	if (set)
3790		cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3791	else
3792		cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3793
3794	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3795	if (priv->ap_fw) {
3796		cmd->mbss.mac_type = cpu_to_le16(mac_type);
3797		memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3798	} else {
3799		memcpy(cmd->mac_addr, mac, ETH_ALEN);
3800	}
3801
3802	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3803	kfree(cmd);
3804
3805	return rc;
3806}
3807
3808/*
3809 * MWL8K_CMD_SET_MAC_ADDR.
3810 */
3811static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3812				  struct ieee80211_vif *vif, u8 *mac)
3813{
3814	return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3815}
3816
3817/*
3818 * MWL8K_CMD_DEL_MAC_ADDR.
3819 */
3820static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3821				  struct ieee80211_vif *vif, u8 *mac)
3822{
3823	return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3824}
3825
3826/*
3827 * CMD_SET_RATEADAPT_MODE.
3828 */
3829struct mwl8k_cmd_set_rate_adapt_mode {
3830	struct mwl8k_cmd_pkt_hdr header;
3831	__le16 action;
3832	__le16 mode;
3833} __packed;
3834
3835static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3836{
3837	struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3838	int rc;
3839
3840	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3841	if (cmd == NULL)
3842		return -ENOMEM;
3843
3844	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3845	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3846	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3847	cmd->mode = cpu_to_le16(mode);
3848
3849	rc = mwl8k_post_cmd(hw, &cmd->header);
3850	kfree(cmd);
3851
3852	return rc;
3853}
3854
3855/*
3856 * CMD_GET_WATCHDOG_BITMAP.
3857 */
3858struct mwl8k_cmd_get_watchdog_bitmap {
3859	struct mwl8k_cmd_pkt_hdr header;
3860	u8	bitmap;
3861} __packed;
3862
3863static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3864{
3865	struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3866	int rc;
3867
3868	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3869	if (cmd == NULL)
3870		return -ENOMEM;
3871
3872	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3873	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3874
3875	rc = mwl8k_post_cmd(hw, &cmd->header);
3876	if (!rc)
3877		*bitmap = cmd->bitmap;
3878
3879	kfree(cmd);
3880
3881	return rc;
3882}
3883
3884#define MWL8K_WMM_QUEUE_NUMBER	3
3885
3886static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3887			     u8 idx);
3888
3889static void mwl8k_watchdog_ba_events(struct work_struct *work)
3890{
3891	int rc;
3892	u8 bitmap = 0, stream_index;
3893	struct mwl8k_ampdu_stream *streams;
3894	struct mwl8k_priv *priv =
3895		container_of(work, struct mwl8k_priv, watchdog_ba_handle);
3896	struct ieee80211_hw *hw = priv->hw;
3897	int i;
3898	u32 status = 0;
3899
3900	mwl8k_fw_lock(hw);
3901
3902	rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3903	if (rc)
3904		goto done;
3905
3906	spin_lock(&priv->stream_lock);
3907
3908	/* the bitmap is the hw queue number.  Map it to the ampdu queue. */
3909	for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
3910		if (bitmap & (1 << i)) {
3911			stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
3912				       TOTAL_HW_TX_QUEUES;
3913			streams = &priv->ampdu[stream_index];
3914			if (streams->state == AMPDU_STREAM_ACTIVE) {
3915				ieee80211_stop_tx_ba_session(streams->sta,
3916							     streams->tid);
3917				spin_unlock(&priv->stream_lock);
3918				mwl8k_destroy_ba(hw, stream_index);
3919				spin_lock(&priv->stream_lock);
3920			}
3921		}
3922	}
3923
3924	spin_unlock(&priv->stream_lock);
3925done:
3926	atomic_dec(&priv->watchdog_event_pending);
3927	status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3928	iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
3929		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3930	mwl8k_fw_unlock(hw);
3931	return;
3932}
3933
3934
3935/*
3936 * CMD_BSS_START.
3937 */
3938struct mwl8k_cmd_bss_start {
3939	struct mwl8k_cmd_pkt_hdr header;
3940	__le32 enable;
3941} __packed;
3942
3943static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3944			       struct ieee80211_vif *vif, int enable)
3945{
3946	struct mwl8k_cmd_bss_start *cmd;
3947	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3948	struct mwl8k_priv *priv = hw->priv;
3949	int rc;
3950
3951	if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
3952		return 0;
3953
3954	if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
3955		return 0;
3956
3957	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3958	if (cmd == NULL)
3959		return -ENOMEM;
3960
3961	cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3962	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3963	cmd->enable = cpu_to_le32(enable);
3964
3965	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3966	kfree(cmd);
3967
3968	if (!rc) {
3969		if (enable)
3970			priv->running_bsses |= (1 << mwl8k_vif->macid);
3971		else
3972			priv->running_bsses &= ~(1 << mwl8k_vif->macid);
3973	}
3974	return rc;
3975}
3976
3977static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
3978{
3979	struct mwl8k_priv *priv = hw->priv;
3980	struct mwl8k_vif *mwl8k_vif, *tmp_vif;
3981	struct ieee80211_vif *vif;
3982
3983	list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
3984		vif = mwl8k_vif->vif;
3985
3986		if (!(bitmap & (1 << mwl8k_vif->macid)))
3987			continue;
3988
3989		if (vif->type == NL80211_IFTYPE_AP)
3990			mwl8k_cmd_bss_start(hw, vif, enable);
3991	}
3992}
3993/*
3994 * CMD_BASTREAM.
3995 */
3996
3997/*
3998 * UPSTREAM is tx direction
3999 */
4000#define BASTREAM_FLAG_DIRECTION_UPSTREAM	0x00
4001#define BASTREAM_FLAG_IMMEDIATE_TYPE		0x01
4002
4003enum ba_stream_action_type {
4004	MWL8K_BA_CREATE,
4005	MWL8K_BA_UPDATE,
4006	MWL8K_BA_DESTROY,
4007	MWL8K_BA_FLUSH,
4008	MWL8K_BA_CHECK,
4009};
4010
4011
4012struct mwl8k_create_ba_stream {
4013	__le32	flags;
4014	__le32	idle_thrs;
4015	__le32	bar_thrs;
4016	__le32	window_size;
4017	u8	peer_mac_addr[6];
4018	u8	dialog_token;
4019	u8	tid;
4020	u8	queue_id;
4021	u8	param_info;
4022	__le32	ba_context;
4023	u8	reset_seq_no_flag;
4024	__le16	curr_seq_no;
4025	u8	sta_src_mac_addr[6];
4026} __packed;
4027
4028struct mwl8k_destroy_ba_stream {
4029	__le32	flags;
4030	__le32	ba_context;
4031} __packed;
4032
4033struct mwl8k_cmd_bastream {
4034	struct mwl8k_cmd_pkt_hdr	header;
4035	__le32	action;
4036	union {
4037		struct mwl8k_create_ba_stream	create_params;
4038		struct mwl8k_destroy_ba_stream	destroy_params;
4039	};
4040} __packed;
4041
4042static int
4043mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
4044	       struct ieee80211_vif *vif)
4045{
4046	struct mwl8k_cmd_bastream *cmd;
4047	int rc;
4048
4049	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4050	if (cmd == NULL)
4051		return -ENOMEM;
4052
4053	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
4054	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4055
4056	cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
4057
4058	cmd->create_params.queue_id = stream->idx;
4059	memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
4060	       ETH_ALEN);
4061	cmd->create_params.tid = stream->tid;
4062
4063	cmd->create_params.flags =
4064		cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
4065		cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
4066
4067	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4068
4069	kfree(cmd);
4070
4071	return rc;
4072}
4073
4074static int
4075mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
4076		u8 buf_size, struct ieee80211_vif *vif)
4077{
4078	struct mwl8k_cmd_bastream *cmd;
4079	int rc;
4080
4081	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4082	if (cmd == NULL)
4083		return -ENOMEM;
4084
4085
4086	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
4087	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4088
4089	cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
4090
4091	cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
4092	cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
4093	cmd->create_params.queue_id = stream->idx;
4094
4095	memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
4096	cmd->create_params.tid = stream->tid;
4097	cmd->create_params.curr_seq_no = cpu_to_le16(0);
4098	cmd->create_params.reset_seq_no_flag = 1;
4099
4100	cmd->create_params.param_info =
4101		(stream->sta->deflink.ht_cap.ampdu_factor &
4102		 IEEE80211_HT_AMPDU_PARM_FACTOR) |
4103		((stream->sta->deflink.ht_cap.ampdu_density << 2) &
4104		 IEEE80211_HT_AMPDU_PARM_DENSITY);
4105
4106	cmd->create_params.flags =
4107		cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
4108					BASTREAM_FLAG_DIRECTION_UPSTREAM);
4109
4110	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4111
4112	wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
4113		stream->sta->addr, stream->tid);
4114	kfree(cmd);
4115
4116	return rc;
4117}
4118
4119static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
4120			     u8 idx)
4121{
4122	struct mwl8k_cmd_bastream *cmd;
4123
4124	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4125	if (cmd == NULL)
4126		return;
4127
4128	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
4129	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4130	cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
4131
4132	cmd->destroy_params.ba_context = cpu_to_le32(idx);
4133	mwl8k_post_cmd(hw, &cmd->header);
4134
4135	wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
4136
4137	kfree(cmd);
4138}
4139
4140/*
4141 * CMD_SET_NEW_STN.
4142 */
4143struct mwl8k_cmd_set_new_stn {
4144	struct mwl8k_cmd_pkt_hdr header;
4145	__le16 aid;
4146	__u8 mac_addr[6];
4147	__le16 stn_id;
4148	__le16 action;
4149	__le16 rsvd;
4150	__le32 legacy_rates;
4151	__u8 ht_rates[4];
4152	__le16 cap_info;
4153	__le16 ht_capabilities_info;
4154	__u8 mac_ht_param_info;
4155	__u8 rev;
4156	__u8 control_channel;
4157	__u8 add_channel;
4158	__le16 op_mode;
4159	__le16 stbc;
4160	__u8 add_qos_info;
4161	__u8 is_qos_sta;
4162	__le32 fw_sta_ptr;
4163} __packed;
4164
4165#define MWL8K_STA_ACTION_ADD		0
4166#define MWL8K_STA_ACTION_REMOVE		2
4167
4168static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
4169				     struct ieee80211_vif *vif,
4170				     struct ieee80211_sta *sta)
4171{
4172	struct mwl8k_cmd_set_new_stn *cmd;
4173	u32 rates;
4174	int rc;
4175
4176	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4177	if (cmd == NULL)
4178		return -ENOMEM;
4179
4180	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4181	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4182	cmd->aid = cpu_to_le16(sta->aid);
4183	memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
4184	cmd->stn_id = cpu_to_le16(sta->aid);
4185	cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
4186	if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
4187		rates = sta->deflink.supp_rates[NL80211_BAND_2GHZ];
4188	else
4189		rates = sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
4190	cmd->legacy_rates = cpu_to_le32(rates);
4191	if (sta->deflink.ht_cap.ht_supported) {
4192		cmd->ht_rates[0] = sta->deflink.ht_cap.mcs.rx_mask[0];
4193		cmd->ht_rates[1] = sta->deflink.ht_cap.mcs.rx_mask[1];
4194		cmd->ht_rates[2] = sta->deflink.ht_cap.mcs.rx_mask[2];
4195		cmd->ht_rates[3] = sta->deflink.ht_cap.mcs.rx_mask[3];
4196		cmd->ht_capabilities_info = cpu_to_le16(sta->deflink.ht_cap.cap);
4197		cmd->mac_ht_param_info = (sta->deflink.ht_cap.ampdu_factor & 3) |
4198			((sta->deflink.ht_cap.ampdu_density & 7) << 2);
4199		cmd->is_qos_sta = 1;
4200	}
4201
4202	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4203	kfree(cmd);
4204
4205	return rc;
4206}
4207
4208static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
4209					  struct ieee80211_vif *vif)
4210{
4211	struct mwl8k_cmd_set_new_stn *cmd;
4212	int rc;
4213
4214	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4215	if (cmd == NULL)
4216		return -ENOMEM;
4217
4218	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4219	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4220	memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
4221
4222	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4223	kfree(cmd);
4224
4225	return rc;
4226}
4227
4228static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
4229				     struct ieee80211_vif *vif, u8 *addr)
4230{
4231	struct mwl8k_cmd_set_new_stn *cmd;
4232	struct mwl8k_priv *priv = hw->priv;
4233	int rc, i;
4234	u8 idx;
4235
4236	spin_lock(&priv->stream_lock);
4237	/* Destroy any active ampdu streams for this sta */
4238	for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
4239		struct mwl8k_ampdu_stream *s;
4240		s = &priv->ampdu[i];
4241		if (s->state != AMPDU_NO_STREAM) {
4242			if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
4243				if (s->state == AMPDU_STREAM_ACTIVE) {
4244					idx = s->idx;
4245					spin_unlock(&priv->stream_lock);
4246					mwl8k_destroy_ba(hw, idx);
4247					spin_lock(&priv->stream_lock);
4248				} else if (s->state == AMPDU_STREAM_NEW) {
4249					mwl8k_remove_stream(hw, s);
4250				}
4251			}
4252		}
4253	}
4254
4255	spin_unlock(&priv->stream_lock);
4256
4257	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4258	if (cmd == NULL)
4259		return -ENOMEM;
4260
4261	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4262	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4263	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4264	cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
4265
4266	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4267	kfree(cmd);
4268
4269	return rc;
4270}
4271
4272/*
4273 * CMD_UPDATE_ENCRYPTION.
4274 */
4275
4276#define MAX_ENCR_KEY_LENGTH	16
4277#define MIC_KEY_LENGTH		8
4278
4279struct mwl8k_cmd_update_encryption {
4280	struct mwl8k_cmd_pkt_hdr header;
4281
4282	__le32 action;
4283	__le32 reserved;
4284	__u8 mac_addr[6];
4285	__u8 encr_type;
4286
4287} __packed;
4288
4289struct mwl8k_cmd_set_key {
4290	struct mwl8k_cmd_pkt_hdr header;
4291
4292	__le32 action;
4293	__le32 reserved;
4294	__le16 length;
4295	__le16 key_type_id;
4296	__le32 key_info;
4297	__le32 key_id;
4298	__le16 key_len;
4299	struct {
4300		__u8 key_material[MAX_ENCR_KEY_LENGTH];
4301		__u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
4302		__u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
4303	} tkip;
4304	__le16 tkip_rsc_low;
4305	__le32 tkip_rsc_high;
4306	__le16 tkip_tsc_low;
4307	__le32 tkip_tsc_high;
4308	__u8 mac_addr[6];
4309} __packed;
4310
4311enum {
4312	MWL8K_ENCR_ENABLE,
4313	MWL8K_ENCR_SET_KEY,
4314	MWL8K_ENCR_REMOVE_KEY,
4315	MWL8K_ENCR_SET_GROUP_KEY,
4316};
4317
4318#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP	0
4319#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE	1
4320#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP	4
4321#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED	7
4322#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES	8
4323
4324enum {
4325	MWL8K_ALG_WEP,
4326	MWL8K_ALG_TKIP,
4327	MWL8K_ALG_CCMP,
4328};
4329
4330#define MWL8K_KEY_FLAG_TXGROUPKEY	0x00000004
4331#define MWL8K_KEY_FLAG_PAIRWISE		0x00000008
4332#define MWL8K_KEY_FLAG_TSC_VALID	0x00000040
4333#define MWL8K_KEY_FLAG_WEP_TXKEY	0x01000000
4334#define MWL8K_KEY_FLAG_MICKEY_VALID	0x02000000
4335
4336static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
4337					      struct ieee80211_vif *vif,
4338					      u8 *addr,
4339					      u8 encr_type)
4340{
4341	struct mwl8k_cmd_update_encryption *cmd;
4342	int rc;
4343
4344	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4345	if (cmd == NULL)
4346		return -ENOMEM;
4347
4348	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4349	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4350	cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
4351	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4352	cmd->encr_type = encr_type;
4353
4354	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4355	kfree(cmd);
4356
4357	return rc;
4358}
4359
4360static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4361						u8 *addr,
4362						struct ieee80211_key_conf *key)
4363{
4364	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4365	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4366	cmd->length = cpu_to_le16(sizeof(*cmd) -
4367				offsetof(struct mwl8k_cmd_set_key, length));
4368	cmd->key_id = cpu_to_le32(key->keyidx);
4369	cmd->key_len = cpu_to_le16(key->keylen);
4370	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4371
4372	switch (key->cipher) {
4373	case WLAN_CIPHER_SUITE_WEP40:
4374	case WLAN_CIPHER_SUITE_WEP104:
4375		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4376		if (key->keyidx == 0)
4377			cmd->key_info =	cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4378
4379		break;
4380	case WLAN_CIPHER_SUITE_TKIP:
4381		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4382		cmd->key_info =	(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4383			? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4384			: cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4385		cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4386						| MWL8K_KEY_FLAG_TSC_VALID);
4387		break;
4388	case WLAN_CIPHER_SUITE_CCMP:
4389		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4390		cmd->key_info =	(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4391			? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4392			: cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4393		break;
4394	default:
4395		return -ENOTSUPP;
4396	}
4397
4398	return 0;
4399}
4400
4401static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4402						struct ieee80211_vif *vif,
4403						u8 *addr,
4404						struct ieee80211_key_conf *key)
4405{
4406	struct mwl8k_cmd_set_key *cmd;
4407	int rc;
4408	int keymlen;
4409	u32 action;
4410	u8 idx;
4411	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4412
4413	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4414	if (cmd == NULL)
4415		return -ENOMEM;
4416
4417	rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4418	if (rc < 0)
4419		goto done;
4420
4421	idx = key->keyidx;
4422
4423	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4424		action = MWL8K_ENCR_SET_KEY;
4425	else
4426		action = MWL8K_ENCR_SET_GROUP_KEY;
4427
4428	switch (key->cipher) {
4429	case WLAN_CIPHER_SUITE_WEP40:
4430	case WLAN_CIPHER_SUITE_WEP104:
4431		if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4432			memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4433						sizeof(*key) + key->keylen);
4434			mwl8k_vif->wep_key_conf[idx].enabled = 1;
4435		}
4436
4437		keymlen = key->keylen;
4438		action = MWL8K_ENCR_SET_KEY;
4439		break;
4440	case WLAN_CIPHER_SUITE_TKIP:
4441		keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4442		break;
4443	case WLAN_CIPHER_SUITE_CCMP:
4444		keymlen = key->keylen;
4445		break;
4446	default:
4447		rc = -ENOTSUPP;
4448		goto done;
4449	}
4450
4451	memcpy(&cmd->tkip, key->key, keymlen);
4452	cmd->action = cpu_to_le32(action);
4453
4454	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4455done:
4456	kfree(cmd);
4457
4458	return rc;
4459}
4460
4461static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4462						struct ieee80211_vif *vif,
4463						u8 *addr,
4464						struct ieee80211_key_conf *key)
4465{
4466	struct mwl8k_cmd_set_key *cmd;
4467	int rc;
4468	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4469
4470	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4471	if (cmd == NULL)
4472		return -ENOMEM;
4473
4474	rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4475	if (rc < 0)
4476		goto done;
4477
4478	if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4479			key->cipher == WLAN_CIPHER_SUITE_WEP104)
4480		mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4481
4482	cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4483
4484	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4485done:
4486	kfree(cmd);
4487
4488	return rc;
4489}
4490
4491static int mwl8k_set_key(struct ieee80211_hw *hw,
4492			 enum set_key_cmd cmd_param,
4493			 struct ieee80211_vif *vif,
4494			 struct ieee80211_sta *sta,
4495			 struct ieee80211_key_conf *key)
4496{
4497	int rc = 0;
4498	u8 encr_type;
4499	u8 *addr;
4500	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4501	struct mwl8k_priv *priv = hw->priv;
4502
4503	if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
4504		return -EOPNOTSUPP;
4505
4506	if (sta == NULL)
4507		addr = vif->addr;
4508	else
4509		addr = sta->addr;
4510
4511	if (cmd_param == SET_KEY) {
4512		rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4513		if (rc)
4514			goto out;
4515
4516		if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4517				|| (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4518			encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4519		else
4520			encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4521
4522		rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4523								encr_type);
4524		if (rc)
4525			goto out;
4526
4527		mwl8k_vif->is_hw_crypto_enabled = true;
4528
4529	} else {
4530		rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4531
4532		if (rc)
4533			goto out;
4534	}
4535out:
4536	return rc;
4537}
4538
4539/*
4540 * CMD_UPDATE_STADB.
4541 */
4542struct ewc_ht_info {
4543	__le16	control1;
4544	__le16	control2;
4545	__le16	control3;
4546} __packed;
4547
4548struct peer_capability_info {
4549	/* Peer type - AP vs. STA.  */
4550	__u8	peer_type;
4551
4552	/* Basic 802.11 capabilities from assoc resp.  */
4553	__le16	basic_caps;
4554
4555	/* Set if peer supports 802.11n high throughput (HT).  */
4556	__u8	ht_support;
4557
4558	/* Valid if HT is supported.  */
4559	__le16	ht_caps;
4560	__u8	extended_ht_caps;
4561	struct ewc_ht_info	ewc_info;
4562
4563	/* Legacy rate table. Intersection of our rates and peer rates.  */
4564	__u8	legacy_rates[12];
4565
4566	/* HT rate table. Intersection of our rates and peer rates.  */
4567	__u8	ht_rates[16];
4568	__u8	pad[16];
4569
4570	/* If set, interoperability mode, no proprietary extensions.  */
4571	__u8	interop;
4572	__u8	pad2;
4573	__u8	station_id;
4574	__le16	amsdu_enabled;
4575} __packed;
4576
4577struct mwl8k_cmd_update_stadb {
4578	struct mwl8k_cmd_pkt_hdr header;
4579
4580	/* See STADB_ACTION_TYPE */
4581	__le32	action;
4582
4583	/* Peer MAC address */
4584	__u8	peer_addr[ETH_ALEN];
4585
4586	__le32	reserved;
4587
4588	/* Peer info - valid during add/update.  */
4589	struct peer_capability_info	peer_info;
4590} __packed;
4591
4592#define MWL8K_STA_DB_MODIFY_ENTRY	1
4593#define MWL8K_STA_DB_DEL_ENTRY		2
4594
4595/* Peer Entry flags - used to define the type of the peer node */
4596#define MWL8K_PEER_TYPE_ACCESSPOINT	2
4597
4598static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
4599				      struct ieee80211_vif *vif,
4600				      struct ieee80211_sta *sta)
4601{
4602	struct mwl8k_cmd_update_stadb *cmd;
4603	struct peer_capability_info *p;
4604	u32 rates;
4605	int rc;
4606
4607	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4608	if (cmd == NULL)
4609		return -ENOMEM;
4610
4611	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4612	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4613	cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
4614	memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
4615
4616	p = &cmd->peer_info;
4617	p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4618	p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
4619	p->ht_support = sta->deflink.ht_cap.ht_supported;
4620	p->ht_caps = cpu_to_le16(sta->deflink.ht_cap.cap);
4621	p->extended_ht_caps = (sta->deflink.ht_cap.ampdu_factor & 3) |
4622		((sta->deflink.ht_cap.ampdu_density & 7) << 2);
4623	if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
4624		rates = sta->deflink.supp_rates[NL80211_BAND_2GHZ];
4625	else
4626		rates = sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
4627	legacy_rate_mask_to_array(p->legacy_rates, rates);
4628	memcpy(p->ht_rates, &sta->deflink.ht_cap.mcs, 16);
4629	p->interop = 1;
4630	p->amsdu_enabled = 0;
4631
4632	rc = mwl8k_post_cmd(hw, &cmd->header);
4633	if (!rc)
4634		rc = p->station_id;
4635	kfree(cmd);
4636
4637	return rc;
4638}
4639
4640static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4641				      struct ieee80211_vif *vif, u8 *addr)
4642{
4643	struct mwl8k_cmd_update_stadb *cmd;
4644	int rc;
4645
4646	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4647	if (cmd == NULL)
4648		return -ENOMEM;
4649
4650	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4651	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4652	cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4653	memcpy(cmd->peer_addr, addr, ETH_ALEN);
4654
4655	rc = mwl8k_post_cmd(hw, &cmd->header);
4656	kfree(cmd);
4657
4658	return rc;
4659}
4660
4661
4662/*
4663 * Interrupt handling.
4664 */
4665static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4666{
4667	struct ieee80211_hw *hw = dev_id;
4668	struct mwl8k_priv *priv = hw->priv;
4669	u32 status;
4670
4671	status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4672	if (!status)
4673		return IRQ_NONE;
4674
4675	if (status & MWL8K_A2H_INT_TX_DONE) {
4676		status &= ~MWL8K_A2H_INT_TX_DONE;
4677		tasklet_schedule(&priv->poll_tx_task);
4678	}
4679
4680	if (status & MWL8K_A2H_INT_RX_READY) {
4681		status &= ~MWL8K_A2H_INT_RX_READY;
4682		tasklet_schedule(&priv->poll_rx_task);
4683	}
4684
4685	if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
4686		iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
4687			  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4688
4689		atomic_inc(&priv->watchdog_event_pending);
4690		status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4691		ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4692	}
4693
4694	if (status)
4695		iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4696
4697	if (status & MWL8K_A2H_INT_OPC_DONE) {
4698		if (priv->hostcmd_wait != NULL)
4699			complete(priv->hostcmd_wait);
4700	}
4701
4702	if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
4703		if (!mutex_is_locked(&priv->fw_mutex) &&
4704		    priv->radio_on && priv->pending_tx_pkts)
4705			mwl8k_tx_start(priv);
4706	}
4707
4708	return IRQ_HANDLED;
4709}
4710
4711static void mwl8k_tx_poll(struct tasklet_struct *t)
4712{
4713	struct mwl8k_priv *priv = from_tasklet(priv, t, poll_tx_task);
4714	struct ieee80211_hw *hw = pci_get_drvdata(priv->pdev);
4715	int limit;
4716	int i;
4717
4718	limit = 32;
4719
4720	spin_lock(&priv->tx_lock);
4721
4722	for (i = 0; i < mwl8k_tx_queues(priv); i++)
4723		limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4724
4725	if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4726		complete(priv->tx_wait);
4727		priv->tx_wait = NULL;
4728	}
4729
4730	spin_unlock(&priv->tx_lock);
4731
4732	if (limit) {
4733		writel(~MWL8K_A2H_INT_TX_DONE,
4734		       priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4735	} else {
4736		tasklet_schedule(&priv->poll_tx_task);
4737	}
4738}
4739
4740static void mwl8k_rx_poll(struct tasklet_struct *t)
4741{
4742	struct mwl8k_priv *priv = from_tasklet(priv, t, poll_rx_task);
4743	struct ieee80211_hw *hw = pci_get_drvdata(priv->pdev);
4744	int limit;
4745
4746	limit = 32;
4747	limit -= rxq_process(hw, 0, limit);
4748	limit -= rxq_refill(hw, 0, limit);
4749
4750	if (limit) {
4751		writel(~MWL8K_A2H_INT_RX_READY,
4752		       priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4753	} else {
4754		tasklet_schedule(&priv->poll_rx_task);
4755	}
4756}
4757
4758
4759/*
4760 * Core driver operations.
4761 */
4762static void mwl8k_tx(struct ieee80211_hw *hw,
4763		     struct ieee80211_tx_control *control,
4764		     struct sk_buff *skb)
4765{
4766	struct mwl8k_priv *priv = hw->priv;
4767	int index = skb_get_queue_mapping(skb);
4768
4769	if (!priv->radio_on) {
4770		wiphy_debug(hw->wiphy,
4771			    "dropped TX frame since radio disabled\n");
4772		dev_kfree_skb(skb);
4773		return;
4774	}
4775
4776	mwl8k_txq_xmit(hw, index, control->sta, skb);
4777}
4778
4779static int mwl8k_start(struct ieee80211_hw *hw)
4780{
4781	struct mwl8k_priv *priv = hw->priv;
4782	int rc;
4783
4784	rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4785			 IRQF_SHARED, MWL8K_NAME, hw);
4786	if (rc) {
4787		priv->irq = -1;
4788		wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4789		return -EIO;
4790	}
4791	priv->irq = priv->pdev->irq;
4792
4793	/* Enable TX reclaim and RX tasklets.  */
4794	tasklet_enable(&priv->poll_tx_task);
4795	tasklet_enable(&priv->poll_rx_task);
4796
4797	/* Enable interrupts */
4798	iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4799	iowrite32(MWL8K_A2H_EVENTS,
4800		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4801
4802	rc = mwl8k_fw_lock(hw);
4803	if (!rc) {
4804		rc = mwl8k_cmd_radio_enable(hw);
4805
4806		if (!priv->ap_fw) {
4807			if (!rc)
4808				rc = mwl8k_cmd_enable_sniffer(hw, 0);
4809
4810			if (!rc)
4811				rc = mwl8k_cmd_set_pre_scan(hw);
4812
4813			if (!rc)
4814				rc = mwl8k_cmd_set_post_scan(hw,
4815						"\x00\x00\x00\x00\x00\x00");
4816		}
4817
4818		if (!rc)
4819			rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
4820
4821		if (!rc)
4822			rc = mwl8k_cmd_set_wmm_mode(hw, 0);
4823
4824		mwl8k_fw_unlock(hw);
4825	}
4826
4827	if (rc) {
4828		iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4829		free_irq(priv->pdev->irq, hw);
4830		priv->irq = -1;
4831		tasklet_disable(&priv->poll_tx_task);
4832		tasklet_disable(&priv->poll_rx_task);
4833	} else {
4834		ieee80211_wake_queues(hw);
4835	}
4836
4837	return rc;
4838}
4839
4840static void mwl8k_stop(struct ieee80211_hw *hw, bool suspend)
4841{
4842	struct mwl8k_priv *priv = hw->priv;
4843	int i;
4844
4845	if (!priv->hw_restart_in_progress)
4846		mwl8k_cmd_radio_disable(hw);
4847
4848	ieee80211_stop_queues(hw);
4849
4850	/* Disable interrupts */
4851	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4852	if (priv->irq != -1) {
4853		free_irq(priv->pdev->irq, hw);
4854		priv->irq = -1;
4855	}
4856
4857	/* Stop finalize join worker */
4858	cancel_work_sync(&priv->finalize_join_worker);
4859	cancel_work_sync(&priv->watchdog_ba_handle);
4860	if (priv->beacon_skb != NULL)
4861		dev_kfree_skb(priv->beacon_skb);
4862
4863	/* Stop TX reclaim and RX tasklets.  */
4864	tasklet_disable(&priv->poll_tx_task);
4865	tasklet_disable(&priv->poll_rx_task);
4866
4867	/* Return all skbs to mac80211 */
4868	for (i = 0; i < mwl8k_tx_queues(priv); i++)
4869		mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
4870}
4871
4872static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4873
4874static int mwl8k_add_interface(struct ieee80211_hw *hw,
4875			       struct ieee80211_vif *vif)
4876{
4877	struct mwl8k_priv *priv = hw->priv;
4878	struct mwl8k_vif *mwl8k_vif;
4879	u32 macids_supported;
4880	int macid, rc;
4881	struct mwl8k_device_info *di;
4882
4883	/*
4884	 * Reject interface creation if sniffer mode is active, as
4885	 * STA operation is mutually exclusive with hardware sniffer
4886	 * mode.  (Sniffer mode is only used on STA firmware.)
4887	 */
4888	if (priv->sniffer_enabled) {
4889		wiphy_info(hw->wiphy,
4890			   "unable to create STA interface because sniffer mode is enabled\n");
4891		return -EINVAL;
4892	}
4893
4894	di = priv->device_info;
4895	switch (vif->type) {
4896	case NL80211_IFTYPE_AP:
4897		if (!priv->ap_fw && di->fw_image_ap) {
4898			/* we must load the ap fw to meet this request */
4899			if (!list_empty(&priv->vif_list))
4900				return -EBUSY;
4901			rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4902			if (rc)
4903				return rc;
4904		}
4905		macids_supported = priv->ap_macids_supported;
4906		break;
4907	case NL80211_IFTYPE_STATION:
4908		if (priv->ap_fw && di->fw_image_sta) {
4909			if (!list_empty(&priv->vif_list)) {
4910				wiphy_warn(hw->wiphy, "AP interface is running.\n"
4911					   "Adding STA interface for WDS");
4912			} else {
4913				/* we must load the sta fw to
4914				 * meet this request.
4915				 */
4916				rc = mwl8k_reload_firmware(hw,
4917							   di->fw_image_sta);
4918				if (rc)
4919					return rc;
4920			}
4921		}
4922		macids_supported = priv->sta_macids_supported;
4923		break;
4924	default:
4925		return -EINVAL;
4926	}
4927
4928	macid = ffs(macids_supported & ~priv->macids_used);
4929	if (!macid--)
4930		return -EBUSY;
4931
4932	/* Setup driver private area. */
4933	mwl8k_vif = MWL8K_VIF(vif);
4934	memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
4935	mwl8k_vif->vif = vif;
4936	mwl8k_vif->macid = macid;
4937	mwl8k_vif->seqno = 0;
4938	memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4939	mwl8k_vif->is_hw_crypto_enabled = false;
4940
4941	/* Set the mac address.  */
4942	mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4943
4944	if (vif->type == NL80211_IFTYPE_AP)
4945		mwl8k_cmd_set_new_stn_add_self(hw, vif);
4946
4947	priv->macids_used |= 1 << mwl8k_vif->macid;
4948	list_add_tail(&mwl8k_vif->list, &priv->vif_list);
4949
4950	return 0;
4951}
4952
4953static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4954{
4955	/* Has ieee80211_restart_hw re-added the removed interfaces? */
4956	if (!priv->macids_used)
4957		return;
4958
4959	priv->macids_used &= ~(1 << vif->macid);
4960	list_del(&vif->list);
4961}
4962
4963static void mwl8k_remove_interface(struct ieee80211_hw *hw,
4964				   struct ieee80211_vif *vif)
4965{
4966	struct mwl8k_priv *priv = hw->priv;
4967	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4968
4969	if (vif->type == NL80211_IFTYPE_AP)
4970		mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4971
4972	mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
4973
4974	mwl8k_remove_vif(priv, mwl8k_vif);
4975}
4976
4977static void mwl8k_hw_restart_work(struct work_struct *work)
4978{
4979	struct mwl8k_priv *priv =
4980		container_of(work, struct mwl8k_priv, fw_reload);
4981	struct ieee80211_hw *hw = priv->hw;
4982	struct mwl8k_device_info *di;
4983	int rc;
4984
4985	/* If some command is waiting for a response, clear it */
4986	if (priv->hostcmd_wait != NULL) {
4987		complete(priv->hostcmd_wait);
4988		priv->hostcmd_wait = NULL;
4989	}
4990
4991	priv->hw_restart_owner = current;
4992	di = priv->device_info;
4993	mwl8k_fw_lock(hw);
4994
4995	if (priv->ap_fw)
4996		rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4997	else
4998		rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4999
5000	if (rc)
5001		goto fail;
5002
5003	priv->hw_restart_owner = NULL;
5004	priv->hw_restart_in_progress = false;
5005
5006	/*
5007	 * This unlock will wake up the queues and
5008	 * also opens the command path for other
5009	 * commands
5010	 */
5011	mwl8k_fw_unlock(hw);
5012
5013	ieee80211_restart_hw(hw);
5014
5015	wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
5016
5017	return;
5018fail:
5019	mwl8k_fw_unlock(hw);
5020
5021	wiphy_err(hw->wiphy, "Firmware restart failed\n");
5022}
5023
5024static int mwl8k_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
5025{
5026	struct ieee80211_conf *conf = &hw->conf;
5027	struct mwl8k_priv *priv = hw->priv;
5028	int rc;
5029
5030	rc = mwl8k_fw_lock(hw);
5031	if (rc)
5032		return rc;
5033
5034	if (conf->flags & IEEE80211_CONF_IDLE)
5035		rc = mwl8k_cmd_radio_disable(hw);
5036	else
5037		rc = mwl8k_cmd_radio_enable(hw);
5038	if (rc)
5039		goto out;
5040
5041	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
5042		rc = mwl8k_cmd_set_rf_channel(hw, conf);
5043		if (rc)
5044			goto out;
5045	}
5046
5047	if (conf->power_level > 18)
5048		conf->power_level = 18;
5049
5050	if (priv->ap_fw) {
5051
5052		if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
5053			rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
5054			if (rc)
5055				goto out;
5056		}
5057
5058
5059	} else {
5060		rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
5061		if (rc)
5062			goto out;
5063		rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
5064	}
5065
5066out:
5067	mwl8k_fw_unlock(hw);
5068
5069	return rc;
5070}
5071
5072static void
5073mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5074			   struct ieee80211_bss_conf *info, u32 changed)
5075{
5076	struct mwl8k_priv *priv = hw->priv;
5077	u32 ap_legacy_rates = 0;
5078	u8 ap_mcs_rates[16];
5079	int rc;
5080
5081	if (mwl8k_fw_lock(hw))
5082		return;
5083
5084	/*
5085	 * No need to capture a beacon if we're no longer associated.
5086	 */
5087	if ((changed & BSS_CHANGED_ASSOC) && !vif->cfg.assoc)
5088		priv->capture_beacon = false;
5089
5090	/*
5091	 * Get the AP's legacy and MCS rates.
5092	 */
5093	if (vif->cfg.assoc) {
5094		struct ieee80211_sta *ap;
5095
5096		rcu_read_lock();
5097
5098		ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
5099		if (ap == NULL) {
5100			rcu_read_unlock();
5101			goto out;
5102		}
5103
5104		if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ) {
5105			ap_legacy_rates = ap->deflink.supp_rates[NL80211_BAND_2GHZ];
5106		} else {
5107			ap_legacy_rates =
5108				ap->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
5109		}
5110		memcpy(ap_mcs_rates, &ap->deflink.ht_cap.mcs, 16);
5111
5112		rcu_read_unlock();
5113
5114		if (changed & BSS_CHANGED_ASSOC) {
5115			if (!priv->ap_fw) {
5116				rc = mwl8k_cmd_set_rate(hw, vif,
5117							ap_legacy_rates,
5118							ap_mcs_rates);
5119				if (rc)
5120					goto out;
5121
5122				rc = mwl8k_cmd_use_fixed_rate_sta(hw);
5123				if (rc)
5124					goto out;
5125			} else {
5126				int idx;
5127				int rate;
5128
5129				/* Use AP firmware specific rate command.
5130				 */
5131				idx = ffs(vif->bss_conf.basic_rates);
5132				if (idx)
5133					idx--;
5134
5135				if (hw->conf.chandef.chan->band ==
5136				    NL80211_BAND_2GHZ)
5137					rate = mwl8k_rates_24[idx].hw_value;
5138				else
5139					rate = mwl8k_rates_50[idx].hw_value;
5140
5141				mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
5142			}
5143		}
5144	}
5145
5146	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5147		rc = mwl8k_set_radio_preamble(hw,
5148				vif->bss_conf.use_short_preamble);
5149		if (rc)
5150			goto out;
5151	}
5152
5153	if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw)  {
5154		rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
5155		if (rc)
5156			goto out;
5157	}
5158
5159	if (vif->cfg.assoc && !priv->ap_fw &&
5160	    (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
5161			BSS_CHANGED_HT))) {
5162		rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
5163		if (rc)
5164			goto out;
5165	}
5166
5167	if (vif->cfg.assoc &&
5168	    (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
5169		/*
5170		 * Finalize the join.  Tell rx handler to process
5171		 * next beacon from our BSSID.
5172		 */
5173		memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
5174		priv->capture_beacon = true;
5175	}
5176
5177out:
5178	mwl8k_fw_unlock(hw);
5179}
5180
5181static void
5182mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5183			  struct ieee80211_bss_conf *info, u32 changed)
5184{
5185	int rc;
5186
5187	if (mwl8k_fw_lock(hw))
5188		return;
5189
5190	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5191		rc = mwl8k_set_radio_preamble(hw,
5192				vif->bss_conf.use_short_preamble);
5193		if (rc)
5194			goto out;
5195	}
5196
5197	if (changed & BSS_CHANGED_BASIC_RATES) {
5198		int idx;
5199		int rate;
5200
5201		/*
5202		 * Use lowest supported basic rate for multicasts
5203		 * and management frames (such as probe responses --
5204		 * beacons will always go out at 1 Mb/s).
5205		 */
5206		idx = ffs(vif->bss_conf.basic_rates);
5207		if (idx)
5208			idx--;
5209
5210		if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
5211			rate = mwl8k_rates_24[idx].hw_value;
5212		else
5213			rate = mwl8k_rates_50[idx].hw_value;
5214
5215		mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
5216	}
5217
5218	if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
5219		struct sk_buff *skb;
5220
5221		skb = ieee80211_beacon_get(hw, vif, 0);
5222		if (skb != NULL) {
5223			mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
5224			kfree_skb(skb);
5225		}
5226	}
5227
5228	if (changed & BSS_CHANGED_BEACON_ENABLED)
5229		mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
5230
5231out:
5232	mwl8k_fw_unlock(hw);
5233}
5234
5235static void
5236mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5237		       struct ieee80211_bss_conf *info, u64 changed)
5238{
5239	if (vif->type == NL80211_IFTYPE_STATION)
5240		mwl8k_bss_info_changed_sta(hw, vif, info, changed);
5241	if (vif->type == NL80211_IFTYPE_AP)
5242		mwl8k_bss_info_changed_ap(hw, vif, info, changed);
5243}
5244
5245static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
5246				   struct netdev_hw_addr_list *mc_list)
5247{
5248	struct mwl8k_cmd_pkt_hdr *cmd;
5249
5250	/*
5251	 * Synthesize and return a command packet that programs the
5252	 * hardware multicast address filter.  At this point we don't
5253	 * know whether FIF_ALLMULTI is being requested, but if it is,
5254	 * we'll end up throwing this packet away and creating a new
5255	 * one in mwl8k_configure_filter().
5256	 */
5257	cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
5258
5259	return (unsigned long)cmd;
5260}
5261
5262static int
5263mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
5264			       unsigned int changed_flags,
5265			       unsigned int *total_flags)
5266{
5267	struct mwl8k_priv *priv = hw->priv;
5268
5269	/*
5270	 * Hardware sniffer mode is mutually exclusive with STA
5271	 * operation, so refuse to enable sniffer mode if a STA
5272	 * interface is active.
5273	 */
5274	if (!list_empty(&priv->vif_list)) {
5275		if (net_ratelimit())
5276			wiphy_info(hw->wiphy,
5277				   "not enabling sniffer mode because STA interface is active\n");
5278		return 0;
5279	}
5280
5281	if (!priv->sniffer_enabled) {
5282		if (mwl8k_cmd_enable_sniffer(hw, 1))
5283			return 0;
5284		priv->sniffer_enabled = true;
5285	}
5286
5287	*total_flags &=	FIF_ALLMULTI |
5288			FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
5289			FIF_OTHER_BSS;
5290
5291	return 1;
5292}
5293
5294static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
5295{
5296	if (!list_empty(&priv->vif_list))
5297		return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
5298
5299	return NULL;
5300}
5301
5302static void mwl8k_configure_filter(struct ieee80211_hw *hw,
5303				   unsigned int changed_flags,
5304				   unsigned int *total_flags,
5305				   u64 multicast)
5306{
5307	struct mwl8k_priv *priv = hw->priv;
5308	struct mwl8k_cmd_pkt_hdr *cmd = (void *)(unsigned long)multicast;
5309
5310	/*
5311	 * AP firmware doesn't allow fine-grained control over
5312	 * the receive filter.
5313	 */
5314	if (priv->ap_fw) {
5315		*total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5316		kfree(cmd);
5317		return;
5318	}
5319
5320	/*
5321	 * Enable hardware sniffer mode if FIF_CONTROL or
5322	 * FIF_OTHER_BSS is requested.
5323	 */
5324	if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
5325	    mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
5326		kfree(cmd);
5327		return;
5328	}
5329
5330	/* Clear unsupported feature flags */
5331	*total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5332
5333	if (mwl8k_fw_lock(hw)) {
5334		kfree(cmd);
5335		return;
5336	}
5337
5338	if (priv->sniffer_enabled) {
5339		mwl8k_cmd_enable_sniffer(hw, 0);
5340		priv->sniffer_enabled = false;
5341	}
5342
5343	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
5344		if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
5345			/*
5346			 * Disable the BSS filter.
5347			 */
5348			mwl8k_cmd_set_pre_scan(hw);
5349		} else {
5350			struct mwl8k_vif *mwl8k_vif;
5351			const u8 *bssid;
5352
5353			/*
5354			 * Enable the BSS filter.
5355			 *
5356			 * If there is an active STA interface, use that
5357			 * interface's BSSID, otherwise use a dummy one
5358			 * (where the OUI part needs to be nonzero for
5359			 * the BSSID to be accepted by POST_SCAN).
5360			 */
5361			mwl8k_vif = mwl8k_first_vif(priv);
5362			if (mwl8k_vif != NULL)
5363				bssid = mwl8k_vif->vif->bss_conf.bssid;
5364			else
5365				bssid = "\x01\x00\x00\x00\x00\x00";
5366
5367			mwl8k_cmd_set_post_scan(hw, bssid);
5368		}
5369	}
5370
5371	/*
5372	 * If FIF_ALLMULTI is being requested, throw away the command
5373	 * packet that ->prepare_multicast() built and replace it with
5374	 * a command packet that enables reception of all multicast
5375	 * packets.
5376	 */
5377	if (*total_flags & FIF_ALLMULTI) {
5378		kfree(cmd);
5379		cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
5380	}
5381
5382	if (cmd != NULL) {
5383		mwl8k_post_cmd(hw, cmd);
5384		kfree(cmd);
5385	}
5386
5387	mwl8k_fw_unlock(hw);
5388}
5389
5390static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
5391				   u32 value)
5392{
5393	return mwl8k_cmd_set_rts_threshold(hw, radio_idx, value);
5394}
5395
5396static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5397			    struct ieee80211_vif *vif,
5398			    struct ieee80211_sta *sta)
5399{
5400	struct mwl8k_priv *priv = hw->priv;
5401
5402	if (priv->ap_fw)
5403		return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5404	else
5405		return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5406}
5407
5408static int mwl8k_sta_add(struct ieee80211_hw *hw,
5409			 struct ieee80211_vif *vif,
5410			 struct ieee80211_sta *sta)
5411{
5412	struct mwl8k_priv *priv = hw->priv;
5413	int ret;
5414	int i;
5415	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5416	struct ieee80211_key_conf *key;
5417
5418	if (!priv->ap_fw) {
5419		ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5420		if (ret >= 0) {
5421			MWL8K_STA(sta)->peer_id = ret;
5422			if (sta->deflink.ht_cap.ht_supported)
5423				MWL8K_STA(sta)->is_ampdu_allowed = true;
5424			ret = 0;
5425		}
5426
5427	} else {
5428		ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
5429	}
5430
5431	for (i = 0; i < NUM_WEP_KEYS; i++) {
5432		key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5433		if (mwl8k_vif->wep_key_conf[i].enabled)
5434			mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5435	}
5436	return ret;
5437}
5438
5439static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5440			 struct ieee80211_vif *vif,
5441			 unsigned int link_id, u16 queue,
5442			 const struct ieee80211_tx_queue_params *params)
5443{
5444	struct mwl8k_priv *priv = hw->priv;
5445	int rc;
5446
5447	rc = mwl8k_fw_lock(hw);
5448	if (!rc) {
5449		BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
5450		memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5451
5452		if (!priv->wmm_enabled)
5453			rc = mwl8k_cmd_set_wmm_mode(hw, 1);
5454
5455		if (!rc) {
5456			int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
5457			rc = mwl8k_cmd_set_edca_params(hw, q,
5458						       params->cw_min,
5459						       params->cw_max,
5460						       params->aifs,
5461						       params->txop);
5462		}
5463
5464		mwl8k_fw_unlock(hw);
5465	}
5466
5467	return rc;
5468}
5469
5470static int mwl8k_get_stats(struct ieee80211_hw *hw,
5471			   struct ieee80211_low_level_stats *stats)
5472{
5473	return mwl8k_cmd_get_stat(hw, stats);
5474}
5475
5476static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5477				struct survey_info *survey)
5478{
5479	struct mwl8k_priv *priv = hw->priv;
5480	struct ieee80211_conf *conf = &hw->conf;
5481	struct ieee80211_supported_band *sband;
5482
5483	if (priv->ap_fw) {
5484		sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
5485
5486		if (sband && idx >= sband->n_channels) {
5487			idx -= sband->n_channels;
5488			sband = NULL;
5489		}
5490
5491		if (!sband)
5492			sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
5493
5494		if (!sband || idx >= sband->n_channels)
5495			return -ENOENT;
5496
5497		memcpy(survey, &priv->survey[idx], sizeof(*survey));
5498		survey->channel = &sband->channels[idx];
5499
5500		return 0;
5501	}
5502
5503	if (idx != 0)
5504		return -ENOENT;
5505
5506	survey->channel = conf->chandef.chan;
5507	survey->filled = SURVEY_INFO_NOISE_DBM;
5508	survey->noise = priv->noise;
5509
5510	return 0;
5511}
5512
5513#define MAX_AMPDU_ATTEMPTS 5
5514
5515static int
5516mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5517		   struct ieee80211_ampdu_params *params)
5518{
5519	struct ieee80211_sta *sta = params->sta;
5520	enum ieee80211_ampdu_mlme_action action = params->action;
5521	u16 tid = params->tid;
5522	u16 *ssn = &params->ssn;
5523	u8 buf_size = params->buf_size;
5524	int i, rc = 0;
5525	struct mwl8k_priv *priv = hw->priv;
5526	struct mwl8k_ampdu_stream *stream;
5527	u8 *addr = sta->addr, idx;
5528	struct mwl8k_sta *sta_info = MWL8K_STA(sta);
5529
5530	if (!ieee80211_hw_check(hw, AMPDU_AGGREGATION))
5531		return -ENOTSUPP;
5532
5533	spin_lock(&priv->stream_lock);
5534	stream = mwl8k_lookup_stream(hw, addr, tid);
5535
5536	switch (action) {
5537	case IEEE80211_AMPDU_RX_START:
5538	case IEEE80211_AMPDU_RX_STOP:
5539		break;
5540	case IEEE80211_AMPDU_TX_START:
5541		/* By the time we get here the hw queues may contain outgoing
5542		 * packets for this RA/TID that are not part of this BA
5543		 * session.  The hw will assign sequence numbers to these
5544		 * packets as they go out.  So if we query the hw for its next
5545		 * sequence number and use that for the SSN here, it may end up
5546		 * being wrong, which will lead to sequence number mismatch at
5547		 * the recipient.  To avoid this, we reset the sequence number
5548		 * to O for the first MPDU in this BA stream.
5549		 */
5550		*ssn = 0;
5551		if (stream == NULL) {
5552			/* This means that somebody outside this driver called
5553			 * ieee80211_start_tx_ba_session.  This is unexpected
5554			 * because we do our own rate control.  Just warn and
5555			 * move on.
5556			 */
5557			wiphy_warn(hw->wiphy, "Unexpected call to %s.  "
5558				   "Proceeding anyway.\n", __func__);
5559			stream = mwl8k_add_stream(hw, sta, tid);
5560		}
5561		if (stream == NULL) {
5562			wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5563			rc = -EBUSY;
5564			break;
5565		}
5566		stream->state = AMPDU_STREAM_IN_PROGRESS;
5567
5568		/* Release the lock before we do the time consuming stuff */
5569		spin_unlock(&priv->stream_lock);
5570		for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
5571
5572			/* Check if link is still valid */
5573			if (!sta_info->is_ampdu_allowed) {
5574				spin_lock(&priv->stream_lock);
5575				mwl8k_remove_stream(hw, stream);
5576				spin_unlock(&priv->stream_lock);
5577				return -EBUSY;
5578			}
5579
5580			rc = mwl8k_check_ba(hw, stream, vif);
5581
5582			/* If HW restart is in progress mwl8k_post_cmd will
5583			 * return -EBUSY. Avoid retrying mwl8k_check_ba in
5584			 * such cases
5585			 */
5586			if (!rc || rc == -EBUSY)
5587				break;
5588			/*
5589			 * HW queues take time to be flushed, give them
5590			 * sufficient time
5591			 */
5592
5593			msleep(1000);
5594		}
5595		spin_lock(&priv->stream_lock);
5596		if (rc) {
5597			wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5598				" attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5599			mwl8k_remove_stream(hw, stream);
5600			rc = -EBUSY;
5601			break;
5602		}
5603		rc = IEEE80211_AMPDU_TX_START_IMMEDIATE;
5604		break;
5605	case IEEE80211_AMPDU_TX_STOP_CONT:
5606	case IEEE80211_AMPDU_TX_STOP_FLUSH:
5607	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5608		if (stream) {
5609			if (stream->state == AMPDU_STREAM_ACTIVE) {
5610				idx = stream->idx;
5611				spin_unlock(&priv->stream_lock);
5612				mwl8k_destroy_ba(hw, idx);
5613				spin_lock(&priv->stream_lock);
5614			}
5615			mwl8k_remove_stream(hw, stream);
5616		}
5617		ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5618		break;
5619	case IEEE80211_AMPDU_TX_OPERATIONAL:
5620		BUG_ON(stream == NULL);
5621		BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5622		spin_unlock(&priv->stream_lock);
5623		rc = mwl8k_create_ba(hw, stream, buf_size, vif);
5624		spin_lock(&priv->stream_lock);
5625		if (!rc)
5626			stream->state = AMPDU_STREAM_ACTIVE;
5627		else {
5628			idx = stream->idx;
5629			spin_unlock(&priv->stream_lock);
5630			mwl8k_destroy_ba(hw, idx);
5631			spin_lock(&priv->stream_lock);
5632			wiphy_debug(hw->wiphy,
5633				"Failed adding stream for sta %pM tid %d\n",
5634				addr, tid);
5635			mwl8k_remove_stream(hw, stream);
5636		}
5637		break;
5638
5639	default:
5640		rc = -ENOTSUPP;
5641	}
5642
5643	spin_unlock(&priv->stream_lock);
5644	return rc;
5645}
5646
5647static void mwl8k_sw_scan_start(struct ieee80211_hw *hw,
5648				struct ieee80211_vif *vif,
5649				const u8 *mac_addr)
5650{
5651	struct mwl8k_priv *priv = hw->priv;
5652	u8 tmp;
5653
5654	if (!priv->ap_fw)
5655		return;
5656
5657	/* clear all stats */
5658	priv->channel_time = 0;
5659	ioread32(priv->regs + BBU_RXRDY_CNT_REG);
5660	ioread32(priv->regs + NOK_CCA_CNT_REG);
5661	mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &tmp);
5662
5663	priv->sw_scan_start = true;
5664}
5665
5666static void mwl8k_sw_scan_complete(struct ieee80211_hw *hw,
5667				   struct ieee80211_vif *vif)
5668{
5669	struct mwl8k_priv *priv = hw->priv;
5670	u8 tmp;
5671
5672	if (!priv->ap_fw)
5673		return;
5674
5675	priv->sw_scan_start = false;
5676
5677	/* clear all stats */
5678	priv->channel_time = 0;
5679	ioread32(priv->regs + BBU_RXRDY_CNT_REG);
5680	ioread32(priv->regs + NOK_CCA_CNT_REG);
5681	mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &tmp);
5682}
5683
5684static const struct ieee80211_ops mwl8k_ops = {
5685	.add_chanctx = ieee80211_emulate_add_chanctx,
5686	.remove_chanctx = ieee80211_emulate_remove_chanctx,
5687	.change_chanctx = ieee80211_emulate_change_chanctx,
5688	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
5689	.tx			= mwl8k_tx,
5690	.wake_tx_queue		= ieee80211_handle_wake_tx_queue,
5691	.start			= mwl8k_start,
5692	.stop			= mwl8k_stop,
5693	.add_interface		= mwl8k_add_interface,
5694	.remove_interface	= mwl8k_remove_interface,
5695	.config			= mwl8k_config,
5696	.bss_info_changed	= mwl8k_bss_info_changed,
5697	.prepare_multicast	= mwl8k_prepare_multicast,
5698	.configure_filter	= mwl8k_configure_filter,
5699	.set_key                = mwl8k_set_key,
5700	.set_rts_threshold	= mwl8k_set_rts_threshold,
5701	.sta_add		= mwl8k_sta_add,
5702	.sta_remove		= mwl8k_sta_remove,
5703	.conf_tx		= mwl8k_conf_tx,
5704	.get_stats		= mwl8k_get_stats,
5705	.get_survey		= mwl8k_get_survey,
5706	.ampdu_action		= mwl8k_ampdu_action,
5707	.sw_scan_start		= mwl8k_sw_scan_start,
5708	.sw_scan_complete	= mwl8k_sw_scan_complete,
5709};
5710
5711static void mwl8k_finalize_join_worker(struct work_struct *work)
5712{
5713	struct mwl8k_priv *priv =
5714		container_of(work, struct mwl8k_priv, finalize_join_worker);
5715	struct sk_buff *skb = priv->beacon_skb;
5716	struct ieee80211_mgmt *mgmt = (void *)skb->data;
5717	int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5718	const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5719					 mgmt->u.beacon.variable, len);
5720	int dtim_period = 1;
5721
5722	if (tim && tim[1] >= 2)
5723		dtim_period = tim[3];
5724
5725	mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
5726
5727	dev_kfree_skb(skb);
5728	priv->beacon_skb = NULL;
5729}
5730
5731enum {
5732	MWL8363 = 0,
5733	MWL8687,
5734	MWL8366,
5735	MWL8764,
5736};
5737
5738#define MWL8K_8366_AP_FW_API 3
5739#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5740#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5741
5742#define MWL8K_8764_AP_FW_API 1
5743#define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
5744#define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
5745
5746static struct mwl8k_device_info mwl8k_info_tbl[] = {
5747	[MWL8363] = {
5748		.part_name	= "88w8363",
5749		.helper_image	= "mwl8k/helper_8363.fw",
5750		.fw_image_sta	= "mwl8k/fmimage_8363.fw",
5751	},
5752	[MWL8687] = {
5753		.part_name	= "88w8687",
5754		.helper_image	= "mwl8k/helper_8687.fw",
5755		.fw_image_sta	= "mwl8k/fmimage_8687.fw",
5756	},
5757	[MWL8366] = {
5758		.part_name	= "88w8366",
5759		.helper_image	= "mwl8k/helper_8366.fw",
5760		.fw_image_sta	= "mwl8k/fmimage_8366.fw",
5761		.fw_image_ap	= MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5762		.fw_api_ap	= MWL8K_8366_AP_FW_API,
5763		.ap_rxd_ops	= &rxd_ap_ops,
5764	},
5765	[MWL8764] = {
5766		.part_name	= "88w8764",
5767		.fw_image_ap	= MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
5768		.fw_api_ap	= MWL8K_8764_AP_FW_API,
5769		.ap_rxd_ops	= &rxd_ap_ops,
5770	},
5771};
5772
5773MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5774MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5775MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5776MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5777MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5778MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
5779MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
5780
5781static const struct pci_device_id mwl8k_pci_id_table[] = {
5782	{ PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
5783	{ PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5784	{ PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
5785	{ PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5786	{ PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5787	{ PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
5788	{ PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
5789	{ PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
5790	{ PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
5791	{ PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
5792	{ },
5793};
5794MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5795
5796static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5797{
5798	int rc;
5799	printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5800	       "Trying alternative firmware %s\n", pci_name(priv->pdev),
5801	       priv->fw_pref, priv->fw_alt);
5802	rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5803	if (rc) {
5804		printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5805		       pci_name(priv->pdev), priv->fw_alt);
5806		return rc;
5807	}
5808	return 0;
5809}
5810
5811static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5812static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5813{
5814	struct mwl8k_priv *priv = context;
5815	struct mwl8k_device_info *di = priv->device_info;
5816	int rc;
5817
5818	switch (priv->fw_state) {
5819	case FW_STATE_INIT:
5820		if (!fw) {
5821			printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5822			       pci_name(priv->pdev), di->helper_image);
5823			goto fail;
5824		}
5825		priv->fw_helper = fw;
5826		rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5827				      true);
5828		if (rc && priv->fw_alt) {
5829			rc = mwl8k_request_alt_fw(priv);
5830			if (rc)
5831				goto fail;
5832			priv->fw_state = FW_STATE_LOADING_ALT;
5833		} else if (rc)
5834			goto fail;
5835		else
5836			priv->fw_state = FW_STATE_LOADING_PREF;
5837		break;
5838
5839	case FW_STATE_LOADING_PREF:
5840		if (!fw) {
5841			if (priv->fw_alt) {
5842				rc = mwl8k_request_alt_fw(priv);
5843				if (rc)
5844					goto fail;
5845				priv->fw_state = FW_STATE_LOADING_ALT;
5846			} else
5847				goto fail;
5848		} else {
5849			priv->fw_ucode = fw;
5850			rc = mwl8k_firmware_load_success(priv);
5851			if (rc)
5852				goto fail;
5853			else
5854				complete(&priv->firmware_loading_complete);
5855		}
5856		break;
5857
5858	case FW_STATE_LOADING_ALT:
5859		if (!fw) {
5860			printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5861			       pci_name(priv->pdev), di->helper_image);
5862			goto fail;
5863		}
5864		priv->fw_ucode = fw;
5865		rc = mwl8k_firmware_load_success(priv);
5866		if (rc)
5867			goto fail;
5868		else
5869			complete(&priv->firmware_loading_complete);
5870		break;
5871
5872	default:
5873		printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5874		       MWL8K_NAME, priv->fw_state);
5875		BUG_ON(1);
5876	}
5877
5878	return;
5879
5880fail:
5881	priv->fw_state = FW_STATE_ERROR;
5882	complete(&priv->firmware_loading_complete);
5883	mwl8k_release_firmware(priv);
5884	device_release_driver(&priv->pdev->dev);
5885}
5886
5887#define MAX_RESTART_ATTEMPTS 1
5888static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5889			       bool nowait)
5890{
5891	struct mwl8k_priv *priv = hw->priv;
5892	int rc;
5893	int count = MAX_RESTART_ATTEMPTS;
5894
5895retry:
5896	/* Reset firmware and hardware */
5897	mwl8k_hw_reset(priv);
5898
5899	/* Ask userland hotplug daemon for the device firmware */
5900	rc = mwl8k_request_firmware(priv, fw_image, nowait);
5901	if (rc) {
5902		wiphy_err(hw->wiphy, "Firmware files not found\n");
5903		return rc;
5904	}
5905
5906	if (nowait)
5907		return rc;
5908
5909	/* Load firmware into hardware */
5910	rc = mwl8k_load_firmware(hw);
5911	if (rc)
5912		wiphy_err(hw->wiphy, "Cannot start firmware\n");
5913
5914	/* Reclaim memory once firmware is successfully loaded */
5915	mwl8k_release_firmware(priv);
5916
5917	if (rc && count) {
5918		/* FW did not start successfully;
5919		 * lets try one more time
5920		 */
5921		count--;
5922		wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5923		msleep(20);
5924		goto retry;
5925	}
5926
5927	return rc;
5928}
5929
5930static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5931{
5932	struct mwl8k_priv *priv = hw->priv;
5933	int rc = 0;
5934	int i;
5935
5936	for (i = 0; i < mwl8k_tx_queues(priv); i++) {
5937		rc = mwl8k_txq_init(hw, i);
5938		if (rc)
5939			break;
5940		if (priv->ap_fw)
5941			iowrite32(priv->txq[i].txd_dma,
5942				  priv->sram + priv->txq_offset[i]);
5943	}
5944	return rc;
5945}
5946
5947/* initialize hw after successfully loading a firmware image */
5948static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5949{
5950	struct mwl8k_priv *priv = hw->priv;
5951	int rc = 0;
5952	int i;
5953
5954	if (priv->ap_fw) {
5955		priv->rxd_ops = priv->device_info->ap_rxd_ops;
5956		if (priv->rxd_ops == NULL) {
5957			wiphy_err(hw->wiphy,
5958				  "Driver does not have AP firmware image support for this hardware\n");
5959			rc = -ENOENT;
5960			goto err_stop_firmware;
5961		}
5962	} else {
5963		priv->rxd_ops = &rxd_sta_ops;
5964	}
5965
5966	priv->sniffer_enabled = false;
5967	priv->wmm_enabled = false;
5968	priv->pending_tx_pkts = 0;
5969	atomic_set(&priv->watchdog_event_pending, 0);
5970
5971	rc = mwl8k_rxq_init(hw, 0);
5972	if (rc)
5973		goto err_stop_firmware;
5974	rxq_refill(hw, 0, INT_MAX);
5975
5976	/* For the sta firmware, we need to know the dma addresses of tx queues
5977	 * before sending MWL8K_CMD_GET_HW_SPEC.  So we must initialize them
5978	 * prior to issuing this command.  But for the AP case, we learn the
5979	 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5980	 * case we must initialize the tx queues after.
5981	 */
5982	priv->num_ampdu_queues = 0;
5983	if (!priv->ap_fw) {
5984		rc = mwl8k_init_txqs(hw);
5985		if (rc)
5986			goto err_free_queues;
5987	}
5988
5989	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5990	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5991	iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5992		  MWL8K_A2H_INT_BA_WATCHDOG,
5993		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
5994	iowrite32(MWL8K_A2H_INT_OPC_DONE,
5995		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
5996
5997	rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5998			 IRQF_SHARED, MWL8K_NAME, hw);
5999	if (rc) {
6000		wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
6001		goto err_free_queues;
6002	}
6003
6004	/*
6005	 * When hw restart is requested,
6006	 * mac80211 will take care of clearing
6007	 * the ampdu streams, so do not clear
6008	 * the ampdu state here
6009	 */
6010	if (!priv->hw_restart_in_progress)
6011		memset(priv->ampdu, 0, sizeof(priv->ampdu));
6012
6013	/*
6014	 * Temporarily enable interrupts.  Initial firmware host
6015	 * commands use interrupts and avoid polling.  Disable
6016	 * interrupts when done.
6017	 */
6018	iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
6019
6020	/* Get config data, mac addrs etc */
6021	if (priv->ap_fw) {
6022		rc = mwl8k_cmd_get_hw_spec_ap(hw);
6023		if (!rc)
6024			rc = mwl8k_init_txqs(hw);
6025		if (!rc)
6026			rc = mwl8k_cmd_set_hw_spec(hw);
6027	} else {
6028		rc = mwl8k_cmd_get_hw_spec_sta(hw);
6029	}
6030	if (rc) {
6031		wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
6032		goto err_free_irq;
6033	}
6034
6035	/* Turn radio off */
6036	rc = mwl8k_cmd_radio_disable(hw);
6037	if (rc) {
6038		wiphy_err(hw->wiphy, "Cannot disable\n");
6039		goto err_free_irq;
6040	}
6041
6042	/* Clear MAC address */
6043	rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
6044	if (rc) {
6045		wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
6046		goto err_free_irq;
6047	}
6048
6049	/* Configure Antennas */
6050	rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
6051	if (rc)
6052		wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
6053	rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
6054	if (rc)
6055		wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
6056
6057
6058	/* Disable interrupts */
6059	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
6060	free_irq(priv->pdev->irq, hw);
6061
6062	wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
6063		   priv->device_info->part_name,
6064		   priv->hw_rev, hw->wiphy->perm_addr,
6065		   priv->ap_fw ? "AP" : "STA",
6066		   (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
6067		   (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
6068
6069	return 0;
6070
6071err_free_irq:
6072	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
6073	free_irq(priv->pdev->irq, hw);
6074
6075err_free_queues:
6076	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6077		mwl8k_txq_deinit(hw, i);
6078	mwl8k_rxq_deinit(hw, 0);
6079
6080err_stop_firmware:
6081	mwl8k_hw_reset(priv);
6082
6083	return rc;
6084}
6085
6086/*
6087 * invoke mwl8k_reload_firmware to change the firmware image after the device
6088 * has already been registered
6089 */
6090static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
6091{
6092	int i, rc = 0;
6093	struct mwl8k_priv *priv = hw->priv;
6094	struct mwl8k_vif *vif, *tmp_vif;
6095
6096	mwl8k_stop(hw, false);
6097	mwl8k_rxq_deinit(hw, 0);
6098
6099	/*
6100	 * All the existing interfaces are re-added by the ieee80211_reconfig;
6101	 * which means driver should remove existing interfaces before calling
6102	 * ieee80211_restart_hw
6103	 */
6104	if (priv->hw_restart_in_progress)
6105		list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
6106			mwl8k_remove_vif(priv, vif);
6107
6108	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6109		mwl8k_txq_deinit(hw, i);
6110
6111	rc = mwl8k_init_firmware(hw, fw_image, false);
6112	if (rc)
6113		goto fail;
6114
6115	rc = mwl8k_probe_hw(hw);
6116	if (rc)
6117		goto fail;
6118
6119	if (priv->hw_restart_in_progress)
6120		return rc;
6121
6122	rc = mwl8k_start(hw);
6123	if (rc)
6124		goto fail;
6125
6126	rc = mwl8k_config(hw, -1, ~0);
6127	if (rc)
6128		goto fail;
6129
6130	for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
6131		rc = mwl8k_conf_tx(hw, NULL, 0, i, &priv->wmm_params[i]);
6132		if (rc)
6133			goto fail;
6134	}
6135
6136	return rc;
6137
6138fail:
6139	printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
6140	return rc;
6141}
6142
6143static const struct ieee80211_iface_limit ap_if_limits[] = {
6144	{ .max = 8,	.types = BIT(NL80211_IFTYPE_AP) },
6145	{ .max = 1,	.types = BIT(NL80211_IFTYPE_STATION) },
6146};
6147
6148static const struct ieee80211_iface_combination ap_if_comb = {
6149	.limits = ap_if_limits,
6150	.n_limits = ARRAY_SIZE(ap_if_limits),
6151	.max_interfaces = 8,
6152	.num_different_channels = 1,
6153};
6154
6155
6156static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
6157{
6158	struct ieee80211_hw *hw = priv->hw;
6159	int i, rc;
6160
6161	rc = mwl8k_load_firmware(hw);
6162	mwl8k_release_firmware(priv);
6163	if (rc) {
6164		wiphy_err(hw->wiphy, "Cannot start firmware\n");
6165		return rc;
6166	}
6167
6168	/*
6169	 * Extra headroom is the size of the required DMA header
6170	 * minus the size of the smallest 802.11 frame (CTS frame).
6171	 */
6172	hw->extra_tx_headroom =
6173		sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
6174
6175	hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
6176
6177	hw->queues = MWL8K_TX_WMM_QUEUES;
6178
6179	/* Set rssi values to dBm */
6180	ieee80211_hw_set(hw, SIGNAL_DBM);
6181	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
6182
6183	/*
6184	 * Ask mac80211 to not to trigger PS mode
6185	 * based on PM bit of incoming frames.
6186	 */
6187	if (priv->ap_fw)
6188		ieee80211_hw_set(hw, AP_LINK_PS);
6189
6190	hw->vif_data_size = sizeof(struct mwl8k_vif);
6191	hw->sta_data_size = sizeof(struct mwl8k_sta);
6192
6193	priv->macids_used = 0;
6194	INIT_LIST_HEAD(&priv->vif_list);
6195
6196	/* Set default radio state and preamble */
6197	priv->radio_on = false;
6198	priv->radio_short_preamble = false;
6199
6200	/* Finalize join worker */
6201	INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
6202	/* Handle watchdog ba events */
6203	INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
6204	/* To reload the firmware if it crashes */
6205	INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
6206
6207	/* TX reclaim and RX tasklets.  */
6208	tasklet_setup(&priv->poll_tx_task, mwl8k_tx_poll);
6209	tasklet_disable(&priv->poll_tx_task);
6210	tasklet_setup(&priv->poll_rx_task, mwl8k_rx_poll);
6211	tasklet_disable(&priv->poll_rx_task);
6212
6213	/* Power management cookie */
6214	priv->cookie = dma_alloc_coherent(&priv->pdev->dev, 4,
6215					  &priv->cookie_dma, GFP_KERNEL);
6216	if (priv->cookie == NULL)
6217		return -ENOMEM;
6218
6219	mutex_init(&priv->fw_mutex);
6220	priv->fw_mutex_owner = NULL;
6221	priv->fw_mutex_depth = 0;
6222	priv->hostcmd_wait = NULL;
6223
6224	spin_lock_init(&priv->tx_lock);
6225
6226	spin_lock_init(&priv->stream_lock);
6227
6228	priv->tx_wait = NULL;
6229
6230	rc = mwl8k_probe_hw(hw);
6231	if (rc)
6232		goto err_free_cookie;
6233
6234	hw->wiphy->interface_modes = 0;
6235
6236	if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
6237		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
6238		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
6239		hw->wiphy->iface_combinations = &ap_if_comb;
6240		hw->wiphy->n_iface_combinations = 1;
6241	}
6242
6243	if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
6244		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
6245
6246	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6247
6248	rc = ieee80211_register_hw(hw);
6249	if (rc) {
6250		wiphy_err(hw->wiphy, "Cannot register device\n");
6251		goto err_unprobe_hw;
6252	}
6253
6254	return 0;
6255
6256err_unprobe_hw:
6257	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6258		mwl8k_txq_deinit(hw, i);
6259	mwl8k_rxq_deinit(hw, 0);
6260
6261err_free_cookie:
6262	if (priv->cookie != NULL)
6263		dma_free_coherent(&priv->pdev->dev, 4, priv->cookie,
6264				  priv->cookie_dma);
6265
6266	return rc;
6267}
6268static int mwl8k_probe(struct pci_dev *pdev,
6269				 const struct pci_device_id *id)
6270{
6271	static int printed_version;
6272	struct ieee80211_hw *hw;
6273	struct mwl8k_priv *priv;
6274	struct mwl8k_device_info *di;
6275	int rc;
6276
6277	if (!printed_version) {
6278		printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
6279		printed_version = 1;
6280	}
6281
6282
6283	rc = pci_enable_device(pdev);
6284	if (rc) {
6285		printk(KERN_ERR "%s: Cannot enable new PCI device\n",
6286		       MWL8K_NAME);
6287		return rc;
6288	}
6289
6290	rc = pci_request_regions(pdev, MWL8K_NAME);
6291	if (rc) {
6292		printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
6293		       MWL8K_NAME);
6294		goto err_disable_device;
6295	}
6296
6297	pci_set_master(pdev);
6298
6299
6300	hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
6301	if (hw == NULL) {
6302		printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
6303		rc = -ENOMEM;
6304		goto err_free_reg;
6305	}
6306
6307	SET_IEEE80211_DEV(hw, &pdev->dev);
6308	pci_set_drvdata(pdev, hw);
6309
6310	priv = hw->priv;
6311	priv->hw = hw;
6312	priv->pdev = pdev;
6313	priv->device_info = &mwl8k_info_tbl[id->driver_data];
6314
6315	if (id->driver_data == MWL8764)
6316		priv->is_8764 = true;
6317
6318	priv->sram = pci_iomap(pdev, 0, 0x10000);
6319	if (priv->sram == NULL) {
6320		wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
6321		rc = -EIO;
6322		goto err_iounmap;
6323	}
6324
6325	/*
6326	 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
6327	 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
6328	 */
6329	priv->regs = pci_iomap(pdev, 1, 0x10000);
6330	if (priv->regs == NULL) {
6331		priv->regs = pci_iomap(pdev, 2, 0x10000);
6332		if (priv->regs == NULL) {
6333			wiphy_err(hw->wiphy, "Cannot map device registers\n");
6334			rc = -EIO;
6335			goto err_iounmap;
6336		}
6337	}
6338
6339	/*
6340	 * Choose the initial fw image depending on user input.  If a second
6341	 * image is available, make it the alternative image that will be
6342	 * loaded if the first one fails.
6343	 */
6344	init_completion(&priv->firmware_loading_complete);
6345	di = priv->device_info;
6346	if (ap_mode_default && di->fw_image_ap) {
6347		priv->fw_pref = di->fw_image_ap;
6348		priv->fw_alt = di->fw_image_sta;
6349	} else if (!ap_mode_default && di->fw_image_sta) {
6350		priv->fw_pref = di->fw_image_sta;
6351		priv->fw_alt = di->fw_image_ap;
6352	} else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
6353		printk(KERN_WARNING "AP fw is unavailable.  Using STA fw.");
6354		priv->fw_pref = di->fw_image_sta;
6355	} else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
6356		printk(KERN_WARNING "STA fw is unavailable.  Using AP fw.");
6357		priv->fw_pref = di->fw_image_ap;
6358	}
6359	rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
6360	if (rc)
6361		goto err_stop_firmware;
6362
6363	priv->hw_restart_in_progress = false;
6364
6365	priv->running_bsses = 0;
6366
6367	return rc;
6368
6369err_stop_firmware:
6370	mwl8k_hw_reset(priv);
6371
6372err_iounmap:
6373	if (priv->regs != NULL)
6374		pci_iounmap(pdev, priv->regs);
6375
6376	if (priv->sram != NULL)
6377		pci_iounmap(pdev, priv->sram);
6378
6379	ieee80211_free_hw(hw);
6380
6381err_free_reg:
6382	pci_release_regions(pdev);
6383
6384err_disable_device:
6385	pci_disable_device(pdev);
6386
6387	return rc;
6388}
6389
6390static void mwl8k_remove(struct pci_dev *pdev)
6391{
6392	struct ieee80211_hw *hw = pci_get_drvdata(pdev);
6393	struct mwl8k_priv *priv;
6394	int i;
6395
6396	if (hw == NULL)
6397		return;
6398	priv = hw->priv;
6399
6400	wait_for_completion(&priv->firmware_loading_complete);
6401
6402	if (priv->fw_state == FW_STATE_ERROR) {
6403		mwl8k_hw_reset(priv);
6404		goto unmap;
6405	}
6406
6407	ieee80211_stop_queues(hw);
6408
6409	ieee80211_unregister_hw(hw);
6410
6411	/* Remove TX reclaim and RX tasklets.  */
6412	tasklet_kill(&priv->poll_tx_task);
6413	tasklet_kill(&priv->poll_rx_task);
6414
6415	/* Stop hardware */
6416	mwl8k_hw_reset(priv);
6417
6418	/* Return all skbs to mac80211 */
6419	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6420		mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
6421
6422	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6423		mwl8k_txq_deinit(hw, i);
6424
6425	mwl8k_rxq_deinit(hw, 0);
6426
6427	dma_free_coherent(&priv->pdev->dev, 4, priv->cookie, priv->cookie_dma);
6428
6429unmap:
6430	pci_iounmap(pdev, priv->regs);
6431	pci_iounmap(pdev, priv->sram);
6432	ieee80211_free_hw(hw);
6433	pci_release_regions(pdev);
6434	pci_disable_device(pdev);
6435}
6436
6437static struct pci_driver mwl8k_driver = {
6438	.name		= MWL8K_NAME,
6439	.id_table	= mwl8k_pci_id_table,
6440	.probe		= mwl8k_probe,
6441	.remove		= mwl8k_remove,
6442};
6443
6444module_pci_driver(mwl8k_driver);
6445
6446MODULE_DESCRIPTION(MWL8K_DESC);
6447MODULE_VERSION(MWL8K_VERSION);
6448MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
6449MODULE_LICENSE("GPL");
~/ Read-only file preview.