blob: b20820a015ccd82c156320a7c535470a6223f80d [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);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010087 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
88 __le16 *qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020089};
90
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020091struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020092 char *part_name;
93 char *helper_image;
94 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010095 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020096};
97
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +020099 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100100
101 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200102 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
104 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200105 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100106
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200107 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200109 struct {
110 struct sk_buff *skb;
111 DECLARE_PCI_UNMAP_ADDR(dma)
112 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100113};
114
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115struct mwl8k_tx_queue {
116 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100118
119 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200120 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100121
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
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100128struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200132 struct mwl8k_device_info *device_info;
133
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100134 void __iomem *sram;
135 void __iomem *regs;
136
137 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100138 struct firmware *fw_helper;
139 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100141 /* hardware/firmware parameters */
142 bool ap_fw;
143 struct rxd_ops *rxd_ops;
144
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200145 /* firmware access */
146 struct mutex fw_mutex;
147 struct task_struct *fw_mutex_owner;
148 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200149 struct completion *hostcmd_wait;
150
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100151 /* lock held over TX and TX reap */
152 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100153
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200154 /* TX quiesce completion, protected by fw_mutex and tx_lock */
155 struct completion *tx_wait;
156
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 struct ieee80211_vif *vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158
159 struct ieee80211_channel *current_channel;
160
161 /* power management status cookie from firmware */
162 u32 *cookie;
163 dma_addr_t cookie_dma;
164
165 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100166 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200167 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168
169 /*
170 * Running count of TX packets in flight, to avoid
171 * iterating over the transmit rings each time.
172 */
173 int pending_tx_pkts;
174
175 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
176 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
177
178 /* PHY parameters */
179 struct ieee80211_supported_band band;
180 struct ieee80211_channel channels[14];
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100181 struct ieee80211_rate rates[14];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100182
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200183 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200184 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200185 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200186 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100187
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100188 /* XXX need to convert this to handle multiple interfaces */
189 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200190 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100191 struct sk_buff *beacon_skb;
192
193 /*
194 * This FJ worker has to be global as it is scheduled from the
195 * RX handler. At this point we don't know which interface it
196 * belongs to until the list of bssids waiting to complete join
197 * is checked.
198 */
199 struct work_struct finalize_join_worker;
200
201 /* Tasklet to reclaim TX descriptors and buffers after tx */
202 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100203};
204
205/* Per interface specific private data */
206struct mwl8k_vif {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100207 /* backpointer to parent config block */
208 struct mwl8k_priv *priv;
209
210 /* BSS config of AP or IBSS from mac80211*/
211 struct ieee80211_bss_conf bss_info;
212
213 /* BSSID of AP or IBSS */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200214 u8 bssid[ETH_ALEN];
215 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100216
Lennert Buytenhek55489b62009-11-30 18:31:33 +0100217 /* Index into station database. Returned by UPDATE_STADB. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218 u8 peer_id;
219
220 /* Non AMPDU sequence number assigned by driver */
221 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100222};
223
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200224#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225
226static const struct ieee80211_channel mwl8k_channels[] = {
227 { .center_freq = 2412, .hw_value = 1, },
228 { .center_freq = 2417, .hw_value = 2, },
229 { .center_freq = 2422, .hw_value = 3, },
230 { .center_freq = 2427, .hw_value = 4, },
231 { .center_freq = 2432, .hw_value = 5, },
232 { .center_freq = 2437, .hw_value = 6, },
233 { .center_freq = 2442, .hw_value = 7, },
234 { .center_freq = 2447, .hw_value = 8, },
235 { .center_freq = 2452, .hw_value = 9, },
236 { .center_freq = 2457, .hw_value = 10, },
237 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100238 { .center_freq = 2467, .hw_value = 12, },
239 { .center_freq = 2472, .hw_value = 13, },
240 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100241};
242
243static const struct ieee80211_rate mwl8k_rates[] = {
244 { .bitrate = 10, .hw_value = 2, },
245 { .bitrate = 20, .hw_value = 4, },
246 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200247 { .bitrate = 110, .hw_value = 22, },
248 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100249 { .bitrate = 60, .hw_value = 12, },
250 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100251 { .bitrate = 120, .hw_value = 24, },
252 { .bitrate = 180, .hw_value = 36, },
253 { .bitrate = 240, .hw_value = 48, },
254 { .bitrate = 360, .hw_value = 72, },
255 { .bitrate = 480, .hw_value = 96, },
256 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100257 { .bitrate = 720, .hw_value = 144, },
258};
259
260static const u8 mwl8k_rateids[12] = {
261 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108,
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100262};
263
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100264/* Set or get info from Firmware */
265#define MWL8K_CMD_SET 0x0001
266#define MWL8K_CMD_GET 0x0000
267
268/* Firmware command codes */
269#define MWL8K_CMD_CODE_DNLD 0x0001
270#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200271#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100272#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
273#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200274#define MWL8K_CMD_RADIO_CONTROL 0x001c
275#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200276#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100277#define MWL8K_CMD_SET_PRE_SCAN 0x0107
278#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200279#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100280#define MWL8K_CMD_SET_AID 0x010d
281#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200282#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100283#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200284#define MWL8K_CMD_SET_SLOT 0x0114
285#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
286#define MWL8K_CMD_SET_WMM_MODE 0x0123
287#define MWL8K_CMD_MIMO_CONFIG 0x0125
288#define MWL8K_CMD_USE_FIXED_RATE 0x0126
289#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200290#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200291#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
292#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100293
294static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
295{
296#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
297 snprintf(buf, bufsize, "%s", #x);\
298 return buf;\
299 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200300 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301 MWL8K_CMDNAME(CODE_DNLD);
302 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200303 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100304 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
305 MWL8K_CMDNAME(GET_STAT);
306 MWL8K_CMDNAME(RADIO_CONTROL);
307 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200308 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100309 MWL8K_CMDNAME(SET_PRE_SCAN);
310 MWL8K_CMDNAME(SET_POST_SCAN);
311 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100312 MWL8K_CMDNAME(SET_AID);
313 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200314 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100315 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200316 MWL8K_CMDNAME(SET_SLOT);
317 MWL8K_CMDNAME(SET_EDCA_PARAMS);
318 MWL8K_CMDNAME(SET_WMM_MODE);
319 MWL8K_CMDNAME(MIMO_CONFIG);
320 MWL8K_CMDNAME(USE_FIXED_RATE);
321 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200322 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200323 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
324 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100325 default:
326 snprintf(buf, bufsize, "0x%x", cmd);
327 }
328#undef MWL8K_CMDNAME
329
330 return buf;
331}
332
333/* Hardware and firmware reset */
334static void mwl8k_hw_reset(struct mwl8k_priv *priv)
335{
336 iowrite32(MWL8K_H2A_INT_RESET,
337 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
338 iowrite32(MWL8K_H2A_INT_RESET,
339 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
340 msleep(20);
341}
342
343/* Release fw image */
344static void mwl8k_release_fw(struct firmware **fw)
345{
346 if (*fw == NULL)
347 return;
348 release_firmware(*fw);
349 *fw = NULL;
350}
351
352static void mwl8k_release_firmware(struct mwl8k_priv *priv)
353{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100354 mwl8k_release_fw(&priv->fw_ucode);
355 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100356}
357
358/* Request fw image */
359static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200360 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100361{
362 /* release current image */
363 if (*fw != NULL)
364 mwl8k_release_fw(fw);
365
366 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200367 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100368}
369
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200370static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100371{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200372 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100373 int rc;
374
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200375 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100376 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200377 if (rc) {
378 printk(KERN_ERR "%s: Error requesting helper "
379 "firmware file %s\n", pci_name(priv->pdev),
380 di->helper_image);
381 return rc;
382 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100383 }
384
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100385 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100386 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200387 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200388 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100389 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100390 return rc;
391 }
392
393 return 0;
394}
395
Ben Hutchings7e75b942009-11-07 22:00:57 +0000396MODULE_FIRMWARE("mwl8k/helper_8687.fw");
397MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
398
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100399struct mwl8k_cmd_pkt {
400 __le16 code;
401 __le16 length;
402 __le16 seq_num;
403 __le16 result;
404 char payload[0];
405} __attribute__((packed));
406
407/*
408 * Firmware loading.
409 */
410static int
411mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
412{
413 void __iomem *regs = priv->regs;
414 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100415 int loops;
416
417 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
418 if (pci_dma_mapping_error(priv->pdev, dma_addr))
419 return -ENOMEM;
420
421 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
422 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
423 iowrite32(MWL8K_H2A_INT_DOORBELL,
424 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
425 iowrite32(MWL8K_H2A_INT_DUMMY,
426 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
427
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100428 loops = 1000;
429 do {
430 u32 int_code;
431
432 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
433 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
434 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100435 break;
436 }
437
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200438 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100439 udelay(1);
440 } while (--loops);
441
442 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
443
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200444 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100445}
446
447static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
448 const u8 *data, size_t length)
449{
450 struct mwl8k_cmd_pkt *cmd;
451 int done;
452 int rc = 0;
453
454 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
455 if (cmd == NULL)
456 return -ENOMEM;
457
458 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
459 cmd->seq_num = 0;
460 cmd->result = 0;
461
462 done = 0;
463 while (length) {
464 int block_size = length > 256 ? 256 : length;
465
466 memcpy(cmd->payload, data + done, block_size);
467 cmd->length = cpu_to_le16(block_size);
468
469 rc = mwl8k_send_fw_load_cmd(priv, cmd,
470 sizeof(*cmd) + block_size);
471 if (rc)
472 break;
473
474 done += block_size;
475 length -= block_size;
476 }
477
478 if (!rc) {
479 cmd->length = 0;
480 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
481 }
482
483 kfree(cmd);
484
485 return rc;
486}
487
488static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
489 const u8 *data, size_t length)
490{
491 unsigned char *buffer;
492 int may_continue, rc = 0;
493 u32 done, prev_block_size;
494
495 buffer = kmalloc(1024, GFP_KERNEL);
496 if (buffer == NULL)
497 return -ENOMEM;
498
499 done = 0;
500 prev_block_size = 0;
501 may_continue = 1000;
502 while (may_continue > 0) {
503 u32 block_size;
504
505 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
506 if (block_size & 1) {
507 block_size &= ~1;
508 may_continue--;
509 } else {
510 done += prev_block_size;
511 length -= prev_block_size;
512 }
513
514 if (block_size > 1024 || block_size > length) {
515 rc = -EOVERFLOW;
516 break;
517 }
518
519 if (length == 0) {
520 rc = 0;
521 break;
522 }
523
524 if (block_size == 0) {
525 rc = -EPROTO;
526 may_continue--;
527 udelay(1);
528 continue;
529 }
530
531 prev_block_size = block_size;
532 memcpy(buffer, data + done, block_size);
533
534 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
535 if (rc)
536 break;
537 }
538
539 if (!rc && length != 0)
540 rc = -EREMOTEIO;
541
542 kfree(buffer);
543
544 return rc;
545}
546
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200547static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100548{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200549 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100550 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200551 int rc;
552 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100553
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200554 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100555 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100556
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200557 if (helper == NULL) {
558 printk(KERN_ERR "%s: helper image needed but none "
559 "given\n", pci_name(priv->pdev));
560 return -EINVAL;
561 }
562
563 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100564 if (rc) {
565 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200566 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100567 return rc;
568 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100569 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100570
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200571 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100572 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200573 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100574 }
575
576 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200577 printk(KERN_ERR "%s: unable to load firmware image\n",
578 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100579 return rc;
580 }
581
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100582 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100583
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100584 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100585 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200586 u32 ready_code;
587
588 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
589 if (ready_code == MWL8K_FWAP_READY) {
590 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100591 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200592 } else if (ready_code == MWL8K_FWSTA_READY) {
593 priv->ap_fw = 0;
594 break;
595 }
596
597 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100598 udelay(1);
599 } while (--loops);
600
601 return loops ? 0 : -ETIMEDOUT;
602}
603
604
605/*
606 * Defines shared between transmission and reception.
607 */
608/* HT control fields for firmware */
609struct ewc_ht_info {
610 __le16 control1;
611 __le16 control2;
612 __le16 control3;
613} __attribute__((packed));
614
615/* Firmware Station database operations */
616#define MWL8K_STA_DB_ADD_ENTRY 0
617#define MWL8K_STA_DB_MODIFY_ENTRY 1
618#define MWL8K_STA_DB_DEL_ENTRY 2
619#define MWL8K_STA_DB_FLUSH 3
620
621/* Peer Entry flags - used to define the type of the peer node */
622#define MWL8K_PEER_TYPE_ACCESSPOINT 2
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100623
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100624struct peer_capability_info {
625 /* Peer type - AP vs. STA. */
626 __u8 peer_type;
627
628 /* Basic 802.11 capabilities from assoc resp. */
629 __le16 basic_caps;
630
631 /* Set if peer supports 802.11n high throughput (HT). */
632 __u8 ht_support;
633
634 /* Valid if HT is supported. */
635 __le16 ht_caps;
636 __u8 extended_ht_caps;
637 struct ewc_ht_info ewc_info;
638
639 /* Legacy rate table. Intersection of our rates and peer rates. */
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100640 __u8 legacy_rates[12];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100641
642 /* HT rate table. Intersection of our rates and peer rates. */
Lennert Buytenhek0b5351a2009-11-30 18:11:18 +0100643 __u8 ht_rates[16];
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +0200644 __u8 pad[16];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100645
646 /* If set, interoperability mode, no proprietary extensions. */
647 __u8 interop;
648 __u8 pad2;
649 __u8 station_id;
650 __le16 amsdu_enabled;
651} __attribute__((packed));
652
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100653/* DMA header used by firmware and hardware. */
654struct mwl8k_dma_data {
655 __le16 fwlen;
656 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100657 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100658} __attribute__((packed));
659
660/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100661static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100662{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100663 struct mwl8k_dma_data *tr;
664 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100665
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100666 tr = (struct mwl8k_dma_data *)skb->data;
667 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
668
669 if (hdrlen != sizeof(tr->wh)) {
670 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
671 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
672 *((__le16 *)(tr->data - 2)) = qos;
673 } else {
674 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
675 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100676 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100677
678 if (hdrlen != sizeof(*tr))
679 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100680}
681
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200682static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100683{
684 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100685 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100686 struct mwl8k_dma_data *tr;
687
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100688 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100689 * Add a firmware DMA header; the firmware requires that we
690 * present a 2-byte payload length followed by a 4-address
691 * header (without QoS field), followed (optionally) by any
692 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100693 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100694 wh = (struct ieee80211_hdr *)skb->data;
695
696 hdrlen = ieee80211_hdrlen(wh->frame_control);
697 if (hdrlen != sizeof(*tr))
698 skb_push(skb, sizeof(*tr) - hdrlen);
699
700 if (ieee80211_is_data_qos(wh->frame_control))
701 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100702
703 tr = (struct mwl8k_dma_data *)skb->data;
704 if (wh != &tr->wh)
705 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100706 if (hdrlen != sizeof(tr->wh))
707 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100708
709 /*
710 * Firmware length is the length of the fully formed "802.11
711 * payload". That is, everything except for the 802.11 header.
712 * This includes all crypto material including the MIC.
713 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100714 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100715}
716
717
718/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100719 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200720 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100721struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200722 __le16 pkt_len;
723 __u8 sq2;
724 __u8 rate;
725 __le32 pkt_phys_addr;
726 __le32 next_rxd_phys_addr;
727 __le16 qos_control;
728 __le16 htsig2;
729 __le32 hw_rssi_info;
730 __le32 hw_noise_floor_info;
731 __u8 noise_floor;
732 __u8 pad0[3];
733 __u8 rssi;
734 __u8 rx_status;
735 __u8 channel;
736 __u8 rx_ctrl;
737} __attribute__((packed));
738
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100739#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
740#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
741#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100742
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100743#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200744
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100745static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200746{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100747 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200748
749 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100750 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200751}
752
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100753static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200754{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100755 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200756
757 rxd->pkt_len = cpu_to_le16(len);
758 rxd->pkt_phys_addr = cpu_to_le32(addr);
759 wmb();
760 rxd->rx_ctrl = 0;
761}
762
763static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100764mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
765 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200766{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100767 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200768
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100769 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200770 return -1;
771 rmb();
772
773 memset(status, 0, sizeof(*status));
774
775 status->signal = -rxd->rssi;
776 status->noise = -rxd->noise_floor;
777
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100778 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200779 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100780 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100781 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100782 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200783 } else {
784 int i;
785
786 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
787 if (mwl8k_rates[i].hw_value == rxd->rate) {
788 status->rate_idx = i;
789 break;
790 }
791 }
792 }
793
794 status->band = IEEE80211_BAND_2GHZ;
795 status->freq = ieee80211_channel_to_frequency(rxd->channel);
796
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100797 *qos = rxd->qos_control;
798
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200799 return le16_to_cpu(rxd->pkt_len);
800}
801
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100802static struct rxd_ops rxd_8366_ap_ops = {
803 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
804 .rxd_init = mwl8k_rxd_8366_ap_init,
805 .rxd_refill = mwl8k_rxd_8366_ap_refill,
806 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200807};
808
809/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100810 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100811 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100812struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100813 __le16 pkt_len;
814 __u8 link_quality;
815 __u8 noise_level;
816 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200817 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100818 __le16 qos_control;
819 __le16 rate_info;
820 __le32 pad0[4];
821 __u8 rssi;
822 __u8 channel;
823 __le16 pad1;
824 __u8 rx_ctrl;
825 __u8 rx_status;
826 __u8 pad2[2];
827} __attribute__((packed));
828
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100829#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
830#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
831#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
832#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
833#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
834#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200835
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100836#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200837
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100838static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200839{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100840 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200841
842 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100843 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200844}
845
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100846static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200847{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100848 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200849
850 rxd->pkt_len = cpu_to_le16(len);
851 rxd->pkt_phys_addr = cpu_to_le32(addr);
852 wmb();
853 rxd->rx_ctrl = 0;
854}
855
856static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100857mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100858 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200859{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100860 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200861 u16 rate_info;
862
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100863 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200864 return -1;
865 rmb();
866
867 rate_info = le16_to_cpu(rxd->rate_info);
868
869 memset(status, 0, sizeof(*status));
870
871 status->signal = -rxd->rssi;
872 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100873 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
874 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200875
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100876 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200877 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100878 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200879 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100880 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200881 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100882 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200883 status->flag |= RX_FLAG_HT;
884
885 status->band = IEEE80211_BAND_2GHZ;
886 status->freq = ieee80211_channel_to_frequency(rxd->channel);
887
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100888 *qos = rxd->qos_control;
889
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200890 return le16_to_cpu(rxd->pkt_len);
891}
892
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100893static struct rxd_ops rxd_sta_ops = {
894 .rxd_size = sizeof(struct mwl8k_rxd_sta),
895 .rxd_init = mwl8k_rxd_sta_init,
896 .rxd_refill = mwl8k_rxd_sta_refill,
897 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200898};
899
900
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100901#define MWL8K_RX_DESCS 256
902#define MWL8K_RX_MAXSZ 3800
903
904static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
905{
906 struct mwl8k_priv *priv = hw->priv;
907 struct mwl8k_rx_queue *rxq = priv->rxq + index;
908 int size;
909 int i;
910
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200911 rxq->rxd_count = 0;
912 rxq->head = 0;
913 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100914
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200915 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100916
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200917 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
918 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100919 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200920 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100921 return -ENOMEM;
922 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200923 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100924
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200925 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
926 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100927 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200928 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200929 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100930 return -ENOMEM;
931 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200932 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100933
934 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200935 int desc_size;
936 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100937 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200938 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100939
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200940 desc_size = priv->rxd_ops->rxd_size;
941 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100942
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200943 nexti = i + 1;
944 if (nexti == MWL8K_RX_DESCS)
945 nexti = 0;
946 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
947
948 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100949 }
950
951 return 0;
952}
953
954static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
955{
956 struct mwl8k_priv *priv = hw->priv;
957 struct mwl8k_rx_queue *rxq = priv->rxq + index;
958 int refilled;
959
960 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200961 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100962 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200963 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100964 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200965 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100966
967 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
968 if (skb == NULL)
969 break;
970
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200971 addr = pci_map_single(priv->pdev, skb->data,
972 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100973
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200974 rxq->rxd_count++;
975 rx = rxq->tail++;
976 if (rxq->tail == MWL8K_RX_DESCS)
977 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200978 rxq->buf[rx].skb = skb;
979 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200980
981 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
982 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100983
984 refilled++;
985 }
986
987 return refilled;
988}
989
990/* Must be called only when the card's reception is completely halted */
991static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
992{
993 struct mwl8k_priv *priv = hw->priv;
994 struct mwl8k_rx_queue *rxq = priv->rxq + index;
995 int i;
996
997 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200998 if (rxq->buf[i].skb != NULL) {
999 pci_unmap_single(priv->pdev,
1000 pci_unmap_addr(&rxq->buf[i], dma),
1001 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1002 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001003
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001004 kfree_skb(rxq->buf[i].skb);
1005 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001006 }
1007 }
1008
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001009 kfree(rxq->buf);
1010 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001011
1012 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001013 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001014 rxq->rxd, rxq->rxd_dma);
1015 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001016}
1017
1018
1019/*
1020 * Scan a list of BSSIDs to process for finalize join.
1021 * Allows for extension to process multiple BSSIDs.
1022 */
1023static inline int
1024mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1025{
1026 return priv->capture_beacon &&
1027 ieee80211_is_beacon(wh->frame_control) &&
1028 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1029}
1030
Lennert Buytenhek37797522009-10-22 20:19:45 +02001031static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1032 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001033{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001034 struct mwl8k_priv *priv = hw->priv;
1035
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001036 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001037 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001038
1039 /*
1040 * Use GFP_ATOMIC as rxq_process is called from
1041 * the primary interrupt handler, memory allocation call
1042 * must not sleep.
1043 */
1044 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1045 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001046 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001047}
1048
1049static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1050{
1051 struct mwl8k_priv *priv = hw->priv;
1052 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1053 int processed;
1054
1055 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001056 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001057 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001058 void *rxd;
1059 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001060 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001061 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001062
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001063 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001064 if (skb == NULL)
1065 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001066
1067 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1068
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001069 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001070 if (pkt_len < 0)
1071 break;
1072
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001073 rxq->buf[rxq->head].skb = NULL;
1074
1075 pci_unmap_single(priv->pdev,
1076 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1077 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1078 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001079
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001080 rxq->head++;
1081 if (rxq->head == MWL8K_RX_DESCS)
1082 rxq->head = 0;
1083
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001084 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001085
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001086 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001087 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001088
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001089 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001090 * Check for a pending join operation. Save a
1091 * copy of the beacon and schedule a tasklet to
1092 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001093 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001094 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001095 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001096
Johannes Bergf1d58c22009-06-17 13:13:00 +02001097 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1098 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001099
1100 processed++;
1101 }
1102
1103 return processed;
1104}
1105
1106
1107/*
1108 * Packet transmission.
1109 */
1110
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001111#define MWL8K_TXD_STATUS_OK 0x00000001
1112#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1113#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1114#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001115#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001116
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001117#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1118#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1119#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1120#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1121#define MWL8K_QOS_EOSP 0x0010
1122
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001123struct mwl8k_tx_desc {
1124 __le32 status;
1125 __u8 data_rate;
1126 __u8 tx_priority;
1127 __le16 qos_control;
1128 __le32 pkt_phys_addr;
1129 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001130 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001131 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001132 __le32 reserved;
1133 __le16 rate_info;
1134 __u8 peer_id;
1135 __u8 tx_frag_cnt;
1136} __attribute__((packed));
1137
1138#define MWL8K_TX_DESCS 128
1139
1140static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1141{
1142 struct mwl8k_priv *priv = hw->priv;
1143 struct mwl8k_tx_queue *txq = priv->txq + index;
1144 int size;
1145 int i;
1146
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001147 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1148 txq->stats.limit = MWL8K_TX_DESCS;
1149 txq->head = 0;
1150 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001151
1152 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1153
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001154 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1155 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001156 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001157 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001158 return -ENOMEM;
1159 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001160 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001161
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001162 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1163 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001164 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001165 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001166 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167 return -ENOMEM;
1168 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001169 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001170
1171 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1172 struct mwl8k_tx_desc *tx_desc;
1173 int nexti;
1174
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001175 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001176 nexti = (i + 1) % MWL8K_TX_DESCS;
1177
1178 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001179 tx_desc->next_txd_phys_addr =
1180 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001181 }
1182
1183 return 0;
1184}
1185
1186static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1187{
1188 iowrite32(MWL8K_H2A_INT_PPA_READY,
1189 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1190 iowrite32(MWL8K_H2A_INT_DUMMY,
1191 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1192 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1193}
1194
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001195static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001196{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001197 struct mwl8k_priv *priv = hw->priv;
1198 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001199
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001200 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1201 struct mwl8k_tx_queue *txq = priv->txq + i;
1202 int fw_owned = 0;
1203 int drv_owned = 0;
1204 int unused = 0;
1205 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001206
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001207 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001208 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1209 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001210
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001211 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001212 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001213 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001214 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001215 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001216
1217 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001218 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001219 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001220
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001221 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1222 "fw_owned=%d drv_owned=%d unused=%d\n",
1223 wiphy_name(hw->wiphy), i,
1224 txq->stats.len, txq->head, txq->tail,
1225 fw_owned, drv_owned, unused);
1226 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001227}
1228
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001229/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001230 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001231 */
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001232#define MWL8K_TX_WAIT_TIMEOUT_MS 1000
1233
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001234static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001235{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001236 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001237 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001238 int retry;
1239 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001240
1241 might_sleep();
1242
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001243 /*
1244 * The TX queues are stopped at this point, so this test
1245 * doesn't need to take ->tx_lock.
1246 */
1247 if (!priv->pending_tx_pkts)
1248 return 0;
1249
1250 retry = 0;
1251 rc = 0;
1252
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001253 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001254 priv->tx_wait = &tx_wait;
1255 while (!rc) {
1256 int oldcount;
1257 unsigned long timeout;
1258
1259 oldcount = priv->pending_tx_pkts;
1260
1261 spin_unlock_bh(&priv->tx_lock);
1262 timeout = wait_for_completion_timeout(&tx_wait,
1263 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1264 spin_lock_bh(&priv->tx_lock);
1265
1266 if (timeout) {
1267 WARN_ON(priv->pending_tx_pkts);
1268 if (retry) {
1269 printk(KERN_NOTICE "%s: tx rings drained\n",
1270 wiphy_name(hw->wiphy));
1271 }
1272 break;
1273 }
1274
1275 if (priv->pending_tx_pkts < oldcount) {
1276 printk(KERN_NOTICE "%s: timeout waiting for tx "
1277 "rings to drain (%d -> %d pkts), retrying\n",
1278 wiphy_name(hw->wiphy), oldcount,
1279 priv->pending_tx_pkts);
1280 retry = 1;
1281 continue;
1282 }
1283
1284 priv->tx_wait = NULL;
1285
1286 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1287 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1288 mwl8k_dump_tx_rings(hw);
1289
1290 rc = -ETIMEDOUT;
1291 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001292 spin_unlock_bh(&priv->tx_lock);
1293
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001294 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001295}
1296
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001297#define MWL8K_TXD_SUCCESS(status) \
1298 ((status) & (MWL8K_TXD_STATUS_OK | \
1299 MWL8K_TXD_STATUS_OK_RETRY | \
1300 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001301
1302static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1303{
1304 struct mwl8k_priv *priv = hw->priv;
1305 struct mwl8k_tx_queue *txq = priv->txq + index;
1306 int wake = 0;
1307
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001308 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001309 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001310 struct mwl8k_tx_desc *tx_desc;
1311 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001312 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001313 struct sk_buff *skb;
1314 struct ieee80211_tx_info *info;
1315 u32 status;
1316
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001317 tx = txq->head;
1318 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001319
1320 status = le32_to_cpu(tx_desc->status);
1321
1322 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1323 if (!force)
1324 break;
1325 tx_desc->status &=
1326 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1327 }
1328
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001329 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1330 BUG_ON(txq->stats.len == 0);
1331 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001332 priv->pending_tx_pkts--;
1333
1334 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001335 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001336 skb = txq->skb[tx];
1337 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001338
1339 BUG_ON(skb == NULL);
1340 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1341
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001342 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001343
1344 /* Mark descriptor as unused */
1345 tx_desc->pkt_phys_addr = 0;
1346 tx_desc->pkt_len = 0;
1347
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001348 info = IEEE80211_SKB_CB(skb);
1349 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001350 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001351 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001352
1353 ieee80211_tx_status_irqsafe(hw, skb);
1354
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001355 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001356 }
1357
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001358 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001359 ieee80211_wake_queue(hw, index);
1360}
1361
1362/* must be called only when the card's transmit is completely halted */
1363static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1364{
1365 struct mwl8k_priv *priv = hw->priv;
1366 struct mwl8k_tx_queue *txq = priv->txq + index;
1367
1368 mwl8k_txq_reclaim(hw, index, 1);
1369
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001370 kfree(txq->skb);
1371 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001372
1373 pci_free_consistent(priv->pdev,
1374 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001375 txq->txd, txq->txd_dma);
1376 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001377}
1378
1379static int
1380mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1381{
1382 struct mwl8k_priv *priv = hw->priv;
1383 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001384 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001385 struct ieee80211_hdr *wh;
1386 struct mwl8k_tx_queue *txq;
1387 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001388 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001389 u32 txstatus;
1390 u8 txdatarate;
1391 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001392
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001393 wh = (struct ieee80211_hdr *)skb->data;
1394 if (ieee80211_is_data_qos(wh->frame_control))
1395 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1396 else
1397 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001398
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001399 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001400 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001401
1402 tx_info = IEEE80211_SKB_CB(skb);
1403 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001404
1405 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1406 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001407
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001408 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1409 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1410 mwl8k_vif->seqno = seqno++ % 4096;
1411 }
1412
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001413 /* Setup firmware control bit fields for each frame type. */
1414 txstatus = 0;
1415 txdatarate = 0;
1416 if (ieee80211_is_mgmt(wh->frame_control) ||
1417 ieee80211_is_ctl(wh->frame_control)) {
1418 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001419 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001420 } else if (ieee80211_is_data(wh->frame_control)) {
1421 txdatarate = 1;
1422 if (is_multicast_ether_addr(wh->addr1))
1423 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1424
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001425 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001426 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001427 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001428 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001429 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001430 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001431
1432 dma = pci_map_single(priv->pdev, skb->data,
1433 skb->len, PCI_DMA_TODEVICE);
1434
1435 if (pci_dma_mapping_error(priv->pdev, dma)) {
1436 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001437 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001438 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439 return NETDEV_TX_OK;
1440 }
1441
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001442 spin_lock_bh(&priv->tx_lock);
1443
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001444 txq = priv->txq + index;
1445
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001446 BUG_ON(txq->skb[txq->tail] != NULL);
1447 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001448
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001449 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001450 tx->data_rate = txdatarate;
1451 tx->tx_priority = index;
1452 tx->qos_control = cpu_to_le16(qos);
1453 tx->pkt_phys_addr = cpu_to_le32(dma);
1454 tx->pkt_len = cpu_to_le16(skb->len);
1455 tx->rate_info = 0;
1456 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001457 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001458 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1459
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001460 txq->stats.count++;
1461 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001462 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001463
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001464 txq->tail++;
1465 if (txq->tail == MWL8K_TX_DESCS)
1466 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001467
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001468 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001469 ieee80211_stop_queue(hw, index);
1470
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001471 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001472
1473 spin_unlock_bh(&priv->tx_lock);
1474
1475 return NETDEV_TX_OK;
1476}
1477
1478
1479/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001480 * Firmware access.
1481 *
1482 * We have the following requirements for issuing firmware commands:
1483 * - Some commands require that the packet transmit path is idle when
1484 * the command is issued. (For simplicity, we'll just quiesce the
1485 * transmit path for every command.)
1486 * - There are certain sequences of commands that need to be issued to
1487 * the hardware sequentially, with no other intervening commands.
1488 *
1489 * This leads to an implementation of a "firmware lock" as a mutex that
1490 * can be taken recursively, and which is taken by both the low-level
1491 * command submission function (mwl8k_post_cmd) as well as any users of
1492 * that function that require issuing of an atomic sequence of commands,
1493 * and quiesces the transmit path whenever it's taken.
1494 */
1495static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1496{
1497 struct mwl8k_priv *priv = hw->priv;
1498
1499 if (priv->fw_mutex_owner != current) {
1500 int rc;
1501
1502 mutex_lock(&priv->fw_mutex);
1503 ieee80211_stop_queues(hw);
1504
1505 rc = mwl8k_tx_wait_empty(hw);
1506 if (rc) {
1507 ieee80211_wake_queues(hw);
1508 mutex_unlock(&priv->fw_mutex);
1509
1510 return rc;
1511 }
1512
1513 priv->fw_mutex_owner = current;
1514 }
1515
1516 priv->fw_mutex_depth++;
1517
1518 return 0;
1519}
1520
1521static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1522{
1523 struct mwl8k_priv *priv = hw->priv;
1524
1525 if (!--priv->fw_mutex_depth) {
1526 ieee80211_wake_queues(hw);
1527 priv->fw_mutex_owner = NULL;
1528 mutex_unlock(&priv->fw_mutex);
1529 }
1530}
1531
1532
1533/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001534 * Command processing.
1535 */
1536
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001537/* Timeout firmware commands after 10s */
1538#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001539
1540static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1541{
1542 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1543 struct mwl8k_priv *priv = hw->priv;
1544 void __iomem *regs = priv->regs;
1545 dma_addr_t dma_addr;
1546 unsigned int dma_size;
1547 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001548 unsigned long timeout = 0;
1549 u8 buf[32];
1550
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001551 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001552 dma_size = le16_to_cpu(cmd->length);
1553 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1554 PCI_DMA_BIDIRECTIONAL);
1555 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1556 return -ENOMEM;
1557
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001558 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001559 if (rc) {
1560 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1561 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001562 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001563 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001564
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001565 priv->hostcmd_wait = &cmd_wait;
1566 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1567 iowrite32(MWL8K_H2A_INT_DOORBELL,
1568 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1569 iowrite32(MWL8K_H2A_INT_DUMMY,
1570 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001571
1572 timeout = wait_for_completion_timeout(&cmd_wait,
1573 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1574
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001575 priv->hostcmd_wait = NULL;
1576
1577 mwl8k_fw_unlock(hw);
1578
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001579 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1580 PCI_DMA_BIDIRECTIONAL);
1581
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001582 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001583 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001584 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001585 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1586 MWL8K_CMD_TIMEOUT_MS);
1587 rc = -ETIMEDOUT;
1588 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001589 int ms;
1590
1591 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1592
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001593 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001594 if (rc)
1595 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001596 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001597 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001598 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001599 else if (ms > 2000)
1600 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1601 wiphy_name(hw->wiphy),
1602 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1603 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001604 }
1605
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001606 return rc;
1607}
1608
1609/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001610 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001611 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001612struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001613 struct mwl8k_cmd_pkt header;
1614 __u8 hw_rev;
1615 __u8 host_interface;
1616 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001617 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001618 __le16 region_code;
1619 __le32 fw_rev;
1620 __le32 ps_cookie;
1621 __le32 caps;
1622 __u8 mcs_bitmap[16];
1623 __le32 rx_queue_ptr;
1624 __le32 num_tx_queues;
1625 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1626 __le32 caps2;
1627 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001628 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001629} __attribute__((packed));
1630
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001631static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001632{
1633 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001634 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001635 int rc;
1636 int i;
1637
1638 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1639 if (cmd == NULL)
1640 return -ENOMEM;
1641
1642 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1643 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1644
1645 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1646 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001647 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001648 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001649 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001650 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001651 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001652 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001653
1654 rc = mwl8k_post_cmd(hw, &cmd->header);
1655
1656 if (!rc) {
1657 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1658 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001659 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001660 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001661 }
1662
1663 kfree(cmd);
1664 return rc;
1665}
1666
1667/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001668 * CMD_GET_HW_SPEC (AP version).
1669 */
1670struct mwl8k_cmd_get_hw_spec_ap {
1671 struct mwl8k_cmd_pkt header;
1672 __u8 hw_rev;
1673 __u8 host_interface;
1674 __le16 num_wcb;
1675 __le16 num_mcaddrs;
1676 __u8 perm_addr[ETH_ALEN];
1677 __le16 region_code;
1678 __le16 num_antenna;
1679 __le32 fw_rev;
1680 __le32 wcbbase0;
1681 __le32 rxwrptr;
1682 __le32 rxrdptr;
1683 __le32 ps_cookie;
1684 __le32 wcbbase1;
1685 __le32 wcbbase2;
1686 __le32 wcbbase3;
1687} __attribute__((packed));
1688
1689static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1690{
1691 struct mwl8k_priv *priv = hw->priv;
1692 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1693 int rc;
1694
1695 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1696 if (cmd == NULL)
1697 return -ENOMEM;
1698
1699 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1700 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1701
1702 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1703 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1704
1705 rc = mwl8k_post_cmd(hw, &cmd->header);
1706
1707 if (!rc) {
1708 int off;
1709
1710 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1711 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1712 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1713 priv->hw_rev = cmd->hw_rev;
1714
1715 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1716 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1717
1718 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1719 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1720
1721 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1722 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1723
1724 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1725 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1726
1727 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1728 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1729
1730 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1731 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1732 }
1733
1734 kfree(cmd);
1735 return rc;
1736}
1737
1738/*
1739 * CMD_SET_HW_SPEC.
1740 */
1741struct mwl8k_cmd_set_hw_spec {
1742 struct mwl8k_cmd_pkt header;
1743 __u8 hw_rev;
1744 __u8 host_interface;
1745 __le16 num_mcaddrs;
1746 __u8 perm_addr[ETH_ALEN];
1747 __le16 region_code;
1748 __le32 fw_rev;
1749 __le32 ps_cookie;
1750 __le32 caps;
1751 __le32 rx_queue_ptr;
1752 __le32 num_tx_queues;
1753 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1754 __le32 flags;
1755 __le32 num_tx_desc_per_queue;
1756 __le32 total_rxd;
1757} __attribute__((packed));
1758
1759#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1760
1761static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1762{
1763 struct mwl8k_priv *priv = hw->priv;
1764 struct mwl8k_cmd_set_hw_spec *cmd;
1765 int rc;
1766 int i;
1767
1768 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1769 if (cmd == NULL)
1770 return -ENOMEM;
1771
1772 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1773 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1774
1775 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1776 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1777 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1778 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1779 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1780 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1781 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1782 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1783
1784 rc = mwl8k_post_cmd(hw, &cmd->header);
1785 kfree(cmd);
1786
1787 return rc;
1788}
1789
1790/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001791 * CMD_MAC_MULTICAST_ADR.
1792 */
1793struct mwl8k_cmd_mac_multicast_adr {
1794 struct mwl8k_cmd_pkt header;
1795 __le16 action;
1796 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001797 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001798};
1799
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001800#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1801#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1802#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1803#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001804
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001805static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001806__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001807 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001808{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001809 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001810 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001811 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001812
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001813 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001814 allmulti = 1;
1815 mc_count = 0;
1816 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001817
1818 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1819
1820 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001821 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001822 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001823
1824 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1825 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001826 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1827 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001828
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001829 if (allmulti) {
1830 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1831 } else if (mc_count) {
1832 int i;
1833
1834 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1835 cmd->numaddr = cpu_to_le16(mc_count);
1836 for (i = 0; i < mc_count && mclist; i++) {
1837 if (mclist->da_addrlen != ETH_ALEN) {
1838 kfree(cmd);
1839 return NULL;
1840 }
1841 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1842 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001843 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001844 }
1845
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001846 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001847}
1848
1849/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001850 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001851 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001852struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001853 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001854 __le32 stats[64];
1855} __attribute__((packed));
1856
1857#define MWL8K_STAT_ACK_FAILURE 9
1858#define MWL8K_STAT_RTS_FAILURE 12
1859#define MWL8K_STAT_FCS_ERROR 24
1860#define MWL8K_STAT_RTS_SUCCESS 11
1861
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001862static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1863 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001864{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001865 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001866 int rc;
1867
1868 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1869 if (cmd == NULL)
1870 return -ENOMEM;
1871
1872 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1873 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001874
1875 rc = mwl8k_post_cmd(hw, &cmd->header);
1876 if (!rc) {
1877 stats->dot11ACKFailureCount =
1878 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1879 stats->dot11RTSFailureCount =
1880 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1881 stats->dot11FCSErrorCount =
1882 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1883 stats->dot11RTSSuccessCount =
1884 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1885 }
1886 kfree(cmd);
1887
1888 return rc;
1889}
1890
1891/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001892 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001893 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001894struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001895 struct mwl8k_cmd_pkt header;
1896 __le16 action;
1897 __le16 control;
1898 __le16 radio_on;
1899} __attribute__((packed));
1900
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001901static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001902mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001903{
1904 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001905 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001906 int rc;
1907
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001908 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001909 return 0;
1910
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001911 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1912 if (cmd == NULL)
1913 return -ENOMEM;
1914
1915 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1916 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1917 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001918 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1920
1921 rc = mwl8k_post_cmd(hw, &cmd->header);
1922 kfree(cmd);
1923
1924 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001925 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001926
1927 return rc;
1928}
1929
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001930static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001931{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001932 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001933}
1934
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001935static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001936{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001937 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001938}
1939
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001940static int
1941mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1942{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001943 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001944
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001945 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001946
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001947 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001948}
1949
1950/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001951 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001952 */
1953#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1954
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001955struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001956 struct mwl8k_cmd_pkt header;
1957 __le16 action;
1958 __le16 support_level;
1959 __le16 current_level;
1960 __le16 reserved;
1961 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1962} __attribute__((packed));
1963
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001964static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001965{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001966 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001967 int rc;
1968
1969 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1970 if (cmd == NULL)
1971 return -ENOMEM;
1972
1973 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1974 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1975 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1976 cmd->support_level = cpu_to_le16(dBm);
1977
1978 rc = mwl8k_post_cmd(hw, &cmd->header);
1979 kfree(cmd);
1980
1981 return rc;
1982}
1983
1984/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02001985 * CMD_RF_ANTENNA.
1986 */
1987struct mwl8k_cmd_rf_antenna {
1988 struct mwl8k_cmd_pkt header;
1989 __le16 antenna;
1990 __le16 mode;
1991} __attribute__((packed));
1992
1993#define MWL8K_RF_ANTENNA_RX 1
1994#define MWL8K_RF_ANTENNA_TX 2
1995
1996static int
1997mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
1998{
1999 struct mwl8k_cmd_rf_antenna *cmd;
2000 int rc;
2001
2002 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2003 if (cmd == NULL)
2004 return -ENOMEM;
2005
2006 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2007 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2008 cmd->antenna = cpu_to_le16(antenna);
2009 cmd->mode = cpu_to_le16(mask);
2010
2011 rc = mwl8k_post_cmd(hw, &cmd->header);
2012 kfree(cmd);
2013
2014 return rc;
2015}
2016
2017/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002018 * CMD_SET_PRE_SCAN.
2019 */
2020struct mwl8k_cmd_set_pre_scan {
2021 struct mwl8k_cmd_pkt header;
2022} __attribute__((packed));
2023
2024static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2025{
2026 struct mwl8k_cmd_set_pre_scan *cmd;
2027 int rc;
2028
2029 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2030 if (cmd == NULL)
2031 return -ENOMEM;
2032
2033 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2034 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2035
2036 rc = mwl8k_post_cmd(hw, &cmd->header);
2037 kfree(cmd);
2038
2039 return rc;
2040}
2041
2042/*
2043 * CMD_SET_POST_SCAN.
2044 */
2045struct mwl8k_cmd_set_post_scan {
2046 struct mwl8k_cmd_pkt header;
2047 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002048 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002049} __attribute__((packed));
2050
2051static int
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002052mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002053{
2054 struct mwl8k_cmd_set_post_scan *cmd;
2055 int rc;
2056
2057 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2058 if (cmd == NULL)
2059 return -ENOMEM;
2060
2061 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2062 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2063 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002064 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002065
2066 rc = mwl8k_post_cmd(hw, &cmd->header);
2067 kfree(cmd);
2068
2069 return rc;
2070}
2071
2072/*
2073 * CMD_SET_RF_CHANNEL.
2074 */
2075struct mwl8k_cmd_set_rf_channel {
2076 struct mwl8k_cmd_pkt header;
2077 __le16 action;
2078 __u8 current_channel;
2079 __le32 channel_flags;
2080} __attribute__((packed));
2081
2082static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2083 struct ieee80211_channel *channel)
2084{
2085 struct mwl8k_cmd_set_rf_channel *cmd;
2086 int rc;
2087
2088 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2089 if (cmd == NULL)
2090 return -ENOMEM;
2091
2092 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2093 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2094 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2095 cmd->current_channel = channel->hw_value;
2096 if (channel->band == IEEE80211_BAND_2GHZ)
2097 cmd->channel_flags = cpu_to_le32(0x00000081);
2098 else
2099 cmd->channel_flags = cpu_to_le32(0x00000000);
2100
2101 rc = mwl8k_post_cmd(hw, &cmd->header);
2102 kfree(cmd);
2103
2104 return rc;
2105}
2106
2107/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002108 * CMD_SET_AID.
2109 */
2110#define MWL8K_FRAME_PROT_DISABLED 0x00
2111#define MWL8K_FRAME_PROT_11G 0x07
2112#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2113#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2114
2115struct mwl8k_cmd_update_set_aid {
2116 struct mwl8k_cmd_pkt header;
2117 __le16 aid;
2118
2119 /* AP's MAC address (BSSID) */
2120 __u8 bssid[ETH_ALEN];
2121 __le16 protection_mode;
2122 __u8 supp_rates[14];
2123} __attribute__((packed));
2124
2125static int
2126mwl8k_cmd_set_aid(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2127{
2128 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2129 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2130 struct mwl8k_cmd_update_set_aid *cmd;
2131 u16 prot_mode;
2132 int rc;
2133
2134 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2135 if (cmd == NULL)
2136 return -ENOMEM;
2137
2138 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2139 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2140 cmd->aid = cpu_to_le16(info->aid);
2141
2142 memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
2143
2144 if (info->use_cts_prot) {
2145 prot_mode = MWL8K_FRAME_PROT_11G;
2146 } else {
2147 switch (info->ht_operation_mode &
2148 IEEE80211_HT_OP_MODE_PROTECTION) {
2149 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2150 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2151 break;
2152 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2153 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2154 break;
2155 default:
2156 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2157 break;
2158 }
2159 }
2160 cmd->protection_mode = cpu_to_le16(prot_mode);
2161
2162 memcpy(cmd->supp_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2163
2164 rc = mwl8k_post_cmd(hw, &cmd->header);
2165 kfree(cmd);
2166
2167 return rc;
2168}
2169
2170/*
2171 * CMD_SET_RATE.
2172 */
2173struct mwl8k_cmd_set_rate {
2174 struct mwl8k_cmd_pkt header;
2175 __u8 legacy_rates[14];
2176
2177 /* Bitmap for supported MCS codes. */
2178 __u8 mcs_set[16];
2179 __u8 reserved[16];
2180} __attribute__((packed));
2181
2182static int
2183mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2184{
2185 struct mwl8k_cmd_set_rate *cmd;
2186 int rc;
2187
2188 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2189 if (cmd == NULL)
2190 return -ENOMEM;
2191
2192 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2193 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2194 memcpy(cmd->legacy_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2195
2196 rc = mwl8k_post_cmd(hw, &cmd->header);
2197 kfree(cmd);
2198
2199 return rc;
2200}
2201
2202/*
2203 * CMD_FINALIZE_JOIN.
2204 */
2205#define MWL8K_FJ_BEACON_MAXLEN 128
2206
2207struct mwl8k_cmd_finalize_join {
2208 struct mwl8k_cmd_pkt header;
2209 __le32 sleep_interval; /* Number of beacon periods to sleep */
2210 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2211} __attribute__((packed));
2212
2213static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2214 int framelen, int dtim)
2215{
2216 struct mwl8k_cmd_finalize_join *cmd;
2217 struct ieee80211_mgmt *payload = frame;
2218 int payload_len;
2219 int rc;
2220
2221 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2222 if (cmd == NULL)
2223 return -ENOMEM;
2224
2225 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2226 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2227 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2228
2229 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2230 if (payload_len < 0)
2231 payload_len = 0;
2232 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2233 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2234
2235 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2236
2237 rc = mwl8k_post_cmd(hw, &cmd->header);
2238 kfree(cmd);
2239
2240 return rc;
2241}
2242
2243/*
2244 * CMD_SET_RTS_THRESHOLD.
2245 */
2246struct mwl8k_cmd_set_rts_threshold {
2247 struct mwl8k_cmd_pkt header;
2248 __le16 action;
2249 __le16 threshold;
2250} __attribute__((packed));
2251
2252static int mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw,
2253 u16 action, u16 threshold)
2254{
2255 struct mwl8k_cmd_set_rts_threshold *cmd;
2256 int rc;
2257
2258 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2259 if (cmd == NULL)
2260 return -ENOMEM;
2261
2262 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2263 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2264 cmd->action = cpu_to_le16(action);
2265 cmd->threshold = cpu_to_le16(threshold);
2266
2267 rc = mwl8k_post_cmd(hw, &cmd->header);
2268 kfree(cmd);
2269
2270 return rc;
2271}
2272
2273/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002274 * CMD_SET_SLOT.
2275 */
2276struct mwl8k_cmd_set_slot {
2277 struct mwl8k_cmd_pkt header;
2278 __le16 action;
2279 __u8 short_slot;
2280} __attribute__((packed));
2281
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002282static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002283{
2284 struct mwl8k_cmd_set_slot *cmd;
2285 int rc;
2286
2287 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2288 if (cmd == NULL)
2289 return -ENOMEM;
2290
2291 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2292 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2293 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002294 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002295
2296 rc = mwl8k_post_cmd(hw, &cmd->header);
2297 kfree(cmd);
2298
2299 return rc;
2300}
2301
2302/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002303 * CMD_SET_EDCA_PARAMS.
2304 */
2305struct mwl8k_cmd_set_edca_params {
2306 struct mwl8k_cmd_pkt header;
2307
2308 /* See MWL8K_SET_EDCA_XXX below */
2309 __le16 action;
2310
2311 /* TX opportunity in units of 32 us */
2312 __le16 txop;
2313
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002314 union {
2315 struct {
2316 /* Log exponent of max contention period: 0...15 */
2317 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002318
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002319 /* Log exponent of min contention period: 0...15 */
2320 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002321
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002322 /* Adaptive interframe spacing in units of 32us */
2323 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002324
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002325 /* TX queue to configure */
2326 __u8 txq;
2327 } ap;
2328 struct {
2329 /* Log exponent of max contention period: 0...15 */
2330 __u8 log_cw_max;
2331
2332 /* Log exponent of min contention period: 0...15 */
2333 __u8 log_cw_min;
2334
2335 /* Adaptive interframe spacing in units of 32us */
2336 __u8 aifs;
2337
2338 /* TX queue to configure */
2339 __u8 txq;
2340 } sta;
2341 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002342} __attribute__((packed));
2343
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002344#define MWL8K_SET_EDCA_CW 0x01
2345#define MWL8K_SET_EDCA_TXOP 0x02
2346#define MWL8K_SET_EDCA_AIFS 0x04
2347
2348#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2349 MWL8K_SET_EDCA_TXOP | \
2350 MWL8K_SET_EDCA_AIFS)
2351
2352static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002353mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2354 __u16 cw_min, __u16 cw_max,
2355 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002356{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002357 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002358 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002359 int rc;
2360
2361 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2362 if (cmd == NULL)
2363 return -ENOMEM;
2364
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002365 /*
2366 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2367 * this call.
2368 */
2369 qnum ^= !(qnum >> 1);
2370
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002371 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2372 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002373 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2374 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002375 if (priv->ap_fw) {
2376 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2377 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2378 cmd->ap.aifs = aifs;
2379 cmd->ap.txq = qnum;
2380 } else {
2381 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2382 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2383 cmd->sta.aifs = aifs;
2384 cmd->sta.txq = qnum;
2385 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002386
2387 rc = mwl8k_post_cmd(hw, &cmd->header);
2388 kfree(cmd);
2389
2390 return rc;
2391}
2392
2393/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002394 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002395 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002396struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002397 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002398 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002399} __attribute__((packed));
2400
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002401static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002402{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002403 struct mwl8k_priv *priv = hw->priv;
2404 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002405 int rc;
2406
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002407 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2408 if (cmd == NULL)
2409 return -ENOMEM;
2410
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002411 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002412 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002413 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002414
2415 rc = mwl8k_post_cmd(hw, &cmd->header);
2416 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002417
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002418 if (!rc)
2419 priv->wmm_enabled = enable;
2420
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002421 return rc;
2422}
2423
2424/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002425 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002426 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002427struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002428 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002429 __le32 action;
2430 __u8 rx_antenna_map;
2431 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002432} __attribute__((packed));
2433
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002434static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002435{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002436 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002437 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002438
2439 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2440 if (cmd == NULL)
2441 return -ENOMEM;
2442
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002443 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002444 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002445 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2446 cmd->rx_antenna_map = rx;
2447 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002448
2449 rc = mwl8k_post_cmd(hw, &cmd->header);
2450 kfree(cmd);
2451
2452 return rc;
2453}
2454
2455/*
2456 * CMD_USE_FIXED_RATE.
2457 */
2458#define MWL8K_RATE_TABLE_SIZE 8
2459#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002460#define MWL8K_USE_AUTO_RATE 0x0002
2461
2462struct mwl8k_rate_entry {
2463 /* Set to 1 if HT rate, 0 if legacy. */
2464 __le32 is_ht_rate;
2465
2466 /* Set to 1 to use retry_count field. */
2467 __le32 enable_retry;
2468
2469 /* Specified legacy rate or MCS. */
2470 __le32 rate;
2471
2472 /* Number of allowed retries. */
2473 __le32 retry_count;
2474} __attribute__((packed));
2475
2476struct mwl8k_rate_table {
2477 /* 1 to allow specified rate and below */
2478 __le32 allow_rate_drop;
2479 __le32 num_rates;
2480 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2481} __attribute__((packed));
2482
2483struct mwl8k_cmd_use_fixed_rate {
2484 struct mwl8k_cmd_pkt header;
2485 __le32 action;
2486 struct mwl8k_rate_table rate_table;
2487
2488 /* Unicast, Broadcast or Multicast */
2489 __le32 rate_type;
2490 __le32 reserved1;
2491 __le32 reserved2;
2492} __attribute__((packed));
2493
2494static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2495 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2496{
2497 struct mwl8k_cmd_use_fixed_rate *cmd;
2498 int count;
2499 int rc;
2500
2501 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2502 if (cmd == NULL)
2503 return -ENOMEM;
2504
2505 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2506 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2507
2508 cmd->action = cpu_to_le32(action);
2509 cmd->rate_type = cpu_to_le32(rate_type);
2510
2511 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002512 /*
2513 * Copy over each field manually so that endian
2514 * conversion can be done.
2515 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002516 cmd->rate_table.allow_rate_drop =
2517 cpu_to_le32(rate_table->allow_rate_drop);
2518 cmd->rate_table.num_rates =
2519 cpu_to_le32(rate_table->num_rates);
2520
2521 for (count = 0; count < rate_table->num_rates; count++) {
2522 struct mwl8k_rate_entry *dst =
2523 &cmd->rate_table.rate_entry[count];
2524 struct mwl8k_rate_entry *src =
2525 &rate_table->rate_entry[count];
2526
2527 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2528 dst->enable_retry = cpu_to_le32(src->enable_retry);
2529 dst->rate = cpu_to_le32(src->rate);
2530 dst->retry_count = cpu_to_le32(src->retry_count);
2531 }
2532 }
2533
2534 rc = mwl8k_post_cmd(hw, &cmd->header);
2535 kfree(cmd);
2536
2537 return rc;
2538}
2539
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002540/*
2541 * CMD_ENABLE_SNIFFER.
2542 */
2543struct mwl8k_cmd_enable_sniffer {
2544 struct mwl8k_cmd_pkt header;
2545 __le32 action;
2546} __attribute__((packed));
2547
2548static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2549{
2550 struct mwl8k_cmd_enable_sniffer *cmd;
2551 int rc;
2552
2553 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2554 if (cmd == NULL)
2555 return -ENOMEM;
2556
2557 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2558 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2559 cmd->action = cpu_to_le32(!!enable);
2560
2561 rc = mwl8k_post_cmd(hw, &cmd->header);
2562 kfree(cmd);
2563
2564 return rc;
2565}
2566
2567/*
2568 * CMD_SET_MAC_ADDR.
2569 */
2570struct mwl8k_cmd_set_mac_addr {
2571 struct mwl8k_cmd_pkt header;
2572 union {
2573 struct {
2574 __le16 mac_type;
2575 __u8 mac_addr[ETH_ALEN];
2576 } mbss;
2577 __u8 mac_addr[ETH_ALEN];
2578 };
2579} __attribute__((packed));
2580
2581static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2582{
2583 struct mwl8k_priv *priv = hw->priv;
2584 struct mwl8k_cmd_set_mac_addr *cmd;
2585 int rc;
2586
2587 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2588 if (cmd == NULL)
2589 return -ENOMEM;
2590
2591 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2592 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2593 if (priv->ap_fw) {
2594 cmd->mbss.mac_type = 0;
2595 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2596 } else {
2597 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2598 }
2599
2600 rc = mwl8k_post_cmd(hw, &cmd->header);
2601 kfree(cmd);
2602
2603 return rc;
2604}
2605
2606/*
2607 * CMD_SET_RATEADAPT_MODE.
2608 */
2609struct mwl8k_cmd_set_rate_adapt_mode {
2610 struct mwl8k_cmd_pkt header;
2611 __le16 action;
2612 __le16 mode;
2613} __attribute__((packed));
2614
2615static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2616{
2617 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2618 int rc;
2619
2620 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2621 if (cmd == NULL)
2622 return -ENOMEM;
2623
2624 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2625 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2626 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2627 cmd->mode = cpu_to_le16(mode);
2628
2629 rc = mwl8k_post_cmd(hw, &cmd->header);
2630 kfree(cmd);
2631
2632 return rc;
2633}
2634
2635/*
2636 * CMD_UPDATE_STADB.
2637 */
2638struct mwl8k_cmd_update_stadb {
2639 struct mwl8k_cmd_pkt header;
2640
2641 /* See STADB_ACTION_TYPE */
2642 __le32 action;
2643
2644 /* Peer MAC address */
2645 __u8 peer_addr[ETH_ALEN];
2646
2647 __le32 reserved;
2648
2649 /* Peer info - valid during add/update. */
2650 struct peer_capability_info peer_info;
2651} __attribute__((packed));
2652
2653static int mwl8k_cmd_update_stadb(struct ieee80211_hw *hw,
2654 struct ieee80211_vif *vif, __u32 action)
2655{
2656 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2657 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2658 struct mwl8k_cmd_update_stadb *cmd;
2659 struct peer_capability_info *peer_info;
2660 int rc;
2661
2662 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2663 if (cmd == NULL)
2664 return -ENOMEM;
2665
2666 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2667 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2668
2669 cmd->action = cpu_to_le32(action);
2670 peer_info = &cmd->peer_info;
2671 memcpy(cmd->peer_addr, mv_vif->bssid, ETH_ALEN);
2672
2673 switch (action) {
2674 case MWL8K_STA_DB_ADD_ENTRY:
2675 case MWL8K_STA_DB_MODIFY_ENTRY:
2676 /* Build peer_info block */
2677 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2678 peer_info->basic_caps = cpu_to_le16(info->assoc_capability);
2679 memcpy(peer_info->legacy_rates, mwl8k_rateids,
2680 sizeof(mwl8k_rateids));
2681 peer_info->interop = 1;
2682 peer_info->amsdu_enabled = 0;
2683
2684 rc = mwl8k_post_cmd(hw, &cmd->header);
2685 if (rc == 0)
2686 mv_vif->peer_id = peer_info->station_id;
2687
2688 break;
2689
2690 case MWL8K_STA_DB_DEL_ENTRY:
2691 case MWL8K_STA_DB_FLUSH:
2692 default:
2693 rc = mwl8k_post_cmd(hw, &cmd->header);
2694 if (rc == 0)
2695 mv_vif->peer_id = 0;
2696 break;
2697 }
2698 kfree(cmd);
2699
2700 return rc;
2701}
2702
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002703
2704/*
2705 * Interrupt handling.
2706 */
2707static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2708{
2709 struct ieee80211_hw *hw = dev_id;
2710 struct mwl8k_priv *priv = hw->priv;
2711 u32 status;
2712
2713 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2714 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2715
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002716 if (!status)
2717 return IRQ_NONE;
2718
2719 if (status & MWL8K_A2H_INT_TX_DONE)
2720 tasklet_schedule(&priv->tx_reclaim_task);
2721
2722 if (status & MWL8K_A2H_INT_RX_READY) {
2723 while (rxq_process(hw, 0, 1))
2724 rxq_refill(hw, 0, 1);
2725 }
2726
2727 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002728 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002729 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002730 }
2731
2732 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002733 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002734 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002735 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002736 }
2737
2738 return IRQ_HANDLED;
2739}
2740
2741
2742/*
2743 * Core driver operations.
2744 */
2745static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2746{
2747 struct mwl8k_priv *priv = hw->priv;
2748 int index = skb_get_queue_mapping(skb);
2749 int rc;
2750
2751 if (priv->current_channel == NULL) {
2752 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002753 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002754 dev_kfree_skb(skb);
2755 return NETDEV_TX_OK;
2756 }
2757
2758 rc = mwl8k_txq_xmit(hw, index, skb);
2759
2760 return rc;
2761}
2762
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002763static int mwl8k_start(struct ieee80211_hw *hw)
2764{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002765 struct mwl8k_priv *priv = hw->priv;
2766 int rc;
2767
Joe Perchesa0607fd2009-11-18 23:29:17 -08002768 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002769 IRQF_SHARED, MWL8K_NAME, hw);
2770 if (rc) {
2771 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002772 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002773 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002774 }
2775
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002776 /* Enable tx reclaim tasklet */
2777 tasklet_enable(&priv->tx_reclaim_task);
2778
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002779 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002780 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002781
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002782 rc = mwl8k_fw_lock(hw);
2783 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002784 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002785
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002786 if (!priv->ap_fw) {
2787 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002788 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002789
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002790 if (!rc)
2791 rc = mwl8k_cmd_set_pre_scan(hw);
2792
2793 if (!rc)
2794 rc = mwl8k_cmd_set_post_scan(hw,
2795 "\x00\x00\x00\x00\x00\x00");
2796 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002797
2798 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002799 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002800
2801 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002802 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002803
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002804 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002805 }
2806
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002807 if (rc) {
2808 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2809 free_irq(priv->pdev->irq, hw);
2810 tasklet_disable(&priv->tx_reclaim_task);
2811 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002812
2813 return rc;
2814}
2815
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002816static void mwl8k_stop(struct ieee80211_hw *hw)
2817{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002818 struct mwl8k_priv *priv = hw->priv;
2819 int i;
2820
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002821 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002822
2823 ieee80211_stop_queues(hw);
2824
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002825 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002826 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002827 free_irq(priv->pdev->irq, hw);
2828
2829 /* Stop finalize join worker */
2830 cancel_work_sync(&priv->finalize_join_worker);
2831 if (priv->beacon_skb != NULL)
2832 dev_kfree_skb(priv->beacon_skb);
2833
2834 /* Stop tx reclaim tasklet */
2835 tasklet_disable(&priv->tx_reclaim_task);
2836
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002837 /* Return all skbs to mac80211 */
2838 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2839 mwl8k_txq_reclaim(hw, i, 1);
2840}
2841
2842static int mwl8k_add_interface(struct ieee80211_hw *hw,
2843 struct ieee80211_if_init_conf *conf)
2844{
2845 struct mwl8k_priv *priv = hw->priv;
2846 struct mwl8k_vif *mwl8k_vif;
2847
2848 /*
2849 * We only support one active interface at a time.
2850 */
2851 if (priv->vif != NULL)
2852 return -EBUSY;
2853
2854 /*
2855 * We only support managed interfaces for now.
2856 */
Lennert Buytenhek240e86e2009-07-16 14:15:44 +02002857 if (conf->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002858 return -EINVAL;
2859
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002860 /*
2861 * Reject interface creation if sniffer mode is active, as
2862 * STA operation is mutually exclusive with hardware sniffer
2863 * mode.
2864 */
2865 if (priv->sniffer_enabled) {
2866 printk(KERN_INFO "%s: unable to create STA "
2867 "interface due to sniffer mode being enabled\n",
2868 wiphy_name(hw->wiphy));
2869 return -EINVAL;
2870 }
2871
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002872 /* Clean out driver private area */
2873 mwl8k_vif = MWL8K_VIF(conf->vif);
2874 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2875
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002876 /* Set and save the mac address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002877 mwl8k_cmd_set_mac_addr(hw, conf->mac_addr);
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002878 memcpy(mwl8k_vif->mac_addr, conf->mac_addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002879
2880 /* Back pointer to parent config block */
2881 mwl8k_vif->priv = priv;
2882
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002883 /* Set Initial sequence number to zero */
2884 mwl8k_vif->seqno = 0;
2885
2886 priv->vif = conf->vif;
2887 priv->current_channel = NULL;
2888
2889 return 0;
2890}
2891
2892static void mwl8k_remove_interface(struct ieee80211_hw *hw,
2893 struct ieee80211_if_init_conf *conf)
2894{
2895 struct mwl8k_priv *priv = hw->priv;
2896
2897 if (priv->vif == NULL)
2898 return;
2899
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002900 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002901
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002902 priv->vif = NULL;
2903}
2904
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002905static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002906{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002907 struct ieee80211_conf *conf = &hw->conf;
2908 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002909 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002910
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002911 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002912 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002913 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002914 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002915 }
2916
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002917 rc = mwl8k_fw_lock(hw);
2918 if (rc)
2919 return rc;
2920
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002921 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002922 if (rc)
2923 goto out;
2924
2925 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2926 if (rc)
2927 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002928
2929 priv->current_channel = conf->channel;
2930
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002931 if (conf->power_level > 18)
2932 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002933 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002934 if (rc)
2935 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002936
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002937 if (priv->ap_fw) {
2938 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2939 if (!rc)
2940 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2941 } else {
2942 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2943 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002944
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002945out:
2946 mwl8k_fw_unlock(hw);
2947
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002948 return rc;
2949}
2950
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002951static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2952 struct ieee80211_vif *vif,
2953 struct ieee80211_bss_conf *info,
2954 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002955{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002956 struct mwl8k_priv *priv = hw->priv;
2957 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002958 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002959
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002960 if ((changed & BSS_CHANGED_ASSOC) == 0)
2961 return;
2962
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002963 priv->capture_beacon = false;
2964
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002965 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002966 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002967 return;
2968
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002969 if (info->assoc) {
2970 memcpy(&mwl8k_vif->bss_info, info,
2971 sizeof(struct ieee80211_bss_conf));
2972
Lennert Buytenhekd1844d72009-11-30 18:13:56 +01002973 memcpy(mwl8k_vif->bssid, info->bssid, ETH_ALEN);
2974
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002975 /* Install rates */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002976 rc = mwl8k_cmd_set_rate(hw, vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002977 if (rc)
2978 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002979
2980 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002981 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
2982 MWL8K_UCAST_RATE, NULL);
2983 if (rc)
2984 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002985
2986 /* Set radio preamble */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002987 rc = mwl8k_set_radio_preamble(hw, info->use_short_preamble);
2988 if (rc)
2989 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002990
2991 /* Set slot time */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002992 rc = mwl8k_cmd_set_slot(hw, info->use_short_slot);
2993 if (rc)
2994 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002995
2996 /* Update peer rate info */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002997 rc = mwl8k_cmd_update_stadb(hw, vif,
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002998 MWL8K_STA_DB_MODIFY_ENTRY);
2999 if (rc)
3000 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003001
3002 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003003 rc = mwl8k_cmd_set_aid(hw, vif);
3004 if (rc)
3005 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003006
3007 /*
3008 * Finalize the join. Tell rx handler to process
3009 * next beacon from our BSSID.
3010 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003011 memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003012 priv->capture_beacon = true;
3013 } else {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003014 rc = mwl8k_cmd_update_stadb(hw, vif, MWL8K_STA_DB_DEL_ENTRY);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003015 memset(&mwl8k_vif->bss_info, 0,
3016 sizeof(struct ieee80211_bss_conf));
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003017 memset(mwl8k_vif->bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003018 }
3019
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003020out:
3021 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003022}
3023
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003024static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3025 int mc_count, struct dev_addr_list *mclist)
3026{
3027 struct mwl8k_cmd_pkt *cmd;
3028
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003029 /*
3030 * Synthesize and return a command packet that programs the
3031 * hardware multicast address filter. At this point we don't
3032 * know whether FIF_ALLMULTI is being requested, but if it is,
3033 * we'll end up throwing this packet away and creating a new
3034 * one in mwl8k_configure_filter().
3035 */
3036 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003037
3038 return (unsigned long)cmd;
3039}
3040
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003041static int
3042mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3043 unsigned int changed_flags,
3044 unsigned int *total_flags)
3045{
3046 struct mwl8k_priv *priv = hw->priv;
3047
3048 /*
3049 * Hardware sniffer mode is mutually exclusive with STA
3050 * operation, so refuse to enable sniffer mode if a STA
3051 * interface is active.
3052 */
3053 if (priv->vif != NULL) {
3054 if (net_ratelimit())
3055 printk(KERN_INFO "%s: not enabling sniffer "
3056 "mode because STA interface is active\n",
3057 wiphy_name(hw->wiphy));
3058 return 0;
3059 }
3060
3061 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003062 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003063 return 0;
3064 priv->sniffer_enabled = true;
3065 }
3066
3067 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3068 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3069 FIF_OTHER_BSS;
3070
3071 return 1;
3072}
3073
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003074static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3075 unsigned int changed_flags,
3076 unsigned int *total_flags,
3077 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003078{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003079 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003080 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3081
3082 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003083 * AP firmware doesn't allow fine-grained control over
3084 * the receive filter.
3085 */
3086 if (priv->ap_fw) {
3087 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3088 kfree(cmd);
3089 return;
3090 }
3091
3092 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003093 * Enable hardware sniffer mode if FIF_CONTROL or
3094 * FIF_OTHER_BSS is requested.
3095 */
3096 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3097 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3098 kfree(cmd);
3099 return;
3100 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003101
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003102 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003103 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003104
3105 if (mwl8k_fw_lock(hw))
3106 return;
3107
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003108 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003109 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003110 priv->sniffer_enabled = false;
3111 }
3112
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003113 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003114 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3115 /*
3116 * Disable the BSS filter.
3117 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003118 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003119 } else {
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003120 u8 *bssid;
3121
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003122 /*
3123 * Enable the BSS filter.
3124 *
3125 * If there is an active STA interface, use that
3126 * interface's BSSID, otherwise use a dummy one
3127 * (where the OUI part needs to be nonzero for
3128 * the BSSID to be accepted by POST_SCAN).
3129 */
3130 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003131 if (priv->vif != NULL)
3132 bssid = MWL8K_VIF(priv->vif)->bssid;
3133
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003134 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003135 }
3136 }
3137
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003138 /*
3139 * If FIF_ALLMULTI is being requested, throw away the command
3140 * packet that ->prepare_multicast() built and replace it with
3141 * a command packet that enables reception of all multicast
3142 * packets.
3143 */
3144 if (*total_flags & FIF_ALLMULTI) {
3145 kfree(cmd);
3146 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3147 }
3148
3149 if (cmd != NULL) {
3150 mwl8k_post_cmd(hw, cmd);
3151 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003152 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003153
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003154 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003155}
3156
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003157static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3158{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003159 return mwl8k_cmd_set_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003160}
3161
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003162static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3163 const struct ieee80211_tx_queue_params *params)
3164{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003165 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003166 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003167
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003168 rc = mwl8k_fw_lock(hw);
3169 if (!rc) {
3170 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003171 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003172
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003173 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003174 rc = mwl8k_cmd_set_edca_params(hw, queue,
3175 params->cw_min,
3176 params->cw_max,
3177 params->aifs,
3178 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003179
3180 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003181 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003182
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003183 return rc;
3184}
3185
3186static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3187 struct ieee80211_tx_queue_stats *stats)
3188{
3189 struct mwl8k_priv *priv = hw->priv;
3190 struct mwl8k_tx_queue *txq;
3191 int index;
3192
3193 spin_lock_bh(&priv->tx_lock);
3194 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3195 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003196 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003197 sizeof(struct ieee80211_tx_queue_stats));
3198 }
3199 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003200
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003201 return 0;
3202}
3203
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003204static int mwl8k_get_stats(struct ieee80211_hw *hw,
3205 struct ieee80211_low_level_stats *stats)
3206{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003207 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003208}
3209
3210static const struct ieee80211_ops mwl8k_ops = {
3211 .tx = mwl8k_tx,
3212 .start = mwl8k_start,
3213 .stop = mwl8k_stop,
3214 .add_interface = mwl8k_add_interface,
3215 .remove_interface = mwl8k_remove_interface,
3216 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003217 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003218 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003219 .configure_filter = mwl8k_configure_filter,
3220 .set_rts_threshold = mwl8k_set_rts_threshold,
3221 .conf_tx = mwl8k_conf_tx,
3222 .get_tx_stats = mwl8k_get_tx_stats,
3223 .get_stats = mwl8k_get_stats,
3224};
3225
3226static void mwl8k_tx_reclaim_handler(unsigned long data)
3227{
3228 int i;
3229 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3230 struct mwl8k_priv *priv = hw->priv;
3231
3232 spin_lock_bh(&priv->tx_lock);
3233 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3234 mwl8k_txq_reclaim(hw, i, 0);
3235
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003236 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003237 complete(priv->tx_wait);
3238 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003239 }
3240 spin_unlock_bh(&priv->tx_lock);
3241}
3242
3243static void mwl8k_finalize_join_worker(struct work_struct *work)
3244{
3245 struct mwl8k_priv *priv =
3246 container_of(work, struct mwl8k_priv, finalize_join_worker);
3247 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003248 u8 dtim = MWL8K_VIF(priv->vif)->bss_info.dtim_period;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003249
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003250 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003251 dev_kfree_skb(skb);
3252
3253 priv->beacon_skb = NULL;
3254}
3255
John W. Linvillebcb628d2009-11-06 16:40:16 -05003256enum {
3257 MWL8687 = 0,
3258 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003259};
3260
John W. Linvillebcb628d2009-11-06 16:40:16 -05003261static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003262 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003263 .part_name = "88w8687",
3264 .helper_image = "mwl8k/helper_8687.fw",
3265 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003266 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003267 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003268 .part_name = "88w8366",
3269 .helper_image = "mwl8k/helper_8366.fw",
3270 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003271 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003272 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003273};
3274
3275static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003276 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3277 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3278 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3279 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003280};
3281MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3282
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003283static int __devinit mwl8k_probe(struct pci_dev *pdev,
3284 const struct pci_device_id *id)
3285{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003286 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003287 struct ieee80211_hw *hw;
3288 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003289 int rc;
3290 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003291
3292 if (!printed_version) {
3293 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3294 printed_version = 1;
3295 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003296
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003297
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003298 rc = pci_enable_device(pdev);
3299 if (rc) {
3300 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3301 MWL8K_NAME);
3302 return rc;
3303 }
3304
3305 rc = pci_request_regions(pdev, MWL8K_NAME);
3306 if (rc) {
3307 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3308 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003309 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003310 }
3311
3312 pci_set_master(pdev);
3313
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003314
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003315 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3316 if (hw == NULL) {
3317 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3318 rc = -ENOMEM;
3319 goto err_free_reg;
3320 }
3321
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003322 SET_IEEE80211_DEV(hw, &pdev->dev);
3323 pci_set_drvdata(pdev, hw);
3324
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003325 priv = hw->priv;
3326 priv->hw = hw;
3327 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003328 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003329
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003330
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003331 priv->sram = pci_iomap(pdev, 0, 0x10000);
3332 if (priv->sram == NULL) {
3333 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003334 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003335 goto err_iounmap;
3336 }
3337
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003338 /*
3339 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3340 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3341 */
3342 priv->regs = pci_iomap(pdev, 1, 0x10000);
3343 if (priv->regs == NULL) {
3344 priv->regs = pci_iomap(pdev, 2, 0x10000);
3345 if (priv->regs == NULL) {
3346 printk(KERN_ERR "%s: Cannot map device registers\n",
3347 wiphy_name(hw->wiphy));
3348 goto err_iounmap;
3349 }
3350 }
3351
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003352
3353 /* Reset firmware and hardware */
3354 mwl8k_hw_reset(priv);
3355
3356 /* Ask userland hotplug daemon for the device firmware */
3357 rc = mwl8k_request_firmware(priv);
3358 if (rc) {
3359 printk(KERN_ERR "%s: Firmware files not found\n",
3360 wiphy_name(hw->wiphy));
3361 goto err_stop_firmware;
3362 }
3363
3364 /* Load firmware into hardware */
3365 rc = mwl8k_load_firmware(hw);
3366 if (rc) {
3367 printk(KERN_ERR "%s: Cannot start firmware\n",
3368 wiphy_name(hw->wiphy));
3369 goto err_stop_firmware;
3370 }
3371
3372 /* Reclaim memory once firmware is successfully loaded */
3373 mwl8k_release_firmware(priv);
3374
3375
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003376 if (priv->ap_fw)
3377 priv->rxd_ops = priv->device_info->ap_rxd_ops;
3378 else
3379 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003380
3381 priv->sniffer_enabled = false;
3382 priv->wmm_enabled = false;
3383 priv->pending_tx_pkts = 0;
3384
3385
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003386 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3387 priv->band.band = IEEE80211_BAND_2GHZ;
3388 priv->band.channels = priv->channels;
3389 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3390 priv->band.bitrates = priv->rates;
3391 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3392 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3393
3394 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3395 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3396
3397 /*
3398 * Extra headroom is the size of the required DMA header
3399 * minus the size of the smallest 802.11 frame (CTS frame).
3400 */
3401 hw->extra_tx_headroom =
3402 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3403
3404 hw->channel_change_time = 10;
3405
3406 hw->queues = MWL8K_TX_QUEUES;
3407
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003408 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003409 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003410 hw->vif_data_size = sizeof(struct mwl8k_vif);
3411 priv->vif = NULL;
3412
3413 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003414 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003415 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003416
3417 /* Finalize join worker */
3418 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3419
3420 /* TX reclaim tasklet */
3421 tasklet_init(&priv->tx_reclaim_task,
3422 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3423 tasklet_disable(&priv->tx_reclaim_task);
3424
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003425 /* Power management cookie */
3426 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3427 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003428 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003429
3430 rc = mwl8k_rxq_init(hw, 0);
3431 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003432 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003433 rxq_refill(hw, 0, INT_MAX);
3434
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003435 mutex_init(&priv->fw_mutex);
3436 priv->fw_mutex_owner = NULL;
3437 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003438 priv->hostcmd_wait = NULL;
3439
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003440 spin_lock_init(&priv->tx_lock);
3441
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003442 priv->tx_wait = NULL;
3443
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003444 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3445 rc = mwl8k_txq_init(hw, i);
3446 if (rc)
3447 goto err_free_queues;
3448 }
3449
3450 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003451 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003452 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3453 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3454
Joe Perchesa0607fd2009-11-18 23:29:17 -08003455 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003456 IRQF_SHARED, MWL8K_NAME, hw);
3457 if (rc) {
3458 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003459 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003460 goto err_free_queues;
3461 }
3462
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003463 /*
3464 * Temporarily enable interrupts. Initial firmware host
3465 * commands use interrupts and avoids polling. Disable
3466 * interrupts when done.
3467 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003468 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003469
3470 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003471 if (priv->ap_fw) {
3472 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3473 if (!rc)
3474 rc = mwl8k_cmd_set_hw_spec(hw);
3475 } else {
3476 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003477
3478 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003479 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003480 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003481 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3482 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003483 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003484 }
3485
3486 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003487 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003488 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003489 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003490 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003491 }
3492
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003493 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003494 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003495 if (rc) {
3496 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3497 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003498 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003499 }
3500
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003501 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003502 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003503 free_irq(priv->pdev->irq, hw);
3504
3505 rc = ieee80211_register_hw(hw);
3506 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003507 printk(KERN_ERR "%s: Cannot register device\n",
3508 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003509 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003510 }
3511
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003512 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003513 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003514 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003515 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003516 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3517 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003518
3519 return 0;
3520
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003521err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003522 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003523 free_irq(priv->pdev->irq, hw);
3524
3525err_free_queues:
3526 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3527 mwl8k_txq_deinit(hw, i);
3528 mwl8k_rxq_deinit(hw, 0);
3529
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003530err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003531 if (priv->cookie != NULL)
3532 pci_free_consistent(priv->pdev, 4,
3533 priv->cookie, priv->cookie_dma);
3534
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003535err_stop_firmware:
3536 mwl8k_hw_reset(priv);
3537 mwl8k_release_firmware(priv);
3538
3539err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003540 if (priv->regs != NULL)
3541 pci_iounmap(pdev, priv->regs);
3542
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003543 if (priv->sram != NULL)
3544 pci_iounmap(pdev, priv->sram);
3545
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003546 pci_set_drvdata(pdev, NULL);
3547 ieee80211_free_hw(hw);
3548
3549err_free_reg:
3550 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003551
3552err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003553 pci_disable_device(pdev);
3554
3555 return rc;
3556}
3557
Joerg Albert230f7af2009-04-18 02:10:45 +02003558static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003559{
3560 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3561}
3562
Joerg Albert230f7af2009-04-18 02:10:45 +02003563static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003564{
3565 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3566 struct mwl8k_priv *priv;
3567 int i;
3568
3569 if (hw == NULL)
3570 return;
3571 priv = hw->priv;
3572
3573 ieee80211_stop_queues(hw);
3574
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003575 ieee80211_unregister_hw(hw);
3576
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003577 /* Remove tx reclaim tasklet */
3578 tasklet_kill(&priv->tx_reclaim_task);
3579
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003580 /* Stop hardware */
3581 mwl8k_hw_reset(priv);
3582
3583 /* Return all skbs to mac80211 */
3584 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3585 mwl8k_txq_reclaim(hw, i, 1);
3586
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003587 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3588 mwl8k_txq_deinit(hw, i);
3589
3590 mwl8k_rxq_deinit(hw, 0);
3591
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003592 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003593
3594 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003595 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003596 pci_set_drvdata(pdev, NULL);
3597 ieee80211_free_hw(hw);
3598 pci_release_regions(pdev);
3599 pci_disable_device(pdev);
3600}
3601
3602static struct pci_driver mwl8k_driver = {
3603 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003604 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003605 .probe = mwl8k_probe,
3606 .remove = __devexit_p(mwl8k_remove),
3607 .shutdown = __devexit_p(mwl8k_shutdown),
3608};
3609
3610static int __init mwl8k_init(void)
3611{
3612 return pci_register_driver(&mwl8k_driver);
3613}
3614
3615static void __exit mwl8k_exit(void)
3616{
3617 pci_unregister_driver(&mwl8k_driver);
3618}
3619
3620module_init(mwl8k_init);
3621module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003622
3623MODULE_DESCRIPTION(MWL8K_DESC);
3624MODULE_VERSION(MWL8K_VERSION);
3625MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3626MODULE_LICENSE("GPL");