blob: 18542fba1f43c87bb75fed560e707f9b036d8e3d [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 Buytenhek54bc3a02009-10-22 20:20:59 +020095 struct rxd_ops *rxd_ops;
Lennert Buytenhek547810e2009-10-22 20:21:02 +020096 u16 modes;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020097};
98
Lennert Buytenheka66098d2009-03-10 10:13:33 +010099struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200100 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100101
102 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200103 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100104
105 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200106 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100107
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200108 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200109 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200110 struct {
111 struct sk_buff *skb;
112 DECLARE_PCI_UNMAP_ADDR(dma)
113 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100114};
115
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100116struct mwl8k_tx_queue {
117 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200118 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100119
120 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200121 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100122
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200123 struct ieee80211_tx_queue_stats stats;
124 struct mwl8k_tx_desc *txd;
125 dma_addr_t txd_dma;
126 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100127};
128
129/* Pointers to the firmware data and meta information about it. */
130struct mwl8k_firmware {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131 /* Boot helper code */
132 struct firmware *helper;
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200133
134 /* Microcode */
135 struct firmware *ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100136};
137
138struct mwl8k_priv {
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +0200139 void __iomem *sram;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140 void __iomem *regs;
141 struct ieee80211_hw *hw;
142
143 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100144
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200145 struct mwl8k_device_info *device_info;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200146 bool ap_fw;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200147 struct rxd_ops *rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200148
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100149 /* firmware files and meta data */
150 struct mwl8k_firmware fw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100151
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200152 /* firmware access */
153 struct mutex fw_mutex;
154 struct task_struct *fw_mutex_owner;
155 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200156 struct completion *hostcmd_wait;
157
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158 /* lock held over TX and TX reap */
159 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100160
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200161 /* TX quiesce completion, protected by fw_mutex and tx_lock */
162 struct completion *tx_wait;
163
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100164 struct ieee80211_vif *vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100165
166 struct ieee80211_channel *current_channel;
167
168 /* power management status cookie from firmware */
169 u32 *cookie;
170 dma_addr_t cookie_dma;
171
172 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100173 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200174 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100175
176 /*
177 * Running count of TX packets in flight, to avoid
178 * iterating over the transmit rings each time.
179 */
180 int pending_tx_pkts;
181
182 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
183 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
184
185 /* PHY parameters */
186 struct ieee80211_supported_band band;
187 struct ieee80211_channel channels[14];
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100188 struct ieee80211_rate rates[14];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100189
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200190 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200191 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200192 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200193 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100194
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 /* XXX need to convert this to handle multiple interfaces */
196 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200197 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100198 struct sk_buff *beacon_skb;
199
200 /*
201 * This FJ worker has to be global as it is scheduled from the
202 * RX handler. At this point we don't know which interface it
203 * belongs to until the list of bssids waiting to complete join
204 * is checked.
205 */
206 struct work_struct finalize_join_worker;
207
208 /* Tasklet to reclaim TX descriptors and buffers after tx */
209 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100210};
211
212/* Per interface specific private data */
213struct mwl8k_vif {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100214 /* backpointer to parent config block */
215 struct mwl8k_priv *priv;
216
217 /* BSS config of AP or IBSS from mac80211*/
218 struct ieee80211_bss_conf bss_info;
219
220 /* BSSID of AP or IBSS */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200221 u8 bssid[ETH_ALEN];
222 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100223
Lennert Buytenhek55489b62009-11-30 18:31:33 +0100224 /* Index into station database. Returned by UPDATE_STADB. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225 u8 peer_id;
226
227 /* Non AMPDU sequence number assigned by driver */
228 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100229};
230
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200231#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100232
233static const struct ieee80211_channel mwl8k_channels[] = {
234 { .center_freq = 2412, .hw_value = 1, },
235 { .center_freq = 2417, .hw_value = 2, },
236 { .center_freq = 2422, .hw_value = 3, },
237 { .center_freq = 2427, .hw_value = 4, },
238 { .center_freq = 2432, .hw_value = 5, },
239 { .center_freq = 2437, .hw_value = 6, },
240 { .center_freq = 2442, .hw_value = 7, },
241 { .center_freq = 2447, .hw_value = 8, },
242 { .center_freq = 2452, .hw_value = 9, },
243 { .center_freq = 2457, .hw_value = 10, },
244 { .center_freq = 2462, .hw_value = 11, },
245};
246
247static const struct ieee80211_rate mwl8k_rates[] = {
248 { .bitrate = 10, .hw_value = 2, },
249 { .bitrate = 20, .hw_value = 4, },
250 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200251 { .bitrate = 110, .hw_value = 22, },
252 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100253 { .bitrate = 60, .hw_value = 12, },
254 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100255 { .bitrate = 120, .hw_value = 24, },
256 { .bitrate = 180, .hw_value = 36, },
257 { .bitrate = 240, .hw_value = 48, },
258 { .bitrate = 360, .hw_value = 72, },
259 { .bitrate = 480, .hw_value = 96, },
260 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100261 { .bitrate = 720, .hw_value = 144, },
262};
263
264static const u8 mwl8k_rateids[12] = {
265 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108,
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100266};
267
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100268/* Set or get info from Firmware */
269#define MWL8K_CMD_SET 0x0001
270#define MWL8K_CMD_GET 0x0000
271
272/* Firmware command codes */
273#define MWL8K_CMD_CODE_DNLD 0x0001
274#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200275#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100276#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
277#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200278#define MWL8K_CMD_RADIO_CONTROL 0x001c
279#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200280#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100281#define MWL8K_CMD_SET_PRE_SCAN 0x0107
282#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200283#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100284#define MWL8K_CMD_SET_AID 0x010d
285#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200286#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100287#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200288#define MWL8K_CMD_SET_SLOT 0x0114
289#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
290#define MWL8K_CMD_SET_WMM_MODE 0x0123
291#define MWL8K_CMD_MIMO_CONFIG 0x0125
292#define MWL8K_CMD_USE_FIXED_RATE 0x0126
293#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200294#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200295#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
296#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100297
298static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
299{
300#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
301 snprintf(buf, bufsize, "%s", #x);\
302 return buf;\
303 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200304 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100305 MWL8K_CMDNAME(CODE_DNLD);
306 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200307 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100308 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
309 MWL8K_CMDNAME(GET_STAT);
310 MWL8K_CMDNAME(RADIO_CONTROL);
311 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200312 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100313 MWL8K_CMDNAME(SET_PRE_SCAN);
314 MWL8K_CMDNAME(SET_POST_SCAN);
315 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100316 MWL8K_CMDNAME(SET_AID);
317 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200318 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100319 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200320 MWL8K_CMDNAME(SET_SLOT);
321 MWL8K_CMDNAME(SET_EDCA_PARAMS);
322 MWL8K_CMDNAME(SET_WMM_MODE);
323 MWL8K_CMDNAME(MIMO_CONFIG);
324 MWL8K_CMDNAME(USE_FIXED_RATE);
325 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200326 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200327 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
328 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100329 default:
330 snprintf(buf, bufsize, "0x%x", cmd);
331 }
332#undef MWL8K_CMDNAME
333
334 return buf;
335}
336
337/* Hardware and firmware reset */
338static void mwl8k_hw_reset(struct mwl8k_priv *priv)
339{
340 iowrite32(MWL8K_H2A_INT_RESET,
341 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
342 iowrite32(MWL8K_H2A_INT_RESET,
343 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
344 msleep(20);
345}
346
347/* Release fw image */
348static void mwl8k_release_fw(struct firmware **fw)
349{
350 if (*fw == NULL)
351 return;
352 release_firmware(*fw);
353 *fw = NULL;
354}
355
356static void mwl8k_release_firmware(struct mwl8k_priv *priv)
357{
358 mwl8k_release_fw(&priv->fw.ucode);
359 mwl8k_release_fw(&priv->fw.helper);
360}
361
362/* Request fw image */
363static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200364 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100365{
366 /* release current image */
367 if (*fw != NULL)
368 mwl8k_release_fw(fw);
369
370 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200371 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100372}
373
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200374static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100375{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200376 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100377 int rc;
378
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200379 if (di->helper_image != NULL) {
380 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw.helper);
381 if (rc) {
382 printk(KERN_ERR "%s: Error requesting helper "
383 "firmware file %s\n", pci_name(priv->pdev),
384 di->helper_image);
385 return rc;
386 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100387 }
388
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200389 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw.ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100390 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200391 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200392 pci_name(priv->pdev), di->fw_image);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100393 mwl8k_release_fw(&priv->fw.helper);
394 return rc;
395 }
396
397 return 0;
398}
399
Ben Hutchings7e75b942009-11-07 22:00:57 +0000400MODULE_FIRMWARE("mwl8k/helper_8687.fw");
401MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
402
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100403struct mwl8k_cmd_pkt {
404 __le16 code;
405 __le16 length;
406 __le16 seq_num;
407 __le16 result;
408 char payload[0];
409} __attribute__((packed));
410
411/*
412 * Firmware loading.
413 */
414static int
415mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
416{
417 void __iomem *regs = priv->regs;
418 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100419 int loops;
420
421 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
422 if (pci_dma_mapping_error(priv->pdev, dma_addr))
423 return -ENOMEM;
424
425 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
426 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
427 iowrite32(MWL8K_H2A_INT_DOORBELL,
428 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
429 iowrite32(MWL8K_H2A_INT_DUMMY,
430 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
431
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100432 loops = 1000;
433 do {
434 u32 int_code;
435
436 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
437 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
438 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100439 break;
440 }
441
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200442 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100443 udelay(1);
444 } while (--loops);
445
446 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
447
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200448 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100449}
450
451static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
452 const u8 *data, size_t length)
453{
454 struct mwl8k_cmd_pkt *cmd;
455 int done;
456 int rc = 0;
457
458 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
459 if (cmd == NULL)
460 return -ENOMEM;
461
462 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
463 cmd->seq_num = 0;
464 cmd->result = 0;
465
466 done = 0;
467 while (length) {
468 int block_size = length > 256 ? 256 : length;
469
470 memcpy(cmd->payload, data + done, block_size);
471 cmd->length = cpu_to_le16(block_size);
472
473 rc = mwl8k_send_fw_load_cmd(priv, cmd,
474 sizeof(*cmd) + block_size);
475 if (rc)
476 break;
477
478 done += block_size;
479 length -= block_size;
480 }
481
482 if (!rc) {
483 cmd->length = 0;
484 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
485 }
486
487 kfree(cmd);
488
489 return rc;
490}
491
492static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
493 const u8 *data, size_t length)
494{
495 unsigned char *buffer;
496 int may_continue, rc = 0;
497 u32 done, prev_block_size;
498
499 buffer = kmalloc(1024, GFP_KERNEL);
500 if (buffer == NULL)
501 return -ENOMEM;
502
503 done = 0;
504 prev_block_size = 0;
505 may_continue = 1000;
506 while (may_continue > 0) {
507 u32 block_size;
508
509 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
510 if (block_size & 1) {
511 block_size &= ~1;
512 may_continue--;
513 } else {
514 done += prev_block_size;
515 length -= prev_block_size;
516 }
517
518 if (block_size > 1024 || block_size > length) {
519 rc = -EOVERFLOW;
520 break;
521 }
522
523 if (length == 0) {
524 rc = 0;
525 break;
526 }
527
528 if (block_size == 0) {
529 rc = -EPROTO;
530 may_continue--;
531 udelay(1);
532 continue;
533 }
534
535 prev_block_size = block_size;
536 memcpy(buffer, data + done, block_size);
537
538 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
539 if (rc)
540 break;
541 }
542
543 if (!rc && length != 0)
544 rc = -EREMOTEIO;
545
546 kfree(buffer);
547
548 return rc;
549}
550
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200551static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100552{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200553 struct mwl8k_priv *priv = hw->priv;
554 struct firmware *fw = priv->fw.ucode;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200555 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200556 int rc;
557 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100558
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200559 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
560 struct firmware *helper = priv->fw.helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100561
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200562 if (helper == NULL) {
563 printk(KERN_ERR "%s: helper image needed but none "
564 "given\n", pci_name(priv->pdev));
565 return -EINVAL;
566 }
567
568 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100569 if (rc) {
570 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200571 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100572 return rc;
573 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100574 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100575
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200576 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100577 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200578 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100579 }
580
581 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200582 printk(KERN_ERR "%s: unable to load firmware image\n",
583 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100584 return rc;
585 }
586
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200587 if (di->modes & BIT(NL80211_IFTYPE_AP))
588 iowrite32(MWL8K_MODE_AP, priv->regs + MWL8K_HIU_GEN_PTR);
589 else
590 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100591
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100592 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100593 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200594 u32 ready_code;
595
596 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
597 if (ready_code == MWL8K_FWAP_READY) {
598 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100599 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200600 } else if (ready_code == MWL8K_FWSTA_READY) {
601 priv->ap_fw = 0;
602 break;
603 }
604
605 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100606 udelay(1);
607 } while (--loops);
608
609 return loops ? 0 : -ETIMEDOUT;
610}
611
612
613/*
614 * Defines shared between transmission and reception.
615 */
616/* HT control fields for firmware */
617struct ewc_ht_info {
618 __le16 control1;
619 __le16 control2;
620 __le16 control3;
621} __attribute__((packed));
622
623/* Firmware Station database operations */
624#define MWL8K_STA_DB_ADD_ENTRY 0
625#define MWL8K_STA_DB_MODIFY_ENTRY 1
626#define MWL8K_STA_DB_DEL_ENTRY 2
627#define MWL8K_STA_DB_FLUSH 3
628
629/* Peer Entry flags - used to define the type of the peer node */
630#define MWL8K_PEER_TYPE_ACCESSPOINT 2
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100631
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100632struct peer_capability_info {
633 /* Peer type - AP vs. STA. */
634 __u8 peer_type;
635
636 /* Basic 802.11 capabilities from assoc resp. */
637 __le16 basic_caps;
638
639 /* Set if peer supports 802.11n high throughput (HT). */
640 __u8 ht_support;
641
642 /* Valid if HT is supported. */
643 __le16 ht_caps;
644 __u8 extended_ht_caps;
645 struct ewc_ht_info ewc_info;
646
647 /* Legacy rate table. Intersection of our rates and peer rates. */
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100648 __u8 legacy_rates[12];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100649
650 /* HT rate table. Intersection of our rates and peer rates. */
Lennert Buytenhek0b5351a2009-11-30 18:11:18 +0100651 __u8 ht_rates[16];
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +0200652 __u8 pad[16];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100653
654 /* If set, interoperability mode, no proprietary extensions. */
655 __u8 interop;
656 __u8 pad2;
657 __u8 station_id;
658 __le16 amsdu_enabled;
659} __attribute__((packed));
660
661/* Inline functions to manipulate QoS field in data descriptor. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100662static inline u16 mwl8k_qos_setbit_eosp(u16 qos)
663{
664 u16 val_mask = 1 << 4;
665
666 /* End of Service Period Bit 4 */
667 return qos | val_mask;
668}
669
670static inline u16 mwl8k_qos_setbit_ack(u16 qos, u8 ack_policy)
671{
672 u16 val_mask = 0x3;
673 u8 shift = 5;
674 u16 qos_mask = ~(val_mask << shift);
675
676 /* Ack Policy Bit 5-6 */
677 return (qos & qos_mask) | ((ack_policy & val_mask) << shift);
678}
679
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100680static inline u16 mwl8k_qos_setbit_qlen(u16 qos, u8 len)
681{
682 u16 val_mask = 0xff;
683 u8 shift = 8;
684 u16 qos_mask = ~(val_mask << shift);
685
686 /* Queue Length Bits 8-15 */
687 return (qos & qos_mask) | ((len & val_mask) << shift);
688}
689
690/* DMA header used by firmware and hardware. */
691struct mwl8k_dma_data {
692 __le16 fwlen;
693 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100694 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100695} __attribute__((packed));
696
697/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100698static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100699{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100700 struct mwl8k_dma_data *tr;
701 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100702
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100703 tr = (struct mwl8k_dma_data *)skb->data;
704 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
705
706 if (hdrlen != sizeof(tr->wh)) {
707 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
708 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
709 *((__le16 *)(tr->data - 2)) = qos;
710 } else {
711 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
712 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100713 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100714
715 if (hdrlen != sizeof(*tr))
716 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100717}
718
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200719static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100720{
721 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100722 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100723 struct mwl8k_dma_data *tr;
724
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100725 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100726 * Add a firmware DMA header; the firmware requires that we
727 * present a 2-byte payload length followed by a 4-address
728 * header (without QoS field), followed (optionally) by any
729 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100730 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100731 wh = (struct ieee80211_hdr *)skb->data;
732
733 hdrlen = ieee80211_hdrlen(wh->frame_control);
734 if (hdrlen != sizeof(*tr))
735 skb_push(skb, sizeof(*tr) - hdrlen);
736
737 if (ieee80211_is_data_qos(wh->frame_control))
738 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100739
740 tr = (struct mwl8k_dma_data *)skb->data;
741 if (wh != &tr->wh)
742 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100743 if (hdrlen != sizeof(tr->wh))
744 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100745
746 /*
747 * Firmware length is the length of the fully formed "802.11
748 * payload". That is, everything except for the 802.11 header.
749 * This includes all crypto material including the MIC.
750 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100751 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100752}
753
754
755/*
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200756 * Packet reception for 88w8366.
757 */
758struct mwl8k_rxd_8366 {
759 __le16 pkt_len;
760 __u8 sq2;
761 __u8 rate;
762 __le32 pkt_phys_addr;
763 __le32 next_rxd_phys_addr;
764 __le16 qos_control;
765 __le16 htsig2;
766 __le32 hw_rssi_info;
767 __le32 hw_noise_floor_info;
768 __u8 noise_floor;
769 __u8 pad0[3];
770 __u8 rssi;
771 __u8 rx_status;
772 __u8 channel;
773 __u8 rx_ctrl;
774} __attribute__((packed));
775
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100776#define MWL8K_8366_RATE_INFO_MCS_FORMAT 0x80
777#define MWL8K_8366_RATE_INFO_40MHZ 0x40
778#define MWL8K_8366_RATE_INFO_RATEID(x) ((x) & 0x3f)
779
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200780#define MWL8K_8366_RX_CTRL_OWNED_BY_HOST 0x80
781
782static void mwl8k_rxd_8366_init(void *_rxd, dma_addr_t next_dma_addr)
783{
784 struct mwl8k_rxd_8366 *rxd = _rxd;
785
786 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
787 rxd->rx_ctrl = MWL8K_8366_RX_CTRL_OWNED_BY_HOST;
788}
789
790static void mwl8k_rxd_8366_refill(void *_rxd, dma_addr_t addr, int len)
791{
792 struct mwl8k_rxd_8366 *rxd = _rxd;
793
794 rxd->pkt_len = cpu_to_le16(len);
795 rxd->pkt_phys_addr = cpu_to_le32(addr);
796 wmb();
797 rxd->rx_ctrl = 0;
798}
799
800static int
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100801mwl8k_rxd_8366_process(void *_rxd, struct ieee80211_rx_status *status,
802 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200803{
804 struct mwl8k_rxd_8366 *rxd = _rxd;
805
806 if (!(rxd->rx_ctrl & MWL8K_8366_RX_CTRL_OWNED_BY_HOST))
807 return -1;
808 rmb();
809
810 memset(status, 0, sizeof(*status));
811
812 status->signal = -rxd->rssi;
813 status->noise = -rxd->noise_floor;
814
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100815 if (rxd->rate & MWL8K_8366_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200816 status->flag |= RX_FLAG_HT;
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100817 if (rxd->rate & MWL8K_8366_RATE_INFO_40MHZ)
818 status->flag |= RX_FLAG_40MHZ;
819 status->rate_idx = MWL8K_8366_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200820 } else {
821 int i;
822
823 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
824 if (mwl8k_rates[i].hw_value == rxd->rate) {
825 status->rate_idx = i;
826 break;
827 }
828 }
829 }
830
831 status->band = IEEE80211_BAND_2GHZ;
832 status->freq = ieee80211_channel_to_frequency(rxd->channel);
833
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100834 *qos = rxd->qos_control;
835
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200836 return le16_to_cpu(rxd->pkt_len);
837}
838
839static struct rxd_ops rxd_8366_ops = {
840 .rxd_size = sizeof(struct mwl8k_rxd_8366),
841 .rxd_init = mwl8k_rxd_8366_init,
842 .rxd_refill = mwl8k_rxd_8366_refill,
843 .rxd_process = mwl8k_rxd_8366_process,
844};
845
846/*
847 * Packet reception for 88w8687.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100848 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200849struct mwl8k_rxd_8687 {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100850 __le16 pkt_len;
851 __u8 link_quality;
852 __u8 noise_level;
853 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200854 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100855 __le16 qos_control;
856 __le16 rate_info;
857 __le32 pad0[4];
858 __u8 rssi;
859 __u8 channel;
860 __le16 pad1;
861 __u8 rx_ctrl;
862 __u8 rx_status;
863 __u8 pad2[2];
864} __attribute__((packed));
865
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200866#define MWL8K_8687_RATE_INFO_SHORTPRE 0x8000
867#define MWL8K_8687_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
868#define MWL8K_8687_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
869#define MWL8K_8687_RATE_INFO_40MHZ 0x0004
870#define MWL8K_8687_RATE_INFO_SHORTGI 0x0002
871#define MWL8K_8687_RATE_INFO_MCS_FORMAT 0x0001
872
873#define MWL8K_8687_RX_CTRL_OWNED_BY_HOST 0x02
874
875static void mwl8k_rxd_8687_init(void *_rxd, dma_addr_t next_dma_addr)
876{
877 struct mwl8k_rxd_8687 *rxd = _rxd;
878
879 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
880 rxd->rx_ctrl = MWL8K_8687_RX_CTRL_OWNED_BY_HOST;
881}
882
883static void mwl8k_rxd_8687_refill(void *_rxd, dma_addr_t addr, int len)
884{
885 struct mwl8k_rxd_8687 *rxd = _rxd;
886
887 rxd->pkt_len = cpu_to_le16(len);
888 rxd->pkt_phys_addr = cpu_to_le32(addr);
889 wmb();
890 rxd->rx_ctrl = 0;
891}
892
893static int
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100894mwl8k_rxd_8687_process(void *_rxd, struct ieee80211_rx_status *status,
895 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200896{
897 struct mwl8k_rxd_8687 *rxd = _rxd;
898 u16 rate_info;
899
900 if (!(rxd->rx_ctrl & MWL8K_8687_RX_CTRL_OWNED_BY_HOST))
901 return -1;
902 rmb();
903
904 rate_info = le16_to_cpu(rxd->rate_info);
905
906 memset(status, 0, sizeof(*status));
907
908 status->signal = -rxd->rssi;
909 status->noise = -rxd->noise_level;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200910 status->antenna = MWL8K_8687_RATE_INFO_ANTSELECT(rate_info);
911 status->rate_idx = MWL8K_8687_RATE_INFO_RATEID(rate_info);
912
913 if (rate_info & MWL8K_8687_RATE_INFO_SHORTPRE)
914 status->flag |= RX_FLAG_SHORTPRE;
915 if (rate_info & MWL8K_8687_RATE_INFO_40MHZ)
916 status->flag |= RX_FLAG_40MHZ;
917 if (rate_info & MWL8K_8687_RATE_INFO_SHORTGI)
918 status->flag |= RX_FLAG_SHORT_GI;
919 if (rate_info & MWL8K_8687_RATE_INFO_MCS_FORMAT)
920 status->flag |= RX_FLAG_HT;
921
922 status->band = IEEE80211_BAND_2GHZ;
923 status->freq = ieee80211_channel_to_frequency(rxd->channel);
924
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100925 *qos = rxd->qos_control;
926
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200927 return le16_to_cpu(rxd->pkt_len);
928}
929
930static struct rxd_ops rxd_8687_ops = {
931 .rxd_size = sizeof(struct mwl8k_rxd_8687),
932 .rxd_init = mwl8k_rxd_8687_init,
933 .rxd_refill = mwl8k_rxd_8687_refill,
934 .rxd_process = mwl8k_rxd_8687_process,
935};
936
937
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100938#define MWL8K_RX_DESCS 256
939#define MWL8K_RX_MAXSZ 3800
940
941static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
942{
943 struct mwl8k_priv *priv = hw->priv;
944 struct mwl8k_rx_queue *rxq = priv->rxq + index;
945 int size;
946 int i;
947
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200948 rxq->rxd_count = 0;
949 rxq->head = 0;
950 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100951
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200952 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100953
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200954 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
955 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100956 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200957 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100958 return -ENOMEM;
959 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200960 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100961
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200962 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
963 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100964 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200965 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200966 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100967 return -ENOMEM;
968 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200969 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100970
971 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200972 int desc_size;
973 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100974 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200975 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100976
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200977 desc_size = priv->rxd_ops->rxd_size;
978 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100979
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200980 nexti = i + 1;
981 if (nexti == MWL8K_RX_DESCS)
982 nexti = 0;
983 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
984
985 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100986 }
987
988 return 0;
989}
990
991static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
992{
993 struct mwl8k_priv *priv = hw->priv;
994 struct mwl8k_rx_queue *rxq = priv->rxq + index;
995 int refilled;
996
997 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200998 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100999 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001000 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001001 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001002 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001003
1004 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1005 if (skb == NULL)
1006 break;
1007
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001008 addr = pci_map_single(priv->pdev, skb->data,
1009 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001010
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001011 rxq->rxd_count++;
1012 rx = rxq->tail++;
1013 if (rxq->tail == MWL8K_RX_DESCS)
1014 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001015 rxq->buf[rx].skb = skb;
1016 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001017
1018 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1019 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001020
1021 refilled++;
1022 }
1023
1024 return refilled;
1025}
1026
1027/* Must be called only when the card's reception is completely halted */
1028static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1029{
1030 struct mwl8k_priv *priv = hw->priv;
1031 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1032 int i;
1033
1034 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001035 if (rxq->buf[i].skb != NULL) {
1036 pci_unmap_single(priv->pdev,
1037 pci_unmap_addr(&rxq->buf[i], dma),
1038 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1039 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001040
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001041 kfree_skb(rxq->buf[i].skb);
1042 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001043 }
1044 }
1045
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001046 kfree(rxq->buf);
1047 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001048
1049 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001050 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001051 rxq->rxd, rxq->rxd_dma);
1052 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001053}
1054
1055
1056/*
1057 * Scan a list of BSSIDs to process for finalize join.
1058 * Allows for extension to process multiple BSSIDs.
1059 */
1060static inline int
1061mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1062{
1063 return priv->capture_beacon &&
1064 ieee80211_is_beacon(wh->frame_control) &&
1065 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1066}
1067
Lennert Buytenhek37797522009-10-22 20:19:45 +02001068static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1069 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001070{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001071 struct mwl8k_priv *priv = hw->priv;
1072
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001073 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001074 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001075
1076 /*
1077 * Use GFP_ATOMIC as rxq_process is called from
1078 * the primary interrupt handler, memory allocation call
1079 * must not sleep.
1080 */
1081 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1082 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001083 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001084}
1085
1086static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1087{
1088 struct mwl8k_priv *priv = hw->priv;
1089 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1090 int processed;
1091
1092 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001093 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001094 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001095 void *rxd;
1096 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001097 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001098 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001099
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001100 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001101 if (skb == NULL)
1102 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001103
1104 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1105
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001106 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001107 if (pkt_len < 0)
1108 break;
1109
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001110 rxq->buf[rxq->head].skb = NULL;
1111
1112 pci_unmap_single(priv->pdev,
1113 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1114 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1115 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001116
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001117 rxq->head++;
1118 if (rxq->head == MWL8K_RX_DESCS)
1119 rxq->head = 0;
1120
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001121 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001122
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001123 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001124 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001125
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001126 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001127 * Check for a pending join operation. Save a
1128 * copy of the beacon and schedule a tasklet to
1129 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001130 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001131 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001132 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001133
Johannes Bergf1d58c22009-06-17 13:13:00 +02001134 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1135 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001136
1137 processed++;
1138 }
1139
1140 return processed;
1141}
1142
1143
1144/*
1145 * Packet transmission.
1146 */
1147
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001148/* Transmit packet ACK policy */
1149#define MWL8K_TXD_ACK_POLICY_NORMAL 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001150#define MWL8K_TXD_ACK_POLICY_BLOCKACK 3
1151
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001152#define MWL8K_TXD_STATUS_OK 0x00000001
1153#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1154#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1155#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001156#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001157
1158struct mwl8k_tx_desc {
1159 __le32 status;
1160 __u8 data_rate;
1161 __u8 tx_priority;
1162 __le16 qos_control;
1163 __le32 pkt_phys_addr;
1164 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001165 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001166 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167 __le32 reserved;
1168 __le16 rate_info;
1169 __u8 peer_id;
1170 __u8 tx_frag_cnt;
1171} __attribute__((packed));
1172
1173#define MWL8K_TX_DESCS 128
1174
1175static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1176{
1177 struct mwl8k_priv *priv = hw->priv;
1178 struct mwl8k_tx_queue *txq = priv->txq + index;
1179 int size;
1180 int i;
1181
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001182 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1183 txq->stats.limit = MWL8K_TX_DESCS;
1184 txq->head = 0;
1185 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001186
1187 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1188
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001189 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1190 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001191 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001192 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001193 return -ENOMEM;
1194 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001195 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001196
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001197 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1198 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001199 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001200 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001201 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001202 return -ENOMEM;
1203 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001204 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205
1206 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1207 struct mwl8k_tx_desc *tx_desc;
1208 int nexti;
1209
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001210 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001211 nexti = (i + 1) % MWL8K_TX_DESCS;
1212
1213 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001214 tx_desc->next_txd_phys_addr =
1215 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001216 }
1217
1218 return 0;
1219}
1220
1221static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1222{
1223 iowrite32(MWL8K_H2A_INT_PPA_READY,
1224 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1225 iowrite32(MWL8K_H2A_INT_DUMMY,
1226 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1227 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1228}
1229
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001230static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001231{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001232 struct mwl8k_priv *priv = hw->priv;
1233 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001234
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001235 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1236 struct mwl8k_tx_queue *txq = priv->txq + i;
1237 int fw_owned = 0;
1238 int drv_owned = 0;
1239 int unused = 0;
1240 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001241
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001242 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001243 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1244 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001245
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001246 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001247 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001248 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001249 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001250 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001251
1252 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001253 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001254 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001255
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001256 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1257 "fw_owned=%d drv_owned=%d unused=%d\n",
1258 wiphy_name(hw->wiphy), i,
1259 txq->stats.len, txq->head, txq->tail,
1260 fw_owned, drv_owned, unused);
1261 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001262}
1263
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001264/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001265 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001266 */
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001267#define MWL8K_TX_WAIT_TIMEOUT_MS 1000
1268
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001269static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001270{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001271 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001272 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001273 int retry;
1274 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001275
1276 might_sleep();
1277
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001278 /*
1279 * The TX queues are stopped at this point, so this test
1280 * doesn't need to take ->tx_lock.
1281 */
1282 if (!priv->pending_tx_pkts)
1283 return 0;
1284
1285 retry = 0;
1286 rc = 0;
1287
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001288 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001289 priv->tx_wait = &tx_wait;
1290 while (!rc) {
1291 int oldcount;
1292 unsigned long timeout;
1293
1294 oldcount = priv->pending_tx_pkts;
1295
1296 spin_unlock_bh(&priv->tx_lock);
1297 timeout = wait_for_completion_timeout(&tx_wait,
1298 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1299 spin_lock_bh(&priv->tx_lock);
1300
1301 if (timeout) {
1302 WARN_ON(priv->pending_tx_pkts);
1303 if (retry) {
1304 printk(KERN_NOTICE "%s: tx rings drained\n",
1305 wiphy_name(hw->wiphy));
1306 }
1307 break;
1308 }
1309
1310 if (priv->pending_tx_pkts < oldcount) {
1311 printk(KERN_NOTICE "%s: timeout waiting for tx "
1312 "rings to drain (%d -> %d pkts), retrying\n",
1313 wiphy_name(hw->wiphy), oldcount,
1314 priv->pending_tx_pkts);
1315 retry = 1;
1316 continue;
1317 }
1318
1319 priv->tx_wait = NULL;
1320
1321 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1322 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1323 mwl8k_dump_tx_rings(hw);
1324
1325 rc = -ETIMEDOUT;
1326 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001327 spin_unlock_bh(&priv->tx_lock);
1328
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001329 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001330}
1331
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001332#define MWL8K_TXD_SUCCESS(status) \
1333 ((status) & (MWL8K_TXD_STATUS_OK | \
1334 MWL8K_TXD_STATUS_OK_RETRY | \
1335 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001336
1337static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1338{
1339 struct mwl8k_priv *priv = hw->priv;
1340 struct mwl8k_tx_queue *txq = priv->txq + index;
1341 int wake = 0;
1342
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001343 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001344 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345 struct mwl8k_tx_desc *tx_desc;
1346 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001347 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001348 struct sk_buff *skb;
1349 struct ieee80211_tx_info *info;
1350 u32 status;
1351
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001352 tx = txq->head;
1353 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001354
1355 status = le32_to_cpu(tx_desc->status);
1356
1357 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1358 if (!force)
1359 break;
1360 tx_desc->status &=
1361 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1362 }
1363
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001364 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1365 BUG_ON(txq->stats.len == 0);
1366 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001367 priv->pending_tx_pkts--;
1368
1369 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001370 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001371 skb = txq->skb[tx];
1372 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001373
1374 BUG_ON(skb == NULL);
1375 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1376
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001377 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001378
1379 /* Mark descriptor as unused */
1380 tx_desc->pkt_phys_addr = 0;
1381 tx_desc->pkt_len = 0;
1382
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001383 info = IEEE80211_SKB_CB(skb);
1384 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001385 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001386 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001387
1388 ieee80211_tx_status_irqsafe(hw, skb);
1389
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001390 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001391 }
1392
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001393 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001394 ieee80211_wake_queue(hw, index);
1395}
1396
1397/* must be called only when the card's transmit is completely halted */
1398static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1399{
1400 struct mwl8k_priv *priv = hw->priv;
1401 struct mwl8k_tx_queue *txq = priv->txq + index;
1402
1403 mwl8k_txq_reclaim(hw, index, 1);
1404
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001405 kfree(txq->skb);
1406 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001407
1408 pci_free_consistent(priv->pdev,
1409 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001410 txq->txd, txq->txd_dma);
1411 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001412}
1413
1414static int
1415mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1416{
1417 struct mwl8k_priv *priv = hw->priv;
1418 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001419 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001420 struct ieee80211_hdr *wh;
1421 struct mwl8k_tx_queue *txq;
1422 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001423 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001424 u32 txstatus;
1425 u8 txdatarate;
1426 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001427
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001428 wh = (struct ieee80211_hdr *)skb->data;
1429 if (ieee80211_is_data_qos(wh->frame_control))
1430 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1431 else
1432 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001433
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001434 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001435 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001436
1437 tx_info = IEEE80211_SKB_CB(skb);
1438 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439
1440 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1441 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001442
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001443 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1444 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1445 mwl8k_vif->seqno = seqno++ % 4096;
1446 }
1447
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001448 /* Setup firmware control bit fields for each frame type. */
1449 txstatus = 0;
1450 txdatarate = 0;
1451 if (ieee80211_is_mgmt(wh->frame_control) ||
1452 ieee80211_is_ctl(wh->frame_control)) {
1453 txdatarate = 0;
1454 qos = mwl8k_qos_setbit_eosp(qos);
1455 /* Set Queue size to unspecified */
1456 qos = mwl8k_qos_setbit_qlen(qos, 0xff);
1457 } else if (ieee80211_is_data(wh->frame_control)) {
1458 txdatarate = 1;
1459 if (is_multicast_ether_addr(wh->addr1))
1460 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1461
1462 /* Send pkt in an aggregate if AMPDU frame. */
1463 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1464 qos = mwl8k_qos_setbit_ack(qos,
1465 MWL8K_TXD_ACK_POLICY_BLOCKACK);
1466 else
1467 qos = mwl8k_qos_setbit_ack(qos,
1468 MWL8K_TXD_ACK_POLICY_NORMAL);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001469 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001470
1471 dma = pci_map_single(priv->pdev, skb->data,
1472 skb->len, PCI_DMA_TODEVICE);
1473
1474 if (pci_dma_mapping_error(priv->pdev, dma)) {
1475 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001476 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001477 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001478 return NETDEV_TX_OK;
1479 }
1480
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001481 spin_lock_bh(&priv->tx_lock);
1482
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001483 txq = priv->txq + index;
1484
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001485 BUG_ON(txq->skb[txq->tail] != NULL);
1486 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001487
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001488 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001489 tx->data_rate = txdatarate;
1490 tx->tx_priority = index;
1491 tx->qos_control = cpu_to_le16(qos);
1492 tx->pkt_phys_addr = cpu_to_le32(dma);
1493 tx->pkt_len = cpu_to_le16(skb->len);
1494 tx->rate_info = 0;
1495 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001497 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1498
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001499 txq->stats.count++;
1500 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001501 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001502
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001503 txq->tail++;
1504 if (txq->tail == MWL8K_TX_DESCS)
1505 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001506
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001507 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001508 ieee80211_stop_queue(hw, index);
1509
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001510 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001511
1512 spin_unlock_bh(&priv->tx_lock);
1513
1514 return NETDEV_TX_OK;
1515}
1516
1517
1518/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001519 * Firmware access.
1520 *
1521 * We have the following requirements for issuing firmware commands:
1522 * - Some commands require that the packet transmit path is idle when
1523 * the command is issued. (For simplicity, we'll just quiesce the
1524 * transmit path for every command.)
1525 * - There are certain sequences of commands that need to be issued to
1526 * the hardware sequentially, with no other intervening commands.
1527 *
1528 * This leads to an implementation of a "firmware lock" as a mutex that
1529 * can be taken recursively, and which is taken by both the low-level
1530 * command submission function (mwl8k_post_cmd) as well as any users of
1531 * that function that require issuing of an atomic sequence of commands,
1532 * and quiesces the transmit path whenever it's taken.
1533 */
1534static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1535{
1536 struct mwl8k_priv *priv = hw->priv;
1537
1538 if (priv->fw_mutex_owner != current) {
1539 int rc;
1540
1541 mutex_lock(&priv->fw_mutex);
1542 ieee80211_stop_queues(hw);
1543
1544 rc = mwl8k_tx_wait_empty(hw);
1545 if (rc) {
1546 ieee80211_wake_queues(hw);
1547 mutex_unlock(&priv->fw_mutex);
1548
1549 return rc;
1550 }
1551
1552 priv->fw_mutex_owner = current;
1553 }
1554
1555 priv->fw_mutex_depth++;
1556
1557 return 0;
1558}
1559
1560static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1561{
1562 struct mwl8k_priv *priv = hw->priv;
1563
1564 if (!--priv->fw_mutex_depth) {
1565 ieee80211_wake_queues(hw);
1566 priv->fw_mutex_owner = NULL;
1567 mutex_unlock(&priv->fw_mutex);
1568 }
1569}
1570
1571
1572/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001573 * Command processing.
1574 */
1575
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001576/* Timeout firmware commands after 10s */
1577#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578
1579static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1580{
1581 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1582 struct mwl8k_priv *priv = hw->priv;
1583 void __iomem *regs = priv->regs;
1584 dma_addr_t dma_addr;
1585 unsigned int dma_size;
1586 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001587 unsigned long timeout = 0;
1588 u8 buf[32];
1589
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001590 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001591 dma_size = le16_to_cpu(cmd->length);
1592 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1593 PCI_DMA_BIDIRECTIONAL);
1594 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1595 return -ENOMEM;
1596
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001597 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001598 if (rc) {
1599 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1600 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001601 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001602 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001603
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001604 priv->hostcmd_wait = &cmd_wait;
1605 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1606 iowrite32(MWL8K_H2A_INT_DOORBELL,
1607 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1608 iowrite32(MWL8K_H2A_INT_DUMMY,
1609 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001610
1611 timeout = wait_for_completion_timeout(&cmd_wait,
1612 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1613
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001614 priv->hostcmd_wait = NULL;
1615
1616 mwl8k_fw_unlock(hw);
1617
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001618 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1619 PCI_DMA_BIDIRECTIONAL);
1620
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001621 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001622 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001623 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001624 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1625 MWL8K_CMD_TIMEOUT_MS);
1626 rc = -ETIMEDOUT;
1627 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001628 int ms;
1629
1630 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1631
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001632 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001633 if (rc)
1634 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001635 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001636 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001637 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001638 else if (ms > 2000)
1639 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1640 wiphy_name(hw->wiphy),
1641 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1642 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001643 }
1644
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001645 return rc;
1646}
1647
1648/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001649 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001650 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001651struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001652 struct mwl8k_cmd_pkt header;
1653 __u8 hw_rev;
1654 __u8 host_interface;
1655 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001656 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 __le16 region_code;
1658 __le32 fw_rev;
1659 __le32 ps_cookie;
1660 __le32 caps;
1661 __u8 mcs_bitmap[16];
1662 __le32 rx_queue_ptr;
1663 __le32 num_tx_queues;
1664 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1665 __le32 caps2;
1666 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001667 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001668} __attribute__((packed));
1669
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001670static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001671{
1672 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001673 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001674 int rc;
1675 int i;
1676
1677 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1678 if (cmd == NULL)
1679 return -ENOMEM;
1680
1681 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1682 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1683
1684 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1685 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001686 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001687 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001688 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001689 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001690 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001691 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001692
1693 rc = mwl8k_post_cmd(hw, &cmd->header);
1694
1695 if (!rc) {
1696 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1697 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001698 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001699 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001700 }
1701
1702 kfree(cmd);
1703 return rc;
1704}
1705
1706/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001707 * CMD_GET_HW_SPEC (AP version).
1708 */
1709struct mwl8k_cmd_get_hw_spec_ap {
1710 struct mwl8k_cmd_pkt header;
1711 __u8 hw_rev;
1712 __u8 host_interface;
1713 __le16 num_wcb;
1714 __le16 num_mcaddrs;
1715 __u8 perm_addr[ETH_ALEN];
1716 __le16 region_code;
1717 __le16 num_antenna;
1718 __le32 fw_rev;
1719 __le32 wcbbase0;
1720 __le32 rxwrptr;
1721 __le32 rxrdptr;
1722 __le32 ps_cookie;
1723 __le32 wcbbase1;
1724 __le32 wcbbase2;
1725 __le32 wcbbase3;
1726} __attribute__((packed));
1727
1728static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1729{
1730 struct mwl8k_priv *priv = hw->priv;
1731 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1732 int rc;
1733
1734 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1735 if (cmd == NULL)
1736 return -ENOMEM;
1737
1738 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1739 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1740
1741 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1742 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1743
1744 rc = mwl8k_post_cmd(hw, &cmd->header);
1745
1746 if (!rc) {
1747 int off;
1748
1749 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1750 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1751 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1752 priv->hw_rev = cmd->hw_rev;
1753
1754 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1755 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1756
1757 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1758 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1759
1760 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1761 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1762
1763 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1764 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1765
1766 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1767 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1768
1769 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1770 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1771 }
1772
1773 kfree(cmd);
1774 return rc;
1775}
1776
1777/*
1778 * CMD_SET_HW_SPEC.
1779 */
1780struct mwl8k_cmd_set_hw_spec {
1781 struct mwl8k_cmd_pkt header;
1782 __u8 hw_rev;
1783 __u8 host_interface;
1784 __le16 num_mcaddrs;
1785 __u8 perm_addr[ETH_ALEN];
1786 __le16 region_code;
1787 __le32 fw_rev;
1788 __le32 ps_cookie;
1789 __le32 caps;
1790 __le32 rx_queue_ptr;
1791 __le32 num_tx_queues;
1792 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1793 __le32 flags;
1794 __le32 num_tx_desc_per_queue;
1795 __le32 total_rxd;
1796} __attribute__((packed));
1797
1798#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1799
1800static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1801{
1802 struct mwl8k_priv *priv = hw->priv;
1803 struct mwl8k_cmd_set_hw_spec *cmd;
1804 int rc;
1805 int i;
1806
1807 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1808 if (cmd == NULL)
1809 return -ENOMEM;
1810
1811 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1812 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1813
1814 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1815 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1816 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1817 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1818 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1819 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1820 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1821 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1822
1823 rc = mwl8k_post_cmd(hw, &cmd->header);
1824 kfree(cmd);
1825
1826 return rc;
1827}
1828
1829/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001830 * CMD_MAC_MULTICAST_ADR.
1831 */
1832struct mwl8k_cmd_mac_multicast_adr {
1833 struct mwl8k_cmd_pkt header;
1834 __le16 action;
1835 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001836 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001837};
1838
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001839#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1840#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1841#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1842#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001843
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001844static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001845__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001846 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001847{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001848 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001849 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001850 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001851
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001852 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001853 allmulti = 1;
1854 mc_count = 0;
1855 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001856
1857 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1858
1859 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001860 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001861 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001862
1863 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1864 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001865 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1866 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001867
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001868 if (allmulti) {
1869 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1870 } else if (mc_count) {
1871 int i;
1872
1873 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1874 cmd->numaddr = cpu_to_le16(mc_count);
1875 for (i = 0; i < mc_count && mclist; i++) {
1876 if (mclist->da_addrlen != ETH_ALEN) {
1877 kfree(cmd);
1878 return NULL;
1879 }
1880 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1881 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001882 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001883 }
1884
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001885 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001886}
1887
1888/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001889 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001890 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001891struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001892 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001893 __le32 stats[64];
1894} __attribute__((packed));
1895
1896#define MWL8K_STAT_ACK_FAILURE 9
1897#define MWL8K_STAT_RTS_FAILURE 12
1898#define MWL8K_STAT_FCS_ERROR 24
1899#define MWL8K_STAT_RTS_SUCCESS 11
1900
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001901static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1902 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001903{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001904 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001905 int rc;
1906
1907 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1908 if (cmd == NULL)
1909 return -ENOMEM;
1910
1911 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1912 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001913
1914 rc = mwl8k_post_cmd(hw, &cmd->header);
1915 if (!rc) {
1916 stats->dot11ACKFailureCount =
1917 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1918 stats->dot11RTSFailureCount =
1919 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1920 stats->dot11FCSErrorCount =
1921 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1922 stats->dot11RTSSuccessCount =
1923 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1924 }
1925 kfree(cmd);
1926
1927 return rc;
1928}
1929
1930/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001931 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001932 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001933struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001934 struct mwl8k_cmd_pkt header;
1935 __le16 action;
1936 __le16 control;
1937 __le16 radio_on;
1938} __attribute__((packed));
1939
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001940static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001941mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001942{
1943 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001944 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001945 int rc;
1946
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001947 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001948 return 0;
1949
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001950 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1951 if (cmd == NULL)
1952 return -ENOMEM;
1953
1954 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1955 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1956 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001957 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001958 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1959
1960 rc = mwl8k_post_cmd(hw, &cmd->header);
1961 kfree(cmd);
1962
1963 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001964 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001965
1966 return rc;
1967}
1968
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001969static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001970{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001971 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001972}
1973
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001974static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001975{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001976 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001977}
1978
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001979static int
1980mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1981{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001982 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001983
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001984 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001985
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001986 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001987}
1988
1989/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001990 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001991 */
1992#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1993
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001994struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001995 struct mwl8k_cmd_pkt header;
1996 __le16 action;
1997 __le16 support_level;
1998 __le16 current_level;
1999 __le16 reserved;
2000 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2001} __attribute__((packed));
2002
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002003static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002004{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002005 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002006 int rc;
2007
2008 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2009 if (cmd == NULL)
2010 return -ENOMEM;
2011
2012 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2013 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2014 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2015 cmd->support_level = cpu_to_le16(dBm);
2016
2017 rc = mwl8k_post_cmd(hw, &cmd->header);
2018 kfree(cmd);
2019
2020 return rc;
2021}
2022
2023/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002024 * CMD_RF_ANTENNA.
2025 */
2026struct mwl8k_cmd_rf_antenna {
2027 struct mwl8k_cmd_pkt header;
2028 __le16 antenna;
2029 __le16 mode;
2030} __attribute__((packed));
2031
2032#define MWL8K_RF_ANTENNA_RX 1
2033#define MWL8K_RF_ANTENNA_TX 2
2034
2035static int
2036mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2037{
2038 struct mwl8k_cmd_rf_antenna *cmd;
2039 int rc;
2040
2041 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2042 if (cmd == NULL)
2043 return -ENOMEM;
2044
2045 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2046 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2047 cmd->antenna = cpu_to_le16(antenna);
2048 cmd->mode = cpu_to_le16(mask);
2049
2050 rc = mwl8k_post_cmd(hw, &cmd->header);
2051 kfree(cmd);
2052
2053 return rc;
2054}
2055
2056/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002057 * CMD_SET_PRE_SCAN.
2058 */
2059struct mwl8k_cmd_set_pre_scan {
2060 struct mwl8k_cmd_pkt header;
2061} __attribute__((packed));
2062
2063static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2064{
2065 struct mwl8k_cmd_set_pre_scan *cmd;
2066 int rc;
2067
2068 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2069 if (cmd == NULL)
2070 return -ENOMEM;
2071
2072 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2073 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2074
2075 rc = mwl8k_post_cmd(hw, &cmd->header);
2076 kfree(cmd);
2077
2078 return rc;
2079}
2080
2081/*
2082 * CMD_SET_POST_SCAN.
2083 */
2084struct mwl8k_cmd_set_post_scan {
2085 struct mwl8k_cmd_pkt header;
2086 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002087 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002088} __attribute__((packed));
2089
2090static int
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002091mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002092{
2093 struct mwl8k_cmd_set_post_scan *cmd;
2094 int rc;
2095
2096 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2097 if (cmd == NULL)
2098 return -ENOMEM;
2099
2100 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2101 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2102 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002103 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002104
2105 rc = mwl8k_post_cmd(hw, &cmd->header);
2106 kfree(cmd);
2107
2108 return rc;
2109}
2110
2111/*
2112 * CMD_SET_RF_CHANNEL.
2113 */
2114struct mwl8k_cmd_set_rf_channel {
2115 struct mwl8k_cmd_pkt header;
2116 __le16 action;
2117 __u8 current_channel;
2118 __le32 channel_flags;
2119} __attribute__((packed));
2120
2121static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2122 struct ieee80211_channel *channel)
2123{
2124 struct mwl8k_cmd_set_rf_channel *cmd;
2125 int rc;
2126
2127 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2128 if (cmd == NULL)
2129 return -ENOMEM;
2130
2131 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2132 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2133 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2134 cmd->current_channel = channel->hw_value;
2135 if (channel->band == IEEE80211_BAND_2GHZ)
2136 cmd->channel_flags = cpu_to_le32(0x00000081);
2137 else
2138 cmd->channel_flags = cpu_to_le32(0x00000000);
2139
2140 rc = mwl8k_post_cmd(hw, &cmd->header);
2141 kfree(cmd);
2142
2143 return rc;
2144}
2145
2146/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002147 * CMD_SET_AID.
2148 */
2149#define MWL8K_FRAME_PROT_DISABLED 0x00
2150#define MWL8K_FRAME_PROT_11G 0x07
2151#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2152#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2153
2154struct mwl8k_cmd_update_set_aid {
2155 struct mwl8k_cmd_pkt header;
2156 __le16 aid;
2157
2158 /* AP's MAC address (BSSID) */
2159 __u8 bssid[ETH_ALEN];
2160 __le16 protection_mode;
2161 __u8 supp_rates[14];
2162} __attribute__((packed));
2163
2164static int
2165mwl8k_cmd_set_aid(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2166{
2167 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2168 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2169 struct mwl8k_cmd_update_set_aid *cmd;
2170 u16 prot_mode;
2171 int rc;
2172
2173 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2174 if (cmd == NULL)
2175 return -ENOMEM;
2176
2177 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2178 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2179 cmd->aid = cpu_to_le16(info->aid);
2180
2181 memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
2182
2183 if (info->use_cts_prot) {
2184 prot_mode = MWL8K_FRAME_PROT_11G;
2185 } else {
2186 switch (info->ht_operation_mode &
2187 IEEE80211_HT_OP_MODE_PROTECTION) {
2188 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2189 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2190 break;
2191 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2192 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2193 break;
2194 default:
2195 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2196 break;
2197 }
2198 }
2199 cmd->protection_mode = cpu_to_le16(prot_mode);
2200
2201 memcpy(cmd->supp_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2202
2203 rc = mwl8k_post_cmd(hw, &cmd->header);
2204 kfree(cmd);
2205
2206 return rc;
2207}
2208
2209/*
2210 * CMD_SET_RATE.
2211 */
2212struct mwl8k_cmd_set_rate {
2213 struct mwl8k_cmd_pkt header;
2214 __u8 legacy_rates[14];
2215
2216 /* Bitmap for supported MCS codes. */
2217 __u8 mcs_set[16];
2218 __u8 reserved[16];
2219} __attribute__((packed));
2220
2221static int
2222mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2223{
2224 struct mwl8k_cmd_set_rate *cmd;
2225 int rc;
2226
2227 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2228 if (cmd == NULL)
2229 return -ENOMEM;
2230
2231 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2232 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2233 memcpy(cmd->legacy_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2234
2235 rc = mwl8k_post_cmd(hw, &cmd->header);
2236 kfree(cmd);
2237
2238 return rc;
2239}
2240
2241/*
2242 * CMD_FINALIZE_JOIN.
2243 */
2244#define MWL8K_FJ_BEACON_MAXLEN 128
2245
2246struct mwl8k_cmd_finalize_join {
2247 struct mwl8k_cmd_pkt header;
2248 __le32 sleep_interval; /* Number of beacon periods to sleep */
2249 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2250} __attribute__((packed));
2251
2252static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2253 int framelen, int dtim)
2254{
2255 struct mwl8k_cmd_finalize_join *cmd;
2256 struct ieee80211_mgmt *payload = frame;
2257 int payload_len;
2258 int rc;
2259
2260 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2261 if (cmd == NULL)
2262 return -ENOMEM;
2263
2264 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2265 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2266 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2267
2268 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2269 if (payload_len < 0)
2270 payload_len = 0;
2271 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2272 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2273
2274 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2275
2276 rc = mwl8k_post_cmd(hw, &cmd->header);
2277 kfree(cmd);
2278
2279 return rc;
2280}
2281
2282/*
2283 * CMD_SET_RTS_THRESHOLD.
2284 */
2285struct mwl8k_cmd_set_rts_threshold {
2286 struct mwl8k_cmd_pkt header;
2287 __le16 action;
2288 __le16 threshold;
2289} __attribute__((packed));
2290
2291static int mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw,
2292 u16 action, u16 threshold)
2293{
2294 struct mwl8k_cmd_set_rts_threshold *cmd;
2295 int rc;
2296
2297 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2298 if (cmd == NULL)
2299 return -ENOMEM;
2300
2301 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2302 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2303 cmd->action = cpu_to_le16(action);
2304 cmd->threshold = cpu_to_le16(threshold);
2305
2306 rc = mwl8k_post_cmd(hw, &cmd->header);
2307 kfree(cmd);
2308
2309 return rc;
2310}
2311
2312/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002313 * CMD_SET_SLOT.
2314 */
2315struct mwl8k_cmd_set_slot {
2316 struct mwl8k_cmd_pkt header;
2317 __le16 action;
2318 __u8 short_slot;
2319} __attribute__((packed));
2320
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002321static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002322{
2323 struct mwl8k_cmd_set_slot *cmd;
2324 int rc;
2325
2326 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2327 if (cmd == NULL)
2328 return -ENOMEM;
2329
2330 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2331 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2332 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002333 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002334
2335 rc = mwl8k_post_cmd(hw, &cmd->header);
2336 kfree(cmd);
2337
2338 return rc;
2339}
2340
2341/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002342 * CMD_SET_EDCA_PARAMS.
2343 */
2344struct mwl8k_cmd_set_edca_params {
2345 struct mwl8k_cmd_pkt header;
2346
2347 /* See MWL8K_SET_EDCA_XXX below */
2348 __le16 action;
2349
2350 /* TX opportunity in units of 32 us */
2351 __le16 txop;
2352
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002353 union {
2354 struct {
2355 /* Log exponent of max contention period: 0...15 */
2356 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002357
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002358 /* Log exponent of min contention period: 0...15 */
2359 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002360
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002361 /* Adaptive interframe spacing in units of 32us */
2362 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002363
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002364 /* TX queue to configure */
2365 __u8 txq;
2366 } ap;
2367 struct {
2368 /* Log exponent of max contention period: 0...15 */
2369 __u8 log_cw_max;
2370
2371 /* Log exponent of min contention period: 0...15 */
2372 __u8 log_cw_min;
2373
2374 /* Adaptive interframe spacing in units of 32us */
2375 __u8 aifs;
2376
2377 /* TX queue to configure */
2378 __u8 txq;
2379 } sta;
2380 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002381} __attribute__((packed));
2382
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002383#define MWL8K_SET_EDCA_CW 0x01
2384#define MWL8K_SET_EDCA_TXOP 0x02
2385#define MWL8K_SET_EDCA_AIFS 0x04
2386
2387#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2388 MWL8K_SET_EDCA_TXOP | \
2389 MWL8K_SET_EDCA_AIFS)
2390
2391static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002392mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2393 __u16 cw_min, __u16 cw_max,
2394 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002395{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002396 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002397 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002398 int rc;
2399
2400 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2401 if (cmd == NULL)
2402 return -ENOMEM;
2403
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002404 /*
2405 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2406 * this call.
2407 */
2408 qnum ^= !(qnum >> 1);
2409
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002410 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2411 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002412 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2413 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002414 if (priv->ap_fw) {
2415 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2416 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2417 cmd->ap.aifs = aifs;
2418 cmd->ap.txq = qnum;
2419 } else {
2420 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2421 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2422 cmd->sta.aifs = aifs;
2423 cmd->sta.txq = qnum;
2424 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002425
2426 rc = mwl8k_post_cmd(hw, &cmd->header);
2427 kfree(cmd);
2428
2429 return rc;
2430}
2431
2432/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002433 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002434 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002435struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002436 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002437 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002438} __attribute__((packed));
2439
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002440static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002441{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002442 struct mwl8k_priv *priv = hw->priv;
2443 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002444 int rc;
2445
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002446 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2447 if (cmd == NULL)
2448 return -ENOMEM;
2449
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002450 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002451 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002452 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002453
2454 rc = mwl8k_post_cmd(hw, &cmd->header);
2455 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002456
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002457 if (!rc)
2458 priv->wmm_enabled = enable;
2459
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002460 return rc;
2461}
2462
2463/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002464 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002465 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002466struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002467 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002468 __le32 action;
2469 __u8 rx_antenna_map;
2470 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002471} __attribute__((packed));
2472
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002473static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002474{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002475 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002476 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002477
2478 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2479 if (cmd == NULL)
2480 return -ENOMEM;
2481
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002482 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002483 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002484 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2485 cmd->rx_antenna_map = rx;
2486 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002487
2488 rc = mwl8k_post_cmd(hw, &cmd->header);
2489 kfree(cmd);
2490
2491 return rc;
2492}
2493
2494/*
2495 * CMD_USE_FIXED_RATE.
2496 */
2497#define MWL8K_RATE_TABLE_SIZE 8
2498#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002499#define MWL8K_USE_AUTO_RATE 0x0002
2500
2501struct mwl8k_rate_entry {
2502 /* Set to 1 if HT rate, 0 if legacy. */
2503 __le32 is_ht_rate;
2504
2505 /* Set to 1 to use retry_count field. */
2506 __le32 enable_retry;
2507
2508 /* Specified legacy rate or MCS. */
2509 __le32 rate;
2510
2511 /* Number of allowed retries. */
2512 __le32 retry_count;
2513} __attribute__((packed));
2514
2515struct mwl8k_rate_table {
2516 /* 1 to allow specified rate and below */
2517 __le32 allow_rate_drop;
2518 __le32 num_rates;
2519 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2520} __attribute__((packed));
2521
2522struct mwl8k_cmd_use_fixed_rate {
2523 struct mwl8k_cmd_pkt header;
2524 __le32 action;
2525 struct mwl8k_rate_table rate_table;
2526
2527 /* Unicast, Broadcast or Multicast */
2528 __le32 rate_type;
2529 __le32 reserved1;
2530 __le32 reserved2;
2531} __attribute__((packed));
2532
2533static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2534 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2535{
2536 struct mwl8k_cmd_use_fixed_rate *cmd;
2537 int count;
2538 int rc;
2539
2540 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2541 if (cmd == NULL)
2542 return -ENOMEM;
2543
2544 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2545 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2546
2547 cmd->action = cpu_to_le32(action);
2548 cmd->rate_type = cpu_to_le32(rate_type);
2549
2550 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002551 /*
2552 * Copy over each field manually so that endian
2553 * conversion can be done.
2554 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002555 cmd->rate_table.allow_rate_drop =
2556 cpu_to_le32(rate_table->allow_rate_drop);
2557 cmd->rate_table.num_rates =
2558 cpu_to_le32(rate_table->num_rates);
2559
2560 for (count = 0; count < rate_table->num_rates; count++) {
2561 struct mwl8k_rate_entry *dst =
2562 &cmd->rate_table.rate_entry[count];
2563 struct mwl8k_rate_entry *src =
2564 &rate_table->rate_entry[count];
2565
2566 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2567 dst->enable_retry = cpu_to_le32(src->enable_retry);
2568 dst->rate = cpu_to_le32(src->rate);
2569 dst->retry_count = cpu_to_le32(src->retry_count);
2570 }
2571 }
2572
2573 rc = mwl8k_post_cmd(hw, &cmd->header);
2574 kfree(cmd);
2575
2576 return rc;
2577}
2578
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002579/*
2580 * CMD_ENABLE_SNIFFER.
2581 */
2582struct mwl8k_cmd_enable_sniffer {
2583 struct mwl8k_cmd_pkt header;
2584 __le32 action;
2585} __attribute__((packed));
2586
2587static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2588{
2589 struct mwl8k_cmd_enable_sniffer *cmd;
2590 int rc;
2591
2592 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2593 if (cmd == NULL)
2594 return -ENOMEM;
2595
2596 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2597 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2598 cmd->action = cpu_to_le32(!!enable);
2599
2600 rc = mwl8k_post_cmd(hw, &cmd->header);
2601 kfree(cmd);
2602
2603 return rc;
2604}
2605
2606/*
2607 * CMD_SET_MAC_ADDR.
2608 */
2609struct mwl8k_cmd_set_mac_addr {
2610 struct mwl8k_cmd_pkt header;
2611 union {
2612 struct {
2613 __le16 mac_type;
2614 __u8 mac_addr[ETH_ALEN];
2615 } mbss;
2616 __u8 mac_addr[ETH_ALEN];
2617 };
2618} __attribute__((packed));
2619
2620static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2621{
2622 struct mwl8k_priv *priv = hw->priv;
2623 struct mwl8k_cmd_set_mac_addr *cmd;
2624 int rc;
2625
2626 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2627 if (cmd == NULL)
2628 return -ENOMEM;
2629
2630 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2631 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2632 if (priv->ap_fw) {
2633 cmd->mbss.mac_type = 0;
2634 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2635 } else {
2636 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2637 }
2638
2639 rc = mwl8k_post_cmd(hw, &cmd->header);
2640 kfree(cmd);
2641
2642 return rc;
2643}
2644
2645/*
2646 * CMD_SET_RATEADAPT_MODE.
2647 */
2648struct mwl8k_cmd_set_rate_adapt_mode {
2649 struct mwl8k_cmd_pkt header;
2650 __le16 action;
2651 __le16 mode;
2652} __attribute__((packed));
2653
2654static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2655{
2656 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2657 int rc;
2658
2659 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2660 if (cmd == NULL)
2661 return -ENOMEM;
2662
2663 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2664 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2665 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2666 cmd->mode = cpu_to_le16(mode);
2667
2668 rc = mwl8k_post_cmd(hw, &cmd->header);
2669 kfree(cmd);
2670
2671 return rc;
2672}
2673
2674/*
2675 * CMD_UPDATE_STADB.
2676 */
2677struct mwl8k_cmd_update_stadb {
2678 struct mwl8k_cmd_pkt header;
2679
2680 /* See STADB_ACTION_TYPE */
2681 __le32 action;
2682
2683 /* Peer MAC address */
2684 __u8 peer_addr[ETH_ALEN];
2685
2686 __le32 reserved;
2687
2688 /* Peer info - valid during add/update. */
2689 struct peer_capability_info peer_info;
2690} __attribute__((packed));
2691
2692static int mwl8k_cmd_update_stadb(struct ieee80211_hw *hw,
2693 struct ieee80211_vif *vif, __u32 action)
2694{
2695 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2696 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2697 struct mwl8k_cmd_update_stadb *cmd;
2698 struct peer_capability_info *peer_info;
2699 int rc;
2700
2701 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2702 if (cmd == NULL)
2703 return -ENOMEM;
2704
2705 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2706 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2707
2708 cmd->action = cpu_to_le32(action);
2709 peer_info = &cmd->peer_info;
2710 memcpy(cmd->peer_addr, mv_vif->bssid, ETH_ALEN);
2711
2712 switch (action) {
2713 case MWL8K_STA_DB_ADD_ENTRY:
2714 case MWL8K_STA_DB_MODIFY_ENTRY:
2715 /* Build peer_info block */
2716 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2717 peer_info->basic_caps = cpu_to_le16(info->assoc_capability);
2718 memcpy(peer_info->legacy_rates, mwl8k_rateids,
2719 sizeof(mwl8k_rateids));
2720 peer_info->interop = 1;
2721 peer_info->amsdu_enabled = 0;
2722
2723 rc = mwl8k_post_cmd(hw, &cmd->header);
2724 if (rc == 0)
2725 mv_vif->peer_id = peer_info->station_id;
2726
2727 break;
2728
2729 case MWL8K_STA_DB_DEL_ENTRY:
2730 case MWL8K_STA_DB_FLUSH:
2731 default:
2732 rc = mwl8k_post_cmd(hw, &cmd->header);
2733 if (rc == 0)
2734 mv_vif->peer_id = 0;
2735 break;
2736 }
2737 kfree(cmd);
2738
2739 return rc;
2740}
2741
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002742
2743/*
2744 * Interrupt handling.
2745 */
2746static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2747{
2748 struct ieee80211_hw *hw = dev_id;
2749 struct mwl8k_priv *priv = hw->priv;
2750 u32 status;
2751
2752 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2753 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2754
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002755 if (!status)
2756 return IRQ_NONE;
2757
2758 if (status & MWL8K_A2H_INT_TX_DONE)
2759 tasklet_schedule(&priv->tx_reclaim_task);
2760
2761 if (status & MWL8K_A2H_INT_RX_READY) {
2762 while (rxq_process(hw, 0, 1))
2763 rxq_refill(hw, 0, 1);
2764 }
2765
2766 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002767 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002768 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002769 }
2770
2771 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002772 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002773 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002774 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002775 }
2776
2777 return IRQ_HANDLED;
2778}
2779
2780
2781/*
2782 * Core driver operations.
2783 */
2784static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2785{
2786 struct mwl8k_priv *priv = hw->priv;
2787 int index = skb_get_queue_mapping(skb);
2788 int rc;
2789
2790 if (priv->current_channel == NULL) {
2791 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002792 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002793 dev_kfree_skb(skb);
2794 return NETDEV_TX_OK;
2795 }
2796
2797 rc = mwl8k_txq_xmit(hw, index, skb);
2798
2799 return rc;
2800}
2801
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002802static int mwl8k_start(struct ieee80211_hw *hw)
2803{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002804 struct mwl8k_priv *priv = hw->priv;
2805 int rc;
2806
Joe Perchesa0607fd2009-11-18 23:29:17 -08002807 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002808 IRQF_SHARED, MWL8K_NAME, hw);
2809 if (rc) {
2810 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002811 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002812 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002813 }
2814
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002815 /* Enable tx reclaim tasklet */
2816 tasklet_enable(&priv->tx_reclaim_task);
2817
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002818 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002819 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002820
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002821 rc = mwl8k_fw_lock(hw);
2822 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002823 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002824
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002825 if (!priv->ap_fw) {
2826 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002827 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002828
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002829 if (!rc)
2830 rc = mwl8k_cmd_set_pre_scan(hw);
2831
2832 if (!rc)
2833 rc = mwl8k_cmd_set_post_scan(hw,
2834 "\x00\x00\x00\x00\x00\x00");
2835 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002836
2837 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002838 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002839
2840 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002841 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002842
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002843 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002844 }
2845
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002846 if (rc) {
2847 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2848 free_irq(priv->pdev->irq, hw);
2849 tasklet_disable(&priv->tx_reclaim_task);
2850 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002851
2852 return rc;
2853}
2854
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002855static void mwl8k_stop(struct ieee80211_hw *hw)
2856{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002857 struct mwl8k_priv *priv = hw->priv;
2858 int i;
2859
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002860 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002861
2862 ieee80211_stop_queues(hw);
2863
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002864 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002865 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002866 free_irq(priv->pdev->irq, hw);
2867
2868 /* Stop finalize join worker */
2869 cancel_work_sync(&priv->finalize_join_worker);
2870 if (priv->beacon_skb != NULL)
2871 dev_kfree_skb(priv->beacon_skb);
2872
2873 /* Stop tx reclaim tasklet */
2874 tasklet_disable(&priv->tx_reclaim_task);
2875
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002876 /* Return all skbs to mac80211 */
2877 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2878 mwl8k_txq_reclaim(hw, i, 1);
2879}
2880
2881static int mwl8k_add_interface(struct ieee80211_hw *hw,
2882 struct ieee80211_if_init_conf *conf)
2883{
2884 struct mwl8k_priv *priv = hw->priv;
2885 struct mwl8k_vif *mwl8k_vif;
2886
2887 /*
2888 * We only support one active interface at a time.
2889 */
2890 if (priv->vif != NULL)
2891 return -EBUSY;
2892
2893 /*
2894 * We only support managed interfaces for now.
2895 */
Lennert Buytenhek240e86e2009-07-16 14:15:44 +02002896 if (conf->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002897 return -EINVAL;
2898
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002899 /*
2900 * Reject interface creation if sniffer mode is active, as
2901 * STA operation is mutually exclusive with hardware sniffer
2902 * mode.
2903 */
2904 if (priv->sniffer_enabled) {
2905 printk(KERN_INFO "%s: unable to create STA "
2906 "interface due to sniffer mode being enabled\n",
2907 wiphy_name(hw->wiphy));
2908 return -EINVAL;
2909 }
2910
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002911 /* Clean out driver private area */
2912 mwl8k_vif = MWL8K_VIF(conf->vif);
2913 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2914
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002915 /* Set and save the mac address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002916 mwl8k_cmd_set_mac_addr(hw, conf->mac_addr);
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002917 memcpy(mwl8k_vif->mac_addr, conf->mac_addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002918
2919 /* Back pointer to parent config block */
2920 mwl8k_vif->priv = priv;
2921
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002922 /* Set Initial sequence number to zero */
2923 mwl8k_vif->seqno = 0;
2924
2925 priv->vif = conf->vif;
2926 priv->current_channel = NULL;
2927
2928 return 0;
2929}
2930
2931static void mwl8k_remove_interface(struct ieee80211_hw *hw,
2932 struct ieee80211_if_init_conf *conf)
2933{
2934 struct mwl8k_priv *priv = hw->priv;
2935
2936 if (priv->vif == NULL)
2937 return;
2938
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002939 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002940
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002941 priv->vif = NULL;
2942}
2943
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002944static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002945{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002946 struct ieee80211_conf *conf = &hw->conf;
2947 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002948 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002949
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002950 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002951 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002952 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002953 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002954 }
2955
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002956 rc = mwl8k_fw_lock(hw);
2957 if (rc)
2958 return rc;
2959
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002960 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002961 if (rc)
2962 goto out;
2963
2964 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2965 if (rc)
2966 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002967
2968 priv->current_channel = conf->channel;
2969
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002970 if (conf->power_level > 18)
2971 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002972 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002973 if (rc)
2974 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002975
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002976 if (priv->ap_fw) {
2977 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2978 if (!rc)
2979 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2980 } else {
2981 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2982 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002983
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002984out:
2985 mwl8k_fw_unlock(hw);
2986
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002987 return rc;
2988}
2989
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002990static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2991 struct ieee80211_vif *vif,
2992 struct ieee80211_bss_conf *info,
2993 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002994{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002995 struct mwl8k_priv *priv = hw->priv;
2996 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002997 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002998
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002999 if ((changed & BSS_CHANGED_ASSOC) == 0)
3000 return;
3001
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003002 priv->capture_beacon = false;
3003
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003004 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02003005 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003006 return;
3007
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003008 if (info->assoc) {
3009 memcpy(&mwl8k_vif->bss_info, info,
3010 sizeof(struct ieee80211_bss_conf));
3011
Lennert Buytenhekd1844d72009-11-30 18:13:56 +01003012 memcpy(mwl8k_vif->bssid, info->bssid, ETH_ALEN);
3013
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003014 /* Install rates */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003015 rc = mwl8k_cmd_set_rate(hw, vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003016 if (rc)
3017 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003018
3019 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003020 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
3021 MWL8K_UCAST_RATE, NULL);
3022 if (rc)
3023 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003024
3025 /* Set radio preamble */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003026 rc = mwl8k_set_radio_preamble(hw, info->use_short_preamble);
3027 if (rc)
3028 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003029
3030 /* Set slot time */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003031 rc = mwl8k_cmd_set_slot(hw, info->use_short_slot);
3032 if (rc)
3033 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003034
3035 /* Update peer rate info */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003036 rc = mwl8k_cmd_update_stadb(hw, vif,
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003037 MWL8K_STA_DB_MODIFY_ENTRY);
3038 if (rc)
3039 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003040
3041 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003042 rc = mwl8k_cmd_set_aid(hw, vif);
3043 if (rc)
3044 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003045
3046 /*
3047 * Finalize the join. Tell rx handler to process
3048 * next beacon from our BSSID.
3049 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003050 memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003051 priv->capture_beacon = true;
3052 } else {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003053 rc = mwl8k_cmd_update_stadb(hw, vif, MWL8K_STA_DB_DEL_ENTRY);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003054 memset(&mwl8k_vif->bss_info, 0,
3055 sizeof(struct ieee80211_bss_conf));
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003056 memset(mwl8k_vif->bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003057 }
3058
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003059out:
3060 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003061}
3062
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003063static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3064 int mc_count, struct dev_addr_list *mclist)
3065{
3066 struct mwl8k_cmd_pkt *cmd;
3067
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003068 /*
3069 * Synthesize and return a command packet that programs the
3070 * hardware multicast address filter. At this point we don't
3071 * know whether FIF_ALLMULTI is being requested, but if it is,
3072 * we'll end up throwing this packet away and creating a new
3073 * one in mwl8k_configure_filter().
3074 */
3075 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003076
3077 return (unsigned long)cmd;
3078}
3079
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003080static int
3081mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3082 unsigned int changed_flags,
3083 unsigned int *total_flags)
3084{
3085 struct mwl8k_priv *priv = hw->priv;
3086
3087 /*
3088 * Hardware sniffer mode is mutually exclusive with STA
3089 * operation, so refuse to enable sniffer mode if a STA
3090 * interface is active.
3091 */
3092 if (priv->vif != NULL) {
3093 if (net_ratelimit())
3094 printk(KERN_INFO "%s: not enabling sniffer "
3095 "mode because STA interface is active\n",
3096 wiphy_name(hw->wiphy));
3097 return 0;
3098 }
3099
3100 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003101 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003102 return 0;
3103 priv->sniffer_enabled = true;
3104 }
3105
3106 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3107 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3108 FIF_OTHER_BSS;
3109
3110 return 1;
3111}
3112
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003113static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3114 unsigned int changed_flags,
3115 unsigned int *total_flags,
3116 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003117{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003118 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003119 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3120
3121 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003122 * AP firmware doesn't allow fine-grained control over
3123 * the receive filter.
3124 */
3125 if (priv->ap_fw) {
3126 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3127 kfree(cmd);
3128 return;
3129 }
3130
3131 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003132 * Enable hardware sniffer mode if FIF_CONTROL or
3133 * FIF_OTHER_BSS is requested.
3134 */
3135 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3136 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3137 kfree(cmd);
3138 return;
3139 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003140
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003141 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003142 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003143
3144 if (mwl8k_fw_lock(hw))
3145 return;
3146
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003147 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003148 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003149 priv->sniffer_enabled = false;
3150 }
3151
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003152 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003153 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3154 /*
3155 * Disable the BSS filter.
3156 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003157 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003158 } else {
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003159 u8 *bssid;
3160
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003161 /*
3162 * Enable the BSS filter.
3163 *
3164 * If there is an active STA interface, use that
3165 * interface's BSSID, otherwise use a dummy one
3166 * (where the OUI part needs to be nonzero for
3167 * the BSSID to be accepted by POST_SCAN).
3168 */
3169 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003170 if (priv->vif != NULL)
3171 bssid = MWL8K_VIF(priv->vif)->bssid;
3172
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003173 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003174 }
3175 }
3176
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003177 /*
3178 * If FIF_ALLMULTI is being requested, throw away the command
3179 * packet that ->prepare_multicast() built and replace it with
3180 * a command packet that enables reception of all multicast
3181 * packets.
3182 */
3183 if (*total_flags & FIF_ALLMULTI) {
3184 kfree(cmd);
3185 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3186 }
3187
3188 if (cmd != NULL) {
3189 mwl8k_post_cmd(hw, cmd);
3190 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003191 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003192
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003193 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003194}
3195
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003196static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3197{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003198 return mwl8k_cmd_set_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003199}
3200
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003201static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3202 const struct ieee80211_tx_queue_params *params)
3203{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003204 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003205 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003206
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003207 rc = mwl8k_fw_lock(hw);
3208 if (!rc) {
3209 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003210 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003211
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003212 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003213 rc = mwl8k_cmd_set_edca_params(hw, queue,
3214 params->cw_min,
3215 params->cw_max,
3216 params->aifs,
3217 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003218
3219 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003220 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003221
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003222 return rc;
3223}
3224
3225static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3226 struct ieee80211_tx_queue_stats *stats)
3227{
3228 struct mwl8k_priv *priv = hw->priv;
3229 struct mwl8k_tx_queue *txq;
3230 int index;
3231
3232 spin_lock_bh(&priv->tx_lock);
3233 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3234 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003235 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003236 sizeof(struct ieee80211_tx_queue_stats));
3237 }
3238 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003239
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003240 return 0;
3241}
3242
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003243static int mwl8k_get_stats(struct ieee80211_hw *hw,
3244 struct ieee80211_low_level_stats *stats)
3245{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003246 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003247}
3248
3249static const struct ieee80211_ops mwl8k_ops = {
3250 .tx = mwl8k_tx,
3251 .start = mwl8k_start,
3252 .stop = mwl8k_stop,
3253 .add_interface = mwl8k_add_interface,
3254 .remove_interface = mwl8k_remove_interface,
3255 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003256 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003257 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003258 .configure_filter = mwl8k_configure_filter,
3259 .set_rts_threshold = mwl8k_set_rts_threshold,
3260 .conf_tx = mwl8k_conf_tx,
3261 .get_tx_stats = mwl8k_get_tx_stats,
3262 .get_stats = mwl8k_get_stats,
3263};
3264
3265static void mwl8k_tx_reclaim_handler(unsigned long data)
3266{
3267 int i;
3268 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3269 struct mwl8k_priv *priv = hw->priv;
3270
3271 spin_lock_bh(&priv->tx_lock);
3272 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3273 mwl8k_txq_reclaim(hw, i, 0);
3274
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003275 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003276 complete(priv->tx_wait);
3277 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003278 }
3279 spin_unlock_bh(&priv->tx_lock);
3280}
3281
3282static void mwl8k_finalize_join_worker(struct work_struct *work)
3283{
3284 struct mwl8k_priv *priv =
3285 container_of(work, struct mwl8k_priv, finalize_join_worker);
3286 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003287 u8 dtim = MWL8K_VIF(priv->vif)->bss_info.dtim_period;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003288
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003289 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003290 dev_kfree_skb(skb);
3291
3292 priv->beacon_skb = NULL;
3293}
3294
John W. Linvillebcb628d2009-11-06 16:40:16 -05003295enum {
3296 MWL8687 = 0,
3297 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003298};
3299
John W. Linvillebcb628d2009-11-06 16:40:16 -05003300static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
3301 {
3302 .part_name = "88w8687",
3303 .helper_image = "mwl8k/helper_8687.fw",
3304 .fw_image = "mwl8k/fmimage_8687.fw",
3305 .rxd_ops = &rxd_8687_ops,
3306 .modes = BIT(NL80211_IFTYPE_STATION),
3307 },
3308 {
3309 .part_name = "88w8366",
3310 .helper_image = "mwl8k/helper_8366.fw",
3311 .fw_image = "mwl8k/fmimage_8366.fw",
3312 .rxd_ops = &rxd_8366_ops,
3313 .modes = 0,
3314 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003315};
3316
3317static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003318 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3319 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3320 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3321 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003322};
3323MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3324
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003325static int __devinit mwl8k_probe(struct pci_dev *pdev,
3326 const struct pci_device_id *id)
3327{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003328 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003329 struct ieee80211_hw *hw;
3330 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003331 int rc;
3332 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003333
3334 if (!printed_version) {
3335 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3336 printed_version = 1;
3337 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003338
3339 rc = pci_enable_device(pdev);
3340 if (rc) {
3341 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3342 MWL8K_NAME);
3343 return rc;
3344 }
3345
3346 rc = pci_request_regions(pdev, MWL8K_NAME);
3347 if (rc) {
3348 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3349 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003350 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003351 }
3352
3353 pci_set_master(pdev);
3354
3355 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3356 if (hw == NULL) {
3357 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3358 rc = -ENOMEM;
3359 goto err_free_reg;
3360 }
3361
3362 priv = hw->priv;
3363 priv->hw = hw;
3364 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003365 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02003366 priv->rxd_ops = priv->device_info->rxd_ops;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003367 priv->sniffer_enabled = false;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02003368 priv->wmm_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003369 priv->pending_tx_pkts = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003370
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003371 SET_IEEE80211_DEV(hw, &pdev->dev);
3372 pci_set_drvdata(pdev, hw);
3373
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003374 priv->sram = pci_iomap(pdev, 0, 0x10000);
3375 if (priv->sram == NULL) {
3376 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003377 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003378 goto err_iounmap;
3379 }
3380
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003381 /*
3382 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3383 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3384 */
3385 priv->regs = pci_iomap(pdev, 1, 0x10000);
3386 if (priv->regs == NULL) {
3387 priv->regs = pci_iomap(pdev, 2, 0x10000);
3388 if (priv->regs == NULL) {
3389 printk(KERN_ERR "%s: Cannot map device registers\n",
3390 wiphy_name(hw->wiphy));
3391 goto err_iounmap;
3392 }
3393 }
3394
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003395 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3396 priv->band.band = IEEE80211_BAND_2GHZ;
3397 priv->band.channels = priv->channels;
3398 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3399 priv->band.bitrates = priv->rates;
3400 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3401 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3402
3403 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3404 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3405
3406 /*
3407 * Extra headroom is the size of the required DMA header
3408 * minus the size of the smallest 802.11 frame (CTS frame).
3409 */
3410 hw->extra_tx_headroom =
3411 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3412
3413 hw->channel_change_time = 10;
3414
3415 hw->queues = MWL8K_TX_QUEUES;
3416
Lennert Buytenhek547810e2009-10-22 20:21:02 +02003417 hw->wiphy->interface_modes = priv->device_info->modes;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003418
3419 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003420 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003421 hw->vif_data_size = sizeof(struct mwl8k_vif);
3422 priv->vif = NULL;
3423
3424 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003425 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003426 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003427
3428 /* Finalize join worker */
3429 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3430
3431 /* TX reclaim tasklet */
3432 tasklet_init(&priv->tx_reclaim_task,
3433 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3434 tasklet_disable(&priv->tx_reclaim_task);
3435
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003436 /* Power management cookie */
3437 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3438 if (priv->cookie == NULL)
3439 goto err_iounmap;
3440
3441 rc = mwl8k_rxq_init(hw, 0);
3442 if (rc)
3443 goto err_iounmap;
3444 rxq_refill(hw, 0, INT_MAX);
3445
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003446 mutex_init(&priv->fw_mutex);
3447 priv->fw_mutex_owner = NULL;
3448 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003449 priv->hostcmd_wait = NULL;
3450
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003451 spin_lock_init(&priv->tx_lock);
3452
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003453 priv->tx_wait = NULL;
3454
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003455 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3456 rc = mwl8k_txq_init(hw, i);
3457 if (rc)
3458 goto err_free_queues;
3459 }
3460
3461 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003462 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003463 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3464 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3465
Joe Perchesa0607fd2009-11-18 23:29:17 -08003466 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003467 IRQF_SHARED, MWL8K_NAME, hw);
3468 if (rc) {
3469 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003470 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003471 goto err_free_queues;
3472 }
3473
3474 /* Reset firmware and hardware */
3475 mwl8k_hw_reset(priv);
3476
3477 /* Ask userland hotplug daemon for the device firmware */
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003478 rc = mwl8k_request_firmware(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003479 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003480 printk(KERN_ERR "%s: Firmware files not found\n",
3481 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003482 goto err_free_irq;
3483 }
3484
3485 /* Load firmware into hardware */
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003486 rc = mwl8k_load_firmware(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003487 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003488 printk(KERN_ERR "%s: Cannot start firmware\n",
3489 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003490 goto err_stop_firmware;
3491 }
3492
3493 /* Reclaim memory once firmware is successfully loaded */
3494 mwl8k_release_firmware(priv);
3495
3496 /*
3497 * Temporarily enable interrupts. Initial firmware host
3498 * commands use interrupts and avoids polling. Disable
3499 * interrupts when done.
3500 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003501 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003502
3503 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003504 if (priv->ap_fw) {
3505 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3506 if (!rc)
3507 rc = mwl8k_cmd_set_hw_spec(hw);
3508 } else {
3509 rc = mwl8k_cmd_get_hw_spec_sta(hw);
3510 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003511 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003512 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3513 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003514 goto err_stop_firmware;
3515 }
3516
3517 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003518 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003519 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003520 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003521 goto err_stop_firmware;
3522 }
3523
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003524 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003525 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003526 if (rc) {
3527 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3528 wiphy_name(hw->wiphy));
3529 goto err_stop_firmware;
3530 }
3531
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003532 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003533 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003534 free_irq(priv->pdev->irq, hw);
3535
3536 rc = ieee80211_register_hw(hw);
3537 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003538 printk(KERN_ERR "%s: Cannot register device\n",
3539 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003540 goto err_stop_firmware;
3541 }
3542
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003543 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003544 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003545 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003546 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003547 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3548 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003549
3550 return 0;
3551
3552err_stop_firmware:
3553 mwl8k_hw_reset(priv);
3554 mwl8k_release_firmware(priv);
3555
3556err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003557 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003558 free_irq(priv->pdev->irq, hw);
3559
3560err_free_queues:
3561 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3562 mwl8k_txq_deinit(hw, i);
3563 mwl8k_rxq_deinit(hw, 0);
3564
3565err_iounmap:
3566 if (priv->cookie != NULL)
3567 pci_free_consistent(priv->pdev, 4,
3568 priv->cookie, priv->cookie_dma);
3569
3570 if (priv->regs != NULL)
3571 pci_iounmap(pdev, priv->regs);
3572
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003573 if (priv->sram != NULL)
3574 pci_iounmap(pdev, priv->sram);
3575
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003576 pci_set_drvdata(pdev, NULL);
3577 ieee80211_free_hw(hw);
3578
3579err_free_reg:
3580 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003581
3582err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003583 pci_disable_device(pdev);
3584
3585 return rc;
3586}
3587
Joerg Albert230f7af2009-04-18 02:10:45 +02003588static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003589{
3590 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3591}
3592
Joerg Albert230f7af2009-04-18 02:10:45 +02003593static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003594{
3595 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3596 struct mwl8k_priv *priv;
3597 int i;
3598
3599 if (hw == NULL)
3600 return;
3601 priv = hw->priv;
3602
3603 ieee80211_stop_queues(hw);
3604
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003605 ieee80211_unregister_hw(hw);
3606
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003607 /* Remove tx reclaim tasklet */
3608 tasklet_kill(&priv->tx_reclaim_task);
3609
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003610 /* Stop hardware */
3611 mwl8k_hw_reset(priv);
3612
3613 /* Return all skbs to mac80211 */
3614 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3615 mwl8k_txq_reclaim(hw, i, 1);
3616
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003617 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3618 mwl8k_txq_deinit(hw, i);
3619
3620 mwl8k_rxq_deinit(hw, 0);
3621
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003622 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003623
3624 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003625 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003626 pci_set_drvdata(pdev, NULL);
3627 ieee80211_free_hw(hw);
3628 pci_release_regions(pdev);
3629 pci_disable_device(pdev);
3630}
3631
3632static struct pci_driver mwl8k_driver = {
3633 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003634 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003635 .probe = mwl8k_probe,
3636 .remove = __devexit_p(mwl8k_remove),
3637 .shutdown = __devexit_p(mwl8k_shutdown),
3638};
3639
3640static int __init mwl8k_init(void)
3641{
3642 return pci_register_driver(&mwl8k_driver);
3643}
3644
3645static void __exit mwl8k_exit(void)
3646{
3647 pci_unregister_driver(&mwl8k_driver);
3648}
3649
3650module_init(mwl8k_init);
3651module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003652
3653MODULE_DESCRIPTION(MWL8K_DESC);
3654MODULE_VERSION(MWL8K_VERSION);
3655MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3656MODULE_LICENSE("GPL");