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