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