mwl8k: correctly set the mac_type field for AP SET_MAC_ADDR
[safe/jmp/linux-2.6] / 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 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 <net/mac80211.h>
23 #include <linux/moduleparam.h>
24 #include <linux/firmware.h>
25 #include <linux/workqueue.h>
26
27 #define MWL8K_DESC      "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
28 #define MWL8K_NAME      KBUILD_MODNAME
29 #define MWL8K_VERSION   "0.11"
30
31 /* Register definitions */
32 #define MWL8K_HIU_GEN_PTR                       0x00000c10
33 #define  MWL8K_MODE_STA                          0x0000005a
34 #define  MWL8K_MODE_AP                           0x000000a5
35 #define MWL8K_HIU_INT_CODE                      0x00000c14
36 #define  MWL8K_FWSTA_READY                       0xf0f1f2f4
37 #define  MWL8K_FWAP_READY                        0xf1f2f4a5
38 #define  MWL8K_INT_CODE_CMD_FINISHED             0x00000005
39 #define MWL8K_HIU_SCRATCH                       0x00000c40
40
41 /* Host->device communications */
42 #define MWL8K_HIU_H2A_INTERRUPT_EVENTS          0x00000c18
43 #define MWL8K_HIU_H2A_INTERRUPT_STATUS          0x00000c1c
44 #define MWL8K_HIU_H2A_INTERRUPT_MASK            0x00000c20
45 #define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL       0x00000c24
46 #define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK     0x00000c28
47 #define  MWL8K_H2A_INT_DUMMY                     (1 << 20)
48 #define  MWL8K_H2A_INT_RESET                     (1 << 15)
49 #define  MWL8K_H2A_INT_DOORBELL                  (1 << 1)
50 #define  MWL8K_H2A_INT_PPA_READY                 (1 << 0)
51
52 /* Device->host communications */
53 #define MWL8K_HIU_A2H_INTERRUPT_EVENTS          0x00000c2c
54 #define MWL8K_HIU_A2H_INTERRUPT_STATUS          0x00000c30
55 #define MWL8K_HIU_A2H_INTERRUPT_MASK            0x00000c34
56 #define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL       0x00000c38
57 #define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK     0x00000c3c
58 #define  MWL8K_A2H_INT_DUMMY                     (1 << 20)
59 #define  MWL8K_A2H_INT_CHNL_SWITCHED             (1 << 11)
60 #define  MWL8K_A2H_INT_QUEUE_EMPTY               (1 << 10)
61 #define  MWL8K_A2H_INT_RADAR_DETECT              (1 << 7)
62 #define  MWL8K_A2H_INT_RADIO_ON                  (1 << 6)
63 #define  MWL8K_A2H_INT_RADIO_OFF                 (1 << 5)
64 #define  MWL8K_A2H_INT_MAC_EVENT                 (1 << 3)
65 #define  MWL8K_A2H_INT_OPC_DONE                  (1 << 2)
66 #define  MWL8K_A2H_INT_RX_READY                  (1 << 1)
67 #define  MWL8K_A2H_INT_TX_DONE                   (1 << 0)
68
69 #define MWL8K_A2H_EVENTS        (MWL8K_A2H_INT_DUMMY | \
70                                  MWL8K_A2H_INT_CHNL_SWITCHED | \
71                                  MWL8K_A2H_INT_QUEUE_EMPTY | \
72                                  MWL8K_A2H_INT_RADAR_DETECT | \
73                                  MWL8K_A2H_INT_RADIO_ON | \
74                                  MWL8K_A2H_INT_RADIO_OFF | \
75                                  MWL8K_A2H_INT_MAC_EVENT | \
76                                  MWL8K_A2H_INT_OPC_DONE | \
77                                  MWL8K_A2H_INT_RX_READY | \
78                                  MWL8K_A2H_INT_TX_DONE)
79
80 #define MWL8K_RX_QUEUES         1
81 #define MWL8K_TX_QUEUES         4
82
83 struct rxd_ops {
84         int rxd_size;
85         void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
86         void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
87         int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
88                            __le16 *qos);
89 };
90
91 struct mwl8k_device_info {
92         char *part_name;
93         char *helper_image;
94         char *fw_image;
95         struct rxd_ops *ap_rxd_ops;
96 };
97
98 struct mwl8k_rx_queue {
99         int rxd_count;
100
101         /* hw receives here */
102         int head;
103
104         /* refill descs here */
105         int tail;
106
107         void *rxd;
108         dma_addr_t rxd_dma;
109         struct {
110                 struct sk_buff *skb;
111                 DECLARE_PCI_UNMAP_ADDR(dma)
112         } *buf;
113 };
114
115 struct mwl8k_tx_queue {
116         /* hw transmits here */
117         int head;
118
119         /* sw appends here */
120         int tail;
121
122         struct ieee80211_tx_queue_stats stats;
123         struct mwl8k_tx_desc *txd;
124         dma_addr_t txd_dma;
125         struct sk_buff **skb;
126 };
127
128 struct mwl8k_priv {
129         struct ieee80211_hw *hw;
130         struct pci_dev *pdev;
131
132         struct mwl8k_device_info *device_info;
133
134         void __iomem *sram;
135         void __iomem *regs;
136
137         /* firmware */
138         struct firmware *fw_helper;
139         struct firmware *fw_ucode;
140
141         /* hardware/firmware parameters */
142         bool ap_fw;
143         struct rxd_ops *rxd_ops;
144
145         /* firmware access */
146         struct mutex fw_mutex;
147         struct task_struct *fw_mutex_owner;
148         int fw_mutex_depth;
149         struct completion *hostcmd_wait;
150
151         /* lock held over TX and TX reap */
152         spinlock_t tx_lock;
153
154         /* TX quiesce completion, protected by fw_mutex and tx_lock */
155         struct completion *tx_wait;
156
157         struct ieee80211_vif *vif;
158
159         struct ieee80211_channel *current_channel;
160
161         /* power management status cookie from firmware */
162         u32 *cookie;
163         dma_addr_t cookie_dma;
164
165         u16 num_mcaddrs;
166         u8 hw_rev;
167         u32 fw_rev;
168
169         /*
170          * Running count of TX packets in flight, to avoid
171          * iterating over the transmit rings each time.
172          */
173         int pending_tx_pkts;
174
175         struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
176         struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
177
178         /* PHY parameters */
179         struct ieee80211_supported_band band;
180         struct ieee80211_channel channels[14];
181         struct ieee80211_rate rates[14];
182
183         bool radio_on;
184         bool radio_short_preamble;
185         bool sniffer_enabled;
186         bool wmm_enabled;
187
188         struct work_struct sta_notify_worker;
189         spinlock_t sta_notify_list_lock;
190         struct list_head sta_notify_list;
191
192         /* XXX need to convert this to handle multiple interfaces */
193         bool capture_beacon;
194         u8 capture_bssid[ETH_ALEN];
195         struct sk_buff *beacon_skb;
196
197         /*
198          * This FJ worker has to be global as it is scheduled from the
199          * RX handler.  At this point we don't know which interface it
200          * belongs to until the list of bssids waiting to complete join
201          * is checked.
202          */
203         struct work_struct finalize_join_worker;
204
205         /* Tasklet to reclaim TX descriptors and buffers after tx */
206         struct tasklet_struct tx_reclaim_task;
207 };
208
209 /* Per interface specific private data */
210 struct mwl8k_vif {
211         /* Non AMPDU sequence number assigned by driver.  */
212         u16 seqno;
213 };
214 #define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
215
216 struct mwl8k_sta {
217         /* Index into station database. Returned by UPDATE_STADB.  */
218         u8 peer_id;
219 };
220 #define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
221
222 static const struct ieee80211_channel mwl8k_channels[] = {
223         { .center_freq = 2412, .hw_value = 1, },
224         { .center_freq = 2417, .hw_value = 2, },
225         { .center_freq = 2422, .hw_value = 3, },
226         { .center_freq = 2427, .hw_value = 4, },
227         { .center_freq = 2432, .hw_value = 5, },
228         { .center_freq = 2437, .hw_value = 6, },
229         { .center_freq = 2442, .hw_value = 7, },
230         { .center_freq = 2447, .hw_value = 8, },
231         { .center_freq = 2452, .hw_value = 9, },
232         { .center_freq = 2457, .hw_value = 10, },
233         { .center_freq = 2462, .hw_value = 11, },
234         { .center_freq = 2467, .hw_value = 12, },
235         { .center_freq = 2472, .hw_value = 13, },
236         { .center_freq = 2484, .hw_value = 14, },
237 };
238
239 static const struct ieee80211_rate mwl8k_rates[] = {
240         { .bitrate = 10, .hw_value = 2, },
241         { .bitrate = 20, .hw_value = 4, },
242         { .bitrate = 55, .hw_value = 11, },
243         { .bitrate = 110, .hw_value = 22, },
244         { .bitrate = 220, .hw_value = 44, },
245         { .bitrate = 60, .hw_value = 12, },
246         { .bitrate = 90, .hw_value = 18, },
247         { .bitrate = 120, .hw_value = 24, },
248         { .bitrate = 180, .hw_value = 36, },
249         { .bitrate = 240, .hw_value = 48, },
250         { .bitrate = 360, .hw_value = 72, },
251         { .bitrate = 480, .hw_value = 96, },
252         { .bitrate = 540, .hw_value = 108, },
253         { .bitrate = 720, .hw_value = 144, },
254 };
255
256 /* Set or get info from Firmware */
257 #define MWL8K_CMD_SET                   0x0001
258 #define MWL8K_CMD_GET                   0x0000
259
260 /* Firmware command codes */
261 #define MWL8K_CMD_CODE_DNLD             0x0001
262 #define MWL8K_CMD_GET_HW_SPEC           0x0003
263 #define MWL8K_CMD_SET_HW_SPEC           0x0004
264 #define MWL8K_CMD_MAC_MULTICAST_ADR     0x0010
265 #define MWL8K_CMD_GET_STAT              0x0014
266 #define MWL8K_CMD_RADIO_CONTROL         0x001c
267 #define MWL8K_CMD_RF_TX_POWER           0x001e
268 #define MWL8K_CMD_RF_ANTENNA            0x0020
269 #define MWL8K_CMD_SET_PRE_SCAN          0x0107
270 #define MWL8K_CMD_SET_POST_SCAN         0x0108
271 #define MWL8K_CMD_SET_RF_CHANNEL        0x010a
272 #define MWL8K_CMD_SET_AID               0x010d
273 #define MWL8K_CMD_SET_RATE              0x0110
274 #define MWL8K_CMD_SET_FINALIZE_JOIN     0x0111
275 #define MWL8K_CMD_RTS_THRESHOLD         0x0113
276 #define MWL8K_CMD_SET_SLOT              0x0114
277 #define MWL8K_CMD_SET_EDCA_PARAMS       0x0115
278 #define MWL8K_CMD_SET_WMM_MODE          0x0123
279 #define MWL8K_CMD_MIMO_CONFIG           0x0125
280 #define MWL8K_CMD_USE_FIXED_RATE        0x0126
281 #define MWL8K_CMD_ENABLE_SNIFFER        0x0150
282 #define MWL8K_CMD_SET_MAC_ADDR          0x0202
283 #define MWL8K_CMD_SET_RATEADAPT_MODE    0x0203
284 #define MWL8K_CMD_SET_NEW_STN           0x1111
285 #define MWL8K_CMD_UPDATE_STADB          0x1123
286
287 static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
288 {
289 #define MWL8K_CMDNAME(x)        case MWL8K_CMD_##x: do {\
290                                         snprintf(buf, bufsize, "%s", #x);\
291                                         return buf;\
292                                         } while (0)
293         switch (cmd & ~0x8000) {
294                 MWL8K_CMDNAME(CODE_DNLD);
295                 MWL8K_CMDNAME(GET_HW_SPEC);
296                 MWL8K_CMDNAME(SET_HW_SPEC);
297                 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
298                 MWL8K_CMDNAME(GET_STAT);
299                 MWL8K_CMDNAME(RADIO_CONTROL);
300                 MWL8K_CMDNAME(RF_TX_POWER);
301                 MWL8K_CMDNAME(RF_ANTENNA);
302                 MWL8K_CMDNAME(SET_PRE_SCAN);
303                 MWL8K_CMDNAME(SET_POST_SCAN);
304                 MWL8K_CMDNAME(SET_RF_CHANNEL);
305                 MWL8K_CMDNAME(SET_AID);
306                 MWL8K_CMDNAME(SET_RATE);
307                 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
308                 MWL8K_CMDNAME(RTS_THRESHOLD);
309                 MWL8K_CMDNAME(SET_SLOT);
310                 MWL8K_CMDNAME(SET_EDCA_PARAMS);
311                 MWL8K_CMDNAME(SET_WMM_MODE);
312                 MWL8K_CMDNAME(MIMO_CONFIG);
313                 MWL8K_CMDNAME(USE_FIXED_RATE);
314                 MWL8K_CMDNAME(ENABLE_SNIFFER);
315                 MWL8K_CMDNAME(SET_MAC_ADDR);
316                 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
317                 MWL8K_CMDNAME(SET_NEW_STN);
318                 MWL8K_CMDNAME(UPDATE_STADB);
319         default:
320                 snprintf(buf, bufsize, "0x%x", cmd);
321         }
322 #undef MWL8K_CMDNAME
323
324         return buf;
325 }
326
327 /* Hardware and firmware reset */
328 static void mwl8k_hw_reset(struct mwl8k_priv *priv)
329 {
330         iowrite32(MWL8K_H2A_INT_RESET,
331                 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
332         iowrite32(MWL8K_H2A_INT_RESET,
333                 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
334         msleep(20);
335 }
336
337 /* Release fw image */
338 static void mwl8k_release_fw(struct firmware **fw)
339 {
340         if (*fw == NULL)
341                 return;
342         release_firmware(*fw);
343         *fw = NULL;
344 }
345
346 static void mwl8k_release_firmware(struct mwl8k_priv *priv)
347 {
348         mwl8k_release_fw(&priv->fw_ucode);
349         mwl8k_release_fw(&priv->fw_helper);
350 }
351
352 /* Request fw image */
353 static int mwl8k_request_fw(struct mwl8k_priv *priv,
354                             const char *fname, struct firmware **fw)
355 {
356         /* release current image */
357         if (*fw != NULL)
358                 mwl8k_release_fw(fw);
359
360         return request_firmware((const struct firmware **)fw,
361                                 fname, &priv->pdev->dev);
362 }
363
364 static int mwl8k_request_firmware(struct mwl8k_priv *priv)
365 {
366         struct mwl8k_device_info *di = priv->device_info;
367         int rc;
368
369         if (di->helper_image != NULL) {
370                 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
371                 if (rc) {
372                         printk(KERN_ERR "%s: Error requesting helper "
373                                "firmware file %s\n", pci_name(priv->pdev),
374                                di->helper_image);
375                         return rc;
376                 }
377         }
378
379         rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
380         if (rc) {
381                 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
382                        pci_name(priv->pdev), di->fw_image);
383                 mwl8k_release_fw(&priv->fw_helper);
384                 return rc;
385         }
386
387         return 0;
388 }
389
390 MODULE_FIRMWARE("mwl8k/helper_8687.fw");
391 MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
392
393 struct mwl8k_cmd_pkt {
394         __le16  code;
395         __le16  length;
396         __le16  seq_num;
397         __le16  result;
398         char    payload[0];
399 } __attribute__((packed));
400
401 /*
402  * Firmware loading.
403  */
404 static int
405 mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
406 {
407         void __iomem *regs = priv->regs;
408         dma_addr_t dma_addr;
409         int loops;
410
411         dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
412         if (pci_dma_mapping_error(priv->pdev, dma_addr))
413                 return -ENOMEM;
414
415         iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
416         iowrite32(0, regs + MWL8K_HIU_INT_CODE);
417         iowrite32(MWL8K_H2A_INT_DOORBELL,
418                 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
419         iowrite32(MWL8K_H2A_INT_DUMMY,
420                 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
421
422         loops = 1000;
423         do {
424                 u32 int_code;
425
426                 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
427                 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
428                         iowrite32(0, regs + MWL8K_HIU_INT_CODE);
429                         break;
430                 }
431
432                 cond_resched();
433                 udelay(1);
434         } while (--loops);
435
436         pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
437
438         return loops ? 0 : -ETIMEDOUT;
439 }
440
441 static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
442                                 const u8 *data, size_t length)
443 {
444         struct mwl8k_cmd_pkt *cmd;
445         int done;
446         int rc = 0;
447
448         cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
449         if (cmd == NULL)
450                 return -ENOMEM;
451
452         cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
453         cmd->seq_num = 0;
454         cmd->result = 0;
455
456         done = 0;
457         while (length) {
458                 int block_size = length > 256 ? 256 : length;
459
460                 memcpy(cmd->payload, data + done, block_size);
461                 cmd->length = cpu_to_le16(block_size);
462
463                 rc = mwl8k_send_fw_load_cmd(priv, cmd,
464                                                 sizeof(*cmd) + block_size);
465                 if (rc)
466                         break;
467
468                 done += block_size;
469                 length -= block_size;
470         }
471
472         if (!rc) {
473                 cmd->length = 0;
474                 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
475         }
476
477         kfree(cmd);
478
479         return rc;
480 }
481
482 static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
483                                 const u8 *data, size_t length)
484 {
485         unsigned char *buffer;
486         int may_continue, rc = 0;
487         u32 done, prev_block_size;
488
489         buffer = kmalloc(1024, GFP_KERNEL);
490         if (buffer == NULL)
491                 return -ENOMEM;
492
493         done = 0;
494         prev_block_size = 0;
495         may_continue = 1000;
496         while (may_continue > 0) {
497                 u32 block_size;
498
499                 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
500                 if (block_size & 1) {
501                         block_size &= ~1;
502                         may_continue--;
503                 } else {
504                         done += prev_block_size;
505                         length -= prev_block_size;
506                 }
507
508                 if (block_size > 1024 || block_size > length) {
509                         rc = -EOVERFLOW;
510                         break;
511                 }
512
513                 if (length == 0) {
514                         rc = 0;
515                         break;
516                 }
517
518                 if (block_size == 0) {
519                         rc = -EPROTO;
520                         may_continue--;
521                         udelay(1);
522                         continue;
523                 }
524
525                 prev_block_size = block_size;
526                 memcpy(buffer, data + done, block_size);
527
528                 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
529                 if (rc)
530                         break;
531         }
532
533         if (!rc && length != 0)
534                 rc = -EREMOTEIO;
535
536         kfree(buffer);
537
538         return rc;
539 }
540
541 static int mwl8k_load_firmware(struct ieee80211_hw *hw)
542 {
543         struct mwl8k_priv *priv = hw->priv;
544         struct firmware *fw = priv->fw_ucode;
545         int rc;
546         int loops;
547
548         if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
549                 struct firmware *helper = priv->fw_helper;
550
551                 if (helper == NULL) {
552                         printk(KERN_ERR "%s: helper image needed but none "
553                                "given\n", pci_name(priv->pdev));
554                         return -EINVAL;
555                 }
556
557                 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
558                 if (rc) {
559                         printk(KERN_ERR "%s: unable to load firmware "
560                                "helper image\n", pci_name(priv->pdev));
561                         return rc;
562                 }
563                 msleep(5);
564
565                 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
566         } else {
567                 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
568         }
569
570         if (rc) {
571                 printk(KERN_ERR "%s: unable to load firmware image\n",
572                        pci_name(priv->pdev));
573                 return rc;
574         }
575
576         iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
577
578         loops = 500000;
579         do {
580                 u32 ready_code;
581
582                 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
583                 if (ready_code == MWL8K_FWAP_READY) {
584                         priv->ap_fw = 1;
585                         break;
586                 } else if (ready_code == MWL8K_FWSTA_READY) {
587                         priv->ap_fw = 0;
588                         break;
589                 }
590
591                 cond_resched();
592                 udelay(1);
593         } while (--loops);
594
595         return loops ? 0 : -ETIMEDOUT;
596 }
597
598
599 /* DMA header used by firmware and hardware.  */
600 struct mwl8k_dma_data {
601         __le16 fwlen;
602         struct ieee80211_hdr wh;
603         char data[0];
604 } __attribute__((packed));
605
606 /* Routines to add/remove DMA header from skb.  */
607 static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
608 {
609         struct mwl8k_dma_data *tr;
610         int hdrlen;
611
612         tr = (struct mwl8k_dma_data *)skb->data;
613         hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
614
615         if (hdrlen != sizeof(tr->wh)) {
616                 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
617                         memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
618                         *((__le16 *)(tr->data - 2)) = qos;
619                 } else {
620                         memmove(tr->data - hdrlen, &tr->wh, hdrlen);
621                 }
622         }
623
624         if (hdrlen != sizeof(*tr))
625                 skb_pull(skb, sizeof(*tr) - hdrlen);
626 }
627
628 static inline void mwl8k_add_dma_header(struct sk_buff *skb)
629 {
630         struct ieee80211_hdr *wh;
631         int hdrlen;
632         struct mwl8k_dma_data *tr;
633
634         /*
635          * Add a firmware DMA header; the firmware requires that we
636          * present a 2-byte payload length followed by a 4-address
637          * header (without QoS field), followed (optionally) by any
638          * WEP/ExtIV header (but only filled in for CCMP).
639          */
640         wh = (struct ieee80211_hdr *)skb->data;
641
642         hdrlen = ieee80211_hdrlen(wh->frame_control);
643         if (hdrlen != sizeof(*tr))
644                 skb_push(skb, sizeof(*tr) - hdrlen);
645
646         if (ieee80211_is_data_qos(wh->frame_control))
647                 hdrlen -= 2;
648
649         tr = (struct mwl8k_dma_data *)skb->data;
650         if (wh != &tr->wh)
651                 memmove(&tr->wh, wh, hdrlen);
652         if (hdrlen != sizeof(tr->wh))
653                 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
654
655         /*
656          * Firmware length is the length of the fully formed "802.11
657          * payload".  That is, everything except for the 802.11 header.
658          * This includes all crypto material including the MIC.
659          */
660         tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
661 }
662
663
664 /*
665  * Packet reception for 88w8366 AP firmware.
666  */
667 struct mwl8k_rxd_8366_ap {
668         __le16 pkt_len;
669         __u8 sq2;
670         __u8 rate;
671         __le32 pkt_phys_addr;
672         __le32 next_rxd_phys_addr;
673         __le16 qos_control;
674         __le16 htsig2;
675         __le32 hw_rssi_info;
676         __le32 hw_noise_floor_info;
677         __u8 noise_floor;
678         __u8 pad0[3];
679         __u8 rssi;
680         __u8 rx_status;
681         __u8 channel;
682         __u8 rx_ctrl;
683 } __attribute__((packed));
684
685 #define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT      0x80
686 #define MWL8K_8366_AP_RATE_INFO_40MHZ           0x40
687 #define MWL8K_8366_AP_RATE_INFO_RATEID(x)       ((x) & 0x3f)
688
689 #define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST     0x80
690
691 static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
692 {
693         struct mwl8k_rxd_8366_ap *rxd = _rxd;
694
695         rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
696         rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
697 }
698
699 static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
700 {
701         struct mwl8k_rxd_8366_ap *rxd = _rxd;
702
703         rxd->pkt_len = cpu_to_le16(len);
704         rxd->pkt_phys_addr = cpu_to_le32(addr);
705         wmb();
706         rxd->rx_ctrl = 0;
707 }
708
709 static int
710 mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
711                           __le16 *qos)
712 {
713         struct mwl8k_rxd_8366_ap *rxd = _rxd;
714
715         if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
716                 return -1;
717         rmb();
718
719         memset(status, 0, sizeof(*status));
720
721         status->signal = -rxd->rssi;
722         status->noise = -rxd->noise_floor;
723
724         if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
725                 status->flag |= RX_FLAG_HT;
726                 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
727                         status->flag |= RX_FLAG_40MHZ;
728                 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
729         } else {
730                 int i;
731
732                 for (i = 0; i < ARRAY_SIZE(mwl8k_rates); i++) {
733                         if (mwl8k_rates[i].hw_value == rxd->rate) {
734                                 status->rate_idx = i;
735                                 break;
736                         }
737                 }
738         }
739
740         status->band = IEEE80211_BAND_2GHZ;
741         status->freq = ieee80211_channel_to_frequency(rxd->channel);
742
743         *qos = rxd->qos_control;
744
745         return le16_to_cpu(rxd->pkt_len);
746 }
747
748 static struct rxd_ops rxd_8366_ap_ops = {
749         .rxd_size       = sizeof(struct mwl8k_rxd_8366_ap),
750         .rxd_init       = mwl8k_rxd_8366_ap_init,
751         .rxd_refill     = mwl8k_rxd_8366_ap_refill,
752         .rxd_process    = mwl8k_rxd_8366_ap_process,
753 };
754
755 /*
756  * Packet reception for STA firmware.
757  */
758 struct mwl8k_rxd_sta {
759         __le16 pkt_len;
760         __u8 link_quality;
761         __u8 noise_level;
762         __le32 pkt_phys_addr;
763         __le32 next_rxd_phys_addr;
764         __le16 qos_control;
765         __le16 rate_info;
766         __le32 pad0[4];
767         __u8 rssi;
768         __u8 channel;
769         __le16 pad1;
770         __u8 rx_ctrl;
771         __u8 rx_status;
772         __u8 pad2[2];
773 } __attribute__((packed));
774
775 #define MWL8K_STA_RATE_INFO_SHORTPRE            0x8000
776 #define MWL8K_STA_RATE_INFO_ANTSELECT(x)        (((x) >> 11) & 0x3)
777 #define MWL8K_STA_RATE_INFO_RATEID(x)           (((x) >> 3) & 0x3f)
778 #define MWL8K_STA_RATE_INFO_40MHZ               0x0004
779 #define MWL8K_STA_RATE_INFO_SHORTGI             0x0002
780 #define MWL8K_STA_RATE_INFO_MCS_FORMAT          0x0001
781
782 #define MWL8K_STA_RX_CTRL_OWNED_BY_HOST         0x02
783
784 static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
785 {
786         struct mwl8k_rxd_sta *rxd = _rxd;
787
788         rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
789         rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
790 }
791
792 static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
793 {
794         struct mwl8k_rxd_sta *rxd = _rxd;
795
796         rxd->pkt_len = cpu_to_le16(len);
797         rxd->pkt_phys_addr = cpu_to_le32(addr);
798         wmb();
799         rxd->rx_ctrl = 0;
800 }
801
802 static int
803 mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
804                        __le16 *qos)
805 {
806         struct mwl8k_rxd_sta *rxd = _rxd;
807         u16 rate_info;
808
809         if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
810                 return -1;
811         rmb();
812
813         rate_info = le16_to_cpu(rxd->rate_info);
814
815         memset(status, 0, sizeof(*status));
816
817         status->signal = -rxd->rssi;
818         status->noise = -rxd->noise_level;
819         status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
820         status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
821
822         if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
823                 status->flag |= RX_FLAG_SHORTPRE;
824         if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
825                 status->flag |= RX_FLAG_40MHZ;
826         if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
827                 status->flag |= RX_FLAG_SHORT_GI;
828         if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
829                 status->flag |= RX_FLAG_HT;
830
831         status->band = IEEE80211_BAND_2GHZ;
832         status->freq = ieee80211_channel_to_frequency(rxd->channel);
833
834         *qos = rxd->qos_control;
835
836         return le16_to_cpu(rxd->pkt_len);
837 }
838
839 static struct rxd_ops rxd_sta_ops = {
840         .rxd_size       = sizeof(struct mwl8k_rxd_sta),
841         .rxd_init       = mwl8k_rxd_sta_init,
842         .rxd_refill     = mwl8k_rxd_sta_refill,
843         .rxd_process    = mwl8k_rxd_sta_process,
844 };
845
846
847 #define MWL8K_RX_DESCS          256
848 #define MWL8K_RX_MAXSZ          3800
849
850 static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
851 {
852         struct mwl8k_priv *priv = hw->priv;
853         struct mwl8k_rx_queue *rxq = priv->rxq + index;
854         int size;
855         int i;
856
857         rxq->rxd_count = 0;
858         rxq->head = 0;
859         rxq->tail = 0;
860
861         size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
862
863         rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
864         if (rxq->rxd == NULL) {
865                 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
866                        wiphy_name(hw->wiphy));
867                 return -ENOMEM;
868         }
869         memset(rxq->rxd, 0, size);
870
871         rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
872         if (rxq->buf == NULL) {
873                 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
874                        wiphy_name(hw->wiphy));
875                 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
876                 return -ENOMEM;
877         }
878         memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
879
880         for (i = 0; i < MWL8K_RX_DESCS; i++) {
881                 int desc_size;
882                 void *rxd;
883                 int nexti;
884                 dma_addr_t next_dma_addr;
885
886                 desc_size = priv->rxd_ops->rxd_size;
887                 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
888
889                 nexti = i + 1;
890                 if (nexti == MWL8K_RX_DESCS)
891                         nexti = 0;
892                 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
893
894                 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
895         }
896
897         return 0;
898 }
899
900 static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
901 {
902         struct mwl8k_priv *priv = hw->priv;
903         struct mwl8k_rx_queue *rxq = priv->rxq + index;
904         int refilled;
905
906         refilled = 0;
907         while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
908                 struct sk_buff *skb;
909                 dma_addr_t addr;
910                 int rx;
911                 void *rxd;
912
913                 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
914                 if (skb == NULL)
915                         break;
916
917                 addr = pci_map_single(priv->pdev, skb->data,
918                                       MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
919
920                 rxq->rxd_count++;
921                 rx = rxq->tail++;
922                 if (rxq->tail == MWL8K_RX_DESCS)
923                         rxq->tail = 0;
924                 rxq->buf[rx].skb = skb;
925                 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
926
927                 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
928                 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
929
930                 refilled++;
931         }
932
933         return refilled;
934 }
935
936 /* Must be called only when the card's reception is completely halted */
937 static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
938 {
939         struct mwl8k_priv *priv = hw->priv;
940         struct mwl8k_rx_queue *rxq = priv->rxq + index;
941         int i;
942
943         for (i = 0; i < MWL8K_RX_DESCS; i++) {
944                 if (rxq->buf[i].skb != NULL) {
945                         pci_unmap_single(priv->pdev,
946                                          pci_unmap_addr(&rxq->buf[i], dma),
947                                          MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
948                         pci_unmap_addr_set(&rxq->buf[i], dma, 0);
949
950                         kfree_skb(rxq->buf[i].skb);
951                         rxq->buf[i].skb = NULL;
952                 }
953         }
954
955         kfree(rxq->buf);
956         rxq->buf = NULL;
957
958         pci_free_consistent(priv->pdev,
959                             MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
960                             rxq->rxd, rxq->rxd_dma);
961         rxq->rxd = NULL;
962 }
963
964
965 /*
966  * Scan a list of BSSIDs to process for finalize join.
967  * Allows for extension to process multiple BSSIDs.
968  */
969 static inline int
970 mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
971 {
972         return priv->capture_beacon &&
973                 ieee80211_is_beacon(wh->frame_control) &&
974                 !compare_ether_addr(wh->addr3, priv->capture_bssid);
975 }
976
977 static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
978                                      struct sk_buff *skb)
979 {
980         struct mwl8k_priv *priv = hw->priv;
981
982         priv->capture_beacon = false;
983         memset(priv->capture_bssid, 0, ETH_ALEN);
984
985         /*
986          * Use GFP_ATOMIC as rxq_process is called from
987          * the primary interrupt handler, memory allocation call
988          * must not sleep.
989          */
990         priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
991         if (priv->beacon_skb != NULL)
992                 ieee80211_queue_work(hw, &priv->finalize_join_worker);
993 }
994
995 static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
996 {
997         struct mwl8k_priv *priv = hw->priv;
998         struct mwl8k_rx_queue *rxq = priv->rxq + index;
999         int processed;
1000
1001         processed = 0;
1002         while (rxq->rxd_count && limit--) {
1003                 struct sk_buff *skb;
1004                 void *rxd;
1005                 int pkt_len;
1006                 struct ieee80211_rx_status status;
1007                 __le16 qos;
1008
1009                 skb = rxq->buf[rxq->head].skb;
1010                 if (skb == NULL)
1011                         break;
1012
1013                 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1014
1015                 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
1016                 if (pkt_len < 0)
1017                         break;
1018
1019                 rxq->buf[rxq->head].skb = NULL;
1020
1021                 pci_unmap_single(priv->pdev,
1022                                  pci_unmap_addr(&rxq->buf[rxq->head], dma),
1023                                  MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1024                 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
1025
1026                 rxq->head++;
1027                 if (rxq->head == MWL8K_RX_DESCS)
1028                         rxq->head = 0;
1029
1030                 rxq->rxd_count--;
1031
1032                 skb_put(skb, pkt_len);
1033                 mwl8k_remove_dma_header(skb, qos);
1034
1035                 /*
1036                  * Check for a pending join operation.  Save a
1037                  * copy of the beacon and schedule a tasklet to
1038                  * send a FINALIZE_JOIN command to the firmware.
1039                  */
1040                 if (mwl8k_capture_bssid(priv, (void *)skb->data))
1041                         mwl8k_save_beacon(hw, skb);
1042
1043                 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1044                 ieee80211_rx_irqsafe(hw, skb);
1045
1046                 processed++;
1047         }
1048
1049         return processed;
1050 }
1051
1052
1053 /*
1054  * Packet transmission.
1055  */
1056
1057 #define MWL8K_TXD_STATUS_OK                     0x00000001
1058 #define MWL8K_TXD_STATUS_OK_RETRY               0x00000002
1059 #define MWL8K_TXD_STATUS_OK_MORE_RETRY          0x00000004
1060 #define MWL8K_TXD_STATUS_MULTICAST_TX           0x00000008
1061 #define MWL8K_TXD_STATUS_FW_OWNED               0x80000000
1062
1063 #define MWL8K_QOS_QLEN_UNSPEC                   0xff00
1064 #define MWL8K_QOS_ACK_POLICY_MASK               0x0060
1065 #define MWL8K_QOS_ACK_POLICY_NORMAL             0x0000
1066 #define MWL8K_QOS_ACK_POLICY_BLOCKACK           0x0060
1067 #define MWL8K_QOS_EOSP                          0x0010
1068
1069 struct mwl8k_tx_desc {
1070         __le32 status;
1071         __u8 data_rate;
1072         __u8 tx_priority;
1073         __le16 qos_control;
1074         __le32 pkt_phys_addr;
1075         __le16 pkt_len;
1076         __u8 dest_MAC_addr[ETH_ALEN];
1077         __le32 next_txd_phys_addr;
1078         __le32 reserved;
1079         __le16 rate_info;
1080         __u8 peer_id;
1081         __u8 tx_frag_cnt;
1082 } __attribute__((packed));
1083
1084 #define MWL8K_TX_DESCS          128
1085
1086 static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1087 {
1088         struct mwl8k_priv *priv = hw->priv;
1089         struct mwl8k_tx_queue *txq = priv->txq + index;
1090         int size;
1091         int i;
1092
1093         memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1094         txq->stats.limit = MWL8K_TX_DESCS;
1095         txq->head = 0;
1096         txq->tail = 0;
1097
1098         size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1099
1100         txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1101         if (txq->txd == NULL) {
1102                 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
1103                        wiphy_name(hw->wiphy));
1104                 return -ENOMEM;
1105         }
1106         memset(txq->txd, 0, size);
1107
1108         txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1109         if (txq->skb == NULL) {
1110                 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
1111                        wiphy_name(hw->wiphy));
1112                 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
1113                 return -ENOMEM;
1114         }
1115         memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
1116
1117         for (i = 0; i < MWL8K_TX_DESCS; i++) {
1118                 struct mwl8k_tx_desc *tx_desc;
1119                 int nexti;
1120
1121                 tx_desc = txq->txd + i;
1122                 nexti = (i + 1) % MWL8K_TX_DESCS;
1123
1124                 tx_desc->status = 0;
1125                 tx_desc->next_txd_phys_addr =
1126                         cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
1127         }
1128
1129         return 0;
1130 }
1131
1132 static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1133 {
1134         iowrite32(MWL8K_H2A_INT_PPA_READY,
1135                 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1136         iowrite32(MWL8K_H2A_INT_DUMMY,
1137                 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1138         ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1139 }
1140
1141 static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
1142 {
1143         struct mwl8k_priv *priv = hw->priv;
1144         int i;
1145
1146         for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1147                 struct mwl8k_tx_queue *txq = priv->txq + i;
1148                 int fw_owned = 0;
1149                 int drv_owned = 0;
1150                 int unused = 0;
1151                 int desc;
1152
1153                 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
1154                         struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1155                         u32 status;
1156
1157                         status = le32_to_cpu(tx_desc->status);
1158                         if (status & MWL8K_TXD_STATUS_FW_OWNED)
1159                                 fw_owned++;
1160                         else
1161                                 drv_owned++;
1162
1163                         if (tx_desc->pkt_len == 0)
1164                                 unused++;
1165                 }
1166
1167                 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1168                        "fw_owned=%d drv_owned=%d unused=%d\n",
1169                        wiphy_name(hw->wiphy), i,
1170                        txq->stats.len, txq->head, txq->tail,
1171                        fw_owned, drv_owned, unused);
1172         }
1173 }
1174
1175 /*
1176  * Must be called with priv->fw_mutex held and tx queues stopped.
1177  */
1178 #define MWL8K_TX_WAIT_TIMEOUT_MS        5000
1179
1180 static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
1181 {
1182         struct mwl8k_priv *priv = hw->priv;
1183         DECLARE_COMPLETION_ONSTACK(tx_wait);
1184         int retry;
1185         int rc;
1186
1187         might_sleep();
1188
1189         /*
1190          * The TX queues are stopped at this point, so this test
1191          * doesn't need to take ->tx_lock.
1192          */
1193         if (!priv->pending_tx_pkts)
1194                 return 0;
1195
1196         retry = 0;
1197         rc = 0;
1198
1199         spin_lock_bh(&priv->tx_lock);
1200         priv->tx_wait = &tx_wait;
1201         while (!rc) {
1202                 int oldcount;
1203                 unsigned long timeout;
1204
1205                 oldcount = priv->pending_tx_pkts;
1206
1207                 spin_unlock_bh(&priv->tx_lock);
1208                 timeout = wait_for_completion_timeout(&tx_wait,
1209                             msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1210                 spin_lock_bh(&priv->tx_lock);
1211
1212                 if (timeout) {
1213                         WARN_ON(priv->pending_tx_pkts);
1214                         if (retry) {
1215                                 printk(KERN_NOTICE "%s: tx rings drained\n",
1216                                        wiphy_name(hw->wiphy));
1217                         }
1218                         break;
1219                 }
1220
1221                 if (priv->pending_tx_pkts < oldcount) {
1222                         printk(KERN_NOTICE "%s: waiting for tx rings "
1223                                "to drain (%d -> %d pkts)\n",
1224                                wiphy_name(hw->wiphy), oldcount,
1225                                priv->pending_tx_pkts);
1226                         retry = 1;
1227                         continue;
1228                 }
1229
1230                 priv->tx_wait = NULL;
1231
1232                 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1233                        wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1234                 mwl8k_dump_tx_rings(hw);
1235
1236                 rc = -ETIMEDOUT;
1237         }
1238         spin_unlock_bh(&priv->tx_lock);
1239
1240         return rc;
1241 }
1242
1243 #define MWL8K_TXD_SUCCESS(status)                               \
1244         ((status) & (MWL8K_TXD_STATUS_OK |                      \
1245                      MWL8K_TXD_STATUS_OK_RETRY |                \
1246                      MWL8K_TXD_STATUS_OK_MORE_RETRY))
1247
1248 static void mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int force)
1249 {
1250         struct mwl8k_priv *priv = hw->priv;
1251         struct mwl8k_tx_queue *txq = priv->txq + index;
1252         int wake = 0;
1253
1254         while (txq->stats.len > 0) {
1255                 int tx;
1256                 struct mwl8k_tx_desc *tx_desc;
1257                 unsigned long addr;
1258                 int size;
1259                 struct sk_buff *skb;
1260                 struct ieee80211_tx_info *info;
1261                 u32 status;
1262
1263                 tx = txq->head;
1264                 tx_desc = txq->txd + tx;
1265
1266                 status = le32_to_cpu(tx_desc->status);
1267
1268                 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1269                         if (!force)
1270                                 break;
1271                         tx_desc->status &=
1272                                 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1273                 }
1274
1275                 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1276                 BUG_ON(txq->stats.len == 0);
1277                 txq->stats.len--;
1278                 priv->pending_tx_pkts--;
1279
1280                 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
1281                 size = le16_to_cpu(tx_desc->pkt_len);
1282                 skb = txq->skb[tx];
1283                 txq->skb[tx] = NULL;
1284
1285                 BUG_ON(skb == NULL);
1286                 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1287
1288                 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
1289
1290                 /* Mark descriptor as unused */
1291                 tx_desc->pkt_phys_addr = 0;
1292                 tx_desc->pkt_len = 0;
1293
1294                 info = IEEE80211_SKB_CB(skb);
1295                 ieee80211_tx_info_clear_status(info);
1296                 if (MWL8K_TXD_SUCCESS(status))
1297                         info->flags |= IEEE80211_TX_STAT_ACK;
1298
1299                 ieee80211_tx_status_irqsafe(hw, skb);
1300
1301                 wake = 1;
1302         }
1303
1304         if (wake && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
1305                 ieee80211_wake_queue(hw, index);
1306 }
1307
1308 /* must be called only when the card's transmit is completely halted */
1309 static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1310 {
1311         struct mwl8k_priv *priv = hw->priv;
1312         struct mwl8k_tx_queue *txq = priv->txq + index;
1313
1314         mwl8k_txq_reclaim(hw, index, 1);
1315
1316         kfree(txq->skb);
1317         txq->skb = NULL;
1318
1319         pci_free_consistent(priv->pdev,
1320                             MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
1321                             txq->txd, txq->txd_dma);
1322         txq->txd = NULL;
1323 }
1324
1325 static int
1326 mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1327 {
1328         struct mwl8k_priv *priv = hw->priv;
1329         struct ieee80211_tx_info *tx_info;
1330         struct mwl8k_vif *mwl8k_vif;
1331         struct ieee80211_hdr *wh;
1332         struct mwl8k_tx_queue *txq;
1333         struct mwl8k_tx_desc *tx;
1334         dma_addr_t dma;
1335         u32 txstatus;
1336         u8 txdatarate;
1337         u16 qos;
1338
1339         wh = (struct ieee80211_hdr *)skb->data;
1340         if (ieee80211_is_data_qos(wh->frame_control))
1341                 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1342         else
1343                 qos = 0;
1344
1345         mwl8k_add_dma_header(skb);
1346         wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1347
1348         tx_info = IEEE80211_SKB_CB(skb);
1349         mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
1350
1351         if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1352                 u16 seqno = mwl8k_vif->seqno;
1353
1354                 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1355                 wh->seq_ctrl |= cpu_to_le16(seqno << 4);
1356                 mwl8k_vif->seqno = seqno++ % 4096;
1357         }
1358
1359         /* Setup firmware control bit fields for each frame type.  */
1360         txstatus = 0;
1361         txdatarate = 0;
1362         if (ieee80211_is_mgmt(wh->frame_control) ||
1363             ieee80211_is_ctl(wh->frame_control)) {
1364                 txdatarate = 0;
1365                 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
1366         } else if (ieee80211_is_data(wh->frame_control)) {
1367                 txdatarate = 1;
1368                 if (is_multicast_ether_addr(wh->addr1))
1369                         txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1370
1371                 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
1372                 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1373                         qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
1374                 else
1375                         qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
1376         }
1377
1378         dma = pci_map_single(priv->pdev, skb->data,
1379                                 skb->len, PCI_DMA_TODEVICE);
1380
1381         if (pci_dma_mapping_error(priv->pdev, dma)) {
1382                 printk(KERN_DEBUG "%s: failed to dma map skb, "
1383                        "dropping TX frame.\n", wiphy_name(hw->wiphy));
1384                 dev_kfree_skb(skb);
1385                 return NETDEV_TX_OK;
1386         }
1387
1388         spin_lock_bh(&priv->tx_lock);
1389
1390         txq = priv->txq + index;
1391
1392         BUG_ON(txq->skb[txq->tail] != NULL);
1393         txq->skb[txq->tail] = skb;
1394
1395         tx = txq->txd + txq->tail;
1396         tx->data_rate = txdatarate;
1397         tx->tx_priority = index;
1398         tx->qos_control = cpu_to_le16(qos);
1399         tx->pkt_phys_addr = cpu_to_le32(dma);
1400         tx->pkt_len = cpu_to_le16(skb->len);
1401         tx->rate_info = 0;
1402         if (!priv->ap_fw && tx_info->control.sta != NULL)
1403                 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1404         else
1405                 tx->peer_id = 0;
1406         wmb();
1407         tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1408
1409         txq->stats.count++;
1410         txq->stats.len++;
1411         priv->pending_tx_pkts++;
1412
1413         txq->tail++;
1414         if (txq->tail == MWL8K_TX_DESCS)
1415                 txq->tail = 0;
1416
1417         if (txq->head == txq->tail)
1418                 ieee80211_stop_queue(hw, index);
1419
1420         mwl8k_tx_start(priv);
1421
1422         spin_unlock_bh(&priv->tx_lock);
1423
1424         return NETDEV_TX_OK;
1425 }
1426
1427
1428 /*
1429  * Firmware access.
1430  *
1431  * We have the following requirements for issuing firmware commands:
1432  * - Some commands require that the packet transmit path is idle when
1433  *   the command is issued.  (For simplicity, we'll just quiesce the
1434  *   transmit path for every command.)
1435  * - There are certain sequences of commands that need to be issued to
1436  *   the hardware sequentially, with no other intervening commands.
1437  *
1438  * This leads to an implementation of a "firmware lock" as a mutex that
1439  * can be taken recursively, and which is taken by both the low-level
1440  * command submission function (mwl8k_post_cmd) as well as any users of
1441  * that function that require issuing of an atomic sequence of commands,
1442  * and quiesces the transmit path whenever it's taken.
1443  */
1444 static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1445 {
1446         struct mwl8k_priv *priv = hw->priv;
1447
1448         if (priv->fw_mutex_owner != current) {
1449                 int rc;
1450
1451                 mutex_lock(&priv->fw_mutex);
1452                 ieee80211_stop_queues(hw);
1453
1454                 rc = mwl8k_tx_wait_empty(hw);
1455                 if (rc) {
1456                         ieee80211_wake_queues(hw);
1457                         mutex_unlock(&priv->fw_mutex);
1458
1459                         return rc;
1460                 }
1461
1462                 priv->fw_mutex_owner = current;
1463         }
1464
1465         priv->fw_mutex_depth++;
1466
1467         return 0;
1468 }
1469
1470 static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1471 {
1472         struct mwl8k_priv *priv = hw->priv;
1473
1474         if (!--priv->fw_mutex_depth) {
1475                 ieee80211_wake_queues(hw);
1476                 priv->fw_mutex_owner = NULL;
1477                 mutex_unlock(&priv->fw_mutex);
1478         }
1479 }
1480
1481
1482 /*
1483  * Command processing.
1484  */
1485
1486 /* Timeout firmware commands after 10s */
1487 #define MWL8K_CMD_TIMEOUT_MS    10000
1488
1489 static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1490 {
1491         DECLARE_COMPLETION_ONSTACK(cmd_wait);
1492         struct mwl8k_priv *priv = hw->priv;
1493         void __iomem *regs = priv->regs;
1494         dma_addr_t dma_addr;
1495         unsigned int dma_size;
1496         int rc;
1497         unsigned long timeout = 0;
1498         u8 buf[32];
1499
1500         cmd->result = 0xffff;
1501         dma_size = le16_to_cpu(cmd->length);
1502         dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1503                                   PCI_DMA_BIDIRECTIONAL);
1504         if (pci_dma_mapping_error(priv->pdev, dma_addr))
1505                 return -ENOMEM;
1506
1507         rc = mwl8k_fw_lock(hw);
1508         if (rc) {
1509                 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1510                                                 PCI_DMA_BIDIRECTIONAL);
1511                 return rc;
1512         }
1513
1514         priv->hostcmd_wait = &cmd_wait;
1515         iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1516         iowrite32(MWL8K_H2A_INT_DOORBELL,
1517                 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1518         iowrite32(MWL8K_H2A_INT_DUMMY,
1519                 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1520
1521         timeout = wait_for_completion_timeout(&cmd_wait,
1522                                 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1523
1524         priv->hostcmd_wait = NULL;
1525
1526         mwl8k_fw_unlock(hw);
1527
1528         pci_unmap_single(priv->pdev, dma_addr, dma_size,
1529                                         PCI_DMA_BIDIRECTIONAL);
1530
1531         if (!timeout) {
1532                 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
1533                        wiphy_name(hw->wiphy),
1534                        mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1535                        MWL8K_CMD_TIMEOUT_MS);
1536                 rc = -ETIMEDOUT;
1537         } else {
1538                 int ms;
1539
1540                 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1541
1542                 rc = cmd->result ? -EINVAL : 0;
1543                 if (rc)
1544                         printk(KERN_ERR "%s: Command %s error 0x%x\n",
1545                                wiphy_name(hw->wiphy),
1546                                mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1547                                le16_to_cpu(cmd->result));
1548                 else if (ms > 2000)
1549                         printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1550                                wiphy_name(hw->wiphy),
1551                                mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1552                                ms);
1553         }
1554
1555         return rc;
1556 }
1557
1558 /*
1559  * CMD_GET_HW_SPEC (STA version).
1560  */
1561 struct mwl8k_cmd_get_hw_spec_sta {
1562         struct mwl8k_cmd_pkt header;
1563         __u8 hw_rev;
1564         __u8 host_interface;
1565         __le16 num_mcaddrs;
1566         __u8 perm_addr[ETH_ALEN];
1567         __le16 region_code;
1568         __le32 fw_rev;
1569         __le32 ps_cookie;
1570         __le32 caps;
1571         __u8 mcs_bitmap[16];
1572         __le32 rx_queue_ptr;
1573         __le32 num_tx_queues;
1574         __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1575         __le32 caps2;
1576         __le32 num_tx_desc_per_queue;
1577         __le32 total_rxd;
1578 } __attribute__((packed));
1579
1580 #define MWL8K_CAP_MAX_AMSDU             0x20000000
1581 #define MWL8K_CAP_GREENFIELD            0x08000000
1582 #define MWL8K_CAP_AMPDU                 0x04000000
1583 #define MWL8K_CAP_RX_STBC               0x01000000
1584 #define MWL8K_CAP_TX_STBC               0x00800000
1585 #define MWL8K_CAP_SHORTGI_40MHZ         0x00400000
1586 #define MWL8K_CAP_SHORTGI_20MHZ         0x00200000
1587 #define MWL8K_CAP_RX_ANTENNA_MASK       0x000e0000
1588 #define MWL8K_CAP_TX_ANTENNA_MASK       0x0001c000
1589 #define MWL8K_CAP_DELAY_BA              0x00003000
1590 #define MWL8K_CAP_MIMO                  0x00000200
1591 #define MWL8K_CAP_40MHZ                 0x00000100
1592
1593 static void mwl8k_set_ht_caps(struct ieee80211_hw *hw, u32 cap)
1594 {
1595         struct mwl8k_priv *priv = hw->priv;
1596         int rx_streams;
1597         int tx_streams;
1598
1599         priv->band.ht_cap.ht_supported = 1;
1600
1601         if (cap & MWL8K_CAP_MAX_AMSDU)
1602                 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
1603         if (cap & MWL8K_CAP_GREENFIELD)
1604                 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
1605         if (cap & MWL8K_CAP_AMPDU) {
1606                 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
1607                 priv->band.ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1608                 priv->band.ht_cap.ampdu_density =
1609                                 IEEE80211_HT_MPDU_DENSITY_NONE;
1610         }
1611         if (cap & MWL8K_CAP_RX_STBC)
1612                 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
1613         if (cap & MWL8K_CAP_TX_STBC)
1614                 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
1615         if (cap & MWL8K_CAP_SHORTGI_40MHZ)
1616                 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
1617         if (cap & MWL8K_CAP_SHORTGI_20MHZ)
1618                 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
1619         if (cap & MWL8K_CAP_DELAY_BA)
1620                 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
1621         if (cap & MWL8K_CAP_40MHZ)
1622                 priv->band.ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1623
1624         rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1625         tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1626
1627         priv->band.ht_cap.mcs.rx_mask[0] = 0xff;
1628         if (rx_streams >= 2)
1629                 priv->band.ht_cap.mcs.rx_mask[1] = 0xff;
1630         if (rx_streams >= 3)
1631                 priv->band.ht_cap.mcs.rx_mask[2] = 0xff;
1632         priv->band.ht_cap.mcs.rx_mask[4] = 0x01;
1633         priv->band.ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
1634
1635         if (rx_streams != tx_streams) {
1636                 priv->band.ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1637                 priv->band.ht_cap.mcs.tx_params |= (tx_streams - 1) <<
1638                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1639         }
1640 }
1641
1642 static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
1643 {
1644         struct mwl8k_priv *priv = hw->priv;
1645         struct mwl8k_cmd_get_hw_spec_sta *cmd;
1646         int rc;
1647         int i;
1648
1649         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1650         if (cmd == NULL)
1651                 return -ENOMEM;
1652
1653         cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1654         cmd->header.length = cpu_to_le16(sizeof(*cmd));
1655
1656         memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1657         cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1658         cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1659         cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1660         for (i = 0; i < MWL8K_TX_QUEUES; i++)
1661                 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1662         cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1663         cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1664
1665         rc = mwl8k_post_cmd(hw, &cmd->header);
1666
1667         if (!rc) {
1668                 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1669                 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1670                 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1671                 priv->hw_rev = cmd->hw_rev;
1672                 if (cmd->caps & cpu_to_le32(MWL8K_CAP_MIMO))
1673                         mwl8k_set_ht_caps(hw, le32_to_cpu(cmd->caps));
1674         }
1675
1676         kfree(cmd);
1677         return rc;
1678 }
1679
1680 /*
1681  * CMD_GET_HW_SPEC (AP version).
1682  */
1683 struct mwl8k_cmd_get_hw_spec_ap {
1684         struct mwl8k_cmd_pkt header;
1685         __u8 hw_rev;
1686         __u8 host_interface;
1687         __le16 num_wcb;
1688         __le16 num_mcaddrs;
1689         __u8 perm_addr[ETH_ALEN];
1690         __le16 region_code;
1691         __le16 num_antenna;
1692         __le32 fw_rev;
1693         __le32 wcbbase0;
1694         __le32 rxwrptr;
1695         __le32 rxrdptr;
1696         __le32 ps_cookie;
1697         __le32 wcbbase1;
1698         __le32 wcbbase2;
1699         __le32 wcbbase3;
1700 } __attribute__((packed));
1701
1702 static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1703 {
1704         struct mwl8k_priv *priv = hw->priv;
1705         struct mwl8k_cmd_get_hw_spec_ap *cmd;
1706         int rc;
1707
1708         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1709         if (cmd == NULL)
1710                 return -ENOMEM;
1711
1712         cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1713         cmd->header.length = cpu_to_le16(sizeof(*cmd));
1714
1715         memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1716         cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1717
1718         rc = mwl8k_post_cmd(hw, &cmd->header);
1719
1720         if (!rc) {
1721                 int off;
1722
1723                 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1724                 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1725                 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1726                 priv->hw_rev = cmd->hw_rev;
1727
1728                 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1729                 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1730
1731                 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1732                 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1733
1734                 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1735                 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1736
1737                 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1738                 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1739
1740                 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1741                 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1742
1743                 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1744                 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1745         }
1746
1747         kfree(cmd);
1748         return rc;
1749 }
1750
1751 /*
1752  * CMD_SET_HW_SPEC.
1753  */
1754 struct mwl8k_cmd_set_hw_spec {
1755         struct mwl8k_cmd_pkt header;
1756         __u8 hw_rev;
1757         __u8 host_interface;
1758         __le16 num_mcaddrs;
1759         __u8 perm_addr[ETH_ALEN];
1760         __le16 region_code;
1761         __le32 fw_rev;
1762         __le32 ps_cookie;
1763         __le32 caps;
1764         __le32 rx_queue_ptr;
1765         __le32 num_tx_queues;
1766         __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1767         __le32 flags;
1768         __le32 num_tx_desc_per_queue;
1769         __le32 total_rxd;
1770 } __attribute__((packed));
1771
1772 #define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT   0x00000080
1773
1774 static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1775 {
1776         struct mwl8k_priv *priv = hw->priv;
1777         struct mwl8k_cmd_set_hw_spec *cmd;
1778         int rc;
1779         int i;
1780
1781         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1782         if (cmd == NULL)
1783                 return -ENOMEM;
1784
1785         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1786         cmd->header.length = cpu_to_le16(sizeof(*cmd));
1787
1788         cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1789         cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1790         cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1791         for (i = 0; i < MWL8K_TX_QUEUES; i++)
1792                 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
1793         cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT);
1794         cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1795         cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1796
1797         rc = mwl8k_post_cmd(hw, &cmd->header);
1798         kfree(cmd);
1799
1800         return rc;
1801 }
1802
1803 /*
1804  * CMD_MAC_MULTICAST_ADR.
1805  */
1806 struct mwl8k_cmd_mac_multicast_adr {
1807         struct mwl8k_cmd_pkt header;
1808         __le16 action;
1809         __le16 numaddr;
1810         __u8 addr[0][ETH_ALEN];
1811 };
1812
1813 #define MWL8K_ENABLE_RX_DIRECTED        0x0001
1814 #define MWL8K_ENABLE_RX_MULTICAST       0x0002
1815 #define MWL8K_ENABLE_RX_ALL_MULTICAST   0x0004
1816 #define MWL8K_ENABLE_RX_BROADCAST       0x0008
1817
1818 static struct mwl8k_cmd_pkt *
1819 __mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
1820                               int mc_count, struct dev_addr_list *mclist)
1821 {
1822         struct mwl8k_priv *priv = hw->priv;
1823         struct mwl8k_cmd_mac_multicast_adr *cmd;
1824         int size;
1825
1826         if (allmulti || mc_count > priv->num_mcaddrs) {
1827                 allmulti = 1;
1828                 mc_count = 0;
1829         }
1830
1831         size = sizeof(*cmd) + mc_count * ETH_ALEN;
1832
1833         cmd = kzalloc(size, GFP_ATOMIC);
1834         if (cmd == NULL)
1835                 return NULL;
1836
1837         cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1838         cmd->header.length = cpu_to_le16(size);
1839         cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1840                                   MWL8K_ENABLE_RX_BROADCAST);
1841
1842         if (allmulti) {
1843                 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1844         } else if (mc_count) {
1845                 int i;
1846
1847                 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1848                 cmd->numaddr = cpu_to_le16(mc_count);
1849                 for (i = 0; i < mc_count && mclist; i++) {
1850                         if (mclist->da_addrlen != ETH_ALEN) {
1851                                 kfree(cmd);
1852                                 return NULL;
1853                         }
1854                         memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1855                         mclist = mclist->next;
1856                 }
1857         }
1858
1859         return &cmd->header;
1860 }
1861
1862 /*
1863  * CMD_GET_STAT.
1864  */
1865 struct mwl8k_cmd_get_stat {
1866         struct mwl8k_cmd_pkt header;
1867         __le32 stats[64];
1868 } __attribute__((packed));
1869
1870 #define MWL8K_STAT_ACK_FAILURE  9
1871 #define MWL8K_STAT_RTS_FAILURE  12
1872 #define MWL8K_STAT_FCS_ERROR    24
1873 #define MWL8K_STAT_RTS_SUCCESS  11
1874
1875 static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1876                               struct ieee80211_low_level_stats *stats)
1877 {
1878         struct mwl8k_cmd_get_stat *cmd;
1879         int rc;
1880
1881         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1882         if (cmd == NULL)
1883                 return -ENOMEM;
1884
1885         cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
1886         cmd->header.length = cpu_to_le16(sizeof(*cmd));
1887
1888         rc = mwl8k_post_cmd(hw, &cmd->header);
1889         if (!rc) {
1890                 stats->dot11ACKFailureCount =
1891                         le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
1892                 stats->dot11RTSFailureCount =
1893                         le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
1894                 stats->dot11FCSErrorCount =
1895                         le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
1896                 stats->dot11RTSSuccessCount =
1897                         le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
1898         }
1899         kfree(cmd);
1900
1901         return rc;
1902 }
1903
1904 /*
1905  * CMD_RADIO_CONTROL.
1906  */
1907 struct mwl8k_cmd_radio_control {
1908         struct mwl8k_cmd_pkt header;
1909         __le16 action;
1910         __le16 control;
1911         __le16 radio_on;
1912 } __attribute__((packed));
1913
1914 static int
1915 mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
1916 {
1917         struct mwl8k_priv *priv = hw->priv;
1918         struct mwl8k_cmd_radio_control *cmd;
1919         int rc;
1920
1921         if (enable == priv->radio_on && !force)
1922                 return 0;
1923
1924         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1925         if (cmd == NULL)
1926                 return -ENOMEM;
1927
1928         cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
1929         cmd->header.length = cpu_to_le16(sizeof(*cmd));
1930         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1931         cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
1932         cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
1933
1934         rc = mwl8k_post_cmd(hw, &cmd->header);
1935         kfree(cmd);
1936
1937         if (!rc)
1938                 priv->radio_on = enable;
1939
1940         return rc;
1941 }
1942
1943 static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
1944 {
1945         return mwl8k_cmd_radio_control(hw, 0, 0);
1946 }
1947
1948 static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
1949 {
1950         return mwl8k_cmd_radio_control(hw, 1, 0);
1951 }
1952
1953 static int
1954 mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
1955 {
1956         struct mwl8k_priv *priv = hw->priv;
1957
1958         priv->radio_short_preamble = short_preamble;
1959
1960         return mwl8k_cmd_radio_control(hw, 1, 1);
1961 }
1962
1963 /*
1964  * CMD_RF_TX_POWER.
1965  */
1966 #define MWL8K_TX_POWER_LEVEL_TOTAL      8
1967
1968 struct mwl8k_cmd_rf_tx_power {
1969         struct mwl8k_cmd_pkt header;
1970         __le16 action;
1971         __le16 support_level;
1972         __le16 current_level;
1973         __le16 reserved;
1974         __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
1975 } __attribute__((packed));
1976
1977 static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
1978 {
1979         struct mwl8k_cmd_rf_tx_power *cmd;
1980         int rc;
1981
1982         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1983         if (cmd == NULL)
1984                 return -ENOMEM;
1985
1986         cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
1987         cmd->header.length = cpu_to_le16(sizeof(*cmd));
1988         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
1989         cmd->support_level = cpu_to_le16(dBm);
1990
1991         rc = mwl8k_post_cmd(hw, &cmd->header);
1992         kfree(cmd);
1993
1994         return rc;
1995 }
1996
1997 /*
1998  * CMD_RF_ANTENNA.
1999  */
2000 struct mwl8k_cmd_rf_antenna {
2001         struct mwl8k_cmd_pkt header;
2002         __le16 antenna;
2003         __le16 mode;
2004 } __attribute__((packed));
2005
2006 #define MWL8K_RF_ANTENNA_RX             1
2007 #define MWL8K_RF_ANTENNA_TX             2
2008
2009 static int
2010 mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2011 {
2012         struct mwl8k_cmd_rf_antenna *cmd;
2013         int rc;
2014
2015         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2016         if (cmd == NULL)
2017                 return -ENOMEM;
2018
2019         cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2020         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2021         cmd->antenna = cpu_to_le16(antenna);
2022         cmd->mode = cpu_to_le16(mask);
2023
2024         rc = mwl8k_post_cmd(hw, &cmd->header);
2025         kfree(cmd);
2026
2027         return rc;
2028 }
2029
2030 /*
2031  * CMD_SET_PRE_SCAN.
2032  */
2033 struct mwl8k_cmd_set_pre_scan {
2034         struct mwl8k_cmd_pkt header;
2035 } __attribute__((packed));
2036
2037 static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2038 {
2039         struct mwl8k_cmd_set_pre_scan *cmd;
2040         int rc;
2041
2042         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2043         if (cmd == NULL)
2044                 return -ENOMEM;
2045
2046         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2047         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2048
2049         rc = mwl8k_post_cmd(hw, &cmd->header);
2050         kfree(cmd);
2051
2052         return rc;
2053 }
2054
2055 /*
2056  * CMD_SET_POST_SCAN.
2057  */
2058 struct mwl8k_cmd_set_post_scan {
2059         struct mwl8k_cmd_pkt header;
2060         __le32 isibss;
2061         __u8 bssid[ETH_ALEN];
2062 } __attribute__((packed));
2063
2064 static int
2065 mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
2066 {
2067         struct mwl8k_cmd_set_post_scan *cmd;
2068         int rc;
2069
2070         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2071         if (cmd == NULL)
2072                 return -ENOMEM;
2073
2074         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2075         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2076         cmd->isibss = 0;
2077         memcpy(cmd->bssid, mac, ETH_ALEN);
2078
2079         rc = mwl8k_post_cmd(hw, &cmd->header);
2080         kfree(cmd);
2081
2082         return rc;
2083 }
2084
2085 /*
2086  * CMD_SET_RF_CHANNEL.
2087  */
2088 struct mwl8k_cmd_set_rf_channel {
2089         struct mwl8k_cmd_pkt header;
2090         __le16 action;
2091         __u8 current_channel;
2092         __le32 channel_flags;
2093 } __attribute__((packed));
2094
2095 static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
2096                                     struct ieee80211_conf *conf)
2097 {
2098         struct ieee80211_channel *channel = conf->channel;
2099         struct mwl8k_cmd_set_rf_channel *cmd;
2100         int rc;
2101
2102         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2103         if (cmd == NULL)
2104                 return -ENOMEM;
2105
2106         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2107         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2108         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2109         cmd->current_channel = channel->hw_value;
2110
2111         if (channel->band == IEEE80211_BAND_2GHZ)
2112                 cmd->channel_flags |= cpu_to_le32(0x00000001);
2113
2114         if (conf->channel_type == NL80211_CHAN_NO_HT ||
2115             conf->channel_type == NL80211_CHAN_HT20)
2116                 cmd->channel_flags |= cpu_to_le32(0x00000080);
2117         else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2118                 cmd->channel_flags |= cpu_to_le32(0x000001900);
2119         else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2120                 cmd->channel_flags |= cpu_to_le32(0x000000900);
2121
2122         rc = mwl8k_post_cmd(hw, &cmd->header);
2123         kfree(cmd);
2124
2125         return rc;
2126 }
2127
2128 /*
2129  * CMD_SET_AID.
2130  */
2131 #define MWL8K_FRAME_PROT_DISABLED                       0x00
2132 #define MWL8K_FRAME_PROT_11G                            0x07
2133 #define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY              0x02
2134 #define MWL8K_FRAME_PROT_11N_HT_ALL                     0x06
2135
2136 struct mwl8k_cmd_update_set_aid {
2137         struct  mwl8k_cmd_pkt header;
2138         __le16  aid;
2139
2140          /* AP's MAC address (BSSID) */
2141         __u8    bssid[ETH_ALEN];
2142         __le16  protection_mode;
2143         __u8    supp_rates[14];
2144 } __attribute__((packed));
2145
2146 static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2147 {
2148         int i;
2149         int j;
2150
2151         /*
2152          * Clear nonstandard rates 4 and 13.
2153          */
2154         mask &= 0x1fef;
2155
2156         for (i = 0, j = 0; i < 14; i++) {
2157                 if (mask & (1 << i))
2158                         rates[j++] = mwl8k_rates[i].hw_value;
2159         }
2160 }
2161
2162 static int
2163 mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2164                   struct ieee80211_vif *vif, u32 legacy_rate_mask)
2165 {
2166         struct mwl8k_cmd_update_set_aid *cmd;
2167         u16 prot_mode;
2168         int rc;
2169
2170         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2171         if (cmd == NULL)
2172                 return -ENOMEM;
2173
2174         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2175         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2176         cmd->aid = cpu_to_le16(vif->bss_conf.aid);
2177         memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
2178
2179         if (vif->bss_conf.use_cts_prot) {
2180                 prot_mode = MWL8K_FRAME_PROT_11G;
2181         } else {
2182                 switch (vif->bss_conf.ht_operation_mode &
2183                         IEEE80211_HT_OP_MODE_PROTECTION) {
2184                 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2185                         prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2186                         break;
2187                 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2188                         prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2189                         break;
2190                 default:
2191                         prot_mode = MWL8K_FRAME_PROT_DISABLED;
2192                         break;
2193                 }
2194         }
2195         cmd->protection_mode = cpu_to_le16(prot_mode);
2196
2197         legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
2198
2199         rc = mwl8k_post_cmd(hw, &cmd->header);
2200         kfree(cmd);
2201
2202         return rc;
2203 }
2204
2205 /*
2206  * CMD_SET_RATE.
2207  */
2208 struct mwl8k_cmd_set_rate {
2209         struct  mwl8k_cmd_pkt header;
2210         __u8    legacy_rates[14];
2211
2212         /* Bitmap for supported MCS codes.  */
2213         __u8    mcs_set[16];
2214         __u8    reserved[16];
2215 } __attribute__((packed));
2216
2217 static int
2218 mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2219                    u32 legacy_rate_mask, u8 *mcs_rates)
2220 {
2221         struct mwl8k_cmd_set_rate *cmd;
2222         int rc;
2223
2224         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2225         if (cmd == NULL)
2226                 return -ENOMEM;
2227
2228         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2229         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2230         legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
2231         memcpy(cmd->mcs_set, mcs_rates, 16);
2232
2233         rc = mwl8k_post_cmd(hw, &cmd->header);
2234         kfree(cmd);
2235
2236         return rc;
2237 }
2238
2239 /*
2240  * CMD_FINALIZE_JOIN.
2241  */
2242 #define MWL8K_FJ_BEACON_MAXLEN  128
2243
2244 struct mwl8k_cmd_finalize_join {
2245         struct mwl8k_cmd_pkt header;
2246         __le32 sleep_interval;  /* Number of beacon periods to sleep */
2247         __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2248 } __attribute__((packed));
2249
2250 static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2251                                    int framelen, int dtim)
2252 {
2253         struct mwl8k_cmd_finalize_join *cmd;
2254         struct ieee80211_mgmt *payload = frame;
2255         int payload_len;
2256         int rc;
2257
2258         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2259         if (cmd == NULL)
2260                 return -ENOMEM;
2261
2262         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2263         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2264         cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2265
2266         payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2267         if (payload_len < 0)
2268                 payload_len = 0;
2269         else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2270                 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2271
2272         memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2273
2274         rc = mwl8k_post_cmd(hw, &cmd->header);
2275         kfree(cmd);
2276
2277         return rc;
2278 }
2279
2280 /*
2281  * CMD_SET_RTS_THRESHOLD.
2282  */
2283 struct mwl8k_cmd_set_rts_threshold {
2284         struct mwl8k_cmd_pkt header;
2285         __le16 action;
2286         __le16 threshold;
2287 } __attribute__((packed));
2288
2289 static int
2290 mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
2291 {
2292         struct mwl8k_cmd_set_rts_threshold *cmd;
2293         int rc;
2294
2295         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2296         if (cmd == NULL)
2297                 return -ENOMEM;
2298
2299         cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2300         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2301         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2302         cmd->threshold = cpu_to_le16(rts_thresh);
2303
2304         rc = mwl8k_post_cmd(hw, &cmd->header);
2305         kfree(cmd);
2306
2307         return rc;
2308 }
2309
2310 /*
2311  * CMD_SET_SLOT.
2312  */
2313 struct mwl8k_cmd_set_slot {
2314         struct mwl8k_cmd_pkt header;
2315         __le16 action;
2316         __u8 short_slot;
2317 } __attribute__((packed));
2318
2319 static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
2320 {
2321         struct mwl8k_cmd_set_slot *cmd;
2322         int rc;
2323
2324         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2325         if (cmd == NULL)
2326                 return -ENOMEM;
2327
2328         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2329         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2330         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2331         cmd->short_slot = short_slot_time;
2332
2333         rc = mwl8k_post_cmd(hw, &cmd->header);
2334         kfree(cmd);
2335
2336         return rc;
2337 }
2338
2339 /*
2340  * CMD_SET_EDCA_PARAMS.
2341  */
2342 struct mwl8k_cmd_set_edca_params {
2343         struct mwl8k_cmd_pkt header;
2344
2345         /* See MWL8K_SET_EDCA_XXX below */
2346         __le16 action;
2347
2348         /* TX opportunity in units of 32 us */
2349         __le16 txop;
2350
2351         union {
2352                 struct {
2353                         /* Log exponent of max contention period: 0...15 */
2354                         __le32 log_cw_max;
2355
2356                         /* Log exponent of min contention period: 0...15 */
2357                         __le32 log_cw_min;
2358
2359                         /* Adaptive interframe spacing in units of 32us */
2360                         __u8 aifs;
2361
2362                         /* TX queue to configure */
2363                         __u8 txq;
2364                 } ap;
2365                 struct {
2366                         /* Log exponent of max contention period: 0...15 */
2367                         __u8 log_cw_max;
2368
2369                         /* Log exponent of min contention period: 0...15 */
2370                         __u8 log_cw_min;
2371
2372                         /* Adaptive interframe spacing in units of 32us */
2373                         __u8 aifs;
2374
2375                         /* TX queue to configure */
2376                         __u8 txq;
2377                 } sta;
2378         };
2379 } __attribute__((packed));
2380
2381 #define MWL8K_SET_EDCA_CW       0x01
2382 #define MWL8K_SET_EDCA_TXOP     0x02
2383 #define MWL8K_SET_EDCA_AIFS     0x04
2384
2385 #define MWL8K_SET_EDCA_ALL      (MWL8K_SET_EDCA_CW | \
2386                                  MWL8K_SET_EDCA_TXOP | \
2387                                  MWL8K_SET_EDCA_AIFS)
2388
2389 static int
2390 mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2391                           __u16 cw_min, __u16 cw_max,
2392                           __u8 aifs, __u16 txop)
2393 {
2394         struct mwl8k_priv *priv = hw->priv;
2395         struct mwl8k_cmd_set_edca_params *cmd;
2396         int rc;
2397
2398         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2399         if (cmd == NULL)
2400                 return -ENOMEM;
2401
2402         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2403         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2404         cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2405         cmd->txop = cpu_to_le16(txop);
2406         if (priv->ap_fw) {
2407                 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2408                 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2409                 cmd->ap.aifs = aifs;
2410                 cmd->ap.txq = qnum;
2411         } else {
2412                 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2413                 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2414                 cmd->sta.aifs = aifs;
2415                 cmd->sta.txq = qnum;
2416         }
2417
2418         rc = mwl8k_post_cmd(hw, &cmd->header);
2419         kfree(cmd);
2420
2421         return rc;
2422 }
2423
2424 /*
2425  * CMD_SET_WMM_MODE.
2426  */
2427 struct mwl8k_cmd_set_wmm_mode {
2428         struct mwl8k_cmd_pkt header;
2429         __le16 action;
2430 } __attribute__((packed));
2431
2432 static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
2433 {
2434         struct mwl8k_priv *priv = hw->priv;
2435         struct mwl8k_cmd_set_wmm_mode *cmd;
2436         int rc;
2437
2438         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2439         if (cmd == NULL)
2440                 return -ENOMEM;
2441
2442         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
2443         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2444         cmd->action = cpu_to_le16(!!enable);
2445
2446         rc = mwl8k_post_cmd(hw, &cmd->header);
2447         kfree(cmd);
2448
2449         if (!rc)
2450                 priv->wmm_enabled = enable;
2451
2452         return rc;
2453 }
2454
2455 /*
2456  * CMD_MIMO_CONFIG.
2457  */
2458 struct mwl8k_cmd_mimo_config {
2459         struct mwl8k_cmd_pkt header;
2460         __le32 action;
2461         __u8 rx_antenna_map;
2462         __u8 tx_antenna_map;
2463 } __attribute__((packed));
2464
2465 static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
2466 {
2467         struct mwl8k_cmd_mimo_config *cmd;
2468         int rc;
2469
2470         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2471         if (cmd == NULL)
2472                 return -ENOMEM;
2473
2474         cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
2475         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2476         cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2477         cmd->rx_antenna_map = rx;
2478         cmd->tx_antenna_map = tx;
2479
2480         rc = mwl8k_post_cmd(hw, &cmd->header);
2481         kfree(cmd);
2482
2483         return rc;
2484 }
2485
2486 /*
2487  * CMD_USE_FIXED_RATE (STA version).
2488  */
2489 struct mwl8k_cmd_use_fixed_rate_sta {
2490         struct mwl8k_cmd_pkt header;
2491         __le32 action;
2492         __le32 allow_rate_drop;
2493         __le32 num_rates;
2494         struct {
2495                 __le32 is_ht_rate;
2496                 __le32 enable_retry;
2497                 __le32 rate;
2498                 __le32 retry_count;
2499         } rate_entry[8];
2500         __le32 rate_type;
2501         __le32 reserved1;
2502         __le32 reserved2;
2503 } __attribute__((packed));
2504
2505 #define MWL8K_USE_AUTO_RATE     0x0002
2506 #define MWL8K_UCAST_RATE        0
2507
2508 static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
2509 {
2510         struct mwl8k_cmd_use_fixed_rate_sta *cmd;
2511         int rc;
2512
2513         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2514         if (cmd == NULL)
2515                 return -ENOMEM;
2516
2517         cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2518         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2519         cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2520         cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
2521
2522         rc = mwl8k_post_cmd(hw, &cmd->header);
2523         kfree(cmd);
2524
2525         return rc;
2526 }
2527
2528 /*
2529  * CMD_USE_FIXED_RATE (AP version).
2530  */
2531 struct mwl8k_cmd_use_fixed_rate_ap {
2532         struct mwl8k_cmd_pkt header;
2533         __le32 action;
2534         __le32 allow_rate_drop;
2535         __le32 num_rates;
2536         struct mwl8k_rate_entry_ap {
2537                 __le32 is_ht_rate;
2538                 __le32 enable_retry;
2539                 __le32 rate;
2540                 __le32 retry_count;
2541         } rate_entry[4];
2542         u8 multicast_rate;
2543         u8 multicast_rate_type;
2544         u8 management_rate;
2545 } __attribute__((packed));
2546
2547 static int
2548 mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2549 {
2550         struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2551         int rc;
2552
2553         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2554         if (cmd == NULL)
2555                 return -ENOMEM;
2556
2557         cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2558         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2559         cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2560         cmd->multicast_rate = mcast;
2561         cmd->management_rate = mgmt;
2562
2563         rc = mwl8k_post_cmd(hw, &cmd->header);
2564         kfree(cmd);
2565
2566         return rc;
2567 }
2568
2569 /*
2570  * CMD_ENABLE_SNIFFER.
2571  */
2572 struct mwl8k_cmd_enable_sniffer {
2573         struct mwl8k_cmd_pkt header;
2574         __le32 action;
2575 } __attribute__((packed));
2576
2577 static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2578 {
2579         struct mwl8k_cmd_enable_sniffer *cmd;
2580         int rc;
2581
2582         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2583         if (cmd == NULL)
2584                 return -ENOMEM;
2585
2586         cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2587         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2588         cmd->action = cpu_to_le32(!!enable);
2589
2590         rc = mwl8k_post_cmd(hw, &cmd->header);
2591         kfree(cmd);
2592
2593         return rc;
2594 }
2595
2596 /*
2597  * CMD_SET_MAC_ADDR.
2598  */
2599 struct mwl8k_cmd_set_mac_addr {
2600         struct mwl8k_cmd_pkt header;
2601         union {
2602                 struct {
2603                         __le16 mac_type;
2604                         __u8 mac_addr[ETH_ALEN];
2605                 } mbss;
2606                 __u8 mac_addr[ETH_ALEN];
2607         };
2608 } __attribute__((packed));
2609
2610 #define MWL8K_MAC_TYPE_PRIMARY_CLIENT   0
2611 #define MWL8K_MAC_TYPE_PRIMARY_AP       2
2612
2613 static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw, u8 *mac)
2614 {
2615         struct mwl8k_priv *priv = hw->priv;
2616         struct mwl8k_cmd_set_mac_addr *cmd;
2617         int rc;
2618
2619         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2620         if (cmd == NULL)
2621                 return -ENOMEM;
2622
2623         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2624         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2625         if (priv->ap_fw) {
2626                 cmd->mbss.mac_type = cpu_to_le16(MWL8K_MAC_TYPE_PRIMARY_AP);
2627                 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2628         } else {
2629                 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2630         }
2631
2632         rc = mwl8k_post_cmd(hw, &cmd->header);
2633         kfree(cmd);
2634
2635         return rc;
2636 }
2637
2638 /*
2639  * CMD_SET_RATEADAPT_MODE.
2640  */
2641 struct mwl8k_cmd_set_rate_adapt_mode {
2642         struct mwl8k_cmd_pkt header;
2643         __le16 action;
2644         __le16 mode;
2645 } __attribute__((packed));
2646
2647 static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2648 {
2649         struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2650         int rc;
2651
2652         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2653         if (cmd == NULL)
2654                 return -ENOMEM;
2655
2656         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2657         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2658         cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2659         cmd->mode = cpu_to_le16(mode);
2660
2661         rc = mwl8k_post_cmd(hw, &cmd->header);
2662         kfree(cmd);
2663
2664         return rc;
2665 }
2666
2667 /*
2668  * CMD_SET_NEW_STN.
2669  */
2670 struct mwl8k_cmd_set_new_stn {
2671         struct mwl8k_cmd_pkt header;
2672         __le16 aid;
2673         __u8 mac_addr[6];
2674         __le16 stn_id;
2675         __le16 action;
2676         __le16 rsvd;
2677         __le32 legacy_rates;
2678         __u8 ht_rates[4];
2679         __le16 cap_info;
2680         __le16 ht_capabilities_info;
2681         __u8 mac_ht_param_info;
2682         __u8 rev;
2683         __u8 control_channel;
2684         __u8 add_channel;
2685         __le16 op_mode;
2686         __le16 stbc;
2687         __u8 add_qos_info;
2688         __u8 is_qos_sta;
2689         __le32 fw_sta_ptr;
2690 } __attribute__((packed));
2691
2692 #define MWL8K_STA_ACTION_ADD            0
2693 #define MWL8K_STA_ACTION_REMOVE         2
2694
2695 static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
2696                                      struct ieee80211_vif *vif,
2697                                      struct ieee80211_sta *sta)
2698 {
2699         struct mwl8k_cmd_set_new_stn *cmd;
2700         int rc;
2701
2702         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2703         if (cmd == NULL)
2704                 return -ENOMEM;
2705
2706         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2707         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2708         cmd->aid = cpu_to_le16(sta->aid);
2709         memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
2710         cmd->stn_id = cpu_to_le16(sta->aid);
2711         cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
2712         cmd->legacy_rates = cpu_to_le32(sta->supp_rates[IEEE80211_BAND_2GHZ]);
2713         if (sta->ht_cap.ht_supported) {
2714                 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
2715                 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
2716                 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
2717                 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
2718                 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
2719                 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
2720                         ((sta->ht_cap.ampdu_density & 7) << 2);
2721                 cmd->is_qos_sta = 1;
2722         }
2723
2724         rc = mwl8k_post_cmd(hw, &cmd->header);
2725         kfree(cmd);
2726
2727         return rc;
2728 }
2729
2730 static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
2731                                      struct ieee80211_vif *vif, u8 *addr)
2732 {
2733         struct mwl8k_cmd_set_new_stn *cmd;
2734         int rc;
2735
2736         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2737         if (cmd == NULL)
2738                 return -ENOMEM;
2739
2740         cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2741         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2742         memcpy(cmd->mac_addr, addr, ETH_ALEN);
2743         cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
2744
2745         rc = mwl8k_post_cmd(hw, &cmd->header);
2746         kfree(cmd);
2747
2748         return rc;
2749 }
2750
2751 /*
2752  * CMD_UPDATE_STADB.
2753  */
2754 struct ewc_ht_info {
2755         __le16  control1;
2756         __le16  control2;
2757         __le16  control3;
2758 } __attribute__((packed));
2759
2760 struct peer_capability_info {
2761         /* Peer type - AP vs. STA.  */
2762         __u8    peer_type;
2763
2764         /* Basic 802.11 capabilities from assoc resp.  */
2765         __le16  basic_caps;
2766
2767         /* Set if peer supports 802.11n high throughput (HT).  */
2768         __u8    ht_support;
2769
2770         /* Valid if HT is supported.  */
2771         __le16  ht_caps;
2772         __u8    extended_ht_caps;
2773         struct ewc_ht_info      ewc_info;
2774
2775         /* Legacy rate table. Intersection of our rates and peer rates.  */
2776         __u8    legacy_rates[12];
2777
2778         /* HT rate table. Intersection of our rates and peer rates.  */
2779         __u8    ht_rates[16];
2780         __u8    pad[16];
2781
2782         /* If set, interoperability mode, no proprietary extensions.  */
2783         __u8    interop;
2784         __u8    pad2;
2785         __u8    station_id;
2786         __le16  amsdu_enabled;
2787 } __attribute__((packed));
2788
2789 struct mwl8k_cmd_update_stadb {
2790         struct mwl8k_cmd_pkt header;
2791
2792         /* See STADB_ACTION_TYPE */
2793         __le32  action;
2794
2795         /* Peer MAC address */
2796         __u8    peer_addr[ETH_ALEN];
2797
2798         __le32  reserved;
2799
2800         /* Peer info - valid during add/update.  */
2801         struct peer_capability_info     peer_info;
2802 } __attribute__((packed));
2803
2804 #define MWL8K_STA_DB_MODIFY_ENTRY       1
2805 #define MWL8K_STA_DB_DEL_ENTRY          2
2806
2807 /* Peer Entry flags - used to define the type of the peer node */
2808 #define MWL8K_PEER_TYPE_ACCESSPOINT     2
2809
2810 static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
2811                                       struct ieee80211_vif *vif,
2812                                       struct ieee80211_sta *sta)
2813 {
2814         struct mwl8k_cmd_update_stadb *cmd;
2815         struct peer_capability_info *p;
2816         int rc;
2817
2818         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2819         if (cmd == NULL)
2820                 return -ENOMEM;
2821
2822         cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2823         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2824         cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
2825         memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
2826
2827         p = &cmd->peer_info;
2828         p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
2829         p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
2830         p->ht_support = sta->ht_cap.ht_supported;
2831         p->ht_caps = sta->ht_cap.cap;
2832         p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
2833                 ((sta->ht_cap.ampdu_density & 7) << 2);
2834         legacy_rate_mask_to_array(p->legacy_rates,
2835                                   sta->supp_rates[IEEE80211_BAND_2GHZ]);
2836         memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
2837         p->interop = 1;
2838         p->amsdu_enabled = 0;
2839
2840         rc = mwl8k_post_cmd(hw, &cmd->header);
2841         kfree(cmd);
2842
2843         return rc ? rc : p->station_id;
2844 }
2845
2846 static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
2847                                       struct ieee80211_vif *vif, u8 *addr)
2848 {
2849         struct mwl8k_cmd_update_stadb *cmd;
2850         int rc;
2851
2852         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2853         if (cmd == NULL)
2854                 return -ENOMEM;
2855
2856         cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
2857         cmd->header.length = cpu_to_le16(sizeof(*cmd));
2858         cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
2859         memcpy(cmd->peer_addr, addr, ETH_ALEN);
2860
2861         rc = mwl8k_post_cmd(hw, &cmd->header);
2862         kfree(cmd);
2863
2864         return rc;
2865 }
2866
2867
2868 /*
2869  * Interrupt handling.
2870  */
2871 static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
2872 {
2873         struct ieee80211_hw *hw = dev_id;
2874         struct mwl8k_priv *priv = hw->priv;
2875         u32 status;
2876
2877         status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2878         iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
2879
2880         if (!status)
2881                 return IRQ_NONE;
2882
2883         if (status & MWL8K_A2H_INT_TX_DONE)
2884                 tasklet_schedule(&priv->tx_reclaim_task);
2885
2886         if (status & MWL8K_A2H_INT_RX_READY) {
2887                 while (rxq_process(hw, 0, 1))
2888                         rxq_refill(hw, 0, 1);
2889         }
2890
2891         if (status & MWL8K_A2H_INT_OPC_DONE) {
2892                 if (priv->hostcmd_wait != NULL)
2893                         complete(priv->hostcmd_wait);
2894         }
2895
2896         if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
2897                 if (!mutex_is_locked(&priv->fw_mutex) &&
2898                     priv->radio_on && priv->pending_tx_pkts)
2899                         mwl8k_tx_start(priv);
2900         }
2901
2902         return IRQ_HANDLED;
2903 }
2904
2905
2906 /*
2907  * Core driver operations.
2908  */
2909 static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2910 {
2911         struct mwl8k_priv *priv = hw->priv;
2912         int index = skb_get_queue_mapping(skb);
2913         int rc;
2914
2915         if (priv->current_channel == NULL) {
2916                 printk(KERN_DEBUG "%s: dropped TX frame since radio "
2917                        "disabled\n", wiphy_name(hw->wiphy));
2918                 dev_kfree_skb(skb);
2919                 return NETDEV_TX_OK;
2920         }
2921
2922         rc = mwl8k_txq_xmit(hw, index, skb);
2923
2924         return rc;
2925 }
2926
2927 static int mwl8k_start(struct ieee80211_hw *hw)
2928 {
2929         struct mwl8k_priv *priv = hw->priv;
2930         int rc;
2931
2932         rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
2933                          IRQF_SHARED, MWL8K_NAME, hw);
2934         if (rc) {
2935                 printk(KERN_ERR "%s: failed to register IRQ handler\n",
2936                        wiphy_name(hw->wiphy));
2937                 return -EIO;
2938         }
2939
2940         /* Enable tx reclaim tasklet */
2941         tasklet_enable(&priv->tx_reclaim_task);
2942
2943         /* Enable interrupts */
2944         iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2945
2946         rc = mwl8k_fw_lock(hw);
2947         if (!rc) {
2948                 rc = mwl8k_cmd_radio_enable(hw);
2949
2950                 if (!priv->ap_fw) {
2951                         if (!rc)
2952                                 rc = mwl8k_cmd_enable_sniffer(hw, 0);
2953
2954                         if (!rc)
2955                                 rc = mwl8k_cmd_set_pre_scan(hw);
2956
2957                         if (!rc)
2958                                 rc = mwl8k_cmd_set_post_scan(hw,
2959                                                 "\x00\x00\x00\x00\x00\x00");
2960                 }
2961
2962                 if (!rc)
2963                         rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
2964
2965                 if (!rc)
2966                         rc = mwl8k_cmd_set_wmm_mode(hw, 0);
2967
2968                 mwl8k_fw_unlock(hw);
2969         }
2970
2971         if (rc) {
2972                 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2973                 free_irq(priv->pdev->irq, hw);
2974                 tasklet_disable(&priv->tx_reclaim_task);
2975         }
2976
2977         return rc;
2978 }
2979
2980 static void mwl8k_stop(struct ieee80211_hw *hw)
2981 {
2982         struct mwl8k_priv *priv = hw->priv;
2983         int i;
2984
2985         mwl8k_cmd_radio_disable(hw);
2986
2987         ieee80211_stop_queues(hw);
2988
2989         /* Disable interrupts */
2990         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
2991         free_irq(priv->pdev->irq, hw);
2992
2993         /* Stop finalize join worker */
2994         cancel_work_sync(&priv->finalize_join_worker);
2995         if (priv->beacon_skb != NULL)
2996                 dev_kfree_skb(priv->beacon_skb);
2997
2998         /* Stop tx reclaim tasklet */
2999         tasklet_disable(&priv->tx_reclaim_task);
3000
3001         /* Return all skbs to mac80211 */
3002         for (i = 0; i < MWL8K_TX_QUEUES; i++)
3003                 mwl8k_txq_reclaim(hw, i, 1);
3004 }
3005
3006 static int mwl8k_add_interface(struct ieee80211_hw *hw,
3007                                 struct ieee80211_vif *vif)
3008 {
3009         struct mwl8k_priv *priv = hw->priv;
3010         struct mwl8k_vif *mwl8k_vif;
3011
3012         /*
3013          * We only support one active interface at a time.
3014          */
3015         if (priv->vif != NULL)
3016                 return -EBUSY;
3017
3018         /*
3019          * We only support managed interfaces for now.
3020          */
3021         if (vif->type != NL80211_IFTYPE_STATION)
3022                 return -EINVAL;
3023
3024         /*
3025          * Reject interface creation if sniffer mode is active, as
3026          * STA operation is mutually exclusive with hardware sniffer
3027          * mode.
3028          */
3029         if (priv->sniffer_enabled) {
3030                 printk(KERN_INFO "%s: unable to create STA "
3031                        "interface due to sniffer mode being enabled\n",
3032                        wiphy_name(hw->wiphy));
3033                 return -EINVAL;
3034         }
3035
3036         /* Set the mac address.  */
3037         mwl8k_cmd_set_mac_addr(hw, vif->addr);
3038
3039         /* Clean out driver private area */
3040         mwl8k_vif = MWL8K_VIF(vif);
3041         memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
3042
3043         /* Set Initial sequence number to zero */
3044         mwl8k_vif->seqno = 0;
3045
3046         priv->vif = vif;
3047         priv->current_channel = NULL;
3048
3049         return 0;
3050 }
3051
3052 static void mwl8k_remove_interface(struct ieee80211_hw *hw,
3053                                    struct ieee80211_vif *vif)
3054 {
3055         struct mwl8k_priv *priv = hw->priv;
3056
3057         mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
3058
3059         priv->vif = NULL;
3060 }
3061
3062 static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
3063 {
3064         struct ieee80211_conf *conf = &hw->conf;
3065         struct mwl8k_priv *priv = hw->priv;
3066         int rc;
3067
3068         if (conf->flags & IEEE80211_CONF_IDLE) {
3069                 mwl8k_cmd_radio_disable(hw);
3070                 priv->current_channel = NULL;
3071                 return 0;
3072         }
3073
3074         rc = mwl8k_fw_lock(hw);
3075         if (rc)
3076                 return rc;
3077
3078         rc = mwl8k_cmd_radio_enable(hw);
3079         if (rc)
3080                 goto out;
3081
3082         rc = mwl8k_cmd_set_rf_channel(hw, conf);
3083         if (rc)
3084                 goto out;
3085
3086         priv->current_channel = conf->channel;
3087
3088         if (conf->power_level > 18)
3089                 conf->power_level = 18;
3090         rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
3091         if (rc)
3092                 goto out;
3093
3094         if (priv->ap_fw) {
3095                 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3096                 if (!rc)
3097                         rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3098         } else {
3099                 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3100         }
3101
3102 out:
3103         mwl8k_fw_unlock(hw);
3104
3105         return rc;
3106 }
3107
3108 static void mwl8k_bss_info_changed(struct ieee80211_hw *hw,
3109                                    struct ieee80211_vif *vif,
3110                                    struct ieee80211_bss_conf *info,
3111                                    u32 changed)
3112 {
3113         struct mwl8k_priv *priv = hw->priv;
3114         u32 ap_legacy_rates;
3115         u8 ap_mcs_rates[16];
3116         int rc;
3117
3118         if (mwl8k_fw_lock(hw))
3119                 return;
3120
3121         /*
3122          * No need to capture a beacon if we're no longer associated.
3123          */
3124         if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3125                 priv->capture_beacon = false;
3126
3127         /*
3128          * Get the AP's legacy and MCS rates.
3129          */
3130         ap_legacy_rates = 0;
3131         if (vif->bss_conf.assoc) {
3132                 struct ieee80211_sta *ap;
3133                 rcu_read_lock();
3134
3135                 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
3136                 if (ap == NULL) {
3137                         rcu_read_unlock();
3138                         goto out;
3139                 }
3140
3141                 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
3142                 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
3143
3144                 rcu_read_unlock();
3145         }
3146
3147         if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
3148                 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
3149                 if (rc)
3150                         goto out;
3151
3152                 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
3153                 if (rc)
3154                         goto out;
3155         }
3156
3157         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3158                 rc = mwl8k_set_radio_preamble(hw,
3159                                 vif->bss_conf.use_short_preamble);
3160                 if (rc)
3161                         goto out;
3162         }
3163
3164         if (changed & BSS_CHANGED_ERP_SLOT) {
3165                 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
3166                 if (rc)
3167                         goto out;
3168         }
3169
3170         if (((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) ||
3171             (changed & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT))) {
3172                 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
3173                 if (rc)
3174                         goto out;
3175         }
3176
3177         if (vif->bss_conf.assoc &&
3178             (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
3179                 /*
3180                  * Finalize the join.  Tell rx handler to process
3181                  * next beacon from our BSSID.
3182                  */
3183                 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
3184                 priv->capture_beacon = true;
3185         }
3186
3187 out:
3188         mwl8k_fw_unlock(hw);
3189 }
3190
3191 static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3192                                    int mc_count, struct dev_addr_list *mclist)
3193 {
3194         struct mwl8k_cmd_pkt *cmd;
3195
3196         /*
3197          * Synthesize and return a command packet that programs the
3198          * hardware multicast address filter.  At this point we don't
3199          * know whether FIF_ALLMULTI is being requested, but if it is,
3200          * we'll end up throwing this packet away and creating a new
3201          * one in mwl8k_configure_filter().
3202          */
3203         cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
3204
3205         return (unsigned long)cmd;
3206 }
3207
3208 static int
3209 mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3210                                unsigned int changed_flags,
3211                                unsigned int *total_flags)
3212 {
3213         struct mwl8k_priv *priv = hw->priv;
3214
3215         /*
3216          * Hardware sniffer mode is mutually exclusive with STA
3217          * operation, so refuse to enable sniffer mode if a STA
3218          * interface is active.
3219          */
3220         if (priv->vif != NULL) {
3221                 if (net_ratelimit())
3222                         printk(KERN_INFO "%s: not enabling sniffer "
3223                                "mode because STA interface is active\n",
3224                                wiphy_name(hw->wiphy));
3225                 return 0;
3226         }
3227
3228         if (!priv->sniffer_enabled) {
3229                 if (mwl8k_cmd_enable_sniffer(hw, 1))
3230                         return 0;
3231                 priv->sniffer_enabled = true;
3232         }
3233
3234         *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3235                         FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3236                         FIF_OTHER_BSS;
3237
3238         return 1;
3239 }
3240
3241 static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3242                                    unsigned int changed_flags,
3243                                    unsigned int *total_flags,
3244                                    u64 multicast)
3245 {
3246         struct mwl8k_priv *priv = hw->priv;
3247         struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3248
3249         /*
3250          * AP firmware doesn't allow fine-grained control over
3251          * the receive filter.
3252          */
3253         if (priv->ap_fw) {
3254                 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3255                 kfree(cmd);
3256                 return;
3257         }
3258
3259         /*
3260          * Enable hardware sniffer mode if FIF_CONTROL or
3261          * FIF_OTHER_BSS is requested.
3262          */
3263         if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3264             mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3265                 kfree(cmd);
3266                 return;
3267         }
3268
3269         /* Clear unsupported feature flags */
3270         *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3271
3272         if (mwl8k_fw_lock(hw)) {
3273                 kfree(cmd);
3274                 return;
3275         }
3276
3277         if (priv->sniffer_enabled) {
3278                 mwl8k_cmd_enable_sniffer(hw, 0);
3279                 priv->sniffer_enabled = false;
3280         }
3281
3282         if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
3283                 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3284                         /*
3285                          * Disable the BSS filter.
3286                          */
3287                         mwl8k_cmd_set_pre_scan(hw);
3288                 } else {
3289                         const u8 *bssid;
3290
3291                         /*
3292                          * Enable the BSS filter.
3293                          *
3294                          * If there is an active STA interface, use that
3295                          * interface's BSSID, otherwise use a dummy one
3296                          * (where the OUI part needs to be nonzero for
3297                          * the BSSID to be accepted by POST_SCAN).
3298                          */
3299                         bssid = "\x01\x00\x00\x00\x00\x00";
3300                         if (priv->vif != NULL)
3301                                 bssid = priv->vif->bss_conf.bssid;
3302
3303                         mwl8k_cmd_set_post_scan(hw, bssid);
3304                 }
3305         }
3306
3307         /*
3308          * If FIF_ALLMULTI is being requested, throw away the command
3309          * packet that ->prepare_multicast() built and replace it with
3310          * a command packet that enables reception of all multicast
3311          * packets.
3312          */
3313         if (*total_flags & FIF_ALLMULTI) {
3314                 kfree(cmd);
3315                 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3316         }
3317
3318         if (cmd != NULL) {
3319                 mwl8k_post_cmd(hw, cmd);
3320                 kfree(cmd);
3321         }
3322
3323         mwl8k_fw_unlock(hw);
3324 }
3325
3326 static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3327 {
3328         return mwl8k_cmd_set_rts_threshold(hw, value);
3329 }
3330
3331 struct mwl8k_sta_notify_item
3332 {
3333         struct list_head list;
3334         struct ieee80211_vif *vif;
3335         enum sta_notify_cmd cmd;
3336         struct ieee80211_sta sta;
3337 };
3338
3339 static void
3340 mwl8k_do_sta_notify(struct ieee80211_hw *hw, struct mwl8k_sta_notify_item *s)
3341 {
3342         struct mwl8k_priv *priv = hw->priv;
3343
3344         /*
3345          * STA firmware uses UPDATE_STADB, AP firmware uses SET_NEW_STN.
3346          */
3347         if (!priv->ap_fw && s->cmd == STA_NOTIFY_ADD) {
3348                 int rc;
3349
3350                 rc = mwl8k_cmd_update_stadb_add(hw, s->vif, &s->sta);
3351                 if (rc >= 0) {
3352                         struct ieee80211_sta *sta;
3353
3354                         rcu_read_lock();
3355                         sta = ieee80211_find_sta(s->vif, s->sta.addr);
3356                         if (sta != NULL)
3357                                 MWL8K_STA(sta)->peer_id = rc;
3358                         rcu_read_unlock();
3359                 }
3360         } else if (!priv->ap_fw && s->cmd == STA_NOTIFY_REMOVE) {
3361                 mwl8k_cmd_update_stadb_del(hw, s->vif, s->sta.addr);
3362         } else if (priv->ap_fw && s->cmd == STA_NOTIFY_ADD) {
3363                 mwl8k_cmd_set_new_stn_add(hw, s->vif, &s->sta);
3364         } else if (priv->ap_fw && s->cmd == STA_NOTIFY_REMOVE) {
3365                 mwl8k_cmd_set_new_stn_del(hw, s->vif, s->sta.addr);
3366         }
3367 }
3368
3369 static void mwl8k_sta_notify_worker(struct work_struct *work)
3370 {
3371         struct mwl8k_priv *priv =
3372                 container_of(work, struct mwl8k_priv, sta_notify_worker);
3373         struct ieee80211_hw *hw = priv->hw;
3374
3375         spin_lock_bh(&priv->sta_notify_list_lock);
3376         while (!list_empty(&priv->sta_notify_list)) {
3377                 struct mwl8k_sta_notify_item *s;
3378
3379                 s = list_entry(priv->sta_notify_list.next,
3380                                struct mwl8k_sta_notify_item, list);
3381                 list_del(&s->list);
3382
3383                 spin_unlock_bh(&priv->sta_notify_list_lock);
3384
3385                 mwl8k_do_sta_notify(hw, s);
3386                 kfree(s);
3387
3388                 spin_lock_bh(&priv->sta_notify_list_lock);
3389         }
3390         spin_unlock_bh(&priv->sta_notify_list_lock);
3391 }
3392
3393 static void
3394 mwl8k_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3395                  enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3396 {
3397         struct mwl8k_priv *priv = hw->priv;
3398         struct mwl8k_sta_notify_item *s;
3399
3400         if (cmd != STA_NOTIFY_ADD && cmd != STA_NOTIFY_REMOVE)
3401                 return;
3402
3403         s = kmalloc(sizeof(*s), GFP_ATOMIC);
3404         if (s != NULL) {
3405                 s->vif = vif;
3406                 s->cmd = cmd;
3407                 s->sta = *sta;
3408
3409                 spin_lock(&priv->sta_notify_list_lock);
3410                 list_add_tail(&s->list, &priv->sta_notify_list);
3411                 spin_unlock(&priv->sta_notify_list_lock);
3412
3413                 ieee80211_queue_work(hw, &priv->sta_notify_worker);
3414         }
3415 }
3416
3417 static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3418                          const struct ieee80211_tx_queue_params *params)
3419 {
3420         struct mwl8k_priv *priv = hw->priv;
3421         int rc;
3422
3423         rc = mwl8k_fw_lock(hw);
3424         if (!rc) {
3425                 if (!priv->wmm_enabled)
3426                         rc = mwl8k_cmd_set_wmm_mode(hw, 1);
3427
3428                 if (!rc)
3429                         rc = mwl8k_cmd_set_edca_params(hw, queue,
3430                                                        params->cw_min,
3431                                                        params->cw_max,
3432                                                        params->aifs,
3433                                                        params->txop);
3434
3435                 mwl8k_fw_unlock(hw);
3436         }
3437
3438         return rc;
3439 }
3440
3441 static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3442                               struct ieee80211_tx_queue_stats *stats)
3443 {
3444         struct mwl8k_priv *priv = hw->priv;
3445         struct mwl8k_tx_queue *txq;
3446         int index;
3447
3448         spin_lock_bh(&priv->tx_lock);
3449         for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3450                 txq = priv->txq + index;
3451                 memcpy(&stats[index], &txq->stats,
3452                         sizeof(struct ieee80211_tx_queue_stats));
3453         }
3454         spin_unlock_bh(&priv->tx_lock);
3455
3456         return 0;
3457 }
3458
3459 static int mwl8k_get_stats(struct ieee80211_hw *hw,
3460                            struct ieee80211_low_level_stats *stats)
3461 {
3462         return mwl8k_cmd_get_stat(hw, stats);
3463 }
3464
3465 static int
3466 mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3467                    enum ieee80211_ampdu_mlme_action action,
3468                    struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3469 {
3470         switch (action) {
3471         case IEEE80211_AMPDU_RX_START:
3472         case IEEE80211_AMPDU_RX_STOP:
3473                 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3474                         return -ENOTSUPP;
3475                 return 0;
3476         default:
3477                 return -ENOTSUPP;
3478         }
3479 }
3480
3481 static const struct ieee80211_ops mwl8k_ops = {
3482         .tx                     = mwl8k_tx,
3483         .start                  = mwl8k_start,
3484         .stop                   = mwl8k_stop,
3485         .add_interface          = mwl8k_add_interface,
3486         .remove_interface       = mwl8k_remove_interface,
3487         .config                 = mwl8k_config,
3488         .bss_info_changed       = mwl8k_bss_info_changed,
3489         .prepare_multicast      = mwl8k_prepare_multicast,
3490         .configure_filter       = mwl8k_configure_filter,
3491         .set_rts_threshold      = mwl8k_set_rts_threshold,
3492         .sta_notify             = mwl8k_sta_notify,
3493         .conf_tx                = mwl8k_conf_tx,
3494         .get_tx_stats           = mwl8k_get_tx_stats,
3495         .get_stats              = mwl8k_get_stats,
3496         .ampdu_action           = mwl8k_ampdu_action,
3497 };
3498
3499 static void mwl8k_tx_reclaim_handler(unsigned long data)
3500 {
3501         int i;
3502         struct ieee80211_hw *hw = (struct ieee80211_hw *) data;
3503         struct mwl8k_priv *priv = hw->priv;
3504
3505         spin_lock_bh(&priv->tx_lock);
3506         for (i = 0; i < MWL8K_TX_QUEUES; i++)
3507                 mwl8k_txq_reclaim(hw, i, 0);
3508
3509         if (priv->tx_wait != NULL && !priv->pending_tx_pkts) {
3510                 complete(priv->tx_wait);
3511                 priv->tx_wait = NULL;
3512         }
3513         spin_unlock_bh(&priv->tx_lock);
3514 }
3515
3516 static void mwl8k_finalize_join_worker(struct work_struct *work)
3517 {
3518         struct mwl8k_priv *priv =
3519                 container_of(work, struct mwl8k_priv, finalize_join_worker);
3520         struct sk_buff *skb = priv->beacon_skb;
3521
3522         mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len,
3523                                 priv->vif->bss_conf.dtim_period);
3524         dev_kfree_skb(skb);
3525
3526         priv->beacon_skb = NULL;
3527 }
3528
3529 enum {
3530         MWL8363 = 0,
3531         MWL8687,
3532         MWL8366,
3533 };
3534
3535 static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
3536         [MWL8363] = {
3537                 .part_name      = "88w8363",
3538                 .helper_image   = "mwl8k/helper_8363.fw",
3539                 .fw_image       = "mwl8k/fmimage_8363.fw",
3540         },
3541         [MWL8687] = {
3542                 .part_name      = "88w8687",
3543                 .helper_image   = "mwl8k/helper_8687.fw",
3544                 .fw_image       = "mwl8k/fmimage_8687.fw",
3545         },
3546         [MWL8366] = {
3547                 .part_name      = "88w8366",
3548                 .helper_image   = "mwl8k/helper_8366.fw",
3549                 .fw_image       = "mwl8k/fmimage_8366.fw",
3550                 .ap_rxd_ops     = &rxd_8366_ap_ops,
3551         },
3552 };
3553
3554 static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
3555         { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3556         { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
3557         { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3558         { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3559         { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
3560         { },
3561 };
3562 MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3563
3564 static int __devinit mwl8k_probe(struct pci_dev *pdev,
3565                                  const struct pci_device_id *id)
3566 {
3567         static int printed_version = 0;
3568         struct ieee80211_hw *hw;
3569         struct mwl8k_priv *priv;
3570         int rc;
3571         int i;
3572
3573         if (!printed_version) {
3574                 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3575                 printed_version = 1;
3576         }
3577
3578
3579         rc = pci_enable_device(pdev);
3580         if (rc) {
3581                 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3582                        MWL8K_NAME);
3583                 return rc;
3584         }
3585
3586         rc = pci_request_regions(pdev, MWL8K_NAME);
3587         if (rc) {
3588                 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3589                        MWL8K_NAME);
3590                 goto err_disable_device;
3591         }
3592
3593         pci_set_master(pdev);
3594
3595
3596         hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3597         if (hw == NULL) {
3598                 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3599                 rc = -ENOMEM;
3600                 goto err_free_reg;
3601         }
3602
3603         SET_IEEE80211_DEV(hw, &pdev->dev);
3604         pci_set_drvdata(pdev, hw);
3605
3606         priv = hw->priv;
3607         priv->hw = hw;
3608         priv->pdev = pdev;
3609         priv->device_info = &mwl8k_info_tbl[id->driver_data];
3610
3611
3612         priv->sram = pci_iomap(pdev, 0, 0x10000);
3613         if (priv->sram == NULL) {
3614                 printk(KERN_ERR "%s: Cannot map device SRAM\n",
3615                        wiphy_name(hw->wiphy));
3616                 goto err_iounmap;
3617         }
3618
3619         /*
3620          * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3621          * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3622          */
3623         priv->regs = pci_iomap(pdev, 1, 0x10000);
3624         if (priv->regs == NULL) {
3625                 priv->regs = pci_iomap(pdev, 2, 0x10000);
3626                 if (priv->regs == NULL) {
3627                         printk(KERN_ERR "%s: Cannot map device registers\n",
3628                                wiphy_name(hw->wiphy));
3629                         goto err_iounmap;
3630                 }
3631         }
3632
3633
3634         /* Reset firmware and hardware */
3635         mwl8k_hw_reset(priv);
3636
3637         /* Ask userland hotplug daemon for the device firmware */
3638         rc = mwl8k_request_firmware(priv);
3639         if (rc) {
3640                 printk(KERN_ERR "%s: Firmware files not found\n",
3641                        wiphy_name(hw->wiphy));
3642                 goto err_stop_firmware;
3643         }
3644
3645         /* Load firmware into hardware */
3646         rc = mwl8k_load_firmware(hw);
3647         if (rc) {
3648                 printk(KERN_ERR "%s: Cannot start firmware\n",
3649                        wiphy_name(hw->wiphy));
3650                 goto err_stop_firmware;
3651         }
3652
3653         /* Reclaim memory once firmware is successfully loaded */
3654         mwl8k_release_firmware(priv);
3655
3656
3657         if (priv->ap_fw) {
3658                 priv->rxd_ops = priv->device_info->ap_rxd_ops;
3659                 if (priv->rxd_ops == NULL) {
3660                         printk(KERN_ERR "%s: Driver does not have AP "
3661                                "firmware image support for this hardware\n",
3662                                wiphy_name(hw->wiphy));
3663                         goto err_stop_firmware;
3664                 }
3665         } else {
3666                 priv->rxd_ops = &rxd_sta_ops;
3667         }
3668
3669         priv->sniffer_enabled = false;
3670         priv->wmm_enabled = false;
3671         priv->pending_tx_pkts = 0;
3672
3673
3674         memcpy(priv->channels, mwl8k_channels, sizeof(mwl8k_channels));
3675         priv->band.band = IEEE80211_BAND_2GHZ;
3676         priv->band.channels = priv->channels;
3677         priv->band.n_channels = ARRAY_SIZE(mwl8k_channels);
3678         priv->band.bitrates = priv->rates;
3679         priv->band.n_bitrates = ARRAY_SIZE(mwl8k_rates);
3680         hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band;
3681
3682         BUILD_BUG_ON(sizeof(priv->rates) != sizeof(mwl8k_rates));
3683         memcpy(priv->rates, mwl8k_rates, sizeof(mwl8k_rates));
3684
3685         /*
3686          * Extra headroom is the size of the required DMA header
3687          * minus the size of the smallest 802.11 frame (CTS frame).
3688          */
3689         hw->extra_tx_headroom =
3690                 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3691
3692         hw->channel_change_time = 10;
3693
3694         hw->queues = MWL8K_TX_QUEUES;
3695
3696         /* Set rssi and noise values to dBm */
3697         hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
3698         hw->vif_data_size = sizeof(struct mwl8k_vif);
3699         hw->sta_data_size = sizeof(struct mwl8k_sta);
3700         priv->vif = NULL;
3701
3702         /* Set default radio state and preamble */
3703         priv->radio_on = 0;
3704         priv->radio_short_preamble = 0;
3705
3706         /* Station database handling */
3707         INIT_WORK(&priv->sta_notify_worker, mwl8k_sta_notify_worker);
3708         spin_lock_init(&priv->sta_notify_list_lock);
3709         INIT_LIST_HEAD(&priv->sta_notify_list);
3710
3711         /* Finalize join worker */
3712         INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
3713
3714         /* TX reclaim tasklet */
3715         tasklet_init(&priv->tx_reclaim_task,
3716                         mwl8k_tx_reclaim_handler, (unsigned long)hw);
3717         tasklet_disable(&priv->tx_reclaim_task);
3718
3719         /* Power management cookie */
3720         priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
3721         if (priv->cookie == NULL)
3722                 goto err_stop_firmware;
3723
3724         rc = mwl8k_rxq_init(hw, 0);
3725         if (rc)
3726                 goto err_free_cookie;
3727         rxq_refill(hw, 0, INT_MAX);
3728
3729         mutex_init(&priv->fw_mutex);
3730         priv->fw_mutex_owner = NULL;
3731         priv->fw_mutex_depth = 0;
3732         priv->hostcmd_wait = NULL;
3733
3734         spin_lock_init(&priv->tx_lock);
3735
3736         priv->tx_wait = NULL;
3737
3738         for (i = 0; i < MWL8K_TX_QUEUES; i++) {
3739                 rc = mwl8k_txq_init(hw, i);
3740                 if (rc)
3741                         goto err_free_queues;
3742         }
3743
3744         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3745         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3746         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
3747         iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3748
3749         rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
3750                          IRQF_SHARED, MWL8K_NAME, hw);
3751         if (rc) {
3752                 printk(KERN_ERR "%s: failed to register IRQ handler\n",
3753                        wiphy_name(hw->wiphy));
3754                 goto err_free_queues;
3755         }
3756
3757         /*
3758          * Temporarily enable interrupts.  Initial firmware host
3759          * commands use interrupts and avoid polling.  Disable
3760          * interrupts when done.
3761          */
3762         iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3763
3764         /* Get config data, mac addrs etc */
3765         if (priv->ap_fw) {
3766                 rc = mwl8k_cmd_get_hw_spec_ap(hw);
3767                 if (!rc)
3768                         rc = mwl8k_cmd_set_hw_spec(hw);
3769         } else {
3770                 rc = mwl8k_cmd_get_hw_spec_sta(hw);
3771
3772                 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
3773         }
3774         if (rc) {
3775                 printk(KERN_ERR "%s: Cannot initialise firmware\n",
3776                        wiphy_name(hw->wiphy));
3777                 goto err_free_irq;
3778         }
3779
3780         /* Turn radio off */
3781         rc = mwl8k_cmd_radio_disable(hw);
3782         if (rc) {
3783                 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
3784                 goto err_free_irq;
3785         }
3786
3787         /* Clear MAC address */
3788         rc = mwl8k_cmd_set_mac_addr(hw, "\x00\x00\x00\x00\x00\x00");
3789         if (rc) {
3790                 printk(KERN_ERR "%s: Cannot clear MAC address\n",
3791                        wiphy_name(hw->wiphy));
3792                 goto err_free_irq;
3793         }
3794
3795         /* Disable interrupts */
3796         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3797         free_irq(priv->pdev->irq, hw);
3798
3799         rc = ieee80211_register_hw(hw);
3800         if (rc) {
3801                 printk(KERN_ERR "%s: Cannot register device\n",
3802                        wiphy_name(hw->wiphy));
3803                 goto err_free_queues;
3804         }
3805
3806         printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
3807                wiphy_name(hw->wiphy), priv->device_info->part_name,
3808                priv->hw_rev, hw->wiphy->perm_addr,
3809                priv->ap_fw ? "AP" : "STA",
3810                (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
3811                (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
3812
3813         return 0;
3814
3815 err_free_irq:
3816         iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3817         free_irq(priv->pdev->irq, hw);
3818
3819 err_free_queues:
3820         for (i = 0; i < MWL8K_TX_QUEUES; i++)
3821                 mwl8k_txq_deinit(hw, i);
3822         mwl8k_rxq_deinit(hw, 0);
3823
3824 err_free_cookie:
3825         if (priv->cookie != NULL)
3826                 pci_free_consistent(priv->pdev, 4,
3827                                 priv->cookie, priv->cookie_dma);
3828
3829 err_stop_firmware:
3830         mwl8k_hw_reset(priv);
3831         mwl8k_release_firmware(priv);
3832
3833 err_iounmap:
3834         if (priv->regs != NULL)
3835                 pci_iounmap(pdev, priv->regs);
3836
3837         if (priv->sram != NULL)
3838                 pci_iounmap(pdev, priv->sram);
3839
3840         pci_set_drvdata(pdev, NULL);
3841         ieee80211_free_hw(hw);
3842
3843 err_free_reg:
3844         pci_release_regions(pdev);
3845
3846 err_disable_device:
3847         pci_disable_device(pdev);
3848
3849         return rc;
3850 }
3851
3852 static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
3853 {
3854         printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
3855 }
3856
3857 static void __devexit mwl8k_remove(struct pci_dev *pdev)
3858 {
3859         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
3860         struct mwl8k_priv *priv;
3861         int i;
3862
3863         if (hw == NULL)
3864                 return;
3865         priv = hw->priv;
3866
3867         ieee80211_stop_queues(hw);
3868
3869         ieee80211_unregister_hw(hw);
3870
3871         /* Remove tx reclaim tasklet */
3872         tasklet_kill(&priv->tx_reclaim_task);
3873
3874         /* Stop hardware */
3875         mwl8k_hw_reset(priv);
3876
3877         /* Return all skbs to mac80211 */
3878         for (i = 0; i < MWL8K_TX_QUEUES; i++)
3879                 mwl8k_txq_reclaim(hw, i, 1);
3880
3881         for (i = 0; i < MWL8K_TX_QUEUES; i++)
3882                 mwl8k_txq_deinit(hw, i);
3883
3884         mwl8k_rxq_deinit(hw, 0);
3885
3886         pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
3887
3888         pci_iounmap(pdev, priv->regs);
3889         pci_iounmap(pdev, priv->sram);
3890         pci_set_drvdata(pdev, NULL);
3891         ieee80211_free_hw(hw);
3892         pci_release_regions(pdev);
3893         pci_disable_device(pdev);
3894 }
3895
3896 static struct pci_driver mwl8k_driver = {
3897         .name           = MWL8K_NAME,
3898         .id_table       = mwl8k_pci_id_table,
3899         .probe          = mwl8k_probe,
3900         .remove         = __devexit_p(mwl8k_remove),
3901         .shutdown       = __devexit_p(mwl8k_shutdown),
3902 };
3903
3904 static int __init mwl8k_init(void)
3905 {
3906         return pci_register_driver(&mwl8k_driver);
3907 }
3908
3909 static void __exit mwl8k_exit(void)
3910 {
3911         pci_unregister_driver(&mwl8k_driver);
3912 }
3913
3914 module_init(mwl8k_init);
3915 module_exit(mwl8k_exit);
3916
3917 MODULE_DESCRIPTION(MWL8K_DESC);
3918 MODULE_VERSION(MWL8K_VERSION);
3919 MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
3920 MODULE_LICENSE("GPL");