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