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