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