Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac802...
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / mwl8k.c
1 /*
2  * drivers/net/wireless/mwl8k.c
3  * Driver for Marvell TOPDOG 802.11 Wireless cards
4  *
5  * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
6  *
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/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/spinlock.h>
18 #include <linux/list.h>
19 #include <linux/pci.h>
20 #include <linux/delay.h>
21 #include <linux/completion.h>
22 #include <linux/etherdevice.h>
23 #include <linux/slab.h>
24 #include <net/mac80211.h>
25 #include <linux/moduleparam.h>
26 #include <linux/firmware.h>
27 #include <linux/workqueue.h>
28
29 #define MWL8K_DESC      "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
30 #define MWL8K_NAME      KBUILD_MODNAME
31 #define MWL8K_VERSION   "0.13"
32
33 /* Module parameters */
34 static bool ap_mode_default;
35 module_param(ap_mode_default, bool, 0);
36 MODULE_PARM_DESC(ap_mode_default,
37                  "Set to 1 to make ap mode the default instead of sta mode");
38
39 /* Register definitions */
40 #define MWL8K_HIU_GEN_PTR                       0x00000c10
41 #define  MWL8K_MODE_STA                          0x0000005a
42 #define  MWL8K_MODE_AP                           0x000000a5
43 #define MWL8K_HIU_INT_CODE                      0x00000c14
44 #define  MWL8K_FWSTA_READY                       0xf0f1f2f4
45 #define  MWL8K_FWAP_READY                        0xf1f2f4a5
46 #define  MWL8K_INT_CODE_CMD_FINISHED             0x00000005
47 #define MWL8K_HIU_SCRATCH                       0x00000c40
48
49 /* Host->device communications */
50 #define MWL8K_HIU_H2A_INTERRUPT_EVENTS          0x00000c18
51 #define MWL8K_HIU_H2A_INTERRUPT_STATUS          0x00000c1c
52 #define MWL8K_HIU_H2A_INTERRUPT_MASK            0x00000c20
53 #define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL       0x00000c24
54 #define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK     0x00000c28
55 #define  MWL8K_H2A_INT_DUMMY                     (1 << 20)
56 #define  MWL8K_H2A_INT_RESET                     (1 << 15)
57 #define  MWL8K_H2A_INT_DOORBELL                  (1 << 1)
58 #define  MWL8K_H2A_INT_PPA_READY                 (1 << 0)
59
60 /* Device->host communications */
61 #define MWL8K_HIU_A2H_INTERRUPT_EVENTS          0x00000c2c
62 #define MWL8K_HIU_A2H_INTERRUPT_STATUS          0x00000c30
63 #define MWL8K_HIU_A2H_INTERRUPT_MASK            0x00000c34
64 #define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL       0x00000c38
65 #define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK     0x00000c3c
66 #define  MWL8K_A2H_INT_DUMMY                     (1 << 20)
67 #define  MWL8K_A2H_INT_BA_WATCHDOG               (1 << 14)
68 #define  MWL8K_A2H_INT_CHNL_SWITCHED             (1 << 11)
69 #define  MWL8K_A2H_INT_QUEUE_EMPTY               (1 << 10)
70 #define  MWL8K_A2H_INT_RADAR_DETECT              (1 << 7)
71 #define  MWL8K_A2H_INT_RADIO_ON                  (1 << 6)
72 #define  MWL8K_A2H_INT_RADIO_OFF                 (1 << 5)
73 #define  MWL8K_A2H_INT_MAC_EVENT                 (1 << 3)
74 #define  MWL8K_A2H_INT_OPC_DONE                  (1 << 2)
75 #define  MWL8K_A2H_INT_RX_READY                  (1 << 1)
76 #define  MWL8K_A2H_INT_TX_DONE                   (1 << 0)
77
78 /* HW micro second timer register
79  * located at offset 0xA600. This
80  * will be used to timestamp tx
81  * packets.
82  */
83
84 #define MWL8K_HW_TIMER_REGISTER                 0x0000a600
85
86 #define MWL8K_A2H_EVENTS        (MWL8K_A2H_INT_DUMMY | \
87                                  MWL8K_A2H_INT_CHNL_SWITCHED | \
88                                  MWL8K_A2H_INT_QUEUE_EMPTY | \
89                                  MWL8K_A2H_INT_RADAR_DETECT | \
90                                  MWL8K_A2H_INT_RADIO_ON | \
91                                  MWL8K_A2H_INT_RADIO_OFF | \
92                                  MWL8K_A2H_INT_MAC_EVENT | \
93                                  MWL8K_A2H_INT_OPC_DONE | \
94                                  MWL8K_A2H_INT_RX_READY | \
95                                  MWL8K_A2H_INT_TX_DONE | \
96                                  MWL8K_A2H_INT_BA_WATCHDOG)
97
98 #define MWL8K_RX_QUEUES         1
99 #define MWL8K_TX_WMM_QUEUES     4
100 #define MWL8K_MAX_AMPDU_QUEUES  8
101 #define MWL8K_MAX_TX_QUEUES     (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
102 #define mwl8k_tx_queues(priv)   (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
103
104 /* txpriorities are mapped with hw queues.
105  * Each hw queue has a txpriority.
106  */
107 #define TOTAL_HW_TX_QUEUES      8
108
109 /* Each HW queue can have one AMPDU stream.
110  * But, because one of the hw queue is reserved,
111  * maximum AMPDU queues that can be created are
112  * one short of total tx queues.
113  */
114 #define MWL8K_NUM_AMPDU_STREAMS (TOTAL_HW_TX_QUEUES - 1)
115
116 struct rxd_ops {
117         int rxd_size;
118         void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
119         void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
120         int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
121                            __le16 *qos, s8 *noise);
122 };
123
124 struct mwl8k_device_info {
125         char *part_name;
126         char *helper_image;
127         char *fw_image_sta;
128         char *fw_image_ap;
129         struct rxd_ops *ap_rxd_ops;
130         u32 fw_api_ap;
131 };
132
133 struct mwl8k_rx_queue {
134         int rxd_count;
135
136         /* hw receives here */
137         int head;
138
139         /* refill descs here */
140         int tail;
141
142         void *rxd;
143         dma_addr_t rxd_dma;
144         struct {
145                 struct sk_buff *skb;
146                 DEFINE_DMA_UNMAP_ADDR(dma);
147         } *buf;
148 };
149
150 struct mwl8k_tx_queue {
151         /* hw transmits here */
152         int head;
153
154         /* sw appends here */
155         int tail;
156
157         unsigned int len;
158         struct mwl8k_tx_desc *txd;
159         dma_addr_t txd_dma;
160         struct sk_buff **skb;
161 };
162
163 enum {
164         AMPDU_NO_STREAM,
165         AMPDU_STREAM_NEW,
166         AMPDU_STREAM_IN_PROGRESS,
167         AMPDU_STREAM_ACTIVE,
168 };
169
170 struct mwl8k_ampdu_stream {
171         struct ieee80211_sta *sta;
172         u8 tid;
173         u8 state;
174         u8 idx;
175 };
176
177 struct mwl8k_priv {
178         struct ieee80211_hw *hw;
179         struct pci_dev *pdev;
180         int irq;
181
182         struct mwl8k_device_info *device_info;
183
184         void __iomem *sram;
185         void __iomem *regs;
186
187         /* firmware */
188         const struct firmware *fw_helper;
189         const struct firmware *fw_ucode;
190
191         /* hardware/firmware parameters */
192         bool ap_fw;
193         struct rxd_ops *rxd_ops;
194         struct ieee80211_supported_band band_24;
195         struct ieee80211_channel channels_24[14];
196         struct ieee80211_rate rates_24[14];
197         struct ieee80211_supported_band band_50;
198         struct ieee80211_channel channels_50[4];
199         struct ieee80211_rate rates_50[9];
200         u32 ap_macids_supported;
201         u32 sta_macids_supported;
202
203         /* Ampdu stream information */
204         u8 num_ampdu_queues;
205         spinlock_t stream_lock;
206         struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
207         struct work_struct watchdog_ba_handle;
208
209         /* firmware access */
210         struct mutex fw_mutex;
211         struct task_struct *fw_mutex_owner;
212         struct task_struct *hw_restart_owner;
213         int fw_mutex_depth;
214         struct completion *hostcmd_wait;
215
216         atomic_t watchdog_event_pending;
217
218         /* lock held over TX and TX reap */
219         spinlock_t tx_lock;
220
221         /* TX quiesce completion, protected by fw_mutex and tx_lock */
222         struct completion *tx_wait;
223
224         /* List of interfaces.  */
225         u32 macids_used;
226         struct list_head vif_list;
227
228         /* power management status cookie from firmware */
229         u32 *cookie;
230         dma_addr_t cookie_dma;
231
232         u16 num_mcaddrs;
233         u8 hw_rev;
234         u32 fw_rev;
235
236         /*
237          * Running count of TX packets in flight, to avoid
238          * iterating over the transmit rings each time.
239          */
240         int pending_tx_pkts;
241
242         struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
243         struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
244         u32 txq_offset[MWL8K_MAX_TX_QUEUES];
245
246         bool radio_on;
247         bool radio_short_preamble;
248         bool sniffer_enabled;
249         bool wmm_enabled;
250
251         /* XXX need to convert this to handle multiple interfaces */
252         bool capture_beacon;
253         u8 capture_bssid[ETH_ALEN];
254         struct sk_buff *beacon_skb;
255
256         /*
257          * This FJ worker has to be global as it is scheduled from the
258          * RX handler.  At this point we don't know which interface it
259          * belongs to until the list of bssids waiting to complete join
260          * is checked.
261          */
262         struct work_struct finalize_join_worker;
263
264         /* Tasklet to perform TX reclaim.  */
265         struct tasklet_struct poll_tx_task;
266
267         /* Tasklet to perform RX.  */
268         struct tasklet_struct poll_rx_task;
269
270         /* Most recently reported noise in dBm */
271         s8 noise;
272
273         /*
274          * preserve the queue configurations so they can be restored if/when
275          * the firmware image is swapped.
276          */
277         struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
278
279         /* To perform the task of reloading the firmware */
280         struct work_struct fw_reload;
281         bool hw_restart_in_progress;
282
283         /* async firmware loading state */
284         unsigned fw_state;
285         char *fw_pref;
286         char *fw_alt;
287         bool is_8764;
288         struct completion firmware_loading_complete;
289
290         /* bitmap of running BSSes */
291         u32 running_bsses;
292 };
293
294 #define MAX_WEP_KEY_LEN         13
295 #define NUM_WEP_KEYS            4
296
297 /* Per interface specific private data */
298 struct mwl8k_vif {
299         struct list_head list;
300         struct ieee80211_vif *vif;
301
302         /* Firmware macid for this vif.  */
303         int macid;
304
305         /* Non AMPDU sequence number assigned by driver.  */
306         u16 seqno;
307
308         /* Saved WEP keys */
309         struct {
310                 u8 enabled;
311                 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
312         } wep_key_conf[NUM_WEP_KEYS];
313
314         /* BSSID */
315         u8 bssid[ETH_ALEN];
316
317         /* A flag to indicate is HW crypto is enabled for this bssid */
318         bool is_hw_crypto_enabled;
319 };
320 #define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
321 #define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
322
323 struct tx_traffic_info {
324         u32 start_time;
325         u32 pkts;
326 };
327
328 #define MWL8K_MAX_TID 8
329 struct mwl8k_sta {
330         /* Index into station database. Returned by UPDATE_STADB.  */
331         u8 peer_id;
332         u8 is_ampdu_allowed;
333         struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
334 };
335 #define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
336
337 static const struct ieee80211_channel mwl8k_channels_24[] = {
338         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2412, .hw_value = 1, },
339         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2417, .hw_value = 2, },
340         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2422, .hw_value = 3, },
341         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2427, .hw_value = 4, },
342         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2432, .hw_value = 5, },
343         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2437, .hw_value = 6, },
344         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2442, .hw_value = 7, },
345         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2447, .hw_value = 8, },
346         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2452, .hw_value = 9, },
347         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2457, .hw_value = 10, },
348         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2462, .hw_value = 11, },
349         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2467, .hw_value = 12, },
350         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2472, .hw_value = 13, },
351         { .band = IEEE80211_BAND_2GHZ, .center_freq = 2484, .hw_value = 14, },
352 };
353
354 static const struct ieee80211_rate mwl8k_rates_24[] = {
355         { .bitrate = 10, .hw_value = 2, },
356         { .bitrate = 20, .hw_value = 4, },
357         { .bitrate = 55, .hw_value = 11, },
358         { .bitrate = 110, .hw_value = 22, },
359         { .bitrate = 220, .hw_value = 44, },
360         { .bitrate = 60, .hw_value = 12, },
361         { .bitrate = 90, .hw_value = 18, },
362         { .bitrate = 120, .hw_value = 24, },
363         { .bitrate = 180, .hw_value = 36, },
364         { .bitrate = 240, .hw_value = 48, },
365         { .bitrate = 360, .hw_value = 72, },
366         { .bitrate = 480, .hw_value = 96, },
367         { .bitrate = 540, .hw_value = 108, },
368         { .bitrate = 720, .hw_value = 144, },
369 };
370
371 static const struct ieee80211_channel mwl8k_channels_50[] = {
372         { .band = IEEE80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
373         { .band = IEEE80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
374         { .band = IEEE80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
375         { .band = IEEE80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
376 };
377
378 static const struct ieee80211_rate mwl8k_rates_50[] = {
379         { .bitrate = 60, .hw_value = 12, },
380         { .bitrate = 90, .hw_value = 18, },
381         { .bitrate = 120, .hw_value = 24, },
382         { .bitrate = 180, .hw_value = 36, },
383         { .bitrate = 240, .hw_value = 48, },
384         { .bitrate = 360, .hw_value = 72, },
385         { .bitrate = 480, .hw_value = 96, },
386         { .bitrate = 540, .hw_value = 108, },
387         { .bitrate = 720, .hw_value = 144, },
388 };
389
390 /* Set or get info from Firmware */
391 #define MWL8K_CMD_GET                   0x0000
392 #define MWL8K_CMD_SET                   0x0001
393 #define MWL8K_CMD_SET_LIST              0x0002
394
395 /* Firmware command codes */
396 #define MWL8K_CMD_CODE_DNLD             0x0001
397 #define MWL8K_CMD_GET_HW_SPEC           0x0003
398 #define MWL8K_CMD_SET_HW_SPEC           0x0004
399 #define MWL8K_CMD_MAC_MULTICAST_ADR     0x0010
400 #define MWL8K_CMD_GET_STAT              0x0014
401 #define MWL8K_CMD_RADIO_CONTROL         0x001c
402 #define MWL8K_CMD_RF_TX_POWER           0x001e
403 #define MWL8K_CMD_TX_POWER              0x001f
404 #define MWL8K_CMD_RF_ANTENNA            0x0020
405 #define MWL8K_CMD_SET_BEACON            0x0100          /* per-vif */
406 #define MWL8K_CMD_SET_PRE_SCAN          0x0107
407 #define MWL8K_CMD_SET_POST_SCAN         0x0108
408 #define MWL8K_CMD_SET_RF_CHANNEL        0x010a
409 #define MWL8K_CMD_SET_AID               0x010d
410 #define MWL8K_CMD_SET_RATE              0x0110
411 #define MWL8K_CMD_SET_FINALIZE_JOIN     0x0111
412 #define MWL8K_CMD_RTS_THRESHOLD         0x0113
413 #define MWL8K_CMD_SET_SLOT              0x0114
414 #define MWL8K_CMD_SET_EDCA_PARAMS       0x0115
415 #define MWL8K_CMD_SET_WMM_MODE          0x0123
416 #define MWL8K_CMD_MIMO_CONFIG           0x0125
417 #define MWL8K_CMD_USE_FIXED_RATE        0x0126
418 #define MWL8K_CMD_ENABLE_SNIFFER        0x0150
419 #define MWL8K_CMD_SET_MAC_ADDR          0x0202          /* per-vif */
420 #define MWL8K_CMD_SET_RATEADAPT_MODE    0x0203
421 #define MWL8K_CMD_GET_WATCHDOG_BITMAP   0x0205
422 #define MWL8K_CMD_DEL_MAC_ADDR          0x0206          /* per-vif */
423 #define MWL8K_CMD_BSS_START             0x1100          /* per-vif */
424 #define MWL8K_CMD_SET_NEW_STN           0x1111          /* per-vif */
425 #define MWL8K_CMD_UPDATE_ENCRYPTION     0x1122          /* per-vif */
426 #define MWL8K_CMD_UPDATE_STADB          0x1123
427 #define MWL8K_CMD_BASTREAM              0x1125
428
429 static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
430 {
431         u16 command = le16_to_cpu(cmd);
432
433 #define MWL8K_CMDNAME(x)        case MWL8K_CMD_##x: do {\
434                                         snprintf(buf, bufsize, "%s", #x);\
435                                         return buf;\
436                                         } while (0)
437         switch (command & ~0x8000) {
438                 MWL8K_CMDNAME(CODE_DNLD);
439                 MWL8K_CMDNAME(GET_HW_SPEC);
440                 MWL8K_CMDNAME(SET_HW_SPEC);
441                 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
442                 MWL8K_CMDNAME(GET_STAT);
443                 MWL8K_CMDNAME(RADIO_CONTROL);
444                 MWL8K_CMDNAME(RF_TX_POWER);
445                 MWL8K_CMDNAME(TX_POWER);
446                 MWL8K_CMDNAME(RF_ANTENNA);
447                 MWL8K_CMDNAME(SET_BEACON);
448                 MWL8K_CMDNAME(SET_PRE_SCAN);
449                 MWL8K_CMDNAME(SET_POST_SCAN);
450                 MWL8K_CMDNAME(SET_RF_CHANNEL);
451                 MWL8K_CMDNAME(SET_AID);
452                 MWL8K_CMDNAME(SET_RATE);
453                 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
454                 MWL8K_CMDNAME(RTS_THRESHOLD);
455                 MWL8K_CMDNAME(SET_SLOT);
456                 MWL8K_CMDNAME(SET_EDCA_PARAMS);
457                 MWL8K_CMDNAME(SET_WMM_MODE);
458                 MWL8K_CMDNAME(MIMO_CONFIG);
459                 MWL8K_CMDNAME(USE_FIXED_RATE);
460                 MWL8K_CMDNAME(ENABLE_SNIFFER);
461                 MWL8K_CMDNAME(SET_MAC_ADDR);
462                 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
463                 MWL8K_CMDNAME(BSS_START);
464                 MWL8K_CMDNAME(SET_NEW_STN);
465                 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
466                 MWL8K_CMDNAME(UPDATE_STADB);
467                 MWL8K_CMDNAME(BASTREAM);
468                 MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
469         default:
470                 snprintf(buf, bufsize, "0x%x", cmd);
471         }
472 #undef MWL8K_CMDNAME
473
474         return buf;
475 }
476
477 /* Hardware and firmware reset */
478 static void mwl8k_hw_reset(struct mwl8k_priv *priv)
479 {
480         iowrite32(MWL8K_H2A_INT_RESET,
481                 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
482         iowrite32(MWL8K_H2A_INT_RESET,
483                 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
484         msleep(20);
485 }
486
487 /* Release fw image */
488 static void mwl8k_release_fw(const struct firmware **fw)
489 {
490         if (*fw == NULL)
491                 return;
492         release_firmware(*fw);
493         *fw = NULL;
494 }
495
496 static void mwl8k_release_firmware(struct mwl8k_priv *priv)
497 {
498         mwl8k_release_fw(&priv->fw_ucode);
499         mwl8k_release_fw(&priv->fw_helper);
500 }
501
502 /* states for asynchronous f/w loading */
503 static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
504 enum {
505         FW_STATE_INIT = 0,
506         FW_STATE_LOADING_PREF,
507         FW_STATE_LOADING_ALT,
508         FW_STATE_ERROR,
509 };
510
511 /* Request fw image */
512 static int mwl8k_request_fw(struct mwl8k_priv *priv,
513                             const char *fname, const struct firmware **fw,
514                             bool nowait)
515 {
516         /* release current image */
517         if (*fw != NULL)
518                 mwl8k_release_fw(fw);
519
520         if (nowait)
521                 return request_firmware_nowait(THIS_MODULE, 1, fname,
522                                                &priv->pdev->dev, GFP_KERNEL,
523                                                priv, mwl8k_fw_state_machine);
524         else
525                 return request_firmware(fw, fname, &priv->pdev->dev);
526 }
527
528 static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
529                                   bool nowait)
530 {
531         struct mwl8k_device_info *di = priv->device_info;
532         int rc;
533
534         if (di->helper_image != NULL) {
535                 if (nowait)
536                         rc = mwl8k_request_fw(priv, di->helper_image,
537                                               &priv->fw_helper, true);
538                 else
539                         rc = mwl8k_request_fw(priv, di->helper_image,
540                                               &priv->fw_helper, false);
541                 if (rc)
542                         printk(KERN_ERR "%s: Error requesting helper fw %s\n",
543                                pci_name(priv->pdev), di->helper_image);
544
545                 if (rc || nowait)
546                         return rc;
547         }
548
549         if (nowait) {
550                 /*
551                  * if we get here, no helper image is needed.  Skip the
552                  * FW_STATE_INIT state.
553                  */
554                 priv->fw_state = FW_STATE_LOADING_PREF;
555                 rc = mwl8k_request_fw(priv, fw_image,
556                                       &priv->fw_ucode,
557                                       true);
558         } else
559                 rc = mwl8k_request_fw(priv, fw_image,
560                                       &priv->fw_ucode, false);
561         if (rc) {
562                 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
563                        pci_name(priv->pdev), fw_image);
564                 mwl8k_release_fw(&priv->fw_helper);
565                 return rc;
566         }
567
568         return 0;
569 }
570
571 struct mwl8k_cmd_pkt {
572         __le16  code;
573         __le16  length;
574         __u8    seq_num;
575         __u8    macid;
576         __le16  result;
577         char    payload[0];
578 } __packed;
579
580 /*
581  * Firmware loading.
582  */
583 static int
584 mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
585 {
586         void __iomem *regs = priv->regs;
587         dma_addr_t dma_addr;
588         int loops;
589
590         dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
591         if (pci_dma_mapping_error(priv->pdev, dma_addr))
592                 return -ENOMEM;
593
594         iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
595         iowrite32(0, regs + MWL8K_HIU_INT_CODE);
596         iowrite32(MWL8K_H2A_INT_DOORBELL,
597                 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
598         iowrite32(MWL8K_H2A_INT_DUMMY,
599                 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
600
601         loops = 1000;
602         do {
603                 u32 int_code;
604                 if (priv->is_8764) {
605                         int_code = ioread32(regs +
606                                             MWL8K_HIU_H2A_INTERRUPT_STATUS);
607                         if (int_code == 0)
608                                 break;
609                 } else {
610                         int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
611                         if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
612                                 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
613                                 break;
614                         }
615                 }
616                 cond_resched();
617                 udelay(1);
618         } while (--loops);
619
620         pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
621
622         return loops ? 0 : -ETIMEDOUT;
623 }
624
625 static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
626                                 const u8 *data, size_t length)
627 {
628         struct mwl8k_cmd_pkt *cmd;
629         int done;
630         int rc = 0;
631
632         cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
633         if (cmd == NULL)
634                 return -ENOMEM;
635
636         cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
637         cmd->seq_num = 0;
638         cmd->macid = 0;
639         cmd->result = 0;
640
641         done = 0;
642         while (length) {
643                 int block_size = length > 256 ? 256 : length;
644
645                 memcpy(cmd->payload, data + done, block_size);
646                 cmd->length = cpu_to_le16(block_size);
647
648                 rc = mwl8k_send_fw_load_cmd(priv, cmd,
649                                                 sizeof(*cmd) + block_size);
650                 if (rc)
651                         break;
652
653                 done += block_size;
654                 length -= block_size;
655         }
656
657         if (!rc) {
658                 cmd->length = 0;
659                 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
660         }
661
662         kfree(cmd);
663
664         return rc;
665 }
666
667 static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
668                                 const u8 *data, size_t length)
669 {
670         unsigned char *buffer;
671         int may_continue, rc = 0;
672         u32 done, prev_block_size;
673
674         buffer = kmalloc(1024, GFP_KERNEL);
675         if (buffer == NULL)
676                 return -ENOMEM;
677
678         done = 0;
679         prev_block_size = 0;
680         may_continue = 1000;
681         while (may_continue > 0) {
682                 u32 block_size;
683
684                 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
685                 if (block_size & 1) {
686                         block_size &= ~1;
687                         may_continue--;
688                 } else {
689                         done += prev_block_size;
690                         length -= prev_block_size;
691                 }
692
693                 if (block_size > 1024 || block_size > length) {
694                         rc = -EOVERFLOW;
695                         break;
696                 }
697
698                 if (length == 0) {
699                         rc = 0;
700                         break;
701                 }
702
703                 if (block_size == 0) {
704                         rc = -EPROTO;
705                         may_continue--;
706                         udelay(1);
707                         continue;
708                 }
709
710                 prev_block_size = block_size;
711                 memcpy(buffer, data + done, block_size);
712
713                 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
714                 if (rc)
715                         break;
716         }
717
718         if (!rc && length != 0)
719                 rc = -EREMOTEIO;
720
721         kfree(buffer);
722
723         return rc;
724 }
725
726 static int mwl8k_load_firmware(struct ieee80211_hw *hw)
727 {
728         struct mwl8k_priv *priv = hw->priv;
729         const struct firmware *fw = priv->fw_ucode;
730         int rc;
731         int loops;
732
733         if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
734                 const struct firmware *helper = priv->fw_helper;
735
736                 if (helper == NULL) {
737                         printk(KERN_ERR "%s: helper image needed but none "
738                                "given\n", pci_name(priv->pdev));
739                         return -EINVAL;
740                 }
741
742                 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
743                 if (rc) {
744                         printk(KERN_ERR "%s: unable to load firmware "
745                                "helper image\n", pci_name(priv->pdev));
746                         return rc;
747                 }
748                 msleep(20);
749
750                 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
751         } else {
752                 if (priv->is_8764)
753                         rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
754                 else
755                         rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
756         }
757
758         if (rc) {
759                 printk(KERN_ERR "%s: unable to load firmware image\n",
760                        pci_name(priv->pdev));
761                 return rc;
762         }
763
764         iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
765
766         loops = 500000;
767         do {
768                 u32 ready_code;
769
770                 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
771                 if (ready_code == MWL8K_FWAP_READY) {
772                         priv->ap_fw = true;
773                         break;
774                 } else if (ready_code == MWL8K_FWSTA_READY) {
775                         priv->ap_fw = false;
776                         break;
777                 }
778
779                 cond_resched();
780                 udelay(1);
781         } while (--loops);
782
783         return loops ? 0 : -ETIMEDOUT;
784 }
785
786
787 /* DMA header used by firmware and hardware.  */
788 struct mwl8k_dma_data {
789         __le16 fwlen;
790         struct ieee80211_hdr wh;
791         char data[0];
792 } __packed;
793
794 /* Routines to add/remove DMA header from skb.  */
795 static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
796 {
797         struct mwl8k_dma_data *tr;
798         int hdrlen;
799
800         tr = (struct mwl8k_dma_data *)skb->data;
801         hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
802
803         if (hdrlen != sizeof(tr->wh)) {
804                 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
805                         memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
806                         *((__le16 *)(tr->data - 2)) = qos;
807                 } else {
808                         memmove(tr->data - hdrlen, &tr->wh, hdrlen);
809                 }
810         }
811
812         if (hdrlen != sizeof(*tr))
813                 skb_pull(skb, sizeof(*tr) - hdrlen);
814 }
815
816 #define REDUCED_TX_HEADROOM     8
817
818 static void
819 mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
820                                                 int head_pad, int tail_pad)
821 {
822         struct ieee80211_hdr *wh;
823         int hdrlen;
824         int reqd_hdrlen;
825         struct mwl8k_dma_data *tr;
826
827         /*
828          * Add a firmware DMA header; the firmware requires that we
829          * present a 2-byte payload length followed by a 4-address
830          * header (without QoS field), followed (optionally) by any
831          * WEP/ExtIV header (but only filled in for CCMP).
832          */
833         wh = (struct ieee80211_hdr *)skb->data;
834
835         hdrlen = ieee80211_hdrlen(wh->frame_control);
836
837         /*
838          * Check if skb_resize is required because of
839          * tx_headroom adjustment.
840          */
841         if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
842                                                 + REDUCED_TX_HEADROOM))) {
843                 if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
844
845                         wiphy_err(priv->hw->wiphy,
846                                         "Failed to reallocate TX buffer\n");
847                         return;
848                 }
849                 skb->truesize += REDUCED_TX_HEADROOM;
850         }
851
852         reqd_hdrlen = sizeof(*tr) + head_pad;
853
854         if (hdrlen != reqd_hdrlen)
855                 skb_push(skb, reqd_hdrlen - hdrlen);
856
857         if (ieee80211_is_data_qos(wh->frame_control))
858                 hdrlen -= IEEE80211_QOS_CTL_LEN;
859
860         tr = (struct mwl8k_dma_data *)skb->data;
861         if (wh != &tr->wh)
862                 memmove(&tr->wh, wh, hdrlen);
863         if (hdrlen != sizeof(tr->wh))
864                 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
865
866         /*
867          * Firmware length is the length of the fully formed "802.11
868          * payload".  That is, everything except for the 802.11 header.
869          * This includes all crypto material including the MIC.
870          */
871         tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
872 }
873
874 static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
875                 struct sk_buff *skb)
876 {
877         struct ieee80211_hdr *wh;
878         struct ieee80211_tx_info *tx_info;
879         struct ieee80211_key_conf *key_conf;
880         int data_pad;
881         int head_pad = 0;
882
883         wh = (struct ieee80211_hdr *)skb->data;
884
885         tx_info = IEEE80211_SKB_CB(skb);
886
887         key_conf = NULL;
888         if (ieee80211_is_data(wh->frame_control))
889                 key_conf = tx_info->control.hw_key;
890
891         /*
892          * Make sure the packet header is in the DMA header format (4-address
893          * without QoS), and add head & tail padding when HW crypto is enabled.
894          *
895          * We have the following trailer padding requirements:
896          * - WEP: 4 trailer bytes (ICV)
897          * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
898          * - CCMP: 8 trailer bytes (MIC)
899          */
900         data_pad = 0;
901         if (key_conf != NULL) {
902                 head_pad = key_conf->iv_len;
903                 switch (key_conf->cipher) {
904                 case WLAN_CIPHER_SUITE_WEP40:
905                 case WLAN_CIPHER_SUITE_WEP104:
906                         data_pad = 4;
907                         break;
908                 case WLAN_CIPHER_SUITE_TKIP:
909                         data_pad = 12;
910                         break;
911                 case WLAN_CIPHER_SUITE_CCMP:
912                         data_pad = 8;
913                         break;
914                 }
915         }
916         mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
917 }
918
919 /*
920  * Packet reception for 88w8366/88w8764 AP firmware.
921  */
922 struct mwl8k_rxd_ap {
923         __le16 pkt_len;
924         __u8 sq2;
925         __u8 rate;
926         __le32 pkt_phys_addr;
927         __le32 next_rxd_phys_addr;
928         __le16 qos_control;
929         __le16 htsig2;
930         __le32 hw_rssi_info;
931         __le32 hw_noise_floor_info;
932         __u8 noise_floor;
933         __u8 pad0[3];
934         __u8 rssi;
935         __u8 rx_status;
936         __u8 channel;
937         __u8 rx_ctrl;
938 } __packed;
939
940 #define MWL8K_AP_RATE_INFO_MCS_FORMAT           0x80
941 #define MWL8K_AP_RATE_INFO_40MHZ                0x40
942 #define MWL8K_AP_RATE_INFO_RATEID(x)            ((x) & 0x3f)
943
944 #define MWL8K_AP_RX_CTRL_OWNED_BY_HOST          0x80
945
946 /* 8366/8764 AP rx_status bits */
947 #define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK                0x80
948 #define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR             0xFF
949 #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR            0x02
950 #define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR             0x04
951 #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR            0x08
952
953 static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
954 {
955         struct mwl8k_rxd_ap *rxd = _rxd;
956
957         rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
958         rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
959 }
960
961 static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
962 {
963         struct mwl8k_rxd_ap *rxd = _rxd;
964
965         rxd->pkt_len = cpu_to_le16(len);
966         rxd->pkt_phys_addr = cpu_to_le32(addr);
967         wmb();
968         rxd->rx_ctrl = 0;
969 }
970
971 static int
972 mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
973                      __le16 *qos, s8 *noise)
974 {
975         struct mwl8k_rxd_ap *rxd = _rxd;
976
977         if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
978                 return -1;
979         rmb();
980
981         memset(status, 0, sizeof(*status));
982
983         status->signal = -rxd->rssi;
984         *noise = -rxd->noise_floor;
985
986         if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
987                 status->flag |= RX_FLAG_HT;
988                 if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
989                         status->flag |= RX_FLAG_40MHZ;
990                 status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
991         } else {
992                 int i;
993
994                 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
995                         if (mwl8k_rates_24[i].hw_value == rxd->rate) {
996                                 status->rate_idx = i;
997                                 break;
998                         }
999                 }
1000         }
1001
1002         if (rxd->channel > 14) {
1003                 status->band = IEEE80211_BAND_5GHZ;
1004                 if (!(status->flag & RX_FLAG_HT))
1005                         status->rate_idx -= 5;
1006         } else {
1007                 status->band = IEEE80211_BAND_2GHZ;
1008         }
1009         status->freq = ieee80211_channel_to_frequency(rxd->channel,
1010                                                       status->band);
1011
1012         *qos = rxd->qos_control;
1013
1014         if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
1015             (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
1016             (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
1017                 status->flag |= RX_FLAG_MMIC_ERROR;
1018
1019         return le16_to_cpu(rxd->pkt_len);
1020 }
1021
1022 static struct rxd_ops rxd_ap_ops = {
1023         .rxd_size       = sizeof(struct mwl8k_rxd_ap),
1024         .rxd_init       = mwl8k_rxd_ap_init,
1025         .rxd_refill     = mwl8k_rxd_ap_refill,
1026         .rxd_process    = mwl8k_rxd_ap_process,
1027 };
1028
1029 /*
1030  * Packet reception for STA firmware.
1031  */
1032 struct mwl8k_rxd_sta {
1033         __le16 pkt_len;
1034         __u8 link_quality;
1035         __u8 noise_level;
1036         __le32 pkt_phys_addr;
1037         __le32 next_rxd_phys_addr;
1038         __le16 qos_control;
1039         __le16 rate_info;
1040         __le32 pad0[4];
1041         __u8 rssi;
1042         __u8 channel;
1043         __le16 pad1;
1044         __u8 rx_ctrl;
1045         __u8 rx_status;
1046         __u8 pad2[2];
1047 } __packed;
1048
1049 #define MWL8K_STA_RATE_INFO_SHORTPRE            0x8000
1050 #define MWL8K_STA_RATE_INFO_ANTSELECT(x)        (((x) >> 11) & 0x3)
1051 #define MWL8K_STA_RATE_INFO_RATEID(x)           (((x) >> 3) & 0x3f)
1052 #define MWL8K_STA_RATE_INFO_40MHZ               0x0004
1053 #define MWL8K_STA_RATE_INFO_SHORTGI             0x0002
1054 #define MWL8K_STA_RATE_INFO_MCS_FORMAT          0x0001
1055
1056 #define MWL8K_STA_RX_CTRL_OWNED_BY_HOST         0x02
1057 #define MWL8K_STA_RX_CTRL_DECRYPT_ERROR         0x04
1058 /* ICV=0 or MIC=1 */
1059 #define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE          0x08
1060 /* Key is uploaded only in failure case */
1061 #define MWL8K_STA_RX_CTRL_KEY_INDEX                     0x30
1062
1063 static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
1064 {
1065         struct mwl8k_rxd_sta *rxd = _rxd;
1066
1067         rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
1068         rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
1069 }
1070
1071 static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
1072 {
1073         struct mwl8k_rxd_sta *rxd = _rxd;
1074
1075         rxd->pkt_len = cpu_to_le16(len);
1076         rxd->pkt_phys_addr = cpu_to_le32(addr);
1077         wmb();
1078         rxd->rx_ctrl = 0;
1079 }
1080
1081 static int
1082 mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
1083                        __le16 *qos, s8 *noise)
1084 {
1085         struct mwl8k_rxd_sta *rxd = _rxd;
1086         u16 rate_info;
1087
1088         if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
1089                 return -1;
1090         rmb();
1091
1092         rate_info = le16_to_cpu(rxd->rate_info);
1093
1094         memset(status, 0, sizeof(*status));
1095
1096         status->signal = -rxd->rssi;
1097         *noise = -rxd->noise_level;
1098         status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1099         status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
1100
1101         if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
1102                 status->flag |= RX_FLAG_SHORTPRE;
1103         if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
1104                 status->flag |= RX_FLAG_40MHZ;
1105         if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
1106                 status->flag |= RX_FLAG_SHORT_GI;
1107         if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
1108                 status->flag |= RX_FLAG_HT;
1109
1110         if (rxd->channel > 14) {
1111                 status->band = IEEE80211_BAND_5GHZ;
1112                 if (!(status->flag & RX_FLAG_HT))
1113                         status->rate_idx -= 5;
1114         } else {
1115                 status->band = IEEE80211_BAND_2GHZ;
1116         }
1117         status->freq = ieee80211_channel_to_frequency(rxd->channel,
1118                                                       status->band);
1119
1120         *qos = rxd->qos_control;
1121         if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1122             (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1123                 status->flag |= RX_FLAG_MMIC_ERROR;
1124
1125         return le16_to_cpu(rxd->pkt_len);
1126 }
1127
1128 static struct rxd_ops rxd_sta_ops = {
1129         .rxd_size       = sizeof(struct mwl8k_rxd_sta),
1130         .rxd_init       = mwl8k_rxd_sta_init,
1131         .rxd_refill     = mwl8k_rxd_sta_refill,
1132         .rxd_process    = mwl8k_rxd_sta_process,
1133 };
1134
1135
1136 #define MWL8K_RX_DESCS          256
1137 #define MWL8K_RX_MAXSZ          3800
1138
1139 static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1140 {
1141         struct mwl8k_priv *priv = hw->priv;
1142         struct mwl8k_rx_queue *rxq = priv->rxq + index;
1143         int size;
1144         int i;
1145
1146         rxq->rxd_count = 0;
1147         rxq->head = 0;
1148         rxq->tail = 0;
1149
1150         size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
1151
1152         rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1153         if (rxq->rxd == NULL) {
1154                 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
1155                 return -ENOMEM;
1156         }
1157         memset(rxq->rxd, 0, size);
1158
1159         rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
1160         if (rxq->buf == NULL) {
1161                 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
1162                 return -ENOMEM;
1163         }
1164
1165         for (i = 0; i < MWL8K_RX_DESCS; i++) {
1166                 int desc_size;
1167                 void *rxd;
1168                 int nexti;
1169                 dma_addr_t next_dma_addr;
1170
1171                 desc_size = priv->rxd_ops->rxd_size;
1172                 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
1173
1174                 nexti = i + 1;
1175                 if (nexti == MWL8K_RX_DESCS)
1176                         nexti = 0;
1177                 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1178
1179                 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
1180         }
1181
1182         return 0;
1183 }
1184
1185 static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1186 {
1187         struct mwl8k_priv *priv = hw->priv;
1188         struct mwl8k_rx_queue *rxq = priv->rxq + index;
1189         int refilled;
1190
1191         refilled = 0;
1192         while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
1193                 struct sk_buff *skb;
1194                 dma_addr_t addr;
1195                 int rx;
1196                 void *rxd;
1197
1198                 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1199                 if (skb == NULL)
1200                         break;
1201
1202                 addr = pci_map_single(priv->pdev, skb->data,
1203                                       MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
1204
1205                 rxq->rxd_count++;
1206                 rx = rxq->tail++;
1207                 if (rxq->tail == MWL8K_RX_DESCS)
1208                         rxq->tail = 0;
1209                 rxq->buf[rx].skb = skb;
1210                 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
1211
1212                 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1213                 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
1214
1215                 refilled++;
1216         }
1217
1218         return refilled;
1219 }
1220
1221 /* Must be called only when the card's reception is completely halted */
1222 static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1223 {
1224         struct mwl8k_priv *priv = hw->priv;
1225         struct mwl8k_rx_queue *rxq = priv->rxq + index;
1226         int i;
1227
1228         if (rxq->rxd == NULL)
1229                 return;
1230
1231         for (i = 0; i < MWL8K_RX_DESCS; i++) {
1232                 if (rxq->buf[i].skb != NULL) {
1233                         pci_unmap_single(priv->pdev,
1234                                          dma_unmap_addr(&rxq->buf[i], dma),
1235                                          MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1236                         dma_unmap_addr_set(&rxq->buf[i], dma, 0);
1237
1238                         kfree_skb(rxq->buf[i].skb);
1239                         rxq->buf[i].skb = NULL;
1240                 }
1241         }
1242
1243         kfree(rxq->buf);
1244         rxq->buf = NULL;
1245
1246         pci_free_consistent(priv->pdev,
1247                             MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
1248                             rxq->rxd, rxq->rxd_dma);
1249         rxq->rxd = NULL;
1250 }
1251
1252
1253 /*
1254  * Scan a list of BSSIDs to process for finalize join.
1255  * Allows for extension to process multiple BSSIDs.
1256  */
1257 static inline int
1258 mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1259 {
1260         return priv->capture_beacon &&
1261                 ieee80211_is_beacon(wh->frame_control) &&
1262                 ether_addr_equal(wh->addr3, priv->capture_bssid);
1263 }
1264
1265 static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1266                                      struct sk_buff *skb)
1267 {
1268         struct mwl8k_priv *priv = hw->priv;
1269
1270         priv->capture_beacon = false;
1271         memset(priv->capture_bssid, 0, ETH_ALEN);
1272
1273         /*
1274          * Use GFP_ATOMIC as rxq_process is called from
1275          * the primary interrupt handler, memory allocation call
1276          * must not sleep.
1277          */
1278         priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1279         if (priv->beacon_skb != NULL)
1280                 ieee80211_queue_work(hw, &priv->finalize_join_worker);
1281 }
1282
1283 static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1284                                                    u8 *bssid)
1285 {
1286         struct mwl8k_vif *mwl8k_vif;
1287
1288         list_for_each_entry(mwl8k_vif,
1289                             vif_list, list) {
1290                 if (memcmp(bssid, mwl8k_vif->bssid,
1291                            ETH_ALEN) == 0)
1292                         return mwl8k_vif;
1293         }
1294
1295         return NULL;
1296 }
1297
1298 static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1299 {
1300         struct mwl8k_priv *priv = hw->priv;
1301         struct mwl8k_vif *mwl8k_vif = NULL;
1302         struct mwl8k_rx_queue *rxq = priv->rxq + index;
1303         int processed;
1304
1305         processed = 0;
1306         while (rxq->rxd_count && limit--) {
1307                 struct sk_buff *skb;
1308                 void *rxd;
1309                 int pkt_len;
1310                 struct ieee80211_rx_status status;
1311                 struct ieee80211_hdr *wh;
1312                 __le16 qos;
1313
1314                 skb = rxq->buf[rxq->head].skb;
1315                 if (skb == NULL)
1316                         break;
1317
1318                 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1319
1320                 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1321                                                         &priv->noise);
1322                 if (pkt_len < 0)
1323                         break;
1324
1325                 rxq->buf[rxq->head].skb = NULL;
1326
1327                 pci_unmap_single(priv->pdev,
1328                                  dma_unmap_addr(&rxq->buf[rxq->head], dma),
1329                                  MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1330                 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
1331
1332                 rxq->head++;
1333                 if (rxq->head == MWL8K_RX_DESCS)
1334                         rxq->head = 0;
1335
1336                 rxq->rxd_count--;
1337
1338                 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1339
1340                 /*
1341                  * Check for a pending join operation.  Save a
1342                  * copy of the beacon and schedule a tasklet to
1343                  * send a FINALIZE_JOIN command to the firmware.
1344                  */
1345                 if (mwl8k_capture_bssid(priv, (void *)skb->data))
1346                         mwl8k_save_beacon(hw, skb);
1347
1348                 if (ieee80211_has_protected(wh->frame_control)) {
1349
1350                         /* Check if hw crypto has been enabled for
1351                          * this bss. If yes, set the status flags
1352                          * accordingly
1353                          */
1354                         mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1355                                                                 wh->addr1);
1356
1357                         if (mwl8k_vif != NULL &&
1358                             mwl8k_vif->is_hw_crypto_enabled) {
1359                                 /*
1360                                  * When MMIC ERROR is encountered
1361                                  * by the firmware, payload is
1362                                  * dropped and only 32 bytes of
1363                                  * mwl8k Firmware header is sent
1364                                  * to the host.
1365                                  *
1366                                  * We need to add four bytes of
1367                                  * key information.  In it
1368                                  * MAC80211 expects keyidx set to
1369                                  * 0 for triggering Counter
1370                                  * Measure of MMIC failure.
1371                                  */
1372                                 if (status.flag & RX_FLAG_MMIC_ERROR) {
1373                                         struct mwl8k_dma_data *tr;
1374                                         tr = (struct mwl8k_dma_data *)skb->data;
1375                                         memset((void *)&(tr->data), 0, 4);
1376                                         pkt_len += 4;
1377                                 }
1378
1379                                 if (!ieee80211_is_auth(wh->frame_control))
1380                                         status.flag |= RX_FLAG_IV_STRIPPED |
1381                                                        RX_FLAG_DECRYPTED |
1382                                                        RX_FLAG_MMIC_STRIPPED;
1383                         }
1384                 }
1385
1386                 skb_put(skb, pkt_len);
1387                 mwl8k_remove_dma_header(skb, qos);
1388                 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1389                 ieee80211_rx_irqsafe(hw, skb);
1390
1391                 processed++;
1392         }
1393
1394         return processed;
1395 }
1396
1397
1398 /*
1399  * Packet transmission.
1400  */
1401
1402 #define MWL8K_TXD_STATUS_OK                     0x00000001
1403 #define MWL8K_TXD_STATUS_OK_RETRY               0x00000002
1404 #define MWL8K_TXD_STATUS_OK_MORE_RETRY          0x00000004
1405 #define MWL8K_TXD_STATUS_MULTICAST_TX           0x00000008
1406 #define MWL8K_TXD_STATUS_FW_OWNED               0x80000000
1407
1408 #define MWL8K_QOS_QLEN_UNSPEC                   0xff00
1409 #define MWL8K_QOS_ACK_POLICY_MASK               0x0060
1410 #define MWL8K_QOS_ACK_POLICY_NORMAL             0x0000
1411 #define MWL8K_QOS_ACK_POLICY_BLOCKACK           0x0060
1412 #define MWL8K_QOS_EOSP                          0x0010
1413
1414 struct mwl8k_tx_desc {
1415         __le32 status;
1416         __u8 data_rate;
1417         __u8 tx_priority;
1418         __le16 qos_control;
1419         __le32 pkt_phys_addr;
1420         __le16 pkt_len;
1421         __u8 dest_MAC_addr[ETH_ALEN];
1422         __le32 next_txd_phys_addr;
1423         __le32 timestamp;
1424         __le16 rate_info;
1425         __u8 peer_id;
1426         __u8 tx_frag_cnt;
1427 } __packed;
1428
1429 #define MWL8K_TX_DESCS          128
1430
1431 static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1432 {
1433         struct mwl8k_priv *priv = hw->priv;
1434         struct mwl8k_tx_queue *txq = priv->txq + index;
1435         int size;
1436         int i;
1437
1438         txq->len = 0;
1439         txq->head = 0;
1440         txq->tail = 0;
1441
1442         size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1443
1444         txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1445         if (txq->txd == NULL) {
1446                 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
1447                 return -ENOMEM;
1448         }
1449         memset(txq->txd, 0, size);
1450
1451         txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
1452         if (txq->skb == NULL) {
1453                 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
1454                 return -ENOMEM;
1455         }
1456
1457         for (i = 0; i < MWL8K_TX_DESCS; i++) {
1458                 struct mwl8k_tx_desc *tx_desc;
1459                 int nexti;
1460
1461                 tx_desc = txq->txd + i;
1462                 nexti = (i + 1) % MWL8K_TX_DESCS;
1463
1464                 tx_desc->status = 0;
1465                 tx_desc->next_txd_phys_addr =
1466                         cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
1467         }
1468
1469         return 0;
1470 }
1471
1472 static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1473 {
1474         iowrite32(MWL8K_H2A_INT_PPA_READY,
1475                 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1476         iowrite32(MWL8K_H2A_INT_DUMMY,
1477                 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1478         ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1479 }
1480
1481 static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
1482 {
1483         struct mwl8k_priv *priv = hw->priv;
1484         int i;
1485
1486         for (i = 0; i < mwl8k_tx_queues(priv); i++) {
1487                 struct mwl8k_tx_queue *txq = priv->txq + i;
1488                 int fw_owned = 0;
1489                 int drv_owned = 0;
1490                 int unused = 0;
1491                 int desc;
1492
1493                 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
1494                         struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1495                         u32 status;
1496
1497                         status = le32_to_cpu(tx_desc->status);
1498                         if (status & MWL8K_TXD_STATUS_FW_OWNED)
1499                                 fw_owned++;
1500                         else
1501                                 drv_owned++;
1502
1503                         if (tx_desc->pkt_len == 0)
1504                                 unused++;
1505                 }
1506
1507                 wiphy_err(hw->wiphy,
1508                           "txq[%d] len=%d head=%d tail=%d "
1509                           "fw_owned=%d drv_owned=%d unused=%d\n",
1510                           i,
1511                           txq->len, txq->head, txq->tail,
1512                           fw_owned, drv_owned, unused);
1513         }
1514 }
1515
1516 /*
1517  * Must be called with priv->fw_mutex held and tx queues stopped.
1518  */
1519 #define MWL8K_TX_WAIT_TIMEOUT_MS        5000
1520
1521 static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
1522 {
1523         struct mwl8k_priv *priv = hw->priv;
1524         DECLARE_COMPLETION_ONSTACK(tx_wait);
1525         int retry;
1526         int rc;
1527
1528         might_sleep();
1529
1530         /* Since fw restart is in progress, allow only the firmware
1531          * commands from the restart code and block the other
1532          * commands since they are going to fail in any case since
1533          * the firmware has crashed
1534          */
1535         if (priv->hw_restart_in_progress) {
1536                 if (priv->hw_restart_owner == current)
1537                         return 0;
1538                 else
1539                         return -EBUSY;
1540         }
1541
1542         if (atomic_read(&priv->watchdog_event_pending))
1543                 return 0;
1544
1545         /*
1546          * The TX queues are stopped at this point, so this test
1547          * doesn't need to take ->tx_lock.
1548          */
1549         if (!priv->pending_tx_pkts)
1550                 return 0;
1551
1552         retry = 0;
1553         rc = 0;
1554
1555         spin_lock_bh(&priv->tx_lock);
1556         priv->tx_wait = &tx_wait;
1557         while (!rc) {
1558                 int oldcount;
1559                 unsigned long timeout;
1560
1561                 oldcount = priv->pending_tx_pkts;
1562
1563                 spin_unlock_bh(&priv->tx_lock);
1564                 timeout = wait_for_completion_timeout(&tx_wait,
1565                             msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1566
1567                 if (atomic_read(&priv->watchdog_event_pending)) {
1568                         spin_lock_bh(&priv->tx_lock);
1569                         priv->tx_wait = NULL;
1570                         spin_unlock_bh(&priv->tx_lock);
1571                         return 0;
1572                 }
1573
1574                 spin_lock_bh(&priv->tx_lock);
1575
1576                 if (timeout) {
1577                         WARN_ON(priv->pending_tx_pkts);
1578                         if (retry)
1579                                 wiphy_notice(hw->wiphy, "tx rings drained\n");
1580                         break;
1581                 }
1582
1583                 if (priv->pending_tx_pkts < oldcount) {
1584                         wiphy_notice(hw->wiphy,
1585                                      "waiting for tx rings to drain (%d -> %d pkts)\n",
1586                                      oldcount, priv->pending_tx_pkts);
1587                         retry = 1;
1588                         continue;
1589                 }
1590
1591                 priv->tx_wait = NULL;
1592
1593                 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1594                           MWL8K_TX_WAIT_TIMEOUT_MS);
1595                 mwl8k_dump_tx_rings(hw);
1596                 priv->hw_restart_in_progress = true;
1597                 ieee80211_queue_work(hw, &priv->fw_reload);
1598
1599                 rc = -ETIMEDOUT;
1600         }
1601         priv->tx_wait = NULL;
1602         spin_unlock_bh(&priv->tx_lock);
1603
1604         return rc;
1605 }
1606
1607 #define MWL8K_TXD_SUCCESS(status)                               \
1608         ((status) & (MWL8K_TXD_STATUS_OK |                      \
1609                      MWL8K_TXD_STATUS_OK_RETRY |                \
1610                      MWL8K_TXD_STATUS_OK_MORE_RETRY))
1611
1612 static int mwl8k_tid_queue_mapping(u8 tid)
1613 {
1614         BUG_ON(tid > 7);
1615
1616         switch (tid) {
1617         case 0:
1618         case 3:
1619                 return IEEE80211_AC_BE;
1620                 break;
1621         case 1:
1622         case 2:
1623                 return IEEE80211_AC_BK;
1624                 break;
1625         case 4:
1626         case 5:
1627                 return IEEE80211_AC_VI;
1628                 break;
1629         case 6:
1630         case 7:
1631                 return IEEE80211_AC_VO;
1632                 break;
1633         default:
1634                 return -1;
1635                 break;
1636         }
1637 }
1638
1639 /* The firmware will fill in the rate information
1640  * for each packet that gets queued in the hardware
1641  * and these macros will interpret that info.
1642  */
1643
1644 #define RI_FORMAT(a)              (a & 0x0001)
1645 #define RI_RATE_ID_MCS(a)        ((a & 0x01f8) >> 3)
1646
1647 static int
1648 mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
1649 {
1650         struct mwl8k_priv *priv = hw->priv;
1651         struct mwl8k_tx_queue *txq = priv->txq + index;
1652         int processed;
1653
1654         processed = 0;
1655         while (txq->len > 0 && limit--) {
1656                 int tx;
1657                 struct mwl8k_tx_desc *tx_desc;
1658                 unsigned long addr;
1659                 int size;
1660                 struct sk_buff *skb;
1661                 struct ieee80211_tx_info *info;
1662                 u32 status;
1663                 struct ieee80211_sta *sta;
1664                 struct mwl8k_sta *sta_info = NULL;
1665                 u16 rate_info;
1666                 struct ieee80211_hdr *wh;
1667
1668                 tx = txq->head;
1669                 tx_desc = txq->txd + tx;
1670
1671                 status = le32_to_cpu(tx_desc->status);
1672
1673                 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1674                         if (!force)
1675                                 break;
1676                         tx_desc->status &=
1677                                 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1678                 }
1679
1680                 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1681                 BUG_ON(txq->len == 0);
1682                 txq->len--;
1683                 priv->pending_tx_pkts--;
1684
1685                 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
1686                 size = le16_to_cpu(tx_desc->pkt_len);
1687                 skb = txq->skb[tx];
1688                 txq->skb[tx] = NULL;
1689
1690                 BUG_ON(skb == NULL);
1691                 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1692
1693                 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
1694
1695                 wh = (struct ieee80211_hdr *) skb->data;
1696
1697                 /* Mark descriptor as unused */
1698                 tx_desc->pkt_phys_addr = 0;
1699                 tx_desc->pkt_len = 0;
1700
1701                 info = IEEE80211_SKB_CB(skb);
1702                 if (ieee80211_is_data(wh->frame_control)) {
1703                         rcu_read_lock();
1704                         sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
1705                                                            wh->addr2);
1706                         if (sta) {
1707                                 sta_info = MWL8K_STA(sta);
1708                                 BUG_ON(sta_info == NULL);
1709                                 rate_info = le16_to_cpu(tx_desc->rate_info);
1710                                 /* If rate is < 6.5 Mpbs for an ht station
1711                                  * do not form an ampdu. If the station is a
1712                                  * legacy station (format = 0), do not form an
1713                                  * ampdu
1714                                  */
1715                                 if (RI_RATE_ID_MCS(rate_info) < 1 ||
1716                                     RI_FORMAT(rate_info) == 0) {
1717                                         sta_info->is_ampdu_allowed = false;
1718                                 } else {
1719                                         sta_info->is_ampdu_allowed = true;
1720                                 }
1721                         }
1722                         rcu_read_unlock();
1723                 }
1724
1725                 ieee80211_tx_info_clear_status(info);
1726
1727                 /* Rate control is happening in the firmware.
1728                  * Ensure no tx rate is being reported.
1729                  */
1730                 info->status.rates[0].idx = -1;
1731                 info->status.rates[0].count = 1;
1732
1733                 if (MWL8K_TXD_SUCCESS(status))
1734                         info->flags |= IEEE80211_TX_STAT_ACK;
1735
1736                 ieee80211_tx_status_irqsafe(hw, skb);
1737
1738                 processed++;
1739         }
1740
1741         return processed;
1742 }
1743
1744 /* must be called only when the card's transmit is completely halted */
1745 static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1746 {
1747         struct mwl8k_priv *priv = hw->priv;
1748         struct mwl8k_tx_queue *txq = priv->txq + index;
1749
1750         if (txq->txd == NULL)
1751                 return;
1752
1753         mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
1754
1755         kfree(txq->skb);
1756         txq->skb = NULL;
1757
1758         pci_free_consistent(priv->pdev,
1759                             MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
1760                             txq->txd, txq->txd_dma);
1761         txq->txd = NULL;
1762 }
1763
1764 /* caller must hold priv->stream_lock when calling the stream functions */
1765 static struct mwl8k_ampdu_stream *
1766 mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1767 {
1768         struct mwl8k_ampdu_stream *stream;
1769         struct mwl8k_priv *priv = hw->priv;
1770         int i;
1771
1772         for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
1773                 stream = &priv->ampdu[i];
1774                 if (stream->state == AMPDU_NO_STREAM) {
1775                         stream->sta = sta;
1776                         stream->state = AMPDU_STREAM_NEW;
1777                         stream->tid = tid;
1778                         stream->idx = i;
1779                         wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1780                                     sta->addr, tid);
1781                         return stream;
1782                 }
1783         }
1784         return NULL;
1785 }
1786
1787 static int
1788 mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1789 {
1790         int ret;
1791
1792         /* if the stream has already been started, don't start it again */
1793         if (stream->state != AMPDU_STREAM_NEW)
1794                 return 0;
1795         ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1796         if (ret)
1797                 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1798                             "%d\n", stream->sta->addr, stream->tid, ret);
1799         else
1800                 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1801                             stream->sta->addr, stream->tid);
1802         return ret;
1803 }
1804
1805 static void
1806 mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1807 {
1808         wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1809                     stream->tid);
1810         memset(stream, 0, sizeof(*stream));
1811 }
1812
1813 static struct mwl8k_ampdu_stream *
1814 mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1815 {
1816         struct mwl8k_priv *priv = hw->priv;
1817         int i;
1818
1819         for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
1820                 struct mwl8k_ampdu_stream *stream;
1821                 stream = &priv->ampdu[i];
1822                 if (stream->state == AMPDU_NO_STREAM)
1823                         continue;
1824                 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1825                     stream->tid == tid)
1826                         return stream;
1827         }
1828         return NULL;
1829 }
1830
1831 #define MWL8K_AMPDU_PACKET_THRESHOLD 64
1832 static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
1833 {
1834         struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1835         struct tx_traffic_info *tx_stats;
1836
1837         BUG_ON(tid >= MWL8K_MAX_TID);
1838         tx_stats = &sta_info->tx_stats[tid];
1839
1840         return sta_info->is_ampdu_allowed &&
1841                 tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
1842 }
1843
1844 static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
1845 {
1846         struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1847         struct tx_traffic_info *tx_stats;
1848
1849         BUG_ON(tid >= MWL8K_MAX_TID);
1850         tx_stats = &sta_info->tx_stats[tid];
1851
1852         if (tx_stats->start_time == 0)
1853                 tx_stats->start_time = jiffies;
1854
1855         /* reset the packet count after each second elapses.  If the number of
1856          * packets ever exceeds the ampdu_min_traffic threshold, we will allow
1857          * an ampdu stream to be started.
1858          */
1859         if (jiffies - tx_stats->start_time > HZ) {
1860                 tx_stats->pkts = 0;
1861                 tx_stats->start_time = 0;
1862         } else
1863                 tx_stats->pkts++;
1864 }
1865
1866 /* The hardware ampdu queues start from 5.
1867  * txpriorities for ampdu queues are
1868  * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
1869  * and queue 3 is lowest (queue 4 is reserved)
1870  */
1871 #define BA_QUEUE                5
1872
1873 static void
1874 mwl8k_txq_xmit(struct ieee80211_hw *hw,
1875                int index,
1876                struct ieee80211_sta *sta,
1877                struct sk_buff *skb)
1878 {
1879         struct mwl8k_priv *priv = hw->priv;
1880         struct ieee80211_tx_info *tx_info;
1881         struct mwl8k_vif *mwl8k_vif;
1882         struct ieee80211_hdr *wh;
1883         struct mwl8k_tx_queue *txq;
1884         struct mwl8k_tx_desc *tx;
1885         dma_addr_t dma;
1886         u32 txstatus;
1887         u8 txdatarate;
1888         u16 qos;
1889         int txpriority;
1890         u8 tid = 0;
1891         struct mwl8k_ampdu_stream *stream = NULL;
1892         bool start_ba_session = false;
1893         bool mgmtframe = false;
1894         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1895         bool eapol_frame = false;
1896
1897         wh = (struct ieee80211_hdr *)skb->data;
1898         if (ieee80211_is_data_qos(wh->frame_control))
1899                 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1900         else
1901                 qos = 0;
1902
1903         if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1904                 eapol_frame = true;
1905
1906         if (ieee80211_is_mgmt(wh->frame_control))
1907                 mgmtframe = true;
1908
1909         if (priv->ap_fw)
1910                 mwl8k_encapsulate_tx_frame(priv, skb);
1911         else
1912                 mwl8k_add_dma_header(priv, skb, 0, 0);
1913
1914         wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1915
1916         tx_info = IEEE80211_SKB_CB(skb);
1917         mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
1918
1919         if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1920                 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1921                 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1922                 mwl8k_vif->seqno += 0x10;
1923         }
1924
1925         /* Setup firmware control bit fields for each frame type.  */
1926         txstatus = 0;
1927         txdatarate = 0;
1928         if (ieee80211_is_mgmt(wh->frame_control) ||
1929             ieee80211_is_ctl(wh->frame_control)) {
1930                 txdatarate = 0;
1931                 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
1932         } else if (ieee80211_is_data(wh->frame_control)) {
1933                 txdatarate = 1;
1934                 if (is_multicast_ether_addr(wh->addr1))
1935                         txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1936
1937                 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
1938                 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1939                         qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
1940                 else
1941                         qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
1942         }
1943
1944         /* Queue ADDBA request in the respective data queue.  While setting up
1945          * the ampdu stream, mac80211 queues further packets for that
1946          * particular ra/tid pair.  However, packets piled up in the hardware
1947          * for that ra/tid pair will still go out. ADDBA request and the
1948          * related data packets going out from different queues asynchronously
1949          * will cause a shift in the receiver window which might result in
1950          * ampdu packets getting dropped at the receiver after the stream has
1951          * been setup.
1952          */
1953         if (unlikely(ieee80211_is_action(wh->frame_control) &&
1954             mgmt->u.action.category == WLAN_CATEGORY_BACK &&
1955             mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
1956             priv->ap_fw)) {
1957                 u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1958                 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1959                 index = mwl8k_tid_queue_mapping(tid);
1960         }
1961
1962         txpriority = index;
1963
1964         if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
1965             ieee80211_is_data_qos(wh->frame_control)) {
1966                 tid = qos & 0xf;
1967                 mwl8k_tx_count_packet(sta, tid);
1968                 spin_lock(&priv->stream_lock);
1969                 stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1970                 if (stream != NULL) {
1971                         if (stream->state == AMPDU_STREAM_ACTIVE) {
1972                                 WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
1973                                 txpriority = (BA_QUEUE + stream->idx) %
1974                                              TOTAL_HW_TX_QUEUES;
1975                                 if (stream->idx <= 1)
1976                                         index = stream->idx +
1977                                                 MWL8K_TX_WMM_QUEUES;
1978
1979                         } else if (stream->state == AMPDU_STREAM_NEW) {
1980                                 /* We get here if the driver sends us packets
1981                                  * after we've initiated a stream, but before
1982                                  * our ampdu_action routine has been called
1983                                  * with IEEE80211_AMPDU_TX_START to get the SSN
1984                                  * for the ADDBA request.  So this packet can
1985                                  * go out with no risk of sequence number
1986                                  * mismatch.  No special handling is required.
1987                                  */
1988                         } else {
1989                                 /* Drop packets that would go out after the
1990                                  * ADDBA request was sent but before the ADDBA
1991                                  * response is received.  If we don't do this,
1992                                  * the recipient would probably receive it
1993                                  * after the ADDBA request with SSN 0.  This
1994                                  * will cause the recipient's BA receive window
1995                                  * to shift, which would cause the subsequent
1996                                  * packets in the BA stream to be discarded.
1997                                  * mac80211 queues our packets for us in this
1998                                  * case, so this is really just a safety check.
1999                                  */
2000                                 wiphy_warn(hw->wiphy,
2001                                            "Cannot send packet while ADDBA "
2002                                            "dialog is underway.\n");
2003                                 spin_unlock(&priv->stream_lock);
2004                                 dev_kfree_skb(skb);
2005                                 return;
2006                         }
2007                 } else {
2008                         /* Defer calling mwl8k_start_stream so that the current
2009                          * skb can go out before the ADDBA request.  This
2010                          * prevents sequence number mismatch at the recepient
2011                          * as described above.
2012                          */
2013                         if (mwl8k_ampdu_allowed(sta, tid)) {
2014                                 stream = mwl8k_add_stream(hw, sta, tid);
2015                                 if (stream != NULL)
2016                                         start_ba_session = true;
2017                         }
2018                 }
2019                 spin_unlock(&priv->stream_lock);
2020         } else {
2021                 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
2022                 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
2023         }
2024
2025         dma = pci_map_single(priv->pdev, skb->data,
2026                                 skb->len, PCI_DMA_TODEVICE);
2027
2028         if (pci_dma_mapping_error(priv->pdev, dma)) {
2029                 wiphy_debug(hw->wiphy,
2030                             "failed to dma map skb, dropping TX frame.\n");
2031                 if (start_ba_session) {
2032                         spin_lock(&priv->stream_lock);
2033                         mwl8k_remove_stream(hw, stream);
2034                         spin_unlock(&priv->stream_lock);
2035                 }
2036                 dev_kfree_skb(skb);
2037                 return;
2038         }
2039
2040         spin_lock_bh(&priv->tx_lock);
2041
2042         txq = priv->txq + index;
2043
2044         /* Mgmt frames that go out frequently are probe
2045          * responses. Other mgmt frames got out relatively
2046          * infrequently. Hence reserve 2 buffers so that
2047          * other mgmt frames do not get dropped due to an
2048          * already queued probe response in one of the
2049          * reserved buffers.
2050          */
2051
2052         if (txq->len >= MWL8K_TX_DESCS - 2) {
2053                 if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
2054                         if (start_ba_session) {
2055                                 spin_lock(&priv->stream_lock);
2056                                 mwl8k_remove_stream(hw, stream);
2057                                 spin_unlock(&priv->stream_lock);
2058                         }
2059                         spin_unlock_bh(&priv->tx_lock);
2060                         pci_unmap_single(priv->pdev, dma, skb->len,
2061                                          PCI_DMA_TODEVICE);
2062                         dev_kfree_skb(skb);
2063                         return;
2064                 }
2065         }
2066
2067         BUG_ON(txq->skb[txq->tail] != NULL);
2068         txq->skb[txq->tail] = skb;
2069
2070         tx = txq->txd + txq->tail;
2071         tx->data_rate = txdatarate;
2072         tx->tx_priority = txpriority;
2073         tx->qos_control = cpu_to_le16(qos);
2074         tx->pkt_phys_addr = cpu_to_le32(dma);
2075         tx->pkt_len = cpu_to_le16(skb->len);
2076         tx->rate_info = 0;
2077         if (!priv->ap_fw && sta != NULL)
2078                 tx->peer_id = MWL8K_STA(sta)->peer_id;
2079         else
2080                 tx->peer_id = 0;
2081
2082         if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
2083                 tx->timestamp = cpu_to_le32(ioread32(priv->regs +
2084                                                 MWL8K_HW_TIMER_REGISTER));
2085         else
2086                 tx->timestamp = 0;
2087
2088         wmb();
2089         tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
2090
2091         txq->len++;
2092         priv->pending_tx_pkts++;
2093
2094         txq->tail++;
2095         if (txq->tail == MWL8K_TX_DESCS)
2096                 txq->tail = 0;
2097
2098         mwl8k_tx_start(priv);
2099
2100         spin_unlock_bh(&priv->tx_lock);
2101
2102         /* Initiate the ampdu session here */
2103         if (start_ba_session) {
2104                 spin_lock(&priv->stream_lock);
2105                 if (mwl8k_start_stream(hw, stream))
2106                         mwl8k_remove_stream(hw, stream);
2107                 spin_unlock(&priv->stream_lock);
2108         }
2109 }
2110
2111
2112 /*
2113  * Firmware access.
2114  *
2115  * We have the following requirements for issuing firmware commands:
2116  * - Some commands require that the packet transmit path is idle when
2117  *   the command is issued.  (For simplicity, we'll just quiesce the
2118  *   transmit path for every command.)
2119  * - There are certain sequences of commands that need to be issued to
2120  *   the hardware sequentially, with no other intervening commands.
2121  *
2122  * This leads to an implementation of a "firmware lock" as a mutex that
2123  * can be taken recursively, and which is taken by both the low-level
2124  * command submission function (mwl8k_post_cmd) as well as any users of
2125  * that function that require issuing of an atomic sequence of commands,
2126  * and quiesces the transmit path whenever it's taken.
2127  */
2128 static int mwl8k_fw_lock(struct ieee80211_hw *hw)
2129 {
2130         struct mwl8k_priv *priv = hw->priv;
2131
2132         if (priv->fw_mutex_owner != current) {
2133                 int rc;
2134
2135                 mutex_lock(&priv->fw_mutex);
2136                 ieee80211_stop_queues(hw);
2137
2138                 rc = mwl8k_tx_wait_empty(hw);
2139                 if (rc) {
2140                         if (!priv->hw_restart_in_progress)
2141                                 ieee80211_wake_queues(hw);
2142
2143                         mutex_unlock(&priv->fw_mutex);
2144
2145                         return rc;
2146                 }
2147
2148                 priv->fw_mutex_owner = current;
2149         }
2150
2151         priv->fw_mutex_depth++;
2152
2153         return 0;
2154 }
2155
2156 static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
2157 {
2158         struct mwl8k_priv *priv = hw->priv;
2159
2160         if (!--priv->fw_mutex_depth) {
2161                 if (!priv->hw_restart_in_progress)
2162                         ieee80211_wake_queues(hw);
2163
2164                 priv->fw_mutex_owner = NULL;
2165                 mutex_unlock(&priv->fw_mutex);
2166         }
2167 }
2168
2169 static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
2170                                u32 bitmap);
2171
2172 /*
2173  * Command processing.
2174  */
2175
2176 /* Timeout firmware commands after 10s */
2177 #define MWL8K_CMD_TIMEOUT_MS    10000
2178
2179 static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
2180 {
2181         DECLARE_COMPLETION_ONSTACK(cmd_wait);
2182         struct mwl8k_priv *priv = hw->priv;
2183         void __iomem *regs = priv->regs;
2184         dma_addr_t dma_addr;
2185         unsigned int dma_size;
2186         int rc;
2187         unsigned long timeout = 0;
2188         u8 buf[32];
2189         u32 bitmap = 0;
2190
2191         wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
2192                   mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
2193
2194         /* Before posting firmware commands that could change the hardware
2195          * characteristics, make sure that all BSSes are stopped temporary.
2196          * Enable these stopped BSSes after completion of the commands
2197          */
2198
2199         rc = mwl8k_fw_lock(hw);
2200         if (rc)
2201                 return rc;
2202
2203         if (priv->ap_fw && priv->running_bsses) {
2204                 switch (le16_to_cpu(cmd->code)) {
2205                 case MWL8K_CMD_SET_RF_CHANNEL:
2206                 case MWL8K_CMD_RADIO_CONTROL:
2207                 case MWL8K_CMD_RF_TX_POWER:
2208                 case MWL8K_CMD_TX_POWER:
2209                 case MWL8K_CMD_RF_ANTENNA:
2210                 case MWL8K_CMD_RTS_THRESHOLD:
2211                 case MWL8K_CMD_MIMO_CONFIG:
2212                         bitmap = priv->running_bsses;
2213                         mwl8k_enable_bsses(hw, false, bitmap);
2214                         break;
2215                 }
2216         }
2217
2218         cmd->result = (__force __le16) 0xffff;
2219         dma_size = le16_to_cpu(cmd->length);
2220         dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
2221                                   PCI_DMA_BIDIRECTIONAL);
2222         if (pci_dma_mapping_error(priv->pdev, dma_addr))
2223                 return -ENOMEM;
2224
2225         priv->hostcmd_wait = &cmd_wait;
2226         iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
2227         iowrite32(MWL8K_H2A_INT_DOORBELL,
2228                 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2229         iowrite32(MWL8K_H2A_INT_DUMMY,
2230                 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2231
2232         timeout = wait_for_completion_timeout(&cmd_wait,
2233                                 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
2234
2235         priv->hostcmd_wait = NULL;
2236
2237
2238         pci_unmap_single(priv->pdev, dma_addr, dma_size,
2239                                         PCI_DMA_BIDIRECTIONAL);
2240
2241         if (!timeout) {
2242                 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
2243                           mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2244                           MWL8K_CMD_TIMEOUT_MS);
2245                 rc = -ETIMEDOUT;
2246         } else {
2247                 int ms;
2248
2249                 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
2250
2251                 rc = cmd->result ? -EINVAL : 0;
2252                 if (rc)
2253                         wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
2254                                   mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2255                                   le16_to_cpu(cmd->result));
2256                 else if (ms > 2000)
2257                         wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
2258                                      mwl8k_cmd_name(cmd->code,
2259                                                     buf, sizeof(buf)),
2260                                      ms);
2261         }
2262
2263         if (bitmap)
2264                 mwl8k_enable_bsses(hw, true, bitmap);
2265
2266         mwl8k_fw_unlock(hw);
2267
2268         return rc;
2269 }
2270
2271 static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
2272                                  struct ieee80211_vif *vif,
2273                                  struct mwl8k_cmd_pkt *cmd)
2274 {
2275         if (vif != NULL)
2276                 cmd->macid = MWL8K_VIF(vif)->macid;
2277         return mwl8k_post_cmd(hw, cmd);
2278 }
2279
2280 /*
2281  * Setup code shared between STA and AP firmware images.
2282  */
2283 static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2284 {
2285         struct mwl8k_priv *priv = hw->priv;
2286
2287         BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2288         memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2289
2290         BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2291         memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2292
2293         priv->band_24.band = IEEE80211_BAND_2GHZ;
2294         priv->band_24.channels = priv->channels_24;
2295         priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2296         priv->band_24.bitrates = priv->rates_24;
2297         priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2298
2299         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
2300 }
2301
2302 static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2303 {
2304         struct mwl8k_priv *priv = hw->priv;
2305
2306         BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2307         memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2308
2309         BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2310         memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2311
2312         priv->band_50.band = IEEE80211_BAND_5GHZ;
2313         priv->band_50.channels = priv->channels_50;
2314         priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2315         priv->band_50.bitrates = priv->rates_50;
2316         priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2317
2318         hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
2319 }
2320
2321 /*
2322  * CMD_GET_HW_SPEC (STA version).
2323  */
2324 struct mwl8k_cmd_get_hw_spec_sta {
2325         struct mwl8k_cmd_pkt header;
2326         __u8 hw_rev;
2327         __u8 host_interface;
2328         __le16 num_mcaddrs;
2329         __u8 perm_addr[ETH_ALEN];
2330         __le16 region_code;
2331         __le32 fw_rev;
2332         __le32 ps_cookie;
2333         __le32 caps;
2334         __u8 mcs_bitmap[16];
2335         __le32 rx_queue_ptr;
2336         __le32 num_tx_queues;
2337         __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
2338         __le32 caps2;
2339         __le32 num_tx_desc_per_queue;
2340         __le32 total_rxd;
2341 } __packed;
2342
2343 #define MWL8K_CAP_MAX_AMSDU             0x20000000
2344 #define MWL8K_CAP_GREENFIELD            0x08000000
2345 #define MWL8K_CAP_AMPDU                 0x04000000
2346 #define MWL8K_CAP_RX_STBC               0x01000000
2347 #define MWL8K_CAP_TX_STBC               0x00800000
2348 #define MWL8K_CAP_SHORTGI_40MHZ         0x00400000
2349 #define MWL8K_CAP_SHORTGI_20MHZ         0x00200000
2350 #define MWL8K_CAP_RX_ANTENNA_MASK       0x000e0000
2351 #define MWL8K_CAP_TX_ANTENNA_MASK       0x0001c000
2352 #define MWL8K_CAP_DELAY_BA              0x00003000
2353 #define MWL8K_CAP_MIMO                  0x00000200
2354 #define MWL8K_CAP_40MHZ                 0x00000100
2355 #define MWL8K_CAP_BAND_MASK             0x00000007
2356 #define MWL8K_CAP_5GHZ                  0x00000004
2357 #define MWL8K_CAP_2GHZ4                 0x00000001
2358
2359 static void
2360 mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2361                   struct ieee80211_supported_band *band, u32 cap)
2362 {
2363         int rx_streams;
2364         int tx_streams;
2365
2366         band->ht_cap.ht_supported = 1;
2367
2368         if (cap & MWL8K_CAP_MAX_AMSDU)
2369                 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
2370         if (cap & MWL8K_CAP_GREENFIELD)
2371                 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
2372         if (cap & MWL8K_CAP_AMPDU) {
2373                 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
2374                 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2375                 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2376         }
2377         if (cap & MWL8K_CAP_RX_STBC)
2378                 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
2379         if (cap & MWL8K_CAP_TX_STBC)
2380                 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
2381         if (cap & MWL8K_CAP_SHORTGI_40MHZ)
2382                 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
2383         if (cap & MWL8K_CAP_SHORTGI_20MHZ)
2384                 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
2385         if (cap & MWL8K_CAP_DELAY_BA)
2386                 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
2387         if (cap & MWL8K_CAP_40MHZ)
2388                 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2389
2390         rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2391         tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2392
2393         band->ht_cap.mcs.rx_mask[0] = 0xff;
2394         if (rx_streams >= 2)
2395                 band->ht_cap.mcs.rx_mask[1] = 0xff;
2396         if (rx_streams >= 3)
2397                 band->ht_cap.mcs.rx_mask[2] = 0xff;
2398         band->ht_cap.mcs.rx_mask[4] = 0x01;
2399         band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2400
2401         if (rx_streams != tx_streams) {
2402                 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2403                 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
2404                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2405         }
2406 }
2407
2408 static void
2409 mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2410 {
2411         struct mwl8k_priv *priv = hw->priv;
2412
2413         if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2414                 mwl8k_setup_2ghz_band(hw);
2415                 if (caps & MWL8K_CAP_MIMO)
2416                         mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2417         }
2418
2419         if (caps & MWL8K_CAP_5GHZ) {
2420                 mwl8k_setup_5ghz_band(hw);
2421                 if (caps & MWL8K_CAP_MIMO)
2422                         mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2423         }
2424 }
2425
2426 static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
2427 {
2428         struct mwl8k_priv *priv = hw->priv;
2429         struct mwl8k_cmd_get_hw_spec_sta *cmd;
2430         int rc;
2431         int i;
2432
2433         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2434         if (cmd == NULL)
2435                 return -ENOMEM;
2436
2437         cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2438         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2439
2440         memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2441         cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2442         cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2443         cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2444         for (i = 0; i < mwl8k_tx_queues(priv); i++)
2445                 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
2446         cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2447         cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2448
2449         rc = mwl8k_post_cmd(hw, &cmd->header);
2450
2451         if (!rc) {
2452                 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2453                 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2454                 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2455                 priv->hw_rev = cmd->hw_rev;
2456                 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
2457                 priv->ap_macids_supported = 0x00000000;
2458                 priv->sta_macids_supported = 0x00000001;
2459         }
2460
2461         kfree(cmd);
2462         return rc;
2463 }
2464
2465 /*
2466  * CMD_GET_HW_SPEC (AP version).
2467  */
2468 struct mwl8k_cmd_get_hw_spec_ap {
2469         struct mwl8k_cmd_pkt header;
2470         __u8 hw_rev;
2471         __u8 host_interface;
2472         __le16 num_wcb;
2473         __le16 num_mcaddrs;
2474         __u8 perm_addr[ETH_ALEN];
2475         __le16 region_code;
2476         __le16 num_antenna;
2477         __le32 fw_rev;
2478         __le32 wcbbase0;
2479         __le32 rxwrptr;
2480         __le32 rxrdptr;
2481         __le32 ps_cookie;
2482         __le32 wcbbase1;
2483         __le32 wcbbase2;
2484         __le32 wcbbase3;
2485         __le32 fw_api_version;
2486         __le32 caps;
2487         __le32 num_of_ampdu_queues;
2488         __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
2489 } __packed;
2490
2491 static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2492 {
2493         struct mwl8k_priv *priv = hw->priv;
2494         struct mwl8k_cmd_get_hw_spec_ap *cmd;
2495         int rc, i;
2496         u32 api_version;
2497
2498         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2499         if (cmd == NULL)
2500                 return -ENOMEM;
2501
2502         cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2503         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2504
2505         memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2506         cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2507
2508         rc = mwl8k_post_cmd(hw, &cmd->header);
2509
2510         if (!rc) {
2511                 int off;
2512
2513                 api_version = le32_to_cpu(cmd->fw_api_version);
2514                 if (priv->device_info->fw_api_ap != api_version) {
2515                         printk(KERN_ERR "%s: Unsupported fw API version for %s."
2516                                "  Expected %d got %d.\n", MWL8K_NAME,
2517                                priv->device_info->part_name,
2518                                priv->device_info->fw_api_ap,
2519                                api_version);
2520                         rc = -EINVAL;
2521                         goto done;
2522                 }
2523                 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2524                 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2525                 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2526                 priv->hw_rev = cmd->hw_rev;
2527                 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
2528                 priv->ap_macids_supported = 0x000000ff;
2529                 priv->sta_macids_supported = 0x00000100;
2530                 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2531                 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2532                         wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2533                                    " but we only support %d.\n",
2534                                    priv->num_ampdu_queues,
2535                                    MWL8K_MAX_AMPDU_QUEUES);
2536                         priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2537                 }
2538                 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
2539                 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
2540
2541                 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
2542                 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
2543
2544                 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2545                 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2546                 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2547                 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
2548
2549                 for (i = 0; i < priv->num_ampdu_queues; i++)
2550                         priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
2551                                 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
2552         }
2553
2554 done:
2555         kfree(cmd);
2556         return rc;
2557 }
2558
2559 /*
2560  * CMD_SET_HW_SPEC.
2561  */
2562 struct mwl8k_cmd_set_hw_spec {
2563         struct mwl8k_cmd_pkt header;
2564         __u8 hw_rev;
2565         __u8 host_interface;
2566         __le16 num_mcaddrs;
2567         __u8 perm_addr[ETH_ALEN];
2568         __le16 region_code;
2569         __le32 fw_rev;
2570         __le32 ps_cookie;
2571         __le32 caps;
2572         __le32 rx_queue_ptr;
2573         __le32 num_tx_queues;
2574         __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
2575         __le32 flags;
2576         __le32 num_tx_desc_per_queue;
2577         __le32 total_rxd;
2578 } __packed;
2579
2580 /* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2581  * packets to expire 500 ms after the timestamp in the tx descriptor.  That is,
2582  * the packets that are queued for more than 500ms, will be dropped in the
2583  * hardware. This helps minimizing the issues caused due to head-of-line
2584  * blocking where a slow client can hog the bandwidth and affect traffic to a
2585  * faster client.
2586  */
2587 #define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY  0x00000400
2588 #define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR        0x00000200
2589 #define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT           0x00000080
2590 #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP       0x00000020
2591 #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON          0x00000010
2592
2593 static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2594 {
2595         struct mwl8k_priv *priv = hw->priv;
2596         struct mwl8k_cmd_set_hw_spec *cmd;
2597         int rc;
2598         int i;
2599
2600         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2601         if (cmd == NULL)
2602                 return -ENOMEM;
2603
2604         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2605         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2606
2607         cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2608         cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2609         cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2610
2611         /*
2612          * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2613          * that order. Firmware has Q3 as highest priority and Q0 as lowest
2614          * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2615          * priority is interpreted the right way in firmware.
2616          */
2617         for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2618                 int j = mwl8k_tx_queues(priv) - 1 - i;
2619                 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2620         }
2621
2622         cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2623                                  MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2624                                  MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
2625                                  MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
2626                                  MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
2627         cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2628         cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2629
2630         rc = mwl8k_post_cmd(hw, &cmd->header);
2631         kfree(cmd);
2632
2633         return rc;
2634 }
2635
2636 /*
2637  * CMD_MAC_MULTICAST_ADR.
2638  */
2639 struct mwl8k_cmd_mac_multicast_adr {
2640         struct mwl8k_cmd_pkt header;
2641         __le16 action;
2642         __le16 numaddr;
2643         __u8 addr[0][ETH_ALEN];
2644 };
2645
2646 #define MWL8K_ENABLE_RX_DIRECTED        0x0001
2647 #define MWL8K_ENABLE_RX_MULTICAST       0x0002
2648 #define MWL8K_ENABLE_RX_ALL_MULTICAST   0x0004
2649 #define MWL8K_ENABLE_RX_BROADCAST       0x0008
2650
2651 static struct mwl8k_cmd_pkt *
2652 __mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
2653                               struct netdev_hw_addr_list *mc_list)
2654 {
2655         struct mwl8k_priv *priv = hw->priv;
2656         struct mwl8k_cmd_mac_multicast_adr *cmd;
2657         int size;
2658         int mc_count = 0;
2659
2660         if (mc_list)
2661                 mc_count = netdev_hw_addr_list_count(mc_list);
2662
2663         if (allmulti || mc_count > priv->num_mcaddrs) {
2664                 allmulti = 1;
2665                 mc_count = 0;
2666         }
2667
2668         size = sizeof(*cmd) + mc_count * ETH_ALEN;
2669
2670         cmd = kzalloc(size, GFP_ATOMIC);
2671         if (cmd == NULL)
2672                 return NULL;
2673
2674         cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2675         cmd->header.length = cpu_to_le16(size);
2676         cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2677                                   MWL8K_ENABLE_RX_BROADCAST);
2678
2679         if (allmulti) {
2680                 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2681         } else if (mc_count) {
2682                 struct netdev_hw_addr *ha;
2683                 int i = 0;
2684
2685                 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2686                 cmd->numaddr = cpu_to_le16(mc_count);
2687                 netdev_hw_addr_list_for_each(ha, mc_list) {
2688                         memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
2689                 }
2690         }
2691
2692         return &cmd->header;
2693 }
2694
2695 /*
2696  * CMD_GET_STAT.
2697  */
2698 struct mwl8k_cmd_get_stat {
2699         struct mwl8k_cmd_pkt header;
2700         __le32 stats[64];
2701 } __packed;
2702
2703 #define MWL8K_STAT_ACK_FAILURE  9
2704 #define MWL8K_STAT_RTS_FAILURE  12
2705 #define MWL8K_STAT_FCS_ERROR    24
2706 #define MWL8K_STAT_RTS_SUCCESS  11
2707
2708 static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2709                               struct ieee80211_low_level_stats *stats)
2710 {
2711         struct mwl8k_cmd_get_stat *cmd;
2712         int rc;
2713
2714         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2715         if (cmd == NULL)
2716                 return -ENOMEM;
2717
2718         cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2719         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2720
2721         rc = mwl8k_post_cmd(hw, &cmd->header);
2722         if (!rc) {
2723                 stats->dot11ACKFailureCount =
2724                         le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2725                 stats->dot11RTSFailureCount =
2726                         le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2727                 stats->dot11FCSErrorCount =
2728                         le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2729                 stats->dot11RTSSuccessCount =
2730                         le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2731         }
2732         kfree(cmd);
2733
2734         return rc;
2735 }
2736
2737 /*
2738  * CMD_RADIO_CONTROL.
2739  */
2740 struct mwl8k_cmd_radio_control {
2741         struct mwl8k_cmd_pkt header;
2742         __le16 action;
2743         __le16 control;
2744         __le16 radio_on;
2745 } __packed;
2746
2747 static int
2748 mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
2749 {
2750         struct mwl8k_priv *priv = hw->priv;
2751         struct mwl8k_cmd_radio_control *cmd;
2752         int rc;
2753
2754         if (enable == priv->radio_on && !force)
2755                 return 0;
2756
2757         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2758         if (cmd == NULL)
2759                 return -ENOMEM;
2760
2761         cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2762         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2763         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2764         cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
2765         cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2766
2767         rc = mwl8k_post_cmd(hw, &cmd->header);
2768         kfree(cmd);
2769
2770         if (!rc)
2771                 priv->radio_on = enable;
2772
2773         return rc;
2774 }
2775
2776 static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
2777 {
2778         return mwl8k_cmd_radio_control(hw, 0, 0);
2779 }
2780
2781 static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
2782 {
2783         return mwl8k_cmd_radio_control(hw, 1, 0);
2784 }
2785
2786 static int
2787 mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2788 {
2789         struct mwl8k_priv *priv = hw->priv;
2790
2791         priv->radio_short_preamble = short_preamble;
2792
2793         return mwl8k_cmd_radio_control(hw, 1, 1);
2794 }
2795
2796 /*
2797  * CMD_RF_TX_POWER.
2798  */
2799 #define MWL8K_RF_TX_POWER_LEVEL_TOTAL   8
2800
2801 struct mwl8k_cmd_rf_tx_power {
2802         struct mwl8k_cmd_pkt header;
2803         __le16 action;
2804         __le16 support_level;
2805         __le16 current_level;
2806         __le16 reserved;
2807         __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
2808 } __packed;
2809
2810 static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
2811 {
2812         struct mwl8k_cmd_rf_tx_power *cmd;
2813         int rc;
2814
2815         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2816         if (cmd == NULL)
2817                 return -ENOMEM;
2818
2819         cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2820         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2821         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2822         cmd->support_level = cpu_to_le16(dBm);
2823
2824         rc = mwl8k_post_cmd(hw, &cmd->header);
2825         kfree(cmd);
2826
2827         return rc;
2828 }
2829
2830 /*
2831  * CMD_TX_POWER.
2832  */
2833 #define MWL8K_TX_POWER_LEVEL_TOTAL      12
2834
2835 struct mwl8k_cmd_tx_power {
2836         struct mwl8k_cmd_pkt header;
2837         __le16 action;
2838         __le16 band;
2839         __le16 channel;
2840         __le16 bw;
2841         __le16 sub_ch;
2842         __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2843 } __packed;
2844
2845 static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2846                                      struct ieee80211_conf *conf,
2847                                      unsigned short pwr)
2848 {
2849         struct ieee80211_channel *channel = conf->channel;
2850         struct mwl8k_cmd_tx_power *cmd;
2851         int rc;
2852         int i;
2853
2854         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2855         if (cmd == NULL)
2856                 return -ENOMEM;
2857
2858         cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2859         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2860         cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2861
2862         if (channel->band == IEEE80211_BAND_2GHZ)
2863                 cmd->band = cpu_to_le16(0x1);
2864         else if (channel->band == IEEE80211_BAND_5GHZ)
2865                 cmd->band = cpu_to_le16(0x4);
2866
2867         cmd->channel = cpu_to_le16(channel->hw_value);
2868
2869         if (conf->channel_type == NL80211_CHAN_NO_HT ||
2870             conf->channel_type == NL80211_CHAN_HT20) {
2871                 cmd->bw = cpu_to_le16(0x2);
2872         } else {
2873                 cmd->bw = cpu_to_le16(0x4);
2874                 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2875                         cmd->sub_ch = cpu_to_le16(0x3);
2876                 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2877                         cmd->sub_ch = cpu_to_le16(0x1);
2878         }
2879
2880         for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2881                 cmd->power_level_list[i] = cpu_to_le16(pwr);
2882
2883         rc = mwl8k_post_cmd(hw, &cmd->header);
2884         kfree(cmd);
2885
2886         return rc;
2887 }
2888
2889 /*
2890  * CMD_RF_ANTENNA.
2891  */
2892 struct mwl8k_cmd_rf_antenna {
2893         struct mwl8k_cmd_pkt header;
2894         __le16 antenna;
2895         __le16 mode;
2896 } __packed;
2897
2898 #define MWL8K_RF_ANTENNA_RX             1
2899 #define MWL8K_RF_ANTENNA_TX             2
2900
2901 static int
2902 mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2903 {
2904         struct mwl8k_cmd_rf_antenna *cmd;
2905         int rc;
2906
2907         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2908         if (cmd == NULL)
2909                 return -ENOMEM;
2910
2911         cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2912         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2913         cmd->antenna = cpu_to_le16(antenna);
2914         cmd->mode = cpu_to_le16(mask);
2915
2916         rc = mwl8k_post_cmd(hw, &cmd->header);
2917         kfree(cmd);
2918
2919         return rc;
2920 }
2921
2922 /*
2923  * CMD_SET_BEACON.
2924  */
2925 struct mwl8k_cmd_set_beacon {
2926         struct mwl8k_cmd_pkt header;
2927         __le16 beacon_len;
2928         __u8 beacon[0];
2929 };
2930
2931 static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2932                                 struct ieee80211_vif *vif, u8 *beacon, int len)
2933 {
2934         struct mwl8k_cmd_set_beacon *cmd;
2935         int rc;
2936
2937         cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2938         if (cmd == NULL)
2939                 return -ENOMEM;
2940
2941         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2942         cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2943         cmd->beacon_len = cpu_to_le16(len);
2944         memcpy(cmd->beacon, beacon, len);
2945
2946         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
2947         kfree(cmd);
2948
2949         return rc;
2950 }
2951
2952 /*
2953  * CMD_SET_PRE_SCAN.
2954  */
2955 struct mwl8k_cmd_set_pre_scan {
2956         struct mwl8k_cmd_pkt header;
2957 } __packed;
2958
2959 static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2960 {
2961         struct mwl8k_cmd_set_pre_scan *cmd;
2962         int rc;
2963
2964         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2965         if (cmd == NULL)
2966                 return -ENOMEM;
2967
2968         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2969         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2970
2971         rc = mwl8k_post_cmd(hw, &cmd->header);
2972         kfree(cmd);
2973
2974         return rc;
2975 }
2976
2977 /*
2978  * CMD_SET_POST_SCAN.
2979  */
2980 struct mwl8k_cmd_set_post_scan {
2981         struct mwl8k_cmd_pkt header;
2982         __le32 isibss;
2983         __u8 bssid[ETH_ALEN];
2984 } __packed;
2985
2986 static int
2987 mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
2988 {
2989         struct mwl8k_cmd_set_post_scan *cmd;
2990         int rc;
2991
2992         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2993         if (cmd == NULL)
2994                 return -ENOMEM;
2995
2996         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2997         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2998         cmd->isibss = 0;
2999         memcpy(cmd->bssid, mac, ETH_ALEN);
3000
3001         rc = mwl8k_post_cmd(hw, &cmd->header);
3002         kfree(cmd);
3003
3004         return rc;
3005 }
3006
3007 /*
3008  * CMD_SET_RF_CHANNEL.
3009  */
3010 struct mwl8k_cmd_set_rf_channel {
3011         struct mwl8k_cmd_pkt header;
3012         __le16 action;
3013         __u8 current_channel;
3014         __le32 channel_flags;
3015 } __packed;
3016
3017 static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
3018                                     struct ieee80211_conf *conf)
3019 {
3020         struct ieee80211_channel *channel = conf->channel;
3021         struct mwl8k_cmd_set_rf_channel *cmd;
3022         int rc;
3023
3024         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3025         if (cmd == NULL)
3026                 return -ENOMEM;
3027
3028         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
3029         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3030         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3031         cmd->current_channel = channel->hw_value;
3032
3033         if (channel->band == IEEE80211_BAND_2GHZ)
3034                 cmd->channel_flags |= cpu_to_le32(0x00000001);
3035         else if (channel->band == IEEE80211_BAND_5GHZ)
3036                 cmd->channel_flags |= cpu_to_le32(0x00000004);
3037
3038         if (conf->channel_type == NL80211_CHAN_NO_HT ||
3039             conf->channel_type == NL80211_CHAN_HT20)
3040                 cmd->channel_flags |= cpu_to_le32(0x00000080);
3041         else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
3042                 cmd->channel_flags |= cpu_to_le32(0x000001900);
3043         else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
3044                 cmd->channel_flags |= cpu_to_le32(0x000000900);
3045
3046         rc = mwl8k_post_cmd(hw, &cmd->header);
3047         kfree(cmd);
3048
3049         return rc;
3050 }
3051
3052 /*
3053  * CMD_SET_AID.
3054  */
3055 #define MWL8K_FRAME_PROT_DISABLED                       0x00
3056 #define MWL8K_FRAME_PROT_11G                            0x07
3057 #define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY              0x02
3058 #define MWL8K_FRAME_PROT_11N_HT_ALL                     0x06
3059
3060 struct mwl8k_cmd_update_set_aid {
3061         struct  mwl8k_cmd_pkt header;
3062         __le16  aid;
3063
3064          /* AP's MAC address (BSSID) */
3065         __u8    bssid[ETH_ALEN];
3066         __le16  protection_mode;
3067         __u8    supp_rates[14];
3068 } __packed;
3069
3070 static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
3071 {
3072         int i;
3073         int j;
3074
3075         /*
3076          * Clear nonstandard rates 4 and 13.
3077          */
3078         mask &= 0x1fef;
3079
3080         for (i = 0, j = 0; i < 14; i++) {
3081                 if (mask & (1 << i))
3082                         rates[j++] = mwl8k_rates_24[i].hw_value;
3083         }
3084 }
3085
3086 static int
3087 mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
3088                   struct ieee80211_vif *vif, u32 legacy_rate_mask)
3089 {
3090         struct mwl8k_cmd_update_set_aid *cmd;
3091         u16 prot_mode;
3092         int rc;
3093
3094         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3095         if (cmd == NULL)
3096                 return -ENOMEM;
3097
3098         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
3099         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3100         cmd->aid = cpu_to_le16(vif->bss_conf.aid);
3101         memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
3102
3103         if (vif->bss_conf.use_cts_prot) {
3104                 prot_mode = MWL8K_FRAME_PROT_11G;
3105         } else {
3106                 switch (vif->bss_conf.ht_operation_mode &
3107                         IEEE80211_HT_OP_MODE_PROTECTION) {
3108                 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
3109                         prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
3110                         break;
3111                 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
3112                         prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
3113                         break;
3114                 default:
3115                         prot_mode = MWL8K_FRAME_PROT_DISABLED;
3116                         break;
3117                 }
3118         }
3119         cmd->protection_mode = cpu_to_le16(prot_mode);
3120
3121         legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
3122
3123         rc = mwl8k_post_cmd(hw, &cmd->header);
3124         kfree(cmd);
3125
3126         return rc;
3127 }
3128
3129 /*
3130  * CMD_SET_RATE.
3131  */
3132 struct mwl8k_cmd_set_rate {
3133         struct  mwl8k_cmd_pkt header;
3134         __u8    legacy_rates[14];
3135
3136         /* Bitmap for supported MCS codes.  */
3137         __u8    mcs_set[16];
3138         __u8    reserved[16];
3139 } __packed;
3140
3141 static int
3142 mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3143                    u32 legacy_rate_mask, u8 *mcs_rates)
3144 {
3145         struct mwl8k_cmd_set_rate *cmd;
3146         int rc;
3147
3148         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3149         if (cmd == NULL)
3150                 return -ENOMEM;
3151
3152         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
3153         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3154         legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
3155         memcpy(cmd->mcs_set, mcs_rates, 16);
3156
3157         rc = mwl8k_post_cmd(hw, &cmd->header);
3158         kfree(cmd);
3159
3160         return rc;
3161 }
3162
3163 /*
3164  * CMD_FINALIZE_JOIN.
3165  */
3166 #define MWL8K_FJ_BEACON_MAXLEN  128
3167
3168 struct mwl8k_cmd_finalize_join {
3169         struct mwl8k_cmd_pkt header;
3170         __le32 sleep_interval;  /* Number of beacon periods to sleep */
3171         __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
3172 } __packed;
3173
3174 static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
3175                                    int framelen, int dtim)
3176 {
3177         struct mwl8k_cmd_finalize_join *cmd;
3178         struct ieee80211_mgmt *payload = frame;
3179         int payload_len;
3180         int rc;
3181
3182         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3183         if (cmd == NULL)
3184                 return -ENOMEM;
3185
3186         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
3187         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3188         cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
3189
3190         payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
3191         if (payload_len < 0)
3192                 payload_len = 0;
3193         else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
3194                 payload_len = MWL8K_FJ_BEACON_MAXLEN;
3195
3196         memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
3197
3198         rc = mwl8k_post_cmd(hw, &cmd->header);
3199         kfree(cmd);
3200
3201         return rc;
3202 }
3203
3204 /*
3205  * CMD_SET_RTS_THRESHOLD.
3206  */
3207 struct mwl8k_cmd_set_rts_threshold {
3208         struct mwl8k_cmd_pkt header;
3209         __le16 action;
3210         __le16 threshold;
3211 } __packed;
3212
3213 static int
3214 mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
3215 {
3216         struct mwl8k_cmd_set_rts_threshold *cmd;
3217         int rc;
3218
3219         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3220         if (cmd == NULL)
3221                 return -ENOMEM;
3222
3223         cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3224         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3225         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3226         cmd->threshold = cpu_to_le16(rts_thresh);
3227
3228         rc = mwl8k_post_cmd(hw, &cmd->header);
3229         kfree(cmd);
3230
3231         return rc;
3232 }
3233
3234 /*
3235  * CMD_SET_SLOT.
3236  */
3237 struct mwl8k_cmd_set_slot {
3238         struct mwl8k_cmd_pkt header;
3239         __le16 action;
3240         __u8 short_slot;
3241 } __packed;
3242
3243 static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
3244 {
3245         struct mwl8k_cmd_set_slot *cmd;
3246         int rc;
3247
3248         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3249         if (cmd == NULL)
3250                 return -ENOMEM;
3251
3252         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3253         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3254         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3255         cmd->short_slot = short_slot_time;
3256
3257         rc = mwl8k_post_cmd(hw, &cmd->header);
3258         kfree(cmd);
3259
3260         return rc;
3261 }
3262
3263 /*
3264  * CMD_SET_EDCA_PARAMS.
3265  */
3266 struct mwl8k_cmd_set_edca_params {
3267         struct mwl8k_cmd_pkt header;
3268
3269         /* See MWL8K_SET_EDCA_XXX below */
3270         __le16 action;
3271
3272         /* TX opportunity in units of 32 us */
3273         __le16 txop;
3274
3275         union {
3276                 struct {
3277                         /* Log exponent of max contention period: 0...15 */
3278                         __le32 log_cw_max;
3279
3280                         /* Log exponent of min contention period: 0...15 */
3281                         __le32 log_cw_min;
3282
3283                         /* Adaptive interframe spacing in units of 32us */
3284                         __u8 aifs;
3285
3286                         /* TX queue to configure */
3287                         __u8 txq;
3288                 } ap;
3289                 struct {
3290                         /* Log exponent of max contention period: 0...15 */
3291                         __u8 log_cw_max;
3292
3293                         /* Log exponent of min contention period: 0...15 */
3294                         __u8 log_cw_min;
3295
3296                         /* Adaptive interframe spacing in units of 32us */
3297                         __u8 aifs;
3298
3299                         /* TX queue to configure */
3300                         __u8 txq;
3301                 } sta;
3302         };
3303 } __packed;
3304
3305 #define MWL8K_SET_EDCA_CW       0x01
3306 #define MWL8K_SET_EDCA_TXOP     0x02
3307 #define MWL8K_SET_EDCA_AIFS     0x04
3308
3309 #define MWL8K_SET_EDCA_ALL      (MWL8K_SET_EDCA_CW | \
3310                                  MWL8K_SET_EDCA_TXOP | \
3311                                  MWL8K_SET_EDCA_AIFS)
3312
3313 static int
3314 mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3315                           __u16 cw_min, __u16 cw_max,
3316                           __u8 aifs, __u16 txop)
3317 {
3318         struct mwl8k_priv *priv = hw->priv;
3319         struct mwl8k_cmd_set_edca_params *cmd;
3320         int rc;
3321
3322         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3323         if (cmd == NULL)
3324                 return -ENOMEM;
3325
3326         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3327         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3328         cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3329         cmd->txop = cpu_to_le16(txop);
3330         if (priv->ap_fw) {
3331                 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3332                 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3333                 cmd->ap.aifs = aifs;
3334                 cmd->ap.txq = qnum;
3335         } else {
3336                 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3337                 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3338                 cmd->sta.aifs = aifs;
3339                 cmd->sta.txq = qnum;
3340         }
3341
3342         rc = mwl8k_post_cmd(hw, &cmd->header);
3343         kfree(cmd);
3344
3345         return rc;
3346 }
3347
3348 /*
3349  * CMD_SET_WMM_MODE.
3350  */
3351 struct mwl8k_cmd_set_wmm_mode {
3352         struct mwl8k_cmd_pkt header;
3353         __le16 action;
3354 } __packed;
3355
3356 static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
3357 {
3358         struct mwl8k_priv *priv = hw->priv;
3359         struct mwl8k_cmd_set_wmm_mode *cmd;
3360         int rc;
3361
3362         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3363         if (cmd == NULL)
3364                 return -ENOMEM;
3365
3366         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
3367         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3368         cmd->action = cpu_to_le16(!!enable);
3369
3370         rc = mwl8k_post_cmd(hw, &cmd->header);
3371         kfree(cmd);
3372
3373         if (!rc)
3374                 priv->wmm_enabled = enable;
3375
3376         return rc;
3377 }
3378
3379 /*
3380  * CMD_MIMO_CONFIG.
3381  */
3382 struct mwl8k_cmd_mimo_config {
3383         struct mwl8k_cmd_pkt header;
3384         __le32 action;
3385         __u8 rx_antenna_map;
3386         __u8 tx_antenna_map;
3387 } __packed;
3388
3389 static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
3390 {
3391         struct mwl8k_cmd_mimo_config *cmd;
3392         int rc;
3393
3394         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3395         if (cmd == NULL)
3396                 return -ENOMEM;
3397
3398         cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
3399         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3400         cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3401         cmd->rx_antenna_map = rx;
3402         cmd->tx_antenna_map = tx;
3403
3404         rc = mwl8k_post_cmd(hw, &cmd->header);
3405         kfree(cmd);
3406
3407         return rc;
3408 }
3409
3410 /*
3411  * CMD_USE_FIXED_RATE (STA version).
3412  */
3413 struct mwl8k_cmd_use_fixed_rate_sta {
3414         struct mwl8k_cmd_pkt header;
3415         __le32 action;
3416         __le32 allow_rate_drop;
3417         __le32 num_rates;
3418         struct {
3419                 __le32 is_ht_rate;
3420                 __le32 enable_retry;
3421                 __le32 rate;
3422                 __le32 retry_count;
3423         } rate_entry[8];
3424         __le32 rate_type;
3425         __le32 reserved1;
3426         __le32 reserved2;
3427 } __packed;
3428
3429 #define MWL8K_USE_AUTO_RATE     0x0002
3430 #define MWL8K_UCAST_RATE        0
3431
3432 static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
3433 {
3434         struct mwl8k_cmd_use_fixed_rate_sta *cmd;
3435         int rc;
3436
3437         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3438         if (cmd == NULL)
3439                 return -ENOMEM;
3440
3441         cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3442         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3443         cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3444         cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
3445
3446         rc = mwl8k_post_cmd(hw, &cmd->header);
3447         kfree(cmd);
3448
3449         return rc;
3450 }
3451
3452 /*
3453  * CMD_USE_FIXED_RATE (AP version).
3454  */
3455 struct mwl8k_cmd_use_fixed_rate_ap {
3456         struct mwl8k_cmd_pkt header;
3457         __le32 action;
3458         __le32 allow_rate_drop;
3459         __le32 num_rates;
3460         struct mwl8k_rate_entry_ap {
3461                 __le32 is_ht_rate;
3462                 __le32 enable_retry;
3463                 __le32 rate;
3464                 __le32 retry_count;
3465         } rate_entry[4];
3466         u8 multicast_rate;
3467         u8 multicast_rate_type;
3468         u8 management_rate;
3469 } __packed;
3470
3471 static int
3472 mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3473 {
3474         struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3475         int rc;
3476
3477         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3478         if (cmd == NULL)
3479                 return -ENOMEM;
3480
3481         cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3482         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3483         cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3484         cmd->multicast_rate = mcast;
3485         cmd->management_rate = mgmt;
3486
3487         rc = mwl8k_post_cmd(hw, &cmd->header);
3488         kfree(cmd);
3489
3490         return rc;
3491 }
3492
3493 /*
3494  * CMD_ENABLE_SNIFFER.
3495  */
3496 struct mwl8k_cmd_enable_sniffer {
3497         struct mwl8k_cmd_pkt header;
3498         __le32 action;
3499 } __packed;
3500
3501 static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3502 {
3503         struct mwl8k_cmd_enable_sniffer *cmd;
3504         int rc;
3505
3506         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3507         if (cmd == NULL)
3508                 return -ENOMEM;
3509
3510         cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3511         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3512         cmd->action = cpu_to_le32(!!enable);
3513
3514         rc = mwl8k_post_cmd(hw, &cmd->header);
3515         kfree(cmd);
3516
3517         return rc;
3518 }
3519
3520 struct mwl8k_cmd_update_mac_addr {
3521         struct mwl8k_cmd_pkt header;
3522         union {
3523                 struct {
3524                         __le16 mac_type;
3525                         __u8 mac_addr[ETH_ALEN];
3526                 } mbss;
3527                 __u8 mac_addr[ETH_ALEN];
3528         };
3529 } __packed;
3530
3531 #define MWL8K_MAC_TYPE_PRIMARY_CLIENT           0
3532 #define MWL8K_MAC_TYPE_SECONDARY_CLIENT         1
3533 #define MWL8K_MAC_TYPE_PRIMARY_AP               2
3534 #define MWL8K_MAC_TYPE_SECONDARY_AP             3
3535
3536 static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3537                                   struct ieee80211_vif *vif, u8 *mac, bool set)
3538 {
3539         struct mwl8k_priv *priv = hw->priv;
3540         struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3541         struct mwl8k_cmd_update_mac_addr *cmd;
3542         int mac_type;
3543         int rc;
3544
3545         mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3546         if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3547                 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3548                         if (priv->ap_fw)
3549                                 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3550                         else
3551                                 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3552                 else
3553                         mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3554         } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3555                 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3556                         mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3557                 else
3558                         mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3559         }
3560
3561         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3562         if (cmd == NULL)
3563                 return -ENOMEM;
3564
3565         if (set)
3566                 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3567         else
3568                 cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3569
3570         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3571         if (priv->ap_fw) {
3572                 cmd->mbss.mac_type = cpu_to_le16(mac_type);
3573                 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3574         } else {
3575                 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3576         }
3577
3578         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3579         kfree(cmd);
3580
3581         return rc;
3582 }
3583
3584 /*
3585  * MWL8K_CMD_SET_MAC_ADDR.
3586  */
3587 static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3588                                   struct ieee80211_vif *vif, u8 *mac)
3589 {
3590         return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3591 }
3592
3593 /*
3594  * MWL8K_CMD_DEL_MAC_ADDR.
3595  */
3596 static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3597                                   struct ieee80211_vif *vif, u8 *mac)
3598 {
3599         return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3600 }
3601
3602 /*
3603  * CMD_SET_RATEADAPT_MODE.
3604  */
3605 struct mwl8k_cmd_set_rate_adapt_mode {
3606         struct mwl8k_cmd_pkt header;
3607         __le16 action;
3608         __le16 mode;
3609 } __packed;
3610
3611 static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3612 {
3613         struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3614         int rc;
3615
3616         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3617         if (cmd == NULL)
3618                 return -ENOMEM;
3619
3620         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3621         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3622         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3623         cmd->mode = cpu_to_le16(mode);
3624
3625         rc = mwl8k_post_cmd(hw, &cmd->header);
3626         kfree(cmd);
3627
3628         return rc;
3629 }
3630
3631 /*
3632  * CMD_GET_WATCHDOG_BITMAP.
3633  */
3634 struct mwl8k_cmd_get_watchdog_bitmap {
3635         struct mwl8k_cmd_pkt header;
3636         u8      bitmap;
3637 } __packed;
3638
3639 static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3640 {
3641         struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3642         int rc;
3643
3644         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3645         if (cmd == NULL)
3646                 return -ENOMEM;
3647
3648         cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3649         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3650
3651         rc = mwl8k_post_cmd(hw, &cmd->header);
3652         if (!rc)
3653                 *bitmap = cmd->bitmap;
3654
3655         kfree(cmd);
3656
3657         return rc;
3658 }
3659
3660 #define MWL8K_WMM_QUEUE_NUMBER  3
3661
3662 static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3663                              u8 idx);
3664
3665 static void mwl8k_watchdog_ba_events(struct work_struct *work)
3666 {
3667         int rc;
3668         u8 bitmap = 0, stream_index;
3669         struct mwl8k_ampdu_stream *streams;
3670         struct mwl8k_priv *priv =
3671                 container_of(work, struct mwl8k_priv, watchdog_ba_handle);
3672         struct ieee80211_hw *hw = priv->hw;
3673         int i;
3674         u32 status = 0;
3675
3676         mwl8k_fw_lock(hw);
3677
3678         rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3679         if (rc)
3680                 goto done;
3681
3682         spin_lock(&priv->stream_lock);
3683
3684         /* the bitmap is the hw queue number.  Map it to the ampdu queue. */
3685         for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
3686                 if (bitmap & (1 << i)) {
3687                         stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
3688                                        TOTAL_HW_TX_QUEUES;
3689                         streams = &priv->ampdu[stream_index];
3690                         if (streams->state == AMPDU_STREAM_ACTIVE) {
3691                                 ieee80211_stop_tx_ba_session(streams->sta,
3692                                                              streams->tid);
3693                                 spin_unlock(&priv->stream_lock);
3694                                 mwl8k_destroy_ba(hw, stream_index);
3695                                 spin_lock(&priv->stream_lock);
3696                         }
3697                 }
3698         }
3699
3700         spin_unlock(&priv->stream_lock);
3701 done:
3702         atomic_dec(&priv->watchdog_event_pending);
3703         status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3704         iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
3705                   priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3706         mwl8k_fw_unlock(hw);
3707         return;
3708 }
3709
3710
3711 /*
3712  * CMD_BSS_START.
3713  */
3714 struct mwl8k_cmd_bss_start {
3715         struct mwl8k_cmd_pkt header;
3716         __le32 enable;
3717 } __packed;
3718
3719 static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3720                                struct ieee80211_vif *vif, int enable)
3721 {
3722         struct mwl8k_cmd_bss_start *cmd;
3723         struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3724         struct mwl8k_priv *priv = hw->priv;
3725         int rc;
3726
3727         if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
3728                 return 0;
3729
3730         if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
3731                 return 0;
3732
3733         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3734         if (cmd == NULL)
3735                 return -ENOMEM;
3736
3737         cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3738         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3739         cmd->enable = cpu_to_le32(enable);
3740
3741         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3742         kfree(cmd);
3743
3744         if (!rc) {
3745                 if (enable)
3746                         priv->running_bsses |= (1 << mwl8k_vif->macid);
3747                 else
3748                         priv->running_bsses &= ~(1 << mwl8k_vif->macid);
3749         }
3750         return rc;
3751 }
3752
3753 static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
3754 {
3755         struct mwl8k_priv *priv = hw->priv;
3756         struct mwl8k_vif *mwl8k_vif, *tmp_vif;
3757         struct ieee80211_vif *vif;
3758
3759         list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
3760                 vif = mwl8k_vif->vif;
3761
3762                 if (!(bitmap & (1 << mwl8k_vif->macid)))
3763                         continue;
3764
3765                 if (vif->type == NL80211_IFTYPE_AP)
3766                         mwl8k_cmd_bss_start(hw, vif, enable);
3767         }
3768 }
3769 /*
3770  * CMD_BASTREAM.
3771  */
3772
3773 /*
3774  * UPSTREAM is tx direction
3775  */
3776 #define BASTREAM_FLAG_DIRECTION_UPSTREAM        0x00
3777 #define BASTREAM_FLAG_IMMEDIATE_TYPE            0x01
3778
3779 enum ba_stream_action_type {
3780         MWL8K_BA_CREATE,
3781         MWL8K_BA_UPDATE,
3782         MWL8K_BA_DESTROY,
3783         MWL8K_BA_FLUSH,
3784         MWL8K_BA_CHECK,
3785 };
3786
3787
3788 struct mwl8k_create_ba_stream {
3789         __le32  flags;
3790         __le32  idle_thrs;
3791         __le32  bar_thrs;
3792         __le32  window_size;
3793         u8      peer_mac_addr[6];
3794         u8      dialog_token;
3795         u8      tid;
3796         u8      queue_id;
3797         u8      param_info;
3798         __le32  ba_context;
3799         u8      reset_seq_no_flag;
3800         __le16  curr_seq_no;
3801         u8      sta_src_mac_addr[6];
3802 } __packed;
3803
3804 struct mwl8k_destroy_ba_stream {
3805         __le32  flags;
3806         __le32  ba_context;
3807 } __packed;
3808
3809 struct mwl8k_cmd_bastream {
3810         struct mwl8k_cmd_pkt    header;
3811         __le32  action;
3812         union {
3813                 struct mwl8k_create_ba_stream   create_params;
3814                 struct mwl8k_destroy_ba_stream  destroy_params;
3815         };
3816 } __packed;
3817
3818 static int
3819 mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3820                struct ieee80211_vif *vif)
3821 {
3822         struct mwl8k_cmd_bastream *cmd;
3823         int rc;
3824
3825         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3826         if (cmd == NULL)
3827                 return -ENOMEM;
3828
3829         cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3830         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3831
3832         cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3833
3834         cmd->create_params.queue_id = stream->idx;
3835         memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3836                ETH_ALEN);
3837         cmd->create_params.tid = stream->tid;
3838
3839         cmd->create_params.flags =
3840                 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3841                 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3842
3843         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3844
3845         kfree(cmd);
3846
3847         return rc;
3848 }
3849
3850 static int
3851 mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3852                 u8 buf_size, struct ieee80211_vif *vif)
3853 {
3854         struct mwl8k_cmd_bastream *cmd;
3855         int rc;
3856
3857         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3858         if (cmd == NULL)
3859                 return -ENOMEM;
3860
3861
3862         cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3863         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3864
3865         cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3866
3867         cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3868         cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3869         cmd->create_params.queue_id = stream->idx;
3870
3871         memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3872         cmd->create_params.tid = stream->tid;
3873         cmd->create_params.curr_seq_no = cpu_to_le16(0);
3874         cmd->create_params.reset_seq_no_flag = 1;
3875
3876         cmd->create_params.param_info =
3877                 (stream->sta->ht_cap.ampdu_factor &
3878                  IEEE80211_HT_AMPDU_PARM_FACTOR) |
3879                 ((stream->sta->ht_cap.ampdu_density << 2) &
3880                  IEEE80211_HT_AMPDU_PARM_DENSITY);
3881
3882         cmd->create_params.flags =
3883                 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3884                                         BASTREAM_FLAG_DIRECTION_UPSTREAM);
3885
3886         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3887
3888         wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3889                 stream->sta->addr, stream->tid);
3890         kfree(cmd);
3891
3892         return rc;
3893 }
3894
3895 static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3896                              u8 idx)
3897 {
3898         struct mwl8k_cmd_bastream *cmd;
3899
3900         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3901         if (cmd == NULL)
3902                 return;
3903
3904         cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3905         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3906         cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3907
3908         cmd->destroy_params.ba_context = cpu_to_le32(idx);
3909         mwl8k_post_cmd(hw, &cmd->header);
3910
3911         wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
3912
3913         kfree(cmd);
3914 }
3915
3916 /*
3917  * CMD_SET_NEW_STN.
3918  */
3919 struct mwl8k_cmd_set_new_stn {
3920         struct mwl8k_cmd_pkt header;
3921         __le16 aid;
3922         __u8 mac_addr[6];
3923         __le16 stn_id;
3924         __le16 action;
3925         __le16 rsvd;
3926         __le32 legacy_rates;
3927         __u8 ht_rates[4];
3928         __le16 cap_info;
3929         __le16 ht_capabilities_info;
3930         __u8 mac_ht_param_info;
3931         __u8 rev;
3932         __u8 control_channel;
3933         __u8 add_channel;
3934         __le16 op_mode;
3935         __le16 stbc;
3936         __u8 add_qos_info;
3937         __u8 is_qos_sta;
3938         __le32 fw_sta_ptr;
3939 } __packed;
3940
3941 #define MWL8K_STA_ACTION_ADD            0
3942 #define MWL8K_STA_ACTION_REMOVE         2
3943
3944 static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3945                                      struct ieee80211_vif *vif,
3946                                      struct ieee80211_sta *sta)
3947 {
3948         struct mwl8k_cmd_set_new_stn *cmd;
3949         u32 rates;
3950         int rc;
3951
3952         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3953         if (cmd == NULL)
3954                 return -ENOMEM;
3955
3956         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3957         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3958         cmd->aid = cpu_to_le16(sta->aid);
3959         memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3960         cmd->stn_id = cpu_to_le16(sta->aid);
3961         cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
3962         if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3963                 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3964         else
3965                 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3966         cmd->legacy_rates = cpu_to_le32(rates);
3967         if (sta->ht_cap.ht_supported) {
3968                 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3969                 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3970                 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3971                 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3972                 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3973                 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3974                         ((sta->ht_cap.ampdu_density & 7) << 2);
3975                 cmd->is_qos_sta = 1;
3976         }
3977
3978         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3979         kfree(cmd);
3980
3981         return rc;
3982 }
3983
3984 static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3985                                           struct ieee80211_vif *vif)
3986 {
3987         struct mwl8k_cmd_set_new_stn *cmd;
3988         int rc;
3989
3990         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3991         if (cmd == NULL)
3992                 return -ENOMEM;
3993
3994         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3995         cmd->header.length = cpu_to_le16(sizeof(*cmd));
3996         memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3997
3998         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3999         kfree(cmd);
4000
4001         return rc;
4002 }
4003
4004 static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
4005                                      struct ieee80211_vif *vif, u8 *addr)
4006 {
4007         struct mwl8k_cmd_set_new_stn *cmd;
4008         struct mwl8k_priv *priv = hw->priv;
4009         int rc, i;
4010         u8 idx;
4011
4012         spin_lock(&priv->stream_lock);
4013         /* Destroy any active ampdu streams for this sta */
4014         for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
4015                 struct mwl8k_ampdu_stream *s;
4016                 s = &priv->ampdu[i];
4017                 if (s->state != AMPDU_NO_STREAM) {
4018                         if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
4019                                 if (s->state == AMPDU_STREAM_ACTIVE) {
4020                                         idx = s->idx;
4021                                         spin_unlock(&priv->stream_lock);
4022                                         mwl8k_destroy_ba(hw, idx);
4023                                         spin_lock(&priv->stream_lock);
4024                                 } else if (s->state == AMPDU_STREAM_NEW) {
4025                                         mwl8k_remove_stream(hw, s);
4026                                 }
4027                         }
4028                 }
4029         }
4030
4031         spin_unlock(&priv->stream_lock);
4032
4033         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4034         if (cmd == NULL)
4035                 return -ENOMEM;
4036
4037         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4038         cmd->header.length = cpu_to_le16(sizeof(*cmd));
4039         memcpy(cmd->mac_addr, addr, ETH_ALEN);
4040         cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
4041
4042         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4043         kfree(cmd);
4044
4045         return rc;
4046 }
4047
4048 /*
4049  * CMD_UPDATE_ENCRYPTION.
4050  */
4051
4052 #define MAX_ENCR_KEY_LENGTH     16
4053 #define MIC_KEY_LENGTH          8
4054
4055 struct mwl8k_cmd_update_encryption {
4056         struct mwl8k_cmd_pkt header;
4057
4058         __le32 action;
4059         __le32 reserved;
4060         __u8 mac_addr[6];
4061         __u8 encr_type;
4062
4063 } __packed;
4064
4065 struct mwl8k_cmd_set_key {
4066         struct mwl8k_cmd_pkt header;
4067
4068         __le32 action;
4069         __le32 reserved;
4070         __le16 length;
4071         __le16 key_type_id;
4072         __le32 key_info;
4073         __le32 key_id;
4074         __le16 key_len;
4075         __u8 key_material[MAX_ENCR_KEY_LENGTH];
4076         __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
4077         __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
4078         __le16 tkip_rsc_low;
4079         __le32 tkip_rsc_high;
4080         __le16 tkip_tsc_low;
4081         __le32 tkip_tsc_high;
4082         __u8 mac_addr[6];
4083 } __packed;
4084
4085 enum {
4086         MWL8K_ENCR_ENABLE,
4087         MWL8K_ENCR_SET_KEY,
4088         MWL8K_ENCR_REMOVE_KEY,
4089         MWL8K_ENCR_SET_GROUP_KEY,
4090 };
4091
4092 #define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP        0
4093 #define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE    1
4094 #define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP       4
4095 #define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED      7
4096 #define MWL8K_UPDATE_ENCRYPTION_TYPE_AES        8
4097
4098 enum {
4099         MWL8K_ALG_WEP,
4100         MWL8K_ALG_TKIP,
4101         MWL8K_ALG_CCMP,
4102 };
4103
4104 #define MWL8K_KEY_FLAG_TXGROUPKEY       0x00000004
4105 #define MWL8K_KEY_FLAG_PAIRWISE         0x00000008
4106 #define MWL8K_KEY_FLAG_TSC_VALID        0x00000040
4107 #define MWL8K_KEY_FLAG_WEP_TXKEY        0x01000000
4108 #define MWL8K_KEY_FLAG_MICKEY_VALID     0x02000000
4109
4110 static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
4111                                               struct ieee80211_vif *vif,
4112                                               u8 *addr,
4113                                               u8 encr_type)
4114 {
4115         struct mwl8k_cmd_update_encryption *cmd;
4116         int rc;
4117
4118         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4119         if (cmd == NULL)
4120                 return -ENOMEM;
4121
4122         cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4123         cmd->header.length = cpu_to_le16(sizeof(*cmd));
4124         cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
4125         memcpy(cmd->mac_addr, addr, ETH_ALEN);
4126         cmd->encr_type = encr_type;
4127
4128         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4129         kfree(cmd);
4130
4131         return rc;
4132 }
4133
4134 static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4135                                                 u8 *addr,
4136                                                 struct ieee80211_key_conf *key)
4137 {
4138         cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4139         cmd->header.length = cpu_to_le16(sizeof(*cmd));
4140         cmd->length = cpu_to_le16(sizeof(*cmd) -
4141                                 offsetof(struct mwl8k_cmd_set_key, length));
4142         cmd->key_id = cpu_to_le32(key->keyidx);
4143         cmd->key_len = cpu_to_le16(key->keylen);
4144         memcpy(cmd->mac_addr, addr, ETH_ALEN);
4145
4146         switch (key->cipher) {
4147         case WLAN_CIPHER_SUITE_WEP40:
4148         case WLAN_CIPHER_SUITE_WEP104:
4149                 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4150                 if (key->keyidx == 0)
4151                         cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4152
4153                 break;
4154         case WLAN_CIPHER_SUITE_TKIP:
4155                 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4156                 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4157                         ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4158                         : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4159                 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4160                                                 | MWL8K_KEY_FLAG_TSC_VALID);
4161                 break;
4162         case WLAN_CIPHER_SUITE_CCMP:
4163                 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4164                 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4165                         ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4166                         : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4167                 break;
4168         default:
4169                 return -ENOTSUPP;
4170         }
4171
4172         return 0;
4173 }
4174
4175 static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4176                                                 struct ieee80211_vif *vif,
4177                                                 u8 *addr,
4178                                                 struct ieee80211_key_conf *key)
4179 {
4180         struct mwl8k_cmd_set_key *cmd;
4181         int rc;
4182         int keymlen;
4183         u32 action;
4184         u8 idx;
4185         struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4186
4187         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4188         if (cmd == NULL)
4189                 return -ENOMEM;
4190
4191         rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4192         if (rc < 0)
4193                 goto done;
4194
4195         idx = key->keyidx;
4196
4197         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4198                 action = MWL8K_ENCR_SET_KEY;
4199         else
4200                 action = MWL8K_ENCR_SET_GROUP_KEY;
4201
4202         switch (key->cipher) {
4203         case WLAN_CIPHER_SUITE_WEP40:
4204         case WLAN_CIPHER_SUITE_WEP104:
4205                 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4206                         memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4207                                                 sizeof(*key) + key->keylen);
4208                         mwl8k_vif->wep_key_conf[idx].enabled = 1;
4209                 }
4210
4211                 keymlen = key->keylen;
4212                 action = MWL8K_ENCR_SET_KEY;
4213                 break;
4214         case WLAN_CIPHER_SUITE_TKIP:
4215                 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4216                 break;
4217         case WLAN_CIPHER_SUITE_CCMP:
4218                 keymlen = key->keylen;
4219                 break;
4220         default:
4221                 rc = -ENOTSUPP;
4222                 goto done;
4223         }
4224
4225         memcpy(cmd->key_material, key->key, keymlen);
4226         cmd->action = cpu_to_le32(action);
4227
4228         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4229 done:
4230         kfree(cmd);
4231
4232         return rc;
4233 }
4234
4235 static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4236                                                 struct ieee80211_vif *vif,
4237                                                 u8 *addr,
4238                                                 struct ieee80211_key_conf *key)
4239 {
4240         struct mwl8k_cmd_set_key *cmd;
4241         int rc;
4242         struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4243
4244         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4245         if (cmd == NULL)
4246                 return -ENOMEM;
4247
4248         rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4249         if (rc < 0)
4250                 goto done;
4251
4252         if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4253                         key->cipher == WLAN_CIPHER_SUITE_WEP104)
4254                 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4255
4256         cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4257
4258         rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4259 done:
4260         kfree(cmd);
4261
4262         return rc;
4263 }
4264
4265 static int mwl8k_set_key(struct ieee80211_hw *hw,
4266                          enum set_key_cmd cmd_param,
4267                          struct ieee80211_vif *vif,
4268                          struct ieee80211_sta *sta,
4269                          struct ieee80211_key_conf *key)
4270 {
4271         int rc = 0;
4272         u8 encr_type;
4273         u8 *addr;
4274         struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4275         struct mwl8k_priv *priv = hw->priv;
4276
4277         if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
4278                 return -EOPNOTSUPP;
4279
4280         if (sta == NULL)
4281                 addr = vif->addr;
4282         else
4283                 addr = sta->addr;
4284
4285         if (cmd_param == SET_KEY) {
4286                 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4287                 if (rc)
4288                         goto out;
4289
4290                 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4291                                 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4292                         encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4293                 else
4294                         encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4295
4296                 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4297                                                                 encr_type);
4298                 if (rc)
4299                         goto out;
4300
4301                 mwl8k_vif->is_hw_crypto_enabled = true;
4302
4303         } else {
4304                 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4305
4306                 if (rc)
4307                         goto out;
4308         }
4309 out:
4310         return rc;
4311 }
4312
4313 /*
4314  * CMD_UPDATE_STADB.
4315  */
4316 struct ewc_ht_info {
4317         __le16  control1;
4318         __le16  control2;
4319         __le16  control3;
4320 } __packed;
4321
4322 struct peer_capability_info {
4323         /* Peer type - AP vs. STA.  */
4324         __u8    peer_type;
4325
4326         /* Basic 802.11 capabilities from assoc resp.  */
4327         __le16  basic_caps;
4328
4329         /* Set if peer supports 802.11n high throughput (HT).  */
4330         __u8    ht_support;
4331
4332         /* Valid if HT is supported.  */
4333         __le16  ht_caps;
4334         __u8    extended_ht_caps;
4335         struct ewc_ht_info      ewc_info;
4336
4337         /* Legacy rate table. Intersection of our rates and peer rates.  */
4338         __u8    legacy_rates[12];
4339
4340         /* HT rate table. Intersection of our rates and peer rates.  */
4341         __u8    ht_rates[16];
4342         __u8    pad[16];
4343
4344         /* If set, interoperability mode, no proprietary extensions.  */
4345         __u8    interop;
4346         __u8    pad2;
4347         __u8    station_id;
4348         __le16  amsdu_enabled;
4349 } __packed;
4350
4351 struct mwl8k_cmd_update_stadb {
4352         struct mwl8k_cmd_pkt header;
4353
4354         /* See STADB_ACTION_TYPE */
4355         __le32  action;
4356
4357         /* Peer MAC address */
4358         __u8    peer_addr[ETH_ALEN];
4359
4360         __le32  reserved;
4361
4362         /* Peer info - valid during add/update.  */
4363         struct peer_capability_info     peer_info;
4364 } __packed;
4365
4366 #define MWL8K_STA_DB_MODIFY_ENTRY       1
4367 #define MWL8K_STA_DB_DEL_ENTRY          2
4368
4369 /* Peer Entry flags - used to define the type of the peer node */
4370 #define MWL8K_PEER_TYPE_ACCESSPOINT     2
4371
4372 static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
4373                                       struct ieee80211_vif *vif,
4374                                       struct ieee80211_sta *sta)
4375 {
4376         struct mwl8k_cmd_update_stadb *cmd;
4377         struct peer_capability_info *p;
4378         u32 rates;
4379         int rc;
4380
4381         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4382         if (cmd == NULL)
4383                 return -ENOMEM;
4384
4385         cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4386         cmd->header.length = cpu_to_le16(sizeof(*cmd));
4387         cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
4388         memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
4389
4390         p = &cmd->peer_info;
4391         p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4392         p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
4393         p->ht_support = sta->ht_cap.ht_supported;
4394         p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
4395         p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
4396                 ((sta->ht_cap.ampdu_density & 7) << 2);
4397         if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4398                 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
4399         else
4400                 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4401         legacy_rate_mask_to_array(p->legacy_rates, rates);
4402         memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
4403         p->interop = 1;
4404         p->amsdu_enabled = 0;
4405
4406         rc = mwl8k_post_cmd(hw, &cmd->header);
4407         if (!rc)
4408                 rc = p->station_id;
4409         kfree(cmd);
4410
4411         return rc;
4412 }
4413
4414 static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4415                                       struct ieee80211_vif *vif, u8 *addr)
4416 {
4417         struct mwl8k_cmd_update_stadb *cmd;
4418         int rc;
4419
4420         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4421         if (cmd == NULL)
4422                 return -ENOMEM;
4423
4424         cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4425         cmd->header.length = cpu_to_le16(sizeof(*cmd));
4426         cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4427         memcpy(cmd->peer_addr, addr, ETH_ALEN);
4428
4429         rc = mwl8k_post_cmd(hw, &cmd->header);
4430         kfree(cmd);
4431
4432         return rc;
4433 }
4434
4435
4436 /*
4437  * Interrupt handling.
4438  */
4439 static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4440 {
4441         struct ieee80211_hw *hw = dev_id;
4442         struct mwl8k_priv *priv = hw->priv;
4443         u32 status;
4444
4445         status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4446         if (!status)
4447                 return IRQ_NONE;
4448
4449         if (status & MWL8K_A2H_INT_TX_DONE) {
4450                 status &= ~MWL8K_A2H_INT_TX_DONE;
4451                 tasklet_schedule(&priv->poll_tx_task);
4452         }
4453
4454         if (status & MWL8K_A2H_INT_RX_READY) {
4455                 status &= ~MWL8K_A2H_INT_RX_READY;
4456                 tasklet_schedule(&priv->poll_rx_task);
4457         }
4458
4459         if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
4460                 iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
4461                           priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4462
4463                 atomic_inc(&priv->watchdog_event_pending);
4464                 status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4465                 ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4466         }
4467
4468         if (status)
4469                 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4470
4471         if (status & MWL8K_A2H_INT_OPC_DONE) {
4472                 if (priv->hostcmd_wait != NULL)
4473                         complete(priv->hostcmd_wait);
4474         }
4475
4476         if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
4477                 if (!mutex_is_locked(&priv->fw_mutex) &&
4478                     priv->radio_on && priv->pending_tx_pkts)
4479                         mwl8k_tx_start(priv);
4480         }
4481
4482         return IRQ_HANDLED;
4483 }
4484
4485 static void mwl8k_tx_poll(unsigned long data)
4486 {
4487         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4488         struct mwl8k_priv *priv = hw->priv;
4489         int limit;
4490         int i;
4491
4492         limit = 32;
4493
4494         spin_lock_bh(&priv->tx_lock);
4495
4496         for (i = 0; i < mwl8k_tx_queues(priv); i++)
4497                 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4498
4499         if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4500                 complete(priv->tx_wait);
4501                 priv->tx_wait = NULL;
4502         }
4503
4504         spin_unlock_bh(&priv->tx_lock);
4505
4506         if (limit) {
4507                 writel(~MWL8K_A2H_INT_TX_DONE,
4508                        priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4509         } else {
4510                 tasklet_schedule(&priv->poll_tx_task);
4511         }
4512 }
4513
4514 static void mwl8k_rx_poll(unsigned long data)
4515 {
4516         struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4517         struct mwl8k_priv *priv = hw->priv;
4518         int limit;
4519
4520         limit = 32;
4521         limit -= rxq_process(hw, 0, limit);
4522         limit -= rxq_refill(hw, 0, limit);
4523
4524         if (limit) {
4525                 writel(~MWL8K_A2H_INT_RX_READY,
4526                        priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4527         } else {
4528                 tasklet_schedule(&priv->poll_rx_task);
4529         }
4530 }
4531
4532
4533 /*
4534  * Core driver operations.
4535  */
4536 static void mwl8k_tx(struct ieee80211_hw *hw,
4537                      struct ieee80211_tx_control *control,
4538                      struct sk_buff *skb)
4539 {
4540         struct mwl8k_priv *priv = hw->priv;
4541         int index = skb_get_queue_mapping(skb);
4542
4543         if (!priv->radio_on) {
4544                 wiphy_debug(hw->wiphy,
4545                             "dropped TX frame since radio disabled\n");
4546                 dev_kfree_skb(skb);
4547                 return;
4548         }
4549
4550         mwl8k_txq_xmit(hw, index, control->sta, skb);
4551 }
4552
4553 static int mwl8k_start(struct ieee80211_hw *hw)
4554 {
4555         struct mwl8k_priv *priv = hw->priv;
4556         int rc;
4557
4558         rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4559                          IRQF_SHARED, MWL8K_NAME, hw);
4560         if (rc) {
4561                 priv->irq = -1;
4562                 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4563                 return -EIO;
4564         }
4565         priv->irq = priv->pdev->irq;
4566
4567         /* Enable TX reclaim and RX tasklets.  */
4568         tasklet_enable(&priv->poll_tx_task);
4569         tasklet_enable(&priv->poll_rx_task);
4570
4571         /* Enable interrupts */
4572         iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4573         iowrite32(MWL8K_A2H_EVENTS,
4574                   priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4575
4576         rc = mwl8k_fw_lock(hw);
4577         if (!rc) {
4578                 rc = mwl8k_cmd_radio_enable(hw);
4579
4580                 if (!priv->ap_fw) {
4581                         if (!rc)
4582                                 rc = mwl8k_cmd_enable_sniffer(hw, 0);
4583
4584                         if (!rc)
4585                                 rc = mwl8k_cmd_set_pre_scan(hw);
4586
4587                         if (!rc)
4588                                 rc = mwl8k_cmd_set_post_scan(hw,
4589                                                 "\x00\x00\x00\x00\x00\x00");
4590                 }
4591
4592                 if (!rc)
4593                         rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
4594
4595                 if (!rc)
4596                         rc = mwl8k_cmd_set_wmm_mode(hw, 0);
4597
4598                 mwl8k_fw_unlock(hw);
4599         }
4600
4601         if (rc) {
4602                 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4603                 free_irq(priv->pdev->irq, hw);
4604                 priv->irq = -1;
4605                 tasklet_disable(&priv->poll_tx_task);
4606                 tasklet_disable(&priv->poll_rx_task);
4607         } else {
4608                 ieee80211_wake_queues(hw);
4609         }
4610
4611         return rc;
4612 }
4613
4614 static void mwl8k_stop(struct ieee80211_hw *hw)
4615 {
4616         struct mwl8k_priv *priv = hw->priv;
4617         int i;
4618
4619         if (!priv->hw_restart_in_progress)
4620                 mwl8k_cmd_radio_disable(hw);
4621
4622         ieee80211_stop_queues(hw);
4623
4624         /* Disable interrupts */
4625         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4626         if (priv->irq != -1) {
4627                 free_irq(priv->pdev->irq, hw);
4628                 priv->irq = -1;
4629         }
4630
4631         /* Stop finalize join worker */
4632         cancel_work_sync(&priv->finalize_join_worker);
4633         cancel_work_sync(&priv->watchdog_ba_handle);
4634         if (priv->beacon_skb != NULL)
4635                 dev_kfree_skb(priv->beacon_skb);
4636
4637         /* Stop TX reclaim and RX tasklets.  */
4638         tasklet_disable(&priv->poll_tx_task);
4639         tasklet_disable(&priv->poll_rx_task);
4640
4641         /* Return all skbs to mac80211 */
4642         for (i = 0; i < mwl8k_tx_queues(priv); i++)
4643                 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
4644 }
4645
4646 static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4647
4648 static int mwl8k_add_interface(struct ieee80211_hw *hw,
4649                                struct ieee80211_vif *vif)
4650 {
4651         struct mwl8k_priv *priv = hw->priv;
4652         struct mwl8k_vif *mwl8k_vif;
4653         u32 macids_supported;
4654         int macid, rc;
4655         struct mwl8k_device_info *di;
4656
4657         /*
4658          * Reject interface creation if sniffer mode is active, as
4659          * STA operation is mutually exclusive with hardware sniffer
4660          * mode.  (Sniffer mode is only used on STA firmware.)
4661          */
4662         if (priv->sniffer_enabled) {
4663                 wiphy_info(hw->wiphy,
4664                            "unable to create STA interface because sniffer mode is enabled\n");
4665                 return -EINVAL;
4666         }
4667
4668         di = priv->device_info;
4669         switch (vif->type) {
4670         case NL80211_IFTYPE_AP:
4671                 if (!priv->ap_fw && di->fw_image_ap) {
4672                         /* we must load the ap fw to meet this request */
4673                         if (!list_empty(&priv->vif_list))
4674                                 return -EBUSY;
4675                         rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4676                         if (rc)
4677                                 return rc;
4678                 }
4679                 macids_supported = priv->ap_macids_supported;
4680                 break;
4681         case NL80211_IFTYPE_STATION:
4682                 if (priv->ap_fw && di->fw_image_sta) {
4683                         if (!list_empty(&priv->vif_list)) {
4684                                 wiphy_warn(hw->wiphy, "AP interface is running.\n"
4685                                            "Adding STA interface for WDS");
4686                         } else {
4687                                 /* we must load the sta fw to
4688                                  * meet this request.
4689                                  */
4690                                 rc = mwl8k_reload_firmware(hw,
4691                                                            di->fw_image_sta);
4692                                 if (rc)
4693                                         return rc;
4694                         }
4695                 }
4696                 macids_supported = priv->sta_macids_supported;
4697                 break;
4698         default:
4699                 return -EINVAL;
4700         }
4701
4702         macid = ffs(macids_supported & ~priv->macids_used);
4703         if (!macid--)
4704                 return -EBUSY;
4705
4706         /* Setup driver private area. */
4707         mwl8k_vif = MWL8K_VIF(vif);
4708         memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
4709         mwl8k_vif->vif = vif;
4710         mwl8k_vif->macid = macid;
4711         mwl8k_vif->seqno = 0;
4712         memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4713         mwl8k_vif->is_hw_crypto_enabled = false;
4714
4715         /* Set the mac address.  */
4716         mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4717
4718         if (vif->type == NL80211_IFTYPE_AP)
4719                 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4720
4721         priv->macids_used |= 1 << mwl8k_vif->macid;
4722         list_add_tail(&mwl8k_vif->list, &priv->vif_list);
4723
4724         return 0;
4725 }
4726
4727 static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4728 {
4729         /* Has ieee80211_restart_hw re-added the removed interfaces? */
4730         if (!priv->macids_used)
4731                 return;
4732
4733         priv->macids_used &= ~(1 << vif->macid);
4734         list_del(&vif->list);
4735 }
4736
4737 static void mwl8k_remove_interface(struct ieee80211_hw *hw,
4738                                    struct ieee80211_vif *vif)
4739 {
4740         struct mwl8k_priv *priv = hw->priv;
4741         struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4742
4743         if (vif->type == NL80211_IFTYPE_AP)
4744                 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4745
4746         mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
4747
4748         mwl8k_remove_vif(priv, mwl8k_vif);
4749 }
4750
4751 static void mwl8k_hw_restart_work(struct work_struct *work)
4752 {
4753         struct mwl8k_priv *priv =
4754                 container_of(work, struct mwl8k_priv, fw_reload);
4755         struct ieee80211_hw *hw = priv->hw;
4756         struct mwl8k_device_info *di;
4757         int rc;
4758
4759         /* If some command is waiting for a response, clear it */
4760         if (priv->hostcmd_wait != NULL) {
4761                 complete(priv->hostcmd_wait);
4762                 priv->hostcmd_wait = NULL;
4763         }
4764
4765         priv->hw_restart_owner = current;
4766         di = priv->device_info;
4767         mwl8k_fw_lock(hw);
4768
4769         if (priv->ap_fw)
4770                 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4771         else
4772                 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4773
4774         if (rc)
4775                 goto fail;
4776
4777         priv->hw_restart_owner = NULL;
4778         priv->hw_restart_in_progress = false;
4779
4780         /*
4781          * This unlock will wake up the queues and
4782          * also opens the command path for other
4783          * commands
4784          */
4785         mwl8k_fw_unlock(hw);
4786
4787         ieee80211_restart_hw(hw);
4788
4789         wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
4790
4791         return;
4792 fail:
4793         mwl8k_fw_unlock(hw);
4794
4795         wiphy_err(hw->wiphy, "Firmware restart failed\n");
4796 }
4797
4798 static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
4799 {
4800         struct ieee80211_conf *conf = &hw->conf;
4801         struct mwl8k_priv *priv = hw->priv;
4802         int rc;
4803
4804         if (conf->flags & IEEE80211_CONF_IDLE) {
4805                 mwl8k_cmd_radio_disable(hw);
4806                 return 0;
4807         }
4808
4809         rc = mwl8k_fw_lock(hw);
4810         if (rc)
4811                 return rc;
4812
4813         rc = mwl8k_cmd_radio_enable(hw);
4814         if (rc)
4815                 goto out;
4816
4817         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
4818                 rc = mwl8k_cmd_set_rf_channel(hw, conf);
4819                 if (rc)
4820                         goto out;
4821         }
4822
4823         if (conf->power_level > 18)
4824                 conf->power_level = 18;
4825
4826         if (priv->ap_fw) {
4827
4828                 if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
4829                         rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4830                         if (rc)
4831                                 goto out;
4832                 }
4833
4834
4835         } else {
4836                 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4837                 if (rc)
4838                         goto out;
4839                 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4840         }
4841
4842 out:
4843         mwl8k_fw_unlock(hw);
4844
4845         return rc;
4846 }
4847
4848 static void
4849 mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4850                            struct ieee80211_bss_conf *info, u32 changed)
4851 {
4852         struct mwl8k_priv *priv = hw->priv;
4853         u32 ap_legacy_rates = 0;
4854         u8 ap_mcs_rates[16];
4855         int rc;
4856
4857         if (mwl8k_fw_lock(hw))
4858                 return;
4859
4860         /*
4861          * No need to capture a beacon if we're no longer associated.
4862          */
4863         if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4864                 priv->capture_beacon = false;
4865
4866         /*
4867          * Get the AP's legacy and MCS rates.
4868          */
4869         if (vif->bss_conf.assoc) {
4870                 struct ieee80211_sta *ap;
4871
4872                 rcu_read_lock();
4873
4874                 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
4875                 if (ap == NULL) {
4876                         rcu_read_unlock();
4877                         goto out;
4878                 }
4879
4880                 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4881                         ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4882                 } else {
4883                         ap_legacy_rates =
4884                                 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4885                 }
4886                 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
4887
4888                 rcu_read_unlock();
4889         }
4890
4891         if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4892             !priv->ap_fw) {
4893                 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
4894                 if (rc)
4895                         goto out;
4896
4897                 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
4898                 if (rc)
4899                         goto out;
4900         } else {
4901                 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4902                     priv->ap_fw) {
4903                         int idx;
4904                         int rate;
4905
4906                         /* Use AP firmware specific rate command.
4907                          */
4908                         idx = ffs(vif->bss_conf.basic_rates);
4909                         if (idx)
4910                                 idx--;
4911
4912                         if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4913                                 rate = mwl8k_rates_24[idx].hw_value;
4914                         else
4915                                 rate = mwl8k_rates_50[idx].hw_value;
4916
4917                         mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4918                 }
4919         }
4920
4921         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4922                 rc = mwl8k_set_radio_preamble(hw,
4923                                 vif->bss_conf.use_short_preamble);
4924                 if (rc)
4925                         goto out;
4926         }
4927
4928         if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw)  {
4929                 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
4930                 if (rc)
4931                         goto out;
4932         }
4933
4934         if (vif->bss_conf.assoc && !priv->ap_fw &&
4935             (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4936                         BSS_CHANGED_HT))) {
4937                 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
4938                 if (rc)
4939                         goto out;
4940         }
4941
4942         if (vif->bss_conf.assoc &&
4943             (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
4944                 /*
4945                  * Finalize the join.  Tell rx handler to process
4946                  * next beacon from our BSSID.
4947                  */
4948                 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
4949                 priv->capture_beacon = true;
4950         }
4951
4952 out:
4953         mwl8k_fw_unlock(hw);
4954 }
4955
4956 static void
4957 mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4958                           struct ieee80211_bss_conf *info, u32 changed)
4959 {
4960         int rc;
4961
4962         if (mwl8k_fw_lock(hw))
4963                 return;
4964
4965         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4966                 rc = mwl8k_set_radio_preamble(hw,
4967                                 vif->bss_conf.use_short_preamble);
4968                 if (rc)
4969                         goto out;
4970         }
4971
4972         if (changed & BSS_CHANGED_BASIC_RATES) {
4973                 int idx;
4974                 int rate;
4975
4976                 /*
4977                  * Use lowest supported basic rate for multicasts
4978                  * and management frames (such as probe responses --
4979                  * beacons will always go out at 1 Mb/s).
4980                  */
4981                 idx = ffs(vif->bss_conf.basic_rates);
4982                 if (idx)
4983                         idx--;
4984
4985                 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4986                         rate = mwl8k_rates_24[idx].hw_value;
4987                 else
4988                         rate = mwl8k_rates_50[idx].hw_value;
4989
4990                 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4991         }
4992
4993         if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4994                 struct sk_buff *skb;
4995
4996                 skb = ieee80211_beacon_get(hw, vif);
4997                 if (skb != NULL) {
4998                         mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
4999                         kfree_skb(skb);
5000                 }
5001         }
5002
5003         if (changed & BSS_CHANGED_BEACON_ENABLED)
5004                 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
5005
5006 out:
5007         mwl8k_fw_unlock(hw);
5008 }
5009
5010 static void
5011 mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5012                        struct ieee80211_bss_conf *info, u32 changed)
5013 {
5014         if (vif->type == NL80211_IFTYPE_STATION)
5015                 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
5016         if (vif->type == NL80211_IFTYPE_AP)
5017                 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
5018 }
5019
5020 static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
5021                                    struct netdev_hw_addr_list *mc_list)
5022 {
5023         struct mwl8k_cmd_pkt *cmd;
5024
5025         /*
5026          * Synthesize and return a command packet that programs the
5027          * hardware multicast address filter.  At this point we don't
5028          * know whether FIF_ALLMULTI is being requested, but if it is,
5029          * we'll end up throwing this packet away and creating a new
5030          * one in mwl8k_configure_filter().
5031          */
5032         cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
5033
5034         return (unsigned long)cmd;
5035 }
5036
5037 static int
5038 mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
5039                                unsigned int changed_flags,
5040                                unsigned int *total_flags)
5041 {
5042         struct mwl8k_priv *priv = hw->priv;
5043
5044         /*
5045          * Hardware sniffer mode is mutually exclusive with STA
5046          * operation, so refuse to enable sniffer mode if a STA
5047          * interface is active.
5048          */
5049         if (!list_empty(&priv->vif_list)) {
5050                 if (net_ratelimit())
5051                         wiphy_info(hw->wiphy,
5052                                    "not enabling sniffer mode because STA interface is active\n");
5053                 return 0;
5054         }
5055
5056         if (!priv->sniffer_enabled) {
5057                 if (mwl8k_cmd_enable_sniffer(hw, 1))
5058                         return 0;
5059                 priv->sniffer_enabled = true;
5060         }
5061
5062         *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
5063                         FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
5064                         FIF_OTHER_BSS;
5065
5066         return 1;
5067 }
5068
5069 static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
5070 {
5071         if (!list_empty(&priv->vif_list))
5072                 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
5073
5074         return NULL;
5075 }
5076
5077 static void mwl8k_configure_filter(struct ieee80211_hw *hw,
5078                                    unsigned int changed_flags,
5079                                    unsigned int *total_flags,
5080                                    u64 multicast)
5081 {
5082         struct mwl8k_priv *priv = hw->priv;
5083         struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
5084
5085         /*
5086          * AP firmware doesn't allow fine-grained control over
5087          * the receive filter.
5088          */
5089         if (priv->ap_fw) {
5090                 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5091                 kfree(cmd);
5092                 return;
5093         }
5094
5095         /*
5096          * Enable hardware sniffer mode if FIF_CONTROL or
5097          * FIF_OTHER_BSS is requested.
5098          */
5099         if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
5100             mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
5101                 kfree(cmd);
5102                 return;
5103         }
5104
5105         /* Clear unsupported feature flags */
5106         *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5107
5108         if (mwl8k_fw_lock(hw)) {
5109                 kfree(cmd);
5110                 return;
5111         }
5112
5113         if (priv->sniffer_enabled) {
5114                 mwl8k_cmd_enable_sniffer(hw, 0);
5115                 priv->sniffer_enabled = false;
5116         }
5117
5118         if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
5119                 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
5120                         /*
5121                          * Disable the BSS filter.
5122                          */
5123                         mwl8k_cmd_set_pre_scan(hw);
5124                 } else {
5125                         struct mwl8k_vif *mwl8k_vif;
5126                         const u8 *bssid;
5127
5128                         /*
5129                          * Enable the BSS filter.
5130                          *
5131                          * If there is an active STA interface, use that
5132                          * interface's BSSID, otherwise use a dummy one
5133                          * (where the OUI part needs to be nonzero for
5134                          * the BSSID to be accepted by POST_SCAN).
5135                          */
5136                         mwl8k_vif = mwl8k_first_vif(priv);
5137                         if (mwl8k_vif != NULL)
5138                                 bssid = mwl8k_vif->vif->bss_conf.bssid;
5139                         else
5140                                 bssid = "\x01\x00\x00\x00\x00\x00";
5141
5142                         mwl8k_cmd_set_post_scan(hw, bssid);
5143                 }
5144         }
5145
5146         /*
5147          * If FIF_ALLMULTI is being requested, throw away the command
5148          * packet that ->prepare_multicast() built and replace it with
5149          * a command packet that enables reception of all multicast
5150          * packets.
5151          */
5152         if (*total_flags & FIF_ALLMULTI) {
5153                 kfree(cmd);
5154                 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
5155         }
5156
5157         if (cmd != NULL) {
5158                 mwl8k_post_cmd(hw, cmd);
5159                 kfree(cmd);
5160         }
5161
5162         mwl8k_fw_unlock(hw);
5163 }
5164
5165 static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5166 {
5167         return mwl8k_cmd_set_rts_threshold(hw, value);
5168 }
5169
5170 static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5171                             struct ieee80211_vif *vif,
5172                             struct ieee80211_sta *sta)
5173 {
5174         struct mwl8k_priv *priv = hw->priv;
5175
5176         if (priv->ap_fw)
5177                 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5178         else
5179                 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5180 }
5181
5182 static int mwl8k_sta_add(struct ieee80211_hw *hw,
5183                          struct ieee80211_vif *vif,
5184                          struct ieee80211_sta *sta)
5185 {
5186         struct mwl8k_priv *priv = hw->priv;
5187         int ret;
5188         int i;
5189         struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5190         struct ieee80211_key_conf *key;
5191
5192         if (!priv->ap_fw) {
5193                 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5194                 if (ret >= 0) {
5195                         MWL8K_STA(sta)->peer_id = ret;
5196                         if (sta->ht_cap.ht_supported)
5197                                 MWL8K_STA(sta)->is_ampdu_allowed = true;
5198                         ret = 0;
5199                 }
5200
5201         } else {
5202                 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
5203         }
5204
5205         for (i = 0; i < NUM_WEP_KEYS; i++) {
5206                 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5207                 if (mwl8k_vif->wep_key_conf[i].enabled)
5208                         mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5209         }
5210         return ret;
5211 }
5212
5213 static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5214                          struct ieee80211_vif *vif, u16 queue,
5215                          const struct ieee80211_tx_queue_params *params)
5216 {
5217         struct mwl8k_priv *priv = hw->priv;
5218         int rc;
5219
5220         rc = mwl8k_fw_lock(hw);
5221         if (!rc) {
5222                 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
5223                 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5224
5225                 if (!priv->wmm_enabled)
5226                         rc = mwl8k_cmd_set_wmm_mode(hw, 1);
5227
5228                 if (!rc) {
5229                         int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
5230                         rc = mwl8k_cmd_set_edca_params(hw, q,
5231                                                        params->cw_min,
5232                                                        params->cw_max,
5233                                                        params->aifs,
5234                                                        params->txop);
5235                 }
5236
5237                 mwl8k_fw_unlock(hw);
5238         }
5239
5240         return rc;
5241 }
5242
5243 static int mwl8k_get_stats(struct ieee80211_hw *hw,
5244                            struct ieee80211_low_level_stats *stats)
5245 {
5246         return mwl8k_cmd_get_stat(hw, stats);
5247 }
5248
5249 static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5250                                 struct survey_info *survey)
5251 {
5252         struct mwl8k_priv *priv = hw->priv;
5253         struct ieee80211_conf *conf = &hw->conf;
5254
5255         if (idx != 0)
5256                 return -ENOENT;
5257
5258         survey->channel = conf->channel;
5259         survey->filled = SURVEY_INFO_NOISE_DBM;
5260         survey->noise = priv->noise;
5261
5262         return 0;
5263 }
5264
5265 #define MAX_AMPDU_ATTEMPTS 5
5266
5267 static int
5268 mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5269                    enum ieee80211_ampdu_mlme_action action,
5270                    struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5271                    u8 buf_size)
5272 {
5273
5274         int i, rc = 0;
5275         struct mwl8k_priv *priv = hw->priv;
5276         struct mwl8k_ampdu_stream *stream;
5277         u8 *addr = sta->addr, idx;
5278         struct mwl8k_sta *sta_info = MWL8K_STA(sta);
5279
5280         if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
5281                 return -ENOTSUPP;
5282
5283         spin_lock(&priv->stream_lock);
5284         stream = mwl8k_lookup_stream(hw, addr, tid);
5285
5286         switch (action) {
5287         case IEEE80211_AMPDU_RX_START:
5288         case IEEE80211_AMPDU_RX_STOP:
5289                 break;
5290         case IEEE80211_AMPDU_TX_START:
5291                 /* By the time we get here the hw queues may contain outgoing
5292                  * packets for this RA/TID that are not part of this BA
5293                  * session.  The hw will assign sequence numbers to these
5294                  * packets as they go out.  So if we query the hw for its next
5295                  * sequence number and use that for the SSN here, it may end up
5296                  * being wrong, which will lead to sequence number mismatch at
5297                  * the recipient.  To avoid this, we reset the sequence number
5298                  * to O for the first MPDU in this BA stream.
5299                  */
5300                 *ssn = 0;
5301                 if (stream == NULL) {
5302                         /* This means that somebody outside this driver called
5303                          * ieee80211_start_tx_ba_session.  This is unexpected
5304                          * because we do our own rate control.  Just warn and
5305                          * move on.
5306                          */
5307                         wiphy_warn(hw->wiphy, "Unexpected call to %s.  "
5308                                    "Proceeding anyway.\n", __func__);
5309                         stream = mwl8k_add_stream(hw, sta, tid);
5310                 }
5311                 if (stream == NULL) {
5312                         wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5313                         rc = -EBUSY;
5314                         break;
5315                 }
5316                 stream->state = AMPDU_STREAM_IN_PROGRESS;
5317
5318                 /* Release the lock before we do the time consuming stuff */
5319                 spin_unlock(&priv->stream_lock);
5320                 for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
5321
5322                         /* Check if link is still valid */
5323                         if (!sta_info->is_ampdu_allowed) {
5324                                 spin_lock(&priv->stream_lock);
5325                                 mwl8k_remove_stream(hw, stream);
5326                                 spin_unlock(&priv->stream_lock);
5327                                 return -EBUSY;
5328                         }
5329
5330                         rc = mwl8k_check_ba(hw, stream, vif);
5331
5332                         /* If HW restart is in progress mwl8k_post_cmd will
5333                          * return -EBUSY. Avoid retrying mwl8k_check_ba in
5334                          * such cases
5335                          */
5336                         if (!rc || rc == -EBUSY)
5337                                 break;
5338                         /*
5339                          * HW queues take time to be flushed, give them
5340                          * sufficient time
5341                          */
5342
5343                         msleep(1000);
5344                 }
5345                 spin_lock(&priv->stream_lock);
5346                 if (rc) {
5347                         wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5348                                 " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5349                         mwl8k_remove_stream(hw, stream);
5350                         rc = -EBUSY;
5351                         break;
5352                 }
5353                 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
5354                 break;
5355         case IEEE80211_AMPDU_TX_STOP_CONT:
5356         case IEEE80211_AMPDU_TX_STOP_FLUSH:
5357         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5358                 if (stream) {
5359                         if (stream->state == AMPDU_STREAM_ACTIVE) {
5360                                 idx = stream->idx;
5361                                 spin_unlock(&priv->stream_lock);
5362                                 mwl8k_destroy_ba(hw, idx);
5363                                 spin_lock(&priv->stream_lock);
5364                         }
5365                         mwl8k_remove_stream(hw, stream);
5366                 }
5367                 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5368                 break;
5369         case IEEE80211_AMPDU_TX_OPERATIONAL:
5370                 BUG_ON(stream == NULL);
5371                 BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5372                 spin_unlock(&priv->stream_lock);
5373                 rc = mwl8k_create_ba(hw, stream, buf_size, vif);
5374                 spin_lock(&priv->stream_lock);
5375                 if (!rc)
5376                         stream->state = AMPDU_STREAM_ACTIVE;
5377                 else {
5378                         idx = stream->idx;
5379                         spin_unlock(&priv->stream_lock);
5380                         mwl8k_destroy_ba(hw, idx);
5381                         spin_lock(&priv->stream_lock);
5382                         wiphy_debug(hw->wiphy,
5383                                 "Failed adding stream for sta %pM tid %d\n",
5384                                 addr, tid);
5385                         mwl8k_remove_stream(hw, stream);
5386                 }
5387                 break;
5388
5389         default:
5390                 rc = -ENOTSUPP;
5391         }
5392
5393         spin_unlock(&priv->stream_lock);
5394         return rc;
5395 }
5396
5397 static const struct ieee80211_ops mwl8k_ops = {
5398         .tx                     = mwl8k_tx,
5399         .start                  = mwl8k_start,
5400         .stop                   = mwl8k_stop,
5401         .add_interface          = mwl8k_add_interface,
5402         .remove_interface       = mwl8k_remove_interface,
5403         .config                 = mwl8k_config,
5404         .bss_info_changed       = mwl8k_bss_info_changed,
5405         .prepare_multicast      = mwl8k_prepare_multicast,
5406         .configure_filter       = mwl8k_configure_filter,
5407         .set_key                = mwl8k_set_key,
5408         .set_rts_threshold      = mwl8k_set_rts_threshold,
5409         .sta_add                = mwl8k_sta_add,
5410         .sta_remove             = mwl8k_sta_remove,
5411         .conf_tx                = mwl8k_conf_tx,
5412         .get_stats              = mwl8k_get_stats,
5413         .get_survey             = mwl8k_get_survey,
5414         .ampdu_action           = mwl8k_ampdu_action,
5415 };
5416
5417 static void mwl8k_finalize_join_worker(struct work_struct *work)
5418 {
5419         struct mwl8k_priv *priv =
5420                 container_of(work, struct mwl8k_priv, finalize_join_worker);
5421         struct sk_buff *skb = priv->beacon_skb;
5422         struct ieee80211_mgmt *mgmt = (void *)skb->data;
5423         int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5424         const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5425                                          mgmt->u.beacon.variable, len);
5426         int dtim_period = 1;
5427
5428         if (tim && tim[1] >= 2)
5429                 dtim_period = tim[3];
5430
5431         mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
5432
5433         dev_kfree_skb(skb);
5434         priv->beacon_skb = NULL;
5435 }
5436
5437 enum {
5438         MWL8363 = 0,
5439         MWL8687,
5440         MWL8366,
5441         MWL8764,
5442 };
5443
5444 #define MWL8K_8366_AP_FW_API 3
5445 #define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5446 #define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5447
5448 #define MWL8K_8764_AP_FW_API 1
5449 #define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
5450 #define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
5451
5452 static struct mwl8k_device_info mwl8k_info_tbl[] = {
5453         [MWL8363] = {
5454                 .part_name      = "88w8363",
5455                 .helper_image   = "mwl8k/helper_8363.fw",
5456                 .fw_image_sta   = "mwl8k/fmimage_8363.fw",
5457         },
5458         [MWL8687] = {
5459                 .part_name      = "88w8687",
5460                 .helper_image   = "mwl8k/helper_8687.fw",
5461                 .fw_image_sta   = "mwl8k/fmimage_8687.fw",
5462         },
5463         [MWL8366] = {
5464                 .part_name      = "88w8366",
5465                 .helper_image   = "mwl8k/helper_8366.fw",
5466                 .fw_image_sta   = "mwl8k/fmimage_8366.fw",
5467                 .fw_image_ap    = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5468                 .fw_api_ap      = MWL8K_8366_AP_FW_API,
5469                 .ap_rxd_ops     = &rxd_ap_ops,
5470         },
5471         [MWL8764] = {
5472                 .part_name      = "88w8764",
5473                 .fw_image_ap    = MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
5474                 .fw_api_ap      = MWL8K_8764_AP_FW_API,
5475                 .ap_rxd_ops     = &rxd_ap_ops,
5476         },
5477 };
5478
5479 MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5480 MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5481 MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5482 MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5483 MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5484 MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
5485 MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
5486
5487 static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
5488         { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
5489         { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5490         { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
5491         { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5492         { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5493         { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
5494         { PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
5495         { PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
5496         { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
5497         { PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
5498         { },
5499 };
5500 MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5501
5502 static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5503 {
5504         int rc;
5505         printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5506                "Trying alternative firmware %s\n", pci_name(priv->pdev),
5507                priv->fw_pref, priv->fw_alt);
5508         rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5509         if (rc) {
5510                 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5511                        pci_name(priv->pdev), priv->fw_alt);
5512                 return rc;
5513         }
5514         return 0;
5515 }
5516
5517 static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5518 static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5519 {
5520         struct mwl8k_priv *priv = context;
5521         struct mwl8k_device_info *di = priv->device_info;
5522         int rc;
5523
5524         switch (priv->fw_state) {
5525         case FW_STATE_INIT:
5526                 if (!fw) {
5527                         printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5528                                pci_name(priv->pdev), di->helper_image);
5529                         goto fail;
5530                 }
5531                 priv->fw_helper = fw;
5532                 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5533                                       true);
5534                 if (rc && priv->fw_alt) {
5535                         rc = mwl8k_request_alt_fw(priv);
5536                         if (rc)
5537                                 goto fail;
5538                         priv->fw_state = FW_STATE_LOADING_ALT;
5539                 } else if (rc)
5540                         goto fail;
5541                 else
5542                         priv->fw_state = FW_STATE_LOADING_PREF;
5543                 break;
5544
5545         case FW_STATE_LOADING_PREF:
5546                 if (!fw) {
5547                         if (priv->fw_alt) {
5548                                 rc = mwl8k_request_alt_fw(priv);
5549                                 if (rc)
5550                                         goto fail;
5551                                 priv->fw_state = FW_STATE_LOADING_ALT;
5552                         } else
5553                                 goto fail;
5554                 } else {
5555                         priv->fw_ucode = fw;
5556                         rc = mwl8k_firmware_load_success(priv);
5557                         if (rc)
5558                                 goto fail;
5559                         else
5560                                 complete(&priv->firmware_loading_complete);
5561                 }
5562                 break;
5563
5564         case FW_STATE_LOADING_ALT:
5565                 if (!fw) {
5566                         printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5567                                pci_name(priv->pdev), di->helper_image);
5568                         goto fail;
5569                 }
5570                 priv->fw_ucode = fw;
5571                 rc = mwl8k_firmware_load_success(priv);
5572                 if (rc)
5573                         goto fail;
5574                 else
5575                         complete(&priv->firmware_loading_complete);
5576                 break;
5577
5578         default:
5579                 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5580                        MWL8K_NAME, priv->fw_state);
5581                 BUG_ON(1);
5582         }
5583
5584         return;
5585
5586 fail:
5587         priv->fw_state = FW_STATE_ERROR;
5588         complete(&priv->firmware_loading_complete);
5589         device_release_driver(&priv->pdev->dev);
5590         mwl8k_release_firmware(priv);
5591 }
5592
5593 #define MAX_RESTART_ATTEMPTS 1
5594 static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5595                                bool nowait)
5596 {
5597         struct mwl8k_priv *priv = hw->priv;
5598         int rc;
5599         int count = MAX_RESTART_ATTEMPTS;
5600
5601 retry:
5602         /* Reset firmware and hardware */
5603         mwl8k_hw_reset(priv);
5604
5605         /* Ask userland hotplug daemon for the device firmware */
5606         rc = mwl8k_request_firmware(priv, fw_image, nowait);
5607         if (rc) {
5608                 wiphy_err(hw->wiphy, "Firmware files not found\n");
5609                 return rc;
5610         }
5611
5612         if (nowait)
5613                 return rc;
5614
5615         /* Load firmware into hardware */
5616         rc = mwl8k_load_firmware(hw);
5617         if (rc)
5618                 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5619
5620         /* Reclaim memory once firmware is successfully loaded */
5621         mwl8k_release_firmware(priv);
5622
5623         if (rc && count) {
5624                 /* FW did not start successfully;
5625                  * lets try one more time
5626                  */
5627                 count--;
5628                 wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5629                 msleep(20);
5630                 goto retry;
5631         }
5632
5633         return rc;
5634 }
5635
5636 static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5637 {
5638         struct mwl8k_priv *priv = hw->priv;
5639         int rc = 0;
5640         int i;
5641
5642         for (i = 0; i < mwl8k_tx_queues(priv); i++) {
5643                 rc = mwl8k_txq_init(hw, i);
5644                 if (rc)
5645                         break;
5646                 if (priv->ap_fw)
5647                         iowrite32(priv->txq[i].txd_dma,
5648                                   priv->sram + priv->txq_offset[i]);
5649         }
5650         return rc;
5651 }
5652
5653 /* initialize hw after successfully loading a firmware image */
5654 static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5655 {
5656         struct mwl8k_priv *priv = hw->priv;
5657         int rc = 0;
5658         int i;
5659
5660         if (priv->ap_fw) {
5661                 priv->rxd_ops = priv->device_info->ap_rxd_ops;
5662                 if (priv->rxd_ops == NULL) {
5663                         wiphy_err(hw->wiphy,
5664                                   "Driver does not have AP firmware image support for this hardware\n");
5665                         rc = -ENOENT;
5666                         goto err_stop_firmware;
5667                 }
5668         } else {
5669                 priv->rxd_ops = &rxd_sta_ops;
5670         }
5671
5672         priv->sniffer_enabled = false;
5673         priv->wmm_enabled = false;
5674         priv->pending_tx_pkts = 0;
5675         atomic_set(&priv->watchdog_event_pending, 0);
5676
5677         rc = mwl8k_rxq_init(hw, 0);
5678         if (rc)
5679                 goto err_stop_firmware;
5680         rxq_refill(hw, 0, INT_MAX);
5681
5682         /* For the sta firmware, we need to know the dma addresses of tx queues
5683          * before sending MWL8K_CMD_GET_HW_SPEC.  So we must initialize them
5684          * prior to issuing this command.  But for the AP case, we learn the
5685          * total number of queues from the result CMD_GET_HW_SPEC, so for this
5686          * case we must initialize the tx queues after.
5687          */
5688         priv->num_ampdu_queues = 0;
5689         if (!priv->ap_fw) {
5690                 rc = mwl8k_init_txqs(hw);
5691                 if (rc)
5692                         goto err_free_queues;
5693         }
5694
5695         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5696         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5697         iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5698                   MWL8K_A2H_INT_BA_WATCHDOG,
5699                   priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
5700         iowrite32(MWL8K_A2H_INT_OPC_DONE,
5701                   priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
5702
5703         rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5704                          IRQF_SHARED, MWL8K_NAME, hw);
5705         if (rc) {
5706                 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5707                 goto err_free_queues;
5708         }
5709
5710         /*
5711          * When hw restart is requested,
5712          * mac80211 will take care of clearing
5713          * the ampdu streams, so do not clear
5714          * the ampdu state here
5715          */
5716         if (!priv->hw_restart_in_progress)
5717                 memset(priv->ampdu, 0, sizeof(priv->ampdu));
5718
5719         /*
5720          * Temporarily enable interrupts.  Initial firmware host
5721          * commands use interrupts and avoid polling.  Disable
5722          * interrupts when done.
5723          */
5724         iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5725
5726         /* Get config data, mac addrs etc */
5727         if (priv->ap_fw) {
5728                 rc = mwl8k_cmd_get_hw_spec_ap(hw);
5729                 if (!rc)
5730                         rc = mwl8k_init_txqs(hw);
5731                 if (!rc)
5732                         rc = mwl8k_cmd_set_hw_spec(hw);
5733         } else {
5734                 rc = mwl8k_cmd_get_hw_spec_sta(hw);
5735         }
5736         if (rc) {
5737                 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5738                 goto err_free_irq;
5739         }
5740
5741         /* Turn radio off */
5742         rc = mwl8k_cmd_radio_disable(hw);
5743         if (rc) {
5744                 wiphy_err(hw->wiphy, "Cannot disable\n");
5745                 goto err_free_irq;
5746         }
5747
5748         /* Clear MAC address */
5749         rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5750         if (rc) {
5751                 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5752                 goto err_free_irq;
5753         }
5754
5755         /* Configure Antennas */
5756         rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
5757         if (rc)
5758                 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
5759         rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
5760         if (rc)
5761                 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
5762
5763
5764         /* Disable interrupts */
5765         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5766         free_irq(priv->pdev->irq, hw);
5767
5768         wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5769                    priv->device_info->part_name,
5770                    priv->hw_rev, hw->wiphy->perm_addr,
5771                    priv->ap_fw ? "AP" : "STA",
5772                    (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5773                    (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5774
5775         return 0;
5776
5777 err_free_irq:
5778         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5779         free_irq(priv->pdev->irq, hw);
5780
5781 err_free_queues:
5782         for (i = 0; i < mwl8k_tx_queues(priv); i++)
5783                 mwl8k_txq_deinit(hw, i);
5784         mwl8k_rxq_deinit(hw, 0);
5785
5786 err_stop_firmware:
5787         mwl8k_hw_reset(priv);
5788
5789         return rc;
5790 }
5791
5792 /*
5793  * invoke mwl8k_reload_firmware to change the firmware image after the device
5794  * has already been registered
5795  */
5796 static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5797 {
5798         int i, rc = 0;
5799         struct mwl8k_priv *priv = hw->priv;
5800         struct mwl8k_vif *vif, *tmp_vif;
5801
5802         mwl8k_stop(hw);
5803         mwl8k_rxq_deinit(hw, 0);
5804
5805         /*
5806          * All the existing interfaces are re-added by the ieee80211_reconfig;
5807          * which means driver should remove existing interfaces before calling
5808          * ieee80211_restart_hw
5809          */
5810         if (priv->hw_restart_in_progress)
5811                 list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
5812                         mwl8k_remove_vif(priv, vif);
5813
5814         for (i = 0; i < mwl8k_tx_queues(priv); i++)
5815                 mwl8k_txq_deinit(hw, i);
5816
5817         rc = mwl8k_init_firmware(hw, fw_image, false);
5818         if (rc)
5819                 goto fail;
5820
5821         rc = mwl8k_probe_hw(hw);
5822         if (rc)
5823                 goto fail;
5824
5825         if (priv->hw_restart_in_progress)
5826                 return rc;
5827
5828         rc = mwl8k_start(hw);
5829         if (rc)
5830                 goto fail;
5831
5832         rc = mwl8k_config(hw, ~0);
5833         if (rc)
5834                 goto fail;
5835
5836         for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
5837                 rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
5838                 if (rc)
5839                         goto fail;
5840         }
5841
5842         return rc;
5843
5844 fail:
5845         printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5846         return rc;
5847 }
5848
5849 static const struct ieee80211_iface_limit ap_if_limits[] = {
5850         { .max = 8,     .types = BIT(NL80211_IFTYPE_AP) },
5851         { .max = 1,     .types = BIT(NL80211_IFTYPE_STATION) },
5852 };
5853
5854 static const struct ieee80211_iface_combination ap_if_comb = {
5855         .limits = ap_if_limits,
5856         .n_limits = ARRAY_SIZE(ap_if_limits),
5857         .max_interfaces = 8,
5858         .num_different_channels = 1,
5859 };
5860
5861
5862 static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5863 {
5864         struct ieee80211_hw *hw = priv->hw;
5865         int i, rc;
5866
5867         rc = mwl8k_load_firmware(hw);
5868         mwl8k_release_firmware(priv);
5869         if (rc) {
5870                 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5871                 return rc;
5872         }
5873
5874         /*
5875          * Extra headroom is the size of the required DMA header
5876          * minus the size of the smallest 802.11 frame (CTS frame).
5877          */
5878         hw->extra_tx_headroom =
5879                 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5880
5881         hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
5882
5883         hw->channel_change_time = 10;
5884
5885         hw->queues = MWL8K_TX_WMM_QUEUES;
5886
5887         /* Set rssi values to dBm */
5888         hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
5889
5890         /*
5891          * Ask mac80211 to not to trigger PS mode
5892          * based on PM bit of incoming frames.
5893          */
5894         if (priv->ap_fw)
5895                 hw->flags |= IEEE80211_HW_AP_LINK_PS;
5896
5897         hw->vif_data_size = sizeof(struct mwl8k_vif);
5898         hw->sta_data_size = sizeof(struct mwl8k_sta);
5899
5900         priv->macids_used = 0;
5901         INIT_LIST_HEAD(&priv->vif_list);
5902
5903         /* Set default radio state and preamble */
5904         priv->radio_on = false;
5905         priv->radio_short_preamble = false;
5906
5907         /* Finalize join worker */
5908         INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
5909         /* Handle watchdog ba events */
5910         INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
5911         /* To reload the firmware if it crashes */
5912         INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
5913
5914         /* TX reclaim and RX tasklets.  */
5915         tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5916         tasklet_disable(&priv->poll_tx_task);
5917         tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5918         tasklet_disable(&priv->poll_rx_task);
5919
5920         /* Power management cookie */
5921         priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5922         if (priv->cookie == NULL)
5923                 return -ENOMEM;
5924
5925         mutex_init(&priv->fw_mutex);
5926         priv->fw_mutex_owner = NULL;
5927         priv->fw_mutex_depth = 0;
5928         priv->hostcmd_wait = NULL;
5929
5930         spin_lock_init(&priv->tx_lock);
5931
5932         spin_lock_init(&priv->stream_lock);
5933
5934         priv->tx_wait = NULL;
5935
5936         rc = mwl8k_probe_hw(hw);
5937         if (rc)
5938                 goto err_free_cookie;
5939
5940         hw->wiphy->interface_modes = 0;
5941
5942         if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
5943                 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
5944                 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5945                 hw->wiphy->iface_combinations = &ap_if_comb;
5946                 hw->wiphy->n_iface_combinations = 1;
5947         }
5948
5949         if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5950                 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5951
5952         rc = ieee80211_register_hw(hw);
5953         if (rc) {
5954                 wiphy_err(hw->wiphy, "Cannot register device\n");
5955                 goto err_unprobe_hw;
5956         }
5957
5958         return 0;
5959
5960 err_unprobe_hw:
5961         for (i = 0; i < mwl8k_tx_queues(priv); i++)
5962                 mwl8k_txq_deinit(hw, i);
5963         mwl8k_rxq_deinit(hw, 0);
5964
5965 err_free_cookie:
5966         if (priv->cookie != NULL)
5967                 pci_free_consistent(priv->pdev, 4,
5968                                 priv->cookie, priv->cookie_dma);
5969
5970         return rc;
5971 }
5972 static int mwl8k_probe(struct pci_dev *pdev,
5973                                  const struct pci_device_id *id)
5974 {
5975         static int printed_version;
5976         struct ieee80211_hw *hw;
5977         struct mwl8k_priv *priv;
5978         struct mwl8k_device_info *di;
5979         int rc;
5980
5981         if (!printed_version) {
5982                 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5983                 printed_version = 1;
5984         }
5985
5986
5987         rc = pci_enable_device(pdev);
5988         if (rc) {
5989                 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
5990                        MWL8K_NAME);
5991                 return rc;
5992         }
5993
5994         rc = pci_request_regions(pdev, MWL8K_NAME);
5995         if (rc) {
5996                 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
5997                        MWL8K_NAME);
5998                 goto err_disable_device;
5999         }
6000
6001         pci_set_master(pdev);
6002
6003
6004         hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
6005         if (hw == NULL) {
6006                 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
6007                 rc = -ENOMEM;
6008                 goto err_free_reg;
6009         }
6010
6011         SET_IEEE80211_DEV(hw, &pdev->dev);
6012         pci_set_drvdata(pdev, hw);
6013
6014         priv = hw->priv;
6015         priv->hw = hw;
6016         priv->pdev = pdev;
6017         priv->device_info = &mwl8k_info_tbl[id->driver_data];
6018
6019         if (id->driver_data == MWL8764)
6020                 priv->is_8764 = true;
6021
6022         priv->sram = pci_iomap(pdev, 0, 0x10000);
6023         if (priv->sram == NULL) {
6024                 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
6025                 rc = -EIO;
6026                 goto err_iounmap;
6027         }
6028
6029         /*
6030          * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
6031          * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
6032          */
6033         priv->regs = pci_iomap(pdev, 1, 0x10000);
6034         if (priv->regs == NULL) {
6035                 priv->regs = pci_iomap(pdev, 2, 0x10000);
6036                 if (priv->regs == NULL) {
6037                         wiphy_err(hw->wiphy, "Cannot map device registers\n");
6038                         rc = -EIO;
6039                         goto err_iounmap;
6040                 }
6041         }
6042
6043         /*
6044          * Choose the initial fw image depending on user input.  If a second
6045          * image is available, make it the alternative image that will be
6046          * loaded if the first one fails.
6047          */
6048         init_completion(&priv->firmware_loading_complete);
6049         di = priv->device_info;
6050         if (ap_mode_default && di->fw_image_ap) {
6051                 priv->fw_pref = di->fw_image_ap;
6052                 priv->fw_alt = di->fw_image_sta;
6053         } else if (!ap_mode_default && di->fw_image_sta) {
6054                 priv->fw_pref = di->fw_image_sta;
6055                 priv->fw_alt = di->fw_image_ap;
6056         } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
6057                 printk(KERN_WARNING "AP fw is unavailable.  Using STA fw.");
6058                 priv->fw_pref = di->fw_image_sta;
6059         } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
6060                 printk(KERN_WARNING "STA fw is unavailable.  Using AP fw.");
6061                 priv->fw_pref = di->fw_image_ap;
6062         }
6063         rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
6064         if (rc)
6065                 goto err_stop_firmware;
6066
6067         priv->hw_restart_in_progress = false;
6068
6069         priv->running_bsses = 0;
6070
6071         return rc;
6072
6073 err_stop_firmware:
6074         mwl8k_hw_reset(priv);
6075
6076 err_iounmap:
6077         if (priv->regs != NULL)
6078                 pci_iounmap(pdev, priv->regs);
6079
6080         if (priv->sram != NULL)
6081                 pci_iounmap(pdev, priv->sram);
6082
6083         pci_set_drvdata(pdev, NULL);
6084         ieee80211_free_hw(hw);
6085
6086 err_free_reg:
6087         pci_release_regions(pdev);
6088
6089 err_disable_device:
6090         pci_disable_device(pdev);
6091
6092         return rc;
6093 }
6094
6095 static void mwl8k_remove(struct pci_dev *pdev)
6096 {
6097         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
6098         struct mwl8k_priv *priv;
6099         int i;
6100
6101         if (hw == NULL)
6102                 return;
6103         priv = hw->priv;
6104
6105         wait_for_completion(&priv->firmware_loading_complete);
6106
6107         if (priv->fw_state == FW_STATE_ERROR) {
6108                 mwl8k_hw_reset(priv);
6109                 goto unmap;
6110         }
6111
6112         ieee80211_stop_queues(hw);
6113
6114         ieee80211_unregister_hw(hw);
6115
6116         /* Remove TX reclaim and RX tasklets.  */
6117         tasklet_kill(&priv->poll_tx_task);
6118         tasklet_kill(&priv->poll_rx_task);
6119
6120         /* Stop hardware */
6121         mwl8k_hw_reset(priv);
6122
6123         /* Return all skbs to mac80211 */
6124         for (i = 0; i < mwl8k_tx_queues(priv); i++)
6125                 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
6126
6127         for (i = 0; i < mwl8k_tx_queues(priv); i++)
6128                 mwl8k_txq_deinit(hw, i);
6129
6130         mwl8k_rxq_deinit(hw, 0);
6131
6132         pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
6133
6134 unmap:
6135         pci_iounmap(pdev, priv->regs);
6136         pci_iounmap(pdev, priv->sram);
6137         pci_set_drvdata(pdev, NULL);
6138         ieee80211_free_hw(hw);
6139         pci_release_regions(pdev);
6140         pci_disable_device(pdev);
6141 }
6142
6143 static struct pci_driver mwl8k_driver = {
6144         .name           = MWL8K_NAME,
6145         .id_table       = mwl8k_pci_id_table,
6146         .probe          = mwl8k_probe,
6147         .remove         = mwl8k_remove,
6148 };
6149
6150 module_pci_driver(mwl8k_driver);
6151
6152 MODULE_DESCRIPTION(MWL8K_DESC);
6153 MODULE_VERSION(MWL8K_VERSION);
6154 MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
6155 MODULE_LICENSE("GPL");