blob: 13b0e32b051af0c0f8b23a89581e4f39e96b8225 [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
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100205 /* Tasklet to perform TX reclaim. */
206 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100207
208 /* Tasklet to perform RX. */
209 struct tasklet_struct poll_rx_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100210};
211
212/* Per interface specific private data */
213struct mwl8k_vif {
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100214 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100215 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100216};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200217#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218
Lennert Buytenheka6804002010-01-04 21:55:42 +0100219struct mwl8k_sta {
220 /* Index into station database. Returned by UPDATE_STADB. */
221 u8 peer_id;
222};
223#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
224
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225static const struct ieee80211_channel mwl8k_channels[] = {
226 { .center_freq = 2412, .hw_value = 1, },
227 { .center_freq = 2417, .hw_value = 2, },
228 { .center_freq = 2422, .hw_value = 3, },
229 { .center_freq = 2427, .hw_value = 4, },
230 { .center_freq = 2432, .hw_value = 5, },
231 { .center_freq = 2437, .hw_value = 6, },
232 { .center_freq = 2442, .hw_value = 7, },
233 { .center_freq = 2447, .hw_value = 8, },
234 { .center_freq = 2452, .hw_value = 9, },
235 { .center_freq = 2457, .hw_value = 10, },
236 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100237 { .center_freq = 2467, .hw_value = 12, },
238 { .center_freq = 2472, .hw_value = 13, },
239 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100240};
241
242static const struct ieee80211_rate mwl8k_rates[] = {
243 { .bitrate = 10, .hw_value = 2, },
244 { .bitrate = 20, .hw_value = 4, },
245 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200246 { .bitrate = 110, .hw_value = 22, },
247 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100248 { .bitrate = 60, .hw_value = 12, },
249 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100250 { .bitrate = 120, .hw_value = 24, },
251 { .bitrate = 180, .hw_value = 36, },
252 { .bitrate = 240, .hw_value = 48, },
253 { .bitrate = 360, .hw_value = 72, },
254 { .bitrate = 480, .hw_value = 96, },
255 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100256 { .bitrate = 720, .hw_value = 144, },
257};
258
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100259/* Set or get info from Firmware */
260#define MWL8K_CMD_SET 0x0001
261#define MWL8K_CMD_GET 0x0000
262
263/* Firmware command codes */
264#define MWL8K_CMD_CODE_DNLD 0x0001
265#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200266#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100267#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
268#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200269#define MWL8K_CMD_RADIO_CONTROL 0x001c
270#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200271#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100272#define MWL8K_CMD_SET_BEACON 0x0100
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100273#define MWL8K_CMD_SET_PRE_SCAN 0x0107
274#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200275#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100276#define MWL8K_CMD_SET_AID 0x010d
277#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200278#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100279#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200280#define MWL8K_CMD_SET_SLOT 0x0114
281#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
282#define MWL8K_CMD_SET_WMM_MODE 0x0123
283#define MWL8K_CMD_MIMO_CONFIG 0x0125
284#define MWL8K_CMD_USE_FIXED_RATE 0x0126
285#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200286#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200287#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100288#define MWL8K_CMD_BSS_START 0x1100
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100289#define MWL8K_CMD_SET_NEW_STN 0x1111
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200290#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100291
292static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
293{
294#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
295 snprintf(buf, bufsize, "%s", #x);\
296 return buf;\
297 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200298 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100299 MWL8K_CMDNAME(CODE_DNLD);
300 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200301 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100302 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
303 MWL8K_CMDNAME(GET_STAT);
304 MWL8K_CMDNAME(RADIO_CONTROL);
305 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200306 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100307 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100308 MWL8K_CMDNAME(SET_PRE_SCAN);
309 MWL8K_CMDNAME(SET_POST_SCAN);
310 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100311 MWL8K_CMDNAME(SET_AID);
312 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200313 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100314 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200315 MWL8K_CMDNAME(SET_SLOT);
316 MWL8K_CMDNAME(SET_EDCA_PARAMS);
317 MWL8K_CMDNAME(SET_WMM_MODE);
318 MWL8K_CMDNAME(MIMO_CONFIG);
319 MWL8K_CMDNAME(USE_FIXED_RATE);
320 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200321 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200322 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100323 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100324 MWL8K_CMDNAME(SET_NEW_STN);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200325 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100326 default:
327 snprintf(buf, bufsize, "0x%x", cmd);
328 }
329#undef MWL8K_CMDNAME
330
331 return buf;
332}
333
334/* Hardware and firmware reset */
335static void mwl8k_hw_reset(struct mwl8k_priv *priv)
336{
337 iowrite32(MWL8K_H2A_INT_RESET,
338 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
339 iowrite32(MWL8K_H2A_INT_RESET,
340 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
341 msleep(20);
342}
343
344/* Release fw image */
345static void mwl8k_release_fw(struct firmware **fw)
346{
347 if (*fw == NULL)
348 return;
349 release_firmware(*fw);
350 *fw = NULL;
351}
352
353static void mwl8k_release_firmware(struct mwl8k_priv *priv)
354{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100355 mwl8k_release_fw(&priv->fw_ucode);
356 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100357}
358
359/* Request fw image */
360static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200361 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100362{
363 /* release current image */
364 if (*fw != NULL)
365 mwl8k_release_fw(fw);
366
367 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200368 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100369}
370
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200371static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100372{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200373 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100374 int rc;
375
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200376 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100377 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200378 if (rc) {
379 printk(KERN_ERR "%s: Error requesting helper "
380 "firmware file %s\n", pci_name(priv->pdev),
381 di->helper_image);
382 return rc;
383 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100384 }
385
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100386 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100387 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200388 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200389 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100390 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100391 return rc;
392 }
393
394 return 0;
395}
396
Ben Hutchings7e75b942009-11-07 22:00:57 +0000397MODULE_FIRMWARE("mwl8k/helper_8687.fw");
398MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
399
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100400struct mwl8k_cmd_pkt {
401 __le16 code;
402 __le16 length;
403 __le16 seq_num;
404 __le16 result;
405 char payload[0];
406} __attribute__((packed));
407
408/*
409 * Firmware loading.
410 */
411static int
412mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
413{
414 void __iomem *regs = priv->regs;
415 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100416 int loops;
417
418 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
419 if (pci_dma_mapping_error(priv->pdev, dma_addr))
420 return -ENOMEM;
421
422 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
423 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
424 iowrite32(MWL8K_H2A_INT_DOORBELL,
425 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
426 iowrite32(MWL8K_H2A_INT_DUMMY,
427 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
428
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100429 loops = 1000;
430 do {
431 u32 int_code;
432
433 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
434 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
435 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100436 break;
437 }
438
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200439 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100440 udelay(1);
441 } while (--loops);
442
443 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
444
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200445 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100446}
447
448static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
449 const u8 *data, size_t length)
450{
451 struct mwl8k_cmd_pkt *cmd;
452 int done;
453 int rc = 0;
454
455 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
456 if (cmd == NULL)
457 return -ENOMEM;
458
459 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
460 cmd->seq_num = 0;
461 cmd->result = 0;
462
463 done = 0;
464 while (length) {
465 int block_size = length > 256 ? 256 : length;
466
467 memcpy(cmd->payload, data + done, block_size);
468 cmd->length = cpu_to_le16(block_size);
469
470 rc = mwl8k_send_fw_load_cmd(priv, cmd,
471 sizeof(*cmd) + block_size);
472 if (rc)
473 break;
474
475 done += block_size;
476 length -= block_size;
477 }
478
479 if (!rc) {
480 cmd->length = 0;
481 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
482 }
483
484 kfree(cmd);
485
486 return rc;
487}
488
489static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
490 const u8 *data, size_t length)
491{
492 unsigned char *buffer;
493 int may_continue, rc = 0;
494 u32 done, prev_block_size;
495
496 buffer = kmalloc(1024, GFP_KERNEL);
497 if (buffer == NULL)
498 return -ENOMEM;
499
500 done = 0;
501 prev_block_size = 0;
502 may_continue = 1000;
503 while (may_continue > 0) {
504 u32 block_size;
505
506 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
507 if (block_size & 1) {
508 block_size &= ~1;
509 may_continue--;
510 } else {
511 done += prev_block_size;
512 length -= prev_block_size;
513 }
514
515 if (block_size > 1024 || block_size > length) {
516 rc = -EOVERFLOW;
517 break;
518 }
519
520 if (length == 0) {
521 rc = 0;
522 break;
523 }
524
525 if (block_size == 0) {
526 rc = -EPROTO;
527 may_continue--;
528 udelay(1);
529 continue;
530 }
531
532 prev_block_size = block_size;
533 memcpy(buffer, data + done, block_size);
534
535 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
536 if (rc)
537 break;
538 }
539
540 if (!rc && length != 0)
541 rc = -EREMOTEIO;
542
543 kfree(buffer);
544
545 return rc;
546}
547
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200548static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100549{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200550 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100551 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200552 int rc;
553 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100554
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200555 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100556 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100557
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200558 if (helper == NULL) {
559 printk(KERN_ERR "%s: helper image needed but none "
560 "given\n", pci_name(priv->pdev));
561 return -EINVAL;
562 }
563
564 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100565 if (rc) {
566 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200567 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100568 return rc;
569 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100570 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100571
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200572 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100573 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200574 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100575 }
576
577 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200578 printk(KERN_ERR "%s: unable to load firmware image\n",
579 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100580 return rc;
581 }
582
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100583 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100584
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100585 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100586 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200587 u32 ready_code;
588
589 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
590 if (ready_code == MWL8K_FWAP_READY) {
591 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100592 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200593 } else if (ready_code == MWL8K_FWSTA_READY) {
594 priv->ap_fw = 0;
595 break;
596 }
597
598 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100599 udelay(1);
600 } while (--loops);
601
602 return loops ? 0 : -ETIMEDOUT;
603}
604
605
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100606/* DMA header used by firmware and hardware. */
607struct mwl8k_dma_data {
608 __le16 fwlen;
609 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100610 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100611} __attribute__((packed));
612
613/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100614static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100615{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100616 struct mwl8k_dma_data *tr;
617 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100618
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100619 tr = (struct mwl8k_dma_data *)skb->data;
620 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
621
622 if (hdrlen != sizeof(tr->wh)) {
623 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
624 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
625 *((__le16 *)(tr->data - 2)) = qos;
626 } else {
627 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
628 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100629 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100630
631 if (hdrlen != sizeof(*tr))
632 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100633}
634
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200635static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100636{
637 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100638 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100639 struct mwl8k_dma_data *tr;
640
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100641 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100642 * Add a firmware DMA header; the firmware requires that we
643 * present a 2-byte payload length followed by a 4-address
644 * header (without QoS field), followed (optionally) by any
645 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100646 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100647 wh = (struct ieee80211_hdr *)skb->data;
648
649 hdrlen = ieee80211_hdrlen(wh->frame_control);
650 if (hdrlen != sizeof(*tr))
651 skb_push(skb, sizeof(*tr) - hdrlen);
652
653 if (ieee80211_is_data_qos(wh->frame_control))
654 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100655
656 tr = (struct mwl8k_dma_data *)skb->data;
657 if (wh != &tr->wh)
658 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100659 if (hdrlen != sizeof(tr->wh))
660 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100661
662 /*
663 * Firmware length is the length of the fully formed "802.11
664 * payload". That is, everything except for the 802.11 header.
665 * This includes all crypto material including the MIC.
666 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100667 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100668}
669
670
671/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100672 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200673 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100674struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200675 __le16 pkt_len;
676 __u8 sq2;
677 __u8 rate;
678 __le32 pkt_phys_addr;
679 __le32 next_rxd_phys_addr;
680 __le16 qos_control;
681 __le16 htsig2;
682 __le32 hw_rssi_info;
683 __le32 hw_noise_floor_info;
684 __u8 noise_floor;
685 __u8 pad0[3];
686 __u8 rssi;
687 __u8 rx_status;
688 __u8 channel;
689 __u8 rx_ctrl;
690} __attribute__((packed));
691
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100692#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
693#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
694#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100695
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100696#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200697
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100698static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200699{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100700 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200701
702 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100703 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200704}
705
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100706static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200707{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100708 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200709
710 rxd->pkt_len = cpu_to_le16(len);
711 rxd->pkt_phys_addr = cpu_to_le32(addr);
712 wmb();
713 rxd->rx_ctrl = 0;
714}
715
716static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100717mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
718 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200719{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100720 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200721
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100722 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200723 return -1;
724 rmb();
725
726 memset(status, 0, sizeof(*status));
727
728 status->signal = -rxd->rssi;
729 status->noise = -rxd->noise_floor;
730
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100731 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200732 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100733 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100734 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100735 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200736 } else {
737 int i;
738
739 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
740 if (mwl8k_rates[i].hw_value == rxd->rate) {
741 status->rate_idx = i;
742 break;
743 }
744 }
745 }
746
747 status->band = IEEE80211_BAND_2GHZ;
748 status->freq = ieee80211_channel_to_frequency(rxd->channel);
749
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100750 *qos = rxd->qos_control;
751
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200752 return le16_to_cpu(rxd->pkt_len);
753}
754
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100755static struct rxd_ops rxd_8366_ap_ops = {
756 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
757 .rxd_init = mwl8k_rxd_8366_ap_init,
758 .rxd_refill = mwl8k_rxd_8366_ap_refill,
759 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200760};
761
762/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100763 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100764 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100765struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100766 __le16 pkt_len;
767 __u8 link_quality;
768 __u8 noise_level;
769 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200770 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100771 __le16 qos_control;
772 __le16 rate_info;
773 __le32 pad0[4];
774 __u8 rssi;
775 __u8 channel;
776 __le16 pad1;
777 __u8 rx_ctrl;
778 __u8 rx_status;
779 __u8 pad2[2];
780} __attribute__((packed));
781
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100782#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
783#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
784#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
785#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
786#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
787#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200788
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100789#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200790
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100791static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200792{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100793 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200794
795 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100796 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200797}
798
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100799static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200800{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100801 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200802
803 rxd->pkt_len = cpu_to_le16(len);
804 rxd->pkt_phys_addr = cpu_to_le32(addr);
805 wmb();
806 rxd->rx_ctrl = 0;
807}
808
809static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100810mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100811 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200812{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100813 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200814 u16 rate_info;
815
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100816 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200817 return -1;
818 rmb();
819
820 rate_info = le16_to_cpu(rxd->rate_info);
821
822 memset(status, 0, sizeof(*status));
823
824 status->signal = -rxd->rssi;
825 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100826 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
827 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200828
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100829 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200830 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100831 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200832 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100833 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200834 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100835 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200836 status->flag |= RX_FLAG_HT;
837
838 status->band = IEEE80211_BAND_2GHZ;
839 status->freq = ieee80211_channel_to_frequency(rxd->channel);
840
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100841 *qos = rxd->qos_control;
842
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200843 return le16_to_cpu(rxd->pkt_len);
844}
845
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100846static struct rxd_ops rxd_sta_ops = {
847 .rxd_size = sizeof(struct mwl8k_rxd_sta),
848 .rxd_init = mwl8k_rxd_sta_init,
849 .rxd_refill = mwl8k_rxd_sta_refill,
850 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200851};
852
853
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100854#define MWL8K_RX_DESCS 256
855#define MWL8K_RX_MAXSZ 3800
856
857static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
858{
859 struct mwl8k_priv *priv = hw->priv;
860 struct mwl8k_rx_queue *rxq = priv->rxq + index;
861 int size;
862 int i;
863
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200864 rxq->rxd_count = 0;
865 rxq->head = 0;
866 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100867
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200868 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100869
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200870 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
871 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100872 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200873 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100874 return -ENOMEM;
875 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200876 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100877
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200878 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
879 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100880 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200881 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200882 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100883 return -ENOMEM;
884 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200885 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100886
887 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200888 int desc_size;
889 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100890 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200891 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100892
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200893 desc_size = priv->rxd_ops->rxd_size;
894 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100895
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200896 nexti = i + 1;
897 if (nexti == MWL8K_RX_DESCS)
898 nexti = 0;
899 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
900
901 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100902 }
903
904 return 0;
905}
906
907static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
908{
909 struct mwl8k_priv *priv = hw->priv;
910 struct mwl8k_rx_queue *rxq = priv->rxq + index;
911 int refilled;
912
913 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200914 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100915 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200916 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100917 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200918 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100919
920 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
921 if (skb == NULL)
922 break;
923
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200924 addr = pci_map_single(priv->pdev, skb->data,
925 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100926
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200927 rxq->rxd_count++;
928 rx = rxq->tail++;
929 if (rxq->tail == MWL8K_RX_DESCS)
930 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200931 rxq->buf[rx].skb = skb;
932 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200933
934 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
935 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100936
937 refilled++;
938 }
939
940 return refilled;
941}
942
943/* Must be called only when the card's reception is completely halted */
944static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
945{
946 struct mwl8k_priv *priv = hw->priv;
947 struct mwl8k_rx_queue *rxq = priv->rxq + index;
948 int i;
949
950 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200951 if (rxq->buf[i].skb != NULL) {
952 pci_unmap_single(priv->pdev,
953 pci_unmap_addr(&rxq->buf[i], dma),
954 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
955 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100956
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200957 kfree_skb(rxq->buf[i].skb);
958 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100959 }
960 }
961
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200962 kfree(rxq->buf);
963 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100964
965 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200966 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200967 rxq->rxd, rxq->rxd_dma);
968 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100969}
970
971
972/*
973 * Scan a list of BSSIDs to process for finalize join.
974 * Allows for extension to process multiple BSSIDs.
975 */
976static inline int
977mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
978{
979 return priv->capture_beacon &&
980 ieee80211_is_beacon(wh->frame_control) &&
981 !compare_ether_addr(wh->addr3, priv->capture_bssid);
982}
983
Lennert Buytenhek37797522009-10-22 20:19:45 +0200984static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
985 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100986{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200987 struct mwl8k_priv *priv = hw->priv;
988
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100989 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200990 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100991
992 /*
993 * Use GFP_ATOMIC as rxq_process is called from
994 * the primary interrupt handler, memory allocation call
995 * must not sleep.
996 */
997 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
998 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200999 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001000}
1001
1002static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1003{
1004 struct mwl8k_priv *priv = hw->priv;
1005 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1006 int processed;
1007
1008 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001009 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001010 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001011 void *rxd;
1012 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001013 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001014 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001015
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001016 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001017 if (skb == NULL)
1018 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001019
1020 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1021
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001022 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001023 if (pkt_len < 0)
1024 break;
1025
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001026 rxq->buf[rxq->head].skb = NULL;
1027
1028 pci_unmap_single(priv->pdev,
1029 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1030 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1031 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001032
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001033 rxq->head++;
1034 if (rxq->head == MWL8K_RX_DESCS)
1035 rxq->head = 0;
1036
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001037 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001038
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001039 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001040 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001041
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001042 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001043 * Check for a pending join operation. Save a
1044 * copy of the beacon and schedule a tasklet to
1045 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001046 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001047 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001048 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001049
Johannes Bergf1d58c22009-06-17 13:13:00 +02001050 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1051 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001052
1053 processed++;
1054 }
1055
1056 return processed;
1057}
1058
1059
1060/*
1061 * Packet transmission.
1062 */
1063
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001064#define MWL8K_TXD_STATUS_OK 0x00000001
1065#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1066#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1067#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001068#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001069
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001070#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1071#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1072#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1073#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1074#define MWL8K_QOS_EOSP 0x0010
1075
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001076struct mwl8k_tx_desc {
1077 __le32 status;
1078 __u8 data_rate;
1079 __u8 tx_priority;
1080 __le16 qos_control;
1081 __le32 pkt_phys_addr;
1082 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001083 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001084 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001085 __le32 reserved;
1086 __le16 rate_info;
1087 __u8 peer_id;
1088 __u8 tx_frag_cnt;
1089} __attribute__((packed));
1090
1091#define MWL8K_TX_DESCS 128
1092
1093static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1094{
1095 struct mwl8k_priv *priv = hw->priv;
1096 struct mwl8k_tx_queue *txq = priv->txq + index;
1097 int size;
1098 int i;
1099
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001100 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1101 txq->stats.limit = MWL8K_TX_DESCS;
1102 txq->head = 0;
1103 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001104
1105 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1106
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001107 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1108 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001109 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001110 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001111 return -ENOMEM;
1112 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001113 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001114
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001115 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1116 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001117 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001118 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001119 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001120 return -ENOMEM;
1121 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001122 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001123
1124 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1125 struct mwl8k_tx_desc *tx_desc;
1126 int nexti;
1127
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001128 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001129 nexti = (i + 1) % MWL8K_TX_DESCS;
1130
1131 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001132 tx_desc->next_txd_phys_addr =
1133 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001134 }
1135
1136 return 0;
1137}
1138
1139static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1140{
1141 iowrite32(MWL8K_H2A_INT_PPA_READY,
1142 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1143 iowrite32(MWL8K_H2A_INT_DUMMY,
1144 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1145 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1146}
1147
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001148static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001149{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001150 struct mwl8k_priv *priv = hw->priv;
1151 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001152
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001153 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1154 struct mwl8k_tx_queue *txq = priv->txq + i;
1155 int fw_owned = 0;
1156 int drv_owned = 0;
1157 int unused = 0;
1158 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001159
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001160 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001161 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1162 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001164 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001165 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001166 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001168 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001169
1170 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001171 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001172 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001173
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001174 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1175 "fw_owned=%d drv_owned=%d unused=%d\n",
1176 wiphy_name(hw->wiphy), i,
1177 txq->stats.len, txq->head, txq->tail,
1178 fw_owned, drv_owned, unused);
1179 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001180}
1181
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001182/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001183 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001184 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001185#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001186
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001187static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001188{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001189 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001190 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001191 int retry;
1192 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001193
1194 might_sleep();
1195
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001196 /*
1197 * The TX queues are stopped at this point, so this test
1198 * doesn't need to take ->tx_lock.
1199 */
1200 if (!priv->pending_tx_pkts)
1201 return 0;
1202
1203 retry = 0;
1204 rc = 0;
1205
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001206 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001207 priv->tx_wait = &tx_wait;
1208 while (!rc) {
1209 int oldcount;
1210 unsigned long timeout;
1211
1212 oldcount = priv->pending_tx_pkts;
1213
1214 spin_unlock_bh(&priv->tx_lock);
1215 timeout = wait_for_completion_timeout(&tx_wait,
1216 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1217 spin_lock_bh(&priv->tx_lock);
1218
1219 if (timeout) {
1220 WARN_ON(priv->pending_tx_pkts);
1221 if (retry) {
1222 printk(KERN_NOTICE "%s: tx rings drained\n",
1223 wiphy_name(hw->wiphy));
1224 }
1225 break;
1226 }
1227
1228 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001229 printk(KERN_NOTICE "%s: waiting for tx rings "
1230 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001231 wiphy_name(hw->wiphy), oldcount,
1232 priv->pending_tx_pkts);
1233 retry = 1;
1234 continue;
1235 }
1236
1237 priv->tx_wait = NULL;
1238
1239 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1240 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1241 mwl8k_dump_tx_rings(hw);
1242
1243 rc = -ETIMEDOUT;
1244 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001245 spin_unlock_bh(&priv->tx_lock);
1246
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001247 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001248}
1249
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001250#define MWL8K_TXD_SUCCESS(status) \
1251 ((status) & (MWL8K_TXD_STATUS_OK | \
1252 MWL8K_TXD_STATUS_OK_RETRY | \
1253 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001254
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001255static int
1256mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001257{
1258 struct mwl8k_priv *priv = hw->priv;
1259 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001260 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001261
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001262 processed = 0;
1263 while (txq->stats.len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001264 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001265 struct mwl8k_tx_desc *tx_desc;
1266 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001267 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001268 struct sk_buff *skb;
1269 struct ieee80211_tx_info *info;
1270 u32 status;
1271
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001272 tx = txq->head;
1273 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001274
1275 status = le32_to_cpu(tx_desc->status);
1276
1277 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1278 if (!force)
1279 break;
1280 tx_desc->status &=
1281 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1282 }
1283
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001284 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1285 BUG_ON(txq->stats.len == 0);
1286 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001287 priv->pending_tx_pkts--;
1288
1289 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001290 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001291 skb = txq->skb[tx];
1292 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001293
1294 BUG_ON(skb == NULL);
1295 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1296
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001297 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001298
1299 /* Mark descriptor as unused */
1300 tx_desc->pkt_phys_addr = 0;
1301 tx_desc->pkt_len = 0;
1302
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303 info = IEEE80211_SKB_CB(skb);
1304 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001305 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001306 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001307
1308 ieee80211_tx_status_irqsafe(hw, skb);
1309
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001310 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001311 }
1312
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001313 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001314 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001315
1316 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001317}
1318
1319/* must be called only when the card's transmit is completely halted */
1320static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1321{
1322 struct mwl8k_priv *priv = hw->priv;
1323 struct mwl8k_tx_queue *txq = priv->txq + index;
1324
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001325 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001326
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001327 kfree(txq->skb);
1328 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001329
1330 pci_free_consistent(priv->pdev,
1331 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001332 txq->txd, txq->txd_dma);
1333 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001334}
1335
1336static int
1337mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1338{
1339 struct mwl8k_priv *priv = hw->priv;
1340 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001341 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001342 struct ieee80211_hdr *wh;
1343 struct mwl8k_tx_queue *txq;
1344 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001346 u32 txstatus;
1347 u8 txdatarate;
1348 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001349
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001350 wh = (struct ieee80211_hdr *)skb->data;
1351 if (ieee80211_is_data_qos(wh->frame_control))
1352 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1353 else
1354 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001355
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001356 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001357 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001358
1359 tx_info = IEEE80211_SKB_CB(skb);
1360 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001361
1362 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1363 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001364
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001365 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1366 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1367 mwl8k_vif->seqno = seqno++ % 4096;
1368 }
1369
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001370 /* Setup firmware control bit fields for each frame type. */
1371 txstatus = 0;
1372 txdatarate = 0;
1373 if (ieee80211_is_mgmt(wh->frame_control) ||
1374 ieee80211_is_ctl(wh->frame_control)) {
1375 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001376 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001377 } else if (ieee80211_is_data(wh->frame_control)) {
1378 txdatarate = 1;
1379 if (is_multicast_ether_addr(wh->addr1))
1380 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1381
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001382 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001383 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001384 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001385 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001386 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001387 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001388
1389 dma = pci_map_single(priv->pdev, skb->data,
1390 skb->len, PCI_DMA_TODEVICE);
1391
1392 if (pci_dma_mapping_error(priv->pdev, dma)) {
1393 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001394 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001395 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001396 return NETDEV_TX_OK;
1397 }
1398
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001399 spin_lock_bh(&priv->tx_lock);
1400
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001401 txq = priv->txq + index;
1402
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001403 BUG_ON(txq->skb[txq->tail] != NULL);
1404 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001405
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001406 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001407 tx->data_rate = txdatarate;
1408 tx->tx_priority = index;
1409 tx->qos_control = cpu_to_le16(qos);
1410 tx->pkt_phys_addr = cpu_to_le32(dma);
1411 tx->pkt_len = cpu_to_le16(skb->len);
1412 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001413 if (!priv->ap_fw && tx_info->control.sta != NULL)
1414 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1415 else
1416 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001417 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001418 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1419
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001420 txq->stats.count++;
1421 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001422 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001423
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001424 txq->tail++;
1425 if (txq->tail == MWL8K_TX_DESCS)
1426 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001427
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001428 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001429 ieee80211_stop_queue(hw, index);
1430
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001431 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001432
1433 spin_unlock_bh(&priv->tx_lock);
1434
1435 return NETDEV_TX_OK;
1436}
1437
1438
1439/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001440 * Firmware access.
1441 *
1442 * We have the following requirements for issuing firmware commands:
1443 * - Some commands require that the packet transmit path is idle when
1444 * the command is issued. (For simplicity, we'll just quiesce the
1445 * transmit path for every command.)
1446 * - There are certain sequences of commands that need to be issued to
1447 * the hardware sequentially, with no other intervening commands.
1448 *
1449 * This leads to an implementation of a "firmware lock" as a mutex that
1450 * can be taken recursively, and which is taken by both the low-level
1451 * command submission function (mwl8k_post_cmd) as well as any users of
1452 * that function that require issuing of an atomic sequence of commands,
1453 * and quiesces the transmit path whenever it's taken.
1454 */
1455static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1456{
1457 struct mwl8k_priv *priv = hw->priv;
1458
1459 if (priv->fw_mutex_owner != current) {
1460 int rc;
1461
1462 mutex_lock(&priv->fw_mutex);
1463 ieee80211_stop_queues(hw);
1464
1465 rc = mwl8k_tx_wait_empty(hw);
1466 if (rc) {
1467 ieee80211_wake_queues(hw);
1468 mutex_unlock(&priv->fw_mutex);
1469
1470 return rc;
1471 }
1472
1473 priv->fw_mutex_owner = current;
1474 }
1475
1476 priv->fw_mutex_depth++;
1477
1478 return 0;
1479}
1480
1481static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1482{
1483 struct mwl8k_priv *priv = hw->priv;
1484
1485 if (!--priv->fw_mutex_depth) {
1486 ieee80211_wake_queues(hw);
1487 priv->fw_mutex_owner = NULL;
1488 mutex_unlock(&priv->fw_mutex);
1489 }
1490}
1491
1492
1493/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001494 * Command processing.
1495 */
1496
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001497/* Timeout firmware commands after 10s */
1498#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001499
1500static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1501{
1502 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1503 struct mwl8k_priv *priv = hw->priv;
1504 void __iomem *regs = priv->regs;
1505 dma_addr_t dma_addr;
1506 unsigned int dma_size;
1507 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001508 unsigned long timeout = 0;
1509 u8 buf[32];
1510
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001511 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001512 dma_size = le16_to_cpu(cmd->length);
1513 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1514 PCI_DMA_BIDIRECTIONAL);
1515 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1516 return -ENOMEM;
1517
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001518 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001519 if (rc) {
1520 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1521 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001522 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001523 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001524
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001525 priv->hostcmd_wait = &cmd_wait;
1526 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1527 iowrite32(MWL8K_H2A_INT_DOORBELL,
1528 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1529 iowrite32(MWL8K_H2A_INT_DUMMY,
1530 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001531
1532 timeout = wait_for_completion_timeout(&cmd_wait,
1533 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1534
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001535 priv->hostcmd_wait = NULL;
1536
1537 mwl8k_fw_unlock(hw);
1538
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001539 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1540 PCI_DMA_BIDIRECTIONAL);
1541
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001542 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001543 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001544 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001545 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1546 MWL8K_CMD_TIMEOUT_MS);
1547 rc = -ETIMEDOUT;
1548 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001549 int ms;
1550
1551 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1552
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001553 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001554 if (rc)
1555 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001556 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001557 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001558 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001559 else if (ms > 2000)
1560 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1561 wiphy_name(hw->wiphy),
1562 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1563 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001564 }
1565
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001566 return rc;
1567}
1568
1569/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001570 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001571 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001572struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001573 struct mwl8k_cmd_pkt header;
1574 __u8 hw_rev;
1575 __u8 host_interface;
1576 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001577 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578 __le16 region_code;
1579 __le32 fw_rev;
1580 __le32 ps_cookie;
1581 __le32 caps;
1582 __u8 mcs_bitmap[16];
1583 __le32 rx_queue_ptr;
1584 __le32 num_tx_queues;
1585 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1586 __le32 caps2;
1587 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001588 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001589} __attribute__((packed));
1590
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001591#define MWL8K_CAP_MAX_AMSDU 0x20000000
1592#define MWL8K_CAP_GREENFIELD 0x08000000
1593#define MWL8K_CAP_AMPDU 0x04000000
1594#define MWL8K_CAP_RX_STBC 0x01000000
1595#define MWL8K_CAP_TX_STBC 0x00800000
1596#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1597#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1598#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1599#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1600#define MWL8K_CAP_DELAY_BA 0x00003000
1601#define MWL8K_CAP_MIMO 0x00000200
1602#define MWL8K_CAP_40MHZ 0x00000100
1603
1604static void mwl8k_set_ht_caps(struct ieee80211_hw *hw, u32 cap)
1605{
1606 struct mwl8k_priv *priv = hw->priv;
1607 int rx_streams;
1608 int tx_streams;
1609
1610 priv->band.ht_cap.ht_supported = 1;
1611
1612 if (cap & MWL8K_CAP_MAX_AMSDU)
1613 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
1614 if (cap & MWL8K_CAP_GREENFIELD)
1615 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
1616 if (cap & MWL8K_CAP_AMPDU) {
1617 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
1618 priv->band.ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1619 priv->band.ht_cap.ampdu_density =
1620 IEEE80211_HT_MPDU_DENSITY_NONE;
1621 }
1622 if (cap & MWL8K_CAP_RX_STBC)
1623 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
1624 if (cap & MWL8K_CAP_TX_STBC)
1625 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
1626 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
1627 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
1628 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
1629 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
1630 if (cap & MWL8K_CAP_DELAY_BA)
1631 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
1632 if (cap & MWL8K_CAP_40MHZ)
1633 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1634
1635 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1636 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1637
1638 priv->band.ht_cap.mcs.rx_mask[0] = 0xff;
1639 if (rx_streams >= 2)
1640 priv->band.ht_cap.mcs.rx_mask[1] = 0xff;
1641 if (rx_streams >= 3)
1642 priv->band.ht_cap.mcs.rx_mask[2] = 0xff;
1643 priv->band.ht_cap.mcs.rx_mask[4] = 0x01;
1644 priv->band.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
1645
1646 if (rx_streams != tx_streams) {
1647 priv->band.ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1648 priv->band.ht_cap.mcs.tx_params |= (tx_streams - 1) <<
1649 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1650 }
1651}
1652
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001653static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001654{
1655 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001656 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 int rc;
1658 int i;
1659
1660 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1661 if (cmd == NULL)
1662 return -ENOMEM;
1663
1664 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1665 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1666
1667 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1668 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001669 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001670 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001671 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001672 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001673 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001674 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001675
1676 rc = mwl8k_post_cmd(hw, &cmd->header);
1677
1678 if (!rc) {
1679 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1680 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001681 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001682 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001683 if (cmd->caps & cpu_to_le32(MWL8K_CAP_MIMO))
1684 mwl8k_set_ht_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001685 }
1686
1687 kfree(cmd);
1688 return rc;
1689}
1690
1691/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001692 * CMD_GET_HW_SPEC (AP version).
1693 */
1694struct mwl8k_cmd_get_hw_spec_ap {
1695 struct mwl8k_cmd_pkt header;
1696 __u8 hw_rev;
1697 __u8 host_interface;
1698 __le16 num_wcb;
1699 __le16 num_mcaddrs;
1700 __u8 perm_addr[ETH_ALEN];
1701 __le16 region_code;
1702 __le16 num_antenna;
1703 __le32 fw_rev;
1704 __le32 wcbbase0;
1705 __le32 rxwrptr;
1706 __le32 rxrdptr;
1707 __le32 ps_cookie;
1708 __le32 wcbbase1;
1709 __le32 wcbbase2;
1710 __le32 wcbbase3;
1711} __attribute__((packed));
1712
1713static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1714{
1715 struct mwl8k_priv *priv = hw->priv;
1716 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1717 int rc;
1718
1719 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1720 if (cmd == NULL)
1721 return -ENOMEM;
1722
1723 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1724 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1725
1726 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1727 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1728
1729 rc = mwl8k_post_cmd(hw, &cmd->header);
1730
1731 if (!rc) {
1732 int off;
1733
1734 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1735 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1736 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1737 priv->hw_rev = cmd->hw_rev;
1738
1739 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1740 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1741
1742 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1743 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1744
1745 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1746 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1747
1748 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1749 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1750
1751 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1752 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1753
1754 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1755 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1756 }
1757
1758 kfree(cmd);
1759 return rc;
1760}
1761
1762/*
1763 * CMD_SET_HW_SPEC.
1764 */
1765struct mwl8k_cmd_set_hw_spec {
1766 struct mwl8k_cmd_pkt header;
1767 __u8 hw_rev;
1768 __u8 host_interface;
1769 __le16 num_mcaddrs;
1770 __u8 perm_addr[ETH_ALEN];
1771 __le16 region_code;
1772 __le32 fw_rev;
1773 __le32 ps_cookie;
1774 __le32 caps;
1775 __le32 rx_queue_ptr;
1776 __le32 num_tx_queues;
1777 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1778 __le32 flags;
1779 __le32 num_tx_desc_per_queue;
1780 __le32 total_rxd;
1781} __attribute__((packed));
1782
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001783#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1784#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
1785#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001786
1787static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1788{
1789 struct mwl8k_priv *priv = hw->priv;
1790 struct mwl8k_cmd_set_hw_spec *cmd;
1791 int rc;
1792 int i;
1793
1794 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1795 if (cmd == NULL)
1796 return -ENOMEM;
1797
1798 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1799 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1800
1801 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1802 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1803 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1804 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1805 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001806 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
1807 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
1808 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001809 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1810 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1811
1812 rc = mwl8k_post_cmd(hw, &cmd->header);
1813 kfree(cmd);
1814
1815 return rc;
1816}
1817
1818/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001819 * CMD_MAC_MULTICAST_ADR.
1820 */
1821struct mwl8k_cmd_mac_multicast_adr {
1822 struct mwl8k_cmd_pkt header;
1823 __le16 action;
1824 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001825 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001826};
1827
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001828#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1829#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1830#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1831#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001832
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001833static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001834__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001835 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001836{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001837 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001838 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001839 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001840
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001841 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001842 allmulti = 1;
1843 mc_count = 0;
1844 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001845
1846 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1847
1848 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001849 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001850 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001851
1852 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1853 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001854 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1855 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001856
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001857 if (allmulti) {
1858 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1859 } else if (mc_count) {
1860 int i;
1861
1862 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1863 cmd->numaddr = cpu_to_le16(mc_count);
1864 for (i = 0; i < mc_count && mclist; i++) {
1865 if (mclist->da_addrlen != ETH_ALEN) {
1866 kfree(cmd);
1867 return NULL;
1868 }
1869 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1870 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001871 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001872 }
1873
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001874 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001875}
1876
1877/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001878 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001879 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001880struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001881 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001882 __le32 stats[64];
1883} __attribute__((packed));
1884
1885#define MWL8K_STAT_ACK_FAILURE 9
1886#define MWL8K_STAT_RTS_FAILURE 12
1887#define MWL8K_STAT_FCS_ERROR 24
1888#define MWL8K_STAT_RTS_SUCCESS 11
1889
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001890static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1891 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001892{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001893 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001894 int rc;
1895
1896 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1897 if (cmd == NULL)
1898 return -ENOMEM;
1899
1900 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1901 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001902
1903 rc = mwl8k_post_cmd(hw, &cmd->header);
1904 if (!rc) {
1905 stats->dot11ACKFailureCount =
1906 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1907 stats->dot11RTSFailureCount =
1908 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1909 stats->dot11FCSErrorCount =
1910 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1911 stats->dot11RTSSuccessCount =
1912 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1913 }
1914 kfree(cmd);
1915
1916 return rc;
1917}
1918
1919/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001920 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001921 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001922struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001923 struct mwl8k_cmd_pkt header;
1924 __le16 action;
1925 __le16 control;
1926 __le16 radio_on;
1927} __attribute__((packed));
1928
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001929static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001930mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001931{
1932 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001933 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001934 int rc;
1935
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001936 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001937 return 0;
1938
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001939 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1940 if (cmd == NULL)
1941 return -ENOMEM;
1942
1943 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1944 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1945 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001946 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001947 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1948
1949 rc = mwl8k_post_cmd(hw, &cmd->header);
1950 kfree(cmd);
1951
1952 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001953 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001954
1955 return rc;
1956}
1957
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001958static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001959{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001960 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001961}
1962
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001963static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001964{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001965 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001966}
1967
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001968static int
1969mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1970{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001971 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001972
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001973 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001974
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001975 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001976}
1977
1978/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001979 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001980 */
1981#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1982
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001983struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001984 struct mwl8k_cmd_pkt header;
1985 __le16 action;
1986 __le16 support_level;
1987 __le16 current_level;
1988 __le16 reserved;
1989 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1990} __attribute__((packed));
1991
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001992static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001993{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001994 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001995 int rc;
1996
1997 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1998 if (cmd == NULL)
1999 return -ENOMEM;
2000
2001 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2002 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2003 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2004 cmd->support_level = cpu_to_le16(dBm);
2005
2006 rc = mwl8k_post_cmd(hw, &cmd->header);
2007 kfree(cmd);
2008
2009 return rc;
2010}
2011
2012/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002013 * CMD_RF_ANTENNA.
2014 */
2015struct mwl8k_cmd_rf_antenna {
2016 struct mwl8k_cmd_pkt header;
2017 __le16 antenna;
2018 __le16 mode;
2019} __attribute__((packed));
2020
2021#define MWL8K_RF_ANTENNA_RX 1
2022#define MWL8K_RF_ANTENNA_TX 2
2023
2024static int
2025mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2026{
2027 struct mwl8k_cmd_rf_antenna *cmd;
2028 int rc;
2029
2030 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2031 if (cmd == NULL)
2032 return -ENOMEM;
2033
2034 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2035 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2036 cmd->antenna = cpu_to_le16(antenna);
2037 cmd->mode = cpu_to_le16(mask);
2038
2039 rc = mwl8k_post_cmd(hw, &cmd->header);
2040 kfree(cmd);
2041
2042 return rc;
2043}
2044
2045/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002046 * CMD_SET_BEACON.
2047 */
2048struct mwl8k_cmd_set_beacon {
2049 struct mwl8k_cmd_pkt header;
2050 __le16 beacon_len;
2051 __u8 beacon[0];
2052};
2053
2054static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw, u8 *beacon, int len)
2055{
2056 struct mwl8k_cmd_set_beacon *cmd;
2057 int rc;
2058
2059 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2060 if (cmd == NULL)
2061 return -ENOMEM;
2062
2063 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2064 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2065 cmd->beacon_len = cpu_to_le16(len);
2066 memcpy(cmd->beacon, beacon, len);
2067
2068 rc = mwl8k_post_cmd(hw, &cmd->header);
2069 kfree(cmd);
2070
2071 return rc;
2072}
2073
2074/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002075 * CMD_SET_PRE_SCAN.
2076 */
2077struct mwl8k_cmd_set_pre_scan {
2078 struct mwl8k_cmd_pkt header;
2079} __attribute__((packed));
2080
2081static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2082{
2083 struct mwl8k_cmd_set_pre_scan *cmd;
2084 int rc;
2085
2086 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2087 if (cmd == NULL)
2088 return -ENOMEM;
2089
2090 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2091 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2092
2093 rc = mwl8k_post_cmd(hw, &cmd->header);
2094 kfree(cmd);
2095
2096 return rc;
2097}
2098
2099/*
2100 * CMD_SET_POST_SCAN.
2101 */
2102struct mwl8k_cmd_set_post_scan {
2103 struct mwl8k_cmd_pkt header;
2104 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002105 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002106} __attribute__((packed));
2107
2108static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002109mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002110{
2111 struct mwl8k_cmd_set_post_scan *cmd;
2112 int rc;
2113
2114 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2115 if (cmd == NULL)
2116 return -ENOMEM;
2117
2118 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2119 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2120 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002121 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002122
2123 rc = mwl8k_post_cmd(hw, &cmd->header);
2124 kfree(cmd);
2125
2126 return rc;
2127}
2128
2129/*
2130 * CMD_SET_RF_CHANNEL.
2131 */
2132struct mwl8k_cmd_set_rf_channel {
2133 struct mwl8k_cmd_pkt header;
2134 __le16 action;
2135 __u8 current_channel;
2136 __le32 channel_flags;
2137} __attribute__((packed));
2138
2139static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002140 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002141{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002142 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002143 struct mwl8k_cmd_set_rf_channel *cmd;
2144 int rc;
2145
2146 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2147 if (cmd == NULL)
2148 return -ENOMEM;
2149
2150 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2151 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2152 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2153 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002154
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002155 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002156 cmd->channel_flags |= cpu_to_le32(0x00000001);
2157
2158 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2159 conf->channel_type == NL80211_CHAN_HT20)
2160 cmd->channel_flags |= cpu_to_le32(0x00000080);
2161 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2162 cmd->channel_flags |= cpu_to_le32(0x000001900);
2163 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2164 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002165
2166 rc = mwl8k_post_cmd(hw, &cmd->header);
2167 kfree(cmd);
2168
2169 return rc;
2170}
2171
2172/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002173 * CMD_SET_AID.
2174 */
2175#define MWL8K_FRAME_PROT_DISABLED 0x00
2176#define MWL8K_FRAME_PROT_11G 0x07
2177#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2178#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2179
2180struct mwl8k_cmd_update_set_aid {
2181 struct mwl8k_cmd_pkt header;
2182 __le16 aid;
2183
2184 /* AP's MAC address (BSSID) */
2185 __u8 bssid[ETH_ALEN];
2186 __le16 protection_mode;
2187 __u8 supp_rates[14];
2188} __attribute__((packed));
2189
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002190static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2191{
2192 int i;
2193 int j;
2194
2195 /*
2196 * Clear nonstandard rates 4 and 13.
2197 */
2198 mask &= 0x1fef;
2199
2200 for (i = 0, j = 0; i < 14; i++) {
2201 if (mask & (1 << i))
2202 rates[j++] = mwl8k_rates[i].hw_value;
2203 }
2204}
2205
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002206static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002207mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2208 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002209{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002210 struct mwl8k_cmd_update_set_aid *cmd;
2211 u16 prot_mode;
2212 int rc;
2213
2214 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2215 if (cmd == NULL)
2216 return -ENOMEM;
2217
2218 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2219 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002220 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002221 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002222
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002223 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002224 prot_mode = MWL8K_FRAME_PROT_11G;
2225 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002226 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002227 IEEE80211_HT_OP_MODE_PROTECTION) {
2228 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2229 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2230 break;
2231 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2232 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2233 break;
2234 default:
2235 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2236 break;
2237 }
2238 }
2239 cmd->protection_mode = cpu_to_le16(prot_mode);
2240
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002241 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002242
2243 rc = mwl8k_post_cmd(hw, &cmd->header);
2244 kfree(cmd);
2245
2246 return rc;
2247}
2248
2249/*
2250 * CMD_SET_RATE.
2251 */
2252struct mwl8k_cmd_set_rate {
2253 struct mwl8k_cmd_pkt header;
2254 __u8 legacy_rates[14];
2255
2256 /* Bitmap for supported MCS codes. */
2257 __u8 mcs_set[16];
2258 __u8 reserved[16];
2259} __attribute__((packed));
2260
2261static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002262mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002263 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002264{
2265 struct mwl8k_cmd_set_rate *cmd;
2266 int rc;
2267
2268 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2269 if (cmd == NULL)
2270 return -ENOMEM;
2271
2272 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2273 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002274 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002275 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002276
2277 rc = mwl8k_post_cmd(hw, &cmd->header);
2278 kfree(cmd);
2279
2280 return rc;
2281}
2282
2283/*
2284 * CMD_FINALIZE_JOIN.
2285 */
2286#define MWL8K_FJ_BEACON_MAXLEN 128
2287
2288struct mwl8k_cmd_finalize_join {
2289 struct mwl8k_cmd_pkt header;
2290 __le32 sleep_interval; /* Number of beacon periods to sleep */
2291 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2292} __attribute__((packed));
2293
2294static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2295 int framelen, int dtim)
2296{
2297 struct mwl8k_cmd_finalize_join *cmd;
2298 struct ieee80211_mgmt *payload = frame;
2299 int payload_len;
2300 int rc;
2301
2302 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2303 if (cmd == NULL)
2304 return -ENOMEM;
2305
2306 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2307 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2308 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2309
2310 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2311 if (payload_len < 0)
2312 payload_len = 0;
2313 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2314 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2315
2316 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2317
2318 rc = mwl8k_post_cmd(hw, &cmd->header);
2319 kfree(cmd);
2320
2321 return rc;
2322}
2323
2324/*
2325 * CMD_SET_RTS_THRESHOLD.
2326 */
2327struct mwl8k_cmd_set_rts_threshold {
2328 struct mwl8k_cmd_pkt header;
2329 __le16 action;
2330 __le16 threshold;
2331} __attribute__((packed));
2332
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002333static int
2334mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002335{
2336 struct mwl8k_cmd_set_rts_threshold *cmd;
2337 int rc;
2338
2339 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2340 if (cmd == NULL)
2341 return -ENOMEM;
2342
2343 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2344 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002345 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2346 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002347
2348 rc = mwl8k_post_cmd(hw, &cmd->header);
2349 kfree(cmd);
2350
2351 return rc;
2352}
2353
2354/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002355 * CMD_SET_SLOT.
2356 */
2357struct mwl8k_cmd_set_slot {
2358 struct mwl8k_cmd_pkt header;
2359 __le16 action;
2360 __u8 short_slot;
2361} __attribute__((packed));
2362
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002363static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002364{
2365 struct mwl8k_cmd_set_slot *cmd;
2366 int rc;
2367
2368 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2369 if (cmd == NULL)
2370 return -ENOMEM;
2371
2372 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2373 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2374 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002375 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002376
2377 rc = mwl8k_post_cmd(hw, &cmd->header);
2378 kfree(cmd);
2379
2380 return rc;
2381}
2382
2383/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002384 * CMD_SET_EDCA_PARAMS.
2385 */
2386struct mwl8k_cmd_set_edca_params {
2387 struct mwl8k_cmd_pkt header;
2388
2389 /* See MWL8K_SET_EDCA_XXX below */
2390 __le16 action;
2391
2392 /* TX opportunity in units of 32 us */
2393 __le16 txop;
2394
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002395 union {
2396 struct {
2397 /* Log exponent of max contention period: 0...15 */
2398 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002399
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002400 /* Log exponent of min contention period: 0...15 */
2401 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002402
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002403 /* Adaptive interframe spacing in units of 32us */
2404 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002405
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002406 /* TX queue to configure */
2407 __u8 txq;
2408 } ap;
2409 struct {
2410 /* Log exponent of max contention period: 0...15 */
2411 __u8 log_cw_max;
2412
2413 /* Log exponent of min contention period: 0...15 */
2414 __u8 log_cw_min;
2415
2416 /* Adaptive interframe spacing in units of 32us */
2417 __u8 aifs;
2418
2419 /* TX queue to configure */
2420 __u8 txq;
2421 } sta;
2422 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002423} __attribute__((packed));
2424
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002425#define MWL8K_SET_EDCA_CW 0x01
2426#define MWL8K_SET_EDCA_TXOP 0x02
2427#define MWL8K_SET_EDCA_AIFS 0x04
2428
2429#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2430 MWL8K_SET_EDCA_TXOP | \
2431 MWL8K_SET_EDCA_AIFS)
2432
2433static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002434mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2435 __u16 cw_min, __u16 cw_max,
2436 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002437{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002438 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002439 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002440 int rc;
2441
2442 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2443 if (cmd == NULL)
2444 return -ENOMEM;
2445
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002446 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2447 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002448 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2449 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002450 if (priv->ap_fw) {
2451 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2452 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2453 cmd->ap.aifs = aifs;
2454 cmd->ap.txq = qnum;
2455 } else {
2456 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2457 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2458 cmd->sta.aifs = aifs;
2459 cmd->sta.txq = qnum;
2460 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002461
2462 rc = mwl8k_post_cmd(hw, &cmd->header);
2463 kfree(cmd);
2464
2465 return rc;
2466}
2467
2468/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002469 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002470 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002471struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002472 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002473 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002474} __attribute__((packed));
2475
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002476static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002477{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002478 struct mwl8k_priv *priv = hw->priv;
2479 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002480 int rc;
2481
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002482 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2483 if (cmd == NULL)
2484 return -ENOMEM;
2485
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002486 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002487 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002488 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002489
2490 rc = mwl8k_post_cmd(hw, &cmd->header);
2491 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002492
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002493 if (!rc)
2494 priv->wmm_enabled = enable;
2495
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002496 return rc;
2497}
2498
2499/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002500 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002501 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002502struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002503 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002504 __le32 action;
2505 __u8 rx_antenna_map;
2506 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002507} __attribute__((packed));
2508
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002509static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002510{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002511 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002512 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002513
2514 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2515 if (cmd == NULL)
2516 return -ENOMEM;
2517
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002518 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002519 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002520 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2521 cmd->rx_antenna_map = rx;
2522 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002523
2524 rc = mwl8k_post_cmd(hw, &cmd->header);
2525 kfree(cmd);
2526
2527 return rc;
2528}
2529
2530/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002531 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002532 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002533struct mwl8k_cmd_use_fixed_rate_sta {
2534 struct mwl8k_cmd_pkt header;
2535 __le32 action;
2536 __le32 allow_rate_drop;
2537 __le32 num_rates;
2538 struct {
2539 __le32 is_ht_rate;
2540 __le32 enable_retry;
2541 __le32 rate;
2542 __le32 retry_count;
2543 } rate_entry[8];
2544 __le32 rate_type;
2545 __le32 reserved1;
2546 __le32 reserved2;
2547} __attribute__((packed));
2548
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002549#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002550#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002551
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002552static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002553{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002554 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002555 int rc;
2556
2557 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2558 if (cmd == NULL)
2559 return -ENOMEM;
2560
2561 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2562 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002563 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2564 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002565
2566 rc = mwl8k_post_cmd(hw, &cmd->header);
2567 kfree(cmd);
2568
2569 return rc;
2570}
2571
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002572/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002573 * CMD_USE_FIXED_RATE (AP version).
2574 */
2575struct mwl8k_cmd_use_fixed_rate_ap {
2576 struct mwl8k_cmd_pkt header;
2577 __le32 action;
2578 __le32 allow_rate_drop;
2579 __le32 num_rates;
2580 struct mwl8k_rate_entry_ap {
2581 __le32 is_ht_rate;
2582 __le32 enable_retry;
2583 __le32 rate;
2584 __le32 retry_count;
2585 } rate_entry[4];
2586 u8 multicast_rate;
2587 u8 multicast_rate_type;
2588 u8 management_rate;
2589} __attribute__((packed));
2590
2591static int
2592mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2593{
2594 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2595 int rc;
2596
2597 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2598 if (cmd == NULL)
2599 return -ENOMEM;
2600
2601 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2602 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2603 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2604 cmd->multicast_rate = mcast;
2605 cmd->management_rate = mgmt;
2606
2607 rc = mwl8k_post_cmd(hw, &cmd->header);
2608 kfree(cmd);
2609
2610 return rc;
2611}
2612
2613/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002614 * CMD_ENABLE_SNIFFER.
2615 */
2616struct mwl8k_cmd_enable_sniffer {
2617 struct mwl8k_cmd_pkt header;
2618 __le32 action;
2619} __attribute__((packed));
2620
2621static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2622{
2623 struct mwl8k_cmd_enable_sniffer *cmd;
2624 int rc;
2625
2626 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2627 if (cmd == NULL)
2628 return -ENOMEM;
2629
2630 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2631 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2632 cmd->action = cpu_to_le32(!!enable);
2633
2634 rc = mwl8k_post_cmd(hw, &cmd->header);
2635 kfree(cmd);
2636
2637 return rc;
2638}
2639
2640/*
2641 * CMD_SET_MAC_ADDR.
2642 */
2643struct mwl8k_cmd_set_mac_addr {
2644 struct mwl8k_cmd_pkt header;
2645 union {
2646 struct {
2647 __le16 mac_type;
2648 __u8 mac_addr[ETH_ALEN];
2649 } mbss;
2650 __u8 mac_addr[ETH_ALEN];
2651 };
2652} __attribute__((packed));
2653
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002654#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
2655#define MWL8K_MAC_TYPE_PRIMARY_AP 2
2656
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002657static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2658{
2659 struct mwl8k_priv *priv = hw->priv;
2660 struct mwl8k_cmd_set_mac_addr *cmd;
2661 int rc;
2662
2663 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2664 if (cmd == NULL)
2665 return -ENOMEM;
2666
2667 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2668 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2669 if (priv->ap_fw) {
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002670 cmd->mbss.mac_type = cpu_to_le16(MWL8K_MAC_TYPE_PRIMARY_AP);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002671 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2672 } else {
2673 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2674 }
2675
2676 rc = mwl8k_post_cmd(hw, &cmd->header);
2677 kfree(cmd);
2678
2679 return rc;
2680}
2681
2682/*
2683 * CMD_SET_RATEADAPT_MODE.
2684 */
2685struct mwl8k_cmd_set_rate_adapt_mode {
2686 struct mwl8k_cmd_pkt header;
2687 __le16 action;
2688 __le16 mode;
2689} __attribute__((packed));
2690
2691static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2692{
2693 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2694 int rc;
2695
2696 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2697 if (cmd == NULL)
2698 return -ENOMEM;
2699
2700 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2701 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2702 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2703 cmd->mode = cpu_to_le16(mode);
2704
2705 rc = mwl8k_post_cmd(hw, &cmd->header);
2706 kfree(cmd);
2707
2708 return rc;
2709}
2710
2711/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002712 * CMD_BSS_START.
2713 */
2714struct mwl8k_cmd_bss_start {
2715 struct mwl8k_cmd_pkt header;
2716 __le32 enable;
2717} __attribute__((packed));
2718
2719static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw, int enable)
2720{
2721 struct mwl8k_cmd_bss_start *cmd;
2722 int rc;
2723
2724 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2725 if (cmd == NULL)
2726 return -ENOMEM;
2727
2728 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
2729 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2730 cmd->enable = cpu_to_le32(enable);
2731
2732 rc = mwl8k_post_cmd(hw, &cmd->header);
2733 kfree(cmd);
2734
2735 return rc;
2736}
2737
2738/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002739 * CMD_SET_NEW_STN.
2740 */
2741struct mwl8k_cmd_set_new_stn {
2742 struct mwl8k_cmd_pkt header;
2743 __le16 aid;
2744 __u8 mac_addr[6];
2745 __le16 stn_id;
2746 __le16 action;
2747 __le16 rsvd;
2748 __le32 legacy_rates;
2749 __u8 ht_rates[4];
2750 __le16 cap_info;
2751 __le16 ht_capabilities_info;
2752 __u8 mac_ht_param_info;
2753 __u8 rev;
2754 __u8 control_channel;
2755 __u8 add_channel;
2756 __le16 op_mode;
2757 __le16 stbc;
2758 __u8 add_qos_info;
2759 __u8 is_qos_sta;
2760 __le32 fw_sta_ptr;
2761} __attribute__((packed));
2762
2763#define MWL8K_STA_ACTION_ADD 0
2764#define MWL8K_STA_ACTION_REMOVE 2
2765
2766static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
2767 struct ieee80211_vif *vif,
2768 struct ieee80211_sta *sta)
2769{
2770 struct mwl8k_cmd_set_new_stn *cmd;
2771 int rc;
2772
2773 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2774 if (cmd == NULL)
2775 return -ENOMEM;
2776
2777 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2778 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2779 cmd->aid = cpu_to_le16(sta->aid);
2780 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
2781 cmd->stn_id = cpu_to_le16(sta->aid);
2782 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
2783 cmd->legacy_rates = cpu_to_le32(sta->supp_rates[IEEE80211_BAND_2GHZ]);
2784 if (sta->ht_cap.ht_supported) {
2785 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
2786 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
2787 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
2788 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
2789 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
2790 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
2791 ((sta->ht_cap.ampdu_density & 7) << 2);
2792 cmd->is_qos_sta = 1;
2793 }
2794
2795 rc = mwl8k_post_cmd(hw, &cmd->header);
2796 kfree(cmd);
2797
2798 return rc;
2799}
2800
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002801static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
2802 struct ieee80211_vif *vif)
2803{
2804 struct mwl8k_cmd_set_new_stn *cmd;
2805 int rc;
2806
2807 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2808 if (cmd == NULL)
2809 return -ENOMEM;
2810
2811 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2812 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2813 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
2814
2815 rc = mwl8k_post_cmd(hw, &cmd->header);
2816 kfree(cmd);
2817
2818 return rc;
2819}
2820
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002821static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
2822 struct ieee80211_vif *vif, u8 *addr)
2823{
2824 struct mwl8k_cmd_set_new_stn *cmd;
2825 int rc;
2826
2827 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2828 if (cmd == NULL)
2829 return -ENOMEM;
2830
2831 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2832 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2833 memcpy(cmd->mac_addr, addr, ETH_ALEN);
2834 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
2835
2836 rc = mwl8k_post_cmd(hw, &cmd->header);
2837 kfree(cmd);
2838
2839 return rc;
2840}
2841
2842/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002843 * CMD_UPDATE_STADB.
2844 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002845struct ewc_ht_info {
2846 __le16 control1;
2847 __le16 control2;
2848 __le16 control3;
2849} __attribute__((packed));
2850
2851struct peer_capability_info {
2852 /* Peer type - AP vs. STA. */
2853 __u8 peer_type;
2854
2855 /* Basic 802.11 capabilities from assoc resp. */
2856 __le16 basic_caps;
2857
2858 /* Set if peer supports 802.11n high throughput (HT). */
2859 __u8 ht_support;
2860
2861 /* Valid if HT is supported. */
2862 __le16 ht_caps;
2863 __u8 extended_ht_caps;
2864 struct ewc_ht_info ewc_info;
2865
2866 /* Legacy rate table. Intersection of our rates and peer rates. */
2867 __u8 legacy_rates[12];
2868
2869 /* HT rate table. Intersection of our rates and peer rates. */
2870 __u8 ht_rates[16];
2871 __u8 pad[16];
2872
2873 /* If set, interoperability mode, no proprietary extensions. */
2874 __u8 interop;
2875 __u8 pad2;
2876 __u8 station_id;
2877 __le16 amsdu_enabled;
2878} __attribute__((packed));
2879
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002880struct mwl8k_cmd_update_stadb {
2881 struct mwl8k_cmd_pkt header;
2882
2883 /* See STADB_ACTION_TYPE */
2884 __le32 action;
2885
2886 /* Peer MAC address */
2887 __u8 peer_addr[ETH_ALEN];
2888
2889 __le32 reserved;
2890
2891 /* Peer info - valid during add/update. */
2892 struct peer_capability_info peer_info;
2893} __attribute__((packed));
2894
Lennert Buytenheka6804002010-01-04 21:55:42 +01002895#define MWL8K_STA_DB_MODIFY_ENTRY 1
2896#define MWL8K_STA_DB_DEL_ENTRY 2
2897
2898/* Peer Entry flags - used to define the type of the peer node */
2899#define MWL8K_PEER_TYPE_ACCESSPOINT 2
2900
2901static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002902 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002903 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002904{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002905 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01002906 struct peer_capability_info *p;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002907 int rc;
2908
2909 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2910 if (cmd == NULL)
2911 return -ENOMEM;
2912
2913 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2914 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01002915 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002916 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002917
Lennert Buytenheka6804002010-01-04 21:55:42 +01002918 p = &cmd->peer_info;
2919 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2920 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002921 p->ht_support = sta->ht_cap.ht_supported;
2922 p->ht_caps = sta->ht_cap.cap;
2923 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
2924 ((sta->ht_cap.ampdu_density & 7) << 2);
2925 legacy_rate_mask_to_array(p->legacy_rates,
2926 sta->supp_rates[IEEE80211_BAND_2GHZ]);
2927 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01002928 p->interop = 1;
2929 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002930
Lennert Buytenheka6804002010-01-04 21:55:42 +01002931 rc = mwl8k_post_cmd(hw, &cmd->header);
2932 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002933
Lennert Buytenheka6804002010-01-04 21:55:42 +01002934 return rc ? rc : p->station_id;
2935}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002936
Lennert Buytenheka6804002010-01-04 21:55:42 +01002937static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
2938 struct ieee80211_vif *vif, u8 *addr)
2939{
2940 struct mwl8k_cmd_update_stadb *cmd;
2941 int rc;
2942
2943 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2944 if (cmd == NULL)
2945 return -ENOMEM;
2946
2947 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2948 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2949 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
2950 memcpy(cmd->peer_addr, addr, ETH_ALEN);
2951
2952 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002953 kfree(cmd);
2954
2955 return rc;
2956}
2957
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002958
2959/*
2960 * Interrupt handling.
2961 */
2962static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2963{
2964 struct ieee80211_hw *hw = dev_id;
2965 struct mwl8k_priv *priv = hw->priv;
2966 u32 status;
2967
2968 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002969 if (!status)
2970 return IRQ_NONE;
2971
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01002972 if (status & MWL8K_A2H_INT_TX_DONE) {
2973 status &= ~MWL8K_A2H_INT_TX_DONE;
2974 tasklet_schedule(&priv->poll_tx_task);
2975 }
2976
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01002977 if (status & MWL8K_A2H_INT_RX_READY) {
2978 status &= ~MWL8K_A2H_INT_RX_READY;
2979 tasklet_schedule(&priv->poll_rx_task);
2980 }
2981
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01002982 if (status)
2983 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002984
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002985 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002986 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002987 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002988 }
2989
2990 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002991 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002992 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002993 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002994 }
2995
2996 return IRQ_HANDLED;
2997}
2998
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01002999static void mwl8k_tx_poll(unsigned long data)
3000{
3001 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3002 struct mwl8k_priv *priv = hw->priv;
3003 int limit;
3004 int i;
3005
3006 limit = 32;
3007
3008 spin_lock_bh(&priv->tx_lock);
3009
3010 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3011 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3012
3013 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3014 complete(priv->tx_wait);
3015 priv->tx_wait = NULL;
3016 }
3017
3018 spin_unlock_bh(&priv->tx_lock);
3019
3020 if (limit) {
3021 writel(~MWL8K_A2H_INT_TX_DONE,
3022 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3023 } else {
3024 tasklet_schedule(&priv->poll_tx_task);
3025 }
3026}
3027
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003028static void mwl8k_rx_poll(unsigned long data)
3029{
3030 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3031 struct mwl8k_priv *priv = hw->priv;
3032 int limit;
3033
3034 limit = 32;
3035 limit -= rxq_process(hw, 0, limit);
3036 limit -= rxq_refill(hw, 0, limit);
3037
3038 if (limit) {
3039 writel(~MWL8K_A2H_INT_RX_READY,
3040 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3041 } else {
3042 tasklet_schedule(&priv->poll_rx_task);
3043 }
3044}
3045
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003046
3047/*
3048 * Core driver operations.
3049 */
3050static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3051{
3052 struct mwl8k_priv *priv = hw->priv;
3053 int index = skb_get_queue_mapping(skb);
3054 int rc;
3055
3056 if (priv->current_channel == NULL) {
3057 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003058 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003059 dev_kfree_skb(skb);
3060 return NETDEV_TX_OK;
3061 }
3062
3063 rc = mwl8k_txq_xmit(hw, index, skb);
3064
3065 return rc;
3066}
3067
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003068static int mwl8k_start(struct ieee80211_hw *hw)
3069{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003070 struct mwl8k_priv *priv = hw->priv;
3071 int rc;
3072
Joe Perchesa0607fd2009-11-18 23:29:17 -08003073 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003074 IRQF_SHARED, MWL8K_NAME, hw);
3075 if (rc) {
3076 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003077 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003078 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003079 }
3080
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003081 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003082 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003083 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003084
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003085 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003086 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003087
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003088 rc = mwl8k_fw_lock(hw);
3089 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003090 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003091
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003092 if (!priv->ap_fw) {
3093 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003094 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003095
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003096 if (!rc)
3097 rc = mwl8k_cmd_set_pre_scan(hw);
3098
3099 if (!rc)
3100 rc = mwl8k_cmd_set_post_scan(hw,
3101 "\x00\x00\x00\x00\x00\x00");
3102 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003103
3104 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003105 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003106
3107 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003108 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003109
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003110 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003111 }
3112
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003113 if (rc) {
3114 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3115 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003116 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003117 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003118 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003119
3120 return rc;
3121}
3122
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003123static void mwl8k_stop(struct ieee80211_hw *hw)
3124{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003125 struct mwl8k_priv *priv = hw->priv;
3126 int i;
3127
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003128 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003129
3130 ieee80211_stop_queues(hw);
3131
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003132 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003133 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003134 free_irq(priv->pdev->irq, hw);
3135
3136 /* Stop finalize join worker */
3137 cancel_work_sync(&priv->finalize_join_worker);
3138 if (priv->beacon_skb != NULL)
3139 dev_kfree_skb(priv->beacon_skb);
3140
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003141 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003142 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003143 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003144
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003145 /* Return all skbs to mac80211 */
3146 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003147 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003148}
3149
3150static int mwl8k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003151 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003152{
3153 struct mwl8k_priv *priv = hw->priv;
3154 struct mwl8k_vif *mwl8k_vif;
3155
3156 /*
3157 * We only support one active interface at a time.
3158 */
3159 if (priv->vif != NULL)
3160 return -EBUSY;
3161
3162 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003163 * Reject interface creation if sniffer mode is active, as
3164 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003165 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003166 */
3167 if (priv->sniffer_enabled) {
3168 printk(KERN_INFO "%s: unable to create STA "
3169 "interface due to sniffer mode being enabled\n",
3170 wiphy_name(hw->wiphy));
3171 return -EINVAL;
3172 }
3173
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003174 /* Set the mac address. */
3175 mwl8k_cmd_set_mac_addr(hw, vif->addr);
3176
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003177 if (priv->ap_fw)
3178 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3179
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003180 /* Clean out driver private area */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003181 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003182 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
3183
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003184 /* Set Initial sequence number to zero */
3185 mwl8k_vif->seqno = 0;
3186
Johannes Berg1ed32e42009-12-23 13:15:45 +01003187 priv->vif = vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003188 priv->current_channel = NULL;
3189
3190 return 0;
3191}
3192
3193static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003194 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003195{
3196 struct mwl8k_priv *priv = hw->priv;
3197
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003198 if (priv->ap_fw)
3199 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3200
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003201 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003202
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003203 priv->vif = NULL;
3204}
3205
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003206static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003207{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003208 struct ieee80211_conf *conf = &hw->conf;
3209 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003210 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003211
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003212 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003213 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003214 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003215 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003216 }
3217
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003218 rc = mwl8k_fw_lock(hw);
3219 if (rc)
3220 return rc;
3221
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003222 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003223 if (rc)
3224 goto out;
3225
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003226 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003227 if (rc)
3228 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003229
3230 priv->current_channel = conf->channel;
3231
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003232 if (conf->power_level > 18)
3233 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003234 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003235 if (rc)
3236 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003237
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003238 if (priv->ap_fw) {
3239 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3240 if (!rc)
3241 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3242 } else {
3243 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3244 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003245
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003246out:
3247 mwl8k_fw_unlock(hw);
3248
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003249 return rc;
3250}
3251
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003252static void
3253mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3254 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003255{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003256 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003257 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003258 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003259 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003260
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003261 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003262 return;
3263
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003264 /*
3265 * No need to capture a beacon if we're no longer associated.
3266 */
3267 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3268 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003269
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003270 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003271 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003272 */
3273 ap_legacy_rates = 0;
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003274 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003275 struct ieee80211_sta *ap;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003276 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003277
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003278 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003279 if (ap == NULL) {
3280 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003281 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003282 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003283
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003284 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003285 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003286
3287 rcu_read_unlock();
3288 }
3289
3290 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003291 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003292 if (rc)
3293 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003294
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003295 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003296 if (rc)
3297 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003298 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003299
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003300 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003301 rc = mwl8k_set_radio_preamble(hw,
3302 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003303 if (rc)
3304 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003305 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003306
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003307 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003308 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003309 if (rc)
3310 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003311 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003312
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003313 if (((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) ||
3314 (changed & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT))) {
3315 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003316 if (rc)
3317 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003318 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003319
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003320 if (vif->bss_conf.assoc &&
3321 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003322 /*
3323 * Finalize the join. Tell rx handler to process
3324 * next beacon from our BSSID.
3325 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003326 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003327 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003328 }
3329
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003330out:
3331 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003332}
3333
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003334static void
3335mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3336 struct ieee80211_bss_conf *info, u32 changed)
3337{
3338 int rc;
3339
3340 if (mwl8k_fw_lock(hw))
3341 return;
3342
3343 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3344 rc = mwl8k_set_radio_preamble(hw,
3345 vif->bss_conf.use_short_preamble);
3346 if (rc)
3347 goto out;
3348 }
3349
3350 if (changed & BSS_CHANGED_BASIC_RATES) {
3351 int idx;
3352 int rate;
3353
3354 /*
3355 * Use lowest supported basic rate for multicasts
3356 * and management frames (such as probe responses --
3357 * beacons will always go out at 1 Mb/s).
3358 */
3359 idx = ffs(vif->bss_conf.basic_rates);
3360 rate = idx ? mwl8k_rates[idx - 1].hw_value : 2;
3361
3362 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
3363 }
3364
3365 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
3366 struct sk_buff *skb;
3367
3368 skb = ieee80211_beacon_get(hw, vif);
3369 if (skb != NULL) {
3370 mwl8k_cmd_set_beacon(hw, skb->data, skb->len);
3371 kfree_skb(skb);
3372 }
3373 }
3374
3375 if (changed & BSS_CHANGED_BEACON_ENABLED)
3376 mwl8k_cmd_bss_start(hw, info->enable_beacon);
3377
3378out:
3379 mwl8k_fw_unlock(hw);
3380}
3381
3382static void
3383mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3384 struct ieee80211_bss_conf *info, u32 changed)
3385{
3386 struct mwl8k_priv *priv = hw->priv;
3387
3388 if (!priv->ap_fw)
3389 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
3390 else
3391 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
3392}
3393
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003394static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3395 int mc_count, struct dev_addr_list *mclist)
3396{
3397 struct mwl8k_cmd_pkt *cmd;
3398
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003399 /*
3400 * Synthesize and return a command packet that programs the
3401 * hardware multicast address filter. At this point we don't
3402 * know whether FIF_ALLMULTI is being requested, but if it is,
3403 * we'll end up throwing this packet away and creating a new
3404 * one in mwl8k_configure_filter().
3405 */
3406 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003407
3408 return (unsigned long)cmd;
3409}
3410
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003411static int
3412mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3413 unsigned int changed_flags,
3414 unsigned int *total_flags)
3415{
3416 struct mwl8k_priv *priv = hw->priv;
3417
3418 /*
3419 * Hardware sniffer mode is mutually exclusive with STA
3420 * operation, so refuse to enable sniffer mode if a STA
3421 * interface is active.
3422 */
3423 if (priv->vif != NULL) {
3424 if (net_ratelimit())
3425 printk(KERN_INFO "%s: not enabling sniffer "
3426 "mode because STA interface is active\n",
3427 wiphy_name(hw->wiphy));
3428 return 0;
3429 }
3430
3431 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003432 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003433 return 0;
3434 priv->sniffer_enabled = true;
3435 }
3436
3437 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3438 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3439 FIF_OTHER_BSS;
3440
3441 return 1;
3442}
3443
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003444static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3445 unsigned int changed_flags,
3446 unsigned int *total_flags,
3447 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003448{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003449 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003450 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3451
3452 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003453 * AP firmware doesn't allow fine-grained control over
3454 * the receive filter.
3455 */
3456 if (priv->ap_fw) {
3457 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3458 kfree(cmd);
3459 return;
3460 }
3461
3462 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003463 * Enable hardware sniffer mode if FIF_CONTROL or
3464 * FIF_OTHER_BSS is requested.
3465 */
3466 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3467 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3468 kfree(cmd);
3469 return;
3470 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003471
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003472 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003473 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003474
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003475 if (mwl8k_fw_lock(hw)) {
3476 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003477 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003478 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003479
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003480 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003481 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003482 priv->sniffer_enabled = false;
3483 }
3484
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003485 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003486 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3487 /*
3488 * Disable the BSS filter.
3489 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003490 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003491 } else {
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003492 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003493
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003494 /*
3495 * Enable the BSS filter.
3496 *
3497 * If there is an active STA interface, use that
3498 * interface's BSSID, otherwise use a dummy one
3499 * (where the OUI part needs to be nonzero for
3500 * the BSSID to be accepted by POST_SCAN).
3501 */
3502 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003503 if (priv->vif != NULL)
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003504 bssid = priv->vif->bss_conf.bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003505
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003506 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003507 }
3508 }
3509
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003510 /*
3511 * If FIF_ALLMULTI is being requested, throw away the command
3512 * packet that ->prepare_multicast() built and replace it with
3513 * a command packet that enables reception of all multicast
3514 * packets.
3515 */
3516 if (*total_flags & FIF_ALLMULTI) {
3517 kfree(cmd);
3518 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3519 }
3520
3521 if (cmd != NULL) {
3522 mwl8k_post_cmd(hw, cmd);
3523 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003524 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003525
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003526 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003527}
3528
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003529static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3530{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003531 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003532}
3533
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003534struct mwl8k_sta_notify_item
3535{
3536 struct list_head list;
3537 struct ieee80211_vif *vif;
3538 enum sta_notify_cmd cmd;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003539 struct ieee80211_sta sta;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003540};
3541
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003542static void
3543mwl8k_do_sta_notify(struct ieee80211_hw *hw, struct mwl8k_sta_notify_item *s)
3544{
3545 struct mwl8k_priv *priv = hw->priv;
3546
3547 /*
3548 * STA firmware uses UPDATE_STADB, AP firmware uses SET_NEW_STN.
3549 */
3550 if (!priv->ap_fw && s->cmd == STA_NOTIFY_ADD) {
3551 int rc;
3552
3553 rc = mwl8k_cmd_update_stadb_add(hw, s->vif, &s->sta);
3554 if (rc >= 0) {
3555 struct ieee80211_sta *sta;
3556
3557 rcu_read_lock();
3558 sta = ieee80211_find_sta(s->vif, s->sta.addr);
3559 if (sta != NULL)
3560 MWL8K_STA(sta)->peer_id = rc;
3561 rcu_read_unlock();
3562 }
3563 } else if (!priv->ap_fw && s->cmd == STA_NOTIFY_REMOVE) {
3564 mwl8k_cmd_update_stadb_del(hw, s->vif, s->sta.addr);
3565 } else if (priv->ap_fw && s->cmd == STA_NOTIFY_ADD) {
3566 mwl8k_cmd_set_new_stn_add(hw, s->vif, &s->sta);
3567 } else if (priv->ap_fw && s->cmd == STA_NOTIFY_REMOVE) {
3568 mwl8k_cmd_set_new_stn_del(hw, s->vif, s->sta.addr);
3569 }
3570}
3571
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003572static void mwl8k_sta_notify_worker(struct work_struct *work)
3573{
3574 struct mwl8k_priv *priv =
3575 container_of(work, struct mwl8k_priv, sta_notify_worker);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003576 struct ieee80211_hw *hw = priv->hw;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003577
3578 spin_lock_bh(&priv->sta_notify_list_lock);
3579 while (!list_empty(&priv->sta_notify_list)) {
3580 struct mwl8k_sta_notify_item *s;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003581
3582 s = list_entry(priv->sta_notify_list.next,
3583 struct mwl8k_sta_notify_item, list);
3584 list_del(&s->list);
3585
3586 spin_unlock_bh(&priv->sta_notify_list_lock);
3587
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003588 mwl8k_do_sta_notify(hw, s);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003589 kfree(s);
3590
3591 spin_lock_bh(&priv->sta_notify_list_lock);
3592 }
3593 spin_unlock_bh(&priv->sta_notify_list_lock);
3594}
3595
3596static void
3597mwl8k_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3598 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3599{
3600 struct mwl8k_priv *priv = hw->priv;
3601 struct mwl8k_sta_notify_item *s;
3602
3603 if (cmd != STA_NOTIFY_ADD && cmd != STA_NOTIFY_REMOVE)
3604 return;
3605
3606 s = kmalloc(sizeof(*s), GFP_ATOMIC);
3607 if (s != NULL) {
3608 s->vif = vif;
3609 s->cmd = cmd;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003610 s->sta = *sta;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003611
3612 spin_lock(&priv->sta_notify_list_lock);
3613 list_add_tail(&s->list, &priv->sta_notify_list);
3614 spin_unlock(&priv->sta_notify_list_lock);
3615
3616 ieee80211_queue_work(hw, &priv->sta_notify_worker);
3617 }
3618}
3619
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003620static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3621 const struct ieee80211_tx_queue_params *params)
3622{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003623 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003624 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003625
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003626 rc = mwl8k_fw_lock(hw);
3627 if (!rc) {
3628 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003629 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003630
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003631 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003632 rc = mwl8k_cmd_set_edca_params(hw, queue,
3633 params->cw_min,
3634 params->cw_max,
3635 params->aifs,
3636 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003637
3638 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003639 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003640
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003641 return rc;
3642}
3643
3644static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3645 struct ieee80211_tx_queue_stats *stats)
3646{
3647 struct mwl8k_priv *priv = hw->priv;
3648 struct mwl8k_tx_queue *txq;
3649 int index;
3650
3651 spin_lock_bh(&priv->tx_lock);
3652 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3653 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003654 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003655 sizeof(struct ieee80211_tx_queue_stats));
3656 }
3657 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003658
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003659 return 0;
3660}
3661
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003662static int mwl8k_get_stats(struct ieee80211_hw *hw,
3663 struct ieee80211_low_level_stats *stats)
3664{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003665 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003666}
3667
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003668static int
3669mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3670 enum ieee80211_ampdu_mlme_action action,
3671 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3672{
3673 switch (action) {
3674 case IEEE80211_AMPDU_RX_START:
3675 case IEEE80211_AMPDU_RX_STOP:
3676 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3677 return -ENOTSUPP;
3678 return 0;
3679 default:
3680 return -ENOTSUPP;
3681 }
3682}
3683
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003684static const struct ieee80211_ops mwl8k_ops = {
3685 .tx = mwl8k_tx,
3686 .start = mwl8k_start,
3687 .stop = mwl8k_stop,
3688 .add_interface = mwl8k_add_interface,
3689 .remove_interface = mwl8k_remove_interface,
3690 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003691 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003692 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003693 .configure_filter = mwl8k_configure_filter,
3694 .set_rts_threshold = mwl8k_set_rts_threshold,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003695 .sta_notify = mwl8k_sta_notify,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003696 .conf_tx = mwl8k_conf_tx,
3697 .get_tx_stats = mwl8k_get_tx_stats,
3698 .get_stats = mwl8k_get_stats,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003699 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003700};
3701
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003702static void mwl8k_finalize_join_worker(struct work_struct *work)
3703{
3704 struct mwl8k_priv *priv =
3705 container_of(work, struct mwl8k_priv, finalize_join_worker);
3706 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003707
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003708 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len,
3709 priv->vif->bss_conf.dtim_period);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003710 dev_kfree_skb(skb);
3711
3712 priv->beacon_skb = NULL;
3713}
3714
John W. Linvillebcb628d2009-11-06 16:40:16 -05003715enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003716 MWL8363 = 0,
3717 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003718 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003719};
3720
John W. Linvillebcb628d2009-11-06 16:40:16 -05003721static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003722 [MWL8363] = {
3723 .part_name = "88w8363",
3724 .helper_image = "mwl8k/helper_8363.fw",
3725 .fw_image = "mwl8k/fmimage_8363.fw",
3726 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003727 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003728 .part_name = "88w8687",
3729 .helper_image = "mwl8k/helper_8687.fw",
3730 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003731 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003732 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003733 .part_name = "88w8366",
3734 .helper_image = "mwl8k/helper_8366.fw",
3735 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003736 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003737 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003738};
3739
3740static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003741 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3742 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003743 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3744 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3745 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3746 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003747};
3748MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3749
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003750static int __devinit mwl8k_probe(struct pci_dev *pdev,
3751 const struct pci_device_id *id)
3752{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003753 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003754 struct ieee80211_hw *hw;
3755 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003756 int rc;
3757 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003758
3759 if (!printed_version) {
3760 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3761 printed_version = 1;
3762 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003763
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003764
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003765 rc = pci_enable_device(pdev);
3766 if (rc) {
3767 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3768 MWL8K_NAME);
3769 return rc;
3770 }
3771
3772 rc = pci_request_regions(pdev, MWL8K_NAME);
3773 if (rc) {
3774 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3775 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003776 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003777 }
3778
3779 pci_set_master(pdev);
3780
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003781
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003782 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3783 if (hw == NULL) {
3784 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3785 rc = -ENOMEM;
3786 goto err_free_reg;
3787 }
3788
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003789 SET_IEEE80211_DEV(hw, &pdev->dev);
3790 pci_set_drvdata(pdev, hw);
3791
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003792 priv = hw->priv;
3793 priv->hw = hw;
3794 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003795 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003796
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003797
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003798 priv->sram = pci_iomap(pdev, 0, 0x10000);
3799 if (priv->sram == NULL) {
3800 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003801 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003802 goto err_iounmap;
3803 }
3804
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003805 /*
3806 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3807 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3808 */
3809 priv->regs = pci_iomap(pdev, 1, 0x10000);
3810 if (priv->regs == NULL) {
3811 priv->regs = pci_iomap(pdev, 2, 0x10000);
3812 if (priv->regs == NULL) {
3813 printk(KERN_ERR "%s: Cannot map device registers\n",
3814 wiphy_name(hw->wiphy));
3815 goto err_iounmap;
3816 }
3817 }
3818
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003819
3820 /* Reset firmware and hardware */
3821 mwl8k_hw_reset(priv);
3822
3823 /* Ask userland hotplug daemon for the device firmware */
3824 rc = mwl8k_request_firmware(priv);
3825 if (rc) {
3826 printk(KERN_ERR "%s: Firmware files not found\n",
3827 wiphy_name(hw->wiphy));
3828 goto err_stop_firmware;
3829 }
3830
3831 /* Load firmware into hardware */
3832 rc = mwl8k_load_firmware(hw);
3833 if (rc) {
3834 printk(KERN_ERR "%s: Cannot start firmware\n",
3835 wiphy_name(hw->wiphy));
3836 goto err_stop_firmware;
3837 }
3838
3839 /* Reclaim memory once firmware is successfully loaded */
3840 mwl8k_release_firmware(priv);
3841
3842
Lennert Buytenhek91942232010-01-04 21:53:54 +01003843 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003844 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003845 if (priv->rxd_ops == NULL) {
3846 printk(KERN_ERR "%s: Driver does not have AP "
3847 "firmware image support for this hardware\n",
3848 wiphy_name(hw->wiphy));
3849 goto err_stop_firmware;
3850 }
3851 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003852 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003853 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003854
3855 priv->sniffer_enabled = false;
3856 priv->wmm_enabled = false;
3857 priv->pending_tx_pkts = 0;
3858
3859
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003860 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3861 priv->band.band = IEEE80211_BAND_2GHZ;
3862 priv->band.channels = priv->channels;
3863 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3864 priv->band.bitrates = priv->rates;
3865 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3866 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3867
3868 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3869 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3870
3871 /*
3872 * Extra headroom is the size of the required DMA header
3873 * minus the size of the smallest 802.11 frame (CTS frame).
3874 */
3875 hw->extra_tx_headroom =
3876 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3877
3878 hw->channel_change_time = 10;
3879
3880 hw->queues = MWL8K_TX_QUEUES;
3881
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003882 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003883 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003884 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003885 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003886 priv->vif = NULL;
3887
3888 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003889 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003890 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003891
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003892 /* Station database handling */
3893 INIT_WORK(&priv->sta_notify_worker, mwl8k_sta_notify_worker);
3894 spin_lock_init(&priv->sta_notify_list_lock);
3895 INIT_LIST_HEAD(&priv->sta_notify_list);
3896
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003897 /* Finalize join worker */
3898 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3899
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003900 /* TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003901 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
3902 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003903 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
3904 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003905
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003906 /* Power management cookie */
3907 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3908 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003909 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003910
3911 rc = mwl8k_rxq_init(hw, 0);
3912 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003913 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003914 rxq_refill(hw, 0, INT_MAX);
3915
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003916 mutex_init(&priv->fw_mutex);
3917 priv->fw_mutex_owner = NULL;
3918 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003919 priv->hostcmd_wait = NULL;
3920
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003921 spin_lock_init(&priv->tx_lock);
3922
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003923 priv->tx_wait = NULL;
3924
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003925 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3926 rc = mwl8k_txq_init(hw, i);
3927 if (rc)
3928 goto err_free_queues;
3929 }
3930
3931 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003932 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003933 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003934 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003935 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3936
Joe Perchesa0607fd2009-11-18 23:29:17 -08003937 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003938 IRQF_SHARED, MWL8K_NAME, hw);
3939 if (rc) {
3940 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003941 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003942 goto err_free_queues;
3943 }
3944
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003945 /*
3946 * Temporarily enable interrupts. Initial firmware host
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003947 * commands use interrupts and avoid polling. Disable
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003948 * interrupts when done.
3949 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003950 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003951
3952 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003953 if (priv->ap_fw) {
3954 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3955 if (!rc)
3956 rc = mwl8k_cmd_set_hw_spec(hw);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003957
3958 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_AP);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003959 } else {
3960 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003961
3962 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02003963 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003964 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003965 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3966 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003967 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003968 }
3969
3970 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003971 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003972 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003973 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003974 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003975 }
3976
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003977 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003978 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003979 if (rc) {
3980 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3981 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003982 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003983 }
3984
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003985 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003986 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003987 free_irq(priv->pdev->irq, hw);
3988
3989 rc = ieee80211_register_hw(hw);
3990 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003991 printk(KERN_ERR "%s: Cannot register device\n",
3992 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01003993 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003994 }
3995
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003996 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003997 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003998 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003999 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004000 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4001 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004002
4003 return 0;
4004
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004005err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004006 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004007 free_irq(priv->pdev->irq, hw);
4008
4009err_free_queues:
4010 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4011 mwl8k_txq_deinit(hw, i);
4012 mwl8k_rxq_deinit(hw, 0);
4013
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004014err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004015 if (priv->cookie != NULL)
4016 pci_free_consistent(priv->pdev, 4,
4017 priv->cookie, priv->cookie_dma);
4018
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004019err_stop_firmware:
4020 mwl8k_hw_reset(priv);
4021 mwl8k_release_firmware(priv);
4022
4023err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004024 if (priv->regs != NULL)
4025 pci_iounmap(pdev, priv->regs);
4026
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004027 if (priv->sram != NULL)
4028 pci_iounmap(pdev, priv->sram);
4029
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004030 pci_set_drvdata(pdev, NULL);
4031 ieee80211_free_hw(hw);
4032
4033err_free_reg:
4034 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004035
4036err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004037 pci_disable_device(pdev);
4038
4039 return rc;
4040}
4041
Joerg Albert230f7af2009-04-18 02:10:45 +02004042static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004043{
4044 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4045}
4046
Joerg Albert230f7af2009-04-18 02:10:45 +02004047static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004048{
4049 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4050 struct mwl8k_priv *priv;
4051 int i;
4052
4053 if (hw == NULL)
4054 return;
4055 priv = hw->priv;
4056
4057 ieee80211_stop_queues(hw);
4058
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004059 ieee80211_unregister_hw(hw);
4060
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004061 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004062 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004063 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004064
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004065 /* Stop hardware */
4066 mwl8k_hw_reset(priv);
4067
4068 /* Return all skbs to mac80211 */
4069 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004070 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004071
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004072 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4073 mwl8k_txq_deinit(hw, i);
4074
4075 mwl8k_rxq_deinit(hw, 0);
4076
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004077 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004078
4079 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004080 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004081 pci_set_drvdata(pdev, NULL);
4082 ieee80211_free_hw(hw);
4083 pci_release_regions(pdev);
4084 pci_disable_device(pdev);
4085}
4086
4087static struct pci_driver mwl8k_driver = {
4088 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004089 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004090 .probe = mwl8k_probe,
4091 .remove = __devexit_p(mwl8k_remove),
4092 .shutdown = __devexit_p(mwl8k_shutdown),
4093};
4094
4095static int __init mwl8k_init(void)
4096{
4097 return pci_register_driver(&mwl8k_driver);
4098}
4099
4100static void __exit mwl8k_exit(void)
4101{
4102 pci_unregister_driver(&mwl8k_driver);
4103}
4104
4105module_init(mwl8k_init);
4106module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004107
4108MODULE_DESCRIPTION(MWL8K_DESC);
4109MODULE_VERSION(MWL8K_VERSION);
4110MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
4111MODULE_LICENSE("GPL");