blob: 76c9263f70ace44d4a4c1fae88d322b8cc67702c [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];
172 struct ieee80211_rate rates[12];
173
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 */
211 struct ieee80211_rate legacy_rates[12];
212
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, },
243 { .bitrate = 60, .hw_value = 12, },
244 { .bitrate = 90, .hw_value = 18, },
245 { .bitrate = 110, .hw_value = 22, },
246 { .bitrate = 120, .hw_value = 24, },
247 { .bitrate = 180, .hw_value = 36, },
248 { .bitrate = 240, .hw_value = 48, },
249 { .bitrate = 360, .hw_value = 72, },
250 { .bitrate = 480, .hw_value = 96, },
251 { .bitrate = 540, .hw_value = 108, },
252};
253
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100254/* Set or get info from Firmware */
255#define MWL8K_CMD_SET 0x0001
256#define MWL8K_CMD_GET 0x0000
257
258/* Firmware command codes */
259#define MWL8K_CMD_CODE_DNLD 0x0001
260#define MWL8K_CMD_GET_HW_SPEC 0x0003
261#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
262#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200263#define MWL8K_CMD_RADIO_CONTROL 0x001c
264#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100265#define MWL8K_CMD_SET_PRE_SCAN 0x0107
266#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200267#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100268#define MWL8K_CMD_SET_AID 0x010d
269#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200270#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100271#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200272#define MWL8K_CMD_SET_SLOT 0x0114
273#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
274#define MWL8K_CMD_SET_WMM_MODE 0x0123
275#define MWL8K_CMD_MIMO_CONFIG 0x0125
276#define MWL8K_CMD_USE_FIXED_RATE 0x0126
277#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200278#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200279#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
280#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100281
282static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
283{
284#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
285 snprintf(buf, bufsize, "%s", #x);\
286 return buf;\
287 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200288 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100289 MWL8K_CMDNAME(CODE_DNLD);
290 MWL8K_CMDNAME(GET_HW_SPEC);
291 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
292 MWL8K_CMDNAME(GET_STAT);
293 MWL8K_CMDNAME(RADIO_CONTROL);
294 MWL8K_CMDNAME(RF_TX_POWER);
295 MWL8K_CMDNAME(SET_PRE_SCAN);
296 MWL8K_CMDNAME(SET_POST_SCAN);
297 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100298 MWL8K_CMDNAME(SET_AID);
299 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200300 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200302 MWL8K_CMDNAME(SET_SLOT);
303 MWL8K_CMDNAME(SET_EDCA_PARAMS);
304 MWL8K_CMDNAME(SET_WMM_MODE);
305 MWL8K_CMDNAME(MIMO_CONFIG);
306 MWL8K_CMDNAME(USE_FIXED_RATE);
307 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200308 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200309 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
310 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100311 default:
312 snprintf(buf, bufsize, "0x%x", cmd);
313 }
314#undef MWL8K_CMDNAME
315
316 return buf;
317}
318
319/* Hardware and firmware reset */
320static void mwl8k_hw_reset(struct mwl8k_priv *priv)
321{
322 iowrite32(MWL8K_H2A_INT_RESET,
323 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
324 iowrite32(MWL8K_H2A_INT_RESET,
325 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
326 msleep(20);
327}
328
329/* Release fw image */
330static void mwl8k_release_fw(struct firmware **fw)
331{
332 if (*fw == NULL)
333 return;
334 release_firmware(*fw);
335 *fw = NULL;
336}
337
338static void mwl8k_release_firmware(struct mwl8k_priv *priv)
339{
340 mwl8k_release_fw(&priv->fw.ucode);
341 mwl8k_release_fw(&priv->fw.helper);
342}
343
344/* Request fw image */
345static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200346 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100347{
348 /* release current image */
349 if (*fw != NULL)
350 mwl8k_release_fw(fw);
351
352 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200353 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100354}
355
356static int mwl8k_request_firmware(struct mwl8k_priv *priv, u32 part_num)
357{
358 u8 filename[64];
359 int rc;
360
361 priv->part_num = part_num;
362
363 snprintf(filename, sizeof(filename),
364 "mwl8k/helper_%u.fw", priv->part_num);
365
366 rc = mwl8k_request_fw(priv, filename, &priv->fw.helper);
367 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200368 printk(KERN_ERR "%s: Error requesting helper firmware "
369 "file %s\n", pci_name(priv->pdev), filename);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100370 return rc;
371 }
372
373 snprintf(filename, sizeof(filename),
374 "mwl8k/fmimage_%u.fw", priv->part_num);
375
376 rc = mwl8k_request_fw(priv, filename, &priv->fw.ucode);
377 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200378 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
379 pci_name(priv->pdev), filename);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100380 mwl8k_release_fw(&priv->fw.helper);
381 return rc;
382 }
383
384 return 0;
385}
386
387struct mwl8k_cmd_pkt {
388 __le16 code;
389 __le16 length;
390 __le16 seq_num;
391 __le16 result;
392 char payload[0];
393} __attribute__((packed));
394
395/*
396 * Firmware loading.
397 */
398static int
399mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
400{
401 void __iomem *regs = priv->regs;
402 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100403 int loops;
404
405 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
406 if (pci_dma_mapping_error(priv->pdev, dma_addr))
407 return -ENOMEM;
408
409 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
410 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
411 iowrite32(MWL8K_H2A_INT_DOORBELL,
412 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
413 iowrite32(MWL8K_H2A_INT_DUMMY,
414 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
415
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100416 loops = 1000;
417 do {
418 u32 int_code;
419
420 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
421 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
422 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100423 break;
424 }
425
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200426 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100427 udelay(1);
428 } while (--loops);
429
430 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
431
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200432 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100433}
434
435static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
436 const u8 *data, size_t length)
437{
438 struct mwl8k_cmd_pkt *cmd;
439 int done;
440 int rc = 0;
441
442 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
443 if (cmd == NULL)
444 return -ENOMEM;
445
446 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
447 cmd->seq_num = 0;
448 cmd->result = 0;
449
450 done = 0;
451 while (length) {
452 int block_size = length > 256 ? 256 : length;
453
454 memcpy(cmd->payload, data + done, block_size);
455 cmd->length = cpu_to_le16(block_size);
456
457 rc = mwl8k_send_fw_load_cmd(priv, cmd,
458 sizeof(*cmd) + block_size);
459 if (rc)
460 break;
461
462 done += block_size;
463 length -= block_size;
464 }
465
466 if (!rc) {
467 cmd->length = 0;
468 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
469 }
470
471 kfree(cmd);
472
473 return rc;
474}
475
476static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
477 const u8 *data, size_t length)
478{
479 unsigned char *buffer;
480 int may_continue, rc = 0;
481 u32 done, prev_block_size;
482
483 buffer = kmalloc(1024, GFP_KERNEL);
484 if (buffer == NULL)
485 return -ENOMEM;
486
487 done = 0;
488 prev_block_size = 0;
489 may_continue = 1000;
490 while (may_continue > 0) {
491 u32 block_size;
492
493 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
494 if (block_size & 1) {
495 block_size &= ~1;
496 may_continue--;
497 } else {
498 done += prev_block_size;
499 length -= prev_block_size;
500 }
501
502 if (block_size > 1024 || block_size > length) {
503 rc = -EOVERFLOW;
504 break;
505 }
506
507 if (length == 0) {
508 rc = 0;
509 break;
510 }
511
512 if (block_size == 0) {
513 rc = -EPROTO;
514 may_continue--;
515 udelay(1);
516 continue;
517 }
518
519 prev_block_size = block_size;
520 memcpy(buffer, data + done, block_size);
521
522 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
523 if (rc)
524 break;
525 }
526
527 if (!rc && length != 0)
528 rc = -EREMOTEIO;
529
530 kfree(buffer);
531
532 return rc;
533}
534
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200535static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100536{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200537 struct mwl8k_priv *priv = hw->priv;
538 struct firmware *fw = priv->fw.ucode;
539 int rc;
540 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100541
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200542 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
543 struct firmware *helper = priv->fw.helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100544
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200545 if (helper == NULL) {
546 printk(KERN_ERR "%s: helper image needed but none "
547 "given\n", pci_name(priv->pdev));
548 return -EINVAL;
549 }
550
551 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100552 if (rc) {
553 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200554 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100555 return rc;
556 }
557 msleep(1);
558
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200559 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100560 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200561 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100562 }
563
564 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200565 printk(KERN_ERR "%s: unable to load firmware image\n",
566 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100567 return rc;
568 }
569
570 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
571 msleep(1);
572
573 loops = 200000;
574 do {
575 if (ioread32(priv->regs + MWL8K_HIU_INT_CODE)
576 == MWL8K_FWSTA_READY)
577 break;
578 udelay(1);
579 } while (--loops);
580
581 return loops ? 0 : -ETIMEDOUT;
582}
583
584
585/*
586 * Defines shared between transmission and reception.
587 */
588/* HT control fields for firmware */
589struct ewc_ht_info {
590 __le16 control1;
591 __le16 control2;
592 __le16 control3;
593} __attribute__((packed));
594
595/* Firmware Station database operations */
596#define MWL8K_STA_DB_ADD_ENTRY 0
597#define MWL8K_STA_DB_MODIFY_ENTRY 1
598#define MWL8K_STA_DB_DEL_ENTRY 2
599#define MWL8K_STA_DB_FLUSH 3
600
601/* Peer Entry flags - used to define the type of the peer node */
602#define MWL8K_PEER_TYPE_ACCESSPOINT 2
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100603
604#define MWL8K_IEEE_LEGACY_DATA_RATES 12
605#define MWL8K_MCS_BITMAP_SIZE 16
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100606
607struct peer_capability_info {
608 /* Peer type - AP vs. STA. */
609 __u8 peer_type;
610
611 /* Basic 802.11 capabilities from assoc resp. */
612 __le16 basic_caps;
613
614 /* Set if peer supports 802.11n high throughput (HT). */
615 __u8 ht_support;
616
617 /* Valid if HT is supported. */
618 __le16 ht_caps;
619 __u8 extended_ht_caps;
620 struct ewc_ht_info ewc_info;
621
622 /* Legacy rate table. Intersection of our rates and peer rates. */
623 __u8 legacy_rates[MWL8K_IEEE_LEGACY_DATA_RATES];
624
625 /* HT rate table. Intersection of our rates and peer rates. */
626 __u8 ht_rates[MWL8K_MCS_BITMAP_SIZE];
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +0200627 __u8 pad[16];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100628
629 /* If set, interoperability mode, no proprietary extensions. */
630 __u8 interop;
631 __u8 pad2;
632 __u8 station_id;
633 __le16 amsdu_enabled;
634} __attribute__((packed));
635
636/* Inline functions to manipulate QoS field in data descriptor. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100637static inline u16 mwl8k_qos_setbit_eosp(u16 qos)
638{
639 u16 val_mask = 1 << 4;
640
641 /* End of Service Period Bit 4 */
642 return qos | val_mask;
643}
644
645static inline u16 mwl8k_qos_setbit_ack(u16 qos, u8 ack_policy)
646{
647 u16 val_mask = 0x3;
648 u8 shift = 5;
649 u16 qos_mask = ~(val_mask << shift);
650
651 /* Ack Policy Bit 5-6 */
652 return (qos & qos_mask) | ((ack_policy & val_mask) << shift);
653}
654
655static inline u16 mwl8k_qos_setbit_amsdu(u16 qos)
656{
657 u16 val_mask = 1 << 7;
658
659 /* AMSDU present Bit 7 */
660 return qos | val_mask;
661}
662
663static inline u16 mwl8k_qos_setbit_qlen(u16 qos, u8 len)
664{
665 u16 val_mask = 0xff;
666 u8 shift = 8;
667 u16 qos_mask = ~(val_mask << shift);
668
669 /* Queue Length Bits 8-15 */
670 return (qos & qos_mask) | ((len & val_mask) << shift);
671}
672
673/* DMA header used by firmware and hardware. */
674struct mwl8k_dma_data {
675 __le16 fwlen;
676 struct ieee80211_hdr wh;
677} __attribute__((packed));
678
679/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200680static inline void mwl8k_remove_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100681{
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200682 struct mwl8k_dma_data *tr = (struct mwl8k_dma_data *)skb->data;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100683 void *dst, *src = &tr->wh;
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200684 int hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100685 u16 space = sizeof(struct mwl8k_dma_data) - hdrlen;
686
687 dst = (void *)tr + space;
688 if (dst != src) {
689 memmove(dst, src, hdrlen);
690 skb_pull(skb, space);
691 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100692}
693
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200694static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100695{
696 struct ieee80211_hdr *wh;
697 u32 hdrlen, pktlen;
698 struct mwl8k_dma_data *tr;
699
700 wh = (struct ieee80211_hdr *)skb->data;
701 hdrlen = ieee80211_hdrlen(wh->frame_control);
702 pktlen = skb->len;
703
704 /*
705 * Copy up/down the 802.11 header; the firmware requires
706 * we present a 2-byte payload length followed by a
707 * 4-address header (w/o QoS), followed (optionally) by
708 * any WEP/ExtIV header (but only filled in for CCMP).
709 */
710 if (hdrlen != sizeof(struct mwl8k_dma_data))
711 skb_push(skb, sizeof(struct mwl8k_dma_data) - hdrlen);
712
713 tr = (struct mwl8k_dma_data *)skb->data;
714 if (wh != &tr->wh)
715 memmove(&tr->wh, wh, hdrlen);
716
717 /* Clear addr4 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200718 memset(tr->wh.addr4, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100719
720 /*
721 * Firmware length is the length of the fully formed "802.11
722 * payload". That is, everything except for the 802.11 header.
723 * This includes all crypto material including the MIC.
724 */
725 tr->fwlen = cpu_to_le16(pktlen - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100726}
727
728
729/*
730 * Packet reception.
731 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100732#define MWL8K_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100733
734struct mwl8k_rx_desc {
735 __le16 pkt_len;
736 __u8 link_quality;
737 __u8 noise_level;
738 __le32 pkt_phys_addr;
739 __le32 next_rx_desc_phys_addr;
740 __le16 qos_control;
741 __le16 rate_info;
742 __le32 pad0[4];
743 __u8 rssi;
744 __u8 channel;
745 __le16 pad1;
746 __u8 rx_ctrl;
747 __u8 rx_status;
748 __u8 pad2[2];
749} __attribute__((packed));
750
751#define MWL8K_RX_DESCS 256
752#define MWL8K_RX_MAXSZ 3800
753
754static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
755{
756 struct mwl8k_priv *priv = hw->priv;
757 struct mwl8k_rx_queue *rxq = priv->rxq + index;
758 int size;
759 int i;
760
761 rxq->rx_desc_count = 0;
762 rxq->rx_head = 0;
763 rxq->rx_tail = 0;
764
765 size = MWL8K_RX_DESCS * sizeof(struct mwl8k_rx_desc);
766
767 rxq->rx_desc_area =
768 pci_alloc_consistent(priv->pdev, size, &rxq->rx_desc_dma);
769 if (rxq->rx_desc_area == NULL) {
770 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200771 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100772 return -ENOMEM;
773 }
774 memset(rxq->rx_desc_area, 0, size);
775
776 rxq->rx_skb = kmalloc(MWL8K_RX_DESCS *
777 sizeof(*rxq->rx_skb), GFP_KERNEL);
778 if (rxq->rx_skb == NULL) {
779 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200780 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100781 pci_free_consistent(priv->pdev, size,
782 rxq->rx_desc_area, rxq->rx_desc_dma);
783 return -ENOMEM;
784 }
785 memset(rxq->rx_skb, 0, MWL8K_RX_DESCS * sizeof(*rxq->rx_skb));
786
787 for (i = 0; i < MWL8K_RX_DESCS; i++) {
788 struct mwl8k_rx_desc *rx_desc;
789 int nexti;
790
791 rx_desc = rxq->rx_desc_area + i;
792 nexti = (i + 1) % MWL8K_RX_DESCS;
793
794 rx_desc->next_rx_desc_phys_addr =
795 cpu_to_le32(rxq->rx_desc_dma
796 + nexti * sizeof(*rx_desc));
Rami Rosenc491bf12009-04-21 16:22:01 +0300797 rx_desc->rx_ctrl = MWL8K_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100798 }
799
800 return 0;
801}
802
803static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
804{
805 struct mwl8k_priv *priv = hw->priv;
806 struct mwl8k_rx_queue *rxq = priv->rxq + index;
807 int refilled;
808
809 refilled = 0;
810 while (rxq->rx_desc_count < MWL8K_RX_DESCS && limit--) {
811 struct sk_buff *skb;
812 int rx;
813
814 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
815 if (skb == NULL)
816 break;
817
818 rxq->rx_desc_count++;
819
820 rx = rxq->rx_tail;
821 rxq->rx_tail = (rx + 1) % MWL8K_RX_DESCS;
822
823 rxq->rx_desc_area[rx].pkt_phys_addr =
824 cpu_to_le32(pci_map_single(priv->pdev, skb->data,
825 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE));
826
827 rxq->rx_desc_area[rx].pkt_len = cpu_to_le16(MWL8K_RX_MAXSZ);
828 rxq->rx_skb[rx] = skb;
829 wmb();
830 rxq->rx_desc_area[rx].rx_ctrl = 0;
831
832 refilled++;
833 }
834
835 return refilled;
836}
837
838/* Must be called only when the card's reception is completely halted */
839static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
840{
841 struct mwl8k_priv *priv = hw->priv;
842 struct mwl8k_rx_queue *rxq = priv->rxq + index;
843 int i;
844
845 for (i = 0; i < MWL8K_RX_DESCS; i++) {
846 if (rxq->rx_skb[i] != NULL) {
847 unsigned long addr;
848
849 addr = le32_to_cpu(rxq->rx_desc_area[i].pkt_phys_addr);
850 pci_unmap_single(priv->pdev, addr, MWL8K_RX_MAXSZ,
851 PCI_DMA_FROMDEVICE);
852 kfree_skb(rxq->rx_skb[i]);
853 rxq->rx_skb[i] = NULL;
854 }
855 }
856
857 kfree(rxq->rx_skb);
858 rxq->rx_skb = NULL;
859
860 pci_free_consistent(priv->pdev,
861 MWL8K_RX_DESCS * sizeof(struct mwl8k_rx_desc),
862 rxq->rx_desc_area, rxq->rx_desc_dma);
863 rxq->rx_desc_area = NULL;
864}
865
866
867/*
868 * Scan a list of BSSIDs to process for finalize join.
869 * Allows for extension to process multiple BSSIDs.
870 */
871static inline int
872mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
873{
874 return priv->capture_beacon &&
875 ieee80211_is_beacon(wh->frame_control) &&
876 !compare_ether_addr(wh->addr3, priv->capture_bssid);
877}
878
Lennert Buytenhek37797522009-10-22 20:19:45 +0200879static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
880 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100881{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200882 struct mwl8k_priv *priv = hw->priv;
883
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100884 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200885 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100886
887 /*
888 * Use GFP_ATOMIC as rxq_process is called from
889 * the primary interrupt handler, memory allocation call
890 * must not sleep.
891 */
892 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
893 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200894 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100895}
896
897static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
898{
899 struct mwl8k_priv *priv = hw->priv;
900 struct mwl8k_rx_queue *rxq = priv->rxq + index;
901 int processed;
902
903 processed = 0;
904 while (rxq->rx_desc_count && limit--) {
905 struct mwl8k_rx_desc *rx_desc;
906 struct sk_buff *skb;
907 struct ieee80211_rx_status status;
908 unsigned long addr;
909 struct ieee80211_hdr *wh;
910
911 rx_desc = rxq->rx_desc_area + rxq->rx_head;
912 if (!(rx_desc->rx_ctrl & MWL8K_RX_CTRL_OWNED_BY_HOST))
913 break;
914 rmb();
915
916 skb = rxq->rx_skb[rxq->rx_head];
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +0200917 if (skb == NULL)
918 break;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100919 rxq->rx_skb[rxq->rx_head] = NULL;
920
921 rxq->rx_head = (rxq->rx_head + 1) % MWL8K_RX_DESCS;
922 rxq->rx_desc_count--;
923
924 addr = le32_to_cpu(rx_desc->pkt_phys_addr);
925 pci_unmap_single(priv->pdev, addr,
926 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
927
928 skb_put(skb, le16_to_cpu(rx_desc->pkt_len));
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200929 mwl8k_remove_dma_header(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100930
931 wh = (struct ieee80211_hdr *)skb->data;
932
933 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200934 * Check for a pending join operation. Save a
935 * copy of the beacon and schedule a tasklet to
936 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100937 */
938 if (mwl8k_capture_bssid(priv, wh))
Lennert Buytenhek37797522009-10-22 20:19:45 +0200939 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100940
941 memset(&status, 0, sizeof(status));
942 status.mactime = 0;
943 status.signal = -rx_desc->rssi;
944 status.noise = -rx_desc->noise_level;
945 status.qual = rx_desc->link_quality;
946 status.antenna = 1;
947 status.rate_idx = 1;
948 status.flag = 0;
949 status.band = IEEE80211_BAND_2GHZ;
950 status.freq = ieee80211_channel_to_frequency(rx_desc->channel);
Johannes Bergf1d58c22009-06-17 13:13:00 +0200951 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
952 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100953
954 processed++;
955 }
956
957 return processed;
958}
959
960
961/*
962 * Packet transmission.
963 */
964
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100965/* Transmit packet ACK policy */
966#define MWL8K_TXD_ACK_POLICY_NORMAL 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100967#define MWL8K_TXD_ACK_POLICY_BLOCKACK 3
968
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100969#define MWL8K_TXD_STATUS_OK 0x00000001
970#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
971#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
972#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100973#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100974
975struct mwl8k_tx_desc {
976 __le32 status;
977 __u8 data_rate;
978 __u8 tx_priority;
979 __le16 qos_control;
980 __le32 pkt_phys_addr;
981 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200982 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100983 __le32 next_tx_desc_phys_addr;
984 __le32 reserved;
985 __le16 rate_info;
986 __u8 peer_id;
987 __u8 tx_frag_cnt;
988} __attribute__((packed));
989
990#define MWL8K_TX_DESCS 128
991
992static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
993{
994 struct mwl8k_priv *priv = hw->priv;
995 struct mwl8k_tx_queue *txq = priv->txq + index;
996 int size;
997 int i;
998
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200999 memset(&txq->tx_stats, 0, sizeof(struct ieee80211_tx_queue_stats));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001000 txq->tx_stats.limit = MWL8K_TX_DESCS;
1001 txq->tx_head = 0;
1002 txq->tx_tail = 0;
1003
1004 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1005
1006 txq->tx_desc_area =
1007 pci_alloc_consistent(priv->pdev, size, &txq->tx_desc_dma);
1008 if (txq->tx_desc_area == NULL) {
1009 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001010 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001011 return -ENOMEM;
1012 }
1013 memset(txq->tx_desc_area, 0, size);
1014
1015 txq->tx_skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->tx_skb),
1016 GFP_KERNEL);
1017 if (txq->tx_skb == NULL) {
1018 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001019 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001020 pci_free_consistent(priv->pdev, size,
1021 txq->tx_desc_area, txq->tx_desc_dma);
1022 return -ENOMEM;
1023 }
1024 memset(txq->tx_skb, 0, MWL8K_TX_DESCS * sizeof(*txq->tx_skb));
1025
1026 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1027 struct mwl8k_tx_desc *tx_desc;
1028 int nexti;
1029
1030 tx_desc = txq->tx_desc_area + i;
1031 nexti = (i + 1) % MWL8K_TX_DESCS;
1032
1033 tx_desc->status = 0;
1034 tx_desc->next_tx_desc_phys_addr =
1035 cpu_to_le32(txq->tx_desc_dma +
1036 nexti * sizeof(*tx_desc));
1037 }
1038
1039 return 0;
1040}
1041
1042static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1043{
1044 iowrite32(MWL8K_H2A_INT_PPA_READY,
1045 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1046 iowrite32(MWL8K_H2A_INT_DUMMY,
1047 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1048 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1049}
1050
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001051struct mwl8k_txq_info {
1052 u32 fw_owned;
1053 u32 drv_owned;
1054 u32 unused;
1055 u32 len;
1056 u32 head;
1057 u32 tail;
1058};
1059
1060static int mwl8k_scan_tx_ring(struct mwl8k_priv *priv,
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001061 struct mwl8k_txq_info *txinfo)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001062{
1063 int count, desc, status;
1064 struct mwl8k_tx_queue *txq;
1065 struct mwl8k_tx_desc *tx_desc;
1066 int ndescs = 0;
1067
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001068 memset(txinfo, 0, MWL8K_TX_QUEUES * sizeof(struct mwl8k_txq_info));
1069
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001070 for (count = 0; count < MWL8K_TX_QUEUES; count++) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001071 txq = priv->txq + count;
1072 txinfo[count].len = txq->tx_stats.len;
1073 txinfo[count].head = txq->tx_head;
1074 txinfo[count].tail = txq->tx_tail;
1075 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
1076 tx_desc = txq->tx_desc_area + desc;
1077 status = le32_to_cpu(tx_desc->status);
1078
1079 if (status & MWL8K_TXD_STATUS_FW_OWNED)
1080 txinfo[count].fw_owned++;
1081 else
1082 txinfo[count].drv_owned++;
1083
1084 if (tx_desc->pkt_len == 0)
1085 txinfo[count].unused++;
1086 }
1087 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001088
1089 return ndescs;
1090}
1091
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001092/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001093 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001094 */
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001095static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001096{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001097 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001098 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001099 u32 count;
1100 unsigned long timeout;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001101
1102 might_sleep();
1103
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001104 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001105 count = priv->pending_tx_pkts;
1106 if (count)
1107 priv->tx_wait = &tx_wait;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001108 spin_unlock_bh(&priv->tx_lock);
1109
1110 if (count) {
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001111 struct mwl8k_txq_info txinfo[MWL8K_TX_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001112 int index;
1113 int newcount;
1114
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001115 timeout = wait_for_completion_timeout(&tx_wait,
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001116 msecs_to_jiffies(5000));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001117 if (timeout)
1118 return 0;
1119
1120 spin_lock_bh(&priv->tx_lock);
1121 priv->tx_wait = NULL;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001122 newcount = priv->pending_tx_pkts;
1123 mwl8k_scan_tx_ring(priv, txinfo);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001124 spin_unlock_bh(&priv->tx_lock);
1125
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001126 printk(KERN_ERR "%s(%u) TIMEDOUT:5000ms Pend:%u-->%u\n",
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001127 __func__, __LINE__, count, newcount);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001128
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001129 for (index = 0; index < MWL8K_TX_QUEUES; index++)
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001130 printk(KERN_ERR "TXQ:%u L:%u H:%u T:%u FW:%u "
1131 "DRV:%u U:%u\n",
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001132 index,
1133 txinfo[index].len,
1134 txinfo[index].head,
1135 txinfo[index].tail,
1136 txinfo[index].fw_owned,
1137 txinfo[index].drv_owned,
1138 txinfo[index].unused);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001139
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001140 return -ETIMEDOUT;
1141 }
1142
1143 return 0;
1144}
1145
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001146#define MWL8K_TXD_SUCCESS(status) \
1147 ((status) & (MWL8K_TXD_STATUS_OK | \
1148 MWL8K_TXD_STATUS_OK_RETRY | \
1149 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001150
1151static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1152{
1153 struct mwl8k_priv *priv = hw->priv;
1154 struct mwl8k_tx_queue *txq = priv->txq + index;
1155 int wake = 0;
1156
1157 while (txq->tx_stats.len > 0) {
1158 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159 struct mwl8k_tx_desc *tx_desc;
1160 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001161 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001162 struct sk_buff *skb;
1163 struct ieee80211_tx_info *info;
1164 u32 status;
1165
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001166 tx = txq->tx_head;
1167 tx_desc = txq->tx_desc_area + tx;
1168
1169 status = le32_to_cpu(tx_desc->status);
1170
1171 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1172 if (!force)
1173 break;
1174 tx_desc->status &=
1175 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1176 }
1177
1178 txq->tx_head = (tx + 1) % MWL8K_TX_DESCS;
1179 BUG_ON(txq->tx_stats.len == 0);
1180 txq->tx_stats.len--;
1181 priv->pending_tx_pkts--;
1182
1183 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001184 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001185 skb = txq->tx_skb[tx];
1186 txq->tx_skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001187
1188 BUG_ON(skb == NULL);
1189 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1190
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001191 mwl8k_remove_dma_header(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001192
1193 /* Mark descriptor as unused */
1194 tx_desc->pkt_phys_addr = 0;
1195 tx_desc->pkt_len = 0;
1196
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001197 info = IEEE80211_SKB_CB(skb);
1198 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001199 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001200 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001201
1202 ieee80211_tx_status_irqsafe(hw, skb);
1203
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001204 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205 }
1206
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001207 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001208 ieee80211_wake_queue(hw, index);
1209}
1210
1211/* must be called only when the card's transmit is completely halted */
1212static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1213{
1214 struct mwl8k_priv *priv = hw->priv;
1215 struct mwl8k_tx_queue *txq = priv->txq + index;
1216
1217 mwl8k_txq_reclaim(hw, index, 1);
1218
1219 kfree(txq->tx_skb);
1220 txq->tx_skb = NULL;
1221
1222 pci_free_consistent(priv->pdev,
1223 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
1224 txq->tx_desc_area, txq->tx_desc_dma);
1225 txq->tx_desc_area = NULL;
1226}
1227
1228static int
1229mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1230{
1231 struct mwl8k_priv *priv = hw->priv;
1232 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001233 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001234 struct ieee80211_hdr *wh;
1235 struct mwl8k_tx_queue *txq;
1236 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001237 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001238 u32 txstatus;
1239 u8 txdatarate;
1240 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001241
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001242 wh = (struct ieee80211_hdr *)skb->data;
1243 if (ieee80211_is_data_qos(wh->frame_control))
1244 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1245 else
1246 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001247
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001248 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001249 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001250
1251 tx_info = IEEE80211_SKB_CB(skb);
1252 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001253
1254 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1255 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001256
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001257 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1258 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1259 mwl8k_vif->seqno = seqno++ % 4096;
1260 }
1261
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001262 /* Setup firmware control bit fields for each frame type. */
1263 txstatus = 0;
1264 txdatarate = 0;
1265 if (ieee80211_is_mgmt(wh->frame_control) ||
1266 ieee80211_is_ctl(wh->frame_control)) {
1267 txdatarate = 0;
1268 qos = mwl8k_qos_setbit_eosp(qos);
1269 /* Set Queue size to unspecified */
1270 qos = mwl8k_qos_setbit_qlen(qos, 0xff);
1271 } else if (ieee80211_is_data(wh->frame_control)) {
1272 txdatarate = 1;
1273 if (is_multicast_ether_addr(wh->addr1))
1274 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1275
1276 /* Send pkt in an aggregate if AMPDU frame. */
1277 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1278 qos = mwl8k_qos_setbit_ack(qos,
1279 MWL8K_TXD_ACK_POLICY_BLOCKACK);
1280 else
1281 qos = mwl8k_qos_setbit_ack(qos,
1282 MWL8K_TXD_ACK_POLICY_NORMAL);
1283
1284 if (qos & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
1285 qos = mwl8k_qos_setbit_amsdu(qos);
1286 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001287
1288 dma = pci_map_single(priv->pdev, skb->data,
1289 skb->len, PCI_DMA_TODEVICE);
1290
1291 if (pci_dma_mapping_error(priv->pdev, dma)) {
1292 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001293 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001294 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001295 return NETDEV_TX_OK;
1296 }
1297
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001298 spin_lock_bh(&priv->tx_lock);
1299
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001300 txq = priv->txq + index;
1301
1302 BUG_ON(txq->tx_skb[txq->tx_tail] != NULL);
1303 txq->tx_skb[txq->tx_tail] = skb;
1304
1305 tx = txq->tx_desc_area + txq->tx_tail;
1306 tx->data_rate = txdatarate;
1307 tx->tx_priority = index;
1308 tx->qos_control = cpu_to_le16(qos);
1309 tx->pkt_phys_addr = cpu_to_le32(dma);
1310 tx->pkt_len = cpu_to_le16(skb->len);
1311 tx->rate_info = 0;
1312 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001313 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001314 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1315
1316 txq->tx_stats.count++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001317 txq->tx_stats.len++;
1318 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001319
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001320 txq->tx_tail++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001321 if (txq->tx_tail == MWL8K_TX_DESCS)
1322 txq->tx_tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001323
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001324 if (txq->tx_head == txq->tx_tail)
1325 ieee80211_stop_queue(hw, index);
1326
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001327 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001328
1329 spin_unlock_bh(&priv->tx_lock);
1330
1331 return NETDEV_TX_OK;
1332}
1333
1334
1335/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001336 * Firmware access.
1337 *
1338 * We have the following requirements for issuing firmware commands:
1339 * - Some commands require that the packet transmit path is idle when
1340 * the command is issued. (For simplicity, we'll just quiesce the
1341 * transmit path for every command.)
1342 * - There are certain sequences of commands that need to be issued to
1343 * the hardware sequentially, with no other intervening commands.
1344 *
1345 * This leads to an implementation of a "firmware lock" as a mutex that
1346 * can be taken recursively, and which is taken by both the low-level
1347 * command submission function (mwl8k_post_cmd) as well as any users of
1348 * that function that require issuing of an atomic sequence of commands,
1349 * and quiesces the transmit path whenever it's taken.
1350 */
1351static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1352{
1353 struct mwl8k_priv *priv = hw->priv;
1354
1355 if (priv->fw_mutex_owner != current) {
1356 int rc;
1357
1358 mutex_lock(&priv->fw_mutex);
1359 ieee80211_stop_queues(hw);
1360
1361 rc = mwl8k_tx_wait_empty(hw);
1362 if (rc) {
1363 ieee80211_wake_queues(hw);
1364 mutex_unlock(&priv->fw_mutex);
1365
1366 return rc;
1367 }
1368
1369 priv->fw_mutex_owner = current;
1370 }
1371
1372 priv->fw_mutex_depth++;
1373
1374 return 0;
1375}
1376
1377static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1378{
1379 struct mwl8k_priv *priv = hw->priv;
1380
1381 if (!--priv->fw_mutex_depth) {
1382 ieee80211_wake_queues(hw);
1383 priv->fw_mutex_owner = NULL;
1384 mutex_unlock(&priv->fw_mutex);
1385 }
1386}
1387
1388
1389/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001390 * Command processing.
1391 */
1392
1393/* Timeout firmware commands after 2000ms */
1394#define MWL8K_CMD_TIMEOUT_MS 2000
1395
1396static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1397{
1398 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1399 struct mwl8k_priv *priv = hw->priv;
1400 void __iomem *regs = priv->regs;
1401 dma_addr_t dma_addr;
1402 unsigned int dma_size;
1403 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001404 unsigned long timeout = 0;
1405 u8 buf[32];
1406
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001407 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001408 dma_size = le16_to_cpu(cmd->length);
1409 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1410 PCI_DMA_BIDIRECTIONAL);
1411 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1412 return -ENOMEM;
1413
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001414 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001415 if (rc) {
1416 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1417 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001418 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001419 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001420
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001421 priv->hostcmd_wait = &cmd_wait;
1422 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1423 iowrite32(MWL8K_H2A_INT_DOORBELL,
1424 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1425 iowrite32(MWL8K_H2A_INT_DUMMY,
1426 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001427
1428 timeout = wait_for_completion_timeout(&cmd_wait,
1429 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1430
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001431 priv->hostcmd_wait = NULL;
1432
1433 mwl8k_fw_unlock(hw);
1434
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001435 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1436 PCI_DMA_BIDIRECTIONAL);
1437
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001438 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001440 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001441 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1442 MWL8K_CMD_TIMEOUT_MS);
1443 rc = -ETIMEDOUT;
1444 } else {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001445 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001446 if (rc)
1447 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001448 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001449 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001450 le16_to_cpu(cmd->result));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001451 }
1452
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001453 return rc;
1454}
1455
1456/*
1457 * GET_HW_SPEC.
1458 */
1459struct mwl8k_cmd_get_hw_spec {
1460 struct mwl8k_cmd_pkt header;
1461 __u8 hw_rev;
1462 __u8 host_interface;
1463 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001464 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001465 __le16 region_code;
1466 __le32 fw_rev;
1467 __le32 ps_cookie;
1468 __le32 caps;
1469 __u8 mcs_bitmap[16];
1470 __le32 rx_queue_ptr;
1471 __le32 num_tx_queues;
1472 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1473 __le32 caps2;
1474 __le32 num_tx_desc_per_queue;
1475 __le32 total_rx_desc;
1476} __attribute__((packed));
1477
1478static int mwl8k_cmd_get_hw_spec(struct ieee80211_hw *hw)
1479{
1480 struct mwl8k_priv *priv = hw->priv;
1481 struct mwl8k_cmd_get_hw_spec *cmd;
1482 int rc;
1483 int i;
1484
1485 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1486 if (cmd == NULL)
1487 return -ENOMEM;
1488
1489 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1490 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1491
1492 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1493 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1494 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rx_desc_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001495 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1497 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].tx_desc_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001498 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1499 cmd->total_rx_desc = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001500
1501 rc = mwl8k_post_cmd(hw, &cmd->header);
1502
1503 if (!rc) {
1504 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1505 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001506 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001507 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001508 }
1509
1510 kfree(cmd);
1511 return rc;
1512}
1513
1514/*
1515 * CMD_MAC_MULTICAST_ADR.
1516 */
1517struct mwl8k_cmd_mac_multicast_adr {
1518 struct mwl8k_cmd_pkt header;
1519 __le16 action;
1520 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001521 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001522};
1523
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001524#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1525#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1526#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1527#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001528
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001529static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001530__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001531 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001532{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001533 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001534 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001535 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001536
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001537 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001538 allmulti = 1;
1539 mc_count = 0;
1540 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001541
1542 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1543
1544 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001545 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001546 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001547
1548 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1549 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001550 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1551 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001552
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001553 if (allmulti) {
1554 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1555 } else if (mc_count) {
1556 int i;
1557
1558 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1559 cmd->numaddr = cpu_to_le16(mc_count);
1560 for (i = 0; i < mc_count && mclist; i++) {
1561 if (mclist->da_addrlen != ETH_ALEN) {
1562 kfree(cmd);
1563 return NULL;
1564 }
1565 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1566 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001567 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001568 }
1569
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001570 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001571}
1572
1573/*
1574 * CMD_802_11_GET_STAT.
1575 */
1576struct mwl8k_cmd_802_11_get_stat {
1577 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578 __le32 stats[64];
1579} __attribute__((packed));
1580
1581#define MWL8K_STAT_ACK_FAILURE 9
1582#define MWL8K_STAT_RTS_FAILURE 12
1583#define MWL8K_STAT_FCS_ERROR 24
1584#define MWL8K_STAT_RTS_SUCCESS 11
1585
1586static int mwl8k_cmd_802_11_get_stat(struct ieee80211_hw *hw,
1587 struct ieee80211_low_level_stats *stats)
1588{
1589 struct mwl8k_cmd_802_11_get_stat *cmd;
1590 int rc;
1591
1592 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1593 if (cmd == NULL)
1594 return -ENOMEM;
1595
1596 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1597 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001598
1599 rc = mwl8k_post_cmd(hw, &cmd->header);
1600 if (!rc) {
1601 stats->dot11ACKFailureCount =
1602 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1603 stats->dot11RTSFailureCount =
1604 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1605 stats->dot11FCSErrorCount =
1606 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1607 stats->dot11RTSSuccessCount =
1608 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1609 }
1610 kfree(cmd);
1611
1612 return rc;
1613}
1614
1615/*
1616 * CMD_802_11_RADIO_CONTROL.
1617 */
1618struct mwl8k_cmd_802_11_radio_control {
1619 struct mwl8k_cmd_pkt header;
1620 __le16 action;
1621 __le16 control;
1622 __le16 radio_on;
1623} __attribute__((packed));
1624
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001625static int
1626mwl8k_cmd_802_11_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001627{
1628 struct mwl8k_priv *priv = hw->priv;
1629 struct mwl8k_cmd_802_11_radio_control *cmd;
1630 int rc;
1631
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001632 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001633 return 0;
1634
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001635 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1636 if (cmd == NULL)
1637 return -ENOMEM;
1638
1639 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1640 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1641 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001642 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001643 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1644
1645 rc = mwl8k_post_cmd(hw, &cmd->header);
1646 kfree(cmd);
1647
1648 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001649 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001650
1651 return rc;
1652}
1653
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001654static int mwl8k_cmd_802_11_radio_disable(struct ieee80211_hw *hw)
1655{
1656 return mwl8k_cmd_802_11_radio_control(hw, 0, 0);
1657}
1658
1659static int mwl8k_cmd_802_11_radio_enable(struct ieee80211_hw *hw)
1660{
1661 return mwl8k_cmd_802_11_radio_control(hw, 1, 0);
1662}
1663
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001664static int
1665mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1666{
1667 struct mwl8k_priv *priv;
1668
1669 if (hw == NULL || hw->priv == NULL)
1670 return -EINVAL;
1671 priv = hw->priv;
1672
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001673 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001674
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001675 return mwl8k_cmd_802_11_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001676}
1677
1678/*
1679 * CMD_802_11_RF_TX_POWER.
1680 */
1681#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1682
1683struct mwl8k_cmd_802_11_rf_tx_power {
1684 struct mwl8k_cmd_pkt header;
1685 __le16 action;
1686 __le16 support_level;
1687 __le16 current_level;
1688 __le16 reserved;
1689 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1690} __attribute__((packed));
1691
1692static int mwl8k_cmd_802_11_rf_tx_power(struct ieee80211_hw *hw, int dBm)
1693{
1694 struct mwl8k_cmd_802_11_rf_tx_power *cmd;
1695 int rc;
1696
1697 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1698 if (cmd == NULL)
1699 return -ENOMEM;
1700
1701 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1702 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1703 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1704 cmd->support_level = cpu_to_le16(dBm);
1705
1706 rc = mwl8k_post_cmd(hw, &cmd->header);
1707 kfree(cmd);
1708
1709 return rc;
1710}
1711
1712/*
1713 * CMD_SET_PRE_SCAN.
1714 */
1715struct mwl8k_cmd_set_pre_scan {
1716 struct mwl8k_cmd_pkt header;
1717} __attribute__((packed));
1718
1719static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
1720{
1721 struct mwl8k_cmd_set_pre_scan *cmd;
1722 int rc;
1723
1724 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1725 if (cmd == NULL)
1726 return -ENOMEM;
1727
1728 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
1729 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1730
1731 rc = mwl8k_post_cmd(hw, &cmd->header);
1732 kfree(cmd);
1733
1734 return rc;
1735}
1736
1737/*
1738 * CMD_SET_POST_SCAN.
1739 */
1740struct mwl8k_cmd_set_post_scan {
1741 struct mwl8k_cmd_pkt header;
1742 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001743 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001744} __attribute__((packed));
1745
1746static int
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001747mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001748{
1749 struct mwl8k_cmd_set_post_scan *cmd;
1750 int rc;
1751
1752 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1753 if (cmd == NULL)
1754 return -ENOMEM;
1755
1756 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
1757 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1758 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001759 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001760
1761 rc = mwl8k_post_cmd(hw, &cmd->header);
1762 kfree(cmd);
1763
1764 return rc;
1765}
1766
1767/*
1768 * CMD_SET_RF_CHANNEL.
1769 */
1770struct mwl8k_cmd_set_rf_channel {
1771 struct mwl8k_cmd_pkt header;
1772 __le16 action;
1773 __u8 current_channel;
1774 __le32 channel_flags;
1775} __attribute__((packed));
1776
1777static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
1778 struct ieee80211_channel *channel)
1779{
1780 struct mwl8k_cmd_set_rf_channel *cmd;
1781 int rc;
1782
1783 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1784 if (cmd == NULL)
1785 return -ENOMEM;
1786
1787 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
1788 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1789 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1790 cmd->current_channel = channel->hw_value;
1791 if (channel->band == IEEE80211_BAND_2GHZ)
1792 cmd->channel_flags = cpu_to_le32(0x00000081);
1793 else
1794 cmd->channel_flags = cpu_to_le32(0x00000000);
1795
1796 rc = mwl8k_post_cmd(hw, &cmd->header);
1797 kfree(cmd);
1798
1799 return rc;
1800}
1801
1802/*
1803 * CMD_SET_SLOT.
1804 */
1805struct mwl8k_cmd_set_slot {
1806 struct mwl8k_cmd_pkt header;
1807 __le16 action;
1808 __u8 short_slot;
1809} __attribute__((packed));
1810
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02001811static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001812{
1813 struct mwl8k_cmd_set_slot *cmd;
1814 int rc;
1815
1816 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1817 if (cmd == NULL)
1818 return -ENOMEM;
1819
1820 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
1821 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1822 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02001823 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001824
1825 rc = mwl8k_post_cmd(hw, &cmd->header);
1826 kfree(cmd);
1827
1828 return rc;
1829}
1830
1831/*
1832 * CMD_MIMO_CONFIG.
1833 */
1834struct mwl8k_cmd_mimo_config {
1835 struct mwl8k_cmd_pkt header;
1836 __le32 action;
1837 __u8 rx_antenna_map;
1838 __u8 tx_antenna_map;
1839} __attribute__((packed));
1840
1841static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
1842{
1843 struct mwl8k_cmd_mimo_config *cmd;
1844 int rc;
1845
1846 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1847 if (cmd == NULL)
1848 return -ENOMEM;
1849
1850 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
1851 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1852 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
1853 cmd->rx_antenna_map = rx;
1854 cmd->tx_antenna_map = tx;
1855
1856 rc = mwl8k_post_cmd(hw, &cmd->header);
1857 kfree(cmd);
1858
1859 return rc;
1860}
1861
1862/*
1863 * CMD_ENABLE_SNIFFER.
1864 */
1865struct mwl8k_cmd_enable_sniffer {
1866 struct mwl8k_cmd_pkt header;
1867 __le32 action;
1868} __attribute__((packed));
1869
1870static int mwl8k_enable_sniffer(struct ieee80211_hw *hw, bool enable)
1871{
1872 struct mwl8k_cmd_enable_sniffer *cmd;
1873 int rc;
1874
1875 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1876 if (cmd == NULL)
1877 return -ENOMEM;
1878
1879 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
1880 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001881 cmd->action = cpu_to_le32(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001882
1883 rc = mwl8k_post_cmd(hw, &cmd->header);
1884 kfree(cmd);
1885
1886 return rc;
1887}
1888
1889/*
Lennert Buytenhek32060e12009-10-22 20:20:04 +02001890 * CMD_SET_MAC_ADDR.
1891 */
1892struct mwl8k_cmd_set_mac_addr {
1893 struct mwl8k_cmd_pkt header;
1894 __u8 mac_addr[ETH_ALEN];
1895} __attribute__((packed));
1896
1897static int mwl8k_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
1898{
1899 struct mwl8k_cmd_set_mac_addr *cmd;
1900 int rc;
1901
1902 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1903 if (cmd == NULL)
1904 return -ENOMEM;
1905
1906 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
1907 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1908 memcpy(cmd->mac_addr, mac, ETH_ALEN);
1909
1910 rc = mwl8k_post_cmd(hw, &cmd->header);
1911 kfree(cmd);
1912
1913 return rc;
1914}
1915
1916
1917/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001918 * CMD_SET_RATEADAPT_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919 */
1920struct mwl8k_cmd_set_rate_adapt_mode {
1921 struct mwl8k_cmd_pkt header;
1922 __le16 action;
1923 __le16 mode;
1924} __attribute__((packed));
1925
1926static int mwl8k_cmd_setrateadaptmode(struct ieee80211_hw *hw, __u16 mode)
1927{
1928 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
1929 int rc;
1930
1931 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1932 if (cmd == NULL)
1933 return -ENOMEM;
1934
1935 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
1936 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1937 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1938 cmd->mode = cpu_to_le16(mode);
1939
1940 rc = mwl8k_post_cmd(hw, &cmd->header);
1941 kfree(cmd);
1942
1943 return rc;
1944}
1945
1946/*
1947 * CMD_SET_WMM_MODE.
1948 */
1949struct mwl8k_cmd_set_wmm {
1950 struct mwl8k_cmd_pkt header;
1951 __le16 action;
1952} __attribute__((packed));
1953
1954static int mwl8k_set_wmm(struct ieee80211_hw *hw, bool enable)
1955{
1956 struct mwl8k_priv *priv = hw->priv;
1957 struct mwl8k_cmd_set_wmm *cmd;
1958 int rc;
1959
1960 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1961 if (cmd == NULL)
1962 return -ENOMEM;
1963
1964 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
1965 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02001966 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001967
1968 rc = mwl8k_post_cmd(hw, &cmd->header);
1969 kfree(cmd);
1970
1971 if (!rc)
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02001972 priv->wmm_enabled = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001973
1974 return rc;
1975}
1976
1977/*
1978 * CMD_SET_RTS_THRESHOLD.
1979 */
1980struct mwl8k_cmd_rts_threshold {
1981 struct mwl8k_cmd_pkt header;
1982 __le16 action;
1983 __le16 threshold;
1984} __attribute__((packed));
1985
1986static int mwl8k_rts_threshold(struct ieee80211_hw *hw,
Lennert Buytenhek733d3062009-07-17 07:24:15 +02001987 u16 action, u16 threshold)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001988{
1989 struct mwl8k_cmd_rts_threshold *cmd;
1990 int rc;
1991
1992 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1993 if (cmd == NULL)
1994 return -ENOMEM;
1995
1996 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
1997 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1998 cmd->action = cpu_to_le16(action);
Lennert Buytenhek733d3062009-07-17 07:24:15 +02001999 cmd->threshold = cpu_to_le16(threshold);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002000
2001 rc = mwl8k_post_cmd(hw, &cmd->header);
2002 kfree(cmd);
2003
2004 return rc;
2005}
2006
2007/*
2008 * CMD_SET_EDCA_PARAMS.
2009 */
2010struct mwl8k_cmd_set_edca_params {
2011 struct mwl8k_cmd_pkt header;
2012
2013 /* See MWL8K_SET_EDCA_XXX below */
2014 __le16 action;
2015
2016 /* TX opportunity in units of 32 us */
2017 __le16 txop;
2018
2019 /* Log exponent of max contention period: 0...15*/
2020 __u8 log_cw_max;
2021
2022 /* Log exponent of min contention period: 0...15 */
2023 __u8 log_cw_min;
2024
2025 /* Adaptive interframe spacing in units of 32us */
2026 __u8 aifs;
2027
2028 /* TX queue to configure */
2029 __u8 txq;
2030} __attribute__((packed));
2031
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002032#define MWL8K_SET_EDCA_CW 0x01
2033#define MWL8K_SET_EDCA_TXOP 0x02
2034#define MWL8K_SET_EDCA_AIFS 0x04
2035
2036#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2037 MWL8K_SET_EDCA_TXOP | \
2038 MWL8K_SET_EDCA_AIFS)
2039
2040static int
2041mwl8k_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2042 __u16 cw_min, __u16 cw_max,
2043 __u8 aifs, __u16 txop)
2044{
2045 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002046 int rc;
2047
2048 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2049 if (cmd == NULL)
2050 return -ENOMEM;
2051
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002052 /*
2053 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2054 * this call.
2055 */
2056 qnum ^= !(qnum >> 1);
2057
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002058 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2059 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002060 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2061 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002062 cmd->log_cw_max = (u8)ilog2(cw_max + 1);
2063 cmd->log_cw_min = (u8)ilog2(cw_min + 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002064 cmd->aifs = aifs;
2065 cmd->txq = qnum;
2066
2067 rc = mwl8k_post_cmd(hw, &cmd->header);
2068 kfree(cmd);
2069
2070 return rc;
2071}
2072
2073/*
2074 * CMD_FINALIZE_JOIN.
2075 */
2076
2077/* FJ beacon buffer size is compiled into the firmware. */
2078#define MWL8K_FJ_BEACON_MAXLEN 128
2079
2080struct mwl8k_cmd_finalize_join {
2081 struct mwl8k_cmd_pkt header;
2082 __le32 sleep_interval; /* Number of beacon periods to sleep */
2083 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2084} __attribute__((packed));
2085
2086static int mwl8k_finalize_join(struct ieee80211_hw *hw, void *frame,
2087 __u16 framelen, __u16 dtim)
2088{
2089 struct mwl8k_cmd_finalize_join *cmd;
2090 struct ieee80211_mgmt *payload = frame;
2091 u16 hdrlen;
2092 u32 payload_len;
2093 int rc;
2094
2095 if (frame == NULL)
2096 return -EINVAL;
2097
2098 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2099 if (cmd == NULL)
2100 return -ENOMEM;
2101
2102 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2103 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002104 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002105
2106 hdrlen = ieee80211_hdrlen(payload->frame_control);
2107
2108 payload_len = framelen > hdrlen ? framelen - hdrlen : 0;
2109
2110 /* XXX TBD Might just have to abort and return an error */
2111 if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2112 printk(KERN_ERR "%s(): WARNING: Incomplete beacon "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002113 "sent to firmware. Sz=%u MAX=%u\n", __func__,
2114 payload_len, MWL8K_FJ_BEACON_MAXLEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002115
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002116 if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2117 payload_len = MWL8K_FJ_BEACON_MAXLEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002118
2119 if (payload && payload_len)
2120 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2121
2122 rc = mwl8k_post_cmd(hw, &cmd->header);
2123 kfree(cmd);
2124 return rc;
2125}
2126
2127/*
2128 * CMD_UPDATE_STADB.
2129 */
2130struct mwl8k_cmd_update_sta_db {
2131 struct mwl8k_cmd_pkt header;
2132
2133 /* See STADB_ACTION_TYPE */
2134 __le32 action;
2135
2136 /* Peer MAC address */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002137 __u8 peer_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002138
2139 __le32 reserved;
2140
2141 /* Peer info - valid during add/update. */
2142 struct peer_capability_info peer_info;
2143} __attribute__((packed));
2144
2145static int mwl8k_cmd_update_sta_db(struct ieee80211_hw *hw,
2146 struct ieee80211_vif *vif, __u32 action)
2147{
2148 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2149 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2150 struct mwl8k_cmd_update_sta_db *cmd;
2151 struct peer_capability_info *peer_info;
2152 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002153 int rc;
2154 __u8 count, *rates;
2155
2156 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2157 if (cmd == NULL)
2158 return -ENOMEM;
2159
2160 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2161 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2162
2163 cmd->action = cpu_to_le32(action);
2164 peer_info = &cmd->peer_info;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002165 memcpy(cmd->peer_addr, mv_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002166
2167 switch (action) {
2168 case MWL8K_STA_DB_ADD_ENTRY:
2169 case MWL8K_STA_DB_MODIFY_ENTRY:
2170 /* Build peer_info block */
2171 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2172 peer_info->basic_caps = cpu_to_le16(info->assoc_capability);
2173 peer_info->interop = 1;
2174 peer_info->amsdu_enabled = 0;
2175
2176 rates = peer_info->legacy_rates;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002177 for (count = 0; count < mv_vif->legacy_nrates; count++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002178 rates[count] = bitrates[count].hw_value;
2179
2180 rc = mwl8k_post_cmd(hw, &cmd->header);
2181 if (rc == 0)
2182 mv_vif->peer_id = peer_info->station_id;
2183
2184 break;
2185
2186 case MWL8K_STA_DB_DEL_ENTRY:
2187 case MWL8K_STA_DB_FLUSH:
2188 default:
2189 rc = mwl8k_post_cmd(hw, &cmd->header);
2190 if (rc == 0)
2191 mv_vif->peer_id = 0;
2192 break;
2193 }
2194 kfree(cmd);
2195
2196 return rc;
2197}
2198
2199/*
2200 * CMD_SET_AID.
2201 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002202#define MWL8K_RATE_INDEX_MAX_ARRAY 14
2203
2204#define MWL8K_FRAME_PROT_DISABLED 0x00
2205#define MWL8K_FRAME_PROT_11G 0x07
2206#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2207#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002208
2209struct mwl8k_cmd_update_set_aid {
2210 struct mwl8k_cmd_pkt header;
2211 __le16 aid;
2212
2213 /* AP's MAC address (BSSID) */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002214 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002215 __le16 protection_mode;
2216 __u8 supp_rates[MWL8K_RATE_INDEX_MAX_ARRAY];
2217} __attribute__((packed));
2218
2219static int mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2220 struct ieee80211_vif *vif)
2221{
2222 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2223 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2224 struct mwl8k_cmd_update_set_aid *cmd;
2225 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
2226 int count;
2227 u16 prot_mode;
2228 int rc;
2229
2230 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2231 if (cmd == NULL)
2232 return -ENOMEM;
2233
2234 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2235 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2236 cmd->aid = cpu_to_le16(info->aid);
2237
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002238 memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002239
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002240 if (info->use_cts_prot) {
2241 prot_mode = MWL8K_FRAME_PROT_11G;
2242 } else {
Johannes Berg9ed6bcc2009-05-08 20:47:39 +02002243 switch (info->ht_operation_mode &
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002244 IEEE80211_HT_OP_MODE_PROTECTION) {
2245 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2246 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2247 break;
2248 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2249 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2250 break;
2251 default:
2252 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2253 break;
2254 }
2255 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002256 cmd->protection_mode = cpu_to_le16(prot_mode);
2257
2258 for (count = 0; count < mv_vif->legacy_nrates; count++)
2259 cmd->supp_rates[count] = bitrates[count].hw_value;
2260
2261 rc = mwl8k_post_cmd(hw, &cmd->header);
2262 kfree(cmd);
2263
2264 return rc;
2265}
2266
2267/*
2268 * CMD_SET_RATE.
2269 */
2270struct mwl8k_cmd_update_rateset {
2271 struct mwl8k_cmd_pkt header;
2272 __u8 legacy_rates[MWL8K_RATE_INDEX_MAX_ARRAY];
2273
2274 /* Bitmap for supported MCS codes. */
2275 __u8 mcs_set[MWL8K_IEEE_LEGACY_DATA_RATES];
2276 __u8 reserved[MWL8K_IEEE_LEGACY_DATA_RATES];
2277} __attribute__((packed));
2278
2279static int mwl8k_update_rateset(struct ieee80211_hw *hw,
2280 struct ieee80211_vif *vif)
2281{
2282 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2283 struct mwl8k_cmd_update_rateset *cmd;
2284 struct ieee80211_rate *bitrates = mv_vif->legacy_rates;
2285 int count;
2286 int rc;
2287
2288 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2289 if (cmd == NULL)
2290 return -ENOMEM;
2291
2292 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2293 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2294
2295 for (count = 0; count < mv_vif->legacy_nrates; count++)
2296 cmd->legacy_rates[count] = bitrates[count].hw_value;
2297
2298 rc = mwl8k_post_cmd(hw, &cmd->header);
2299 kfree(cmd);
2300
2301 return rc;
2302}
2303
2304/*
2305 * CMD_USE_FIXED_RATE.
2306 */
2307#define MWL8K_RATE_TABLE_SIZE 8
2308#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002309#define MWL8K_USE_AUTO_RATE 0x0002
2310
2311struct mwl8k_rate_entry {
2312 /* Set to 1 if HT rate, 0 if legacy. */
2313 __le32 is_ht_rate;
2314
2315 /* Set to 1 to use retry_count field. */
2316 __le32 enable_retry;
2317
2318 /* Specified legacy rate or MCS. */
2319 __le32 rate;
2320
2321 /* Number of allowed retries. */
2322 __le32 retry_count;
2323} __attribute__((packed));
2324
2325struct mwl8k_rate_table {
2326 /* 1 to allow specified rate and below */
2327 __le32 allow_rate_drop;
2328 __le32 num_rates;
2329 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2330} __attribute__((packed));
2331
2332struct mwl8k_cmd_use_fixed_rate {
2333 struct mwl8k_cmd_pkt header;
2334 __le32 action;
2335 struct mwl8k_rate_table rate_table;
2336
2337 /* Unicast, Broadcast or Multicast */
2338 __le32 rate_type;
2339 __le32 reserved1;
2340 __le32 reserved2;
2341} __attribute__((packed));
2342
2343static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2344 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2345{
2346 struct mwl8k_cmd_use_fixed_rate *cmd;
2347 int count;
2348 int rc;
2349
2350 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2351 if (cmd == NULL)
2352 return -ENOMEM;
2353
2354 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2355 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2356
2357 cmd->action = cpu_to_le32(action);
2358 cmd->rate_type = cpu_to_le32(rate_type);
2359
2360 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002361 /*
2362 * Copy over each field manually so that endian
2363 * conversion can be done.
2364 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002365 cmd->rate_table.allow_rate_drop =
2366 cpu_to_le32(rate_table->allow_rate_drop);
2367 cmd->rate_table.num_rates =
2368 cpu_to_le32(rate_table->num_rates);
2369
2370 for (count = 0; count < rate_table->num_rates; count++) {
2371 struct mwl8k_rate_entry *dst =
2372 &cmd->rate_table.rate_entry[count];
2373 struct mwl8k_rate_entry *src =
2374 &rate_table->rate_entry[count];
2375
2376 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2377 dst->enable_retry = cpu_to_le32(src->enable_retry);
2378 dst->rate = cpu_to_le32(src->rate);
2379 dst->retry_count = cpu_to_le32(src->retry_count);
2380 }
2381 }
2382
2383 rc = mwl8k_post_cmd(hw, &cmd->header);
2384 kfree(cmd);
2385
2386 return rc;
2387}
2388
2389
2390/*
2391 * Interrupt handling.
2392 */
2393static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2394{
2395 struct ieee80211_hw *hw = dev_id;
2396 struct mwl8k_priv *priv = hw->priv;
2397 u32 status;
2398
2399 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2400 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2401
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002402 if (!status)
2403 return IRQ_NONE;
2404
2405 if (status & MWL8K_A2H_INT_TX_DONE)
2406 tasklet_schedule(&priv->tx_reclaim_task);
2407
2408 if (status & MWL8K_A2H_INT_RX_READY) {
2409 while (rxq_process(hw, 0, 1))
2410 rxq_refill(hw, 0, 1);
2411 }
2412
2413 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002414 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002415 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002416 }
2417
2418 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002419 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002420 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002421 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002422 }
2423
2424 return IRQ_HANDLED;
2425}
2426
2427
2428/*
2429 * Core driver operations.
2430 */
2431static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2432{
2433 struct mwl8k_priv *priv = hw->priv;
2434 int index = skb_get_queue_mapping(skb);
2435 int rc;
2436
2437 if (priv->current_channel == NULL) {
2438 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002439 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002440 dev_kfree_skb(skb);
2441 return NETDEV_TX_OK;
2442 }
2443
2444 rc = mwl8k_txq_xmit(hw, index, skb);
2445
2446 return rc;
2447}
2448
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002449static int mwl8k_start(struct ieee80211_hw *hw)
2450{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002451 struct mwl8k_priv *priv = hw->priv;
2452 int rc;
2453
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002454 rc = request_irq(priv->pdev->irq, &mwl8k_interrupt,
2455 IRQF_SHARED, MWL8K_NAME, hw);
2456 if (rc) {
2457 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002458 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002459 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002460 }
2461
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002462 /* Enable tx reclaim tasklet */
2463 tasklet_enable(&priv->tx_reclaim_task);
2464
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002465 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002466 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002467
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002468 rc = mwl8k_fw_lock(hw);
2469 if (!rc) {
2470 rc = mwl8k_cmd_802_11_radio_enable(hw);
2471
2472 if (!rc)
2473 rc = mwl8k_cmd_set_pre_scan(hw);
2474
2475 if (!rc)
2476 rc = mwl8k_cmd_set_post_scan(hw,
2477 "\x00\x00\x00\x00\x00\x00");
2478
2479 if (!rc)
2480 rc = mwl8k_cmd_setrateadaptmode(hw, 0);
2481
2482 if (!rc)
2483 rc = mwl8k_set_wmm(hw, 0);
2484
2485 if (!rc)
2486 rc = mwl8k_enable_sniffer(hw, 0);
2487
2488 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002489 }
2490
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002491 if (rc) {
2492 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2493 free_irq(priv->pdev->irq, hw);
2494 tasklet_disable(&priv->tx_reclaim_task);
2495 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002496
2497 return rc;
2498}
2499
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002500static void mwl8k_stop(struct ieee80211_hw *hw)
2501{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002502 struct mwl8k_priv *priv = hw->priv;
2503 int i;
2504
Lennert Buytenhekd3cea0b2009-07-17 07:15:49 +02002505 mwl8k_cmd_802_11_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002506
2507 ieee80211_stop_queues(hw);
2508
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002509 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002510 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002511 free_irq(priv->pdev->irq, hw);
2512
2513 /* Stop finalize join worker */
2514 cancel_work_sync(&priv->finalize_join_worker);
2515 if (priv->beacon_skb != NULL)
2516 dev_kfree_skb(priv->beacon_skb);
2517
2518 /* Stop tx reclaim tasklet */
2519 tasklet_disable(&priv->tx_reclaim_task);
2520
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002521 /* Return all skbs to mac80211 */
2522 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2523 mwl8k_txq_reclaim(hw, i, 1);
2524}
2525
2526static int mwl8k_add_interface(struct ieee80211_hw *hw,
2527 struct ieee80211_if_init_conf *conf)
2528{
2529 struct mwl8k_priv *priv = hw->priv;
2530 struct mwl8k_vif *mwl8k_vif;
2531
2532 /*
2533 * We only support one active interface at a time.
2534 */
2535 if (priv->vif != NULL)
2536 return -EBUSY;
2537
2538 /*
2539 * We only support managed interfaces for now.
2540 */
Lennert Buytenhek240e86e2009-07-16 14:15:44 +02002541 if (conf->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002542 return -EINVAL;
2543
2544 /* Clean out driver private area */
2545 mwl8k_vif = MWL8K_VIF(conf->vif);
2546 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2547
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002548 /* Set and save the mac address */
2549 mwl8k_set_mac_addr(hw, conf->mac_addr);
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002550 memcpy(mwl8k_vif->mac_addr, conf->mac_addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002551
2552 /* Back pointer to parent config block */
2553 mwl8k_vif->priv = priv;
2554
2555 /* Setup initial PHY parameters */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002556 memcpy(mwl8k_vif->legacy_rates,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002557 priv->rates, sizeof(mwl8k_vif->legacy_rates));
2558 mwl8k_vif->legacy_nrates = ARRAY_SIZE(priv->rates);
2559
2560 /* Set Initial sequence number to zero */
2561 mwl8k_vif->seqno = 0;
2562
2563 priv->vif = conf->vif;
2564 priv->current_channel = NULL;
2565
2566 return 0;
2567}
2568
2569static void mwl8k_remove_interface(struct ieee80211_hw *hw,
2570 struct ieee80211_if_init_conf *conf)
2571{
2572 struct mwl8k_priv *priv = hw->priv;
2573
2574 if (priv->vif == NULL)
2575 return;
2576
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002577 mwl8k_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
2578
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002579 priv->vif = NULL;
2580}
2581
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002582static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002583{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002584 struct ieee80211_conf *conf = &hw->conf;
2585 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002586 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002587
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002588 if (conf->flags & IEEE80211_CONF_IDLE) {
2589 mwl8k_cmd_802_11_radio_disable(hw);
2590 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002591 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002592 }
2593
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002594 rc = mwl8k_fw_lock(hw);
2595 if (rc)
2596 return rc;
2597
2598 rc = mwl8k_cmd_802_11_radio_enable(hw);
2599 if (rc)
2600 goto out;
2601
2602 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2603 if (rc)
2604 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002605
2606 priv->current_channel = conf->channel;
2607
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002608 if (conf->power_level > 18)
2609 conf->power_level = 18;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002610 rc = mwl8k_cmd_802_11_rf_tx_power(hw, conf->power_level);
2611 if (rc)
2612 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002613
2614 if (mwl8k_cmd_mimo_config(hw, 0x7, 0x7))
2615 rc = -EINVAL;
2616
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002617out:
2618 mwl8k_fw_unlock(hw);
2619
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002620 return rc;
2621}
2622
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002623static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2624 struct ieee80211_vif *vif,
2625 struct ieee80211_bss_conf *info,
2626 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002627{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002628 struct mwl8k_priv *priv = hw->priv;
2629 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002630 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002631
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002632 if (changed & BSS_CHANGED_BSSID)
2633 memcpy(mwl8k_vif->bssid, info->bssid, ETH_ALEN);
2634
2635 if ((changed & BSS_CHANGED_ASSOC) == 0)
2636 return;
2637
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002638 priv->capture_beacon = false;
2639
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002640 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002641 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002642 return;
2643
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002644 if (info->assoc) {
2645 memcpy(&mwl8k_vif->bss_info, info,
2646 sizeof(struct ieee80211_bss_conf));
2647
2648 /* Install rates */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002649 rc = mwl8k_update_rateset(hw, vif);
2650 if (rc)
2651 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002652
2653 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002654 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
2655 MWL8K_UCAST_RATE, NULL);
2656 if (rc)
2657 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002658
2659 /* Set radio preamble */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002660 rc = mwl8k_set_radio_preamble(hw, info->use_short_preamble);
2661 if (rc)
2662 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002663
2664 /* Set slot time */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002665 rc = mwl8k_cmd_set_slot(hw, info->use_short_slot);
2666 if (rc)
2667 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002668
2669 /* Update peer rate info */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002670 rc = mwl8k_cmd_update_sta_db(hw, vif,
2671 MWL8K_STA_DB_MODIFY_ENTRY);
2672 if (rc)
2673 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002674
2675 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002676 rc = mwl8k_cmd_set_aid(hw, vif);
2677 if (rc)
2678 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002679
2680 /*
2681 * Finalize the join. Tell rx handler to process
2682 * next beacon from our BSSID.
2683 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002684 memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002685 priv->capture_beacon = true;
2686 } else {
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002687 rc = mwl8k_cmd_update_sta_db(hw, vif, MWL8K_STA_DB_DEL_ENTRY);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002688 memset(&mwl8k_vif->bss_info, 0,
2689 sizeof(struct ieee80211_bss_conf));
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002690 memset(mwl8k_vif->bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002691 }
2692
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002693out:
2694 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002695}
2696
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002697static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
2698 int mc_count, struct dev_addr_list *mclist)
2699{
2700 struct mwl8k_cmd_pkt *cmd;
2701
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002702 /*
2703 * Synthesize and return a command packet that programs the
2704 * hardware multicast address filter. At this point we don't
2705 * know whether FIF_ALLMULTI is being requested, but if it is,
2706 * we'll end up throwing this packet away and creating a new
2707 * one in mwl8k_configure_filter().
2708 */
2709 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002710
2711 return (unsigned long)cmd;
2712}
2713
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002714static void mwl8k_configure_filter(struct ieee80211_hw *hw,
2715 unsigned int changed_flags,
2716 unsigned int *total_flags,
2717 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002718{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002719 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002720 struct mwl8k_cmd_pkt *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002721
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002722 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002723 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002724
2725 if (mwl8k_fw_lock(hw))
2726 return;
2727
2728 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02002729 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
2730 /*
2731 * Disable the BSS filter.
2732 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002733 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02002734 } else {
Lennert Buytenheka94cc972009-08-03 21:58:57 +02002735 u8 *bssid;
2736
Lennert Buytenhek77165d82009-10-22 20:19:53 +02002737 /*
2738 * Enable the BSS filter.
2739 *
2740 * If there is an active STA interface, use that
2741 * interface's BSSID, otherwise use a dummy one
2742 * (where the OUI part needs to be nonzero for
2743 * the BSSID to be accepted by POST_SCAN).
2744 */
2745 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02002746 if (priv->vif != NULL)
2747 bssid = MWL8K_VIF(priv->vif)->bssid;
2748
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002749 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002750 }
2751 }
2752
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002753 cmd = (void *)(unsigned long)multicast;
2754
2755 /*
2756 * If FIF_ALLMULTI is being requested, throw away the command
2757 * packet that ->prepare_multicast() built and replace it with
2758 * a command packet that enables reception of all multicast
2759 * packets.
2760 */
2761 if (*total_flags & FIF_ALLMULTI) {
2762 kfree(cmd);
2763 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
2764 }
2765
2766 if (cmd != NULL) {
2767 mwl8k_post_cmd(hw, cmd);
2768 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002769 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002770
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02002771 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002772}
2773
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002774static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2775{
Lennert Buytenhek733d3062009-07-17 07:24:15 +02002776 return mwl8k_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002777}
2778
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002779static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
2780 const struct ieee80211_tx_queue_params *params)
2781{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02002782 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002783 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002784
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02002785 rc = mwl8k_fw_lock(hw);
2786 if (!rc) {
2787 if (!priv->wmm_enabled)
2788 rc = mwl8k_set_wmm(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002789
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02002790 if (!rc)
2791 rc = mwl8k_set_edca_params(hw, queue,
2792 params->cw_min,
2793 params->cw_max,
2794 params->aifs,
2795 params->txop);
2796
2797 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002798 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02002799
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002800 return rc;
2801}
2802
2803static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
2804 struct ieee80211_tx_queue_stats *stats)
2805{
2806 struct mwl8k_priv *priv = hw->priv;
2807 struct mwl8k_tx_queue *txq;
2808 int index;
2809
2810 spin_lock_bh(&priv->tx_lock);
2811 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
2812 txq = priv->txq + index;
2813 memcpy(&stats[index], &txq->tx_stats,
2814 sizeof(struct ieee80211_tx_queue_stats));
2815 }
2816 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02002817
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002818 return 0;
2819}
2820
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002821static int mwl8k_get_stats(struct ieee80211_hw *hw,
2822 struct ieee80211_low_level_stats *stats)
2823{
Lennert Buytenhek954ef502009-07-17 07:26:27 +02002824 return mwl8k_cmd_802_11_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002825}
2826
2827static const struct ieee80211_ops mwl8k_ops = {
2828 .tx = mwl8k_tx,
2829 .start = mwl8k_start,
2830 .stop = mwl8k_stop,
2831 .add_interface = mwl8k_add_interface,
2832 .remove_interface = mwl8k_remove_interface,
2833 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002834 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02002835 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002836 .configure_filter = mwl8k_configure_filter,
2837 .set_rts_threshold = mwl8k_set_rts_threshold,
2838 .conf_tx = mwl8k_conf_tx,
2839 .get_tx_stats = mwl8k_get_tx_stats,
2840 .get_stats = mwl8k_get_stats,
2841};
2842
2843static void mwl8k_tx_reclaim_handler(unsigned long data)
2844{
2845 int i;
2846 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
2847 struct mwl8k_priv *priv = hw->priv;
2848
2849 spin_lock_bh(&priv->tx_lock);
2850 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2851 mwl8k_txq_reclaim(hw, i, 0);
2852
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002853 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002854 complete(priv->tx_wait);
2855 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002856 }
2857 spin_unlock_bh(&priv->tx_lock);
2858}
2859
2860static void mwl8k_finalize_join_worker(struct work_struct *work)
2861{
2862 struct mwl8k_priv *priv =
2863 container_of(work, struct mwl8k_priv, finalize_join_worker);
2864 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002865 u8 dtim = MWL8K_VIF(priv->vif)->bss_info.dtim_period;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002866
2867 mwl8k_finalize_join(priv->hw, skb->data, skb->len, dtim);
2868 dev_kfree_skb(skb);
2869
2870 priv->beacon_skb = NULL;
2871}
2872
2873static int __devinit mwl8k_probe(struct pci_dev *pdev,
2874 const struct pci_device_id *id)
2875{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02002876 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002877 struct ieee80211_hw *hw;
2878 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002879 int rc;
2880 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02002881
2882 if (!printed_version) {
2883 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
2884 printed_version = 1;
2885 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002886
2887 rc = pci_enable_device(pdev);
2888 if (rc) {
2889 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
2890 MWL8K_NAME);
2891 return rc;
2892 }
2893
2894 rc = pci_request_regions(pdev, MWL8K_NAME);
2895 if (rc) {
2896 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
2897 MWL8K_NAME);
2898 return rc;
2899 }
2900
2901 pci_set_master(pdev);
2902
2903 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
2904 if (hw == NULL) {
2905 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
2906 rc = -ENOMEM;
2907 goto err_free_reg;
2908 }
2909
2910 priv = hw->priv;
2911 priv->hw = hw;
2912 priv->pdev = pdev;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +02002913 priv->wmm_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002914 priv->pending_tx_pkts = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002915
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002916 SET_IEEE80211_DEV(hw, &pdev->dev);
2917 pci_set_drvdata(pdev, hw);
2918
2919 priv->regs = pci_iomap(pdev, 1, 0x10000);
2920 if (priv->regs == NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002921 printk(KERN_ERR "%s: Cannot map device memory\n",
2922 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002923 goto err_iounmap;
2924 }
2925
2926 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
2927 priv->band.band = IEEE80211_BAND_2GHZ;
2928 priv->band.channels = priv->channels;
2929 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
2930 priv->band.bitrates = priv->rates;
2931 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
2932 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
2933
2934 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
2935 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
2936
2937 /*
2938 * Extra headroom is the size of the required DMA header
2939 * minus the size of the smallest 802.11 frame (CTS frame).
2940 */
2941 hw->extra_tx_headroom =
2942 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
2943
2944 hw->channel_change_time = 10;
2945
2946 hw->queues = MWL8K_TX_QUEUES;
2947
Lennert Buytenhek240e86e2009-07-16 14:15:44 +02002948 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002949
2950 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002951 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002952 hw->vif_data_size = sizeof(struct mwl8k_vif);
2953 priv->vif = NULL;
2954
2955 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002956 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002957 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002958
2959 /* Finalize join worker */
2960 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
2961
2962 /* TX reclaim tasklet */
2963 tasklet_init(&priv->tx_reclaim_task,
2964 mwl8k_tx_reclaim_handler, (unsigned long)hw);
2965 tasklet_disable(&priv->tx_reclaim_task);
2966
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002967 /* Power management cookie */
2968 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
2969 if (priv->cookie == NULL)
2970 goto err_iounmap;
2971
2972 rc = mwl8k_rxq_init(hw, 0);
2973 if (rc)
2974 goto err_iounmap;
2975 rxq_refill(hw, 0, INT_MAX);
2976
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002977 mutex_init(&priv->fw_mutex);
2978 priv->fw_mutex_owner = NULL;
2979 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002980 priv->hostcmd_wait = NULL;
2981
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002982 spin_lock_init(&priv->tx_lock);
2983
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002984 priv->tx_wait = NULL;
2985
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002986 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
2987 rc = mwl8k_txq_init(hw, i);
2988 if (rc)
2989 goto err_free_queues;
2990 }
2991
2992 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002993 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002994 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
2995 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
2996
2997 rc = request_irq(priv->pdev->irq, &mwl8k_interrupt,
2998 IRQF_SHARED, MWL8K_NAME, hw);
2999 if (rc) {
3000 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003001 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003002 goto err_free_queues;
3003 }
3004
3005 /* Reset firmware and hardware */
3006 mwl8k_hw_reset(priv);
3007
3008 /* Ask userland hotplug daemon for the device firmware */
3009 rc = mwl8k_request_firmware(priv, (u32)id->driver_data);
3010 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003011 printk(KERN_ERR "%s: Firmware files not found\n",
3012 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003013 goto err_free_irq;
3014 }
3015
3016 /* Load firmware into hardware */
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003017 rc = mwl8k_load_firmware(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003018 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003019 printk(KERN_ERR "%s: Cannot start firmware\n",
3020 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003021 goto err_stop_firmware;
3022 }
3023
3024 /* Reclaim memory once firmware is successfully loaded */
3025 mwl8k_release_firmware(priv);
3026
3027 /*
3028 * Temporarily enable interrupts. Initial firmware host
3029 * commands use interrupts and avoids polling. Disable
3030 * interrupts when done.
3031 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003032 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003033
3034 /* Get config data, mac addrs etc */
3035 rc = mwl8k_cmd_get_hw_spec(hw);
3036 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003037 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3038 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003039 goto err_stop_firmware;
3040 }
3041
3042 /* Turn radio off */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003043 rc = mwl8k_cmd_802_11_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003044 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003045 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003046 goto err_stop_firmware;
3047 }
3048
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003049 /* Clear MAC address */
3050 rc = mwl8k_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
3051 if (rc) {
3052 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3053 wiphy_name(hw->wiphy));
3054 goto err_stop_firmware;
3055 }
3056
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003057 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003058 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003059 free_irq(priv->pdev->irq, hw);
3060
3061 rc = ieee80211_register_hw(hw);
3062 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003063 printk(KERN_ERR "%s: Cannot register device\n",
3064 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003065 goto err_stop_firmware;
3066 }
3067
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003068 printk(KERN_INFO "%s: 88w%u v%d, %pM, firmware version %u.%u.%u.%u\n",
3069 wiphy_name(hw->wiphy), priv->part_num, priv->hw_rev,
3070 hw->wiphy->perm_addr,
3071 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3072 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003073
3074 return 0;
3075
3076err_stop_firmware:
3077 mwl8k_hw_reset(priv);
3078 mwl8k_release_firmware(priv);
3079
3080err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003081 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003082 free_irq(priv->pdev->irq, hw);
3083
3084err_free_queues:
3085 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3086 mwl8k_txq_deinit(hw, i);
3087 mwl8k_rxq_deinit(hw, 0);
3088
3089err_iounmap:
3090 if (priv->cookie != NULL)
3091 pci_free_consistent(priv->pdev, 4,
3092 priv->cookie, priv->cookie_dma);
3093
3094 if (priv->regs != NULL)
3095 pci_iounmap(pdev, priv->regs);
3096
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003097 pci_set_drvdata(pdev, NULL);
3098 ieee80211_free_hw(hw);
3099
3100err_free_reg:
3101 pci_release_regions(pdev);
3102 pci_disable_device(pdev);
3103
3104 return rc;
3105}
3106
Joerg Albert230f7af2009-04-18 02:10:45 +02003107static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003108{
3109 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3110}
3111
Joerg Albert230f7af2009-04-18 02:10:45 +02003112static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003113{
3114 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3115 struct mwl8k_priv *priv;
3116 int i;
3117
3118 if (hw == NULL)
3119 return;
3120 priv = hw->priv;
3121
3122 ieee80211_stop_queues(hw);
3123
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003124 ieee80211_unregister_hw(hw);
3125
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003126 /* Remove tx reclaim tasklet */
3127 tasklet_kill(&priv->tx_reclaim_task);
3128
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003129 /* Stop hardware */
3130 mwl8k_hw_reset(priv);
3131
3132 /* Return all skbs to mac80211 */
3133 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3134 mwl8k_txq_reclaim(hw, i, 1);
3135
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003136 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3137 mwl8k_txq_deinit(hw, i);
3138
3139 mwl8k_rxq_deinit(hw, 0);
3140
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003141 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003142
3143 pci_iounmap(pdev, priv->regs);
3144 pci_set_drvdata(pdev, NULL);
3145 ieee80211_free_hw(hw);
3146 pci_release_regions(pdev);
3147 pci_disable_device(pdev);
3148}
3149
3150static struct pci_driver mwl8k_driver = {
3151 .name = MWL8K_NAME,
3152 .id_table = mwl8k_table,
3153 .probe = mwl8k_probe,
3154 .remove = __devexit_p(mwl8k_remove),
3155 .shutdown = __devexit_p(mwl8k_shutdown),
3156};
3157
3158static int __init mwl8k_init(void)
3159{
3160 return pci_register_driver(&mwl8k_driver);
3161}
3162
3163static void __exit mwl8k_exit(void)
3164{
3165 pci_unregister_driver(&mwl8k_driver);
3166}
3167
3168module_init(mwl8k_init);
3169module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003170
3171MODULE_DESCRIPTION(MWL8K_DESC);
3172MODULE_VERSION(MWL8K_VERSION);
3173MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3174MODULE_LICENSE("GPL");