blob: 4ee68f7d20f499eda721e45af3c4fec4b5ecef37 [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 Buytenheka145d572009-08-18 04:34:26 +02005 * Copyright (C) 2008-2009 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 Buytenheka145d572009-08-18 04:34:26 +020029#define MWL8K_VERSION "0.10"
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);
87 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status);
88};
89
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020090struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020091 char *part_name;
92 char *helper_image;
93 char *fw_image;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020094 struct rxd_ops *rxd_ops;
Lennert Buytenhek547810e2009-10-22 20:21:02 +020095 u16 modes;
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
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200122 struct ieee80211_tx_queue_stats stats;
123 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
128/* Pointers to the firmware data and meta information about it. */
129struct mwl8k_firmware {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 /* Boot helper code */
131 struct firmware *helper;
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200132
133 /* Microcode */
134 struct firmware *ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100135};
136
137struct mwl8k_priv {
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +0200138 void __iomem *sram;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100139 void __iomem *regs;
140 struct ieee80211_hw *hw;
141
142 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100143
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200144 struct mwl8k_device_info *device_info;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200145 bool ap_fw;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200146 struct rxd_ops *rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200147
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100148 /* firmware files and meta data */
149 struct mwl8k_firmware fw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100150
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200151 /* firmware access */
152 struct mutex fw_mutex;
153 struct task_struct *fw_mutex_owner;
154 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200155 struct completion *hostcmd_wait;
156
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 /* lock held over TX and TX reap */
158 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100159
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200160 /* TX quiesce completion, protected by fw_mutex and tx_lock */
161 struct completion *tx_wait;
162
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100163 struct ieee80211_vif *vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100164
165 struct ieee80211_channel *current_channel;
166
167 /* power management status cookie from firmware */
168 u32 *cookie;
169 dma_addr_t cookie_dma;
170
171 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100172 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200173 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100174
175 /*
176 * Running count of TX packets in flight, to avoid
177 * iterating over the transmit rings each time.
178 */
179 int pending_tx_pkts;
180
181 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
182 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
183
184 /* PHY parameters */
185 struct ieee80211_supported_band band;
186 struct ieee80211_channel channels[14];
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200187 struct ieee80211_rate rates[13];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100188
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200189 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200190 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200191 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200192 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100193
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100194 /* XXX need to convert this to handle multiple interfaces */
195 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200196 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100197 struct sk_buff *beacon_skb;
198
199 /*
200 * This FJ worker has to be global as it is scheduled from the
201 * RX handler. At this point we don't know which interface it
202 * belongs to until the list of bssids waiting to complete join
203 * is checked.
204 */
205 struct work_struct finalize_join_worker;
206
207 /* Tasklet to reclaim TX descriptors and buffers after tx */
208 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100209};
210
211/* Per interface specific private data */
212struct mwl8k_vif {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100213 /* backpointer to parent config block */
214 struct mwl8k_priv *priv;
215
216 /* BSS config of AP or IBSS from mac80211*/
217 struct ieee80211_bss_conf bss_info;
218
219 /* BSSID of AP or IBSS */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200220 u8 bssid[ETH_ALEN];
221 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100222
223 /*
224 * Subset of supported legacy rates.
225 * Intersection of AP and STA supported rates.
226 */
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200227 struct ieee80211_rate legacy_rates[13];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100228
229 /* number of supported legacy rates */
230 u8 legacy_nrates;
231
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100232 /* Index into station database.Returned by update_sta_db call */
233 u8 peer_id;
234
235 /* Non AMPDU sequence number assigned by driver */
236 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100237};
238
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200239#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100240
241static const struct ieee80211_channel mwl8k_channels[] = {
242 { .center_freq = 2412, .hw_value = 1, },
243 { .center_freq = 2417, .hw_value = 2, },
244 { .center_freq = 2422, .hw_value = 3, },
245 { .center_freq = 2427, .hw_value = 4, },
246 { .center_freq = 2432, .hw_value = 5, },
247 { .center_freq = 2437, .hw_value = 6, },
248 { .center_freq = 2442, .hw_value = 7, },
249 { .center_freq = 2447, .hw_value = 8, },
250 { .center_freq = 2452, .hw_value = 9, },
251 { .center_freq = 2457, .hw_value = 10, },
252 { .center_freq = 2462, .hw_value = 11, },
253};
254
255static const struct ieee80211_rate mwl8k_rates[] = {
256 { .bitrate = 10, .hw_value = 2, },
257 { .bitrate = 20, .hw_value = 4, },
258 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200259 { .bitrate = 110, .hw_value = 22, },
260 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100261 { .bitrate = 60, .hw_value = 12, },
262 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100263 { .bitrate = 120, .hw_value = 24, },
264 { .bitrate = 180, .hw_value = 36, },
265 { .bitrate = 240, .hw_value = 48, },
266 { .bitrate = 360, .hw_value = 72, },
267 { .bitrate = 480, .hw_value = 96, },
268 { .bitrate = 540, .hw_value = 108, },
269};
270
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100271/* Set or get info from Firmware */
272#define MWL8K_CMD_SET 0x0001
273#define MWL8K_CMD_GET 0x0000
274
275/* Firmware command codes */
276#define MWL8K_CMD_CODE_DNLD 0x0001
277#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200278#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100279#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
280#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200281#define MWL8K_CMD_RADIO_CONTROL 0x001c
282#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200283#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100284#define MWL8K_CMD_SET_PRE_SCAN 0x0107
285#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200286#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100287#define MWL8K_CMD_SET_AID 0x010d
288#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200289#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100290#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200291#define MWL8K_CMD_SET_SLOT 0x0114
292#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
293#define MWL8K_CMD_SET_WMM_MODE 0x0123
294#define MWL8K_CMD_MIMO_CONFIG 0x0125
295#define MWL8K_CMD_USE_FIXED_RATE 0x0126
296#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200297#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200298#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
299#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100300
301static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
302{
303#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
304 snprintf(buf, bufsize, "%s", #x);\
305 return buf;\
306 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200307 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100308 MWL8K_CMDNAME(CODE_DNLD);
309 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200310 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100311 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
312 MWL8K_CMDNAME(GET_STAT);
313 MWL8K_CMDNAME(RADIO_CONTROL);
314 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200315 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100316 MWL8K_CMDNAME(SET_PRE_SCAN);
317 MWL8K_CMDNAME(SET_POST_SCAN);
318 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100319 MWL8K_CMDNAME(SET_AID);
320 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200321 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100322 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200323 MWL8K_CMDNAME(SET_SLOT);
324 MWL8K_CMDNAME(SET_EDCA_PARAMS);
325 MWL8K_CMDNAME(SET_WMM_MODE);
326 MWL8K_CMDNAME(MIMO_CONFIG);
327 MWL8K_CMDNAME(USE_FIXED_RATE);
328 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200329 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200330 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
331 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100332 default:
333 snprintf(buf, bufsize, "0x%x", cmd);
334 }
335#undef MWL8K_CMDNAME
336
337 return buf;
338}
339
340/* Hardware and firmware reset */
341static void mwl8k_hw_reset(struct mwl8k_priv *priv)
342{
343 iowrite32(MWL8K_H2A_INT_RESET,
344 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
345 iowrite32(MWL8K_H2A_INT_RESET,
346 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
347 msleep(20);
348}
349
350/* Release fw image */
351static void mwl8k_release_fw(struct firmware **fw)
352{
353 if (*fw == NULL)
354 return;
355 release_firmware(*fw);
356 *fw = NULL;
357}
358
359static void mwl8k_release_firmware(struct mwl8k_priv *priv)
360{
361 mwl8k_release_fw(&priv->fw.ucode);
362 mwl8k_release_fw(&priv->fw.helper);
363}
364
365/* Request fw image */
366static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200367 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100368{
369 /* release current image */
370 if (*fw != NULL)
371 mwl8k_release_fw(fw);
372
373 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200374 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100375}
376
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200377static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100378{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200379 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100380 int rc;
381
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200382 if (di->helper_image != NULL) {
383 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw.helper);
384 if (rc) {
385 printk(KERN_ERR "%s: Error requesting helper "
386 "firmware file %s\n", pci_name(priv->pdev),
387 di->helper_image);
388 return rc;
389 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100390 }
391
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200392 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw.ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100393 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200394 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200395 pci_name(priv->pdev), di->fw_image);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100396 mwl8k_release_fw(&priv->fw.helper);
397 return rc;
398 }
399
400 return 0;
401}
402
403struct mwl8k_cmd_pkt {
404 __le16 code;
405 __le16 length;
406 __le16 seq_num;
407 __le16 result;
408 char payload[0];
409} __attribute__((packed));
410
411/*
412 * Firmware loading.
413 */
414static int
415mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
416{
417 void __iomem *regs = priv->regs;
418 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100419 int loops;
420
421 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
422 if (pci_dma_mapping_error(priv->pdev, dma_addr))
423 return -ENOMEM;
424
425 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
426 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
427 iowrite32(MWL8K_H2A_INT_DOORBELL,
428 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
429 iowrite32(MWL8K_H2A_INT_DUMMY,
430 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
431
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100432 loops = 1000;
433 do {
434 u32 int_code;
435
436 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
437 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
438 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100439 break;
440 }
441
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200442 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100443 udelay(1);
444 } while (--loops);
445
446 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
447
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200448 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100449}
450
451static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
452 const u8 *data, size_t length)
453{
454 struct mwl8k_cmd_pkt *cmd;
455 int done;
456 int rc = 0;
457
458 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
459 if (cmd == NULL)
460 return -ENOMEM;
461
462 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
463 cmd->seq_num = 0;
464 cmd->result = 0;
465
466 done = 0;
467 while (length) {
468 int block_size = length > 256 ? 256 : length;
469
470 memcpy(cmd->payload, data + done, block_size);
471 cmd->length = cpu_to_le16(block_size);
472
473 rc = mwl8k_send_fw_load_cmd(priv, cmd,
474 sizeof(*cmd) + block_size);
475 if (rc)
476 break;
477
478 done += block_size;
479 length -= block_size;
480 }
481
482 if (!rc) {
483 cmd->length = 0;
484 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
485 }
486
487 kfree(cmd);
488
489 return rc;
490}
491
492static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
493 const u8 *data, size_t length)
494{
495 unsigned char *buffer;
496 int may_continue, rc = 0;
497 u32 done, prev_block_size;
498
499 buffer = kmalloc(1024, GFP_KERNEL);
500 if (buffer == NULL)
501 return -ENOMEM;
502
503 done = 0;
504 prev_block_size = 0;
505 may_continue = 1000;
506 while (may_continue > 0) {
507 u32 block_size;
508
509 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
510 if (block_size & 1) {
511 block_size &= ~1;
512 may_continue--;
513 } else {
514 done += prev_block_size;
515 length -= prev_block_size;
516 }
517
518 if (block_size > 1024 || block_size > length) {
519 rc = -EOVERFLOW;
520 break;
521 }
522
523 if (length == 0) {
524 rc = 0;
525 break;
526 }
527
528 if (block_size == 0) {
529 rc = -EPROTO;
530 may_continue--;
531 udelay(1);
532 continue;
533 }
534
535 prev_block_size = block_size;
536 memcpy(buffer, data + done, block_size);
537
538 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
539 if (rc)
540 break;
541 }
542
543 if (!rc && length != 0)
544 rc = -EREMOTEIO;
545
546 kfree(buffer);
547
548 return rc;
549}
550
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200551static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100552{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200553 struct mwl8k_priv *priv = hw->priv;
554 struct firmware *fw = priv->fw.ucode;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200555 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200556 int rc;
557 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100558
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200559 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
560 struct firmware *helper = priv->fw.helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100561
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200562 if (helper == NULL) {
563 printk(KERN_ERR "%s: helper image needed but none "
564 "given\n", pci_name(priv->pdev));
565 return -EINVAL;
566 }
567
568 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100569 if (rc) {
570 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200571 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100572 return rc;
573 }
574 msleep(1);
575
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200576 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100577 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200578 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100579 }
580
581 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200582 printk(KERN_ERR "%s: unable to load firmware image\n",
583 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100584 return rc;
585 }
586
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200587 if (di->modes & BIT(NL80211_IFTYPE_AP))
588 iowrite32(MWL8K_MODE_AP, priv->regs + MWL8K_HIU_GEN_PTR);
589 else
590 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100591 msleep(1);
592
593 loops = 200000;
594 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200595 u32 ready_code;
596
597 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
598 if (ready_code == MWL8K_FWAP_READY) {
599 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100600 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200601 } else if (ready_code == MWL8K_FWSTA_READY) {
602 priv->ap_fw = 0;
603 break;
604 }
605
606 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100607 udelay(1);
608 } while (--loops);
609
610 return loops ? 0 : -ETIMEDOUT;
611}
612
613
614/*
615 * Defines shared between transmission and reception.
616 */
617/* HT control fields for firmware */
618struct ewc_ht_info {
619 __le16 control1;
620 __le16 control2;
621 __le16 control3;
622} __attribute__((packed));
623
624/* Firmware Station database operations */
625#define MWL8K_STA_DB_ADD_ENTRY 0
626#define MWL8K_STA_DB_MODIFY_ENTRY 1
627#define MWL8K_STA_DB_DEL_ENTRY 2
628#define MWL8K_STA_DB_FLUSH 3
629
630/* Peer Entry flags - used to define the type of the peer node */
631#define MWL8K_PEER_TYPE_ACCESSPOINT 2
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100632
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200633#define MWL8K_IEEE_LEGACY_DATA_RATES 13
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100634#define MWL8K_MCS_BITMAP_SIZE 16
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100635
636struct peer_capability_info {
637 /* Peer type - AP vs. STA. */
638 __u8 peer_type;
639
640 /* Basic 802.11 capabilities from assoc resp. */
641 __le16 basic_caps;
642
643 /* Set if peer supports 802.11n high throughput (HT). */
644 __u8 ht_support;
645
646 /* Valid if HT is supported. */
647 __le16 ht_caps;
648 __u8 extended_ht_caps;
649 struct ewc_ht_info ewc_info;
650
651 /* Legacy rate table. Intersection of our rates and peer rates. */
652 __u8 legacy_rates[MWL8K_IEEE_LEGACY_DATA_RATES];
653
654 /* HT rate table. Intersection of our rates and peer rates. */
655 __u8 ht_rates[MWL8K_MCS_BITMAP_SIZE];
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +0200656 __u8 pad[16];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100657
658 /* If set, interoperability mode, no proprietary extensions. */
659 __u8 interop;
660 __u8 pad2;
661 __u8 station_id;
662 __le16 amsdu_enabled;
663} __attribute__((packed));
664
665/* Inline functions to manipulate QoS field in data descriptor. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100666static inline u16 mwl8k_qos_setbit_eosp(u16 qos)
667{
668 u16 val_mask = 1 << 4;
669
670 /* End of Service Period Bit 4 */
671 return qos | val_mask;
672}
673
674static inline u16 mwl8k_qos_setbit_ack(u16 qos, u8 ack_policy)
675{
676 u16 val_mask = 0x3;
677 u8 shift = 5;
678 u16 qos_mask = ~(val_mask << shift);
679
680 /* Ack Policy Bit 5-6 */
681 return (qos & qos_mask) | ((ack_policy & val_mask) << shift);
682}
683
684static inline u16 mwl8k_qos_setbit_amsdu(u16 qos)
685{
686 u16 val_mask = 1 << 7;
687
688 /* AMSDU present Bit 7 */
689 return qos | val_mask;
690}
691
692static inline u16 mwl8k_qos_setbit_qlen(u16 qos, u8 len)
693{
694 u16 val_mask = 0xff;
695 u8 shift = 8;
696 u16 qos_mask = ~(val_mask << shift);
697
698 /* Queue Length Bits 8-15 */
699 return (qos & qos_mask) | ((len & val_mask) << shift);
700}
701
702/* DMA header used by firmware and hardware. */
703struct mwl8k_dma_data {
704 __le16 fwlen;
705 struct ieee80211_hdr wh;
706} __attribute__((packed));
707
708/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200709static inline void mwl8k_remove_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100710{
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200711 struct mwl8k_dma_data *tr = (struct mwl8k_dma_data *)skb->data;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100712 void *dst, *src = &tr->wh;
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200713 int hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100714 u16 space = sizeof(struct mwl8k_dma_data) - hdrlen;
715
716 dst = (void *)tr + space;
717 if (dst != src) {
718 memmove(dst, src, hdrlen);
719 skb_pull(skb, space);
720 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100721}
722
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200723static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100724{
725 struct ieee80211_hdr *wh;
726 u32 hdrlen, pktlen;
727 struct mwl8k_dma_data *tr;
728
729 wh = (struct ieee80211_hdr *)skb->data;
730 hdrlen = ieee80211_hdrlen(wh->frame_control);
731 pktlen = skb->len;
732
733 /*
734 * Copy up/down the 802.11 header; the firmware requires
735 * we present a 2-byte payload length followed by a
736 * 4-address header (w/o QoS), followed (optionally) by
737 * any WEP/ExtIV header (but only filled in for CCMP).
738 */
739 if (hdrlen != sizeof(struct mwl8k_dma_data))
740 skb_push(skb, sizeof(struct mwl8k_dma_data) - hdrlen);
741
742 tr = (struct mwl8k_dma_data *)skb->data;
743 if (wh != &tr->wh)
744 memmove(&tr->wh, wh, hdrlen);
745
746 /* Clear addr4 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200747 memset(tr->wh.addr4, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100748
749 /*
750 * Firmware length is the length of the fully formed "802.11
751 * payload". That is, everything except for the 802.11 header.
752 * This includes all crypto material including the MIC.
753 */
754 tr->fwlen = cpu_to_le16(pktlen - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100755}
756
757
758/*
759 * Packet reception.
760 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200761struct mwl8k_rxd_8687 {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100762 __le16 pkt_len;
763 __u8 link_quality;
764 __u8 noise_level;
765 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200766 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100767 __le16 qos_control;
768 __le16 rate_info;
769 __le32 pad0[4];
770 __u8 rssi;
771 __u8 channel;
772 __le16 pad1;
773 __u8 rx_ctrl;
774 __u8 rx_status;
775 __u8 pad2[2];
776} __attribute__((packed));
777
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200778#define MWL8K_8687_RATE_INFO_SHORTPRE 0x8000
779#define MWL8K_8687_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
780#define MWL8K_8687_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
781#define MWL8K_8687_RATE_INFO_40MHZ 0x0004
782#define MWL8K_8687_RATE_INFO_SHORTGI 0x0002
783#define MWL8K_8687_RATE_INFO_MCS_FORMAT 0x0001
784
785#define MWL8K_8687_RX_CTRL_OWNED_BY_HOST 0x02
786
787static void mwl8k_rxd_8687_init(void *_rxd, dma_addr_t next_dma_addr)
788{
789 struct mwl8k_rxd_8687 *rxd = _rxd;
790
791 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
792 rxd->rx_ctrl = MWL8K_8687_RX_CTRL_OWNED_BY_HOST;
793}
794
795static void mwl8k_rxd_8687_refill(void *_rxd, dma_addr_t addr, int len)
796{
797 struct mwl8k_rxd_8687 *rxd = _rxd;
798
799 rxd->pkt_len = cpu_to_le16(len);
800 rxd->pkt_phys_addr = cpu_to_le32(addr);
801 wmb();
802 rxd->rx_ctrl = 0;
803}
804
805static int
806mwl8k_rxd_8687_process(void *_rxd, struct ieee80211_rx_status *status)
807{
808 struct mwl8k_rxd_8687 *rxd = _rxd;
809 u16 rate_info;
810
811 if (!(rxd->rx_ctrl & MWL8K_8687_RX_CTRL_OWNED_BY_HOST))
812 return -1;
813 rmb();
814
815 rate_info = le16_to_cpu(rxd->rate_info);
816
817 memset(status, 0, sizeof(*status));
818
819 status->signal = -rxd->rssi;
820 status->noise = -rxd->noise_level;
821 status->qual = rxd->link_quality;
822 status->antenna = MWL8K_8687_RATE_INFO_ANTSELECT(rate_info);
823 status->rate_idx = MWL8K_8687_RATE_INFO_RATEID(rate_info);
824
825 if (rate_info & MWL8K_8687_RATE_INFO_SHORTPRE)
826 status->flag |= RX_FLAG_SHORTPRE;
827 if (rate_info & MWL8K_8687_RATE_INFO_40MHZ)
828 status->flag |= RX_FLAG_40MHZ;
829 if (rate_info & MWL8K_8687_RATE_INFO_SHORTGI)
830 status->flag |= RX_FLAG_SHORT_GI;
831 if (rate_info & MWL8K_8687_RATE_INFO_MCS_FORMAT)
832 status->flag |= RX_FLAG_HT;
833
834 status->band = IEEE80211_BAND_2GHZ;
835 status->freq = ieee80211_channel_to_frequency(rxd->channel);
836
837 return le16_to_cpu(rxd->pkt_len);
838}
839
840static struct rxd_ops rxd_8687_ops = {
841 .rxd_size = sizeof(struct mwl8k_rxd_8687),
842 .rxd_init = mwl8k_rxd_8687_init,
843 .rxd_refill = mwl8k_rxd_8687_refill,
844 .rxd_process = mwl8k_rxd_8687_process,
845};
846
847
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100848#define MWL8K_RX_DESCS 256
849#define MWL8K_RX_MAXSZ 3800
850
851static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
852{
853 struct mwl8k_priv *priv = hw->priv;
854 struct mwl8k_rx_queue *rxq = priv->rxq + index;
855 int size;
856 int i;
857
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200858 rxq->rxd_count = 0;
859 rxq->head = 0;
860 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100861
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200862 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100863
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200864 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
865 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100866 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200867 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100868 return -ENOMEM;
869 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200870 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100871
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200872 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
873 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100874 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200875 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200876 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100877 return -ENOMEM;
878 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200879 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100880
881 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200882 int desc_size;
883 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100884 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200885 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100886
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200887 desc_size = priv->rxd_ops->rxd_size;
888 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100889
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200890 nexti = i + 1;
891 if (nexti == MWL8K_RX_DESCS)
892 nexti = 0;
893 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
894
895 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100896 }
897
898 return 0;
899}
900
901static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
902{
903 struct mwl8k_priv *priv = hw->priv;
904 struct mwl8k_rx_queue *rxq = priv->rxq + index;
905 int refilled;
906
907 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200908 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100909 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200910 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100911 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200912 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100913
914 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
915 if (skb == NULL)
916 break;
917
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200918 addr = pci_map_single(priv->pdev, skb->data,
919 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100920
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200921 rxq->rxd_count++;
922 rx = rxq->tail++;
923 if (rxq->tail == MWL8K_RX_DESCS)
924 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200925 rxq->buf[rx].skb = skb;
926 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200927
928 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
929 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100930
931 refilled++;
932 }
933
934 return refilled;
935}
936
937/* Must be called only when the card's reception is completely halted */
938static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
939{
940 struct mwl8k_priv *priv = hw->priv;
941 struct mwl8k_rx_queue *rxq = priv->rxq + index;
942 int i;
943
944 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200945 if (rxq->buf[i].skb != NULL) {
946 pci_unmap_single(priv->pdev,
947 pci_unmap_addr(&rxq->buf[i], dma),
948 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
949 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100950
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200951 kfree_skb(rxq->buf[i].skb);
952 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100953 }
954 }
955
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200956 kfree(rxq->buf);
957 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100958
959 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200960 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200961 rxq->rxd, rxq->rxd_dma);
962 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100963}
964
965
966/*
967 * Scan a list of BSSIDs to process for finalize join.
968 * Allows for extension to process multiple BSSIDs.
969 */
970static inline int
971mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
972{
973 return priv->capture_beacon &&
974 ieee80211_is_beacon(wh->frame_control) &&
975 !compare_ether_addr(wh->addr3, priv->capture_bssid);
976}
977
Lennert Buytenhek37797522009-10-22 20:19:45 +0200978static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
979 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100980{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200981 struct mwl8k_priv *priv = hw->priv;
982
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100983 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200984 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100985
986 /*
987 * Use GFP_ATOMIC as rxq_process is called from
988 * the primary interrupt handler, memory allocation call
989 * must not sleep.
990 */
991 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
992 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200993 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100994}
995
996static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
997{
998 struct mwl8k_priv *priv = hw->priv;
999 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1000 int processed;
1001
1002 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001003 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001004 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001005 void *rxd;
1006 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001007 struct ieee80211_rx_status status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001008
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001009 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001010 if (skb == NULL)
1011 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001012
1013 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1014
1015 pkt_len = priv->rxd_ops->rxd_process(rxd, &status);
1016 if (pkt_len < 0)
1017 break;
1018
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001019 rxq->buf[rxq->head].skb = NULL;
1020
1021 pci_unmap_single(priv->pdev,
1022 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1023 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1024 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001025
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001026 rxq->head++;
1027 if (rxq->head == MWL8K_RX_DESCS)
1028 rxq->head = 0;
1029
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001030 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001031
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001032 skb_put(skb, pkt_len);
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001033 mwl8k_remove_dma_header(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001034
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001035 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001036 * Check for a pending join operation. Save a
1037 * copy of the beacon and schedule a tasklet to
1038 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001039 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001040 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001041 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001042
Johannes Bergf1d58c22009-06-17 13:13:00 +02001043 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1044 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001045
1046 processed++;
1047 }
1048
1049 return processed;
1050}
1051
1052
1053/*
1054 * Packet transmission.
1055 */
1056
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001057/* Transmit packet ACK policy */
1058#define MWL8K_TXD_ACK_POLICY_NORMAL 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001059#define MWL8K_TXD_ACK_POLICY_BLOCKACK 3
1060
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001061#define MWL8K_TXD_STATUS_OK 0x00000001
1062#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1063#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1064#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001065#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001066
1067struct mwl8k_tx_desc {
1068 __le32 status;
1069 __u8 data_rate;
1070 __u8 tx_priority;
1071 __le16 qos_control;
1072 __le32 pkt_phys_addr;
1073 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001074 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001075 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001076 __le32 reserved;
1077 __le16 rate_info;
1078 __u8 peer_id;
1079 __u8 tx_frag_cnt;
1080} __attribute__((packed));
1081
1082#define MWL8K_TX_DESCS 128
1083
1084static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1085{
1086 struct mwl8k_priv *priv = hw->priv;
1087 struct mwl8k_tx_queue *txq = priv->txq + index;
1088 int size;
1089 int i;
1090
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001091 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1092 txq->stats.limit = MWL8K_TX_DESCS;
1093 txq->head = 0;
1094 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001095
1096 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1097
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001098 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1099 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001100 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001101 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001102 return -ENOMEM;
1103 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001104 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001105
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001106 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1107 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001108 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001109 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001110 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001111 return -ENOMEM;
1112 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001113 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001114
1115 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1116 struct mwl8k_tx_desc *tx_desc;
1117 int nexti;
1118
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001119 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001120 nexti = (i + 1) % MWL8K_TX_DESCS;
1121
1122 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001123 tx_desc->next_txd_phys_addr =
1124 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001125 }
1126
1127 return 0;
1128}
1129
1130static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1131{
1132 iowrite32(MWL8K_H2A_INT_PPA_READY,
1133 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1134 iowrite32(MWL8K_H2A_INT_DUMMY,
1135 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1136 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1137}
1138
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001139struct mwl8k_txq_info {
1140 u32 fw_owned;
1141 u32 drv_owned;
1142 u32 unused;
1143 u32 len;
1144 u32 head;
1145 u32 tail;
1146};
1147
1148static int mwl8k_scan_tx_ring(struct mwl8k_priv *priv,
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001149 struct mwl8k_txq_info *txinfo)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001150{
1151 int count, desc, status;
1152 struct mwl8k_tx_queue *txq;
1153 struct mwl8k_tx_desc *tx_desc;
1154 int ndescs = 0;
1155
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001156 memset(txinfo, 0, MWL8K_TX_QUEUES * sizeof(struct mwl8k_txq_info));
1157
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001158 for (count = 0; count < MWL8K_TX_QUEUES; count++) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159 txq = priv->txq + count;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001160 txinfo[count].len = txq->stats.len;
1161 txinfo[count].head = txq->head;
1162 txinfo[count].tail = txq->tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001164 tx_desc = txq->txd + desc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001165 status = le32_to_cpu(tx_desc->status);
1166
1167 if (status & MWL8K_TXD_STATUS_FW_OWNED)
1168 txinfo[count].fw_owned++;
1169 else
1170 txinfo[count].drv_owned++;
1171
1172 if (tx_desc->pkt_len == 0)
1173 txinfo[count].unused++;
1174 }
1175 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001176
1177 return ndescs;
1178}
1179
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001180/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001181 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001182 */
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001183static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001184{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001185 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001186 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001187 u32 count;
1188 unsigned long timeout;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001189
1190 might_sleep();
1191
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001192 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001193 count = priv->pending_tx_pkts;
1194 if (count)
1195 priv->tx_wait = &tx_wait;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001196 spin_unlock_bh(&priv->tx_lock);
1197
1198 if (count) {
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001199 struct mwl8k_txq_info txinfo[MWL8K_TX_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001200 int index;
1201 int newcount;
1202
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001203 timeout = wait_for_completion_timeout(&tx_wait,
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001204 msecs_to_jiffies(5000));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205 if (timeout)
1206 return 0;
1207
1208 spin_lock_bh(&priv->tx_lock);
1209 priv->tx_wait = NULL;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001210 newcount = priv->pending_tx_pkts;
1211 mwl8k_scan_tx_ring(priv, txinfo);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001212 spin_unlock_bh(&priv->tx_lock);
1213
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001214 printk(KERN_ERR "%s(%u) TIMEDOUT:5000ms Pend:%u-->%u\n",
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001215 __func__, __LINE__, count, newcount);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001216
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001217 for (index = 0; index < MWL8K_TX_QUEUES; index++)
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001218 printk(KERN_ERR "TXQ:%u L:%u H:%u T:%u FW:%u "
1219 "DRV:%u U:%u\n",
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001220 index,
1221 txinfo[index].len,
1222 txinfo[index].head,
1223 txinfo[index].tail,
1224 txinfo[index].fw_owned,
1225 txinfo[index].drv_owned,
1226 txinfo[index].unused);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001227
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001228 return -ETIMEDOUT;
1229 }
1230
1231 return 0;
1232}
1233
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001234#define MWL8K_TXD_SUCCESS(status) \
1235 ((status) & (MWL8K_TXD_STATUS_OK | \
1236 MWL8K_TXD_STATUS_OK_RETRY | \
1237 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001238
1239static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1240{
1241 struct mwl8k_priv *priv = hw->priv;
1242 struct mwl8k_tx_queue *txq = priv->txq + index;
1243 int wake = 0;
1244
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001245 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001246 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001247 struct mwl8k_tx_desc *tx_desc;
1248 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001249 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001250 struct sk_buff *skb;
1251 struct ieee80211_tx_info *info;
1252 u32 status;
1253
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001254 tx = txq->head;
1255 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001256
1257 status = le32_to_cpu(tx_desc->status);
1258
1259 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1260 if (!force)
1261 break;
1262 tx_desc->status &=
1263 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1264 }
1265
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001266 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1267 BUG_ON(txq->stats.len == 0);
1268 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001269 priv->pending_tx_pkts--;
1270
1271 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001272 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001273 skb = txq->skb[tx];
1274 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001275
1276 BUG_ON(skb == NULL);
1277 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1278
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001279 mwl8k_remove_dma_header(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001280
1281 /* Mark descriptor as unused */
1282 tx_desc->pkt_phys_addr = 0;
1283 tx_desc->pkt_len = 0;
1284
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001285 info = IEEE80211_SKB_CB(skb);
1286 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001287 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001288 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001289
1290 ieee80211_tx_status_irqsafe(hw, skb);
1291
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001292 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001293 }
1294
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001295 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001296 ieee80211_wake_queue(hw, index);
1297}
1298
1299/* must be called only when the card's transmit is completely halted */
1300static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1301{
1302 struct mwl8k_priv *priv = hw->priv;
1303 struct mwl8k_tx_queue *txq = priv->txq + index;
1304
1305 mwl8k_txq_reclaim(hw, index, 1);
1306
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001307 kfree(txq->skb);
1308 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001309
1310 pci_free_consistent(priv->pdev,
1311 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001312 txq->txd, txq->txd_dma);
1313 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001314}
1315
1316static int
1317mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1318{
1319 struct mwl8k_priv *priv = hw->priv;
1320 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001321 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001322 struct ieee80211_hdr *wh;
1323 struct mwl8k_tx_queue *txq;
1324 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001325 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001326 u32 txstatus;
1327 u8 txdatarate;
1328 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001329
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001330 wh = (struct ieee80211_hdr *)skb->data;
1331 if (ieee80211_is_data_qos(wh->frame_control))
1332 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1333 else
1334 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001335
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001336 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001337 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001338
1339 tx_info = IEEE80211_SKB_CB(skb);
1340 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341
1342 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1343 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001344
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1346 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1347 mwl8k_vif->seqno = seqno++ % 4096;
1348 }
1349
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001350 /* Setup firmware control bit fields for each frame type. */
1351 txstatus = 0;
1352 txdatarate = 0;
1353 if (ieee80211_is_mgmt(wh->frame_control) ||
1354 ieee80211_is_ctl(wh->frame_control)) {
1355 txdatarate = 0;
1356 qos = mwl8k_qos_setbit_eosp(qos);
1357 /* Set Queue size to unspecified */
1358 qos = mwl8k_qos_setbit_qlen(qos, 0xff);
1359 } else if (ieee80211_is_data(wh->frame_control)) {
1360 txdatarate = 1;
1361 if (is_multicast_ether_addr(wh->addr1))
1362 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1363
1364 /* Send pkt in an aggregate if AMPDU frame. */
1365 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1366 qos = mwl8k_qos_setbit_ack(qos,
1367 MWL8K_TXD_ACK_POLICY_BLOCKACK);
1368 else
1369 qos = mwl8k_qos_setbit_ack(qos,
1370 MWL8K_TXD_ACK_POLICY_NORMAL);
1371
1372 if (qos & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
1373 qos = mwl8k_qos_setbit_amsdu(qos);
1374 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001375
1376 dma = pci_map_single(priv->pdev, skb->data,
1377 skb->len, PCI_DMA_TODEVICE);
1378
1379 if (pci_dma_mapping_error(priv->pdev, dma)) {
1380 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001381 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001382 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001383 return NETDEV_TX_OK;
1384 }
1385
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001386 spin_lock_bh(&priv->tx_lock);
1387
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001388 txq = priv->txq + index;
1389
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001390 BUG_ON(txq->skb[txq->tail] != NULL);
1391 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001392
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001393 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001394 tx->data_rate = txdatarate;
1395 tx->tx_priority = index;
1396 tx->qos_control = cpu_to_le16(qos);
1397 tx->pkt_phys_addr = cpu_to_le32(dma);
1398 tx->pkt_len = cpu_to_le16(skb->len);
1399 tx->rate_info = 0;
1400 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001401 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001402 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1403
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001404 txq->stats.count++;
1405 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001406 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001407
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001408 txq->tail++;
1409 if (txq->tail == MWL8K_TX_DESCS)
1410 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001411
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001412 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001413 ieee80211_stop_queue(hw, index);
1414
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001415 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001416
1417 spin_unlock_bh(&priv->tx_lock);
1418
1419 return NETDEV_TX_OK;
1420}
1421
1422
1423/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001424 * Firmware access.
1425 *
1426 * We have the following requirements for issuing firmware commands:
1427 * - Some commands require that the packet transmit path is idle when
1428 * the command is issued. (For simplicity, we'll just quiesce the
1429 * transmit path for every command.)
1430 * - There are certain sequences of commands that need to be issued to
1431 * the hardware sequentially, with no other intervening commands.
1432 *
1433 * This leads to an implementation of a "firmware lock" as a mutex that
1434 * can be taken recursively, and which is taken by both the low-level
1435 * command submission function (mwl8k_post_cmd) as well as any users of
1436 * that function that require issuing of an atomic sequence of commands,
1437 * and quiesces the transmit path whenever it's taken.
1438 */
1439static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1440{
1441 struct mwl8k_priv *priv = hw->priv;
1442
1443 if (priv->fw_mutex_owner != current) {
1444 int rc;
1445
1446 mutex_lock(&priv->fw_mutex);
1447 ieee80211_stop_queues(hw);
1448
1449 rc = mwl8k_tx_wait_empty(hw);
1450 if (rc) {
1451 ieee80211_wake_queues(hw);
1452 mutex_unlock(&priv->fw_mutex);
1453
1454 return rc;
1455 }
1456
1457 priv->fw_mutex_owner = current;
1458 }
1459
1460 priv->fw_mutex_depth++;
1461
1462 return 0;
1463}
1464
1465static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1466{
1467 struct mwl8k_priv *priv = hw->priv;
1468
1469 if (!--priv->fw_mutex_depth) {
1470 ieee80211_wake_queues(hw);
1471 priv->fw_mutex_owner = NULL;
1472 mutex_unlock(&priv->fw_mutex);
1473 }
1474}
1475
1476
1477/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001478 * Command processing.
1479 */
1480
1481/* Timeout firmware commands after 2000ms */
1482#define MWL8K_CMD_TIMEOUT_MS 2000
1483
1484static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1485{
1486 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1487 struct mwl8k_priv *priv = hw->priv;
1488 void __iomem *regs = priv->regs;
1489 dma_addr_t dma_addr;
1490 unsigned int dma_size;
1491 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001492 unsigned long timeout = 0;
1493 u8 buf[32];
1494
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001495 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496 dma_size = le16_to_cpu(cmd->length);
1497 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1498 PCI_DMA_BIDIRECTIONAL);
1499 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1500 return -ENOMEM;
1501
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001502 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001503 if (rc) {
1504 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1505 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001506 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001507 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001508
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001509 priv->hostcmd_wait = &cmd_wait;
1510 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1511 iowrite32(MWL8K_H2A_INT_DOORBELL,
1512 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1513 iowrite32(MWL8K_H2A_INT_DUMMY,
1514 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001515
1516 timeout = wait_for_completion_timeout(&cmd_wait,
1517 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1518
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001519 priv->hostcmd_wait = NULL;
1520
1521 mwl8k_fw_unlock(hw);
1522
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001523 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1524 PCI_DMA_BIDIRECTIONAL);
1525
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001526 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001527 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001528 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001529 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1530 MWL8K_CMD_TIMEOUT_MS);
1531 rc = -ETIMEDOUT;
1532 } else {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001533 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001534 if (rc)
1535 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001536 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001537 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001538 le16_to_cpu(cmd->result));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001539 }
1540
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001541 return rc;
1542}
1543
1544/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001545 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001546 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001547struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001548 struct mwl8k_cmd_pkt header;
1549 __u8 hw_rev;
1550 __u8 host_interface;
1551 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001552 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001553 __le16 region_code;
1554 __le32 fw_rev;
1555 __le32 ps_cookie;
1556 __le32 caps;
1557 __u8 mcs_bitmap[16];
1558 __le32 rx_queue_ptr;
1559 __le32 num_tx_queues;
1560 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1561 __le32 caps2;
1562 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001563 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001564} __attribute__((packed));
1565
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001566static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001567{
1568 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001569 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001570 int rc;
1571 int i;
1572
1573 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1574 if (cmd == NULL)
1575 return -ENOMEM;
1576
1577 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1578 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1579
1580 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1581 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001582 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001583 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001584 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001585 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001586 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001587 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001588
1589 rc = mwl8k_post_cmd(hw, &cmd->header);
1590
1591 if (!rc) {
1592 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1593 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001594 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001595 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001596 }
1597
1598 kfree(cmd);
1599 return rc;
1600}
1601
1602/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001603 * CMD_GET_HW_SPEC (AP version).
1604 */
1605struct mwl8k_cmd_get_hw_spec_ap {
1606 struct mwl8k_cmd_pkt header;
1607 __u8 hw_rev;
1608 __u8 host_interface;
1609 __le16 num_wcb;
1610 __le16 num_mcaddrs;
1611 __u8 perm_addr[ETH_ALEN];
1612 __le16 region_code;
1613 __le16 num_antenna;
1614 __le32 fw_rev;
1615 __le32 wcbbase0;
1616 __le32 rxwrptr;
1617 __le32 rxrdptr;
1618 __le32 ps_cookie;
1619 __le32 wcbbase1;
1620 __le32 wcbbase2;
1621 __le32 wcbbase3;
1622} __attribute__((packed));
1623
1624static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1625{
1626 struct mwl8k_priv *priv = hw->priv;
1627 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1628 int rc;
1629
1630 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1631 if (cmd == NULL)
1632 return -ENOMEM;
1633
1634 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1635 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1636
1637 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1638 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1639
1640 rc = mwl8k_post_cmd(hw, &cmd->header);
1641
1642 if (!rc) {
1643 int off;
1644
1645 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1646 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1647 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1648 priv->hw_rev = cmd->hw_rev;
1649
1650 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1651 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1652
1653 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1654 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1655
1656 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1657 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1658
1659 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1660 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1661
1662 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1663 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1664
1665 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1666 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1667 }
1668
1669 kfree(cmd);
1670 return rc;
1671}
1672
1673/*
1674 * CMD_SET_HW_SPEC.
1675 */
1676struct mwl8k_cmd_set_hw_spec {
1677 struct mwl8k_cmd_pkt header;
1678 __u8 hw_rev;
1679 __u8 host_interface;
1680 __le16 num_mcaddrs;
1681 __u8 perm_addr[ETH_ALEN];
1682 __le16 region_code;
1683 __le32 fw_rev;
1684 __le32 ps_cookie;
1685 __le32 caps;
1686 __le32 rx_queue_ptr;
1687 __le32 num_tx_queues;
1688 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1689 __le32 flags;
1690 __le32 num_tx_desc_per_queue;
1691 __le32 total_rxd;
1692} __attribute__((packed));
1693
1694#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1695
1696static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1697{
1698 struct mwl8k_priv *priv = hw->priv;
1699 struct mwl8k_cmd_set_hw_spec *cmd;
1700 int rc;
1701 int i;
1702
1703 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1704 if (cmd == NULL)
1705 return -ENOMEM;
1706
1707 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1708 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1709
1710 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1711 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1712 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1713 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1714 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1715 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1716 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1717 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1718
1719 rc = mwl8k_post_cmd(hw, &cmd->header);
1720 kfree(cmd);
1721
1722 return rc;
1723}
1724
1725/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001726 * CMD_MAC_MULTICAST_ADR.
1727 */
1728struct mwl8k_cmd_mac_multicast_adr {
1729 struct mwl8k_cmd_pkt header;
1730 __le16 action;
1731 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001732 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001733};
1734
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001735#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1736#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1737#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1738#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001739
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001740static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001741__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001742 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001743{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001744 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001745 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001746 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001747
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001748 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001749 allmulti = 1;
1750 mc_count = 0;
1751 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001752
1753 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1754
1755 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001756 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001757 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001758
1759 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1760 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001761 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1762 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001763
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001764 if (allmulti) {
1765 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1766 } else if (mc_count) {
1767 int i;
1768
1769 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1770 cmd->numaddr = cpu_to_le16(mc_count);
1771 for (i = 0; i < mc_count && mclist; i++) {
1772 if (mclist->da_addrlen != ETH_ALEN) {
1773 kfree(cmd);
1774 return NULL;
1775 }
1776 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1777 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001778 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001779 }
1780
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001781 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001782}
1783
1784/*
1785 * CMD_802_11_GET_STAT.
1786 */
1787struct mwl8k_cmd_802_11_get_stat {
1788 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001789 __le32 stats[64];
1790} __attribute__((packed));
1791
1792#define MWL8K_STAT_ACK_FAILURE 9
1793#define MWL8K_STAT_RTS_FAILURE 12
1794#define MWL8K_STAT_FCS_ERROR 24
1795#define MWL8K_STAT_RTS_SUCCESS 11
1796
1797static int mwl8k_cmd_802_11_get_stat(struct ieee80211_hw *hw,
1798 struct ieee80211_low_level_stats *stats)
1799{
1800 struct mwl8k_cmd_802_11_get_stat *cmd;
1801 int rc;
1802
1803 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1804 if (cmd == NULL)
1805 return -ENOMEM;
1806
1807 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1808 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001809
1810 rc = mwl8k_post_cmd(hw, &cmd->header);
1811 if (!rc) {
1812 stats->dot11ACKFailureCount =
1813 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1814 stats->dot11RTSFailureCount =
1815 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1816 stats->dot11FCSErrorCount =
1817 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1818 stats->dot11RTSSuccessCount =
1819 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1820 }
1821 kfree(cmd);
1822
1823 return rc;
1824}
1825
1826/*
1827 * CMD_802_11_RADIO_CONTROL.
1828 */
1829struct mwl8k_cmd_802_11_radio_control {
1830 struct mwl8k_cmd_pkt header;
1831 __le16 action;
1832 __le16 control;
1833 __le16 radio_on;
1834} __attribute__((packed));
1835
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001836static int
1837mwl8k_cmd_802_11_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001838{
1839 struct mwl8k_priv *priv = hw->priv;
1840 struct mwl8k_cmd_802_11_radio_control *cmd;
1841 int rc;
1842
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001843 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001844 return 0;
1845
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001846 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1847 if (cmd == NULL)
1848 return -ENOMEM;
1849
1850 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1851 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1852 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001853 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001854 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1855
1856 rc = mwl8k_post_cmd(hw, &cmd->header);
1857 kfree(cmd);
1858
1859 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001860 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001861
1862 return rc;
1863}
1864
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001865static int mwl8k_cmd_802_11_radio_disable(struct ieee80211_hw *hw)
1866{
1867 return mwl8k_cmd_802_11_radio_control(hw, 0, 0);
1868}
1869
1870static int mwl8k_cmd_802_11_radio_enable(struct ieee80211_hw *hw)
1871{
1872 return mwl8k_cmd_802_11_radio_control(hw, 1, 0);
1873}
1874
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001875static int
1876mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1877{
1878 struct mwl8k_priv *priv;
1879
1880 if (hw == NULL || hw->priv == NULL)
1881 return -EINVAL;
1882 priv = hw->priv;
1883
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001884 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001885
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001886 return mwl8k_cmd_802_11_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001887}
1888
1889/*
1890 * CMD_802_11_RF_TX_POWER.
1891 */
1892#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1893
1894struct mwl8k_cmd_802_11_rf_tx_power {
1895 struct mwl8k_cmd_pkt header;
1896 __le16 action;
1897 __le16 support_level;
1898 __le16 current_level;
1899 __le16 reserved;
1900 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1901} __attribute__((packed));
1902
1903static int mwl8k_cmd_802_11_rf_tx_power(struct ieee80211_hw *hw, int dBm)
1904{
1905 struct mwl8k_cmd_802_11_rf_tx_power *cmd;
1906 int rc;
1907
1908 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1909 if (cmd == NULL)
1910 return -ENOMEM;
1911
1912 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1913 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1914 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1915 cmd->support_level = cpu_to_le16(dBm);
1916
1917 rc = mwl8k_post_cmd(hw, &cmd->header);
1918 kfree(cmd);
1919
1920 return rc;
1921}
1922
1923/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02001924 * CMD_RF_ANTENNA.
1925 */
1926struct mwl8k_cmd_rf_antenna {
1927 struct mwl8k_cmd_pkt header;
1928 __le16 antenna;
1929 __le16 mode;
1930} __attribute__((packed));
1931
1932#define MWL8K_RF_ANTENNA_RX 1
1933#define MWL8K_RF_ANTENNA_TX 2
1934
1935static int
1936mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
1937{
1938 struct mwl8k_cmd_rf_antenna *cmd;
1939 int rc;
1940
1941 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1942 if (cmd == NULL)
1943 return -ENOMEM;
1944
1945 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
1946 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1947 cmd->antenna = cpu_to_le16(antenna);
1948 cmd->mode = cpu_to_le16(mask);
1949
1950 rc = mwl8k_post_cmd(hw, &cmd->header);
1951 kfree(cmd);
1952
1953 return rc;
1954}
1955
1956/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001957 * CMD_SET_PRE_SCAN.
1958 */
1959struct mwl8k_cmd_set_pre_scan {
1960 struct mwl8k_cmd_pkt header;
1961} __attribute__((packed));
1962
1963static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
1964{
1965 struct mwl8k_cmd_set_pre_scan *cmd;
1966 int rc;
1967
1968 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1969 if (cmd == NULL)
1970 return -ENOMEM;
1971
1972 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
1973 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1974
1975 rc = mwl8k_post_cmd(hw, &cmd->header);
1976 kfree(cmd);
1977
1978 return rc;
1979}
1980
1981/*
1982 * CMD_SET_POST_SCAN.
1983 */
1984struct mwl8k_cmd_set_post_scan {
1985 struct mwl8k_cmd_pkt header;
1986 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001987 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001988} __attribute__((packed));
1989
1990static int
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001991mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001992{
1993 struct mwl8k_cmd_set_post_scan *cmd;
1994 int rc;
1995
1996 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1997 if (cmd == NULL)
1998 return -ENOMEM;
1999
2000 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2001 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2002 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002003 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002004
2005 rc = mwl8k_post_cmd(hw, &cmd->header);
2006 kfree(cmd);
2007
2008 return rc;
2009}
2010
2011/*
2012 * CMD_SET_RF_CHANNEL.
2013 */
2014struct mwl8k_cmd_set_rf_channel {
2015 struct mwl8k_cmd_pkt header;
2016 __le16 action;
2017 __u8 current_channel;
2018 __le32 channel_flags;
2019} __attribute__((packed));
2020
2021static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2022 struct ieee80211_channel *channel)
2023{
2024 struct mwl8k_cmd_set_rf_channel *cmd;
2025 int rc;
2026
2027 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2028 if (cmd == NULL)
2029 return -ENOMEM;
2030
2031 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2032 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2033 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2034 cmd->current_channel = channel->hw_value;
2035 if (channel->band == IEEE80211_BAND_2GHZ)
2036 cmd->channel_flags = cpu_to_le32(0x00000081);
2037 else
2038 cmd->channel_flags = cpu_to_le32(0x00000000);
2039
2040 rc = mwl8k_post_cmd(hw, &cmd->header);
2041 kfree(cmd);
2042
2043 return rc;
2044}
2045
2046/*
2047 * CMD_SET_SLOT.
2048 */
2049struct mwl8k_cmd_set_slot {
2050 struct mwl8k_cmd_pkt header;
2051 __le16 action;
2052 __u8 short_slot;
2053} __attribute__((packed));
2054
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002055static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002056{
2057 struct mwl8k_cmd_set_slot *cmd;
2058 int rc;
2059
2060 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2061 if (cmd == NULL)
2062 return -ENOMEM;
2063
2064 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2065 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2066 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002067 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002068
2069 rc = mwl8k_post_cmd(hw, &cmd->header);
2070 kfree(cmd);
2071
2072 return rc;
2073}
2074
2075/*
2076 * CMD_MIMO_CONFIG.
2077 */
2078struct mwl8k_cmd_mimo_config {
2079 struct mwl8k_cmd_pkt header;
2080 __le32 action;
2081 __u8 rx_antenna_map;
2082 __u8 tx_antenna_map;
2083} __attribute__((packed));
2084
2085static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
2086{
2087 struct mwl8k_cmd_mimo_config *cmd;
2088 int rc;
2089
2090 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2091 if (cmd == NULL)
2092 return -ENOMEM;
2093
2094 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
2095 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2096 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2097 cmd->rx_antenna_map = rx;
2098 cmd->tx_antenna_map = tx;
2099
2100 rc = mwl8k_post_cmd(hw, &cmd->header);
2101 kfree(cmd);
2102
2103 return rc;
2104}
2105
2106/*
2107 * CMD_ENABLE_SNIFFER.
2108 */
2109struct mwl8k_cmd_enable_sniffer {
2110 struct mwl8k_cmd_pkt header;
2111 __le32 action;
2112} __attribute__((packed));
2113
2114static int mwl8k_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2115{
2116 struct mwl8k_cmd_enable_sniffer *cmd;
2117 int rc;
2118
2119 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2120 if (cmd == NULL)
2121 return -ENOMEM;
2122
2123 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2124 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002125 cmd->action = cpu_to_le32(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002126
2127 rc = mwl8k_post_cmd(hw, &cmd->header);
2128 kfree(cmd);
2129
2130 return rc;
2131}
2132
2133/*
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002134 * CMD_SET_MAC_ADDR.
2135 */
2136struct mwl8k_cmd_set_mac_addr {
2137 struct mwl8k_cmd_pkt header;
Lennert Buytenhek259a8e72009-10-22 20:21:40 +02002138 union {
2139 struct {
2140 __le16 mac_type;
2141 __u8 mac_addr[ETH_ALEN];
2142 } mbss;
2143 __u8 mac_addr[ETH_ALEN];
2144 };
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002145} __attribute__((packed));
2146
2147static int mwl8k_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2148{
Lennert Buytenhek259a8e72009-10-22 20:21:40 +02002149 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002150 struct mwl8k_cmd_set_mac_addr *cmd;
2151 int rc;
2152
2153 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2154 if (cmd == NULL)
2155 return -ENOMEM;
2156
2157 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2158 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek259a8e72009-10-22 20:21:40 +02002159 if (priv->ap_fw) {
2160 cmd->mbss.mac_type = 0;
2161 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2162 } else {
2163 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2164 }
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002165
2166 rc = mwl8k_post_cmd(hw, &cmd->header);
2167 kfree(cmd);
2168
2169 return rc;
2170}
2171
2172
2173/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002174 * CMD_SET_RATEADAPT_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002175 */
2176struct mwl8k_cmd_set_rate_adapt_mode {
2177 struct mwl8k_cmd_pkt header;
2178 __le16 action;
2179 __le16 mode;
2180} __attribute__((packed));
2181
2182static int mwl8k_cmd_setrateadaptmode(struct ieee80211_hw *hw, __u16 mode)
2183{
2184 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2185 int rc;
2186
2187 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2188 if (cmd == NULL)
2189 return -ENOMEM;
2190
2191 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2192 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2193 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2194 cmd->mode = cpu_to_le16(mode);
2195
2196 rc = mwl8k_post_cmd(hw, &cmd->header);
2197 kfree(cmd);
2198
2199 return rc;
2200}
2201
2202/*
2203 * CMD_SET_WMM_MODE.
2204 */
2205struct mwl8k_cmd_set_wmm {
2206 struct mwl8k_cmd_pkt header;
2207 __le16 action;
2208} __attribute__((packed));
2209
2210static int mwl8k_set_wmm(struct ieee80211_hw *hw, bool enable)
2211{
2212 struct mwl8k_priv *priv = hw->priv;
2213 struct mwl8k_cmd_set_wmm *cmd;
2214 int rc;
2215
2216 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2217 if (cmd == NULL)
2218 return -ENOMEM;
2219
2220 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
2221 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02002222 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002223
2224 rc = mwl8k_post_cmd(hw, &cmd->header);
2225 kfree(cmd);
2226
2227 if (!rc)
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02002228 priv->wmm_enabled = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002229
2230 return rc;
2231}
2232
2233/*
2234 * CMD_SET_RTS_THRESHOLD.
2235 */
2236struct mwl8k_cmd_rts_threshold {
2237 struct mwl8k_cmd_pkt header;
2238 __le16 action;
2239 __le16 threshold;
2240} __attribute__((packed));
2241
2242static int mwl8k_rts_threshold(struct ieee80211_hw *hw,
Lennert Buytenhek733d3062009-07-17 07:24:15 +02002243 u16 action, u16 threshold)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002244{
2245 struct mwl8k_cmd_rts_threshold *cmd;
2246 int rc;
2247
2248 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2249 if (cmd == NULL)
2250 return -ENOMEM;
2251
2252 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2253 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2254 cmd->action = cpu_to_le16(action);
Lennert Buytenhek733d3062009-07-17 07:24:15 +02002255 cmd->threshold = cpu_to_le16(threshold);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002256
2257 rc = mwl8k_post_cmd(hw, &cmd->header);
2258 kfree(cmd);
2259
2260 return rc;
2261}
2262
2263/*
2264 * CMD_SET_EDCA_PARAMS.
2265 */
2266struct mwl8k_cmd_set_edca_params {
2267 struct mwl8k_cmd_pkt header;
2268
2269 /* See MWL8K_SET_EDCA_XXX below */
2270 __le16 action;
2271
2272 /* TX opportunity in units of 32 us */
2273 __le16 txop;
2274
2275 /* Log exponent of max contention period: 0...15*/
2276 __u8 log_cw_max;
2277
2278 /* Log exponent of min contention period: 0...15 */
2279 __u8 log_cw_min;
2280
2281 /* Adaptive interframe spacing in units of 32us */
2282 __u8 aifs;
2283
2284 /* TX queue to configure */
2285 __u8 txq;
2286} __attribute__((packed));
2287
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002288#define MWL8K_SET_EDCA_CW 0x01
2289#define MWL8K_SET_EDCA_TXOP 0x02
2290#define MWL8K_SET_EDCA_AIFS 0x04
2291
2292#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2293 MWL8K_SET_EDCA_TXOP | \
2294 MWL8K_SET_EDCA_AIFS)
2295
2296static int
2297mwl8k_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2298 __u16 cw_min, __u16 cw_max,
2299 __u8 aifs, __u16 txop)
2300{
2301 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002302 int rc;
2303
2304 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2305 if (cmd == NULL)
2306 return -ENOMEM;
2307
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002308 /*
2309 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2310 * this call.
2311 */
2312 qnum ^= !(qnum >> 1);
2313
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002314 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2315 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002316 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2317 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002318 cmd->log_cw_max = (u8)ilog2(cw_max + 1);
2319 cmd->log_cw_min = (u8)ilog2(cw_min + 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002320 cmd->aifs = aifs;
2321 cmd->txq = qnum;
2322
2323 rc = mwl8k_post_cmd(hw, &cmd->header);
2324 kfree(cmd);
2325
2326 return rc;
2327}
2328
2329/*
2330 * CMD_FINALIZE_JOIN.
2331 */
2332
2333/* FJ beacon buffer size is compiled into the firmware. */
2334#define MWL8K_FJ_BEACON_MAXLEN 128
2335
2336struct mwl8k_cmd_finalize_join {
2337 struct mwl8k_cmd_pkt header;
2338 __le32 sleep_interval; /* Number of beacon periods to sleep */
2339 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2340} __attribute__((packed));
2341
2342static int mwl8k_finalize_join(struct ieee80211_hw *hw, void *frame,
2343 __u16 framelen, __u16 dtim)
2344{
2345 struct mwl8k_cmd_finalize_join *cmd;
2346 struct ieee80211_mgmt *payload = frame;
2347 u16 hdrlen;
2348 u32 payload_len;
2349 int rc;
2350
2351 if (frame == NULL)
2352 return -EINVAL;
2353
2354 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2355 if (cmd == NULL)
2356 return -ENOMEM;
2357
2358 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2359 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002360 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002361
2362 hdrlen = ieee80211_hdrlen(payload->frame_control);
2363
2364 payload_len = framelen > hdrlen ? framelen - hdrlen : 0;
2365
2366 /* XXX TBD Might just have to abort and return an error */
2367 if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2368 printk(KERN_ERR "%s(): WARNING: Incomplete beacon "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002369 "sent to firmware. Sz=%u MAX=%u\n", __func__,
2370 payload_len, MWL8K_FJ_BEACON_MAXLEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002371
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002372 if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2373 payload_len = MWL8K_FJ_BEACON_MAXLEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002374
2375 if (payload && payload_len)
2376 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2377
2378 rc = mwl8k_post_cmd(hw, &cmd->header);
2379 kfree(cmd);
2380 return rc;
2381}
2382
2383/*
2384 * CMD_UPDATE_STADB.
2385 */
2386struct mwl8k_cmd_update_sta_db {
2387 struct mwl8k_cmd_pkt header;
2388
2389 /* See STADB_ACTION_TYPE */
2390 __le32 action;
2391
2392 /* Peer MAC address */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002393 __u8 peer_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002394
2395 __le32 reserved;
2396
2397 /* Peer info - valid during add/update. */
2398 struct peer_capability_info peer_info;
2399} __attribute__((packed));
2400
2401static int mwl8k_cmd_update_sta_db(struct ieee80211_hw *hw,
2402 struct ieee80211_vif *vif, __u32 action)
2403{
2404 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2405 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2406 struct mwl8k_cmd_update_sta_db *cmd;
2407 struct peer_capability_info *peer_info;
2408 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002409 int rc;
2410 __u8 count, *rates;
2411
2412 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2413 if (cmd == NULL)
2414 return -ENOMEM;
2415
2416 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2417 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2418
2419 cmd->action = cpu_to_le32(action);
2420 peer_info = &cmd->peer_info;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002421 memcpy(cmd->peer_addr, mv_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002422
2423 switch (action) {
2424 case MWL8K_STA_DB_ADD_ENTRY:
2425 case MWL8K_STA_DB_MODIFY_ENTRY:
2426 /* Build peer_info block */
2427 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2428 peer_info->basic_caps = cpu_to_le16(info->assoc_capability);
2429 peer_info->interop = 1;
2430 peer_info->amsdu_enabled = 0;
2431
2432 rates = peer_info->legacy_rates;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002433 for (count = 0; count < mv_vif->legacy_nrates; count++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002434 rates[count] = bitrates[count].hw_value;
2435
2436 rc = mwl8k_post_cmd(hw, &cmd->header);
2437 if (rc == 0)
2438 mv_vif->peer_id = peer_info->station_id;
2439
2440 break;
2441
2442 case MWL8K_STA_DB_DEL_ENTRY:
2443 case MWL8K_STA_DB_FLUSH:
2444 default:
2445 rc = mwl8k_post_cmd(hw, &cmd->header);
2446 if (rc == 0)
2447 mv_vif->peer_id = 0;
2448 break;
2449 }
2450 kfree(cmd);
2451
2452 return rc;
2453}
2454
2455/*
2456 * CMD_SET_AID.
2457 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002458#define MWL8K_RATE_INDEX_MAX_ARRAY 14
2459
2460#define MWL8K_FRAME_PROT_DISABLED 0x00
2461#define MWL8K_FRAME_PROT_11G 0x07
2462#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2463#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002464
2465struct mwl8k_cmd_update_set_aid {
2466 struct mwl8k_cmd_pkt header;
2467 __le16 aid;
2468
2469 /* AP's MAC address (BSSID) */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002470 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002471 __le16 protection_mode;
2472 __u8 supp_rates[MWL8K_RATE_INDEX_MAX_ARRAY];
2473} __attribute__((packed));
2474
2475static int mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2476 struct ieee80211_vif *vif)
2477{
2478 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2479 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2480 struct mwl8k_cmd_update_set_aid *cmd;
2481 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
2482 int count;
2483 u16 prot_mode;
2484 int rc;
2485
2486 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2487 if (cmd == NULL)
2488 return -ENOMEM;
2489
2490 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2491 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2492 cmd->aid = cpu_to_le16(info->aid);
2493
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002494 memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002495
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002496 if (info->use_cts_prot) {
2497 prot_mode = MWL8K_FRAME_PROT_11G;
2498 } else {
Johannes Berg9ed6bcc2009-05-08 20:47:39 +02002499 switch (info->ht_operation_mode &
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002500 IEEE80211_HT_OP_MODE_PROTECTION) {
2501 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2502 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2503 break;
2504 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2505 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2506 break;
2507 default:
2508 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2509 break;
2510 }
2511 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002512 cmd->protection_mode = cpu_to_le16(prot_mode);
2513
2514 for (count = 0; count < mv_vif->legacy_nrates; count++)
2515 cmd->supp_rates[count] = bitrates[count].hw_value;
2516
2517 rc = mwl8k_post_cmd(hw, &cmd->header);
2518 kfree(cmd);
2519
2520 return rc;
2521}
2522
2523/*
2524 * CMD_SET_RATE.
2525 */
2526struct mwl8k_cmd_update_rateset {
2527 struct mwl8k_cmd_pkt header;
2528 __u8 legacy_rates[MWL8K_RATE_INDEX_MAX_ARRAY];
2529
2530 /* Bitmap for supported MCS codes. */
2531 __u8 mcs_set[MWL8K_IEEE_LEGACY_DATA_RATES];
2532 __u8 reserved[MWL8K_IEEE_LEGACY_DATA_RATES];
2533} __attribute__((packed));
2534
2535static int mwl8k_update_rateset(struct ieee80211_hw *hw,
2536 struct ieee80211_vif *vif)
2537{
2538 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2539 struct mwl8k_cmd_update_rateset *cmd;
2540 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
2541 int count;
2542 int rc;
2543
2544 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2545 if (cmd == NULL)
2546 return -ENOMEM;
2547
2548 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2549 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2550
2551 for (count = 0; count < mv_vif->legacy_nrates; count++)
2552 cmd->legacy_rates[count] = bitrates[count].hw_value;
2553
2554 rc = mwl8k_post_cmd(hw, &cmd->header);
2555 kfree(cmd);
2556
2557 return rc;
2558}
2559
2560/*
2561 * CMD_USE_FIXED_RATE.
2562 */
2563#define MWL8K_RATE_TABLE_SIZE 8
2564#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002565#define MWL8K_USE_AUTO_RATE 0x0002
2566
2567struct mwl8k_rate_entry {
2568 /* Set to 1 if HT rate, 0 if legacy. */
2569 __le32 is_ht_rate;
2570
2571 /* Set to 1 to use retry_count field. */
2572 __le32 enable_retry;
2573
2574 /* Specified legacy rate or MCS. */
2575 __le32 rate;
2576
2577 /* Number of allowed retries. */
2578 __le32 retry_count;
2579} __attribute__((packed));
2580
2581struct mwl8k_rate_table {
2582 /* 1 to allow specified rate and below */
2583 __le32 allow_rate_drop;
2584 __le32 num_rates;
2585 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2586} __attribute__((packed));
2587
2588struct mwl8k_cmd_use_fixed_rate {
2589 struct mwl8k_cmd_pkt header;
2590 __le32 action;
2591 struct mwl8k_rate_table rate_table;
2592
2593 /* Unicast, Broadcast or Multicast */
2594 __le32 rate_type;
2595 __le32 reserved1;
2596 __le32 reserved2;
2597} __attribute__((packed));
2598
2599static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2600 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2601{
2602 struct mwl8k_cmd_use_fixed_rate *cmd;
2603 int count;
2604 int rc;
2605
2606 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2607 if (cmd == NULL)
2608 return -ENOMEM;
2609
2610 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2611 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2612
2613 cmd->action = cpu_to_le32(action);
2614 cmd->rate_type = cpu_to_le32(rate_type);
2615
2616 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002617 /*
2618 * Copy over each field manually so that endian
2619 * conversion can be done.
2620 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002621 cmd->rate_table.allow_rate_drop =
2622 cpu_to_le32(rate_table->allow_rate_drop);
2623 cmd->rate_table.num_rates =
2624 cpu_to_le32(rate_table->num_rates);
2625
2626 for (count = 0; count < rate_table->num_rates; count++) {
2627 struct mwl8k_rate_entry *dst =
2628 &cmd->rate_table.rate_entry[count];
2629 struct mwl8k_rate_entry *src =
2630 &rate_table->rate_entry[count];
2631
2632 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2633 dst->enable_retry = cpu_to_le32(src->enable_retry);
2634 dst->rate = cpu_to_le32(src->rate);
2635 dst->retry_count = cpu_to_le32(src->retry_count);
2636 }
2637 }
2638
2639 rc = mwl8k_post_cmd(hw, &cmd->header);
2640 kfree(cmd);
2641
2642 return rc;
2643}
2644
2645
2646/*
2647 * Interrupt handling.
2648 */
2649static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2650{
2651 struct ieee80211_hw *hw = dev_id;
2652 struct mwl8k_priv *priv = hw->priv;
2653 u32 status;
2654
2655 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2656 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2657
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002658 if (!status)
2659 return IRQ_NONE;
2660
2661 if (status & MWL8K_A2H_INT_TX_DONE)
2662 tasklet_schedule(&priv->tx_reclaim_task);
2663
2664 if (status & MWL8K_A2H_INT_RX_READY) {
2665 while (rxq_process(hw, 0, 1))
2666 rxq_refill(hw, 0, 1);
2667 }
2668
2669 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002670 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002671 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002672 }
2673
2674 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002675 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002676 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002677 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002678 }
2679
2680 return IRQ_HANDLED;
2681}
2682
2683
2684/*
2685 * Core driver operations.
2686 */
2687static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2688{
2689 struct mwl8k_priv *priv = hw->priv;
2690 int index = skb_get_queue_mapping(skb);
2691 int rc;
2692
2693 if (priv->current_channel == NULL) {
2694 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002695 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002696 dev_kfree_skb(skb);
2697 return NETDEV_TX_OK;
2698 }
2699
2700 rc = mwl8k_txq_xmit(hw, index, skb);
2701
2702 return rc;
2703}
2704
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002705static int mwl8k_start(struct ieee80211_hw *hw)
2706{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002707 struct mwl8k_priv *priv = hw->priv;
2708 int rc;
2709
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002710 rc = request_irq(priv->pdev->irq, &mwl8k_interrupt,
2711 IRQF_SHARED, MWL8K_NAME, hw);
2712 if (rc) {
2713 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002714 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002715 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002716 }
2717
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002718 /* Enable tx reclaim tasklet */
2719 tasklet_enable(&priv->tx_reclaim_task);
2720
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002721 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002722 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002723
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002724 rc = mwl8k_fw_lock(hw);
2725 if (!rc) {
2726 rc = mwl8k_cmd_802_11_radio_enable(hw);
2727
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002728 if (!priv->ap_fw) {
2729 if (!rc)
2730 rc = mwl8k_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002731
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002732 if (!rc)
2733 rc = mwl8k_cmd_set_pre_scan(hw);
2734
2735 if (!rc)
2736 rc = mwl8k_cmd_set_post_scan(hw,
2737 "\x00\x00\x00\x00\x00\x00");
2738 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002739
2740 if (!rc)
2741 rc = mwl8k_cmd_setrateadaptmode(hw, 0);
2742
2743 if (!rc)
2744 rc = mwl8k_set_wmm(hw, 0);
2745
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002746 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002747 }
2748
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002749 if (rc) {
2750 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2751 free_irq(priv->pdev->irq, hw);
2752 tasklet_disable(&priv->tx_reclaim_task);
2753 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002754
2755 return rc;
2756}
2757
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002758static void mwl8k_stop(struct ieee80211_hw *hw)
2759{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002760 struct mwl8k_priv *priv = hw->priv;
2761 int i;
2762
Lennert Buytenhekd3cea0b2009-07-17 07:15:49 +02002763 mwl8k_cmd_802_11_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002764
2765 ieee80211_stop_queues(hw);
2766
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002767 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002768 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002769 free_irq(priv->pdev->irq, hw);
2770
2771 /* Stop finalize join worker */
2772 cancel_work_sync(&priv->finalize_join_worker);
2773 if (priv->beacon_skb != NULL)
2774 dev_kfree_skb(priv->beacon_skb);
2775
2776 /* Stop tx reclaim tasklet */
2777 tasklet_disable(&priv->tx_reclaim_task);
2778
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002779 /* Return all skbs to mac80211 */
2780 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2781 mwl8k_txq_reclaim(hw, i, 1);
2782}
2783
2784static int mwl8k_add_interface(struct ieee80211_hw *hw,
2785 struct ieee80211_if_init_conf *conf)
2786{
2787 struct mwl8k_priv *priv = hw->priv;
2788 struct mwl8k_vif *mwl8k_vif;
2789
2790 /*
2791 * We only support one active interface at a time.
2792 */
2793 if (priv->vif != NULL)
2794 return -EBUSY;
2795
2796 /*
2797 * We only support managed interfaces for now.
2798 */
Lennert Buytenhek240e86e2009-07-16 14:15:44 +02002799 if (conf->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002800 return -EINVAL;
2801
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002802 /*
2803 * Reject interface creation if sniffer mode is active, as
2804 * STA operation is mutually exclusive with hardware sniffer
2805 * mode.
2806 */
2807 if (priv->sniffer_enabled) {
2808 printk(KERN_INFO "%s: unable to create STA "
2809 "interface due to sniffer mode being enabled\n",
2810 wiphy_name(hw->wiphy));
2811 return -EINVAL;
2812 }
2813
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002814 /* Clean out driver private area */
2815 mwl8k_vif = MWL8K_VIF(conf->vif);
2816 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2817
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002818 /* Set and save the mac address */
2819 mwl8k_set_mac_addr(hw, conf->mac_addr);
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002820 memcpy(mwl8k_vif->mac_addr, conf->mac_addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002821
2822 /* Back pointer to parent config block */
2823 mwl8k_vif->priv = priv;
2824
2825 /* Setup initial PHY parameters */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002826 memcpy(mwl8k_vif->legacy_rates,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002827 priv->rates, sizeof(mwl8k_vif->legacy_rates));
2828 mwl8k_vif->legacy_nrates = ARRAY_SIZE(priv->rates);
2829
2830 /* Set Initial sequence number to zero */
2831 mwl8k_vif->seqno = 0;
2832
2833 priv->vif = conf->vif;
2834 priv->current_channel = NULL;
2835
2836 return 0;
2837}
2838
2839static void mwl8k_remove_interface(struct ieee80211_hw *hw,
2840 struct ieee80211_if_init_conf *conf)
2841{
2842 struct mwl8k_priv *priv = hw->priv;
2843
2844 if (priv->vif == NULL)
2845 return;
2846
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002847 mwl8k_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
2848
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002849 priv->vif = NULL;
2850}
2851
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002852static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002853{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002854 struct ieee80211_conf *conf = &hw->conf;
2855 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002856 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002857
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002858 if (conf->flags & IEEE80211_CONF_IDLE) {
2859 mwl8k_cmd_802_11_radio_disable(hw);
2860 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002861 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002862 }
2863
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002864 rc = mwl8k_fw_lock(hw);
2865 if (rc)
2866 return rc;
2867
2868 rc = mwl8k_cmd_802_11_radio_enable(hw);
2869 if (rc)
2870 goto out;
2871
2872 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2873 if (rc)
2874 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002875
2876 priv->current_channel = conf->channel;
2877
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002878 if (conf->power_level > 18)
2879 conf->power_level = 18;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002880 rc = mwl8k_cmd_802_11_rf_tx_power(hw, conf->power_level);
2881 if (rc)
2882 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002883
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002884 if (priv->ap_fw) {
2885 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2886 if (!rc)
2887 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2888 } else {
2889 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2890 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002891
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002892out:
2893 mwl8k_fw_unlock(hw);
2894
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002895 return rc;
2896}
2897
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002898static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2899 struct ieee80211_vif *vif,
2900 struct ieee80211_bss_conf *info,
2901 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002902{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002903 struct mwl8k_priv *priv = hw->priv;
2904 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002905 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002906
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002907 if (changed & BSS_CHANGED_BSSID)
2908 memcpy(mwl8k_vif->bssid, info->bssid, ETH_ALEN);
2909
2910 if ((changed & BSS_CHANGED_ASSOC) == 0)
2911 return;
2912
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002913 priv->capture_beacon = false;
2914
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002915 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002916 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002917 return;
2918
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002919 if (info->assoc) {
2920 memcpy(&mwl8k_vif->bss_info, info,
2921 sizeof(struct ieee80211_bss_conf));
2922
2923 /* Install rates */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002924 rc = mwl8k_update_rateset(hw, vif);
2925 if (rc)
2926 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002927
2928 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002929 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
2930 MWL8K_UCAST_RATE, NULL);
2931 if (rc)
2932 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002933
2934 /* Set radio preamble */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002935 rc = mwl8k_set_radio_preamble(hw, info->use_short_preamble);
2936 if (rc)
2937 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002938
2939 /* Set slot time */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002940 rc = mwl8k_cmd_set_slot(hw, info->use_short_slot);
2941 if (rc)
2942 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002943
2944 /* Update peer rate info */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002945 rc = mwl8k_cmd_update_sta_db(hw, vif,
2946 MWL8K_STA_DB_MODIFY_ENTRY);
2947 if (rc)
2948 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002949
2950 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002951 rc = mwl8k_cmd_set_aid(hw, vif);
2952 if (rc)
2953 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002954
2955 /*
2956 * Finalize the join. Tell rx handler to process
2957 * next beacon from our BSSID.
2958 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002959 memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002960 priv->capture_beacon = true;
2961 } else {
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002962 rc = mwl8k_cmd_update_sta_db(hw, vif, MWL8K_STA_DB_DEL_ENTRY);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002963 memset(&mwl8k_vif->bss_info, 0,
2964 sizeof(struct ieee80211_bss_conf));
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002965 memset(mwl8k_vif->bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002966 }
2967
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002968out:
2969 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002970}
2971
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002972static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
2973 int mc_count, struct dev_addr_list *mclist)
2974{
2975 struct mwl8k_cmd_pkt *cmd;
2976
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002977 /*
2978 * Synthesize and return a command packet that programs the
2979 * hardware multicast address filter. At this point we don't
2980 * know whether FIF_ALLMULTI is being requested, but if it is,
2981 * we'll end up throwing this packet away and creating a new
2982 * one in mwl8k_configure_filter().
2983 */
2984 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002985
2986 return (unsigned long)cmd;
2987}
2988
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002989static int
2990mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
2991 unsigned int changed_flags,
2992 unsigned int *total_flags)
2993{
2994 struct mwl8k_priv *priv = hw->priv;
2995
2996 /*
2997 * Hardware sniffer mode is mutually exclusive with STA
2998 * operation, so refuse to enable sniffer mode if a STA
2999 * interface is active.
3000 */
3001 if (priv->vif != NULL) {
3002 if (net_ratelimit())
3003 printk(KERN_INFO "%s: not enabling sniffer "
3004 "mode because STA interface is active\n",
3005 wiphy_name(hw->wiphy));
3006 return 0;
3007 }
3008
3009 if (!priv->sniffer_enabled) {
3010 if (mwl8k_enable_sniffer(hw, 1))
3011 return 0;
3012 priv->sniffer_enabled = true;
3013 }
3014
3015 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3016 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3017 FIF_OTHER_BSS;
3018
3019 return 1;
3020}
3021
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003022static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3023 unsigned int changed_flags,
3024 unsigned int *total_flags,
3025 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003026{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003027 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003028 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3029
3030 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003031 * AP firmware doesn't allow fine-grained control over
3032 * the receive filter.
3033 */
3034 if (priv->ap_fw) {
3035 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3036 kfree(cmd);
3037 return;
3038 }
3039
3040 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003041 * Enable hardware sniffer mode if FIF_CONTROL or
3042 * FIF_OTHER_BSS is requested.
3043 */
3044 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3045 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3046 kfree(cmd);
3047 return;
3048 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003049
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003050 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003051 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003052
3053 if (mwl8k_fw_lock(hw))
3054 return;
3055
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003056 if (priv->sniffer_enabled) {
3057 mwl8k_enable_sniffer(hw, 0);
3058 priv->sniffer_enabled = false;
3059 }
3060
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003061 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003062 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3063 /*
3064 * Disable the BSS filter.
3065 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003066 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003067 } else {
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003068 u8 *bssid;
3069
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003070 /*
3071 * Enable the BSS filter.
3072 *
3073 * If there is an active STA interface, use that
3074 * interface's BSSID, otherwise use a dummy one
3075 * (where the OUI part needs to be nonzero for
3076 * the BSSID to be accepted by POST_SCAN).
3077 */
3078 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003079 if (priv->vif != NULL)
3080 bssid = MWL8K_VIF(priv->vif)->bssid;
3081
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003082 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003083 }
3084 }
3085
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003086 /*
3087 * If FIF_ALLMULTI is being requested, throw away the command
3088 * packet that ->prepare_multicast() built and replace it with
3089 * a command packet that enables reception of all multicast
3090 * packets.
3091 */
3092 if (*total_flags & FIF_ALLMULTI) {
3093 kfree(cmd);
3094 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3095 }
3096
3097 if (cmd != NULL) {
3098 mwl8k_post_cmd(hw, cmd);
3099 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003100 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003101
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003102 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003103}
3104
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003105static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3106{
Lennert Buytenhek733d3062009-07-17 07:24:15 +02003107 return mwl8k_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003108}
3109
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003110static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3111 const struct ieee80211_tx_queue_params *params)
3112{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003113 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003114 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003115
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003116 rc = mwl8k_fw_lock(hw);
3117 if (!rc) {
3118 if (!priv->wmm_enabled)
3119 rc = mwl8k_set_wmm(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003120
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003121 if (!rc)
3122 rc = mwl8k_set_edca_params(hw, queue,
3123 params->cw_min,
3124 params->cw_max,
3125 params->aifs,
3126 params->txop);
3127
3128 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003129 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003130
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003131 return rc;
3132}
3133
3134static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3135 struct ieee80211_tx_queue_stats *stats)
3136{
3137 struct mwl8k_priv *priv = hw->priv;
3138 struct mwl8k_tx_queue *txq;
3139 int index;
3140
3141 spin_lock_bh(&priv->tx_lock);
3142 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3143 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003144 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003145 sizeof(struct ieee80211_tx_queue_stats));
3146 }
3147 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003148
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003149 return 0;
3150}
3151
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003152static int mwl8k_get_stats(struct ieee80211_hw *hw,
3153 struct ieee80211_low_level_stats *stats)
3154{
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003155 return mwl8k_cmd_802_11_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003156}
3157
3158static const struct ieee80211_ops mwl8k_ops = {
3159 .tx = mwl8k_tx,
3160 .start = mwl8k_start,
3161 .stop = mwl8k_stop,
3162 .add_interface = mwl8k_add_interface,
3163 .remove_interface = mwl8k_remove_interface,
3164 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003165 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003166 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003167 .configure_filter = mwl8k_configure_filter,
3168 .set_rts_threshold = mwl8k_set_rts_threshold,
3169 .conf_tx = mwl8k_conf_tx,
3170 .get_tx_stats = mwl8k_get_tx_stats,
3171 .get_stats = mwl8k_get_stats,
3172};
3173
3174static void mwl8k_tx_reclaim_handler(unsigned long data)
3175{
3176 int i;
3177 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3178 struct mwl8k_priv *priv = hw->priv;
3179
3180 spin_lock_bh(&priv->tx_lock);
3181 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3182 mwl8k_txq_reclaim(hw, i, 0);
3183
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003184 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003185 complete(priv->tx_wait);
3186 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003187 }
3188 spin_unlock_bh(&priv->tx_lock);
3189}
3190
3191static void mwl8k_finalize_join_worker(struct work_struct *work)
3192{
3193 struct mwl8k_priv *priv =
3194 container_of(work, struct mwl8k_priv, finalize_join_worker);
3195 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003196 u8 dtim = MWL8K_VIF(priv->vif)->bss_info.dtim_period;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003197
3198 mwl8k_finalize_join(priv->hw, skb->data, skb->len, dtim);
3199 dev_kfree_skb(skb);
3200
3201 priv->beacon_skb = NULL;
3202}
3203
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003204static struct mwl8k_device_info di_8687 = {
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003205 .part_name = "88w8687",
3206 .helper_image = "mwl8k/helper_8687.fw",
3207 .fw_image = "mwl8k/fmimage_8687.fw",
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02003208 .rxd_ops = &rxd_8687_ops,
Lennert Buytenhek547810e2009-10-22 20:21:02 +02003209 .modes = BIT(NL80211_IFTYPE_STATION),
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003210};
3211
3212static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
3213 {
3214 PCI_VDEVICE(MARVELL, 0x2a2b),
3215 .driver_data = (unsigned long)&di_8687,
3216 }, {
3217 PCI_VDEVICE(MARVELL, 0x2a30),
3218 .driver_data = (unsigned long)&di_8687,
3219 }, {
3220 },
3221};
3222MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3223
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003224static int __devinit mwl8k_probe(struct pci_dev *pdev,
3225 const struct pci_device_id *id)
3226{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003227 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003228 struct ieee80211_hw *hw;
3229 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003230 int rc;
3231 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003232
3233 if (!printed_version) {
3234 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3235 printed_version = 1;
3236 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003237
3238 rc = pci_enable_device(pdev);
3239 if (rc) {
3240 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3241 MWL8K_NAME);
3242 return rc;
3243 }
3244
3245 rc = pci_request_regions(pdev, MWL8K_NAME);
3246 if (rc) {
3247 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3248 MWL8K_NAME);
3249 return rc;
3250 }
3251
3252 pci_set_master(pdev);
3253
3254 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3255 if (hw == NULL) {
3256 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3257 rc = -ENOMEM;
3258 goto err_free_reg;
3259 }
3260
3261 priv = hw->priv;
3262 priv->hw = hw;
3263 priv->pdev = pdev;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003264 priv->device_info = (void *)id->driver_data;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02003265 priv->rxd_ops = priv->device_info->rxd_ops;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003266 priv->sniffer_enabled = false;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02003267 priv->wmm_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003268 priv->pending_tx_pkts = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003269
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003270 SET_IEEE80211_DEV(hw, &pdev->dev);
3271 pci_set_drvdata(pdev, hw);
3272
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003273 priv->sram = pci_iomap(pdev, 0, 0x10000);
3274 if (priv->sram == NULL) {
3275 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003276 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003277 goto err_iounmap;
3278 }
3279
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003280 /*
3281 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3282 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3283 */
3284 priv->regs = pci_iomap(pdev, 1, 0x10000);
3285 if (priv->regs == NULL) {
3286 priv->regs = pci_iomap(pdev, 2, 0x10000);
3287 if (priv->regs == NULL) {
3288 printk(KERN_ERR "%s: Cannot map device registers\n",
3289 wiphy_name(hw->wiphy));
3290 goto err_iounmap;
3291 }
3292 }
3293
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003294 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3295 priv->band.band = IEEE80211_BAND_2GHZ;
3296 priv->band.channels = priv->channels;
3297 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3298 priv->band.bitrates = priv->rates;
3299 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3300 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3301
3302 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3303 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3304
3305 /*
3306 * Extra headroom is the size of the required DMA header
3307 * minus the size of the smallest 802.11 frame (CTS frame).
3308 */
3309 hw->extra_tx_headroom =
3310 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3311
3312 hw->channel_change_time = 10;
3313
3314 hw->queues = MWL8K_TX_QUEUES;
3315
Lennert Buytenhek547810e2009-10-22 20:21:02 +02003316 hw->wiphy->interface_modes = priv->device_info->modes;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003317
3318 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003319 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003320 hw->vif_data_size = sizeof(struct mwl8k_vif);
3321 priv->vif = NULL;
3322
3323 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003324 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003325 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003326
3327 /* Finalize join worker */
3328 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3329
3330 /* TX reclaim tasklet */
3331 tasklet_init(&priv->tx_reclaim_task,
3332 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3333 tasklet_disable(&priv->tx_reclaim_task);
3334
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003335 /* Power management cookie */
3336 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3337 if (priv->cookie == NULL)
3338 goto err_iounmap;
3339
3340 rc = mwl8k_rxq_init(hw, 0);
3341 if (rc)
3342 goto err_iounmap;
3343 rxq_refill(hw, 0, INT_MAX);
3344
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003345 mutex_init(&priv->fw_mutex);
3346 priv->fw_mutex_owner = NULL;
3347 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003348 priv->hostcmd_wait = NULL;
3349
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003350 spin_lock_init(&priv->tx_lock);
3351
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003352 priv->tx_wait = NULL;
3353
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003354 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3355 rc = mwl8k_txq_init(hw, i);
3356 if (rc)
3357 goto err_free_queues;
3358 }
3359
3360 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003361 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003362 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3363 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3364
3365 rc = request_irq(priv->pdev->irq, &mwl8k_interrupt,
3366 IRQF_SHARED, MWL8K_NAME, hw);
3367 if (rc) {
3368 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003369 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003370 goto err_free_queues;
3371 }
3372
3373 /* Reset firmware and hardware */
3374 mwl8k_hw_reset(priv);
3375
3376 /* Ask userland hotplug daemon for the device firmware */
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003377 rc = mwl8k_request_firmware(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003378 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003379 printk(KERN_ERR "%s: Firmware files not found\n",
3380 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003381 goto err_free_irq;
3382 }
3383
3384 /* Load firmware into hardware */
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003385 rc = mwl8k_load_firmware(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003386 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003387 printk(KERN_ERR "%s: Cannot start firmware\n",
3388 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003389 goto err_stop_firmware;
3390 }
3391
3392 /* Reclaim memory once firmware is successfully loaded */
3393 mwl8k_release_firmware(priv);
3394
3395 /*
3396 * Temporarily enable interrupts. Initial firmware host
3397 * commands use interrupts and avoids polling. Disable
3398 * interrupts when done.
3399 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003400 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003401
3402 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02003403 if (priv->ap_fw) {
3404 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3405 if (!rc)
3406 rc = mwl8k_cmd_set_hw_spec(hw);
3407 } else {
3408 rc = mwl8k_cmd_get_hw_spec_sta(hw);
3409 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003410 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003411 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3412 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003413 goto err_stop_firmware;
3414 }
3415
3416 /* Turn radio off */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003417 rc = mwl8k_cmd_802_11_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003418 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003419 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003420 goto err_stop_firmware;
3421 }
3422
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003423 /* Clear MAC address */
3424 rc = mwl8k_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
3425 if (rc) {
3426 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3427 wiphy_name(hw->wiphy));
3428 goto err_stop_firmware;
3429 }
3430
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003431 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003432 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003433 free_irq(priv->pdev->irq, hw);
3434
3435 rc = ieee80211_register_hw(hw);
3436 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003437 printk(KERN_ERR "%s: Cannot register device\n",
3438 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003439 goto err_stop_firmware;
3440 }
3441
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003442 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003443 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003444 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003445 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003446 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3447 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003448
3449 return 0;
3450
3451err_stop_firmware:
3452 mwl8k_hw_reset(priv);
3453 mwl8k_release_firmware(priv);
3454
3455err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003456 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003457 free_irq(priv->pdev->irq, hw);
3458
3459err_free_queues:
3460 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3461 mwl8k_txq_deinit(hw, i);
3462 mwl8k_rxq_deinit(hw, 0);
3463
3464err_iounmap:
3465 if (priv->cookie != NULL)
3466 pci_free_consistent(priv->pdev, 4,
3467 priv->cookie, priv->cookie_dma);
3468
3469 if (priv->regs != NULL)
3470 pci_iounmap(pdev, priv->regs);
3471
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003472 if (priv->sram != NULL)
3473 pci_iounmap(pdev, priv->sram);
3474
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003475 pci_set_drvdata(pdev, NULL);
3476 ieee80211_free_hw(hw);
3477
3478err_free_reg:
3479 pci_release_regions(pdev);
3480 pci_disable_device(pdev);
3481
3482 return rc;
3483}
3484
Joerg Albert230f7af2009-04-18 02:10:45 +02003485static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003486{
3487 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3488}
3489
Joerg Albert230f7af2009-04-18 02:10:45 +02003490static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003491{
3492 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3493 struct mwl8k_priv *priv;
3494 int i;
3495
3496 if (hw == NULL)
3497 return;
3498 priv = hw->priv;
3499
3500 ieee80211_stop_queues(hw);
3501
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003502 ieee80211_unregister_hw(hw);
3503
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003504 /* Remove tx reclaim tasklet */
3505 tasklet_kill(&priv->tx_reclaim_task);
3506
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003507 /* Stop hardware */
3508 mwl8k_hw_reset(priv);
3509
3510 /* Return all skbs to mac80211 */
3511 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3512 mwl8k_txq_reclaim(hw, i, 1);
3513
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003514 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3515 mwl8k_txq_deinit(hw, i);
3516
3517 mwl8k_rxq_deinit(hw, 0);
3518
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003519 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003520
3521 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003522 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003523 pci_set_drvdata(pdev, NULL);
3524 ieee80211_free_hw(hw);
3525 pci_release_regions(pdev);
3526 pci_disable_device(pdev);
3527}
3528
3529static struct pci_driver mwl8k_driver = {
3530 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003531 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003532 .probe = mwl8k_probe,
3533 .remove = __devexit_p(mwl8k_remove),
3534 .shutdown = __devexit_p(mwl8k_shutdown),
3535};
3536
3537static int __init mwl8k_init(void)
3538{
3539 return pci_register_driver(&mwl8k_driver);
3540}
3541
3542static void __exit mwl8k_exit(void)
3543{
3544 pci_unregister_driver(&mwl8k_driver);
3545}
3546
3547module_init(mwl8k_init);
3548module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003549
3550MODULE_DESCRIPTION(MWL8K_DESC);
3551MODULE_VERSION(MWL8K_VERSION);
3552MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3553MODULE_LICENSE("GPL");