blob: 3dbdea9048330c536ccc79db95c421174fea11b2 [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 Buytenheka66098d2009-03-10 10:13:33 +0100188 /* XXX need to convert this to handle multiple interfaces */
189 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200190 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100191 struct sk_buff *beacon_skb;
192
193 /*
194 * This FJ worker has to be global as it is scheduled from the
195 * RX handler. At this point we don't know which interface it
196 * belongs to until the list of bssids waiting to complete join
197 * is checked.
198 */
199 struct work_struct finalize_join_worker;
200
201 /* Tasklet to reclaim TX descriptors and buffers after tx */
202 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100203};
204
205/* Per interface specific private data */
206struct mwl8k_vif {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +0100207 /* Local MAC address. */
208 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100209
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +0100210 /* BSSID of AP. */
211 u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100212
Lennert Buytenhek55489b62009-11-30 18:31:33 +0100213 /* Index into station database. Returned by UPDATE_STADB. */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100214 u8 peer_id;
215
216 /* Non AMPDU sequence number assigned by driver */
217 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218};
219
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200220#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100221
222static const struct ieee80211_channel mwl8k_channels[] = {
223 { .center_freq = 2412, .hw_value = 1, },
224 { .center_freq = 2417, .hw_value = 2, },
225 { .center_freq = 2422, .hw_value = 3, },
226 { .center_freq = 2427, .hw_value = 4, },
227 { .center_freq = 2432, .hw_value = 5, },
228 { .center_freq = 2437, .hw_value = 6, },
229 { .center_freq = 2442, .hw_value = 7, },
230 { .center_freq = 2447, .hw_value = 8, },
231 { .center_freq = 2452, .hw_value = 9, },
232 { .center_freq = 2457, .hw_value = 10, },
233 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100234 { .center_freq = 2467, .hw_value = 12, },
235 { .center_freq = 2472, .hw_value = 13, },
236 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100237};
238
239static const struct ieee80211_rate mwl8k_rates[] = {
240 { .bitrate = 10, .hw_value = 2, },
241 { .bitrate = 20, .hw_value = 4, },
242 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200243 { .bitrate = 110, .hw_value = 22, },
244 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100245 { .bitrate = 60, .hw_value = 12, },
246 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100247 { .bitrate = 120, .hw_value = 24, },
248 { .bitrate = 180, .hw_value = 36, },
249 { .bitrate = 240, .hw_value = 48, },
250 { .bitrate = 360, .hw_value = 72, },
251 { .bitrate = 480, .hw_value = 96, },
252 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100253 { .bitrate = 720, .hw_value = 144, },
254};
255
256static const u8 mwl8k_rateids[12] = {
257 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108,
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100258};
259
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100260/* Set or get info from Firmware */
261#define MWL8K_CMD_SET 0x0001
262#define MWL8K_CMD_GET 0x0000
263
264/* Firmware command codes */
265#define MWL8K_CMD_CODE_DNLD 0x0001
266#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200267#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100268#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
269#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200270#define MWL8K_CMD_RADIO_CONTROL 0x001c
271#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200272#define MWL8K_CMD_RF_ANTENNA 0x0020
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
288#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100289
290static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
291{
292#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
293 snprintf(buf, bufsize, "%s", #x);\
294 return buf;\
295 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200296 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100297 MWL8K_CMDNAME(CODE_DNLD);
298 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200299 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100300 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
301 MWL8K_CMDNAME(GET_STAT);
302 MWL8K_CMDNAME(RADIO_CONTROL);
303 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200304 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100305 MWL8K_CMDNAME(SET_PRE_SCAN);
306 MWL8K_CMDNAME(SET_POST_SCAN);
307 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100308 MWL8K_CMDNAME(SET_AID);
309 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200310 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100311 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200312 MWL8K_CMDNAME(SET_SLOT);
313 MWL8K_CMDNAME(SET_EDCA_PARAMS);
314 MWL8K_CMDNAME(SET_WMM_MODE);
315 MWL8K_CMDNAME(MIMO_CONFIG);
316 MWL8K_CMDNAME(USE_FIXED_RATE);
317 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200318 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200319 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
320 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100321 default:
322 snprintf(buf, bufsize, "0x%x", cmd);
323 }
324#undef MWL8K_CMDNAME
325
326 return buf;
327}
328
329/* Hardware and firmware reset */
330static void mwl8k_hw_reset(struct mwl8k_priv *priv)
331{
332 iowrite32(MWL8K_H2A_INT_RESET,
333 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
334 iowrite32(MWL8K_H2A_INT_RESET,
335 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
336 msleep(20);
337}
338
339/* Release fw image */
340static void mwl8k_release_fw(struct firmware **fw)
341{
342 if (*fw == NULL)
343 return;
344 release_firmware(*fw);
345 *fw = NULL;
346}
347
348static void mwl8k_release_firmware(struct mwl8k_priv *priv)
349{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100350 mwl8k_release_fw(&priv->fw_ucode);
351 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100352}
353
354/* Request fw image */
355static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200356 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100357{
358 /* release current image */
359 if (*fw != NULL)
360 mwl8k_release_fw(fw);
361
362 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200363 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100364}
365
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200366static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100367{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200368 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100369 int rc;
370
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200371 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100372 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200373 if (rc) {
374 printk(KERN_ERR "%s: Error requesting helper "
375 "firmware file %s\n", pci_name(priv->pdev),
376 di->helper_image);
377 return rc;
378 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100379 }
380
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100381 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100382 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200383 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200384 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100385 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100386 return rc;
387 }
388
389 return 0;
390}
391
Ben Hutchings7e75b942009-11-07 22:00:57 +0000392MODULE_FIRMWARE("mwl8k/helper_8687.fw");
393MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
394
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100395struct mwl8k_cmd_pkt {
396 __le16 code;
397 __le16 length;
398 __le16 seq_num;
399 __le16 result;
400 char payload[0];
401} __attribute__((packed));
402
403/*
404 * Firmware loading.
405 */
406static int
407mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
408{
409 void __iomem *regs = priv->regs;
410 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100411 int loops;
412
413 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
414 if (pci_dma_mapping_error(priv->pdev, dma_addr))
415 return -ENOMEM;
416
417 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
418 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
419 iowrite32(MWL8K_H2A_INT_DOORBELL,
420 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
421 iowrite32(MWL8K_H2A_INT_DUMMY,
422 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
423
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100424 loops = 1000;
425 do {
426 u32 int_code;
427
428 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
429 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
430 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100431 break;
432 }
433
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200434 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100435 udelay(1);
436 } while (--loops);
437
438 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
439
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200440 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100441}
442
443static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
444 const u8 *data, size_t length)
445{
446 struct mwl8k_cmd_pkt *cmd;
447 int done;
448 int rc = 0;
449
450 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
451 if (cmd == NULL)
452 return -ENOMEM;
453
454 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
455 cmd->seq_num = 0;
456 cmd->result = 0;
457
458 done = 0;
459 while (length) {
460 int block_size = length > 256 ? 256 : length;
461
462 memcpy(cmd->payload, data + done, block_size);
463 cmd->length = cpu_to_le16(block_size);
464
465 rc = mwl8k_send_fw_load_cmd(priv, cmd,
466 sizeof(*cmd) + block_size);
467 if (rc)
468 break;
469
470 done += block_size;
471 length -= block_size;
472 }
473
474 if (!rc) {
475 cmd->length = 0;
476 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
477 }
478
479 kfree(cmd);
480
481 return rc;
482}
483
484static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
485 const u8 *data, size_t length)
486{
487 unsigned char *buffer;
488 int may_continue, rc = 0;
489 u32 done, prev_block_size;
490
491 buffer = kmalloc(1024, GFP_KERNEL);
492 if (buffer == NULL)
493 return -ENOMEM;
494
495 done = 0;
496 prev_block_size = 0;
497 may_continue = 1000;
498 while (may_continue > 0) {
499 u32 block_size;
500
501 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
502 if (block_size & 1) {
503 block_size &= ~1;
504 may_continue--;
505 } else {
506 done += prev_block_size;
507 length -= prev_block_size;
508 }
509
510 if (block_size > 1024 || block_size > length) {
511 rc = -EOVERFLOW;
512 break;
513 }
514
515 if (length == 0) {
516 rc = 0;
517 break;
518 }
519
520 if (block_size == 0) {
521 rc = -EPROTO;
522 may_continue--;
523 udelay(1);
524 continue;
525 }
526
527 prev_block_size = block_size;
528 memcpy(buffer, data + done, block_size);
529
530 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
531 if (rc)
532 break;
533 }
534
535 if (!rc && length != 0)
536 rc = -EREMOTEIO;
537
538 kfree(buffer);
539
540 return rc;
541}
542
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200543static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100544{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200545 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100546 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200547 int rc;
548 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100549
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200550 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100551 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100552
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200553 if (helper == NULL) {
554 printk(KERN_ERR "%s: helper image needed but none "
555 "given\n", pci_name(priv->pdev));
556 return -EINVAL;
557 }
558
559 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100560 if (rc) {
561 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200562 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100563 return rc;
564 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100565 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100566
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200567 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100568 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200569 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100570 }
571
572 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200573 printk(KERN_ERR "%s: unable to load firmware image\n",
574 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100575 return rc;
576 }
577
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100578 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100579
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100580 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100581 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200582 u32 ready_code;
583
584 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
585 if (ready_code == MWL8K_FWAP_READY) {
586 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100587 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200588 } else if (ready_code == MWL8K_FWSTA_READY) {
589 priv->ap_fw = 0;
590 break;
591 }
592
593 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100594 udelay(1);
595 } while (--loops);
596
597 return loops ? 0 : -ETIMEDOUT;
598}
599
600
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100601/* DMA header used by firmware and hardware. */
602struct mwl8k_dma_data {
603 __le16 fwlen;
604 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100605 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100606} __attribute__((packed));
607
608/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100609static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100610{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100611 struct mwl8k_dma_data *tr;
612 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100613
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100614 tr = (struct mwl8k_dma_data *)skb->data;
615 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
616
617 if (hdrlen != sizeof(tr->wh)) {
618 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
619 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
620 *((__le16 *)(tr->data - 2)) = qos;
621 } else {
622 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
623 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100624 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100625
626 if (hdrlen != sizeof(*tr))
627 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100628}
629
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200630static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100631{
632 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100633 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100634 struct mwl8k_dma_data *tr;
635
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100636 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100637 * Add a firmware DMA header; the firmware requires that we
638 * present a 2-byte payload length followed by a 4-address
639 * header (without QoS field), followed (optionally) by any
640 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100641 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100642 wh = (struct ieee80211_hdr *)skb->data;
643
644 hdrlen = ieee80211_hdrlen(wh->frame_control);
645 if (hdrlen != sizeof(*tr))
646 skb_push(skb, sizeof(*tr) - hdrlen);
647
648 if (ieee80211_is_data_qos(wh->frame_control))
649 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100650
651 tr = (struct mwl8k_dma_data *)skb->data;
652 if (wh != &tr->wh)
653 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100654 if (hdrlen != sizeof(tr->wh))
655 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100656
657 /*
658 * Firmware length is the length of the fully formed "802.11
659 * payload". That is, everything except for the 802.11 header.
660 * This includes all crypto material including the MIC.
661 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100662 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100663}
664
665
666/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100667 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200668 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100669struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200670 __le16 pkt_len;
671 __u8 sq2;
672 __u8 rate;
673 __le32 pkt_phys_addr;
674 __le32 next_rxd_phys_addr;
675 __le16 qos_control;
676 __le16 htsig2;
677 __le32 hw_rssi_info;
678 __le32 hw_noise_floor_info;
679 __u8 noise_floor;
680 __u8 pad0[3];
681 __u8 rssi;
682 __u8 rx_status;
683 __u8 channel;
684 __u8 rx_ctrl;
685} __attribute__((packed));
686
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100687#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
688#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
689#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100690
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100691#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200692
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100693static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200694{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100695 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200696
697 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100698 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200699}
700
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100701static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200702{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100703 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200704
705 rxd->pkt_len = cpu_to_le16(len);
706 rxd->pkt_phys_addr = cpu_to_le32(addr);
707 wmb();
708 rxd->rx_ctrl = 0;
709}
710
711static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100712mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
713 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200714{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100715 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200716
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100717 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200718 return -1;
719 rmb();
720
721 memset(status, 0, sizeof(*status));
722
723 status->signal = -rxd->rssi;
724 status->noise = -rxd->noise_floor;
725
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100726 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200727 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100728 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100729 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100730 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200731 } else {
732 int i;
733
734 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
735 if (mwl8k_rates[i].hw_value == rxd->rate) {
736 status->rate_idx = i;
737 break;
738 }
739 }
740 }
741
742 status->band = IEEE80211_BAND_2GHZ;
743 status->freq = ieee80211_channel_to_frequency(rxd->channel);
744
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100745 *qos = rxd->qos_control;
746
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200747 return le16_to_cpu(rxd->pkt_len);
748}
749
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100750static struct rxd_ops rxd_8366_ap_ops = {
751 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
752 .rxd_init = mwl8k_rxd_8366_ap_init,
753 .rxd_refill = mwl8k_rxd_8366_ap_refill,
754 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200755};
756
757/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100758 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100759 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100760struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100761 __le16 pkt_len;
762 __u8 link_quality;
763 __u8 noise_level;
764 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200765 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100766 __le16 qos_control;
767 __le16 rate_info;
768 __le32 pad0[4];
769 __u8 rssi;
770 __u8 channel;
771 __le16 pad1;
772 __u8 rx_ctrl;
773 __u8 rx_status;
774 __u8 pad2[2];
775} __attribute__((packed));
776
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100777#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
778#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
779#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
780#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
781#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
782#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200783
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100784#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200785
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100786static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200787{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100788 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200789
790 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100791 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200792}
793
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100794static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200795{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100796 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200797
798 rxd->pkt_len = cpu_to_le16(len);
799 rxd->pkt_phys_addr = cpu_to_le32(addr);
800 wmb();
801 rxd->rx_ctrl = 0;
802}
803
804static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100805mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100806 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200807{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100808 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200809 u16 rate_info;
810
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100811 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200812 return -1;
813 rmb();
814
815 rate_info = le16_to_cpu(rxd->rate_info);
816
817 memset(status, 0, sizeof(*status));
818
819 status->signal = -rxd->rssi;
820 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100821 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
822 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200823
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100824 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200825 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100826 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200827 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100828 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200829 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100830 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200831 status->flag |= RX_FLAG_HT;
832
833 status->band = IEEE80211_BAND_2GHZ;
834 status->freq = ieee80211_channel_to_frequency(rxd->channel);
835
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100836 *qos = rxd->qos_control;
837
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200838 return le16_to_cpu(rxd->pkt_len);
839}
840
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100841static struct rxd_ops rxd_sta_ops = {
842 .rxd_size = sizeof(struct mwl8k_rxd_sta),
843 .rxd_init = mwl8k_rxd_sta_init,
844 .rxd_refill = mwl8k_rxd_sta_refill,
845 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200846};
847
848
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100849#define MWL8K_RX_DESCS 256
850#define MWL8K_RX_MAXSZ 3800
851
852static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
853{
854 struct mwl8k_priv *priv = hw->priv;
855 struct mwl8k_rx_queue *rxq = priv->rxq + index;
856 int size;
857 int i;
858
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200859 rxq->rxd_count = 0;
860 rxq->head = 0;
861 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100862
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200863 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100864
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200865 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
866 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100867 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200868 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100869 return -ENOMEM;
870 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200871 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100872
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200873 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
874 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100875 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200876 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200877 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100878 return -ENOMEM;
879 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200880 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100881
882 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200883 int desc_size;
884 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100885 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200886 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100887
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200888 desc_size = priv->rxd_ops->rxd_size;
889 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100890
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200891 nexti = i + 1;
892 if (nexti == MWL8K_RX_DESCS)
893 nexti = 0;
894 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
895
896 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100897 }
898
899 return 0;
900}
901
902static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
903{
904 struct mwl8k_priv *priv = hw->priv;
905 struct mwl8k_rx_queue *rxq = priv->rxq + index;
906 int refilled;
907
908 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200909 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100910 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200911 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100912 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200913 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100914
915 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
916 if (skb == NULL)
917 break;
918
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200919 addr = pci_map_single(priv->pdev, skb->data,
920 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100921
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200922 rxq->rxd_count++;
923 rx = rxq->tail++;
924 if (rxq->tail == MWL8K_RX_DESCS)
925 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200926 rxq->buf[rx].skb = skb;
927 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200928
929 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
930 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100931
932 refilled++;
933 }
934
935 return refilled;
936}
937
938/* Must be called only when the card's reception is completely halted */
939static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
940{
941 struct mwl8k_priv *priv = hw->priv;
942 struct mwl8k_rx_queue *rxq = priv->rxq + index;
943 int i;
944
945 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200946 if (rxq->buf[i].skb != NULL) {
947 pci_unmap_single(priv->pdev,
948 pci_unmap_addr(&rxq->buf[i], dma),
949 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
950 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100951
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200952 kfree_skb(rxq->buf[i].skb);
953 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100954 }
955 }
956
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200957 kfree(rxq->buf);
958 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100959
960 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200961 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200962 rxq->rxd, rxq->rxd_dma);
963 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100964}
965
966
967/*
968 * Scan a list of BSSIDs to process for finalize join.
969 * Allows for extension to process multiple BSSIDs.
970 */
971static inline int
972mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
973{
974 return priv->capture_beacon &&
975 ieee80211_is_beacon(wh->frame_control) &&
976 !compare_ether_addr(wh->addr3, priv->capture_bssid);
977}
978
Lennert Buytenhek37797522009-10-22 20:19:45 +0200979static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
980 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100981{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200982 struct mwl8k_priv *priv = hw->priv;
983
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100984 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200985 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100986
987 /*
988 * Use GFP_ATOMIC as rxq_process is called from
989 * the primary interrupt handler, memory allocation call
990 * must not sleep.
991 */
992 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
993 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200994 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100995}
996
997static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
998{
999 struct mwl8k_priv *priv = hw->priv;
1000 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1001 int processed;
1002
1003 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001004 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001005 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001006 void *rxd;
1007 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001008 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001009 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001010
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001011 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001012 if (skb == NULL)
1013 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001014
1015 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1016
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001017 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001018 if (pkt_len < 0)
1019 break;
1020
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001021 rxq->buf[rxq->head].skb = NULL;
1022
1023 pci_unmap_single(priv->pdev,
1024 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1025 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1026 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001027
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001028 rxq->head++;
1029 if (rxq->head == MWL8K_RX_DESCS)
1030 rxq->head = 0;
1031
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001032 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001033
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001034 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001035 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001036
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001037 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001038 * Check for a pending join operation. Save a
1039 * copy of the beacon and schedule a tasklet to
1040 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001041 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001042 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001043 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001044
Johannes Bergf1d58c22009-06-17 13:13:00 +02001045 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1046 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001047
1048 processed++;
1049 }
1050
1051 return processed;
1052}
1053
1054
1055/*
1056 * Packet transmission.
1057 */
1058
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001059#define MWL8K_TXD_STATUS_OK 0x00000001
1060#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1061#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1062#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001063#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001064
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001065#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1066#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1067#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1068#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1069#define MWL8K_QOS_EOSP 0x0010
1070
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001071struct mwl8k_tx_desc {
1072 __le32 status;
1073 __u8 data_rate;
1074 __u8 tx_priority;
1075 __le16 qos_control;
1076 __le32 pkt_phys_addr;
1077 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001078 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001079 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001080 __le32 reserved;
1081 __le16 rate_info;
1082 __u8 peer_id;
1083 __u8 tx_frag_cnt;
1084} __attribute__((packed));
1085
1086#define MWL8K_TX_DESCS 128
1087
1088static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1089{
1090 struct mwl8k_priv *priv = hw->priv;
1091 struct mwl8k_tx_queue *txq = priv->txq + index;
1092 int size;
1093 int i;
1094
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001095 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1096 txq->stats.limit = MWL8K_TX_DESCS;
1097 txq->head = 0;
1098 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001099
1100 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1101
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001102 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1103 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001104 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001105 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001106 return -ENOMEM;
1107 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001108 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001109
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001110 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1111 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001112 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001113 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001114 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001115 return -ENOMEM;
1116 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001117 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001118
1119 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1120 struct mwl8k_tx_desc *tx_desc;
1121 int nexti;
1122
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001123 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001124 nexti = (i + 1) % MWL8K_TX_DESCS;
1125
1126 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001127 tx_desc->next_txd_phys_addr =
1128 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001129 }
1130
1131 return 0;
1132}
1133
1134static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1135{
1136 iowrite32(MWL8K_H2A_INT_PPA_READY,
1137 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1138 iowrite32(MWL8K_H2A_INT_DUMMY,
1139 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1140 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1141}
1142
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001143static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001144{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001145 struct mwl8k_priv *priv = hw->priv;
1146 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001147
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001148 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1149 struct mwl8k_tx_queue *txq = priv->txq + i;
1150 int fw_owned = 0;
1151 int drv_owned = 0;
1152 int unused = 0;
1153 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001154
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001155 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001156 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1157 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001158
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001159 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001160 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001161 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001162 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001163 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001164
1165 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001166 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001168
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001169 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1170 "fw_owned=%d drv_owned=%d unused=%d\n",
1171 wiphy_name(hw->wiphy), i,
1172 txq->stats.len, txq->head, txq->tail,
1173 fw_owned, drv_owned, unused);
1174 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001175}
1176
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001177/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001178 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001179 */
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001180#define MWL8K_TX_WAIT_TIMEOUT_MS 1000
1181
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001182static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001183{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001184 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001185 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001186 int retry;
1187 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001188
1189 might_sleep();
1190
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001191 /*
1192 * The TX queues are stopped at this point, so this test
1193 * doesn't need to take ->tx_lock.
1194 */
1195 if (!priv->pending_tx_pkts)
1196 return 0;
1197
1198 retry = 0;
1199 rc = 0;
1200
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001201 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001202 priv->tx_wait = &tx_wait;
1203 while (!rc) {
1204 int oldcount;
1205 unsigned long timeout;
1206
1207 oldcount = priv->pending_tx_pkts;
1208
1209 spin_unlock_bh(&priv->tx_lock);
1210 timeout = wait_for_completion_timeout(&tx_wait,
1211 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1212 spin_lock_bh(&priv->tx_lock);
1213
1214 if (timeout) {
1215 WARN_ON(priv->pending_tx_pkts);
1216 if (retry) {
1217 printk(KERN_NOTICE "%s: tx rings drained\n",
1218 wiphy_name(hw->wiphy));
1219 }
1220 break;
1221 }
1222
1223 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001224 printk(KERN_NOTICE "%s: waiting for tx rings "
1225 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001226 wiphy_name(hw->wiphy), oldcount,
1227 priv->pending_tx_pkts);
1228 retry = 1;
1229 continue;
1230 }
1231
1232 priv->tx_wait = NULL;
1233
1234 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1235 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1236 mwl8k_dump_tx_rings(hw);
1237
1238 rc = -ETIMEDOUT;
1239 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001240 spin_unlock_bh(&priv->tx_lock);
1241
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001242 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001243}
1244
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001245#define MWL8K_TXD_SUCCESS(status) \
1246 ((status) & (MWL8K_TXD_STATUS_OK | \
1247 MWL8K_TXD_STATUS_OK_RETRY | \
1248 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001249
1250static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1251{
1252 struct mwl8k_priv *priv = hw->priv;
1253 struct mwl8k_tx_queue *txq = priv->txq + index;
1254 int wake = 0;
1255
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001256 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001257 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001258 struct mwl8k_tx_desc *tx_desc;
1259 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001260 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001261 struct sk_buff *skb;
1262 struct ieee80211_tx_info *info;
1263 u32 status;
1264
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001265 tx = txq->head;
1266 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001267
1268 status = le32_to_cpu(tx_desc->status);
1269
1270 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1271 if (!force)
1272 break;
1273 tx_desc->status &=
1274 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1275 }
1276
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001277 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1278 BUG_ON(txq->stats.len == 0);
1279 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001280 priv->pending_tx_pkts--;
1281
1282 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001283 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001284 skb = txq->skb[tx];
1285 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001286
1287 BUG_ON(skb == NULL);
1288 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1289
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001290 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001291
1292 /* Mark descriptor as unused */
1293 tx_desc->pkt_phys_addr = 0;
1294 tx_desc->pkt_len = 0;
1295
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001296 info = IEEE80211_SKB_CB(skb);
1297 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001298 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300
1301 ieee80211_tx_status_irqsafe(hw, skb);
1302
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001303 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001304 }
1305
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001306 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001307 ieee80211_wake_queue(hw, index);
1308}
1309
1310/* must be called only when the card's transmit is completely halted */
1311static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1312{
1313 struct mwl8k_priv *priv = hw->priv;
1314 struct mwl8k_tx_queue *txq = priv->txq + index;
1315
1316 mwl8k_txq_reclaim(hw, index, 1);
1317
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001318 kfree(txq->skb);
1319 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001320
1321 pci_free_consistent(priv->pdev,
1322 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001323 txq->txd, txq->txd_dma);
1324 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001325}
1326
1327static int
1328mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1329{
1330 struct mwl8k_priv *priv = hw->priv;
1331 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001332 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001333 struct ieee80211_hdr *wh;
1334 struct mwl8k_tx_queue *txq;
1335 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001336 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001337 u32 txstatus;
1338 u8 txdatarate;
1339 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001341 wh = (struct ieee80211_hdr *)skb->data;
1342 if (ieee80211_is_data_qos(wh->frame_control))
1343 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1344 else
1345 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001346
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001347 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001348 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001349
1350 tx_info = IEEE80211_SKB_CB(skb);
1351 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001352
1353 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1354 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001355
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001356 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1357 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1358 mwl8k_vif->seqno = seqno++ % 4096;
1359 }
1360
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001361 /* Setup firmware control bit fields for each frame type. */
1362 txstatus = 0;
1363 txdatarate = 0;
1364 if (ieee80211_is_mgmt(wh->frame_control) ||
1365 ieee80211_is_ctl(wh->frame_control)) {
1366 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001367 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001368 } else if (ieee80211_is_data(wh->frame_control)) {
1369 txdatarate = 1;
1370 if (is_multicast_ether_addr(wh->addr1))
1371 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1372
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001373 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001374 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001375 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001376 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001377 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001378 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001379
1380 dma = pci_map_single(priv->pdev, skb->data,
1381 skb->len, PCI_DMA_TODEVICE);
1382
1383 if (pci_dma_mapping_error(priv->pdev, dma)) {
1384 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001385 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001386 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001387 return NETDEV_TX_OK;
1388 }
1389
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001390 spin_lock_bh(&priv->tx_lock);
1391
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001392 txq = priv->txq + index;
1393
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001394 BUG_ON(txq->skb[txq->tail] != NULL);
1395 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001396
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001397 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001398 tx->data_rate = txdatarate;
1399 tx->tx_priority = index;
1400 tx->qos_control = cpu_to_le16(qos);
1401 tx->pkt_phys_addr = cpu_to_le32(dma);
1402 tx->pkt_len = cpu_to_le16(skb->len);
1403 tx->rate_info = 0;
1404 tx->peer_id = mwl8k_vif->peer_id;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001405 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001406 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1407
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001408 txq->stats.count++;
1409 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001410 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001411
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001412 txq->tail++;
1413 if (txq->tail == MWL8K_TX_DESCS)
1414 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001415
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001416 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001417 ieee80211_stop_queue(hw, index);
1418
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001419 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001420
1421 spin_unlock_bh(&priv->tx_lock);
1422
1423 return NETDEV_TX_OK;
1424}
1425
1426
1427/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001428 * Firmware access.
1429 *
1430 * We have the following requirements for issuing firmware commands:
1431 * - Some commands require that the packet transmit path is idle when
1432 * the command is issued. (For simplicity, we'll just quiesce the
1433 * transmit path for every command.)
1434 * - There are certain sequences of commands that need to be issued to
1435 * the hardware sequentially, with no other intervening commands.
1436 *
1437 * This leads to an implementation of a "firmware lock" as a mutex that
1438 * can be taken recursively, and which is taken by both the low-level
1439 * command submission function (mwl8k_post_cmd) as well as any users of
1440 * that function that require issuing of an atomic sequence of commands,
1441 * and quiesces the transmit path whenever it's taken.
1442 */
1443static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1444{
1445 struct mwl8k_priv *priv = hw->priv;
1446
1447 if (priv->fw_mutex_owner != current) {
1448 int rc;
1449
1450 mutex_lock(&priv->fw_mutex);
1451 ieee80211_stop_queues(hw);
1452
1453 rc = mwl8k_tx_wait_empty(hw);
1454 if (rc) {
1455 ieee80211_wake_queues(hw);
1456 mutex_unlock(&priv->fw_mutex);
1457
1458 return rc;
1459 }
1460
1461 priv->fw_mutex_owner = current;
1462 }
1463
1464 priv->fw_mutex_depth++;
1465
1466 return 0;
1467}
1468
1469static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1470{
1471 struct mwl8k_priv *priv = hw->priv;
1472
1473 if (!--priv->fw_mutex_depth) {
1474 ieee80211_wake_queues(hw);
1475 priv->fw_mutex_owner = NULL;
1476 mutex_unlock(&priv->fw_mutex);
1477 }
1478}
1479
1480
1481/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001482 * Command processing.
1483 */
1484
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001485/* Timeout firmware commands after 10s */
1486#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001487
1488static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1489{
1490 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1491 struct mwl8k_priv *priv = hw->priv;
1492 void __iomem *regs = priv->regs;
1493 dma_addr_t dma_addr;
1494 unsigned int dma_size;
1495 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496 unsigned long timeout = 0;
1497 u8 buf[32];
1498
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001499 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001500 dma_size = le16_to_cpu(cmd->length);
1501 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1502 PCI_DMA_BIDIRECTIONAL);
1503 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1504 return -ENOMEM;
1505
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001506 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001507 if (rc) {
1508 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1509 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001510 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001511 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001512
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001513 priv->hostcmd_wait = &cmd_wait;
1514 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1515 iowrite32(MWL8K_H2A_INT_DOORBELL,
1516 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1517 iowrite32(MWL8K_H2A_INT_DUMMY,
1518 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001519
1520 timeout = wait_for_completion_timeout(&cmd_wait,
1521 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1522
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001523 priv->hostcmd_wait = NULL;
1524
1525 mwl8k_fw_unlock(hw);
1526
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001527 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1528 PCI_DMA_BIDIRECTIONAL);
1529
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001530 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001531 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001532 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001533 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1534 MWL8K_CMD_TIMEOUT_MS);
1535 rc = -ETIMEDOUT;
1536 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001537 int ms;
1538
1539 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1540
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001541 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001542 if (rc)
1543 printk(KERN_ERR "%s: Command %s error 0x%x\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)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001546 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001547 else if (ms > 2000)
1548 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1549 wiphy_name(hw->wiphy),
1550 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1551 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001552 }
1553
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001554 return rc;
1555}
1556
1557/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001558 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001559 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001560struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001561 struct mwl8k_cmd_pkt header;
1562 __u8 hw_rev;
1563 __u8 host_interface;
1564 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001565 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001566 __le16 region_code;
1567 __le32 fw_rev;
1568 __le32 ps_cookie;
1569 __le32 caps;
1570 __u8 mcs_bitmap[16];
1571 __le32 rx_queue_ptr;
1572 __le32 num_tx_queues;
1573 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1574 __le32 caps2;
1575 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001576 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001577} __attribute__((packed));
1578
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001579static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001580{
1581 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001582 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001583 int rc;
1584 int i;
1585
1586 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1587 if (cmd == NULL)
1588 return -ENOMEM;
1589
1590 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1591 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1592
1593 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1594 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001595 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001596 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001597 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001598 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001599 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001600 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001601
1602 rc = mwl8k_post_cmd(hw, &cmd->header);
1603
1604 if (!rc) {
1605 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1606 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001607 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001608 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001609 }
1610
1611 kfree(cmd);
1612 return rc;
1613}
1614
1615/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001616 * CMD_GET_HW_SPEC (AP version).
1617 */
1618struct mwl8k_cmd_get_hw_spec_ap {
1619 struct mwl8k_cmd_pkt header;
1620 __u8 hw_rev;
1621 __u8 host_interface;
1622 __le16 num_wcb;
1623 __le16 num_mcaddrs;
1624 __u8 perm_addr[ETH_ALEN];
1625 __le16 region_code;
1626 __le16 num_antenna;
1627 __le32 fw_rev;
1628 __le32 wcbbase0;
1629 __le32 rxwrptr;
1630 __le32 rxrdptr;
1631 __le32 ps_cookie;
1632 __le32 wcbbase1;
1633 __le32 wcbbase2;
1634 __le32 wcbbase3;
1635} __attribute__((packed));
1636
1637static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1638{
1639 struct mwl8k_priv *priv = hw->priv;
1640 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1641 int rc;
1642
1643 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1644 if (cmd == NULL)
1645 return -ENOMEM;
1646
1647 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1648 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1649
1650 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1651 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1652
1653 rc = mwl8k_post_cmd(hw, &cmd->header);
1654
1655 if (!rc) {
1656 int off;
1657
1658 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1659 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1660 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1661 priv->hw_rev = cmd->hw_rev;
1662
1663 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1664 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1665
1666 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1667 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1668
1669 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1670 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1671
1672 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1673 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1674
1675 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1676 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1677
1678 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1679 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1680 }
1681
1682 kfree(cmd);
1683 return rc;
1684}
1685
1686/*
1687 * CMD_SET_HW_SPEC.
1688 */
1689struct mwl8k_cmd_set_hw_spec {
1690 struct mwl8k_cmd_pkt header;
1691 __u8 hw_rev;
1692 __u8 host_interface;
1693 __le16 num_mcaddrs;
1694 __u8 perm_addr[ETH_ALEN];
1695 __le16 region_code;
1696 __le32 fw_rev;
1697 __le32 ps_cookie;
1698 __le32 caps;
1699 __le32 rx_queue_ptr;
1700 __le32 num_tx_queues;
1701 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1702 __le32 flags;
1703 __le32 num_tx_desc_per_queue;
1704 __le32 total_rxd;
1705} __attribute__((packed));
1706
1707#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1708
1709static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1710{
1711 struct mwl8k_priv *priv = hw->priv;
1712 struct mwl8k_cmd_set_hw_spec *cmd;
1713 int rc;
1714 int i;
1715
1716 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1717 if (cmd == NULL)
1718 return -ENOMEM;
1719
1720 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1721 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1722
1723 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1724 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1725 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1726 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1727 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1728 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1729 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1730 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1731
1732 rc = mwl8k_post_cmd(hw, &cmd->header);
1733 kfree(cmd);
1734
1735 return rc;
1736}
1737
1738/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001739 * CMD_MAC_MULTICAST_ADR.
1740 */
1741struct mwl8k_cmd_mac_multicast_adr {
1742 struct mwl8k_cmd_pkt header;
1743 __le16 action;
1744 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001745 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001746};
1747
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001748#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1749#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1750#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1751#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001752
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001753static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001754__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001755 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001756{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001757 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001758 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001759 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001760
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001761 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001762 allmulti = 1;
1763 mc_count = 0;
1764 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001765
1766 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1767
1768 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001769 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001770 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001771
1772 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1773 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001774 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1775 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001776
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001777 if (allmulti) {
1778 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1779 } else if (mc_count) {
1780 int i;
1781
1782 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1783 cmd->numaddr = cpu_to_le16(mc_count);
1784 for (i = 0; i < mc_count && mclist; i++) {
1785 if (mclist->da_addrlen != ETH_ALEN) {
1786 kfree(cmd);
1787 return NULL;
1788 }
1789 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1790 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001791 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001792 }
1793
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001794 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001795}
1796
1797/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001798 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001799 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001800struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001801 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001802 __le32 stats[64];
1803} __attribute__((packed));
1804
1805#define MWL8K_STAT_ACK_FAILURE 9
1806#define MWL8K_STAT_RTS_FAILURE 12
1807#define MWL8K_STAT_FCS_ERROR 24
1808#define MWL8K_STAT_RTS_SUCCESS 11
1809
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001810static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1811 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001812{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001813 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001814 int rc;
1815
1816 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1817 if (cmd == NULL)
1818 return -ENOMEM;
1819
1820 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1821 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001822
1823 rc = mwl8k_post_cmd(hw, &cmd->header);
1824 if (!rc) {
1825 stats->dot11ACKFailureCount =
1826 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1827 stats->dot11RTSFailureCount =
1828 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1829 stats->dot11FCSErrorCount =
1830 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1831 stats->dot11RTSSuccessCount =
1832 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1833 }
1834 kfree(cmd);
1835
1836 return rc;
1837}
1838
1839/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001840 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001841 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001842struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001843 struct mwl8k_cmd_pkt header;
1844 __le16 action;
1845 __le16 control;
1846 __le16 radio_on;
1847} __attribute__((packed));
1848
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001849static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001850mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001851{
1852 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001853 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001854 int rc;
1855
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001856 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001857 return 0;
1858
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001859 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1860 if (cmd == NULL)
1861 return -ENOMEM;
1862
1863 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1864 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1865 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001866 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001867 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1868
1869 rc = mwl8k_post_cmd(hw, &cmd->header);
1870 kfree(cmd);
1871
1872 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001873 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001874
1875 return rc;
1876}
1877
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001878static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001879{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001880 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001881}
1882
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001883static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001884{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001885 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001886}
1887
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001888static int
1889mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1890{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001891 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001892
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001893 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001894
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001895 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001896}
1897
1898/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001899 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001900 */
1901#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1902
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001903struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001904 struct mwl8k_cmd_pkt header;
1905 __le16 action;
1906 __le16 support_level;
1907 __le16 current_level;
1908 __le16 reserved;
1909 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1910} __attribute__((packed));
1911
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001912static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001913{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001914 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001915 int rc;
1916
1917 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1918 if (cmd == NULL)
1919 return -ENOMEM;
1920
1921 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1922 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1923 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1924 cmd->support_level = cpu_to_le16(dBm);
1925
1926 rc = mwl8k_post_cmd(hw, &cmd->header);
1927 kfree(cmd);
1928
1929 return rc;
1930}
1931
1932/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02001933 * CMD_RF_ANTENNA.
1934 */
1935struct mwl8k_cmd_rf_antenna {
1936 struct mwl8k_cmd_pkt header;
1937 __le16 antenna;
1938 __le16 mode;
1939} __attribute__((packed));
1940
1941#define MWL8K_RF_ANTENNA_RX 1
1942#define MWL8K_RF_ANTENNA_TX 2
1943
1944static int
1945mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
1946{
1947 struct mwl8k_cmd_rf_antenna *cmd;
1948 int rc;
1949
1950 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1951 if (cmd == NULL)
1952 return -ENOMEM;
1953
1954 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
1955 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1956 cmd->antenna = cpu_to_le16(antenna);
1957 cmd->mode = cpu_to_le16(mask);
1958
1959 rc = mwl8k_post_cmd(hw, &cmd->header);
1960 kfree(cmd);
1961
1962 return rc;
1963}
1964
1965/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001966 * CMD_SET_PRE_SCAN.
1967 */
1968struct mwl8k_cmd_set_pre_scan {
1969 struct mwl8k_cmd_pkt header;
1970} __attribute__((packed));
1971
1972static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
1973{
1974 struct mwl8k_cmd_set_pre_scan *cmd;
1975 int rc;
1976
1977 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1978 if (cmd == NULL)
1979 return -ENOMEM;
1980
1981 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
1982 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1983
1984 rc = mwl8k_post_cmd(hw, &cmd->header);
1985 kfree(cmd);
1986
1987 return rc;
1988}
1989
1990/*
1991 * CMD_SET_POST_SCAN.
1992 */
1993struct mwl8k_cmd_set_post_scan {
1994 struct mwl8k_cmd_pkt header;
1995 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001996 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001997} __attribute__((packed));
1998
1999static int
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002000mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002001{
2002 struct mwl8k_cmd_set_post_scan *cmd;
2003 int rc;
2004
2005 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2006 if (cmd == NULL)
2007 return -ENOMEM;
2008
2009 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2010 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2011 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002012 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002013
2014 rc = mwl8k_post_cmd(hw, &cmd->header);
2015 kfree(cmd);
2016
2017 return rc;
2018}
2019
2020/*
2021 * CMD_SET_RF_CHANNEL.
2022 */
2023struct mwl8k_cmd_set_rf_channel {
2024 struct mwl8k_cmd_pkt header;
2025 __le16 action;
2026 __u8 current_channel;
2027 __le32 channel_flags;
2028} __attribute__((packed));
2029
2030static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2031 struct ieee80211_channel *channel)
2032{
2033 struct mwl8k_cmd_set_rf_channel *cmd;
2034 int rc;
2035
2036 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2037 if (cmd == NULL)
2038 return -ENOMEM;
2039
2040 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2041 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2042 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2043 cmd->current_channel = channel->hw_value;
2044 if (channel->band == IEEE80211_BAND_2GHZ)
2045 cmd->channel_flags = cpu_to_le32(0x00000081);
2046 else
2047 cmd->channel_flags = cpu_to_le32(0x00000000);
2048
2049 rc = mwl8k_post_cmd(hw, &cmd->header);
2050 kfree(cmd);
2051
2052 return rc;
2053}
2054
2055/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002056 * CMD_SET_AID.
2057 */
2058#define MWL8K_FRAME_PROT_DISABLED 0x00
2059#define MWL8K_FRAME_PROT_11G 0x07
2060#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2061#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2062
2063struct mwl8k_cmd_update_set_aid {
2064 struct mwl8k_cmd_pkt header;
2065 __le16 aid;
2066
2067 /* AP's MAC address (BSSID) */
2068 __u8 bssid[ETH_ALEN];
2069 __le16 protection_mode;
2070 __u8 supp_rates[14];
2071} __attribute__((packed));
2072
2073static int
2074mwl8k_cmd_set_aid(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2075{
2076 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002077 struct mwl8k_cmd_update_set_aid *cmd;
2078 u16 prot_mode;
2079 int rc;
2080
2081 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2082 if (cmd == NULL)
2083 return -ENOMEM;
2084
2085 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2086 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002087 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002088
2089 memcpy(cmd->bssid, mv_vif->bssid, ETH_ALEN);
2090
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002091 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002092 prot_mode = MWL8K_FRAME_PROT_11G;
2093 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002094 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002095 IEEE80211_HT_OP_MODE_PROTECTION) {
2096 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2097 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2098 break;
2099 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2100 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2101 break;
2102 default:
2103 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2104 break;
2105 }
2106 }
2107 cmd->protection_mode = cpu_to_le16(prot_mode);
2108
2109 memcpy(cmd->supp_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2110
2111 rc = mwl8k_post_cmd(hw, &cmd->header);
2112 kfree(cmd);
2113
2114 return rc;
2115}
2116
2117/*
2118 * CMD_SET_RATE.
2119 */
2120struct mwl8k_cmd_set_rate {
2121 struct mwl8k_cmd_pkt header;
2122 __u8 legacy_rates[14];
2123
2124 /* Bitmap for supported MCS codes. */
2125 __u8 mcs_set[16];
2126 __u8 reserved[16];
2127} __attribute__((packed));
2128
2129static int
2130mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2131{
2132 struct mwl8k_cmd_set_rate *cmd;
2133 int rc;
2134
2135 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2136 if (cmd == NULL)
2137 return -ENOMEM;
2138
2139 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2140 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2141 memcpy(cmd->legacy_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2142
2143 rc = mwl8k_post_cmd(hw, &cmd->header);
2144 kfree(cmd);
2145
2146 return rc;
2147}
2148
2149/*
2150 * CMD_FINALIZE_JOIN.
2151 */
2152#define MWL8K_FJ_BEACON_MAXLEN 128
2153
2154struct mwl8k_cmd_finalize_join {
2155 struct mwl8k_cmd_pkt header;
2156 __le32 sleep_interval; /* Number of beacon periods to sleep */
2157 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2158} __attribute__((packed));
2159
2160static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2161 int framelen, int dtim)
2162{
2163 struct mwl8k_cmd_finalize_join *cmd;
2164 struct ieee80211_mgmt *payload = frame;
2165 int payload_len;
2166 int rc;
2167
2168 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2169 if (cmd == NULL)
2170 return -ENOMEM;
2171
2172 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2173 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2174 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2175
2176 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2177 if (payload_len < 0)
2178 payload_len = 0;
2179 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2180 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2181
2182 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2183
2184 rc = mwl8k_post_cmd(hw, &cmd->header);
2185 kfree(cmd);
2186
2187 return rc;
2188}
2189
2190/*
2191 * CMD_SET_RTS_THRESHOLD.
2192 */
2193struct mwl8k_cmd_set_rts_threshold {
2194 struct mwl8k_cmd_pkt header;
2195 __le16 action;
2196 __le16 threshold;
2197} __attribute__((packed));
2198
2199static int mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw,
2200 u16 action, u16 threshold)
2201{
2202 struct mwl8k_cmd_set_rts_threshold *cmd;
2203 int rc;
2204
2205 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2206 if (cmd == NULL)
2207 return -ENOMEM;
2208
2209 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2210 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2211 cmd->action = cpu_to_le16(action);
2212 cmd->threshold = cpu_to_le16(threshold);
2213
2214 rc = mwl8k_post_cmd(hw, &cmd->header);
2215 kfree(cmd);
2216
2217 return rc;
2218}
2219
2220/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002221 * CMD_SET_SLOT.
2222 */
2223struct mwl8k_cmd_set_slot {
2224 struct mwl8k_cmd_pkt header;
2225 __le16 action;
2226 __u8 short_slot;
2227} __attribute__((packed));
2228
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002229static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002230{
2231 struct mwl8k_cmd_set_slot *cmd;
2232 int rc;
2233
2234 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2235 if (cmd == NULL)
2236 return -ENOMEM;
2237
2238 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2239 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2240 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002241 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002242
2243 rc = mwl8k_post_cmd(hw, &cmd->header);
2244 kfree(cmd);
2245
2246 return rc;
2247}
2248
2249/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002250 * CMD_SET_EDCA_PARAMS.
2251 */
2252struct mwl8k_cmd_set_edca_params {
2253 struct mwl8k_cmd_pkt header;
2254
2255 /* See MWL8K_SET_EDCA_XXX below */
2256 __le16 action;
2257
2258 /* TX opportunity in units of 32 us */
2259 __le16 txop;
2260
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002261 union {
2262 struct {
2263 /* Log exponent of max contention period: 0...15 */
2264 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002265
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002266 /* Log exponent of min contention period: 0...15 */
2267 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002268
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002269 /* Adaptive interframe spacing in units of 32us */
2270 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002271
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002272 /* TX queue to configure */
2273 __u8 txq;
2274 } ap;
2275 struct {
2276 /* Log exponent of max contention period: 0...15 */
2277 __u8 log_cw_max;
2278
2279 /* Log exponent of min contention period: 0...15 */
2280 __u8 log_cw_min;
2281
2282 /* Adaptive interframe spacing in units of 32us */
2283 __u8 aifs;
2284
2285 /* TX queue to configure */
2286 __u8 txq;
2287 } sta;
2288 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002289} __attribute__((packed));
2290
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002291#define MWL8K_SET_EDCA_CW 0x01
2292#define MWL8K_SET_EDCA_TXOP 0x02
2293#define MWL8K_SET_EDCA_AIFS 0x04
2294
2295#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2296 MWL8K_SET_EDCA_TXOP | \
2297 MWL8K_SET_EDCA_AIFS)
2298
2299static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002300mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2301 __u16 cw_min, __u16 cw_max,
2302 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002303{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002304 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002305 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002306 int rc;
2307
2308 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2309 if (cmd == NULL)
2310 return -ENOMEM;
2311
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002312 /*
2313 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2314 * this call.
2315 */
2316 qnum ^= !(qnum >> 1);
2317
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002318 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2319 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002320 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2321 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002322 if (priv->ap_fw) {
2323 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2324 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2325 cmd->ap.aifs = aifs;
2326 cmd->ap.txq = qnum;
2327 } else {
2328 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2329 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2330 cmd->sta.aifs = aifs;
2331 cmd->sta.txq = qnum;
2332 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002333
2334 rc = mwl8k_post_cmd(hw, &cmd->header);
2335 kfree(cmd);
2336
2337 return rc;
2338}
2339
2340/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002341 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002342 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002343struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002344 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002345 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002346} __attribute__((packed));
2347
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002348static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002349{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002350 struct mwl8k_priv *priv = hw->priv;
2351 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002352 int rc;
2353
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002354 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2355 if (cmd == NULL)
2356 return -ENOMEM;
2357
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002358 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002359 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002360 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002361
2362 rc = mwl8k_post_cmd(hw, &cmd->header);
2363 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002364
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002365 if (!rc)
2366 priv->wmm_enabled = enable;
2367
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002368 return rc;
2369}
2370
2371/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002372 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002373 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002374struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002375 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002376 __le32 action;
2377 __u8 rx_antenna_map;
2378 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002379} __attribute__((packed));
2380
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002381static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002382{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002383 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002384 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002385
2386 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2387 if (cmd == NULL)
2388 return -ENOMEM;
2389
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002390 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002391 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002392 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2393 cmd->rx_antenna_map = rx;
2394 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002395
2396 rc = mwl8k_post_cmd(hw, &cmd->header);
2397 kfree(cmd);
2398
2399 return rc;
2400}
2401
2402/*
2403 * CMD_USE_FIXED_RATE.
2404 */
2405#define MWL8K_RATE_TABLE_SIZE 8
2406#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002407#define MWL8K_USE_AUTO_RATE 0x0002
2408
2409struct mwl8k_rate_entry {
2410 /* Set to 1 if HT rate, 0 if legacy. */
2411 __le32 is_ht_rate;
2412
2413 /* Set to 1 to use retry_count field. */
2414 __le32 enable_retry;
2415
2416 /* Specified legacy rate or MCS. */
2417 __le32 rate;
2418
2419 /* Number of allowed retries. */
2420 __le32 retry_count;
2421} __attribute__((packed));
2422
2423struct mwl8k_rate_table {
2424 /* 1 to allow specified rate and below */
2425 __le32 allow_rate_drop;
2426 __le32 num_rates;
2427 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2428} __attribute__((packed));
2429
2430struct mwl8k_cmd_use_fixed_rate {
2431 struct mwl8k_cmd_pkt header;
2432 __le32 action;
2433 struct mwl8k_rate_table rate_table;
2434
2435 /* Unicast, Broadcast or Multicast */
2436 __le32 rate_type;
2437 __le32 reserved1;
2438 __le32 reserved2;
2439} __attribute__((packed));
2440
2441static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2442 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2443{
2444 struct mwl8k_cmd_use_fixed_rate *cmd;
2445 int count;
2446 int rc;
2447
2448 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2449 if (cmd == NULL)
2450 return -ENOMEM;
2451
2452 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2453 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2454
2455 cmd->action = cpu_to_le32(action);
2456 cmd->rate_type = cpu_to_le32(rate_type);
2457
2458 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002459 /*
2460 * Copy over each field manually so that endian
2461 * conversion can be done.
2462 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002463 cmd->rate_table.allow_rate_drop =
2464 cpu_to_le32(rate_table->allow_rate_drop);
2465 cmd->rate_table.num_rates =
2466 cpu_to_le32(rate_table->num_rates);
2467
2468 for (count = 0; count < rate_table->num_rates; count++) {
2469 struct mwl8k_rate_entry *dst =
2470 &cmd->rate_table.rate_entry[count];
2471 struct mwl8k_rate_entry *src =
2472 &rate_table->rate_entry[count];
2473
2474 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2475 dst->enable_retry = cpu_to_le32(src->enable_retry);
2476 dst->rate = cpu_to_le32(src->rate);
2477 dst->retry_count = cpu_to_le32(src->retry_count);
2478 }
2479 }
2480
2481 rc = mwl8k_post_cmd(hw, &cmd->header);
2482 kfree(cmd);
2483
2484 return rc;
2485}
2486
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002487/*
2488 * CMD_ENABLE_SNIFFER.
2489 */
2490struct mwl8k_cmd_enable_sniffer {
2491 struct mwl8k_cmd_pkt header;
2492 __le32 action;
2493} __attribute__((packed));
2494
2495static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2496{
2497 struct mwl8k_cmd_enable_sniffer *cmd;
2498 int rc;
2499
2500 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2501 if (cmd == NULL)
2502 return -ENOMEM;
2503
2504 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2505 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2506 cmd->action = cpu_to_le32(!!enable);
2507
2508 rc = mwl8k_post_cmd(hw, &cmd->header);
2509 kfree(cmd);
2510
2511 return rc;
2512}
2513
2514/*
2515 * CMD_SET_MAC_ADDR.
2516 */
2517struct mwl8k_cmd_set_mac_addr {
2518 struct mwl8k_cmd_pkt header;
2519 union {
2520 struct {
2521 __le16 mac_type;
2522 __u8 mac_addr[ETH_ALEN];
2523 } mbss;
2524 __u8 mac_addr[ETH_ALEN];
2525 };
2526} __attribute__((packed));
2527
2528static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2529{
2530 struct mwl8k_priv *priv = hw->priv;
2531 struct mwl8k_cmd_set_mac_addr *cmd;
2532 int rc;
2533
2534 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2535 if (cmd == NULL)
2536 return -ENOMEM;
2537
2538 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2539 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2540 if (priv->ap_fw) {
2541 cmd->mbss.mac_type = 0;
2542 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2543 } else {
2544 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2545 }
2546
2547 rc = mwl8k_post_cmd(hw, &cmd->header);
2548 kfree(cmd);
2549
2550 return rc;
2551}
2552
2553/*
2554 * CMD_SET_RATEADAPT_MODE.
2555 */
2556struct mwl8k_cmd_set_rate_adapt_mode {
2557 struct mwl8k_cmd_pkt header;
2558 __le16 action;
2559 __le16 mode;
2560} __attribute__((packed));
2561
2562static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2563{
2564 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2565 int rc;
2566
2567 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2568 if (cmd == NULL)
2569 return -ENOMEM;
2570
2571 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2572 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2573 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2574 cmd->mode = cpu_to_le16(mode);
2575
2576 rc = mwl8k_post_cmd(hw, &cmd->header);
2577 kfree(cmd);
2578
2579 return rc;
2580}
2581
2582/*
2583 * CMD_UPDATE_STADB.
2584 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002585#define MWL8K_STA_DB_ADD_ENTRY 0
2586#define MWL8K_STA_DB_MODIFY_ENTRY 1
2587#define MWL8K_STA_DB_DEL_ENTRY 2
2588#define MWL8K_STA_DB_FLUSH 3
2589
2590/* Peer Entry flags - used to define the type of the peer node */
2591#define MWL8K_PEER_TYPE_ACCESSPOINT 2
2592
2593struct ewc_ht_info {
2594 __le16 control1;
2595 __le16 control2;
2596 __le16 control3;
2597} __attribute__((packed));
2598
2599struct peer_capability_info {
2600 /* Peer type - AP vs. STA. */
2601 __u8 peer_type;
2602
2603 /* Basic 802.11 capabilities from assoc resp. */
2604 __le16 basic_caps;
2605
2606 /* Set if peer supports 802.11n high throughput (HT). */
2607 __u8 ht_support;
2608
2609 /* Valid if HT is supported. */
2610 __le16 ht_caps;
2611 __u8 extended_ht_caps;
2612 struct ewc_ht_info ewc_info;
2613
2614 /* Legacy rate table. Intersection of our rates and peer rates. */
2615 __u8 legacy_rates[12];
2616
2617 /* HT rate table. Intersection of our rates and peer rates. */
2618 __u8 ht_rates[16];
2619 __u8 pad[16];
2620
2621 /* If set, interoperability mode, no proprietary extensions. */
2622 __u8 interop;
2623 __u8 pad2;
2624 __u8 station_id;
2625 __le16 amsdu_enabled;
2626} __attribute__((packed));
2627
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002628struct mwl8k_cmd_update_stadb {
2629 struct mwl8k_cmd_pkt header;
2630
2631 /* See STADB_ACTION_TYPE */
2632 __le32 action;
2633
2634 /* Peer MAC address */
2635 __u8 peer_addr[ETH_ALEN];
2636
2637 __le32 reserved;
2638
2639 /* Peer info - valid during add/update. */
2640 struct peer_capability_info peer_info;
2641} __attribute__((packed));
2642
2643static int mwl8k_cmd_update_stadb(struct ieee80211_hw *hw,
2644 struct ieee80211_vif *vif, __u32 action)
2645{
2646 struct mwl8k_vif *mv_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002647 struct mwl8k_cmd_update_stadb *cmd;
2648 struct peer_capability_info *peer_info;
2649 int rc;
2650
2651 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2652 if (cmd == NULL)
2653 return -ENOMEM;
2654
2655 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2656 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2657
2658 cmd->action = cpu_to_le32(action);
2659 peer_info = &cmd->peer_info;
2660 memcpy(cmd->peer_addr, mv_vif->bssid, ETH_ALEN);
2661
2662 switch (action) {
2663 case MWL8K_STA_DB_ADD_ENTRY:
2664 case MWL8K_STA_DB_MODIFY_ENTRY:
2665 /* Build peer_info block */
2666 peer_info->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002667 peer_info->basic_caps =
2668 cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002669 memcpy(peer_info->legacy_rates, mwl8k_rateids,
2670 sizeof(mwl8k_rateids));
2671 peer_info->interop = 1;
2672 peer_info->amsdu_enabled = 0;
2673
2674 rc = mwl8k_post_cmd(hw, &cmd->header);
2675 if (rc == 0)
2676 mv_vif->peer_id = peer_info->station_id;
2677
2678 break;
2679
2680 case MWL8K_STA_DB_DEL_ENTRY:
2681 case MWL8K_STA_DB_FLUSH:
2682 default:
2683 rc = mwl8k_post_cmd(hw, &cmd->header);
2684 if (rc == 0)
2685 mv_vif->peer_id = 0;
2686 break;
2687 }
2688 kfree(cmd);
2689
2690 return rc;
2691}
2692
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002693
2694/*
2695 * Interrupt handling.
2696 */
2697static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2698{
2699 struct ieee80211_hw *hw = dev_id;
2700 struct mwl8k_priv *priv = hw->priv;
2701 u32 status;
2702
2703 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2704 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2705
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002706 if (!status)
2707 return IRQ_NONE;
2708
2709 if (status & MWL8K_A2H_INT_TX_DONE)
2710 tasklet_schedule(&priv->tx_reclaim_task);
2711
2712 if (status & MWL8K_A2H_INT_RX_READY) {
2713 while (rxq_process(hw, 0, 1))
2714 rxq_refill(hw, 0, 1);
2715 }
2716
2717 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002718 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002719 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002720 }
2721
2722 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002723 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002724 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002725 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002726 }
2727
2728 return IRQ_HANDLED;
2729}
2730
2731
2732/*
2733 * Core driver operations.
2734 */
2735static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2736{
2737 struct mwl8k_priv *priv = hw->priv;
2738 int index = skb_get_queue_mapping(skb);
2739 int rc;
2740
2741 if (priv->current_channel == NULL) {
2742 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002743 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002744 dev_kfree_skb(skb);
2745 return NETDEV_TX_OK;
2746 }
2747
2748 rc = mwl8k_txq_xmit(hw, index, skb);
2749
2750 return rc;
2751}
2752
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002753static int mwl8k_start(struct ieee80211_hw *hw)
2754{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002755 struct mwl8k_priv *priv = hw->priv;
2756 int rc;
2757
Joe Perchesa0607fd2009-11-18 23:29:17 -08002758 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002759 IRQF_SHARED, MWL8K_NAME, hw);
2760 if (rc) {
2761 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002762 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002763 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002764 }
2765
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002766 /* Enable tx reclaim tasklet */
2767 tasklet_enable(&priv->tx_reclaim_task);
2768
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002769 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002770 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002771
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002772 rc = mwl8k_fw_lock(hw);
2773 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002774 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002775
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002776 if (!priv->ap_fw) {
2777 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002778 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002779
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002780 if (!rc)
2781 rc = mwl8k_cmd_set_pre_scan(hw);
2782
2783 if (!rc)
2784 rc = mwl8k_cmd_set_post_scan(hw,
2785 "\x00\x00\x00\x00\x00\x00");
2786 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002787
2788 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002789 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002790
2791 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002792 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002793
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002794 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002795 }
2796
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002797 if (rc) {
2798 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2799 free_irq(priv->pdev->irq, hw);
2800 tasklet_disable(&priv->tx_reclaim_task);
2801 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002802
2803 return rc;
2804}
2805
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002806static void mwl8k_stop(struct ieee80211_hw *hw)
2807{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002808 struct mwl8k_priv *priv = hw->priv;
2809 int i;
2810
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002811 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002812
2813 ieee80211_stop_queues(hw);
2814
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002815 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002816 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002817 free_irq(priv->pdev->irq, hw);
2818
2819 /* Stop finalize join worker */
2820 cancel_work_sync(&priv->finalize_join_worker);
2821 if (priv->beacon_skb != NULL)
2822 dev_kfree_skb(priv->beacon_skb);
2823
2824 /* Stop tx reclaim tasklet */
2825 tasklet_disable(&priv->tx_reclaim_task);
2826
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002827 /* Return all skbs to mac80211 */
2828 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2829 mwl8k_txq_reclaim(hw, i, 1);
2830}
2831
2832static int mwl8k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002833 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002834{
2835 struct mwl8k_priv *priv = hw->priv;
2836 struct mwl8k_vif *mwl8k_vif;
2837
2838 /*
2839 * We only support one active interface at a time.
2840 */
2841 if (priv->vif != NULL)
2842 return -EBUSY;
2843
2844 /*
2845 * We only support managed interfaces for now.
2846 */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002847 if (vif->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002848 return -EINVAL;
2849
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002850 /*
2851 * Reject interface creation if sniffer mode is active, as
2852 * STA operation is mutually exclusive with hardware sniffer
2853 * mode.
2854 */
2855 if (priv->sniffer_enabled) {
2856 printk(KERN_INFO "%s: unable to create STA "
2857 "interface due to sniffer mode being enabled\n",
2858 wiphy_name(hw->wiphy));
2859 return -EINVAL;
2860 }
2861
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002862 /* Clean out driver private area */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002863 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002864 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2865
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002866 /* Set and save the mac address */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002867 mwl8k_cmd_set_mac_addr(hw, vif->addr);
2868 memcpy(mwl8k_vif->mac_addr, vif->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002869
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002870 /* Set Initial sequence number to zero */
2871 mwl8k_vif->seqno = 0;
2872
Johannes Berg1ed32e42009-12-23 13:15:45 +01002873 priv->vif = vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002874 priv->current_channel = NULL;
2875
2876 return 0;
2877}
2878
2879static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002880 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002881{
2882 struct mwl8k_priv *priv = hw->priv;
2883
2884 if (priv->vif == NULL)
2885 return;
2886
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002887 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002888
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002889 priv->vif = NULL;
2890}
2891
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002892static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002893{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002894 struct ieee80211_conf *conf = &hw->conf;
2895 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002896 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002897
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002898 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002899 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002900 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002901 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002902 }
2903
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002904 rc = mwl8k_fw_lock(hw);
2905 if (rc)
2906 return rc;
2907
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002908 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002909 if (rc)
2910 goto out;
2911
2912 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2913 if (rc)
2914 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002915
2916 priv->current_channel = conf->channel;
2917
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002918 if (conf->power_level > 18)
2919 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002920 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002921 if (rc)
2922 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002923
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002924 if (priv->ap_fw) {
2925 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2926 if (!rc)
2927 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2928 } else {
2929 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2930 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002931
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002932out:
2933 mwl8k_fw_unlock(hw);
2934
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002935 return rc;
2936}
2937
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002938static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2939 struct ieee80211_vif *vif,
2940 struct ieee80211_bss_conf *info,
2941 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002942{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002943 struct mwl8k_priv *priv = hw->priv;
2944 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002945 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002946
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002947 if ((changed & BSS_CHANGED_ASSOC) == 0)
2948 return;
2949
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002950 priv->capture_beacon = false;
2951
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002952 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002953 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002954 return;
2955
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002956 if (vif->bss_conf.assoc) {
2957 memcpy(mwl8k_vif->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhekd1844d72009-11-30 18:13:56 +01002958
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002959 /* Install rates */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002960 rc = mwl8k_cmd_set_rate(hw, vif);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002961 if (rc)
2962 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002963
2964 /* Turn on rate adaptation */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002965 rc = mwl8k_cmd_use_fixed_rate(hw, MWL8K_USE_AUTO_RATE,
2966 MWL8K_UCAST_RATE, NULL);
2967 if (rc)
2968 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002969
2970 /* Set radio preamble */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002971 rc = mwl8k_set_radio_preamble(hw,
2972 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002973 if (rc)
2974 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002975
2976 /* Set slot time */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002977 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002978 if (rc)
2979 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002980
2981 /* Update peer rate info */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002982 rc = mwl8k_cmd_update_stadb(hw, vif,
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002983 MWL8K_STA_DB_MODIFY_ENTRY);
2984 if (rc)
2985 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002986
2987 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002988 rc = mwl8k_cmd_set_aid(hw, vif);
2989 if (rc)
2990 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002991
2992 /*
2993 * Finalize the join. Tell rx handler to process
2994 * next beacon from our BSSID.
2995 */
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002996 memcpy(priv->capture_bssid, mwl8k_vif->bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002997 priv->capture_beacon = true;
2998 } else {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002999 rc = mwl8k_cmd_update_stadb(hw, vif, MWL8K_STA_DB_DEL_ENTRY);
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003000 memset(mwl8k_vif->bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003001 }
3002
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003003out:
3004 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003005}
3006
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003007static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3008 int mc_count, struct dev_addr_list *mclist)
3009{
3010 struct mwl8k_cmd_pkt *cmd;
3011
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003012 /*
3013 * Synthesize and return a command packet that programs the
3014 * hardware multicast address filter. At this point we don't
3015 * know whether FIF_ALLMULTI is being requested, but if it is,
3016 * we'll end up throwing this packet away and creating a new
3017 * one in mwl8k_configure_filter().
3018 */
3019 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003020
3021 return (unsigned long)cmd;
3022}
3023
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003024static int
3025mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3026 unsigned int changed_flags,
3027 unsigned int *total_flags)
3028{
3029 struct mwl8k_priv *priv = hw->priv;
3030
3031 /*
3032 * Hardware sniffer mode is mutually exclusive with STA
3033 * operation, so refuse to enable sniffer mode if a STA
3034 * interface is active.
3035 */
3036 if (priv->vif != NULL) {
3037 if (net_ratelimit())
3038 printk(KERN_INFO "%s: not enabling sniffer "
3039 "mode because STA interface is active\n",
3040 wiphy_name(hw->wiphy));
3041 return 0;
3042 }
3043
3044 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003045 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003046 return 0;
3047 priv->sniffer_enabled = true;
3048 }
3049
3050 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3051 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3052 FIF_OTHER_BSS;
3053
3054 return 1;
3055}
3056
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003057static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3058 unsigned int changed_flags,
3059 unsigned int *total_flags,
3060 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003061{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003062 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003063 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3064
3065 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003066 * AP firmware doesn't allow fine-grained control over
3067 * the receive filter.
3068 */
3069 if (priv->ap_fw) {
3070 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3071 kfree(cmd);
3072 return;
3073 }
3074
3075 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003076 * Enable hardware sniffer mode if FIF_CONTROL or
3077 * FIF_OTHER_BSS is requested.
3078 */
3079 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3080 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3081 kfree(cmd);
3082 return;
3083 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003084
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003085 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003086 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003087
3088 if (mwl8k_fw_lock(hw))
3089 return;
3090
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003091 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003092 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003093 priv->sniffer_enabled = false;
3094 }
3095
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003096 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003097 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3098 /*
3099 * Disable the BSS filter.
3100 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003101 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003102 } else {
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003103 u8 *bssid;
3104
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003105 /*
3106 * Enable the BSS filter.
3107 *
3108 * If there is an active STA interface, use that
3109 * interface's BSSID, otherwise use a dummy one
3110 * (where the OUI part needs to be nonzero for
3111 * the BSSID to be accepted by POST_SCAN).
3112 */
3113 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003114 if (priv->vif != NULL)
3115 bssid = MWL8K_VIF(priv->vif)->bssid;
3116
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003117 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003118 }
3119 }
3120
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003121 /*
3122 * If FIF_ALLMULTI is being requested, throw away the command
3123 * packet that ->prepare_multicast() built and replace it with
3124 * a command packet that enables reception of all multicast
3125 * packets.
3126 */
3127 if (*total_flags & FIF_ALLMULTI) {
3128 kfree(cmd);
3129 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3130 }
3131
3132 if (cmd != NULL) {
3133 mwl8k_post_cmd(hw, cmd);
3134 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003135 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003136
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003137 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003138}
3139
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003140static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3141{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003142 return mwl8k_cmd_set_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003143}
3144
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003145static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3146 const struct ieee80211_tx_queue_params *params)
3147{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003148 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003149 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003150
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003151 rc = mwl8k_fw_lock(hw);
3152 if (!rc) {
3153 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003154 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003155
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003156 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003157 rc = mwl8k_cmd_set_edca_params(hw, queue,
3158 params->cw_min,
3159 params->cw_max,
3160 params->aifs,
3161 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003162
3163 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003164 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003165
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003166 return rc;
3167}
3168
3169static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3170 struct ieee80211_tx_queue_stats *stats)
3171{
3172 struct mwl8k_priv *priv = hw->priv;
3173 struct mwl8k_tx_queue *txq;
3174 int index;
3175
3176 spin_lock_bh(&priv->tx_lock);
3177 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3178 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003179 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003180 sizeof(struct ieee80211_tx_queue_stats));
3181 }
3182 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003183
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003184 return 0;
3185}
3186
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003187static int mwl8k_get_stats(struct ieee80211_hw *hw,
3188 struct ieee80211_low_level_stats *stats)
3189{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003190 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003191}
3192
3193static const struct ieee80211_ops mwl8k_ops = {
3194 .tx = mwl8k_tx,
3195 .start = mwl8k_start,
3196 .stop = mwl8k_stop,
3197 .add_interface = mwl8k_add_interface,
3198 .remove_interface = mwl8k_remove_interface,
3199 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003200 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003201 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003202 .configure_filter = mwl8k_configure_filter,
3203 .set_rts_threshold = mwl8k_set_rts_threshold,
3204 .conf_tx = mwl8k_conf_tx,
3205 .get_tx_stats = mwl8k_get_tx_stats,
3206 .get_stats = mwl8k_get_stats,
3207};
3208
3209static void mwl8k_tx_reclaim_handler(unsigned long data)
3210{
3211 int i;
3212 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3213 struct mwl8k_priv *priv = hw->priv;
3214
3215 spin_lock_bh(&priv->tx_lock);
3216 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3217 mwl8k_txq_reclaim(hw, i, 0);
3218
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003219 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003220 complete(priv->tx_wait);
3221 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003222 }
3223 spin_unlock_bh(&priv->tx_lock);
3224}
3225
3226static void mwl8k_finalize_join_worker(struct work_struct *work)
3227{
3228 struct mwl8k_priv *priv =
3229 container_of(work, struct mwl8k_priv, finalize_join_worker);
3230 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003231
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003232 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len,
3233 priv->vif->bss_conf.dtim_period);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003234 dev_kfree_skb(skb);
3235
3236 priv->beacon_skb = NULL;
3237}
3238
John W. Linvillebcb628d2009-11-06 16:40:16 -05003239enum {
3240 MWL8687 = 0,
3241 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003242};
3243
John W. Linvillebcb628d2009-11-06 16:40:16 -05003244static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003245 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003246 .part_name = "88w8687",
3247 .helper_image = "mwl8k/helper_8687.fw",
3248 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003249 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003250 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003251 .part_name = "88w8366",
3252 .helper_image = "mwl8k/helper_8366.fw",
3253 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003254 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003255 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003256};
3257
3258static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003259 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3260 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3261 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3262 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003263};
3264MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3265
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003266static int __devinit mwl8k_probe(struct pci_dev *pdev,
3267 const struct pci_device_id *id)
3268{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003269 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003270 struct ieee80211_hw *hw;
3271 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003272 int rc;
3273 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003274
3275 if (!printed_version) {
3276 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3277 printed_version = 1;
3278 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003279
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003280
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003281 rc = pci_enable_device(pdev);
3282 if (rc) {
3283 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3284 MWL8K_NAME);
3285 return rc;
3286 }
3287
3288 rc = pci_request_regions(pdev, MWL8K_NAME);
3289 if (rc) {
3290 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3291 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003292 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003293 }
3294
3295 pci_set_master(pdev);
3296
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003297
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003298 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3299 if (hw == NULL) {
3300 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3301 rc = -ENOMEM;
3302 goto err_free_reg;
3303 }
3304
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003305 SET_IEEE80211_DEV(hw, &pdev->dev);
3306 pci_set_drvdata(pdev, hw);
3307
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003308 priv = hw->priv;
3309 priv->hw = hw;
3310 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003311 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003312
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003313
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003314 priv->sram = pci_iomap(pdev, 0, 0x10000);
3315 if (priv->sram == NULL) {
3316 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003317 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003318 goto err_iounmap;
3319 }
3320
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003321 /*
3322 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3323 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3324 */
3325 priv->regs = pci_iomap(pdev, 1, 0x10000);
3326 if (priv->regs == NULL) {
3327 priv->regs = pci_iomap(pdev, 2, 0x10000);
3328 if (priv->regs == NULL) {
3329 printk(KERN_ERR "%s: Cannot map device registers\n",
3330 wiphy_name(hw->wiphy));
3331 goto err_iounmap;
3332 }
3333 }
3334
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003335
3336 /* Reset firmware and hardware */
3337 mwl8k_hw_reset(priv);
3338
3339 /* Ask userland hotplug daemon for the device firmware */
3340 rc = mwl8k_request_firmware(priv);
3341 if (rc) {
3342 printk(KERN_ERR "%s: Firmware files not found\n",
3343 wiphy_name(hw->wiphy));
3344 goto err_stop_firmware;
3345 }
3346
3347 /* Load firmware into hardware */
3348 rc = mwl8k_load_firmware(hw);
3349 if (rc) {
3350 printk(KERN_ERR "%s: Cannot start firmware\n",
3351 wiphy_name(hw->wiphy));
3352 goto err_stop_firmware;
3353 }
3354
3355 /* Reclaim memory once firmware is successfully loaded */
3356 mwl8k_release_firmware(priv);
3357
3358
Lennert Buytenhek91942232010-01-04 21:53:54 +01003359 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003360 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003361 if (priv->rxd_ops == NULL) {
3362 printk(KERN_ERR "%s: Driver does not have AP "
3363 "firmware image support for this hardware\n",
3364 wiphy_name(hw->wiphy));
3365 goto err_stop_firmware;
3366 }
3367 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003368 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003369 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003370
3371 priv->sniffer_enabled = false;
3372 priv->wmm_enabled = false;
3373 priv->pending_tx_pkts = 0;
3374
3375
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003376 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3377 priv->band.band = IEEE80211_BAND_2GHZ;
3378 priv->band.channels = priv->channels;
3379 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3380 priv->band.bitrates = priv->rates;
3381 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3382 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3383
3384 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3385 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3386
3387 /*
3388 * Extra headroom is the size of the required DMA header
3389 * minus the size of the smallest 802.11 frame (CTS frame).
3390 */
3391 hw->extra_tx_headroom =
3392 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3393
3394 hw->channel_change_time = 10;
3395
3396 hw->queues = MWL8K_TX_QUEUES;
3397
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003398 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003399 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003400 hw->vif_data_size = sizeof(struct mwl8k_vif);
3401 priv->vif = NULL;
3402
3403 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003404 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003405 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003406
3407 /* Finalize join worker */
3408 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3409
3410 /* TX reclaim tasklet */
3411 tasklet_init(&priv->tx_reclaim_task,
3412 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3413 tasklet_disable(&priv->tx_reclaim_task);
3414
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003415 /* Power management cookie */
3416 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3417 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003418 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003419
3420 rc = mwl8k_rxq_init(hw, 0);
3421 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003422 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003423 rxq_refill(hw, 0, INT_MAX);
3424
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003425 mutex_init(&priv->fw_mutex);
3426 priv->fw_mutex_owner = NULL;
3427 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003428 priv->hostcmd_wait = NULL;
3429
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003430 spin_lock_init(&priv->tx_lock);
3431
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003432 priv->tx_wait = NULL;
3433
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003434 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3435 rc = mwl8k_txq_init(hw, i);
3436 if (rc)
3437 goto err_free_queues;
3438 }
3439
3440 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003441 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003442 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3443 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3444
Joe Perchesa0607fd2009-11-18 23:29:17 -08003445 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003446 IRQF_SHARED, MWL8K_NAME, hw);
3447 if (rc) {
3448 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003449 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003450 goto err_free_queues;
3451 }
3452
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003453 /*
3454 * Temporarily enable interrupts. Initial firmware host
3455 * commands use interrupts and avoids polling. Disable
3456 * interrupts when done.
3457 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003458 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003459
3460 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02003461 if (priv->ap_fw) {
3462 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3463 if (!rc)
3464 rc = mwl8k_cmd_set_hw_spec(hw);
3465 } else {
3466 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003467
3468 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02003469 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003470 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003471 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3472 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003473 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003474 }
3475
3476 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003477 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003478 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003479 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003480 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003481 }
3482
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003483 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003484 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003485 if (rc) {
3486 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3487 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003488 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003489 }
3490
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003491 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003492 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003493 free_irq(priv->pdev->irq, hw);
3494
3495 rc = ieee80211_register_hw(hw);
3496 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003497 printk(KERN_ERR "%s: Cannot register device\n",
3498 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01003499 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003500 }
3501
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003502 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003503 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003504 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003505 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003506 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3507 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003508
3509 return 0;
3510
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003511err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003512 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003513 free_irq(priv->pdev->irq, hw);
3514
3515err_free_queues:
3516 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3517 mwl8k_txq_deinit(hw, i);
3518 mwl8k_rxq_deinit(hw, 0);
3519
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003520err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003521 if (priv->cookie != NULL)
3522 pci_free_consistent(priv->pdev, 4,
3523 priv->cookie, priv->cookie_dma);
3524
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003525err_stop_firmware:
3526 mwl8k_hw_reset(priv);
3527 mwl8k_release_firmware(priv);
3528
3529err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003530 if (priv->regs != NULL)
3531 pci_iounmap(pdev, priv->regs);
3532
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003533 if (priv->sram != NULL)
3534 pci_iounmap(pdev, priv->sram);
3535
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003536 pci_set_drvdata(pdev, NULL);
3537 ieee80211_free_hw(hw);
3538
3539err_free_reg:
3540 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003541
3542err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003543 pci_disable_device(pdev);
3544
3545 return rc;
3546}
3547
Joerg Albert230f7af2009-04-18 02:10:45 +02003548static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003549{
3550 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3551}
3552
Joerg Albert230f7af2009-04-18 02:10:45 +02003553static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003554{
3555 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3556 struct mwl8k_priv *priv;
3557 int i;
3558
3559 if (hw == NULL)
3560 return;
3561 priv = hw->priv;
3562
3563 ieee80211_stop_queues(hw);
3564
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003565 ieee80211_unregister_hw(hw);
3566
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003567 /* Remove tx reclaim tasklet */
3568 tasklet_kill(&priv->tx_reclaim_task);
3569
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003570 /* Stop hardware */
3571 mwl8k_hw_reset(priv);
3572
3573 /* Return all skbs to mac80211 */
3574 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3575 mwl8k_txq_reclaim(hw, i, 1);
3576
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003577 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3578 mwl8k_txq_deinit(hw, i);
3579
3580 mwl8k_rxq_deinit(hw, 0);
3581
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003582 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003583
3584 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003585 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003586 pci_set_drvdata(pdev, NULL);
3587 ieee80211_free_hw(hw);
3588 pci_release_regions(pdev);
3589 pci_disable_device(pdev);
3590}
3591
3592static struct pci_driver mwl8k_driver = {
3593 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003594 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003595 .probe = mwl8k_probe,
3596 .remove = __devexit_p(mwl8k_remove),
3597 .shutdown = __devexit_p(mwl8k_shutdown),
3598};
3599
3600static int __init mwl8k_init(void)
3601{
3602 return pci_register_driver(&mwl8k_driver);
3603}
3604
3605static void __exit mwl8k_exit(void)
3606{
3607 pci_unregister_driver(&mwl8k_driver);
3608}
3609
3610module_init(mwl8k_init);
3611module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003612
3613MODULE_DESCRIPTION(MWL8K_DESC);
3614MODULE_VERSION(MWL8K_VERSION);
3615MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3616MODULE_LICENSE("GPL");