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