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