blob: 2ae6c363f374d336d6d9022641ee096a39242043 [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 Buytenhek6976b662010-01-02 10:31:50 +010029#define MWL8K_VERSION "0.11"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010030
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031/* Register definitions */
32#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020033#define MWL8K_MODE_STA 0x0000005a
34#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010035#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020036#define MWL8K_FWSTA_READY 0xf0f1f2f4
37#define MWL8K_FWAP_READY 0xf1f2f4a5
38#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010039#define MWL8K_HIU_SCRATCH 0x00000c40
40
41/* Host->device communications */
42#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
43#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
44#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
45#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
46#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020047#define MWL8K_H2A_INT_DUMMY (1 << 20)
48#define MWL8K_H2A_INT_RESET (1 << 15)
49#define MWL8K_H2A_INT_DOORBELL (1 << 1)
50#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010051
52/* Device->host communications */
53#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
54#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
55#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
56#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
57#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020058#define MWL8K_A2H_INT_DUMMY (1 << 20)
59#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
60#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
61#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
62#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
63#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
64#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
65#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
66#define MWL8K_A2H_INT_RX_READY (1 << 1)
67#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010068
69#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
70 MWL8K_A2H_INT_CHNL_SWITCHED | \
71 MWL8K_A2H_INT_QUEUE_EMPTY | \
72 MWL8K_A2H_INT_RADAR_DETECT | \
73 MWL8K_A2H_INT_RADIO_ON | \
74 MWL8K_A2H_INT_RADIO_OFF | \
75 MWL8K_A2H_INT_MAC_EVENT | \
76 MWL8K_A2H_INT_OPC_DONE | \
77 MWL8K_A2H_INT_RX_READY | \
78 MWL8K_A2H_INT_TX_DONE)
79
Lennert Buytenheka66098d2009-03-10 10:13:33 +010080#define MWL8K_RX_QUEUES 1
81#define MWL8K_TX_QUEUES 4
82
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020083struct rxd_ops {
84 int rxd_size;
85 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
86 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010087 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
88 __le16 *qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020089};
90
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020091struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020092 char *part_name;
93 char *helper_image;
94 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010095 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020096};
97
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +020099 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100100
101 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200102 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
104 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200105 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100106
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200107 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200109 struct {
110 struct sk_buff *skb;
111 DECLARE_PCI_UNMAP_ADDR(dma)
112 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100113};
114
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115struct mwl8k_tx_queue {
116 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100118
119 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200120 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100121
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200122 struct ieee80211_tx_queue_stats stats;
123 struct mwl8k_tx_desc *txd;
124 dma_addr_t txd_dma;
125 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100126};
127
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100128struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200132 struct mwl8k_device_info *device_info;
133
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100134 void __iomem *sram;
135 void __iomem *regs;
136
137 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100138 struct firmware *fw_helper;
139 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100141 /* hardware/firmware parameters */
142 bool ap_fw;
143 struct rxd_ops *rxd_ops;
144
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200145 /* firmware access */
146 struct mutex fw_mutex;
147 struct task_struct *fw_mutex_owner;
148 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200149 struct completion *hostcmd_wait;
150
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100151 /* lock held over TX and TX reap */
152 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100153
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200154 /* TX quiesce completion, protected by fw_mutex and tx_lock */
155 struct completion *tx_wait;
156
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 struct ieee80211_vif *vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158
159 struct ieee80211_channel *current_channel;
160
161 /* power management status cookie from firmware */
162 u32 *cookie;
163 dma_addr_t cookie_dma;
164
165 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100166 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200167 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168
169 /*
170 * Running count of TX packets in flight, to avoid
171 * iterating over the transmit rings each time.
172 */
173 int pending_tx_pkts;
174
175 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
176 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
177
178 /* PHY parameters */
179 struct ieee80211_supported_band band;
180 struct ieee80211_channel channels[14];
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100181 struct ieee80211_rate rates[14];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100182
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200183 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200184 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200185 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200186 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100187
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +0100188 struct work_struct sta_notify_worker;
189 spinlock_t sta_notify_list_lock;
190 struct list_head sta_notify_list;
191
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100192 /* XXX need to convert this to handle multiple interfaces */
193 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200194 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 struct sk_buff *beacon_skb;
196
197 /*
198 * This FJ worker has to be global as it is scheduled from the
199 * RX handler. At this point we don't know which interface it
200 * belongs to until the list of bssids waiting to complete join
201 * is checked.
202 */
203 struct work_struct finalize_join_worker;
204
205 /* Tasklet to reclaim TX descriptors and buffers after tx */
206 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100207};
208
209/* Per interface specific private data */
210struct mwl8k_vif {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +0100211 /* Local MAC address. */
212 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100213
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100214 /* Non AMPDU sequence number assigned by driver */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100215 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100216};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200217#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218
Lennert Buytenheka6804002010-01-04 21:55:42 +0100219struct mwl8k_sta {
220 /* Index into station database. Returned by UPDATE_STADB. */
221 u8 peer_id;
222};
223#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
224
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225static 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, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100237 { .center_freq = 2467, .hw_value = 12, },
238 { .center_freq = 2472, .hw_value = 13, },
239 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100240};
241
242static const struct ieee80211_rate mwl8k_rates[] = {
243 { .bitrate = 10, .hw_value = 2, },
244 { .bitrate = 20, .hw_value = 4, },
245 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200246 { .bitrate = 110, .hw_value = 22, },
247 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100248 { .bitrate = 60, .hw_value = 12, },
249 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100250 { .bitrate = 120, .hw_value = 24, },
251 { .bitrate = 180, .hw_value = 36, },
252 { .bitrate = 240, .hw_value = 48, },
253 { .bitrate = 360, .hw_value = 72, },
254 { .bitrate = 480, .hw_value = 96, },
255 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100256 { .bitrate = 720, .hw_value = 144, },
257};
258
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100259/* Set or get info from Firmware */
260#define MWL8K_CMD_SET 0x0001
261#define MWL8K_CMD_GET 0x0000
262
263/* Firmware command codes */
264#define MWL8K_CMD_CODE_DNLD 0x0001
265#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200266#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100267#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
268#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200269#define MWL8K_CMD_RADIO_CONTROL 0x001c
270#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200271#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100272#define MWL8K_CMD_SET_PRE_SCAN 0x0107
273#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200274#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100275#define MWL8K_CMD_SET_AID 0x010d
276#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200277#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100278#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200279#define MWL8K_CMD_SET_SLOT 0x0114
280#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
281#define MWL8K_CMD_SET_WMM_MODE 0x0123
282#define MWL8K_CMD_MIMO_CONFIG 0x0125
283#define MWL8K_CMD_USE_FIXED_RATE 0x0126
284#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200285#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200286#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
287#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100288
289static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
290{
291#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
292 snprintf(buf, bufsize, "%s", #x);\
293 return buf;\
294 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200295 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100296 MWL8K_CMDNAME(CODE_DNLD);
297 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200298 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100299 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
300 MWL8K_CMDNAME(GET_STAT);
301 MWL8K_CMDNAME(RADIO_CONTROL);
302 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200303 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100304 MWL8K_CMDNAME(SET_PRE_SCAN);
305 MWL8K_CMDNAME(SET_POST_SCAN);
306 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100307 MWL8K_CMDNAME(SET_AID);
308 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200309 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100310 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200311 MWL8K_CMDNAME(SET_SLOT);
312 MWL8K_CMDNAME(SET_EDCA_PARAMS);
313 MWL8K_CMDNAME(SET_WMM_MODE);
314 MWL8K_CMDNAME(MIMO_CONFIG);
315 MWL8K_CMDNAME(USE_FIXED_RATE);
316 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200317 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200318 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
319 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100320 default:
321 snprintf(buf, bufsize, "0x%x", cmd);
322 }
323#undef MWL8K_CMDNAME
324
325 return buf;
326}
327
328/* Hardware and firmware reset */
329static void mwl8k_hw_reset(struct mwl8k_priv *priv)
330{
331 iowrite32(MWL8K_H2A_INT_RESET,
332 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
333 iowrite32(MWL8K_H2A_INT_RESET,
334 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
335 msleep(20);
336}
337
338/* Release fw image */
339static void mwl8k_release_fw(struct firmware **fw)
340{
341 if (*fw == NULL)
342 return;
343 release_firmware(*fw);
344 *fw = NULL;
345}
346
347static void mwl8k_release_firmware(struct mwl8k_priv *priv)
348{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100349 mwl8k_release_fw(&priv->fw_ucode);
350 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100351}
352
353/* Request fw image */
354static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200355 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100356{
357 /* release current image */
358 if (*fw != NULL)
359 mwl8k_release_fw(fw);
360
361 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200362 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100363}
364
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200365static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100366{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200367 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100368 int rc;
369
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200370 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100371 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200372 if (rc) {
373 printk(KERN_ERR "%s: Error requesting helper "
374 "firmware file %s\n", pci_name(priv->pdev),
375 di->helper_image);
376 return rc;
377 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100378 }
379
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100380 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100381 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200382 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200383 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100384 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100385 return rc;
386 }
387
388 return 0;
389}
390
Ben Hutchings7e75b942009-11-07 22:00:57 +0000391MODULE_FIRMWARE("mwl8k/helper_8687.fw");
392MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
393
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100394struct mwl8k_cmd_pkt {
395 __le16 code;
396 __le16 length;
397 __le16 seq_num;
398 __le16 result;
399 char payload[0];
400} __attribute__((packed));
401
402/*
403 * Firmware loading.
404 */
405static int
406mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
407{
408 void __iomem *regs = priv->regs;
409 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100410 int loops;
411
412 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
413 if (pci_dma_mapping_error(priv->pdev, dma_addr))
414 return -ENOMEM;
415
416 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
417 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
418 iowrite32(MWL8K_H2A_INT_DOORBELL,
419 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
420 iowrite32(MWL8K_H2A_INT_DUMMY,
421 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
422
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100423 loops = 1000;
424 do {
425 u32 int_code;
426
427 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
428 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
429 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100430 break;
431 }
432
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200433 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100434 udelay(1);
435 } while (--loops);
436
437 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
438
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200439 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100440}
441
442static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
443 const u8 *data, size_t length)
444{
445 struct mwl8k_cmd_pkt *cmd;
446 int done;
447 int rc = 0;
448
449 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
450 if (cmd == NULL)
451 return -ENOMEM;
452
453 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
454 cmd->seq_num = 0;
455 cmd->result = 0;
456
457 done = 0;
458 while (length) {
459 int block_size = length > 256 ? 256 : length;
460
461 memcpy(cmd->payload, data + done, block_size);
462 cmd->length = cpu_to_le16(block_size);
463
464 rc = mwl8k_send_fw_load_cmd(priv, cmd,
465 sizeof(*cmd) + block_size);
466 if (rc)
467 break;
468
469 done += block_size;
470 length -= block_size;
471 }
472
473 if (!rc) {
474 cmd->length = 0;
475 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
476 }
477
478 kfree(cmd);
479
480 return rc;
481}
482
483static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
484 const u8 *data, size_t length)
485{
486 unsigned char *buffer;
487 int may_continue, rc = 0;
488 u32 done, prev_block_size;
489
490 buffer = kmalloc(1024, GFP_KERNEL);
491 if (buffer == NULL)
492 return -ENOMEM;
493
494 done = 0;
495 prev_block_size = 0;
496 may_continue = 1000;
497 while (may_continue > 0) {
498 u32 block_size;
499
500 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
501 if (block_size & 1) {
502 block_size &= ~1;
503 may_continue--;
504 } else {
505 done += prev_block_size;
506 length -= prev_block_size;
507 }
508
509 if (block_size > 1024 || block_size > length) {
510 rc = -EOVERFLOW;
511 break;
512 }
513
514 if (length == 0) {
515 rc = 0;
516 break;
517 }
518
519 if (block_size == 0) {
520 rc = -EPROTO;
521 may_continue--;
522 udelay(1);
523 continue;
524 }
525
526 prev_block_size = block_size;
527 memcpy(buffer, data + done, block_size);
528
529 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
530 if (rc)
531 break;
532 }
533
534 if (!rc && length != 0)
535 rc = -EREMOTEIO;
536
537 kfree(buffer);
538
539 return rc;
540}
541
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200542static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100543{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200544 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100545 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200546 int rc;
547 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100548
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200549 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100550 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100551
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200552 if (helper == NULL) {
553 printk(KERN_ERR "%s: helper image needed but none "
554 "given\n", pci_name(priv->pdev));
555 return -EINVAL;
556 }
557
558 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100559 if (rc) {
560 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200561 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100562 return rc;
563 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100564 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100565
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200566 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100567 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200568 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100569 }
570
571 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200572 printk(KERN_ERR "%s: unable to load firmware image\n",
573 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100574 return rc;
575 }
576
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100577 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100578
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100579 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100580 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200581 u32 ready_code;
582
583 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
584 if (ready_code == MWL8K_FWAP_READY) {
585 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100586 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200587 } else if (ready_code == MWL8K_FWSTA_READY) {
588 priv->ap_fw = 0;
589 break;
590 }
591
592 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100593 udelay(1);
594 } while (--loops);
595
596 return loops ? 0 : -ETIMEDOUT;
597}
598
599
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100600/* DMA header used by firmware and hardware. */
601struct mwl8k_dma_data {
602 __le16 fwlen;
603 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100604 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100605} __attribute__((packed));
606
607/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100608static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100609{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100610 struct mwl8k_dma_data *tr;
611 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100612
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100613 tr = (struct mwl8k_dma_data *)skb->data;
614 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
615
616 if (hdrlen != sizeof(tr->wh)) {
617 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
618 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
619 *((__le16 *)(tr->data - 2)) = qos;
620 } else {
621 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
622 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100623 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100624
625 if (hdrlen != sizeof(*tr))
626 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100627}
628
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200629static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100630{
631 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100632 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100633 struct mwl8k_dma_data *tr;
634
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100635 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100636 * Add a firmware DMA header; the firmware requires that we
637 * present a 2-byte payload length followed by a 4-address
638 * header (without QoS field), followed (optionally) by any
639 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100640 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100641 wh = (struct ieee80211_hdr *)skb->data;
642
643 hdrlen = ieee80211_hdrlen(wh->frame_control);
644 if (hdrlen != sizeof(*tr))
645 skb_push(skb, sizeof(*tr) - hdrlen);
646
647 if (ieee80211_is_data_qos(wh->frame_control))
648 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100649
650 tr = (struct mwl8k_dma_data *)skb->data;
651 if (wh != &tr->wh)
652 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100653 if (hdrlen != sizeof(tr->wh))
654 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100655
656 /*
657 * Firmware length is the length of the fully formed "802.11
658 * payload". That is, everything except for the 802.11 header.
659 * This includes all crypto material including the MIC.
660 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100661 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100662}
663
664
665/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100666 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200667 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100668struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200669 __le16 pkt_len;
670 __u8 sq2;
671 __u8 rate;
672 __le32 pkt_phys_addr;
673 __le32 next_rxd_phys_addr;
674 __le16 qos_control;
675 __le16 htsig2;
676 __le32 hw_rssi_info;
677 __le32 hw_noise_floor_info;
678 __u8 noise_floor;
679 __u8 pad0[3];
680 __u8 rssi;
681 __u8 rx_status;
682 __u8 channel;
683 __u8 rx_ctrl;
684} __attribute__((packed));
685
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100686#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
687#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
688#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100689
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100690#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200691
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100692static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200693{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100694 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200695
696 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100697 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200698}
699
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100700static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200701{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100702 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200703
704 rxd->pkt_len = cpu_to_le16(len);
705 rxd->pkt_phys_addr = cpu_to_le32(addr);
706 wmb();
707 rxd->rx_ctrl = 0;
708}
709
710static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100711mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
712 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200713{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100714 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200715
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100716 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200717 return -1;
718 rmb();
719
720 memset(status, 0, sizeof(*status));
721
722 status->signal = -rxd->rssi;
723 status->noise = -rxd->noise_floor;
724
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100725 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200726 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100727 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100728 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100729 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200730 } else {
731 int i;
732
733 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
734 if (mwl8k_rates[i].hw_value == rxd->rate) {
735 status->rate_idx = i;
736 break;
737 }
738 }
739 }
740
741 status->band = IEEE80211_BAND_2GHZ;
742 status->freq = ieee80211_channel_to_frequency(rxd->channel);
743
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100744 *qos = rxd->qos_control;
745
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200746 return le16_to_cpu(rxd->pkt_len);
747}
748
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100749static struct rxd_ops rxd_8366_ap_ops = {
750 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
751 .rxd_init = mwl8k_rxd_8366_ap_init,
752 .rxd_refill = mwl8k_rxd_8366_ap_refill,
753 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200754};
755
756/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100757 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100758 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100759struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100760 __le16 pkt_len;
761 __u8 link_quality;
762 __u8 noise_level;
763 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200764 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100765 __le16 qos_control;
766 __le16 rate_info;
767 __le32 pad0[4];
768 __u8 rssi;
769 __u8 channel;
770 __le16 pad1;
771 __u8 rx_ctrl;
772 __u8 rx_status;
773 __u8 pad2[2];
774} __attribute__((packed));
775
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100776#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
777#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
778#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
779#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
780#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
781#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200782
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100783#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200784
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100785static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200786{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100787 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200788
789 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100790 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200791}
792
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100793static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200794{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100795 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200796
797 rxd->pkt_len = cpu_to_le16(len);
798 rxd->pkt_phys_addr = cpu_to_le32(addr);
799 wmb();
800 rxd->rx_ctrl = 0;
801}
802
803static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100804mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100805 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200806{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100807 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200808 u16 rate_info;
809
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100810 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200811 return -1;
812 rmb();
813
814 rate_info = le16_to_cpu(rxd->rate_info);
815
816 memset(status, 0, sizeof(*status));
817
818 status->signal = -rxd->rssi;
819 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100820 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
821 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200822
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100823 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200824 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100825 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200826 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100827 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200828 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100829 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200830 status->flag |= RX_FLAG_HT;
831
832 status->band = IEEE80211_BAND_2GHZ;
833 status->freq = ieee80211_channel_to_frequency(rxd->channel);
834
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100835 *qos = rxd->qos_control;
836
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200837 return le16_to_cpu(rxd->pkt_len);
838}
839
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100840static struct rxd_ops rxd_sta_ops = {
841 .rxd_size = sizeof(struct mwl8k_rxd_sta),
842 .rxd_init = mwl8k_rxd_sta_init,
843 .rxd_refill = mwl8k_rxd_sta_refill,
844 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200845};
846
847
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100848#define MWL8K_RX_DESCS 256
849#define MWL8K_RX_MAXSZ 3800
850
851static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
852{
853 struct mwl8k_priv *priv = hw->priv;
854 struct mwl8k_rx_queue *rxq = priv->rxq + index;
855 int size;
856 int i;
857
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200858 rxq->rxd_count = 0;
859 rxq->head = 0;
860 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100861
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200862 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100863
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200864 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
865 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100866 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200867 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100868 return -ENOMEM;
869 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200870 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100871
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200872 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
873 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100874 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200875 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200876 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100877 return -ENOMEM;
878 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200879 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100880
881 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200882 int desc_size;
883 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100884 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200885 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100886
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200887 desc_size = priv->rxd_ops->rxd_size;
888 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100889
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200890 nexti = i + 1;
891 if (nexti == MWL8K_RX_DESCS)
892 nexti = 0;
893 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
894
895 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100896 }
897
898 return 0;
899}
900
901static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
902{
903 struct mwl8k_priv *priv = hw->priv;
904 struct mwl8k_rx_queue *rxq = priv->rxq + index;
905 int refilled;
906
907 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200908 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100909 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200910 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100911 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200912 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100913
914 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
915 if (skb == NULL)
916 break;
917
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200918 addr = pci_map_single(priv->pdev, skb->data,
919 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100920
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200921 rxq->rxd_count++;
922 rx = rxq->tail++;
923 if (rxq->tail == MWL8K_RX_DESCS)
924 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200925 rxq->buf[rx].skb = skb;
926 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200927
928 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
929 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100930
931 refilled++;
932 }
933
934 return refilled;
935}
936
937/* Must be called only when the card's reception is completely halted */
938static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
939{
940 struct mwl8k_priv *priv = hw->priv;
941 struct mwl8k_rx_queue *rxq = priv->rxq + index;
942 int i;
943
944 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200945 if (rxq->buf[i].skb != NULL) {
946 pci_unmap_single(priv->pdev,
947 pci_unmap_addr(&rxq->buf[i], dma),
948 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
949 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100950
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200951 kfree_skb(rxq->buf[i].skb);
952 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100953 }
954 }
955
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200956 kfree(rxq->buf);
957 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100958
959 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200960 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200961 rxq->rxd, rxq->rxd_dma);
962 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100963}
964
965
966/*
967 * Scan a list of BSSIDs to process for finalize join.
968 * Allows for extension to process multiple BSSIDs.
969 */
970static inline int
971mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
972{
973 return priv->capture_beacon &&
974 ieee80211_is_beacon(wh->frame_control) &&
975 !compare_ether_addr(wh->addr3, priv->capture_bssid);
976}
977
Lennert Buytenhek37797522009-10-22 20:19:45 +0200978static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
979 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100980{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200981 struct mwl8k_priv *priv = hw->priv;
982
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100983 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200984 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100985
986 /*
987 * Use GFP_ATOMIC as rxq_process is called from
988 * the primary interrupt handler, memory allocation call
989 * must not sleep.
990 */
991 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
992 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200993 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100994}
995
996static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
997{
998 struct mwl8k_priv *priv = hw->priv;
999 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1000 int processed;
1001
1002 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001003 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001004 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001005 void *rxd;
1006 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001007 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001008 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001009
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001010 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001011 if (skb == NULL)
1012 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001013
1014 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1015
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001016 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001017 if (pkt_len < 0)
1018 break;
1019
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001020 rxq->buf[rxq->head].skb = NULL;
1021
1022 pci_unmap_single(priv->pdev,
1023 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1024 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1025 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001026
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001027 rxq->head++;
1028 if (rxq->head == MWL8K_RX_DESCS)
1029 rxq->head = 0;
1030
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001031 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001032
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001033 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001034 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001035
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001036 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001037 * Check for a pending join operation. Save a
1038 * copy of the beacon and schedule a tasklet to
1039 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001040 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001041 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001042 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001043
Johannes Bergf1d58c22009-06-17 13:13:00 +02001044 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1045 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001046
1047 processed++;
1048 }
1049
1050 return processed;
1051}
1052
1053
1054/*
1055 * Packet transmission.
1056 */
1057
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001058#define MWL8K_TXD_STATUS_OK 0x00000001
1059#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1060#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1061#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001062#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001063
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001064#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1065#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1066#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1067#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1068#define MWL8K_QOS_EOSP 0x0010
1069
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001070struct mwl8k_tx_desc {
1071 __le32 status;
1072 __u8 data_rate;
1073 __u8 tx_priority;
1074 __le16 qos_control;
1075 __le32 pkt_phys_addr;
1076 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001077 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001078 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001079 __le32 reserved;
1080 __le16 rate_info;
1081 __u8 peer_id;
1082 __u8 tx_frag_cnt;
1083} __attribute__((packed));
1084
1085#define MWL8K_TX_DESCS 128
1086
1087static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1088{
1089 struct mwl8k_priv *priv = hw->priv;
1090 struct mwl8k_tx_queue *txq = priv->txq + index;
1091 int size;
1092 int i;
1093
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001094 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1095 txq->stats.limit = MWL8K_TX_DESCS;
1096 txq->head = 0;
1097 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001098
1099 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1100
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001101 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1102 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001103 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001104 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001105 return -ENOMEM;
1106 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001107 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001108
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001109 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1110 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001111 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001112 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001113 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001114 return -ENOMEM;
1115 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001116 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001117
1118 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1119 struct mwl8k_tx_desc *tx_desc;
1120 int nexti;
1121
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001122 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001123 nexti = (i + 1) % MWL8K_TX_DESCS;
1124
1125 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001126 tx_desc->next_txd_phys_addr =
1127 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001128 }
1129
1130 return 0;
1131}
1132
1133static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1134{
1135 iowrite32(MWL8K_H2A_INT_PPA_READY,
1136 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1137 iowrite32(MWL8K_H2A_INT_DUMMY,
1138 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1139 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1140}
1141
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001142static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001143{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001144 struct mwl8k_priv *priv = hw->priv;
1145 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001146
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001147 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1148 struct mwl8k_tx_queue *txq = priv->txq + i;
1149 int fw_owned = 0;
1150 int drv_owned = 0;
1151 int unused = 0;
1152 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001153
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001154 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001155 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1156 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001157
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001158 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001160 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001161 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001162 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163
1164 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001165 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001166 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001168 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1169 "fw_owned=%d drv_owned=%d unused=%d\n",
1170 wiphy_name(hw->wiphy), i,
1171 txq->stats.len, txq->head, txq->tail,
1172 fw_owned, drv_owned, unused);
1173 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001174}
1175
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001176/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001177 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001178 */
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001179#define MWL8K_TX_WAIT_TIMEOUT_MS 1000
1180
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001181static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001182{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001183 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001184 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001185 int retry;
1186 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001187
1188 might_sleep();
1189
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001190 /*
1191 * The TX queues are stopped at this point, so this test
1192 * doesn't need to take ->tx_lock.
1193 */
1194 if (!priv->pending_tx_pkts)
1195 return 0;
1196
1197 retry = 0;
1198 rc = 0;
1199
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001200 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001201 priv->tx_wait = &tx_wait;
1202 while (!rc) {
1203 int oldcount;
1204 unsigned long timeout;
1205
1206 oldcount = priv->pending_tx_pkts;
1207
1208 spin_unlock_bh(&priv->tx_lock);
1209 timeout = wait_for_completion_timeout(&tx_wait,
1210 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1211 spin_lock_bh(&priv->tx_lock);
1212
1213 if (timeout) {
1214 WARN_ON(priv->pending_tx_pkts);
1215 if (retry) {
1216 printk(KERN_NOTICE "%s: tx rings drained\n",
1217 wiphy_name(hw->wiphy));
1218 }
1219 break;
1220 }
1221
1222 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001223 printk(KERN_NOTICE "%s: waiting for tx rings "
1224 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001225 wiphy_name(hw->wiphy), oldcount,
1226 priv->pending_tx_pkts);
1227 retry = 1;
1228 continue;
1229 }
1230
1231 priv->tx_wait = NULL;
1232
1233 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1234 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1235 mwl8k_dump_tx_rings(hw);
1236
1237 rc = -ETIMEDOUT;
1238 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001239 spin_unlock_bh(&priv->tx_lock);
1240
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001241 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001242}
1243
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001244#define MWL8K_TXD_SUCCESS(status) \
1245 ((status) & (MWL8K_TXD_STATUS_OK | \
1246 MWL8K_TXD_STATUS_OK_RETRY | \
1247 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001248
1249static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1250{
1251 struct mwl8k_priv *priv = hw->priv;
1252 struct mwl8k_tx_queue *txq = priv->txq + index;
1253 int wake = 0;
1254
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001255 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001256 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001257 struct mwl8k_tx_desc *tx_desc;
1258 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001259 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001260 struct sk_buff *skb;
1261 struct ieee80211_tx_info *info;
1262 u32 status;
1263
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001264 tx = txq->head;
1265 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001266
1267 status = le32_to_cpu(tx_desc->status);
1268
1269 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1270 if (!force)
1271 break;
1272 tx_desc->status &=
1273 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1274 }
1275
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001276 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1277 BUG_ON(txq->stats.len == 0);
1278 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001279 priv->pending_tx_pkts--;
1280
1281 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001282 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001283 skb = txq->skb[tx];
1284 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001285
1286 BUG_ON(skb == NULL);
1287 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1288
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001289 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001290
1291 /* Mark descriptor as unused */
1292 tx_desc->pkt_phys_addr = 0;
1293 tx_desc->pkt_len = 0;
1294
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001295 info = IEEE80211_SKB_CB(skb);
1296 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001297 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001298 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299
1300 ieee80211_tx_status_irqsafe(hw, skb);
1301
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001302 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303 }
1304
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001305 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001306 ieee80211_wake_queue(hw, index);
1307}
1308
1309/* must be called only when the card's transmit is completely halted */
1310static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1311{
1312 struct mwl8k_priv *priv = hw->priv;
1313 struct mwl8k_tx_queue *txq = priv->txq + index;
1314
1315 mwl8k_txq_reclaim(hw, index, 1);
1316
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001317 kfree(txq->skb);
1318 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001319
1320 pci_free_consistent(priv->pdev,
1321 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001322 txq->txd, txq->txd_dma);
1323 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001324}
1325
1326static int
1327mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1328{
1329 struct mwl8k_priv *priv = hw->priv;
1330 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001331 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001332 struct ieee80211_hdr *wh;
1333 struct mwl8k_tx_queue *txq;
1334 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001335 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001336 u32 txstatus;
1337 u8 txdatarate;
1338 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001339
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001340 wh = (struct ieee80211_hdr *)skb->data;
1341 if (ieee80211_is_data_qos(wh->frame_control))
1342 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1343 else
1344 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001346 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001347 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001348
1349 tx_info = IEEE80211_SKB_CB(skb);
1350 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001351
1352 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1353 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001354
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001355 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1356 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1357 mwl8k_vif->seqno = seqno++ % 4096;
1358 }
1359
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001360 /* Setup firmware control bit fields for each frame type. */
1361 txstatus = 0;
1362 txdatarate = 0;
1363 if (ieee80211_is_mgmt(wh->frame_control) ||
1364 ieee80211_is_ctl(wh->frame_control)) {
1365 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001366 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001367 } else if (ieee80211_is_data(wh->frame_control)) {
1368 txdatarate = 1;
1369 if (is_multicast_ether_addr(wh->addr1))
1370 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1371
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001372 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001373 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001374 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001375 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001376 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001377 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001378
1379 dma = pci_map_single(priv->pdev, skb->data,
1380 skb->len, PCI_DMA_TODEVICE);
1381
1382 if (pci_dma_mapping_error(priv->pdev, dma)) {
1383 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001384 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001385 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001386 return NETDEV_TX_OK;
1387 }
1388
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001389 spin_lock_bh(&priv->tx_lock);
1390
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001391 txq = priv->txq + index;
1392
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001393 BUG_ON(txq->skb[txq->tail] != NULL);
1394 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001395
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001396 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001397 tx->data_rate = txdatarate;
1398 tx->tx_priority = index;
1399 tx->qos_control = cpu_to_le16(qos);
1400 tx->pkt_phys_addr = cpu_to_le32(dma);
1401 tx->pkt_len = cpu_to_le16(skb->len);
1402 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001403 if (!priv->ap_fw && tx_info->control.sta != NULL)
1404 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1405 else
1406 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001407 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001408 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1409
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001410 txq->stats.count++;
1411 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001412 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001413
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001414 txq->tail++;
1415 if (txq->tail == MWL8K_TX_DESCS)
1416 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001417
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001418 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001419 ieee80211_stop_queue(hw, index);
1420
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001421 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001422
1423 spin_unlock_bh(&priv->tx_lock);
1424
1425 return NETDEV_TX_OK;
1426}
1427
1428
1429/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001430 * Firmware access.
1431 *
1432 * We have the following requirements for issuing firmware commands:
1433 * - Some commands require that the packet transmit path is idle when
1434 * the command is issued. (For simplicity, we'll just quiesce the
1435 * transmit path for every command.)
1436 * - There are certain sequences of commands that need to be issued to
1437 * the hardware sequentially, with no other intervening commands.
1438 *
1439 * This leads to an implementation of a "firmware lock" as a mutex that
1440 * can be taken recursively, and which is taken by both the low-level
1441 * command submission function (mwl8k_post_cmd) as well as any users of
1442 * that function that require issuing of an atomic sequence of commands,
1443 * and quiesces the transmit path whenever it's taken.
1444 */
1445static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1446{
1447 struct mwl8k_priv *priv = hw->priv;
1448
1449 if (priv->fw_mutex_owner != current) {
1450 int rc;
1451
1452 mutex_lock(&priv->fw_mutex);
1453 ieee80211_stop_queues(hw);
1454
1455 rc = mwl8k_tx_wait_empty(hw);
1456 if (rc) {
1457 ieee80211_wake_queues(hw);
1458 mutex_unlock(&priv->fw_mutex);
1459
1460 return rc;
1461 }
1462
1463 priv->fw_mutex_owner = current;
1464 }
1465
1466 priv->fw_mutex_depth++;
1467
1468 return 0;
1469}
1470
1471static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1472{
1473 struct mwl8k_priv *priv = hw->priv;
1474
1475 if (!--priv->fw_mutex_depth) {
1476 ieee80211_wake_queues(hw);
1477 priv->fw_mutex_owner = NULL;
1478 mutex_unlock(&priv->fw_mutex);
1479 }
1480}
1481
1482
1483/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001484 * Command processing.
1485 */
1486
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001487/* Timeout firmware commands after 10s */
1488#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001489
1490static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1491{
1492 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1493 struct mwl8k_priv *priv = hw->priv;
1494 void __iomem *regs = priv->regs;
1495 dma_addr_t dma_addr;
1496 unsigned int dma_size;
1497 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001498 unsigned long timeout = 0;
1499 u8 buf[32];
1500
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001501 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001502 dma_size = le16_to_cpu(cmd->length);
1503 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1504 PCI_DMA_BIDIRECTIONAL);
1505 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1506 return -ENOMEM;
1507
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001508 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001509 if (rc) {
1510 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1511 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001512 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001513 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001514
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001515 priv->hostcmd_wait = &cmd_wait;
1516 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1517 iowrite32(MWL8K_H2A_INT_DOORBELL,
1518 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1519 iowrite32(MWL8K_H2A_INT_DUMMY,
1520 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001521
1522 timeout = wait_for_completion_timeout(&cmd_wait,
1523 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1524
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001525 priv->hostcmd_wait = NULL;
1526
1527 mwl8k_fw_unlock(hw);
1528
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001529 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1530 PCI_DMA_BIDIRECTIONAL);
1531
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001532 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001533 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001534 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001535 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1536 MWL8K_CMD_TIMEOUT_MS);
1537 rc = -ETIMEDOUT;
1538 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001539 int ms;
1540
1541 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1542
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001543 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001544 if (rc)
1545 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001546 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001547 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001548 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001549 else if (ms > 2000)
1550 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1551 wiphy_name(hw->wiphy),
1552 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1553 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001554 }
1555
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001556 return rc;
1557}
1558
1559/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001560 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001561 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001562struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001563 struct mwl8k_cmd_pkt header;
1564 __u8 hw_rev;
1565 __u8 host_interface;
1566 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001567 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001568 __le16 region_code;
1569 __le32 fw_rev;
1570 __le32 ps_cookie;
1571 __le32 caps;
1572 __u8 mcs_bitmap[16];
1573 __le32 rx_queue_ptr;
1574 __le32 num_tx_queues;
1575 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1576 __le32 caps2;
1577 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001578 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001579} __attribute__((packed));
1580
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001581static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001582{
1583 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001584 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001585 int rc;
1586 int i;
1587
1588 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1589 if (cmd == NULL)
1590 return -ENOMEM;
1591
1592 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1593 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1594
1595 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1596 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001597 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001598 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001599 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001600 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001601 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001602 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001603
1604 rc = mwl8k_post_cmd(hw, &cmd->header);
1605
1606 if (!rc) {
1607 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1608 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001609 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001610 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001611 }
1612
1613 kfree(cmd);
1614 return rc;
1615}
1616
1617/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001618 * CMD_GET_HW_SPEC (AP version).
1619 */
1620struct mwl8k_cmd_get_hw_spec_ap {
1621 struct mwl8k_cmd_pkt header;
1622 __u8 hw_rev;
1623 __u8 host_interface;
1624 __le16 num_wcb;
1625 __le16 num_mcaddrs;
1626 __u8 perm_addr[ETH_ALEN];
1627 __le16 region_code;
1628 __le16 num_antenna;
1629 __le32 fw_rev;
1630 __le32 wcbbase0;
1631 __le32 rxwrptr;
1632 __le32 rxrdptr;
1633 __le32 ps_cookie;
1634 __le32 wcbbase1;
1635 __le32 wcbbase2;
1636 __le32 wcbbase3;
1637} __attribute__((packed));
1638
1639static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1640{
1641 struct mwl8k_priv *priv = hw->priv;
1642 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1643 int rc;
1644
1645 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1646 if (cmd == NULL)
1647 return -ENOMEM;
1648
1649 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1650 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1651
1652 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1653 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1654
1655 rc = mwl8k_post_cmd(hw, &cmd->header);
1656
1657 if (!rc) {
1658 int off;
1659
1660 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1661 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1662 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1663 priv->hw_rev = cmd->hw_rev;
1664
1665 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1666 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1667
1668 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1669 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1670
1671 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1672 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1673
1674 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1675 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1676
1677 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1678 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1679
1680 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1681 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1682 }
1683
1684 kfree(cmd);
1685 return rc;
1686}
1687
1688/*
1689 * CMD_SET_HW_SPEC.
1690 */
1691struct mwl8k_cmd_set_hw_spec {
1692 struct mwl8k_cmd_pkt header;
1693 __u8 hw_rev;
1694 __u8 host_interface;
1695 __le16 num_mcaddrs;
1696 __u8 perm_addr[ETH_ALEN];
1697 __le16 region_code;
1698 __le32 fw_rev;
1699 __le32 ps_cookie;
1700 __le32 caps;
1701 __le32 rx_queue_ptr;
1702 __le32 num_tx_queues;
1703 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1704 __le32 flags;
1705 __le32 num_tx_desc_per_queue;
1706 __le32 total_rxd;
1707} __attribute__((packed));
1708
1709#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1710
1711static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1712{
1713 struct mwl8k_priv *priv = hw->priv;
1714 struct mwl8k_cmd_set_hw_spec *cmd;
1715 int rc;
1716 int i;
1717
1718 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1719 if (cmd == NULL)
1720 return -ENOMEM;
1721
1722 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1723 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1724
1725 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1726 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1727 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1728 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1729 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1730 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1731 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1732 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1733
1734 rc = mwl8k_post_cmd(hw, &cmd->header);
1735 kfree(cmd);
1736
1737 return rc;
1738}
1739
1740/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001741 * CMD_MAC_MULTICAST_ADR.
1742 */
1743struct mwl8k_cmd_mac_multicast_adr {
1744 struct mwl8k_cmd_pkt header;
1745 __le16 action;
1746 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001747 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001748};
1749
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001750#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1751#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1752#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1753#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001754
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001755static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001756__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001757 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001758{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001759 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001760 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001761 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001762
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001763 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001764 allmulti = 1;
1765 mc_count = 0;
1766 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001767
1768 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1769
1770 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001771 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001772 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001773
1774 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1775 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001776 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1777 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001778
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001779 if (allmulti) {
1780 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1781 } else if (mc_count) {
1782 int i;
1783
1784 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1785 cmd->numaddr = cpu_to_le16(mc_count);
1786 for (i = 0; i < mc_count && mclist; i++) {
1787 if (mclist->da_addrlen != ETH_ALEN) {
1788 kfree(cmd);
1789 return NULL;
1790 }
1791 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1792 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001793 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001794 }
1795
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001796 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001797}
1798
1799/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001800 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001801 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001802struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001803 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001804 __le32 stats[64];
1805} __attribute__((packed));
1806
1807#define MWL8K_STAT_ACK_FAILURE 9
1808#define MWL8K_STAT_RTS_FAILURE 12
1809#define MWL8K_STAT_FCS_ERROR 24
1810#define MWL8K_STAT_RTS_SUCCESS 11
1811
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001812static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1813 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001814{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001815 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001816 int rc;
1817
1818 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1819 if (cmd == NULL)
1820 return -ENOMEM;
1821
1822 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1823 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001824
1825 rc = mwl8k_post_cmd(hw, &cmd->header);
1826 if (!rc) {
1827 stats->dot11ACKFailureCount =
1828 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1829 stats->dot11RTSFailureCount =
1830 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1831 stats->dot11FCSErrorCount =
1832 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1833 stats->dot11RTSSuccessCount =
1834 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1835 }
1836 kfree(cmd);
1837
1838 return rc;
1839}
1840
1841/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001842 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001843 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001844struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001845 struct mwl8k_cmd_pkt header;
1846 __le16 action;
1847 __le16 control;
1848 __le16 radio_on;
1849} __attribute__((packed));
1850
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001851static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001852mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001853{
1854 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001855 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001856 int rc;
1857
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001858 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001859 return 0;
1860
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001861 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1862 if (cmd == NULL)
1863 return -ENOMEM;
1864
1865 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1866 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1867 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001868 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001869 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1870
1871 rc = mwl8k_post_cmd(hw, &cmd->header);
1872 kfree(cmd);
1873
1874 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001875 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001876
1877 return rc;
1878}
1879
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001880static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001881{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001882 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001883}
1884
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001885static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001886{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001887 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001888}
1889
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001890static int
1891mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1892{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001893 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001894
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001895 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001896
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001897 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001898}
1899
1900/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001901 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001902 */
1903#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1904
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001905struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001906 struct mwl8k_cmd_pkt header;
1907 __le16 action;
1908 __le16 support_level;
1909 __le16 current_level;
1910 __le16 reserved;
1911 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1912} __attribute__((packed));
1913
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001914static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001915{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001916 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001917 int rc;
1918
1919 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1920 if (cmd == NULL)
1921 return -ENOMEM;
1922
1923 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1924 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1925 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1926 cmd->support_level = cpu_to_le16(dBm);
1927
1928 rc = mwl8k_post_cmd(hw, &cmd->header);
1929 kfree(cmd);
1930
1931 return rc;
1932}
1933
1934/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02001935 * CMD_RF_ANTENNA.
1936 */
1937struct mwl8k_cmd_rf_antenna {
1938 struct mwl8k_cmd_pkt header;
1939 __le16 antenna;
1940 __le16 mode;
1941} __attribute__((packed));
1942
1943#define MWL8K_RF_ANTENNA_RX 1
1944#define MWL8K_RF_ANTENNA_TX 2
1945
1946static int
1947mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
1948{
1949 struct mwl8k_cmd_rf_antenna *cmd;
1950 int rc;
1951
1952 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1953 if (cmd == NULL)
1954 return -ENOMEM;
1955
1956 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
1957 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1958 cmd->antenna = cpu_to_le16(antenna);
1959 cmd->mode = cpu_to_le16(mask);
1960
1961 rc = mwl8k_post_cmd(hw, &cmd->header);
1962 kfree(cmd);
1963
1964 return rc;
1965}
1966
1967/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001968 * CMD_SET_PRE_SCAN.
1969 */
1970struct mwl8k_cmd_set_pre_scan {
1971 struct mwl8k_cmd_pkt header;
1972} __attribute__((packed));
1973
1974static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
1975{
1976 struct mwl8k_cmd_set_pre_scan *cmd;
1977 int rc;
1978
1979 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1980 if (cmd == NULL)
1981 return -ENOMEM;
1982
1983 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
1984 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1985
1986 rc = mwl8k_post_cmd(hw, &cmd->header);
1987 kfree(cmd);
1988
1989 return rc;
1990}
1991
1992/*
1993 * CMD_SET_POST_SCAN.
1994 */
1995struct mwl8k_cmd_set_post_scan {
1996 struct mwl8k_cmd_pkt header;
1997 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001998 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001999} __attribute__((packed));
2000
2001static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002002mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002003{
2004 struct mwl8k_cmd_set_post_scan *cmd;
2005 int rc;
2006
2007 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2008 if (cmd == NULL)
2009 return -ENOMEM;
2010
2011 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2012 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2013 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002014 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002015
2016 rc = mwl8k_post_cmd(hw, &cmd->header);
2017 kfree(cmd);
2018
2019 return rc;
2020}
2021
2022/*
2023 * CMD_SET_RF_CHANNEL.
2024 */
2025struct mwl8k_cmd_set_rf_channel {
2026 struct mwl8k_cmd_pkt header;
2027 __le16 action;
2028 __u8 current_channel;
2029 __le32 channel_flags;
2030} __attribute__((packed));
2031
2032static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002033 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002034{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002035 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002036 struct mwl8k_cmd_set_rf_channel *cmd;
2037 int rc;
2038
2039 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2040 if (cmd == NULL)
2041 return -ENOMEM;
2042
2043 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2044 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2045 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2046 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002047
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002048 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002049 cmd->channel_flags |= cpu_to_le32(0x00000001);
2050
2051 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2052 conf->channel_type == NL80211_CHAN_HT20)
2053 cmd->channel_flags |= cpu_to_le32(0x00000080);
2054 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2055 cmd->channel_flags |= cpu_to_le32(0x000001900);
2056 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2057 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002058
2059 rc = mwl8k_post_cmd(hw, &cmd->header);
2060 kfree(cmd);
2061
2062 return rc;
2063}
2064
2065/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002066 * CMD_SET_AID.
2067 */
2068#define MWL8K_FRAME_PROT_DISABLED 0x00
2069#define MWL8K_FRAME_PROT_11G 0x07
2070#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2071#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2072
2073struct mwl8k_cmd_update_set_aid {
2074 struct mwl8k_cmd_pkt header;
2075 __le16 aid;
2076
2077 /* AP's MAC address (BSSID) */
2078 __u8 bssid[ETH_ALEN];
2079 __le16 protection_mode;
2080 __u8 supp_rates[14];
2081} __attribute__((packed));
2082
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002083static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2084{
2085 int i;
2086 int j;
2087
2088 /*
2089 * Clear nonstandard rates 4 and 13.
2090 */
2091 mask &= 0x1fef;
2092
2093 for (i = 0, j = 0; i < 14; i++) {
2094 if (mask & (1 << i))
2095 rates[j++] = mwl8k_rates[i].hw_value;
2096 }
2097}
2098
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002099static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002100mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2101 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002102{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002103 struct mwl8k_cmd_update_set_aid *cmd;
2104 u16 prot_mode;
2105 int rc;
2106
2107 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2108 if (cmd == NULL)
2109 return -ENOMEM;
2110
2111 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2112 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002113 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002114 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002115
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002116 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002117 prot_mode = MWL8K_FRAME_PROT_11G;
2118 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002119 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002120 IEEE80211_HT_OP_MODE_PROTECTION) {
2121 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2122 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2123 break;
2124 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2125 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2126 break;
2127 default:
2128 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2129 break;
2130 }
2131 }
2132 cmd->protection_mode = cpu_to_le16(prot_mode);
2133
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002134 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002135
2136 rc = mwl8k_post_cmd(hw, &cmd->header);
2137 kfree(cmd);
2138
2139 return rc;
2140}
2141
2142/*
2143 * CMD_SET_RATE.
2144 */
2145struct mwl8k_cmd_set_rate {
2146 struct mwl8k_cmd_pkt header;
2147 __u8 legacy_rates[14];
2148
2149 /* Bitmap for supported MCS codes. */
2150 __u8 mcs_set[16];
2151 __u8 reserved[16];
2152} __attribute__((packed));
2153
2154static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002155mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2156 u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002157{
2158 struct mwl8k_cmd_set_rate *cmd;
2159 int rc;
2160
2161 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2162 if (cmd == NULL)
2163 return -ENOMEM;
2164
2165 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2166 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002167 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002168
2169 rc = mwl8k_post_cmd(hw, &cmd->header);
2170 kfree(cmd);
2171
2172 return rc;
2173}
2174
2175/*
2176 * CMD_FINALIZE_JOIN.
2177 */
2178#define MWL8K_FJ_BEACON_MAXLEN 128
2179
2180struct mwl8k_cmd_finalize_join {
2181 struct mwl8k_cmd_pkt header;
2182 __le32 sleep_interval; /* Number of beacon periods to sleep */
2183 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2184} __attribute__((packed));
2185
2186static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2187 int framelen, int dtim)
2188{
2189 struct mwl8k_cmd_finalize_join *cmd;
2190 struct ieee80211_mgmt *payload = frame;
2191 int payload_len;
2192 int rc;
2193
2194 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2195 if (cmd == NULL)
2196 return -ENOMEM;
2197
2198 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2199 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2200 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2201
2202 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2203 if (payload_len < 0)
2204 payload_len = 0;
2205 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2206 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2207
2208 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2209
2210 rc = mwl8k_post_cmd(hw, &cmd->header);
2211 kfree(cmd);
2212
2213 return rc;
2214}
2215
2216/*
2217 * CMD_SET_RTS_THRESHOLD.
2218 */
2219struct mwl8k_cmd_set_rts_threshold {
2220 struct mwl8k_cmd_pkt header;
2221 __le16 action;
2222 __le16 threshold;
2223} __attribute__((packed));
2224
2225static int mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw,
2226 u16 action, u16 threshold)
2227{
2228 struct mwl8k_cmd_set_rts_threshold *cmd;
2229 int rc;
2230
2231 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2232 if (cmd == NULL)
2233 return -ENOMEM;
2234
2235 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2236 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2237 cmd->action = cpu_to_le16(action);
2238 cmd->threshold = cpu_to_le16(threshold);
2239
2240 rc = mwl8k_post_cmd(hw, &cmd->header);
2241 kfree(cmd);
2242
2243 return rc;
2244}
2245
2246/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002247 * CMD_SET_SLOT.
2248 */
2249struct mwl8k_cmd_set_slot {
2250 struct mwl8k_cmd_pkt header;
2251 __le16 action;
2252 __u8 short_slot;
2253} __attribute__((packed));
2254
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002255static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002256{
2257 struct mwl8k_cmd_set_slot *cmd;
2258 int rc;
2259
2260 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2261 if (cmd == NULL)
2262 return -ENOMEM;
2263
2264 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2265 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2266 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002267 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002268
2269 rc = mwl8k_post_cmd(hw, &cmd->header);
2270 kfree(cmd);
2271
2272 return rc;
2273}
2274
2275/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002276 * CMD_SET_EDCA_PARAMS.
2277 */
2278struct mwl8k_cmd_set_edca_params {
2279 struct mwl8k_cmd_pkt header;
2280
2281 /* See MWL8K_SET_EDCA_XXX below */
2282 __le16 action;
2283
2284 /* TX opportunity in units of 32 us */
2285 __le16 txop;
2286
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002287 union {
2288 struct {
2289 /* Log exponent of max contention period: 0...15 */
2290 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002291
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002292 /* Log exponent of min contention period: 0...15 */
2293 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002294
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002295 /* Adaptive interframe spacing in units of 32us */
2296 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002297
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002298 /* TX queue to configure */
2299 __u8 txq;
2300 } ap;
2301 struct {
2302 /* Log exponent of max contention period: 0...15 */
2303 __u8 log_cw_max;
2304
2305 /* Log exponent of min contention period: 0...15 */
2306 __u8 log_cw_min;
2307
2308 /* Adaptive interframe spacing in units of 32us */
2309 __u8 aifs;
2310
2311 /* TX queue to configure */
2312 __u8 txq;
2313 } sta;
2314 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002315} __attribute__((packed));
2316
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002317#define MWL8K_SET_EDCA_CW 0x01
2318#define MWL8K_SET_EDCA_TXOP 0x02
2319#define MWL8K_SET_EDCA_AIFS 0x04
2320
2321#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2322 MWL8K_SET_EDCA_TXOP | \
2323 MWL8K_SET_EDCA_AIFS)
2324
2325static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002326mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2327 __u16 cw_min, __u16 cw_max,
2328 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002329{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002330 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002331 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002332 int rc;
2333
2334 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2335 if (cmd == NULL)
2336 return -ENOMEM;
2337
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002338 /*
2339 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2340 * this call.
2341 */
2342 qnum ^= !(qnum >> 1);
2343
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002344 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2345 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002346 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2347 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002348 if (priv->ap_fw) {
2349 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2350 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2351 cmd->ap.aifs = aifs;
2352 cmd->ap.txq = qnum;
2353 } else {
2354 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2355 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2356 cmd->sta.aifs = aifs;
2357 cmd->sta.txq = qnum;
2358 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002359
2360 rc = mwl8k_post_cmd(hw, &cmd->header);
2361 kfree(cmd);
2362
2363 return rc;
2364}
2365
2366/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002367 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002368 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002369struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002370 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002371 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002372} __attribute__((packed));
2373
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002374static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002375{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002376 struct mwl8k_priv *priv = hw->priv;
2377 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002378 int rc;
2379
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002380 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2381 if (cmd == NULL)
2382 return -ENOMEM;
2383
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002384 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002385 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002386 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002387
2388 rc = mwl8k_post_cmd(hw, &cmd->header);
2389 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002390
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002391 if (!rc)
2392 priv->wmm_enabled = enable;
2393
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002394 return rc;
2395}
2396
2397/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002398 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002399 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002400struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002401 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002402 __le32 action;
2403 __u8 rx_antenna_map;
2404 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002405} __attribute__((packed));
2406
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002407static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002408{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002409 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002410 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002411
2412 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2413 if (cmd == NULL)
2414 return -ENOMEM;
2415
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002416 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002417 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002418 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2419 cmd->rx_antenna_map = rx;
2420 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002421
2422 rc = mwl8k_post_cmd(hw, &cmd->header);
2423 kfree(cmd);
2424
2425 return rc;
2426}
2427
2428/*
2429 * CMD_USE_FIXED_RATE.
2430 */
2431#define MWL8K_RATE_TABLE_SIZE 8
2432#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002433#define MWL8K_USE_AUTO_RATE 0x0002
2434
2435struct mwl8k_rate_entry {
2436 /* Set to 1 if HT rate, 0 if legacy. */
2437 __le32 is_ht_rate;
2438
2439 /* Set to 1 to use retry_count field. */
2440 __le32 enable_retry;
2441
2442 /* Specified legacy rate or MCS. */
2443 __le32 rate;
2444
2445 /* Number of allowed retries. */
2446 __le32 retry_count;
2447} __attribute__((packed));
2448
2449struct mwl8k_rate_table {
2450 /* 1 to allow specified rate and below */
2451 __le32 allow_rate_drop;
2452 __le32 num_rates;
2453 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2454} __attribute__((packed));
2455
2456struct mwl8k_cmd_use_fixed_rate {
2457 struct mwl8k_cmd_pkt header;
2458 __le32 action;
2459 struct mwl8k_rate_table rate_table;
2460
2461 /* Unicast, Broadcast or Multicast */
2462 __le32 rate_type;
2463 __le32 reserved1;
2464 __le32 reserved2;
2465} __attribute__((packed));
2466
2467static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2468 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2469{
2470 struct mwl8k_cmd_use_fixed_rate *cmd;
2471 int count;
2472 int rc;
2473
2474 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2475 if (cmd == NULL)
2476 return -ENOMEM;
2477
2478 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2479 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2480
2481 cmd->action = cpu_to_le32(action);
2482 cmd->rate_type = cpu_to_le32(rate_type);
2483
2484 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002485 /*
2486 * Copy over each field manually so that endian
2487 * conversion can be done.
2488 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002489 cmd->rate_table.allow_rate_drop =
2490 cpu_to_le32(rate_table->allow_rate_drop);
2491 cmd->rate_table.num_rates =
2492 cpu_to_le32(rate_table->num_rates);
2493
2494 for (count = 0; count < rate_table->num_rates; count++) {
2495 struct mwl8k_rate_entry *dst =
2496 &cmd->rate_table.rate_entry[count];
2497 struct mwl8k_rate_entry *src =
2498 &rate_table->rate_entry[count];
2499
2500 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2501 dst->enable_retry = cpu_to_le32(src->enable_retry);
2502 dst->rate = cpu_to_le32(src->rate);
2503 dst->retry_count = cpu_to_le32(src->retry_count);
2504 }
2505 }
2506
2507 rc = mwl8k_post_cmd(hw, &cmd->header);
2508 kfree(cmd);
2509
2510 return rc;
2511}
2512
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002513/*
2514 * CMD_ENABLE_SNIFFER.
2515 */
2516struct mwl8k_cmd_enable_sniffer {
2517 struct mwl8k_cmd_pkt header;
2518 __le32 action;
2519} __attribute__((packed));
2520
2521static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2522{
2523 struct mwl8k_cmd_enable_sniffer *cmd;
2524 int rc;
2525
2526 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2527 if (cmd == NULL)
2528 return -ENOMEM;
2529
2530 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2531 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2532 cmd->action = cpu_to_le32(!!enable);
2533
2534 rc = mwl8k_post_cmd(hw, &cmd->header);
2535 kfree(cmd);
2536
2537 return rc;
2538}
2539
2540/*
2541 * CMD_SET_MAC_ADDR.
2542 */
2543struct mwl8k_cmd_set_mac_addr {
2544 struct mwl8k_cmd_pkt header;
2545 union {
2546 struct {
2547 __le16 mac_type;
2548 __u8 mac_addr[ETH_ALEN];
2549 } mbss;
2550 __u8 mac_addr[ETH_ALEN];
2551 };
2552} __attribute__((packed));
2553
2554static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2555{
2556 struct mwl8k_priv *priv = hw->priv;
2557 struct mwl8k_cmd_set_mac_addr *cmd;
2558 int rc;
2559
2560 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2561 if (cmd == NULL)
2562 return -ENOMEM;
2563
2564 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2565 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2566 if (priv->ap_fw) {
2567 cmd->mbss.mac_type = 0;
2568 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2569 } else {
2570 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2571 }
2572
2573 rc = mwl8k_post_cmd(hw, &cmd->header);
2574 kfree(cmd);
2575
2576 return rc;
2577}
2578
2579/*
2580 * CMD_SET_RATEADAPT_MODE.
2581 */
2582struct mwl8k_cmd_set_rate_adapt_mode {
2583 struct mwl8k_cmd_pkt header;
2584 __le16 action;
2585 __le16 mode;
2586} __attribute__((packed));
2587
2588static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2589{
2590 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2591 int rc;
2592
2593 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2594 if (cmd == NULL)
2595 return -ENOMEM;
2596
2597 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2598 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2599 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2600 cmd->mode = cpu_to_le16(mode);
2601
2602 rc = mwl8k_post_cmd(hw, &cmd->header);
2603 kfree(cmd);
2604
2605 return rc;
2606}
2607
2608/*
2609 * CMD_UPDATE_STADB.
2610 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002611struct ewc_ht_info {
2612 __le16 control1;
2613 __le16 control2;
2614 __le16 control3;
2615} __attribute__((packed));
2616
2617struct peer_capability_info {
2618 /* Peer type - AP vs. STA. */
2619 __u8 peer_type;
2620
2621 /* Basic 802.11 capabilities from assoc resp. */
2622 __le16 basic_caps;
2623
2624 /* Set if peer supports 802.11n high throughput (HT). */
2625 __u8 ht_support;
2626
2627 /* Valid if HT is supported. */
2628 __le16 ht_caps;
2629 __u8 extended_ht_caps;
2630 struct ewc_ht_info ewc_info;
2631
2632 /* Legacy rate table. Intersection of our rates and peer rates. */
2633 __u8 legacy_rates[12];
2634
2635 /* HT rate table. Intersection of our rates and peer rates. */
2636 __u8 ht_rates[16];
2637 __u8 pad[16];
2638
2639 /* If set, interoperability mode, no proprietary extensions. */
2640 __u8 interop;
2641 __u8 pad2;
2642 __u8 station_id;
2643 __le16 amsdu_enabled;
2644} __attribute__((packed));
2645
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002646struct mwl8k_cmd_update_stadb {
2647 struct mwl8k_cmd_pkt header;
2648
2649 /* See STADB_ACTION_TYPE */
2650 __le32 action;
2651
2652 /* Peer MAC address */
2653 __u8 peer_addr[ETH_ALEN];
2654
2655 __le32 reserved;
2656
2657 /* Peer info - valid during add/update. */
2658 struct peer_capability_info peer_info;
2659} __attribute__((packed));
2660
Lennert Buytenheka6804002010-01-04 21:55:42 +01002661#define MWL8K_STA_DB_MODIFY_ENTRY 1
2662#define MWL8K_STA_DB_DEL_ENTRY 2
2663
2664/* Peer Entry flags - used to define the type of the peer node */
2665#define MWL8K_PEER_TYPE_ACCESSPOINT 2
2666
2667static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002668 struct ieee80211_vif *vif,
2669 u8 *addr, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002670{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002671 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01002672 struct peer_capability_info *p;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002673 int rc;
2674
2675 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2676 if (cmd == NULL)
2677 return -ENOMEM;
2678
2679 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2680 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01002681 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01002682 memcpy(cmd->peer_addr, addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002683
Lennert Buytenheka6804002010-01-04 21:55:42 +01002684 p = &cmd->peer_info;
2685 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2686 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002687 legacy_rate_mask_to_array(p->legacy_rates, legacy_rate_mask);
Lennert Buytenheka6804002010-01-04 21:55:42 +01002688 p->interop = 1;
2689 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002690
Lennert Buytenheka6804002010-01-04 21:55:42 +01002691 rc = mwl8k_post_cmd(hw, &cmd->header);
2692 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002693
Lennert Buytenheka6804002010-01-04 21:55:42 +01002694 return rc ? rc : p->station_id;
2695}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002696
Lennert Buytenheka6804002010-01-04 21:55:42 +01002697static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
2698 struct ieee80211_vif *vif, u8 *addr)
2699{
2700 struct mwl8k_cmd_update_stadb *cmd;
2701 int rc;
2702
2703 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2704 if (cmd == NULL)
2705 return -ENOMEM;
2706
2707 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2708 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2709 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
2710 memcpy(cmd->peer_addr, addr, ETH_ALEN);
2711
2712 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002713 kfree(cmd);
2714
2715 return rc;
2716}
2717
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002718
2719/*
2720 * Interrupt handling.
2721 */
2722static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2723{
2724 struct ieee80211_hw *hw = dev_id;
2725 struct mwl8k_priv *priv = hw->priv;
2726 u32 status;
2727
2728 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2729 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2730
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002731 if (!status)
2732 return IRQ_NONE;
2733
2734 if (status & MWL8K_A2H_INT_TX_DONE)
2735 tasklet_schedule(&priv->tx_reclaim_task);
2736
2737 if (status & MWL8K_A2H_INT_RX_READY) {
2738 while (rxq_process(hw, 0, 1))
2739 rxq_refill(hw, 0, 1);
2740 }
2741
2742 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002743 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002744 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002745 }
2746
2747 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002748 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002749 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002750 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002751 }
2752
2753 return IRQ_HANDLED;
2754}
2755
2756
2757/*
2758 * Core driver operations.
2759 */
2760static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2761{
2762 struct mwl8k_priv *priv = hw->priv;
2763 int index = skb_get_queue_mapping(skb);
2764 int rc;
2765
2766 if (priv->current_channel == NULL) {
2767 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002768 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002769 dev_kfree_skb(skb);
2770 return NETDEV_TX_OK;
2771 }
2772
2773 rc = mwl8k_txq_xmit(hw, index, skb);
2774
2775 return rc;
2776}
2777
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002778static int mwl8k_start(struct ieee80211_hw *hw)
2779{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002780 struct mwl8k_priv *priv = hw->priv;
2781 int rc;
2782
Joe Perchesa0607fd2009-11-18 23:29:17 -08002783 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002784 IRQF_SHARED, MWL8K_NAME, hw);
2785 if (rc) {
2786 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002787 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002788 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002789 }
2790
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002791 /* Enable tx reclaim tasklet */
2792 tasklet_enable(&priv->tx_reclaim_task);
2793
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002794 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002795 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002796
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002797 rc = mwl8k_fw_lock(hw);
2798 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002799 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002800
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002801 if (!priv->ap_fw) {
2802 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002803 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002804
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002805 if (!rc)
2806 rc = mwl8k_cmd_set_pre_scan(hw);
2807
2808 if (!rc)
2809 rc = mwl8k_cmd_set_post_scan(hw,
2810 "\x00\x00\x00\x00\x00\x00");
2811 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002812
2813 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002814 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002815
2816 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002817 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002818
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002819 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002820 }
2821
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002822 if (rc) {
2823 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2824 free_irq(priv->pdev->irq, hw);
2825 tasklet_disable(&priv->tx_reclaim_task);
2826 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002827
2828 return rc;
2829}
2830
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002831static void mwl8k_stop(struct ieee80211_hw *hw)
2832{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002833 struct mwl8k_priv *priv = hw->priv;
2834 int i;
2835
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002836 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002837
2838 ieee80211_stop_queues(hw);
2839
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002840 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002841 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002842 free_irq(priv->pdev->irq, hw);
2843
2844 /* Stop finalize join worker */
2845 cancel_work_sync(&priv->finalize_join_worker);
2846 if (priv->beacon_skb != NULL)
2847 dev_kfree_skb(priv->beacon_skb);
2848
2849 /* Stop tx reclaim tasklet */
2850 tasklet_disable(&priv->tx_reclaim_task);
2851
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002852 /* Return all skbs to mac80211 */
2853 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2854 mwl8k_txq_reclaim(hw, i, 1);
2855}
2856
2857static int mwl8k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002858 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002859{
2860 struct mwl8k_priv *priv = hw->priv;
2861 struct mwl8k_vif *mwl8k_vif;
2862
2863 /*
2864 * We only support one active interface at a time.
2865 */
2866 if (priv->vif != NULL)
2867 return -EBUSY;
2868
2869 /*
2870 * We only support managed interfaces for now.
2871 */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002872 if (vif->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002873 return -EINVAL;
2874
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002875 /*
2876 * Reject interface creation if sniffer mode is active, as
2877 * STA operation is mutually exclusive with hardware sniffer
2878 * mode.
2879 */
2880 if (priv->sniffer_enabled) {
2881 printk(KERN_INFO "%s: unable to create STA "
2882 "interface due to sniffer mode being enabled\n",
2883 wiphy_name(hw->wiphy));
2884 return -EINVAL;
2885 }
2886
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002887 /* Clean out driver private area */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002888 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002889 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2890
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002891 /* Set and save the mac address */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002892 mwl8k_cmd_set_mac_addr(hw, vif->addr);
2893 memcpy(mwl8k_vif->mac_addr, vif->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002894
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002895 /* Set Initial sequence number to zero */
2896 mwl8k_vif->seqno = 0;
2897
Johannes Berg1ed32e42009-12-23 13:15:45 +01002898 priv->vif = vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002899 priv->current_channel = NULL;
2900
2901 return 0;
2902}
2903
2904static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002905 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002906{
2907 struct mwl8k_priv *priv = hw->priv;
2908
2909 if (priv->vif == NULL)
2910 return;
2911
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002912 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002913
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002914 priv->vif = NULL;
2915}
2916
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002917static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002918{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002919 struct ieee80211_conf *conf = &hw->conf;
2920 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002921 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002922
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002923 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002924 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002925 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002926 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002927 }
2928
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002929 rc = mwl8k_fw_lock(hw);
2930 if (rc)
2931 return rc;
2932
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002933 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002934 if (rc)
2935 goto out;
2936
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002937 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002938 if (rc)
2939 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002940
2941 priv->current_channel = conf->channel;
2942
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002943 if (conf->power_level > 18)
2944 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002945 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002946 if (rc)
2947 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002948
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002949 if (priv->ap_fw) {
2950 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2951 if (!rc)
2952 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2953 } else {
2954 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2955 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002956
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002957out:
2958 mwl8k_fw_unlock(hw);
2959
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002960 return rc;
2961}
2962
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002963static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2964 struct ieee80211_vif *vif,
2965 struct ieee80211_bss_conf *info,
2966 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002967{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002968 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01002969 u32 ap_legacy_rates;
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002970 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002971
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01002972 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002973 return;
2974
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01002975 /*
2976 * No need to capture a beacon if we're no longer associated.
2977 */
2978 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
2979 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002980
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01002981 /*
2982 * Get the AP's legacy rates.
2983 */
2984 ap_legacy_rates = 0;
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002985 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002986 struct ieee80211_sta *ap;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002987
2988 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01002989
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002990 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01002991 if (ap == NULL) {
2992 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002993 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01002994 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002995
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01002996 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
2997
2998 rcu_read_unlock();
2999 }
3000
3001 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
3002 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003003 if (rc)
3004 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003005
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003006 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
3007 MWL8K_UCAST_RATE, NULL);
3008 if (rc)
3009 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003010 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003011
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003012 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003013 rc = mwl8k_set_radio_preamble(hw,
3014 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003015 if (rc)
3016 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003017 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003018
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003019 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003020 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003021 if (rc)
3022 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003023 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003024
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003025 if (((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) ||
3026 (changed & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT))) {
3027 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003028 if (rc)
3029 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003030 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003031
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003032 if (vif->bss_conf.assoc &&
3033 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003034 /*
3035 * Finalize the join. Tell rx handler to process
3036 * next beacon from our BSSID.
3037 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003038 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003039 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003040 }
3041
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003042out:
3043 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003044}
3045
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003046static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3047 int mc_count, struct dev_addr_list *mclist)
3048{
3049 struct mwl8k_cmd_pkt *cmd;
3050
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003051 /*
3052 * Synthesize and return a command packet that programs the
3053 * hardware multicast address filter. At this point we don't
3054 * know whether FIF_ALLMULTI is being requested, but if it is,
3055 * we'll end up throwing this packet away and creating a new
3056 * one in mwl8k_configure_filter().
3057 */
3058 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003059
3060 return (unsigned long)cmd;
3061}
3062
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003063static int
3064mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3065 unsigned int changed_flags,
3066 unsigned int *total_flags)
3067{
3068 struct mwl8k_priv *priv = hw->priv;
3069
3070 /*
3071 * Hardware sniffer mode is mutually exclusive with STA
3072 * operation, so refuse to enable sniffer mode if a STA
3073 * interface is active.
3074 */
3075 if (priv->vif != NULL) {
3076 if (net_ratelimit())
3077 printk(KERN_INFO "%s: not enabling sniffer "
3078 "mode because STA interface is active\n",
3079 wiphy_name(hw->wiphy));
3080 return 0;
3081 }
3082
3083 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003084 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003085 return 0;
3086 priv->sniffer_enabled = true;
3087 }
3088
3089 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3090 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3091 FIF_OTHER_BSS;
3092
3093 return 1;
3094}
3095
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003096static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3097 unsigned int changed_flags,
3098 unsigned int *total_flags,
3099 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003100{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003101 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003102 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3103
3104 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003105 * AP firmware doesn't allow fine-grained control over
3106 * the receive filter.
3107 */
3108 if (priv->ap_fw) {
3109 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3110 kfree(cmd);
3111 return;
3112 }
3113
3114 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003115 * Enable hardware sniffer mode if FIF_CONTROL or
3116 * FIF_OTHER_BSS is requested.
3117 */
3118 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3119 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3120 kfree(cmd);
3121 return;
3122 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003123
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003124 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003125 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003126
3127 if (mwl8k_fw_lock(hw))
3128 return;
3129
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003130 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003131 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003132 priv->sniffer_enabled = false;
3133 }
3134
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003135 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003136 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3137 /*
3138 * Disable the BSS filter.
3139 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003140 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003141 } else {
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003142 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003143
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003144 /*
3145 * Enable the BSS filter.
3146 *
3147 * If there is an active STA interface, use that
3148 * interface's BSSID, otherwise use a dummy one
3149 * (where the OUI part needs to be nonzero for
3150 * the BSSID to be accepted by POST_SCAN).
3151 */
3152 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003153 if (priv->vif != NULL)
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003154 bssid = priv->vif->bss_conf.bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003155
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003156 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003157 }
3158 }
3159
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003160 /*
3161 * If FIF_ALLMULTI is being requested, throw away the command
3162 * packet that ->prepare_multicast() built and replace it with
3163 * a command packet that enables reception of all multicast
3164 * packets.
3165 */
3166 if (*total_flags & FIF_ALLMULTI) {
3167 kfree(cmd);
3168 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3169 }
3170
3171 if (cmd != NULL) {
3172 mwl8k_post_cmd(hw, cmd);
3173 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003174 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003175
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003176 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003177}
3178
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003179static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3180{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003181 return mwl8k_cmd_set_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003182}
3183
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003184struct mwl8k_sta_notify_item
3185{
3186 struct list_head list;
3187 struct ieee80211_vif *vif;
3188 enum sta_notify_cmd cmd;
3189 u8 addr[ETH_ALEN];
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003190 u32 legacy_rate_mask;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003191};
3192
3193static void mwl8k_sta_notify_worker(struct work_struct *work)
3194{
3195 struct mwl8k_priv *priv =
3196 container_of(work, struct mwl8k_priv, sta_notify_worker);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003197 struct ieee80211_hw *hw = priv->hw;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003198
3199 spin_lock_bh(&priv->sta_notify_list_lock);
3200 while (!list_empty(&priv->sta_notify_list)) {
3201 struct mwl8k_sta_notify_item *s;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003202
3203 s = list_entry(priv->sta_notify_list.next,
3204 struct mwl8k_sta_notify_item, list);
3205 list_del(&s->list);
3206
3207 spin_unlock_bh(&priv->sta_notify_list_lock);
3208
Lennert Buytenheka6804002010-01-04 21:55:42 +01003209 if (s->cmd == STA_NOTIFY_ADD) {
3210 int rc;
3211
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003212 rc = mwl8k_cmd_update_stadb_add(hw, s->vif,
3213 s->addr, s->legacy_rate_mask);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003214 if (rc >= 0) {
3215 struct ieee80211_sta *sta;
3216
3217 rcu_read_lock();
3218 sta = ieee80211_find_sta(s->vif, s->addr);
3219 if (sta != NULL)
3220 MWL8K_STA(sta)->peer_id = rc;
3221 rcu_read_unlock();
3222 }
3223 } else {
3224 mwl8k_cmd_update_stadb_del(hw, s->vif, s->addr);
3225 }
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003226
3227 kfree(s);
3228
3229 spin_lock_bh(&priv->sta_notify_list_lock);
3230 }
3231 spin_unlock_bh(&priv->sta_notify_list_lock);
3232}
3233
3234static void
3235mwl8k_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3236 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3237{
3238 struct mwl8k_priv *priv = hw->priv;
3239 struct mwl8k_sta_notify_item *s;
3240
3241 if (cmd != STA_NOTIFY_ADD && cmd != STA_NOTIFY_REMOVE)
3242 return;
3243
3244 s = kmalloc(sizeof(*s), GFP_ATOMIC);
3245 if (s != NULL) {
3246 s->vif = vif;
3247 s->cmd = cmd;
3248 memcpy(s->addr, sta->addr, ETH_ALEN);
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003249 s->legacy_rate_mask = sta->supp_rates[IEEE80211_BAND_2GHZ];
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003250
3251 spin_lock(&priv->sta_notify_list_lock);
3252 list_add_tail(&s->list, &priv->sta_notify_list);
3253 spin_unlock(&priv->sta_notify_list_lock);
3254
3255 ieee80211_queue_work(hw, &priv->sta_notify_worker);
3256 }
3257}
3258
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003259static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3260 const struct ieee80211_tx_queue_params *params)
3261{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003262 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003263 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003264
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003265 rc = mwl8k_fw_lock(hw);
3266 if (!rc) {
3267 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003268 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003269
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003270 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003271 rc = mwl8k_cmd_set_edca_params(hw, queue,
3272 params->cw_min,
3273 params->cw_max,
3274 params->aifs,
3275 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003276
3277 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003278 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003279
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003280 return rc;
3281}
3282
3283static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3284 struct ieee80211_tx_queue_stats *stats)
3285{
3286 struct mwl8k_priv *priv = hw->priv;
3287 struct mwl8k_tx_queue *txq;
3288 int index;
3289
3290 spin_lock_bh(&priv->tx_lock);
3291 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3292 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003293 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003294 sizeof(struct ieee80211_tx_queue_stats));
3295 }
3296 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003297
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003298 return 0;
3299}
3300
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003301static int mwl8k_get_stats(struct ieee80211_hw *hw,
3302 struct ieee80211_low_level_stats *stats)
3303{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003304 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003305}
3306
3307static const struct ieee80211_ops mwl8k_ops = {
3308 .tx = mwl8k_tx,
3309 .start = mwl8k_start,
3310 .stop = mwl8k_stop,
3311 .add_interface = mwl8k_add_interface,
3312 .remove_interface = mwl8k_remove_interface,
3313 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003314 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003315 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003316 .configure_filter = mwl8k_configure_filter,
3317 .set_rts_threshold = mwl8k_set_rts_threshold,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003318 .sta_notify = mwl8k_sta_notify,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003319 .conf_tx = mwl8k_conf_tx,
3320 .get_tx_stats = mwl8k_get_tx_stats,
3321 .get_stats = mwl8k_get_stats,
3322};
3323
3324static void mwl8k_tx_reclaim_handler(unsigned long data)
3325{
3326 int i;
3327 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3328 struct mwl8k_priv *priv = hw->priv;
3329
3330 spin_lock_bh(&priv->tx_lock);
3331 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3332 mwl8k_txq_reclaim(hw, i, 0);
3333
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003334 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003335 complete(priv->tx_wait);
3336 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003337 }
3338 spin_unlock_bh(&priv->tx_lock);
3339}
3340
3341static void mwl8k_finalize_join_worker(struct work_struct *work)
3342{
3343 struct mwl8k_priv *priv =
3344 container_of(work, struct mwl8k_priv, finalize_join_worker);
3345 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003346
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003347 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len,
3348 priv->vif->bss_conf.dtim_period);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003349 dev_kfree_skb(skb);
3350
3351 priv->beacon_skb = NULL;
3352}
3353
John W. Linvillebcb628d2009-11-06 16:40:16 -05003354enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003355 MWL8363 = 0,
3356 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003357 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003358};
3359
John W. Linvillebcb628d2009-11-06 16:40:16 -05003360static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003361 [MWL8363] = {
3362 .part_name = "88w8363",
3363 .helper_image = "mwl8k/helper_8363.fw",
3364 .fw_image = "mwl8k/fmimage_8363.fw",
3365 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003366 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003367 .part_name = "88w8687",
3368 .helper_image = "mwl8k/helper_8687.fw",
3369 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003370 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003371 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003372 .part_name = "88w8366",
3373 .helper_image = "mwl8k/helper_8366.fw",
3374 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003375 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003376 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003377};
3378
3379static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003380 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3381 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003382 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3383 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3384 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3385 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003386};
3387MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3388
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003389static int __devinit mwl8k_probe(struct pci_dev *pdev,
3390 const struct pci_device_id *id)
3391{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003392 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003393 struct ieee80211_hw *hw;
3394 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003395 int rc;
3396 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003397
3398 if (!printed_version) {
3399 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3400 printed_version = 1;
3401 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003402
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003403
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003404 rc = pci_enable_device(pdev);
3405 if (rc) {
3406 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3407 MWL8K_NAME);
3408 return rc;
3409 }
3410
3411 rc = pci_request_regions(pdev, MWL8K_NAME);
3412 if (rc) {
3413 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3414 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003415 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003416 }
3417
3418 pci_set_master(pdev);
3419
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003420
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003421 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3422 if (hw == NULL) {
3423 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3424 rc = -ENOMEM;
3425 goto err_free_reg;
3426 }
3427
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003428 SET_IEEE80211_DEV(hw, &pdev->dev);
3429 pci_set_drvdata(pdev, hw);
3430
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003431 priv = hw->priv;
3432 priv->hw = hw;
3433 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003434 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003435
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003436
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003437 priv->sram = pci_iomap(pdev, 0, 0x10000);
3438 if (priv->sram == NULL) {
3439 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003440 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003441 goto err_iounmap;
3442 }
3443
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003444 /*
3445 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3446 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3447 */
3448 priv->regs = pci_iomap(pdev, 1, 0x10000);
3449 if (priv->regs == NULL) {
3450 priv->regs = pci_iomap(pdev, 2, 0x10000);
3451 if (priv->regs == NULL) {
3452 printk(KERN_ERR "%s: Cannot map device registers\n",
3453 wiphy_name(hw->wiphy));
3454 goto err_iounmap;
3455 }
3456 }
3457
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003458
3459 /* Reset firmware and hardware */
3460 mwl8k_hw_reset(priv);
3461
3462 /* Ask userland hotplug daemon for the device firmware */
3463 rc = mwl8k_request_firmware(priv);
3464 if (rc) {
3465 printk(KERN_ERR "%s: Firmware files not found\n",
3466 wiphy_name(hw->wiphy));
3467 goto err_stop_firmware;
3468 }
3469
3470 /* Load firmware into hardware */
3471 rc = mwl8k_load_firmware(hw);
3472 if (rc) {
3473 printk(KERN_ERR "%s: Cannot start firmware\n",
3474 wiphy_name(hw->wiphy));
3475 goto err_stop_firmware;
3476 }
3477
3478 /* Reclaim memory once firmware is successfully loaded */
3479 mwl8k_release_firmware(priv);
3480
3481
Lennert Buytenhek91942232010-01-04 21:53:54 +01003482 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003483 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003484 if (priv->rxd_ops == NULL) {
3485 printk(KERN_ERR "%s: Driver does not have AP "
3486 "firmware image support for this hardware\n",
3487 wiphy_name(hw->wiphy));
3488 goto err_stop_firmware;
3489 }
3490 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003491 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003492 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003493
3494 priv->sniffer_enabled = false;
3495 priv->wmm_enabled = false;
3496 priv->pending_tx_pkts = 0;
3497
3498
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003499 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3500 priv->band.band = IEEE80211_BAND_2GHZ;
3501 priv->band.channels = priv->channels;
3502 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3503 priv->band.bitrates = priv->rates;
3504 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3505 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3506
3507 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3508 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3509
3510 /*
3511 * Extra headroom is the size of the required DMA header
3512 * minus the size of the smallest 802.11 frame (CTS frame).
3513 */
3514 hw->extra_tx_headroom =
3515 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3516
3517 hw->channel_change_time = 10;
3518
3519 hw->queues = MWL8K_TX_QUEUES;
3520
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003521 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003522 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003523 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003524 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003525 priv->vif = NULL;
3526
3527 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003528 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003529 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003530
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003531 /* Station database handling */
3532 INIT_WORK(&priv->sta_notify_worker, mwl8k_sta_notify_worker);
3533 spin_lock_init(&priv->sta_notify_list_lock);
3534 INIT_LIST_HEAD(&priv->sta_notify_list);
3535
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003536 /* Finalize join worker */
3537 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3538
3539 /* TX reclaim tasklet */
3540 tasklet_init(&priv->tx_reclaim_task,
3541 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3542 tasklet_disable(&priv->tx_reclaim_task);
3543
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003544 /* Power management cookie */
3545 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3546 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003547 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003548
3549 rc = mwl8k_rxq_init(hw, 0);
3550 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003551 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003552 rxq_refill(hw, 0, INT_MAX);
3553
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003554 mutex_init(&priv->fw_mutex);
3555 priv->fw_mutex_owner = NULL;
3556 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003557 priv->hostcmd_wait = NULL;
3558
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003559 spin_lock_init(&priv->tx_lock);
3560
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003561 priv->tx_wait = NULL;
3562
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003563 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3564 rc = mwl8k_txq_init(hw, i);
3565 if (rc)
3566 goto err_free_queues;
3567 }
3568
3569 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003570 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003571 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3572 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3573
Joe Perchesa0607fd2009-11-18 23:29:17 -08003574 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003575 IRQF_SHARED, MWL8K_NAME, hw);
3576 if (rc) {
3577 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003578 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003579 goto err_free_queues;
3580 }
3581
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003582 /*
3583 * Temporarily enable interrupts. Initial firmware host
3584 * commands use interrupts and avoids polling. Disable
3585 * interrupts when done.
3586 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003587 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003588
3589 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02003590 if (priv->ap_fw) {
3591 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3592 if (!rc)
3593 rc = mwl8k_cmd_set_hw_spec(hw);
3594 } else {
3595 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003596
3597 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02003598 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003599 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003600 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3601 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003602 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003603 }
3604
3605 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003606 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003607 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003608 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003609 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003610 }
3611
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003612 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003613 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003614 if (rc) {
3615 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3616 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003617 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003618 }
3619
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003620 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003621 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003622 free_irq(priv->pdev->irq, hw);
3623
3624 rc = ieee80211_register_hw(hw);
3625 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003626 printk(KERN_ERR "%s: Cannot register device\n",
3627 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01003628 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003629 }
3630
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003631 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003632 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003633 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003634 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003635 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3636 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003637
3638 return 0;
3639
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003640err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003641 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003642 free_irq(priv->pdev->irq, hw);
3643
3644err_free_queues:
3645 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3646 mwl8k_txq_deinit(hw, i);
3647 mwl8k_rxq_deinit(hw, 0);
3648
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003649err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003650 if (priv->cookie != NULL)
3651 pci_free_consistent(priv->pdev, 4,
3652 priv->cookie, priv->cookie_dma);
3653
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003654err_stop_firmware:
3655 mwl8k_hw_reset(priv);
3656 mwl8k_release_firmware(priv);
3657
3658err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003659 if (priv->regs != NULL)
3660 pci_iounmap(pdev, priv->regs);
3661
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003662 if (priv->sram != NULL)
3663 pci_iounmap(pdev, priv->sram);
3664
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003665 pci_set_drvdata(pdev, NULL);
3666 ieee80211_free_hw(hw);
3667
3668err_free_reg:
3669 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003670
3671err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003672 pci_disable_device(pdev);
3673
3674 return rc;
3675}
3676
Joerg Albert230f7af2009-04-18 02:10:45 +02003677static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003678{
3679 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3680}
3681
Joerg Albert230f7af2009-04-18 02:10:45 +02003682static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003683{
3684 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3685 struct mwl8k_priv *priv;
3686 int i;
3687
3688 if (hw == NULL)
3689 return;
3690 priv = hw->priv;
3691
3692 ieee80211_stop_queues(hw);
3693
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003694 ieee80211_unregister_hw(hw);
3695
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003696 /* Remove tx reclaim tasklet */
3697 tasklet_kill(&priv->tx_reclaim_task);
3698
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003699 /* Stop hardware */
3700 mwl8k_hw_reset(priv);
3701
3702 /* Return all skbs to mac80211 */
3703 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3704 mwl8k_txq_reclaim(hw, i, 1);
3705
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003706 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3707 mwl8k_txq_deinit(hw, i);
3708
3709 mwl8k_rxq_deinit(hw, 0);
3710
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003711 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003712
3713 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003714 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003715 pci_set_drvdata(pdev, NULL);
3716 ieee80211_free_hw(hw);
3717 pci_release_regions(pdev);
3718 pci_disable_device(pdev);
3719}
3720
3721static struct pci_driver mwl8k_driver = {
3722 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003723 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003724 .probe = mwl8k_probe,
3725 .remove = __devexit_p(mwl8k_remove),
3726 .shutdown = __devexit_p(mwl8k_shutdown),
3727};
3728
3729static int __init mwl8k_init(void)
3730{
3731 return pci_register_driver(&mwl8k_driver);
3732}
3733
3734static void __exit mwl8k_exit(void)
3735{
3736 pci_unregister_driver(&mwl8k_driver);
3737}
3738
3739module_init(mwl8k_init);
3740module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003741
3742MODULE_DESCRIPTION(MWL8K_DESC);
3743MODULE_VERSION(MWL8K_VERSION);
3744MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3745MODULE_LICENSE("GPL");