Staging: unisys: Remove RETINT macro
[linux/fpc-iii.git] / drivers / net / wireless / mwl8k.c
blob4987c3f942ce7dff2bb599feab7bb1aa26d40f50
1 /*
2 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
5 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
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.
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/spinlock.h>
17 #include <linux/list.h>
18 #include <linux/pci.h>
19 #include <linux/delay.h>
20 #include <linux/completion.h>
21 #include <linux/etherdevice.h>
22 #include <linux/slab.h>
23 #include <net/mac80211.h>
24 #include <linux/moduleparam.h>
25 #include <linux/firmware.h>
26 #include <linux/workqueue.h>
28 #define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
29 #define MWL8K_NAME KBUILD_MODNAME
30 #define MWL8K_VERSION "0.13"
32 /* Module parameters */
33 static bool ap_mode_default;
34 module_param(ap_mode_default, bool, 0);
35 MODULE_PARM_DESC(ap_mode_default,
36 "Set to 1 to make ap mode the default instead of sta mode");
38 /* Register definitions */
39 #define MWL8K_HIU_GEN_PTR 0x00000c10
40 #define MWL8K_MODE_STA 0x0000005a
41 #define MWL8K_MODE_AP 0x000000a5
42 #define MWL8K_HIU_INT_CODE 0x00000c14
43 #define MWL8K_FWSTA_READY 0xf0f1f2f4
44 #define MWL8K_FWAP_READY 0xf1f2f4a5
45 #define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
46 #define MWL8K_HIU_SCRATCH 0x00000c40
48 /* Host->device communications */
49 #define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
50 #define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
51 #define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
52 #define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
53 #define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
54 #define MWL8K_H2A_INT_DUMMY (1 << 20)
55 #define MWL8K_H2A_INT_RESET (1 << 15)
56 #define MWL8K_H2A_INT_DOORBELL (1 << 1)
57 #define MWL8K_H2A_INT_PPA_READY (1 << 0)
59 /* Device->host communications */
60 #define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
61 #define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
62 #define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
63 #define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
64 #define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
65 #define MWL8K_A2H_INT_DUMMY (1 << 20)
66 #define MWL8K_A2H_INT_BA_WATCHDOG (1 << 14)
67 #define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
68 #define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
69 #define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
70 #define MWL8K_A2H_INT_RADIO_ON (1 << 6)
71 #define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
72 #define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
73 #define MWL8K_A2H_INT_OPC_DONE (1 << 2)
74 #define MWL8K_A2H_INT_RX_READY (1 << 1)
75 #define MWL8K_A2H_INT_TX_DONE (1 << 0)
77 /* HW micro second timer register
78 * located at offset 0xA600. This
79 * will be used to timestamp tx
80 * packets.
83 #define MWL8K_HW_TIMER_REGISTER 0x0000a600
85 #define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
86 MWL8K_A2H_INT_CHNL_SWITCHED | \
87 MWL8K_A2H_INT_QUEUE_EMPTY | \
88 MWL8K_A2H_INT_RADAR_DETECT | \
89 MWL8K_A2H_INT_RADIO_ON | \
90 MWL8K_A2H_INT_RADIO_OFF | \
91 MWL8K_A2H_INT_MAC_EVENT | \
92 MWL8K_A2H_INT_OPC_DONE | \
93 MWL8K_A2H_INT_RX_READY | \
94 MWL8K_A2H_INT_TX_DONE | \
95 MWL8K_A2H_INT_BA_WATCHDOG)
97 #define MWL8K_RX_QUEUES 1
98 #define MWL8K_TX_WMM_QUEUES 4
99 #define MWL8K_MAX_AMPDU_QUEUES 8
100 #define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
101 #define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
103 /* txpriorities are mapped with hw queues.
104 * Each hw queue has a txpriority.
106 #define TOTAL_HW_TX_QUEUES 8
108 /* Each HW queue can have one AMPDU stream.
109 * But, because one of the hw queue is reserved,
110 * maximum AMPDU queues that can be created are
111 * one short of total tx queues.
113 #define MWL8K_NUM_AMPDU_STREAMS (TOTAL_HW_TX_QUEUES - 1)
115 struct rxd_ops {
116 int rxd_size;
117 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
118 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
119 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
120 __le16 *qos, s8 *noise);
123 struct mwl8k_device_info {
124 char *part_name;
125 char *helper_image;
126 char *fw_image_sta;
127 char *fw_image_ap;
128 struct rxd_ops *ap_rxd_ops;
129 u32 fw_api_ap;
132 struct mwl8k_rx_queue {
133 int rxd_count;
135 /* hw receives here */
136 int head;
138 /* refill descs here */
139 int tail;
141 void *rxd;
142 dma_addr_t rxd_dma;
143 struct {
144 struct sk_buff *skb;
145 DEFINE_DMA_UNMAP_ADDR(dma);
146 } *buf;
149 struct mwl8k_tx_queue {
150 /* hw transmits here */
151 int head;
153 /* sw appends here */
154 int tail;
156 unsigned int len;
157 struct mwl8k_tx_desc *txd;
158 dma_addr_t txd_dma;
159 struct sk_buff **skb;
162 enum {
163 AMPDU_NO_STREAM,
164 AMPDU_STREAM_NEW,
165 AMPDU_STREAM_IN_PROGRESS,
166 AMPDU_STREAM_ACTIVE,
169 struct mwl8k_ampdu_stream {
170 struct ieee80211_sta *sta;
171 u8 tid;
172 u8 state;
173 u8 idx;
176 struct mwl8k_priv {
177 struct ieee80211_hw *hw;
178 struct pci_dev *pdev;
179 int irq;
181 struct mwl8k_device_info *device_info;
183 void __iomem *sram;
184 void __iomem *regs;
186 /* firmware */
187 const struct firmware *fw_helper;
188 const struct firmware *fw_ucode;
190 /* hardware/firmware parameters */
191 bool ap_fw;
192 struct rxd_ops *rxd_ops;
193 struct ieee80211_supported_band band_24;
194 struct ieee80211_channel channels_24[14];
195 struct ieee80211_rate rates_24[13];
196 struct ieee80211_supported_band band_50;
197 struct ieee80211_channel channels_50[4];
198 struct ieee80211_rate rates_50[8];
199 u32 ap_macids_supported;
200 u32 sta_macids_supported;
202 /* Ampdu stream information */
203 u8 num_ampdu_queues;
204 spinlock_t stream_lock;
205 struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
206 struct work_struct watchdog_ba_handle;
208 /* firmware access */
209 struct mutex fw_mutex;
210 struct task_struct *fw_mutex_owner;
211 struct task_struct *hw_restart_owner;
212 int fw_mutex_depth;
213 struct completion *hostcmd_wait;
215 atomic_t watchdog_event_pending;
217 /* lock held over TX and TX reap */
218 spinlock_t tx_lock;
220 /* TX quiesce completion, protected by fw_mutex and tx_lock */
221 struct completion *tx_wait;
223 /* List of interfaces. */
224 u32 macids_used;
225 struct list_head vif_list;
227 /* power management status cookie from firmware */
228 u32 *cookie;
229 dma_addr_t cookie_dma;
231 u16 num_mcaddrs;
232 u8 hw_rev;
233 u32 fw_rev;
234 u32 caps;
237 * Running count of TX packets in flight, to avoid
238 * iterating over the transmit rings each time.
240 int pending_tx_pkts;
242 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
243 struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
244 u32 txq_offset[MWL8K_MAX_TX_QUEUES];
246 bool radio_on;
247 bool radio_short_preamble;
248 bool sniffer_enabled;
249 bool wmm_enabled;
251 /* XXX need to convert this to handle multiple interfaces */
252 bool capture_beacon;
253 u8 capture_bssid[ETH_ALEN];
254 struct sk_buff *beacon_skb;
257 * This FJ worker has to be global as it is scheduled from the
258 * RX handler. At this point we don't know which interface it
259 * belongs to until the list of bssids waiting to complete join
260 * is checked.
262 struct work_struct finalize_join_worker;
264 /* Tasklet to perform TX reclaim. */
265 struct tasklet_struct poll_tx_task;
267 /* Tasklet to perform RX. */
268 struct tasklet_struct poll_rx_task;
270 /* Most recently reported noise in dBm */
271 s8 noise;
274 * preserve the queue configurations so they can be restored if/when
275 * the firmware image is swapped.
277 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
279 /* To perform the task of reloading the firmware */
280 struct work_struct fw_reload;
281 bool hw_restart_in_progress;
283 /* async firmware loading state */
284 unsigned fw_state;
285 char *fw_pref;
286 char *fw_alt;
287 bool is_8764;
288 struct completion firmware_loading_complete;
290 /* bitmap of running BSSes */
291 u32 running_bsses;
294 #define MAX_WEP_KEY_LEN 13
295 #define NUM_WEP_KEYS 4
297 /* Per interface specific private data */
298 struct mwl8k_vif {
299 struct list_head list;
300 struct ieee80211_vif *vif;
302 /* Firmware macid for this vif. */
303 int macid;
305 /* Non AMPDU sequence number assigned by driver. */
306 u16 seqno;
308 /* Saved WEP keys */
309 struct {
310 u8 enabled;
311 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
312 } wep_key_conf[NUM_WEP_KEYS];
314 /* BSSID */
315 u8 bssid[ETH_ALEN];
317 /* A flag to indicate is HW crypto is enabled for this bssid */
318 bool is_hw_crypto_enabled;
320 #define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
321 #define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
323 struct tx_traffic_info {
324 u32 start_time;
325 u32 pkts;
328 #define MWL8K_MAX_TID 8
329 struct mwl8k_sta {
330 /* Index into station database. Returned by UPDATE_STADB. */
331 u8 peer_id;
332 u8 is_ampdu_allowed;
333 struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
335 #define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
337 static const struct ieee80211_channel mwl8k_channels_24[] = {
338 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2412, .hw_value = 1, },
339 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2417, .hw_value = 2, },
340 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2422, .hw_value = 3, },
341 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2427, .hw_value = 4, },
342 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2432, .hw_value = 5, },
343 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2437, .hw_value = 6, },
344 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2442, .hw_value = 7, },
345 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2447, .hw_value = 8, },
346 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2452, .hw_value = 9, },
347 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2457, .hw_value = 10, },
348 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2462, .hw_value = 11, },
349 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2467, .hw_value = 12, },
350 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2472, .hw_value = 13, },
351 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2484, .hw_value = 14, },
354 static const struct ieee80211_rate mwl8k_rates_24[] = {
355 { .bitrate = 10, .hw_value = 2, },
356 { .bitrate = 20, .hw_value = 4, },
357 { .bitrate = 55, .hw_value = 11, },
358 { .bitrate = 110, .hw_value = 22, },
359 { .bitrate = 220, .hw_value = 44, },
360 { .bitrate = 60, .hw_value = 12, },
361 { .bitrate = 90, .hw_value = 18, },
362 { .bitrate = 120, .hw_value = 24, },
363 { .bitrate = 180, .hw_value = 36, },
364 { .bitrate = 240, .hw_value = 48, },
365 { .bitrate = 360, .hw_value = 72, },
366 { .bitrate = 480, .hw_value = 96, },
367 { .bitrate = 540, .hw_value = 108, },
370 static const struct ieee80211_channel mwl8k_channels_50[] = {
371 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
372 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
373 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
374 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
377 static const struct ieee80211_rate mwl8k_rates_50[] = {
378 { .bitrate = 60, .hw_value = 12, },
379 { .bitrate = 90, .hw_value = 18, },
380 { .bitrate = 120, .hw_value = 24, },
381 { .bitrate = 180, .hw_value = 36, },
382 { .bitrate = 240, .hw_value = 48, },
383 { .bitrate = 360, .hw_value = 72, },
384 { .bitrate = 480, .hw_value = 96, },
385 { .bitrate = 540, .hw_value = 108, },
388 /* Set or get info from Firmware */
389 #define MWL8K_CMD_GET 0x0000
390 #define MWL8K_CMD_SET 0x0001
391 #define MWL8K_CMD_SET_LIST 0x0002
393 /* Firmware command codes */
394 #define MWL8K_CMD_CODE_DNLD 0x0001
395 #define MWL8K_CMD_GET_HW_SPEC 0x0003
396 #define MWL8K_CMD_SET_HW_SPEC 0x0004
397 #define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
398 #define MWL8K_CMD_GET_STAT 0x0014
399 #define MWL8K_CMD_RADIO_CONTROL 0x001c
400 #define MWL8K_CMD_RF_TX_POWER 0x001e
401 #define MWL8K_CMD_TX_POWER 0x001f
402 #define MWL8K_CMD_RF_ANTENNA 0x0020
403 #define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
404 #define MWL8K_CMD_SET_PRE_SCAN 0x0107
405 #define MWL8K_CMD_SET_POST_SCAN 0x0108
406 #define MWL8K_CMD_SET_RF_CHANNEL 0x010a
407 #define MWL8K_CMD_SET_AID 0x010d
408 #define MWL8K_CMD_SET_RATE 0x0110
409 #define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
410 #define MWL8K_CMD_RTS_THRESHOLD 0x0113
411 #define MWL8K_CMD_SET_SLOT 0x0114
412 #define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
413 #define MWL8K_CMD_SET_WMM_MODE 0x0123
414 #define MWL8K_CMD_MIMO_CONFIG 0x0125
415 #define MWL8K_CMD_USE_FIXED_RATE 0x0126
416 #define MWL8K_CMD_ENABLE_SNIFFER 0x0150
417 #define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
418 #define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
419 #define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
420 #define MWL8K_CMD_DEL_MAC_ADDR 0x0206 /* per-vif */
421 #define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
422 #define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
423 #define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
424 #define MWL8K_CMD_UPDATE_STADB 0x1123
425 #define MWL8K_CMD_BASTREAM 0x1125
427 static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
429 u16 command = le16_to_cpu(cmd);
431 #define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
432 snprintf(buf, bufsize, "%s", #x);\
433 return buf;\
434 } while (0)
435 switch (command & ~0x8000) {
436 MWL8K_CMDNAME(CODE_DNLD);
437 MWL8K_CMDNAME(GET_HW_SPEC);
438 MWL8K_CMDNAME(SET_HW_SPEC);
439 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
440 MWL8K_CMDNAME(GET_STAT);
441 MWL8K_CMDNAME(RADIO_CONTROL);
442 MWL8K_CMDNAME(RF_TX_POWER);
443 MWL8K_CMDNAME(TX_POWER);
444 MWL8K_CMDNAME(RF_ANTENNA);
445 MWL8K_CMDNAME(SET_BEACON);
446 MWL8K_CMDNAME(SET_PRE_SCAN);
447 MWL8K_CMDNAME(SET_POST_SCAN);
448 MWL8K_CMDNAME(SET_RF_CHANNEL);
449 MWL8K_CMDNAME(SET_AID);
450 MWL8K_CMDNAME(SET_RATE);
451 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
452 MWL8K_CMDNAME(RTS_THRESHOLD);
453 MWL8K_CMDNAME(SET_SLOT);
454 MWL8K_CMDNAME(SET_EDCA_PARAMS);
455 MWL8K_CMDNAME(SET_WMM_MODE);
456 MWL8K_CMDNAME(MIMO_CONFIG);
457 MWL8K_CMDNAME(USE_FIXED_RATE);
458 MWL8K_CMDNAME(ENABLE_SNIFFER);
459 MWL8K_CMDNAME(SET_MAC_ADDR);
460 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
461 MWL8K_CMDNAME(BSS_START);
462 MWL8K_CMDNAME(SET_NEW_STN);
463 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
464 MWL8K_CMDNAME(UPDATE_STADB);
465 MWL8K_CMDNAME(BASTREAM);
466 MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
467 default:
468 snprintf(buf, bufsize, "0x%x", cmd);
470 #undef MWL8K_CMDNAME
472 return buf;
475 /* Hardware and firmware reset */
476 static void mwl8k_hw_reset(struct mwl8k_priv *priv)
478 iowrite32(MWL8K_H2A_INT_RESET,
479 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
480 iowrite32(MWL8K_H2A_INT_RESET,
481 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
482 msleep(20);
485 /* Release fw image */
486 static void mwl8k_release_fw(const struct firmware **fw)
488 if (*fw == NULL)
489 return;
490 release_firmware(*fw);
491 *fw = NULL;
494 static void mwl8k_release_firmware(struct mwl8k_priv *priv)
496 mwl8k_release_fw(&priv->fw_ucode);
497 mwl8k_release_fw(&priv->fw_helper);
500 /* states for asynchronous f/w loading */
501 static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
502 enum {
503 FW_STATE_INIT = 0,
504 FW_STATE_LOADING_PREF,
505 FW_STATE_LOADING_ALT,
506 FW_STATE_ERROR,
509 /* Request fw image */
510 static int mwl8k_request_fw(struct mwl8k_priv *priv,
511 const char *fname, const struct firmware **fw,
512 bool nowait)
514 /* release current image */
515 if (*fw != NULL)
516 mwl8k_release_fw(fw);
518 if (nowait)
519 return request_firmware_nowait(THIS_MODULE, 1, fname,
520 &priv->pdev->dev, GFP_KERNEL,
521 priv, mwl8k_fw_state_machine);
522 else
523 return request_firmware(fw, fname, &priv->pdev->dev);
526 static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
527 bool nowait)
529 struct mwl8k_device_info *di = priv->device_info;
530 int rc;
532 if (di->helper_image != NULL) {
533 if (nowait)
534 rc = mwl8k_request_fw(priv, di->helper_image,
535 &priv->fw_helper, true);
536 else
537 rc = mwl8k_request_fw(priv, di->helper_image,
538 &priv->fw_helper, false);
539 if (rc)
540 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
541 pci_name(priv->pdev), di->helper_image);
543 if (rc || nowait)
544 return rc;
547 if (nowait) {
549 * if we get here, no helper image is needed. Skip the
550 * FW_STATE_INIT state.
552 priv->fw_state = FW_STATE_LOADING_PREF;
553 rc = mwl8k_request_fw(priv, fw_image,
554 &priv->fw_ucode,
555 true);
556 } else
557 rc = mwl8k_request_fw(priv, fw_image,
558 &priv->fw_ucode, false);
559 if (rc) {
560 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
561 pci_name(priv->pdev), fw_image);
562 mwl8k_release_fw(&priv->fw_helper);
563 return rc;
566 return 0;
569 struct mwl8k_cmd_pkt {
570 __le16 code;
571 __le16 length;
572 __u8 seq_num;
573 __u8 macid;
574 __le16 result;
575 char payload[0];
576 } __packed;
579 * Firmware loading.
581 static int
582 mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
584 void __iomem *regs = priv->regs;
585 dma_addr_t dma_addr;
586 int loops;
588 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
589 if (pci_dma_mapping_error(priv->pdev, dma_addr))
590 return -ENOMEM;
592 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
593 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
594 iowrite32(MWL8K_H2A_INT_DOORBELL,
595 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
596 iowrite32(MWL8K_H2A_INT_DUMMY,
597 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
599 loops = 1000;
600 do {
601 u32 int_code;
602 if (priv->is_8764) {
603 int_code = ioread32(regs +
604 MWL8K_HIU_H2A_INTERRUPT_STATUS);
605 if (int_code == 0)
606 break;
607 } else {
608 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
609 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
610 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
611 break;
614 cond_resched();
615 udelay(1);
616 } while (--loops);
618 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
620 return loops ? 0 : -ETIMEDOUT;
623 static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
624 const u8 *data, size_t length)
626 struct mwl8k_cmd_pkt *cmd;
627 int done;
628 int rc = 0;
630 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
631 if (cmd == NULL)
632 return -ENOMEM;
634 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
635 cmd->seq_num = 0;
636 cmd->macid = 0;
637 cmd->result = 0;
639 done = 0;
640 while (length) {
641 int block_size = length > 256 ? 256 : length;
643 memcpy(cmd->payload, data + done, block_size);
644 cmd->length = cpu_to_le16(block_size);
646 rc = mwl8k_send_fw_load_cmd(priv, cmd,
647 sizeof(*cmd) + block_size);
648 if (rc)
649 break;
651 done += block_size;
652 length -= block_size;
655 if (!rc) {
656 cmd->length = 0;
657 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
660 kfree(cmd);
662 return rc;
665 static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
666 const u8 *data, size_t length)
668 unsigned char *buffer;
669 int may_continue, rc = 0;
670 u32 done, prev_block_size;
672 buffer = kmalloc(1024, GFP_KERNEL);
673 if (buffer == NULL)
674 return -ENOMEM;
676 done = 0;
677 prev_block_size = 0;
678 may_continue = 1000;
679 while (may_continue > 0) {
680 u32 block_size;
682 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
683 if (block_size & 1) {
684 block_size &= ~1;
685 may_continue--;
686 } else {
687 done += prev_block_size;
688 length -= prev_block_size;
691 if (block_size > 1024 || block_size > length) {
692 rc = -EOVERFLOW;
693 break;
696 if (length == 0) {
697 rc = 0;
698 break;
701 if (block_size == 0) {
702 rc = -EPROTO;
703 may_continue--;
704 udelay(1);
705 continue;
708 prev_block_size = block_size;
709 memcpy(buffer, data + done, block_size);
711 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
712 if (rc)
713 break;
716 if (!rc && length != 0)
717 rc = -EREMOTEIO;
719 kfree(buffer);
721 return rc;
724 static int mwl8k_load_firmware(struct ieee80211_hw *hw)
726 struct mwl8k_priv *priv = hw->priv;
727 const struct firmware *fw = priv->fw_ucode;
728 int rc;
729 int loops;
731 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
732 const struct firmware *helper = priv->fw_helper;
734 if (helper == NULL) {
735 printk(KERN_ERR "%s: helper image needed but none "
736 "given\n", pci_name(priv->pdev));
737 return -EINVAL;
740 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
741 if (rc) {
742 printk(KERN_ERR "%s: unable to load firmware "
743 "helper image\n", pci_name(priv->pdev));
744 return rc;
746 msleep(20);
748 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
749 } else {
750 if (priv->is_8764)
751 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
752 else
753 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
756 if (rc) {
757 printk(KERN_ERR "%s: unable to load firmware image\n",
758 pci_name(priv->pdev));
759 return rc;
762 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
764 loops = 500000;
765 do {
766 u32 ready_code;
768 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
769 if (ready_code == MWL8K_FWAP_READY) {
770 priv->ap_fw = true;
771 break;
772 } else if (ready_code == MWL8K_FWSTA_READY) {
773 priv->ap_fw = false;
774 break;
777 cond_resched();
778 udelay(1);
779 } while (--loops);
781 return loops ? 0 : -ETIMEDOUT;
785 /* DMA header used by firmware and hardware. */
786 struct mwl8k_dma_data {
787 __le16 fwlen;
788 struct ieee80211_hdr wh;
789 char data[0];
790 } __packed;
792 /* Routines to add/remove DMA header from skb. */
793 static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
795 struct mwl8k_dma_data *tr;
796 int hdrlen;
798 tr = (struct mwl8k_dma_data *)skb->data;
799 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
801 if (hdrlen != sizeof(tr->wh)) {
802 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
803 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
804 *((__le16 *)(tr->data - 2)) = qos;
805 } else {
806 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
810 if (hdrlen != sizeof(*tr))
811 skb_pull(skb, sizeof(*tr) - hdrlen);
814 #define REDUCED_TX_HEADROOM 8
816 static void
817 mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
818 int head_pad, int tail_pad)
820 struct ieee80211_hdr *wh;
821 int hdrlen;
822 int reqd_hdrlen;
823 struct mwl8k_dma_data *tr;
826 * Add a firmware DMA header; the firmware requires that we
827 * present a 2-byte payload length followed by a 4-address
828 * header (without QoS field), followed (optionally) by any
829 * WEP/ExtIV header (but only filled in for CCMP).
831 wh = (struct ieee80211_hdr *)skb->data;
833 hdrlen = ieee80211_hdrlen(wh->frame_control);
836 * Check if skb_resize is required because of
837 * tx_headroom adjustment.
839 if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
840 + REDUCED_TX_HEADROOM))) {
841 if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
843 wiphy_err(priv->hw->wiphy,
844 "Failed to reallocate TX buffer\n");
845 return;
847 skb->truesize += REDUCED_TX_HEADROOM;
850 reqd_hdrlen = sizeof(*tr) + head_pad;
852 if (hdrlen != reqd_hdrlen)
853 skb_push(skb, reqd_hdrlen - hdrlen);
855 if (ieee80211_is_data_qos(wh->frame_control))
856 hdrlen -= IEEE80211_QOS_CTL_LEN;
858 tr = (struct mwl8k_dma_data *)skb->data;
859 if (wh != &tr->wh)
860 memmove(&tr->wh, wh, hdrlen);
861 if (hdrlen != sizeof(tr->wh))
862 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
865 * Firmware length is the length of the fully formed "802.11
866 * payload". That is, everything except for the 802.11 header.
867 * This includes all crypto material including the MIC.
869 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
872 static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
873 struct sk_buff *skb)
875 struct ieee80211_hdr *wh;
876 struct ieee80211_tx_info *tx_info;
877 struct ieee80211_key_conf *key_conf;
878 int data_pad;
879 int head_pad = 0;
881 wh = (struct ieee80211_hdr *)skb->data;
883 tx_info = IEEE80211_SKB_CB(skb);
885 key_conf = NULL;
886 if (ieee80211_is_data(wh->frame_control))
887 key_conf = tx_info->control.hw_key;
890 * Make sure the packet header is in the DMA header format (4-address
891 * without QoS), and add head & tail padding when HW crypto is enabled.
893 * We have the following trailer padding requirements:
894 * - WEP: 4 trailer bytes (ICV)
895 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
896 * - CCMP: 8 trailer bytes (MIC)
898 data_pad = 0;
899 if (key_conf != NULL) {
900 head_pad = key_conf->iv_len;
901 switch (key_conf->cipher) {
902 case WLAN_CIPHER_SUITE_WEP40:
903 case WLAN_CIPHER_SUITE_WEP104:
904 data_pad = 4;
905 break;
906 case WLAN_CIPHER_SUITE_TKIP:
907 data_pad = 12;
908 break;
909 case WLAN_CIPHER_SUITE_CCMP:
910 data_pad = 8;
911 break;
914 mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
918 * Packet reception for 88w8366/88w8764 AP firmware.
920 struct mwl8k_rxd_ap {
921 __le16 pkt_len;
922 __u8 sq2;
923 __u8 rate;
924 __le32 pkt_phys_addr;
925 __le32 next_rxd_phys_addr;
926 __le16 qos_control;
927 __le16 htsig2;
928 __le32 hw_rssi_info;
929 __le32 hw_noise_floor_info;
930 __u8 noise_floor;
931 __u8 pad0[3];
932 __u8 rssi;
933 __u8 rx_status;
934 __u8 channel;
935 __u8 rx_ctrl;
936 } __packed;
938 #define MWL8K_AP_RATE_INFO_MCS_FORMAT 0x80
939 #define MWL8K_AP_RATE_INFO_40MHZ 0x40
940 #define MWL8K_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
942 #define MWL8K_AP_RX_CTRL_OWNED_BY_HOST 0x80
944 /* 8366/8764 AP rx_status bits */
945 #define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
946 #define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
947 #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
948 #define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
949 #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
951 static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
953 struct mwl8k_rxd_ap *rxd = _rxd;
955 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
956 rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
959 static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
961 struct mwl8k_rxd_ap *rxd = _rxd;
963 rxd->pkt_len = cpu_to_le16(len);
964 rxd->pkt_phys_addr = cpu_to_le32(addr);
965 wmb();
966 rxd->rx_ctrl = 0;
969 static int
970 mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
971 __le16 *qos, s8 *noise)
973 struct mwl8k_rxd_ap *rxd = _rxd;
975 if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
976 return -1;
977 rmb();
979 memset(status, 0, sizeof(*status));
981 status->signal = -rxd->rssi;
982 *noise = -rxd->noise_floor;
984 if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
985 status->flag |= RX_FLAG_HT;
986 if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
987 status->flag |= RX_FLAG_40MHZ;
988 status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
989 } else {
990 int i;
992 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
993 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
994 status->rate_idx = i;
995 break;
1000 if (rxd->channel > 14) {
1001 status->band = IEEE80211_BAND_5GHZ;
1002 if (!(status->flag & RX_FLAG_HT))
1003 status->rate_idx -= 5;
1004 } else {
1005 status->band = IEEE80211_BAND_2GHZ;
1007 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1008 status->band);
1010 *qos = rxd->qos_control;
1012 if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
1013 (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
1014 (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
1015 status->flag |= RX_FLAG_MMIC_ERROR;
1017 return le16_to_cpu(rxd->pkt_len);
1020 static struct rxd_ops rxd_ap_ops = {
1021 .rxd_size = sizeof(struct mwl8k_rxd_ap),
1022 .rxd_init = mwl8k_rxd_ap_init,
1023 .rxd_refill = mwl8k_rxd_ap_refill,
1024 .rxd_process = mwl8k_rxd_ap_process,
1028 * Packet reception for STA firmware.
1030 struct mwl8k_rxd_sta {
1031 __le16 pkt_len;
1032 __u8 link_quality;
1033 __u8 noise_level;
1034 __le32 pkt_phys_addr;
1035 __le32 next_rxd_phys_addr;
1036 __le16 qos_control;
1037 __le16 rate_info;
1038 __le32 pad0[4];
1039 __u8 rssi;
1040 __u8 channel;
1041 __le16 pad1;
1042 __u8 rx_ctrl;
1043 __u8 rx_status;
1044 __u8 pad2[2];
1045 } __packed;
1047 #define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
1048 #define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
1049 #define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
1050 #define MWL8K_STA_RATE_INFO_40MHZ 0x0004
1051 #define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
1052 #define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
1054 #define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
1055 #define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
1056 /* ICV=0 or MIC=1 */
1057 #define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
1058 /* Key is uploaded only in failure case */
1059 #define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
1061 static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
1063 struct mwl8k_rxd_sta *rxd = _rxd;
1065 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
1066 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
1069 static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
1071 struct mwl8k_rxd_sta *rxd = _rxd;
1073 rxd->pkt_len = cpu_to_le16(len);
1074 rxd->pkt_phys_addr = cpu_to_le32(addr);
1075 wmb();
1076 rxd->rx_ctrl = 0;
1079 static int
1080 mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
1081 __le16 *qos, s8 *noise)
1083 struct mwl8k_rxd_sta *rxd = _rxd;
1084 u16 rate_info;
1086 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
1087 return -1;
1088 rmb();
1090 rate_info = le16_to_cpu(rxd->rate_info);
1092 memset(status, 0, sizeof(*status));
1094 status->signal = -rxd->rssi;
1095 *noise = -rxd->noise_level;
1096 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1097 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
1099 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
1100 status->flag |= RX_FLAG_SHORTPRE;
1101 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
1102 status->flag |= RX_FLAG_40MHZ;
1103 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
1104 status->flag |= RX_FLAG_SHORT_GI;
1105 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
1106 status->flag |= RX_FLAG_HT;
1108 if (rxd->channel > 14) {
1109 status->band = IEEE80211_BAND_5GHZ;
1110 if (!(status->flag & RX_FLAG_HT))
1111 status->rate_idx -= 5;
1112 } else {
1113 status->band = IEEE80211_BAND_2GHZ;
1115 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1116 status->band);
1118 *qos = rxd->qos_control;
1119 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1120 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1121 status->flag |= RX_FLAG_MMIC_ERROR;
1123 return le16_to_cpu(rxd->pkt_len);
1126 static struct rxd_ops rxd_sta_ops = {
1127 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1128 .rxd_init = mwl8k_rxd_sta_init,
1129 .rxd_refill = mwl8k_rxd_sta_refill,
1130 .rxd_process = mwl8k_rxd_sta_process,
1134 #define MWL8K_RX_DESCS 256
1135 #define MWL8K_RX_MAXSZ 3800
1137 static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1139 struct mwl8k_priv *priv = hw->priv;
1140 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1141 int size;
1142 int i;
1144 rxq->rxd_count = 0;
1145 rxq->head = 0;
1146 rxq->tail = 0;
1148 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
1150 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1151 if (rxq->rxd == NULL) {
1152 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
1153 return -ENOMEM;
1155 memset(rxq->rxd, 0, size);
1157 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
1158 if (rxq->buf == NULL) {
1159 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
1160 return -ENOMEM;
1163 for (i = 0; i < MWL8K_RX_DESCS; i++) {
1164 int desc_size;
1165 void *rxd;
1166 int nexti;
1167 dma_addr_t next_dma_addr;
1169 desc_size = priv->rxd_ops->rxd_size;
1170 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
1172 nexti = i + 1;
1173 if (nexti == MWL8K_RX_DESCS)
1174 nexti = 0;
1175 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1177 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
1180 return 0;
1183 static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1185 struct mwl8k_priv *priv = hw->priv;
1186 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1187 int refilled;
1189 refilled = 0;
1190 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
1191 struct sk_buff *skb;
1192 dma_addr_t addr;
1193 int rx;
1194 void *rxd;
1196 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1197 if (skb == NULL)
1198 break;
1200 addr = pci_map_single(priv->pdev, skb->data,
1201 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
1203 rxq->rxd_count++;
1204 rx = rxq->tail++;
1205 if (rxq->tail == MWL8K_RX_DESCS)
1206 rxq->tail = 0;
1207 rxq->buf[rx].skb = skb;
1208 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
1210 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1211 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
1213 refilled++;
1216 return refilled;
1219 /* Must be called only when the card's reception is completely halted */
1220 static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1222 struct mwl8k_priv *priv = hw->priv;
1223 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1224 int i;
1226 if (rxq->rxd == NULL)
1227 return;
1229 for (i = 0; i < MWL8K_RX_DESCS; i++) {
1230 if (rxq->buf[i].skb != NULL) {
1231 pci_unmap_single(priv->pdev,
1232 dma_unmap_addr(&rxq->buf[i], dma),
1233 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1234 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
1236 kfree_skb(rxq->buf[i].skb);
1237 rxq->buf[i].skb = NULL;
1241 kfree(rxq->buf);
1242 rxq->buf = NULL;
1244 pci_free_consistent(priv->pdev,
1245 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
1246 rxq->rxd, rxq->rxd_dma);
1247 rxq->rxd = NULL;
1252 * Scan a list of BSSIDs to process for finalize join.
1253 * Allows for extension to process multiple BSSIDs.
1255 static inline int
1256 mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1258 return priv->capture_beacon &&
1259 ieee80211_is_beacon(wh->frame_control) &&
1260 ether_addr_equal_64bits(wh->addr3, priv->capture_bssid);
1263 static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1264 struct sk_buff *skb)
1266 struct mwl8k_priv *priv = hw->priv;
1268 priv->capture_beacon = false;
1269 memset(priv->capture_bssid, 0, ETH_ALEN);
1272 * Use GFP_ATOMIC as rxq_process is called from
1273 * the primary interrupt handler, memory allocation call
1274 * must not sleep.
1276 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1277 if (priv->beacon_skb != NULL)
1278 ieee80211_queue_work(hw, &priv->finalize_join_worker);
1281 static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1282 u8 *bssid)
1284 struct mwl8k_vif *mwl8k_vif;
1286 list_for_each_entry(mwl8k_vif,
1287 vif_list, list) {
1288 if (memcmp(bssid, mwl8k_vif->bssid,
1289 ETH_ALEN) == 0)
1290 return mwl8k_vif;
1293 return NULL;
1296 static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1298 struct mwl8k_priv *priv = hw->priv;
1299 struct mwl8k_vif *mwl8k_vif = NULL;
1300 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1301 int processed;
1303 processed = 0;
1304 while (rxq->rxd_count && limit--) {
1305 struct sk_buff *skb;
1306 void *rxd;
1307 int pkt_len;
1308 struct ieee80211_rx_status status;
1309 struct ieee80211_hdr *wh;
1310 __le16 qos;
1312 skb = rxq->buf[rxq->head].skb;
1313 if (skb == NULL)
1314 break;
1316 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1318 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1319 &priv->noise);
1320 if (pkt_len < 0)
1321 break;
1323 rxq->buf[rxq->head].skb = NULL;
1325 pci_unmap_single(priv->pdev,
1326 dma_unmap_addr(&rxq->buf[rxq->head], dma),
1327 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1328 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
1330 rxq->head++;
1331 if (rxq->head == MWL8K_RX_DESCS)
1332 rxq->head = 0;
1334 rxq->rxd_count--;
1336 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1339 * Check for a pending join operation. Save a
1340 * copy of the beacon and schedule a tasklet to
1341 * send a FINALIZE_JOIN command to the firmware.
1343 if (mwl8k_capture_bssid(priv, (void *)skb->data))
1344 mwl8k_save_beacon(hw, skb);
1346 if (ieee80211_has_protected(wh->frame_control)) {
1348 /* Check if hw crypto has been enabled for
1349 * this bss. If yes, set the status flags
1350 * accordingly
1352 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1353 wh->addr1);
1355 if (mwl8k_vif != NULL &&
1356 mwl8k_vif->is_hw_crypto_enabled) {
1358 * When MMIC ERROR is encountered
1359 * by the firmware, payload is
1360 * dropped and only 32 bytes of
1361 * mwl8k Firmware header is sent
1362 * to the host.
1364 * We need to add four bytes of
1365 * key information. In it
1366 * MAC80211 expects keyidx set to
1367 * 0 for triggering Counter
1368 * Measure of MMIC failure.
1370 if (status.flag & RX_FLAG_MMIC_ERROR) {
1371 struct mwl8k_dma_data *tr;
1372 tr = (struct mwl8k_dma_data *)skb->data;
1373 memset((void *)&(tr->data), 0, 4);
1374 pkt_len += 4;
1377 if (!ieee80211_is_auth(wh->frame_control))
1378 status.flag |= RX_FLAG_IV_STRIPPED |
1379 RX_FLAG_DECRYPTED |
1380 RX_FLAG_MMIC_STRIPPED;
1384 skb_put(skb, pkt_len);
1385 mwl8k_remove_dma_header(skb, qos);
1386 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1387 ieee80211_rx_irqsafe(hw, skb);
1389 processed++;
1392 return processed;
1397 * Packet transmission.
1400 #define MWL8K_TXD_STATUS_OK 0x00000001
1401 #define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1402 #define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1403 #define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
1404 #define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
1406 #define MWL8K_QOS_QLEN_UNSPEC 0xff00
1407 #define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1408 #define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1409 #define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1410 #define MWL8K_QOS_EOSP 0x0010
1412 struct mwl8k_tx_desc {
1413 __le32 status;
1414 __u8 data_rate;
1415 __u8 tx_priority;
1416 __le16 qos_control;
1417 __le32 pkt_phys_addr;
1418 __le16 pkt_len;
1419 __u8 dest_MAC_addr[ETH_ALEN];
1420 __le32 next_txd_phys_addr;
1421 __le32 timestamp;
1422 __le16 rate_info;
1423 __u8 peer_id;
1424 __u8 tx_frag_cnt;
1425 } __packed;
1427 #define MWL8K_TX_DESCS 128
1429 static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1431 struct mwl8k_priv *priv = hw->priv;
1432 struct mwl8k_tx_queue *txq = priv->txq + index;
1433 int size;
1434 int i;
1436 txq->len = 0;
1437 txq->head = 0;
1438 txq->tail = 0;
1440 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1442 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1443 if (txq->txd == NULL) {
1444 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
1445 return -ENOMEM;
1447 memset(txq->txd, 0, size);
1449 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
1450 if (txq->skb == NULL) {
1451 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
1452 return -ENOMEM;
1455 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1456 struct mwl8k_tx_desc *tx_desc;
1457 int nexti;
1459 tx_desc = txq->txd + i;
1460 nexti = (i + 1) % MWL8K_TX_DESCS;
1462 tx_desc->status = 0;
1463 tx_desc->next_txd_phys_addr =
1464 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
1467 return 0;
1470 static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1472 iowrite32(MWL8K_H2A_INT_PPA_READY,
1473 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1474 iowrite32(MWL8K_H2A_INT_DUMMY,
1475 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1476 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1479 static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
1481 struct mwl8k_priv *priv = hw->priv;
1482 int i;
1484 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
1485 struct mwl8k_tx_queue *txq = priv->txq + i;
1486 int fw_owned = 0;
1487 int drv_owned = 0;
1488 int unused = 0;
1489 int desc;
1491 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
1492 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1493 u32 status;
1495 status = le32_to_cpu(tx_desc->status);
1496 if (status & MWL8K_TXD_STATUS_FW_OWNED)
1497 fw_owned++;
1498 else
1499 drv_owned++;
1501 if (tx_desc->pkt_len == 0)
1502 unused++;
1505 wiphy_err(hw->wiphy,
1506 "txq[%d] len=%d head=%d tail=%d "
1507 "fw_owned=%d drv_owned=%d unused=%d\n",
1509 txq->len, txq->head, txq->tail,
1510 fw_owned, drv_owned, unused);
1515 * Must be called with priv->fw_mutex held and tx queues stopped.
1517 #define MWL8K_TX_WAIT_TIMEOUT_MS 5000
1519 static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
1521 struct mwl8k_priv *priv = hw->priv;
1522 DECLARE_COMPLETION_ONSTACK(tx_wait);
1523 int retry;
1524 int rc;
1526 might_sleep();
1528 /* Since fw restart is in progress, allow only the firmware
1529 * commands from the restart code and block the other
1530 * commands since they are going to fail in any case since
1531 * the firmware has crashed
1533 if (priv->hw_restart_in_progress) {
1534 if (priv->hw_restart_owner == current)
1535 return 0;
1536 else
1537 return -EBUSY;
1540 if (atomic_read(&priv->watchdog_event_pending))
1541 return 0;
1544 * The TX queues are stopped at this point, so this test
1545 * doesn't need to take ->tx_lock.
1547 if (!priv->pending_tx_pkts)
1548 return 0;
1550 retry = 1;
1551 rc = 0;
1553 spin_lock_bh(&priv->tx_lock);
1554 priv->tx_wait = &tx_wait;
1555 while (!rc) {
1556 int oldcount;
1557 unsigned long timeout;
1559 oldcount = priv->pending_tx_pkts;
1561 spin_unlock_bh(&priv->tx_lock);
1562 timeout = wait_for_completion_timeout(&tx_wait,
1563 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1565 if (atomic_read(&priv->watchdog_event_pending)) {
1566 spin_lock_bh(&priv->tx_lock);
1567 priv->tx_wait = NULL;
1568 spin_unlock_bh(&priv->tx_lock);
1569 return 0;
1572 spin_lock_bh(&priv->tx_lock);
1574 if (timeout || !priv->pending_tx_pkts) {
1575 WARN_ON(priv->pending_tx_pkts);
1576 if (retry)
1577 wiphy_notice(hw->wiphy, "tx rings drained\n");
1578 break;
1581 if (retry) {
1582 mwl8k_tx_start(priv);
1583 retry = 0;
1584 continue;
1587 if (priv->pending_tx_pkts < oldcount) {
1588 wiphy_notice(hw->wiphy,
1589 "waiting for tx rings to drain (%d -> %d pkts)\n",
1590 oldcount, priv->pending_tx_pkts);
1591 retry = 1;
1592 continue;
1595 priv->tx_wait = NULL;
1597 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1598 MWL8K_TX_WAIT_TIMEOUT_MS);
1599 mwl8k_dump_tx_rings(hw);
1600 priv->hw_restart_in_progress = true;
1601 ieee80211_queue_work(hw, &priv->fw_reload);
1603 rc = -ETIMEDOUT;
1605 priv->tx_wait = NULL;
1606 spin_unlock_bh(&priv->tx_lock);
1608 return rc;
1611 #define MWL8K_TXD_SUCCESS(status) \
1612 ((status) & (MWL8K_TXD_STATUS_OK | \
1613 MWL8K_TXD_STATUS_OK_RETRY | \
1614 MWL8K_TXD_STATUS_OK_MORE_RETRY))
1616 static int mwl8k_tid_queue_mapping(u8 tid)
1618 BUG_ON(tid > 7);
1620 switch (tid) {
1621 case 0:
1622 case 3:
1623 return IEEE80211_AC_BE;
1624 break;
1625 case 1:
1626 case 2:
1627 return IEEE80211_AC_BK;
1628 break;
1629 case 4:
1630 case 5:
1631 return IEEE80211_AC_VI;
1632 break;
1633 case 6:
1634 case 7:
1635 return IEEE80211_AC_VO;
1636 break;
1637 default:
1638 return -1;
1639 break;
1643 /* The firmware will fill in the rate information
1644 * for each packet that gets queued in the hardware
1645 * and these macros will interpret that info.
1648 #define RI_FORMAT(a) (a & 0x0001)
1649 #define RI_RATE_ID_MCS(a) ((a & 0x01f8) >> 3)
1651 static int
1652 mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
1654 struct mwl8k_priv *priv = hw->priv;
1655 struct mwl8k_tx_queue *txq = priv->txq + index;
1656 int processed;
1658 processed = 0;
1659 while (txq->len > 0 && limit--) {
1660 int tx;
1661 struct mwl8k_tx_desc *tx_desc;
1662 unsigned long addr;
1663 int size;
1664 struct sk_buff *skb;
1665 struct ieee80211_tx_info *info;
1666 u32 status;
1667 struct ieee80211_sta *sta;
1668 struct mwl8k_sta *sta_info = NULL;
1669 u16 rate_info;
1670 struct ieee80211_hdr *wh;
1672 tx = txq->head;
1673 tx_desc = txq->txd + tx;
1675 status = le32_to_cpu(tx_desc->status);
1677 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1678 if (!force)
1679 break;
1680 tx_desc->status &=
1681 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1684 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1685 BUG_ON(txq->len == 0);
1686 txq->len--;
1687 priv->pending_tx_pkts--;
1689 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
1690 size = le16_to_cpu(tx_desc->pkt_len);
1691 skb = txq->skb[tx];
1692 txq->skb[tx] = NULL;
1694 BUG_ON(skb == NULL);
1695 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1697 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
1699 wh = (struct ieee80211_hdr *) skb->data;
1701 /* Mark descriptor as unused */
1702 tx_desc->pkt_phys_addr = 0;
1703 tx_desc->pkt_len = 0;
1705 info = IEEE80211_SKB_CB(skb);
1706 if (ieee80211_is_data(wh->frame_control)) {
1707 rcu_read_lock();
1708 sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
1709 wh->addr2);
1710 if (sta) {
1711 sta_info = MWL8K_STA(sta);
1712 BUG_ON(sta_info == NULL);
1713 rate_info = le16_to_cpu(tx_desc->rate_info);
1714 /* If rate is < 6.5 Mpbs for an ht station
1715 * do not form an ampdu. If the station is a
1716 * legacy station (format = 0), do not form an
1717 * ampdu
1719 if (RI_RATE_ID_MCS(rate_info) < 1 ||
1720 RI_FORMAT(rate_info) == 0) {
1721 sta_info->is_ampdu_allowed = false;
1722 } else {
1723 sta_info->is_ampdu_allowed = true;
1726 rcu_read_unlock();
1729 ieee80211_tx_info_clear_status(info);
1731 /* Rate control is happening in the firmware.
1732 * Ensure no tx rate is being reported.
1734 info->status.rates[0].idx = -1;
1735 info->status.rates[0].count = 1;
1737 if (MWL8K_TXD_SUCCESS(status))
1738 info->flags |= IEEE80211_TX_STAT_ACK;
1740 ieee80211_tx_status_irqsafe(hw, skb);
1742 processed++;
1745 return processed;
1748 /* must be called only when the card's transmit is completely halted */
1749 static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1751 struct mwl8k_priv *priv = hw->priv;
1752 struct mwl8k_tx_queue *txq = priv->txq + index;
1754 if (txq->txd == NULL)
1755 return;
1757 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
1759 kfree(txq->skb);
1760 txq->skb = NULL;
1762 pci_free_consistent(priv->pdev,
1763 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
1764 txq->txd, txq->txd_dma);
1765 txq->txd = NULL;
1768 /* caller must hold priv->stream_lock when calling the stream functions */
1769 static struct mwl8k_ampdu_stream *
1770 mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1772 struct mwl8k_ampdu_stream *stream;
1773 struct mwl8k_priv *priv = hw->priv;
1774 int i;
1776 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
1777 stream = &priv->ampdu[i];
1778 if (stream->state == AMPDU_NO_STREAM) {
1779 stream->sta = sta;
1780 stream->state = AMPDU_STREAM_NEW;
1781 stream->tid = tid;
1782 stream->idx = i;
1783 wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1784 sta->addr, tid);
1785 return stream;
1788 return NULL;
1791 static int
1792 mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1794 int ret;
1796 /* if the stream has already been started, don't start it again */
1797 if (stream->state != AMPDU_STREAM_NEW)
1798 return 0;
1799 ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1800 if (ret)
1801 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1802 "%d\n", stream->sta->addr, stream->tid, ret);
1803 else
1804 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1805 stream->sta->addr, stream->tid);
1806 return ret;
1809 static void
1810 mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1812 wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1813 stream->tid);
1814 memset(stream, 0, sizeof(*stream));
1817 static struct mwl8k_ampdu_stream *
1818 mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1820 struct mwl8k_priv *priv = hw->priv;
1821 int i;
1823 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
1824 struct mwl8k_ampdu_stream *stream;
1825 stream = &priv->ampdu[i];
1826 if (stream->state == AMPDU_NO_STREAM)
1827 continue;
1828 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1829 stream->tid == tid)
1830 return stream;
1832 return NULL;
1835 #define MWL8K_AMPDU_PACKET_THRESHOLD 64
1836 static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
1838 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1839 struct tx_traffic_info *tx_stats;
1841 BUG_ON(tid >= MWL8K_MAX_TID);
1842 tx_stats = &sta_info->tx_stats[tid];
1844 return sta_info->is_ampdu_allowed &&
1845 tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
1848 static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
1850 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1851 struct tx_traffic_info *tx_stats;
1853 BUG_ON(tid >= MWL8K_MAX_TID);
1854 tx_stats = &sta_info->tx_stats[tid];
1856 if (tx_stats->start_time == 0)
1857 tx_stats->start_time = jiffies;
1859 /* reset the packet count after each second elapses. If the number of
1860 * packets ever exceeds the ampdu_min_traffic threshold, we will allow
1861 * an ampdu stream to be started.
1863 if (jiffies - tx_stats->start_time > HZ) {
1864 tx_stats->pkts = 0;
1865 tx_stats->start_time = 0;
1866 } else
1867 tx_stats->pkts++;
1870 /* The hardware ampdu queues start from 5.
1871 * txpriorities for ampdu queues are
1872 * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
1873 * and queue 3 is lowest (queue 4 is reserved)
1875 #define BA_QUEUE 5
1877 static void
1878 mwl8k_txq_xmit(struct ieee80211_hw *hw,
1879 int index,
1880 struct ieee80211_sta *sta,
1881 struct sk_buff *skb)
1883 struct mwl8k_priv *priv = hw->priv;
1884 struct ieee80211_tx_info *tx_info;
1885 struct mwl8k_vif *mwl8k_vif;
1886 struct ieee80211_hdr *wh;
1887 struct mwl8k_tx_queue *txq;
1888 struct mwl8k_tx_desc *tx;
1889 dma_addr_t dma;
1890 u32 txstatus;
1891 u8 txdatarate;
1892 u16 qos;
1893 int txpriority;
1894 u8 tid = 0;
1895 struct mwl8k_ampdu_stream *stream = NULL;
1896 bool start_ba_session = false;
1897 bool mgmtframe = false;
1898 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1899 bool eapol_frame = false;
1901 wh = (struct ieee80211_hdr *)skb->data;
1902 if (ieee80211_is_data_qos(wh->frame_control))
1903 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1904 else
1905 qos = 0;
1907 if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1908 eapol_frame = true;
1910 if (ieee80211_is_mgmt(wh->frame_control))
1911 mgmtframe = true;
1913 if (priv->ap_fw)
1914 mwl8k_encapsulate_tx_frame(priv, skb);
1915 else
1916 mwl8k_add_dma_header(priv, skb, 0, 0);
1918 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1920 tx_info = IEEE80211_SKB_CB(skb);
1921 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
1923 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1924 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1925 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1926 mwl8k_vif->seqno += 0x10;
1929 /* Setup firmware control bit fields for each frame type. */
1930 txstatus = 0;
1931 txdatarate = 0;
1932 if (ieee80211_is_mgmt(wh->frame_control) ||
1933 ieee80211_is_ctl(wh->frame_control)) {
1934 txdatarate = 0;
1935 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
1936 } else if (ieee80211_is_data(wh->frame_control)) {
1937 txdatarate = 1;
1938 if (is_multicast_ether_addr(wh->addr1))
1939 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1941 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
1942 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1943 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
1944 else
1945 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
1948 /* Queue ADDBA request in the respective data queue. While setting up
1949 * the ampdu stream, mac80211 queues further packets for that
1950 * particular ra/tid pair. However, packets piled up in the hardware
1951 * for that ra/tid pair will still go out. ADDBA request and the
1952 * related data packets going out from different queues asynchronously
1953 * will cause a shift in the receiver window which might result in
1954 * ampdu packets getting dropped at the receiver after the stream has
1955 * been setup.
1957 if (unlikely(ieee80211_is_action(wh->frame_control) &&
1958 mgmt->u.action.category == WLAN_CATEGORY_BACK &&
1959 mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
1960 priv->ap_fw)) {
1961 u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1962 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1963 index = mwl8k_tid_queue_mapping(tid);
1966 txpriority = index;
1968 if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
1969 ieee80211_is_data_qos(wh->frame_control)) {
1970 tid = qos & 0xf;
1971 mwl8k_tx_count_packet(sta, tid);
1972 spin_lock(&priv->stream_lock);
1973 stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1974 if (stream != NULL) {
1975 if (stream->state == AMPDU_STREAM_ACTIVE) {
1976 WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
1977 txpriority = (BA_QUEUE + stream->idx) %
1978 TOTAL_HW_TX_QUEUES;
1979 if (stream->idx <= 1)
1980 index = stream->idx +
1981 MWL8K_TX_WMM_QUEUES;
1983 } else if (stream->state == AMPDU_STREAM_NEW) {
1984 /* We get here if the driver sends us packets
1985 * after we've initiated a stream, but before
1986 * our ampdu_action routine has been called
1987 * with IEEE80211_AMPDU_TX_START to get the SSN
1988 * for the ADDBA request. So this packet can
1989 * go out with no risk of sequence number
1990 * mismatch. No special handling is required.
1992 } else {
1993 /* Drop packets that would go out after the
1994 * ADDBA request was sent but before the ADDBA
1995 * response is received. If we don't do this,
1996 * the recipient would probably receive it
1997 * after the ADDBA request with SSN 0. This
1998 * will cause the recipient's BA receive window
1999 * to shift, which would cause the subsequent
2000 * packets in the BA stream to be discarded.
2001 * mac80211 queues our packets for us in this
2002 * case, so this is really just a safety check.
2004 wiphy_warn(hw->wiphy,
2005 "Cannot send packet while ADDBA "
2006 "dialog is underway.\n");
2007 spin_unlock(&priv->stream_lock);
2008 dev_kfree_skb(skb);
2009 return;
2011 } else {
2012 /* Defer calling mwl8k_start_stream so that the current
2013 * skb can go out before the ADDBA request. This
2014 * prevents sequence number mismatch at the recepient
2015 * as described above.
2017 if (mwl8k_ampdu_allowed(sta, tid)) {
2018 stream = mwl8k_add_stream(hw, sta, tid);
2019 if (stream != NULL)
2020 start_ba_session = true;
2023 spin_unlock(&priv->stream_lock);
2024 } else {
2025 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
2026 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
2029 dma = pci_map_single(priv->pdev, skb->data,
2030 skb->len, PCI_DMA_TODEVICE);
2032 if (pci_dma_mapping_error(priv->pdev, dma)) {
2033 wiphy_debug(hw->wiphy,
2034 "failed to dma map skb, dropping TX frame.\n");
2035 if (start_ba_session) {
2036 spin_lock(&priv->stream_lock);
2037 mwl8k_remove_stream(hw, stream);
2038 spin_unlock(&priv->stream_lock);
2040 dev_kfree_skb(skb);
2041 return;
2044 spin_lock_bh(&priv->tx_lock);
2046 txq = priv->txq + index;
2048 /* Mgmt frames that go out frequently are probe
2049 * responses. Other mgmt frames got out relatively
2050 * infrequently. Hence reserve 2 buffers so that
2051 * other mgmt frames do not get dropped due to an
2052 * already queued probe response in one of the
2053 * reserved buffers.
2056 if (txq->len >= MWL8K_TX_DESCS - 2) {
2057 if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
2058 if (start_ba_session) {
2059 spin_lock(&priv->stream_lock);
2060 mwl8k_remove_stream(hw, stream);
2061 spin_unlock(&priv->stream_lock);
2063 mwl8k_tx_start(priv);
2064 spin_unlock_bh(&priv->tx_lock);
2065 pci_unmap_single(priv->pdev, dma, skb->len,
2066 PCI_DMA_TODEVICE);
2067 dev_kfree_skb(skb);
2068 return;
2072 BUG_ON(txq->skb[txq->tail] != NULL);
2073 txq->skb[txq->tail] = skb;
2075 tx = txq->txd + txq->tail;
2076 tx->data_rate = txdatarate;
2077 tx->tx_priority = txpriority;
2078 tx->qos_control = cpu_to_le16(qos);
2079 tx->pkt_phys_addr = cpu_to_le32(dma);
2080 tx->pkt_len = cpu_to_le16(skb->len);
2081 tx->rate_info = 0;
2082 if (!priv->ap_fw && sta != NULL)
2083 tx->peer_id = MWL8K_STA(sta)->peer_id;
2084 else
2085 tx->peer_id = 0;
2087 if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
2088 tx->timestamp = cpu_to_le32(ioread32(priv->regs +
2089 MWL8K_HW_TIMER_REGISTER));
2090 else
2091 tx->timestamp = 0;
2093 wmb();
2094 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
2096 txq->len++;
2097 priv->pending_tx_pkts++;
2099 txq->tail++;
2100 if (txq->tail == MWL8K_TX_DESCS)
2101 txq->tail = 0;
2103 mwl8k_tx_start(priv);
2105 spin_unlock_bh(&priv->tx_lock);
2107 /* Initiate the ampdu session here */
2108 if (start_ba_session) {
2109 spin_lock(&priv->stream_lock);
2110 if (mwl8k_start_stream(hw, stream))
2111 mwl8k_remove_stream(hw, stream);
2112 spin_unlock(&priv->stream_lock);
2118 * Firmware access.
2120 * We have the following requirements for issuing firmware commands:
2121 * - Some commands require that the packet transmit path is idle when
2122 * the command is issued. (For simplicity, we'll just quiesce the
2123 * transmit path for every command.)
2124 * - There are certain sequences of commands that need to be issued to
2125 * the hardware sequentially, with no other intervening commands.
2127 * This leads to an implementation of a "firmware lock" as a mutex that
2128 * can be taken recursively, and which is taken by both the low-level
2129 * command submission function (mwl8k_post_cmd) as well as any users of
2130 * that function that require issuing of an atomic sequence of commands,
2131 * and quiesces the transmit path whenever it's taken.
2133 static int mwl8k_fw_lock(struct ieee80211_hw *hw)
2135 struct mwl8k_priv *priv = hw->priv;
2137 if (priv->fw_mutex_owner != current) {
2138 int rc;
2140 mutex_lock(&priv->fw_mutex);
2141 ieee80211_stop_queues(hw);
2143 rc = mwl8k_tx_wait_empty(hw);
2144 if (rc) {
2145 if (!priv->hw_restart_in_progress)
2146 ieee80211_wake_queues(hw);
2148 mutex_unlock(&priv->fw_mutex);
2150 return rc;
2153 priv->fw_mutex_owner = current;
2156 priv->fw_mutex_depth++;
2158 return 0;
2161 static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
2163 struct mwl8k_priv *priv = hw->priv;
2165 if (!--priv->fw_mutex_depth) {
2166 if (!priv->hw_restart_in_progress)
2167 ieee80211_wake_queues(hw);
2169 priv->fw_mutex_owner = NULL;
2170 mutex_unlock(&priv->fw_mutex);
2174 static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
2175 u32 bitmap);
2178 * Command processing.
2181 /* Timeout firmware commands after 10s */
2182 #define MWL8K_CMD_TIMEOUT_MS 10000
2184 static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
2186 DECLARE_COMPLETION_ONSTACK(cmd_wait);
2187 struct mwl8k_priv *priv = hw->priv;
2188 void __iomem *regs = priv->regs;
2189 dma_addr_t dma_addr;
2190 unsigned int dma_size;
2191 int rc;
2192 unsigned long timeout = 0;
2193 u8 buf[32];
2194 u32 bitmap = 0;
2196 wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
2197 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
2199 /* Before posting firmware commands that could change the hardware
2200 * characteristics, make sure that all BSSes are stopped temporary.
2201 * Enable these stopped BSSes after completion of the commands
2204 rc = mwl8k_fw_lock(hw);
2205 if (rc)
2206 return rc;
2208 if (priv->ap_fw && priv->running_bsses) {
2209 switch (le16_to_cpu(cmd->code)) {
2210 case MWL8K_CMD_SET_RF_CHANNEL:
2211 case MWL8K_CMD_RADIO_CONTROL:
2212 case MWL8K_CMD_RF_TX_POWER:
2213 case MWL8K_CMD_TX_POWER:
2214 case MWL8K_CMD_RF_ANTENNA:
2215 case MWL8K_CMD_RTS_THRESHOLD:
2216 case MWL8K_CMD_MIMO_CONFIG:
2217 bitmap = priv->running_bsses;
2218 mwl8k_enable_bsses(hw, false, bitmap);
2219 break;
2223 cmd->result = (__force __le16) 0xffff;
2224 dma_size = le16_to_cpu(cmd->length);
2225 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
2226 PCI_DMA_BIDIRECTIONAL);
2227 if (pci_dma_mapping_error(priv->pdev, dma_addr))
2228 return -ENOMEM;
2230 priv->hostcmd_wait = &cmd_wait;
2231 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
2232 iowrite32(MWL8K_H2A_INT_DOORBELL,
2233 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2234 iowrite32(MWL8K_H2A_INT_DUMMY,
2235 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2237 timeout = wait_for_completion_timeout(&cmd_wait,
2238 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
2240 priv->hostcmd_wait = NULL;
2243 pci_unmap_single(priv->pdev, dma_addr, dma_size,
2244 PCI_DMA_BIDIRECTIONAL);
2246 if (!timeout) {
2247 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
2248 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2249 MWL8K_CMD_TIMEOUT_MS);
2250 rc = -ETIMEDOUT;
2251 } else {
2252 int ms;
2254 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
2256 rc = cmd->result ? -EINVAL : 0;
2257 if (rc)
2258 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
2259 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2260 le16_to_cpu(cmd->result));
2261 else if (ms > 2000)
2262 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
2263 mwl8k_cmd_name(cmd->code,
2264 buf, sizeof(buf)),
2265 ms);
2268 if (bitmap)
2269 mwl8k_enable_bsses(hw, true, bitmap);
2271 mwl8k_fw_unlock(hw);
2273 return rc;
2276 static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
2277 struct ieee80211_vif *vif,
2278 struct mwl8k_cmd_pkt *cmd)
2280 if (vif != NULL)
2281 cmd->macid = MWL8K_VIF(vif)->macid;
2282 return mwl8k_post_cmd(hw, cmd);
2286 * Setup code shared between STA and AP firmware images.
2288 static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2290 struct mwl8k_priv *priv = hw->priv;
2292 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2293 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2295 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2296 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2298 priv->band_24.band = IEEE80211_BAND_2GHZ;
2299 priv->band_24.channels = priv->channels_24;
2300 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2301 priv->band_24.bitrates = priv->rates_24;
2302 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2304 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
2307 static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2309 struct mwl8k_priv *priv = hw->priv;
2311 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2312 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2314 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2315 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2317 priv->band_50.band = IEEE80211_BAND_5GHZ;
2318 priv->band_50.channels = priv->channels_50;
2319 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2320 priv->band_50.bitrates = priv->rates_50;
2321 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2323 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
2327 * CMD_GET_HW_SPEC (STA version).
2329 struct mwl8k_cmd_get_hw_spec_sta {
2330 struct mwl8k_cmd_pkt header;
2331 __u8 hw_rev;
2332 __u8 host_interface;
2333 __le16 num_mcaddrs;
2334 __u8 perm_addr[ETH_ALEN];
2335 __le16 region_code;
2336 __le32 fw_rev;
2337 __le32 ps_cookie;
2338 __le32 caps;
2339 __u8 mcs_bitmap[16];
2340 __le32 rx_queue_ptr;
2341 __le32 num_tx_queues;
2342 __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
2343 __le32 caps2;
2344 __le32 num_tx_desc_per_queue;
2345 __le32 total_rxd;
2346 } __packed;
2348 #define MWL8K_CAP_MAX_AMSDU 0x20000000
2349 #define MWL8K_CAP_GREENFIELD 0x08000000
2350 #define MWL8K_CAP_AMPDU 0x04000000
2351 #define MWL8K_CAP_RX_STBC 0x01000000
2352 #define MWL8K_CAP_TX_STBC 0x00800000
2353 #define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
2354 #define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
2355 #define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
2356 #define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
2357 #define MWL8K_CAP_DELAY_BA 0x00003000
2358 #define MWL8K_CAP_MIMO 0x00000200
2359 #define MWL8K_CAP_40MHZ 0x00000100
2360 #define MWL8K_CAP_BAND_MASK 0x00000007
2361 #define MWL8K_CAP_5GHZ 0x00000004
2362 #define MWL8K_CAP_2GHZ4 0x00000001
2364 static void
2365 mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2366 struct ieee80211_supported_band *band, u32 cap)
2368 int rx_streams;
2369 int tx_streams;
2371 band->ht_cap.ht_supported = 1;
2373 if (cap & MWL8K_CAP_MAX_AMSDU)
2374 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
2375 if (cap & MWL8K_CAP_GREENFIELD)
2376 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
2377 if (cap & MWL8K_CAP_AMPDU) {
2378 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
2379 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2380 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2382 if (cap & MWL8K_CAP_RX_STBC)
2383 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
2384 if (cap & MWL8K_CAP_TX_STBC)
2385 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
2386 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
2387 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
2388 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
2389 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
2390 if (cap & MWL8K_CAP_DELAY_BA)
2391 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
2392 if (cap & MWL8K_CAP_40MHZ)
2393 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2395 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2396 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2398 band->ht_cap.mcs.rx_mask[0] = 0xff;
2399 if (rx_streams >= 2)
2400 band->ht_cap.mcs.rx_mask[1] = 0xff;
2401 if (rx_streams >= 3)
2402 band->ht_cap.mcs.rx_mask[2] = 0xff;
2403 band->ht_cap.mcs.rx_mask[4] = 0x01;
2404 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2406 if (rx_streams != tx_streams) {
2407 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2408 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
2409 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2413 static void
2414 mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2416 struct mwl8k_priv *priv = hw->priv;
2418 if (priv->caps)
2419 return;
2421 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2422 mwl8k_setup_2ghz_band(hw);
2423 if (caps & MWL8K_CAP_MIMO)
2424 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2427 if (caps & MWL8K_CAP_5GHZ) {
2428 mwl8k_setup_5ghz_band(hw);
2429 if (caps & MWL8K_CAP_MIMO)
2430 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2433 priv->caps = caps;
2436 static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
2438 struct mwl8k_priv *priv = hw->priv;
2439 struct mwl8k_cmd_get_hw_spec_sta *cmd;
2440 int rc;
2441 int i;
2443 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2444 if (cmd == NULL)
2445 return -ENOMEM;
2447 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2448 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2450 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2451 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2452 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2453 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2454 for (i = 0; i < mwl8k_tx_queues(priv); i++)
2455 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
2456 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2457 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2459 rc = mwl8k_post_cmd(hw, &cmd->header);
2461 if (!rc) {
2462 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2463 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2464 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2465 priv->hw_rev = cmd->hw_rev;
2466 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
2467 priv->ap_macids_supported = 0x00000000;
2468 priv->sta_macids_supported = 0x00000001;
2471 kfree(cmd);
2472 return rc;
2476 * CMD_GET_HW_SPEC (AP version).
2478 struct mwl8k_cmd_get_hw_spec_ap {
2479 struct mwl8k_cmd_pkt header;
2480 __u8 hw_rev;
2481 __u8 host_interface;
2482 __le16 num_wcb;
2483 __le16 num_mcaddrs;
2484 __u8 perm_addr[ETH_ALEN];
2485 __le16 region_code;
2486 __le16 num_antenna;
2487 __le32 fw_rev;
2488 __le32 wcbbase0;
2489 __le32 rxwrptr;
2490 __le32 rxrdptr;
2491 __le32 ps_cookie;
2492 __le32 wcbbase1;
2493 __le32 wcbbase2;
2494 __le32 wcbbase3;
2495 __le32 fw_api_version;
2496 __le32 caps;
2497 __le32 num_of_ampdu_queues;
2498 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
2499 } __packed;
2501 static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2503 struct mwl8k_priv *priv = hw->priv;
2504 struct mwl8k_cmd_get_hw_spec_ap *cmd;
2505 int rc, i;
2506 u32 api_version;
2508 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2509 if (cmd == NULL)
2510 return -ENOMEM;
2512 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2513 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2515 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2516 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2518 rc = mwl8k_post_cmd(hw, &cmd->header);
2520 if (!rc) {
2521 int off;
2523 api_version = le32_to_cpu(cmd->fw_api_version);
2524 if (priv->device_info->fw_api_ap != api_version) {
2525 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2526 " Expected %d got %d.\n", MWL8K_NAME,
2527 priv->device_info->part_name,
2528 priv->device_info->fw_api_ap,
2529 api_version);
2530 rc = -EINVAL;
2531 goto done;
2533 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2534 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2535 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2536 priv->hw_rev = cmd->hw_rev;
2537 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
2538 priv->ap_macids_supported = 0x000000ff;
2539 priv->sta_macids_supported = 0x00000100;
2540 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2541 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2542 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2543 " but we only support %d.\n",
2544 priv->num_ampdu_queues,
2545 MWL8K_MAX_AMPDU_QUEUES);
2546 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2548 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
2549 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
2551 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
2552 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
2554 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2555 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2556 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2557 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
2559 for (i = 0; i < priv->num_ampdu_queues; i++)
2560 priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
2561 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
2564 done:
2565 kfree(cmd);
2566 return rc;
2570 * CMD_SET_HW_SPEC.
2572 struct mwl8k_cmd_set_hw_spec {
2573 struct mwl8k_cmd_pkt header;
2574 __u8 hw_rev;
2575 __u8 host_interface;
2576 __le16 num_mcaddrs;
2577 __u8 perm_addr[ETH_ALEN];
2578 __le16 region_code;
2579 __le32 fw_rev;
2580 __le32 ps_cookie;
2581 __le32 caps;
2582 __le32 rx_queue_ptr;
2583 __le32 num_tx_queues;
2584 __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
2585 __le32 flags;
2586 __le32 num_tx_desc_per_queue;
2587 __le32 total_rxd;
2588 } __packed;
2590 /* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2591 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2592 * the packets that are queued for more than 500ms, will be dropped in the
2593 * hardware. This helps minimizing the issues caused due to head-of-line
2594 * blocking where a slow client can hog the bandwidth and affect traffic to a
2595 * faster client.
2597 #define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
2598 #define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR 0x00000200
2599 #define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2600 #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2601 #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
2603 static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2605 struct mwl8k_priv *priv = hw->priv;
2606 struct mwl8k_cmd_set_hw_spec *cmd;
2607 int rc;
2608 int i;
2610 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2611 if (cmd == NULL)
2612 return -ENOMEM;
2614 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2615 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2617 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2618 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2619 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2622 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2623 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2624 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2625 * priority is interpreted the right way in firmware.
2627 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2628 int j = mwl8k_tx_queues(priv) - 1 - i;
2629 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2632 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2633 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2634 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
2635 MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
2636 MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
2637 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2638 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2640 rc = mwl8k_post_cmd(hw, &cmd->header);
2641 kfree(cmd);
2643 return rc;
2647 * CMD_MAC_MULTICAST_ADR.
2649 struct mwl8k_cmd_mac_multicast_adr {
2650 struct mwl8k_cmd_pkt header;
2651 __le16 action;
2652 __le16 numaddr;
2653 __u8 addr[0][ETH_ALEN];
2656 #define MWL8K_ENABLE_RX_DIRECTED 0x0001
2657 #define MWL8K_ENABLE_RX_MULTICAST 0x0002
2658 #define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2659 #define MWL8K_ENABLE_RX_BROADCAST 0x0008
2661 static struct mwl8k_cmd_pkt *
2662 __mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
2663 struct netdev_hw_addr_list *mc_list)
2665 struct mwl8k_priv *priv = hw->priv;
2666 struct mwl8k_cmd_mac_multicast_adr *cmd;
2667 int size;
2668 int mc_count = 0;
2670 if (mc_list)
2671 mc_count = netdev_hw_addr_list_count(mc_list);
2673 if (allmulti || mc_count > priv->num_mcaddrs) {
2674 allmulti = 1;
2675 mc_count = 0;
2678 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2680 cmd = kzalloc(size, GFP_ATOMIC);
2681 if (cmd == NULL)
2682 return NULL;
2684 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2685 cmd->header.length = cpu_to_le16(size);
2686 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2687 MWL8K_ENABLE_RX_BROADCAST);
2689 if (allmulti) {
2690 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2691 } else if (mc_count) {
2692 struct netdev_hw_addr *ha;
2693 int i = 0;
2695 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2696 cmd->numaddr = cpu_to_le16(mc_count);
2697 netdev_hw_addr_list_for_each(ha, mc_list) {
2698 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
2702 return &cmd->header;
2706 * CMD_GET_STAT.
2708 struct mwl8k_cmd_get_stat {
2709 struct mwl8k_cmd_pkt header;
2710 __le32 stats[64];
2711 } __packed;
2713 #define MWL8K_STAT_ACK_FAILURE 9
2714 #define MWL8K_STAT_RTS_FAILURE 12
2715 #define MWL8K_STAT_FCS_ERROR 24
2716 #define MWL8K_STAT_RTS_SUCCESS 11
2718 static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2719 struct ieee80211_low_level_stats *stats)
2721 struct mwl8k_cmd_get_stat *cmd;
2722 int rc;
2724 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2725 if (cmd == NULL)
2726 return -ENOMEM;
2728 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2729 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2731 rc = mwl8k_post_cmd(hw, &cmd->header);
2732 if (!rc) {
2733 stats->dot11ACKFailureCount =
2734 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2735 stats->dot11RTSFailureCount =
2736 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2737 stats->dot11FCSErrorCount =
2738 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2739 stats->dot11RTSSuccessCount =
2740 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2742 kfree(cmd);
2744 return rc;
2748 * CMD_RADIO_CONTROL.
2750 struct mwl8k_cmd_radio_control {
2751 struct mwl8k_cmd_pkt header;
2752 __le16 action;
2753 __le16 control;
2754 __le16 radio_on;
2755 } __packed;
2757 static int
2758 mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
2760 struct mwl8k_priv *priv = hw->priv;
2761 struct mwl8k_cmd_radio_control *cmd;
2762 int rc;
2764 if (enable == priv->radio_on && !force)
2765 return 0;
2767 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2768 if (cmd == NULL)
2769 return -ENOMEM;
2771 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2772 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2773 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2774 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
2775 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2777 rc = mwl8k_post_cmd(hw, &cmd->header);
2778 kfree(cmd);
2780 if (!rc)
2781 priv->radio_on = enable;
2783 return rc;
2786 static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
2788 return mwl8k_cmd_radio_control(hw, 0, 0);
2791 static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
2793 return mwl8k_cmd_radio_control(hw, 1, 0);
2796 static int
2797 mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2799 struct mwl8k_priv *priv = hw->priv;
2801 priv->radio_short_preamble = short_preamble;
2803 return mwl8k_cmd_radio_control(hw, 1, 1);
2807 * CMD_RF_TX_POWER.
2809 #define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
2811 struct mwl8k_cmd_rf_tx_power {
2812 struct mwl8k_cmd_pkt header;
2813 __le16 action;
2814 __le16 support_level;
2815 __le16 current_level;
2816 __le16 reserved;
2817 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
2818 } __packed;
2820 static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
2822 struct mwl8k_cmd_rf_tx_power *cmd;
2823 int rc;
2825 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2826 if (cmd == NULL)
2827 return -ENOMEM;
2829 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2830 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2831 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2832 cmd->support_level = cpu_to_le16(dBm);
2834 rc = mwl8k_post_cmd(hw, &cmd->header);
2835 kfree(cmd);
2837 return rc;
2841 * CMD_TX_POWER.
2843 #define MWL8K_TX_POWER_LEVEL_TOTAL 12
2845 struct mwl8k_cmd_tx_power {
2846 struct mwl8k_cmd_pkt header;
2847 __le16 action;
2848 __le16 band;
2849 __le16 channel;
2850 __le16 bw;
2851 __le16 sub_ch;
2852 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2853 } __packed;
2855 static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2856 struct ieee80211_conf *conf,
2857 unsigned short pwr)
2859 struct ieee80211_channel *channel = conf->chandef.chan;
2860 enum nl80211_channel_type channel_type =
2861 cfg80211_get_chandef_type(&conf->chandef);
2862 struct mwl8k_cmd_tx_power *cmd;
2863 int rc;
2864 int i;
2866 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2867 if (cmd == NULL)
2868 return -ENOMEM;
2870 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2871 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2872 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2874 if (channel->band == IEEE80211_BAND_2GHZ)
2875 cmd->band = cpu_to_le16(0x1);
2876 else if (channel->band == IEEE80211_BAND_5GHZ)
2877 cmd->band = cpu_to_le16(0x4);
2879 cmd->channel = cpu_to_le16(channel->hw_value);
2881 if (channel_type == NL80211_CHAN_NO_HT ||
2882 channel_type == NL80211_CHAN_HT20) {
2883 cmd->bw = cpu_to_le16(0x2);
2884 } else {
2885 cmd->bw = cpu_to_le16(0x4);
2886 if (channel_type == NL80211_CHAN_HT40MINUS)
2887 cmd->sub_ch = cpu_to_le16(0x3);
2888 else if (channel_type == NL80211_CHAN_HT40PLUS)
2889 cmd->sub_ch = cpu_to_le16(0x1);
2892 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2893 cmd->power_level_list[i] = cpu_to_le16(pwr);
2895 rc = mwl8k_post_cmd(hw, &cmd->header);
2896 kfree(cmd);
2898 return rc;
2902 * CMD_RF_ANTENNA.
2904 struct mwl8k_cmd_rf_antenna {
2905 struct mwl8k_cmd_pkt header;
2906 __le16 antenna;
2907 __le16 mode;
2908 } __packed;
2910 #define MWL8K_RF_ANTENNA_RX 1
2911 #define MWL8K_RF_ANTENNA_TX 2
2913 static int
2914 mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2916 struct mwl8k_cmd_rf_antenna *cmd;
2917 int rc;
2919 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2920 if (cmd == NULL)
2921 return -ENOMEM;
2923 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2924 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2925 cmd->antenna = cpu_to_le16(antenna);
2926 cmd->mode = cpu_to_le16(mask);
2928 rc = mwl8k_post_cmd(hw, &cmd->header);
2929 kfree(cmd);
2931 return rc;
2935 * CMD_SET_BEACON.
2937 struct mwl8k_cmd_set_beacon {
2938 struct mwl8k_cmd_pkt header;
2939 __le16 beacon_len;
2940 __u8 beacon[0];
2943 static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2944 struct ieee80211_vif *vif, u8 *beacon, int len)
2946 struct mwl8k_cmd_set_beacon *cmd;
2947 int rc;
2949 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2950 if (cmd == NULL)
2951 return -ENOMEM;
2953 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2954 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2955 cmd->beacon_len = cpu_to_le16(len);
2956 memcpy(cmd->beacon, beacon, len);
2958 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
2959 kfree(cmd);
2961 return rc;
2965 * CMD_SET_PRE_SCAN.
2967 struct mwl8k_cmd_set_pre_scan {
2968 struct mwl8k_cmd_pkt header;
2969 } __packed;
2971 static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2973 struct mwl8k_cmd_set_pre_scan *cmd;
2974 int rc;
2976 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2977 if (cmd == NULL)
2978 return -ENOMEM;
2980 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2981 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2983 rc = mwl8k_post_cmd(hw, &cmd->header);
2984 kfree(cmd);
2986 return rc;
2990 * CMD_SET_POST_SCAN.
2992 struct mwl8k_cmd_set_post_scan {
2993 struct mwl8k_cmd_pkt header;
2994 __le32 isibss;
2995 __u8 bssid[ETH_ALEN];
2996 } __packed;
2998 static int
2999 mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
3001 struct mwl8k_cmd_set_post_scan *cmd;
3002 int rc;
3004 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3005 if (cmd == NULL)
3006 return -ENOMEM;
3008 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
3009 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3010 cmd->isibss = 0;
3011 memcpy(cmd->bssid, mac, ETH_ALEN);
3013 rc = mwl8k_post_cmd(hw, &cmd->header);
3014 kfree(cmd);
3016 return rc;
3020 * CMD_SET_RF_CHANNEL.
3022 struct mwl8k_cmd_set_rf_channel {
3023 struct mwl8k_cmd_pkt header;
3024 __le16 action;
3025 __u8 current_channel;
3026 __le32 channel_flags;
3027 } __packed;
3029 static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
3030 struct ieee80211_conf *conf)
3032 struct ieee80211_channel *channel = conf->chandef.chan;
3033 enum nl80211_channel_type channel_type =
3034 cfg80211_get_chandef_type(&conf->chandef);
3035 struct mwl8k_cmd_set_rf_channel *cmd;
3036 int rc;
3038 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3039 if (cmd == NULL)
3040 return -ENOMEM;
3042 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
3043 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3044 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3045 cmd->current_channel = channel->hw_value;
3047 if (channel->band == IEEE80211_BAND_2GHZ)
3048 cmd->channel_flags |= cpu_to_le32(0x00000001);
3049 else if (channel->band == IEEE80211_BAND_5GHZ)
3050 cmd->channel_flags |= cpu_to_le32(0x00000004);
3052 if (channel_type == NL80211_CHAN_NO_HT ||
3053 channel_type == NL80211_CHAN_HT20)
3054 cmd->channel_flags |= cpu_to_le32(0x00000080);
3055 else if (channel_type == NL80211_CHAN_HT40MINUS)
3056 cmd->channel_flags |= cpu_to_le32(0x000001900);
3057 else if (channel_type == NL80211_CHAN_HT40PLUS)
3058 cmd->channel_flags |= cpu_to_le32(0x000000900);
3060 rc = mwl8k_post_cmd(hw, &cmd->header);
3061 kfree(cmd);
3063 return rc;
3067 * CMD_SET_AID.
3069 #define MWL8K_FRAME_PROT_DISABLED 0x00
3070 #define MWL8K_FRAME_PROT_11G 0x07
3071 #define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
3072 #define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
3074 struct mwl8k_cmd_update_set_aid {
3075 struct mwl8k_cmd_pkt header;
3076 __le16 aid;
3078 /* AP's MAC address (BSSID) */
3079 __u8 bssid[ETH_ALEN];
3080 __le16 protection_mode;
3081 __u8 supp_rates[14];
3082 } __packed;
3084 static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
3086 int i;
3087 int j;
3090 * Clear nonstandard rate 4.
3092 mask &= 0x1fef;
3094 for (i = 0, j = 0; i < 13; i++) {
3095 if (mask & (1 << i))
3096 rates[j++] = mwl8k_rates_24[i].hw_value;
3100 static int
3101 mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
3102 struct ieee80211_vif *vif, u32 legacy_rate_mask)
3104 struct mwl8k_cmd_update_set_aid *cmd;
3105 u16 prot_mode;
3106 int rc;
3108 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3109 if (cmd == NULL)
3110 return -ENOMEM;
3112 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
3113 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3114 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
3115 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
3117 if (vif->bss_conf.use_cts_prot) {
3118 prot_mode = MWL8K_FRAME_PROT_11G;
3119 } else {
3120 switch (vif->bss_conf.ht_operation_mode &
3121 IEEE80211_HT_OP_MODE_PROTECTION) {
3122 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
3123 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
3124 break;
3125 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
3126 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
3127 break;
3128 default:
3129 prot_mode = MWL8K_FRAME_PROT_DISABLED;
3130 break;
3133 cmd->protection_mode = cpu_to_le16(prot_mode);
3135 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
3137 rc = mwl8k_post_cmd(hw, &cmd->header);
3138 kfree(cmd);
3140 return rc;
3144 * CMD_SET_RATE.
3146 struct mwl8k_cmd_set_rate {
3147 struct mwl8k_cmd_pkt header;
3148 __u8 legacy_rates[14];
3150 /* Bitmap for supported MCS codes. */
3151 __u8 mcs_set[16];
3152 __u8 reserved[16];
3153 } __packed;
3155 static int
3156 mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3157 u32 legacy_rate_mask, u8 *mcs_rates)
3159 struct mwl8k_cmd_set_rate *cmd;
3160 int rc;
3162 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3163 if (cmd == NULL)
3164 return -ENOMEM;
3166 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
3167 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3168 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
3169 memcpy(cmd->mcs_set, mcs_rates, 16);
3171 rc = mwl8k_post_cmd(hw, &cmd->header);
3172 kfree(cmd);
3174 return rc;
3178 * CMD_FINALIZE_JOIN.
3180 #define MWL8K_FJ_BEACON_MAXLEN 128
3182 struct mwl8k_cmd_finalize_join {
3183 struct mwl8k_cmd_pkt header;
3184 __le32 sleep_interval; /* Number of beacon periods to sleep */
3185 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
3186 } __packed;
3188 static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
3189 int framelen, int dtim)
3191 struct mwl8k_cmd_finalize_join *cmd;
3192 struct ieee80211_mgmt *payload = frame;
3193 int payload_len;
3194 int rc;
3196 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3197 if (cmd == NULL)
3198 return -ENOMEM;
3200 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
3201 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3202 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
3204 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
3205 if (payload_len < 0)
3206 payload_len = 0;
3207 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
3208 payload_len = MWL8K_FJ_BEACON_MAXLEN;
3210 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
3212 rc = mwl8k_post_cmd(hw, &cmd->header);
3213 kfree(cmd);
3215 return rc;
3219 * CMD_SET_RTS_THRESHOLD.
3221 struct mwl8k_cmd_set_rts_threshold {
3222 struct mwl8k_cmd_pkt header;
3223 __le16 action;
3224 __le16 threshold;
3225 } __packed;
3227 static int
3228 mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
3230 struct mwl8k_cmd_set_rts_threshold *cmd;
3231 int rc;
3233 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3234 if (cmd == NULL)
3235 return -ENOMEM;
3237 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3238 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3239 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3240 cmd->threshold = cpu_to_le16(rts_thresh);
3242 rc = mwl8k_post_cmd(hw, &cmd->header);
3243 kfree(cmd);
3245 return rc;
3249 * CMD_SET_SLOT.
3251 struct mwl8k_cmd_set_slot {
3252 struct mwl8k_cmd_pkt header;
3253 __le16 action;
3254 __u8 short_slot;
3255 } __packed;
3257 static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
3259 struct mwl8k_cmd_set_slot *cmd;
3260 int rc;
3262 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3263 if (cmd == NULL)
3264 return -ENOMEM;
3266 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3267 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3268 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3269 cmd->short_slot = short_slot_time;
3271 rc = mwl8k_post_cmd(hw, &cmd->header);
3272 kfree(cmd);
3274 return rc;
3278 * CMD_SET_EDCA_PARAMS.
3280 struct mwl8k_cmd_set_edca_params {
3281 struct mwl8k_cmd_pkt header;
3283 /* See MWL8K_SET_EDCA_XXX below */
3284 __le16 action;
3286 /* TX opportunity in units of 32 us */
3287 __le16 txop;
3289 union {
3290 struct {
3291 /* Log exponent of max contention period: 0...15 */
3292 __le32 log_cw_max;
3294 /* Log exponent of min contention period: 0...15 */
3295 __le32 log_cw_min;
3297 /* Adaptive interframe spacing in units of 32us */
3298 __u8 aifs;
3300 /* TX queue to configure */
3301 __u8 txq;
3302 } ap;
3303 struct {
3304 /* Log exponent of max contention period: 0...15 */
3305 __u8 log_cw_max;
3307 /* Log exponent of min contention period: 0...15 */
3308 __u8 log_cw_min;
3310 /* Adaptive interframe spacing in units of 32us */
3311 __u8 aifs;
3313 /* TX queue to configure */
3314 __u8 txq;
3315 } sta;
3317 } __packed;
3319 #define MWL8K_SET_EDCA_CW 0x01
3320 #define MWL8K_SET_EDCA_TXOP 0x02
3321 #define MWL8K_SET_EDCA_AIFS 0x04
3323 #define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
3324 MWL8K_SET_EDCA_TXOP | \
3325 MWL8K_SET_EDCA_AIFS)
3327 static int
3328 mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3329 __u16 cw_min, __u16 cw_max,
3330 __u8 aifs, __u16 txop)
3332 struct mwl8k_priv *priv = hw->priv;
3333 struct mwl8k_cmd_set_edca_params *cmd;
3334 int rc;
3336 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3337 if (cmd == NULL)
3338 return -ENOMEM;
3340 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3341 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3342 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3343 cmd->txop = cpu_to_le16(txop);
3344 if (priv->ap_fw) {
3345 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3346 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3347 cmd->ap.aifs = aifs;
3348 cmd->ap.txq = qnum;
3349 } else {
3350 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3351 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3352 cmd->sta.aifs = aifs;
3353 cmd->sta.txq = qnum;
3356 rc = mwl8k_post_cmd(hw, &cmd->header);
3357 kfree(cmd);
3359 return rc;
3363 * CMD_SET_WMM_MODE.
3365 struct mwl8k_cmd_set_wmm_mode {
3366 struct mwl8k_cmd_pkt header;
3367 __le16 action;
3368 } __packed;
3370 static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
3372 struct mwl8k_priv *priv = hw->priv;
3373 struct mwl8k_cmd_set_wmm_mode *cmd;
3374 int rc;
3376 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3377 if (cmd == NULL)
3378 return -ENOMEM;
3380 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
3381 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3382 cmd->action = cpu_to_le16(!!enable);
3384 rc = mwl8k_post_cmd(hw, &cmd->header);
3385 kfree(cmd);
3387 if (!rc)
3388 priv->wmm_enabled = enable;
3390 return rc;
3394 * CMD_MIMO_CONFIG.
3396 struct mwl8k_cmd_mimo_config {
3397 struct mwl8k_cmd_pkt header;
3398 __le32 action;
3399 __u8 rx_antenna_map;
3400 __u8 tx_antenna_map;
3401 } __packed;
3403 static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
3405 struct mwl8k_cmd_mimo_config *cmd;
3406 int rc;
3408 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3409 if (cmd == NULL)
3410 return -ENOMEM;
3412 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
3413 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3414 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3415 cmd->rx_antenna_map = rx;
3416 cmd->tx_antenna_map = tx;
3418 rc = mwl8k_post_cmd(hw, &cmd->header);
3419 kfree(cmd);
3421 return rc;
3425 * CMD_USE_FIXED_RATE (STA version).
3427 struct mwl8k_cmd_use_fixed_rate_sta {
3428 struct mwl8k_cmd_pkt header;
3429 __le32 action;
3430 __le32 allow_rate_drop;
3431 __le32 num_rates;
3432 struct {
3433 __le32 is_ht_rate;
3434 __le32 enable_retry;
3435 __le32 rate;
3436 __le32 retry_count;
3437 } rate_entry[8];
3438 __le32 rate_type;
3439 __le32 reserved1;
3440 __le32 reserved2;
3441 } __packed;
3443 #define MWL8K_USE_AUTO_RATE 0x0002
3444 #define MWL8K_UCAST_RATE 0
3446 static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
3448 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
3449 int rc;
3451 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3452 if (cmd == NULL)
3453 return -ENOMEM;
3455 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3456 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3457 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3458 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
3460 rc = mwl8k_post_cmd(hw, &cmd->header);
3461 kfree(cmd);
3463 return rc;
3467 * CMD_USE_FIXED_RATE (AP version).
3469 struct mwl8k_cmd_use_fixed_rate_ap {
3470 struct mwl8k_cmd_pkt header;
3471 __le32 action;
3472 __le32 allow_rate_drop;
3473 __le32 num_rates;
3474 struct mwl8k_rate_entry_ap {
3475 __le32 is_ht_rate;
3476 __le32 enable_retry;
3477 __le32 rate;
3478 __le32 retry_count;
3479 } rate_entry[4];
3480 u8 multicast_rate;
3481 u8 multicast_rate_type;
3482 u8 management_rate;
3483 } __packed;
3485 static int
3486 mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3488 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3489 int rc;
3491 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3492 if (cmd == NULL)
3493 return -ENOMEM;
3495 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3496 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3497 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3498 cmd->multicast_rate = mcast;
3499 cmd->management_rate = mgmt;
3501 rc = mwl8k_post_cmd(hw, &cmd->header);
3502 kfree(cmd);
3504 return rc;
3508 * CMD_ENABLE_SNIFFER.
3510 struct mwl8k_cmd_enable_sniffer {
3511 struct mwl8k_cmd_pkt header;
3512 __le32 action;
3513 } __packed;
3515 static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3517 struct mwl8k_cmd_enable_sniffer *cmd;
3518 int rc;
3520 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3521 if (cmd == NULL)
3522 return -ENOMEM;
3524 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3525 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3526 cmd->action = cpu_to_le32(!!enable);
3528 rc = mwl8k_post_cmd(hw, &cmd->header);
3529 kfree(cmd);
3531 return rc;
3534 struct mwl8k_cmd_update_mac_addr {
3535 struct mwl8k_cmd_pkt header;
3536 union {
3537 struct {
3538 __le16 mac_type;
3539 __u8 mac_addr[ETH_ALEN];
3540 } mbss;
3541 __u8 mac_addr[ETH_ALEN];
3543 } __packed;
3545 #define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3546 #define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3547 #define MWL8K_MAC_TYPE_PRIMARY_AP 2
3548 #define MWL8K_MAC_TYPE_SECONDARY_AP 3
3550 static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3551 struct ieee80211_vif *vif, u8 *mac, bool set)
3553 struct mwl8k_priv *priv = hw->priv;
3554 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3555 struct mwl8k_cmd_update_mac_addr *cmd;
3556 int mac_type;
3557 int rc;
3559 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3560 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3561 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3562 if (priv->ap_fw)
3563 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3564 else
3565 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3566 else
3567 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3568 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3569 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3570 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3571 else
3572 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3575 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3576 if (cmd == NULL)
3577 return -ENOMEM;
3579 if (set)
3580 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3581 else
3582 cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3584 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3585 if (priv->ap_fw) {
3586 cmd->mbss.mac_type = cpu_to_le16(mac_type);
3587 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3588 } else {
3589 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3592 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3593 kfree(cmd);
3595 return rc;
3599 * MWL8K_CMD_SET_MAC_ADDR.
3601 static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3602 struct ieee80211_vif *vif, u8 *mac)
3604 return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3608 * MWL8K_CMD_DEL_MAC_ADDR.
3610 static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3611 struct ieee80211_vif *vif, u8 *mac)
3613 return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3617 * CMD_SET_RATEADAPT_MODE.
3619 struct mwl8k_cmd_set_rate_adapt_mode {
3620 struct mwl8k_cmd_pkt header;
3621 __le16 action;
3622 __le16 mode;
3623 } __packed;
3625 static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3627 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3628 int rc;
3630 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3631 if (cmd == NULL)
3632 return -ENOMEM;
3634 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3635 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3636 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3637 cmd->mode = cpu_to_le16(mode);
3639 rc = mwl8k_post_cmd(hw, &cmd->header);
3640 kfree(cmd);
3642 return rc;
3646 * CMD_GET_WATCHDOG_BITMAP.
3648 struct mwl8k_cmd_get_watchdog_bitmap {
3649 struct mwl8k_cmd_pkt header;
3650 u8 bitmap;
3651 } __packed;
3653 static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3655 struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3656 int rc;
3658 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3659 if (cmd == NULL)
3660 return -ENOMEM;
3662 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3663 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3665 rc = mwl8k_post_cmd(hw, &cmd->header);
3666 if (!rc)
3667 *bitmap = cmd->bitmap;
3669 kfree(cmd);
3671 return rc;
3674 #define MWL8K_WMM_QUEUE_NUMBER 3
3676 static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3677 u8 idx);
3679 static void mwl8k_watchdog_ba_events(struct work_struct *work)
3681 int rc;
3682 u8 bitmap = 0, stream_index;
3683 struct mwl8k_ampdu_stream *streams;
3684 struct mwl8k_priv *priv =
3685 container_of(work, struct mwl8k_priv, watchdog_ba_handle);
3686 struct ieee80211_hw *hw = priv->hw;
3687 int i;
3688 u32 status = 0;
3690 mwl8k_fw_lock(hw);
3692 rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3693 if (rc)
3694 goto done;
3696 spin_lock(&priv->stream_lock);
3698 /* the bitmap is the hw queue number. Map it to the ampdu queue. */
3699 for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
3700 if (bitmap & (1 << i)) {
3701 stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
3702 TOTAL_HW_TX_QUEUES;
3703 streams = &priv->ampdu[stream_index];
3704 if (streams->state == AMPDU_STREAM_ACTIVE) {
3705 ieee80211_stop_tx_ba_session(streams->sta,
3706 streams->tid);
3707 spin_unlock(&priv->stream_lock);
3708 mwl8k_destroy_ba(hw, stream_index);
3709 spin_lock(&priv->stream_lock);
3714 spin_unlock(&priv->stream_lock);
3715 done:
3716 atomic_dec(&priv->watchdog_event_pending);
3717 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3718 iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
3719 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3720 mwl8k_fw_unlock(hw);
3721 return;
3726 * CMD_BSS_START.
3728 struct mwl8k_cmd_bss_start {
3729 struct mwl8k_cmd_pkt header;
3730 __le32 enable;
3731 } __packed;
3733 static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3734 struct ieee80211_vif *vif, int enable)
3736 struct mwl8k_cmd_bss_start *cmd;
3737 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3738 struct mwl8k_priv *priv = hw->priv;
3739 int rc;
3741 if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
3742 return 0;
3744 if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
3745 return 0;
3747 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3748 if (cmd == NULL)
3749 return -ENOMEM;
3751 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3752 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3753 cmd->enable = cpu_to_le32(enable);
3755 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3756 kfree(cmd);
3758 if (!rc) {
3759 if (enable)
3760 priv->running_bsses |= (1 << mwl8k_vif->macid);
3761 else
3762 priv->running_bsses &= ~(1 << mwl8k_vif->macid);
3764 return rc;
3767 static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
3769 struct mwl8k_priv *priv = hw->priv;
3770 struct mwl8k_vif *mwl8k_vif, *tmp_vif;
3771 struct ieee80211_vif *vif;
3773 list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
3774 vif = mwl8k_vif->vif;
3776 if (!(bitmap & (1 << mwl8k_vif->macid)))
3777 continue;
3779 if (vif->type == NL80211_IFTYPE_AP)
3780 mwl8k_cmd_bss_start(hw, vif, enable);
3784 * CMD_BASTREAM.
3788 * UPSTREAM is tx direction
3790 #define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3791 #define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3793 enum ba_stream_action_type {
3794 MWL8K_BA_CREATE,
3795 MWL8K_BA_UPDATE,
3796 MWL8K_BA_DESTROY,
3797 MWL8K_BA_FLUSH,
3798 MWL8K_BA_CHECK,
3802 struct mwl8k_create_ba_stream {
3803 __le32 flags;
3804 __le32 idle_thrs;
3805 __le32 bar_thrs;
3806 __le32 window_size;
3807 u8 peer_mac_addr[6];
3808 u8 dialog_token;
3809 u8 tid;
3810 u8 queue_id;
3811 u8 param_info;
3812 __le32 ba_context;
3813 u8 reset_seq_no_flag;
3814 __le16 curr_seq_no;
3815 u8 sta_src_mac_addr[6];
3816 } __packed;
3818 struct mwl8k_destroy_ba_stream {
3819 __le32 flags;
3820 __le32 ba_context;
3821 } __packed;
3823 struct mwl8k_cmd_bastream {
3824 struct mwl8k_cmd_pkt header;
3825 __le32 action;
3826 union {
3827 struct mwl8k_create_ba_stream create_params;
3828 struct mwl8k_destroy_ba_stream destroy_params;
3830 } __packed;
3832 static int
3833 mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3834 struct ieee80211_vif *vif)
3836 struct mwl8k_cmd_bastream *cmd;
3837 int rc;
3839 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3840 if (cmd == NULL)
3841 return -ENOMEM;
3843 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3844 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3846 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3848 cmd->create_params.queue_id = stream->idx;
3849 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3850 ETH_ALEN);
3851 cmd->create_params.tid = stream->tid;
3853 cmd->create_params.flags =
3854 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3855 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3857 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3859 kfree(cmd);
3861 return rc;
3864 static int
3865 mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3866 u8 buf_size, struct ieee80211_vif *vif)
3868 struct mwl8k_cmd_bastream *cmd;
3869 int rc;
3871 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3872 if (cmd == NULL)
3873 return -ENOMEM;
3876 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3877 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3879 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3881 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3882 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3883 cmd->create_params.queue_id = stream->idx;
3885 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3886 cmd->create_params.tid = stream->tid;
3887 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3888 cmd->create_params.reset_seq_no_flag = 1;
3890 cmd->create_params.param_info =
3891 (stream->sta->ht_cap.ampdu_factor &
3892 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3893 ((stream->sta->ht_cap.ampdu_density << 2) &
3894 IEEE80211_HT_AMPDU_PARM_DENSITY);
3896 cmd->create_params.flags =
3897 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3898 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3900 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3902 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3903 stream->sta->addr, stream->tid);
3904 kfree(cmd);
3906 return rc;
3909 static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3910 u8 idx)
3912 struct mwl8k_cmd_bastream *cmd;
3914 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3915 if (cmd == NULL)
3916 return;
3918 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3919 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3920 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3922 cmd->destroy_params.ba_context = cpu_to_le32(idx);
3923 mwl8k_post_cmd(hw, &cmd->header);
3925 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
3927 kfree(cmd);
3931 * CMD_SET_NEW_STN.
3933 struct mwl8k_cmd_set_new_stn {
3934 struct mwl8k_cmd_pkt header;
3935 __le16 aid;
3936 __u8 mac_addr[6];
3937 __le16 stn_id;
3938 __le16 action;
3939 __le16 rsvd;
3940 __le32 legacy_rates;
3941 __u8 ht_rates[4];
3942 __le16 cap_info;
3943 __le16 ht_capabilities_info;
3944 __u8 mac_ht_param_info;
3945 __u8 rev;
3946 __u8 control_channel;
3947 __u8 add_channel;
3948 __le16 op_mode;
3949 __le16 stbc;
3950 __u8 add_qos_info;
3951 __u8 is_qos_sta;
3952 __le32 fw_sta_ptr;
3953 } __packed;
3955 #define MWL8K_STA_ACTION_ADD 0
3956 #define MWL8K_STA_ACTION_REMOVE 2
3958 static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3959 struct ieee80211_vif *vif,
3960 struct ieee80211_sta *sta)
3962 struct mwl8k_cmd_set_new_stn *cmd;
3963 u32 rates;
3964 int rc;
3966 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3967 if (cmd == NULL)
3968 return -ENOMEM;
3970 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3971 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3972 cmd->aid = cpu_to_le16(sta->aid);
3973 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3974 cmd->stn_id = cpu_to_le16(sta->aid);
3975 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
3976 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
3977 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3978 else
3979 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3980 cmd->legacy_rates = cpu_to_le32(rates);
3981 if (sta->ht_cap.ht_supported) {
3982 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3983 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3984 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3985 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3986 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3987 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3988 ((sta->ht_cap.ampdu_density & 7) << 2);
3989 cmd->is_qos_sta = 1;
3992 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3993 kfree(cmd);
3995 return rc;
3998 static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3999 struct ieee80211_vif *vif)
4001 struct mwl8k_cmd_set_new_stn *cmd;
4002 int rc;
4004 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4005 if (cmd == NULL)
4006 return -ENOMEM;
4008 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4009 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4010 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
4012 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4013 kfree(cmd);
4015 return rc;
4018 static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
4019 struct ieee80211_vif *vif, u8 *addr)
4021 struct mwl8k_cmd_set_new_stn *cmd;
4022 struct mwl8k_priv *priv = hw->priv;
4023 int rc, i;
4024 u8 idx;
4026 spin_lock(&priv->stream_lock);
4027 /* Destroy any active ampdu streams for this sta */
4028 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
4029 struct mwl8k_ampdu_stream *s;
4030 s = &priv->ampdu[i];
4031 if (s->state != AMPDU_NO_STREAM) {
4032 if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
4033 if (s->state == AMPDU_STREAM_ACTIVE) {
4034 idx = s->idx;
4035 spin_unlock(&priv->stream_lock);
4036 mwl8k_destroy_ba(hw, idx);
4037 spin_lock(&priv->stream_lock);
4038 } else if (s->state == AMPDU_STREAM_NEW) {
4039 mwl8k_remove_stream(hw, s);
4045 spin_unlock(&priv->stream_lock);
4047 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4048 if (cmd == NULL)
4049 return -ENOMEM;
4051 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4052 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4053 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4054 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
4056 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4057 kfree(cmd);
4059 return rc;
4063 * CMD_UPDATE_ENCRYPTION.
4066 #define MAX_ENCR_KEY_LENGTH 16
4067 #define MIC_KEY_LENGTH 8
4069 struct mwl8k_cmd_update_encryption {
4070 struct mwl8k_cmd_pkt header;
4072 __le32 action;
4073 __le32 reserved;
4074 __u8 mac_addr[6];
4075 __u8 encr_type;
4077 } __packed;
4079 struct mwl8k_cmd_set_key {
4080 struct mwl8k_cmd_pkt header;
4082 __le32 action;
4083 __le32 reserved;
4084 __le16 length;
4085 __le16 key_type_id;
4086 __le32 key_info;
4087 __le32 key_id;
4088 __le16 key_len;
4089 __u8 key_material[MAX_ENCR_KEY_LENGTH];
4090 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
4091 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
4092 __le16 tkip_rsc_low;
4093 __le32 tkip_rsc_high;
4094 __le16 tkip_tsc_low;
4095 __le32 tkip_tsc_high;
4096 __u8 mac_addr[6];
4097 } __packed;
4099 enum {
4100 MWL8K_ENCR_ENABLE,
4101 MWL8K_ENCR_SET_KEY,
4102 MWL8K_ENCR_REMOVE_KEY,
4103 MWL8K_ENCR_SET_GROUP_KEY,
4106 #define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
4107 #define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
4108 #define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
4109 #define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
4110 #define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
4112 enum {
4113 MWL8K_ALG_WEP,
4114 MWL8K_ALG_TKIP,
4115 MWL8K_ALG_CCMP,
4118 #define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
4119 #define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
4120 #define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
4121 #define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
4122 #define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
4124 static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
4125 struct ieee80211_vif *vif,
4126 u8 *addr,
4127 u8 encr_type)
4129 struct mwl8k_cmd_update_encryption *cmd;
4130 int rc;
4132 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4133 if (cmd == NULL)
4134 return -ENOMEM;
4136 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4137 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4138 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
4139 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4140 cmd->encr_type = encr_type;
4142 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4143 kfree(cmd);
4145 return rc;
4148 static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4149 u8 *addr,
4150 struct ieee80211_key_conf *key)
4152 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4153 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4154 cmd->length = cpu_to_le16(sizeof(*cmd) -
4155 offsetof(struct mwl8k_cmd_set_key, length));
4156 cmd->key_id = cpu_to_le32(key->keyidx);
4157 cmd->key_len = cpu_to_le16(key->keylen);
4158 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4160 switch (key->cipher) {
4161 case WLAN_CIPHER_SUITE_WEP40:
4162 case WLAN_CIPHER_SUITE_WEP104:
4163 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4164 if (key->keyidx == 0)
4165 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4167 break;
4168 case WLAN_CIPHER_SUITE_TKIP:
4169 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4170 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4171 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4172 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4173 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4174 | MWL8K_KEY_FLAG_TSC_VALID);
4175 break;
4176 case WLAN_CIPHER_SUITE_CCMP:
4177 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4178 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4179 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4180 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4181 break;
4182 default:
4183 return -ENOTSUPP;
4186 return 0;
4189 static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4190 struct ieee80211_vif *vif,
4191 u8 *addr,
4192 struct ieee80211_key_conf *key)
4194 struct mwl8k_cmd_set_key *cmd;
4195 int rc;
4196 int keymlen;
4197 u32 action;
4198 u8 idx;
4199 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4201 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4202 if (cmd == NULL)
4203 return -ENOMEM;
4205 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4206 if (rc < 0)
4207 goto done;
4209 idx = key->keyidx;
4211 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4212 action = MWL8K_ENCR_SET_KEY;
4213 else
4214 action = MWL8K_ENCR_SET_GROUP_KEY;
4216 switch (key->cipher) {
4217 case WLAN_CIPHER_SUITE_WEP40:
4218 case WLAN_CIPHER_SUITE_WEP104:
4219 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4220 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4221 sizeof(*key) + key->keylen);
4222 mwl8k_vif->wep_key_conf[idx].enabled = 1;
4225 keymlen = key->keylen;
4226 action = MWL8K_ENCR_SET_KEY;
4227 break;
4228 case WLAN_CIPHER_SUITE_TKIP:
4229 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4230 break;
4231 case WLAN_CIPHER_SUITE_CCMP:
4232 keymlen = key->keylen;
4233 break;
4234 default:
4235 rc = -ENOTSUPP;
4236 goto done;
4239 memcpy(cmd->key_material, key->key, keymlen);
4240 cmd->action = cpu_to_le32(action);
4242 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4243 done:
4244 kfree(cmd);
4246 return rc;
4249 static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4250 struct ieee80211_vif *vif,
4251 u8 *addr,
4252 struct ieee80211_key_conf *key)
4254 struct mwl8k_cmd_set_key *cmd;
4255 int rc;
4256 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4258 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4259 if (cmd == NULL)
4260 return -ENOMEM;
4262 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4263 if (rc < 0)
4264 goto done;
4266 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4267 key->cipher == WLAN_CIPHER_SUITE_WEP104)
4268 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4270 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4272 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4273 done:
4274 kfree(cmd);
4276 return rc;
4279 static int mwl8k_set_key(struct ieee80211_hw *hw,
4280 enum set_key_cmd cmd_param,
4281 struct ieee80211_vif *vif,
4282 struct ieee80211_sta *sta,
4283 struct ieee80211_key_conf *key)
4285 int rc = 0;
4286 u8 encr_type;
4287 u8 *addr;
4288 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4289 struct mwl8k_priv *priv = hw->priv;
4291 if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
4292 return -EOPNOTSUPP;
4294 if (sta == NULL)
4295 addr = vif->addr;
4296 else
4297 addr = sta->addr;
4299 if (cmd_param == SET_KEY) {
4300 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4301 if (rc)
4302 goto out;
4304 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4305 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4306 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4307 else
4308 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4310 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4311 encr_type);
4312 if (rc)
4313 goto out;
4315 mwl8k_vif->is_hw_crypto_enabled = true;
4317 } else {
4318 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4320 if (rc)
4321 goto out;
4323 out:
4324 return rc;
4328 * CMD_UPDATE_STADB.
4330 struct ewc_ht_info {
4331 __le16 control1;
4332 __le16 control2;
4333 __le16 control3;
4334 } __packed;
4336 struct peer_capability_info {
4337 /* Peer type - AP vs. STA. */
4338 __u8 peer_type;
4340 /* Basic 802.11 capabilities from assoc resp. */
4341 __le16 basic_caps;
4343 /* Set if peer supports 802.11n high throughput (HT). */
4344 __u8 ht_support;
4346 /* Valid if HT is supported. */
4347 __le16 ht_caps;
4348 __u8 extended_ht_caps;
4349 struct ewc_ht_info ewc_info;
4351 /* Legacy rate table. Intersection of our rates and peer rates. */
4352 __u8 legacy_rates[12];
4354 /* HT rate table. Intersection of our rates and peer rates. */
4355 __u8 ht_rates[16];
4356 __u8 pad[16];
4358 /* If set, interoperability mode, no proprietary extensions. */
4359 __u8 interop;
4360 __u8 pad2;
4361 __u8 station_id;
4362 __le16 amsdu_enabled;
4363 } __packed;
4365 struct mwl8k_cmd_update_stadb {
4366 struct mwl8k_cmd_pkt header;
4368 /* See STADB_ACTION_TYPE */
4369 __le32 action;
4371 /* Peer MAC address */
4372 __u8 peer_addr[ETH_ALEN];
4374 __le32 reserved;
4376 /* Peer info - valid during add/update. */
4377 struct peer_capability_info peer_info;
4378 } __packed;
4380 #define MWL8K_STA_DB_MODIFY_ENTRY 1
4381 #define MWL8K_STA_DB_DEL_ENTRY 2
4383 /* Peer Entry flags - used to define the type of the peer node */
4384 #define MWL8K_PEER_TYPE_ACCESSPOINT 2
4386 static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
4387 struct ieee80211_vif *vif,
4388 struct ieee80211_sta *sta)
4390 struct mwl8k_cmd_update_stadb *cmd;
4391 struct peer_capability_info *p;
4392 u32 rates;
4393 int rc;
4395 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4396 if (cmd == NULL)
4397 return -ENOMEM;
4399 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4400 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4401 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
4402 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
4404 p = &cmd->peer_info;
4405 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4406 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
4407 p->ht_support = sta->ht_cap.ht_supported;
4408 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
4409 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
4410 ((sta->ht_cap.ampdu_density & 7) << 2);
4411 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
4412 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
4413 else
4414 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4415 legacy_rate_mask_to_array(p->legacy_rates, rates);
4416 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
4417 p->interop = 1;
4418 p->amsdu_enabled = 0;
4420 rc = mwl8k_post_cmd(hw, &cmd->header);
4421 if (!rc)
4422 rc = p->station_id;
4423 kfree(cmd);
4425 return rc;
4428 static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4429 struct ieee80211_vif *vif, u8 *addr)
4431 struct mwl8k_cmd_update_stadb *cmd;
4432 int rc;
4434 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4435 if (cmd == NULL)
4436 return -ENOMEM;
4438 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4439 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4440 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4441 memcpy(cmd->peer_addr, addr, ETH_ALEN);
4443 rc = mwl8k_post_cmd(hw, &cmd->header);
4444 kfree(cmd);
4446 return rc;
4451 * Interrupt handling.
4453 static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4455 struct ieee80211_hw *hw = dev_id;
4456 struct mwl8k_priv *priv = hw->priv;
4457 u32 status;
4459 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4460 if (!status)
4461 return IRQ_NONE;
4463 if (status & MWL8K_A2H_INT_TX_DONE) {
4464 status &= ~MWL8K_A2H_INT_TX_DONE;
4465 tasklet_schedule(&priv->poll_tx_task);
4468 if (status & MWL8K_A2H_INT_RX_READY) {
4469 status &= ~MWL8K_A2H_INT_RX_READY;
4470 tasklet_schedule(&priv->poll_rx_task);
4473 if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
4474 iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
4475 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4477 atomic_inc(&priv->watchdog_event_pending);
4478 status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4479 ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4482 if (status)
4483 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4485 if (status & MWL8K_A2H_INT_OPC_DONE) {
4486 if (priv->hostcmd_wait != NULL)
4487 complete(priv->hostcmd_wait);
4490 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
4491 if (!mutex_is_locked(&priv->fw_mutex) &&
4492 priv->radio_on && priv->pending_tx_pkts)
4493 mwl8k_tx_start(priv);
4496 return IRQ_HANDLED;
4499 static void mwl8k_tx_poll(unsigned long data)
4501 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4502 struct mwl8k_priv *priv = hw->priv;
4503 int limit;
4504 int i;
4506 limit = 32;
4508 spin_lock_bh(&priv->tx_lock);
4510 for (i = 0; i < mwl8k_tx_queues(priv); i++)
4511 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4513 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4514 complete(priv->tx_wait);
4515 priv->tx_wait = NULL;
4518 spin_unlock_bh(&priv->tx_lock);
4520 if (limit) {
4521 writel(~MWL8K_A2H_INT_TX_DONE,
4522 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4523 } else {
4524 tasklet_schedule(&priv->poll_tx_task);
4528 static void mwl8k_rx_poll(unsigned long data)
4530 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4531 struct mwl8k_priv *priv = hw->priv;
4532 int limit;
4534 limit = 32;
4535 limit -= rxq_process(hw, 0, limit);
4536 limit -= rxq_refill(hw, 0, limit);
4538 if (limit) {
4539 writel(~MWL8K_A2H_INT_RX_READY,
4540 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4541 } else {
4542 tasklet_schedule(&priv->poll_rx_task);
4548 * Core driver operations.
4550 static void mwl8k_tx(struct ieee80211_hw *hw,
4551 struct ieee80211_tx_control *control,
4552 struct sk_buff *skb)
4554 struct mwl8k_priv *priv = hw->priv;
4555 int index = skb_get_queue_mapping(skb);
4557 if (!priv->radio_on) {
4558 wiphy_debug(hw->wiphy,
4559 "dropped TX frame since radio disabled\n");
4560 dev_kfree_skb(skb);
4561 return;
4564 mwl8k_txq_xmit(hw, index, control->sta, skb);
4567 static int mwl8k_start(struct ieee80211_hw *hw)
4569 struct mwl8k_priv *priv = hw->priv;
4570 int rc;
4572 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4573 IRQF_SHARED, MWL8K_NAME, hw);
4574 if (rc) {
4575 priv->irq = -1;
4576 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4577 return -EIO;
4579 priv->irq = priv->pdev->irq;
4581 /* Enable TX reclaim and RX tasklets. */
4582 tasklet_enable(&priv->poll_tx_task);
4583 tasklet_enable(&priv->poll_rx_task);
4585 /* Enable interrupts */
4586 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4587 iowrite32(MWL8K_A2H_EVENTS,
4588 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4590 rc = mwl8k_fw_lock(hw);
4591 if (!rc) {
4592 rc = mwl8k_cmd_radio_enable(hw);
4594 if (!priv->ap_fw) {
4595 if (!rc)
4596 rc = mwl8k_cmd_enable_sniffer(hw, 0);
4598 if (!rc)
4599 rc = mwl8k_cmd_set_pre_scan(hw);
4601 if (!rc)
4602 rc = mwl8k_cmd_set_post_scan(hw,
4603 "\x00\x00\x00\x00\x00\x00");
4606 if (!rc)
4607 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
4609 if (!rc)
4610 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
4612 mwl8k_fw_unlock(hw);
4615 if (rc) {
4616 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4617 free_irq(priv->pdev->irq, hw);
4618 priv->irq = -1;
4619 tasklet_disable(&priv->poll_tx_task);
4620 tasklet_disable(&priv->poll_rx_task);
4621 } else {
4622 ieee80211_wake_queues(hw);
4625 return rc;
4628 static void mwl8k_stop(struct ieee80211_hw *hw)
4630 struct mwl8k_priv *priv = hw->priv;
4631 int i;
4633 if (!priv->hw_restart_in_progress)
4634 mwl8k_cmd_radio_disable(hw);
4636 ieee80211_stop_queues(hw);
4638 /* Disable interrupts */
4639 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4640 if (priv->irq != -1) {
4641 free_irq(priv->pdev->irq, hw);
4642 priv->irq = -1;
4645 /* Stop finalize join worker */
4646 cancel_work_sync(&priv->finalize_join_worker);
4647 cancel_work_sync(&priv->watchdog_ba_handle);
4648 if (priv->beacon_skb != NULL)
4649 dev_kfree_skb(priv->beacon_skb);
4651 /* Stop TX reclaim and RX tasklets. */
4652 tasklet_disable(&priv->poll_tx_task);
4653 tasklet_disable(&priv->poll_rx_task);
4655 /* Return all skbs to mac80211 */
4656 for (i = 0; i < mwl8k_tx_queues(priv); i++)
4657 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
4660 static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4662 static int mwl8k_add_interface(struct ieee80211_hw *hw,
4663 struct ieee80211_vif *vif)
4665 struct mwl8k_priv *priv = hw->priv;
4666 struct mwl8k_vif *mwl8k_vif;
4667 u32 macids_supported;
4668 int macid, rc;
4669 struct mwl8k_device_info *di;
4672 * Reject interface creation if sniffer mode is active, as
4673 * STA operation is mutually exclusive with hardware sniffer
4674 * mode. (Sniffer mode is only used on STA firmware.)
4676 if (priv->sniffer_enabled) {
4677 wiphy_info(hw->wiphy,
4678 "unable to create STA interface because sniffer mode is enabled\n");
4679 return -EINVAL;
4682 di = priv->device_info;
4683 switch (vif->type) {
4684 case NL80211_IFTYPE_AP:
4685 if (!priv->ap_fw && di->fw_image_ap) {
4686 /* we must load the ap fw to meet this request */
4687 if (!list_empty(&priv->vif_list))
4688 return -EBUSY;
4689 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4690 if (rc)
4691 return rc;
4693 macids_supported = priv->ap_macids_supported;
4694 break;
4695 case NL80211_IFTYPE_STATION:
4696 if (priv->ap_fw && di->fw_image_sta) {
4697 if (!list_empty(&priv->vif_list)) {
4698 wiphy_warn(hw->wiphy, "AP interface is running.\n"
4699 "Adding STA interface for WDS");
4700 } else {
4701 /* we must load the sta fw to
4702 * meet this request.
4704 rc = mwl8k_reload_firmware(hw,
4705 di->fw_image_sta);
4706 if (rc)
4707 return rc;
4710 macids_supported = priv->sta_macids_supported;
4711 break;
4712 default:
4713 return -EINVAL;
4716 macid = ffs(macids_supported & ~priv->macids_used);
4717 if (!macid--)
4718 return -EBUSY;
4720 /* Setup driver private area. */
4721 mwl8k_vif = MWL8K_VIF(vif);
4722 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
4723 mwl8k_vif->vif = vif;
4724 mwl8k_vif->macid = macid;
4725 mwl8k_vif->seqno = 0;
4726 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4727 mwl8k_vif->is_hw_crypto_enabled = false;
4729 /* Set the mac address. */
4730 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4732 if (vif->type == NL80211_IFTYPE_AP)
4733 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4735 priv->macids_used |= 1 << mwl8k_vif->macid;
4736 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
4738 return 0;
4741 static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4743 /* Has ieee80211_restart_hw re-added the removed interfaces? */
4744 if (!priv->macids_used)
4745 return;
4747 priv->macids_used &= ~(1 << vif->macid);
4748 list_del(&vif->list);
4751 static void mwl8k_remove_interface(struct ieee80211_hw *hw,
4752 struct ieee80211_vif *vif)
4754 struct mwl8k_priv *priv = hw->priv;
4755 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4757 if (vif->type == NL80211_IFTYPE_AP)
4758 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4760 mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
4762 mwl8k_remove_vif(priv, mwl8k_vif);
4765 static void mwl8k_hw_restart_work(struct work_struct *work)
4767 struct mwl8k_priv *priv =
4768 container_of(work, struct mwl8k_priv, fw_reload);
4769 struct ieee80211_hw *hw = priv->hw;
4770 struct mwl8k_device_info *di;
4771 int rc;
4773 /* If some command is waiting for a response, clear it */
4774 if (priv->hostcmd_wait != NULL) {
4775 complete(priv->hostcmd_wait);
4776 priv->hostcmd_wait = NULL;
4779 priv->hw_restart_owner = current;
4780 di = priv->device_info;
4781 mwl8k_fw_lock(hw);
4783 if (priv->ap_fw)
4784 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4785 else
4786 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4788 if (rc)
4789 goto fail;
4791 priv->hw_restart_owner = NULL;
4792 priv->hw_restart_in_progress = false;
4795 * This unlock will wake up the queues and
4796 * also opens the command path for other
4797 * commands
4799 mwl8k_fw_unlock(hw);
4801 ieee80211_restart_hw(hw);
4803 wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
4805 return;
4806 fail:
4807 mwl8k_fw_unlock(hw);
4809 wiphy_err(hw->wiphy, "Firmware restart failed\n");
4812 static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
4814 struct ieee80211_conf *conf = &hw->conf;
4815 struct mwl8k_priv *priv = hw->priv;
4816 int rc;
4818 rc = mwl8k_fw_lock(hw);
4819 if (rc)
4820 return rc;
4822 if (conf->flags & IEEE80211_CONF_IDLE)
4823 rc = mwl8k_cmd_radio_disable(hw);
4824 else
4825 rc = mwl8k_cmd_radio_enable(hw);
4826 if (rc)
4827 goto out;
4829 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
4830 rc = mwl8k_cmd_set_rf_channel(hw, conf);
4831 if (rc)
4832 goto out;
4835 if (conf->power_level > 18)
4836 conf->power_level = 18;
4838 if (priv->ap_fw) {
4840 if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
4841 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4842 if (rc)
4843 goto out;
4847 } else {
4848 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4849 if (rc)
4850 goto out;
4851 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4854 out:
4855 mwl8k_fw_unlock(hw);
4857 return rc;
4860 static void
4861 mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4862 struct ieee80211_bss_conf *info, u32 changed)
4864 struct mwl8k_priv *priv = hw->priv;
4865 u32 ap_legacy_rates = 0;
4866 u8 ap_mcs_rates[16];
4867 int rc;
4869 if (mwl8k_fw_lock(hw))
4870 return;
4873 * No need to capture a beacon if we're no longer associated.
4875 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4876 priv->capture_beacon = false;
4879 * Get the AP's legacy and MCS rates.
4881 if (vif->bss_conf.assoc) {
4882 struct ieee80211_sta *ap;
4884 rcu_read_lock();
4886 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
4887 if (ap == NULL) {
4888 rcu_read_unlock();
4889 goto out;
4892 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ) {
4893 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4894 } else {
4895 ap_legacy_rates =
4896 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4898 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
4900 rcu_read_unlock();
4903 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4904 !priv->ap_fw) {
4905 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
4906 if (rc)
4907 goto out;
4909 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
4910 if (rc)
4911 goto out;
4912 } else {
4913 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4914 priv->ap_fw) {
4915 int idx;
4916 int rate;
4918 /* Use AP firmware specific rate command.
4920 idx = ffs(vif->bss_conf.basic_rates);
4921 if (idx)
4922 idx--;
4924 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
4925 rate = mwl8k_rates_24[idx].hw_value;
4926 else
4927 rate = mwl8k_rates_50[idx].hw_value;
4929 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4933 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4934 rc = mwl8k_set_radio_preamble(hw,
4935 vif->bss_conf.use_short_preamble);
4936 if (rc)
4937 goto out;
4940 if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw) {
4941 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
4942 if (rc)
4943 goto out;
4946 if (vif->bss_conf.assoc && !priv->ap_fw &&
4947 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4948 BSS_CHANGED_HT))) {
4949 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
4950 if (rc)
4951 goto out;
4954 if (vif->bss_conf.assoc &&
4955 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
4957 * Finalize the join. Tell rx handler to process
4958 * next beacon from our BSSID.
4960 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
4961 priv->capture_beacon = true;
4964 out:
4965 mwl8k_fw_unlock(hw);
4968 static void
4969 mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4970 struct ieee80211_bss_conf *info, u32 changed)
4972 int rc;
4974 if (mwl8k_fw_lock(hw))
4975 return;
4977 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4978 rc = mwl8k_set_radio_preamble(hw,
4979 vif->bss_conf.use_short_preamble);
4980 if (rc)
4981 goto out;
4984 if (changed & BSS_CHANGED_BASIC_RATES) {
4985 int idx;
4986 int rate;
4989 * Use lowest supported basic rate for multicasts
4990 * and management frames (such as probe responses --
4991 * beacons will always go out at 1 Mb/s).
4993 idx = ffs(vif->bss_conf.basic_rates);
4994 if (idx)
4995 idx--;
4997 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
4998 rate = mwl8k_rates_24[idx].hw_value;
4999 else
5000 rate = mwl8k_rates_50[idx].hw_value;
5002 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
5005 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
5006 struct sk_buff *skb;
5008 skb = ieee80211_beacon_get(hw, vif);
5009 if (skb != NULL) {
5010 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
5011 kfree_skb(skb);
5015 if (changed & BSS_CHANGED_BEACON_ENABLED)
5016 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
5018 out:
5019 mwl8k_fw_unlock(hw);
5022 static void
5023 mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5024 struct ieee80211_bss_conf *info, u32 changed)
5026 if (vif->type == NL80211_IFTYPE_STATION)
5027 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
5028 if (vif->type == NL80211_IFTYPE_AP)
5029 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
5032 static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
5033 struct netdev_hw_addr_list *mc_list)
5035 struct mwl8k_cmd_pkt *cmd;
5038 * Synthesize and return a command packet that programs the
5039 * hardware multicast address filter. At this point we don't
5040 * know whether FIF_ALLMULTI is being requested, but if it is,
5041 * we'll end up throwing this packet away and creating a new
5042 * one in mwl8k_configure_filter().
5044 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
5046 return (unsigned long)cmd;
5049 static int
5050 mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
5051 unsigned int changed_flags,
5052 unsigned int *total_flags)
5054 struct mwl8k_priv *priv = hw->priv;
5057 * Hardware sniffer mode is mutually exclusive with STA
5058 * operation, so refuse to enable sniffer mode if a STA
5059 * interface is active.
5061 if (!list_empty(&priv->vif_list)) {
5062 if (net_ratelimit())
5063 wiphy_info(hw->wiphy,
5064 "not enabling sniffer mode because STA interface is active\n");
5065 return 0;
5068 if (!priv->sniffer_enabled) {
5069 if (mwl8k_cmd_enable_sniffer(hw, 1))
5070 return 0;
5071 priv->sniffer_enabled = true;
5074 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
5075 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
5076 FIF_OTHER_BSS;
5078 return 1;
5081 static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
5083 if (!list_empty(&priv->vif_list))
5084 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
5086 return NULL;
5089 static void mwl8k_configure_filter(struct ieee80211_hw *hw,
5090 unsigned int changed_flags,
5091 unsigned int *total_flags,
5092 u64 multicast)
5094 struct mwl8k_priv *priv = hw->priv;
5095 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
5098 * AP firmware doesn't allow fine-grained control over
5099 * the receive filter.
5101 if (priv->ap_fw) {
5102 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5103 kfree(cmd);
5104 return;
5108 * Enable hardware sniffer mode if FIF_CONTROL or
5109 * FIF_OTHER_BSS is requested.
5111 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
5112 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
5113 kfree(cmd);
5114 return;
5117 /* Clear unsupported feature flags */
5118 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5120 if (mwl8k_fw_lock(hw)) {
5121 kfree(cmd);
5122 return;
5125 if (priv->sniffer_enabled) {
5126 mwl8k_cmd_enable_sniffer(hw, 0);
5127 priv->sniffer_enabled = false;
5130 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
5131 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
5133 * Disable the BSS filter.
5135 mwl8k_cmd_set_pre_scan(hw);
5136 } else {
5137 struct mwl8k_vif *mwl8k_vif;
5138 const u8 *bssid;
5141 * Enable the BSS filter.
5143 * If there is an active STA interface, use that
5144 * interface's BSSID, otherwise use a dummy one
5145 * (where the OUI part needs to be nonzero for
5146 * the BSSID to be accepted by POST_SCAN).
5148 mwl8k_vif = mwl8k_first_vif(priv);
5149 if (mwl8k_vif != NULL)
5150 bssid = mwl8k_vif->vif->bss_conf.bssid;
5151 else
5152 bssid = "\x01\x00\x00\x00\x00\x00";
5154 mwl8k_cmd_set_post_scan(hw, bssid);
5159 * If FIF_ALLMULTI is being requested, throw away the command
5160 * packet that ->prepare_multicast() built and replace it with
5161 * a command packet that enables reception of all multicast
5162 * packets.
5164 if (*total_flags & FIF_ALLMULTI) {
5165 kfree(cmd);
5166 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
5169 if (cmd != NULL) {
5170 mwl8k_post_cmd(hw, cmd);
5171 kfree(cmd);
5174 mwl8k_fw_unlock(hw);
5177 static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5179 return mwl8k_cmd_set_rts_threshold(hw, value);
5182 static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5183 struct ieee80211_vif *vif,
5184 struct ieee80211_sta *sta)
5186 struct mwl8k_priv *priv = hw->priv;
5188 if (priv->ap_fw)
5189 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5190 else
5191 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5194 static int mwl8k_sta_add(struct ieee80211_hw *hw,
5195 struct ieee80211_vif *vif,
5196 struct ieee80211_sta *sta)
5198 struct mwl8k_priv *priv = hw->priv;
5199 int ret;
5200 int i;
5201 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5202 struct ieee80211_key_conf *key;
5204 if (!priv->ap_fw) {
5205 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5206 if (ret >= 0) {
5207 MWL8K_STA(sta)->peer_id = ret;
5208 if (sta->ht_cap.ht_supported)
5209 MWL8K_STA(sta)->is_ampdu_allowed = true;
5210 ret = 0;
5213 } else {
5214 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
5217 for (i = 0; i < NUM_WEP_KEYS; i++) {
5218 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5219 if (mwl8k_vif->wep_key_conf[i].enabled)
5220 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5222 return ret;
5225 static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5226 struct ieee80211_vif *vif, u16 queue,
5227 const struct ieee80211_tx_queue_params *params)
5229 struct mwl8k_priv *priv = hw->priv;
5230 int rc;
5232 rc = mwl8k_fw_lock(hw);
5233 if (!rc) {
5234 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
5235 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5237 if (!priv->wmm_enabled)
5238 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
5240 if (!rc) {
5241 int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
5242 rc = mwl8k_cmd_set_edca_params(hw, q,
5243 params->cw_min,
5244 params->cw_max,
5245 params->aifs,
5246 params->txop);
5249 mwl8k_fw_unlock(hw);
5252 return rc;
5255 static int mwl8k_get_stats(struct ieee80211_hw *hw,
5256 struct ieee80211_low_level_stats *stats)
5258 return mwl8k_cmd_get_stat(hw, stats);
5261 static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5262 struct survey_info *survey)
5264 struct mwl8k_priv *priv = hw->priv;
5265 struct ieee80211_conf *conf = &hw->conf;
5267 if (idx != 0)
5268 return -ENOENT;
5270 survey->channel = conf->chandef.chan;
5271 survey->filled = SURVEY_INFO_NOISE_DBM;
5272 survey->noise = priv->noise;
5274 return 0;
5277 #define MAX_AMPDU_ATTEMPTS 5
5279 static int
5280 mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5281 enum ieee80211_ampdu_mlme_action action,
5282 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5283 u8 buf_size)
5286 int i, rc = 0;
5287 struct mwl8k_priv *priv = hw->priv;
5288 struct mwl8k_ampdu_stream *stream;
5289 u8 *addr = sta->addr, idx;
5290 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
5292 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
5293 return -ENOTSUPP;
5295 spin_lock(&priv->stream_lock);
5296 stream = mwl8k_lookup_stream(hw, addr, tid);
5298 switch (action) {
5299 case IEEE80211_AMPDU_RX_START:
5300 case IEEE80211_AMPDU_RX_STOP:
5301 break;
5302 case IEEE80211_AMPDU_TX_START:
5303 /* By the time we get here the hw queues may contain outgoing
5304 * packets for this RA/TID that are not part of this BA
5305 * session. The hw will assign sequence numbers to these
5306 * packets as they go out. So if we query the hw for its next
5307 * sequence number and use that for the SSN here, it may end up
5308 * being wrong, which will lead to sequence number mismatch at
5309 * the recipient. To avoid this, we reset the sequence number
5310 * to O for the first MPDU in this BA stream.
5312 *ssn = 0;
5313 if (stream == NULL) {
5314 /* This means that somebody outside this driver called
5315 * ieee80211_start_tx_ba_session. This is unexpected
5316 * because we do our own rate control. Just warn and
5317 * move on.
5319 wiphy_warn(hw->wiphy, "Unexpected call to %s. "
5320 "Proceeding anyway.\n", __func__);
5321 stream = mwl8k_add_stream(hw, sta, tid);
5323 if (stream == NULL) {
5324 wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5325 rc = -EBUSY;
5326 break;
5328 stream->state = AMPDU_STREAM_IN_PROGRESS;
5330 /* Release the lock before we do the time consuming stuff */
5331 spin_unlock(&priv->stream_lock);
5332 for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
5334 /* Check if link is still valid */
5335 if (!sta_info->is_ampdu_allowed) {
5336 spin_lock(&priv->stream_lock);
5337 mwl8k_remove_stream(hw, stream);
5338 spin_unlock(&priv->stream_lock);
5339 return -EBUSY;
5342 rc = mwl8k_check_ba(hw, stream, vif);
5344 /* If HW restart is in progress mwl8k_post_cmd will
5345 * return -EBUSY. Avoid retrying mwl8k_check_ba in
5346 * such cases
5348 if (!rc || rc == -EBUSY)
5349 break;
5351 * HW queues take time to be flushed, give them
5352 * sufficient time
5355 msleep(1000);
5357 spin_lock(&priv->stream_lock);
5358 if (rc) {
5359 wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5360 " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5361 mwl8k_remove_stream(hw, stream);
5362 rc = -EBUSY;
5363 break;
5365 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
5366 break;
5367 case IEEE80211_AMPDU_TX_STOP_CONT:
5368 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5369 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5370 if (stream) {
5371 if (stream->state == AMPDU_STREAM_ACTIVE) {
5372 idx = stream->idx;
5373 spin_unlock(&priv->stream_lock);
5374 mwl8k_destroy_ba(hw, idx);
5375 spin_lock(&priv->stream_lock);
5377 mwl8k_remove_stream(hw, stream);
5379 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5380 break;
5381 case IEEE80211_AMPDU_TX_OPERATIONAL:
5382 BUG_ON(stream == NULL);
5383 BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5384 spin_unlock(&priv->stream_lock);
5385 rc = mwl8k_create_ba(hw, stream, buf_size, vif);
5386 spin_lock(&priv->stream_lock);
5387 if (!rc)
5388 stream->state = AMPDU_STREAM_ACTIVE;
5389 else {
5390 idx = stream->idx;
5391 spin_unlock(&priv->stream_lock);
5392 mwl8k_destroy_ba(hw, idx);
5393 spin_lock(&priv->stream_lock);
5394 wiphy_debug(hw->wiphy,
5395 "Failed adding stream for sta %pM tid %d\n",
5396 addr, tid);
5397 mwl8k_remove_stream(hw, stream);
5399 break;
5401 default:
5402 rc = -ENOTSUPP;
5405 spin_unlock(&priv->stream_lock);
5406 return rc;
5409 static const struct ieee80211_ops mwl8k_ops = {
5410 .tx = mwl8k_tx,
5411 .start = mwl8k_start,
5412 .stop = mwl8k_stop,
5413 .add_interface = mwl8k_add_interface,
5414 .remove_interface = mwl8k_remove_interface,
5415 .config = mwl8k_config,
5416 .bss_info_changed = mwl8k_bss_info_changed,
5417 .prepare_multicast = mwl8k_prepare_multicast,
5418 .configure_filter = mwl8k_configure_filter,
5419 .set_key = mwl8k_set_key,
5420 .set_rts_threshold = mwl8k_set_rts_threshold,
5421 .sta_add = mwl8k_sta_add,
5422 .sta_remove = mwl8k_sta_remove,
5423 .conf_tx = mwl8k_conf_tx,
5424 .get_stats = mwl8k_get_stats,
5425 .get_survey = mwl8k_get_survey,
5426 .ampdu_action = mwl8k_ampdu_action,
5429 static void mwl8k_finalize_join_worker(struct work_struct *work)
5431 struct mwl8k_priv *priv =
5432 container_of(work, struct mwl8k_priv, finalize_join_worker);
5433 struct sk_buff *skb = priv->beacon_skb;
5434 struct ieee80211_mgmt *mgmt = (void *)skb->data;
5435 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5436 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5437 mgmt->u.beacon.variable, len);
5438 int dtim_period = 1;
5440 if (tim && tim[1] >= 2)
5441 dtim_period = tim[3];
5443 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
5445 dev_kfree_skb(skb);
5446 priv->beacon_skb = NULL;
5449 enum {
5450 MWL8363 = 0,
5451 MWL8687,
5452 MWL8366,
5453 MWL8764,
5456 #define MWL8K_8366_AP_FW_API 3
5457 #define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5458 #define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5460 #define MWL8K_8764_AP_FW_API 1
5461 #define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
5462 #define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
5464 static struct mwl8k_device_info mwl8k_info_tbl[] = {
5465 [MWL8363] = {
5466 .part_name = "88w8363",
5467 .helper_image = "mwl8k/helper_8363.fw",
5468 .fw_image_sta = "mwl8k/fmimage_8363.fw",
5470 [MWL8687] = {
5471 .part_name = "88w8687",
5472 .helper_image = "mwl8k/helper_8687.fw",
5473 .fw_image_sta = "mwl8k/fmimage_8687.fw",
5475 [MWL8366] = {
5476 .part_name = "88w8366",
5477 .helper_image = "mwl8k/helper_8366.fw",
5478 .fw_image_sta = "mwl8k/fmimage_8366.fw",
5479 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5480 .fw_api_ap = MWL8K_8366_AP_FW_API,
5481 .ap_rxd_ops = &rxd_ap_ops,
5483 [MWL8764] = {
5484 .part_name = "88w8764",
5485 .fw_image_ap = MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
5486 .fw_api_ap = MWL8K_8764_AP_FW_API,
5487 .ap_rxd_ops = &rxd_ap_ops,
5491 MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5492 MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5493 MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5494 MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5495 MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5496 MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
5497 MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
5499 static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
5500 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
5501 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5502 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
5503 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5504 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5505 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
5506 { PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
5507 { PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
5508 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
5509 { PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
5510 { },
5512 MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5514 static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5516 int rc;
5517 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5518 "Trying alternative firmware %s\n", pci_name(priv->pdev),
5519 priv->fw_pref, priv->fw_alt);
5520 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5521 if (rc) {
5522 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5523 pci_name(priv->pdev), priv->fw_alt);
5524 return rc;
5526 return 0;
5529 static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5530 static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5532 struct mwl8k_priv *priv = context;
5533 struct mwl8k_device_info *di = priv->device_info;
5534 int rc;
5536 switch (priv->fw_state) {
5537 case FW_STATE_INIT:
5538 if (!fw) {
5539 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5540 pci_name(priv->pdev), di->helper_image);
5541 goto fail;
5543 priv->fw_helper = fw;
5544 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5545 true);
5546 if (rc && priv->fw_alt) {
5547 rc = mwl8k_request_alt_fw(priv);
5548 if (rc)
5549 goto fail;
5550 priv->fw_state = FW_STATE_LOADING_ALT;
5551 } else if (rc)
5552 goto fail;
5553 else
5554 priv->fw_state = FW_STATE_LOADING_PREF;
5555 break;
5557 case FW_STATE_LOADING_PREF:
5558 if (!fw) {
5559 if (priv->fw_alt) {
5560 rc = mwl8k_request_alt_fw(priv);
5561 if (rc)
5562 goto fail;
5563 priv->fw_state = FW_STATE_LOADING_ALT;
5564 } else
5565 goto fail;
5566 } else {
5567 priv->fw_ucode = fw;
5568 rc = mwl8k_firmware_load_success(priv);
5569 if (rc)
5570 goto fail;
5571 else
5572 complete(&priv->firmware_loading_complete);
5574 break;
5576 case FW_STATE_LOADING_ALT:
5577 if (!fw) {
5578 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5579 pci_name(priv->pdev), di->helper_image);
5580 goto fail;
5582 priv->fw_ucode = fw;
5583 rc = mwl8k_firmware_load_success(priv);
5584 if (rc)
5585 goto fail;
5586 else
5587 complete(&priv->firmware_loading_complete);
5588 break;
5590 default:
5591 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5592 MWL8K_NAME, priv->fw_state);
5593 BUG_ON(1);
5596 return;
5598 fail:
5599 priv->fw_state = FW_STATE_ERROR;
5600 complete(&priv->firmware_loading_complete);
5601 device_release_driver(&priv->pdev->dev);
5602 mwl8k_release_firmware(priv);
5605 #define MAX_RESTART_ATTEMPTS 1
5606 static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5607 bool nowait)
5609 struct mwl8k_priv *priv = hw->priv;
5610 int rc;
5611 int count = MAX_RESTART_ATTEMPTS;
5613 retry:
5614 /* Reset firmware and hardware */
5615 mwl8k_hw_reset(priv);
5617 /* Ask userland hotplug daemon for the device firmware */
5618 rc = mwl8k_request_firmware(priv, fw_image, nowait);
5619 if (rc) {
5620 wiphy_err(hw->wiphy, "Firmware files not found\n");
5621 return rc;
5624 if (nowait)
5625 return rc;
5627 /* Load firmware into hardware */
5628 rc = mwl8k_load_firmware(hw);
5629 if (rc)
5630 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5632 /* Reclaim memory once firmware is successfully loaded */
5633 mwl8k_release_firmware(priv);
5635 if (rc && count) {
5636 /* FW did not start successfully;
5637 * lets try one more time
5639 count--;
5640 wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5641 msleep(20);
5642 goto retry;
5645 return rc;
5648 static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5650 struct mwl8k_priv *priv = hw->priv;
5651 int rc = 0;
5652 int i;
5654 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
5655 rc = mwl8k_txq_init(hw, i);
5656 if (rc)
5657 break;
5658 if (priv->ap_fw)
5659 iowrite32(priv->txq[i].txd_dma,
5660 priv->sram + priv->txq_offset[i]);
5662 return rc;
5665 /* initialize hw after successfully loading a firmware image */
5666 static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5668 struct mwl8k_priv *priv = hw->priv;
5669 int rc = 0;
5670 int i;
5672 if (priv->ap_fw) {
5673 priv->rxd_ops = priv->device_info->ap_rxd_ops;
5674 if (priv->rxd_ops == NULL) {
5675 wiphy_err(hw->wiphy,
5676 "Driver does not have AP firmware image support for this hardware\n");
5677 rc = -ENOENT;
5678 goto err_stop_firmware;
5680 } else {
5681 priv->rxd_ops = &rxd_sta_ops;
5684 priv->sniffer_enabled = false;
5685 priv->wmm_enabled = false;
5686 priv->pending_tx_pkts = 0;
5687 atomic_set(&priv->watchdog_event_pending, 0);
5689 rc = mwl8k_rxq_init(hw, 0);
5690 if (rc)
5691 goto err_stop_firmware;
5692 rxq_refill(hw, 0, INT_MAX);
5694 /* For the sta firmware, we need to know the dma addresses of tx queues
5695 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
5696 * prior to issuing this command. But for the AP case, we learn the
5697 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5698 * case we must initialize the tx queues after.
5700 priv->num_ampdu_queues = 0;
5701 if (!priv->ap_fw) {
5702 rc = mwl8k_init_txqs(hw);
5703 if (rc)
5704 goto err_free_queues;
5707 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5708 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5709 iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5710 MWL8K_A2H_INT_BA_WATCHDOG,
5711 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
5712 iowrite32(MWL8K_A2H_INT_OPC_DONE,
5713 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
5715 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5716 IRQF_SHARED, MWL8K_NAME, hw);
5717 if (rc) {
5718 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5719 goto err_free_queues;
5723 * When hw restart is requested,
5724 * mac80211 will take care of clearing
5725 * the ampdu streams, so do not clear
5726 * the ampdu state here
5728 if (!priv->hw_restart_in_progress)
5729 memset(priv->ampdu, 0, sizeof(priv->ampdu));
5732 * Temporarily enable interrupts. Initial firmware host
5733 * commands use interrupts and avoid polling. Disable
5734 * interrupts when done.
5736 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5738 /* Get config data, mac addrs etc */
5739 if (priv->ap_fw) {
5740 rc = mwl8k_cmd_get_hw_spec_ap(hw);
5741 if (!rc)
5742 rc = mwl8k_init_txqs(hw);
5743 if (!rc)
5744 rc = mwl8k_cmd_set_hw_spec(hw);
5745 } else {
5746 rc = mwl8k_cmd_get_hw_spec_sta(hw);
5748 if (rc) {
5749 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5750 goto err_free_irq;
5753 /* Turn radio off */
5754 rc = mwl8k_cmd_radio_disable(hw);
5755 if (rc) {
5756 wiphy_err(hw->wiphy, "Cannot disable\n");
5757 goto err_free_irq;
5760 /* Clear MAC address */
5761 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5762 if (rc) {
5763 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5764 goto err_free_irq;
5767 /* Configure Antennas */
5768 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
5769 if (rc)
5770 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
5771 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
5772 if (rc)
5773 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
5776 /* Disable interrupts */
5777 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5778 free_irq(priv->pdev->irq, hw);
5780 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5781 priv->device_info->part_name,
5782 priv->hw_rev, hw->wiphy->perm_addr,
5783 priv->ap_fw ? "AP" : "STA",
5784 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5785 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5787 return 0;
5789 err_free_irq:
5790 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5791 free_irq(priv->pdev->irq, hw);
5793 err_free_queues:
5794 for (i = 0; i < mwl8k_tx_queues(priv); i++)
5795 mwl8k_txq_deinit(hw, i);
5796 mwl8k_rxq_deinit(hw, 0);
5798 err_stop_firmware:
5799 mwl8k_hw_reset(priv);
5801 return rc;
5805 * invoke mwl8k_reload_firmware to change the firmware image after the device
5806 * has already been registered
5808 static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5810 int i, rc = 0;
5811 struct mwl8k_priv *priv = hw->priv;
5812 struct mwl8k_vif *vif, *tmp_vif;
5814 mwl8k_stop(hw);
5815 mwl8k_rxq_deinit(hw, 0);
5818 * All the existing interfaces are re-added by the ieee80211_reconfig;
5819 * which means driver should remove existing interfaces before calling
5820 * ieee80211_restart_hw
5822 if (priv->hw_restart_in_progress)
5823 list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
5824 mwl8k_remove_vif(priv, vif);
5826 for (i = 0; i < mwl8k_tx_queues(priv); i++)
5827 mwl8k_txq_deinit(hw, i);
5829 rc = mwl8k_init_firmware(hw, fw_image, false);
5830 if (rc)
5831 goto fail;
5833 rc = mwl8k_probe_hw(hw);
5834 if (rc)
5835 goto fail;
5837 if (priv->hw_restart_in_progress)
5838 return rc;
5840 rc = mwl8k_start(hw);
5841 if (rc)
5842 goto fail;
5844 rc = mwl8k_config(hw, ~0);
5845 if (rc)
5846 goto fail;
5848 for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
5849 rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
5850 if (rc)
5851 goto fail;
5854 return rc;
5856 fail:
5857 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5858 return rc;
5861 static const struct ieee80211_iface_limit ap_if_limits[] = {
5862 { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
5863 { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
5866 static const struct ieee80211_iface_combination ap_if_comb = {
5867 .limits = ap_if_limits,
5868 .n_limits = ARRAY_SIZE(ap_if_limits),
5869 .max_interfaces = 8,
5870 .num_different_channels = 1,
5874 static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5876 struct ieee80211_hw *hw = priv->hw;
5877 int i, rc;
5879 rc = mwl8k_load_firmware(hw);
5880 mwl8k_release_firmware(priv);
5881 if (rc) {
5882 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5883 return rc;
5887 * Extra headroom is the size of the required DMA header
5888 * minus the size of the smallest 802.11 frame (CTS frame).
5890 hw->extra_tx_headroom =
5891 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5893 hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
5895 hw->queues = MWL8K_TX_WMM_QUEUES;
5897 /* Set rssi values to dBm */
5898 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
5901 * Ask mac80211 to not to trigger PS mode
5902 * based on PM bit of incoming frames.
5904 if (priv->ap_fw)
5905 hw->flags |= IEEE80211_HW_AP_LINK_PS;
5907 hw->vif_data_size = sizeof(struct mwl8k_vif);
5908 hw->sta_data_size = sizeof(struct mwl8k_sta);
5910 priv->macids_used = 0;
5911 INIT_LIST_HEAD(&priv->vif_list);
5913 /* Set default radio state and preamble */
5914 priv->radio_on = false;
5915 priv->radio_short_preamble = false;
5917 /* Finalize join worker */
5918 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
5919 /* Handle watchdog ba events */
5920 INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
5921 /* To reload the firmware if it crashes */
5922 INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
5924 /* TX reclaim and RX tasklets. */
5925 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5926 tasklet_disable(&priv->poll_tx_task);
5927 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5928 tasklet_disable(&priv->poll_rx_task);
5930 /* Power management cookie */
5931 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5932 if (priv->cookie == NULL)
5933 return -ENOMEM;
5935 mutex_init(&priv->fw_mutex);
5936 priv->fw_mutex_owner = NULL;
5937 priv->fw_mutex_depth = 0;
5938 priv->hostcmd_wait = NULL;
5940 spin_lock_init(&priv->tx_lock);
5942 spin_lock_init(&priv->stream_lock);
5944 priv->tx_wait = NULL;
5946 rc = mwl8k_probe_hw(hw);
5947 if (rc)
5948 goto err_free_cookie;
5950 hw->wiphy->interface_modes = 0;
5952 if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
5953 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
5954 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5955 hw->wiphy->iface_combinations = &ap_if_comb;
5956 hw->wiphy->n_iface_combinations = 1;
5959 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5960 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5962 rc = ieee80211_register_hw(hw);
5963 if (rc) {
5964 wiphy_err(hw->wiphy, "Cannot register device\n");
5965 goto err_unprobe_hw;
5968 return 0;
5970 err_unprobe_hw:
5971 for (i = 0; i < mwl8k_tx_queues(priv); i++)
5972 mwl8k_txq_deinit(hw, i);
5973 mwl8k_rxq_deinit(hw, 0);
5975 err_free_cookie:
5976 if (priv->cookie != NULL)
5977 pci_free_consistent(priv->pdev, 4,
5978 priv->cookie, priv->cookie_dma);
5980 return rc;
5982 static int mwl8k_probe(struct pci_dev *pdev,
5983 const struct pci_device_id *id)
5985 static int printed_version;
5986 struct ieee80211_hw *hw;
5987 struct mwl8k_priv *priv;
5988 struct mwl8k_device_info *di;
5989 int rc;
5991 if (!printed_version) {
5992 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5993 printed_version = 1;
5997 rc = pci_enable_device(pdev);
5998 if (rc) {
5999 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
6000 MWL8K_NAME);
6001 return rc;
6004 rc = pci_request_regions(pdev, MWL8K_NAME);
6005 if (rc) {
6006 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
6007 MWL8K_NAME);
6008 goto err_disable_device;
6011 pci_set_master(pdev);
6014 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
6015 if (hw == NULL) {
6016 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
6017 rc = -ENOMEM;
6018 goto err_free_reg;
6021 SET_IEEE80211_DEV(hw, &pdev->dev);
6022 pci_set_drvdata(pdev, hw);
6024 priv = hw->priv;
6025 priv->hw = hw;
6026 priv->pdev = pdev;
6027 priv->device_info = &mwl8k_info_tbl[id->driver_data];
6029 if (id->driver_data == MWL8764)
6030 priv->is_8764 = true;
6032 priv->sram = pci_iomap(pdev, 0, 0x10000);
6033 if (priv->sram == NULL) {
6034 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
6035 rc = -EIO;
6036 goto err_iounmap;
6040 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
6041 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
6043 priv->regs = pci_iomap(pdev, 1, 0x10000);
6044 if (priv->regs == NULL) {
6045 priv->regs = pci_iomap(pdev, 2, 0x10000);
6046 if (priv->regs == NULL) {
6047 wiphy_err(hw->wiphy, "Cannot map device registers\n");
6048 rc = -EIO;
6049 goto err_iounmap;
6054 * Choose the initial fw image depending on user input. If a second
6055 * image is available, make it the alternative image that will be
6056 * loaded if the first one fails.
6058 init_completion(&priv->firmware_loading_complete);
6059 di = priv->device_info;
6060 if (ap_mode_default && di->fw_image_ap) {
6061 priv->fw_pref = di->fw_image_ap;
6062 priv->fw_alt = di->fw_image_sta;
6063 } else if (!ap_mode_default && di->fw_image_sta) {
6064 priv->fw_pref = di->fw_image_sta;
6065 priv->fw_alt = di->fw_image_ap;
6066 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
6067 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
6068 priv->fw_pref = di->fw_image_sta;
6069 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
6070 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
6071 priv->fw_pref = di->fw_image_ap;
6073 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
6074 if (rc)
6075 goto err_stop_firmware;
6077 priv->hw_restart_in_progress = false;
6079 priv->running_bsses = 0;
6081 return rc;
6083 err_stop_firmware:
6084 mwl8k_hw_reset(priv);
6086 err_iounmap:
6087 if (priv->regs != NULL)
6088 pci_iounmap(pdev, priv->regs);
6090 if (priv->sram != NULL)
6091 pci_iounmap(pdev, priv->sram);
6093 ieee80211_free_hw(hw);
6095 err_free_reg:
6096 pci_release_regions(pdev);
6098 err_disable_device:
6099 pci_disable_device(pdev);
6101 return rc;
6104 static void mwl8k_remove(struct pci_dev *pdev)
6106 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
6107 struct mwl8k_priv *priv;
6108 int i;
6110 if (hw == NULL)
6111 return;
6112 priv = hw->priv;
6114 wait_for_completion(&priv->firmware_loading_complete);
6116 if (priv->fw_state == FW_STATE_ERROR) {
6117 mwl8k_hw_reset(priv);
6118 goto unmap;
6121 ieee80211_stop_queues(hw);
6123 ieee80211_unregister_hw(hw);
6125 /* Remove TX reclaim and RX tasklets. */
6126 tasklet_kill(&priv->poll_tx_task);
6127 tasklet_kill(&priv->poll_rx_task);
6129 /* Stop hardware */
6130 mwl8k_hw_reset(priv);
6132 /* Return all skbs to mac80211 */
6133 for (i = 0; i < mwl8k_tx_queues(priv); i++)
6134 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
6136 for (i = 0; i < mwl8k_tx_queues(priv); i++)
6137 mwl8k_txq_deinit(hw, i);
6139 mwl8k_rxq_deinit(hw, 0);
6141 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
6143 unmap:
6144 pci_iounmap(pdev, priv->regs);
6145 pci_iounmap(pdev, priv->sram);
6146 ieee80211_free_hw(hw);
6147 pci_release_regions(pdev);
6148 pci_disable_device(pdev);
6151 static struct pci_driver mwl8k_driver = {
6152 .name = MWL8K_NAME,
6153 .id_table = mwl8k_pci_id_table,
6154 .probe = mwl8k_probe,
6155 .remove = mwl8k_remove,
6158 module_pci_driver(mwl8k_driver);
6160 MODULE_DESCRIPTION(MWL8K_DESC);
6161 MODULE_VERSION(MWL8K_VERSION);
6162 MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
6163 MODULE_LICENSE("GPL");