blob: 6fa2bde5b618ab8733904eddb25cd4a07412d98b [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka145d572009-08-18 04:34:26 +02005 * Copyright (C) 2008-2009 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020015#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010016#include <linux/spinlock.h>
17#include <linux/list.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
20#include <linux/completion.h>
21#include <linux/etherdevice.h>
22#include <net/mac80211.h>
23#include <linux/moduleparam.h>
24#include <linux/firmware.h>
25#include <linux/workqueue.h>
26
27#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
28#define MWL8K_NAME KBUILD_MODNAME
Lennert Buytenheka145d572009-08-18 04:34:26 +020029#define MWL8K_VERSION "0.10"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010030
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031/* Register definitions */
32#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020033#define MWL8K_MODE_STA 0x0000005a
34#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010035#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020036#define MWL8K_FWSTA_READY 0xf0f1f2f4
37#define MWL8K_FWAP_READY 0xf1f2f4a5
38#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010039#define MWL8K_HIU_SCRATCH 0x00000c40
40
41/* Host->device communications */
42#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
43#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
44#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
45#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
46#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020047#define MWL8K_H2A_INT_DUMMY (1 << 20)
48#define MWL8K_H2A_INT_RESET (1 << 15)
49#define MWL8K_H2A_INT_DOORBELL (1 << 1)
50#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010051
52/* Device->host communications */
53#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
54#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
55#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
56#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
57#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020058#define MWL8K_A2H_INT_DUMMY (1 << 20)
59#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
60#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
61#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
62#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
63#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
64#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
65#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
66#define MWL8K_A2H_INT_RX_READY (1 << 1)
67#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010068
69#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
70 MWL8K_A2H_INT_CHNL_SWITCHED | \
71 MWL8K_A2H_INT_QUEUE_EMPTY | \
72 MWL8K_A2H_INT_RADAR_DETECT | \
73 MWL8K_A2H_INT_RADIO_ON | \
74 MWL8K_A2H_INT_RADIO_OFF | \
75 MWL8K_A2H_INT_MAC_EVENT | \
76 MWL8K_A2H_INT_OPC_DONE | \
77 MWL8K_A2H_INT_RX_READY | \
78 MWL8K_A2H_INT_TX_DONE)
79
Lennert Buytenheka66098d2009-03-10 10:13:33 +010080#define MWL8K_RX_QUEUES 1
81#define MWL8K_TX_QUEUES 4
82
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020083struct rxd_ops {
84 int rxd_size;
85 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
86 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010087 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
88 __le16 *qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020089};
90
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020091struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020092 char *part_name;
93 char *helper_image;
94 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010095 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020096};
97
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +020099 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100100
101 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200102 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
104 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200105 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100106
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200107 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200109 struct {
110 struct sk_buff *skb;
111 DECLARE_PCI_UNMAP_ADDR(dma)
112 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100113};
114
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115struct mwl8k_tx_queue {
116 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100118
119 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200120 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100121
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200122 struct ieee80211_tx_queue_stats stats;
123 struct mwl8k_tx_desc *txd;
124 dma_addr_t txd_dma;
125 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100126};
127
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100128struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200132 struct mwl8k_device_info *device_info;
133
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100134 void __iomem *sram;
135 void __iomem *regs;
136
137 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100138 struct firmware *fw_helper;
139 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100141 /* hardware/firmware parameters */
142 bool ap_fw;
143 struct rxd_ops *rxd_ops;
144
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200145 /* firmware access */
146 struct mutex fw_mutex;
147 struct task_struct *fw_mutex_owner;
148 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200149 struct completion *hostcmd_wait;
150
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100151 /* lock held over TX and TX reap */
152 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100153
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200154 /* TX quiesce completion, protected by fw_mutex and tx_lock */
155 struct completion *tx_wait;
156
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 struct ieee80211_vif *vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158
159 struct ieee80211_channel *current_channel;
160
161 /* power management status cookie from firmware */
162 u32 *cookie;
163 dma_addr_t cookie_dma;
164
165 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100166 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200167 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168
169 /*
170 * Running count of TX packets in flight, to avoid
171 * iterating over the transmit rings each time.
172 */
173 int pending_tx_pkts;
174
175 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
176 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
177
178 /* PHY parameters */
179 struct ieee80211_supported_band band;
180 struct ieee80211_channel channels[14];
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100181 struct ieee80211_rate rates[14];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100182
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200183 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200184 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200185 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200186 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100187
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100188 /* XXX need to convert this to handle multiple interfaces */
189 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200190 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100191 struct sk_buff *beacon_skb;
192
193 /*
194 * This FJ worker has to be global as it is scheduled from the
195 * RX handler. At this point we don't know which interface it
196 * belongs to until the list of bssids waiting to complete join
197 * is checked.
198 */
199 struct work_struct finalize_join_worker;
200
201 /* Tasklet to reclaim TX descriptors and buffers after tx */
202 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100203};
204
205/* Per interface specific private data */
206struct mwl8k_vif {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100207 /* BSS config of AP or IBSS from mac80211*/
208 struct ieee80211_bss_conf bss_info;
209
210 /* BSSID of AP or IBSS */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200211 u8 bssid[ETH_ALEN];
212 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100213
Lennert Buytenhek55489b62009-11-30 18:31:33 +0100214 /* Index into station database. Returned by UPDATE_STADB. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100215 u8 peer_id;
216
217 /* Non AMPDU sequence number assigned by driver */
218 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100219};
220
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200221#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100222
223static const struct ieee80211_channel mwl8k_channels[] = {
224 { .center_freq = 2412, .hw_value = 1, },
225 { .center_freq = 2417, .hw_value = 2, },
226 { .center_freq = 2422, .hw_value = 3, },
227 { .center_freq = 2427, .hw_value = 4, },
228 { .center_freq = 2432, .hw_value = 5, },
229 { .center_freq = 2437, .hw_value = 6, },
230 { .center_freq = 2442, .hw_value = 7, },
231 { .center_freq = 2447, .hw_value = 8, },
232 { .center_freq = 2452, .hw_value = 9, },
233 { .center_freq = 2457, .hw_value = 10, },
234 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100235 { .center_freq = 2467, .hw_value = 12, },
236 { .center_freq = 2472, .hw_value = 13, },
237 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100238};
239
240static const struct ieee80211_rate mwl8k_rates[] = {
241 { .bitrate = 10, .hw_value = 2, },
242 { .bitrate = 20, .hw_value = 4, },
243 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200244 { .bitrate = 110, .hw_value = 22, },
245 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100246 { .bitrate = 60, .hw_value = 12, },
247 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100248 { .bitrate = 120, .hw_value = 24, },
249 { .bitrate = 180, .hw_value = 36, },
250 { .bitrate = 240, .hw_value = 48, },
251 { .bitrate = 360, .hw_value = 72, },
252 { .bitrate = 480, .hw_value = 96, },
253 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100254 { .bitrate = 720, .hw_value = 144, },
255};
256
257static const u8 mwl8k_rateids[12] = {
258 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108,
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100259};
260
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100261/* Set or get info from Firmware */
262#define MWL8K_CMD_SET 0x0001
263#define MWL8K_CMD_GET 0x0000
264
265/* Firmware command codes */
266#define MWL8K_CMD_CODE_DNLD 0x0001
267#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200268#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100269#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
270#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200271#define MWL8K_CMD_RADIO_CONTROL 0x001c
272#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200273#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100274#define MWL8K_CMD_SET_PRE_SCAN 0x0107
275#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200276#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100277#define MWL8K_CMD_SET_AID 0x010d
278#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200279#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100280#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200281#define MWL8K_CMD_SET_SLOT 0x0114
282#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
283#define MWL8K_CMD_SET_WMM_MODE 0x0123
284#define MWL8K_CMD_MIMO_CONFIG 0x0125
285#define MWL8K_CMD_USE_FIXED_RATE 0x0126
286#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200287#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200288#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
289#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100290
291static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
292{
293#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
294 snprintf(buf, bufsize, "%s", #x);\
295 return buf;\
296 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200297 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100298 MWL8K_CMDNAME(CODE_DNLD);
299 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200300 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
302 MWL8K_CMDNAME(GET_STAT);
303 MWL8K_CMDNAME(RADIO_CONTROL);
304 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200305 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100306 MWL8K_CMDNAME(SET_PRE_SCAN);
307 MWL8K_CMDNAME(SET_POST_SCAN);
308 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100309 MWL8K_CMDNAME(SET_AID);
310 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200311 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100312 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200313 MWL8K_CMDNAME(SET_SLOT);
314 MWL8K_CMDNAME(SET_EDCA_PARAMS);
315 MWL8K_CMDNAME(SET_WMM_MODE);
316 MWL8K_CMDNAME(MIMO_CONFIG);
317 MWL8K_CMDNAME(USE_FIXED_RATE);
318 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200319 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200320 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
321 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100322 default:
323 snprintf(buf, bufsize, "0x%x", cmd);
324 }
325#undef MWL8K_CMDNAME
326
327 return buf;
328}
329
330/* Hardware and firmware reset */
331static void mwl8k_hw_reset(struct mwl8k_priv *priv)
332{
333 iowrite32(MWL8K_H2A_INT_RESET,
334 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
335 iowrite32(MWL8K_H2A_INT_RESET,
336 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
337 msleep(20);
338}
339
340/* Release fw image */
341static void mwl8k_release_fw(struct firmware **fw)
342{
343 if (*fw == NULL)
344 return;
345 release_firmware(*fw);
346 *fw = NULL;
347}
348
349static void mwl8k_release_firmware(struct mwl8k_priv *priv)
350{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100351 mwl8k_release_fw(&priv->fw_ucode);
352 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100353}
354
355/* Request fw image */
356static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200357 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100358{
359 /* release current image */
360 if (*fw != NULL)
361 mwl8k_release_fw(fw);
362
363 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200364 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100365}
366
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200367static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100368{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200369 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100370 int rc;
371
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200372 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100373 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200374 if (rc) {
375 printk(KERN_ERR "%s: Error requesting helper "
376 "firmware file %s\n", pci_name(priv->pdev),
377 di->helper_image);
378 return rc;
379 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100380 }
381
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100382 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100383 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200384 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200385 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100386 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100387 return rc;
388 }
389
390 return 0;
391}
392
Ben Hutchings7e75b942009-11-07 22:00:57 +0000393MODULE_FIRMWARE("mwl8k/helper_8687.fw");
394MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
395
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100396struct mwl8k_cmd_pkt {
397 __le16 code;
398 __le16 length;
399 __le16 seq_num;
400 __le16 result;
401 char payload[0];
402} __attribute__((packed));
403
404/*
405 * Firmware loading.
406 */
407static int
408mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
409{
410 void __iomem *regs = priv->regs;
411 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100412 int loops;
413
414 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
415 if (pci_dma_mapping_error(priv->pdev, dma_addr))
416 return -ENOMEM;
417
418 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
419 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
420 iowrite32(MWL8K_H2A_INT_DOORBELL,
421 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
422 iowrite32(MWL8K_H2A_INT_DUMMY,
423 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
424
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100425 loops = 1000;
426 do {
427 u32 int_code;
428
429 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
430 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
431 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100432 break;
433 }
434
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200435 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100436 udelay(1);
437 } while (--loops);
438
439 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
440
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200441 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100442}
443
444static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
445 const u8 *data, size_t length)
446{
447 struct mwl8k_cmd_pkt *cmd;
448 int done;
449 int rc = 0;
450
451 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
452 if (cmd == NULL)
453 return -ENOMEM;
454
455 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
456 cmd->seq_num = 0;
457 cmd->result = 0;
458
459 done = 0;
460 while (length) {
461 int block_size = length > 256 ? 256 : length;
462
463 memcpy(cmd->payload, data + done, block_size);
464 cmd->length = cpu_to_le16(block_size);
465
466 rc = mwl8k_send_fw_load_cmd(priv, cmd,
467 sizeof(*cmd) + block_size);
468 if (rc)
469 break;
470
471 done += block_size;
472 length -= block_size;
473 }
474
475 if (!rc) {
476 cmd->length = 0;
477 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
478 }
479
480 kfree(cmd);
481
482 return rc;
483}
484
485static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
486 const u8 *data, size_t length)
487{
488 unsigned char *buffer;
489 int may_continue, rc = 0;
490 u32 done, prev_block_size;
491
492 buffer = kmalloc(1024, GFP_KERNEL);
493 if (buffer == NULL)
494 return -ENOMEM;
495
496 done = 0;
497 prev_block_size = 0;
498 may_continue = 1000;
499 while (may_continue > 0) {
500 u32 block_size;
501
502 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
503 if (block_size & 1) {
504 block_size &= ~1;
505 may_continue--;
506 } else {
507 done += prev_block_size;
508 length -= prev_block_size;
509 }
510
511 if (block_size > 1024 || block_size > length) {
512 rc = -EOVERFLOW;
513 break;
514 }
515
516 if (length == 0) {
517 rc = 0;
518 break;
519 }
520
521 if (block_size == 0) {
522 rc = -EPROTO;
523 may_continue--;
524 udelay(1);
525 continue;
526 }
527
528 prev_block_size = block_size;
529 memcpy(buffer, data + done, block_size);
530
531 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
532 if (rc)
533 break;
534 }
535
536 if (!rc && length != 0)
537 rc = -EREMOTEIO;
538
539 kfree(buffer);
540
541 return rc;
542}
543
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200544static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100545{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200546 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100547 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200548 int rc;
549 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100550
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200551 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100552 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100553
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200554 if (helper == NULL) {
555 printk(KERN_ERR "%s: helper image needed but none "
556 "given\n", pci_name(priv->pdev));
557 return -EINVAL;
558 }
559
560 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100561 if (rc) {
562 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200563 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100564 return rc;
565 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100566 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100567
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200568 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100569 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200570 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100571 }
572
573 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200574 printk(KERN_ERR "%s: unable to load firmware image\n",
575 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100576 return rc;
577 }
578
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100579 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100580
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100581 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100582 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200583 u32 ready_code;
584
585 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
586 if (ready_code == MWL8K_FWAP_READY) {
587 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100588 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200589 } else if (ready_code == MWL8K_FWSTA_READY) {
590 priv->ap_fw = 0;
591 break;
592 }
593
594 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100595 udelay(1);
596 } while (--loops);
597
598 return loops ? 0 : -ETIMEDOUT;
599}
600
601
602/*
603 * Defines shared between transmission and reception.
604 */
605/* HT control fields for firmware */
606struct ewc_ht_info {
607 __le16 control1;
608 __le16 control2;
609 __le16 control3;
610} __attribute__((packed));
611
612/* Firmware Station database operations */
613#define MWL8K_STA_DB_ADD_ENTRY 0
614#define MWL8K_STA_DB_MODIFY_ENTRY 1
615#define MWL8K_STA_DB_DEL_ENTRY 2
616#define MWL8K_STA_DB_FLUSH 3
617
618/* Peer Entry flags - used to define the type of the peer node */
619#define MWL8K_PEER_TYPE_ACCESSPOINT 2
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100620
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100621struct peer_capability_info {
622 /* Peer type - AP vs. STA. */
623 __u8 peer_type;
624
625 /* Basic 802.11 capabilities from assoc resp. */
626 __le16 basic_caps;
627
628 /* Set if peer supports 802.11n high throughput (HT). */
629 __u8 ht_support;
630
631 /* Valid if HT is supported. */
632 __le16 ht_caps;
633 __u8 extended_ht_caps;
634 struct ewc_ht_info ewc_info;
635
636 /* Legacy rate table. Intersection of our rates and peer rates. */
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100637 __u8 legacy_rates[12];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100638
639 /* HT rate table. Intersection of our rates and peer rates. */
Lennert Buytenhek0b5351a2009-11-30 18:11:18 +0100640 __u8 ht_rates[16];
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +0200641 __u8 pad[16];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100642
643 /* If set, interoperability mode, no proprietary extensions. */
644 __u8 interop;
645 __u8 pad2;
646 __u8 station_id;
647 __le16 amsdu_enabled;
648} __attribute__((packed));
649
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100650/* DMA header used by firmware and hardware. */
651struct mwl8k_dma_data {
652 __le16 fwlen;
653 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100654 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100655} __attribute__((packed));
656
657/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100658static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100659{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100660 struct mwl8k_dma_data *tr;
661 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100662
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100663 tr = (struct mwl8k_dma_data *)skb->data;
664 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
665
666 if (hdrlen != sizeof(tr->wh)) {
667 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
668 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
669 *((__le16 *)(tr->data - 2)) = qos;
670 } else {
671 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
672 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100673 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100674
675 if (hdrlen != sizeof(*tr))
676 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100677}
678
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200679static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100680{
681 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100682 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100683 struct mwl8k_dma_data *tr;
684
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100685 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100686 * Add a firmware DMA header; the firmware requires that we
687 * present a 2-byte payload length followed by a 4-address
688 * header (without QoS field), followed (optionally) by any
689 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100690 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100691 wh = (struct ieee80211_hdr *)skb->data;
692
693 hdrlen = ieee80211_hdrlen(wh->frame_control);
694 if (hdrlen != sizeof(*tr))
695 skb_push(skb, sizeof(*tr) - hdrlen);
696
697 if (ieee80211_is_data_qos(wh->frame_control))
698 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100699
700 tr = (struct mwl8k_dma_data *)skb->data;
701 if (wh != &tr->wh)
702 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100703 if (hdrlen != sizeof(tr->wh))
704 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100705
706 /*
707 * Firmware length is the length of the fully formed "802.11
708 * payload". That is, everything except for the 802.11 header.
709 * This includes all crypto material including the MIC.
710 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100711 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100712}
713
714
715/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100716 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200717 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100718struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200719 __le16 pkt_len;
720 __u8 sq2;
721 __u8 rate;
722 __le32 pkt_phys_addr;
723 __le32 next_rxd_phys_addr;
724 __le16 qos_control;
725 __le16 htsig2;
726 __le32 hw_rssi_info;
727 __le32 hw_noise_floor_info;
728 __u8 noise_floor;
729 __u8 pad0[3];
730 __u8 rssi;
731 __u8 rx_status;
732 __u8 channel;
733 __u8 rx_ctrl;
734} __attribute__((packed));
735
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100736#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
737#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
738#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100739
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100740#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200741
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100742static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200743{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100744 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200745
746 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100747 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200748}
749
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100750static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200751{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100752 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200753
754 rxd->pkt_len = cpu_to_le16(len);
755 rxd->pkt_phys_addr = cpu_to_le32(addr);
756 wmb();
757 rxd->rx_ctrl = 0;
758}
759
760static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100761mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
762 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200763{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100764 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200765
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100766 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200767 return -1;
768 rmb();
769
770 memset(status, 0, sizeof(*status));
771
772 status->signal = -rxd->rssi;
773 status->noise = -rxd->noise_floor;
774
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100775 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200776 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100777 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100778 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100779 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200780 } else {
781 int i;
782
783 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
784 if (mwl8k_rates[i].hw_value == rxd->rate) {
785 status->rate_idx = i;
786 break;
787 }
788 }
789 }
790
791 status->band = IEEE80211_BAND_2GHZ;
792 status->freq = ieee80211_channel_to_frequency(rxd->channel);
793
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100794 *qos = rxd->qos_control;
795
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200796 return le16_to_cpu(rxd->pkt_len);
797}
798
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100799static struct rxd_ops rxd_8366_ap_ops = {
800 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
801 .rxd_init = mwl8k_rxd_8366_ap_init,
802 .rxd_refill = mwl8k_rxd_8366_ap_refill,
803 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200804};
805
806/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100807 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100808 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100809struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100810 __le16 pkt_len;
811 __u8 link_quality;
812 __u8 noise_level;
813 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200814 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100815 __le16 qos_control;
816 __le16 rate_info;
817 __le32 pad0[4];
818 __u8 rssi;
819 __u8 channel;
820 __le16 pad1;
821 __u8 rx_ctrl;
822 __u8 rx_status;
823 __u8 pad2[2];
824} __attribute__((packed));
825
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100826#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
827#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
828#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
829#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
830#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
831#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200832
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100833#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200834
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100835static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200836{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100837 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200838
839 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100840 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200841}
842
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100843static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200844{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100845 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200846
847 rxd->pkt_len = cpu_to_le16(len);
848 rxd->pkt_phys_addr = cpu_to_le32(addr);
849 wmb();
850 rxd->rx_ctrl = 0;
851}
852
853static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100854mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100855 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200856{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100857 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200858 u16 rate_info;
859
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100860 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200861 return -1;
862 rmb();
863
864 rate_info = le16_to_cpu(rxd->rate_info);
865
866 memset(status, 0, sizeof(*status));
867
868 status->signal = -rxd->rssi;
869 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100870 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
871 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200872
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100873 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200874 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100875 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200876 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100877 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200878 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100879 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200880 status->flag |= RX_FLAG_HT;
881
882 status->band = IEEE80211_BAND_2GHZ;
883 status->freq = ieee80211_channel_to_frequency(rxd->channel);
884
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100885 *qos = rxd->qos_control;
886
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200887 return le16_to_cpu(rxd->pkt_len);
888}
889
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100890static struct rxd_ops rxd_sta_ops = {
891 .rxd_size = sizeof(struct mwl8k_rxd_sta),
892 .rxd_init = mwl8k_rxd_sta_init,
893 .rxd_refill = mwl8k_rxd_sta_refill,
894 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200895};
896
897
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100898#define MWL8K_RX_DESCS 256
899#define MWL8K_RX_MAXSZ 3800
900
901static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
902{
903 struct mwl8k_priv *priv = hw->priv;
904 struct mwl8k_rx_queue *rxq = priv->rxq + index;
905 int size;
906 int i;
907
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200908 rxq->rxd_count = 0;
909 rxq->head = 0;
910 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100911
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200912 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100913
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200914 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
915 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100916 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200917 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100918 return -ENOMEM;
919 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200920 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100921
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200922 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
923 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100924 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200925 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200926 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100927 return -ENOMEM;
928 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200929 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100930
931 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200932 int desc_size;
933 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100934 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200935 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100936
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200937 desc_size = priv->rxd_ops->rxd_size;
938 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100939
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200940 nexti = i + 1;
941 if (nexti == MWL8K_RX_DESCS)
942 nexti = 0;
943 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
944
945 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100946 }
947
948 return 0;
949}
950
951static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
952{
953 struct mwl8k_priv *priv = hw->priv;
954 struct mwl8k_rx_queue *rxq = priv->rxq + index;
955 int refilled;
956
957 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200958 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100959 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200960 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100961 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200962 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100963
964 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
965 if (skb == NULL)
966 break;
967
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200968 addr = pci_map_single(priv->pdev, skb->data,
969 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100970
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200971 rxq->rxd_count++;
972 rx = rxq->tail++;
973 if (rxq->tail == MWL8K_RX_DESCS)
974 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200975 rxq->buf[rx].skb = skb;
976 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200977
978 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
979 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100980
981 refilled++;
982 }
983
984 return refilled;
985}
986
987/* Must be called only when the card's reception is completely halted */
988static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
989{
990 struct mwl8k_priv *priv = hw->priv;
991 struct mwl8k_rx_queue *rxq = priv->rxq + index;
992 int i;
993
994 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200995 if (rxq->buf[i].skb != NULL) {
996 pci_unmap_single(priv->pdev,
997 pci_unmap_addr(&rxq->buf[i], dma),
998 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
999 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001000
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001001 kfree_skb(rxq->buf[i].skb);
1002 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001003 }
1004 }
1005
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001006 kfree(rxq->buf);
1007 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001008
1009 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001010 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001011 rxq->rxd, rxq->rxd_dma);
1012 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001013}
1014
1015
1016/*
1017 * Scan a list of BSSIDs to process for finalize join.
1018 * Allows for extension to process multiple BSSIDs.
1019 */
1020static inline int
1021mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1022{
1023 return priv->capture_beacon &&
1024 ieee80211_is_beacon(wh->frame_control) &&
1025 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1026}
1027
Lennert Buytenhek37797522009-10-22 20:19:45 +02001028static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1029 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001030{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001031 struct mwl8k_priv *priv = hw->priv;
1032
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001033 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001034 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001035
1036 /*
1037 * Use GFP_ATOMIC as rxq_process is called from
1038 * the primary interrupt handler, memory allocation call
1039 * must not sleep.
1040 */
1041 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1042 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001043 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001044}
1045
1046static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1047{
1048 struct mwl8k_priv *priv = hw->priv;
1049 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1050 int processed;
1051
1052 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001053 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001054 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001055 void *rxd;
1056 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001057 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001058 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001059
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001060 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001061 if (skb == NULL)
1062 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001063
1064 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1065
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001066 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001067 if (pkt_len < 0)
1068 break;
1069
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001070 rxq->buf[rxq->head].skb = NULL;
1071
1072 pci_unmap_single(priv->pdev,
1073 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1074 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1075 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001076
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001077 rxq->head++;
1078 if (rxq->head == MWL8K_RX_DESCS)
1079 rxq->head = 0;
1080
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001081 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001082
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001083 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001084 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001085
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001086 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001087 * Check for a pending join operation. Save a
1088 * copy of the beacon and schedule a tasklet to
1089 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001090 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001091 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001092 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001093
Johannes Bergf1d58c22009-06-17 13:13:00 +02001094 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1095 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001096
1097 processed++;
1098 }
1099
1100 return processed;
1101}
1102
1103
1104/*
1105 * Packet transmission.
1106 */
1107
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001108#define MWL8K_TXD_STATUS_OK 0x00000001
1109#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1110#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1111#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001112#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001113
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001114#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1115#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1116#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1117#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1118#define MWL8K_QOS_EOSP 0x0010
1119
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001120struct mwl8k_tx_desc {
1121 __le32 status;
1122 __u8 data_rate;
1123 __u8 tx_priority;
1124 __le16 qos_control;
1125 __le32 pkt_phys_addr;
1126 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001127 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001128 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001129 __le32 reserved;
1130 __le16 rate_info;
1131 __u8 peer_id;
1132 __u8 tx_frag_cnt;
1133} __attribute__((packed));
1134
1135#define MWL8K_TX_DESCS 128
1136
1137static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1138{
1139 struct mwl8k_priv *priv = hw->priv;
1140 struct mwl8k_tx_queue *txq = priv->txq + index;
1141 int size;
1142 int i;
1143
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001144 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1145 txq->stats.limit = MWL8K_TX_DESCS;
1146 txq->head = 0;
1147 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001148
1149 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1150
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001151 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1152 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001153 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001154 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001155 return -ENOMEM;
1156 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001157 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001158
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001159 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1160 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001161 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001162 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001163 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001164 return -ENOMEM;
1165 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001166 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167
1168 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1169 struct mwl8k_tx_desc *tx_desc;
1170 int nexti;
1171
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001172 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001173 nexti = (i + 1) % MWL8K_TX_DESCS;
1174
1175 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001176 tx_desc->next_txd_phys_addr =
1177 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001178 }
1179
1180 return 0;
1181}
1182
1183static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1184{
1185 iowrite32(MWL8K_H2A_INT_PPA_READY,
1186 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1187 iowrite32(MWL8K_H2A_INT_DUMMY,
1188 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1189 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1190}
1191
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001192static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001193{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001194 struct mwl8k_priv *priv = hw->priv;
1195 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001196
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001197 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1198 struct mwl8k_tx_queue *txq = priv->txq + i;
1199 int fw_owned = 0;
1200 int drv_owned = 0;
1201 int unused = 0;
1202 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001203
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001204 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001205 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1206 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001207
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001208 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001209 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001210 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001211 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001212 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001213
1214 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001215 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001216 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001217
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001218 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1219 "fw_owned=%d drv_owned=%d unused=%d\n",
1220 wiphy_name(hw->wiphy), i,
1221 txq->stats.len, txq->head, txq->tail,
1222 fw_owned, drv_owned, unused);
1223 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001224}
1225
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001226/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001227 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001228 */
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001229#define MWL8K_TX_WAIT_TIMEOUT_MS 1000
1230
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001231static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001232{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001233 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001234 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001235 int retry;
1236 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001237
1238 might_sleep();
1239
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001240 /*
1241 * The TX queues are stopped at this point, so this test
1242 * doesn't need to take ->tx_lock.
1243 */
1244 if (!priv->pending_tx_pkts)
1245 return 0;
1246
1247 retry = 0;
1248 rc = 0;
1249
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001250 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001251 priv->tx_wait = &tx_wait;
1252 while (!rc) {
1253 int oldcount;
1254 unsigned long timeout;
1255
1256 oldcount = priv->pending_tx_pkts;
1257
1258 spin_unlock_bh(&priv->tx_lock);
1259 timeout = wait_for_completion_timeout(&tx_wait,
1260 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1261 spin_lock_bh(&priv->tx_lock);
1262
1263 if (timeout) {
1264 WARN_ON(priv->pending_tx_pkts);
1265 if (retry) {
1266 printk(KERN_NOTICE "%s: tx rings drained\n",
1267 wiphy_name(hw->wiphy));
1268 }
1269 break;
1270 }
1271
1272 if (priv->pending_tx_pkts < oldcount) {
1273 printk(KERN_NOTICE "%s: timeout waiting for tx "
1274 "rings to drain (%d -> %d pkts), retrying\n",
1275 wiphy_name(hw->wiphy), oldcount,
1276 priv->pending_tx_pkts);
1277 retry = 1;
1278 continue;
1279 }
1280
1281 priv->tx_wait = NULL;
1282
1283 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1284 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1285 mwl8k_dump_tx_rings(hw);
1286
1287 rc = -ETIMEDOUT;
1288 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001289 spin_unlock_bh(&priv->tx_lock);
1290
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001291 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001292}
1293
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001294#define MWL8K_TXD_SUCCESS(status) \
1295 ((status) & (MWL8K_TXD_STATUS_OK | \
1296 MWL8K_TXD_STATUS_OK_RETRY | \
1297 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001298
1299static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1300{
1301 struct mwl8k_priv *priv = hw->priv;
1302 struct mwl8k_tx_queue *txq = priv->txq + index;
1303 int wake = 0;
1304
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001305 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001306 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001307 struct mwl8k_tx_desc *tx_desc;
1308 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001309 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001310 struct sk_buff *skb;
1311 struct ieee80211_tx_info *info;
1312 u32 status;
1313
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001314 tx = txq->head;
1315 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001316
1317 status = le32_to_cpu(tx_desc->status);
1318
1319 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1320 if (!force)
1321 break;
1322 tx_desc->status &=
1323 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1324 }
1325
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001326 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1327 BUG_ON(txq->stats.len == 0);
1328 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001329 priv->pending_tx_pkts--;
1330
1331 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001332 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001333 skb = txq->skb[tx];
1334 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001335
1336 BUG_ON(skb == NULL);
1337 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1338
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001339 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340
1341 /* Mark descriptor as unused */
1342 tx_desc->pkt_phys_addr = 0;
1343 tx_desc->pkt_len = 0;
1344
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345 info = IEEE80211_SKB_CB(skb);
1346 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001347 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001348 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001349
1350 ieee80211_tx_status_irqsafe(hw, skb);
1351
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001352 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001353 }
1354
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001355 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001356 ieee80211_wake_queue(hw, index);
1357}
1358
1359/* must be called only when the card's transmit is completely halted */
1360static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1361{
1362 struct mwl8k_priv *priv = hw->priv;
1363 struct mwl8k_tx_queue *txq = priv->txq + index;
1364
1365 mwl8k_txq_reclaim(hw, index, 1);
1366
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001367 kfree(txq->skb);
1368 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001369
1370 pci_free_consistent(priv->pdev,
1371 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001372 txq->txd, txq->txd_dma);
1373 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001374}
1375
1376static int
1377mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1378{
1379 struct mwl8k_priv *priv = hw->priv;
1380 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001381 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001382 struct ieee80211_hdr *wh;
1383 struct mwl8k_tx_queue *txq;
1384 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001385 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001386 u32 txstatus;
1387 u8 txdatarate;
1388 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001389
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001390 wh = (struct ieee80211_hdr *)skb->data;
1391 if (ieee80211_is_data_qos(wh->frame_control))
1392 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1393 else
1394 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001395
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001396 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001397 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001398
1399 tx_info = IEEE80211_SKB_CB(skb);
1400 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001401
1402 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1403 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001404
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001405 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1406 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1407 mwl8k_vif->seqno = seqno++ % 4096;
1408 }
1409
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001410 /* Setup firmware control bit fields for each frame type. */
1411 txstatus = 0;
1412 txdatarate = 0;
1413 if (ieee80211_is_mgmt(wh->frame_control) ||
1414 ieee80211_is_ctl(wh->frame_control)) {
1415 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001416 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001417 } else if (ieee80211_is_data(wh->frame_control)) {
1418 txdatarate = 1;
1419 if (is_multicast_ether_addr(wh->addr1))
1420 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1421
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001422 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001423 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001424 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001425 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001426 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001427 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001428
1429 dma = pci_map_single(priv->pdev, skb->data,
1430 skb->len, PCI_DMA_TODEVICE);
1431
1432 if (pci_dma_mapping_error(priv->pdev, dma)) {
1433 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001434 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001435 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001436 return NETDEV_TX_OK;
1437 }
1438
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439 spin_lock_bh(&priv->tx_lock);
1440
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001441 txq = priv->txq + index;
1442
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001443 BUG_ON(txq->skb[txq->tail] != NULL);
1444 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001445
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001446 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001447 tx->data_rate = txdatarate;
1448 tx->tx_priority = index;
1449 tx->qos_control = cpu_to_le16(qos);
1450 tx->pkt_phys_addr = cpu_to_le32(dma);
1451 tx->pkt_len = cpu_to_le16(skb->len);
1452 tx->rate_info = 0;
1453 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001454 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001455 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1456
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001457 txq->stats.count++;
1458 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001459 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001460
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001461 txq->tail++;
1462 if (txq->tail == MWL8K_TX_DESCS)
1463 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001464
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001465 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001466 ieee80211_stop_queue(hw, index);
1467
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001468 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001469
1470 spin_unlock_bh(&priv->tx_lock);
1471
1472 return NETDEV_TX_OK;
1473}
1474
1475
1476/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001477 * Firmware access.
1478 *
1479 * We have the following requirements for issuing firmware commands:
1480 * - Some commands require that the packet transmit path is idle when
1481 * the command is issued. (For simplicity, we'll just quiesce the
1482 * transmit path for every command.)
1483 * - There are certain sequences of commands that need to be issued to
1484 * the hardware sequentially, with no other intervening commands.
1485 *
1486 * This leads to an implementation of a "firmware lock" as a mutex that
1487 * can be taken recursively, and which is taken by both the low-level
1488 * command submission function (mwl8k_post_cmd) as well as any users of
1489 * that function that require issuing of an atomic sequence of commands,
1490 * and quiesces the transmit path whenever it's taken.
1491 */
1492static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1493{
1494 struct mwl8k_priv *priv = hw->priv;
1495
1496 if (priv->fw_mutex_owner != current) {
1497 int rc;
1498
1499 mutex_lock(&priv->fw_mutex);
1500 ieee80211_stop_queues(hw);
1501
1502 rc = mwl8k_tx_wait_empty(hw);
1503 if (rc) {
1504 ieee80211_wake_queues(hw);
1505 mutex_unlock(&priv->fw_mutex);
1506
1507 return rc;
1508 }
1509
1510 priv->fw_mutex_owner = current;
1511 }
1512
1513 priv->fw_mutex_depth++;
1514
1515 return 0;
1516}
1517
1518static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1519{
1520 struct mwl8k_priv *priv = hw->priv;
1521
1522 if (!--priv->fw_mutex_depth) {
1523 ieee80211_wake_queues(hw);
1524 priv->fw_mutex_owner = NULL;
1525 mutex_unlock(&priv->fw_mutex);
1526 }
1527}
1528
1529
1530/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001531 * Command processing.
1532 */
1533
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001534/* Timeout firmware commands after 10s */
1535#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001536
1537static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1538{
1539 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1540 struct mwl8k_priv *priv = hw->priv;
1541 void __iomem *regs = priv->regs;
1542 dma_addr_t dma_addr;
1543 unsigned int dma_size;
1544 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001545 unsigned long timeout = 0;
1546 u8 buf[32];
1547
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001548 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001549 dma_size = le16_to_cpu(cmd->length);
1550 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1551 PCI_DMA_BIDIRECTIONAL);
1552 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1553 return -ENOMEM;
1554
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001555 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001556 if (rc) {
1557 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1558 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001559 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001560 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001561
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001562 priv->hostcmd_wait = &cmd_wait;
1563 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1564 iowrite32(MWL8K_H2A_INT_DOORBELL,
1565 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1566 iowrite32(MWL8K_H2A_INT_DUMMY,
1567 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001568
1569 timeout = wait_for_completion_timeout(&cmd_wait,
1570 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1571
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001572 priv->hostcmd_wait = NULL;
1573
1574 mwl8k_fw_unlock(hw);
1575
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001576 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1577 PCI_DMA_BIDIRECTIONAL);
1578
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001579 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001580 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001581 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001582 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1583 MWL8K_CMD_TIMEOUT_MS);
1584 rc = -ETIMEDOUT;
1585 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001586 int ms;
1587
1588 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1589
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001590 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001591 if (rc)
1592 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001593 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001594 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001595 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001596 else if (ms > 2000)
1597 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1598 wiphy_name(hw->wiphy),
1599 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1600 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001601 }
1602
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001603 return rc;
1604}
1605
1606/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001607 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001608 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001609struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001610 struct mwl8k_cmd_pkt header;
1611 __u8 hw_rev;
1612 __u8 host_interface;
1613 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001614 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001615 __le16 region_code;
1616 __le32 fw_rev;
1617 __le32 ps_cookie;
1618 __le32 caps;
1619 __u8 mcs_bitmap[16];
1620 __le32 rx_queue_ptr;
1621 __le32 num_tx_queues;
1622 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1623 __le32 caps2;
1624 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001625 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001626} __attribute__((packed));
1627
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001628static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001629{
1630 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001631 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001632 int rc;
1633 int i;
1634
1635 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1636 if (cmd == NULL)
1637 return -ENOMEM;
1638
1639 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1640 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1641
1642 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1643 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001644 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001645 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001646 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001647 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001648 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001649 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001650
1651 rc = mwl8k_post_cmd(hw, &cmd->header);
1652
1653 if (!rc) {
1654 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1655 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001656 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001658 }
1659
1660 kfree(cmd);
1661 return rc;
1662}
1663
1664/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001665 * CMD_GET_HW_SPEC (AP version).
1666 */
1667struct mwl8k_cmd_get_hw_spec_ap {
1668 struct mwl8k_cmd_pkt header;
1669 __u8 hw_rev;
1670 __u8 host_interface;
1671 __le16 num_wcb;
1672 __le16 num_mcaddrs;
1673 __u8 perm_addr[ETH_ALEN];
1674 __le16 region_code;
1675 __le16 num_antenna;
1676 __le32 fw_rev;
1677 __le32 wcbbase0;
1678 __le32 rxwrptr;
1679 __le32 rxrdptr;
1680 __le32 ps_cookie;
1681 __le32 wcbbase1;
1682 __le32 wcbbase2;
1683 __le32 wcbbase3;
1684} __attribute__((packed));
1685
1686static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1687{
1688 struct mwl8k_priv *priv = hw->priv;
1689 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1690 int rc;
1691
1692 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1693 if (cmd == NULL)
1694 return -ENOMEM;
1695
1696 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1697 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1698
1699 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1700 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1701
1702 rc = mwl8k_post_cmd(hw, &cmd->header);
1703
1704 if (!rc) {
1705 int off;
1706
1707 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1708 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1709 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1710 priv->hw_rev = cmd->hw_rev;
1711
1712 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1713 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1714
1715 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1716 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1717
1718 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1719 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1720
1721 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1722 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1723
1724 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1725 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1726
1727 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1728 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1729 }
1730
1731 kfree(cmd);
1732 return rc;
1733}
1734
1735/*
1736 * CMD_SET_HW_SPEC.
1737 */
1738struct mwl8k_cmd_set_hw_spec {
1739 struct mwl8k_cmd_pkt header;
1740 __u8 hw_rev;
1741 __u8 host_interface;
1742 __le16 num_mcaddrs;
1743 __u8 perm_addr[ETH_ALEN];
1744 __le16 region_code;
1745 __le32 fw_rev;
1746 __le32 ps_cookie;
1747 __le32 caps;
1748 __le32 rx_queue_ptr;
1749 __le32 num_tx_queues;
1750 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1751 __le32 flags;
1752 __le32 num_tx_desc_per_queue;
1753 __le32 total_rxd;
1754} __attribute__((packed));
1755
1756#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1757
1758static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1759{
1760 struct mwl8k_priv *priv = hw->priv;
1761 struct mwl8k_cmd_set_hw_spec *cmd;
1762 int rc;
1763 int i;
1764
1765 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1766 if (cmd == NULL)
1767 return -ENOMEM;
1768
1769 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1770 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1771
1772 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1773 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1774 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1775 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1776 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1777 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1778 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1779 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1780
1781 rc = mwl8k_post_cmd(hw, &cmd->header);
1782 kfree(cmd);
1783
1784 return rc;
1785}
1786
1787/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001788 * CMD_MAC_MULTICAST_ADR.
1789 */
1790struct mwl8k_cmd_mac_multicast_adr {
1791 struct mwl8k_cmd_pkt header;
1792 __le16 action;
1793 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001794 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001795};
1796
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001797#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1798#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1799#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1800#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001801
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001802static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001803__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001804 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001805{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001806 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001807 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001808 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001809
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001810 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001811 allmulti = 1;
1812 mc_count = 0;
1813 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001814
1815 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1816
1817 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001818 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001819 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001820
1821 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1822 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001823 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1824 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001825
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001826 if (allmulti) {
1827 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1828 } else if (mc_count) {
1829 int i;
1830
1831 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1832 cmd->numaddr = cpu_to_le16(mc_count);
1833 for (i = 0; i < mc_count && mclist; i++) {
1834 if (mclist->da_addrlen != ETH_ALEN) {
1835 kfree(cmd);
1836 return NULL;
1837 }
1838 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1839 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001840 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001841 }
1842
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001843 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001844}
1845
1846/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001847 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001848 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001849struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001850 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001851 __le32 stats[64];
1852} __attribute__((packed));
1853
1854#define MWL8K_STAT_ACK_FAILURE 9
1855#define MWL8K_STAT_RTS_FAILURE 12
1856#define MWL8K_STAT_FCS_ERROR 24
1857#define MWL8K_STAT_RTS_SUCCESS 11
1858
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001859static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1860 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001861{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001862 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001863 int rc;
1864
1865 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1866 if (cmd == NULL)
1867 return -ENOMEM;
1868
1869 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1870 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001871
1872 rc = mwl8k_post_cmd(hw, &cmd->header);
1873 if (!rc) {
1874 stats->dot11ACKFailureCount =
1875 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1876 stats->dot11RTSFailureCount =
1877 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1878 stats->dot11FCSErrorCount =
1879 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1880 stats->dot11RTSSuccessCount =
1881 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1882 }
1883 kfree(cmd);
1884
1885 return rc;
1886}
1887
1888/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001889 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001890 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001891struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001892 struct mwl8k_cmd_pkt header;
1893 __le16 action;
1894 __le16 control;
1895 __le16 radio_on;
1896} __attribute__((packed));
1897
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001898static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001899mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001900{
1901 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001902 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001903 int rc;
1904
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001905 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001906 return 0;
1907
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001908 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1909 if (cmd == NULL)
1910 return -ENOMEM;
1911
1912 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1913 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1914 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001915 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001916 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1917
1918 rc = mwl8k_post_cmd(hw, &cmd->header);
1919 kfree(cmd);
1920
1921 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001922 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001923
1924 return rc;
1925}
1926
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001927static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001928{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001929 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001930}
1931
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001932static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001933{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001934 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001935}
1936
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001937static int
1938mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1939{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001940 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001941
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001942 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001943
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001944 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001945}
1946
1947/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001948 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001949 */
1950#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1951
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001952struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001953 struct mwl8k_cmd_pkt header;
1954 __le16 action;
1955 __le16 support_level;
1956 __le16 current_level;
1957 __le16 reserved;
1958 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1959} __attribute__((packed));
1960
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001961static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001962{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001963 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001964 int rc;
1965
1966 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1967 if (cmd == NULL)
1968 return -ENOMEM;
1969
1970 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1971 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1972 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1973 cmd->support_level = cpu_to_le16(dBm);
1974
1975 rc = mwl8k_post_cmd(hw, &cmd->header);
1976 kfree(cmd);
1977
1978 return rc;
1979}
1980
1981/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02001982 * CMD_RF_ANTENNA.
1983 */
1984struct mwl8k_cmd_rf_antenna {
1985 struct mwl8k_cmd_pkt header;
1986 __le16 antenna;
1987 __le16 mode;
1988} __attribute__((packed));
1989
1990#define MWL8K_RF_ANTENNA_RX 1
1991#define MWL8K_RF_ANTENNA_TX 2
1992
1993static int
1994mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
1995{
1996 struct mwl8k_cmd_rf_antenna *cmd;
1997 int rc;
1998
1999 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2000 if (cmd == NULL)
2001 return -ENOMEM;
2002
2003 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2004 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2005 cmd->antenna = cpu_to_le16(antenna);
2006 cmd->mode = cpu_to_le16(mask);
2007
2008 rc = mwl8k_post_cmd(hw, &cmd->header);
2009 kfree(cmd);
2010
2011 return rc;
2012}
2013
2014/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002015 * CMD_SET_PRE_SCAN.
2016 */
2017struct mwl8k_cmd_set_pre_scan {
2018 struct mwl8k_cmd_pkt header;
2019} __attribute__((packed));
2020
2021static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2022{
2023 struct mwl8k_cmd_set_pre_scan *cmd;
2024 int rc;
2025
2026 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2027 if (cmd == NULL)
2028 return -ENOMEM;
2029
2030 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2031 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2032
2033 rc = mwl8k_post_cmd(hw, &cmd->header);
2034 kfree(cmd);
2035
2036 return rc;
2037}
2038
2039/*
2040 * CMD_SET_POST_SCAN.
2041 */
2042struct mwl8k_cmd_set_post_scan {
2043 struct mwl8k_cmd_pkt header;
2044 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002045 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002046} __attribute__((packed));
2047
2048static int
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002049mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002050{
2051 struct mwl8k_cmd_set_post_scan *cmd;
2052 int rc;
2053
2054 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2055 if (cmd == NULL)
2056 return -ENOMEM;
2057
2058 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2059 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2060 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002061 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002062
2063 rc = mwl8k_post_cmd(hw, &cmd->header);
2064 kfree(cmd);
2065
2066 return rc;
2067}
2068
2069/*
2070 * CMD_SET_RF_CHANNEL.
2071 */
2072struct mwl8k_cmd_set_rf_channel {
2073 struct mwl8k_cmd_pkt header;
2074 __le16 action;
2075 __u8 current_channel;
2076 __le32 channel_flags;
2077} __attribute__((packed));
2078
2079static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2080 struct ieee80211_channel *channel)
2081{
2082 struct mwl8k_cmd_set_rf_channel *cmd;
2083 int rc;
2084
2085 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2086 if (cmd == NULL)
2087 return -ENOMEM;
2088
2089 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2090 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2091 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2092 cmd->current_channel = channel->hw_value;
2093 if (channel->band == IEEE80211_BAND_2GHZ)
2094 cmd->channel_flags = cpu_to_le32(0x00000081);
2095 else
2096 cmd->channel_flags = cpu_to_le32(0x00000000);
2097
2098 rc = mwl8k_post_cmd(hw, &cmd->header);
2099 kfree(cmd);
2100
2101 return rc;
2102}
2103
2104/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002105 * CMD_SET_AID.
2106 */
2107#define MWL8K_FRAME_PROT_DISABLED 0x00
2108#define MWL8K_FRAME_PROT_11G 0x07
2109#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2110#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2111
2112struct mwl8k_cmd_update_set_aid {
2113 struct mwl8k_cmd_pkt header;
2114 __le16 aid;
2115
2116 /* AP's MAC address (BSSID) */
2117 __u8 bssid[ETH_ALEN];
2118 __le16 protection_mode;
2119 __u8 supp_rates[14];
2120} __attribute__((packed));
2121
2122static int
2123mwl8k_cmd_set_aid(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2124{
2125 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2126 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2127 struct mwl8k_cmd_update_set_aid *cmd;
2128 u16 prot_mode;
2129 int rc;
2130
2131 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2132 if (cmd == NULL)
2133 return -ENOMEM;
2134
2135 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2136 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2137 cmd->aid = cpu_to_le16(info->aid);
2138
2139 memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
2140
2141 if (info->use_cts_prot) {
2142 prot_mode = MWL8K_FRAME_PROT_11G;
2143 } else {
2144 switch (info->ht_operation_mode &
2145 IEEE80211_HT_OP_MODE_PROTECTION) {
2146 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2147 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2148 break;
2149 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2150 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2151 break;
2152 default:
2153 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2154 break;
2155 }
2156 }
2157 cmd->protection_mode = cpu_to_le16(prot_mode);
2158
2159 memcpy(cmd->supp_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2160
2161 rc = mwl8k_post_cmd(hw, &cmd->header);
2162 kfree(cmd);
2163
2164 return rc;
2165}
2166
2167/*
2168 * CMD_SET_RATE.
2169 */
2170struct mwl8k_cmd_set_rate {
2171 struct mwl8k_cmd_pkt header;
2172 __u8 legacy_rates[14];
2173
2174 /* Bitmap for supported MCS codes. */
2175 __u8 mcs_set[16];
2176 __u8 reserved[16];
2177} __attribute__((packed));
2178
2179static int
2180mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2181{
2182 struct mwl8k_cmd_set_rate *cmd;
2183 int rc;
2184
2185 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2186 if (cmd == NULL)
2187 return -ENOMEM;
2188
2189 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2190 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2191 memcpy(cmd->legacy_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2192
2193 rc = mwl8k_post_cmd(hw, &cmd->header);
2194 kfree(cmd);
2195
2196 return rc;
2197}
2198
2199/*
2200 * CMD_FINALIZE_JOIN.
2201 */
2202#define MWL8K_FJ_BEACON_MAXLEN 128
2203
2204struct mwl8k_cmd_finalize_join {
2205 struct mwl8k_cmd_pkt header;
2206 __le32 sleep_interval; /* Number of beacon periods to sleep */
2207 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2208} __attribute__((packed));
2209
2210static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2211 int framelen, int dtim)
2212{
2213 struct mwl8k_cmd_finalize_join *cmd;
2214 struct ieee80211_mgmt *payload = frame;
2215 int payload_len;
2216 int rc;
2217
2218 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2219 if (cmd == NULL)
2220 return -ENOMEM;
2221
2222 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2223 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2224 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2225
2226 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2227 if (payload_len < 0)
2228 payload_len = 0;
2229 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2230 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2231
2232 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2233
2234 rc = mwl8k_post_cmd(hw, &cmd->header);
2235 kfree(cmd);
2236
2237 return rc;
2238}
2239
2240/*
2241 * CMD_SET_RTS_THRESHOLD.
2242 */
2243struct mwl8k_cmd_set_rts_threshold {
2244 struct mwl8k_cmd_pkt header;
2245 __le16 action;
2246 __le16 threshold;
2247} __attribute__((packed));
2248
2249static int mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw,
2250 u16 action, u16 threshold)
2251{
2252 struct mwl8k_cmd_set_rts_threshold *cmd;
2253 int rc;
2254
2255 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2256 if (cmd == NULL)
2257 return -ENOMEM;
2258
2259 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2260 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2261 cmd->action = cpu_to_le16(action);
2262 cmd->threshold = cpu_to_le16(threshold);
2263
2264 rc = mwl8k_post_cmd(hw, &cmd->header);
2265 kfree(cmd);
2266
2267 return rc;
2268}
2269
2270/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002271 * CMD_SET_SLOT.
2272 */
2273struct mwl8k_cmd_set_slot {
2274 struct mwl8k_cmd_pkt header;
2275 __le16 action;
2276 __u8 short_slot;
2277} __attribute__((packed));
2278
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002279static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002280{
2281 struct mwl8k_cmd_set_slot *cmd;
2282 int rc;
2283
2284 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2285 if (cmd == NULL)
2286 return -ENOMEM;
2287
2288 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2289 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2290 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002291 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002292
2293 rc = mwl8k_post_cmd(hw, &cmd->header);
2294 kfree(cmd);
2295
2296 return rc;
2297}
2298
2299/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002300 * CMD_SET_EDCA_PARAMS.
2301 */
2302struct mwl8k_cmd_set_edca_params {
2303 struct mwl8k_cmd_pkt header;
2304
2305 /* See MWL8K_SET_EDCA_XXX below */
2306 __le16 action;
2307
2308 /* TX opportunity in units of 32 us */
2309 __le16 txop;
2310
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002311 union {
2312 struct {
2313 /* Log exponent of max contention period: 0...15 */
2314 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002315
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002316 /* Log exponent of min contention period: 0...15 */
2317 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002318
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002319 /* Adaptive interframe spacing in units of 32us */
2320 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002321
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002322 /* TX queue to configure */
2323 __u8 txq;
2324 } ap;
2325 struct {
2326 /* Log exponent of max contention period: 0...15 */
2327 __u8 log_cw_max;
2328
2329 /* Log exponent of min contention period: 0...15 */
2330 __u8 log_cw_min;
2331
2332 /* Adaptive interframe spacing in units of 32us */
2333 __u8 aifs;
2334
2335 /* TX queue to configure */
2336 __u8 txq;
2337 } sta;
2338 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002339} __attribute__((packed));
2340
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002341#define MWL8K_SET_EDCA_CW 0x01
2342#define MWL8K_SET_EDCA_TXOP 0x02
2343#define MWL8K_SET_EDCA_AIFS 0x04
2344
2345#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2346 MWL8K_SET_EDCA_TXOP | \
2347 MWL8K_SET_EDCA_AIFS)
2348
2349static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002350mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2351 __u16 cw_min, __u16 cw_max,
2352 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002353{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002354 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002355 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002356 int rc;
2357
2358 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2359 if (cmd == NULL)
2360 return -ENOMEM;
2361
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002362 /*
2363 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2364 * this call.
2365 */
2366 qnum ^= !(qnum >> 1);
2367
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002368 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2369 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002370 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2371 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002372 if (priv->ap_fw) {
2373 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2374 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2375 cmd->ap.aifs = aifs;
2376 cmd->ap.txq = qnum;
2377 } else {
2378 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2379 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2380 cmd->sta.aifs = aifs;
2381 cmd->sta.txq = qnum;
2382 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002383
2384 rc = mwl8k_post_cmd(hw, &cmd->header);
2385 kfree(cmd);
2386
2387 return rc;
2388}
2389
2390/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002391 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002392 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002393struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002394 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002395 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002396} __attribute__((packed));
2397
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002398static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002399{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002400 struct mwl8k_priv *priv = hw->priv;
2401 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002402 int rc;
2403
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002404 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2405 if (cmd == NULL)
2406 return -ENOMEM;
2407
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002408 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002409 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002410 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002411
2412 rc = mwl8k_post_cmd(hw, &cmd->header);
2413 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002414
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002415 if (!rc)
2416 priv->wmm_enabled = enable;
2417
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002418 return rc;
2419}
2420
2421/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002422 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002423 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002424struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002425 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002426 __le32 action;
2427 __u8 rx_antenna_map;
2428 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002429} __attribute__((packed));
2430
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002431static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002432{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002433 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002434 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002435
2436 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2437 if (cmd == NULL)
2438 return -ENOMEM;
2439
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002440 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002441 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002442 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2443 cmd->rx_antenna_map = rx;
2444 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002445
2446 rc = mwl8k_post_cmd(hw, &cmd->header);
2447 kfree(cmd);
2448
2449 return rc;
2450}
2451
2452/*
2453 * CMD_USE_FIXED_RATE.
2454 */
2455#define MWL8K_RATE_TABLE_SIZE 8
2456#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002457#define MWL8K_USE_AUTO_RATE 0x0002
2458
2459struct mwl8k_rate_entry {
2460 /* Set to 1 if HT rate, 0 if legacy. */
2461 __le32 is_ht_rate;
2462
2463 /* Set to 1 to use retry_count field. */
2464 __le32 enable_retry;
2465
2466 /* Specified legacy rate or MCS. */
2467 __le32 rate;
2468
2469 /* Number of allowed retries. */
2470 __le32 retry_count;
2471} __attribute__((packed));
2472
2473struct mwl8k_rate_table {
2474 /* 1 to allow specified rate and below */
2475 __le32 allow_rate_drop;
2476 __le32 num_rates;
2477 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2478} __attribute__((packed));
2479
2480struct mwl8k_cmd_use_fixed_rate {
2481 struct mwl8k_cmd_pkt header;
2482 __le32 action;
2483 struct mwl8k_rate_table rate_table;
2484
2485 /* Unicast, Broadcast or Multicast */
2486 __le32 rate_type;
2487 __le32 reserved1;
2488 __le32 reserved2;
2489} __attribute__((packed));
2490
2491static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2492 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2493{
2494 struct mwl8k_cmd_use_fixed_rate *cmd;
2495 int count;
2496 int rc;
2497
2498 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2499 if (cmd == NULL)
2500 return -ENOMEM;
2501
2502 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2503 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2504
2505 cmd->action = cpu_to_le32(action);
2506 cmd->rate_type = cpu_to_le32(rate_type);
2507
2508 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002509 /*
2510 * Copy over each field manually so that endian
2511 * conversion can be done.
2512 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002513 cmd->rate_table.allow_rate_drop =
2514 cpu_to_le32(rate_table->allow_rate_drop);
2515 cmd->rate_table.num_rates =
2516 cpu_to_le32(rate_table->num_rates);
2517
2518 for (count = 0; count < rate_table->num_rates; count++) {
2519 struct mwl8k_rate_entry *dst =
2520 &cmd->rate_table.rate_entry[count];
2521 struct mwl8k_rate_entry *src =
2522 &rate_table->rate_entry[count];
2523
2524 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2525 dst->enable_retry = cpu_to_le32(src->enable_retry);
2526 dst->rate = cpu_to_le32(src->rate);
2527 dst->retry_count = cpu_to_le32(src->retry_count);
2528 }
2529 }
2530
2531 rc = mwl8k_post_cmd(hw, &cmd->header);
2532 kfree(cmd);
2533
2534 return rc;
2535}
2536
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002537/*
2538 * CMD_ENABLE_SNIFFER.
2539 */
2540struct mwl8k_cmd_enable_sniffer {
2541 struct mwl8k_cmd_pkt header;
2542 __le32 action;
2543} __attribute__((packed));
2544
2545static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2546{
2547 struct mwl8k_cmd_enable_sniffer *cmd;
2548 int rc;
2549
2550 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2551 if (cmd == NULL)
2552 return -ENOMEM;
2553
2554 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2555 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2556 cmd->action = cpu_to_le32(!!enable);
2557
2558 rc = mwl8k_post_cmd(hw, &cmd->header);
2559 kfree(cmd);
2560
2561 return rc;
2562}
2563
2564/*
2565 * CMD_SET_MAC_ADDR.
2566 */
2567struct mwl8k_cmd_set_mac_addr {
2568 struct mwl8k_cmd_pkt header;
2569 union {
2570 struct {
2571 __le16 mac_type;
2572 __u8 mac_addr[ETH_ALEN];
2573 } mbss;
2574 __u8 mac_addr[ETH_ALEN];
2575 };
2576} __attribute__((packed));
2577
2578static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2579{
2580 struct mwl8k_priv *priv = hw->priv;
2581 struct mwl8k_cmd_set_mac_addr *cmd;
2582 int rc;
2583
2584 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2585 if (cmd == NULL)
2586 return -ENOMEM;
2587
2588 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2589 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2590 if (priv->ap_fw) {
2591 cmd->mbss.mac_type = 0;
2592 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2593 } else {
2594 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2595 }
2596
2597 rc = mwl8k_post_cmd(hw, &cmd->header);
2598 kfree(cmd);
2599
2600 return rc;
2601}
2602
2603/*
2604 * CMD_SET_RATEADAPT_MODE.
2605 */
2606struct mwl8k_cmd_set_rate_adapt_mode {
2607 struct mwl8k_cmd_pkt header;
2608 __le16 action;
2609 __le16 mode;
2610} __attribute__((packed));
2611
2612static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2613{
2614 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2615 int rc;
2616
2617 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2618 if (cmd == NULL)
2619 return -ENOMEM;
2620
2621 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2622 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2623 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2624 cmd->mode = cpu_to_le16(mode);
2625
2626 rc = mwl8k_post_cmd(hw, &cmd->header);
2627 kfree(cmd);
2628
2629 return rc;
2630}
2631
2632/*
2633 * CMD_UPDATE_STADB.
2634 */
2635struct mwl8k_cmd_update_stadb {
2636 struct mwl8k_cmd_pkt header;
2637
2638 /* See STADB_ACTION_TYPE */
2639 __le32 action;
2640
2641 /* Peer MAC address */
2642 __u8 peer_addr[ETH_ALEN];
2643
2644 __le32 reserved;
2645
2646 /* Peer info - valid during add/update. */
2647 struct peer_capability_info peer_info;
2648} __attribute__((packed));
2649
2650static int mwl8k_cmd_update_stadb(struct ieee80211_hw *hw,
2651 struct ieee80211_vif *vif, __u32 action)
2652{
2653 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
2654 struct ieee80211_bss_conf *info = &mv_vif->bss_info;
2655 struct mwl8k_cmd_update_stadb *cmd;
2656 struct peer_capability_info *peer_info;
2657 int rc;
2658
2659 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2660 if (cmd == NULL)
2661 return -ENOMEM;
2662
2663 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2664 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2665
2666 cmd->action = cpu_to_le32(action);
2667 peer_info = &cmd->peer_info;
2668 memcpy(cmd->peer_addr, mv_vif->bssid, ETH_ALEN);
2669
2670 switch (action) {
2671 case MWL8K_STA_DB_ADD_ENTRY:
2672 case MWL8K_STA_DB_MODIFY_ENTRY:
2673 /* Build peer_info block */
2674 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2675 peer_info->basic_caps = cpu_to_le16(info->assoc_capability);
2676 memcpy(peer_info->legacy_rates, mwl8k_rateids,
2677 sizeof(mwl8k_rateids));
2678 peer_info->interop = 1;
2679 peer_info->amsdu_enabled = 0;
2680
2681 rc = mwl8k_post_cmd(hw, &cmd->header);
2682 if (rc == 0)
2683 mv_vif->peer_id = peer_info->station_id;
2684
2685 break;
2686
2687 case MWL8K_STA_DB_DEL_ENTRY:
2688 case MWL8K_STA_DB_FLUSH:
2689 default:
2690 rc = mwl8k_post_cmd(hw, &cmd->header);
2691 if (rc == 0)
2692 mv_vif->peer_id = 0;
2693 break;
2694 }
2695 kfree(cmd);
2696
2697 return rc;
2698}
2699
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002700
2701/*
2702 * Interrupt handling.
2703 */
2704static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2705{
2706 struct ieee80211_hw *hw = dev_id;
2707 struct mwl8k_priv *priv = hw->priv;
2708 u32 status;
2709
2710 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2711 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2712
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002713 if (!status)
2714 return IRQ_NONE;
2715
2716 if (status & MWL8K_A2H_INT_TX_DONE)
2717 tasklet_schedule(&priv->tx_reclaim_task);
2718
2719 if (status & MWL8K_A2H_INT_RX_READY) {
2720 while (rxq_process(hw, 0, 1))
2721 rxq_refill(hw, 0, 1);
2722 }
2723
2724 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002725 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002726 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002727 }
2728
2729 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002730 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002731 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002732 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002733 }
2734
2735 return IRQ_HANDLED;
2736}
2737
2738
2739/*
2740 * Core driver operations.
2741 */
2742static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2743{
2744 struct mwl8k_priv *priv = hw->priv;
2745 int index = skb_get_queue_mapping(skb);
2746 int rc;
2747
2748 if (priv->current_channel == NULL) {
2749 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002750 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002751 dev_kfree_skb(skb);
2752 return NETDEV_TX_OK;
2753 }
2754
2755 rc = mwl8k_txq_xmit(hw, index, skb);
2756
2757 return rc;
2758}
2759
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002760static int mwl8k_start(struct ieee80211_hw *hw)
2761{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002762 struct mwl8k_priv *priv = hw->priv;
2763 int rc;
2764
Joe Perchesa0607fd2009-11-18 23:29:17 -08002765 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002766 IRQF_SHARED, MWL8K_NAME, hw);
2767 if (rc) {
2768 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002769 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002770 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002771 }
2772
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002773 /* Enable tx reclaim tasklet */
2774 tasklet_enable(&priv->tx_reclaim_task);
2775
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002776 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002777 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002778
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002779 rc = mwl8k_fw_lock(hw);
2780 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002781 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002782
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002783 if (!priv->ap_fw) {
2784 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002785 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002786
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002787 if (!rc)
2788 rc = mwl8k_cmd_set_pre_scan(hw);
2789
2790 if (!rc)
2791 rc = mwl8k_cmd_set_post_scan(hw,
2792 "\x00\x00\x00\x00\x00\x00");
2793 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002794
2795 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002796 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002797
2798 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002799 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002800
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002801 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002802 }
2803
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002804 if (rc) {
2805 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2806 free_irq(priv->pdev->irq, hw);
2807 tasklet_disable(&priv->tx_reclaim_task);
2808 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002809
2810 return rc;
2811}
2812
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002813static void mwl8k_stop(struct ieee80211_hw *hw)
2814{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002815 struct mwl8k_priv *priv = hw->priv;
2816 int i;
2817
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002818 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002819
2820 ieee80211_stop_queues(hw);
2821
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002822 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002823 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002824 free_irq(priv->pdev->irq, hw);
2825
2826 /* Stop finalize join worker */
2827 cancel_work_sync(&priv->finalize_join_worker);
2828 if (priv->beacon_skb != NULL)
2829 dev_kfree_skb(priv->beacon_skb);
2830
2831 /* Stop tx reclaim tasklet */
2832 tasklet_disable(&priv->tx_reclaim_task);
2833
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002834 /* Return all skbs to mac80211 */
2835 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2836 mwl8k_txq_reclaim(hw, i, 1);
2837}
2838
2839static int mwl8k_add_interface(struct ieee80211_hw *hw,
2840 struct ieee80211_if_init_conf *conf)
2841{
2842 struct mwl8k_priv *priv = hw->priv;
2843 struct mwl8k_vif *mwl8k_vif;
2844
2845 /*
2846 * We only support one active interface at a time.
2847 */
2848 if (priv->vif != NULL)
2849 return -EBUSY;
2850
2851 /*
2852 * We only support managed interfaces for now.
2853 */
Lennert Buytenhek240e86e2009-07-16 14:15:44 +02002854 if (conf->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002855 return -EINVAL;
2856
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002857 /*
2858 * Reject interface creation if sniffer mode is active, as
2859 * STA operation is mutually exclusive with hardware sniffer
2860 * mode.
2861 */
2862 if (priv->sniffer_enabled) {
2863 printk(KERN_INFO "%s: unable to create STA "
2864 "interface due to sniffer mode being enabled\n",
2865 wiphy_name(hw->wiphy));
2866 return -EINVAL;
2867 }
2868
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002869 /* Clean out driver private area */
2870 mwl8k_vif = MWL8K_VIF(conf->vif);
2871 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2872
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002873 /* Set and save the mac address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002874 mwl8k_cmd_set_mac_addr(hw, conf->mac_addr);
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002875 memcpy(mwl8k_vif->mac_addr, conf->mac_addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002876
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002877 /* Set Initial sequence number to zero */
2878 mwl8k_vif->seqno = 0;
2879
2880 priv->vif = conf->vif;
2881 priv->current_channel = NULL;
2882
2883 return 0;
2884}
2885
2886static void mwl8k_remove_interface(struct ieee80211_hw *hw,
2887 struct ieee80211_if_init_conf *conf)
2888{
2889 struct mwl8k_priv *priv = hw->priv;
2890
2891 if (priv->vif == NULL)
2892 return;
2893
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002894 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002895
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002896 priv->vif = NULL;
2897}
2898
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002899static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002900{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002901 struct ieee80211_conf *conf = &hw->conf;
2902 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002903 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002904
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002905 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002906 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002907 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002908 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002909 }
2910
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002911 rc = mwl8k_fw_lock(hw);
2912 if (rc)
2913 return rc;
2914
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002915 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002916 if (rc)
2917 goto out;
2918
2919 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2920 if (rc)
2921 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002922
2923 priv->current_channel = conf->channel;
2924
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002925 if (conf->power_level > 18)
2926 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002927 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002928 if (rc)
2929 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002930
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002931 if (priv->ap_fw) {
2932 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2933 if (!rc)
2934 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2935 } else {
2936 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2937 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002938
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002939out:
2940 mwl8k_fw_unlock(hw);
2941
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002942 return rc;
2943}
2944
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002945static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2946 struct ieee80211_vif *vif,
2947 struct ieee80211_bss_conf *info,
2948 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002949{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002950 struct mwl8k_priv *priv = hw->priv;
2951 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002952 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002953
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002954 if ((changed & BSS_CHANGED_ASSOC) == 0)
2955 return;
2956
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002957 priv->capture_beacon = false;
2958
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002959 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002960 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002961 return;
2962
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002963 if (info->assoc) {
2964 memcpy(&mwl8k_vif->bss_info, info,
2965 sizeof(struct ieee80211_bss_conf));
2966
Lennert Buytenhekd1844d72009-11-30 18:13:56 +01002967 memcpy(mwl8k_vif->bssid, info->bssid, ETH_ALEN);
2968
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002969 /* Install rates */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002970 rc = mwl8k_cmd_set_rate(hw, vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002971 if (rc)
2972 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002973
2974 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002975 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
2976 MWL8K_UCAST_RATE, NULL);
2977 if (rc)
2978 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002979
2980 /* Set radio preamble */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002981 rc = mwl8k_set_radio_preamble(hw, info->use_short_preamble);
2982 if (rc)
2983 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002984
2985 /* Set slot time */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002986 rc = mwl8k_cmd_set_slot(hw, info->use_short_slot);
2987 if (rc)
2988 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002989
2990 /* Update peer rate info */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002991 rc = mwl8k_cmd_update_stadb(hw, vif,
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002992 MWL8K_STA_DB_MODIFY_ENTRY);
2993 if (rc)
2994 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002995
2996 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002997 rc = mwl8k_cmd_set_aid(hw, vif);
2998 if (rc)
2999 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003000
3001 /*
3002 * Finalize the join. Tell rx handler to process
3003 * next beacon from our BSSID.
3004 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003005 memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003006 priv->capture_beacon = true;
3007 } else {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003008 rc = mwl8k_cmd_update_stadb(hw, vif, MWL8K_STA_DB_DEL_ENTRY);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003009 memset(&mwl8k_vif->bss_info, 0,
3010 sizeof(struct ieee80211_bss_conf));
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003011 memset(mwl8k_vif->bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003012 }
3013
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003014out:
3015 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003016}
3017
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003018static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3019 int mc_count, struct dev_addr_list *mclist)
3020{
3021 struct mwl8k_cmd_pkt *cmd;
3022
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003023 /*
3024 * Synthesize and return a command packet that programs the
3025 * hardware multicast address filter. At this point we don't
3026 * know whether FIF_ALLMULTI is being requested, but if it is,
3027 * we'll end up throwing this packet away and creating a new
3028 * one in mwl8k_configure_filter().
3029 */
3030 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003031
3032 return (unsigned long)cmd;
3033}
3034
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003035static int
3036mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3037 unsigned int changed_flags,
3038 unsigned int *total_flags)
3039{
3040 struct mwl8k_priv *priv = hw->priv;
3041
3042 /*
3043 * Hardware sniffer mode is mutually exclusive with STA
3044 * operation, so refuse to enable sniffer mode if a STA
3045 * interface is active.
3046 */
3047 if (priv->vif != NULL) {
3048 if (net_ratelimit())
3049 printk(KERN_INFO "%s: not enabling sniffer "
3050 "mode because STA interface is active\n",
3051 wiphy_name(hw->wiphy));
3052 return 0;
3053 }
3054
3055 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003056 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003057 return 0;
3058 priv->sniffer_enabled = true;
3059 }
3060
3061 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3062 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3063 FIF_OTHER_BSS;
3064
3065 return 1;
3066}
3067
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003068static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3069 unsigned int changed_flags,
3070 unsigned int *total_flags,
3071 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003072{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003073 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003074 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3075
3076 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003077 * AP firmware doesn't allow fine-grained control over
3078 * the receive filter.
3079 */
3080 if (priv->ap_fw) {
3081 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3082 kfree(cmd);
3083 return;
3084 }
3085
3086 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003087 * Enable hardware sniffer mode if FIF_CONTROL or
3088 * FIF_OTHER_BSS is requested.
3089 */
3090 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3091 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3092 kfree(cmd);
3093 return;
3094 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003095
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003096 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003097 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003098
3099 if (mwl8k_fw_lock(hw))
3100 return;
3101
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003102 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003103 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003104 priv->sniffer_enabled = false;
3105 }
3106
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003107 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003108 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3109 /*
3110 * Disable the BSS filter.
3111 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003112 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003113 } else {
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003114 u8 *bssid;
3115
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003116 /*
3117 * Enable the BSS filter.
3118 *
3119 * If there is an active STA interface, use that
3120 * interface's BSSID, otherwise use a dummy one
3121 * (where the OUI part needs to be nonzero for
3122 * the BSSID to be accepted by POST_SCAN).
3123 */
3124 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003125 if (priv->vif != NULL)
3126 bssid = MWL8K_VIF(priv->vif)->bssid;
3127
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003128 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003129 }
3130 }
3131
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003132 /*
3133 * If FIF_ALLMULTI is being requested, throw away the command
3134 * packet that ->prepare_multicast() built and replace it with
3135 * a command packet that enables reception of all multicast
3136 * packets.
3137 */
3138 if (*total_flags & FIF_ALLMULTI) {
3139 kfree(cmd);
3140 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3141 }
3142
3143 if (cmd != NULL) {
3144 mwl8k_post_cmd(hw, cmd);
3145 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003146 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003147
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003148 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003149}
3150
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003151static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3152{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003153 return mwl8k_cmd_set_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003154}
3155
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003156static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3157 const struct ieee80211_tx_queue_params *params)
3158{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003159 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003160 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003161
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003162 rc = mwl8k_fw_lock(hw);
3163 if (!rc) {
3164 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003165 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003166
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003167 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003168 rc = mwl8k_cmd_set_edca_params(hw, queue,
3169 params->cw_min,
3170 params->cw_max,
3171 params->aifs,
3172 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003173
3174 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003175 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003176
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003177 return rc;
3178}
3179
3180static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3181 struct ieee80211_tx_queue_stats *stats)
3182{
3183 struct mwl8k_priv *priv = hw->priv;
3184 struct mwl8k_tx_queue *txq;
3185 int index;
3186
3187 spin_lock_bh(&priv->tx_lock);
3188 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3189 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003190 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003191 sizeof(struct ieee80211_tx_queue_stats));
3192 }
3193 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003194
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003195 return 0;
3196}
3197
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003198static int mwl8k_get_stats(struct ieee80211_hw *hw,
3199 struct ieee80211_low_level_stats *stats)
3200{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003201 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003202}
3203
3204static const struct ieee80211_ops mwl8k_ops = {
3205 .tx = mwl8k_tx,
3206 .start = mwl8k_start,
3207 .stop = mwl8k_stop,
3208 .add_interface = mwl8k_add_interface,
3209 .remove_interface = mwl8k_remove_interface,
3210 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003211 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003212 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003213 .configure_filter = mwl8k_configure_filter,
3214 .set_rts_threshold = mwl8k_set_rts_threshold,
3215 .conf_tx = mwl8k_conf_tx,
3216 .get_tx_stats = mwl8k_get_tx_stats,
3217 .get_stats = mwl8k_get_stats,
3218};
3219
3220static void mwl8k_tx_reclaim_handler(unsigned long data)
3221{
3222 int i;
3223 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3224 struct mwl8k_priv *priv = hw->priv;
3225
3226 spin_lock_bh(&priv->tx_lock);
3227 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3228 mwl8k_txq_reclaim(hw, i, 0);
3229
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003230 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003231 complete(priv->tx_wait);
3232 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003233 }
3234 spin_unlock_bh(&priv->tx_lock);
3235}
3236
3237static void mwl8k_finalize_join_worker(struct work_struct *work)
3238{
3239 struct mwl8k_priv *priv =
3240 container_of(work, struct mwl8k_priv, finalize_join_worker);
3241 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003242 u8 dtim = MWL8K_VIF(priv->vif)->bss_info.dtim_period;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003243
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003244 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003245 dev_kfree_skb(skb);
3246
3247 priv->beacon_skb = NULL;
3248}
3249
John W. Linvillebcb628d2009-11-06 16:40:16 -05003250enum {
3251 MWL8687 = 0,
3252 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003253};
3254
John W. Linvillebcb628d2009-11-06 16:40:16 -05003255static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003256 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003257 .part_name = "88w8687",
3258 .helper_image = "mwl8k/helper_8687.fw",
3259 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003260 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003261 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003262 .part_name = "88w8366",
3263 .helper_image = "mwl8k/helper_8366.fw",
3264 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003265 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003266 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003267};
3268
3269static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003270 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3271 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3272 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3273 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003274};
3275MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3276
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003277static int __devinit mwl8k_probe(struct pci_dev *pdev,
3278 const struct pci_device_id *id)
3279{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003280 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003281 struct ieee80211_hw *hw;
3282 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003283 int rc;
3284 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003285
3286 if (!printed_version) {
3287 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3288 printed_version = 1;
3289 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003290
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003291
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003292 rc = pci_enable_device(pdev);
3293 if (rc) {
3294 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3295 MWL8K_NAME);
3296 return rc;
3297 }
3298
3299 rc = pci_request_regions(pdev, MWL8K_NAME);
3300 if (rc) {
3301 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3302 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003303 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003304 }
3305
3306 pci_set_master(pdev);
3307
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003308
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003309 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3310 if (hw == NULL) {
3311 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3312 rc = -ENOMEM;
3313 goto err_free_reg;
3314 }
3315
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003316 SET_IEEE80211_DEV(hw, &pdev->dev);
3317 pci_set_drvdata(pdev, hw);
3318
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003319 priv = hw->priv;
3320 priv->hw = hw;
3321 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003322 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003323
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003324
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003325 priv->sram = pci_iomap(pdev, 0, 0x10000);
3326 if (priv->sram == NULL) {
3327 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003328 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003329 goto err_iounmap;
3330 }
3331
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003332 /*
3333 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3334 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3335 */
3336 priv->regs = pci_iomap(pdev, 1, 0x10000);
3337 if (priv->regs == NULL) {
3338 priv->regs = pci_iomap(pdev, 2, 0x10000);
3339 if (priv->regs == NULL) {
3340 printk(KERN_ERR "%s: Cannot map device registers\n",
3341 wiphy_name(hw->wiphy));
3342 goto err_iounmap;
3343 }
3344 }
3345
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003346
3347 /* Reset firmware and hardware */
3348 mwl8k_hw_reset(priv);
3349
3350 /* Ask userland hotplug daemon for the device firmware */
3351 rc = mwl8k_request_firmware(priv);
3352 if (rc) {
3353 printk(KERN_ERR "%s: Firmware files not found\n",
3354 wiphy_name(hw->wiphy));
3355 goto err_stop_firmware;
3356 }
3357
3358 /* Load firmware into hardware */
3359 rc = mwl8k_load_firmware(hw);
3360 if (rc) {
3361 printk(KERN_ERR "%s: Cannot start firmware\n",
3362 wiphy_name(hw->wiphy));
3363 goto err_stop_firmware;
3364 }
3365
3366 /* Reclaim memory once firmware is successfully loaded */
3367 mwl8k_release_firmware(priv);
3368
3369
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003370 if (priv->ap_fw)
3371 priv->rxd_ops = priv->device_info->ap_rxd_ops;
3372 else
3373 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003374
3375 priv->sniffer_enabled = false;
3376 priv->wmm_enabled = false;
3377 priv->pending_tx_pkts = 0;
3378
3379
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003380 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3381 priv->band.band = IEEE80211_BAND_2GHZ;
3382 priv->band.channels = priv->channels;
3383 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3384 priv->band.bitrates = priv->rates;
3385 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3386 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3387
3388 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3389 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3390
3391 /*
3392 * Extra headroom is the size of the required DMA header
3393 * minus the size of the smallest 802.11 frame (CTS frame).
3394 */
3395 hw->extra_tx_headroom =
3396 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3397
3398 hw->channel_change_time = 10;
3399
3400 hw->queues = MWL8K_TX_QUEUES;
3401
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003402 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003403 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003404 hw->vif_data_size = sizeof(struct mwl8k_vif);
3405 priv->vif = NULL;
3406
3407 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003408 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003409 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003410
3411 /* Finalize join worker */
3412 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3413
3414 /* TX reclaim tasklet */
3415 tasklet_init(&priv->tx_reclaim_task,
3416 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3417 tasklet_disable(&priv->tx_reclaim_task);
3418
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003419 /* Power management cookie */
3420 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3421 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003422 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003423
3424 rc = mwl8k_rxq_init(hw, 0);
3425 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003426 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003427 rxq_refill(hw, 0, INT_MAX);
3428
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003429 mutex_init(&priv->fw_mutex);
3430 priv->fw_mutex_owner = NULL;
3431 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003432 priv->hostcmd_wait = NULL;
3433
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003434 spin_lock_init(&priv->tx_lock);
3435
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003436 priv->tx_wait = NULL;
3437
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003438 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3439 rc = mwl8k_txq_init(hw, i);
3440 if (rc)
3441 goto err_free_queues;
3442 }
3443
3444 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003445 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003446 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3447 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3448
Joe Perchesa0607fd2009-11-18 23:29:17 -08003449 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003450 IRQF_SHARED, MWL8K_NAME, hw);
3451 if (rc) {
3452 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003453 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003454 goto err_free_queues;
3455 }
3456
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003457 /*
3458 * Temporarily enable interrupts. Initial firmware host
3459 * commands use interrupts and avoids polling. Disable
3460 * interrupts when done.
3461 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003462 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003463
3464 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003465 if (priv->ap_fw) {
3466 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3467 if (!rc)
3468 rc = mwl8k_cmd_set_hw_spec(hw);
3469 } else {
3470 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003471
3472 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003473 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003474 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003475 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3476 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003477 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003478 }
3479
3480 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003481 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003482 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003483 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003484 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003485 }
3486
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003487 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003488 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003489 if (rc) {
3490 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3491 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003492 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003493 }
3494
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003495 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003496 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003497 free_irq(priv->pdev->irq, hw);
3498
3499 rc = ieee80211_register_hw(hw);
3500 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003501 printk(KERN_ERR "%s: Cannot register device\n",
3502 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003503 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003504 }
3505
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003506 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003507 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003508 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003509 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003510 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3511 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003512
3513 return 0;
3514
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003515err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003516 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003517 free_irq(priv->pdev->irq, hw);
3518
3519err_free_queues:
3520 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3521 mwl8k_txq_deinit(hw, i);
3522 mwl8k_rxq_deinit(hw, 0);
3523
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003524err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003525 if (priv->cookie != NULL)
3526 pci_free_consistent(priv->pdev, 4,
3527 priv->cookie, priv->cookie_dma);
3528
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003529err_stop_firmware:
3530 mwl8k_hw_reset(priv);
3531 mwl8k_release_firmware(priv);
3532
3533err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003534 if (priv->regs != NULL)
3535 pci_iounmap(pdev, priv->regs);
3536
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003537 if (priv->sram != NULL)
3538 pci_iounmap(pdev, priv->sram);
3539
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003540 pci_set_drvdata(pdev, NULL);
3541 ieee80211_free_hw(hw);
3542
3543err_free_reg:
3544 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003545
3546err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003547 pci_disable_device(pdev);
3548
3549 return rc;
3550}
3551
Joerg Albert230f7af2009-04-18 02:10:45 +02003552static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003553{
3554 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3555}
3556
Joerg Albert230f7af2009-04-18 02:10:45 +02003557static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003558{
3559 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3560 struct mwl8k_priv *priv;
3561 int i;
3562
3563 if (hw == NULL)
3564 return;
3565 priv = hw->priv;
3566
3567 ieee80211_stop_queues(hw);
3568
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003569 ieee80211_unregister_hw(hw);
3570
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003571 /* Remove tx reclaim tasklet */
3572 tasklet_kill(&priv->tx_reclaim_task);
3573
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003574 /* Stop hardware */
3575 mwl8k_hw_reset(priv);
3576
3577 /* Return all skbs to mac80211 */
3578 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3579 mwl8k_txq_reclaim(hw, i, 1);
3580
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003581 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3582 mwl8k_txq_deinit(hw, i);
3583
3584 mwl8k_rxq_deinit(hw, 0);
3585
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003586 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003587
3588 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003589 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003590 pci_set_drvdata(pdev, NULL);
3591 ieee80211_free_hw(hw);
3592 pci_release_regions(pdev);
3593 pci_disable_device(pdev);
3594}
3595
3596static struct pci_driver mwl8k_driver = {
3597 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003598 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003599 .probe = mwl8k_probe,
3600 .remove = __devexit_p(mwl8k_remove),
3601 .shutdown = __devexit_p(mwl8k_shutdown),
3602};
3603
3604static int __init mwl8k_init(void)
3605{
3606 return pci_register_driver(&mwl8k_driver);
3607}
3608
3609static void __exit mwl8k_exit(void)
3610{
3611 pci_unregister_driver(&mwl8k_driver);
3612}
3613
3614module_init(mwl8k_init);
3615module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003616
3617MODULE_DESCRIPTION(MWL8K_DESC);
3618MODULE_VERSION(MWL8K_VERSION);
3619MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3620MODULE_LICENSE("GPL");