blob: ac65e13eb0de8ea31fed54c02dd42add8528b85f [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka5fb2972010-01-12 13:51:38 +01005 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
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/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020015#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010016#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 <net/mac80211.h>
23#include <linux/moduleparam.h>
24#include <linux/firmware.h>
25#include <linux/workqueue.h>
26
27#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
28#define MWL8K_NAME KBUILD_MODNAME
Lennert Buytenheka5fb2972010-01-12 13:51:38 +010029#define MWL8K_VERSION "0.12"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010030
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031/* Register definitions */
32#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020033#define MWL8K_MODE_STA 0x0000005a
34#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010035#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020036#define MWL8K_FWSTA_READY 0xf0f1f2f4
37#define MWL8K_FWAP_READY 0xf1f2f4a5
38#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010039#define MWL8K_HIU_SCRATCH 0x00000c40
40
41/* Host->device communications */
42#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
43#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
44#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
45#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
46#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020047#define MWL8K_H2A_INT_DUMMY (1 << 20)
48#define MWL8K_H2A_INT_RESET (1 << 15)
49#define MWL8K_H2A_INT_DOORBELL (1 << 1)
50#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010051
52/* Device->host communications */
53#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
54#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
55#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
56#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
57#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020058#define MWL8K_A2H_INT_DUMMY (1 << 20)
59#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
60#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
61#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
62#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
63#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
64#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
65#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
66#define MWL8K_A2H_INT_RX_READY (1 << 1)
67#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010068
69#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
70 MWL8K_A2H_INT_CHNL_SWITCHED | \
71 MWL8K_A2H_INT_QUEUE_EMPTY | \
72 MWL8K_A2H_INT_RADAR_DETECT | \
73 MWL8K_A2H_INT_RADIO_ON | \
74 MWL8K_A2H_INT_RADIO_OFF | \
75 MWL8K_A2H_INT_MAC_EVENT | \
76 MWL8K_A2H_INT_OPC_DONE | \
77 MWL8K_A2H_INT_RX_READY | \
78 MWL8K_A2H_INT_TX_DONE)
79
Lennert Buytenheka66098d2009-03-10 10:13:33 +010080#define MWL8K_RX_QUEUES 1
81#define MWL8K_TX_QUEUES 4
82
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020083struct rxd_ops {
84 int rxd_size;
85 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
86 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010087 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
88 __le16 *qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020089};
90
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020091struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020092 char *part_name;
93 char *helper_image;
94 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010095 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020096};
97
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +020099 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100100
101 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200102 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
104 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200105 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100106
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200107 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200109 struct {
110 struct sk_buff *skb;
111 DECLARE_PCI_UNMAP_ADDR(dma)
112 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100113};
114
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115struct mwl8k_tx_queue {
116 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100118
119 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200120 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100121
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200122 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200123 struct mwl8k_tx_desc *txd;
124 dma_addr_t txd_dma;
125 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100126};
127
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100128struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200132 struct mwl8k_device_info *device_info;
133
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100134 void __iomem *sram;
135 void __iomem *regs;
136
137 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100138 struct firmware *fw_helper;
139 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100141 /* hardware/firmware parameters */
142 bool ap_fw;
143 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100144 struct ieee80211_supported_band band_24;
145 struct ieee80211_channel channels_24[14];
146 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100147 struct ieee80211_supported_band band_50;
148 struct ieee80211_channel channels_50[4];
149 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100150 u32 ap_macids_supported;
151 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100152
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200153 /* firmware access */
154 struct mutex fw_mutex;
155 struct task_struct *fw_mutex_owner;
156 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200157 struct completion *hostcmd_wait;
158
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100159 /* lock held over TX and TX reap */
160 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100161
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200162 /* TX quiesce completion, protected by fw_mutex and tx_lock */
163 struct completion *tx_wait;
164
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100165 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100166 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100167 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100169 /* power management status cookie from firmware */
170 u32 *cookie;
171 dma_addr_t cookie_dma;
172
173 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100174 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200175 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100176
177 /*
178 * Running count of TX packets in flight, to avoid
179 * iterating over the transmit rings each time.
180 */
181 int pending_tx_pkts;
182
183 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
184 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
185
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200186 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200187 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200188 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200189 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100190
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100191 /* XXX need to convert this to handle multiple interfaces */
192 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200193 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100194 struct sk_buff *beacon_skb;
195
196 /*
197 * This FJ worker has to be global as it is scheduled from the
198 * RX handler. At this point we don't know which interface it
199 * belongs to until the list of bssids waiting to complete join
200 * is checked.
201 */
202 struct work_struct finalize_join_worker;
203
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100204 /* Tasklet to perform TX reclaim. */
205 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100206
207 /* Tasklet to perform RX. */
208 struct tasklet_struct poll_rx_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100209};
210
211/* Per interface specific private data */
212struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100213 struct list_head list;
214 struct ieee80211_vif *vif;
215
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100216 /* Firmware macid for this vif. */
217 int macid;
218
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100219 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100220 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100221};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200222#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100223
Lennert Buytenheka6804002010-01-04 21:55:42 +0100224struct mwl8k_sta {
225 /* Index into station database. Returned by UPDATE_STADB. */
226 u8 peer_id;
227};
228#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
229
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100230static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100231 { .center_freq = 2412, .hw_value = 1, },
232 { .center_freq = 2417, .hw_value = 2, },
233 { .center_freq = 2422, .hw_value = 3, },
234 { .center_freq = 2427, .hw_value = 4, },
235 { .center_freq = 2432, .hw_value = 5, },
236 { .center_freq = 2437, .hw_value = 6, },
237 { .center_freq = 2442, .hw_value = 7, },
238 { .center_freq = 2447, .hw_value = 8, },
239 { .center_freq = 2452, .hw_value = 9, },
240 { .center_freq = 2457, .hw_value = 10, },
241 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100242 { .center_freq = 2467, .hw_value = 12, },
243 { .center_freq = 2472, .hw_value = 13, },
244 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100245};
246
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100247static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100248 { .bitrate = 10, .hw_value = 2, },
249 { .bitrate = 20, .hw_value = 4, },
250 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200251 { .bitrate = 110, .hw_value = 22, },
252 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100253 { .bitrate = 60, .hw_value = 12, },
254 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100255 { .bitrate = 120, .hw_value = 24, },
256 { .bitrate = 180, .hw_value = 36, },
257 { .bitrate = 240, .hw_value = 48, },
258 { .bitrate = 360, .hw_value = 72, },
259 { .bitrate = 480, .hw_value = 96, },
260 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100261 { .bitrate = 720, .hw_value = 144, },
262};
263
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100264static const struct ieee80211_channel mwl8k_channels_50[] = {
265 { .center_freq = 5180, .hw_value = 36, },
266 { .center_freq = 5200, .hw_value = 40, },
267 { .center_freq = 5220, .hw_value = 44, },
268 { .center_freq = 5240, .hw_value = 48, },
269};
270
271static const struct ieee80211_rate mwl8k_rates_50[] = {
272 { .bitrate = 60, .hw_value = 12, },
273 { .bitrate = 90, .hw_value = 18, },
274 { .bitrate = 120, .hw_value = 24, },
275 { .bitrate = 180, .hw_value = 36, },
276 { .bitrate = 240, .hw_value = 48, },
277 { .bitrate = 360, .hw_value = 72, },
278 { .bitrate = 480, .hw_value = 96, },
279 { .bitrate = 540, .hw_value = 108, },
280 { .bitrate = 720, .hw_value = 144, },
281};
282
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100283/* Set or get info from Firmware */
284#define MWL8K_CMD_SET 0x0001
285#define MWL8K_CMD_GET 0x0000
286
287/* Firmware command codes */
288#define MWL8K_CMD_CODE_DNLD 0x0001
289#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200290#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100291#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
292#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200293#define MWL8K_CMD_RADIO_CONTROL 0x001c
294#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200295#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100296#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100297#define MWL8K_CMD_SET_PRE_SCAN 0x0107
298#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200299#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100300#define MWL8K_CMD_SET_AID 0x010d
301#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200302#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100303#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200304#define MWL8K_CMD_SET_SLOT 0x0114
305#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
306#define MWL8K_CMD_SET_WMM_MODE 0x0123
307#define MWL8K_CMD_MIMO_CONFIG 0x0125
308#define MWL8K_CMD_USE_FIXED_RATE 0x0126
309#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100310#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200311#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100312#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
313#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200314#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100315
316static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
317{
318#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
319 snprintf(buf, bufsize, "%s", #x);\
320 return buf;\
321 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200322 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100323 MWL8K_CMDNAME(CODE_DNLD);
324 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200325 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100326 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
327 MWL8K_CMDNAME(GET_STAT);
328 MWL8K_CMDNAME(RADIO_CONTROL);
329 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200330 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100331 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100332 MWL8K_CMDNAME(SET_PRE_SCAN);
333 MWL8K_CMDNAME(SET_POST_SCAN);
334 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100335 MWL8K_CMDNAME(SET_AID);
336 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200337 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100338 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200339 MWL8K_CMDNAME(SET_SLOT);
340 MWL8K_CMDNAME(SET_EDCA_PARAMS);
341 MWL8K_CMDNAME(SET_WMM_MODE);
342 MWL8K_CMDNAME(MIMO_CONFIG);
343 MWL8K_CMDNAME(USE_FIXED_RATE);
344 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200345 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200346 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100347 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100348 MWL8K_CMDNAME(SET_NEW_STN);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200349 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100350 default:
351 snprintf(buf, bufsize, "0x%x", cmd);
352 }
353#undef MWL8K_CMDNAME
354
355 return buf;
356}
357
358/* Hardware and firmware reset */
359static void mwl8k_hw_reset(struct mwl8k_priv *priv)
360{
361 iowrite32(MWL8K_H2A_INT_RESET,
362 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
363 iowrite32(MWL8K_H2A_INT_RESET,
364 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
365 msleep(20);
366}
367
368/* Release fw image */
369static void mwl8k_release_fw(struct firmware **fw)
370{
371 if (*fw == NULL)
372 return;
373 release_firmware(*fw);
374 *fw = NULL;
375}
376
377static void mwl8k_release_firmware(struct mwl8k_priv *priv)
378{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100379 mwl8k_release_fw(&priv->fw_ucode);
380 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100381}
382
383/* Request fw image */
384static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200385 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100386{
387 /* release current image */
388 if (*fw != NULL)
389 mwl8k_release_fw(fw);
390
391 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200392 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100393}
394
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200395static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100396{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200397 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100398 int rc;
399
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200400 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100401 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200402 if (rc) {
403 printk(KERN_ERR "%s: Error requesting helper "
404 "firmware file %s\n", pci_name(priv->pdev),
405 di->helper_image);
406 return rc;
407 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100408 }
409
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100410 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100411 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200412 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200413 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100414 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100415 return rc;
416 }
417
418 return 0;
419}
420
421struct mwl8k_cmd_pkt {
422 __le16 code;
423 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100424 __u8 seq_num;
425 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100426 __le16 result;
427 char payload[0];
428} __attribute__((packed));
429
430/*
431 * Firmware loading.
432 */
433static int
434mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
435{
436 void __iomem *regs = priv->regs;
437 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100438 int loops;
439
440 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
441 if (pci_dma_mapping_error(priv->pdev, dma_addr))
442 return -ENOMEM;
443
444 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
445 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
446 iowrite32(MWL8K_H2A_INT_DOORBELL,
447 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
448 iowrite32(MWL8K_H2A_INT_DUMMY,
449 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
450
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100451 loops = 1000;
452 do {
453 u32 int_code;
454
455 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
456 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
457 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100458 break;
459 }
460
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200461 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100462 udelay(1);
463 } while (--loops);
464
465 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
466
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200467 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100468}
469
470static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
471 const u8 *data, size_t length)
472{
473 struct mwl8k_cmd_pkt *cmd;
474 int done;
475 int rc = 0;
476
477 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
478 if (cmd == NULL)
479 return -ENOMEM;
480
481 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
482 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100483 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100484 cmd->result = 0;
485
486 done = 0;
487 while (length) {
488 int block_size = length > 256 ? 256 : length;
489
490 memcpy(cmd->payload, data + done, block_size);
491 cmd->length = cpu_to_le16(block_size);
492
493 rc = mwl8k_send_fw_load_cmd(priv, cmd,
494 sizeof(*cmd) + block_size);
495 if (rc)
496 break;
497
498 done += block_size;
499 length -= block_size;
500 }
501
502 if (!rc) {
503 cmd->length = 0;
504 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
505 }
506
507 kfree(cmd);
508
509 return rc;
510}
511
512static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
513 const u8 *data, size_t length)
514{
515 unsigned char *buffer;
516 int may_continue, rc = 0;
517 u32 done, prev_block_size;
518
519 buffer = kmalloc(1024, GFP_KERNEL);
520 if (buffer == NULL)
521 return -ENOMEM;
522
523 done = 0;
524 prev_block_size = 0;
525 may_continue = 1000;
526 while (may_continue > 0) {
527 u32 block_size;
528
529 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
530 if (block_size & 1) {
531 block_size &= ~1;
532 may_continue--;
533 } else {
534 done += prev_block_size;
535 length -= prev_block_size;
536 }
537
538 if (block_size > 1024 || block_size > length) {
539 rc = -EOVERFLOW;
540 break;
541 }
542
543 if (length == 0) {
544 rc = 0;
545 break;
546 }
547
548 if (block_size == 0) {
549 rc = -EPROTO;
550 may_continue--;
551 udelay(1);
552 continue;
553 }
554
555 prev_block_size = block_size;
556 memcpy(buffer, data + done, block_size);
557
558 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
559 if (rc)
560 break;
561 }
562
563 if (!rc && length != 0)
564 rc = -EREMOTEIO;
565
566 kfree(buffer);
567
568 return rc;
569}
570
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200571static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100572{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200573 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100574 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200575 int rc;
576 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100577
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200578 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100579 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100580
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200581 if (helper == NULL) {
582 printk(KERN_ERR "%s: helper image needed but none "
583 "given\n", pci_name(priv->pdev));
584 return -EINVAL;
585 }
586
587 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100588 if (rc) {
589 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200590 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100591 return rc;
592 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100593 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100594
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200595 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100596 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200597 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100598 }
599
600 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200601 printk(KERN_ERR "%s: unable to load firmware image\n",
602 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100603 return rc;
604 }
605
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100606 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100607
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100608 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100609 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200610 u32 ready_code;
611
612 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
613 if (ready_code == MWL8K_FWAP_READY) {
614 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100615 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200616 } else if (ready_code == MWL8K_FWSTA_READY) {
617 priv->ap_fw = 0;
618 break;
619 }
620
621 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100622 udelay(1);
623 } while (--loops);
624
625 return loops ? 0 : -ETIMEDOUT;
626}
627
628
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100629/* DMA header used by firmware and hardware. */
630struct mwl8k_dma_data {
631 __le16 fwlen;
632 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100633 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100634} __attribute__((packed));
635
636/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100637static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100638{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100639 struct mwl8k_dma_data *tr;
640 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100641
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100642 tr = (struct mwl8k_dma_data *)skb->data;
643 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
644
645 if (hdrlen != sizeof(tr->wh)) {
646 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
647 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
648 *((__le16 *)(tr->data - 2)) = qos;
649 } else {
650 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
651 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100652 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100653
654 if (hdrlen != sizeof(*tr))
655 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100656}
657
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200658static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100659{
660 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100661 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100662 struct mwl8k_dma_data *tr;
663
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100664 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100665 * Add a firmware DMA header; the firmware requires that we
666 * present a 2-byte payload length followed by a 4-address
667 * header (without QoS field), followed (optionally) by any
668 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100669 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100670 wh = (struct ieee80211_hdr *)skb->data;
671
672 hdrlen = ieee80211_hdrlen(wh->frame_control);
673 if (hdrlen != sizeof(*tr))
674 skb_push(skb, sizeof(*tr) - hdrlen);
675
676 if (ieee80211_is_data_qos(wh->frame_control))
677 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100678
679 tr = (struct mwl8k_dma_data *)skb->data;
680 if (wh != &tr->wh)
681 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100682 if (hdrlen != sizeof(tr->wh))
683 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100684
685 /*
686 * Firmware length is the length of the fully formed "802.11
687 * payload". That is, everything except for the 802.11 header.
688 * This includes all crypto material including the MIC.
689 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100690 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100691}
692
693
694/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100695 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200696 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100697struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200698 __le16 pkt_len;
699 __u8 sq2;
700 __u8 rate;
701 __le32 pkt_phys_addr;
702 __le32 next_rxd_phys_addr;
703 __le16 qos_control;
704 __le16 htsig2;
705 __le32 hw_rssi_info;
706 __le32 hw_noise_floor_info;
707 __u8 noise_floor;
708 __u8 pad0[3];
709 __u8 rssi;
710 __u8 rx_status;
711 __u8 channel;
712 __u8 rx_ctrl;
713} __attribute__((packed));
714
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100715#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
716#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
717#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100718
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100719#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200720
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100721static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200722{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100723 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200724
725 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100726 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200727}
728
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100729static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200730{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100731 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200732
733 rxd->pkt_len = cpu_to_le16(len);
734 rxd->pkt_phys_addr = cpu_to_le32(addr);
735 wmb();
736 rxd->rx_ctrl = 0;
737}
738
739static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100740mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
741 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200742{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100743 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200744
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100745 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200746 return -1;
747 rmb();
748
749 memset(status, 0, sizeof(*status));
750
751 status->signal = -rxd->rssi;
752 status->noise = -rxd->noise_floor;
753
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100754 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200755 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100756 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100757 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100758 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200759 } else {
760 int i;
761
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100762 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
763 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200764 status->rate_idx = i;
765 break;
766 }
767 }
768 }
769
Lennert Buytenhek85478342010-01-12 13:48:56 +0100770 if (rxd->channel > 14) {
771 status->band = IEEE80211_BAND_5GHZ;
772 if (!(status->flag & RX_FLAG_HT))
773 status->rate_idx -= 5;
774 } else {
775 status->band = IEEE80211_BAND_2GHZ;
776 }
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200777 status->freq = ieee80211_channel_to_frequency(rxd->channel);
778
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100779 *qos = rxd->qos_control;
780
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200781 return le16_to_cpu(rxd->pkt_len);
782}
783
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100784static struct rxd_ops rxd_8366_ap_ops = {
785 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
786 .rxd_init = mwl8k_rxd_8366_ap_init,
787 .rxd_refill = mwl8k_rxd_8366_ap_refill,
788 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200789};
790
791/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100792 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100793 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100794struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100795 __le16 pkt_len;
796 __u8 link_quality;
797 __u8 noise_level;
798 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200799 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100800 __le16 qos_control;
801 __le16 rate_info;
802 __le32 pad0[4];
803 __u8 rssi;
804 __u8 channel;
805 __le16 pad1;
806 __u8 rx_ctrl;
807 __u8 rx_status;
808 __u8 pad2[2];
809} __attribute__((packed));
810
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100811#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
812#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
813#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
814#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
815#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
816#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200817
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100818#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200819
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100820static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200821{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100822 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200823
824 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100825 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200826}
827
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100828static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200829{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100830 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200831
832 rxd->pkt_len = cpu_to_le16(len);
833 rxd->pkt_phys_addr = cpu_to_le32(addr);
834 wmb();
835 rxd->rx_ctrl = 0;
836}
837
838static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100839mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100840 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200841{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100842 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200843 u16 rate_info;
844
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100845 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200846 return -1;
847 rmb();
848
849 rate_info = le16_to_cpu(rxd->rate_info);
850
851 memset(status, 0, sizeof(*status));
852
853 status->signal = -rxd->rssi;
854 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100855 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
856 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200857
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100858 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200859 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100860 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200861 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100862 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200863 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100864 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200865 status->flag |= RX_FLAG_HT;
866
Lennert Buytenhek85478342010-01-12 13:48:56 +0100867 if (rxd->channel > 14) {
868 status->band = IEEE80211_BAND_5GHZ;
869 if (!(status->flag & RX_FLAG_HT))
870 status->rate_idx -= 5;
871 } else {
872 status->band = IEEE80211_BAND_2GHZ;
873 }
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200874 status->freq = ieee80211_channel_to_frequency(rxd->channel);
875
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100876 *qos = rxd->qos_control;
877
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200878 return le16_to_cpu(rxd->pkt_len);
879}
880
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100881static struct rxd_ops rxd_sta_ops = {
882 .rxd_size = sizeof(struct mwl8k_rxd_sta),
883 .rxd_init = mwl8k_rxd_sta_init,
884 .rxd_refill = mwl8k_rxd_sta_refill,
885 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200886};
887
888
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100889#define MWL8K_RX_DESCS 256
890#define MWL8K_RX_MAXSZ 3800
891
892static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
893{
894 struct mwl8k_priv *priv = hw->priv;
895 struct mwl8k_rx_queue *rxq = priv->rxq + index;
896 int size;
897 int i;
898
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200899 rxq->rxd_count = 0;
900 rxq->head = 0;
901 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100902
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200903 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100904
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200905 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
906 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100907 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200908 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100909 return -ENOMEM;
910 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200911 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100912
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200913 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
914 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100915 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200916 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200917 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100918 return -ENOMEM;
919 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200920 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100921
922 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200923 int desc_size;
924 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100925 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200926 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100927
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200928 desc_size = priv->rxd_ops->rxd_size;
929 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100930
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200931 nexti = i + 1;
932 if (nexti == MWL8K_RX_DESCS)
933 nexti = 0;
934 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
935
936 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100937 }
938
939 return 0;
940}
941
942static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
943{
944 struct mwl8k_priv *priv = hw->priv;
945 struct mwl8k_rx_queue *rxq = priv->rxq + index;
946 int refilled;
947
948 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200949 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100950 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200951 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100952 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200953 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100954
955 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
956 if (skb == NULL)
957 break;
958
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200959 addr = pci_map_single(priv->pdev, skb->data,
960 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100961
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200962 rxq->rxd_count++;
963 rx = rxq->tail++;
964 if (rxq->tail == MWL8K_RX_DESCS)
965 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200966 rxq->buf[rx].skb = skb;
967 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200968
969 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
970 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100971
972 refilled++;
973 }
974
975 return refilled;
976}
977
978/* Must be called only when the card's reception is completely halted */
979static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
980{
981 struct mwl8k_priv *priv = hw->priv;
982 struct mwl8k_rx_queue *rxq = priv->rxq + index;
983 int i;
984
985 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200986 if (rxq->buf[i].skb != NULL) {
987 pci_unmap_single(priv->pdev,
988 pci_unmap_addr(&rxq->buf[i], dma),
989 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
990 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100991
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200992 kfree_skb(rxq->buf[i].skb);
993 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100994 }
995 }
996
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200997 kfree(rxq->buf);
998 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100999
1000 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001001 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001002 rxq->rxd, rxq->rxd_dma);
1003 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001004}
1005
1006
1007/*
1008 * Scan a list of BSSIDs to process for finalize join.
1009 * Allows for extension to process multiple BSSIDs.
1010 */
1011static inline int
1012mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1013{
1014 return priv->capture_beacon &&
1015 ieee80211_is_beacon(wh->frame_control) &&
1016 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1017}
1018
Lennert Buytenhek37797522009-10-22 20:19:45 +02001019static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1020 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001021{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001022 struct mwl8k_priv *priv = hw->priv;
1023
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001024 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001025 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001026
1027 /*
1028 * Use GFP_ATOMIC as rxq_process is called from
1029 * the primary interrupt handler, memory allocation call
1030 * must not sleep.
1031 */
1032 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1033 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001034 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001035}
1036
1037static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1038{
1039 struct mwl8k_priv *priv = hw->priv;
1040 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1041 int processed;
1042
1043 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001044 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001045 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001046 void *rxd;
1047 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001048 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001049 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001050
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001051 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001052 if (skb == NULL)
1053 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001054
1055 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1056
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001057 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001058 if (pkt_len < 0)
1059 break;
1060
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001061 rxq->buf[rxq->head].skb = NULL;
1062
1063 pci_unmap_single(priv->pdev,
1064 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1065 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1066 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001067
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001068 rxq->head++;
1069 if (rxq->head == MWL8K_RX_DESCS)
1070 rxq->head = 0;
1071
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001072 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001073
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001074 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001075 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001076
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001077 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001078 * Check for a pending join operation. Save a
1079 * copy of the beacon and schedule a tasklet to
1080 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001081 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001082 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001083 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001084
Johannes Bergf1d58c22009-06-17 13:13:00 +02001085 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1086 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001087
1088 processed++;
1089 }
1090
1091 return processed;
1092}
1093
1094
1095/*
1096 * Packet transmission.
1097 */
1098
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001099#define MWL8K_TXD_STATUS_OK 0x00000001
1100#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1101#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1102#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001103#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001104
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001105#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1106#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1107#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1108#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1109#define MWL8K_QOS_EOSP 0x0010
1110
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001111struct mwl8k_tx_desc {
1112 __le32 status;
1113 __u8 data_rate;
1114 __u8 tx_priority;
1115 __le16 qos_control;
1116 __le32 pkt_phys_addr;
1117 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001118 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001119 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001120 __le32 reserved;
1121 __le16 rate_info;
1122 __u8 peer_id;
1123 __u8 tx_frag_cnt;
1124} __attribute__((packed));
1125
1126#define MWL8K_TX_DESCS 128
1127
1128static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1129{
1130 struct mwl8k_priv *priv = hw->priv;
1131 struct mwl8k_tx_queue *txq = priv->txq + index;
1132 int size;
1133 int i;
1134
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001135 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001136 txq->head = 0;
1137 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001138
1139 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1140
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001141 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1142 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001143 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001144 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001145 return -ENOMEM;
1146 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001147 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001148
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001149 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1150 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001151 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001152 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001153 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001154 return -ENOMEM;
1155 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001156 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001157
1158 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1159 struct mwl8k_tx_desc *tx_desc;
1160 int nexti;
1161
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001162 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163 nexti = (i + 1) % MWL8K_TX_DESCS;
1164
1165 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001166 tx_desc->next_txd_phys_addr =
1167 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001168 }
1169
1170 return 0;
1171}
1172
1173static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1174{
1175 iowrite32(MWL8K_H2A_INT_PPA_READY,
1176 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1177 iowrite32(MWL8K_H2A_INT_DUMMY,
1178 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1179 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1180}
1181
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001182static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001183{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001184 struct mwl8k_priv *priv = hw->priv;
1185 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001186
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001187 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1188 struct mwl8k_tx_queue *txq = priv->txq + i;
1189 int fw_owned = 0;
1190 int drv_owned = 0;
1191 int unused = 0;
1192 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001193
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001194 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001195 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1196 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001197
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001198 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001199 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001200 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001201 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001202 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001203
1204 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001205 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001206 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001207
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001208 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1209 "fw_owned=%d drv_owned=%d unused=%d\n",
1210 wiphy_name(hw->wiphy), i,
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001211 txq->len, txq->head, txq->tail,
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001212 fw_owned, drv_owned, unused);
1213 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001214}
1215
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001216/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001217 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001218 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001219#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001220
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001221static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001222{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001223 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001224 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001225 int retry;
1226 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001227
1228 might_sleep();
1229
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001230 /*
1231 * The TX queues are stopped at this point, so this test
1232 * doesn't need to take ->tx_lock.
1233 */
1234 if (!priv->pending_tx_pkts)
1235 return 0;
1236
1237 retry = 0;
1238 rc = 0;
1239
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001240 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001241 priv->tx_wait = &tx_wait;
1242 while (!rc) {
1243 int oldcount;
1244 unsigned long timeout;
1245
1246 oldcount = priv->pending_tx_pkts;
1247
1248 spin_unlock_bh(&priv->tx_lock);
1249 timeout = wait_for_completion_timeout(&tx_wait,
1250 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1251 spin_lock_bh(&priv->tx_lock);
1252
1253 if (timeout) {
1254 WARN_ON(priv->pending_tx_pkts);
1255 if (retry) {
1256 printk(KERN_NOTICE "%s: tx rings drained\n",
1257 wiphy_name(hw->wiphy));
1258 }
1259 break;
1260 }
1261
1262 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001263 printk(KERN_NOTICE "%s: waiting for tx rings "
1264 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001265 wiphy_name(hw->wiphy), oldcount,
1266 priv->pending_tx_pkts);
1267 retry = 1;
1268 continue;
1269 }
1270
1271 priv->tx_wait = NULL;
1272
1273 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1274 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1275 mwl8k_dump_tx_rings(hw);
1276
1277 rc = -ETIMEDOUT;
1278 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001279 spin_unlock_bh(&priv->tx_lock);
1280
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001281 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001282}
1283
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001284#define MWL8K_TXD_SUCCESS(status) \
1285 ((status) & (MWL8K_TXD_STATUS_OK | \
1286 MWL8K_TXD_STATUS_OK_RETRY | \
1287 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001288
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001289static int
1290mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001291{
1292 struct mwl8k_priv *priv = hw->priv;
1293 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001294 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001295
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001296 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001297 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001298 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299 struct mwl8k_tx_desc *tx_desc;
1300 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001301 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001302 struct sk_buff *skb;
1303 struct ieee80211_tx_info *info;
1304 u32 status;
1305
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001306 tx = txq->head;
1307 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001308
1309 status = le32_to_cpu(tx_desc->status);
1310
1311 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1312 if (!force)
1313 break;
1314 tx_desc->status &=
1315 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1316 }
1317
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001318 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001319 BUG_ON(txq->len == 0);
1320 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001321 priv->pending_tx_pkts--;
1322
1323 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001324 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001325 skb = txq->skb[tx];
1326 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001327
1328 BUG_ON(skb == NULL);
1329 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1330
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001331 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001332
1333 /* Mark descriptor as unused */
1334 tx_desc->pkt_phys_addr = 0;
1335 tx_desc->pkt_len = 0;
1336
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001337 info = IEEE80211_SKB_CB(skb);
1338 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001339 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341
1342 ieee80211_tx_status_irqsafe(hw, skb);
1343
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001344 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345 }
1346
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001347 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001348 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001349
1350 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001351}
1352
1353/* must be called only when the card's transmit is completely halted */
1354static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1355{
1356 struct mwl8k_priv *priv = hw->priv;
1357 struct mwl8k_tx_queue *txq = priv->txq + index;
1358
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001359 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001360
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001361 kfree(txq->skb);
1362 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001363
1364 pci_free_consistent(priv->pdev,
1365 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001366 txq->txd, txq->txd_dma);
1367 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001368}
1369
1370static int
1371mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1372{
1373 struct mwl8k_priv *priv = hw->priv;
1374 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001375 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001376 struct ieee80211_hdr *wh;
1377 struct mwl8k_tx_queue *txq;
1378 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001379 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001380 u32 txstatus;
1381 u8 txdatarate;
1382 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001383
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001384 wh = (struct ieee80211_hdr *)skb->data;
1385 if (ieee80211_is_data_qos(wh->frame_control))
1386 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1387 else
1388 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001389
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001390 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001391 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001392
1393 tx_info = IEEE80211_SKB_CB(skb);
1394 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001395
1396 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001397 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001398 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1399 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001400 }
1401
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001402 /* Setup firmware control bit fields for each frame type. */
1403 txstatus = 0;
1404 txdatarate = 0;
1405 if (ieee80211_is_mgmt(wh->frame_control) ||
1406 ieee80211_is_ctl(wh->frame_control)) {
1407 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001408 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001409 } else if (ieee80211_is_data(wh->frame_control)) {
1410 txdatarate = 1;
1411 if (is_multicast_ether_addr(wh->addr1))
1412 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1413
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001414 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001415 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001416 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001417 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001418 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001419 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001420
1421 dma = pci_map_single(priv->pdev, skb->data,
1422 skb->len, PCI_DMA_TODEVICE);
1423
1424 if (pci_dma_mapping_error(priv->pdev, dma)) {
1425 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001426 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001427 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001428 return NETDEV_TX_OK;
1429 }
1430
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001431 spin_lock_bh(&priv->tx_lock);
1432
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001433 txq = priv->txq + index;
1434
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001435 BUG_ON(txq->skb[txq->tail] != NULL);
1436 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001437
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001438 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001439 tx->data_rate = txdatarate;
1440 tx->tx_priority = index;
1441 tx->qos_control = cpu_to_le16(qos);
1442 tx->pkt_phys_addr = cpu_to_le32(dma);
1443 tx->pkt_len = cpu_to_le16(skb->len);
1444 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001445 if (!priv->ap_fw && tx_info->control.sta != NULL)
1446 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1447 else
1448 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001449 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001450 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1451
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001452 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001453 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001454
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001455 txq->tail++;
1456 if (txq->tail == MWL8K_TX_DESCS)
1457 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001458
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001459 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001460 ieee80211_stop_queue(hw, index);
1461
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001462 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001463
1464 spin_unlock_bh(&priv->tx_lock);
1465
1466 return NETDEV_TX_OK;
1467}
1468
1469
1470/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001471 * Firmware access.
1472 *
1473 * We have the following requirements for issuing firmware commands:
1474 * - Some commands require that the packet transmit path is idle when
1475 * the command is issued. (For simplicity, we'll just quiesce the
1476 * transmit path for every command.)
1477 * - There are certain sequences of commands that need to be issued to
1478 * the hardware sequentially, with no other intervening commands.
1479 *
1480 * This leads to an implementation of a "firmware lock" as a mutex that
1481 * can be taken recursively, and which is taken by both the low-level
1482 * command submission function (mwl8k_post_cmd) as well as any users of
1483 * that function that require issuing of an atomic sequence of commands,
1484 * and quiesces the transmit path whenever it's taken.
1485 */
1486static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1487{
1488 struct mwl8k_priv *priv = hw->priv;
1489
1490 if (priv->fw_mutex_owner != current) {
1491 int rc;
1492
1493 mutex_lock(&priv->fw_mutex);
1494 ieee80211_stop_queues(hw);
1495
1496 rc = mwl8k_tx_wait_empty(hw);
1497 if (rc) {
1498 ieee80211_wake_queues(hw);
1499 mutex_unlock(&priv->fw_mutex);
1500
1501 return rc;
1502 }
1503
1504 priv->fw_mutex_owner = current;
1505 }
1506
1507 priv->fw_mutex_depth++;
1508
1509 return 0;
1510}
1511
1512static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1513{
1514 struct mwl8k_priv *priv = hw->priv;
1515
1516 if (!--priv->fw_mutex_depth) {
1517 ieee80211_wake_queues(hw);
1518 priv->fw_mutex_owner = NULL;
1519 mutex_unlock(&priv->fw_mutex);
1520 }
1521}
1522
1523
1524/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001525 * Command processing.
1526 */
1527
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001528/* Timeout firmware commands after 10s */
1529#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001530
1531static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1532{
1533 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1534 struct mwl8k_priv *priv = hw->priv;
1535 void __iomem *regs = priv->regs;
1536 dma_addr_t dma_addr;
1537 unsigned int dma_size;
1538 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001539 unsigned long timeout = 0;
1540 u8 buf[32];
1541
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001542 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001543 dma_size = le16_to_cpu(cmd->length);
1544 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1545 PCI_DMA_BIDIRECTIONAL);
1546 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1547 return -ENOMEM;
1548
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001549 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001550 if (rc) {
1551 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1552 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001553 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001554 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001555
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001556 priv->hostcmd_wait = &cmd_wait;
1557 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1558 iowrite32(MWL8K_H2A_INT_DOORBELL,
1559 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1560 iowrite32(MWL8K_H2A_INT_DUMMY,
1561 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001562
1563 timeout = wait_for_completion_timeout(&cmd_wait,
1564 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1565
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001566 priv->hostcmd_wait = NULL;
1567
1568 mwl8k_fw_unlock(hw);
1569
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001570 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1571 PCI_DMA_BIDIRECTIONAL);
1572
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001573 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001574 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001575 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001576 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1577 MWL8K_CMD_TIMEOUT_MS);
1578 rc = -ETIMEDOUT;
1579 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001580 int ms;
1581
1582 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1583
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001584 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001585 if (rc)
1586 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001587 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001588 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001589 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001590 else if (ms > 2000)
1591 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1592 wiphy_name(hw->wiphy),
1593 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1594 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001595 }
1596
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001597 return rc;
1598}
1599
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001600static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1601 struct ieee80211_vif *vif,
1602 struct mwl8k_cmd_pkt *cmd)
1603{
1604 if (vif != NULL)
1605 cmd->macid = MWL8K_VIF(vif)->macid;
1606 return mwl8k_post_cmd(hw, cmd);
1607}
1608
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001609/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001610 * Setup code shared between STA and AP firmware images.
1611 */
1612static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1613{
1614 struct mwl8k_priv *priv = hw->priv;
1615
1616 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1617 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1618
1619 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1620 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1621
1622 priv->band_24.band = IEEE80211_BAND_2GHZ;
1623 priv->band_24.channels = priv->channels_24;
1624 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1625 priv->band_24.bitrates = priv->rates_24;
1626 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1627
1628 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1629}
1630
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001631static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1632{
1633 struct mwl8k_priv *priv = hw->priv;
1634
1635 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1636 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1637
1638 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1639 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1640
1641 priv->band_50.band = IEEE80211_BAND_5GHZ;
1642 priv->band_50.channels = priv->channels_50;
1643 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1644 priv->band_50.bitrates = priv->rates_50;
1645 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1646
1647 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1648}
1649
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001650/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001651 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001652 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001653struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001654 struct mwl8k_cmd_pkt header;
1655 __u8 hw_rev;
1656 __u8 host_interface;
1657 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001658 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001659 __le16 region_code;
1660 __le32 fw_rev;
1661 __le32 ps_cookie;
1662 __le32 caps;
1663 __u8 mcs_bitmap[16];
1664 __le32 rx_queue_ptr;
1665 __le32 num_tx_queues;
1666 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1667 __le32 caps2;
1668 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001669 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001670} __attribute__((packed));
1671
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001672#define MWL8K_CAP_MAX_AMSDU 0x20000000
1673#define MWL8K_CAP_GREENFIELD 0x08000000
1674#define MWL8K_CAP_AMPDU 0x04000000
1675#define MWL8K_CAP_RX_STBC 0x01000000
1676#define MWL8K_CAP_TX_STBC 0x00800000
1677#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1678#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1679#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1680#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1681#define MWL8K_CAP_DELAY_BA 0x00003000
1682#define MWL8K_CAP_MIMO 0x00000200
1683#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001684#define MWL8K_CAP_BAND_MASK 0x00000007
1685#define MWL8K_CAP_5GHZ 0x00000004
1686#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001687
Lennert Buytenhek06953232010-01-12 13:49:41 +01001688static void
1689mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1690 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001691{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001692 int rx_streams;
1693 int tx_streams;
1694
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001695 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001696
1697 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001698 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001699 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001700 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001701 if (cap & MWL8K_CAP_AMPDU) {
1702 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001703 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1704 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001705 }
1706 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001707 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001708 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001709 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001710 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001711 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001712 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001713 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001714 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001715 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001716 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001717 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001718
1719 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1720 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1721
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001722 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001723 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001724 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001725 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001726 band->ht_cap.mcs.rx_mask[2] = 0xff;
1727 band->ht_cap.mcs.rx_mask[4] = 0x01;
1728 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001729
1730 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001731 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1732 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001733 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1734 }
1735}
1736
Lennert Buytenhek06953232010-01-12 13:49:41 +01001737static void
1738mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1739{
1740 struct mwl8k_priv *priv = hw->priv;
1741
1742 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1743 mwl8k_setup_2ghz_band(hw);
1744 if (caps & MWL8K_CAP_MIMO)
1745 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1746 }
1747
1748 if (caps & MWL8K_CAP_5GHZ) {
1749 mwl8k_setup_5ghz_band(hw);
1750 if (caps & MWL8K_CAP_MIMO)
1751 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1752 }
1753}
1754
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001755static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001756{
1757 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001758 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001759 int rc;
1760 int i;
1761
1762 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1763 if (cmd == NULL)
1764 return -ENOMEM;
1765
1766 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1767 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1768
1769 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1770 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001771 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001772 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001773 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001774 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001775 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001776 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001777
1778 rc = mwl8k_post_cmd(hw, &cmd->header);
1779
1780 if (!rc) {
1781 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1782 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001783 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001784 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001785 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001786 priv->ap_macids_supported = 0x00000000;
1787 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001788 }
1789
1790 kfree(cmd);
1791 return rc;
1792}
1793
1794/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001795 * CMD_GET_HW_SPEC (AP version).
1796 */
1797struct mwl8k_cmd_get_hw_spec_ap {
1798 struct mwl8k_cmd_pkt header;
1799 __u8 hw_rev;
1800 __u8 host_interface;
1801 __le16 num_wcb;
1802 __le16 num_mcaddrs;
1803 __u8 perm_addr[ETH_ALEN];
1804 __le16 region_code;
1805 __le16 num_antenna;
1806 __le32 fw_rev;
1807 __le32 wcbbase0;
1808 __le32 rxwrptr;
1809 __le32 rxrdptr;
1810 __le32 ps_cookie;
1811 __le32 wcbbase1;
1812 __le32 wcbbase2;
1813 __le32 wcbbase3;
1814} __attribute__((packed));
1815
1816static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1817{
1818 struct mwl8k_priv *priv = hw->priv;
1819 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1820 int rc;
1821
1822 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1823 if (cmd == NULL)
1824 return -ENOMEM;
1825
1826 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1827 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1828
1829 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1830 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1831
1832 rc = mwl8k_post_cmd(hw, &cmd->header);
1833
1834 if (!rc) {
1835 int off;
1836
1837 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1838 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1839 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1840 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001841 mwl8k_setup_2ghz_band(hw);
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001842 priv->ap_macids_supported = 0x000000ff;
1843 priv->sta_macids_supported = 0x00000000;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001844
1845 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1846 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1847
1848 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1849 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1850
1851 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1852 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1853
1854 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1855 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1856
1857 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1858 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1859
1860 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1861 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1862 }
1863
1864 kfree(cmd);
1865 return rc;
1866}
1867
1868/*
1869 * CMD_SET_HW_SPEC.
1870 */
1871struct mwl8k_cmd_set_hw_spec {
1872 struct mwl8k_cmd_pkt header;
1873 __u8 hw_rev;
1874 __u8 host_interface;
1875 __le16 num_mcaddrs;
1876 __u8 perm_addr[ETH_ALEN];
1877 __le16 region_code;
1878 __le32 fw_rev;
1879 __le32 ps_cookie;
1880 __le32 caps;
1881 __le32 rx_queue_ptr;
1882 __le32 num_tx_queues;
1883 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1884 __le32 flags;
1885 __le32 num_tx_desc_per_queue;
1886 __le32 total_rxd;
1887} __attribute__((packed));
1888
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001889#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1890#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
1891#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001892
1893static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1894{
1895 struct mwl8k_priv *priv = hw->priv;
1896 struct mwl8k_cmd_set_hw_spec *cmd;
1897 int rc;
1898 int i;
1899
1900 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1901 if (cmd == NULL)
1902 return -ENOMEM;
1903
1904 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1905 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1906
1907 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1908 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1909 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1910 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1911 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001912 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
1913 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
1914 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001915 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1916 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1917
1918 rc = mwl8k_post_cmd(hw, &cmd->header);
1919 kfree(cmd);
1920
1921 return rc;
1922}
1923
1924/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001925 * CMD_MAC_MULTICAST_ADR.
1926 */
1927struct mwl8k_cmd_mac_multicast_adr {
1928 struct mwl8k_cmd_pkt header;
1929 __le16 action;
1930 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001931 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001932};
1933
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001934#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1935#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1936#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1937#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001938
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001939static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001940__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001941 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001942{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001943 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001944 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001945 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001946
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001947 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001948 allmulti = 1;
1949 mc_count = 0;
1950 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001951
1952 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1953
1954 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001955 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001956 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001957
1958 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1959 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001960 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1961 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001962
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001963 if (allmulti) {
1964 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1965 } else if (mc_count) {
1966 int i;
1967
1968 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1969 cmd->numaddr = cpu_to_le16(mc_count);
1970 for (i = 0; i < mc_count && mclist; i++) {
1971 if (mclist->da_addrlen != ETH_ALEN) {
1972 kfree(cmd);
1973 return NULL;
1974 }
1975 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1976 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001977 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001978 }
1979
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001980 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001981}
1982
1983/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001984 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001985 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001986struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001987 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001988 __le32 stats[64];
1989} __attribute__((packed));
1990
1991#define MWL8K_STAT_ACK_FAILURE 9
1992#define MWL8K_STAT_RTS_FAILURE 12
1993#define MWL8K_STAT_FCS_ERROR 24
1994#define MWL8K_STAT_RTS_SUCCESS 11
1995
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001996static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1997 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001998{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001999 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002000 int rc;
2001
2002 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2003 if (cmd == NULL)
2004 return -ENOMEM;
2005
2006 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2007 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002008
2009 rc = mwl8k_post_cmd(hw, &cmd->header);
2010 if (!rc) {
2011 stats->dot11ACKFailureCount =
2012 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2013 stats->dot11RTSFailureCount =
2014 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2015 stats->dot11FCSErrorCount =
2016 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2017 stats->dot11RTSSuccessCount =
2018 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2019 }
2020 kfree(cmd);
2021
2022 return rc;
2023}
2024
2025/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002026 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002027 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002028struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002029 struct mwl8k_cmd_pkt header;
2030 __le16 action;
2031 __le16 control;
2032 __le16 radio_on;
2033} __attribute__((packed));
2034
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002035static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002036mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002037{
2038 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002039 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002040 int rc;
2041
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002042 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002043 return 0;
2044
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002045 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2046 if (cmd == NULL)
2047 return -ENOMEM;
2048
2049 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2050 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2051 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002052 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002053 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2054
2055 rc = mwl8k_post_cmd(hw, &cmd->header);
2056 kfree(cmd);
2057
2058 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002059 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002060
2061 return rc;
2062}
2063
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002064static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002065{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002066 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002067}
2068
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002069static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002070{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002071 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002072}
2073
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002074static int
2075mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2076{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002077 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002078
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002079 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002080
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002081 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002082}
2083
2084/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002085 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002086 */
2087#define MWL8K_TX_POWER_LEVEL_TOTAL 8
2088
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002089struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002090 struct mwl8k_cmd_pkt header;
2091 __le16 action;
2092 __le16 support_level;
2093 __le16 current_level;
2094 __le16 reserved;
2095 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2096} __attribute__((packed));
2097
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002098static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002099{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002100 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002101 int rc;
2102
2103 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2104 if (cmd == NULL)
2105 return -ENOMEM;
2106
2107 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2108 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2109 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2110 cmd->support_level = cpu_to_le16(dBm);
2111
2112 rc = mwl8k_post_cmd(hw, &cmd->header);
2113 kfree(cmd);
2114
2115 return rc;
2116}
2117
2118/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002119 * CMD_RF_ANTENNA.
2120 */
2121struct mwl8k_cmd_rf_antenna {
2122 struct mwl8k_cmd_pkt header;
2123 __le16 antenna;
2124 __le16 mode;
2125} __attribute__((packed));
2126
2127#define MWL8K_RF_ANTENNA_RX 1
2128#define MWL8K_RF_ANTENNA_TX 2
2129
2130static int
2131mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2132{
2133 struct mwl8k_cmd_rf_antenna *cmd;
2134 int rc;
2135
2136 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2137 if (cmd == NULL)
2138 return -ENOMEM;
2139
2140 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2141 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2142 cmd->antenna = cpu_to_le16(antenna);
2143 cmd->mode = cpu_to_le16(mask);
2144
2145 rc = mwl8k_post_cmd(hw, &cmd->header);
2146 kfree(cmd);
2147
2148 return rc;
2149}
2150
2151/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002152 * CMD_SET_BEACON.
2153 */
2154struct mwl8k_cmd_set_beacon {
2155 struct mwl8k_cmd_pkt header;
2156 __le16 beacon_len;
2157 __u8 beacon[0];
2158};
2159
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002160static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2161 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002162{
2163 struct mwl8k_cmd_set_beacon *cmd;
2164 int rc;
2165
2166 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2167 if (cmd == NULL)
2168 return -ENOMEM;
2169
2170 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2171 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2172 cmd->beacon_len = cpu_to_le16(len);
2173 memcpy(cmd->beacon, beacon, len);
2174
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002175 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002176 kfree(cmd);
2177
2178 return rc;
2179}
2180
2181/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002182 * CMD_SET_PRE_SCAN.
2183 */
2184struct mwl8k_cmd_set_pre_scan {
2185 struct mwl8k_cmd_pkt header;
2186} __attribute__((packed));
2187
2188static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2189{
2190 struct mwl8k_cmd_set_pre_scan *cmd;
2191 int rc;
2192
2193 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2194 if (cmd == NULL)
2195 return -ENOMEM;
2196
2197 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2198 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2199
2200 rc = mwl8k_post_cmd(hw, &cmd->header);
2201 kfree(cmd);
2202
2203 return rc;
2204}
2205
2206/*
2207 * CMD_SET_POST_SCAN.
2208 */
2209struct mwl8k_cmd_set_post_scan {
2210 struct mwl8k_cmd_pkt header;
2211 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002212 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002213} __attribute__((packed));
2214
2215static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002216mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002217{
2218 struct mwl8k_cmd_set_post_scan *cmd;
2219 int rc;
2220
2221 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2222 if (cmd == NULL)
2223 return -ENOMEM;
2224
2225 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2226 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2227 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002228 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002229
2230 rc = mwl8k_post_cmd(hw, &cmd->header);
2231 kfree(cmd);
2232
2233 return rc;
2234}
2235
2236/*
2237 * CMD_SET_RF_CHANNEL.
2238 */
2239struct mwl8k_cmd_set_rf_channel {
2240 struct mwl8k_cmd_pkt header;
2241 __le16 action;
2242 __u8 current_channel;
2243 __le32 channel_flags;
2244} __attribute__((packed));
2245
2246static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002247 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002248{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002249 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002250 struct mwl8k_cmd_set_rf_channel *cmd;
2251 int rc;
2252
2253 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2254 if (cmd == NULL)
2255 return -ENOMEM;
2256
2257 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2258 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2259 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2260 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002261
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002262 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002263 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002264 else if (channel->band == IEEE80211_BAND_5GHZ)
2265 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002266
2267 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2268 conf->channel_type == NL80211_CHAN_HT20)
2269 cmd->channel_flags |= cpu_to_le32(0x00000080);
2270 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2271 cmd->channel_flags |= cpu_to_le32(0x000001900);
2272 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2273 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002274
2275 rc = mwl8k_post_cmd(hw, &cmd->header);
2276 kfree(cmd);
2277
2278 return rc;
2279}
2280
2281/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002282 * CMD_SET_AID.
2283 */
2284#define MWL8K_FRAME_PROT_DISABLED 0x00
2285#define MWL8K_FRAME_PROT_11G 0x07
2286#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2287#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2288
2289struct mwl8k_cmd_update_set_aid {
2290 struct mwl8k_cmd_pkt header;
2291 __le16 aid;
2292
2293 /* AP's MAC address (BSSID) */
2294 __u8 bssid[ETH_ALEN];
2295 __le16 protection_mode;
2296 __u8 supp_rates[14];
2297} __attribute__((packed));
2298
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002299static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2300{
2301 int i;
2302 int j;
2303
2304 /*
2305 * Clear nonstandard rates 4 and 13.
2306 */
2307 mask &= 0x1fef;
2308
2309 for (i = 0, j = 0; i < 14; i++) {
2310 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002311 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002312 }
2313}
2314
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002315static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002316mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2317 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002318{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002319 struct mwl8k_cmd_update_set_aid *cmd;
2320 u16 prot_mode;
2321 int rc;
2322
2323 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2324 if (cmd == NULL)
2325 return -ENOMEM;
2326
2327 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2328 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002329 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002330 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002331
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002332 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002333 prot_mode = MWL8K_FRAME_PROT_11G;
2334 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002335 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002336 IEEE80211_HT_OP_MODE_PROTECTION) {
2337 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2338 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2339 break;
2340 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2341 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2342 break;
2343 default:
2344 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2345 break;
2346 }
2347 }
2348 cmd->protection_mode = cpu_to_le16(prot_mode);
2349
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002350 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002351
2352 rc = mwl8k_post_cmd(hw, &cmd->header);
2353 kfree(cmd);
2354
2355 return rc;
2356}
2357
2358/*
2359 * CMD_SET_RATE.
2360 */
2361struct mwl8k_cmd_set_rate {
2362 struct mwl8k_cmd_pkt header;
2363 __u8 legacy_rates[14];
2364
2365 /* Bitmap for supported MCS codes. */
2366 __u8 mcs_set[16];
2367 __u8 reserved[16];
2368} __attribute__((packed));
2369
2370static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002371mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002372 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002373{
2374 struct mwl8k_cmd_set_rate *cmd;
2375 int rc;
2376
2377 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2378 if (cmd == NULL)
2379 return -ENOMEM;
2380
2381 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2382 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002383 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002384 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002385
2386 rc = mwl8k_post_cmd(hw, &cmd->header);
2387 kfree(cmd);
2388
2389 return rc;
2390}
2391
2392/*
2393 * CMD_FINALIZE_JOIN.
2394 */
2395#define MWL8K_FJ_BEACON_MAXLEN 128
2396
2397struct mwl8k_cmd_finalize_join {
2398 struct mwl8k_cmd_pkt header;
2399 __le32 sleep_interval; /* Number of beacon periods to sleep */
2400 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2401} __attribute__((packed));
2402
2403static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2404 int framelen, int dtim)
2405{
2406 struct mwl8k_cmd_finalize_join *cmd;
2407 struct ieee80211_mgmt *payload = frame;
2408 int payload_len;
2409 int rc;
2410
2411 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2412 if (cmd == NULL)
2413 return -ENOMEM;
2414
2415 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2416 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2417 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2418
2419 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2420 if (payload_len < 0)
2421 payload_len = 0;
2422 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2423 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2424
2425 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2426
2427 rc = mwl8k_post_cmd(hw, &cmd->header);
2428 kfree(cmd);
2429
2430 return rc;
2431}
2432
2433/*
2434 * CMD_SET_RTS_THRESHOLD.
2435 */
2436struct mwl8k_cmd_set_rts_threshold {
2437 struct mwl8k_cmd_pkt header;
2438 __le16 action;
2439 __le16 threshold;
2440} __attribute__((packed));
2441
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002442static int
2443mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002444{
2445 struct mwl8k_cmd_set_rts_threshold *cmd;
2446 int rc;
2447
2448 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2449 if (cmd == NULL)
2450 return -ENOMEM;
2451
2452 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2453 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002454 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2455 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002456
2457 rc = mwl8k_post_cmd(hw, &cmd->header);
2458 kfree(cmd);
2459
2460 return rc;
2461}
2462
2463/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002464 * CMD_SET_SLOT.
2465 */
2466struct mwl8k_cmd_set_slot {
2467 struct mwl8k_cmd_pkt header;
2468 __le16 action;
2469 __u8 short_slot;
2470} __attribute__((packed));
2471
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002472static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002473{
2474 struct mwl8k_cmd_set_slot *cmd;
2475 int rc;
2476
2477 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2478 if (cmd == NULL)
2479 return -ENOMEM;
2480
2481 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2482 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2483 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002484 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002485
2486 rc = mwl8k_post_cmd(hw, &cmd->header);
2487 kfree(cmd);
2488
2489 return rc;
2490}
2491
2492/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002493 * CMD_SET_EDCA_PARAMS.
2494 */
2495struct mwl8k_cmd_set_edca_params {
2496 struct mwl8k_cmd_pkt header;
2497
2498 /* See MWL8K_SET_EDCA_XXX below */
2499 __le16 action;
2500
2501 /* TX opportunity in units of 32 us */
2502 __le16 txop;
2503
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002504 union {
2505 struct {
2506 /* Log exponent of max contention period: 0...15 */
2507 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002508
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002509 /* Log exponent of min contention period: 0...15 */
2510 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002511
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002512 /* Adaptive interframe spacing in units of 32us */
2513 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002514
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002515 /* TX queue to configure */
2516 __u8 txq;
2517 } ap;
2518 struct {
2519 /* Log exponent of max contention period: 0...15 */
2520 __u8 log_cw_max;
2521
2522 /* Log exponent of min contention period: 0...15 */
2523 __u8 log_cw_min;
2524
2525 /* Adaptive interframe spacing in units of 32us */
2526 __u8 aifs;
2527
2528 /* TX queue to configure */
2529 __u8 txq;
2530 } sta;
2531 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002532} __attribute__((packed));
2533
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002534#define MWL8K_SET_EDCA_CW 0x01
2535#define MWL8K_SET_EDCA_TXOP 0x02
2536#define MWL8K_SET_EDCA_AIFS 0x04
2537
2538#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2539 MWL8K_SET_EDCA_TXOP | \
2540 MWL8K_SET_EDCA_AIFS)
2541
2542static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002543mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2544 __u16 cw_min, __u16 cw_max,
2545 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002546{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002547 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002548 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002549 int rc;
2550
2551 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2552 if (cmd == NULL)
2553 return -ENOMEM;
2554
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002555 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2556 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002557 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2558 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002559 if (priv->ap_fw) {
2560 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2561 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2562 cmd->ap.aifs = aifs;
2563 cmd->ap.txq = qnum;
2564 } else {
2565 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2566 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2567 cmd->sta.aifs = aifs;
2568 cmd->sta.txq = qnum;
2569 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002570
2571 rc = mwl8k_post_cmd(hw, &cmd->header);
2572 kfree(cmd);
2573
2574 return rc;
2575}
2576
2577/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002578 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002579 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002580struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002581 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002582 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002583} __attribute__((packed));
2584
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002585static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002586{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002587 struct mwl8k_priv *priv = hw->priv;
2588 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002589 int rc;
2590
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002591 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2592 if (cmd == NULL)
2593 return -ENOMEM;
2594
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002595 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002596 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002597 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002598
2599 rc = mwl8k_post_cmd(hw, &cmd->header);
2600 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002601
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002602 if (!rc)
2603 priv->wmm_enabled = enable;
2604
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002605 return rc;
2606}
2607
2608/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002609 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002610 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002611struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002612 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002613 __le32 action;
2614 __u8 rx_antenna_map;
2615 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002616} __attribute__((packed));
2617
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002618static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002619{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002620 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002621 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002622
2623 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2624 if (cmd == NULL)
2625 return -ENOMEM;
2626
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002627 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002628 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002629 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2630 cmd->rx_antenna_map = rx;
2631 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002632
2633 rc = mwl8k_post_cmd(hw, &cmd->header);
2634 kfree(cmd);
2635
2636 return rc;
2637}
2638
2639/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002640 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002641 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002642struct mwl8k_cmd_use_fixed_rate_sta {
2643 struct mwl8k_cmd_pkt header;
2644 __le32 action;
2645 __le32 allow_rate_drop;
2646 __le32 num_rates;
2647 struct {
2648 __le32 is_ht_rate;
2649 __le32 enable_retry;
2650 __le32 rate;
2651 __le32 retry_count;
2652 } rate_entry[8];
2653 __le32 rate_type;
2654 __le32 reserved1;
2655 __le32 reserved2;
2656} __attribute__((packed));
2657
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002658#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002659#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002660
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002661static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002662{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002663 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002664 int rc;
2665
2666 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2667 if (cmd == NULL)
2668 return -ENOMEM;
2669
2670 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2671 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002672 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2673 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002674
2675 rc = mwl8k_post_cmd(hw, &cmd->header);
2676 kfree(cmd);
2677
2678 return rc;
2679}
2680
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002681/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002682 * CMD_USE_FIXED_RATE (AP version).
2683 */
2684struct mwl8k_cmd_use_fixed_rate_ap {
2685 struct mwl8k_cmd_pkt header;
2686 __le32 action;
2687 __le32 allow_rate_drop;
2688 __le32 num_rates;
2689 struct mwl8k_rate_entry_ap {
2690 __le32 is_ht_rate;
2691 __le32 enable_retry;
2692 __le32 rate;
2693 __le32 retry_count;
2694 } rate_entry[4];
2695 u8 multicast_rate;
2696 u8 multicast_rate_type;
2697 u8 management_rate;
2698} __attribute__((packed));
2699
2700static int
2701mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2702{
2703 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2704 int rc;
2705
2706 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2707 if (cmd == NULL)
2708 return -ENOMEM;
2709
2710 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2711 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2712 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2713 cmd->multicast_rate = mcast;
2714 cmd->management_rate = mgmt;
2715
2716 rc = mwl8k_post_cmd(hw, &cmd->header);
2717 kfree(cmd);
2718
2719 return rc;
2720}
2721
2722/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002723 * CMD_ENABLE_SNIFFER.
2724 */
2725struct mwl8k_cmd_enable_sniffer {
2726 struct mwl8k_cmd_pkt header;
2727 __le32 action;
2728} __attribute__((packed));
2729
2730static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2731{
2732 struct mwl8k_cmd_enable_sniffer *cmd;
2733 int rc;
2734
2735 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2736 if (cmd == NULL)
2737 return -ENOMEM;
2738
2739 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2740 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2741 cmd->action = cpu_to_le32(!!enable);
2742
2743 rc = mwl8k_post_cmd(hw, &cmd->header);
2744 kfree(cmd);
2745
2746 return rc;
2747}
2748
2749/*
2750 * CMD_SET_MAC_ADDR.
2751 */
2752struct mwl8k_cmd_set_mac_addr {
2753 struct mwl8k_cmd_pkt header;
2754 union {
2755 struct {
2756 __le16 mac_type;
2757 __u8 mac_addr[ETH_ALEN];
2758 } mbss;
2759 __u8 mac_addr[ETH_ALEN];
2760 };
2761} __attribute__((packed));
2762
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002763#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
2764#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
2765#define MWL8K_MAC_TYPE_PRIMARY_AP 2
2766#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002767
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002768static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
2769 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002770{
2771 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002772 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002773 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002774 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002775 int rc;
2776
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002777 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2778 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
2779 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
2780 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
2781 else
2782 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
2783 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
2784 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
2785 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2786 else
2787 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
2788 }
2789
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002790 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2791 if (cmd == NULL)
2792 return -ENOMEM;
2793
2794 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2795 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2796 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002797 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002798 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2799 } else {
2800 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2801 }
2802
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002803 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002804 kfree(cmd);
2805
2806 return rc;
2807}
2808
2809/*
2810 * CMD_SET_RATEADAPT_MODE.
2811 */
2812struct mwl8k_cmd_set_rate_adapt_mode {
2813 struct mwl8k_cmd_pkt header;
2814 __le16 action;
2815 __le16 mode;
2816} __attribute__((packed));
2817
2818static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2819{
2820 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2821 int rc;
2822
2823 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2824 if (cmd == NULL)
2825 return -ENOMEM;
2826
2827 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2828 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2829 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2830 cmd->mode = cpu_to_le16(mode);
2831
2832 rc = mwl8k_post_cmd(hw, &cmd->header);
2833 kfree(cmd);
2834
2835 return rc;
2836}
2837
2838/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002839 * CMD_BSS_START.
2840 */
2841struct mwl8k_cmd_bss_start {
2842 struct mwl8k_cmd_pkt header;
2843 __le32 enable;
2844} __attribute__((packed));
2845
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002846static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
2847 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002848{
2849 struct mwl8k_cmd_bss_start *cmd;
2850 int rc;
2851
2852 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2853 if (cmd == NULL)
2854 return -ENOMEM;
2855
2856 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
2857 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2858 cmd->enable = cpu_to_le32(enable);
2859
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002860 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002861 kfree(cmd);
2862
2863 return rc;
2864}
2865
2866/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002867 * CMD_SET_NEW_STN.
2868 */
2869struct mwl8k_cmd_set_new_stn {
2870 struct mwl8k_cmd_pkt header;
2871 __le16 aid;
2872 __u8 mac_addr[6];
2873 __le16 stn_id;
2874 __le16 action;
2875 __le16 rsvd;
2876 __le32 legacy_rates;
2877 __u8 ht_rates[4];
2878 __le16 cap_info;
2879 __le16 ht_capabilities_info;
2880 __u8 mac_ht_param_info;
2881 __u8 rev;
2882 __u8 control_channel;
2883 __u8 add_channel;
2884 __le16 op_mode;
2885 __le16 stbc;
2886 __u8 add_qos_info;
2887 __u8 is_qos_sta;
2888 __le32 fw_sta_ptr;
2889} __attribute__((packed));
2890
2891#define MWL8K_STA_ACTION_ADD 0
2892#define MWL8K_STA_ACTION_REMOVE 2
2893
2894static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
2895 struct ieee80211_vif *vif,
2896 struct ieee80211_sta *sta)
2897{
2898 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002899 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002900 int rc;
2901
2902 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2903 if (cmd == NULL)
2904 return -ENOMEM;
2905
2906 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2907 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2908 cmd->aid = cpu_to_le16(sta->aid);
2909 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
2910 cmd->stn_id = cpu_to_le16(sta->aid);
2911 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002912 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
2913 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
2914 else
2915 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
2916 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002917 if (sta->ht_cap.ht_supported) {
2918 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
2919 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
2920 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
2921 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
2922 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
2923 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
2924 ((sta->ht_cap.ampdu_density & 7) << 2);
2925 cmd->is_qos_sta = 1;
2926 }
2927
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002928 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002929 kfree(cmd);
2930
2931 return rc;
2932}
2933
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002934static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
2935 struct ieee80211_vif *vif)
2936{
2937 struct mwl8k_cmd_set_new_stn *cmd;
2938 int rc;
2939
2940 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2941 if (cmd == NULL)
2942 return -ENOMEM;
2943
2944 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2945 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2946 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
2947
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002948 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002949 kfree(cmd);
2950
2951 return rc;
2952}
2953
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002954static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
2955 struct ieee80211_vif *vif, u8 *addr)
2956{
2957 struct mwl8k_cmd_set_new_stn *cmd;
2958 int rc;
2959
2960 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2961 if (cmd == NULL)
2962 return -ENOMEM;
2963
2964 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2965 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2966 memcpy(cmd->mac_addr, addr, ETH_ALEN);
2967 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
2968
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002969 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002970 kfree(cmd);
2971
2972 return rc;
2973}
2974
2975/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002976 * CMD_UPDATE_STADB.
2977 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002978struct ewc_ht_info {
2979 __le16 control1;
2980 __le16 control2;
2981 __le16 control3;
2982} __attribute__((packed));
2983
2984struct peer_capability_info {
2985 /* Peer type - AP vs. STA. */
2986 __u8 peer_type;
2987
2988 /* Basic 802.11 capabilities from assoc resp. */
2989 __le16 basic_caps;
2990
2991 /* Set if peer supports 802.11n high throughput (HT). */
2992 __u8 ht_support;
2993
2994 /* Valid if HT is supported. */
2995 __le16 ht_caps;
2996 __u8 extended_ht_caps;
2997 struct ewc_ht_info ewc_info;
2998
2999 /* Legacy rate table. Intersection of our rates and peer rates. */
3000 __u8 legacy_rates[12];
3001
3002 /* HT rate table. Intersection of our rates and peer rates. */
3003 __u8 ht_rates[16];
3004 __u8 pad[16];
3005
3006 /* If set, interoperability mode, no proprietary extensions. */
3007 __u8 interop;
3008 __u8 pad2;
3009 __u8 station_id;
3010 __le16 amsdu_enabled;
3011} __attribute__((packed));
3012
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003013struct mwl8k_cmd_update_stadb {
3014 struct mwl8k_cmd_pkt header;
3015
3016 /* See STADB_ACTION_TYPE */
3017 __le32 action;
3018
3019 /* Peer MAC address */
3020 __u8 peer_addr[ETH_ALEN];
3021
3022 __le32 reserved;
3023
3024 /* Peer info - valid during add/update. */
3025 struct peer_capability_info peer_info;
3026} __attribute__((packed));
3027
Lennert Buytenheka6804002010-01-04 21:55:42 +01003028#define MWL8K_STA_DB_MODIFY_ENTRY 1
3029#define MWL8K_STA_DB_DEL_ENTRY 2
3030
3031/* Peer Entry flags - used to define the type of the peer node */
3032#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3033
3034static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003035 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003036 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003037{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003038 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003039 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003040 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003041 int rc;
3042
3043 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3044 if (cmd == NULL)
3045 return -ENOMEM;
3046
3047 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3048 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003049 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003050 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003051
Lennert Buytenheka6804002010-01-04 21:55:42 +01003052 p = &cmd->peer_info;
3053 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3054 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003055 p->ht_support = sta->ht_cap.ht_supported;
3056 p->ht_caps = sta->ht_cap.cap;
3057 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3058 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003059 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3060 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3061 else
3062 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3063 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003064 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003065 p->interop = 1;
3066 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003067
Lennert Buytenheka6804002010-01-04 21:55:42 +01003068 rc = mwl8k_post_cmd(hw, &cmd->header);
3069 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003070
Lennert Buytenheka6804002010-01-04 21:55:42 +01003071 return rc ? rc : p->station_id;
3072}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003073
Lennert Buytenheka6804002010-01-04 21:55:42 +01003074static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3075 struct ieee80211_vif *vif, u8 *addr)
3076{
3077 struct mwl8k_cmd_update_stadb *cmd;
3078 int rc;
3079
3080 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3081 if (cmd == NULL)
3082 return -ENOMEM;
3083
3084 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3085 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3086 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3087 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3088
3089 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003090 kfree(cmd);
3091
3092 return rc;
3093}
3094
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003095
3096/*
3097 * Interrupt handling.
3098 */
3099static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3100{
3101 struct ieee80211_hw *hw = dev_id;
3102 struct mwl8k_priv *priv = hw->priv;
3103 u32 status;
3104
3105 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003106 if (!status)
3107 return IRQ_NONE;
3108
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003109 if (status & MWL8K_A2H_INT_TX_DONE) {
3110 status &= ~MWL8K_A2H_INT_TX_DONE;
3111 tasklet_schedule(&priv->poll_tx_task);
3112 }
3113
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003114 if (status & MWL8K_A2H_INT_RX_READY) {
3115 status &= ~MWL8K_A2H_INT_RX_READY;
3116 tasklet_schedule(&priv->poll_rx_task);
3117 }
3118
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003119 if (status)
3120 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003121
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003122 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003123 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003124 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003125 }
3126
3127 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003128 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003129 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003130 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003131 }
3132
3133 return IRQ_HANDLED;
3134}
3135
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003136static void mwl8k_tx_poll(unsigned long data)
3137{
3138 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3139 struct mwl8k_priv *priv = hw->priv;
3140 int limit;
3141 int i;
3142
3143 limit = 32;
3144
3145 spin_lock_bh(&priv->tx_lock);
3146
3147 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3148 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3149
3150 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3151 complete(priv->tx_wait);
3152 priv->tx_wait = NULL;
3153 }
3154
3155 spin_unlock_bh(&priv->tx_lock);
3156
3157 if (limit) {
3158 writel(~MWL8K_A2H_INT_TX_DONE,
3159 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3160 } else {
3161 tasklet_schedule(&priv->poll_tx_task);
3162 }
3163}
3164
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003165static void mwl8k_rx_poll(unsigned long data)
3166{
3167 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3168 struct mwl8k_priv *priv = hw->priv;
3169 int limit;
3170
3171 limit = 32;
3172 limit -= rxq_process(hw, 0, limit);
3173 limit -= rxq_refill(hw, 0, limit);
3174
3175 if (limit) {
3176 writel(~MWL8K_A2H_INT_RX_READY,
3177 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3178 } else {
3179 tasklet_schedule(&priv->poll_rx_task);
3180 }
3181}
3182
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003183
3184/*
3185 * Core driver operations.
3186 */
3187static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3188{
3189 struct mwl8k_priv *priv = hw->priv;
3190 int index = skb_get_queue_mapping(skb);
3191 int rc;
3192
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003193 if (!priv->radio_on) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003194 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003195 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003196 dev_kfree_skb(skb);
3197 return NETDEV_TX_OK;
3198 }
3199
3200 rc = mwl8k_txq_xmit(hw, index, skb);
3201
3202 return rc;
3203}
3204
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003205static int mwl8k_start(struct ieee80211_hw *hw)
3206{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003207 struct mwl8k_priv *priv = hw->priv;
3208 int rc;
3209
Joe Perchesa0607fd2009-11-18 23:29:17 -08003210 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003211 IRQF_SHARED, MWL8K_NAME, hw);
3212 if (rc) {
3213 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003214 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003215 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003216 }
3217
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003218 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003219 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003220 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003221
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003222 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003223 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003224
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003225 rc = mwl8k_fw_lock(hw);
3226 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003227 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003228
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003229 if (!priv->ap_fw) {
3230 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003231 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003232
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003233 if (!rc)
3234 rc = mwl8k_cmd_set_pre_scan(hw);
3235
3236 if (!rc)
3237 rc = mwl8k_cmd_set_post_scan(hw,
3238 "\x00\x00\x00\x00\x00\x00");
3239 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003240
3241 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003242 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003243
3244 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003245 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003246
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003247 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003248 }
3249
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003250 if (rc) {
3251 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3252 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003253 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003254 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003255 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003256
3257 return rc;
3258}
3259
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003260static void mwl8k_stop(struct ieee80211_hw *hw)
3261{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003262 struct mwl8k_priv *priv = hw->priv;
3263 int i;
3264
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003265 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003266
3267 ieee80211_stop_queues(hw);
3268
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003269 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003270 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003271 free_irq(priv->pdev->irq, hw);
3272
3273 /* Stop finalize join worker */
3274 cancel_work_sync(&priv->finalize_join_worker);
3275 if (priv->beacon_skb != NULL)
3276 dev_kfree_skb(priv->beacon_skb);
3277
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003278 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003279 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003280 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003281
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003282 /* Return all skbs to mac80211 */
3283 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003284 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003285}
3286
3287static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003288 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003289{
3290 struct mwl8k_priv *priv = hw->priv;
3291 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003292 u32 macids_supported;
3293 int macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003294
3295 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003296 * Reject interface creation if sniffer mode is active, as
3297 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003298 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003299 */
3300 if (priv->sniffer_enabled) {
3301 printk(KERN_INFO "%s: unable to create STA "
3302 "interface due to sniffer mode being enabled\n",
3303 wiphy_name(hw->wiphy));
3304 return -EINVAL;
3305 }
3306
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003307
3308 switch (vif->type) {
3309 case NL80211_IFTYPE_AP:
3310 macids_supported = priv->ap_macids_supported;
3311 break;
3312 case NL80211_IFTYPE_STATION:
3313 macids_supported = priv->sta_macids_supported;
3314 break;
3315 default:
3316 return -EINVAL;
3317 }
3318
3319 macid = ffs(macids_supported & ~priv->macids_used);
3320 if (!macid--)
3321 return -EBUSY;
3322
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003323 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003324 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003325 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003326 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003327 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003328 mwl8k_vif->seqno = 0;
3329
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003330 /* Set the mac address. */
3331 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3332
3333 if (priv->ap_fw)
3334 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3335
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003336 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003337 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003338
3339 return 0;
3340}
3341
3342static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003343 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003344{
3345 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003346 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003347
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003348 if (priv->ap_fw)
3349 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3350
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003351 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003352
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003353 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003354 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003355}
3356
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003357static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003358{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003359 struct ieee80211_conf *conf = &hw->conf;
3360 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003361 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003362
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003363 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003364 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003365 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003366 }
3367
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003368 rc = mwl8k_fw_lock(hw);
3369 if (rc)
3370 return rc;
3371
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003372 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003373 if (rc)
3374 goto out;
3375
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003376 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003377 if (rc)
3378 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003379
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003380 if (conf->power_level > 18)
3381 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003382 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003383 if (rc)
3384 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003385
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003386 if (priv->ap_fw) {
3387 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3388 if (!rc)
3389 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3390 } else {
3391 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3392 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003393
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003394out:
3395 mwl8k_fw_unlock(hw);
3396
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003397 return rc;
3398}
3399
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003400static void
3401mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3402 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003403{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003404 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003405 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003406 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003407 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003408
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003409 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003410 return;
3411
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003412 /*
3413 * No need to capture a beacon if we're no longer associated.
3414 */
3415 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3416 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003417
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003418 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003419 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003420 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003421 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003422 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003423
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003424 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003425
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003426 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003427 if (ap == NULL) {
3428 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003429 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003430 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003431
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003432 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
3433 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
3434 } else {
3435 ap_legacy_rates =
3436 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3437 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003438 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003439
3440 rcu_read_unlock();
3441 }
3442
3443 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003444 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003445 if (rc)
3446 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003447
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003448 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003449 if (rc)
3450 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003451 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003452
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003453 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003454 rc = mwl8k_set_radio_preamble(hw,
3455 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003456 if (rc)
3457 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003458 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003459
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003460 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003461 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003462 if (rc)
3463 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003464 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003465
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003466 if (vif->bss_conf.assoc &&
3467 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
3468 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003469 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003470 if (rc)
3471 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003472 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003473
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003474 if (vif->bss_conf.assoc &&
3475 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003476 /*
3477 * Finalize the join. Tell rx handler to process
3478 * next beacon from our BSSID.
3479 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003480 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003481 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003482 }
3483
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003484out:
3485 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003486}
3487
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003488static void
3489mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3490 struct ieee80211_bss_conf *info, u32 changed)
3491{
3492 int rc;
3493
3494 if (mwl8k_fw_lock(hw))
3495 return;
3496
3497 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3498 rc = mwl8k_set_radio_preamble(hw,
3499 vif->bss_conf.use_short_preamble);
3500 if (rc)
3501 goto out;
3502 }
3503
3504 if (changed & BSS_CHANGED_BASIC_RATES) {
3505 int idx;
3506 int rate;
3507
3508 /*
3509 * Use lowest supported basic rate for multicasts
3510 * and management frames (such as probe responses --
3511 * beacons will always go out at 1 Mb/s).
3512 */
3513 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003514 if (idx)
3515 idx--;
3516
3517 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3518 rate = mwl8k_rates_24[idx].hw_value;
3519 else
3520 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003521
3522 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
3523 }
3524
3525 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
3526 struct sk_buff *skb;
3527
3528 skb = ieee80211_beacon_get(hw, vif);
3529 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003530 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003531 kfree_skb(skb);
3532 }
3533 }
3534
3535 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003536 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003537
3538out:
3539 mwl8k_fw_unlock(hw);
3540}
3541
3542static void
3543mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3544 struct ieee80211_bss_conf *info, u32 changed)
3545{
3546 struct mwl8k_priv *priv = hw->priv;
3547
3548 if (!priv->ap_fw)
3549 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
3550 else
3551 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
3552}
3553
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003554static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3555 int mc_count, struct dev_addr_list *mclist)
3556{
3557 struct mwl8k_cmd_pkt *cmd;
3558
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003559 /*
3560 * Synthesize and return a command packet that programs the
3561 * hardware multicast address filter. At this point we don't
3562 * know whether FIF_ALLMULTI is being requested, but if it is,
3563 * we'll end up throwing this packet away and creating a new
3564 * one in mwl8k_configure_filter().
3565 */
3566 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003567
3568 return (unsigned long)cmd;
3569}
3570
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003571static int
3572mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3573 unsigned int changed_flags,
3574 unsigned int *total_flags)
3575{
3576 struct mwl8k_priv *priv = hw->priv;
3577
3578 /*
3579 * Hardware sniffer mode is mutually exclusive with STA
3580 * operation, so refuse to enable sniffer mode if a STA
3581 * interface is active.
3582 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003583 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003584 if (net_ratelimit())
3585 printk(KERN_INFO "%s: not enabling sniffer "
3586 "mode because STA interface is active\n",
3587 wiphy_name(hw->wiphy));
3588 return 0;
3589 }
3590
3591 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003592 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003593 return 0;
3594 priv->sniffer_enabled = true;
3595 }
3596
3597 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3598 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3599 FIF_OTHER_BSS;
3600
3601 return 1;
3602}
3603
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003604static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
3605{
3606 if (!list_empty(&priv->vif_list))
3607 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
3608
3609 return NULL;
3610}
3611
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003612static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3613 unsigned int changed_flags,
3614 unsigned int *total_flags,
3615 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003616{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003617 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003618 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3619
3620 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003621 * AP firmware doesn't allow fine-grained control over
3622 * the receive filter.
3623 */
3624 if (priv->ap_fw) {
3625 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3626 kfree(cmd);
3627 return;
3628 }
3629
3630 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003631 * Enable hardware sniffer mode if FIF_CONTROL or
3632 * FIF_OTHER_BSS is requested.
3633 */
3634 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3635 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3636 kfree(cmd);
3637 return;
3638 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003639
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003640 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003641 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003642
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003643 if (mwl8k_fw_lock(hw)) {
3644 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003645 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003646 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003647
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003648 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003649 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003650 priv->sniffer_enabled = false;
3651 }
3652
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003653 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003654 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3655 /*
3656 * Disable the BSS filter.
3657 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003658 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003659 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003660 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003661 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003662
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003663 /*
3664 * Enable the BSS filter.
3665 *
3666 * If there is an active STA interface, use that
3667 * interface's BSSID, otherwise use a dummy one
3668 * (where the OUI part needs to be nonzero for
3669 * the BSSID to be accepted by POST_SCAN).
3670 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003671 mwl8k_vif = mwl8k_first_vif(priv);
3672 if (mwl8k_vif != NULL)
3673 bssid = mwl8k_vif->vif->bss_conf.bssid;
3674 else
3675 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003676
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003677 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003678 }
3679 }
3680
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003681 /*
3682 * If FIF_ALLMULTI is being requested, throw away the command
3683 * packet that ->prepare_multicast() built and replace it with
3684 * a command packet that enables reception of all multicast
3685 * packets.
3686 */
3687 if (*total_flags & FIF_ALLMULTI) {
3688 kfree(cmd);
3689 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3690 }
3691
3692 if (cmd != NULL) {
3693 mwl8k_post_cmd(hw, cmd);
3694 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003695 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003696
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003697 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003698}
3699
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003700static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3701{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003702 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003703}
3704
Johannes Berg4a6967b2010-02-19 19:18:37 +01003705static int mwl8k_sta_remove(struct ieee80211_hw *hw,
3706 struct ieee80211_vif *vif,
3707 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003708{
3709 struct mwl8k_priv *priv = hw->priv;
3710
Johannes Berg4a6967b2010-02-19 19:18:37 +01003711 if (priv->ap_fw)
3712 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
3713 else
3714 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
3715}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003716
Johannes Berg4a6967b2010-02-19 19:18:37 +01003717static int mwl8k_sta_add(struct ieee80211_hw *hw,
3718 struct ieee80211_vif *vif,
3719 struct ieee80211_sta *sta)
3720{
3721 struct mwl8k_priv *priv = hw->priv;
3722 int ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003723
Johannes Berg4a6967b2010-02-19 19:18:37 +01003724 if (!priv->ap_fw) {
3725 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
3726 if (ret >= 0) {
3727 MWL8K_STA(sta)->peer_id = ret;
3728 return 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003729 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01003730
3731 return ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003732 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003733
Johannes Berg4a6967b2010-02-19 19:18:37 +01003734 return mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003735}
3736
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003737static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3738 const struct ieee80211_tx_queue_params *params)
3739{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003740 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003741 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003742
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003743 rc = mwl8k_fw_lock(hw);
3744 if (!rc) {
3745 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003746 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003747
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003748 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003749 rc = mwl8k_cmd_set_edca_params(hw, queue,
3750 params->cw_min,
3751 params->cw_max,
3752 params->aifs,
3753 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003754
3755 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003756 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003757
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003758 return rc;
3759}
3760
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003761static int mwl8k_get_stats(struct ieee80211_hw *hw,
3762 struct ieee80211_low_level_stats *stats)
3763{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003764 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003765}
3766
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003767static int
3768mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3769 enum ieee80211_ampdu_mlme_action action,
3770 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3771{
3772 switch (action) {
3773 case IEEE80211_AMPDU_RX_START:
3774 case IEEE80211_AMPDU_RX_STOP:
3775 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3776 return -ENOTSUPP;
3777 return 0;
3778 default:
3779 return -ENOTSUPP;
3780 }
3781}
3782
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003783static const struct ieee80211_ops mwl8k_ops = {
3784 .tx = mwl8k_tx,
3785 .start = mwl8k_start,
3786 .stop = mwl8k_stop,
3787 .add_interface = mwl8k_add_interface,
3788 .remove_interface = mwl8k_remove_interface,
3789 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003790 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003791 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003792 .configure_filter = mwl8k_configure_filter,
3793 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01003794 .sta_add = mwl8k_sta_add,
3795 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003796 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003797 .get_stats = mwl8k_get_stats,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003798 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003799};
3800
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003801static void mwl8k_finalize_join_worker(struct work_struct *work)
3802{
3803 struct mwl8k_priv *priv =
3804 container_of(work, struct mwl8k_priv, finalize_join_worker);
3805 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01003806 struct ieee80211_mgmt *mgmt = (void *)skb->data;
3807 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
3808 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
3809 mgmt->u.beacon.variable, len);
3810 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003811
Johannes Berg56007a02010-01-26 14:19:52 +01003812 if (tim && tim[1] >= 2)
3813 dtim_period = tim[3];
3814
3815 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003816
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003817 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003818 priv->beacon_skb = NULL;
3819}
3820
John W. Linvillebcb628d2009-11-06 16:40:16 -05003821enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003822 MWL8363 = 0,
3823 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003824 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003825};
3826
John W. Linvillebcb628d2009-11-06 16:40:16 -05003827static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003828 [MWL8363] = {
3829 .part_name = "88w8363",
3830 .helper_image = "mwl8k/helper_8363.fw",
3831 .fw_image = "mwl8k/fmimage_8363.fw",
3832 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003833 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003834 .part_name = "88w8687",
3835 .helper_image = "mwl8k/helper_8687.fw",
3836 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003837 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003838 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003839 .part_name = "88w8366",
3840 .helper_image = "mwl8k/helper_8366.fw",
3841 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003842 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003843 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003844};
3845
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01003846MODULE_FIRMWARE("mwl8k/helper_8363.fw");
3847MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
3848MODULE_FIRMWARE("mwl8k/helper_8687.fw");
3849MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
3850MODULE_FIRMWARE("mwl8k/helper_8366.fw");
3851MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
3852
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003853static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003854 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3855 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003856 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3857 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3858 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01003859 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003860 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003861};
3862MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3863
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003864static int __devinit mwl8k_probe(struct pci_dev *pdev,
3865 const struct pci_device_id *id)
3866{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003867 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003868 struct ieee80211_hw *hw;
3869 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003870 int rc;
3871 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003872
3873 if (!printed_version) {
3874 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3875 printed_version = 1;
3876 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003877
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003878
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003879 rc = pci_enable_device(pdev);
3880 if (rc) {
3881 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3882 MWL8K_NAME);
3883 return rc;
3884 }
3885
3886 rc = pci_request_regions(pdev, MWL8K_NAME);
3887 if (rc) {
3888 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3889 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003890 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003891 }
3892
3893 pci_set_master(pdev);
3894
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003895
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003896 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3897 if (hw == NULL) {
3898 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3899 rc = -ENOMEM;
3900 goto err_free_reg;
3901 }
3902
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003903 SET_IEEE80211_DEV(hw, &pdev->dev);
3904 pci_set_drvdata(pdev, hw);
3905
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003906 priv = hw->priv;
3907 priv->hw = hw;
3908 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003909 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003910
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003911
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003912 priv->sram = pci_iomap(pdev, 0, 0x10000);
3913 if (priv->sram == NULL) {
3914 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003915 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003916 goto err_iounmap;
3917 }
3918
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003919 /*
3920 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3921 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3922 */
3923 priv->regs = pci_iomap(pdev, 1, 0x10000);
3924 if (priv->regs == NULL) {
3925 priv->regs = pci_iomap(pdev, 2, 0x10000);
3926 if (priv->regs == NULL) {
3927 printk(KERN_ERR "%s: Cannot map device registers\n",
3928 wiphy_name(hw->wiphy));
3929 goto err_iounmap;
3930 }
3931 }
3932
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003933
3934 /* Reset firmware and hardware */
3935 mwl8k_hw_reset(priv);
3936
3937 /* Ask userland hotplug daemon for the device firmware */
3938 rc = mwl8k_request_firmware(priv);
3939 if (rc) {
3940 printk(KERN_ERR "%s: Firmware files not found\n",
3941 wiphy_name(hw->wiphy));
3942 goto err_stop_firmware;
3943 }
3944
3945 /* Load firmware into hardware */
3946 rc = mwl8k_load_firmware(hw);
3947 if (rc) {
3948 printk(KERN_ERR "%s: Cannot start firmware\n",
3949 wiphy_name(hw->wiphy));
3950 goto err_stop_firmware;
3951 }
3952
3953 /* Reclaim memory once firmware is successfully loaded */
3954 mwl8k_release_firmware(priv);
3955
3956
Lennert Buytenhek91942232010-01-04 21:53:54 +01003957 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003958 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003959 if (priv->rxd_ops == NULL) {
3960 printk(KERN_ERR "%s: Driver does not have AP "
3961 "firmware image support for this hardware\n",
3962 wiphy_name(hw->wiphy));
3963 goto err_stop_firmware;
3964 }
3965 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003966 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003967 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003968
3969 priv->sniffer_enabled = false;
3970 priv->wmm_enabled = false;
3971 priv->pending_tx_pkts = 0;
3972
3973
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003974 /*
3975 * Extra headroom is the size of the required DMA header
3976 * minus the size of the smallest 802.11 frame (CTS frame).
3977 */
3978 hw->extra_tx_headroom =
3979 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3980
3981 hw->channel_change_time = 10;
3982
3983 hw->queues = MWL8K_TX_QUEUES;
3984
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003985 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003986 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003987 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003988 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003989
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003990 priv->macids_used = 0;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003991 INIT_LIST_HEAD(&priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003992
3993 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003994 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003995 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003996
3997 /* Finalize join worker */
3998 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3999
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004000 /* TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004001 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4002 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004003 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4004 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004005
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004006 /* Power management cookie */
4007 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4008 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004009 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004010
4011 rc = mwl8k_rxq_init(hw, 0);
4012 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004013 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004014 rxq_refill(hw, 0, INT_MAX);
4015
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004016 mutex_init(&priv->fw_mutex);
4017 priv->fw_mutex_owner = NULL;
4018 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004019 priv->hostcmd_wait = NULL;
4020
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004021 spin_lock_init(&priv->tx_lock);
4022
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004023 priv->tx_wait = NULL;
4024
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004025 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4026 rc = mwl8k_txq_init(hw, i);
4027 if (rc)
4028 goto err_free_queues;
4029 }
4030
4031 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004032 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004033 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004034 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004035 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4036
Joe Perchesa0607fd2009-11-18 23:29:17 -08004037 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004038 IRQF_SHARED, MWL8K_NAME, hw);
4039 if (rc) {
4040 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004041 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004042 goto err_free_queues;
4043 }
4044
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004045 /*
4046 * Temporarily enable interrupts. Initial firmware host
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004047 * commands use interrupts and avoid polling. Disable
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004048 * interrupts when done.
4049 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004050 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004051
4052 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02004053 if (priv->ap_fw) {
4054 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4055 if (!rc)
4056 rc = mwl8k_cmd_set_hw_spec(hw);
4057 } else {
4058 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4059 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004060 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004061 printk(KERN_ERR "%s: Cannot initialise firmware\n",
4062 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004063 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004064 }
4065
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004066 hw->wiphy->interface_modes = 0;
4067 if (priv->ap_macids_supported)
4068 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4069 if (priv->sta_macids_supported)
4070 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4071
4072
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004073 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004074 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004075 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004076 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004077 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004078 }
4079
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004080 /* Clear MAC address */
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004081 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004082 if (rc) {
4083 printk(KERN_ERR "%s: Cannot clear MAC address\n",
4084 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004085 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004086 }
4087
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004088 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004089 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004090 free_irq(priv->pdev->irq, hw);
4091
4092 rc = ieee80211_register_hw(hw);
4093 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004094 printk(KERN_ERR "%s: Cannot register device\n",
4095 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01004096 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004097 }
4098
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02004099 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02004100 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004101 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02004102 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004103 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4104 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004105
4106 return 0;
4107
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004108err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004109 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004110 free_irq(priv->pdev->irq, hw);
4111
4112err_free_queues:
4113 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4114 mwl8k_txq_deinit(hw, i);
4115 mwl8k_rxq_deinit(hw, 0);
4116
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004117err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004118 if (priv->cookie != NULL)
4119 pci_free_consistent(priv->pdev, 4,
4120 priv->cookie, priv->cookie_dma);
4121
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004122err_stop_firmware:
4123 mwl8k_hw_reset(priv);
4124 mwl8k_release_firmware(priv);
4125
4126err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004127 if (priv->regs != NULL)
4128 pci_iounmap(pdev, priv->regs);
4129
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004130 if (priv->sram != NULL)
4131 pci_iounmap(pdev, priv->sram);
4132
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004133 pci_set_drvdata(pdev, NULL);
4134 ieee80211_free_hw(hw);
4135
4136err_free_reg:
4137 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004138
4139err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004140 pci_disable_device(pdev);
4141
4142 return rc;
4143}
4144
Joerg Albert230f7af2009-04-18 02:10:45 +02004145static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004146{
4147 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4148}
4149
Joerg Albert230f7af2009-04-18 02:10:45 +02004150static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004151{
4152 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4153 struct mwl8k_priv *priv;
4154 int i;
4155
4156 if (hw == NULL)
4157 return;
4158 priv = hw->priv;
4159
4160 ieee80211_stop_queues(hw);
4161
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004162 ieee80211_unregister_hw(hw);
4163
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004164 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004165 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004166 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004167
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004168 /* Stop hardware */
4169 mwl8k_hw_reset(priv);
4170
4171 /* Return all skbs to mac80211 */
4172 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004173 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004174
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004175 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4176 mwl8k_txq_deinit(hw, i);
4177
4178 mwl8k_rxq_deinit(hw, 0);
4179
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004180 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004181
4182 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004183 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004184 pci_set_drvdata(pdev, NULL);
4185 ieee80211_free_hw(hw);
4186 pci_release_regions(pdev);
4187 pci_disable_device(pdev);
4188}
4189
4190static struct pci_driver mwl8k_driver = {
4191 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004192 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004193 .probe = mwl8k_probe,
4194 .remove = __devexit_p(mwl8k_remove),
4195 .shutdown = __devexit_p(mwl8k_shutdown),
4196};
4197
4198static int __init mwl8k_init(void)
4199{
4200 return pci_register_driver(&mwl8k_driver);
4201}
4202
4203static void __exit mwl8k_exit(void)
4204{
4205 pci_unregister_driver(&mwl8k_driver);
4206}
4207
4208module_init(mwl8k_init);
4209module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004210
4211MODULE_DESCRIPTION(MWL8K_DESC);
4212MODULE_VERSION(MWL8K_VERSION);
4213MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
4214MODULE_LICENSE("GPL");