blob: 5b5cf9357dbf3a3b6d19fcf831cb6ad11f78693f [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka145d572009-08-18 04:34:26 +02005 * Copyright (C) 2008-2009 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020015#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010016#include <linux/spinlock.h>
17#include <linux/list.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
20#include <linux/completion.h>
21#include <linux/etherdevice.h>
22#include <net/mac80211.h>
23#include <linux/moduleparam.h>
24#include <linux/firmware.h>
25#include <linux/workqueue.h>
26
27#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
28#define MWL8K_NAME KBUILD_MODNAME
Lennert Buytenhek6976b662010-01-02 10:31:50 +010029#define MWL8K_VERSION "0.11"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010030
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031/* Register definitions */
32#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020033#define MWL8K_MODE_STA 0x0000005a
34#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010035#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020036#define MWL8K_FWSTA_READY 0xf0f1f2f4
37#define MWL8K_FWAP_READY 0xf1f2f4a5
38#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010039#define MWL8K_HIU_SCRATCH 0x00000c40
40
41/* Host->device communications */
42#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
43#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
44#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
45#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
46#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020047#define MWL8K_H2A_INT_DUMMY (1 << 20)
48#define MWL8K_H2A_INT_RESET (1 << 15)
49#define MWL8K_H2A_INT_DOORBELL (1 << 1)
50#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010051
52/* Device->host communications */
53#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
54#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
55#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
56#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
57#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020058#define MWL8K_A2H_INT_DUMMY (1 << 20)
59#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
60#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
61#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
62#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
63#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
64#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
65#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
66#define MWL8K_A2H_INT_RX_READY (1 << 1)
67#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010068
69#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
70 MWL8K_A2H_INT_CHNL_SWITCHED | \
71 MWL8K_A2H_INT_QUEUE_EMPTY | \
72 MWL8K_A2H_INT_RADAR_DETECT | \
73 MWL8K_A2H_INT_RADIO_ON | \
74 MWL8K_A2H_INT_RADIO_OFF | \
75 MWL8K_A2H_INT_MAC_EVENT | \
76 MWL8K_A2H_INT_OPC_DONE | \
77 MWL8K_A2H_INT_RX_READY | \
78 MWL8K_A2H_INT_TX_DONE)
79
Lennert Buytenheka66098d2009-03-10 10:13:33 +010080#define MWL8K_RX_QUEUES 1
81#define MWL8K_TX_QUEUES 4
82
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020083struct rxd_ops {
84 int rxd_size;
85 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
86 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010087 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
88 __le16 *qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020089};
90
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020091struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020092 char *part_name;
93 char *helper_image;
94 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010095 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020096};
97
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +020099 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100100
101 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200102 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
104 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200105 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100106
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200107 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200109 struct {
110 struct sk_buff *skb;
111 DECLARE_PCI_UNMAP_ADDR(dma)
112 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100113};
114
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115struct mwl8k_tx_queue {
116 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100118
119 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200120 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100121
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200122 struct ieee80211_tx_queue_stats stats;
123 struct mwl8k_tx_desc *txd;
124 dma_addr_t txd_dma;
125 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100126};
127
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100128struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200132 struct mwl8k_device_info *device_info;
133
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100134 void __iomem *sram;
135 void __iomem *regs;
136
137 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100138 struct firmware *fw_helper;
139 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100141 /* hardware/firmware parameters */
142 bool ap_fw;
143 struct rxd_ops *rxd_ops;
144
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200145 /* firmware access */
146 struct mutex fw_mutex;
147 struct task_struct *fw_mutex_owner;
148 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200149 struct completion *hostcmd_wait;
150
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100151 /* lock held over TX and TX reap */
152 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100153
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200154 /* TX quiesce completion, protected by fw_mutex and tx_lock */
155 struct completion *tx_wait;
156
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 struct ieee80211_vif *vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158
159 struct ieee80211_channel *current_channel;
160
161 /* power management status cookie from firmware */
162 u32 *cookie;
163 dma_addr_t cookie_dma;
164
165 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100166 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200167 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168
169 /*
170 * Running count of TX packets in flight, to avoid
171 * iterating over the transmit rings each time.
172 */
173 int pending_tx_pkts;
174
175 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
176 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
177
178 /* PHY parameters */
179 struct ieee80211_supported_band band;
180 struct ieee80211_channel channels[14];
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100181 struct ieee80211_rate rates[14];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100182
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200183 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200184 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200185 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200186 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100187
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +0100188 struct work_struct sta_notify_worker;
189 spinlock_t sta_notify_list_lock;
190 struct list_head sta_notify_list;
191
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100192 /* XXX need to convert this to handle multiple interfaces */
193 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200194 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 struct sk_buff *beacon_skb;
196
197 /*
198 * This FJ worker has to be global as it is scheduled from the
199 * RX handler. At this point we don't know which interface it
200 * belongs to until the list of bssids waiting to complete join
201 * is checked.
202 */
203 struct work_struct finalize_join_worker;
204
205 /* Tasklet to reclaim TX descriptors and buffers after tx */
206 struct tasklet_struct tx_reclaim_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100207};
208
209/* Per interface specific private data */
210struct mwl8k_vif {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +0100211 /* Local MAC address. */
212 u8 mac_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100213
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100214 /* Non AMPDU sequence number assigned by driver */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100215 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100216};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200217#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218
Lennert Buytenheka6804002010-01-04 21:55:42 +0100219struct mwl8k_sta {
220 /* Index into station database. Returned by UPDATE_STADB. */
221 u8 peer_id;
222};
223#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
224
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225static const struct ieee80211_channel mwl8k_channels[] = {
226 { .center_freq = 2412, .hw_value = 1, },
227 { .center_freq = 2417, .hw_value = 2, },
228 { .center_freq = 2422, .hw_value = 3, },
229 { .center_freq = 2427, .hw_value = 4, },
230 { .center_freq = 2432, .hw_value = 5, },
231 { .center_freq = 2437, .hw_value = 6, },
232 { .center_freq = 2442, .hw_value = 7, },
233 { .center_freq = 2447, .hw_value = 8, },
234 { .center_freq = 2452, .hw_value = 9, },
235 { .center_freq = 2457, .hw_value = 10, },
236 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100237 { .center_freq = 2467, .hw_value = 12, },
238 { .center_freq = 2472, .hw_value = 13, },
239 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100240};
241
242static const struct ieee80211_rate mwl8k_rates[] = {
243 { .bitrate = 10, .hw_value = 2, },
244 { .bitrate = 20, .hw_value = 4, },
245 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200246 { .bitrate = 110, .hw_value = 22, },
247 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100248 { .bitrate = 60, .hw_value = 12, },
249 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100250 { .bitrate = 120, .hw_value = 24, },
251 { .bitrate = 180, .hw_value = 36, },
252 { .bitrate = 240, .hw_value = 48, },
253 { .bitrate = 360, .hw_value = 72, },
254 { .bitrate = 480, .hw_value = 96, },
255 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100256 { .bitrate = 720, .hw_value = 144, },
257};
258
259static const u8 mwl8k_rateids[12] = {
260 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108,
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100261};
262
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100263/* Set or get info from Firmware */
264#define MWL8K_CMD_SET 0x0001
265#define MWL8K_CMD_GET 0x0000
266
267/* Firmware command codes */
268#define MWL8K_CMD_CODE_DNLD 0x0001
269#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200270#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100271#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
272#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200273#define MWL8K_CMD_RADIO_CONTROL 0x001c
274#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200275#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100276#define MWL8K_CMD_SET_PRE_SCAN 0x0107
277#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200278#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100279#define MWL8K_CMD_SET_AID 0x010d
280#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200281#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100282#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200283#define MWL8K_CMD_SET_SLOT 0x0114
284#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
285#define MWL8K_CMD_SET_WMM_MODE 0x0123
286#define MWL8K_CMD_MIMO_CONFIG 0x0125
287#define MWL8K_CMD_USE_FIXED_RATE 0x0126
288#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200289#define MWL8K_CMD_SET_MAC_ADDR 0x0202
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200290#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
291#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100292
293static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
294{
295#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
296 snprintf(buf, bufsize, "%s", #x);\
297 return buf;\
298 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200299 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100300 MWL8K_CMDNAME(CODE_DNLD);
301 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200302 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100303 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
304 MWL8K_CMDNAME(GET_STAT);
305 MWL8K_CMDNAME(RADIO_CONTROL);
306 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200307 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100308 MWL8K_CMDNAME(SET_PRE_SCAN);
309 MWL8K_CMDNAME(SET_POST_SCAN);
310 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100311 MWL8K_CMDNAME(SET_AID);
312 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200313 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100314 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200315 MWL8K_CMDNAME(SET_SLOT);
316 MWL8K_CMDNAME(SET_EDCA_PARAMS);
317 MWL8K_CMDNAME(SET_WMM_MODE);
318 MWL8K_CMDNAME(MIMO_CONFIG);
319 MWL8K_CMDNAME(USE_FIXED_RATE);
320 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200321 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200322 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
323 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100324 default:
325 snprintf(buf, bufsize, "0x%x", cmd);
326 }
327#undef MWL8K_CMDNAME
328
329 return buf;
330}
331
332/* Hardware and firmware reset */
333static void mwl8k_hw_reset(struct mwl8k_priv *priv)
334{
335 iowrite32(MWL8K_H2A_INT_RESET,
336 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
337 iowrite32(MWL8K_H2A_INT_RESET,
338 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
339 msleep(20);
340}
341
342/* Release fw image */
343static void mwl8k_release_fw(struct firmware **fw)
344{
345 if (*fw == NULL)
346 return;
347 release_firmware(*fw);
348 *fw = NULL;
349}
350
351static void mwl8k_release_firmware(struct mwl8k_priv *priv)
352{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100353 mwl8k_release_fw(&priv->fw_ucode);
354 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100355}
356
357/* Request fw image */
358static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200359 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100360{
361 /* release current image */
362 if (*fw != NULL)
363 mwl8k_release_fw(fw);
364
365 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200366 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100367}
368
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200369static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100370{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200371 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100372 int rc;
373
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200374 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100375 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200376 if (rc) {
377 printk(KERN_ERR "%s: Error requesting helper "
378 "firmware file %s\n", pci_name(priv->pdev),
379 di->helper_image);
380 return rc;
381 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100382 }
383
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100384 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100385 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200386 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200387 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100388 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100389 return rc;
390 }
391
392 return 0;
393}
394
Ben Hutchings7e75b942009-11-07 22:00:57 +0000395MODULE_FIRMWARE("mwl8k/helper_8687.fw");
396MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
397
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100398struct mwl8k_cmd_pkt {
399 __le16 code;
400 __le16 length;
401 __le16 seq_num;
402 __le16 result;
403 char payload[0];
404} __attribute__((packed));
405
406/*
407 * Firmware loading.
408 */
409static int
410mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
411{
412 void __iomem *regs = priv->regs;
413 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100414 int loops;
415
416 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
417 if (pci_dma_mapping_error(priv->pdev, dma_addr))
418 return -ENOMEM;
419
420 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
421 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
422 iowrite32(MWL8K_H2A_INT_DOORBELL,
423 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
424 iowrite32(MWL8K_H2A_INT_DUMMY,
425 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
426
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100427 loops = 1000;
428 do {
429 u32 int_code;
430
431 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
432 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
433 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100434 break;
435 }
436
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200437 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100438 udelay(1);
439 } while (--loops);
440
441 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
442
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200443 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100444}
445
446static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
447 const u8 *data, size_t length)
448{
449 struct mwl8k_cmd_pkt *cmd;
450 int done;
451 int rc = 0;
452
453 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
454 if (cmd == NULL)
455 return -ENOMEM;
456
457 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
458 cmd->seq_num = 0;
459 cmd->result = 0;
460
461 done = 0;
462 while (length) {
463 int block_size = length > 256 ? 256 : length;
464
465 memcpy(cmd->payload, data + done, block_size);
466 cmd->length = cpu_to_le16(block_size);
467
468 rc = mwl8k_send_fw_load_cmd(priv, cmd,
469 sizeof(*cmd) + block_size);
470 if (rc)
471 break;
472
473 done += block_size;
474 length -= block_size;
475 }
476
477 if (!rc) {
478 cmd->length = 0;
479 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
480 }
481
482 kfree(cmd);
483
484 return rc;
485}
486
487static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
488 const u8 *data, size_t length)
489{
490 unsigned char *buffer;
491 int may_continue, rc = 0;
492 u32 done, prev_block_size;
493
494 buffer = kmalloc(1024, GFP_KERNEL);
495 if (buffer == NULL)
496 return -ENOMEM;
497
498 done = 0;
499 prev_block_size = 0;
500 may_continue = 1000;
501 while (may_continue > 0) {
502 u32 block_size;
503
504 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
505 if (block_size & 1) {
506 block_size &= ~1;
507 may_continue--;
508 } else {
509 done += prev_block_size;
510 length -= prev_block_size;
511 }
512
513 if (block_size > 1024 || block_size > length) {
514 rc = -EOVERFLOW;
515 break;
516 }
517
518 if (length == 0) {
519 rc = 0;
520 break;
521 }
522
523 if (block_size == 0) {
524 rc = -EPROTO;
525 may_continue--;
526 udelay(1);
527 continue;
528 }
529
530 prev_block_size = block_size;
531 memcpy(buffer, data + done, block_size);
532
533 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
534 if (rc)
535 break;
536 }
537
538 if (!rc && length != 0)
539 rc = -EREMOTEIO;
540
541 kfree(buffer);
542
543 return rc;
544}
545
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200546static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100547{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200548 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100549 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200550 int rc;
551 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100552
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200553 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100554 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100555
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200556 if (helper == NULL) {
557 printk(KERN_ERR "%s: helper image needed but none "
558 "given\n", pci_name(priv->pdev));
559 return -EINVAL;
560 }
561
562 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100563 if (rc) {
564 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200565 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100566 return rc;
567 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100568 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100569
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200570 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100571 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200572 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100573 }
574
575 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200576 printk(KERN_ERR "%s: unable to load firmware image\n",
577 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100578 return rc;
579 }
580
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100581 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100582
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100583 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100584 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200585 u32 ready_code;
586
587 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
588 if (ready_code == MWL8K_FWAP_READY) {
589 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100590 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200591 } else if (ready_code == MWL8K_FWSTA_READY) {
592 priv->ap_fw = 0;
593 break;
594 }
595
596 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100597 udelay(1);
598 } while (--loops);
599
600 return loops ? 0 : -ETIMEDOUT;
601}
602
603
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100604/* DMA header used by firmware and hardware. */
605struct mwl8k_dma_data {
606 __le16 fwlen;
607 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100608 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100609} __attribute__((packed));
610
611/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100612static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100613{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100614 struct mwl8k_dma_data *tr;
615 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100616
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100617 tr = (struct mwl8k_dma_data *)skb->data;
618 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
619
620 if (hdrlen != sizeof(tr->wh)) {
621 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
622 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
623 *((__le16 *)(tr->data - 2)) = qos;
624 } else {
625 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
626 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100627 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100628
629 if (hdrlen != sizeof(*tr))
630 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100631}
632
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200633static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100634{
635 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100636 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100637 struct mwl8k_dma_data *tr;
638
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100639 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100640 * Add a firmware DMA header; the firmware requires that we
641 * present a 2-byte payload length followed by a 4-address
642 * header (without QoS field), followed (optionally) by any
643 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100644 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100645 wh = (struct ieee80211_hdr *)skb->data;
646
647 hdrlen = ieee80211_hdrlen(wh->frame_control);
648 if (hdrlen != sizeof(*tr))
649 skb_push(skb, sizeof(*tr) - hdrlen);
650
651 if (ieee80211_is_data_qos(wh->frame_control))
652 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100653
654 tr = (struct mwl8k_dma_data *)skb->data;
655 if (wh != &tr->wh)
656 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100657 if (hdrlen != sizeof(tr->wh))
658 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100659
660 /*
661 * Firmware length is the length of the fully formed "802.11
662 * payload". That is, everything except for the 802.11 header.
663 * This includes all crypto material including the MIC.
664 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100665 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100666}
667
668
669/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100670 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200671 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100672struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200673 __le16 pkt_len;
674 __u8 sq2;
675 __u8 rate;
676 __le32 pkt_phys_addr;
677 __le32 next_rxd_phys_addr;
678 __le16 qos_control;
679 __le16 htsig2;
680 __le32 hw_rssi_info;
681 __le32 hw_noise_floor_info;
682 __u8 noise_floor;
683 __u8 pad0[3];
684 __u8 rssi;
685 __u8 rx_status;
686 __u8 channel;
687 __u8 rx_ctrl;
688} __attribute__((packed));
689
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100690#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
691#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
692#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100693
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100694#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200695
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100696static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200697{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100698 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200699
700 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100701 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200702}
703
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100704static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200705{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100706 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200707
708 rxd->pkt_len = cpu_to_le16(len);
709 rxd->pkt_phys_addr = cpu_to_le32(addr);
710 wmb();
711 rxd->rx_ctrl = 0;
712}
713
714static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100715mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
716 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200717{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100718 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200719
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100720 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200721 return -1;
722 rmb();
723
724 memset(status, 0, sizeof(*status));
725
726 status->signal = -rxd->rssi;
727 status->noise = -rxd->noise_floor;
728
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100729 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200730 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100731 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100732 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100733 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200734 } else {
735 int i;
736
737 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
738 if (mwl8k_rates[i].hw_value == rxd->rate) {
739 status->rate_idx = i;
740 break;
741 }
742 }
743 }
744
745 status->band = IEEE80211_BAND_2GHZ;
746 status->freq = ieee80211_channel_to_frequency(rxd->channel);
747
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100748 *qos = rxd->qos_control;
749
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200750 return le16_to_cpu(rxd->pkt_len);
751}
752
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100753static struct rxd_ops rxd_8366_ap_ops = {
754 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
755 .rxd_init = mwl8k_rxd_8366_ap_init,
756 .rxd_refill = mwl8k_rxd_8366_ap_refill,
757 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200758};
759
760/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100761 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100762 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100763struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100764 __le16 pkt_len;
765 __u8 link_quality;
766 __u8 noise_level;
767 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200768 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100769 __le16 qos_control;
770 __le16 rate_info;
771 __le32 pad0[4];
772 __u8 rssi;
773 __u8 channel;
774 __le16 pad1;
775 __u8 rx_ctrl;
776 __u8 rx_status;
777 __u8 pad2[2];
778} __attribute__((packed));
779
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100780#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
781#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
782#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
783#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
784#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
785#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200786
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100787#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200788
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100789static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200790{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100791 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200792
793 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100794 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200795}
796
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100797static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200798{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100799 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200800
801 rxd->pkt_len = cpu_to_le16(len);
802 rxd->pkt_phys_addr = cpu_to_le32(addr);
803 wmb();
804 rxd->rx_ctrl = 0;
805}
806
807static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100808mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100809 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200810{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100811 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200812 u16 rate_info;
813
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100814 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200815 return -1;
816 rmb();
817
818 rate_info = le16_to_cpu(rxd->rate_info);
819
820 memset(status, 0, sizeof(*status));
821
822 status->signal = -rxd->rssi;
823 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100824 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
825 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200826
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100827 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200828 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100829 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200830 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100831 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200832 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100833 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200834 status->flag |= RX_FLAG_HT;
835
836 status->band = IEEE80211_BAND_2GHZ;
837 status->freq = ieee80211_channel_to_frequency(rxd->channel);
838
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100839 *qos = rxd->qos_control;
840
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200841 return le16_to_cpu(rxd->pkt_len);
842}
843
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100844static struct rxd_ops rxd_sta_ops = {
845 .rxd_size = sizeof(struct mwl8k_rxd_sta),
846 .rxd_init = mwl8k_rxd_sta_init,
847 .rxd_refill = mwl8k_rxd_sta_refill,
848 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200849};
850
851
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100852#define MWL8K_RX_DESCS 256
853#define MWL8K_RX_MAXSZ 3800
854
855static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
856{
857 struct mwl8k_priv *priv = hw->priv;
858 struct mwl8k_rx_queue *rxq = priv->rxq + index;
859 int size;
860 int i;
861
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200862 rxq->rxd_count = 0;
863 rxq->head = 0;
864 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100865
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200866 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100867
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200868 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
869 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100870 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200871 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100872 return -ENOMEM;
873 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200874 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100875
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200876 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
877 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100878 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200879 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200880 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100881 return -ENOMEM;
882 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200883 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100884
885 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200886 int desc_size;
887 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100888 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200889 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100890
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200891 desc_size = priv->rxd_ops->rxd_size;
892 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100893
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200894 nexti = i + 1;
895 if (nexti == MWL8K_RX_DESCS)
896 nexti = 0;
897 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
898
899 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100900 }
901
902 return 0;
903}
904
905static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
906{
907 struct mwl8k_priv *priv = hw->priv;
908 struct mwl8k_rx_queue *rxq = priv->rxq + index;
909 int refilled;
910
911 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200912 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100913 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200914 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100915 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200916 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100917
918 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
919 if (skb == NULL)
920 break;
921
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200922 addr = pci_map_single(priv->pdev, skb->data,
923 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100924
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200925 rxq->rxd_count++;
926 rx = rxq->tail++;
927 if (rxq->tail == MWL8K_RX_DESCS)
928 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200929 rxq->buf[rx].skb = skb;
930 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200931
932 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
933 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100934
935 refilled++;
936 }
937
938 return refilled;
939}
940
941/* Must be called only when the card's reception is completely halted */
942static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
943{
944 struct mwl8k_priv *priv = hw->priv;
945 struct mwl8k_rx_queue *rxq = priv->rxq + index;
946 int i;
947
948 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200949 if (rxq->buf[i].skb != NULL) {
950 pci_unmap_single(priv->pdev,
951 pci_unmap_addr(&rxq->buf[i], dma),
952 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
953 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100954
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200955 kfree_skb(rxq->buf[i].skb);
956 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100957 }
958 }
959
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200960 kfree(rxq->buf);
961 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100962
963 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200964 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200965 rxq->rxd, rxq->rxd_dma);
966 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100967}
968
969
970/*
971 * Scan a list of BSSIDs to process for finalize join.
972 * Allows for extension to process multiple BSSIDs.
973 */
974static inline int
975mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
976{
977 return priv->capture_beacon &&
978 ieee80211_is_beacon(wh->frame_control) &&
979 !compare_ether_addr(wh->addr3, priv->capture_bssid);
980}
981
Lennert Buytenhek37797522009-10-22 20:19:45 +0200982static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
983 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100984{
Lennert Buytenhek37797522009-10-22 20:19:45 +0200985 struct mwl8k_priv *priv = hw->priv;
986
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100987 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200988 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100989
990 /*
991 * Use GFP_ATOMIC as rxq_process is called from
992 * the primary interrupt handler, memory allocation call
993 * must not sleep.
994 */
995 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
996 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +0200997 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100998}
999
1000static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1001{
1002 struct mwl8k_priv *priv = hw->priv;
1003 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1004 int processed;
1005
1006 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001007 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001008 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001009 void *rxd;
1010 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001011 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001012 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001013
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001014 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001015 if (skb == NULL)
1016 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001017
1018 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1019
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001020 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001021 if (pkt_len < 0)
1022 break;
1023
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001024 rxq->buf[rxq->head].skb = NULL;
1025
1026 pci_unmap_single(priv->pdev,
1027 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1028 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1029 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001030
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001031 rxq->head++;
1032 if (rxq->head == MWL8K_RX_DESCS)
1033 rxq->head = 0;
1034
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001035 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001036
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001037 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001038 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001039
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001040 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001041 * Check for a pending join operation. Save a
1042 * copy of the beacon and schedule a tasklet to
1043 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001044 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001045 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001046 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001047
Johannes Bergf1d58c22009-06-17 13:13:00 +02001048 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1049 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001050
1051 processed++;
1052 }
1053
1054 return processed;
1055}
1056
1057
1058/*
1059 * Packet transmission.
1060 */
1061
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001062#define MWL8K_TXD_STATUS_OK 0x00000001
1063#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1064#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1065#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001066#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001067
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001068#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1069#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1070#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1071#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1072#define MWL8K_QOS_EOSP 0x0010
1073
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001074struct mwl8k_tx_desc {
1075 __le32 status;
1076 __u8 data_rate;
1077 __u8 tx_priority;
1078 __le16 qos_control;
1079 __le32 pkt_phys_addr;
1080 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001081 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001082 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001083 __le32 reserved;
1084 __le16 rate_info;
1085 __u8 peer_id;
1086 __u8 tx_frag_cnt;
1087} __attribute__((packed));
1088
1089#define MWL8K_TX_DESCS 128
1090
1091static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1092{
1093 struct mwl8k_priv *priv = hw->priv;
1094 struct mwl8k_tx_queue *txq = priv->txq + index;
1095 int size;
1096 int i;
1097
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001098 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1099 txq->stats.limit = MWL8K_TX_DESCS;
1100 txq->head = 0;
1101 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001102
1103 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1104
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001105 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1106 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001107 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001108 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001109 return -ENOMEM;
1110 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001111 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001112
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001113 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1114 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001115 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001116 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001117 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001118 return -ENOMEM;
1119 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001120 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001121
1122 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1123 struct mwl8k_tx_desc *tx_desc;
1124 int nexti;
1125
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001126 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001127 nexti = (i + 1) % MWL8K_TX_DESCS;
1128
1129 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001130 tx_desc->next_txd_phys_addr =
1131 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001132 }
1133
1134 return 0;
1135}
1136
1137static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1138{
1139 iowrite32(MWL8K_H2A_INT_PPA_READY,
1140 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1141 iowrite32(MWL8K_H2A_INT_DUMMY,
1142 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1143 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1144}
1145
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001146static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001147{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001148 struct mwl8k_priv *priv = hw->priv;
1149 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001150
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001151 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1152 struct mwl8k_tx_queue *txq = priv->txq + i;
1153 int fw_owned = 0;
1154 int drv_owned = 0;
1155 int unused = 0;
1156 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001157
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001158 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001159 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1160 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001161
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001162 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001164 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001165 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001166 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167
1168 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001169 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001170 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001171
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001172 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1173 "fw_owned=%d drv_owned=%d unused=%d\n",
1174 wiphy_name(hw->wiphy), i,
1175 txq->stats.len, txq->head, txq->tail,
1176 fw_owned, drv_owned, unused);
1177 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001178}
1179
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001180/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001181 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001182 */
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001183#define MWL8K_TX_WAIT_TIMEOUT_MS 1000
1184
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001185static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001186{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001187 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001188 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001189 int retry;
1190 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001191
1192 might_sleep();
1193
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001194 /*
1195 * The TX queues are stopped at this point, so this test
1196 * doesn't need to take ->tx_lock.
1197 */
1198 if (!priv->pending_tx_pkts)
1199 return 0;
1200
1201 retry = 0;
1202 rc = 0;
1203
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001204 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001205 priv->tx_wait = &tx_wait;
1206 while (!rc) {
1207 int oldcount;
1208 unsigned long timeout;
1209
1210 oldcount = priv->pending_tx_pkts;
1211
1212 spin_unlock_bh(&priv->tx_lock);
1213 timeout = wait_for_completion_timeout(&tx_wait,
1214 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1215 spin_lock_bh(&priv->tx_lock);
1216
1217 if (timeout) {
1218 WARN_ON(priv->pending_tx_pkts);
1219 if (retry) {
1220 printk(KERN_NOTICE "%s: tx rings drained\n",
1221 wiphy_name(hw->wiphy));
1222 }
1223 break;
1224 }
1225
1226 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001227 printk(KERN_NOTICE "%s: waiting for tx rings "
1228 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001229 wiphy_name(hw->wiphy), oldcount,
1230 priv->pending_tx_pkts);
1231 retry = 1;
1232 continue;
1233 }
1234
1235 priv->tx_wait = NULL;
1236
1237 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1238 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1239 mwl8k_dump_tx_rings(hw);
1240
1241 rc = -ETIMEDOUT;
1242 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001243 spin_unlock_bh(&priv->tx_lock);
1244
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001245 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001246}
1247
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001248#define MWL8K_TXD_SUCCESS(status) \
1249 ((status) & (MWL8K_TXD_STATUS_OK | \
1250 MWL8K_TXD_STATUS_OK_RETRY | \
1251 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001252
1253static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1254{
1255 struct mwl8k_priv *priv = hw->priv;
1256 struct mwl8k_tx_queue *txq = priv->txq + index;
1257 int wake = 0;
1258
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001259 while (txq->stats.len > 0) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001260 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001261 struct mwl8k_tx_desc *tx_desc;
1262 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001263 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001264 struct sk_buff *skb;
1265 struct ieee80211_tx_info *info;
1266 u32 status;
1267
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001268 tx = txq->head;
1269 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001270
1271 status = le32_to_cpu(tx_desc->status);
1272
1273 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1274 if (!force)
1275 break;
1276 tx_desc->status &=
1277 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1278 }
1279
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001280 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1281 BUG_ON(txq->stats.len == 0);
1282 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001283 priv->pending_tx_pkts--;
1284
1285 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001286 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001287 skb = txq->skb[tx];
1288 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001289
1290 BUG_ON(skb == NULL);
1291 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1292
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001293 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001294
1295 /* Mark descriptor as unused */
1296 tx_desc->pkt_phys_addr = 0;
1297 tx_desc->pkt_len = 0;
1298
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299 info = IEEE80211_SKB_CB(skb);
1300 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001301 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001302 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303
1304 ieee80211_tx_status_irqsafe(hw, skb);
1305
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001306 wake = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001307 }
1308
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001309 if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001310 ieee80211_wake_queue(hw, index);
1311}
1312
1313/* must be called only when the card's transmit is completely halted */
1314static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1315{
1316 struct mwl8k_priv *priv = hw->priv;
1317 struct mwl8k_tx_queue *txq = priv->txq + index;
1318
1319 mwl8k_txq_reclaim(hw, index, 1);
1320
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001321 kfree(txq->skb);
1322 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001323
1324 pci_free_consistent(priv->pdev,
1325 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001326 txq->txd, txq->txd_dma);
1327 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001328}
1329
1330static int
1331mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1332{
1333 struct mwl8k_priv *priv = hw->priv;
1334 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001335 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001336 struct ieee80211_hdr *wh;
1337 struct mwl8k_tx_queue *txq;
1338 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001339 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001340 u32 txstatus;
1341 u8 txdatarate;
1342 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001343
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001344 wh = (struct ieee80211_hdr *)skb->data;
1345 if (ieee80211_is_data_qos(wh->frame_control))
1346 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1347 else
1348 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001349
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001350 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001351 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001352
1353 tx_info = IEEE80211_SKB_CB(skb);
1354 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001355
1356 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1357 u16 seqno = mwl8k_vif->seqno;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001358
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001359 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1360 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1361 mwl8k_vif->seqno = seqno++ % 4096;
1362 }
1363
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001364 /* Setup firmware control bit fields for each frame type. */
1365 txstatus = 0;
1366 txdatarate = 0;
1367 if (ieee80211_is_mgmt(wh->frame_control) ||
1368 ieee80211_is_ctl(wh->frame_control)) {
1369 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001370 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001371 } else if (ieee80211_is_data(wh->frame_control)) {
1372 txdatarate = 1;
1373 if (is_multicast_ether_addr(wh->addr1))
1374 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1375
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001376 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001377 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001378 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001379 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001380 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001381 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001382
1383 dma = pci_map_single(priv->pdev, skb->data,
1384 skb->len, PCI_DMA_TODEVICE);
1385
1386 if (pci_dma_mapping_error(priv->pdev, dma)) {
1387 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001388 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001389 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001390 return NETDEV_TX_OK;
1391 }
1392
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001393 spin_lock_bh(&priv->tx_lock);
1394
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001395 txq = priv->txq + index;
1396
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001397 BUG_ON(txq->skb[txq->tail] != NULL);
1398 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001399
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001400 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001401 tx->data_rate = txdatarate;
1402 tx->tx_priority = index;
1403 tx->qos_control = cpu_to_le16(qos);
1404 tx->pkt_phys_addr = cpu_to_le32(dma);
1405 tx->pkt_len = cpu_to_le16(skb->len);
1406 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001407 if (!priv->ap_fw && tx_info->control.sta != NULL)
1408 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1409 else
1410 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001411 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001412 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1413
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001414 txq->stats.count++;
1415 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001416 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001417
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001418 txq->tail++;
1419 if (txq->tail == MWL8K_TX_DESCS)
1420 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001421
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001422 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001423 ieee80211_stop_queue(hw, index);
1424
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001425 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001426
1427 spin_unlock_bh(&priv->tx_lock);
1428
1429 return NETDEV_TX_OK;
1430}
1431
1432
1433/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001434 * Firmware access.
1435 *
1436 * We have the following requirements for issuing firmware commands:
1437 * - Some commands require that the packet transmit path is idle when
1438 * the command is issued. (For simplicity, we'll just quiesce the
1439 * transmit path for every command.)
1440 * - There are certain sequences of commands that need to be issued to
1441 * the hardware sequentially, with no other intervening commands.
1442 *
1443 * This leads to an implementation of a "firmware lock" as a mutex that
1444 * can be taken recursively, and which is taken by both the low-level
1445 * command submission function (mwl8k_post_cmd) as well as any users of
1446 * that function that require issuing of an atomic sequence of commands,
1447 * and quiesces the transmit path whenever it's taken.
1448 */
1449static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1450{
1451 struct mwl8k_priv *priv = hw->priv;
1452
1453 if (priv->fw_mutex_owner != current) {
1454 int rc;
1455
1456 mutex_lock(&priv->fw_mutex);
1457 ieee80211_stop_queues(hw);
1458
1459 rc = mwl8k_tx_wait_empty(hw);
1460 if (rc) {
1461 ieee80211_wake_queues(hw);
1462 mutex_unlock(&priv->fw_mutex);
1463
1464 return rc;
1465 }
1466
1467 priv->fw_mutex_owner = current;
1468 }
1469
1470 priv->fw_mutex_depth++;
1471
1472 return 0;
1473}
1474
1475static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1476{
1477 struct mwl8k_priv *priv = hw->priv;
1478
1479 if (!--priv->fw_mutex_depth) {
1480 ieee80211_wake_queues(hw);
1481 priv->fw_mutex_owner = NULL;
1482 mutex_unlock(&priv->fw_mutex);
1483 }
1484}
1485
1486
1487/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001488 * Command processing.
1489 */
1490
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001491/* Timeout firmware commands after 10s */
1492#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001493
1494static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1495{
1496 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1497 struct mwl8k_priv *priv = hw->priv;
1498 void __iomem *regs = priv->regs;
1499 dma_addr_t dma_addr;
1500 unsigned int dma_size;
1501 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001502 unsigned long timeout = 0;
1503 u8 buf[32];
1504
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001505 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001506 dma_size = le16_to_cpu(cmd->length);
1507 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1508 PCI_DMA_BIDIRECTIONAL);
1509 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1510 return -ENOMEM;
1511
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001512 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001513 if (rc) {
1514 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1515 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001516 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001517 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001518
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001519 priv->hostcmd_wait = &cmd_wait;
1520 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1521 iowrite32(MWL8K_H2A_INT_DOORBELL,
1522 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1523 iowrite32(MWL8K_H2A_INT_DUMMY,
1524 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001525
1526 timeout = wait_for_completion_timeout(&cmd_wait,
1527 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1528
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001529 priv->hostcmd_wait = NULL;
1530
1531 mwl8k_fw_unlock(hw);
1532
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001533 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1534 PCI_DMA_BIDIRECTIONAL);
1535
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001536 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001537 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001538 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001539 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1540 MWL8K_CMD_TIMEOUT_MS);
1541 rc = -ETIMEDOUT;
1542 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001543 int ms;
1544
1545 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1546
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001547 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001548 if (rc)
1549 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001550 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001551 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001552 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001553 else if (ms > 2000)
1554 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1555 wiphy_name(hw->wiphy),
1556 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1557 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001558 }
1559
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001560 return rc;
1561}
1562
1563/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001564 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001565 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001566struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001567 struct mwl8k_cmd_pkt header;
1568 __u8 hw_rev;
1569 __u8 host_interface;
1570 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001571 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001572 __le16 region_code;
1573 __le32 fw_rev;
1574 __le32 ps_cookie;
1575 __le32 caps;
1576 __u8 mcs_bitmap[16];
1577 __le32 rx_queue_ptr;
1578 __le32 num_tx_queues;
1579 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1580 __le32 caps2;
1581 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001582 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001583} __attribute__((packed));
1584
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001585static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001586{
1587 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001588 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001589 int rc;
1590 int i;
1591
1592 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1593 if (cmd == NULL)
1594 return -ENOMEM;
1595
1596 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1597 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1598
1599 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1600 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001601 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001602 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001603 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001604 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001605 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001606 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001607
1608 rc = mwl8k_post_cmd(hw, &cmd->header);
1609
1610 if (!rc) {
1611 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1612 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001613 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001614 priv->hw_rev = cmd->hw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001615 }
1616
1617 kfree(cmd);
1618 return rc;
1619}
1620
1621/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001622 * CMD_GET_HW_SPEC (AP version).
1623 */
1624struct mwl8k_cmd_get_hw_spec_ap {
1625 struct mwl8k_cmd_pkt header;
1626 __u8 hw_rev;
1627 __u8 host_interface;
1628 __le16 num_wcb;
1629 __le16 num_mcaddrs;
1630 __u8 perm_addr[ETH_ALEN];
1631 __le16 region_code;
1632 __le16 num_antenna;
1633 __le32 fw_rev;
1634 __le32 wcbbase0;
1635 __le32 rxwrptr;
1636 __le32 rxrdptr;
1637 __le32 ps_cookie;
1638 __le32 wcbbase1;
1639 __le32 wcbbase2;
1640 __le32 wcbbase3;
1641} __attribute__((packed));
1642
1643static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1644{
1645 struct mwl8k_priv *priv = hw->priv;
1646 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1647 int rc;
1648
1649 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1650 if (cmd == NULL)
1651 return -ENOMEM;
1652
1653 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1654 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1655
1656 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1657 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1658
1659 rc = mwl8k_post_cmd(hw, &cmd->header);
1660
1661 if (!rc) {
1662 int off;
1663
1664 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1665 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1666 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1667 priv->hw_rev = cmd->hw_rev;
1668
1669 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1670 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1671
1672 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1673 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1674
1675 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1676 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1677
1678 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1679 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1680
1681 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1682 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1683
1684 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1685 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1686 }
1687
1688 kfree(cmd);
1689 return rc;
1690}
1691
1692/*
1693 * CMD_SET_HW_SPEC.
1694 */
1695struct mwl8k_cmd_set_hw_spec {
1696 struct mwl8k_cmd_pkt header;
1697 __u8 hw_rev;
1698 __u8 host_interface;
1699 __le16 num_mcaddrs;
1700 __u8 perm_addr[ETH_ALEN];
1701 __le16 region_code;
1702 __le32 fw_rev;
1703 __le32 ps_cookie;
1704 __le32 caps;
1705 __le32 rx_queue_ptr;
1706 __le32 num_tx_queues;
1707 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1708 __le32 flags;
1709 __le32 num_tx_desc_per_queue;
1710 __le32 total_rxd;
1711} __attribute__((packed));
1712
1713#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1714
1715static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1716{
1717 struct mwl8k_priv *priv = hw->priv;
1718 struct mwl8k_cmd_set_hw_spec *cmd;
1719 int rc;
1720 int i;
1721
1722 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1723 if (cmd == NULL)
1724 return -ENOMEM;
1725
1726 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1727 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1728
1729 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1730 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1731 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1732 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1733 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1734 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1735 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1736 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1737
1738 rc = mwl8k_post_cmd(hw, &cmd->header);
1739 kfree(cmd);
1740
1741 return rc;
1742}
1743
1744/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001745 * CMD_MAC_MULTICAST_ADR.
1746 */
1747struct mwl8k_cmd_mac_multicast_adr {
1748 struct mwl8k_cmd_pkt header;
1749 __le16 action;
1750 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001751 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001752};
1753
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001754#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1755#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1756#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1757#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001758
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001759static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001760__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001761 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001763 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001764 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001765 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001766
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001767 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001768 allmulti = 1;
1769 mc_count = 0;
1770 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001771
1772 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1773
1774 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001775 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001776 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001777
1778 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1779 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001780 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1781 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001782
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001783 if (allmulti) {
1784 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1785 } else if (mc_count) {
1786 int i;
1787
1788 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1789 cmd->numaddr = cpu_to_le16(mc_count);
1790 for (i = 0; i < mc_count && mclist; i++) {
1791 if (mclist->da_addrlen != ETH_ALEN) {
1792 kfree(cmd);
1793 return NULL;
1794 }
1795 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1796 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001797 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001798 }
1799
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001800 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001801}
1802
1803/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001804 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001805 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001806struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001807 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001808 __le32 stats[64];
1809} __attribute__((packed));
1810
1811#define MWL8K_STAT_ACK_FAILURE 9
1812#define MWL8K_STAT_RTS_FAILURE 12
1813#define MWL8K_STAT_FCS_ERROR 24
1814#define MWL8K_STAT_RTS_SUCCESS 11
1815
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001816static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1817 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001818{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001819 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001820 int rc;
1821
1822 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1823 if (cmd == NULL)
1824 return -ENOMEM;
1825
1826 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1827 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001828
1829 rc = mwl8k_post_cmd(hw, &cmd->header);
1830 if (!rc) {
1831 stats->dot11ACKFailureCount =
1832 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1833 stats->dot11RTSFailureCount =
1834 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1835 stats->dot11FCSErrorCount =
1836 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1837 stats->dot11RTSSuccessCount =
1838 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1839 }
1840 kfree(cmd);
1841
1842 return rc;
1843}
1844
1845/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001846 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001847 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001848struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001849 struct mwl8k_cmd_pkt header;
1850 __le16 action;
1851 __le16 control;
1852 __le16 radio_on;
1853} __attribute__((packed));
1854
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001855static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001856mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001857{
1858 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001859 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001860 int rc;
1861
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001862 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001863 return 0;
1864
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001865 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1866 if (cmd == NULL)
1867 return -ENOMEM;
1868
1869 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1870 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1871 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001872 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001873 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1874
1875 rc = mwl8k_post_cmd(hw, &cmd->header);
1876 kfree(cmd);
1877
1878 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001879 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001880
1881 return rc;
1882}
1883
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001884static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001885{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001886 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001887}
1888
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001889static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001890{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001891 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02001892}
1893
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001894static int
1895mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1896{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01001897 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001898
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02001899 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001900
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001901 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001902}
1903
1904/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001905 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001906 */
1907#define MWL8K_TX_POWER_LEVEL_TOTAL 8
1908
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001909struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001910 struct mwl8k_cmd_pkt header;
1911 __le16 action;
1912 __le16 support_level;
1913 __le16 current_level;
1914 __le16 reserved;
1915 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1916} __attribute__((packed));
1917
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001918static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001920 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001921 int rc;
1922
1923 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1924 if (cmd == NULL)
1925 return -ENOMEM;
1926
1927 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1928 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1929 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1930 cmd->support_level = cpu_to_le16(dBm);
1931
1932 rc = mwl8k_post_cmd(hw, &cmd->header);
1933 kfree(cmd);
1934
1935 return rc;
1936}
1937
1938/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02001939 * CMD_RF_ANTENNA.
1940 */
1941struct mwl8k_cmd_rf_antenna {
1942 struct mwl8k_cmd_pkt header;
1943 __le16 antenna;
1944 __le16 mode;
1945} __attribute__((packed));
1946
1947#define MWL8K_RF_ANTENNA_RX 1
1948#define MWL8K_RF_ANTENNA_TX 2
1949
1950static int
1951mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
1952{
1953 struct mwl8k_cmd_rf_antenna *cmd;
1954 int rc;
1955
1956 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1957 if (cmd == NULL)
1958 return -ENOMEM;
1959
1960 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
1961 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1962 cmd->antenna = cpu_to_le16(antenna);
1963 cmd->mode = cpu_to_le16(mask);
1964
1965 rc = mwl8k_post_cmd(hw, &cmd->header);
1966 kfree(cmd);
1967
1968 return rc;
1969}
1970
1971/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001972 * CMD_SET_PRE_SCAN.
1973 */
1974struct mwl8k_cmd_set_pre_scan {
1975 struct mwl8k_cmd_pkt header;
1976} __attribute__((packed));
1977
1978static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
1979{
1980 struct mwl8k_cmd_set_pre_scan *cmd;
1981 int rc;
1982
1983 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1984 if (cmd == NULL)
1985 return -ENOMEM;
1986
1987 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
1988 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1989
1990 rc = mwl8k_post_cmd(hw, &cmd->header);
1991 kfree(cmd);
1992
1993 return rc;
1994}
1995
1996/*
1997 * CMD_SET_POST_SCAN.
1998 */
1999struct mwl8k_cmd_set_post_scan {
2000 struct mwl8k_cmd_pkt header;
2001 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002002 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002003} __attribute__((packed));
2004
2005static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002006mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002007{
2008 struct mwl8k_cmd_set_post_scan *cmd;
2009 int rc;
2010
2011 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2012 if (cmd == NULL)
2013 return -ENOMEM;
2014
2015 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2016 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2017 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002018 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002019
2020 rc = mwl8k_post_cmd(hw, &cmd->header);
2021 kfree(cmd);
2022
2023 return rc;
2024}
2025
2026/*
2027 * CMD_SET_RF_CHANNEL.
2028 */
2029struct mwl8k_cmd_set_rf_channel {
2030 struct mwl8k_cmd_pkt header;
2031 __le16 action;
2032 __u8 current_channel;
2033 __le32 channel_flags;
2034} __attribute__((packed));
2035
2036static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2037 struct ieee80211_channel *channel)
2038{
2039 struct mwl8k_cmd_set_rf_channel *cmd;
2040 int rc;
2041
2042 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2043 if (cmd == NULL)
2044 return -ENOMEM;
2045
2046 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2047 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2048 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2049 cmd->current_channel = channel->hw_value;
2050 if (channel->band == IEEE80211_BAND_2GHZ)
2051 cmd->channel_flags = cpu_to_le32(0x00000081);
2052 else
2053 cmd->channel_flags = cpu_to_le32(0x00000000);
2054
2055 rc = mwl8k_post_cmd(hw, &cmd->header);
2056 kfree(cmd);
2057
2058 return rc;
2059}
2060
2061/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002062 * CMD_SET_AID.
2063 */
2064#define MWL8K_FRAME_PROT_DISABLED 0x00
2065#define MWL8K_FRAME_PROT_11G 0x07
2066#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2067#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2068
2069struct mwl8k_cmd_update_set_aid {
2070 struct mwl8k_cmd_pkt header;
2071 __le16 aid;
2072
2073 /* AP's MAC address (BSSID) */
2074 __u8 bssid[ETH_ALEN];
2075 __le16 protection_mode;
2076 __u8 supp_rates[14];
2077} __attribute__((packed));
2078
2079static int
2080mwl8k_cmd_set_aid(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2081{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002082 struct mwl8k_cmd_update_set_aid *cmd;
2083 u16 prot_mode;
2084 int rc;
2085
2086 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2087 if (cmd == NULL)
2088 return -ENOMEM;
2089
2090 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2091 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002092 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002093
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002094 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002095
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002096 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002097 prot_mode = MWL8K_FRAME_PROT_11G;
2098 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002099 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002100 IEEE80211_HT_OP_MODE_PROTECTION) {
2101 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2102 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2103 break;
2104 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2105 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2106 break;
2107 default:
2108 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2109 break;
2110 }
2111 }
2112 cmd->protection_mode = cpu_to_le16(prot_mode);
2113
2114 memcpy(cmd->supp_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2115
2116 rc = mwl8k_post_cmd(hw, &cmd->header);
2117 kfree(cmd);
2118
2119 return rc;
2120}
2121
2122/*
2123 * CMD_SET_RATE.
2124 */
2125struct mwl8k_cmd_set_rate {
2126 struct mwl8k_cmd_pkt header;
2127 __u8 legacy_rates[14];
2128
2129 /* Bitmap for supported MCS codes. */
2130 __u8 mcs_set[16];
2131 __u8 reserved[16];
2132} __attribute__((packed));
2133
2134static int
2135mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2136{
2137 struct mwl8k_cmd_set_rate *cmd;
2138 int rc;
2139
2140 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2141 if (cmd == NULL)
2142 return -ENOMEM;
2143
2144 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2145 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2146 memcpy(cmd->legacy_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2147
2148 rc = mwl8k_post_cmd(hw, &cmd->header);
2149 kfree(cmd);
2150
2151 return rc;
2152}
2153
2154/*
2155 * CMD_FINALIZE_JOIN.
2156 */
2157#define MWL8K_FJ_BEACON_MAXLEN 128
2158
2159struct mwl8k_cmd_finalize_join {
2160 struct mwl8k_cmd_pkt header;
2161 __le32 sleep_interval; /* Number of beacon periods to sleep */
2162 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2163} __attribute__((packed));
2164
2165static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2166 int framelen, int dtim)
2167{
2168 struct mwl8k_cmd_finalize_join *cmd;
2169 struct ieee80211_mgmt *payload = frame;
2170 int payload_len;
2171 int rc;
2172
2173 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2174 if (cmd == NULL)
2175 return -ENOMEM;
2176
2177 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2178 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2179 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2180
2181 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2182 if (payload_len < 0)
2183 payload_len = 0;
2184 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2185 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2186
2187 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2188
2189 rc = mwl8k_post_cmd(hw, &cmd->header);
2190 kfree(cmd);
2191
2192 return rc;
2193}
2194
2195/*
2196 * CMD_SET_RTS_THRESHOLD.
2197 */
2198struct mwl8k_cmd_set_rts_threshold {
2199 struct mwl8k_cmd_pkt header;
2200 __le16 action;
2201 __le16 threshold;
2202} __attribute__((packed));
2203
2204static int mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw,
2205 u16 action, u16 threshold)
2206{
2207 struct mwl8k_cmd_set_rts_threshold *cmd;
2208 int rc;
2209
2210 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2211 if (cmd == NULL)
2212 return -ENOMEM;
2213
2214 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2215 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2216 cmd->action = cpu_to_le16(action);
2217 cmd->threshold = cpu_to_le16(threshold);
2218
2219 rc = mwl8k_post_cmd(hw, &cmd->header);
2220 kfree(cmd);
2221
2222 return rc;
2223}
2224
2225/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002226 * CMD_SET_SLOT.
2227 */
2228struct mwl8k_cmd_set_slot {
2229 struct mwl8k_cmd_pkt header;
2230 __le16 action;
2231 __u8 short_slot;
2232} __attribute__((packed));
2233
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002234static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002235{
2236 struct mwl8k_cmd_set_slot *cmd;
2237 int rc;
2238
2239 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2240 if (cmd == NULL)
2241 return -ENOMEM;
2242
2243 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2244 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2245 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002246 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002247
2248 rc = mwl8k_post_cmd(hw, &cmd->header);
2249 kfree(cmd);
2250
2251 return rc;
2252}
2253
2254/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002255 * CMD_SET_EDCA_PARAMS.
2256 */
2257struct mwl8k_cmd_set_edca_params {
2258 struct mwl8k_cmd_pkt header;
2259
2260 /* See MWL8K_SET_EDCA_XXX below */
2261 __le16 action;
2262
2263 /* TX opportunity in units of 32 us */
2264 __le16 txop;
2265
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002266 union {
2267 struct {
2268 /* Log exponent of max contention period: 0...15 */
2269 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002270
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002271 /* Log exponent of min contention period: 0...15 */
2272 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002273
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002274 /* Adaptive interframe spacing in units of 32us */
2275 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002276
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002277 /* TX queue to configure */
2278 __u8 txq;
2279 } ap;
2280 struct {
2281 /* Log exponent of max contention period: 0...15 */
2282 __u8 log_cw_max;
2283
2284 /* Log exponent of min contention period: 0...15 */
2285 __u8 log_cw_min;
2286
2287 /* Adaptive interframe spacing in units of 32us */
2288 __u8 aifs;
2289
2290 /* TX queue to configure */
2291 __u8 txq;
2292 } sta;
2293 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002294} __attribute__((packed));
2295
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002296#define MWL8K_SET_EDCA_CW 0x01
2297#define MWL8K_SET_EDCA_TXOP 0x02
2298#define MWL8K_SET_EDCA_AIFS 0x04
2299
2300#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2301 MWL8K_SET_EDCA_TXOP | \
2302 MWL8K_SET_EDCA_AIFS)
2303
2304static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002305mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2306 __u16 cw_min, __u16 cw_max,
2307 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002308{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002309 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002310 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002311 int rc;
2312
2313 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2314 if (cmd == NULL)
2315 return -ENOMEM;
2316
Lennert Buytenhek22995b22009-10-22 20:20:25 +02002317 /*
2318 * Queues 0 (BE) and 1 (BK) are swapped in hardware for
2319 * this call.
2320 */
2321 qnum ^= !(qnum >> 1);
2322
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002323 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2324 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002325 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2326 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002327 if (priv->ap_fw) {
2328 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2329 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2330 cmd->ap.aifs = aifs;
2331 cmd->ap.txq = qnum;
2332 } else {
2333 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2334 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2335 cmd->sta.aifs = aifs;
2336 cmd->sta.txq = qnum;
2337 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002338
2339 rc = mwl8k_post_cmd(hw, &cmd->header);
2340 kfree(cmd);
2341
2342 return rc;
2343}
2344
2345/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002346 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002347 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002348struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002349 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002350 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002351} __attribute__((packed));
2352
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002353static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002354{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002355 struct mwl8k_priv *priv = hw->priv;
2356 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002357 int rc;
2358
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002359 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2360 if (cmd == NULL)
2361 return -ENOMEM;
2362
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002363 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002364 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002365 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002366
2367 rc = mwl8k_post_cmd(hw, &cmd->header);
2368 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002369
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002370 if (!rc)
2371 priv->wmm_enabled = enable;
2372
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002373 return rc;
2374}
2375
2376/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002377 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002378 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002379struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002380 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002381 __le32 action;
2382 __u8 rx_antenna_map;
2383 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002384} __attribute__((packed));
2385
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002386static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002387{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002388 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002389 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002390
2391 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2392 if (cmd == NULL)
2393 return -ENOMEM;
2394
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002395 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002396 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002397 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2398 cmd->rx_antenna_map = rx;
2399 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002400
2401 rc = mwl8k_post_cmd(hw, &cmd->header);
2402 kfree(cmd);
2403
2404 return rc;
2405}
2406
2407/*
2408 * CMD_USE_FIXED_RATE.
2409 */
2410#define MWL8K_RATE_TABLE_SIZE 8
2411#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002412#define MWL8K_USE_AUTO_RATE 0x0002
2413
2414struct mwl8k_rate_entry {
2415 /* Set to 1 if HT rate, 0 if legacy. */
2416 __le32 is_ht_rate;
2417
2418 /* Set to 1 to use retry_count field. */
2419 __le32 enable_retry;
2420
2421 /* Specified legacy rate or MCS. */
2422 __le32 rate;
2423
2424 /* Number of allowed retries. */
2425 __le32 retry_count;
2426} __attribute__((packed));
2427
2428struct mwl8k_rate_table {
2429 /* 1 to allow specified rate and below */
2430 __le32 allow_rate_drop;
2431 __le32 num_rates;
2432 struct mwl8k_rate_entry rate_entry[MWL8K_RATE_TABLE_SIZE];
2433} __attribute__((packed));
2434
2435struct mwl8k_cmd_use_fixed_rate {
2436 struct mwl8k_cmd_pkt header;
2437 __le32 action;
2438 struct mwl8k_rate_table rate_table;
2439
2440 /* Unicast, Broadcast or Multicast */
2441 __le32 rate_type;
2442 __le32 reserved1;
2443 __le32 reserved2;
2444} __attribute__((packed));
2445
2446static int mwl8k_cmd_use_fixed_rate(struct ieee80211_hw *hw,
2447 u32 action, u32 rate_type, struct mwl8k_rate_table *rate_table)
2448{
2449 struct mwl8k_cmd_use_fixed_rate *cmd;
2450 int count;
2451 int rc;
2452
2453 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2454 if (cmd == NULL)
2455 return -ENOMEM;
2456
2457 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2458 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2459
2460 cmd->action = cpu_to_le32(action);
2461 cmd->rate_type = cpu_to_le32(rate_type);
2462
2463 if (rate_table != NULL) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002464 /*
2465 * Copy over each field manually so that endian
2466 * conversion can be done.
2467 */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002468 cmd->rate_table.allow_rate_drop =
2469 cpu_to_le32(rate_table->allow_rate_drop);
2470 cmd->rate_table.num_rates =
2471 cpu_to_le32(rate_table->num_rates);
2472
2473 for (count = 0; count < rate_table->num_rates; count++) {
2474 struct mwl8k_rate_entry *dst =
2475 &cmd->rate_table.rate_entry[count];
2476 struct mwl8k_rate_entry *src =
2477 &rate_table->rate_entry[count];
2478
2479 dst->is_ht_rate = cpu_to_le32(src->is_ht_rate);
2480 dst->enable_retry = cpu_to_le32(src->enable_retry);
2481 dst->rate = cpu_to_le32(src->rate);
2482 dst->retry_count = cpu_to_le32(src->retry_count);
2483 }
2484 }
2485
2486 rc = mwl8k_post_cmd(hw, &cmd->header);
2487 kfree(cmd);
2488
2489 return rc;
2490}
2491
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002492/*
2493 * CMD_ENABLE_SNIFFER.
2494 */
2495struct mwl8k_cmd_enable_sniffer {
2496 struct mwl8k_cmd_pkt header;
2497 __le32 action;
2498} __attribute__((packed));
2499
2500static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2501{
2502 struct mwl8k_cmd_enable_sniffer *cmd;
2503 int rc;
2504
2505 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2506 if (cmd == NULL)
2507 return -ENOMEM;
2508
2509 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2510 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2511 cmd->action = cpu_to_le32(!!enable);
2512
2513 rc = mwl8k_post_cmd(hw, &cmd->header);
2514 kfree(cmd);
2515
2516 return rc;
2517}
2518
2519/*
2520 * CMD_SET_MAC_ADDR.
2521 */
2522struct mwl8k_cmd_set_mac_addr {
2523 struct mwl8k_cmd_pkt header;
2524 union {
2525 struct {
2526 __le16 mac_type;
2527 __u8 mac_addr[ETH_ALEN];
2528 } mbss;
2529 __u8 mac_addr[ETH_ALEN];
2530 };
2531} __attribute__((packed));
2532
2533static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2534{
2535 struct mwl8k_priv *priv = hw->priv;
2536 struct mwl8k_cmd_set_mac_addr *cmd;
2537 int rc;
2538
2539 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2540 if (cmd == NULL)
2541 return -ENOMEM;
2542
2543 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2544 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2545 if (priv->ap_fw) {
2546 cmd->mbss.mac_type = 0;
2547 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2548 } else {
2549 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2550 }
2551
2552 rc = mwl8k_post_cmd(hw, &cmd->header);
2553 kfree(cmd);
2554
2555 return rc;
2556}
2557
2558/*
2559 * CMD_SET_RATEADAPT_MODE.
2560 */
2561struct mwl8k_cmd_set_rate_adapt_mode {
2562 struct mwl8k_cmd_pkt header;
2563 __le16 action;
2564 __le16 mode;
2565} __attribute__((packed));
2566
2567static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2568{
2569 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2570 int rc;
2571
2572 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2573 if (cmd == NULL)
2574 return -ENOMEM;
2575
2576 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2577 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2578 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2579 cmd->mode = cpu_to_le16(mode);
2580
2581 rc = mwl8k_post_cmd(hw, &cmd->header);
2582 kfree(cmd);
2583
2584 return rc;
2585}
2586
2587/*
2588 * CMD_UPDATE_STADB.
2589 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002590struct ewc_ht_info {
2591 __le16 control1;
2592 __le16 control2;
2593 __le16 control3;
2594} __attribute__((packed));
2595
2596struct peer_capability_info {
2597 /* Peer type - AP vs. STA. */
2598 __u8 peer_type;
2599
2600 /* Basic 802.11 capabilities from assoc resp. */
2601 __le16 basic_caps;
2602
2603 /* Set if peer supports 802.11n high throughput (HT). */
2604 __u8 ht_support;
2605
2606 /* Valid if HT is supported. */
2607 __le16 ht_caps;
2608 __u8 extended_ht_caps;
2609 struct ewc_ht_info ewc_info;
2610
2611 /* Legacy rate table. Intersection of our rates and peer rates. */
2612 __u8 legacy_rates[12];
2613
2614 /* HT rate table. Intersection of our rates and peer rates. */
2615 __u8 ht_rates[16];
2616 __u8 pad[16];
2617
2618 /* If set, interoperability mode, no proprietary extensions. */
2619 __u8 interop;
2620 __u8 pad2;
2621 __u8 station_id;
2622 __le16 amsdu_enabled;
2623} __attribute__((packed));
2624
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002625struct mwl8k_cmd_update_stadb {
2626 struct mwl8k_cmd_pkt header;
2627
2628 /* See STADB_ACTION_TYPE */
2629 __le32 action;
2630
2631 /* Peer MAC address */
2632 __u8 peer_addr[ETH_ALEN];
2633
2634 __le32 reserved;
2635
2636 /* Peer info - valid during add/update. */
2637 struct peer_capability_info peer_info;
2638} __attribute__((packed));
2639
Lennert Buytenheka6804002010-01-04 21:55:42 +01002640#define MWL8K_STA_DB_MODIFY_ENTRY 1
2641#define MWL8K_STA_DB_DEL_ENTRY 2
2642
2643/* Peer Entry flags - used to define the type of the peer node */
2644#define MWL8K_PEER_TYPE_ACCESSPOINT 2
2645
2646static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
2647 struct ieee80211_vif *vif, u8 *addr)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002648{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002649 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01002650 struct peer_capability_info *p;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002651 int rc;
2652
2653 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2654 if (cmd == NULL)
2655 return -ENOMEM;
2656
2657 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2658 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01002659 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01002660 memcpy(cmd->peer_addr, addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002661
Lennert Buytenheka6804002010-01-04 21:55:42 +01002662 p = &cmd->peer_info;
2663 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2664 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
2665 memcpy(p->legacy_rates, mwl8k_rateids, sizeof(mwl8k_rateids));
2666 p->interop = 1;
2667 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002668
Lennert Buytenheka6804002010-01-04 21:55:42 +01002669 rc = mwl8k_post_cmd(hw, &cmd->header);
2670 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002671
Lennert Buytenheka6804002010-01-04 21:55:42 +01002672 return rc ? rc : p->station_id;
2673}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002674
Lennert Buytenheka6804002010-01-04 21:55:42 +01002675static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
2676 struct ieee80211_vif *vif, u8 *addr)
2677{
2678 struct mwl8k_cmd_update_stadb *cmd;
2679 int rc;
2680
2681 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2682 if (cmd == NULL)
2683 return -ENOMEM;
2684
2685 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2686 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2687 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
2688 memcpy(cmd->peer_addr, addr, ETH_ALEN);
2689
2690 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002691 kfree(cmd);
2692
2693 return rc;
2694}
2695
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002696
2697/*
2698 * Interrupt handling.
2699 */
2700static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2701{
2702 struct ieee80211_hw *hw = dev_id;
2703 struct mwl8k_priv *priv = hw->priv;
2704 u32 status;
2705
2706 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2707 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2708
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002709 if (!status)
2710 return IRQ_NONE;
2711
2712 if (status & MWL8K_A2H_INT_TX_DONE)
2713 tasklet_schedule(&priv->tx_reclaim_task);
2714
2715 if (status & MWL8K_A2H_INT_RX_READY) {
2716 while (rxq_process(hw, 0, 1))
2717 rxq_refill(hw, 0, 1);
2718 }
2719
2720 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002721 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002722 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002723 }
2724
2725 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002726 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02002727 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002728 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002729 }
2730
2731 return IRQ_HANDLED;
2732}
2733
2734
2735/*
2736 * Core driver operations.
2737 */
2738static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2739{
2740 struct mwl8k_priv *priv = hw->priv;
2741 int index = skb_get_queue_mapping(skb);
2742 int rc;
2743
2744 if (priv->current_channel == NULL) {
2745 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002746 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002747 dev_kfree_skb(skb);
2748 return NETDEV_TX_OK;
2749 }
2750
2751 rc = mwl8k_txq_xmit(hw, index, skb);
2752
2753 return rc;
2754}
2755
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002756static int mwl8k_start(struct ieee80211_hw *hw)
2757{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002758 struct mwl8k_priv *priv = hw->priv;
2759 int rc;
2760
Joe Perchesa0607fd2009-11-18 23:29:17 -08002761 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002762 IRQF_SHARED, MWL8K_NAME, hw);
2763 if (rc) {
2764 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02002765 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002766 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002767 }
2768
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002769 /* Enable tx reclaim tasklet */
2770 tasklet_enable(&priv->tx_reclaim_task);
2771
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002772 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02002773 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002774
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002775 rc = mwl8k_fw_lock(hw);
2776 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002777 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002778
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002779 if (!priv->ap_fw) {
2780 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002781 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002782
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02002783 if (!rc)
2784 rc = mwl8k_cmd_set_pre_scan(hw);
2785
2786 if (!rc)
2787 rc = mwl8k_cmd_set_post_scan(hw,
2788 "\x00\x00\x00\x00\x00\x00");
2789 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002790
2791 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002792 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002793
2794 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002795 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002796
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002797 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002798 }
2799
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02002800 if (rc) {
2801 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2802 free_irq(priv->pdev->irq, hw);
2803 tasklet_disable(&priv->tx_reclaim_task);
2804 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002805
2806 return rc;
2807}
2808
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002809static void mwl8k_stop(struct ieee80211_hw *hw)
2810{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002811 struct mwl8k_priv *priv = hw->priv;
2812 int i;
2813
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002814 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002815
2816 ieee80211_stop_queues(hw);
2817
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002818 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002819 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002820 free_irq(priv->pdev->irq, hw);
2821
2822 /* Stop finalize join worker */
2823 cancel_work_sync(&priv->finalize_join_worker);
2824 if (priv->beacon_skb != NULL)
2825 dev_kfree_skb(priv->beacon_skb);
2826
2827 /* Stop tx reclaim tasklet */
2828 tasklet_disable(&priv->tx_reclaim_task);
2829
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002830 /* Return all skbs to mac80211 */
2831 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2832 mwl8k_txq_reclaim(hw, i, 1);
2833}
2834
2835static int mwl8k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002836 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002837{
2838 struct mwl8k_priv *priv = hw->priv;
2839 struct mwl8k_vif *mwl8k_vif;
2840
2841 /*
2842 * We only support one active interface at a time.
2843 */
2844 if (priv->vif != NULL)
2845 return -EBUSY;
2846
2847 /*
2848 * We only support managed interfaces for now.
2849 */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002850 if (vif->type != NL80211_IFTYPE_STATION)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002851 return -EINVAL;
2852
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02002853 /*
2854 * Reject interface creation if sniffer mode is active, as
2855 * STA operation is mutually exclusive with hardware sniffer
2856 * mode.
2857 */
2858 if (priv->sniffer_enabled) {
2859 printk(KERN_INFO "%s: unable to create STA "
2860 "interface due to sniffer mode being enabled\n",
2861 wiphy_name(hw->wiphy));
2862 return -EINVAL;
2863 }
2864
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002865 /* Clean out driver private area */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002866 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002867 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
2868
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002869 /* Set and save the mac address */
Johannes Berg1ed32e42009-12-23 13:15:45 +01002870 mwl8k_cmd_set_mac_addr(hw, vif->addr);
2871 memcpy(mwl8k_vif->mac_addr, vif->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002872
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002873 /* Set Initial sequence number to zero */
2874 mwl8k_vif->seqno = 0;
2875
Johannes Berg1ed32e42009-12-23 13:15:45 +01002876 priv->vif = vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002877 priv->current_channel = NULL;
2878
2879 return 0;
2880}
2881
2882static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002883 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002884{
2885 struct mwl8k_priv *priv = hw->priv;
2886
2887 if (priv->vif == NULL)
2888 return;
2889
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002890 mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02002891
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002892 priv->vif = NULL;
2893}
2894
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002895static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002896{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002897 struct ieee80211_conf *conf = &hw->conf;
2898 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002899 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002900
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002901 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002902 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002903 priv->current_channel = NULL;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002904 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02002905 }
2906
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002907 rc = mwl8k_fw_lock(hw);
2908 if (rc)
2909 return rc;
2910
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002911 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002912 if (rc)
2913 goto out;
2914
2915 rc = mwl8k_cmd_set_rf_channel(hw, conf->channel);
2916 if (rc)
2917 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002918
2919 priv->current_channel = conf->channel;
2920
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002921 if (conf->power_level > 18)
2922 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002923 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002924 if (rc)
2925 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002926
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002927 if (priv->ap_fw) {
2928 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
2929 if (!rc)
2930 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
2931 } else {
2932 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
2933 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002934
Lennert Buytenhekee03a932009-07-17 07:19:37 +02002935out:
2936 mwl8k_fw_unlock(hw);
2937
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002938 return rc;
2939}
2940
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002941static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
2942 struct ieee80211_vif *vif,
2943 struct ieee80211_bss_conf *info,
2944 u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002945{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002946 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002947 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002948
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002949 if ((changed & BSS_CHANGED_ASSOC) == 0)
2950 return;
2951
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002952 priv->capture_beacon = false;
2953
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002954 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek942457d2009-08-24 15:42:36 +02002955 if (rc)
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002956 return;
2957
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002958 if (vif->bss_conf.assoc) {
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
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002981 /* Set AID */
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002982 rc = mwl8k_cmd_set_aid(hw, vif);
2983 if (rc)
2984 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002985
2986 /*
2987 * Finalize the join. Tell rx handler to process
2988 * next beacon from our BSSID.
2989 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002990 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002991 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002992 }
2993
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02002994out:
2995 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002996}
2997
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002998static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
2999 int mc_count, struct dev_addr_list *mclist)
3000{
3001 struct mwl8k_cmd_pkt *cmd;
3002
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003003 /*
3004 * Synthesize and return a command packet that programs the
3005 * hardware multicast address filter. At this point we don't
3006 * know whether FIF_ALLMULTI is being requested, but if it is,
3007 * we'll end up throwing this packet away and creating a new
3008 * one in mwl8k_configure_filter().
3009 */
3010 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003011
3012 return (unsigned long)cmd;
3013}
3014
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003015static int
3016mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3017 unsigned int changed_flags,
3018 unsigned int *total_flags)
3019{
3020 struct mwl8k_priv *priv = hw->priv;
3021
3022 /*
3023 * Hardware sniffer mode is mutually exclusive with STA
3024 * operation, so refuse to enable sniffer mode if a STA
3025 * interface is active.
3026 */
3027 if (priv->vif != NULL) {
3028 if (net_ratelimit())
3029 printk(KERN_INFO "%s: not enabling sniffer "
3030 "mode because STA interface is active\n",
3031 wiphy_name(hw->wiphy));
3032 return 0;
3033 }
3034
3035 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003036 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003037 return 0;
3038 priv->sniffer_enabled = true;
3039 }
3040
3041 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3042 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3043 FIF_OTHER_BSS;
3044
3045 return 1;
3046}
3047
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003048static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3049 unsigned int changed_flags,
3050 unsigned int *total_flags,
3051 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003052{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003053 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003054 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3055
3056 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003057 * AP firmware doesn't allow fine-grained control over
3058 * the receive filter.
3059 */
3060 if (priv->ap_fw) {
3061 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3062 kfree(cmd);
3063 return;
3064 }
3065
3066 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003067 * Enable hardware sniffer mode if FIF_CONTROL or
3068 * FIF_OTHER_BSS is requested.
3069 */
3070 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3071 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3072 kfree(cmd);
3073 return;
3074 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003075
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003076 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003077 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003078
3079 if (mwl8k_fw_lock(hw))
3080 return;
3081
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003082 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003083 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003084 priv->sniffer_enabled = false;
3085 }
3086
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003087 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003088 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3089 /*
3090 * Disable the BSS filter.
3091 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003092 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003093 } else {
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003094 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003095
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003096 /*
3097 * Enable the BSS filter.
3098 *
3099 * If there is an active STA interface, use that
3100 * interface's BSSID, otherwise use a dummy one
3101 * (where the OUI part needs to be nonzero for
3102 * the BSSID to be accepted by POST_SCAN).
3103 */
3104 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003105 if (priv->vif != NULL)
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003106 bssid = priv->vif->bss_conf.bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003107
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003108 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003109 }
3110 }
3111
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003112 /*
3113 * If FIF_ALLMULTI is being requested, throw away the command
3114 * packet that ->prepare_multicast() built and replace it with
3115 * a command packet that enables reception of all multicast
3116 * packets.
3117 */
3118 if (*total_flags & FIF_ALLMULTI) {
3119 kfree(cmd);
3120 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3121 }
3122
3123 if (cmd != NULL) {
3124 mwl8k_post_cmd(hw, cmd);
3125 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003126 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003127
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003128 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003129}
3130
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003131static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3132{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003133 return mwl8k_cmd_set_rts_threshold(hw, MWL8K_CMD_SET, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003134}
3135
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003136struct mwl8k_sta_notify_item
3137{
3138 struct list_head list;
3139 struct ieee80211_vif *vif;
3140 enum sta_notify_cmd cmd;
3141 u8 addr[ETH_ALEN];
3142};
3143
3144static void mwl8k_sta_notify_worker(struct work_struct *work)
3145{
3146 struct mwl8k_priv *priv =
3147 container_of(work, struct mwl8k_priv, sta_notify_worker);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003148 struct ieee80211_hw *hw = priv->hw;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003149
3150 spin_lock_bh(&priv->sta_notify_list_lock);
3151 while (!list_empty(&priv->sta_notify_list)) {
3152 struct mwl8k_sta_notify_item *s;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003153
3154 s = list_entry(priv->sta_notify_list.next,
3155 struct mwl8k_sta_notify_item, list);
3156 list_del(&s->list);
3157
3158 spin_unlock_bh(&priv->sta_notify_list_lock);
3159
Lennert Buytenheka6804002010-01-04 21:55:42 +01003160 if (s->cmd == STA_NOTIFY_ADD) {
3161 int rc;
3162
3163 rc = mwl8k_cmd_update_stadb_add(hw, s->vif, s->addr);
3164 if (rc >= 0) {
3165 struct ieee80211_sta *sta;
3166
3167 rcu_read_lock();
3168 sta = ieee80211_find_sta(s->vif, s->addr);
3169 if (sta != NULL)
3170 MWL8K_STA(sta)->peer_id = rc;
3171 rcu_read_unlock();
3172 }
3173 } else {
3174 mwl8k_cmd_update_stadb_del(hw, s->vif, s->addr);
3175 }
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003176
3177 kfree(s);
3178
3179 spin_lock_bh(&priv->sta_notify_list_lock);
3180 }
3181 spin_unlock_bh(&priv->sta_notify_list_lock);
3182}
3183
3184static void
3185mwl8k_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3186 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3187{
3188 struct mwl8k_priv *priv = hw->priv;
3189 struct mwl8k_sta_notify_item *s;
3190
3191 if (cmd != STA_NOTIFY_ADD && cmd != STA_NOTIFY_REMOVE)
3192 return;
3193
3194 s = kmalloc(sizeof(*s), GFP_ATOMIC);
3195 if (s != NULL) {
3196 s->vif = vif;
3197 s->cmd = cmd;
3198 memcpy(s->addr, sta->addr, ETH_ALEN);
3199
3200 spin_lock(&priv->sta_notify_list_lock);
3201 list_add_tail(&s->list, &priv->sta_notify_list);
3202 spin_unlock(&priv->sta_notify_list_lock);
3203
3204 ieee80211_queue_work(hw, &priv->sta_notify_worker);
3205 }
3206}
3207
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003208static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3209 const struct ieee80211_tx_queue_params *params)
3210{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003211 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003212 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003213
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003214 rc = mwl8k_fw_lock(hw);
3215 if (!rc) {
3216 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003217 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003218
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003219 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003220 rc = mwl8k_cmd_set_edca_params(hw, queue,
3221 params->cw_min,
3222 params->cw_max,
3223 params->aifs,
3224 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003225
3226 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003227 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003228
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003229 return rc;
3230}
3231
3232static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3233 struct ieee80211_tx_queue_stats *stats)
3234{
3235 struct mwl8k_priv *priv = hw->priv;
3236 struct mwl8k_tx_queue *txq;
3237 int index;
3238
3239 spin_lock_bh(&priv->tx_lock);
3240 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3241 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003242 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003243 sizeof(struct ieee80211_tx_queue_stats));
3244 }
3245 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003246
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003247 return 0;
3248}
3249
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003250static int mwl8k_get_stats(struct ieee80211_hw *hw,
3251 struct ieee80211_low_level_stats *stats)
3252{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003253 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003254}
3255
3256static const struct ieee80211_ops mwl8k_ops = {
3257 .tx = mwl8k_tx,
3258 .start = mwl8k_start,
3259 .stop = mwl8k_stop,
3260 .add_interface = mwl8k_add_interface,
3261 .remove_interface = mwl8k_remove_interface,
3262 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003263 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003264 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003265 .configure_filter = mwl8k_configure_filter,
3266 .set_rts_threshold = mwl8k_set_rts_threshold,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003267 .sta_notify = mwl8k_sta_notify,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003268 .conf_tx = mwl8k_conf_tx,
3269 .get_tx_stats = mwl8k_get_tx_stats,
3270 .get_stats = mwl8k_get_stats,
3271};
3272
3273static void mwl8k_tx_reclaim_handler(unsigned long data)
3274{
3275 int i;
3276 struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3277 struct mwl8k_priv *priv = hw->priv;
3278
3279 spin_lock_bh(&priv->tx_lock);
3280 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3281 mwl8k_txq_reclaim(hw, i, 0);
3282
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003283 if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003284 complete(priv->tx_wait);
3285 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003286 }
3287 spin_unlock_bh(&priv->tx_lock);
3288}
3289
3290static void mwl8k_finalize_join_worker(struct work_struct *work)
3291{
3292 struct mwl8k_priv *priv =
3293 container_of(work, struct mwl8k_priv, finalize_join_worker);
3294 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003295
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003296 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len,
3297 priv->vif->bss_conf.dtim_period);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003298 dev_kfree_skb(skb);
3299
3300 priv->beacon_skb = NULL;
3301}
3302
John W. Linvillebcb628d2009-11-06 16:40:16 -05003303enum {
3304 MWL8687 = 0,
3305 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003306};
3307
John W. Linvillebcb628d2009-11-06 16:40:16 -05003308static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003309 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003310 .part_name = "88w8687",
3311 .helper_image = "mwl8k/helper_8687.fw",
3312 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003313 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003314 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003315 .part_name = "88w8366",
3316 .helper_image = "mwl8k/helper_8366.fw",
3317 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003318 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003319 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003320};
3321
3322static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003323 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3324 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3325 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3326 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003327};
3328MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3329
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003330static int __devinit mwl8k_probe(struct pci_dev *pdev,
3331 const struct pci_device_id *id)
3332{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003333 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003334 struct ieee80211_hw *hw;
3335 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003336 int rc;
3337 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003338
3339 if (!printed_version) {
3340 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3341 printed_version = 1;
3342 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003343
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003344
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003345 rc = pci_enable_device(pdev);
3346 if (rc) {
3347 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3348 MWL8K_NAME);
3349 return rc;
3350 }
3351
3352 rc = pci_request_regions(pdev, MWL8K_NAME);
3353 if (rc) {
3354 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3355 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003356 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003357 }
3358
3359 pci_set_master(pdev);
3360
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003361
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003362 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3363 if (hw == NULL) {
3364 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3365 rc = -ENOMEM;
3366 goto err_free_reg;
3367 }
3368
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003369 SET_IEEE80211_DEV(hw, &pdev->dev);
3370 pci_set_drvdata(pdev, hw);
3371
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003372 priv = hw->priv;
3373 priv->hw = hw;
3374 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003375 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003376
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003377
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003378 priv->sram = pci_iomap(pdev, 0, 0x10000);
3379 if (priv->sram == NULL) {
3380 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003381 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003382 goto err_iounmap;
3383 }
3384
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003385 /*
3386 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3387 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3388 */
3389 priv->regs = pci_iomap(pdev, 1, 0x10000);
3390 if (priv->regs == NULL) {
3391 priv->regs = pci_iomap(pdev, 2, 0x10000);
3392 if (priv->regs == NULL) {
3393 printk(KERN_ERR "%s: Cannot map device registers\n",
3394 wiphy_name(hw->wiphy));
3395 goto err_iounmap;
3396 }
3397 }
3398
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003399
3400 /* Reset firmware and hardware */
3401 mwl8k_hw_reset(priv);
3402
3403 /* Ask userland hotplug daemon for the device firmware */
3404 rc = mwl8k_request_firmware(priv);
3405 if (rc) {
3406 printk(KERN_ERR "%s: Firmware files not found\n",
3407 wiphy_name(hw->wiphy));
3408 goto err_stop_firmware;
3409 }
3410
3411 /* Load firmware into hardware */
3412 rc = mwl8k_load_firmware(hw);
3413 if (rc) {
3414 printk(KERN_ERR "%s: Cannot start firmware\n",
3415 wiphy_name(hw->wiphy));
3416 goto err_stop_firmware;
3417 }
3418
3419 /* Reclaim memory once firmware is successfully loaded */
3420 mwl8k_release_firmware(priv);
3421
3422
Lennert Buytenhek91942232010-01-04 21:53:54 +01003423 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003424 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003425 if (priv->rxd_ops == NULL) {
3426 printk(KERN_ERR "%s: Driver does not have AP "
3427 "firmware image support for this hardware\n",
3428 wiphy_name(hw->wiphy));
3429 goto err_stop_firmware;
3430 }
3431 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003432 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003433 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003434
3435 priv->sniffer_enabled = false;
3436 priv->wmm_enabled = false;
3437 priv->pending_tx_pkts = 0;
3438
3439
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003440 memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3441 priv->band.band = IEEE80211_BAND_2GHZ;
3442 priv->band.channels = priv->channels;
3443 priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3444 priv->band.bitrates = priv->rates;
3445 priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3446 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3447
3448 BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3449 memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3450
3451 /*
3452 * Extra headroom is the size of the required DMA header
3453 * minus the size of the smallest 802.11 frame (CTS frame).
3454 */
3455 hw->extra_tx_headroom =
3456 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3457
3458 hw->channel_change_time = 10;
3459
3460 hw->queues = MWL8K_TX_QUEUES;
3461
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003462 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003463 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003464 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003465 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003466 priv->vif = NULL;
3467
3468 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003469 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003470 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003471
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003472 /* Station database handling */
3473 INIT_WORK(&priv->sta_notify_worker, mwl8k_sta_notify_worker);
3474 spin_lock_init(&priv->sta_notify_list_lock);
3475 INIT_LIST_HEAD(&priv->sta_notify_list);
3476
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003477 /* Finalize join worker */
3478 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3479
3480 /* TX reclaim tasklet */
3481 tasklet_init(&priv->tx_reclaim_task,
3482 mwl8k_tx_reclaim_handler, (unsigned long)hw);
3483 tasklet_disable(&priv->tx_reclaim_task);
3484
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003485 /* Power management cookie */
3486 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3487 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003488 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003489
3490 rc = mwl8k_rxq_init(hw, 0);
3491 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003492 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003493 rxq_refill(hw, 0, INT_MAX);
3494
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003495 mutex_init(&priv->fw_mutex);
3496 priv->fw_mutex_owner = NULL;
3497 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003498 priv->hostcmd_wait = NULL;
3499
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003500 spin_lock_init(&priv->tx_lock);
3501
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003502 priv->tx_wait = NULL;
3503
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003504 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3505 rc = mwl8k_txq_init(hw, i);
3506 if (rc)
3507 goto err_free_queues;
3508 }
3509
3510 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003511 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003512 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3513 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3514
Joe Perchesa0607fd2009-11-18 23:29:17 -08003515 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003516 IRQF_SHARED, MWL8K_NAME, hw);
3517 if (rc) {
3518 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003519 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003520 goto err_free_queues;
3521 }
3522
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003523 /*
3524 * Temporarily enable interrupts. Initial firmware host
3525 * commands use interrupts and avoids polling. Disable
3526 * interrupts when done.
3527 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003528 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003529
3530 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02003531 if (priv->ap_fw) {
3532 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3533 if (!rc)
3534 rc = mwl8k_cmd_set_hw_spec(hw);
3535 } else {
3536 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003537
3538 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02003539 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003540 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003541 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3542 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003543 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003544 }
3545
3546 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003547 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003548 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003549 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003550 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003551 }
3552
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003553 /* Clear MAC address */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003554 rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003555 if (rc) {
3556 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3557 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003558 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003559 }
3560
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003561 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003562 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003563 free_irq(priv->pdev->irq, hw);
3564
3565 rc = ieee80211_register_hw(hw);
3566 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003567 printk(KERN_ERR "%s: Cannot register device\n",
3568 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01003569 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003570 }
3571
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003572 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02003573 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003574 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02003575 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003576 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3577 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003578
3579 return 0;
3580
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003581err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003582 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003583 free_irq(priv->pdev->irq, hw);
3584
3585err_free_queues:
3586 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3587 mwl8k_txq_deinit(hw, i);
3588 mwl8k_rxq_deinit(hw, 0);
3589
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003590err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003591 if (priv->cookie != NULL)
3592 pci_free_consistent(priv->pdev, 4,
3593 priv->cookie, priv->cookie_dma);
3594
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003595err_stop_firmware:
3596 mwl8k_hw_reset(priv);
3597 mwl8k_release_firmware(priv);
3598
3599err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003600 if (priv->regs != NULL)
3601 pci_iounmap(pdev, priv->regs);
3602
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003603 if (priv->sram != NULL)
3604 pci_iounmap(pdev, priv->sram);
3605
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003606 pci_set_drvdata(pdev, NULL);
3607 ieee80211_free_hw(hw);
3608
3609err_free_reg:
3610 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003611
3612err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003613 pci_disable_device(pdev);
3614
3615 return rc;
3616}
3617
Joerg Albert230f7af2009-04-18 02:10:45 +02003618static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003619{
3620 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3621}
3622
Joerg Albert230f7af2009-04-18 02:10:45 +02003623static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003624{
3625 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3626 struct mwl8k_priv *priv;
3627 int i;
3628
3629 if (hw == NULL)
3630 return;
3631 priv = hw->priv;
3632
3633 ieee80211_stop_queues(hw);
3634
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02003635 ieee80211_unregister_hw(hw);
3636
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003637 /* Remove tx reclaim tasklet */
3638 tasklet_kill(&priv->tx_reclaim_task);
3639
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003640 /* Stop hardware */
3641 mwl8k_hw_reset(priv);
3642
3643 /* Return all skbs to mac80211 */
3644 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3645 mwl8k_txq_reclaim(hw, i, 1);
3646
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003647 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3648 mwl8k_txq_deinit(hw, i);
3649
3650 mwl8k_rxq_deinit(hw, 0);
3651
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003652 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003653
3654 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003655 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003656 pci_set_drvdata(pdev, NULL);
3657 ieee80211_free_hw(hw);
3658 pci_release_regions(pdev);
3659 pci_disable_device(pdev);
3660}
3661
3662static struct pci_driver mwl8k_driver = {
3663 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003664 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003665 .probe = mwl8k_probe,
3666 .remove = __devexit_p(mwl8k_remove),
3667 .shutdown = __devexit_p(mwl8k_shutdown),
3668};
3669
3670static int __init mwl8k_init(void)
3671{
3672 return pci_register_driver(&mwl8k_driver);
3673}
3674
3675static void __exit mwl8k_exit(void)
3676{
3677 pci_unregister_driver(&mwl8k_driver);
3678}
3679
3680module_init(mwl8k_init);
3681module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003682
3683MODULE_DESCRIPTION(MWL8K_DESC);
3684MODULE_VERSION(MWL8K_VERSION);
3685MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3686MODULE_LICENSE("GPL");