blob: 75c9261abad85fc2b3c1f5c9e743503bc1e49674 [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 +010031static DEFINE_PCI_DEVICE_TABLE(mwl8k_table) = {
32 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = 8687, },
33 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = 8687, },
34 { }
35};
36MODULE_DEVICE_TABLE(pci, mwl8k_table);
37
Lennert Buytenheka66098d2009-03-10 10:13:33 +010038/* Register definitions */
39#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020040#define MWL8K_MODE_STA 0x0000005a
41#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010042#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020043#define MWL8K_FWSTA_READY 0xf0f1f2f4
44#define MWL8K_FWAP_READY 0xf1f2f4a5
45#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010046#define MWL8K_HIU_SCRATCH 0x00000c40
47
48/* Host->device communications */
49#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
50#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
51#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
52#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
53#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020054#define MWL8K_H2A_INT_DUMMY (1 << 20)
55#define MWL8K_H2A_INT_RESET (1 << 15)
56#define MWL8K_H2A_INT_DOORBELL (1 << 1)
57#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010058
59/* Device->host communications */
60#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
61#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
62#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
63#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
64#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020065#define MWL8K_A2H_INT_DUMMY (1 << 20)
66#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
67#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
68#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
69#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
70#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
71#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
72#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
73#define MWL8K_A2H_INT_RX_READY (1 << 1)
74#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010075
76#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
77 MWL8K_A2H_INT_CHNL_SWITCHED | \
78 MWL8K_A2H_INT_QUEUE_EMPTY | \
79 MWL8K_A2H_INT_RADAR_DETECT | \
80 MWL8K_A2H_INT_RADIO_ON | \
81 MWL8K_A2H_INT_RADIO_OFF | \
82 MWL8K_A2H_INT_MAC_EVENT | \
83 MWL8K_A2H_INT_OPC_DONE | \
84 MWL8K_A2H_INT_RX_READY | \
85 MWL8K_A2H_INT_TX_DONE)
86
Lennert Buytenheka66098d2009-03-10 10:13:33 +010087#define MWL8K_RX_QUEUES 1
88#define MWL8K_TX_QUEUES 4
89
90struct mwl8k_rx_queue {
91 int rx_desc_count;
92
93 /* hw receives here */
94 int rx_head;
95
96 /* refill descs here */
97 int rx_tail;
98
99 struct mwl8k_rx_desc *rx_desc_area;
100 dma_addr_t rx_desc_dma;
101 struct sk_buff **rx_skb;
102};
103
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100104struct mwl8k_tx_queue {
105 /* hw transmits here */
106 int tx_head;
107
108 /* sw appends here */
109 int tx_tail;
110
111 struct ieee80211_tx_queue_stats tx_stats;
112 struct mwl8k_tx_desc *tx_desc_area;
113 dma_addr_t tx_desc_dma;
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200114 struct sk_buff **tx_skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115};
116
117/* Pointers to the firmware data and meta information about it. */
118struct mwl8k_firmware {
119 /* Microcode */
120 struct firmware *ucode;
121
122 /* Boot helper code */
123 struct firmware *helper;
124};
125
126struct mwl8k_priv {
127 void __iomem *regs;
128 struct ieee80211_hw *hw;
129
130 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
132 /* firmware files and meta data */
133 struct mwl8k_firmware fw;
134 u32 part_num;
135
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200136 /* firmware access */
137 struct mutex fw_mutex;
138 struct task_struct *fw_mutex_owner;
139 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200140 struct completion *hostcmd_wait;
141
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100142 /* lock held over TX and TX reap */
143 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100144
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200145 /* TX quiesce completion, protected by fw_mutex and tx_lock */
146 struct completion *tx_wait;
147
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100148 struct ieee80211_vif *vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100149
150 struct ieee80211_channel *current_channel;
151
152 /* power management status cookie from firmware */
153 u32 *cookie;
154 dma_addr_t cookie_dma;
155
156 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200158 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100159
160 /*
161 * Running count of TX packets in flight, to avoid
162 * iterating over the transmit rings each time.
163 */
164 int pending_tx_pkts;
165
166 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
167 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
168
169 /* PHY parameters */
170 struct ieee80211_supported_band band;
171 struct ieee80211_channel channels[14];
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200172 struct ieee80211_rate rates[13];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100173
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200174 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200175 bool radio_short_preamble;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200176 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100178 /* XXX need to convert this to handle multiple interfaces */
179 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200180 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100181 struct sk_buff *beacon_skb;
182
183 /*
184 * This FJ worker has to be global as it is scheduled from the
185 * RX handler. At this point we don't know which interface it
186 * belongs to until the list of bssids waiting to complete join
187 * is checked.
188 */
189 struct work_struct finalize_join_worker;
190
191 /* Tasklet to reclaim TX descriptors and buffers after tx */
192 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100193};
194
195/* Per interface specific private data */
196struct mwl8k_vif {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100197 /* backpointer to parent config block */
198 struct mwl8k_priv *priv;
199
200 /* BSS config of AP or IBSS from mac80211*/
201 struct ieee80211_bss_conf bss_info;
202
203 /* BSSID of AP or IBSS */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200204 u8 bssid[ETH_ALEN];
205 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100206
207 /*
208 * Subset of supported legacy rates.
209 * Intersection of AP and STA supported rates.
210 */
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200211 struct ieee80211_rate legacy_rates[13];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100212
213 /* number of supported legacy rates */
214 u8 legacy_nrates;
215
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100216 /* Index into station database.Returned by update_sta_db call */
217 u8 peer_id;
218
219 /* Non AMPDU sequence number assigned by driver */
220 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100221};
222
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200223#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100224
225static const struct ieee80211_channel mwl8k_channels[] = {
226 { .center_freq = 2412, .hw_value = 1, },
227 { .center_freq = 2417, .hw_value = 2, },
228 { .center_freq = 2422, .hw_value = 3, },
229 { .center_freq = 2427, .hw_value = 4, },
230 { .center_freq = 2432, .hw_value = 5, },
231 { .center_freq = 2437, .hw_value = 6, },
232 { .center_freq = 2442, .hw_value = 7, },
233 { .center_freq = 2447, .hw_value = 8, },
234 { .center_freq = 2452, .hw_value = 9, },
235 { .center_freq = 2457, .hw_value = 10, },
236 { .center_freq = 2462, .hw_value = 11, },
237};
238
239static const struct ieee80211_rate mwl8k_rates[] = {
240 { .bitrate = 10, .hw_value = 2, },
241 { .bitrate = 20, .hw_value = 4, },
242 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200243 { .bitrate = 110, .hw_value = 22, },
244 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100245 { .bitrate = 60, .hw_value = 12, },
246 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100247 { .bitrate = 120, .hw_value = 24, },
248 { .bitrate = 180, .hw_value = 36, },
249 { .bitrate = 240, .hw_value = 48, },
250 { .bitrate = 360, .hw_value = 72, },
251 { .bitrate = 480, .hw_value = 96, },
252 { .bitrate = 540, .hw_value = 108, },
253};
254
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100255/* Set or get info from Firmware */
256#define MWL8K_CMD_SET 0x0001
257#define MWL8K_CMD_GET 0x0000
258
259/* Firmware command codes */
260#define MWL8K_CMD_CODE_DNLD 0x0001
261#define MWL8K_CMD_GET_HW_SPEC 0x0003
262#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
263#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200264#define MWL8K_CMD_RADIO_CONTROL 0x001c
265#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100266#define MWL8K_CMD_SET_PRE_SCAN 0x0107
267#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200268#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100269#define MWL8K_CMD_SET_AID 0x010d
270#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200271#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100272#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200273#define MWL8K_CMD_SET_SLOT 0x0114
274#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
275#define MWL8K_CMD_SET_WMM_MODE 0x0123
276#define MWL8K_CMD_MIMO_CONFIG 0x0125
277#define MWL8K_CMD_USE_FIXED_RATE 0x0126
278#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200279#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200280#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
281#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100282
283static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
284{
285#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
286 snprintf(buf, bufsize, "%s", #x);\
287 return buf;\
288 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200289 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100290 MWL8K_CMDNAME(CODE_DNLD);
291 MWL8K_CMDNAME(GET_HW_SPEC);
292 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
293 MWL8K_CMDNAME(GET_STAT);
294 MWL8K_CMDNAME(RADIO_CONTROL);
295 MWL8K_CMDNAME(RF_TX_POWER);
296 MWL8K_CMDNAME(SET_PRE_SCAN);
297 MWL8K_CMDNAME(SET_POST_SCAN);
298 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100299 MWL8K_CMDNAME(SET_AID);
300 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200301 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100302 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200303 MWL8K_CMDNAME(SET_SLOT);
304 MWL8K_CMDNAME(SET_EDCA_PARAMS);
305 MWL8K_CMDNAME(SET_WMM_MODE);
306 MWL8K_CMDNAME(MIMO_CONFIG);
307 MWL8K_CMDNAME(USE_FIXED_RATE);
308 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200309 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200310 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
311 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100312 default:
313 snprintf(buf, bufsize, "0x%x", cmd);
314 }
315#undef MWL8K_CMDNAME
316
317 return buf;
318}
319
320/* Hardware and firmware reset */
321static void mwl8k_hw_reset(struct mwl8k_priv *priv)
322{
323 iowrite32(MWL8K_H2A_INT_RESET,
324 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
325 iowrite32(MWL8K_H2A_INT_RESET,
326 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
327 msleep(20);
328}
329
330/* Release fw image */
331static void mwl8k_release_fw(struct firmware **fw)
332{
333 if (*fw == NULL)
334 return;
335 release_firmware(*fw);
336 *fw = NULL;
337}
338
339static void mwl8k_release_firmware(struct mwl8k_priv *priv)
340{
341 mwl8k_release_fw(&priv->fw.ucode);
342 mwl8k_release_fw(&priv->fw.helper);
343}
344
345/* Request fw image */
346static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200347 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100348{
349 /* release current image */
350 if (*fw != NULL)
351 mwl8k_release_fw(fw);
352
353 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200354 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100355}
356
357static int mwl8k_request_firmware(struct mwl8k_priv *priv, u32 part_num)
358{
359 u8 filename[64];
360 int rc;
361
362 priv->part_num = part_num;
363
364 snprintf(filename, sizeof(filename),
365 "mwl8k/helper_%u.fw", priv->part_num);
366
367 rc = mwl8k_request_fw(priv, filename, &priv->fw.helper);
368 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200369 printk(KERN_ERR "%s: Error requesting helper firmware "
370 "file %s\n", pci_name(priv->pdev), filename);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100371 return rc;
372 }
373
374 snprintf(filename, sizeof(filename),
375 "mwl8k/fmimage_%u.fw", priv->part_num);
376
377 rc = mwl8k_request_fw(priv, filename, &priv->fw.ucode);
378 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200379 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
380 pci_name(priv->pdev), filename);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100381 mwl8k_release_fw(&priv->fw.helper);
382 return rc;
383 }
384
385 return 0;
386}
387
388struct mwl8k_cmd_pkt {
389 __le16 code;
390 __le16 length;
391 __le16 seq_num;
392 __le16 result;
393 char payload[0];
394} __attribute__((packed));
395
396/*
397 * Firmware loading.
398 */
399static int
400mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
401{
402 void __iomem *regs = priv->regs;
403 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100404 int loops;
405
406 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
407 if (pci_dma_mapping_error(priv->pdev, dma_addr))
408 return -ENOMEM;
409
410 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
411 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
412 iowrite32(MWL8K_H2A_INT_DOORBELL,
413 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
414 iowrite32(MWL8K_H2A_INT_DUMMY,
415 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
416
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100417 loops = 1000;
418 do {
419 u32 int_code;
420
421 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
422 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
423 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100424 break;
425 }
426
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200427 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100428 udelay(1);
429 } while (--loops);
430
431 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
432
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200433 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100434}
435
436static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
437 const u8 *data, size_t length)
438{
439 struct mwl8k_cmd_pkt *cmd;
440 int done;
441 int rc = 0;
442
443 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
444 if (cmd == NULL)
445 return -ENOMEM;
446
447 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
448 cmd->seq_num = 0;
449 cmd->result = 0;
450
451 done = 0;
452 while (length) {
453 int block_size = length > 256 ? 256 : length;
454
455 memcpy(cmd->payload, data + done, block_size);
456 cmd->length = cpu_to_le16(block_size);
457
458 rc = mwl8k_send_fw_load_cmd(priv, cmd,
459 sizeof(*cmd) + block_size);
460 if (rc)
461 break;
462
463 done += block_size;
464 length -= block_size;
465 }
466
467 if (!rc) {
468 cmd->length = 0;
469 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
470 }
471
472 kfree(cmd);
473
474 return rc;
475}
476
477static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
478 const u8 *data, size_t length)
479{
480 unsigned char *buffer;
481 int may_continue, rc = 0;
482 u32 done, prev_block_size;
483
484 buffer = kmalloc(1024, GFP_KERNEL);
485 if (buffer == NULL)
486 return -ENOMEM;
487
488 done = 0;
489 prev_block_size = 0;
490 may_continue = 1000;
491 while (may_continue > 0) {
492 u32 block_size;
493
494 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
495 if (block_size & 1) {
496 block_size &= ~1;
497 may_continue--;
498 } else {
499 done += prev_block_size;
500 length -= prev_block_size;
501 }
502
503 if (block_size > 1024 || block_size > length) {
504 rc = -EOVERFLOW;
505 break;
506 }
507
508 if (length == 0) {
509 rc = 0;
510 break;
511 }
512
513 if (block_size == 0) {
514 rc = -EPROTO;
515 may_continue--;
516 udelay(1);
517 continue;
518 }
519
520 prev_block_size = block_size;
521 memcpy(buffer, data + done, block_size);
522
523 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
524 if (rc)
525 break;
526 }
527
528 if (!rc && length != 0)
529 rc = -EREMOTEIO;
530
531 kfree(buffer);
532
533 return rc;
534}
535
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200536static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100537{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200538 struct mwl8k_priv *priv = hw->priv;
539 struct firmware *fw = priv->fw.ucode;
540 int rc;
541 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100542
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200543 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
544 struct firmware *helper = priv->fw.helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100545
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200546 if (helper == NULL) {
547 printk(KERN_ERR "%s: helper image needed but none "
548 "given\n", pci_name(priv->pdev));
549 return -EINVAL;
550 }
551
552 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100553 if (rc) {
554 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200555 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100556 return rc;
557 }
558 msleep(1);
559
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200560 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100561 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200562 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100563 }
564
565 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200566 printk(KERN_ERR "%s: unable to load firmware image\n",
567 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100568 return rc;
569 }
570
571 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
572 msleep(1);
573
574 loops = 200000;
575 do {
576 if (ioread32(priv->regs + MWL8K_HIU_INT_CODE)
577 == MWL8K_FWSTA_READY)
578 break;
579 udelay(1);
580 } while (--loops);
581
582 return loops ? 0 : -ETIMEDOUT;
583}
584
585
586/*
587 * Defines shared between transmission and reception.
588 */
589/* HT control fields for firmware */
590struct ewc_ht_info {
591 __le16 control1;
592 __le16 control2;
593 __le16 control3;
594} __attribute__((packed));
595
596/* Firmware Station database operations */
597#define MWL8K_STA_DB_ADD_ENTRY 0
598#define MWL8K_STA_DB_MODIFY_ENTRY 1
599#define MWL8K_STA_DB_DEL_ENTRY 2
600#define MWL8K_STA_DB_FLUSH 3
601
602/* Peer Entry flags - used to define the type of the peer node */
603#define MWL8K_PEER_TYPE_ACCESSPOINT 2
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100604
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200605#define MWL8K_IEEE_LEGACY_DATA_RATES 13
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100606#define MWL8K_MCS_BITMAP_SIZE 16
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100607
608struct peer_capability_info {
609 /* Peer type - AP vs. STA. */
610 __u8 peer_type;
611
612 /* Basic 802.11 capabilities from assoc resp. */
613 __le16 basic_caps;
614
615 /* Set if peer supports 802.11n high throughput (HT). */
616 __u8 ht_support;
617
618 /* Valid if HT is supported. */
619 __le16 ht_caps;
620 __u8 extended_ht_caps;
621 struct ewc_ht_info ewc_info;
622
623 /* Legacy rate table. Intersection of our rates and peer rates. */
624 __u8 legacy_rates[MWL8K_IEEE_LEGACY_DATA_RATES];
625
626 /* HT rate table. Intersection of our rates and peer rates. */
627 __u8 ht_rates[MWL8K_MCS_BITMAP_SIZE];
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +0200628 __u8 pad[16];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100629
630 /* If set, interoperability mode, no proprietary extensions. */
631 __u8 interop;
632 __u8 pad2;
633 __u8 station_id;
634 __le16 amsdu_enabled;
635} __attribute__((packed));
636
637/* Inline functions to manipulate QoS field in data descriptor. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100638static inline u16 mwl8k_qos_setbit_eosp(u16 qos)
639{
640 u16 val_mask = 1 << 4;
641
642 /* End of Service Period Bit 4 */
643 return qos | val_mask;
644}
645
646static inline u16 mwl8k_qos_setbit_ack(u16 qos, u8 ack_policy)
647{
648 u16 val_mask = 0x3;
649 u8 shift = 5;
650 u16 qos_mask = ~(val_mask << shift);
651
652 /* Ack Policy Bit 5-6 */
653 return (qos & qos_mask) | ((ack_policy & val_mask) << shift);
654}
655
656static inline u16 mwl8k_qos_setbit_amsdu(u16 qos)
657{
658 u16 val_mask = 1 << 7;
659
660 /* AMSDU present Bit 7 */
661 return qos | val_mask;
662}
663
664static inline u16 mwl8k_qos_setbit_qlen(u16 qos, u8 len)
665{
666 u16 val_mask = 0xff;
667 u8 shift = 8;
668 u16 qos_mask = ~(val_mask << shift);
669
670 /* Queue Length Bits 8-15 */
671 return (qos & qos_mask) | ((len & val_mask) << shift);
672}
673
674/* DMA header used by firmware and hardware. */
675struct mwl8k_dma_data {
676 __le16 fwlen;
677 struct ieee80211_hdr wh;
678} __attribute__((packed));
679
680/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200681static inline void mwl8k_remove_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100682{
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200683 struct mwl8k_dma_data *tr = (struct mwl8k_dma_data *)skb->data;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100684 void *dst, *src = &tr->wh;
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200685 int hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100686 u16 space = sizeof(struct mwl8k_dma_data) - hdrlen;
687
688 dst = (void *)tr + space;
689 if (dst != src) {
690 memmove(dst, src, hdrlen);
691 skb_pull(skb, space);
692 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100693}
694
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200695static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100696{
697 struct ieee80211_hdr *wh;
698 u32 hdrlen, pktlen;
699 struct mwl8k_dma_data *tr;
700
701 wh = (struct ieee80211_hdr *)skb->data;
702 hdrlen = ieee80211_hdrlen(wh->frame_control);
703 pktlen = skb->len;
704
705 /*
706 * Copy up/down the 802.11 header; the firmware requires
707 * we present a 2-byte payload length followed by a
708 * 4-address header (w/o QoS), followed (optionally) by
709 * any WEP/ExtIV header (but only filled in for CCMP).
710 */
711 if (hdrlen != sizeof(struct mwl8k_dma_data))
712 skb_push(skb, sizeof(struct mwl8k_dma_data) - hdrlen);
713
714 tr = (struct mwl8k_dma_data *)skb->data;
715 if (wh != &tr->wh)
716 memmove(&tr->wh, wh, hdrlen);
717
718 /* Clear addr4 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200719 memset(tr->wh.addr4, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100720
721 /*
722 * Firmware length is the length of the fully formed "802.11
723 * payload". That is, everything except for the 802.11 header.
724 * This includes all crypto material including the MIC.
725 */
726 tr->fwlen = cpu_to_le16(pktlen - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100727}
728
729
730/*
731 * Packet reception.
732 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100733#define MWL8K_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100734
735struct mwl8k_rx_desc {
736 __le16 pkt_len;
737 __u8 link_quality;
738 __u8 noise_level;
739 __le32 pkt_phys_addr;
740 __le32 next_rx_desc_phys_addr;
741 __le16 qos_control;
742 __le16 rate_info;
743 __le32 pad0[4];
744 __u8 rssi;
745 __u8 channel;
746 __le16 pad1;
747 __u8 rx_ctrl;
748 __u8 rx_status;
749 __u8 pad2[2];
750} __attribute__((packed));
751
752#define MWL8K_RX_DESCS 256
753#define MWL8K_RX_MAXSZ 3800
754
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200755#define RATE_INFO_SHORTPRE 0x8000
756#define RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
757#define RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
758#define RATE_INFO_40MHZ 0x0004
759#define RATE_INFO_SHORTGI 0x0002
760#define RATE_INFO_MCS_FORMAT 0x0001
761
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100762static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
763{
764 struct mwl8k_priv *priv = hw->priv;
765 struct mwl8k_rx_queue *rxq = priv->rxq + index;
766 int size;
767 int i;
768
769 rxq->rx_desc_count = 0;
770 rxq->rx_head = 0;
771 rxq->rx_tail = 0;
772
773 size = MWL8K_RX_DESCS * sizeof(struct mwl8k_rx_desc);
774
775 rxq->rx_desc_area =
776 pci_alloc_consistent(priv->pdev, size, &rxq->rx_desc_dma);
777 if (rxq->rx_desc_area == NULL) {
778 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200779 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100780 return -ENOMEM;
781 }
782 memset(rxq->rx_desc_area, 0, size);
783
784 rxq->rx_skb = kmalloc(MWL8K_RX_DESCS *
785 sizeof(*rxq->rx_skb), GFP_KERNEL);
786 if (rxq->rx_skb == NULL) {
787 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200788 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100789 pci_free_consistent(priv->pdev, size,
790 rxq->rx_desc_area, rxq->rx_desc_dma);
791 return -ENOMEM;
792 }
793 memset(rxq->rx_skb, 0, MWL8K_RX_DESCS * sizeof(*rxq->rx_skb));
794
795 for (i = 0; i < MWL8K_RX_DESCS; i++) {
796 struct mwl8k_rx_desc *rx_desc;
797 int nexti;
798
799 rx_desc = rxq->rx_desc_area + i;
800 nexti = (i + 1) % MWL8K_RX_DESCS;
801
802 rx_desc->next_rx_desc_phys_addr =
803 cpu_to_le32(rxq->rx_desc_dma
804 + nexti * sizeof(*rx_desc));
Rami Rosenc491bf12009-04-21 16:22:01 +0300805 rx_desc->rx_ctrl = MWL8K_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100806 }
807
808 return 0;
809}
810
811static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
812{
813 struct mwl8k_priv *priv = hw->priv;
814 struct mwl8k_rx_queue *rxq = priv->rxq + index;
815 int refilled;
816
817 refilled = 0;
818 while (rxq->rx_desc_count < MWL8K_RX_DESCS && limit--) {
819 struct sk_buff *skb;
820 int rx;
821
822 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
823 if (skb == NULL)
824 break;
825
826 rxq->rx_desc_count++;
827
828 rx = rxq->rx_tail;
829 rxq->rx_tail = (rx + 1) % MWL8K_RX_DESCS;
830
831 rxq->rx_desc_area[rx].pkt_phys_addr =
832 cpu_to_le32(pci_map_single(priv->pdev, skb->data,
833 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE));
834
835 rxq->rx_desc_area[rx].pkt_len = cpu_to_le16(MWL8K_RX_MAXSZ);
836 rxq->rx_skb[rx] = skb;
837 wmb();
838 rxq->rx_desc_area[rx].rx_ctrl = 0;
839
840 refilled++;
841 }
842
843 return refilled;
844}
845
846/* Must be called only when the card's reception is completely halted */
847static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
848{
849 struct mwl8k_priv *priv = hw->priv;
850 struct mwl8k_rx_queue *rxq = priv->rxq + index;
851 int i;
852
853 for (i = 0; i < MWL8K_RX_DESCS; i++) {
854 if (rxq->rx_skb[i] != NULL) {
855 unsigned long addr;
856
857 addr = le32_to_cpu(rxq->rx_desc_area[i].pkt_phys_addr);
858 pci_unmap_single(priv->pdev, addr, MWL8K_RX_MAXSZ,
859 PCI_DMA_FROMDEVICE);
860 kfree_skb(rxq->rx_skb[i]);
861 rxq->rx_skb[i] = NULL;
862 }
863 }
864
865 kfree(rxq->rx_skb);
866 rxq->rx_skb = NULL;
867
868 pci_free_consistent(priv->pdev,
869 MWL8K_RX_DESCS * sizeof(struct mwl8k_rx_desc),
870 rxq->rx_desc_area, rxq->rx_desc_dma);
871 rxq->rx_desc_area = NULL;
872}
873
874
875/*
876 * Scan a list of BSSIDs to process for finalize join.
877 * Allows for extension to process multiple BSSIDs.
878 */
879static inline int
880mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
881{
882 return priv->capture_beacon &&
883 ieee80211_is_beacon(wh->frame_control) &&
884 !compare_ether_addr(wh->addr3, priv->capture_bssid);
885}
886
Lennert Buytenhek37797522009-10-22 20:19:45 +0200887static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
888 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100889{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200890 struct mwl8k_priv *priv = hw->priv;
891
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100892 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200893 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100894
895 /*
896 * Use GFP_ATOMIC as rxq_process is called from
897 * the primary interrupt handler, memory allocation call
898 * must not sleep.
899 */
900 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
901 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200902 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100903}
904
905static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
906{
907 struct mwl8k_priv *priv = hw->priv;
908 struct mwl8k_rx_queue *rxq = priv->rxq + index;
909 int processed;
910
911 processed = 0;
912 while (rxq->rx_desc_count && limit--) {
913 struct mwl8k_rx_desc *rx_desc;
914 struct sk_buff *skb;
915 struct ieee80211_rx_status status;
916 unsigned long addr;
917 struct ieee80211_hdr *wh;
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200918 u16 rate_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100919
920 rx_desc = rxq->rx_desc_area + rxq->rx_head;
921 if (!(rx_desc->rx_ctrl & MWL8K_RX_CTRL_OWNED_BY_HOST))
922 break;
923 rmb();
924
925 skb = rxq->rx_skb[rxq->rx_head];
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +0200926 if (skb == NULL)
927 break;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100928 rxq->rx_skb[rxq->rx_head] = NULL;
929
930 rxq->rx_head = (rxq->rx_head + 1) % MWL8K_RX_DESCS;
931 rxq->rx_desc_count--;
932
933 addr = le32_to_cpu(rx_desc->pkt_phys_addr);
934 pci_unmap_single(priv->pdev, addr,
935 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
936
937 skb_put(skb, le16_to_cpu(rx_desc->pkt_len));
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200938 mwl8k_remove_dma_header(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100939
940 wh = (struct ieee80211_hdr *)skb->data;
941
942 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200943 * Check for a pending join operation. Save a
944 * copy of the beacon and schedule a tasklet to
945 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100946 */
947 if (mwl8k_capture_bssid(priv, wh))
Lennert Buytenhek37797522009-10-22 20:19:45 +0200948 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100949
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200950 rate_info = le16_to_cpu(rx_desc->rate_info);
951
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100952 memset(&status, 0, sizeof(status));
953 status.mactime = 0;
954 status.signal = -rx_desc->rssi;
955 status.noise = -rx_desc->noise_level;
956 status.qual = rx_desc->link_quality;
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200957 status.antenna = RATE_INFO_ANTSELECT(rate_info);
958 status.rate_idx = RATE_INFO_RATEID(rate_info);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100959 status.flag = 0;
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200960 if (rate_info & RATE_INFO_SHORTPRE)
961 status.flag |= RX_FLAG_SHORTPRE;
962 if (rate_info & RATE_INFO_40MHZ)
963 status.flag |= RX_FLAG_40MHZ;
964 if (rate_info & RATE_INFO_SHORTGI)
965 status.flag |= RX_FLAG_SHORT_GI;
966 if (rate_info & RATE_INFO_MCS_FORMAT)
967 status.flag |= RX_FLAG_HT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100968 status.band = IEEE80211_BAND_2GHZ;
969 status.freq = ieee80211_channel_to_frequency(rx_desc->channel);
Johannes Bergf1d58c22009-06-17 13:13:00 +0200970 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
971 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100972
973 processed++;
974 }
975
976 return processed;
977}
978
979
980/*
981 * Packet transmission.
982 */
983
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100984/* Transmit packet ACK policy */
985#define MWL8K_TXD_ACK_POLICY_NORMAL 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100986#define MWL8K_TXD_ACK_POLICY_BLOCKACK 3
987
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100988#define MWL8K_TXD_STATUS_OK 0x00000001
989#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
990#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
991#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100992#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100993
994struct mwl8k_tx_desc {
995 __le32 status;
996 __u8 data_rate;
997 __u8 tx_priority;
998 __le16 qos_control;
999 __le32 pkt_phys_addr;
1000 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001001 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001002 __le32 next_tx_desc_phys_addr;
1003 __le32 reserved;
1004 __le16 rate_info;
1005 __u8 peer_id;
1006 __u8 tx_frag_cnt;
1007} __attribute__((packed));
1008
1009#define MWL8K_TX_DESCS 128
1010
1011static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1012{
1013 struct mwl8k_priv *priv = hw->priv;
1014 struct mwl8k_tx_queue *txq = priv->txq + index;
1015 int size;
1016 int i;
1017
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001018 memset(&txq->tx_stats, 0, sizeof(struct ieee80211_tx_queue_stats));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001019 txq->tx_stats.limit = MWL8K_TX_DESCS;
1020 txq->tx_head = 0;
1021 txq->tx_tail = 0;
1022
1023 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1024
1025 txq->tx_desc_area =
1026 pci_alloc_consistent(priv->pdev, size, &txq->tx_desc_dma);
1027 if (txq->tx_desc_area == NULL) {
1028 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001029 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001030 return -ENOMEM;
1031 }
1032 memset(txq->tx_desc_area, 0, size);
1033
1034 txq->tx_skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->tx_skb),
1035 GFP_KERNEL);
1036 if (txq->tx_skb == NULL) {
1037 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001038 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001039 pci_free_consistent(priv->pdev, size,
1040 txq->tx_desc_area, txq->tx_desc_dma);
1041 return -ENOMEM;
1042 }
1043 memset(txq->tx_skb, 0, MWL8K_TX_DESCS * sizeof(*txq->tx_skb));
1044
1045 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1046 struct mwl8k_tx_desc *tx_desc;
1047 int nexti;
1048
1049 tx_desc = txq->tx_desc_area + i;
1050 nexti = (i + 1) % MWL8K_TX_DESCS;
1051
1052 tx_desc->status = 0;
1053 tx_desc->next_tx_desc_phys_addr =
1054 cpu_to_le32(txq->tx_desc_dma +
1055 nexti * sizeof(*tx_desc));
1056 }
1057
1058 return 0;
1059}
1060
1061static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1062{
1063 iowrite32(MWL8K_H2A_INT_PPA_READY,
1064 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1065 iowrite32(MWL8K_H2A_INT_DUMMY,
1066 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1067 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1068}
1069
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001070struct mwl8k_txq_info {
1071 u32 fw_owned;
1072 u32 drv_owned;
1073 u32 unused;
1074 u32 len;
1075 u32 head;
1076 u32 tail;
1077};
1078
1079static int mwl8k_scan_tx_ring(struct mwl8k_priv *priv,
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001080 struct mwl8k_txq_info *txinfo)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001081{
1082 int count, desc, status;
1083 struct mwl8k_tx_queue *txq;
1084 struct mwl8k_tx_desc *tx_desc;
1085 int ndescs = 0;
1086
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001087 memset(txinfo, 0, MWL8K_TX_QUEUES * sizeof(struct mwl8k_txq_info));
1088
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001089 for (count = 0; count < MWL8K_TX_QUEUES; count++) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001090 txq = priv->txq + count;
1091 txinfo[count].len = txq->tx_stats.len;
1092 txinfo[count].head = txq->tx_head;
1093 txinfo[count].tail = txq->tx_tail;
1094 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
1095 tx_desc = txq->tx_desc_area + desc;
1096 status = le32_to_cpu(tx_desc->status);
1097
1098 if (status & MWL8K_TXD_STATUS_FW_OWNED)
1099 txinfo[count].fw_owned++;
1100 else
1101 txinfo[count].drv_owned++;
1102
1103 if (tx_desc->pkt_len == 0)
1104 txinfo[count].unused++;
1105 }
1106 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001107
1108 return ndescs;
1109}
1110
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001111/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001112 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001113 */
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001114static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001115{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001116 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001117 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001118 u32 count;
1119 unsigned long timeout;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001120
1121 might_sleep();
1122
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001123 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001124 count = priv->pending_tx_pkts;
1125 if (count)
1126 priv->tx_wait = &tx_wait;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001127 spin_unlock_bh(&priv->tx_lock);
1128
1129 if (count) {
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001130 struct mwl8k_txq_info txinfo[MWL8K_TX_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001131 int index;
1132 int newcount;
1133
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001134 timeout = wait_for_completion_timeout(&tx_wait,
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001135 msecs_to_jiffies(5000));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001136 if (timeout)
1137 return 0;
1138
1139 spin_lock_bh(&priv->tx_lock);
1140 priv->tx_wait = NULL;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001141 newcount = priv->pending_tx_pkts;
1142 mwl8k_scan_tx_ring(priv, txinfo);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001143 spin_unlock_bh(&priv->tx_lock);
1144
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001145 printk(KERN_ERR "%s(%u) TIMEDOUT:5000ms Pend:%u-->%u\n",
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001146 __func__, __LINE__, count, newcount);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001147
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001148 for (index = 0; index < MWL8K_TX_QUEUES; index++)
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001149 printk(KERN_ERR "TXQ:%u L:%u H:%u T:%u FW:%u "
1150 "DRV:%u U:%u\n",
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001151 index,
1152 txinfo[index].len,
1153 txinfo[index].head,
1154 txinfo[index].tail,
1155 txinfo[index].fw_owned,
1156 txinfo[index].drv_owned,
1157 txinfo[index].unused);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001158
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159 return -ETIMEDOUT;
1160 }
1161
1162 return 0;
1163}
1164
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001165#define MWL8K_TXD_SUCCESS(status) \
1166 ((status) & (MWL8K_TXD_STATUS_OK | \
1167 MWL8K_TXD_STATUS_OK_RETRY | \
1168 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001169
1170static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1171{
1172 struct mwl8k_priv *priv = hw->priv;
1173 struct mwl8k_tx_queue *txq = priv->txq + index;
1174 int wake = 0;
1175
1176 while (txq->tx_stats.len > 0) {
1177 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001178 struct mwl8k_tx_desc *tx_desc;
1179 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001180 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001181 struct sk_buff *skb;
1182 struct ieee80211_tx_info *info;
1183 u32 status;
1184
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001185 tx = txq->tx_head;
1186 tx_desc = txq->tx_desc_area + tx;
1187
1188 status = le32_to_cpu(tx_desc->status);
1189
1190 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1191 if (!force)
1192 break;
1193 tx_desc->status &=
1194 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1195 }
1196
1197 txq->tx_head = (tx + 1) % MWL8K_TX_DESCS;
1198 BUG_ON(txq->tx_stats.len == 0);
1199 txq->tx_stats.len--;
1200 priv->pending_tx_pkts--;
1201
1202 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001203 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001204 skb = txq->tx_skb[tx];
1205 txq->tx_skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001206
1207 BUG_ON(skb == NULL);
1208 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1209
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001210 mwl8k_remove_dma_header(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001211
1212 /* Mark descriptor as unused */
1213 tx_desc->pkt_phys_addr = 0;
1214 tx_desc->pkt_len = 0;
1215
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001216 info = IEEE80211_SKB_CB(skb);
1217 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001218 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001219 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001220
1221 ieee80211_tx_status_irqsafe(hw, skb);
1222
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001223 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001224 }
1225
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001226 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001227 ieee80211_wake_queue(hw, index);
1228}
1229
1230/* must be called only when the card's transmit is completely halted */
1231static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1232{
1233 struct mwl8k_priv *priv = hw->priv;
1234 struct mwl8k_tx_queue *txq = priv->txq + index;
1235
1236 mwl8k_txq_reclaim(hw, index, 1);
1237
1238 kfree(txq->tx_skb);
1239 txq->tx_skb = NULL;
1240
1241 pci_free_consistent(priv->pdev,
1242 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
1243 txq->tx_desc_area, txq->tx_desc_dma);
1244 txq->tx_desc_area = NULL;
1245}
1246
1247static int
1248mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1249{
1250 struct mwl8k_priv *priv = hw->priv;
1251 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001252 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001253 struct ieee80211_hdr *wh;
1254 struct mwl8k_tx_queue *txq;
1255 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001256 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001257 u32 txstatus;
1258 u8 txdatarate;
1259 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001260
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001261 wh = (struct ieee80211_hdr *)skb->data;
1262 if (ieee80211_is_data_qos(wh->frame_control))
1263 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1264 else
1265 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001266
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001267 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001268 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001269
1270 tx_info = IEEE80211_SKB_CB(skb);
1271 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001272
1273 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1274 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001275
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001276 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1277 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1278 mwl8k_vif->seqno = seqno++ % 4096;
1279 }
1280
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001281 /* Setup firmware control bit fields for each frame type. */
1282 txstatus = 0;
1283 txdatarate = 0;
1284 if (ieee80211_is_mgmt(wh->frame_control) ||
1285 ieee80211_is_ctl(wh->frame_control)) {
1286 txdatarate = 0;
1287 qos = mwl8k_qos_setbit_eosp(qos);
1288 /* Set Queue size to unspecified */
1289 qos = mwl8k_qos_setbit_qlen(qos, 0xff);
1290 } else if (ieee80211_is_data(wh->frame_control)) {
1291 txdatarate = 1;
1292 if (is_multicast_ether_addr(wh->addr1))
1293 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1294
1295 /* Send pkt in an aggregate if AMPDU frame. */
1296 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1297 qos = mwl8k_qos_setbit_ack(qos,
1298 MWL8K_TXD_ACK_POLICY_BLOCKACK);
1299 else
1300 qos = mwl8k_qos_setbit_ack(qos,
1301 MWL8K_TXD_ACK_POLICY_NORMAL);
1302
1303 if (qos & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
1304 qos = mwl8k_qos_setbit_amsdu(qos);
1305 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001306
1307 dma = pci_map_single(priv->pdev, skb->data,
1308 skb->len, PCI_DMA_TODEVICE);
1309
1310 if (pci_dma_mapping_error(priv->pdev, dma)) {
1311 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001312 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001313 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001314 return NETDEV_TX_OK;
1315 }
1316
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001317 spin_lock_bh(&priv->tx_lock);
1318
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001319 txq = priv->txq + index;
1320
1321 BUG_ON(txq->tx_skb[txq->tx_tail] != NULL);
1322 txq->tx_skb[txq->tx_tail] = skb;
1323
1324 tx = txq->tx_desc_area + txq->tx_tail;
1325 tx->data_rate = txdatarate;
1326 tx->tx_priority = index;
1327 tx->qos_control = cpu_to_le16(qos);
1328 tx->pkt_phys_addr = cpu_to_le32(dma);
1329 tx->pkt_len = cpu_to_le16(skb->len);
1330 tx->rate_info = 0;
1331 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001332 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001333 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1334
1335 txq->tx_stats.count++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001336 txq->tx_stats.len++;
1337 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001338
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001339 txq->tx_tail++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340 if (txq->tx_tail == MWL8K_TX_DESCS)
1341 txq->tx_tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001342
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001343 if (txq->tx_head == txq->tx_tail)
1344 ieee80211_stop_queue(hw, index);
1345
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001346 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001347
1348 spin_unlock_bh(&priv->tx_lock);
1349
1350 return NETDEV_TX_OK;
1351}
1352
1353
1354/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001355 * Firmware access.
1356 *
1357 * We have the following requirements for issuing firmware commands:
1358 * - Some commands require that the packet transmit path is idle when
1359 * the command is issued. (For simplicity, we'll just quiesce the
1360 * transmit path for every command.)
1361 * - There are certain sequences of commands that need to be issued to
1362 * the hardware sequentially, with no other intervening commands.
1363 *
1364 * This leads to an implementation of a "firmware lock" as a mutex that
1365 * can be taken recursively, and which is taken by both the low-level
1366 * command submission function (mwl8k_post_cmd) as well as any users of
1367 * that function that require issuing of an atomic sequence of commands,
1368 * and quiesces the transmit path whenever it's taken.
1369 */
1370static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1371{
1372 struct mwl8k_priv *priv = hw->priv;
1373
1374 if (priv->fw_mutex_owner != current) {
1375 int rc;
1376
1377 mutex_lock(&priv->fw_mutex);
1378 ieee80211_stop_queues(hw);
1379
1380 rc = mwl8k_tx_wait_empty(hw);
1381 if (rc) {
1382 ieee80211_wake_queues(hw);
1383 mutex_unlock(&priv->fw_mutex);
1384
1385 return rc;
1386 }
1387
1388 priv->fw_mutex_owner = current;
1389 }
1390
1391 priv->fw_mutex_depth++;
1392
1393 return 0;
1394}
1395
1396static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1397{
1398 struct mwl8k_priv *priv = hw->priv;
1399
1400 if (!--priv->fw_mutex_depth) {
1401 ieee80211_wake_queues(hw);
1402 priv->fw_mutex_owner = NULL;
1403 mutex_unlock(&priv->fw_mutex);
1404 }
1405}
1406
1407
1408/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001409 * Command processing.
1410 */
1411
1412/* Timeout firmware commands after 2000ms */
1413#define MWL8K_CMD_TIMEOUT_MS 2000
1414
1415static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1416{
1417 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1418 struct mwl8k_priv *priv = hw->priv;
1419 void __iomem *regs = priv->regs;
1420 dma_addr_t dma_addr;
1421 unsigned int dma_size;
1422 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001423 unsigned long timeout = 0;
1424 u8 buf[32];
1425
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001426 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001427 dma_size = le16_to_cpu(cmd->length);
1428 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1429 PCI_DMA_BIDIRECTIONAL);
1430 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1431 return -ENOMEM;
1432
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001433 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001434 if (rc) {
1435 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1436 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001437 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001438 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001440 priv->hostcmd_wait = &cmd_wait;
1441 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1442 iowrite32(MWL8K_H2A_INT_DOORBELL,
1443 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1444 iowrite32(MWL8K_H2A_INT_DUMMY,
1445 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001446
1447 timeout = wait_for_completion_timeout(&cmd_wait,
1448 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1449
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001450 priv->hostcmd_wait = NULL;
1451
1452 mwl8k_fw_unlock(hw);
1453
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001454 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1455 PCI_DMA_BIDIRECTIONAL);
1456
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001457 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001458 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001459 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001460 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1461 MWL8K_CMD_TIMEOUT_MS);
1462 rc = -ETIMEDOUT;
1463 } else {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001464 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001465 if (rc)
1466 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001467 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001468 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001469 le16_to_cpu(cmd->result));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001470 }
1471
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001472 return rc;
1473}
1474
1475/*
1476 * GET_HW_SPEC.
1477 */
1478struct mwl8k_cmd_get_hw_spec {
1479 struct mwl8k_cmd_pkt header;
1480 __u8 hw_rev;
1481 __u8 host_interface;
1482 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001483 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001484 __le16 region_code;
1485 __le32 fw_rev;
1486 __le32 ps_cookie;
1487 __le32 caps;
1488 __u8 mcs_bitmap[16];
1489 __le32 rx_queue_ptr;
1490 __le32 num_tx_queues;
1491 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1492 __le32 caps2;
1493 __le32 num_tx_desc_per_queue;
1494 __le32 total_rx_desc;
1495} __attribute__((packed));
1496
1497static int mwl8k_cmd_get_hw_spec(struct ieee80211_hw *hw)
1498{
1499 struct mwl8k_priv *priv = hw->priv;
1500 struct mwl8k_cmd_get_hw_spec *cmd;
1501 int rc;
1502 int i;
1503
1504 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1505 if (cmd == NULL)
1506 return -ENOMEM;
1507
1508 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1509 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1510
1511 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1512 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1513 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rx_desc_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001514 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001515 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1516 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].tx_desc_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001517 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1518 cmd->total_rx_desc = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001519
1520 rc = mwl8k_post_cmd(hw, &cmd->header);
1521
1522 if (!rc) {
1523 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1524 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001525 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001526 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001527 }
1528
1529 kfree(cmd);
1530 return rc;
1531}
1532
1533/*
1534 * CMD_MAC_MULTICAST_ADR.
1535 */
1536struct mwl8k_cmd_mac_multicast_adr {
1537 struct mwl8k_cmd_pkt header;
1538 __le16 action;
1539 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001540 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001541};
1542
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001543#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1544#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1545#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1546#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001547
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001548static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001549__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001550 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001551{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001552 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001553 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001554 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001555
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001556 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001557 allmulti = 1;
1558 mc_count = 0;
1559 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001560
1561 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1562
1563 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001564 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001565 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001566
1567 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1568 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001569 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1570 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001571
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001572 if (allmulti) {
1573 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1574 } else if (mc_count) {
1575 int i;
1576
1577 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1578 cmd->numaddr = cpu_to_le16(mc_count);
1579 for (i = 0; i < mc_count && mclist; i++) {
1580 if (mclist->da_addrlen != ETH_ALEN) {
1581 kfree(cmd);
1582 return NULL;
1583 }
1584 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1585 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001586 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001587 }
1588
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001589 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001590}
1591
1592/*
1593 * CMD_802_11_GET_STAT.
1594 */
1595struct mwl8k_cmd_802_11_get_stat {
1596 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001597 __le32 stats[64];
1598} __attribute__((packed));
1599
1600#define MWL8K_STAT_ACK_FAILURE 9
1601#define MWL8K_STAT_RTS_FAILURE 12
1602#define MWL8K_STAT_FCS_ERROR 24
1603#define MWL8K_STAT_RTS_SUCCESS 11
1604
1605static int mwl8k_cmd_802_11_get_stat(struct ieee80211_hw *hw,
1606 struct ieee80211_low_level_stats *stats)
1607{
1608 struct mwl8k_cmd_802_11_get_stat *cmd;
1609 int rc;
1610
1611 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1612 if (cmd == NULL)
1613 return -ENOMEM;
1614
1615 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1616 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001617
1618 rc = mwl8k_post_cmd(hw, &cmd->header);
1619 if (!rc) {
1620 stats->dot11ACKFailureCount =
1621 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1622 stats->dot11RTSFailureCount =
1623 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1624 stats->dot11FCSErrorCount =
1625 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1626 stats->dot11RTSSuccessCount =
1627 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1628 }
1629 kfree(cmd);
1630
1631 return rc;
1632}
1633
1634/*
1635 * CMD_802_11_RADIO_CONTROL.
1636 */
1637struct mwl8k_cmd_802_11_radio_control {
1638 struct mwl8k_cmd_pkt header;
1639 __le16 action;
1640 __le16 control;
1641 __le16 radio_on;
1642} __attribute__((packed));
1643
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001644static int
1645mwl8k_cmd_802_11_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001646{
1647 struct mwl8k_priv *priv = hw->priv;
1648 struct mwl8k_cmd_802_11_radio_control *cmd;
1649 int rc;
1650
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001651 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001652 return 0;
1653
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001654 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1655 if (cmd == NULL)
1656 return -ENOMEM;
1657
1658 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1659 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1660 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001661 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001662 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1663
1664 rc = mwl8k_post_cmd(hw, &cmd->header);
1665 kfree(cmd);
1666
1667 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001668 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001669
1670 return rc;
1671}
1672
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001673static int mwl8k_cmd_802_11_radio_disable(struct ieee80211_hw *hw)
1674{
1675 return mwl8k_cmd_802_11_radio_control(hw, 0, 0);
1676}
1677
1678static int mwl8k_cmd_802_11_radio_enable(struct ieee80211_hw *hw)
1679{
1680 return mwl8k_cmd_802_11_radio_control(hw, 1, 0);
1681}
1682
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001683static int
1684mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1685{
1686 struct mwl8k_priv *priv;
1687
1688 if (hw == NULL || hw->priv == NULL)
1689 return -EINVAL;
1690 priv = hw->priv;
1691
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001692 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001693
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001694 return mwl8k_cmd_802_11_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001695}
1696
1697/*
1698 * CMD_802_11_RF_TX_POWER.
1699 */
1700#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1701
1702struct mwl8k_cmd_802_11_rf_tx_power {
1703 struct mwl8k_cmd_pkt header;
1704 __le16 action;
1705 __le16 support_level;
1706 __le16 current_level;
1707 __le16 reserved;
1708 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1709} __attribute__((packed));
1710
1711static int mwl8k_cmd_802_11_rf_tx_power(struct ieee80211_hw *hw, int dBm)
1712{
1713 struct mwl8k_cmd_802_11_rf_tx_power *cmd;
1714 int rc;
1715
1716 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1717 if (cmd == NULL)
1718 return -ENOMEM;
1719
1720 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1721 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1722 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1723 cmd->support_level = cpu_to_le16(dBm);
1724
1725 rc = mwl8k_post_cmd(hw, &cmd->header);
1726 kfree(cmd);
1727
1728 return rc;
1729}
1730
1731/*
1732 * CMD_SET_PRE_SCAN.
1733 */
1734struct mwl8k_cmd_set_pre_scan {
1735 struct mwl8k_cmd_pkt header;
1736} __attribute__((packed));
1737
1738static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
1739{
1740 struct mwl8k_cmd_set_pre_scan *cmd;
1741 int rc;
1742
1743 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1744 if (cmd == NULL)
1745 return -ENOMEM;
1746
1747 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
1748 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1749
1750 rc = mwl8k_post_cmd(hw, &cmd->header);
1751 kfree(cmd);
1752
1753 return rc;
1754}
1755
1756/*
1757 * CMD_SET_POST_SCAN.
1758 */
1759struct mwl8k_cmd_set_post_scan {
1760 struct mwl8k_cmd_pkt header;
1761 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001762 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001763} __attribute__((packed));
1764
1765static int
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001766mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001767{
1768 struct mwl8k_cmd_set_post_scan *cmd;
1769 int rc;
1770
1771 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1772 if (cmd == NULL)
1773 return -ENOMEM;
1774
1775 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
1776 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1777 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001778 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001779
1780 rc = mwl8k_post_cmd(hw, &cmd->header);
1781 kfree(cmd);
1782
1783 return rc;
1784}
1785
1786/*
1787 * CMD_SET_RF_CHANNEL.
1788 */
1789struct mwl8k_cmd_set_rf_channel {
1790 struct mwl8k_cmd_pkt header;
1791 __le16 action;
1792 __u8 current_channel;
1793 __le32 channel_flags;
1794} __attribute__((packed));
1795
1796static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
1797 struct ieee80211_channel *channel)
1798{
1799 struct mwl8k_cmd_set_rf_channel *cmd;
1800 int rc;
1801
1802 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1803 if (cmd == NULL)
1804 return -ENOMEM;
1805
1806 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
1807 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1808 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1809 cmd->current_channel = channel->hw_value;
1810 if (channel->band == IEEE80211_BAND_2GHZ)
1811 cmd->channel_flags = cpu_to_le32(0x00000081);
1812 else
1813 cmd->channel_flags = cpu_to_le32(0x00000000);
1814
1815 rc = mwl8k_post_cmd(hw, &cmd->header);
1816 kfree(cmd);
1817
1818 return rc;
1819}
1820
1821/*
1822 * CMD_SET_SLOT.
1823 */
1824struct mwl8k_cmd_set_slot {
1825 struct mwl8k_cmd_pkt header;
1826 __le16 action;
1827 __u8 short_slot;
1828} __attribute__((packed));
1829
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02001830static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001831{
1832 struct mwl8k_cmd_set_slot *cmd;
1833 int rc;
1834
1835 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1836 if (cmd == NULL)
1837 return -ENOMEM;
1838
1839 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
1840 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1841 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02001842 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001843
1844 rc = mwl8k_post_cmd(hw, &cmd->header);
1845 kfree(cmd);
1846
1847 return rc;
1848}
1849
1850/*
1851 * CMD_MIMO_CONFIG.
1852 */
1853struct mwl8k_cmd_mimo_config {
1854 struct mwl8k_cmd_pkt header;
1855 __le32 action;
1856 __u8 rx_antenna_map;
1857 __u8 tx_antenna_map;
1858} __attribute__((packed));
1859
1860static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
1861{
1862 struct mwl8k_cmd_mimo_config *cmd;
1863 int rc;
1864
1865 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1866 if (cmd == NULL)
1867 return -ENOMEM;
1868
1869 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
1870 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1871 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
1872 cmd->rx_antenna_map = rx;
1873 cmd->tx_antenna_map = tx;
1874
1875 rc = mwl8k_post_cmd(hw, &cmd->header);
1876 kfree(cmd);
1877
1878 return rc;
1879}
1880
1881/*
1882 * CMD_ENABLE_SNIFFER.
1883 */
1884struct mwl8k_cmd_enable_sniffer {
1885 struct mwl8k_cmd_pkt header;
1886 __le32 action;
1887} __attribute__((packed));
1888
1889static int mwl8k_enable_sniffer(struct ieee80211_hw *hw, bool enable)
1890{
1891 struct mwl8k_cmd_enable_sniffer *cmd;
1892 int rc;
1893
1894 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1895 if (cmd == NULL)
1896 return -ENOMEM;
1897
1898 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
1899 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001900 cmd->action = cpu_to_le32(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001901
1902 rc = mwl8k_post_cmd(hw, &cmd->header);
1903 kfree(cmd);
1904
1905 return rc;
1906}
1907
1908/*
Lennert Buytenhek32060e12009-10-22 20:20:04 +02001909 * CMD_SET_MAC_ADDR.
1910 */
1911struct mwl8k_cmd_set_mac_addr {
1912 struct mwl8k_cmd_pkt header;
1913 __u8 mac_addr[ETH_ALEN];
1914} __attribute__((packed));
1915
1916static int mwl8k_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
1917{
1918 struct mwl8k_cmd_set_mac_addr *cmd;
1919 int rc;
1920
1921 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1922 if (cmd == NULL)
1923 return -ENOMEM;
1924
1925 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
1926 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1927 memcpy(cmd->mac_addr, mac, ETH_ALEN);
1928
1929 rc = mwl8k_post_cmd(hw, &cmd->header);
1930 kfree(cmd);
1931
1932 return rc;
1933}
1934
1935
1936/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001937 * CMD_SET_RATEADAPT_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001938 */
1939struct mwl8k_cmd_set_rate_adapt_mode {
1940 struct mwl8k_cmd_pkt header;
1941 __le16 action;
1942 __le16 mode;
1943} __attribute__((packed));
1944
1945static int mwl8k_cmd_setrateadaptmode(struct ieee80211_hw *hw, __u16 mode)
1946{
1947 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
1948 int rc;
1949
1950 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1951 if (cmd == NULL)
1952 return -ENOMEM;
1953
1954 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
1955 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1956 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1957 cmd->mode = cpu_to_le16(mode);
1958
1959 rc = mwl8k_post_cmd(hw, &cmd->header);
1960 kfree(cmd);
1961
1962 return rc;
1963}
1964
1965/*
1966 * CMD_SET_WMM_MODE.
1967 */
1968struct mwl8k_cmd_set_wmm {
1969 struct mwl8k_cmd_pkt header;
1970 __le16 action;
1971} __attribute__((packed));
1972
1973static int mwl8k_set_wmm(struct ieee80211_hw *hw, bool enable)
1974{
1975 struct mwl8k_priv *priv = hw->priv;
1976 struct mwl8k_cmd_set_wmm *cmd;
1977 int rc;
1978
1979 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1980 if (cmd == NULL)
1981 return -ENOMEM;
1982
1983 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
1984 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02001985 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001986
1987 rc = mwl8k_post_cmd(hw, &cmd->header);
1988 kfree(cmd);
1989
1990 if (!rc)
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02001991 priv->wmm_enabled = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001992
1993 return rc;
1994}
1995
1996/*
1997 * CMD_SET_RTS_THRESHOLD.
1998 */
1999struct mwl8k_cmd_rts_threshold {
2000 struct mwl8k_cmd_pkt header;
2001 __le16 action;
2002 __le16 threshold;
2003} __attribute__((packed));
2004
2005static int mwl8k_rts_threshold(struct ieee80211_hw *hw,
Lennert Buytenhek733d3062009-07-17 07:24:15 +02002006 u16 action, u16 threshold)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002007{
2008 struct mwl8k_cmd_rts_threshold *cmd;
2009 int rc;
2010
2011 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2012 if (cmd == NULL)
2013 return -ENOMEM;
2014
2015 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2016 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2017 cmd->action = cpu_to_le16(action);
Lennert Buytenhek733d3062009-07-17 07:24:15 +02002018 cmd->threshold = cpu_to_le16(threshold);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002019
2020 rc = mwl8k_post_cmd(hw, &cmd->header);
2021 kfree(cmd);
2022
2023 return rc;
2024}
2025
2026/*
2027 * CMD_SET_EDCA_PARAMS.
2028 */
2029struct mwl8k_cmd_set_edca_params {
2030 struct mwl8k_cmd_pkt header;
2031
2032 /* See MWL8K_SET_EDCA_XXX below */
2033 __le16 action;
2034
2035 /* TX opportunity in units of 32 us */
2036 __le16 txop;
2037
2038 /* Log exponent of max contention period: 0...15*/
2039 __u8 log_cw_max;
2040
2041 /* Log exponent of min contention period: 0...15 */
2042 __u8 log_cw_min;
2043
2044 /* Adaptive interframe spacing in units of 32us */
2045 __u8 aifs;
2046
2047 /* TX queue to configure */
2048 __u8 txq;
2049} __attribute__((packed));
2050
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002051#define MWL8K_SET_EDCA_CW 0x01
2052#define MWL8K_SET_EDCA_TXOP 0x02
2053#define MWL8K_SET_EDCA_AIFS 0x04
2054
2055#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2056 MWL8K_SET_EDCA_TXOP | \
2057 MWL8K_SET_EDCA_AIFS)
2058
2059static int
2060mwl8k_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2061 __u16 cw_min, __u16 cw_max,
2062 __u8 aifs, __u16 txop)
2063{
2064 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002065 int rc;
2066
2067 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2068 if (cmd == NULL)
2069 return -ENOMEM;
2070
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002071 /*
2072 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2073 * this call.
2074 */
2075 qnum ^= !(qnum >> 1);
2076
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002077 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2078 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002079 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2080 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002081 cmd->log_cw_max = (u8)ilog2(cw_max + 1);
2082 cmd->log_cw_min = (u8)ilog2(cw_min + 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002083 cmd->aifs = aifs;
2084 cmd->txq = qnum;
2085
2086 rc = mwl8k_post_cmd(hw, &cmd->header);
2087 kfree(cmd);
2088
2089 return rc;
2090}
2091
2092/*
2093 * CMD_FINALIZE_JOIN.
2094 */
2095
2096/* FJ beacon buffer size is compiled into the firmware. */
2097#define MWL8K_FJ_BEACON_MAXLEN 128
2098
2099struct mwl8k_cmd_finalize_join {
2100 struct mwl8k_cmd_pkt header;
2101 __le32 sleep_interval; /* Number of beacon periods to sleep */
2102 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2103} __attribute__((packed));
2104
2105static int mwl8k_finalize_join(struct ieee80211_hw *hw, void *frame,
2106 __u16 framelen, __u16 dtim)
2107{
2108 struct mwl8k_cmd_finalize_join *cmd;
2109 struct ieee80211_mgmt *payload = frame;
2110 u16 hdrlen;
2111 u32 payload_len;
2112 int rc;
2113
2114 if (frame == NULL)
2115 return -EINVAL;
2116
2117 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2118 if (cmd == NULL)
2119 return -ENOMEM;
2120
2121 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2122 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002123 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002124
2125 hdrlen = ieee80211_hdrlen(payload->frame_control);
2126
2127 payload_len = framelen > hdrlen ? framelen - hdrlen : 0;
2128
2129 /* XXX TBD Might just have to abort and return an error */
2130 if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2131 printk(KERN_ERR "%s(): WARNING: Incomplete beacon "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002132 "sent to firmware. Sz=%u MAX=%u\n", __func__,
2133 payload_len, MWL8K_FJ_BEACON_MAXLEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002134
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002135 if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2136 payload_len = MWL8K_FJ_BEACON_MAXLEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002137
2138 if (payload && payload_len)
2139 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2140
2141 rc = mwl8k_post_cmd(hw, &cmd->header);
2142 kfree(cmd);
2143 return rc;
2144}
2145
2146/*
2147 * CMD_UPDATE_STADB.
2148 */
2149struct mwl8k_cmd_update_sta_db {
2150 struct mwl8k_cmd_pkt header;
2151
2152 /* See STADB_ACTION_TYPE */
2153 __le32 action;
2154
2155 /* Peer MAC address */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002156 __u8 peer_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002157
2158 __le32 reserved;
2159
2160 /* Peer info - valid during add/update. */
2161 struct peer_capability_info peer_info;
2162} __attribute__((packed));
2163
2164static int mwl8k_cmd_update_sta_db(struct ieee80211_hw *hw,
2165 struct ieee80211_vif *vif, __u32 action)
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_sta_db *cmd;
2170 struct peer_capability_info *peer_info;
2171 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002172 int rc;
2173 __u8 count, *rates;
2174
2175 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2176 if (cmd == NULL)
2177 return -ENOMEM;
2178
2179 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2180 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2181
2182 cmd->action = cpu_to_le32(action);
2183 peer_info = &cmd->peer_info;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002184 memcpy(cmd->peer_addr, mv_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002185
2186 switch (action) {
2187 case MWL8K_STA_DB_ADD_ENTRY:
2188 case MWL8K_STA_DB_MODIFY_ENTRY:
2189 /* Build peer_info block */
2190 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2191 peer_info->basic_caps = cpu_to_le16(info->assoc_capability);
2192 peer_info->interop = 1;
2193 peer_info->amsdu_enabled = 0;
2194
2195 rates = peer_info->legacy_rates;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002196 for (count = 0; count < mv_vif->legacy_nrates; count++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002197 rates[count] = bitrates[count].hw_value;
2198
2199 rc = mwl8k_post_cmd(hw, &cmd->header);
2200 if (rc == 0)
2201 mv_vif->peer_id = peer_info->station_id;
2202
2203 break;
2204
2205 case MWL8K_STA_DB_DEL_ENTRY:
2206 case MWL8K_STA_DB_FLUSH:
2207 default:
2208 rc = mwl8k_post_cmd(hw, &cmd->header);
2209 if (rc == 0)
2210 mv_vif->peer_id = 0;
2211 break;
2212 }
2213 kfree(cmd);
2214
2215 return rc;
2216}
2217
2218/*
2219 * CMD_SET_AID.
2220 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002221#define MWL8K_RATE_INDEX_MAX_ARRAY 14
2222
2223#define MWL8K_FRAME_PROT_DISABLED 0x00
2224#define MWL8K_FRAME_PROT_11G 0x07
2225#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2226#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002227
2228struct mwl8k_cmd_update_set_aid {
2229 struct mwl8k_cmd_pkt header;
2230 __le16 aid;
2231
2232 /* AP's MAC address (BSSID) */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002233 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002234 __le16 protection_mode;
2235 __u8 supp_rates[MWL8K_RATE_INDEX_MAX_ARRAY];
2236} __attribute__((packed));
2237
2238static int mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2239 struct ieee80211_vif *vif)
2240{
2241 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2242 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2243 struct mwl8k_cmd_update_set_aid *cmd;
2244 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
2245 int count;
2246 u16 prot_mode;
2247 int rc;
2248
2249 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2250 if (cmd == NULL)
2251 return -ENOMEM;
2252
2253 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2254 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2255 cmd->aid = cpu_to_le16(info->aid);
2256
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002257 memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002258
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002259 if (info->use_cts_prot) {
2260 prot_mode = MWL8K_FRAME_PROT_11G;
2261 } else {
Johannes Berg9ed6bcc2009-05-08 20:47:39 +02002262 switch (info->ht_operation_mode &
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002263 IEEE80211_HT_OP_MODE_PROTECTION) {
2264 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2265 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2266 break;
2267 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2268 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2269 break;
2270 default:
2271 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2272 break;
2273 }
2274 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002275 cmd->protection_mode = cpu_to_le16(prot_mode);
2276
2277 for (count = 0; count < mv_vif->legacy_nrates; count++)
2278 cmd->supp_rates[count] = bitrates[count].hw_value;
2279
2280 rc = mwl8k_post_cmd(hw, &cmd->header);
2281 kfree(cmd);
2282
2283 return rc;
2284}
2285
2286/*
2287 * CMD_SET_RATE.
2288 */
2289struct mwl8k_cmd_update_rateset {
2290 struct mwl8k_cmd_pkt header;
2291 __u8 legacy_rates[MWL8K_RATE_INDEX_MAX_ARRAY];
2292
2293 /* Bitmap for supported MCS codes. */
2294 __u8 mcs_set[MWL8K_IEEE_LEGACY_DATA_RATES];
2295 __u8 reserved[MWL8K_IEEE_LEGACY_DATA_RATES];
2296} __attribute__((packed));
2297
2298static int mwl8k_update_rateset(struct ieee80211_hw *hw,
2299 struct ieee80211_vif *vif)
2300{
2301 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2302 struct mwl8k_cmd_update_rateset *cmd;
2303 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
2304 int count;
2305 int rc;
2306
2307 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2308 if (cmd == NULL)
2309 return -ENOMEM;
2310
2311 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2312 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2313
2314 for (count = 0; count < mv_vif->legacy_nrates; count++)
2315 cmd->legacy_rates[count] = bitrates[count].hw_value;
2316
2317 rc = mwl8k_post_cmd(hw, &cmd->header);
2318 kfree(cmd);
2319
2320 return rc;
2321}
2322
2323/*
2324 * CMD_USE_FIXED_RATE.
2325 */
2326#define MWL8K_RATE_TABLE_SIZE 8
2327#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002328#define MWL8K_USE_AUTO_RATE 0x0002
2329
2330struct mwl8k_rate_entry {
2331 /* Set to 1 if HT rate, 0 if legacy. */
2332 __le32 is_ht_rate;
2333
2334 /* Set to 1 to use retry_count field. */
2335 __le32 enable_retry;
2336
2337 /* Specified legacy rate or MCS. */
2338 __le32 rate;
2339
2340 /* Number of allowed retries. */
2341 __le32 retry_count;
2342} __attribute__((packed));
2343
2344struct mwl8k_rate_table {
2345 /* 1 to allow specified rate and below */
2346 __le32 allow_rate_drop;
2347 __le32 num_rates;
2348 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2349} __attribute__((packed));
2350
2351struct mwl8k_cmd_use_fixed_rate {
2352 struct mwl8k_cmd_pkt header;
2353 __le32 action;
2354 struct mwl8k_rate_table rate_table;
2355
2356 /* Unicast, Broadcast or Multicast */
2357 __le32 rate_type;
2358 __le32 reserved1;
2359 __le32 reserved2;
2360} __attribute__((packed));
2361
2362static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2363 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2364{
2365 struct mwl8k_cmd_use_fixed_rate *cmd;
2366 int count;
2367 int rc;
2368
2369 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2370 if (cmd == NULL)
2371 return -ENOMEM;
2372
2373 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2374 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2375
2376 cmd->action = cpu_to_le32(action);
2377 cmd->rate_type = cpu_to_le32(rate_type);
2378
2379 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002380 /*
2381 * Copy over each field manually so that endian
2382 * conversion can be done.
2383 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002384 cmd->rate_table.allow_rate_drop =
2385 cpu_to_le32(rate_table->allow_rate_drop);
2386 cmd->rate_table.num_rates =
2387 cpu_to_le32(rate_table->num_rates);
2388
2389 for (count = 0; count < rate_table->num_rates; count++) {
2390 struct mwl8k_rate_entry *dst =
2391 &cmd->rate_table.rate_entry[count];
2392 struct mwl8k_rate_entry *src =
2393 &rate_table->rate_entry[count];
2394
2395 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2396 dst->enable_retry = cpu_to_le32(src->enable_retry);
2397 dst->rate = cpu_to_le32(src->rate);
2398 dst->retry_count = cpu_to_le32(src->retry_count);
2399 }
2400 }
2401
2402 rc = mwl8k_post_cmd(hw, &cmd->header);
2403 kfree(cmd);
2404
2405 return rc;
2406}
2407
2408
2409/*
2410 * Interrupt handling.
2411 */
2412static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2413{
2414 struct ieee80211_hw *hw = dev_id;
2415 struct mwl8k_priv *priv = hw->priv;
2416 u32 status;
2417
2418 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2419 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2420
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002421 if (!status)
2422 return IRQ_NONE;
2423
2424 if (status & MWL8K_A2H_INT_TX_DONE)
2425 tasklet_schedule(&priv->tx_reclaim_task);
2426
2427 if (status & MWL8K_A2H_INT_RX_READY) {
2428 while (rxq_process(hw, 0, 1))
2429 rxq_refill(hw, 0, 1);
2430 }
2431
2432 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002433 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002434 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002435 }
2436
2437 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002438 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002439 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002440 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002441 }
2442
2443 return IRQ_HANDLED;
2444}
2445
2446
2447/*
2448 * Core driver operations.
2449 */
2450static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2451{
2452 struct mwl8k_priv *priv = hw->priv;
2453 int index = skb_get_queue_mapping(skb);
2454 int rc;
2455
2456 if (priv->current_channel == NULL) {
2457 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002458 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002459 dev_kfree_skb(skb);
2460 return NETDEV_TX_OK;
2461 }
2462
2463 rc = mwl8k_txq_xmit(hw, index, skb);
2464
2465 return rc;
2466}
2467
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002468static int mwl8k_start(struct ieee80211_hw *hw)
2469{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002470 struct mwl8k_priv *priv = hw->priv;
2471 int rc;
2472
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002473 rc = request_irq(priv->pdev->irq, &mwl8k_interrupt,
2474 IRQF_SHARED, MWL8K_NAME, hw);
2475 if (rc) {
2476 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002477 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002478 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002479 }
2480
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002481 /* Enable tx reclaim tasklet */
2482 tasklet_enable(&priv->tx_reclaim_task);
2483
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002484 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002485 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002486
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002487 rc = mwl8k_fw_lock(hw);
2488 if (!rc) {
2489 rc = mwl8k_cmd_802_11_radio_enable(hw);
2490
2491 if (!rc)
2492 rc = mwl8k_cmd_set_pre_scan(hw);
2493
2494 if (!rc)
2495 rc = mwl8k_cmd_set_post_scan(hw,
2496 "\x00\x00\x00\x00\x00\x00");
2497
2498 if (!rc)
2499 rc = mwl8k_cmd_setrateadaptmode(hw, 0);
2500
2501 if (!rc)
2502 rc = mwl8k_set_wmm(hw, 0);
2503
2504 if (!rc)
2505 rc = mwl8k_enable_sniffer(hw, 0);
2506
2507 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002508 }
2509
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002510 if (rc) {
2511 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2512 free_irq(priv->pdev->irq, hw);
2513 tasklet_disable(&priv->tx_reclaim_task);
2514 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002515
2516 return rc;
2517}
2518
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002519static void mwl8k_stop(struct ieee80211_hw *hw)
2520{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002521 struct mwl8k_priv *priv = hw->priv;
2522 int i;
2523
Lennert Buytenhekd3cea0b2009-07-17 07:15:49 +02002524 mwl8k_cmd_802_11_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002525
2526 ieee80211_stop_queues(hw);
2527
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002528 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002529 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002530 free_irq(priv->pdev->irq, hw);
2531
2532 /* Stop finalize join worker */
2533 cancel_work_sync(&priv->finalize_join_worker);
2534 if (priv->beacon_skb != NULL)
2535 dev_kfree_skb(priv->beacon_skb);
2536
2537 /* Stop tx reclaim tasklet */
2538 tasklet_disable(&priv->tx_reclaim_task);
2539
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002540 /* Return all skbs to mac80211 */
2541 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2542 mwl8k_txq_reclaim(hw, i, 1);
2543}
2544
2545static int mwl8k_add_interface(struct ieee80211_hw *hw,
2546 struct ieee80211_if_init_conf *conf)
2547{
2548 struct mwl8k_priv *priv = hw->priv;
2549 struct mwl8k_vif *mwl8k_vif;
2550
2551 /*
2552 * We only support one active interface at a time.
2553 */
2554 if (priv->vif != NULL)
2555 return -EBUSY;
2556
2557 /*
2558 * We only support managed interfaces for now.
2559 */
Lennert Buytenhek240e86e2009-07-16 14:15:44 +02002560 if (conf->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002561 return -EINVAL;
2562
2563 /* Clean out driver private area */
2564 mwl8k_vif = MWL8K_VIF(conf->vif);
2565 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2566
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002567 /* Set and save the mac address */
2568 mwl8k_set_mac_addr(hw, conf->mac_addr);
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002569 memcpy(mwl8k_vif->mac_addr, conf->mac_addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002570
2571 /* Back pointer to parent config block */
2572 mwl8k_vif->priv = priv;
2573
2574 /* Setup initial PHY parameters */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002575 memcpy(mwl8k_vif->legacy_rates,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002576 priv->rates, sizeof(mwl8k_vif->legacy_rates));
2577 mwl8k_vif->legacy_nrates = ARRAY_SIZE(priv->rates);
2578
2579 /* Set Initial sequence number to zero */
2580 mwl8k_vif->seqno = 0;
2581
2582 priv->vif = conf->vif;
2583 priv->current_channel = NULL;
2584
2585 return 0;
2586}
2587
2588static void mwl8k_remove_interface(struct ieee80211_hw *hw,
2589 struct ieee80211_if_init_conf *conf)
2590{
2591 struct mwl8k_priv *priv = hw->priv;
2592
2593 if (priv->vif == NULL)
2594 return;
2595
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002596 mwl8k_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
2597
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002598 priv->vif = NULL;
2599}
2600
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002601static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002602{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002603 struct ieee80211_conf *conf = &hw->conf;
2604 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002605 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002606
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002607 if (conf->flags & IEEE80211_CONF_IDLE) {
2608 mwl8k_cmd_802_11_radio_disable(hw);
2609 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002610 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002611 }
2612
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002613 rc = mwl8k_fw_lock(hw);
2614 if (rc)
2615 return rc;
2616
2617 rc = mwl8k_cmd_802_11_radio_enable(hw);
2618 if (rc)
2619 goto out;
2620
2621 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2622 if (rc)
2623 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002624
2625 priv->current_channel = conf->channel;
2626
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002627 if (conf->power_level > 18)
2628 conf->power_level = 18;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002629 rc = mwl8k_cmd_802_11_rf_tx_power(hw, conf->power_level);
2630 if (rc)
2631 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002632
2633 if (mwl8k_cmd_mimo_config(hw, 0x7, 0x7))
2634 rc = -EINVAL;
2635
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002636out:
2637 mwl8k_fw_unlock(hw);
2638
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002639 return rc;
2640}
2641
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002642static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2643 struct ieee80211_vif *vif,
2644 struct ieee80211_bss_conf *info,
2645 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002646{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002647 struct mwl8k_priv *priv = hw->priv;
2648 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002649 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002650
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002651 if (changed & BSS_CHANGED_BSSID)
2652 memcpy(mwl8k_vif->bssid, info->bssid, ETH_ALEN);
2653
2654 if ((changed & BSS_CHANGED_ASSOC) == 0)
2655 return;
2656
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002657 priv->capture_beacon = false;
2658
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002659 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002660 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002661 return;
2662
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002663 if (info->assoc) {
2664 memcpy(&mwl8k_vif->bss_info, info,
2665 sizeof(struct ieee80211_bss_conf));
2666
2667 /* Install rates */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002668 rc = mwl8k_update_rateset(hw, vif);
2669 if (rc)
2670 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002671
2672 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002673 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
2674 MWL8K_UCAST_RATE, NULL);
2675 if (rc)
2676 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002677
2678 /* Set radio preamble */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002679 rc = mwl8k_set_radio_preamble(hw, info->use_short_preamble);
2680 if (rc)
2681 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002682
2683 /* Set slot time */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002684 rc = mwl8k_cmd_set_slot(hw, info->use_short_slot);
2685 if (rc)
2686 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002687
2688 /* Update peer rate info */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002689 rc = mwl8k_cmd_update_sta_db(hw, vif,
2690 MWL8K_STA_DB_MODIFY_ENTRY);
2691 if (rc)
2692 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002693
2694 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002695 rc = mwl8k_cmd_set_aid(hw, vif);
2696 if (rc)
2697 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002698
2699 /*
2700 * Finalize the join. Tell rx handler to process
2701 * next beacon from our BSSID.
2702 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002703 memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002704 priv->capture_beacon = true;
2705 } else {
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002706 rc = mwl8k_cmd_update_sta_db(hw, vif, MWL8K_STA_DB_DEL_ENTRY);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002707 memset(&mwl8k_vif->bss_info, 0,
2708 sizeof(struct ieee80211_bss_conf));
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002709 memset(mwl8k_vif->bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002710 }
2711
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002712out:
2713 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002714}
2715
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002716static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
2717 int mc_count, struct dev_addr_list *mclist)
2718{
2719 struct mwl8k_cmd_pkt *cmd;
2720
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002721 /*
2722 * Synthesize and return a command packet that programs the
2723 * hardware multicast address filter. At this point we don't
2724 * know whether FIF_ALLMULTI is being requested, but if it is,
2725 * we'll end up throwing this packet away and creating a new
2726 * one in mwl8k_configure_filter().
2727 */
2728 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002729
2730 return (unsigned long)cmd;
2731}
2732
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002733static void mwl8k_configure_filter(struct ieee80211_hw *hw,
2734 unsigned int changed_flags,
2735 unsigned int *total_flags,
2736 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002737{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002738 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002739 struct mwl8k_cmd_pkt *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002740
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002741 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002742 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002743
2744 if (mwl8k_fw_lock(hw))
2745 return;
2746
2747 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02002748 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
2749 /*
2750 * Disable the BSS filter.
2751 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002752 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02002753 } else {
Lennert Buytenheka94cc972009-08-03 21:58:57 +02002754 u8 *bssid;
2755
Lennert Buytenhek77165d82009-10-22 20:19:53 +02002756 /*
2757 * Enable the BSS filter.
2758 *
2759 * If there is an active STA interface, use that
2760 * interface's BSSID, otherwise use a dummy one
2761 * (where the OUI part needs to be nonzero for
2762 * the BSSID to be accepted by POST_SCAN).
2763 */
2764 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02002765 if (priv->vif != NULL)
2766 bssid = MWL8K_VIF(priv->vif)->bssid;
2767
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002768 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002769 }
2770 }
2771
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002772 cmd = (void *)(unsigned long)multicast;
2773
2774 /*
2775 * If FIF_ALLMULTI is being requested, throw away the command
2776 * packet that ->prepare_multicast() built and replace it with
2777 * a command packet that enables reception of all multicast
2778 * packets.
2779 */
2780 if (*total_flags & FIF_ALLMULTI) {
2781 kfree(cmd);
2782 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
2783 }
2784
2785 if (cmd != NULL) {
2786 mwl8k_post_cmd(hw, cmd);
2787 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002788 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002789
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002790 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002791}
2792
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002793static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2794{
Lennert Buytenhek733d3062009-07-17 07:24:15 +02002795 return mwl8k_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002796}
2797
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002798static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
2799 const struct ieee80211_tx_queue_params *params)
2800{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02002801 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002802 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002803
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02002804 rc = mwl8k_fw_lock(hw);
2805 if (!rc) {
2806 if (!priv->wmm_enabled)
2807 rc = mwl8k_set_wmm(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002808
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02002809 if (!rc)
2810 rc = mwl8k_set_edca_params(hw, queue,
2811 params->cw_min,
2812 params->cw_max,
2813 params->aifs,
2814 params->txop);
2815
2816 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002817 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02002818
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002819 return rc;
2820}
2821
2822static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
2823 struct ieee80211_tx_queue_stats *stats)
2824{
2825 struct mwl8k_priv *priv = hw->priv;
2826 struct mwl8k_tx_queue *txq;
2827 int index;
2828
2829 spin_lock_bh(&priv->tx_lock);
2830 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
2831 txq = priv->txq + index;
2832 memcpy(&stats[index], &txq->tx_stats,
2833 sizeof(struct ieee80211_tx_queue_stats));
2834 }
2835 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02002836
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002837 return 0;
2838}
2839
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002840static int mwl8k_get_stats(struct ieee80211_hw *hw,
2841 struct ieee80211_low_level_stats *stats)
2842{
Lennert Buytenhek954ef502009-07-17 07:26:27 +02002843 return mwl8k_cmd_802_11_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002844}
2845
2846static const struct ieee80211_ops mwl8k_ops = {
2847 .tx = mwl8k_tx,
2848 .start = mwl8k_start,
2849 .stop = mwl8k_stop,
2850 .add_interface = mwl8k_add_interface,
2851 .remove_interface = mwl8k_remove_interface,
2852 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002853 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02002854 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002855 .configure_filter = mwl8k_configure_filter,
2856 .set_rts_threshold = mwl8k_set_rts_threshold,
2857 .conf_tx = mwl8k_conf_tx,
2858 .get_tx_stats = mwl8k_get_tx_stats,
2859 .get_stats = mwl8k_get_stats,
2860};
2861
2862static void mwl8k_tx_reclaim_handler(unsigned long data)
2863{
2864 int i;
2865 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
2866 struct mwl8k_priv *priv = hw->priv;
2867
2868 spin_lock_bh(&priv->tx_lock);
2869 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2870 mwl8k_txq_reclaim(hw, i, 0);
2871
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002872 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002873 complete(priv->tx_wait);
2874 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002875 }
2876 spin_unlock_bh(&priv->tx_lock);
2877}
2878
2879static void mwl8k_finalize_join_worker(struct work_struct *work)
2880{
2881 struct mwl8k_priv *priv =
2882 container_of(work, struct mwl8k_priv, finalize_join_worker);
2883 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002884 u8 dtim = MWL8K_VIF(priv->vif)->bss_info.dtim_period;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002885
2886 mwl8k_finalize_join(priv->hw, skb->data, skb->len, dtim);
2887 dev_kfree_skb(skb);
2888
2889 priv->beacon_skb = NULL;
2890}
2891
2892static int __devinit mwl8k_probe(struct pci_dev *pdev,
2893 const struct pci_device_id *id)
2894{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02002895 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002896 struct ieee80211_hw *hw;
2897 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002898 int rc;
2899 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02002900
2901 if (!printed_version) {
2902 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
2903 printed_version = 1;
2904 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002905
2906 rc = pci_enable_device(pdev);
2907 if (rc) {
2908 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
2909 MWL8K_NAME);
2910 return rc;
2911 }
2912
2913 rc = pci_request_regions(pdev, MWL8K_NAME);
2914 if (rc) {
2915 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
2916 MWL8K_NAME);
2917 return rc;
2918 }
2919
2920 pci_set_master(pdev);
2921
2922 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
2923 if (hw == NULL) {
2924 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
2925 rc = -ENOMEM;
2926 goto err_free_reg;
2927 }
2928
2929 priv = hw->priv;
2930 priv->hw = hw;
2931 priv->pdev = pdev;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02002932 priv->wmm_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002933 priv->pending_tx_pkts = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002934
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002935 SET_IEEE80211_DEV(hw, &pdev->dev);
2936 pci_set_drvdata(pdev, hw);
2937
2938 priv->regs = pci_iomap(pdev, 1, 0x10000);
2939 if (priv->regs == NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002940 printk(KERN_ERR "%s: Cannot map device memory\n",
2941 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002942 goto err_iounmap;
2943 }
2944
2945 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
2946 priv->band.band = IEEE80211_BAND_2GHZ;
2947 priv->band.channels = priv->channels;
2948 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
2949 priv->band.bitrates = priv->rates;
2950 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
2951 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
2952
2953 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
2954 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
2955
2956 /*
2957 * Extra headroom is the size of the required DMA header
2958 * minus the size of the smallest 802.11 frame (CTS frame).
2959 */
2960 hw->extra_tx_headroom =
2961 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
2962
2963 hw->channel_change_time = 10;
2964
2965 hw->queues = MWL8K_TX_QUEUES;
2966
Lennert Buytenhek240e86e2009-07-16 14:15:44 +02002967 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002968
2969 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002970 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002971 hw->vif_data_size = sizeof(struct mwl8k_vif);
2972 priv->vif = NULL;
2973
2974 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002975 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002976 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002977
2978 /* Finalize join worker */
2979 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
2980
2981 /* TX reclaim tasklet */
2982 tasklet_init(&priv->tx_reclaim_task,
2983 mwl8k_tx_reclaim_handler, (unsigned long)hw);
2984 tasklet_disable(&priv->tx_reclaim_task);
2985
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002986 /* Power management cookie */
2987 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
2988 if (priv->cookie == NULL)
2989 goto err_iounmap;
2990
2991 rc = mwl8k_rxq_init(hw, 0);
2992 if (rc)
2993 goto err_iounmap;
2994 rxq_refill(hw, 0, INT_MAX);
2995
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002996 mutex_init(&priv->fw_mutex);
2997 priv->fw_mutex_owner = NULL;
2998 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002999 priv->hostcmd_wait = NULL;
3000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003001 spin_lock_init(&priv->tx_lock);
3002
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003003 priv->tx_wait = NULL;
3004
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003005 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3006 rc = mwl8k_txq_init(hw, i);
3007 if (rc)
3008 goto err_free_queues;
3009 }
3010
3011 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003012 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003013 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3014 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3015
3016 rc = request_irq(priv->pdev->irq, &mwl8k_interrupt,
3017 IRQF_SHARED, MWL8K_NAME, hw);
3018 if (rc) {
3019 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003020 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003021 goto err_free_queues;
3022 }
3023
3024 /* Reset firmware and hardware */
3025 mwl8k_hw_reset(priv);
3026
3027 /* Ask userland hotplug daemon for the device firmware */
3028 rc = mwl8k_request_firmware(priv, (u32)id->driver_data);
3029 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003030 printk(KERN_ERR "%s: Firmware files not found\n",
3031 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003032 goto err_free_irq;
3033 }
3034
3035 /* Load firmware into hardware */
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003036 rc = mwl8k_load_firmware(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003037 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003038 printk(KERN_ERR "%s: Cannot start firmware\n",
3039 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003040 goto err_stop_firmware;
3041 }
3042
3043 /* Reclaim memory once firmware is successfully loaded */
3044 mwl8k_release_firmware(priv);
3045
3046 /*
3047 * Temporarily enable interrupts. Initial firmware host
3048 * commands use interrupts and avoids polling. Disable
3049 * interrupts when done.
3050 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003051 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003052
3053 /* Get config data, mac addrs etc */
3054 rc = mwl8k_cmd_get_hw_spec(hw);
3055 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003056 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3057 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003058 goto err_stop_firmware;
3059 }
3060
3061 /* Turn radio off */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003062 rc = mwl8k_cmd_802_11_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003063 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003064 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003065 goto err_stop_firmware;
3066 }
3067
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003068 /* Clear MAC address */
3069 rc = mwl8k_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
3070 if (rc) {
3071 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3072 wiphy_name(hw->wiphy));
3073 goto err_stop_firmware;
3074 }
3075
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003076 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003077 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003078 free_irq(priv->pdev->irq, hw);
3079
3080 rc = ieee80211_register_hw(hw);
3081 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003082 printk(KERN_ERR "%s: Cannot register device\n",
3083 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003084 goto err_stop_firmware;
3085 }
3086
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003087 printk(KERN_INFO "%s: 88w%u v%d, %pM, firmware version %u.%u.%u.%u\n",
3088 wiphy_name(hw->wiphy), priv->part_num, priv->hw_rev,
3089 hw->wiphy->perm_addr,
3090 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3091 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003092
3093 return 0;
3094
3095err_stop_firmware:
3096 mwl8k_hw_reset(priv);
3097 mwl8k_release_firmware(priv);
3098
3099err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003100 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003101 free_irq(priv->pdev->irq, hw);
3102
3103err_free_queues:
3104 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3105 mwl8k_txq_deinit(hw, i);
3106 mwl8k_rxq_deinit(hw, 0);
3107
3108err_iounmap:
3109 if (priv->cookie != NULL)
3110 pci_free_consistent(priv->pdev, 4,
3111 priv->cookie, priv->cookie_dma);
3112
3113 if (priv->regs != NULL)
3114 pci_iounmap(pdev, priv->regs);
3115
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003116 pci_set_drvdata(pdev, NULL);
3117 ieee80211_free_hw(hw);
3118
3119err_free_reg:
3120 pci_release_regions(pdev);
3121 pci_disable_device(pdev);
3122
3123 return rc;
3124}
3125
Joerg Albert230f7af2009-04-18 02:10:45 +02003126static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003127{
3128 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3129}
3130
Joerg Albert230f7af2009-04-18 02:10:45 +02003131static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003132{
3133 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3134 struct mwl8k_priv *priv;
3135 int i;
3136
3137 if (hw == NULL)
3138 return;
3139 priv = hw->priv;
3140
3141 ieee80211_stop_queues(hw);
3142
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003143 ieee80211_unregister_hw(hw);
3144
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003145 /* Remove tx reclaim tasklet */
3146 tasklet_kill(&priv->tx_reclaim_task);
3147
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003148 /* Stop hardware */
3149 mwl8k_hw_reset(priv);
3150
3151 /* Return all skbs to mac80211 */
3152 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3153 mwl8k_txq_reclaim(hw, i, 1);
3154
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003155 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3156 mwl8k_txq_deinit(hw, i);
3157
3158 mwl8k_rxq_deinit(hw, 0);
3159
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003160 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003161
3162 pci_iounmap(pdev, priv->regs);
3163 pci_set_drvdata(pdev, NULL);
3164 ieee80211_free_hw(hw);
3165 pci_release_regions(pdev);
3166 pci_disable_device(pdev);
3167}
3168
3169static struct pci_driver mwl8k_driver = {
3170 .name = MWL8K_NAME,
3171 .id_table = mwl8k_table,
3172 .probe = mwl8k_probe,
3173 .remove = __devexit_p(mwl8k_remove),
3174 .shutdown = __devexit_p(mwl8k_shutdown),
3175};
3176
3177static int __init mwl8k_init(void)
3178{
3179 return pci_register_driver(&mwl8k_driver);
3180}
3181
3182static void __exit mwl8k_exit(void)
3183{
3184 pci_unregister_driver(&mwl8k_driver);
3185}
3186
3187module_init(mwl8k_init);
3188module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003189
3190MODULE_DESCRIPTION(MWL8K_DESC);
3191MODULE_VERSION(MWL8K_VERSION);
3192MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3193MODULE_LICENSE("GPL");