blob: ed666b7e7e412ae0af38509ed2728859e49b904d [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 Buytenhek55489b62009-11-30 18:31:33 +0100214 /* Index into station database. Returned by UPDATE_STADB. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100215 u8 peer_id;
216
217 /* Non AMPDU sequence number assigned by driver */
218 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100219};
220
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200221#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100222
223static const struct ieee80211_channel mwl8k_channels[] = {
224 { .center_freq = 2412, .hw_value = 1, },
225 { .center_freq = 2417, .hw_value = 2, },
226 { .center_freq = 2422, .hw_value = 3, },
227 { .center_freq = 2427, .hw_value = 4, },
228 { .center_freq = 2432, .hw_value = 5, },
229 { .center_freq = 2437, .hw_value = 6, },
230 { .center_freq = 2442, .hw_value = 7, },
231 { .center_freq = 2447, .hw_value = 8, },
232 { .center_freq = 2452, .hw_value = 9, },
233 { .center_freq = 2457, .hw_value = 10, },
234 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100235 { .center_freq = 2467, .hw_value = 12, },
236 { .center_freq = 2472, .hw_value = 13, },
237 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100238};
239
240static const struct ieee80211_rate mwl8k_rates[] = {
241 { .bitrate = 10, .hw_value = 2, },
242 { .bitrate = 20, .hw_value = 4, },
243 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200244 { .bitrate = 110, .hw_value = 22, },
245 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100246 { .bitrate = 60, .hw_value = 12, },
247 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100248 { .bitrate = 120, .hw_value = 24, },
249 { .bitrate = 180, .hw_value = 36, },
250 { .bitrate = 240, .hw_value = 48, },
251 { .bitrate = 360, .hw_value = 72, },
252 { .bitrate = 480, .hw_value = 96, },
253 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100254 { .bitrate = 720, .hw_value = 144, },
255};
256
257static const u8 mwl8k_rateids[12] = {
258 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108,
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100259};
260
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100261/* Set or get info from Firmware */
262#define MWL8K_CMD_SET 0x0001
263#define MWL8K_CMD_GET 0x0000
264
265/* Firmware command codes */
266#define MWL8K_CMD_CODE_DNLD 0x0001
267#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200268#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100269#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
270#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200271#define MWL8K_CMD_RADIO_CONTROL 0x001c
272#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200273#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100274#define MWL8K_CMD_SET_PRE_SCAN 0x0107
275#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200276#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100277#define MWL8K_CMD_SET_AID 0x010d
278#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200279#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100280#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200281#define MWL8K_CMD_SET_SLOT 0x0114
282#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
283#define MWL8K_CMD_SET_WMM_MODE 0x0123
284#define MWL8K_CMD_MIMO_CONFIG 0x0125
285#define MWL8K_CMD_USE_FIXED_RATE 0x0126
286#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200287#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200288#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
289#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100290
291static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
292{
293#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
294 snprintf(buf, bufsize, "%s", #x);\
295 return buf;\
296 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200297 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100298 MWL8K_CMDNAME(CODE_DNLD);
299 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200300 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
302 MWL8K_CMDNAME(GET_STAT);
303 MWL8K_CMDNAME(RADIO_CONTROL);
304 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200305 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100306 MWL8K_CMDNAME(SET_PRE_SCAN);
307 MWL8K_CMDNAME(SET_POST_SCAN);
308 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100309 MWL8K_CMDNAME(SET_AID);
310 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200311 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100312 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200313 MWL8K_CMDNAME(SET_SLOT);
314 MWL8K_CMDNAME(SET_EDCA_PARAMS);
315 MWL8K_CMDNAME(SET_WMM_MODE);
316 MWL8K_CMDNAME(MIMO_CONFIG);
317 MWL8K_CMDNAME(USE_FIXED_RATE);
318 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200319 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200320 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
321 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100322 default:
323 snprintf(buf, bufsize, "0x%x", cmd);
324 }
325#undef MWL8K_CMDNAME
326
327 return buf;
328}
329
330/* Hardware and firmware reset */
331static void mwl8k_hw_reset(struct mwl8k_priv *priv)
332{
333 iowrite32(MWL8K_H2A_INT_RESET,
334 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
335 iowrite32(MWL8K_H2A_INT_RESET,
336 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
337 msleep(20);
338}
339
340/* Release fw image */
341static void mwl8k_release_fw(struct firmware **fw)
342{
343 if (*fw == NULL)
344 return;
345 release_firmware(*fw);
346 *fw = NULL;
347}
348
349static void mwl8k_release_firmware(struct mwl8k_priv *priv)
350{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100351 mwl8k_release_fw(&priv->fw_ucode);
352 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100353}
354
355/* Request fw image */
356static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200357 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100358{
359 /* release current image */
360 if (*fw != NULL)
361 mwl8k_release_fw(fw);
362
363 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200364 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100365}
366
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200367static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100368{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200369 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100370 int rc;
371
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200372 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100373 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200374 if (rc) {
375 printk(KERN_ERR "%s: Error requesting helper "
376 "firmware file %s\n", pci_name(priv->pdev),
377 di->helper_image);
378 return rc;
379 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100380 }
381
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100382 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100383 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200384 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200385 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100386 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100387 return rc;
388 }
389
390 return 0;
391}
392
Ben Hutchings7e75b942009-11-07 22:00:57 +0000393MODULE_FIRMWARE("mwl8k/helper_8687.fw");
394MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
395
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100396struct mwl8k_cmd_pkt {
397 __le16 code;
398 __le16 length;
399 __le16 seq_num;
400 __le16 result;
401 char payload[0];
402} __attribute__((packed));
403
404/*
405 * Firmware loading.
406 */
407static int
408mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
409{
410 void __iomem *regs = priv->regs;
411 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100412 int loops;
413
414 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
415 if (pci_dma_mapping_error(priv->pdev, dma_addr))
416 return -ENOMEM;
417
418 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
419 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
420 iowrite32(MWL8K_H2A_INT_DOORBELL,
421 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
422 iowrite32(MWL8K_H2A_INT_DUMMY,
423 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
424
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100425 loops = 1000;
426 do {
427 u32 int_code;
428
429 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
430 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
431 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100432 break;
433 }
434
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200435 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100436 udelay(1);
437 } while (--loops);
438
439 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
440
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200441 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100442}
443
444static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
445 const u8 *data, size_t length)
446{
447 struct mwl8k_cmd_pkt *cmd;
448 int done;
449 int rc = 0;
450
451 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
452 if (cmd == NULL)
453 return -ENOMEM;
454
455 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
456 cmd->seq_num = 0;
457 cmd->result = 0;
458
459 done = 0;
460 while (length) {
461 int block_size = length > 256 ? 256 : length;
462
463 memcpy(cmd->payload, data + done, block_size);
464 cmd->length = cpu_to_le16(block_size);
465
466 rc = mwl8k_send_fw_load_cmd(priv, cmd,
467 sizeof(*cmd) + block_size);
468 if (rc)
469 break;
470
471 done += block_size;
472 length -= block_size;
473 }
474
475 if (!rc) {
476 cmd->length = 0;
477 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
478 }
479
480 kfree(cmd);
481
482 return rc;
483}
484
485static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
486 const u8 *data, size_t length)
487{
488 unsigned char *buffer;
489 int may_continue, rc = 0;
490 u32 done, prev_block_size;
491
492 buffer = kmalloc(1024, GFP_KERNEL);
493 if (buffer == NULL)
494 return -ENOMEM;
495
496 done = 0;
497 prev_block_size = 0;
498 may_continue = 1000;
499 while (may_continue > 0) {
500 u32 block_size;
501
502 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
503 if (block_size & 1) {
504 block_size &= ~1;
505 may_continue--;
506 } else {
507 done += prev_block_size;
508 length -= prev_block_size;
509 }
510
511 if (block_size > 1024 || block_size > length) {
512 rc = -EOVERFLOW;
513 break;
514 }
515
516 if (length == 0) {
517 rc = 0;
518 break;
519 }
520
521 if (block_size == 0) {
522 rc = -EPROTO;
523 may_continue--;
524 udelay(1);
525 continue;
526 }
527
528 prev_block_size = block_size;
529 memcpy(buffer, data + done, block_size);
530
531 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
532 if (rc)
533 break;
534 }
535
536 if (!rc && length != 0)
537 rc = -EREMOTEIO;
538
539 kfree(buffer);
540
541 return rc;
542}
543
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200544static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100545{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200546 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100547 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200548 int rc;
549 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100550
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200551 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100552 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100553
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200554 if (helper == NULL) {
555 printk(KERN_ERR "%s: helper image needed but none "
556 "given\n", pci_name(priv->pdev));
557 return -EINVAL;
558 }
559
560 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100561 if (rc) {
562 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200563 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100564 return rc;
565 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100566 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100567
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200568 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100569 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200570 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100571 }
572
573 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200574 printk(KERN_ERR "%s: unable to load firmware image\n",
575 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100576 return rc;
577 }
578
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100579 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100580
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100581 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100582 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200583 u32 ready_code;
584
585 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
586 if (ready_code == MWL8K_FWAP_READY) {
587 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100588 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200589 } else if (ready_code == MWL8K_FWSTA_READY) {
590 priv->ap_fw = 0;
591 break;
592 }
593
594 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100595 udelay(1);
596 } while (--loops);
597
598 return loops ? 0 : -ETIMEDOUT;
599}
600
601
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100602/* DMA header used by firmware and hardware. */
603struct mwl8k_dma_data {
604 __le16 fwlen;
605 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100606 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100607} __attribute__((packed));
608
609/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100610static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100611{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100612 struct mwl8k_dma_data *tr;
613 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100614
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100615 tr = (struct mwl8k_dma_data *)skb->data;
616 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
617
618 if (hdrlen != sizeof(tr->wh)) {
619 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
620 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
621 *((__le16 *)(tr->data - 2)) = qos;
622 } else {
623 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
624 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100625 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100626
627 if (hdrlen != sizeof(*tr))
628 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100629}
630
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200631static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100632{
633 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100634 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100635 struct mwl8k_dma_data *tr;
636
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100637 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100638 * Add a firmware DMA header; the firmware requires that we
639 * present a 2-byte payload length followed by a 4-address
640 * header (without QoS field), followed (optionally) by any
641 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100642 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100643 wh = (struct ieee80211_hdr *)skb->data;
644
645 hdrlen = ieee80211_hdrlen(wh->frame_control);
646 if (hdrlen != sizeof(*tr))
647 skb_push(skb, sizeof(*tr) - hdrlen);
648
649 if (ieee80211_is_data_qos(wh->frame_control))
650 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100651
652 tr = (struct mwl8k_dma_data *)skb->data;
653 if (wh != &tr->wh)
654 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100655 if (hdrlen != sizeof(tr->wh))
656 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100657
658 /*
659 * Firmware length is the length of the fully formed "802.11
660 * payload". That is, everything except for the 802.11 header.
661 * This includes all crypto material including the MIC.
662 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100663 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100664}
665
666
667/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100668 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200669 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100670struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200671 __le16 pkt_len;
672 __u8 sq2;
673 __u8 rate;
674 __le32 pkt_phys_addr;
675 __le32 next_rxd_phys_addr;
676 __le16 qos_control;
677 __le16 htsig2;
678 __le32 hw_rssi_info;
679 __le32 hw_noise_floor_info;
680 __u8 noise_floor;
681 __u8 pad0[3];
682 __u8 rssi;
683 __u8 rx_status;
684 __u8 channel;
685 __u8 rx_ctrl;
686} __attribute__((packed));
687
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100688#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
689#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
690#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100691
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100692#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200693
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100694static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200695{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100696 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200697
698 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100699 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200700}
701
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100702static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200703{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100704 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200705
706 rxd->pkt_len = cpu_to_le16(len);
707 rxd->pkt_phys_addr = cpu_to_le32(addr);
708 wmb();
709 rxd->rx_ctrl = 0;
710}
711
712static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100713mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
714 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200715{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100716 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200717
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100718 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200719 return -1;
720 rmb();
721
722 memset(status, 0, sizeof(*status));
723
724 status->signal = -rxd->rssi;
725 status->noise = -rxd->noise_floor;
726
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100727 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200728 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100729 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100730 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100731 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200732 } else {
733 int i;
734
735 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
736 if (mwl8k_rates[i].hw_value == rxd->rate) {
737 status->rate_idx = i;
738 break;
739 }
740 }
741 }
742
743 status->band = IEEE80211_BAND_2GHZ;
744 status->freq = ieee80211_channel_to_frequency(rxd->channel);
745
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100746 *qos = rxd->qos_control;
747
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200748 return le16_to_cpu(rxd->pkt_len);
749}
750
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100751static struct rxd_ops rxd_8366_ap_ops = {
752 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
753 .rxd_init = mwl8k_rxd_8366_ap_init,
754 .rxd_refill = mwl8k_rxd_8366_ap_refill,
755 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200756};
757
758/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100759 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100760 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100761struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100762 __le16 pkt_len;
763 __u8 link_quality;
764 __u8 noise_level;
765 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200766 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100767 __le16 qos_control;
768 __le16 rate_info;
769 __le32 pad0[4];
770 __u8 rssi;
771 __u8 channel;
772 __le16 pad1;
773 __u8 rx_ctrl;
774 __u8 rx_status;
775 __u8 pad2[2];
776} __attribute__((packed));
777
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100778#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
779#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
780#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
781#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
782#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
783#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200784
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100785#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200786
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100787static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200788{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100789 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200790
791 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100792 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200793}
794
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100795static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200796{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100797 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200798
799 rxd->pkt_len = cpu_to_le16(len);
800 rxd->pkt_phys_addr = cpu_to_le32(addr);
801 wmb();
802 rxd->rx_ctrl = 0;
803}
804
805static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100806mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100807 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200808{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100809 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200810 u16 rate_info;
811
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100812 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200813 return -1;
814 rmb();
815
816 rate_info = le16_to_cpu(rxd->rate_info);
817
818 memset(status, 0, sizeof(*status));
819
820 status->signal = -rxd->rssi;
821 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100822 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
823 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200824
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100825 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200826 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100827 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200828 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100829 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200830 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100831 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200832 status->flag |= RX_FLAG_HT;
833
834 status->band = IEEE80211_BAND_2GHZ;
835 status->freq = ieee80211_channel_to_frequency(rxd->channel);
836
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100837 *qos = rxd->qos_control;
838
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200839 return le16_to_cpu(rxd->pkt_len);
840}
841
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100842static struct rxd_ops rxd_sta_ops = {
843 .rxd_size = sizeof(struct mwl8k_rxd_sta),
844 .rxd_init = mwl8k_rxd_sta_init,
845 .rxd_refill = mwl8k_rxd_sta_refill,
846 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200847};
848
849
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100850#define MWL8K_RX_DESCS 256
851#define MWL8K_RX_MAXSZ 3800
852
853static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
854{
855 struct mwl8k_priv *priv = hw->priv;
856 struct mwl8k_rx_queue *rxq = priv->rxq + index;
857 int size;
858 int i;
859
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200860 rxq->rxd_count = 0;
861 rxq->head = 0;
862 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100863
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200864 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100865
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200866 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
867 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100868 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200869 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100870 return -ENOMEM;
871 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200872 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100873
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200874 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
875 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100876 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200877 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200878 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100879 return -ENOMEM;
880 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200881 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100882
883 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200884 int desc_size;
885 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100886 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200887 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100888
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200889 desc_size = priv->rxd_ops->rxd_size;
890 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100891
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200892 nexti = i + 1;
893 if (nexti == MWL8K_RX_DESCS)
894 nexti = 0;
895 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
896
897 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100898 }
899
900 return 0;
901}
902
903static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
904{
905 struct mwl8k_priv *priv = hw->priv;
906 struct mwl8k_rx_queue *rxq = priv->rxq + index;
907 int refilled;
908
909 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200910 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100911 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200912 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100913 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200914 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100915
916 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
917 if (skb == NULL)
918 break;
919
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200920 addr = pci_map_single(priv->pdev, skb->data,
921 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100922
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200923 rxq->rxd_count++;
924 rx = rxq->tail++;
925 if (rxq->tail == MWL8K_RX_DESCS)
926 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200927 rxq->buf[rx].skb = skb;
928 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200929
930 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
931 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100932
933 refilled++;
934 }
935
936 return refilled;
937}
938
939/* Must be called only when the card's reception is completely halted */
940static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
941{
942 struct mwl8k_priv *priv = hw->priv;
943 struct mwl8k_rx_queue *rxq = priv->rxq + index;
944 int i;
945
946 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200947 if (rxq->buf[i].skb != NULL) {
948 pci_unmap_single(priv->pdev,
949 pci_unmap_addr(&rxq->buf[i], dma),
950 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
951 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100952
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200953 kfree_skb(rxq->buf[i].skb);
954 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100955 }
956 }
957
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200958 kfree(rxq->buf);
959 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100960
961 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200962 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200963 rxq->rxd, rxq->rxd_dma);
964 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100965}
966
967
968/*
969 * Scan a list of BSSIDs to process for finalize join.
970 * Allows for extension to process multiple BSSIDs.
971 */
972static inline int
973mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
974{
975 return priv->capture_beacon &&
976 ieee80211_is_beacon(wh->frame_control) &&
977 !compare_ether_addr(wh->addr3, priv->capture_bssid);
978}
979
Lennert Buytenhek37797522009-10-22 20:19:45 +0200980static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
981 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100982{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200983 struct mwl8k_priv *priv = hw->priv;
984
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100985 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200986 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100987
988 /*
989 * Use GFP_ATOMIC as rxq_process is called from
990 * the primary interrupt handler, memory allocation call
991 * must not sleep.
992 */
993 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
994 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200995 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100996}
997
998static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
999{
1000 struct mwl8k_priv *priv = hw->priv;
1001 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1002 int processed;
1003
1004 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001005 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001006 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001007 void *rxd;
1008 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001009 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001010 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001011
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001012 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001013 if (skb == NULL)
1014 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001015
1016 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1017
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001018 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001019 if (pkt_len < 0)
1020 break;
1021
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001022 rxq->buf[rxq->head].skb = NULL;
1023
1024 pci_unmap_single(priv->pdev,
1025 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1026 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1027 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001028
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001029 rxq->head++;
1030 if (rxq->head == MWL8K_RX_DESCS)
1031 rxq->head = 0;
1032
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001033 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001034
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001035 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001036 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001037
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001038 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001039 * Check for a pending join operation. Save a
1040 * copy of the beacon and schedule a tasklet to
1041 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001042 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001043 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001044 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001045
Johannes Bergf1d58c22009-06-17 13:13:00 +02001046 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1047 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001048
1049 processed++;
1050 }
1051
1052 return processed;
1053}
1054
1055
1056/*
1057 * Packet transmission.
1058 */
1059
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001060#define MWL8K_TXD_STATUS_OK 0x00000001
1061#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1062#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1063#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001064#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001065
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001066#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1067#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1068#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1069#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1070#define MWL8K_QOS_EOSP 0x0010
1071
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001072struct mwl8k_tx_desc {
1073 __le32 status;
1074 __u8 data_rate;
1075 __u8 tx_priority;
1076 __le16 qos_control;
1077 __le32 pkt_phys_addr;
1078 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001079 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001080 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001081 __le32 reserved;
1082 __le16 rate_info;
1083 __u8 peer_id;
1084 __u8 tx_frag_cnt;
1085} __attribute__((packed));
1086
1087#define MWL8K_TX_DESCS 128
1088
1089static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1090{
1091 struct mwl8k_priv *priv = hw->priv;
1092 struct mwl8k_tx_queue *txq = priv->txq + index;
1093 int size;
1094 int i;
1095
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001096 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1097 txq->stats.limit = MWL8K_TX_DESCS;
1098 txq->head = 0;
1099 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001100
1101 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1102
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001103 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1104 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001105 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001106 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001107 return -ENOMEM;
1108 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001109 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001110
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001111 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1112 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001113 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001114 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001115 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001116 return -ENOMEM;
1117 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001118 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001119
1120 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1121 struct mwl8k_tx_desc *tx_desc;
1122 int nexti;
1123
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001124 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001125 nexti = (i + 1) % MWL8K_TX_DESCS;
1126
1127 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001128 tx_desc->next_txd_phys_addr =
1129 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001130 }
1131
1132 return 0;
1133}
1134
1135static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1136{
1137 iowrite32(MWL8K_H2A_INT_PPA_READY,
1138 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1139 iowrite32(MWL8K_H2A_INT_DUMMY,
1140 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1141 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1142}
1143
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001144static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001145{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001146 struct mwl8k_priv *priv = hw->priv;
1147 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001148
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001149 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1150 struct mwl8k_tx_queue *txq = priv->txq + i;
1151 int fw_owned = 0;
1152 int drv_owned = 0;
1153 int unused = 0;
1154 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001155
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001156 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001157 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1158 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001160 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001161 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001162 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001164 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001165
1166 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001167 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001168 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001169
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001170 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1171 "fw_owned=%d drv_owned=%d unused=%d\n",
1172 wiphy_name(hw->wiphy), i,
1173 txq->stats.len, txq->head, txq->tail,
1174 fw_owned, drv_owned, unused);
1175 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001176}
1177
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001178/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001179 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001180 */
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001181#define MWL8K_TX_WAIT_TIMEOUT_MS 1000
1182
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001183static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001184{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001185 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001186 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001187 int retry;
1188 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001189
1190 might_sleep();
1191
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001192 /*
1193 * The TX queues are stopped at this point, so this test
1194 * doesn't need to take ->tx_lock.
1195 */
1196 if (!priv->pending_tx_pkts)
1197 return 0;
1198
1199 retry = 0;
1200 rc = 0;
1201
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001202 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001203 priv->tx_wait = &tx_wait;
1204 while (!rc) {
1205 int oldcount;
1206 unsigned long timeout;
1207
1208 oldcount = priv->pending_tx_pkts;
1209
1210 spin_unlock_bh(&priv->tx_lock);
1211 timeout = wait_for_completion_timeout(&tx_wait,
1212 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1213 spin_lock_bh(&priv->tx_lock);
1214
1215 if (timeout) {
1216 WARN_ON(priv->pending_tx_pkts);
1217 if (retry) {
1218 printk(KERN_NOTICE "%s: tx rings drained\n",
1219 wiphy_name(hw->wiphy));
1220 }
1221 break;
1222 }
1223
1224 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001225 printk(KERN_NOTICE "%s: waiting for tx rings "
1226 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001227 wiphy_name(hw->wiphy), oldcount,
1228 priv->pending_tx_pkts);
1229 retry = 1;
1230 continue;
1231 }
1232
1233 priv->tx_wait = NULL;
1234
1235 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1236 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1237 mwl8k_dump_tx_rings(hw);
1238
1239 rc = -ETIMEDOUT;
1240 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001241 spin_unlock_bh(&priv->tx_lock);
1242
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001243 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001244}
1245
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001246#define MWL8K_TXD_SUCCESS(status) \
1247 ((status) & (MWL8K_TXD_STATUS_OK | \
1248 MWL8K_TXD_STATUS_OK_RETRY | \
1249 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001250
1251static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1252{
1253 struct mwl8k_priv *priv = hw->priv;
1254 struct mwl8k_tx_queue *txq = priv->txq + index;
1255 int wake = 0;
1256
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001257 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001258 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001259 struct mwl8k_tx_desc *tx_desc;
1260 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001261 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001262 struct sk_buff *skb;
1263 struct ieee80211_tx_info *info;
1264 u32 status;
1265
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001266 tx = txq->head;
1267 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001268
1269 status = le32_to_cpu(tx_desc->status);
1270
1271 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1272 if (!force)
1273 break;
1274 tx_desc->status &=
1275 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1276 }
1277
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001278 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1279 BUG_ON(txq->stats.len == 0);
1280 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001281 priv->pending_tx_pkts--;
1282
1283 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001284 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001285 skb = txq->skb[tx];
1286 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001287
1288 BUG_ON(skb == NULL);
1289 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1290
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001291 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001292
1293 /* Mark descriptor as unused */
1294 tx_desc->pkt_phys_addr = 0;
1295 tx_desc->pkt_len = 0;
1296
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001297 info = IEEE80211_SKB_CB(skb);
1298 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001299 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001301
1302 ieee80211_tx_status_irqsafe(hw, skb);
1303
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001304 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001305 }
1306
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001307 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001308 ieee80211_wake_queue(hw, index);
1309}
1310
1311/* must be called only when the card's transmit is completely halted */
1312static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1313{
1314 struct mwl8k_priv *priv = hw->priv;
1315 struct mwl8k_tx_queue *txq = priv->txq + index;
1316
1317 mwl8k_txq_reclaim(hw, index, 1);
1318
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001319 kfree(txq->skb);
1320 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001321
1322 pci_free_consistent(priv->pdev,
1323 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001324 txq->txd, txq->txd_dma);
1325 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001326}
1327
1328static int
1329mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1330{
1331 struct mwl8k_priv *priv = hw->priv;
1332 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001333 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001334 struct ieee80211_hdr *wh;
1335 struct mwl8k_tx_queue *txq;
1336 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001337 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001338 u32 txstatus;
1339 u8 txdatarate;
1340 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001342 wh = (struct ieee80211_hdr *)skb->data;
1343 if (ieee80211_is_data_qos(wh->frame_control))
1344 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1345 else
1346 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001347
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001348 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001349 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001350
1351 tx_info = IEEE80211_SKB_CB(skb);
1352 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001353
1354 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1355 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001356
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001357 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1358 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1359 mwl8k_vif->seqno = seqno++ % 4096;
1360 }
1361
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001362 /* Setup firmware control bit fields for each frame type. */
1363 txstatus = 0;
1364 txdatarate = 0;
1365 if (ieee80211_is_mgmt(wh->frame_control) ||
1366 ieee80211_is_ctl(wh->frame_control)) {
1367 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001368 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001369 } else if (ieee80211_is_data(wh->frame_control)) {
1370 txdatarate = 1;
1371 if (is_multicast_ether_addr(wh->addr1))
1372 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1373
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001374 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001375 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001376 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001377 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001378 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001379 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001380
1381 dma = pci_map_single(priv->pdev, skb->data,
1382 skb->len, PCI_DMA_TODEVICE);
1383
1384 if (pci_dma_mapping_error(priv->pdev, dma)) {
1385 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001386 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001387 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001388 return NETDEV_TX_OK;
1389 }
1390
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001391 spin_lock_bh(&priv->tx_lock);
1392
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001393 txq = priv->txq + index;
1394
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001395 BUG_ON(txq->skb[txq->tail] != NULL);
1396 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001397
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001398 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001399 tx->data_rate = txdatarate;
1400 tx->tx_priority = index;
1401 tx->qos_control = cpu_to_le16(qos);
1402 tx->pkt_phys_addr = cpu_to_le32(dma);
1403 tx->pkt_len = cpu_to_le16(skb->len);
1404 tx->rate_info = 0;
1405 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001406 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001407 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1408
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001409 txq->stats.count++;
1410 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001411 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001412
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001413 txq->tail++;
1414 if (txq->tail == MWL8K_TX_DESCS)
1415 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001416
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001417 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001418 ieee80211_stop_queue(hw, index);
1419
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001420 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001421
1422 spin_unlock_bh(&priv->tx_lock);
1423
1424 return NETDEV_TX_OK;
1425}
1426
1427
1428/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001429 * Firmware access.
1430 *
1431 * We have the following requirements for issuing firmware commands:
1432 * - Some commands require that the packet transmit path is idle when
1433 * the command is issued. (For simplicity, we'll just quiesce the
1434 * transmit path for every command.)
1435 * - There are certain sequences of commands that need to be issued to
1436 * the hardware sequentially, with no other intervening commands.
1437 *
1438 * This leads to an implementation of a "firmware lock" as a mutex that
1439 * can be taken recursively, and which is taken by both the low-level
1440 * command submission function (mwl8k_post_cmd) as well as any users of
1441 * that function that require issuing of an atomic sequence of commands,
1442 * and quiesces the transmit path whenever it's taken.
1443 */
1444static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1445{
1446 struct mwl8k_priv *priv = hw->priv;
1447
1448 if (priv->fw_mutex_owner != current) {
1449 int rc;
1450
1451 mutex_lock(&priv->fw_mutex);
1452 ieee80211_stop_queues(hw);
1453
1454 rc = mwl8k_tx_wait_empty(hw);
1455 if (rc) {
1456 ieee80211_wake_queues(hw);
1457 mutex_unlock(&priv->fw_mutex);
1458
1459 return rc;
1460 }
1461
1462 priv->fw_mutex_owner = current;
1463 }
1464
1465 priv->fw_mutex_depth++;
1466
1467 return 0;
1468}
1469
1470static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1471{
1472 struct mwl8k_priv *priv = hw->priv;
1473
1474 if (!--priv->fw_mutex_depth) {
1475 ieee80211_wake_queues(hw);
1476 priv->fw_mutex_owner = NULL;
1477 mutex_unlock(&priv->fw_mutex);
1478 }
1479}
1480
1481
1482/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001483 * Command processing.
1484 */
1485
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001486/* Timeout firmware commands after 10s */
1487#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001488
1489static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1490{
1491 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1492 struct mwl8k_priv *priv = hw->priv;
1493 void __iomem *regs = priv->regs;
1494 dma_addr_t dma_addr;
1495 unsigned int dma_size;
1496 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001497 unsigned long timeout = 0;
1498 u8 buf[32];
1499
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001500 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001501 dma_size = le16_to_cpu(cmd->length);
1502 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1503 PCI_DMA_BIDIRECTIONAL);
1504 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1505 return -ENOMEM;
1506
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001507 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001508 if (rc) {
1509 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1510 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001511 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001512 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001513
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001514 priv->hostcmd_wait = &cmd_wait;
1515 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1516 iowrite32(MWL8K_H2A_INT_DOORBELL,
1517 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1518 iowrite32(MWL8K_H2A_INT_DUMMY,
1519 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001520
1521 timeout = wait_for_completion_timeout(&cmd_wait,
1522 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1523
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001524 priv->hostcmd_wait = NULL;
1525
1526 mwl8k_fw_unlock(hw);
1527
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001528 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1529 PCI_DMA_BIDIRECTIONAL);
1530
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001531 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001532 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001533 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001534 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1535 MWL8K_CMD_TIMEOUT_MS);
1536 rc = -ETIMEDOUT;
1537 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001538 int ms;
1539
1540 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1541
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001542 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001543 if (rc)
1544 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001545 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001546 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001547 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001548 else if (ms > 2000)
1549 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1550 wiphy_name(hw->wiphy),
1551 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1552 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001553 }
1554
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001555 return rc;
1556}
1557
1558/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001559 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001560 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001561struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001562 struct mwl8k_cmd_pkt header;
1563 __u8 hw_rev;
1564 __u8 host_interface;
1565 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001566 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001567 __le16 region_code;
1568 __le32 fw_rev;
1569 __le32 ps_cookie;
1570 __le32 caps;
1571 __u8 mcs_bitmap[16];
1572 __le32 rx_queue_ptr;
1573 __le32 num_tx_queues;
1574 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1575 __le32 caps2;
1576 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001577 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578} __attribute__((packed));
1579
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001580static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001581{
1582 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001583 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001584 int rc;
1585 int i;
1586
1587 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1588 if (cmd == NULL)
1589 return -ENOMEM;
1590
1591 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1592 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1593
1594 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1595 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001596 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001597 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001598 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001599 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001600 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001601 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001602
1603 rc = mwl8k_post_cmd(hw, &cmd->header);
1604
1605 if (!rc) {
1606 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1607 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001608 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001609 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001610 }
1611
1612 kfree(cmd);
1613 return rc;
1614}
1615
1616/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001617 * CMD_GET_HW_SPEC (AP version).
1618 */
1619struct mwl8k_cmd_get_hw_spec_ap {
1620 struct mwl8k_cmd_pkt header;
1621 __u8 hw_rev;
1622 __u8 host_interface;
1623 __le16 num_wcb;
1624 __le16 num_mcaddrs;
1625 __u8 perm_addr[ETH_ALEN];
1626 __le16 region_code;
1627 __le16 num_antenna;
1628 __le32 fw_rev;
1629 __le32 wcbbase0;
1630 __le32 rxwrptr;
1631 __le32 rxrdptr;
1632 __le32 ps_cookie;
1633 __le32 wcbbase1;
1634 __le32 wcbbase2;
1635 __le32 wcbbase3;
1636} __attribute__((packed));
1637
1638static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1639{
1640 struct mwl8k_priv *priv = hw->priv;
1641 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1642 int rc;
1643
1644 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1645 if (cmd == NULL)
1646 return -ENOMEM;
1647
1648 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1649 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1650
1651 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1652 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1653
1654 rc = mwl8k_post_cmd(hw, &cmd->header);
1655
1656 if (!rc) {
1657 int off;
1658
1659 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1660 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1661 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1662 priv->hw_rev = cmd->hw_rev;
1663
1664 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1665 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1666
1667 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1668 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1669
1670 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1671 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1672
1673 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1674 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1675
1676 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1677 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1678
1679 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1680 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1681 }
1682
1683 kfree(cmd);
1684 return rc;
1685}
1686
1687/*
1688 * CMD_SET_HW_SPEC.
1689 */
1690struct mwl8k_cmd_set_hw_spec {
1691 struct mwl8k_cmd_pkt header;
1692 __u8 hw_rev;
1693 __u8 host_interface;
1694 __le16 num_mcaddrs;
1695 __u8 perm_addr[ETH_ALEN];
1696 __le16 region_code;
1697 __le32 fw_rev;
1698 __le32 ps_cookie;
1699 __le32 caps;
1700 __le32 rx_queue_ptr;
1701 __le32 num_tx_queues;
1702 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1703 __le32 flags;
1704 __le32 num_tx_desc_per_queue;
1705 __le32 total_rxd;
1706} __attribute__((packed));
1707
1708#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1709
1710static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1711{
1712 struct mwl8k_priv *priv = hw->priv;
1713 struct mwl8k_cmd_set_hw_spec *cmd;
1714 int rc;
1715 int i;
1716
1717 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1718 if (cmd == NULL)
1719 return -ENOMEM;
1720
1721 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1722 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1723
1724 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1725 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1726 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1727 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1728 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1729 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1730 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1731 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1732
1733 rc = mwl8k_post_cmd(hw, &cmd->header);
1734 kfree(cmd);
1735
1736 return rc;
1737}
1738
1739/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001740 * CMD_MAC_MULTICAST_ADR.
1741 */
1742struct mwl8k_cmd_mac_multicast_adr {
1743 struct mwl8k_cmd_pkt header;
1744 __le16 action;
1745 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001746 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001747};
1748
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001749#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1750#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1751#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1752#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001753
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001754static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001755__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001756 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001757{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001758 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001759 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001760 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001761
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001762 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001763 allmulti = 1;
1764 mc_count = 0;
1765 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001766
1767 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1768
1769 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001770 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001771 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001772
1773 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1774 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001775 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1776 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001777
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001778 if (allmulti) {
1779 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1780 } else if (mc_count) {
1781 int i;
1782
1783 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1784 cmd->numaddr = cpu_to_le16(mc_count);
1785 for (i = 0; i < mc_count && mclist; i++) {
1786 if (mclist->da_addrlen != ETH_ALEN) {
1787 kfree(cmd);
1788 return NULL;
1789 }
1790 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1791 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001792 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001793 }
1794
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001795 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001796}
1797
1798/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001799 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001800 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001801struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001802 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001803 __le32 stats[64];
1804} __attribute__((packed));
1805
1806#define MWL8K_STAT_ACK_FAILURE 9
1807#define MWL8K_STAT_RTS_FAILURE 12
1808#define MWL8K_STAT_FCS_ERROR 24
1809#define MWL8K_STAT_RTS_SUCCESS 11
1810
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001811static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1812 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001813{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001814 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001815 int rc;
1816
1817 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1818 if (cmd == NULL)
1819 return -ENOMEM;
1820
1821 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1822 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001823
1824 rc = mwl8k_post_cmd(hw, &cmd->header);
1825 if (!rc) {
1826 stats->dot11ACKFailureCount =
1827 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1828 stats->dot11RTSFailureCount =
1829 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1830 stats->dot11FCSErrorCount =
1831 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1832 stats->dot11RTSSuccessCount =
1833 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1834 }
1835 kfree(cmd);
1836
1837 return rc;
1838}
1839
1840/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001841 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001842 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001843struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001844 struct mwl8k_cmd_pkt header;
1845 __le16 action;
1846 __le16 control;
1847 __le16 radio_on;
1848} __attribute__((packed));
1849
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001850static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001851mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001852{
1853 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001854 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001855 int rc;
1856
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001857 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001858 return 0;
1859
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001860 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1861 if (cmd == NULL)
1862 return -ENOMEM;
1863
1864 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1865 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1866 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001867 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001868 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1869
1870 rc = mwl8k_post_cmd(hw, &cmd->header);
1871 kfree(cmd);
1872
1873 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001874 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001875
1876 return rc;
1877}
1878
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001879static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001880{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001881 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001882}
1883
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001884static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001885{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001886 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001887}
1888
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001889static int
1890mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1891{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001892 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001893
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001894 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001895
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001896 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001897}
1898
1899/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001900 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001901 */
1902#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1903
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001904struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001905 struct mwl8k_cmd_pkt header;
1906 __le16 action;
1907 __le16 support_level;
1908 __le16 current_level;
1909 __le16 reserved;
1910 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1911} __attribute__((packed));
1912
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001913static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001914{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001915 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001916 int rc;
1917
1918 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1919 if (cmd == NULL)
1920 return -ENOMEM;
1921
1922 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1923 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1924 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1925 cmd->support_level = cpu_to_le16(dBm);
1926
1927 rc = mwl8k_post_cmd(hw, &cmd->header);
1928 kfree(cmd);
1929
1930 return rc;
1931}
1932
1933/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02001934 * CMD_RF_ANTENNA.
1935 */
1936struct mwl8k_cmd_rf_antenna {
1937 struct mwl8k_cmd_pkt header;
1938 __le16 antenna;
1939 __le16 mode;
1940} __attribute__((packed));
1941
1942#define MWL8K_RF_ANTENNA_RX 1
1943#define MWL8K_RF_ANTENNA_TX 2
1944
1945static int
1946mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
1947{
1948 struct mwl8k_cmd_rf_antenna *cmd;
1949 int rc;
1950
1951 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1952 if (cmd == NULL)
1953 return -ENOMEM;
1954
1955 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
1956 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1957 cmd->antenna = cpu_to_le16(antenna);
1958 cmd->mode = cpu_to_le16(mask);
1959
1960 rc = mwl8k_post_cmd(hw, &cmd->header);
1961 kfree(cmd);
1962
1963 return rc;
1964}
1965
1966/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001967 * CMD_SET_PRE_SCAN.
1968 */
1969struct mwl8k_cmd_set_pre_scan {
1970 struct mwl8k_cmd_pkt header;
1971} __attribute__((packed));
1972
1973static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
1974{
1975 struct mwl8k_cmd_set_pre_scan *cmd;
1976 int rc;
1977
1978 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1979 if (cmd == NULL)
1980 return -ENOMEM;
1981
1982 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
1983 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1984
1985 rc = mwl8k_post_cmd(hw, &cmd->header);
1986 kfree(cmd);
1987
1988 return rc;
1989}
1990
1991/*
1992 * CMD_SET_POST_SCAN.
1993 */
1994struct mwl8k_cmd_set_post_scan {
1995 struct mwl8k_cmd_pkt header;
1996 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001997 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001998} __attribute__((packed));
1999
2000static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002001mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002002{
2003 struct mwl8k_cmd_set_post_scan *cmd;
2004 int rc;
2005
2006 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2007 if (cmd == NULL)
2008 return -ENOMEM;
2009
2010 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2011 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2012 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002013 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002014
2015 rc = mwl8k_post_cmd(hw, &cmd->header);
2016 kfree(cmd);
2017
2018 return rc;
2019}
2020
2021/*
2022 * CMD_SET_RF_CHANNEL.
2023 */
2024struct mwl8k_cmd_set_rf_channel {
2025 struct mwl8k_cmd_pkt header;
2026 __le16 action;
2027 __u8 current_channel;
2028 __le32 channel_flags;
2029} __attribute__((packed));
2030
2031static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2032 struct ieee80211_channel *channel)
2033{
2034 struct mwl8k_cmd_set_rf_channel *cmd;
2035 int rc;
2036
2037 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2038 if (cmd == NULL)
2039 return -ENOMEM;
2040
2041 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2042 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2043 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2044 cmd->current_channel = channel->hw_value;
2045 if (channel->band == IEEE80211_BAND_2GHZ)
2046 cmd->channel_flags = cpu_to_le32(0x00000081);
2047 else
2048 cmd->channel_flags = cpu_to_le32(0x00000000);
2049
2050 rc = mwl8k_post_cmd(hw, &cmd->header);
2051 kfree(cmd);
2052
2053 return rc;
2054}
2055
2056/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002057 * CMD_SET_AID.
2058 */
2059#define MWL8K_FRAME_PROT_DISABLED 0x00
2060#define MWL8K_FRAME_PROT_11G 0x07
2061#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2062#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2063
2064struct mwl8k_cmd_update_set_aid {
2065 struct mwl8k_cmd_pkt header;
2066 __le16 aid;
2067
2068 /* AP's MAC address (BSSID) */
2069 __u8 bssid[ETH_ALEN];
2070 __le16 protection_mode;
2071 __u8 supp_rates[14];
2072} __attribute__((packed));
2073
2074static int
2075mwl8k_cmd_set_aid(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2076{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002077 struct mwl8k_cmd_update_set_aid *cmd;
2078 u16 prot_mode;
2079 int rc;
2080
2081 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2082 if (cmd == NULL)
2083 return -ENOMEM;
2084
2085 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2086 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002087 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002088
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002089 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002090
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002091 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002092 prot_mode = MWL8K_FRAME_PROT_11G;
2093 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002094 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002095 IEEE80211_HT_OP_MODE_PROTECTION) {
2096 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2097 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2098 break;
2099 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2100 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2101 break;
2102 default:
2103 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2104 break;
2105 }
2106 }
2107 cmd->protection_mode = cpu_to_le16(prot_mode);
2108
2109 memcpy(cmd->supp_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2110
2111 rc = mwl8k_post_cmd(hw, &cmd->header);
2112 kfree(cmd);
2113
2114 return rc;
2115}
2116
2117/*
2118 * CMD_SET_RATE.
2119 */
2120struct mwl8k_cmd_set_rate {
2121 struct mwl8k_cmd_pkt header;
2122 __u8 legacy_rates[14];
2123
2124 /* Bitmap for supported MCS codes. */
2125 __u8 mcs_set[16];
2126 __u8 reserved[16];
2127} __attribute__((packed));
2128
2129static int
2130mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2131{
2132 struct mwl8k_cmd_set_rate *cmd;
2133 int rc;
2134
2135 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2136 if (cmd == NULL)
2137 return -ENOMEM;
2138
2139 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2140 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2141 memcpy(cmd->legacy_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2142
2143 rc = mwl8k_post_cmd(hw, &cmd->header);
2144 kfree(cmd);
2145
2146 return rc;
2147}
2148
2149/*
2150 * CMD_FINALIZE_JOIN.
2151 */
2152#define MWL8K_FJ_BEACON_MAXLEN 128
2153
2154struct mwl8k_cmd_finalize_join {
2155 struct mwl8k_cmd_pkt header;
2156 __le32 sleep_interval; /* Number of beacon periods to sleep */
2157 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2158} __attribute__((packed));
2159
2160static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2161 int framelen, int dtim)
2162{
2163 struct mwl8k_cmd_finalize_join *cmd;
2164 struct ieee80211_mgmt *payload = frame;
2165 int payload_len;
2166 int rc;
2167
2168 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2169 if (cmd == NULL)
2170 return -ENOMEM;
2171
2172 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2173 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2174 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2175
2176 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2177 if (payload_len < 0)
2178 payload_len = 0;
2179 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2180 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2181
2182 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2183
2184 rc = mwl8k_post_cmd(hw, &cmd->header);
2185 kfree(cmd);
2186
2187 return rc;
2188}
2189
2190/*
2191 * CMD_SET_RTS_THRESHOLD.
2192 */
2193struct mwl8k_cmd_set_rts_threshold {
2194 struct mwl8k_cmd_pkt header;
2195 __le16 action;
2196 __le16 threshold;
2197} __attribute__((packed));
2198
2199static int mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw,
2200 u16 action, u16 threshold)
2201{
2202 struct mwl8k_cmd_set_rts_threshold *cmd;
2203 int rc;
2204
2205 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2206 if (cmd == NULL)
2207 return -ENOMEM;
2208
2209 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2210 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2211 cmd->action = cpu_to_le16(action);
2212 cmd->threshold = cpu_to_le16(threshold);
2213
2214 rc = mwl8k_post_cmd(hw, &cmd->header);
2215 kfree(cmd);
2216
2217 return rc;
2218}
2219
2220/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002221 * CMD_SET_SLOT.
2222 */
2223struct mwl8k_cmd_set_slot {
2224 struct mwl8k_cmd_pkt header;
2225 __le16 action;
2226 __u8 short_slot;
2227} __attribute__((packed));
2228
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002229static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002230{
2231 struct mwl8k_cmd_set_slot *cmd;
2232 int rc;
2233
2234 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2235 if (cmd == NULL)
2236 return -ENOMEM;
2237
2238 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2239 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2240 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002241 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002242
2243 rc = mwl8k_post_cmd(hw, &cmd->header);
2244 kfree(cmd);
2245
2246 return rc;
2247}
2248
2249/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002250 * CMD_SET_EDCA_PARAMS.
2251 */
2252struct mwl8k_cmd_set_edca_params {
2253 struct mwl8k_cmd_pkt header;
2254
2255 /* See MWL8K_SET_EDCA_XXX below */
2256 __le16 action;
2257
2258 /* TX opportunity in units of 32 us */
2259 __le16 txop;
2260
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002261 union {
2262 struct {
2263 /* Log exponent of max contention period: 0...15 */
2264 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002265
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002266 /* Log exponent of min contention period: 0...15 */
2267 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002268
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002269 /* Adaptive interframe spacing in units of 32us */
2270 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002271
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002272 /* TX queue to configure */
2273 __u8 txq;
2274 } ap;
2275 struct {
2276 /* Log exponent of max contention period: 0...15 */
2277 __u8 log_cw_max;
2278
2279 /* Log exponent of min contention period: 0...15 */
2280 __u8 log_cw_min;
2281
2282 /* Adaptive interframe spacing in units of 32us */
2283 __u8 aifs;
2284
2285 /* TX queue to configure */
2286 __u8 txq;
2287 } sta;
2288 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002289} __attribute__((packed));
2290
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002291#define MWL8K_SET_EDCA_CW 0x01
2292#define MWL8K_SET_EDCA_TXOP 0x02
2293#define MWL8K_SET_EDCA_AIFS 0x04
2294
2295#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2296 MWL8K_SET_EDCA_TXOP | \
2297 MWL8K_SET_EDCA_AIFS)
2298
2299static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002300mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2301 __u16 cw_min, __u16 cw_max,
2302 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002303{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002304 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002305 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002306 int rc;
2307
2308 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2309 if (cmd == NULL)
2310 return -ENOMEM;
2311
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002312 /*
2313 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2314 * this call.
2315 */
2316 qnum ^= !(qnum >> 1);
2317
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002318 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2319 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002320 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2321 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002322 if (priv->ap_fw) {
2323 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2324 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2325 cmd->ap.aifs = aifs;
2326 cmd->ap.txq = qnum;
2327 } else {
2328 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2329 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2330 cmd->sta.aifs = aifs;
2331 cmd->sta.txq = qnum;
2332 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002333
2334 rc = mwl8k_post_cmd(hw, &cmd->header);
2335 kfree(cmd);
2336
2337 return rc;
2338}
2339
2340/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002341 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002342 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002343struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002344 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002345 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002346} __attribute__((packed));
2347
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002348static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002349{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002350 struct mwl8k_priv *priv = hw->priv;
2351 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002352 int rc;
2353
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002354 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2355 if (cmd == NULL)
2356 return -ENOMEM;
2357
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002358 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002359 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002360 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002361
2362 rc = mwl8k_post_cmd(hw, &cmd->header);
2363 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002364
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002365 if (!rc)
2366 priv->wmm_enabled = enable;
2367
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002368 return rc;
2369}
2370
2371/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002372 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002373 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002374struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002375 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002376 __le32 action;
2377 __u8 rx_antenna_map;
2378 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002379} __attribute__((packed));
2380
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002381static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002382{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002383 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002384 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002385
2386 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2387 if (cmd == NULL)
2388 return -ENOMEM;
2389
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002390 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002391 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002392 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2393 cmd->rx_antenna_map = rx;
2394 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002395
2396 rc = mwl8k_post_cmd(hw, &cmd->header);
2397 kfree(cmd);
2398
2399 return rc;
2400}
2401
2402/*
2403 * CMD_USE_FIXED_RATE.
2404 */
2405#define MWL8K_RATE_TABLE_SIZE 8
2406#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002407#define MWL8K_USE_AUTO_RATE 0x0002
2408
2409struct mwl8k_rate_entry {
2410 /* Set to 1 if HT rate, 0 if legacy. */
2411 __le32 is_ht_rate;
2412
2413 /* Set to 1 to use retry_count field. */
2414 __le32 enable_retry;
2415
2416 /* Specified legacy rate or MCS. */
2417 __le32 rate;
2418
2419 /* Number of allowed retries. */
2420 __le32 retry_count;
2421} __attribute__((packed));
2422
2423struct mwl8k_rate_table {
2424 /* 1 to allow specified rate and below */
2425 __le32 allow_rate_drop;
2426 __le32 num_rates;
2427 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2428} __attribute__((packed));
2429
2430struct mwl8k_cmd_use_fixed_rate {
2431 struct mwl8k_cmd_pkt header;
2432 __le32 action;
2433 struct mwl8k_rate_table rate_table;
2434
2435 /* Unicast, Broadcast or Multicast */
2436 __le32 rate_type;
2437 __le32 reserved1;
2438 __le32 reserved2;
2439} __attribute__((packed));
2440
2441static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2442 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2443{
2444 struct mwl8k_cmd_use_fixed_rate *cmd;
2445 int count;
2446 int rc;
2447
2448 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2449 if (cmd == NULL)
2450 return -ENOMEM;
2451
2452 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2453 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2454
2455 cmd->action = cpu_to_le32(action);
2456 cmd->rate_type = cpu_to_le32(rate_type);
2457
2458 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002459 /*
2460 * Copy over each field manually so that endian
2461 * conversion can be done.
2462 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002463 cmd->rate_table.allow_rate_drop =
2464 cpu_to_le32(rate_table->allow_rate_drop);
2465 cmd->rate_table.num_rates =
2466 cpu_to_le32(rate_table->num_rates);
2467
2468 for (count = 0; count < rate_table->num_rates; count++) {
2469 struct mwl8k_rate_entry *dst =
2470 &cmd->rate_table.rate_entry[count];
2471 struct mwl8k_rate_entry *src =
2472 &rate_table->rate_entry[count];
2473
2474 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2475 dst->enable_retry = cpu_to_le32(src->enable_retry);
2476 dst->rate = cpu_to_le32(src->rate);
2477 dst->retry_count = cpu_to_le32(src->retry_count);
2478 }
2479 }
2480
2481 rc = mwl8k_post_cmd(hw, &cmd->header);
2482 kfree(cmd);
2483
2484 return rc;
2485}
2486
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002487/*
2488 * CMD_ENABLE_SNIFFER.
2489 */
2490struct mwl8k_cmd_enable_sniffer {
2491 struct mwl8k_cmd_pkt header;
2492 __le32 action;
2493} __attribute__((packed));
2494
2495static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2496{
2497 struct mwl8k_cmd_enable_sniffer *cmd;
2498 int rc;
2499
2500 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2501 if (cmd == NULL)
2502 return -ENOMEM;
2503
2504 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2505 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2506 cmd->action = cpu_to_le32(!!enable);
2507
2508 rc = mwl8k_post_cmd(hw, &cmd->header);
2509 kfree(cmd);
2510
2511 return rc;
2512}
2513
2514/*
2515 * CMD_SET_MAC_ADDR.
2516 */
2517struct mwl8k_cmd_set_mac_addr {
2518 struct mwl8k_cmd_pkt header;
2519 union {
2520 struct {
2521 __le16 mac_type;
2522 __u8 mac_addr[ETH_ALEN];
2523 } mbss;
2524 __u8 mac_addr[ETH_ALEN];
2525 };
2526} __attribute__((packed));
2527
2528static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2529{
2530 struct mwl8k_priv *priv = hw->priv;
2531 struct mwl8k_cmd_set_mac_addr *cmd;
2532 int rc;
2533
2534 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2535 if (cmd == NULL)
2536 return -ENOMEM;
2537
2538 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2539 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2540 if (priv->ap_fw) {
2541 cmd->mbss.mac_type = 0;
2542 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2543 } else {
2544 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2545 }
2546
2547 rc = mwl8k_post_cmd(hw, &cmd->header);
2548 kfree(cmd);
2549
2550 return rc;
2551}
2552
2553/*
2554 * CMD_SET_RATEADAPT_MODE.
2555 */
2556struct mwl8k_cmd_set_rate_adapt_mode {
2557 struct mwl8k_cmd_pkt header;
2558 __le16 action;
2559 __le16 mode;
2560} __attribute__((packed));
2561
2562static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2563{
2564 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2565 int rc;
2566
2567 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2568 if (cmd == NULL)
2569 return -ENOMEM;
2570
2571 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2572 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2573 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2574 cmd->mode = cpu_to_le16(mode);
2575
2576 rc = mwl8k_post_cmd(hw, &cmd->header);
2577 kfree(cmd);
2578
2579 return rc;
2580}
2581
2582/*
2583 * CMD_UPDATE_STADB.
2584 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002585#define MWL8K_STA_DB_ADD_ENTRY 0
2586#define MWL8K_STA_DB_MODIFY_ENTRY 1
2587#define MWL8K_STA_DB_DEL_ENTRY 2
2588#define MWL8K_STA_DB_FLUSH 3
2589
2590/* Peer Entry flags - used to define the type of the peer node */
2591#define MWL8K_PEER_TYPE_ACCESSPOINT 2
2592
2593struct ewc_ht_info {
2594 __le16 control1;
2595 __le16 control2;
2596 __le16 control3;
2597} __attribute__((packed));
2598
2599struct peer_capability_info {
2600 /* Peer type - AP vs. STA. */
2601 __u8 peer_type;
2602
2603 /* Basic 802.11 capabilities from assoc resp. */
2604 __le16 basic_caps;
2605
2606 /* Set if peer supports 802.11n high throughput (HT). */
2607 __u8 ht_support;
2608
2609 /* Valid if HT is supported. */
2610 __le16 ht_caps;
2611 __u8 extended_ht_caps;
2612 struct ewc_ht_info ewc_info;
2613
2614 /* Legacy rate table. Intersection of our rates and peer rates. */
2615 __u8 legacy_rates[12];
2616
2617 /* HT rate table. Intersection of our rates and peer rates. */
2618 __u8 ht_rates[16];
2619 __u8 pad[16];
2620
2621 /* If set, interoperability mode, no proprietary extensions. */
2622 __u8 interop;
2623 __u8 pad2;
2624 __u8 station_id;
2625 __le16 amsdu_enabled;
2626} __attribute__((packed));
2627
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002628struct mwl8k_cmd_update_stadb {
2629 struct mwl8k_cmd_pkt header;
2630
2631 /* See STADB_ACTION_TYPE */
2632 __le32 action;
2633
2634 /* Peer MAC address */
2635 __u8 peer_addr[ETH_ALEN];
2636
2637 __le32 reserved;
2638
2639 /* Peer info - valid during add/update. */
2640 struct peer_capability_info peer_info;
2641} __attribute__((packed));
2642
2643static int mwl8k_cmd_update_stadb(struct ieee80211_hw *hw,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01002644 struct ieee80211_vif *vif, __u32 action, u8 *addr)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002645{
2646 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002647 struct mwl8k_cmd_update_stadb *cmd;
2648 struct peer_capability_info *peer_info;
2649 int rc;
2650
2651 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2652 if (cmd == NULL)
2653 return -ENOMEM;
2654
2655 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2656 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2657
2658 cmd->action = cpu_to_le32(action);
2659 peer_info = &cmd->peer_info;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01002660 memcpy(cmd->peer_addr, addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002661
2662 switch (action) {
2663 case MWL8K_STA_DB_ADD_ENTRY:
2664 case MWL8K_STA_DB_MODIFY_ENTRY:
2665 /* Build peer_info block */
2666 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002667 peer_info->basic_caps =
2668 cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002669 memcpy(peer_info->legacy_rates, mwl8k_rateids,
2670 sizeof(mwl8k_rateids));
2671 peer_info->interop = 1;
2672 peer_info->amsdu_enabled = 0;
2673
2674 rc = mwl8k_post_cmd(hw, &cmd->header);
2675 if (rc == 0)
2676 mv_vif->peer_id = peer_info->station_id;
2677
2678 break;
2679
2680 case MWL8K_STA_DB_DEL_ENTRY:
2681 case MWL8K_STA_DB_FLUSH:
2682 default:
2683 rc = mwl8k_post_cmd(hw, &cmd->header);
2684 if (rc == 0)
2685 mv_vif->peer_id = 0;
2686 break;
2687 }
2688 kfree(cmd);
2689
2690 return rc;
2691}
2692
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002693
2694/*
2695 * Interrupt handling.
2696 */
2697static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2698{
2699 struct ieee80211_hw *hw = dev_id;
2700 struct mwl8k_priv *priv = hw->priv;
2701 u32 status;
2702
2703 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2704 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2705
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002706 if (!status)
2707 return IRQ_NONE;
2708
2709 if (status & MWL8K_A2H_INT_TX_DONE)
2710 tasklet_schedule(&priv->tx_reclaim_task);
2711
2712 if (status & MWL8K_A2H_INT_RX_READY) {
2713 while (rxq_process(hw, 0, 1))
2714 rxq_refill(hw, 0, 1);
2715 }
2716
2717 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002718 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002719 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002720 }
2721
2722 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002723 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002724 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002725 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002726 }
2727
2728 return IRQ_HANDLED;
2729}
2730
2731
2732/*
2733 * Core driver operations.
2734 */
2735static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2736{
2737 struct mwl8k_priv *priv = hw->priv;
2738 int index = skb_get_queue_mapping(skb);
2739 int rc;
2740
2741 if (priv->current_channel == NULL) {
2742 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002743 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002744 dev_kfree_skb(skb);
2745 return NETDEV_TX_OK;
2746 }
2747
2748 rc = mwl8k_txq_xmit(hw, index, skb);
2749
2750 return rc;
2751}
2752
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002753static int mwl8k_start(struct ieee80211_hw *hw)
2754{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002755 struct mwl8k_priv *priv = hw->priv;
2756 int rc;
2757
Joe Perchesa0607fd2009-11-18 23:29:17 -08002758 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002759 IRQF_SHARED, MWL8K_NAME, hw);
2760 if (rc) {
2761 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002762 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002763 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002764 }
2765
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002766 /* Enable tx reclaim tasklet */
2767 tasklet_enable(&priv->tx_reclaim_task);
2768
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002769 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002770 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002771
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002772 rc = mwl8k_fw_lock(hw);
2773 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002774 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002775
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002776 if (!priv->ap_fw) {
2777 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002778 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002779
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002780 if (!rc)
2781 rc = mwl8k_cmd_set_pre_scan(hw);
2782
2783 if (!rc)
2784 rc = mwl8k_cmd_set_post_scan(hw,
2785 "\x00\x00\x00\x00\x00\x00");
2786 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002787
2788 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002789 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002790
2791 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002792 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002793
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002794 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002795 }
2796
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002797 if (rc) {
2798 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2799 free_irq(priv->pdev->irq, hw);
2800 tasklet_disable(&priv->tx_reclaim_task);
2801 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002802
2803 return rc;
2804}
2805
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002806static void mwl8k_stop(struct ieee80211_hw *hw)
2807{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002808 struct mwl8k_priv *priv = hw->priv;
2809 int i;
2810
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002811 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002812
2813 ieee80211_stop_queues(hw);
2814
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002815 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002816 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002817 free_irq(priv->pdev->irq, hw);
2818
2819 /* Stop finalize join worker */
2820 cancel_work_sync(&priv->finalize_join_worker);
2821 if (priv->beacon_skb != NULL)
2822 dev_kfree_skb(priv->beacon_skb);
2823
2824 /* Stop tx reclaim tasklet */
2825 tasklet_disable(&priv->tx_reclaim_task);
2826
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002827 /* Return all skbs to mac80211 */
2828 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2829 mwl8k_txq_reclaim(hw, i, 1);
2830}
2831
2832static int mwl8k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002833 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002834{
2835 struct mwl8k_priv *priv = hw->priv;
2836 struct mwl8k_vif *mwl8k_vif;
2837
2838 /*
2839 * We only support one active interface at a time.
2840 */
2841 if (priv->vif != NULL)
2842 return -EBUSY;
2843
2844 /*
2845 * We only support managed interfaces for now.
2846 */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002847 if (vif->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002848 return -EINVAL;
2849
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002850 /*
2851 * Reject interface creation if sniffer mode is active, as
2852 * STA operation is mutually exclusive with hardware sniffer
2853 * mode.
2854 */
2855 if (priv->sniffer_enabled) {
2856 printk(KERN_INFO "%s: unable to create STA "
2857 "interface due to sniffer mode being enabled\n",
2858 wiphy_name(hw->wiphy));
2859 return -EINVAL;
2860 }
2861
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002862 /* Clean out driver private area */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002863 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002864 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2865
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002866 /* Set and save the mac address */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002867 mwl8k_cmd_set_mac_addr(hw, vif->addr);
2868 memcpy(mwl8k_vif->mac_addr, vif->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002869
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002870 /* Set Initial sequence number to zero */
2871 mwl8k_vif->seqno = 0;
2872
Johannes Berg1ed32e42009-12-23 13:15:45 +01002873 priv->vif = vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002874 priv->current_channel = NULL;
2875
2876 return 0;
2877}
2878
2879static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002880 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002881{
2882 struct mwl8k_priv *priv = hw->priv;
2883
2884 if (priv->vif == NULL)
2885 return;
2886
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002887 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002888
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002889 priv->vif = NULL;
2890}
2891
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002892static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002893{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002894 struct ieee80211_conf *conf = &hw->conf;
2895 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002896 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002897
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002898 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002899 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002900 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002901 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002902 }
2903
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002904 rc = mwl8k_fw_lock(hw);
2905 if (rc)
2906 return rc;
2907
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002908 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002909 if (rc)
2910 goto out;
2911
2912 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2913 if (rc)
2914 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002915
2916 priv->current_channel = conf->channel;
2917
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002918 if (conf->power_level > 18)
2919 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002920 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002921 if (rc)
2922 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002923
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002924 if (priv->ap_fw) {
2925 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2926 if (!rc)
2927 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2928 } else {
2929 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2930 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002931
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002932out:
2933 mwl8k_fw_unlock(hw);
2934
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002935 return rc;
2936}
2937
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002938static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2939 struct ieee80211_vif *vif,
2940 struct ieee80211_bss_conf *info,
2941 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002942{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002943 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002944 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002945
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002946 if ((changed & BSS_CHANGED_ASSOC) == 0)
2947 return;
2948
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002949 priv->capture_beacon = false;
2950
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002951 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002952 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002953 return;
2954
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002955 if (vif->bss_conf.assoc) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002956 /* Install rates */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002957 rc = mwl8k_cmd_set_rate(hw, vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002958 if (rc)
2959 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002960
2961 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002962 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
2963 MWL8K_UCAST_RATE, NULL);
2964 if (rc)
2965 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002966
2967 /* Set radio preamble */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002968 rc = mwl8k_set_radio_preamble(hw,
2969 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002970 if (rc)
2971 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002972
2973 /* Set slot time */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002974 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002975 if (rc)
2976 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002977
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002978 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002979 rc = mwl8k_cmd_set_aid(hw, vif);
2980 if (rc)
2981 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002982
2983 /*
2984 * Finalize the join. Tell rx handler to process
2985 * next beacon from our BSSID.
2986 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002987 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002988 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002989 }
2990
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002991out:
2992 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002993}
2994
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002995static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
2996 int mc_count, struct dev_addr_list *mclist)
2997{
2998 struct mwl8k_cmd_pkt *cmd;
2999
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003000 /*
3001 * Synthesize and return a command packet that programs the
3002 * hardware multicast address filter. At this point we don't
3003 * know whether FIF_ALLMULTI is being requested, but if it is,
3004 * we'll end up throwing this packet away and creating a new
3005 * one in mwl8k_configure_filter().
3006 */
3007 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003008
3009 return (unsigned long)cmd;
3010}
3011
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003012static int
3013mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3014 unsigned int changed_flags,
3015 unsigned int *total_flags)
3016{
3017 struct mwl8k_priv *priv = hw->priv;
3018
3019 /*
3020 * Hardware sniffer mode is mutually exclusive with STA
3021 * operation, so refuse to enable sniffer mode if a STA
3022 * interface is active.
3023 */
3024 if (priv->vif != NULL) {
3025 if (net_ratelimit())
3026 printk(KERN_INFO "%s: not enabling sniffer "
3027 "mode because STA interface is active\n",
3028 wiphy_name(hw->wiphy));
3029 return 0;
3030 }
3031
3032 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003033 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003034 return 0;
3035 priv->sniffer_enabled = true;
3036 }
3037
3038 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3039 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3040 FIF_OTHER_BSS;
3041
3042 return 1;
3043}
3044
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003045static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3046 unsigned int changed_flags,
3047 unsigned int *total_flags,
3048 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003049{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003050 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003051 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3052
3053 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003054 * AP firmware doesn't allow fine-grained control over
3055 * the receive filter.
3056 */
3057 if (priv->ap_fw) {
3058 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3059 kfree(cmd);
3060 return;
3061 }
3062
3063 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003064 * Enable hardware sniffer mode if FIF_CONTROL or
3065 * FIF_OTHER_BSS is requested.
3066 */
3067 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3068 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3069 kfree(cmd);
3070 return;
3071 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003072
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003073 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003074 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003075
3076 if (mwl8k_fw_lock(hw))
3077 return;
3078
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003079 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003080 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003081 priv->sniffer_enabled = false;
3082 }
3083
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003084 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003085 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3086 /*
3087 * Disable the BSS filter.
3088 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003089 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003090 } else {
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003091 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003092
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003093 /*
3094 * Enable the BSS filter.
3095 *
3096 * If there is an active STA interface, use that
3097 * interface's BSSID, otherwise use a dummy one
3098 * (where the OUI part needs to be nonzero for
3099 * the BSSID to be accepted by POST_SCAN).
3100 */
3101 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003102 if (priv->vif != NULL)
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003103 bssid = priv->vif->bss_conf.bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003104
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003105 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003106 }
3107 }
3108
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003109 /*
3110 * If FIF_ALLMULTI is being requested, throw away the command
3111 * packet that ->prepare_multicast() built and replace it with
3112 * a command packet that enables reception of all multicast
3113 * packets.
3114 */
3115 if (*total_flags & FIF_ALLMULTI) {
3116 kfree(cmd);
3117 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3118 }
3119
3120 if (cmd != NULL) {
3121 mwl8k_post_cmd(hw, cmd);
3122 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003123 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003124
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003125 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003126}
3127
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003128static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3129{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003130 return mwl8k_cmd_set_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003131}
3132
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003133struct mwl8k_sta_notify_item
3134{
3135 struct list_head list;
3136 struct ieee80211_vif *vif;
3137 enum sta_notify_cmd cmd;
3138 u8 addr[ETH_ALEN];
3139};
3140
3141static void mwl8k_sta_notify_worker(struct work_struct *work)
3142{
3143 struct mwl8k_priv *priv =
3144 container_of(work, struct mwl8k_priv, sta_notify_worker);
3145
3146 spin_lock_bh(&priv->sta_notify_list_lock);
3147 while (!list_empty(&priv->sta_notify_list)) {
3148 struct mwl8k_sta_notify_item *s;
3149 int action;
3150
3151 s = list_entry(priv->sta_notify_list.next,
3152 struct mwl8k_sta_notify_item, list);
3153 list_del(&s->list);
3154
3155 spin_unlock_bh(&priv->sta_notify_list_lock);
3156
3157 if (s->cmd == STA_NOTIFY_ADD)
3158 action = MWL8K_STA_DB_MODIFY_ENTRY;
3159 else
3160 action = MWL8K_STA_DB_DEL_ENTRY;
3161 mwl8k_cmd_update_stadb(priv->hw, s->vif, action, s->addr);
3162
3163 kfree(s);
3164
3165 spin_lock_bh(&priv->sta_notify_list_lock);
3166 }
3167 spin_unlock_bh(&priv->sta_notify_list_lock);
3168}
3169
3170static void
3171mwl8k_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3172 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3173{
3174 struct mwl8k_priv *priv = hw->priv;
3175 struct mwl8k_sta_notify_item *s;
3176
3177 if (cmd != STA_NOTIFY_ADD && cmd != STA_NOTIFY_REMOVE)
3178 return;
3179
3180 s = kmalloc(sizeof(*s), GFP_ATOMIC);
3181 if (s != NULL) {
3182 s->vif = vif;
3183 s->cmd = cmd;
3184 memcpy(s->addr, sta->addr, ETH_ALEN);
3185
3186 spin_lock(&priv->sta_notify_list_lock);
3187 list_add_tail(&s->list, &priv->sta_notify_list);
3188 spin_unlock(&priv->sta_notify_list_lock);
3189
3190 ieee80211_queue_work(hw, &priv->sta_notify_worker);
3191 }
3192}
3193
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003194static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3195 const struct ieee80211_tx_queue_params *params)
3196{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003197 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003198 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003199
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003200 rc = mwl8k_fw_lock(hw);
3201 if (!rc) {
3202 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003203 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003204
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003205 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003206 rc = mwl8k_cmd_set_edca_params(hw, queue,
3207 params->cw_min,
3208 params->cw_max,
3209 params->aifs,
3210 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003211
3212 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003213 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003214
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003215 return rc;
3216}
3217
3218static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3219 struct ieee80211_tx_queue_stats *stats)
3220{
3221 struct mwl8k_priv *priv = hw->priv;
3222 struct mwl8k_tx_queue *txq;
3223 int index;
3224
3225 spin_lock_bh(&priv->tx_lock);
3226 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3227 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003228 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003229 sizeof(struct ieee80211_tx_queue_stats));
3230 }
3231 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003232
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003233 return 0;
3234}
3235
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003236static int mwl8k_get_stats(struct ieee80211_hw *hw,
3237 struct ieee80211_low_level_stats *stats)
3238{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003239 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003240}
3241
3242static const struct ieee80211_ops mwl8k_ops = {
3243 .tx = mwl8k_tx,
3244 .start = mwl8k_start,
3245 .stop = mwl8k_stop,
3246 .add_interface = mwl8k_add_interface,
3247 .remove_interface = mwl8k_remove_interface,
3248 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003249 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003250 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003251 .configure_filter = mwl8k_configure_filter,
3252 .set_rts_threshold = mwl8k_set_rts_threshold,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003253 .sta_notify = mwl8k_sta_notify,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003254 .conf_tx = mwl8k_conf_tx,
3255 .get_tx_stats = mwl8k_get_tx_stats,
3256 .get_stats = mwl8k_get_stats,
3257};
3258
3259static void mwl8k_tx_reclaim_handler(unsigned long data)
3260{
3261 int i;
3262 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3263 struct mwl8k_priv *priv = hw->priv;
3264
3265 spin_lock_bh(&priv->tx_lock);
3266 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3267 mwl8k_txq_reclaim(hw, i, 0);
3268
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003269 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003270 complete(priv->tx_wait);
3271 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003272 }
3273 spin_unlock_bh(&priv->tx_lock);
3274}
3275
3276static void mwl8k_finalize_join_worker(struct work_struct *work)
3277{
3278 struct mwl8k_priv *priv =
3279 container_of(work, struct mwl8k_priv, finalize_join_worker);
3280 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003281
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003282 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len,
3283 priv->vif->bss_conf.dtim_period);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003284 dev_kfree_skb(skb);
3285
3286 priv->beacon_skb = NULL;
3287}
3288
John W. Linvillebcb628d2009-11-06 16:40:16 -05003289enum {
3290 MWL8687 = 0,
3291 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003292};
3293
John W. Linvillebcb628d2009-11-06 16:40:16 -05003294static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003295 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003296 .part_name = "88w8687",
3297 .helper_image = "mwl8k/helper_8687.fw",
3298 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003299 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003300 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003301 .part_name = "88w8366",
3302 .helper_image = "mwl8k/helper_8366.fw",
3303 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003304 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003305 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003306};
3307
3308static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003309 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3310 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3311 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3312 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003313};
3314MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3315
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003316static int __devinit mwl8k_probe(struct pci_dev *pdev,
3317 const struct pci_device_id *id)
3318{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003319 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003320 struct ieee80211_hw *hw;
3321 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003322 int rc;
3323 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003324
3325 if (!printed_version) {
3326 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3327 printed_version = 1;
3328 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003329
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003330
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003331 rc = pci_enable_device(pdev);
3332 if (rc) {
3333 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3334 MWL8K_NAME);
3335 return rc;
3336 }
3337
3338 rc = pci_request_regions(pdev, MWL8K_NAME);
3339 if (rc) {
3340 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3341 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003342 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003343 }
3344
3345 pci_set_master(pdev);
3346
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003347
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003348 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3349 if (hw == NULL) {
3350 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3351 rc = -ENOMEM;
3352 goto err_free_reg;
3353 }
3354
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003355 SET_IEEE80211_DEV(hw, &pdev->dev);
3356 pci_set_drvdata(pdev, hw);
3357
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003358 priv = hw->priv;
3359 priv->hw = hw;
3360 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003361 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003362
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003363
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003364 priv->sram = pci_iomap(pdev, 0, 0x10000);
3365 if (priv->sram == NULL) {
3366 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003367 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003368 goto err_iounmap;
3369 }
3370
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003371 /*
3372 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3373 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3374 */
3375 priv->regs = pci_iomap(pdev, 1, 0x10000);
3376 if (priv->regs == NULL) {
3377 priv->regs = pci_iomap(pdev, 2, 0x10000);
3378 if (priv->regs == NULL) {
3379 printk(KERN_ERR "%s: Cannot map device registers\n",
3380 wiphy_name(hw->wiphy));
3381 goto err_iounmap;
3382 }
3383 }
3384
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003385
3386 /* Reset firmware and hardware */
3387 mwl8k_hw_reset(priv);
3388
3389 /* Ask userland hotplug daemon for the device firmware */
3390 rc = mwl8k_request_firmware(priv);
3391 if (rc) {
3392 printk(KERN_ERR "%s: Firmware files not found\n",
3393 wiphy_name(hw->wiphy));
3394 goto err_stop_firmware;
3395 }
3396
3397 /* Load firmware into hardware */
3398 rc = mwl8k_load_firmware(hw);
3399 if (rc) {
3400 printk(KERN_ERR "%s: Cannot start firmware\n",
3401 wiphy_name(hw->wiphy));
3402 goto err_stop_firmware;
3403 }
3404
3405 /* Reclaim memory once firmware is successfully loaded */
3406 mwl8k_release_firmware(priv);
3407
3408
Lennert Buytenhek91942232010-01-04 21:53:54 +01003409 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003410 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003411 if (priv->rxd_ops == NULL) {
3412 printk(KERN_ERR "%s: Driver does not have AP "
3413 "firmware image support for this hardware\n",
3414 wiphy_name(hw->wiphy));
3415 goto err_stop_firmware;
3416 }
3417 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003418 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003419 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003420
3421 priv->sniffer_enabled = false;
3422 priv->wmm_enabled = false;
3423 priv->pending_tx_pkts = 0;
3424
3425
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003426 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3427 priv->band.band = IEEE80211_BAND_2GHZ;
3428 priv->band.channels = priv->channels;
3429 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3430 priv->band.bitrates = priv->rates;
3431 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3432 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3433
3434 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3435 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3436
3437 /*
3438 * Extra headroom is the size of the required DMA header
3439 * minus the size of the smallest 802.11 frame (CTS frame).
3440 */
3441 hw->extra_tx_headroom =
3442 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3443
3444 hw->channel_change_time = 10;
3445
3446 hw->queues = MWL8K_TX_QUEUES;
3447
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003448 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003449 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003450 hw->vif_data_size = sizeof(struct mwl8k_vif);
3451 priv->vif = NULL;
3452
3453 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003454 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003455 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003456
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003457 /* Station database handling */
3458 INIT_WORK(&priv->sta_notify_worker, mwl8k_sta_notify_worker);
3459 spin_lock_init(&priv->sta_notify_list_lock);
3460 INIT_LIST_HEAD(&priv->sta_notify_list);
3461
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003462 /* Finalize join worker */
3463 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3464
3465 /* TX reclaim tasklet */
3466 tasklet_init(&priv->tx_reclaim_task,
3467 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3468 tasklet_disable(&priv->tx_reclaim_task);
3469
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003470 /* Power management cookie */
3471 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3472 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003473 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003474
3475 rc = mwl8k_rxq_init(hw, 0);
3476 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003477 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003478 rxq_refill(hw, 0, INT_MAX);
3479
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003480 mutex_init(&priv->fw_mutex);
3481 priv->fw_mutex_owner = NULL;
3482 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003483 priv->hostcmd_wait = NULL;
3484
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003485 spin_lock_init(&priv->tx_lock);
3486
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003487 priv->tx_wait = NULL;
3488
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003489 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3490 rc = mwl8k_txq_init(hw, i);
3491 if (rc)
3492 goto err_free_queues;
3493 }
3494
3495 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003496 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003497 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3498 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3499
Joe Perchesa0607fd2009-11-18 23:29:17 -08003500 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003501 IRQF_SHARED, MWL8K_NAME, hw);
3502 if (rc) {
3503 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003504 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003505 goto err_free_queues;
3506 }
3507
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003508 /*
3509 * Temporarily enable interrupts. Initial firmware host
3510 * commands use interrupts and avoids polling. Disable
3511 * interrupts when done.
3512 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003513 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003514
3515 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003516 if (priv->ap_fw) {
3517 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3518 if (!rc)
3519 rc = mwl8k_cmd_set_hw_spec(hw);
3520 } else {
3521 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003522
3523 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003524 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003525 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003526 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3527 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003528 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003529 }
3530
3531 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003532 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003533 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003534 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003535 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003536 }
3537
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003538 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003539 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003540 if (rc) {
3541 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3542 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003543 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003544 }
3545
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003546 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003547 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003548 free_irq(priv->pdev->irq, hw);
3549
3550 rc = ieee80211_register_hw(hw);
3551 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003552 printk(KERN_ERR "%s: Cannot register device\n",
3553 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01003554 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003555 }
3556
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003557 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003558 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003559 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003560 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003561 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3562 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003563
3564 return 0;
3565
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003566err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003567 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003568 free_irq(priv->pdev->irq, hw);
3569
3570err_free_queues:
3571 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3572 mwl8k_txq_deinit(hw, i);
3573 mwl8k_rxq_deinit(hw, 0);
3574
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003575err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003576 if (priv->cookie != NULL)
3577 pci_free_consistent(priv->pdev, 4,
3578 priv->cookie, priv->cookie_dma);
3579
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003580err_stop_firmware:
3581 mwl8k_hw_reset(priv);
3582 mwl8k_release_firmware(priv);
3583
3584err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003585 if (priv->regs != NULL)
3586 pci_iounmap(pdev, priv->regs);
3587
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003588 if (priv->sram != NULL)
3589 pci_iounmap(pdev, priv->sram);
3590
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003591 pci_set_drvdata(pdev, NULL);
3592 ieee80211_free_hw(hw);
3593
3594err_free_reg:
3595 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003596
3597err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003598 pci_disable_device(pdev);
3599
3600 return rc;
3601}
3602
Joerg Albert230f7af2009-04-18 02:10:45 +02003603static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003604{
3605 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3606}
3607
Joerg Albert230f7af2009-04-18 02:10:45 +02003608static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003609{
3610 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3611 struct mwl8k_priv *priv;
3612 int i;
3613
3614 if (hw == NULL)
3615 return;
3616 priv = hw->priv;
3617
3618 ieee80211_stop_queues(hw);
3619
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003620 ieee80211_unregister_hw(hw);
3621
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003622 /* Remove tx reclaim tasklet */
3623 tasklet_kill(&priv->tx_reclaim_task);
3624
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003625 /* Stop hardware */
3626 mwl8k_hw_reset(priv);
3627
3628 /* Return all skbs to mac80211 */
3629 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3630 mwl8k_txq_reclaim(hw, i, 1);
3631
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003632 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3633 mwl8k_txq_deinit(hw, i);
3634
3635 mwl8k_rxq_deinit(hw, 0);
3636
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003637 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003638
3639 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003640 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003641 pci_set_drvdata(pdev, NULL);
3642 ieee80211_free_hw(hw);
3643 pci_release_regions(pdev);
3644 pci_disable_device(pdev);
3645}
3646
3647static struct pci_driver mwl8k_driver = {
3648 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003649 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003650 .probe = mwl8k_probe,
3651 .remove = __devexit_p(mwl8k_remove),
3652 .shutdown = __devexit_p(mwl8k_shutdown),
3653};
3654
3655static int __init mwl8k_init(void)
3656{
3657 return pci_register_driver(&mwl8k_driver);
3658}
3659
3660static void __exit mwl8k_exit(void)
3661{
3662 pci_unregister_driver(&mwl8k_driver);
3663}
3664
3665module_init(mwl8k_init);
3666module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003667
3668MODULE_DESCRIPTION(MWL8K_DESC);
3669MODULE_VERSION(MWL8K_VERSION);
3670MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3671MODULE_LICENSE("GPL");