blob: df0596ca710d9d977d252db5d96de49cff3d4e4a [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka145d572009-08-18 04:34:26 +02005 * Copyright (C) 2008-2009 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020015#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010016#include <linux/spinlock.h>
17#include <linux/list.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
20#include <linux/completion.h>
21#include <linux/etherdevice.h>
22#include <net/mac80211.h>
23#include <linux/moduleparam.h>
24#include <linux/firmware.h>
25#include <linux/workqueue.h>
26
27#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
28#define MWL8K_NAME KBUILD_MODNAME
Lennert Buytenhek6976b662010-01-02 10:31:50 +010029#define MWL8K_VERSION "0.11"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010030
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031/* Register definitions */
32#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020033#define MWL8K_MODE_STA 0x0000005a
34#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010035#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020036#define MWL8K_FWSTA_READY 0xf0f1f2f4
37#define MWL8K_FWAP_READY 0xf1f2f4a5
38#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010039#define MWL8K_HIU_SCRATCH 0x00000c40
40
41/* Host->device communications */
42#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
43#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
44#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
45#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
46#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020047#define MWL8K_H2A_INT_DUMMY (1 << 20)
48#define MWL8K_H2A_INT_RESET (1 << 15)
49#define MWL8K_H2A_INT_DOORBELL (1 << 1)
50#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010051
52/* Device->host communications */
53#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
54#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
55#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
56#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
57#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020058#define MWL8K_A2H_INT_DUMMY (1 << 20)
59#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
60#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
61#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
62#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
63#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
64#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
65#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
66#define MWL8K_A2H_INT_RX_READY (1 << 1)
67#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010068
69#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
70 MWL8K_A2H_INT_CHNL_SWITCHED | \
71 MWL8K_A2H_INT_QUEUE_EMPTY | \
72 MWL8K_A2H_INT_RADAR_DETECT | \
73 MWL8K_A2H_INT_RADIO_ON | \
74 MWL8K_A2H_INT_RADIO_OFF | \
75 MWL8K_A2H_INT_MAC_EVENT | \
76 MWL8K_A2H_INT_OPC_DONE | \
77 MWL8K_A2H_INT_RX_READY | \
78 MWL8K_A2H_INT_TX_DONE)
79
Lennert Buytenheka66098d2009-03-10 10:13:33 +010080#define MWL8K_RX_QUEUES 1
81#define MWL8K_TX_QUEUES 4
82
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020083struct rxd_ops {
84 int rxd_size;
85 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
86 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010087 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
88 __le16 *qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020089};
90
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020091struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020092 char *part_name;
93 char *helper_image;
94 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010095 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020096};
97
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +020099 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100100
101 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200102 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
104 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200105 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100106
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200107 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200109 struct {
110 struct sk_buff *skb;
111 DECLARE_PCI_UNMAP_ADDR(dma)
112 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100113};
114
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115struct mwl8k_tx_queue {
116 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100118
119 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200120 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100121
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200122 struct ieee80211_tx_queue_stats stats;
123 struct mwl8k_tx_desc *txd;
124 dma_addr_t txd_dma;
125 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100126};
127
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100128struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200132 struct mwl8k_device_info *device_info;
133
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100134 void __iomem *sram;
135 void __iomem *regs;
136
137 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100138 struct firmware *fw_helper;
139 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100141 /* hardware/firmware parameters */
142 bool ap_fw;
143 struct rxd_ops *rxd_ops;
144
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200145 /* firmware access */
146 struct mutex fw_mutex;
147 struct task_struct *fw_mutex_owner;
148 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200149 struct completion *hostcmd_wait;
150
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100151 /* lock held over TX and TX reap */
152 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100153
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200154 /* TX quiesce completion, protected by fw_mutex and tx_lock */
155 struct completion *tx_wait;
156
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 struct ieee80211_vif *vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158
159 struct ieee80211_channel *current_channel;
160
161 /* power management status cookie from firmware */
162 u32 *cookie;
163 dma_addr_t cookie_dma;
164
165 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100166 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200167 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168
169 /*
170 * Running count of TX packets in flight, to avoid
171 * iterating over the transmit rings each time.
172 */
173 int pending_tx_pkts;
174
175 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
176 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
177
178 /* PHY parameters */
179 struct ieee80211_supported_band band;
180 struct ieee80211_channel channels[14];
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100181 struct ieee80211_rate rates[14];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100182
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200183 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200184 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200185 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200186 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100187
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +0100188 struct work_struct sta_notify_worker;
189 spinlock_t sta_notify_list_lock;
190 struct list_head sta_notify_list;
191
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100192 /* XXX need to convert this to handle multiple interfaces */
193 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200194 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 struct sk_buff *beacon_skb;
196
197 /*
198 * This FJ worker has to be global as it is scheduled from the
199 * RX handler. At this point we don't know which interface it
200 * belongs to until the list of bssids waiting to complete join
201 * is checked.
202 */
203 struct work_struct finalize_join_worker;
204
205 /* Tasklet to reclaim TX descriptors and buffers after tx */
206 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100207};
208
209/* Per interface specific private data */
210struct mwl8k_vif {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +0100211 /* Local MAC address. */
212 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100213
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +0100214 /* BSSID of AP. */
215 u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100216
Lennert Buytenhek55489b62009-11-30 18:31:33 +0100217 /* Index into station database. Returned by UPDATE_STADB. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218 u8 peer_id;
219
220 /* Non AMPDU sequence number assigned by driver */
221 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100222};
223
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200224#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225
226static const struct ieee80211_channel mwl8k_channels[] = {
227 { .center_freq = 2412, .hw_value = 1, },
228 { .center_freq = 2417, .hw_value = 2, },
229 { .center_freq = 2422, .hw_value = 3, },
230 { .center_freq = 2427, .hw_value = 4, },
231 { .center_freq = 2432, .hw_value = 5, },
232 { .center_freq = 2437, .hw_value = 6, },
233 { .center_freq = 2442, .hw_value = 7, },
234 { .center_freq = 2447, .hw_value = 8, },
235 { .center_freq = 2452, .hw_value = 9, },
236 { .center_freq = 2457, .hw_value = 10, },
237 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100238 { .center_freq = 2467, .hw_value = 12, },
239 { .center_freq = 2472, .hw_value = 13, },
240 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100241};
242
243static const struct ieee80211_rate mwl8k_rates[] = {
244 { .bitrate = 10, .hw_value = 2, },
245 { .bitrate = 20, .hw_value = 4, },
246 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200247 { .bitrate = 110, .hw_value = 22, },
248 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100249 { .bitrate = 60, .hw_value = 12, },
250 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100251 { .bitrate = 120, .hw_value = 24, },
252 { .bitrate = 180, .hw_value = 36, },
253 { .bitrate = 240, .hw_value = 48, },
254 { .bitrate = 360, .hw_value = 72, },
255 { .bitrate = 480, .hw_value = 96, },
256 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100257 { .bitrate = 720, .hw_value = 144, },
258};
259
260static const u8 mwl8k_rateids[12] = {
261 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108,
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100262};
263
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100264/* Set or get info from Firmware */
265#define MWL8K_CMD_SET 0x0001
266#define MWL8K_CMD_GET 0x0000
267
268/* Firmware command codes */
269#define MWL8K_CMD_CODE_DNLD 0x0001
270#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200271#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100272#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
273#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200274#define MWL8K_CMD_RADIO_CONTROL 0x001c
275#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200276#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100277#define MWL8K_CMD_SET_PRE_SCAN 0x0107
278#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200279#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100280#define MWL8K_CMD_SET_AID 0x010d
281#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200282#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100283#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200284#define MWL8K_CMD_SET_SLOT 0x0114
285#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
286#define MWL8K_CMD_SET_WMM_MODE 0x0123
287#define MWL8K_CMD_MIMO_CONFIG 0x0125
288#define MWL8K_CMD_USE_FIXED_RATE 0x0126
289#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200290#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200291#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
292#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100293
294static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
295{
296#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
297 snprintf(buf, bufsize, "%s", #x);\
298 return buf;\
299 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200300 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301 MWL8K_CMDNAME(CODE_DNLD);
302 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200303 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100304 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
305 MWL8K_CMDNAME(GET_STAT);
306 MWL8K_CMDNAME(RADIO_CONTROL);
307 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200308 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100309 MWL8K_CMDNAME(SET_PRE_SCAN);
310 MWL8K_CMDNAME(SET_POST_SCAN);
311 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100312 MWL8K_CMDNAME(SET_AID);
313 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200314 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100315 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200316 MWL8K_CMDNAME(SET_SLOT);
317 MWL8K_CMDNAME(SET_EDCA_PARAMS);
318 MWL8K_CMDNAME(SET_WMM_MODE);
319 MWL8K_CMDNAME(MIMO_CONFIG);
320 MWL8K_CMDNAME(USE_FIXED_RATE);
321 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200322 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200323 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
324 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100325 default:
326 snprintf(buf, bufsize, "0x%x", cmd);
327 }
328#undef MWL8K_CMDNAME
329
330 return buf;
331}
332
333/* Hardware and firmware reset */
334static void mwl8k_hw_reset(struct mwl8k_priv *priv)
335{
336 iowrite32(MWL8K_H2A_INT_RESET,
337 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
338 iowrite32(MWL8K_H2A_INT_RESET,
339 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
340 msleep(20);
341}
342
343/* Release fw image */
344static void mwl8k_release_fw(struct firmware **fw)
345{
346 if (*fw == NULL)
347 return;
348 release_firmware(*fw);
349 *fw = NULL;
350}
351
352static void mwl8k_release_firmware(struct mwl8k_priv *priv)
353{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100354 mwl8k_release_fw(&priv->fw_ucode);
355 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100356}
357
358/* Request fw image */
359static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200360 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100361{
362 /* release current image */
363 if (*fw != NULL)
364 mwl8k_release_fw(fw);
365
366 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200367 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100368}
369
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200370static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100371{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200372 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100373 int rc;
374
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200375 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100376 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200377 if (rc) {
378 printk(KERN_ERR "%s: Error requesting helper "
379 "firmware file %s\n", pci_name(priv->pdev),
380 di->helper_image);
381 return rc;
382 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100383 }
384
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100385 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100386 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200387 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200388 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100389 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100390 return rc;
391 }
392
393 return 0;
394}
395
Ben Hutchings7e75b942009-11-07 22:00:57 +0000396MODULE_FIRMWARE("mwl8k/helper_8687.fw");
397MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
398
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100399struct mwl8k_cmd_pkt {
400 __le16 code;
401 __le16 length;
402 __le16 seq_num;
403 __le16 result;
404 char payload[0];
405} __attribute__((packed));
406
407/*
408 * Firmware loading.
409 */
410static int
411mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
412{
413 void __iomem *regs = priv->regs;
414 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100415 int loops;
416
417 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
418 if (pci_dma_mapping_error(priv->pdev, dma_addr))
419 return -ENOMEM;
420
421 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
422 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
423 iowrite32(MWL8K_H2A_INT_DOORBELL,
424 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
425 iowrite32(MWL8K_H2A_INT_DUMMY,
426 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
427
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100428 loops = 1000;
429 do {
430 u32 int_code;
431
432 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
433 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
434 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100435 break;
436 }
437
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200438 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100439 udelay(1);
440 } while (--loops);
441
442 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
443
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200444 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100445}
446
447static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
448 const u8 *data, size_t length)
449{
450 struct mwl8k_cmd_pkt *cmd;
451 int done;
452 int rc = 0;
453
454 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
455 if (cmd == NULL)
456 return -ENOMEM;
457
458 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
459 cmd->seq_num = 0;
460 cmd->result = 0;
461
462 done = 0;
463 while (length) {
464 int block_size = length > 256 ? 256 : length;
465
466 memcpy(cmd->payload, data + done, block_size);
467 cmd->length = cpu_to_le16(block_size);
468
469 rc = mwl8k_send_fw_load_cmd(priv, cmd,
470 sizeof(*cmd) + block_size);
471 if (rc)
472 break;
473
474 done += block_size;
475 length -= block_size;
476 }
477
478 if (!rc) {
479 cmd->length = 0;
480 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
481 }
482
483 kfree(cmd);
484
485 return rc;
486}
487
488static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
489 const u8 *data, size_t length)
490{
491 unsigned char *buffer;
492 int may_continue, rc = 0;
493 u32 done, prev_block_size;
494
495 buffer = kmalloc(1024, GFP_KERNEL);
496 if (buffer == NULL)
497 return -ENOMEM;
498
499 done = 0;
500 prev_block_size = 0;
501 may_continue = 1000;
502 while (may_continue > 0) {
503 u32 block_size;
504
505 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
506 if (block_size & 1) {
507 block_size &= ~1;
508 may_continue--;
509 } else {
510 done += prev_block_size;
511 length -= prev_block_size;
512 }
513
514 if (block_size > 1024 || block_size > length) {
515 rc = -EOVERFLOW;
516 break;
517 }
518
519 if (length == 0) {
520 rc = 0;
521 break;
522 }
523
524 if (block_size == 0) {
525 rc = -EPROTO;
526 may_continue--;
527 udelay(1);
528 continue;
529 }
530
531 prev_block_size = block_size;
532 memcpy(buffer, data + done, block_size);
533
534 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
535 if (rc)
536 break;
537 }
538
539 if (!rc && length != 0)
540 rc = -EREMOTEIO;
541
542 kfree(buffer);
543
544 return rc;
545}
546
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200547static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100548{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200549 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100550 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200551 int rc;
552 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100553
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200554 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100555 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100556
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200557 if (helper == NULL) {
558 printk(KERN_ERR "%s: helper image needed but none "
559 "given\n", pci_name(priv->pdev));
560 return -EINVAL;
561 }
562
563 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100564 if (rc) {
565 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200566 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100567 return rc;
568 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100569 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100570
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200571 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100572 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200573 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100574 }
575
576 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200577 printk(KERN_ERR "%s: unable to load firmware image\n",
578 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100579 return rc;
580 }
581
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100582 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100583
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100584 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100585 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200586 u32 ready_code;
587
588 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
589 if (ready_code == MWL8K_FWAP_READY) {
590 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100591 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200592 } else if (ready_code == MWL8K_FWSTA_READY) {
593 priv->ap_fw = 0;
594 break;
595 }
596
597 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100598 udelay(1);
599 } while (--loops);
600
601 return loops ? 0 : -ETIMEDOUT;
602}
603
604
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100605/* DMA header used by firmware and hardware. */
606struct mwl8k_dma_data {
607 __le16 fwlen;
608 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100609 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100610} __attribute__((packed));
611
612/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100613static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100614{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100615 struct mwl8k_dma_data *tr;
616 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100617
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100618 tr = (struct mwl8k_dma_data *)skb->data;
619 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
620
621 if (hdrlen != sizeof(tr->wh)) {
622 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
623 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
624 *((__le16 *)(tr->data - 2)) = qos;
625 } else {
626 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
627 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100628 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100629
630 if (hdrlen != sizeof(*tr))
631 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100632}
633
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200634static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100635{
636 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100637 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100638 struct mwl8k_dma_data *tr;
639
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100640 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100641 * Add a firmware DMA header; the firmware requires that we
642 * present a 2-byte payload length followed by a 4-address
643 * header (without QoS field), followed (optionally) by any
644 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100645 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100646 wh = (struct ieee80211_hdr *)skb->data;
647
648 hdrlen = ieee80211_hdrlen(wh->frame_control);
649 if (hdrlen != sizeof(*tr))
650 skb_push(skb, sizeof(*tr) - hdrlen);
651
652 if (ieee80211_is_data_qos(wh->frame_control))
653 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100654
655 tr = (struct mwl8k_dma_data *)skb->data;
656 if (wh != &tr->wh)
657 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100658 if (hdrlen != sizeof(tr->wh))
659 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100660
661 /*
662 * Firmware length is the length of the fully formed "802.11
663 * payload". That is, everything except for the 802.11 header.
664 * This includes all crypto material including the MIC.
665 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100666 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100667}
668
669
670/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100671 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200672 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100673struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200674 __le16 pkt_len;
675 __u8 sq2;
676 __u8 rate;
677 __le32 pkt_phys_addr;
678 __le32 next_rxd_phys_addr;
679 __le16 qos_control;
680 __le16 htsig2;
681 __le32 hw_rssi_info;
682 __le32 hw_noise_floor_info;
683 __u8 noise_floor;
684 __u8 pad0[3];
685 __u8 rssi;
686 __u8 rx_status;
687 __u8 channel;
688 __u8 rx_ctrl;
689} __attribute__((packed));
690
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100691#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
692#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
693#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100694
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100695#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200696
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100697static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200698{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100699 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200700
701 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100702 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200703}
704
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100705static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200706{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100707 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200708
709 rxd->pkt_len = cpu_to_le16(len);
710 rxd->pkt_phys_addr = cpu_to_le32(addr);
711 wmb();
712 rxd->rx_ctrl = 0;
713}
714
715static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100716mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
717 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200718{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100719 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200720
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100721 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200722 return -1;
723 rmb();
724
725 memset(status, 0, sizeof(*status));
726
727 status->signal = -rxd->rssi;
728 status->noise = -rxd->noise_floor;
729
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100730 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200731 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100732 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100733 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100734 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200735 } else {
736 int i;
737
738 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
739 if (mwl8k_rates[i].hw_value == rxd->rate) {
740 status->rate_idx = i;
741 break;
742 }
743 }
744 }
745
746 status->band = IEEE80211_BAND_2GHZ;
747 status->freq = ieee80211_channel_to_frequency(rxd->channel);
748
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100749 *qos = rxd->qos_control;
750
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200751 return le16_to_cpu(rxd->pkt_len);
752}
753
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100754static struct rxd_ops rxd_8366_ap_ops = {
755 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
756 .rxd_init = mwl8k_rxd_8366_ap_init,
757 .rxd_refill = mwl8k_rxd_8366_ap_refill,
758 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200759};
760
761/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100762 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100763 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100764struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100765 __le16 pkt_len;
766 __u8 link_quality;
767 __u8 noise_level;
768 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200769 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100770 __le16 qos_control;
771 __le16 rate_info;
772 __le32 pad0[4];
773 __u8 rssi;
774 __u8 channel;
775 __le16 pad1;
776 __u8 rx_ctrl;
777 __u8 rx_status;
778 __u8 pad2[2];
779} __attribute__((packed));
780
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100781#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
782#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
783#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
784#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
785#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
786#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200787
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100788#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200789
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100790static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200791{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100792 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200793
794 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100795 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200796}
797
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100798static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200799{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100800 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200801
802 rxd->pkt_len = cpu_to_le16(len);
803 rxd->pkt_phys_addr = cpu_to_le32(addr);
804 wmb();
805 rxd->rx_ctrl = 0;
806}
807
808static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100809mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100810 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200811{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100812 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200813 u16 rate_info;
814
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100815 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200816 return -1;
817 rmb();
818
819 rate_info = le16_to_cpu(rxd->rate_info);
820
821 memset(status, 0, sizeof(*status));
822
823 status->signal = -rxd->rssi;
824 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100825 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
826 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200827
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100828 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200829 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100830 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200831 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100832 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200833 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100834 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200835 status->flag |= RX_FLAG_HT;
836
837 status->band = IEEE80211_BAND_2GHZ;
838 status->freq = ieee80211_channel_to_frequency(rxd->channel);
839
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100840 *qos = rxd->qos_control;
841
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200842 return le16_to_cpu(rxd->pkt_len);
843}
844
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100845static struct rxd_ops rxd_sta_ops = {
846 .rxd_size = sizeof(struct mwl8k_rxd_sta),
847 .rxd_init = mwl8k_rxd_sta_init,
848 .rxd_refill = mwl8k_rxd_sta_refill,
849 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200850};
851
852
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100853#define MWL8K_RX_DESCS 256
854#define MWL8K_RX_MAXSZ 3800
855
856static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
857{
858 struct mwl8k_priv *priv = hw->priv;
859 struct mwl8k_rx_queue *rxq = priv->rxq + index;
860 int size;
861 int i;
862
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200863 rxq->rxd_count = 0;
864 rxq->head = 0;
865 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100866
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200867 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100868
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200869 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
870 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100871 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200872 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100873 return -ENOMEM;
874 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200875 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100876
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200877 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
878 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100879 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200880 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200881 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100882 return -ENOMEM;
883 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200884 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100885
886 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200887 int desc_size;
888 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100889 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200890 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100891
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200892 desc_size = priv->rxd_ops->rxd_size;
893 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100894
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200895 nexti = i + 1;
896 if (nexti == MWL8K_RX_DESCS)
897 nexti = 0;
898 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
899
900 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100901 }
902
903 return 0;
904}
905
906static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
907{
908 struct mwl8k_priv *priv = hw->priv;
909 struct mwl8k_rx_queue *rxq = priv->rxq + index;
910 int refilled;
911
912 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200913 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100914 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200915 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100916 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200917 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100918
919 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
920 if (skb == NULL)
921 break;
922
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200923 addr = pci_map_single(priv->pdev, skb->data,
924 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100925
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200926 rxq->rxd_count++;
927 rx = rxq->tail++;
928 if (rxq->tail == MWL8K_RX_DESCS)
929 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200930 rxq->buf[rx].skb = skb;
931 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200932
933 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
934 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100935
936 refilled++;
937 }
938
939 return refilled;
940}
941
942/* Must be called only when the card's reception is completely halted */
943static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
944{
945 struct mwl8k_priv *priv = hw->priv;
946 struct mwl8k_rx_queue *rxq = priv->rxq + index;
947 int i;
948
949 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200950 if (rxq->buf[i].skb != NULL) {
951 pci_unmap_single(priv->pdev,
952 pci_unmap_addr(&rxq->buf[i], dma),
953 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
954 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100955
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200956 kfree_skb(rxq->buf[i].skb);
957 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100958 }
959 }
960
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200961 kfree(rxq->buf);
962 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100963
964 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200965 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200966 rxq->rxd, rxq->rxd_dma);
967 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100968}
969
970
971/*
972 * Scan a list of BSSIDs to process for finalize join.
973 * Allows for extension to process multiple BSSIDs.
974 */
975static inline int
976mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
977{
978 return priv->capture_beacon &&
979 ieee80211_is_beacon(wh->frame_control) &&
980 !compare_ether_addr(wh->addr3, priv->capture_bssid);
981}
982
Lennert Buytenhek37797522009-10-22 20:19:45 +0200983static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
984 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100985{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200986 struct mwl8k_priv *priv = hw->priv;
987
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100988 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200989 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100990
991 /*
992 * Use GFP_ATOMIC as rxq_process is called from
993 * the primary interrupt handler, memory allocation call
994 * must not sleep.
995 */
996 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
997 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200998 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100999}
1000
1001static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1002{
1003 struct mwl8k_priv *priv = hw->priv;
1004 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1005 int processed;
1006
1007 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001008 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001009 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001010 void *rxd;
1011 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001012 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001013 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001014
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001015 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001016 if (skb == NULL)
1017 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001018
1019 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1020
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001021 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001022 if (pkt_len < 0)
1023 break;
1024
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001025 rxq->buf[rxq->head].skb = NULL;
1026
1027 pci_unmap_single(priv->pdev,
1028 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1029 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1030 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001031
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001032 rxq->head++;
1033 if (rxq->head == MWL8K_RX_DESCS)
1034 rxq->head = 0;
1035
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001036 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001037
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001038 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001039 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001040
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001041 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001042 * Check for a pending join operation. Save a
1043 * copy of the beacon and schedule a tasklet to
1044 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001045 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001046 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001047 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001048
Johannes Bergf1d58c22009-06-17 13:13:00 +02001049 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1050 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001051
1052 processed++;
1053 }
1054
1055 return processed;
1056}
1057
1058
1059/*
1060 * Packet transmission.
1061 */
1062
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001063#define MWL8K_TXD_STATUS_OK 0x00000001
1064#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1065#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1066#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001067#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001068
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001069#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1070#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1071#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1072#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1073#define MWL8K_QOS_EOSP 0x0010
1074
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001075struct mwl8k_tx_desc {
1076 __le32 status;
1077 __u8 data_rate;
1078 __u8 tx_priority;
1079 __le16 qos_control;
1080 __le32 pkt_phys_addr;
1081 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001082 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001083 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001084 __le32 reserved;
1085 __le16 rate_info;
1086 __u8 peer_id;
1087 __u8 tx_frag_cnt;
1088} __attribute__((packed));
1089
1090#define MWL8K_TX_DESCS 128
1091
1092static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1093{
1094 struct mwl8k_priv *priv = hw->priv;
1095 struct mwl8k_tx_queue *txq = priv->txq + index;
1096 int size;
1097 int i;
1098
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001099 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1100 txq->stats.limit = MWL8K_TX_DESCS;
1101 txq->head = 0;
1102 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001103
1104 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1105
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001106 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1107 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001108 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001109 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001110 return -ENOMEM;
1111 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001112 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001113
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001114 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1115 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001116 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001117 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001118 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001119 return -ENOMEM;
1120 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001121 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001122
1123 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1124 struct mwl8k_tx_desc *tx_desc;
1125 int nexti;
1126
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001127 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001128 nexti = (i + 1) % MWL8K_TX_DESCS;
1129
1130 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001131 tx_desc->next_txd_phys_addr =
1132 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001133 }
1134
1135 return 0;
1136}
1137
1138static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1139{
1140 iowrite32(MWL8K_H2A_INT_PPA_READY,
1141 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1142 iowrite32(MWL8K_H2A_INT_DUMMY,
1143 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1144 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1145}
1146
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001147static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001148{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001149 struct mwl8k_priv *priv = hw->priv;
1150 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001151
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001152 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1153 struct mwl8k_tx_queue *txq = priv->txq + i;
1154 int fw_owned = 0;
1155 int drv_owned = 0;
1156 int unused = 0;
1157 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001158
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001160 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1161 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001162
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001163 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001164 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001165 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001166 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001167 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001168
1169 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001170 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001171 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001172
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001173 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1174 "fw_owned=%d drv_owned=%d unused=%d\n",
1175 wiphy_name(hw->wiphy), i,
1176 txq->stats.len, txq->head, txq->tail,
1177 fw_owned, drv_owned, unused);
1178 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001179}
1180
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001181/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001182 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001183 */
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001184#define MWL8K_TX_WAIT_TIMEOUT_MS 1000
1185
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001186static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001187{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001188 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001189 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001190 int retry;
1191 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001192
1193 might_sleep();
1194
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001195 /*
1196 * The TX queues are stopped at this point, so this test
1197 * doesn't need to take ->tx_lock.
1198 */
1199 if (!priv->pending_tx_pkts)
1200 return 0;
1201
1202 retry = 0;
1203 rc = 0;
1204
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001206 priv->tx_wait = &tx_wait;
1207 while (!rc) {
1208 int oldcount;
1209 unsigned long timeout;
1210
1211 oldcount = priv->pending_tx_pkts;
1212
1213 spin_unlock_bh(&priv->tx_lock);
1214 timeout = wait_for_completion_timeout(&tx_wait,
1215 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1216 spin_lock_bh(&priv->tx_lock);
1217
1218 if (timeout) {
1219 WARN_ON(priv->pending_tx_pkts);
1220 if (retry) {
1221 printk(KERN_NOTICE "%s: tx rings drained\n",
1222 wiphy_name(hw->wiphy));
1223 }
1224 break;
1225 }
1226
1227 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001228 printk(KERN_NOTICE "%s: waiting for tx rings "
1229 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001230 wiphy_name(hw->wiphy), oldcount,
1231 priv->pending_tx_pkts);
1232 retry = 1;
1233 continue;
1234 }
1235
1236 priv->tx_wait = NULL;
1237
1238 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1239 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1240 mwl8k_dump_tx_rings(hw);
1241
1242 rc = -ETIMEDOUT;
1243 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001244 spin_unlock_bh(&priv->tx_lock);
1245
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001246 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001247}
1248
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001249#define MWL8K_TXD_SUCCESS(status) \
1250 ((status) & (MWL8K_TXD_STATUS_OK | \
1251 MWL8K_TXD_STATUS_OK_RETRY | \
1252 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001253
1254static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1255{
1256 struct mwl8k_priv *priv = hw->priv;
1257 struct mwl8k_tx_queue *txq = priv->txq + index;
1258 int wake = 0;
1259
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001260 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001261 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001262 struct mwl8k_tx_desc *tx_desc;
1263 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001264 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001265 struct sk_buff *skb;
1266 struct ieee80211_tx_info *info;
1267 u32 status;
1268
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001269 tx = txq->head;
1270 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001271
1272 status = le32_to_cpu(tx_desc->status);
1273
1274 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1275 if (!force)
1276 break;
1277 tx_desc->status &=
1278 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1279 }
1280
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001281 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1282 BUG_ON(txq->stats.len == 0);
1283 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001284 priv->pending_tx_pkts--;
1285
1286 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001287 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001288 skb = txq->skb[tx];
1289 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001290
1291 BUG_ON(skb == NULL);
1292 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1293
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001294 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001295
1296 /* Mark descriptor as unused */
1297 tx_desc->pkt_phys_addr = 0;
1298 tx_desc->pkt_len = 0;
1299
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300 info = IEEE80211_SKB_CB(skb);
1301 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001302 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001304
1305 ieee80211_tx_status_irqsafe(hw, skb);
1306
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001307 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001308 }
1309
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001310 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001311 ieee80211_wake_queue(hw, index);
1312}
1313
1314/* must be called only when the card's transmit is completely halted */
1315static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1316{
1317 struct mwl8k_priv *priv = hw->priv;
1318 struct mwl8k_tx_queue *txq = priv->txq + index;
1319
1320 mwl8k_txq_reclaim(hw, index, 1);
1321
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001322 kfree(txq->skb);
1323 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001324
1325 pci_free_consistent(priv->pdev,
1326 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001327 txq->txd, txq->txd_dma);
1328 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001329}
1330
1331static int
1332mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1333{
1334 struct mwl8k_priv *priv = hw->priv;
1335 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001336 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001337 struct ieee80211_hdr *wh;
1338 struct mwl8k_tx_queue *txq;
1339 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001341 u32 txstatus;
1342 u8 txdatarate;
1343 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001344
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001345 wh = (struct ieee80211_hdr *)skb->data;
1346 if (ieee80211_is_data_qos(wh->frame_control))
1347 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1348 else
1349 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001350
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001351 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001352 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001353
1354 tx_info = IEEE80211_SKB_CB(skb);
1355 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001356
1357 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1358 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001359
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001360 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1361 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1362 mwl8k_vif->seqno = seqno++ % 4096;
1363 }
1364
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001365 /* Setup firmware control bit fields for each frame type. */
1366 txstatus = 0;
1367 txdatarate = 0;
1368 if (ieee80211_is_mgmt(wh->frame_control) ||
1369 ieee80211_is_ctl(wh->frame_control)) {
1370 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001371 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001372 } else if (ieee80211_is_data(wh->frame_control)) {
1373 txdatarate = 1;
1374 if (is_multicast_ether_addr(wh->addr1))
1375 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1376
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001377 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001378 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001379 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001380 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001381 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001382 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001383
1384 dma = pci_map_single(priv->pdev, skb->data,
1385 skb->len, PCI_DMA_TODEVICE);
1386
1387 if (pci_dma_mapping_error(priv->pdev, dma)) {
1388 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001389 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001390 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001391 return NETDEV_TX_OK;
1392 }
1393
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001394 spin_lock_bh(&priv->tx_lock);
1395
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001396 txq = priv->txq + index;
1397
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001398 BUG_ON(txq->skb[txq->tail] != NULL);
1399 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001400
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001401 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001402 tx->data_rate = txdatarate;
1403 tx->tx_priority = index;
1404 tx->qos_control = cpu_to_le16(qos);
1405 tx->pkt_phys_addr = cpu_to_le32(dma);
1406 tx->pkt_len = cpu_to_le16(skb->len);
1407 tx->rate_info = 0;
1408 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001409 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001410 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1411
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001412 txq->stats.count++;
1413 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001414 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001415
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001416 txq->tail++;
1417 if (txq->tail == MWL8K_TX_DESCS)
1418 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001419
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001420 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001421 ieee80211_stop_queue(hw, index);
1422
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001423 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001424
1425 spin_unlock_bh(&priv->tx_lock);
1426
1427 return NETDEV_TX_OK;
1428}
1429
1430
1431/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001432 * Firmware access.
1433 *
1434 * We have the following requirements for issuing firmware commands:
1435 * - Some commands require that the packet transmit path is idle when
1436 * the command is issued. (For simplicity, we'll just quiesce the
1437 * transmit path for every command.)
1438 * - There are certain sequences of commands that need to be issued to
1439 * the hardware sequentially, with no other intervening commands.
1440 *
1441 * This leads to an implementation of a "firmware lock" as a mutex that
1442 * can be taken recursively, and which is taken by both the low-level
1443 * command submission function (mwl8k_post_cmd) as well as any users of
1444 * that function that require issuing of an atomic sequence of commands,
1445 * and quiesces the transmit path whenever it's taken.
1446 */
1447static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1448{
1449 struct mwl8k_priv *priv = hw->priv;
1450
1451 if (priv->fw_mutex_owner != current) {
1452 int rc;
1453
1454 mutex_lock(&priv->fw_mutex);
1455 ieee80211_stop_queues(hw);
1456
1457 rc = mwl8k_tx_wait_empty(hw);
1458 if (rc) {
1459 ieee80211_wake_queues(hw);
1460 mutex_unlock(&priv->fw_mutex);
1461
1462 return rc;
1463 }
1464
1465 priv->fw_mutex_owner = current;
1466 }
1467
1468 priv->fw_mutex_depth++;
1469
1470 return 0;
1471}
1472
1473static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1474{
1475 struct mwl8k_priv *priv = hw->priv;
1476
1477 if (!--priv->fw_mutex_depth) {
1478 ieee80211_wake_queues(hw);
1479 priv->fw_mutex_owner = NULL;
1480 mutex_unlock(&priv->fw_mutex);
1481 }
1482}
1483
1484
1485/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001486 * Command processing.
1487 */
1488
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001489/* Timeout firmware commands after 10s */
1490#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001491
1492static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1493{
1494 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1495 struct mwl8k_priv *priv = hw->priv;
1496 void __iomem *regs = priv->regs;
1497 dma_addr_t dma_addr;
1498 unsigned int dma_size;
1499 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001500 unsigned long timeout = 0;
1501 u8 buf[32];
1502
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001503 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001504 dma_size = le16_to_cpu(cmd->length);
1505 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1506 PCI_DMA_BIDIRECTIONAL);
1507 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1508 return -ENOMEM;
1509
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001510 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001511 if (rc) {
1512 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1513 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001514 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001515 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001516
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001517 priv->hostcmd_wait = &cmd_wait;
1518 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1519 iowrite32(MWL8K_H2A_INT_DOORBELL,
1520 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1521 iowrite32(MWL8K_H2A_INT_DUMMY,
1522 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001523
1524 timeout = wait_for_completion_timeout(&cmd_wait,
1525 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1526
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001527 priv->hostcmd_wait = NULL;
1528
1529 mwl8k_fw_unlock(hw);
1530
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001531 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1532 PCI_DMA_BIDIRECTIONAL);
1533
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001534 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001535 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001536 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001537 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1538 MWL8K_CMD_TIMEOUT_MS);
1539 rc = -ETIMEDOUT;
1540 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001541 int ms;
1542
1543 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1544
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001545 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001546 if (rc)
1547 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001548 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001549 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001550 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001551 else if (ms > 2000)
1552 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1553 wiphy_name(hw->wiphy),
1554 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1555 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001556 }
1557
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001558 return rc;
1559}
1560
1561/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001562 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001563 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001564struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001565 struct mwl8k_cmd_pkt header;
1566 __u8 hw_rev;
1567 __u8 host_interface;
1568 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001569 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001570 __le16 region_code;
1571 __le32 fw_rev;
1572 __le32 ps_cookie;
1573 __le32 caps;
1574 __u8 mcs_bitmap[16];
1575 __le32 rx_queue_ptr;
1576 __le32 num_tx_queues;
1577 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1578 __le32 caps2;
1579 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001580 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001581} __attribute__((packed));
1582
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001583static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001584{
1585 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001586 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001587 int rc;
1588 int i;
1589
1590 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1591 if (cmd == NULL)
1592 return -ENOMEM;
1593
1594 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1595 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1596
1597 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1598 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001599 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001600 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001601 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001602 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001603 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001604 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001605
1606 rc = mwl8k_post_cmd(hw, &cmd->header);
1607
1608 if (!rc) {
1609 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1610 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001611 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001612 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001613 }
1614
1615 kfree(cmd);
1616 return rc;
1617}
1618
1619/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001620 * CMD_GET_HW_SPEC (AP version).
1621 */
1622struct mwl8k_cmd_get_hw_spec_ap {
1623 struct mwl8k_cmd_pkt header;
1624 __u8 hw_rev;
1625 __u8 host_interface;
1626 __le16 num_wcb;
1627 __le16 num_mcaddrs;
1628 __u8 perm_addr[ETH_ALEN];
1629 __le16 region_code;
1630 __le16 num_antenna;
1631 __le32 fw_rev;
1632 __le32 wcbbase0;
1633 __le32 rxwrptr;
1634 __le32 rxrdptr;
1635 __le32 ps_cookie;
1636 __le32 wcbbase1;
1637 __le32 wcbbase2;
1638 __le32 wcbbase3;
1639} __attribute__((packed));
1640
1641static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1642{
1643 struct mwl8k_priv *priv = hw->priv;
1644 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1645 int rc;
1646
1647 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1648 if (cmd == NULL)
1649 return -ENOMEM;
1650
1651 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1652 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1653
1654 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1655 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1656
1657 rc = mwl8k_post_cmd(hw, &cmd->header);
1658
1659 if (!rc) {
1660 int off;
1661
1662 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1663 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1664 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1665 priv->hw_rev = cmd->hw_rev;
1666
1667 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1668 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1669
1670 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1671 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1672
1673 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1674 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1675
1676 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1677 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1678
1679 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1680 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1681
1682 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1683 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1684 }
1685
1686 kfree(cmd);
1687 return rc;
1688}
1689
1690/*
1691 * CMD_SET_HW_SPEC.
1692 */
1693struct mwl8k_cmd_set_hw_spec {
1694 struct mwl8k_cmd_pkt header;
1695 __u8 hw_rev;
1696 __u8 host_interface;
1697 __le16 num_mcaddrs;
1698 __u8 perm_addr[ETH_ALEN];
1699 __le16 region_code;
1700 __le32 fw_rev;
1701 __le32 ps_cookie;
1702 __le32 caps;
1703 __le32 rx_queue_ptr;
1704 __le32 num_tx_queues;
1705 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1706 __le32 flags;
1707 __le32 num_tx_desc_per_queue;
1708 __le32 total_rxd;
1709} __attribute__((packed));
1710
1711#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1712
1713static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1714{
1715 struct mwl8k_priv *priv = hw->priv;
1716 struct mwl8k_cmd_set_hw_spec *cmd;
1717 int rc;
1718 int i;
1719
1720 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1721 if (cmd == NULL)
1722 return -ENOMEM;
1723
1724 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1725 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1726
1727 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1728 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1729 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1730 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1731 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1732 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1733 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1734 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1735
1736 rc = mwl8k_post_cmd(hw, &cmd->header);
1737 kfree(cmd);
1738
1739 return rc;
1740}
1741
1742/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001743 * CMD_MAC_MULTICAST_ADR.
1744 */
1745struct mwl8k_cmd_mac_multicast_adr {
1746 struct mwl8k_cmd_pkt header;
1747 __le16 action;
1748 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001749 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001750};
1751
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001752#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1753#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1754#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1755#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001756
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001757static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001758__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001759 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001760{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001761 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001763 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001764
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001765 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001766 allmulti = 1;
1767 mc_count = 0;
1768 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001769
1770 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1771
1772 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001773 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001774 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001775
1776 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1777 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001778 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1779 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001780
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001781 if (allmulti) {
1782 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1783 } else if (mc_count) {
1784 int i;
1785
1786 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1787 cmd->numaddr = cpu_to_le16(mc_count);
1788 for (i = 0; i < mc_count && mclist; i++) {
1789 if (mclist->da_addrlen != ETH_ALEN) {
1790 kfree(cmd);
1791 return NULL;
1792 }
1793 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1794 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001795 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001796 }
1797
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001798 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001799}
1800
1801/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001802 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001803 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001804struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001805 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001806 __le32 stats[64];
1807} __attribute__((packed));
1808
1809#define MWL8K_STAT_ACK_FAILURE 9
1810#define MWL8K_STAT_RTS_FAILURE 12
1811#define MWL8K_STAT_FCS_ERROR 24
1812#define MWL8K_STAT_RTS_SUCCESS 11
1813
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001814static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1815 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001816{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001817 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001818 int rc;
1819
1820 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1821 if (cmd == NULL)
1822 return -ENOMEM;
1823
1824 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1825 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001826
1827 rc = mwl8k_post_cmd(hw, &cmd->header);
1828 if (!rc) {
1829 stats->dot11ACKFailureCount =
1830 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1831 stats->dot11RTSFailureCount =
1832 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1833 stats->dot11FCSErrorCount =
1834 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1835 stats->dot11RTSSuccessCount =
1836 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1837 }
1838 kfree(cmd);
1839
1840 return rc;
1841}
1842
1843/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001844 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001845 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001846struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001847 struct mwl8k_cmd_pkt header;
1848 __le16 action;
1849 __le16 control;
1850 __le16 radio_on;
1851} __attribute__((packed));
1852
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001853static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001854mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001855{
1856 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001857 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001858 int rc;
1859
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001860 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001861 return 0;
1862
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001863 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1864 if (cmd == NULL)
1865 return -ENOMEM;
1866
1867 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1868 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1869 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001870 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001871 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1872
1873 rc = mwl8k_post_cmd(hw, &cmd->header);
1874 kfree(cmd);
1875
1876 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001877 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001878
1879 return rc;
1880}
1881
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001882static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001883{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001884 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001885}
1886
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001887static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001888{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001889 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001890}
1891
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001892static int
1893mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1894{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001895 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001896
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001897 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001898
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001899 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001900}
1901
1902/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001903 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001904 */
1905#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1906
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001907struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001908 struct mwl8k_cmd_pkt header;
1909 __le16 action;
1910 __le16 support_level;
1911 __le16 current_level;
1912 __le16 reserved;
1913 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1914} __attribute__((packed));
1915
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001916static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001917{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001918 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919 int rc;
1920
1921 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1922 if (cmd == NULL)
1923 return -ENOMEM;
1924
1925 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1926 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1927 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1928 cmd->support_level = cpu_to_le16(dBm);
1929
1930 rc = mwl8k_post_cmd(hw, &cmd->header);
1931 kfree(cmd);
1932
1933 return rc;
1934}
1935
1936/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02001937 * CMD_RF_ANTENNA.
1938 */
1939struct mwl8k_cmd_rf_antenna {
1940 struct mwl8k_cmd_pkt header;
1941 __le16 antenna;
1942 __le16 mode;
1943} __attribute__((packed));
1944
1945#define MWL8K_RF_ANTENNA_RX 1
1946#define MWL8K_RF_ANTENNA_TX 2
1947
1948static int
1949mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
1950{
1951 struct mwl8k_cmd_rf_antenna *cmd;
1952 int rc;
1953
1954 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1955 if (cmd == NULL)
1956 return -ENOMEM;
1957
1958 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
1959 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1960 cmd->antenna = cpu_to_le16(antenna);
1961 cmd->mode = cpu_to_le16(mask);
1962
1963 rc = mwl8k_post_cmd(hw, &cmd->header);
1964 kfree(cmd);
1965
1966 return rc;
1967}
1968
1969/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001970 * CMD_SET_PRE_SCAN.
1971 */
1972struct mwl8k_cmd_set_pre_scan {
1973 struct mwl8k_cmd_pkt header;
1974} __attribute__((packed));
1975
1976static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
1977{
1978 struct mwl8k_cmd_set_pre_scan *cmd;
1979 int rc;
1980
1981 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1982 if (cmd == NULL)
1983 return -ENOMEM;
1984
1985 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
1986 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1987
1988 rc = mwl8k_post_cmd(hw, &cmd->header);
1989 kfree(cmd);
1990
1991 return rc;
1992}
1993
1994/*
1995 * CMD_SET_POST_SCAN.
1996 */
1997struct mwl8k_cmd_set_post_scan {
1998 struct mwl8k_cmd_pkt header;
1999 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002000 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002001} __attribute__((packed));
2002
2003static int
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002004mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002005{
2006 struct mwl8k_cmd_set_post_scan *cmd;
2007 int rc;
2008
2009 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2010 if (cmd == NULL)
2011 return -ENOMEM;
2012
2013 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2014 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2015 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002016 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002017
2018 rc = mwl8k_post_cmd(hw, &cmd->header);
2019 kfree(cmd);
2020
2021 return rc;
2022}
2023
2024/*
2025 * CMD_SET_RF_CHANNEL.
2026 */
2027struct mwl8k_cmd_set_rf_channel {
2028 struct mwl8k_cmd_pkt header;
2029 __le16 action;
2030 __u8 current_channel;
2031 __le32 channel_flags;
2032} __attribute__((packed));
2033
2034static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2035 struct ieee80211_channel *channel)
2036{
2037 struct mwl8k_cmd_set_rf_channel *cmd;
2038 int rc;
2039
2040 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2041 if (cmd == NULL)
2042 return -ENOMEM;
2043
2044 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2045 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2046 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2047 cmd->current_channel = channel->hw_value;
2048 if (channel->band == IEEE80211_BAND_2GHZ)
2049 cmd->channel_flags = cpu_to_le32(0x00000081);
2050 else
2051 cmd->channel_flags = cpu_to_le32(0x00000000);
2052
2053 rc = mwl8k_post_cmd(hw, &cmd->header);
2054 kfree(cmd);
2055
2056 return rc;
2057}
2058
2059/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002060 * CMD_SET_AID.
2061 */
2062#define MWL8K_FRAME_PROT_DISABLED 0x00
2063#define MWL8K_FRAME_PROT_11G 0x07
2064#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2065#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2066
2067struct mwl8k_cmd_update_set_aid {
2068 struct mwl8k_cmd_pkt header;
2069 __le16 aid;
2070
2071 /* AP's MAC address (BSSID) */
2072 __u8 bssid[ETH_ALEN];
2073 __le16 protection_mode;
2074 __u8 supp_rates[14];
2075} __attribute__((packed));
2076
2077static int
2078mwl8k_cmd_set_aid(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2079{
2080 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002081 struct mwl8k_cmd_update_set_aid *cmd;
2082 u16 prot_mode;
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_AID);
2090 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002091 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002092
2093 memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
2094
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002095 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002096 prot_mode = MWL8K_FRAME_PROT_11G;
2097 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002098 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002099 IEEE80211_HT_OP_MODE_PROTECTION) {
2100 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2101 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2102 break;
2103 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2104 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2105 break;
2106 default:
2107 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2108 break;
2109 }
2110 }
2111 cmd->protection_mode = cpu_to_le16(prot_mode);
2112
2113 memcpy(cmd->supp_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2114
2115 rc = mwl8k_post_cmd(hw, &cmd->header);
2116 kfree(cmd);
2117
2118 return rc;
2119}
2120
2121/*
2122 * CMD_SET_RATE.
2123 */
2124struct mwl8k_cmd_set_rate {
2125 struct mwl8k_cmd_pkt header;
2126 __u8 legacy_rates[14];
2127
2128 /* Bitmap for supported MCS codes. */
2129 __u8 mcs_set[16];
2130 __u8 reserved[16];
2131} __attribute__((packed));
2132
2133static int
2134mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2135{
2136 struct mwl8k_cmd_set_rate *cmd;
2137 int rc;
2138
2139 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2140 if (cmd == NULL)
2141 return -ENOMEM;
2142
2143 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2144 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2145 memcpy(cmd->legacy_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2146
2147 rc = mwl8k_post_cmd(hw, &cmd->header);
2148 kfree(cmd);
2149
2150 return rc;
2151}
2152
2153/*
2154 * CMD_FINALIZE_JOIN.
2155 */
2156#define MWL8K_FJ_BEACON_MAXLEN 128
2157
2158struct mwl8k_cmd_finalize_join {
2159 struct mwl8k_cmd_pkt header;
2160 __le32 sleep_interval; /* Number of beacon periods to sleep */
2161 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2162} __attribute__((packed));
2163
2164static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2165 int framelen, int dtim)
2166{
2167 struct mwl8k_cmd_finalize_join *cmd;
2168 struct ieee80211_mgmt *payload = frame;
2169 int payload_len;
2170 int rc;
2171
2172 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2173 if (cmd == NULL)
2174 return -ENOMEM;
2175
2176 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2177 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2178 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2179
2180 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2181 if (payload_len < 0)
2182 payload_len = 0;
2183 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2184 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2185
2186 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2187
2188 rc = mwl8k_post_cmd(hw, &cmd->header);
2189 kfree(cmd);
2190
2191 return rc;
2192}
2193
2194/*
2195 * CMD_SET_RTS_THRESHOLD.
2196 */
2197struct mwl8k_cmd_set_rts_threshold {
2198 struct mwl8k_cmd_pkt header;
2199 __le16 action;
2200 __le16 threshold;
2201} __attribute__((packed));
2202
2203static int mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw,
2204 u16 action, u16 threshold)
2205{
2206 struct mwl8k_cmd_set_rts_threshold *cmd;
2207 int rc;
2208
2209 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2210 if (cmd == NULL)
2211 return -ENOMEM;
2212
2213 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2214 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2215 cmd->action = cpu_to_le16(action);
2216 cmd->threshold = cpu_to_le16(threshold);
2217
2218 rc = mwl8k_post_cmd(hw, &cmd->header);
2219 kfree(cmd);
2220
2221 return rc;
2222}
2223
2224/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002225 * CMD_SET_SLOT.
2226 */
2227struct mwl8k_cmd_set_slot {
2228 struct mwl8k_cmd_pkt header;
2229 __le16 action;
2230 __u8 short_slot;
2231} __attribute__((packed));
2232
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002233static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002234{
2235 struct mwl8k_cmd_set_slot *cmd;
2236 int rc;
2237
2238 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2239 if (cmd == NULL)
2240 return -ENOMEM;
2241
2242 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2243 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2244 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002245 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002246
2247 rc = mwl8k_post_cmd(hw, &cmd->header);
2248 kfree(cmd);
2249
2250 return rc;
2251}
2252
2253/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002254 * CMD_SET_EDCA_PARAMS.
2255 */
2256struct mwl8k_cmd_set_edca_params {
2257 struct mwl8k_cmd_pkt header;
2258
2259 /* See MWL8K_SET_EDCA_XXX below */
2260 __le16 action;
2261
2262 /* TX opportunity in units of 32 us */
2263 __le16 txop;
2264
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002265 union {
2266 struct {
2267 /* Log exponent of max contention period: 0...15 */
2268 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002269
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002270 /* Log exponent of min contention period: 0...15 */
2271 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002272
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002273 /* Adaptive interframe spacing in units of 32us */
2274 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002275
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002276 /* TX queue to configure */
2277 __u8 txq;
2278 } ap;
2279 struct {
2280 /* Log exponent of max contention period: 0...15 */
2281 __u8 log_cw_max;
2282
2283 /* Log exponent of min contention period: 0...15 */
2284 __u8 log_cw_min;
2285
2286 /* Adaptive interframe spacing in units of 32us */
2287 __u8 aifs;
2288
2289 /* TX queue to configure */
2290 __u8 txq;
2291 } sta;
2292 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002293} __attribute__((packed));
2294
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002295#define MWL8K_SET_EDCA_CW 0x01
2296#define MWL8K_SET_EDCA_TXOP 0x02
2297#define MWL8K_SET_EDCA_AIFS 0x04
2298
2299#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2300 MWL8K_SET_EDCA_TXOP | \
2301 MWL8K_SET_EDCA_AIFS)
2302
2303static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002304mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2305 __u16 cw_min, __u16 cw_max,
2306 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002307{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002308 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002309 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002310 int rc;
2311
2312 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2313 if (cmd == NULL)
2314 return -ENOMEM;
2315
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002316 /*
2317 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2318 * this call.
2319 */
2320 qnum ^= !(qnum >> 1);
2321
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002322 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2323 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002324 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2325 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002326 if (priv->ap_fw) {
2327 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2328 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2329 cmd->ap.aifs = aifs;
2330 cmd->ap.txq = qnum;
2331 } else {
2332 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2333 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2334 cmd->sta.aifs = aifs;
2335 cmd->sta.txq = qnum;
2336 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002337
2338 rc = mwl8k_post_cmd(hw, &cmd->header);
2339 kfree(cmd);
2340
2341 return rc;
2342}
2343
2344/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002345 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002346 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002347struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002348 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002349 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002350} __attribute__((packed));
2351
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002352static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002353{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002354 struct mwl8k_priv *priv = hw->priv;
2355 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002356 int rc;
2357
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002358 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2359 if (cmd == NULL)
2360 return -ENOMEM;
2361
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002362 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002363 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002364 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002365
2366 rc = mwl8k_post_cmd(hw, &cmd->header);
2367 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002368
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002369 if (!rc)
2370 priv->wmm_enabled = enable;
2371
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002372 return rc;
2373}
2374
2375/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002376 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002377 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002378struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002379 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002380 __le32 action;
2381 __u8 rx_antenna_map;
2382 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002383} __attribute__((packed));
2384
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002385static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002386{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002387 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002388 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002389
2390 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2391 if (cmd == NULL)
2392 return -ENOMEM;
2393
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002394 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002395 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002396 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2397 cmd->rx_antenna_map = rx;
2398 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002399
2400 rc = mwl8k_post_cmd(hw, &cmd->header);
2401 kfree(cmd);
2402
2403 return rc;
2404}
2405
2406/*
2407 * CMD_USE_FIXED_RATE.
2408 */
2409#define MWL8K_RATE_TABLE_SIZE 8
2410#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002411#define MWL8K_USE_AUTO_RATE 0x0002
2412
2413struct mwl8k_rate_entry {
2414 /* Set to 1 if HT rate, 0 if legacy. */
2415 __le32 is_ht_rate;
2416
2417 /* Set to 1 to use retry_count field. */
2418 __le32 enable_retry;
2419
2420 /* Specified legacy rate or MCS. */
2421 __le32 rate;
2422
2423 /* Number of allowed retries. */
2424 __le32 retry_count;
2425} __attribute__((packed));
2426
2427struct mwl8k_rate_table {
2428 /* 1 to allow specified rate and below */
2429 __le32 allow_rate_drop;
2430 __le32 num_rates;
2431 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2432} __attribute__((packed));
2433
2434struct mwl8k_cmd_use_fixed_rate {
2435 struct mwl8k_cmd_pkt header;
2436 __le32 action;
2437 struct mwl8k_rate_table rate_table;
2438
2439 /* Unicast, Broadcast or Multicast */
2440 __le32 rate_type;
2441 __le32 reserved1;
2442 __le32 reserved2;
2443} __attribute__((packed));
2444
2445static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2446 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2447{
2448 struct mwl8k_cmd_use_fixed_rate *cmd;
2449 int count;
2450 int rc;
2451
2452 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2453 if (cmd == NULL)
2454 return -ENOMEM;
2455
2456 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2457 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2458
2459 cmd->action = cpu_to_le32(action);
2460 cmd->rate_type = cpu_to_le32(rate_type);
2461
2462 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002463 /*
2464 * Copy over each field manually so that endian
2465 * conversion can be done.
2466 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002467 cmd->rate_table.allow_rate_drop =
2468 cpu_to_le32(rate_table->allow_rate_drop);
2469 cmd->rate_table.num_rates =
2470 cpu_to_le32(rate_table->num_rates);
2471
2472 for (count = 0; count < rate_table->num_rates; count++) {
2473 struct mwl8k_rate_entry *dst =
2474 &cmd->rate_table.rate_entry[count];
2475 struct mwl8k_rate_entry *src =
2476 &rate_table->rate_entry[count];
2477
2478 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2479 dst->enable_retry = cpu_to_le32(src->enable_retry);
2480 dst->rate = cpu_to_le32(src->rate);
2481 dst->retry_count = cpu_to_le32(src->retry_count);
2482 }
2483 }
2484
2485 rc = mwl8k_post_cmd(hw, &cmd->header);
2486 kfree(cmd);
2487
2488 return rc;
2489}
2490
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002491/*
2492 * CMD_ENABLE_SNIFFER.
2493 */
2494struct mwl8k_cmd_enable_sniffer {
2495 struct mwl8k_cmd_pkt header;
2496 __le32 action;
2497} __attribute__((packed));
2498
2499static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2500{
2501 struct mwl8k_cmd_enable_sniffer *cmd;
2502 int rc;
2503
2504 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2505 if (cmd == NULL)
2506 return -ENOMEM;
2507
2508 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2509 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2510 cmd->action = cpu_to_le32(!!enable);
2511
2512 rc = mwl8k_post_cmd(hw, &cmd->header);
2513 kfree(cmd);
2514
2515 return rc;
2516}
2517
2518/*
2519 * CMD_SET_MAC_ADDR.
2520 */
2521struct mwl8k_cmd_set_mac_addr {
2522 struct mwl8k_cmd_pkt header;
2523 union {
2524 struct {
2525 __le16 mac_type;
2526 __u8 mac_addr[ETH_ALEN];
2527 } mbss;
2528 __u8 mac_addr[ETH_ALEN];
2529 };
2530} __attribute__((packed));
2531
2532static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2533{
2534 struct mwl8k_priv *priv = hw->priv;
2535 struct mwl8k_cmd_set_mac_addr *cmd;
2536 int rc;
2537
2538 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2539 if (cmd == NULL)
2540 return -ENOMEM;
2541
2542 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2543 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2544 if (priv->ap_fw) {
2545 cmd->mbss.mac_type = 0;
2546 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2547 } else {
2548 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2549 }
2550
2551 rc = mwl8k_post_cmd(hw, &cmd->header);
2552 kfree(cmd);
2553
2554 return rc;
2555}
2556
2557/*
2558 * CMD_SET_RATEADAPT_MODE.
2559 */
2560struct mwl8k_cmd_set_rate_adapt_mode {
2561 struct mwl8k_cmd_pkt header;
2562 __le16 action;
2563 __le16 mode;
2564} __attribute__((packed));
2565
2566static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2567{
2568 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2569 int rc;
2570
2571 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2572 if (cmd == NULL)
2573 return -ENOMEM;
2574
2575 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2576 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2577 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2578 cmd->mode = cpu_to_le16(mode);
2579
2580 rc = mwl8k_post_cmd(hw, &cmd->header);
2581 kfree(cmd);
2582
2583 return rc;
2584}
2585
2586/*
2587 * CMD_UPDATE_STADB.
2588 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002589#define MWL8K_STA_DB_ADD_ENTRY 0
2590#define MWL8K_STA_DB_MODIFY_ENTRY 1
2591#define MWL8K_STA_DB_DEL_ENTRY 2
2592#define MWL8K_STA_DB_FLUSH 3
2593
2594/* Peer Entry flags - used to define the type of the peer node */
2595#define MWL8K_PEER_TYPE_ACCESSPOINT 2
2596
2597struct ewc_ht_info {
2598 __le16 control1;
2599 __le16 control2;
2600 __le16 control3;
2601} __attribute__((packed));
2602
2603struct peer_capability_info {
2604 /* Peer type - AP vs. STA. */
2605 __u8 peer_type;
2606
2607 /* Basic 802.11 capabilities from assoc resp. */
2608 __le16 basic_caps;
2609
2610 /* Set if peer supports 802.11n high throughput (HT). */
2611 __u8 ht_support;
2612
2613 /* Valid if HT is supported. */
2614 __le16 ht_caps;
2615 __u8 extended_ht_caps;
2616 struct ewc_ht_info ewc_info;
2617
2618 /* Legacy rate table. Intersection of our rates and peer rates. */
2619 __u8 legacy_rates[12];
2620
2621 /* HT rate table. Intersection of our rates and peer rates. */
2622 __u8 ht_rates[16];
2623 __u8 pad[16];
2624
2625 /* If set, interoperability mode, no proprietary extensions. */
2626 __u8 interop;
2627 __u8 pad2;
2628 __u8 station_id;
2629 __le16 amsdu_enabled;
2630} __attribute__((packed));
2631
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002632struct mwl8k_cmd_update_stadb {
2633 struct mwl8k_cmd_pkt header;
2634
2635 /* See STADB_ACTION_TYPE */
2636 __le32 action;
2637
2638 /* Peer MAC address */
2639 __u8 peer_addr[ETH_ALEN];
2640
2641 __le32 reserved;
2642
2643 /* Peer info - valid during add/update. */
2644 struct peer_capability_info peer_info;
2645} __attribute__((packed));
2646
2647static int mwl8k_cmd_update_stadb(struct ieee80211_hw *hw,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01002648 struct ieee80211_vif *vif, __u32 action, u8 *addr)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002649{
2650 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002651 struct mwl8k_cmd_update_stadb *cmd;
2652 struct peer_capability_info *peer_info;
2653 int rc;
2654
2655 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2656 if (cmd == NULL)
2657 return -ENOMEM;
2658
2659 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2660 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2661
2662 cmd->action = cpu_to_le32(action);
2663 peer_info = &cmd->peer_info;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01002664 memcpy(cmd->peer_addr, addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002665
2666 switch (action) {
2667 case MWL8K_STA_DB_ADD_ENTRY:
2668 case MWL8K_STA_DB_MODIFY_ENTRY:
2669 /* Build peer_info block */
2670 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002671 peer_info->basic_caps =
2672 cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002673 memcpy(peer_info->legacy_rates, mwl8k_rateids,
2674 sizeof(mwl8k_rateids));
2675 peer_info->interop = 1;
2676 peer_info->amsdu_enabled = 0;
2677
2678 rc = mwl8k_post_cmd(hw, &cmd->header);
2679 if (rc == 0)
2680 mv_vif->peer_id = peer_info->station_id;
2681
2682 break;
2683
2684 case MWL8K_STA_DB_DEL_ENTRY:
2685 case MWL8K_STA_DB_FLUSH:
2686 default:
2687 rc = mwl8k_post_cmd(hw, &cmd->header);
2688 if (rc == 0)
2689 mv_vif->peer_id = 0;
2690 break;
2691 }
2692 kfree(cmd);
2693
2694 return rc;
2695}
2696
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002697
2698/*
2699 * Interrupt handling.
2700 */
2701static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2702{
2703 struct ieee80211_hw *hw = dev_id;
2704 struct mwl8k_priv *priv = hw->priv;
2705 u32 status;
2706
2707 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2708 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2709
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002710 if (!status)
2711 return IRQ_NONE;
2712
2713 if (status & MWL8K_A2H_INT_TX_DONE)
2714 tasklet_schedule(&priv->tx_reclaim_task);
2715
2716 if (status & MWL8K_A2H_INT_RX_READY) {
2717 while (rxq_process(hw, 0, 1))
2718 rxq_refill(hw, 0, 1);
2719 }
2720
2721 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002722 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002723 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002724 }
2725
2726 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002727 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002728 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002729 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002730 }
2731
2732 return IRQ_HANDLED;
2733}
2734
2735
2736/*
2737 * Core driver operations.
2738 */
2739static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2740{
2741 struct mwl8k_priv *priv = hw->priv;
2742 int index = skb_get_queue_mapping(skb);
2743 int rc;
2744
2745 if (priv->current_channel == NULL) {
2746 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002747 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002748 dev_kfree_skb(skb);
2749 return NETDEV_TX_OK;
2750 }
2751
2752 rc = mwl8k_txq_xmit(hw, index, skb);
2753
2754 return rc;
2755}
2756
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002757static int mwl8k_start(struct ieee80211_hw *hw)
2758{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002759 struct mwl8k_priv *priv = hw->priv;
2760 int rc;
2761
Joe Perchesa0607fd2009-11-18 23:29:17 -08002762 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002763 IRQF_SHARED, MWL8K_NAME, hw);
2764 if (rc) {
2765 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002766 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002767 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002768 }
2769
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002770 /* Enable tx reclaim tasklet */
2771 tasklet_enable(&priv->tx_reclaim_task);
2772
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002773 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002774 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002775
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002776 rc = mwl8k_fw_lock(hw);
2777 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002778 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002779
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002780 if (!priv->ap_fw) {
2781 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002782 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002783
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002784 if (!rc)
2785 rc = mwl8k_cmd_set_pre_scan(hw);
2786
2787 if (!rc)
2788 rc = mwl8k_cmd_set_post_scan(hw,
2789 "\x00\x00\x00\x00\x00\x00");
2790 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002791
2792 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002793 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002794
2795 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002796 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002797
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002798 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002799 }
2800
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002801 if (rc) {
2802 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2803 free_irq(priv->pdev->irq, hw);
2804 tasklet_disable(&priv->tx_reclaim_task);
2805 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002806
2807 return rc;
2808}
2809
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002810static void mwl8k_stop(struct ieee80211_hw *hw)
2811{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002812 struct mwl8k_priv *priv = hw->priv;
2813 int i;
2814
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002815 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002816
2817 ieee80211_stop_queues(hw);
2818
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002819 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002820 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002821 free_irq(priv->pdev->irq, hw);
2822
2823 /* Stop finalize join worker */
2824 cancel_work_sync(&priv->finalize_join_worker);
2825 if (priv->beacon_skb != NULL)
2826 dev_kfree_skb(priv->beacon_skb);
2827
2828 /* Stop tx reclaim tasklet */
2829 tasklet_disable(&priv->tx_reclaim_task);
2830
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002831 /* Return all skbs to mac80211 */
2832 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2833 mwl8k_txq_reclaim(hw, i, 1);
2834}
2835
2836static int mwl8k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002837 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002838{
2839 struct mwl8k_priv *priv = hw->priv;
2840 struct mwl8k_vif *mwl8k_vif;
2841
2842 /*
2843 * We only support one active interface at a time.
2844 */
2845 if (priv->vif != NULL)
2846 return -EBUSY;
2847
2848 /*
2849 * We only support managed interfaces for now.
2850 */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002851 if (vif->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002852 return -EINVAL;
2853
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002854 /*
2855 * Reject interface creation if sniffer mode is active, as
2856 * STA operation is mutually exclusive with hardware sniffer
2857 * mode.
2858 */
2859 if (priv->sniffer_enabled) {
2860 printk(KERN_INFO "%s: unable to create STA "
2861 "interface due to sniffer mode being enabled\n",
2862 wiphy_name(hw->wiphy));
2863 return -EINVAL;
2864 }
2865
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002866 /* Clean out driver private area */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002867 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002868 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2869
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002870 /* Set and save the mac address */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002871 mwl8k_cmd_set_mac_addr(hw, vif->addr);
2872 memcpy(mwl8k_vif->mac_addr, vif->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002873
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002874 /* Set Initial sequence number to zero */
2875 mwl8k_vif->seqno = 0;
2876
Johannes Berg1ed32e42009-12-23 13:15:45 +01002877 priv->vif = vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002878 priv->current_channel = NULL;
2879
2880 return 0;
2881}
2882
2883static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002884 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002885{
2886 struct mwl8k_priv *priv = hw->priv;
2887
2888 if (priv->vif == NULL)
2889 return;
2890
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002891 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002892
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002893 priv->vif = NULL;
2894}
2895
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002896static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002897{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002898 struct ieee80211_conf *conf = &hw->conf;
2899 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002900 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002901
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002902 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002903 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002904 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002905 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002906 }
2907
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002908 rc = mwl8k_fw_lock(hw);
2909 if (rc)
2910 return rc;
2911
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002912 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002913 if (rc)
2914 goto out;
2915
2916 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2917 if (rc)
2918 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002919
2920 priv->current_channel = conf->channel;
2921
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002922 if (conf->power_level > 18)
2923 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002924 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002925 if (rc)
2926 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002927
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002928 if (priv->ap_fw) {
2929 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2930 if (!rc)
2931 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2932 } else {
2933 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2934 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002935
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002936out:
2937 mwl8k_fw_unlock(hw);
2938
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002939 return rc;
2940}
2941
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002942static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2943 struct ieee80211_vif *vif,
2944 struct ieee80211_bss_conf *info,
2945 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002946{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002947 struct mwl8k_priv *priv = hw->priv;
2948 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002949 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002950
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002951 if ((changed & BSS_CHANGED_ASSOC) == 0)
2952 return;
2953
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002954 priv->capture_beacon = false;
2955
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002956 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002957 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002958 return;
2959
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002960 if (vif->bss_conf.assoc) {
2961 memcpy(mwl8k_vif->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhekd1844d72009-11-30 18:13:56 +01002962
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002963 /* Install rates */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002964 rc = mwl8k_cmd_set_rate(hw, vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002965 if (rc)
2966 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002967
2968 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002969 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
2970 MWL8K_UCAST_RATE, NULL);
2971 if (rc)
2972 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002973
2974 /* Set radio preamble */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002975 rc = mwl8k_set_radio_preamble(hw,
2976 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002977 if (rc)
2978 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002979
2980 /* Set slot time */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002981 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002982 if (rc)
2983 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002984
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002985 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002986 rc = mwl8k_cmd_set_aid(hw, vif);
2987 if (rc)
2988 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002989
2990 /*
2991 * Finalize the join. Tell rx handler to process
2992 * next beacon from our BSSID.
2993 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002994 memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002995 priv->capture_beacon = true;
2996 } else {
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002997 memset(mwl8k_vif->bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002998 }
2999
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003000out:
3001 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003002}
3003
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003004static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3005 int mc_count, struct dev_addr_list *mclist)
3006{
3007 struct mwl8k_cmd_pkt *cmd;
3008
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003009 /*
3010 * Synthesize and return a command packet that programs the
3011 * hardware multicast address filter. At this point we don't
3012 * know whether FIF_ALLMULTI is being requested, but if it is,
3013 * we'll end up throwing this packet away and creating a new
3014 * one in mwl8k_configure_filter().
3015 */
3016 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003017
3018 return (unsigned long)cmd;
3019}
3020
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003021static int
3022mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3023 unsigned int changed_flags,
3024 unsigned int *total_flags)
3025{
3026 struct mwl8k_priv *priv = hw->priv;
3027
3028 /*
3029 * Hardware sniffer mode is mutually exclusive with STA
3030 * operation, so refuse to enable sniffer mode if a STA
3031 * interface is active.
3032 */
3033 if (priv->vif != NULL) {
3034 if (net_ratelimit())
3035 printk(KERN_INFO "%s: not enabling sniffer "
3036 "mode because STA interface is active\n",
3037 wiphy_name(hw->wiphy));
3038 return 0;
3039 }
3040
3041 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003042 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003043 return 0;
3044 priv->sniffer_enabled = true;
3045 }
3046
3047 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3048 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3049 FIF_OTHER_BSS;
3050
3051 return 1;
3052}
3053
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003054static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3055 unsigned int changed_flags,
3056 unsigned int *total_flags,
3057 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003058{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003059 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003060 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3061
3062 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003063 * AP firmware doesn't allow fine-grained control over
3064 * the receive filter.
3065 */
3066 if (priv->ap_fw) {
3067 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3068 kfree(cmd);
3069 return;
3070 }
3071
3072 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003073 * Enable hardware sniffer mode if FIF_CONTROL or
3074 * FIF_OTHER_BSS is requested.
3075 */
3076 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3077 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3078 kfree(cmd);
3079 return;
3080 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003081
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003082 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003083 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003084
3085 if (mwl8k_fw_lock(hw))
3086 return;
3087
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003088 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003089 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003090 priv->sniffer_enabled = false;
3091 }
3092
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003093 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003094 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3095 /*
3096 * Disable the BSS filter.
3097 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003098 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003099 } else {
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003100 u8 *bssid;
3101
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003102 /*
3103 * Enable the BSS filter.
3104 *
3105 * If there is an active STA interface, use that
3106 * interface's BSSID, otherwise use a dummy one
3107 * (where the OUI part needs to be nonzero for
3108 * the BSSID to be accepted by POST_SCAN).
3109 */
3110 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003111 if (priv->vif != NULL)
3112 bssid = MWL8K_VIF(priv->vif)->bssid;
3113
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003114 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003115 }
3116 }
3117
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003118 /*
3119 * If FIF_ALLMULTI is being requested, throw away the command
3120 * packet that ->prepare_multicast() built and replace it with
3121 * a command packet that enables reception of all multicast
3122 * packets.
3123 */
3124 if (*total_flags & FIF_ALLMULTI) {
3125 kfree(cmd);
3126 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3127 }
3128
3129 if (cmd != NULL) {
3130 mwl8k_post_cmd(hw, cmd);
3131 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003132 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003133
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003134 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003135}
3136
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003137static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3138{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003139 return mwl8k_cmd_set_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003140}
3141
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003142struct mwl8k_sta_notify_item
3143{
3144 struct list_head list;
3145 struct ieee80211_vif *vif;
3146 enum sta_notify_cmd cmd;
3147 u8 addr[ETH_ALEN];
3148};
3149
3150static void mwl8k_sta_notify_worker(struct work_struct *work)
3151{
3152 struct mwl8k_priv *priv =
3153 container_of(work, struct mwl8k_priv, sta_notify_worker);
3154
3155 spin_lock_bh(&priv->sta_notify_list_lock);
3156 while (!list_empty(&priv->sta_notify_list)) {
3157 struct mwl8k_sta_notify_item *s;
3158 int action;
3159
3160 s = list_entry(priv->sta_notify_list.next,
3161 struct mwl8k_sta_notify_item, list);
3162 list_del(&s->list);
3163
3164 spin_unlock_bh(&priv->sta_notify_list_lock);
3165
3166 if (s->cmd == STA_NOTIFY_ADD)
3167 action = MWL8K_STA_DB_MODIFY_ENTRY;
3168 else
3169 action = MWL8K_STA_DB_DEL_ENTRY;
3170 mwl8k_cmd_update_stadb(priv->hw, s->vif, action, s->addr);
3171
3172 kfree(s);
3173
3174 spin_lock_bh(&priv->sta_notify_list_lock);
3175 }
3176 spin_unlock_bh(&priv->sta_notify_list_lock);
3177}
3178
3179static void
3180mwl8k_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3181 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3182{
3183 struct mwl8k_priv *priv = hw->priv;
3184 struct mwl8k_sta_notify_item *s;
3185
3186 if (cmd != STA_NOTIFY_ADD && cmd != STA_NOTIFY_REMOVE)
3187 return;
3188
3189 s = kmalloc(sizeof(*s), GFP_ATOMIC);
3190 if (s != NULL) {
3191 s->vif = vif;
3192 s->cmd = cmd;
3193 memcpy(s->addr, sta->addr, ETH_ALEN);
3194
3195 spin_lock(&priv->sta_notify_list_lock);
3196 list_add_tail(&s->list, &priv->sta_notify_list);
3197 spin_unlock(&priv->sta_notify_list_lock);
3198
3199 ieee80211_queue_work(hw, &priv->sta_notify_worker);
3200 }
3201}
3202
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003203static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3204 const struct ieee80211_tx_queue_params *params)
3205{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003206 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003207 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003208
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003209 rc = mwl8k_fw_lock(hw);
3210 if (!rc) {
3211 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003212 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003213
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003214 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003215 rc = mwl8k_cmd_set_edca_params(hw, queue,
3216 params->cw_min,
3217 params->cw_max,
3218 params->aifs,
3219 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003220
3221 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003222 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003223
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003224 return rc;
3225}
3226
3227static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3228 struct ieee80211_tx_queue_stats *stats)
3229{
3230 struct mwl8k_priv *priv = hw->priv;
3231 struct mwl8k_tx_queue *txq;
3232 int index;
3233
3234 spin_lock_bh(&priv->tx_lock);
3235 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3236 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003237 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003238 sizeof(struct ieee80211_tx_queue_stats));
3239 }
3240 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003241
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003242 return 0;
3243}
3244
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003245static int mwl8k_get_stats(struct ieee80211_hw *hw,
3246 struct ieee80211_low_level_stats *stats)
3247{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003248 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003249}
3250
3251static const struct ieee80211_ops mwl8k_ops = {
3252 .tx = mwl8k_tx,
3253 .start = mwl8k_start,
3254 .stop = mwl8k_stop,
3255 .add_interface = mwl8k_add_interface,
3256 .remove_interface = mwl8k_remove_interface,
3257 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003258 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003259 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003260 .configure_filter = mwl8k_configure_filter,
3261 .set_rts_threshold = mwl8k_set_rts_threshold,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003262 .sta_notify = mwl8k_sta_notify,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003263 .conf_tx = mwl8k_conf_tx,
3264 .get_tx_stats = mwl8k_get_tx_stats,
3265 .get_stats = mwl8k_get_stats,
3266};
3267
3268static void mwl8k_tx_reclaim_handler(unsigned long data)
3269{
3270 int i;
3271 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3272 struct mwl8k_priv *priv = hw->priv;
3273
3274 spin_lock_bh(&priv->tx_lock);
3275 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3276 mwl8k_txq_reclaim(hw, i, 0);
3277
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003278 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003279 complete(priv->tx_wait);
3280 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003281 }
3282 spin_unlock_bh(&priv->tx_lock);
3283}
3284
3285static void mwl8k_finalize_join_worker(struct work_struct *work)
3286{
3287 struct mwl8k_priv *priv =
3288 container_of(work, struct mwl8k_priv, finalize_join_worker);
3289 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003290
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003291 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len,
3292 priv->vif->bss_conf.dtim_period);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003293 dev_kfree_skb(skb);
3294
3295 priv->beacon_skb = NULL;
3296}
3297
John W. Linvillebcb628d2009-11-06 16:40:16 -05003298enum {
3299 MWL8687 = 0,
3300 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003301};
3302
John W. Linvillebcb628d2009-11-06 16:40:16 -05003303static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003304 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003305 .part_name = "88w8687",
3306 .helper_image = "mwl8k/helper_8687.fw",
3307 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003308 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003309 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003310 .part_name = "88w8366",
3311 .helper_image = "mwl8k/helper_8366.fw",
3312 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003313 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003314 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003315};
3316
3317static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003318 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3319 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3320 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3321 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003322};
3323MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3324
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003325static int __devinit mwl8k_probe(struct pci_dev *pdev,
3326 const struct pci_device_id *id)
3327{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003328 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003329 struct ieee80211_hw *hw;
3330 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003331 int rc;
3332 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003333
3334 if (!printed_version) {
3335 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3336 printed_version = 1;
3337 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003338
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003339
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003340 rc = pci_enable_device(pdev);
3341 if (rc) {
3342 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3343 MWL8K_NAME);
3344 return rc;
3345 }
3346
3347 rc = pci_request_regions(pdev, MWL8K_NAME);
3348 if (rc) {
3349 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3350 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003351 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003352 }
3353
3354 pci_set_master(pdev);
3355
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003356
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003357 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3358 if (hw == NULL) {
3359 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3360 rc = -ENOMEM;
3361 goto err_free_reg;
3362 }
3363
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003364 SET_IEEE80211_DEV(hw, &pdev->dev);
3365 pci_set_drvdata(pdev, hw);
3366
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003367 priv = hw->priv;
3368 priv->hw = hw;
3369 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003370 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003371
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003372
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003373 priv->sram = pci_iomap(pdev, 0, 0x10000);
3374 if (priv->sram == NULL) {
3375 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003376 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003377 goto err_iounmap;
3378 }
3379
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003380 /*
3381 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3382 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3383 */
3384 priv->regs = pci_iomap(pdev, 1, 0x10000);
3385 if (priv->regs == NULL) {
3386 priv->regs = pci_iomap(pdev, 2, 0x10000);
3387 if (priv->regs == NULL) {
3388 printk(KERN_ERR "%s: Cannot map device registers\n",
3389 wiphy_name(hw->wiphy));
3390 goto err_iounmap;
3391 }
3392 }
3393
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003394
3395 /* Reset firmware and hardware */
3396 mwl8k_hw_reset(priv);
3397
3398 /* Ask userland hotplug daemon for the device firmware */
3399 rc = mwl8k_request_firmware(priv);
3400 if (rc) {
3401 printk(KERN_ERR "%s: Firmware files not found\n",
3402 wiphy_name(hw->wiphy));
3403 goto err_stop_firmware;
3404 }
3405
3406 /* Load firmware into hardware */
3407 rc = mwl8k_load_firmware(hw);
3408 if (rc) {
3409 printk(KERN_ERR "%s: Cannot start firmware\n",
3410 wiphy_name(hw->wiphy));
3411 goto err_stop_firmware;
3412 }
3413
3414 /* Reclaim memory once firmware is successfully loaded */
3415 mwl8k_release_firmware(priv);
3416
3417
Lennert Buytenhek91942232010-01-04 21:53:54 +01003418 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003419 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003420 if (priv->rxd_ops == NULL) {
3421 printk(KERN_ERR "%s: Driver does not have AP "
3422 "firmware image support for this hardware\n",
3423 wiphy_name(hw->wiphy));
3424 goto err_stop_firmware;
3425 }
3426 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003427 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003428 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003429
3430 priv->sniffer_enabled = false;
3431 priv->wmm_enabled = false;
3432 priv->pending_tx_pkts = 0;
3433
3434
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003435 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3436 priv->band.band = IEEE80211_BAND_2GHZ;
3437 priv->band.channels = priv->channels;
3438 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3439 priv->band.bitrates = priv->rates;
3440 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3441 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3442
3443 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3444 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3445
3446 /*
3447 * Extra headroom is the size of the required DMA header
3448 * minus the size of the smallest 802.11 frame (CTS frame).
3449 */
3450 hw->extra_tx_headroom =
3451 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3452
3453 hw->channel_change_time = 10;
3454
3455 hw->queues = MWL8K_TX_QUEUES;
3456
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003457 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003458 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003459 hw->vif_data_size = sizeof(struct mwl8k_vif);
3460 priv->vif = NULL;
3461
3462 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003463 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003464 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003465
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003466 /* Station database handling */
3467 INIT_WORK(&priv->sta_notify_worker, mwl8k_sta_notify_worker);
3468 spin_lock_init(&priv->sta_notify_list_lock);
3469 INIT_LIST_HEAD(&priv->sta_notify_list);
3470
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003471 /* Finalize join worker */
3472 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3473
3474 /* TX reclaim tasklet */
3475 tasklet_init(&priv->tx_reclaim_task,
3476 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3477 tasklet_disable(&priv->tx_reclaim_task);
3478
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003479 /* Power management cookie */
3480 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3481 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003482 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003483
3484 rc = mwl8k_rxq_init(hw, 0);
3485 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003486 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003487 rxq_refill(hw, 0, INT_MAX);
3488
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003489 mutex_init(&priv->fw_mutex);
3490 priv->fw_mutex_owner = NULL;
3491 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003492 priv->hostcmd_wait = NULL;
3493
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003494 spin_lock_init(&priv->tx_lock);
3495
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003496 priv->tx_wait = NULL;
3497
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003498 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3499 rc = mwl8k_txq_init(hw, i);
3500 if (rc)
3501 goto err_free_queues;
3502 }
3503
3504 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003505 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003506 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3507 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3508
Joe Perchesa0607fd2009-11-18 23:29:17 -08003509 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003510 IRQF_SHARED, MWL8K_NAME, hw);
3511 if (rc) {
3512 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003513 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003514 goto err_free_queues;
3515 }
3516
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003517 /*
3518 * Temporarily enable interrupts. Initial firmware host
3519 * commands use interrupts and avoids polling. Disable
3520 * interrupts when done.
3521 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003522 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003523
3524 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003525 if (priv->ap_fw) {
3526 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3527 if (!rc)
3528 rc = mwl8k_cmd_set_hw_spec(hw);
3529 } else {
3530 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003531
3532 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003533 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003534 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003535 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3536 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003537 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003538 }
3539
3540 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003541 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003542 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003543 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003544 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003545 }
3546
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003547 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003548 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003549 if (rc) {
3550 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3551 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003552 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003553 }
3554
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003555 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003556 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003557 free_irq(priv->pdev->irq, hw);
3558
3559 rc = ieee80211_register_hw(hw);
3560 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003561 printk(KERN_ERR "%s: Cannot register device\n",
3562 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01003563 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003564 }
3565
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003566 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003567 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003568 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003569 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003570 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3571 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003572
3573 return 0;
3574
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003575err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003576 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003577 free_irq(priv->pdev->irq, hw);
3578
3579err_free_queues:
3580 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3581 mwl8k_txq_deinit(hw, i);
3582 mwl8k_rxq_deinit(hw, 0);
3583
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003584err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003585 if (priv->cookie != NULL)
3586 pci_free_consistent(priv->pdev, 4,
3587 priv->cookie, priv->cookie_dma);
3588
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003589err_stop_firmware:
3590 mwl8k_hw_reset(priv);
3591 mwl8k_release_firmware(priv);
3592
3593err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003594 if (priv->regs != NULL)
3595 pci_iounmap(pdev, priv->regs);
3596
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003597 if (priv->sram != NULL)
3598 pci_iounmap(pdev, priv->sram);
3599
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003600 pci_set_drvdata(pdev, NULL);
3601 ieee80211_free_hw(hw);
3602
3603err_free_reg:
3604 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003605
3606err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003607 pci_disable_device(pdev);
3608
3609 return rc;
3610}
3611
Joerg Albert230f7af2009-04-18 02:10:45 +02003612static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003613{
3614 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3615}
3616
Joerg Albert230f7af2009-04-18 02:10:45 +02003617static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003618{
3619 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3620 struct mwl8k_priv *priv;
3621 int i;
3622
3623 if (hw == NULL)
3624 return;
3625 priv = hw->priv;
3626
3627 ieee80211_stop_queues(hw);
3628
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003629 ieee80211_unregister_hw(hw);
3630
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003631 /* Remove tx reclaim tasklet */
3632 tasklet_kill(&priv->tx_reclaim_task);
3633
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003634 /* Stop hardware */
3635 mwl8k_hw_reset(priv);
3636
3637 /* Return all skbs to mac80211 */
3638 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3639 mwl8k_txq_reclaim(hw, i, 1);
3640
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003641 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3642 mwl8k_txq_deinit(hw, i);
3643
3644 mwl8k_rxq_deinit(hw, 0);
3645
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003646 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003647
3648 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003649 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003650 pci_set_drvdata(pdev, NULL);
3651 ieee80211_free_hw(hw);
3652 pci_release_regions(pdev);
3653 pci_disable_device(pdev);
3654}
3655
3656static struct pci_driver mwl8k_driver = {
3657 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003658 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003659 .probe = mwl8k_probe,
3660 .remove = __devexit_p(mwl8k_remove),
3661 .shutdown = __devexit_p(mwl8k_shutdown),
3662};
3663
3664static int __init mwl8k_init(void)
3665{
3666 return pci_register_driver(&mwl8k_driver);
3667}
3668
3669static void __exit mwl8k_exit(void)
3670{
3671 pci_unregister_driver(&mwl8k_driver);
3672}
3673
3674module_init(mwl8k_init);
3675module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003676
3677MODULE_DESCRIPTION(MWL8K_DESC);
3678MODULE_VERSION(MWL8K_VERSION);
3679MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3680MODULE_LICENSE("GPL");