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