blob: 627ac9b320123e41b1902dd641433cef3924364a [file] [log] [blame]
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08001/******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/etherdevice.h>
32#include <linux/sched.h>
33#include <linux/slab.h>
Stanislaw Gruszka0cdc2132011-11-15 13:40:15 +010034#include <linux/types.h>
35#include <linux/lockdep.h>
36#include <linux/init.h>
37#include <linux/pci.h>
38#include <linux/dma-mapping.h>
39#include <linux/delay.h>
40#include <linux/skbuff.h>
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080041#include <net/mac80211.h>
42
Stanislaw Gruszka98613be2011-11-15 14:19:34 +010043#include "common.h"
Wey-Yi Guybe663ab2011-02-21 11:27:26 -080044
Stanislaw Gruszka0cdc2132011-11-15 13:40:15 +010045const char *il_get_cmd_string(u8 cmd)
46{
47 switch (cmd) {
48 IL_CMD(N_ALIVE);
49 IL_CMD(N_ERROR);
50 IL_CMD(C_RXON);
51 IL_CMD(C_RXON_ASSOC);
52 IL_CMD(C_QOS_PARAM);
53 IL_CMD(C_RXON_TIMING);
54 IL_CMD(C_ADD_STA);
55 IL_CMD(C_REM_STA);
56 IL_CMD(C_WEPKEY);
57 IL_CMD(N_3945_RX);
58 IL_CMD(C_TX);
59 IL_CMD(C_RATE_SCALE);
60 IL_CMD(C_LEDS);
61 IL_CMD(C_TX_LINK_QUALITY_CMD);
62 IL_CMD(C_CHANNEL_SWITCH);
63 IL_CMD(N_CHANNEL_SWITCH);
64 IL_CMD(C_SPECTRUM_MEASUREMENT);
65 IL_CMD(N_SPECTRUM_MEASUREMENT);
66 IL_CMD(C_POWER_TBL);
67 IL_CMD(N_PM_SLEEP);
68 IL_CMD(N_PM_DEBUG_STATS);
69 IL_CMD(C_SCAN);
70 IL_CMD(C_SCAN_ABORT);
71 IL_CMD(N_SCAN_START);
72 IL_CMD(N_SCAN_RESULTS);
73 IL_CMD(N_SCAN_COMPLETE);
74 IL_CMD(N_BEACON);
75 IL_CMD(C_TX_BEACON);
76 IL_CMD(C_TX_PWR_TBL);
77 IL_CMD(C_BT_CONFIG);
78 IL_CMD(C_STATS);
79 IL_CMD(N_STATS);
80 IL_CMD(N_CARD_STATE);
81 IL_CMD(N_MISSED_BEACONS);
82 IL_CMD(C_CT_KILL_CONFIG);
83 IL_CMD(C_SENSITIVITY);
84 IL_CMD(C_PHY_CALIBRATION);
85 IL_CMD(N_RX_PHY);
86 IL_CMD(N_RX_MPDU);
87 IL_CMD(N_RX);
88 IL_CMD(N_COMPRESSED_BA);
89 default:
90 return "UNKNOWN";
91
92 }
93}
94EXPORT_SYMBOL(il_get_cmd_string);
95
96#define HOST_COMPLETE_TIMEOUT (HZ / 2)
97
98static void il_generic_cmd_callback(struct il_priv *il,
99 struct il_device_cmd *cmd,
100 struct il_rx_pkt *pkt)
101{
102 if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
103 IL_ERR("Bad return from %s (0x%08X)\n",
104 il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
105 return;
106 }
107
108#ifdef CONFIG_IWLEGACY_DEBUG
109 switch (cmd->hdr.cmd) {
110 case C_TX_LINK_QUALITY_CMD:
111 case C_SENSITIVITY:
112 D_HC_DUMP("back from %s (0x%08X)\n",
113 il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
114 break;
115 default:
116 D_HC("back from %s (0x%08X)\n",
117 il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
118 }
119#endif
120}
121
122static int
123il_send_cmd_async(struct il_priv *il, struct il_host_cmd *cmd)
124{
125 int ret;
126
127 BUG_ON(!(cmd->flags & CMD_ASYNC));
128
129 /* An asynchronous command can not expect an SKB to be set. */
130 BUG_ON(cmd->flags & CMD_WANT_SKB);
131
132 /* Assign a generic callback if one is not provided */
133 if (!cmd->callback)
134 cmd->callback = il_generic_cmd_callback;
135
136 if (test_bit(S_EXIT_PENDING, &il->status))
137 return -EBUSY;
138
139 ret = il_enqueue_hcmd(il, cmd);
140 if (ret < 0) {
141 IL_ERR("Error sending %s: enqueue_hcmd failed: %d\n",
142 il_get_cmd_string(cmd->id), ret);
143 return ret;
144 }
145 return 0;
146}
147
148int il_send_cmd_sync(struct il_priv *il, struct il_host_cmd *cmd)
149{
150 int cmd_idx;
151 int ret;
152
153 lockdep_assert_held(&il->mutex);
154
155 BUG_ON(cmd->flags & CMD_ASYNC);
156
157 /* A synchronous command can not have a callback set. */
158 BUG_ON(cmd->callback);
159
160 D_INFO("Attempting to send sync command %s\n",
161 il_get_cmd_string(cmd->id));
162
163 set_bit(S_HCMD_ACTIVE, &il->status);
164 D_INFO("Setting HCMD_ACTIVE for command %s\n",
165 il_get_cmd_string(cmd->id));
166
167 cmd_idx = il_enqueue_hcmd(il, cmd);
168 if (cmd_idx < 0) {
169 ret = cmd_idx;
170 IL_ERR("Error sending %s: enqueue_hcmd failed: %d\n",
171 il_get_cmd_string(cmd->id), ret);
172 goto out;
173 }
174
175 ret = wait_event_timeout(il->wait_command_queue,
176 !test_bit(S_HCMD_ACTIVE, &il->status),
177 HOST_COMPLETE_TIMEOUT);
178 if (!ret) {
179 if (test_bit(S_HCMD_ACTIVE, &il->status)) {
180 IL_ERR(
181 "Error sending %s: time out after %dms.\n",
182 il_get_cmd_string(cmd->id),
183 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
184
185 clear_bit(S_HCMD_ACTIVE, &il->status);
186 D_INFO(
187 "Clearing HCMD_ACTIVE for command %s\n",
188 il_get_cmd_string(cmd->id));
189 ret = -ETIMEDOUT;
190 goto cancel;
191 }
192 }
193
194 if (test_bit(S_RF_KILL_HW, &il->status)) {
195 IL_ERR("Command %s aborted: RF KILL Switch\n",
196 il_get_cmd_string(cmd->id));
197 ret = -ECANCELED;
198 goto fail;
199 }
200 if (test_bit(S_FW_ERROR, &il->status)) {
201 IL_ERR("Command %s failed: FW Error\n",
202 il_get_cmd_string(cmd->id));
203 ret = -EIO;
204 goto fail;
205 }
206 if ((cmd->flags & CMD_WANT_SKB) && !cmd->reply_page) {
207 IL_ERR("Error: Response NULL in '%s'\n",
208 il_get_cmd_string(cmd->id));
209 ret = -EIO;
210 goto cancel;
211 }
212
213 ret = 0;
214 goto out;
215
216cancel:
217 if (cmd->flags & CMD_WANT_SKB) {
218 /*
219 * Cancel the CMD_WANT_SKB flag for the cmd in the
220 * TX cmd queue. Otherwise in case the cmd comes
221 * in later, it will possibly set an invalid
222 * address (cmd->meta.source).
223 */
224 il->txq[il->cmd_queue].meta[cmd_idx].flags &=
225 ~CMD_WANT_SKB;
226 }
227fail:
228 if (cmd->reply_page) {
229 il_free_pages(il, cmd->reply_page);
230 cmd->reply_page = 0;
231 }
232out:
233 return ret;
234}
235EXPORT_SYMBOL(il_send_cmd_sync);
236
237int il_send_cmd(struct il_priv *il, struct il_host_cmd *cmd)
238{
239 if (cmd->flags & CMD_ASYNC)
240 return il_send_cmd_async(il, cmd);
241
242 return il_send_cmd_sync(il, cmd);
243}
244EXPORT_SYMBOL(il_send_cmd);
245
246int
247il_send_cmd_pdu(struct il_priv *il, u8 id, u16 len, const void *data)
248{
249 struct il_host_cmd cmd = {
250 .id = id,
251 .len = len,
252 .data = data,
253 };
254
255 return il_send_cmd_sync(il, &cmd);
256}
257EXPORT_SYMBOL(il_send_cmd_pdu);
258
259int il_send_cmd_pdu_async(struct il_priv *il,
260 u8 id, u16 len, const void *data,
261 void (*callback)(struct il_priv *il,
262 struct il_device_cmd *cmd,
263 struct il_rx_pkt *pkt))
264{
265 struct il_host_cmd cmd = {
266 .id = id,
267 .len = len,
268 .data = data,
269 };
270
271 cmd.flags |= CMD_ASYNC;
272 cmd.callback = callback;
273
274 return il_send_cmd_async(il, &cmd);
275}
276EXPORT_SYMBOL(il_send_cmd_pdu_async);
277
278/* default: IL_LED_BLINK(0) using blinking idx table */
279static int led_mode;
280module_param(led_mode, int, S_IRUGO);
281MODULE_PARM_DESC(led_mode, "0=system default, "
282 "1=On(RF On)/Off(RF Off), 2=blinking");
283
284/* Throughput OFF time(ms) ON time (ms)
285 * >300 25 25
286 * >200 to 300 40 40
287 * >100 to 200 55 55
288 * >70 to 100 65 65
289 * >50 to 70 75 75
290 * >20 to 50 85 85
291 * >10 to 20 95 95
292 * >5 to 10 110 110
293 * >1 to 5 130 130
294 * >0 to 1 167 167
295 * <=0 SOLID ON
296 */
297static const struct ieee80211_tpt_blink il_blink[] = {
298 { .throughput = 0, .blink_time = 334 },
299 { .throughput = 1 * 1024 - 1, .blink_time = 260 },
300 { .throughput = 5 * 1024 - 1, .blink_time = 220 },
301 { .throughput = 10 * 1024 - 1, .blink_time = 190 },
302 { .throughput = 20 * 1024 - 1, .blink_time = 170 },
303 { .throughput = 50 * 1024 - 1, .blink_time = 150 },
304 { .throughput = 70 * 1024 - 1, .blink_time = 130 },
305 { .throughput = 100 * 1024 - 1, .blink_time = 110 },
306 { .throughput = 200 * 1024 - 1, .blink_time = 80 },
307 { .throughput = 300 * 1024 - 1, .blink_time = 50 },
308};
309
310/*
311 * Adjust led blink rate to compensate on a MAC Clock difference on every HW
312 * Led blink rate analysis showed an average deviation of 0% on 3945,
313 * 5% on 4965 HW.
314 * Need to compensate on the led on/off time per HW according to the deviation
315 * to achieve the desired led frequency
316 * The calculation is: (100-averageDeviation)/100 * blinkTime
317 * For code efficiency the calculation will be:
318 * compensation = (100 - averageDeviation) * 64 / 100
319 * NewBlinkTime = (compensation * BlinkTime) / 64
320 */
321static inline u8 il_blink_compensation(struct il_priv *il,
322 u8 time, u16 compensation)
323{
324 if (!compensation) {
325 IL_ERR("undefined blink compensation: "
326 "use pre-defined blinking time\n");
327 return time;
328 }
329
330 return (u8)((time * compensation) >> 6);
331}
332
333/* Set led pattern command */
334static int il_led_cmd(struct il_priv *il,
335 unsigned long on,
336 unsigned long off)
337{
338 struct il_led_cmd led_cmd = {
339 .id = IL_LED_LINK,
340 .interval = IL_DEF_LED_INTRVL
341 };
342 int ret;
343
344 if (!test_bit(S_READY, &il->status))
345 return -EBUSY;
346
347 if (il->blink_on == on && il->blink_off == off)
348 return 0;
349
350 if (off == 0) {
351 /* led is SOLID_ON */
352 on = IL_LED_SOLID;
353 }
354
355 D_LED("Led blink time compensation=%u\n",
356 il->cfg->base_params->led_compensation);
357 led_cmd.on = il_blink_compensation(il, on,
358 il->cfg->base_params->led_compensation);
359 led_cmd.off = il_blink_compensation(il, off,
360 il->cfg->base_params->led_compensation);
361
362 ret = il->cfg->ops->led->cmd(il, &led_cmd);
363 if (!ret) {
364 il->blink_on = on;
365 il->blink_off = off;
366 }
367 return ret;
368}
369
370static void il_led_brightness_set(struct led_classdev *led_cdev,
371 enum led_brightness brightness)
372{
373 struct il_priv *il = container_of(led_cdev, struct il_priv, led);
374 unsigned long on = 0;
375
376 if (brightness > 0)
377 on = IL_LED_SOLID;
378
379 il_led_cmd(il, on, 0);
380}
381
382static int il_led_blink_set(struct led_classdev *led_cdev,
383 unsigned long *delay_on,
384 unsigned long *delay_off)
385{
386 struct il_priv *il = container_of(led_cdev, struct il_priv, led);
387
388 return il_led_cmd(il, *delay_on, *delay_off);
389}
390
391void il_leds_init(struct il_priv *il)
392{
393 int mode = led_mode;
394 int ret;
395
396 if (mode == IL_LED_DEFAULT)
397 mode = il->cfg->led_mode;
398
399 il->led.name = kasprintf(GFP_KERNEL, "%s-led",
400 wiphy_name(il->hw->wiphy));
401 il->led.brightness_set = il_led_brightness_set;
402 il->led.blink_set = il_led_blink_set;
403 il->led.max_brightness = 1;
404
405 switch (mode) {
406 case IL_LED_DEFAULT:
407 WARN_ON(1);
408 break;
409 case IL_LED_BLINK:
410 il->led.default_trigger =
411 ieee80211_create_tpt_led_trigger(il->hw,
412 IEEE80211_TPT_LEDTRIG_FL_CONNECTED,
413 il_blink, ARRAY_SIZE(il_blink));
414 break;
415 case IL_LED_RF_STATE:
416 il->led.default_trigger =
417 ieee80211_get_radio_led_name(il->hw);
418 break;
419 }
420
421 ret = led_classdev_register(&il->pci_dev->dev, &il->led);
422 if (ret) {
423 kfree(il->led.name);
424 return;
425 }
426
427 il->led_registered = true;
428}
429EXPORT_SYMBOL(il_leds_init);
430
431void il_leds_exit(struct il_priv *il)
432{
433 if (!il->led_registered)
434 return;
435
436 led_classdev_unregister(&il->led);
437 kfree(il->led.name);
438}
439EXPORT_SYMBOL(il_leds_exit);
440
441/************************** EEPROM BANDS ****************************
442 *
443 * The il_eeprom_band definitions below provide the mapping from the
444 * EEPROM contents to the specific channel number supported for each
445 * band.
446 *
447 * For example, il_priv->eeprom.band_3_channels[4] from the band_3
448 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
449 * The specific geography and calibration information for that channel
450 * is contained in the eeprom map itself.
451 *
452 * During init, we copy the eeprom information and channel map
453 * information into il->channel_info_24/52 and il->channel_map_24/52
454 *
455 * channel_map_24/52 provides the idx in the channel_info array for a
456 * given channel. We have to have two separate maps as there is channel
457 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
458 * band_2
459 *
460 * A value of 0xff stored in the channel_map indicates that the channel
461 * is not supported by the hardware at all.
462 *
463 * A value of 0xfe in the channel_map indicates that the channel is not
464 * valid for Tx with the current hardware. This means that
465 * while the system can tune and receive on a given channel, it may not
466 * be able to associate or transmit any frames on that
467 * channel. There is no corresponding channel information for that
468 * entry.
469 *
470 *********************************************************************/
471
472/* 2.4 GHz */
473const u8 il_eeprom_band_1[14] = {
474 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
475};
476
477/* 5.2 GHz bands */
478static const u8 il_eeprom_band_2[] = { /* 4915-5080MHz */
479 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
480};
481
482static const u8 il_eeprom_band_3[] = { /* 5170-5320MHz */
483 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
484};
485
486static const u8 il_eeprom_band_4[] = { /* 5500-5700MHz */
487 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
488};
489
490static const u8 il_eeprom_band_5[] = { /* 5725-5825MHz */
491 145, 149, 153, 157, 161, 165
492};
493
494static const u8 il_eeprom_band_6[] = { /* 2.4 ht40 channel */
495 1, 2, 3, 4, 5, 6, 7
496};
497
498static const u8 il_eeprom_band_7[] = { /* 5.2 ht40 channel */
499 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
500};
501
502/******************************************************************************
503 *
504 * EEPROM related functions
505 *
506******************************************************************************/
507
508static int il_eeprom_verify_signature(struct il_priv *il)
509{
510 u32 gp = _il_rd(il, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
511 int ret = 0;
512
513 D_EEPROM("EEPROM signature=0x%08x\n", gp);
514 switch (gp) {
515 case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
516 case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
517 break;
518 default:
519 IL_ERR("bad EEPROM signature,"
520 "EEPROM_GP=0x%08x\n", gp);
521 ret = -ENOENT;
522 break;
523 }
524 return ret;
525}
526
527const u8
528*il_eeprom_query_addr(const struct il_priv *il, size_t offset)
529{
530 BUG_ON(offset >= il->cfg->base_params->eeprom_size);
531 return &il->eeprom[offset];
532}
533EXPORT_SYMBOL(il_eeprom_query_addr);
534
535u16 il_eeprom_query16(const struct il_priv *il, size_t offset)
536{
537 if (!il->eeprom)
538 return 0;
539 return (u16)il->eeprom[offset] | ((u16)il->eeprom[offset + 1] << 8);
540}
541EXPORT_SYMBOL(il_eeprom_query16);
542
543/**
544 * il_eeprom_init - read EEPROM contents
545 *
546 * Load the EEPROM contents from adapter into il->eeprom
547 *
548 * NOTE: This routine uses the non-debug IO access functions.
549 */
550int il_eeprom_init(struct il_priv *il)
551{
552 __le16 *e;
553 u32 gp = _il_rd(il, CSR_EEPROM_GP);
554 int sz;
555 int ret;
556 u16 addr;
557
558 /* allocate eeprom */
559 sz = il->cfg->base_params->eeprom_size;
560 D_EEPROM("NVM size = %d\n", sz);
561 il->eeprom = kzalloc(sz, GFP_KERNEL);
562 if (!il->eeprom) {
563 ret = -ENOMEM;
564 goto alloc_err;
565 }
566 e = (__le16 *)il->eeprom;
567
568 il->cfg->ops->lib->apm_ops.init(il);
569
570 ret = il_eeprom_verify_signature(il);
571 if (ret < 0) {
572 IL_ERR("EEPROM not found, EEPROM_GP=0x%08x\n", gp);
573 ret = -ENOENT;
574 goto err;
575 }
576
577 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
578 ret = il->cfg->ops->lib->eeprom_ops.acquire_semaphore(il);
579 if (ret < 0) {
580 IL_ERR("Failed to acquire EEPROM semaphore.\n");
581 ret = -ENOENT;
582 goto err;
583 }
584
585 /* eeprom is an array of 16bit values */
586 for (addr = 0; addr < sz; addr += sizeof(u16)) {
587 u32 r;
588
589 _il_wr(il, CSR_EEPROM_REG,
590 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
591
592 ret = _il_poll_bit(il, CSR_EEPROM_REG,
593 CSR_EEPROM_REG_READ_VALID_MSK,
594 CSR_EEPROM_REG_READ_VALID_MSK,
595 IL_EEPROM_ACCESS_TIMEOUT);
596 if (ret < 0) {
597 IL_ERR("Time out reading EEPROM[%d]\n",
598 addr);
599 goto done;
600 }
601 r = _il_rd(il, CSR_EEPROM_REG);
602 e[addr / 2] = cpu_to_le16(r >> 16);
603 }
604
605 D_EEPROM("NVM Type: %s, version: 0x%x\n",
606 "EEPROM",
607 il_eeprom_query16(il, EEPROM_VERSION));
608
609 ret = 0;
610done:
611 il->cfg->ops->lib->eeprom_ops.release_semaphore(il);
612
613err:
614 if (ret)
615 il_eeprom_free(il);
616 /* Reset chip to save power until we load uCode during "up". */
617 il_apm_stop(il);
618alloc_err:
619 return ret;
620}
621EXPORT_SYMBOL(il_eeprom_init);
622
623void il_eeprom_free(struct il_priv *il)
624{
625 kfree(il->eeprom);
626 il->eeprom = NULL;
627}
628EXPORT_SYMBOL(il_eeprom_free);
629
630static void il_init_band_reference(const struct il_priv *il,
631 int eep_band, int *eeprom_ch_count,
632 const struct il_eeprom_channel **eeprom_ch_info,
633 const u8 **eeprom_ch_idx)
634{
635 u32 offset = il->cfg->ops->lib->
636 eeprom_ops.regulatory_bands[eep_band - 1];
637 switch (eep_band) {
638 case 1: /* 2.4GHz band */
639 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_1);
640 *eeprom_ch_info = (struct il_eeprom_channel *)
641 il_eeprom_query_addr(il, offset);
642 *eeprom_ch_idx = il_eeprom_band_1;
643 break;
644 case 2: /* 4.9GHz band */
645 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_2);
646 *eeprom_ch_info = (struct il_eeprom_channel *)
647 il_eeprom_query_addr(il, offset);
648 *eeprom_ch_idx = il_eeprom_band_2;
649 break;
650 case 3: /* 5.2GHz band */
651 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_3);
652 *eeprom_ch_info = (struct il_eeprom_channel *)
653 il_eeprom_query_addr(il, offset);
654 *eeprom_ch_idx = il_eeprom_band_3;
655 break;
656 case 4: /* 5.5GHz band */
657 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_4);
658 *eeprom_ch_info = (struct il_eeprom_channel *)
659 il_eeprom_query_addr(il, offset);
660 *eeprom_ch_idx = il_eeprom_band_4;
661 break;
662 case 5: /* 5.7GHz band */
663 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_5);
664 *eeprom_ch_info = (struct il_eeprom_channel *)
665 il_eeprom_query_addr(il, offset);
666 *eeprom_ch_idx = il_eeprom_band_5;
667 break;
668 case 6: /* 2.4GHz ht40 channels */
669 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_6);
670 *eeprom_ch_info = (struct il_eeprom_channel *)
671 il_eeprom_query_addr(il, offset);
672 *eeprom_ch_idx = il_eeprom_band_6;
673 break;
674 case 7: /* 5 GHz ht40 channels */
675 *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_7);
676 *eeprom_ch_info = (struct il_eeprom_channel *)
677 il_eeprom_query_addr(il, offset);
678 *eeprom_ch_idx = il_eeprom_band_7;
679 break;
680 default:
681 BUG();
682 }
683}
684
685#define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
686 ? # x " " : "")
687/**
688 * il_mod_ht40_chan_info - Copy ht40 channel info into driver's il.
689 *
690 * Does not set up a command, or touch hardware.
691 */
692static int il_mod_ht40_chan_info(struct il_priv *il,
693 enum ieee80211_band band, u16 channel,
694 const struct il_eeprom_channel *eeprom_ch,
695 u8 clear_ht40_extension_channel)
696{
697 struct il_channel_info *ch_info;
698
699 ch_info = (struct il_channel_info *)
700 il_get_channel_info(il, band, channel);
701
702 if (!il_is_channel_valid(ch_info))
703 return -1;
704
705 D_EEPROM("HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm):"
706 " Ad-Hoc %ssupported\n",
707 ch_info->channel,
708 il_is_channel_a_band(ch_info) ?
709 "5.2" : "2.4",
710 CHECK_AND_PRINT(IBSS),
711 CHECK_AND_PRINT(ACTIVE),
712 CHECK_AND_PRINT(RADAR),
713 CHECK_AND_PRINT(WIDE),
714 CHECK_AND_PRINT(DFS),
715 eeprom_ch->flags,
716 eeprom_ch->max_power_avg,
717 ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS)
718 && !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ?
719 "" : "not ");
720
721 ch_info->ht40_eeprom = *eeprom_ch;
722 ch_info->ht40_max_power_avg = eeprom_ch->max_power_avg;
723 ch_info->ht40_flags = eeprom_ch->flags;
724 if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
725 ch_info->ht40_extension_channel &=
726 ~clear_ht40_extension_channel;
727
728 return 0;
729}
730
731#define CHECK_AND_PRINT_I(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
732 ? # x " " : "")
733
734/**
735 * il_init_channel_map - Set up driver's info for all possible channels
736 */
737int il_init_channel_map(struct il_priv *il)
738{
739 int eeprom_ch_count = 0;
740 const u8 *eeprom_ch_idx = NULL;
741 const struct il_eeprom_channel *eeprom_ch_info = NULL;
742 int band, ch;
743 struct il_channel_info *ch_info;
744
745 if (il->channel_count) {
746 D_EEPROM("Channel map already initialized.\n");
747 return 0;
748 }
749
750 D_EEPROM("Initializing regulatory info from EEPROM\n");
751
752 il->channel_count =
753 ARRAY_SIZE(il_eeprom_band_1) +
754 ARRAY_SIZE(il_eeprom_band_2) +
755 ARRAY_SIZE(il_eeprom_band_3) +
756 ARRAY_SIZE(il_eeprom_band_4) +
757 ARRAY_SIZE(il_eeprom_band_5);
758
759 D_EEPROM("Parsing data for %d channels.\n",
760 il->channel_count);
761
762 il->channel_info = kzalloc(sizeof(struct il_channel_info) *
763 il->channel_count, GFP_KERNEL);
764 if (!il->channel_info) {
765 IL_ERR("Could not allocate channel_info\n");
766 il->channel_count = 0;
767 return -ENOMEM;
768 }
769
770 ch_info = il->channel_info;
771
772 /* Loop through the 5 EEPROM bands adding them in order to the
773 * channel map we maintain (that contains additional information than
774 * what just in the EEPROM) */
775 for (band = 1; band <= 5; band++) {
776
777 il_init_band_reference(il, band, &eeprom_ch_count,
778 &eeprom_ch_info, &eeprom_ch_idx);
779
780 /* Loop through each band adding each of the channels */
781 for (ch = 0; ch < eeprom_ch_count; ch++) {
782 ch_info->channel = eeprom_ch_idx[ch];
783 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
784 IEEE80211_BAND_5GHZ;
785
786 /* permanently store EEPROM's channel regulatory flags
787 * and max power in channel info database. */
788 ch_info->eeprom = eeprom_ch_info[ch];
789
790 /* Copy the run-time flags so they are there even on
791 * invalid channels */
792 ch_info->flags = eeprom_ch_info[ch].flags;
793 /* First write that ht40 is not enabled, and then enable
794 * one by one */
795 ch_info->ht40_extension_channel =
796 IEEE80211_CHAN_NO_HT40;
797
798 if (!(il_is_channel_valid(ch_info))) {
799 D_EEPROM(
800 "Ch. %d Flags %x [%sGHz] - "
801 "No traffic\n",
802 ch_info->channel,
803 ch_info->flags,
804 il_is_channel_a_band(ch_info) ?
805 "5.2" : "2.4");
806 ch_info++;
807 continue;
808 }
809
810 /* Initialize regulatory-based run-time data */
811 ch_info->max_power_avg = ch_info->curr_txpow =
812 eeprom_ch_info[ch].max_power_avg;
813 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
814 ch_info->min_power = 0;
815
816 D_EEPROM("Ch. %d [%sGHz] "
817 "%s%s%s%s%s%s(0x%02x %ddBm):"
818 " Ad-Hoc %ssupported\n",
819 ch_info->channel,
820 il_is_channel_a_band(ch_info) ?
821 "5.2" : "2.4",
822 CHECK_AND_PRINT_I(VALID),
823 CHECK_AND_PRINT_I(IBSS),
824 CHECK_AND_PRINT_I(ACTIVE),
825 CHECK_AND_PRINT_I(RADAR),
826 CHECK_AND_PRINT_I(WIDE),
827 CHECK_AND_PRINT_I(DFS),
828 eeprom_ch_info[ch].flags,
829 eeprom_ch_info[ch].max_power_avg,
830 ((eeprom_ch_info[ch].
831 flags & EEPROM_CHANNEL_IBSS)
832 && !(eeprom_ch_info[ch].
833 flags & EEPROM_CHANNEL_RADAR))
834 ? "" : "not ");
835
836 ch_info++;
837 }
838 }
839
840 /* Check if we do have HT40 channels */
841 if (il->cfg->ops->lib->eeprom_ops.regulatory_bands[5] ==
842 EEPROM_REGULATORY_BAND_NO_HT40 &&
843 il->cfg->ops->lib->eeprom_ops.regulatory_bands[6] ==
844 EEPROM_REGULATORY_BAND_NO_HT40)
845 return 0;
846
847 /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
848 for (band = 6; band <= 7; band++) {
849 enum ieee80211_band ieeeband;
850
851 il_init_band_reference(il, band, &eeprom_ch_count,
852 &eeprom_ch_info, &eeprom_ch_idx);
853
854 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
855 ieeeband =
856 (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
857
858 /* Loop through each band adding each of the channels */
859 for (ch = 0; ch < eeprom_ch_count; ch++) {
860 /* Set up driver's info for lower half */
861 il_mod_ht40_chan_info(il, ieeeband,
862 eeprom_ch_idx[ch],
863 &eeprom_ch_info[ch],
864 IEEE80211_CHAN_NO_HT40PLUS);
865
866 /* Set up driver's info for upper half */
867 il_mod_ht40_chan_info(il, ieeeband,
868 eeprom_ch_idx[ch] + 4,
869 &eeprom_ch_info[ch],
870 IEEE80211_CHAN_NO_HT40MINUS);
871 }
872 }
873
874 return 0;
875}
876EXPORT_SYMBOL(il_init_channel_map);
877
878/*
879 * il_free_channel_map - undo allocations in il_init_channel_map
880 */
881void il_free_channel_map(struct il_priv *il)
882{
883 kfree(il->channel_info);
884 il->channel_count = 0;
885}
886EXPORT_SYMBOL(il_free_channel_map);
887
888/**
889 * il_get_channel_info - Find driver's ilate channel info
890 *
891 * Based on band and channel number.
892 */
893const struct
894il_channel_info *il_get_channel_info(const struct il_priv *il,
895 enum ieee80211_band band, u16 channel)
896{
897 int i;
898
899 switch (band) {
900 case IEEE80211_BAND_5GHZ:
901 for (i = 14; i < il->channel_count; i++) {
902 if (il->channel_info[i].channel == channel)
903 return &il->channel_info[i];
904 }
905 break;
906 case IEEE80211_BAND_2GHZ:
907 if (channel >= 1 && channel <= 14)
908 return &il->channel_info[channel - 1];
909 break;
910 default:
911 BUG();
912 }
913
914 return NULL;
915}
916EXPORT_SYMBOL(il_get_channel_info);
917
918/*
919 * Setting power level allows the card to go to sleep when not busy.
920 *
921 * We calculate a sleep command based on the required latency, which
922 * we get from mac80211. In order to handle thermal throttling, we can
923 * also use pre-defined power levels.
924 */
925
926/*
927 * This defines the old power levels. They are still used by default
928 * (level 1) and for thermal throttle (levels 3 through 5)
929 */
930
931struct il_power_vec_entry {
932 struct il_powertable_cmd cmd;
933 u8 no_dtim; /* number of skip dtim */
934};
935
936static void il_power_sleep_cam_cmd(struct il_priv *il,
937 struct il_powertable_cmd *cmd)
938{
939 memset(cmd, 0, sizeof(*cmd));
940
941 if (il->power_data.pci_pm)
942 cmd->flags |= IL_POWER_PCI_PM_MSK;
943
944 D_POWER("Sleep command for CAM\n");
945}
946
947static int
948il_set_power(struct il_priv *il, struct il_powertable_cmd *cmd)
949{
950 D_POWER("Sending power/sleep command\n");
951 D_POWER("Flags value = 0x%08X\n", cmd->flags);
952 D_POWER("Tx timeout = %u\n",
953 le32_to_cpu(cmd->tx_data_timeout));
954 D_POWER("Rx timeout = %u\n",
955 le32_to_cpu(cmd->rx_data_timeout));
956 D_POWER(
957 "Sleep interval vector = { %d , %d , %d , %d , %d }\n",
958 le32_to_cpu(cmd->sleep_interval[0]),
959 le32_to_cpu(cmd->sleep_interval[1]),
960 le32_to_cpu(cmd->sleep_interval[2]),
961 le32_to_cpu(cmd->sleep_interval[3]),
962 le32_to_cpu(cmd->sleep_interval[4]));
963
964 return il_send_cmd_pdu(il, C_POWER_TBL,
965 sizeof(struct il_powertable_cmd), cmd);
966}
967
968int
969il_power_set_mode(struct il_priv *il, struct il_powertable_cmd *cmd,
970 bool force)
971{
972 int ret;
973 bool update_chains;
974
975 lockdep_assert_held(&il->mutex);
976
977 /* Don't update the RX chain when chain noise calibration is running */
978 update_chains = il->chain_noise_data.state == IL_CHAIN_NOISE_DONE ||
979 il->chain_noise_data.state == IL_CHAIN_NOISE_ALIVE;
980
981 if (!memcmp(&il->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
982 return 0;
983
984 if (!il_is_ready_rf(il))
985 return -EIO;
986
987 /* scan complete use sleep_power_next, need to be updated */
988 memcpy(&il->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
989 if (test_bit(S_SCANNING, &il->status) && !force) {
990 D_INFO("Defer power set mode while scanning\n");
991 return 0;
992 }
993
994 if (cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK)
995 set_bit(S_POWER_PMI, &il->status);
996
997 ret = il_set_power(il, cmd);
998 if (!ret) {
999 if (!(cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK))
1000 clear_bit(S_POWER_PMI, &il->status);
1001
1002 if (il->cfg->ops->lib->update_chain_flags && update_chains)
1003 il->cfg->ops->lib->update_chain_flags(il);
1004 else if (il->cfg->ops->lib->update_chain_flags)
1005 D_POWER(
1006 "Cannot update the power, chain noise "
1007 "calibration running: %d\n",
1008 il->chain_noise_data.state);
1009
1010 memcpy(&il->power_data.sleep_cmd, cmd, sizeof(*cmd));
1011 } else
1012 IL_ERR("set power fail, ret = %d", ret);
1013
1014 return ret;
1015}
1016
1017int il_power_update_mode(struct il_priv *il, bool force)
1018{
1019 struct il_powertable_cmd cmd;
1020
1021 il_power_sleep_cam_cmd(il, &cmd);
1022 return il_power_set_mode(il, &cmd, force);
1023}
1024EXPORT_SYMBOL(il_power_update_mode);
1025
1026/* initialize to default */
1027void il_power_initialize(struct il_priv *il)
1028{
1029 u16 lctl = il_pcie_link_ctl(il);
1030
1031 il->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
1032
1033 il->power_data.debug_sleep_level_override = -1;
1034
1035 memset(&il->power_data.sleep_cmd, 0,
1036 sizeof(il->power_data.sleep_cmd));
1037}
1038EXPORT_SYMBOL(il_power_initialize);
1039
1040/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
1041 * sending probe req. This should be set long enough to hear probe responses
1042 * from more than one AP. */
1043#define IL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
1044#define IL_ACTIVE_DWELL_TIME_52 (20)
1045
1046#define IL_ACTIVE_DWELL_FACTOR_24GHZ (3)
1047#define IL_ACTIVE_DWELL_FACTOR_52GHZ (2)
1048
1049/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
1050 * Must be set longer than active dwell time.
1051 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
1052#define IL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
1053#define IL_PASSIVE_DWELL_TIME_52 (10)
1054#define IL_PASSIVE_DWELL_BASE (100)
1055#define IL_CHANNEL_TUNE_TIME 5
1056
1057static int il_send_scan_abort(struct il_priv *il)
1058{
1059 int ret;
1060 struct il_rx_pkt *pkt;
1061 struct il_host_cmd cmd = {
1062 .id = C_SCAN_ABORT,
1063 .flags = CMD_WANT_SKB,
1064 };
1065
1066 /* Exit instantly with error when device is not ready
1067 * to receive scan abort command or it does not perform
1068 * hardware scan currently */
1069 if (!test_bit(S_READY, &il->status) ||
1070 !test_bit(S_GEO_CONFIGURED, &il->status) ||
1071 !test_bit(S_SCAN_HW, &il->status) ||
1072 test_bit(S_FW_ERROR, &il->status) ||
1073 test_bit(S_EXIT_PENDING, &il->status))
1074 return -EIO;
1075
1076 ret = il_send_cmd_sync(il, &cmd);
1077 if (ret)
1078 return ret;
1079
1080 pkt = (struct il_rx_pkt *)cmd.reply_page;
1081 if (pkt->u.status != CAN_ABORT_STATUS) {
1082 /* The scan abort will return 1 for success or
1083 * 2 for "failure". A failure condition can be
1084 * due to simply not being in an active scan which
1085 * can occur if we send the scan abort before we
1086 * the microcode has notified us that a scan is
1087 * completed. */
1088 D_SCAN("SCAN_ABORT ret %d.\n", pkt->u.status);
1089 ret = -EIO;
1090 }
1091
1092 il_free_pages(il, cmd.reply_page);
1093 return ret;
1094}
1095
1096static void il_complete_scan(struct il_priv *il, bool aborted)
1097{
1098 /* check if scan was requested from mac80211 */
1099 if (il->scan_request) {
1100 D_SCAN("Complete scan in mac80211\n");
1101 ieee80211_scan_completed(il->hw, aborted);
1102 }
1103
1104 il->scan_vif = NULL;
1105 il->scan_request = NULL;
1106}
1107
1108void il_force_scan_end(struct il_priv *il)
1109{
1110 lockdep_assert_held(&il->mutex);
1111
1112 if (!test_bit(S_SCANNING, &il->status)) {
1113 D_SCAN("Forcing scan end while not scanning\n");
1114 return;
1115 }
1116
1117 D_SCAN("Forcing scan end\n");
1118 clear_bit(S_SCANNING, &il->status);
1119 clear_bit(S_SCAN_HW, &il->status);
1120 clear_bit(S_SCAN_ABORTING, &il->status);
1121 il_complete_scan(il, true);
1122}
1123
1124static void il_do_scan_abort(struct il_priv *il)
1125{
1126 int ret;
1127
1128 lockdep_assert_held(&il->mutex);
1129
1130 if (!test_bit(S_SCANNING, &il->status)) {
1131 D_SCAN("Not performing scan to abort\n");
1132 return;
1133 }
1134
1135 if (test_and_set_bit(S_SCAN_ABORTING, &il->status)) {
1136 D_SCAN("Scan abort in progress\n");
1137 return;
1138 }
1139
1140 ret = il_send_scan_abort(il);
1141 if (ret) {
1142 D_SCAN("Send scan abort failed %d\n", ret);
1143 il_force_scan_end(il);
1144 } else
1145 D_SCAN("Successfully send scan abort\n");
1146}
1147
1148/**
1149 * il_scan_cancel - Cancel any currently executing HW scan
1150 */
1151int il_scan_cancel(struct il_priv *il)
1152{
1153 D_SCAN("Queuing abort scan\n");
1154 queue_work(il->workqueue, &il->abort_scan);
1155 return 0;
1156}
1157EXPORT_SYMBOL(il_scan_cancel);
1158
1159/**
1160 * il_scan_cancel_timeout - Cancel any currently executing HW scan
1161 * @ms: amount of time to wait (in milliseconds) for scan to abort
1162 *
1163 */
1164int il_scan_cancel_timeout(struct il_priv *il, unsigned long ms)
1165{
1166 unsigned long timeout = jiffies + msecs_to_jiffies(ms);
1167
1168 lockdep_assert_held(&il->mutex);
1169
1170 D_SCAN("Scan cancel timeout\n");
1171
1172 il_do_scan_abort(il);
1173
1174 while (time_before_eq(jiffies, timeout)) {
1175 if (!test_bit(S_SCAN_HW, &il->status))
1176 break;
1177 msleep(20);
1178 }
1179
1180 return test_bit(S_SCAN_HW, &il->status);
1181}
1182EXPORT_SYMBOL(il_scan_cancel_timeout);
1183
1184/* Service response to C_SCAN (0x80) */
1185static void il_hdl_scan(struct il_priv *il,
1186 struct il_rx_buf *rxb)
1187{
1188#ifdef CONFIG_IWLEGACY_DEBUG
1189 struct il_rx_pkt *pkt = rxb_addr(rxb);
1190 struct il_scanreq_notification *notif =
1191 (struct il_scanreq_notification *)pkt->u.raw;
1192
1193 D_SCAN("Scan request status = 0x%x\n", notif->status);
1194#endif
1195}
1196
1197/* Service N_SCAN_START (0x82) */
1198static void il_hdl_scan_start(struct il_priv *il,
1199 struct il_rx_buf *rxb)
1200{
1201 struct il_rx_pkt *pkt = rxb_addr(rxb);
1202 struct il_scanstart_notification *notif =
1203 (struct il_scanstart_notification *)pkt->u.raw;
1204 il->scan_start_tsf = le32_to_cpu(notif->tsf_low);
1205 D_SCAN("Scan start: "
1206 "%d [802.11%s] "
1207 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
1208 notif->channel,
1209 notif->band ? "bg" : "a",
1210 le32_to_cpu(notif->tsf_high),
1211 le32_to_cpu(notif->tsf_low),
1212 notif->status, notif->beacon_timer);
1213}
1214
1215/* Service N_SCAN_RESULTS (0x83) */
1216static void il_hdl_scan_results(struct il_priv *il,
1217 struct il_rx_buf *rxb)
1218{
1219#ifdef CONFIG_IWLEGACY_DEBUG
1220 struct il_rx_pkt *pkt = rxb_addr(rxb);
1221 struct il_scanresults_notification *notif =
1222 (struct il_scanresults_notification *)pkt->u.raw;
1223
1224 D_SCAN("Scan ch.res: "
1225 "%d [802.11%s] "
1226 "(TSF: 0x%08X:%08X) - %d "
1227 "elapsed=%lu usec\n",
1228 notif->channel,
1229 notif->band ? "bg" : "a",
1230 le32_to_cpu(notif->tsf_high),
1231 le32_to_cpu(notif->tsf_low),
1232 le32_to_cpu(notif->stats[0]),
1233 le32_to_cpu(notif->tsf_low) - il->scan_start_tsf);
1234#endif
1235}
1236
1237/* Service N_SCAN_COMPLETE (0x84) */
1238static void il_hdl_scan_complete(struct il_priv *il,
1239 struct il_rx_buf *rxb)
1240{
1241
1242#ifdef CONFIG_IWLEGACY_DEBUG
1243 struct il_rx_pkt *pkt = rxb_addr(rxb);
1244 struct il_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
1245#endif
1246
1247 D_SCAN(
1248 "Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
1249 scan_notif->scanned_channels,
1250 scan_notif->tsf_low,
1251 scan_notif->tsf_high, scan_notif->status);
1252
1253 /* The HW is no longer scanning */
1254 clear_bit(S_SCAN_HW, &il->status);
1255
1256 D_SCAN("Scan on %sGHz took %dms\n",
1257 (il->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
1258 jiffies_to_msecs(jiffies - il->scan_start));
1259
1260 queue_work(il->workqueue, &il->scan_completed);
1261}
1262
1263void il_setup_rx_scan_handlers(struct il_priv *il)
1264{
1265 /* scan handlers */
1266 il->handlers[C_SCAN] = il_hdl_scan;
1267 il->handlers[N_SCAN_START] =
1268 il_hdl_scan_start;
1269 il->handlers[N_SCAN_RESULTS] =
1270 il_hdl_scan_results;
1271 il->handlers[N_SCAN_COMPLETE] =
1272 il_hdl_scan_complete;
1273}
1274EXPORT_SYMBOL(il_setup_rx_scan_handlers);
1275
1276inline u16 il_get_active_dwell_time(struct il_priv *il,
1277 enum ieee80211_band band,
1278 u8 n_probes)
1279{
1280 if (band == IEEE80211_BAND_5GHZ)
1281 return IL_ACTIVE_DWELL_TIME_52 +
1282 IL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
1283 else
1284 return IL_ACTIVE_DWELL_TIME_24 +
1285 IL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
1286}
1287EXPORT_SYMBOL(il_get_active_dwell_time);
1288
1289u16 il_get_passive_dwell_time(struct il_priv *il,
1290 enum ieee80211_band band,
1291 struct ieee80211_vif *vif)
1292{
1293 struct il_rxon_context *ctx = &il->ctx;
1294 u16 value;
1295
1296 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
1297 IL_PASSIVE_DWELL_BASE + IL_PASSIVE_DWELL_TIME_24 :
1298 IL_PASSIVE_DWELL_BASE + IL_PASSIVE_DWELL_TIME_52;
1299
1300 if (il_is_any_associated(il)) {
1301 /*
1302 * If we're associated, we clamp the maximum passive
1303 * dwell time to be 98% of the smallest beacon interval
1304 * (minus 2 * channel tune time)
1305 */
1306 value = ctx->vif ? ctx->vif->bss_conf.beacon_int : 0;
1307 if (value > IL_PASSIVE_DWELL_BASE || !value)
1308 value = IL_PASSIVE_DWELL_BASE;
1309 value = (value * 98) / 100 - IL_CHANNEL_TUNE_TIME * 2;
1310 passive = min(value, passive);
1311 }
1312
1313 return passive;
1314}
1315EXPORT_SYMBOL(il_get_passive_dwell_time);
1316
1317void il_init_scan_params(struct il_priv *il)
1318{
1319 u8 ant_idx = fls(il->hw_params.valid_tx_ant) - 1;
1320 if (!il->scan_tx_ant[IEEE80211_BAND_5GHZ])
1321 il->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
1322 if (!il->scan_tx_ant[IEEE80211_BAND_2GHZ])
1323 il->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
1324}
1325EXPORT_SYMBOL(il_init_scan_params);
1326
1327static int il_scan_initiate(struct il_priv *il,
1328 struct ieee80211_vif *vif)
1329{
1330 int ret;
1331
1332 lockdep_assert_held(&il->mutex);
1333
1334 if (WARN_ON(!il->cfg->ops->utils->request_scan))
1335 return -EOPNOTSUPP;
1336
1337 cancel_delayed_work(&il->scan_check);
1338
1339 if (!il_is_ready_rf(il)) {
1340 IL_WARN("Request scan called when driver not ready.\n");
1341 return -EIO;
1342 }
1343
1344 if (test_bit(S_SCAN_HW, &il->status)) {
1345 D_SCAN(
1346 "Multiple concurrent scan requests in parallel.\n");
1347 return -EBUSY;
1348 }
1349
1350 if (test_bit(S_SCAN_ABORTING, &il->status)) {
1351 D_SCAN("Scan request while abort pending.\n");
1352 return -EBUSY;
1353 }
1354
1355 D_SCAN("Starting scan...\n");
1356
1357 set_bit(S_SCANNING, &il->status);
1358 il->scan_start = jiffies;
1359
1360 ret = il->cfg->ops->utils->request_scan(il, vif);
1361 if (ret) {
1362 clear_bit(S_SCANNING, &il->status);
1363 return ret;
1364 }
1365
1366 queue_delayed_work(il->workqueue, &il->scan_check,
1367 IL_SCAN_CHECK_WATCHDOG);
1368
1369 return 0;
1370}
1371
1372int il_mac_hw_scan(struct ieee80211_hw *hw,
1373 struct ieee80211_vif *vif,
1374 struct cfg80211_scan_request *req)
1375{
1376 struct il_priv *il = hw->priv;
1377 int ret;
1378
1379 D_MAC80211("enter\n");
1380
1381 if (req->n_channels == 0)
1382 return -EINVAL;
1383
1384 mutex_lock(&il->mutex);
1385
1386 if (test_bit(S_SCANNING, &il->status)) {
1387 D_SCAN("Scan already in progress.\n");
1388 ret = -EAGAIN;
1389 goto out_unlock;
1390 }
1391
1392 /* mac80211 will only ask for one band at a time */
1393 il->scan_request = req;
1394 il->scan_vif = vif;
1395 il->scan_band = req->channels[0]->band;
1396
1397 ret = il_scan_initiate(il, vif);
1398
1399 D_MAC80211("leave\n");
1400
1401out_unlock:
1402 mutex_unlock(&il->mutex);
1403
1404 return ret;
1405}
1406EXPORT_SYMBOL(il_mac_hw_scan);
1407
1408static void il_bg_scan_check(struct work_struct *data)
1409{
1410 struct il_priv *il =
1411 container_of(data, struct il_priv, scan_check.work);
1412
1413 D_SCAN("Scan check work\n");
1414
1415 /* Since we are here firmware does not finish scan and
1416 * most likely is in bad shape, so we don't bother to
1417 * send abort command, just force scan complete to mac80211 */
1418 mutex_lock(&il->mutex);
1419 il_force_scan_end(il);
1420 mutex_unlock(&il->mutex);
1421}
1422
1423/**
1424 * il_fill_probe_req - fill in all required fields and IE for probe request
1425 */
1426
1427u16
1428il_fill_probe_req(struct il_priv *il, struct ieee80211_mgmt *frame,
1429 const u8 *ta, const u8 *ies, int ie_len, int left)
1430{
1431 int len = 0;
1432 u8 *pos = NULL;
1433
1434 /* Make sure there is enough space for the probe request,
1435 * two mandatory IEs and the data */
1436 left -= 24;
1437 if (left < 0)
1438 return 0;
1439
1440 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1441 memcpy(frame->da, il_bcast_addr, ETH_ALEN);
1442 memcpy(frame->sa, ta, ETH_ALEN);
1443 memcpy(frame->bssid, il_bcast_addr, ETH_ALEN);
1444 frame->seq_ctrl = 0;
1445
1446 len += 24;
1447
1448 /* ...next IE... */
1449 pos = &frame->u.probe_req.variable[0];
1450
1451 /* fill in our indirect SSID IE */
1452 left -= 2;
1453 if (left < 0)
1454 return 0;
1455 *pos++ = WLAN_EID_SSID;
1456 *pos++ = 0;
1457
1458 len += 2;
1459
1460 if (WARN_ON(left < ie_len))
1461 return len;
1462
1463 if (ies && ie_len) {
1464 memcpy(pos, ies, ie_len);
1465 len += ie_len;
1466 }
1467
1468 return (u16)len;
1469}
1470EXPORT_SYMBOL(il_fill_probe_req);
1471
1472static void il_bg_abort_scan(struct work_struct *work)
1473{
1474 struct il_priv *il = container_of(work, struct il_priv, abort_scan);
1475
1476 D_SCAN("Abort scan work\n");
1477
1478 /* We keep scan_check work queued in case when firmware will not
1479 * report back scan completed notification */
1480 mutex_lock(&il->mutex);
1481 il_scan_cancel_timeout(il, 200);
1482 mutex_unlock(&il->mutex);
1483}
1484
1485static void il_bg_scan_completed(struct work_struct *work)
1486{
1487 struct il_priv *il =
1488 container_of(work, struct il_priv, scan_completed);
1489 bool aborted;
1490
1491 D_SCAN("Completed scan.\n");
1492
1493 cancel_delayed_work(&il->scan_check);
1494
1495 mutex_lock(&il->mutex);
1496
1497 aborted = test_and_clear_bit(S_SCAN_ABORTING, &il->status);
1498 if (aborted)
1499 D_SCAN("Aborted scan completed.\n");
1500
1501 if (!test_and_clear_bit(S_SCANNING, &il->status)) {
1502 D_SCAN("Scan already completed.\n");
1503 goto out_settings;
1504 }
1505
1506 il_complete_scan(il, aborted);
1507
1508out_settings:
1509 /* Can we still talk to firmware ? */
1510 if (!il_is_ready_rf(il))
1511 goto out;
1512
1513 /*
1514 * We do not commit power settings while scan is pending,
1515 * do it now if the settings changed.
1516 */
1517 il_power_set_mode(il, &il->power_data.sleep_cmd_next, false);
1518 il_set_tx_power(il, il->tx_power_next, false);
1519
1520 il->cfg->ops->utils->post_scan(il);
1521
1522out:
1523 mutex_unlock(&il->mutex);
1524}
1525
1526void il_setup_scan_deferred_work(struct il_priv *il)
1527{
1528 INIT_WORK(&il->scan_completed, il_bg_scan_completed);
1529 INIT_WORK(&il->abort_scan, il_bg_abort_scan);
1530 INIT_DELAYED_WORK(&il->scan_check, il_bg_scan_check);
1531}
1532EXPORT_SYMBOL(il_setup_scan_deferred_work);
1533
1534void il_cancel_scan_deferred_work(struct il_priv *il)
1535{
1536 cancel_work_sync(&il->abort_scan);
1537 cancel_work_sync(&il->scan_completed);
1538
1539 if (cancel_delayed_work_sync(&il->scan_check)) {
1540 mutex_lock(&il->mutex);
1541 il_force_scan_end(il);
1542 mutex_unlock(&il->mutex);
1543 }
1544}
1545EXPORT_SYMBOL(il_cancel_scan_deferred_work);
1546
1547/* il->sta_lock must be held */
1548static void il_sta_ucode_activate(struct il_priv *il, u8 sta_id)
1549{
1550
1551 if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE))
1552 IL_ERR(
1553 "ACTIVATE a non DRIVER active station id %u addr %pM\n",
1554 sta_id, il->stations[sta_id].sta.sta.addr);
1555
1556 if (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) {
1557 D_ASSOC(
1558 "STA id %u addr %pM already present"
1559 " in uCode (according to driver)\n",
1560 sta_id, il->stations[sta_id].sta.sta.addr);
1561 } else {
1562 il->stations[sta_id].used |= IL_STA_UCODE_ACTIVE;
1563 D_ASSOC("Added STA id %u addr %pM to uCode\n",
1564 sta_id, il->stations[sta_id].sta.sta.addr);
1565 }
1566}
1567
1568static int il_process_add_sta_resp(struct il_priv *il,
1569 struct il_addsta_cmd *addsta,
1570 struct il_rx_pkt *pkt,
1571 bool sync)
1572{
1573 u8 sta_id = addsta->sta.sta_id;
1574 unsigned long flags;
1575 int ret = -EIO;
1576
1577 if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
1578 IL_ERR("Bad return from C_ADD_STA (0x%08X)\n",
1579 pkt->hdr.flags);
1580 return ret;
1581 }
1582
1583 D_INFO("Processing response for adding station %u\n",
1584 sta_id);
1585
1586 spin_lock_irqsave(&il->sta_lock, flags);
1587
1588 switch (pkt->u.add_sta.status) {
1589 case ADD_STA_SUCCESS_MSK:
1590 D_INFO("C_ADD_STA PASSED\n");
1591 il_sta_ucode_activate(il, sta_id);
1592 ret = 0;
1593 break;
1594 case ADD_STA_NO_ROOM_IN_TBL:
1595 IL_ERR("Adding station %d failed, no room in table.\n",
1596 sta_id);
1597 break;
1598 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
1599 IL_ERR(
1600 "Adding station %d failed, no block ack resource.\n",
1601 sta_id);
1602 break;
1603 case ADD_STA_MODIFY_NON_EXIST_STA:
1604 IL_ERR("Attempting to modify non-existing station %d\n",
1605 sta_id);
1606 break;
1607 default:
1608 D_ASSOC("Received C_ADD_STA:(0x%08X)\n",
1609 pkt->u.add_sta.status);
1610 break;
1611 }
1612
1613 D_INFO("%s station id %u addr %pM\n",
1614 il->stations[sta_id].sta.mode ==
1615 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
1616 sta_id, il->stations[sta_id].sta.sta.addr);
1617
1618 /*
1619 * XXX: The MAC address in the command buffer is often changed from
1620 * the original sent to the device. That is, the MAC address
1621 * written to the command buffer often is not the same MAC address
1622 * read from the command buffer when the command returns. This
1623 * issue has not yet been resolved and this debugging is left to
1624 * observe the problem.
1625 */
1626 D_INFO("%s station according to cmd buffer %pM\n",
1627 il->stations[sta_id].sta.mode ==
1628 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
1629 addsta->sta.addr);
1630 spin_unlock_irqrestore(&il->sta_lock, flags);
1631
1632 return ret;
1633}
1634
1635static void il_add_sta_callback(struct il_priv *il,
1636 struct il_device_cmd *cmd,
1637 struct il_rx_pkt *pkt)
1638{
1639 struct il_addsta_cmd *addsta =
1640 (struct il_addsta_cmd *)cmd->cmd.payload;
1641
1642 il_process_add_sta_resp(il, addsta, pkt, false);
1643
1644}
1645
1646int il_send_add_sta(struct il_priv *il,
1647 struct il_addsta_cmd *sta, u8 flags)
1648{
1649 struct il_rx_pkt *pkt = NULL;
1650 int ret = 0;
1651 u8 data[sizeof(*sta)];
1652 struct il_host_cmd cmd = {
1653 .id = C_ADD_STA,
1654 .flags = flags,
1655 .data = data,
1656 };
1657 u8 sta_id __maybe_unused = sta->sta.sta_id;
1658
1659 D_INFO("Adding sta %u (%pM) %ssynchronously\n",
1660 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
1661
1662 if (flags & CMD_ASYNC)
1663 cmd.callback = il_add_sta_callback;
1664 else {
1665 cmd.flags |= CMD_WANT_SKB;
1666 might_sleep();
1667 }
1668
1669 cmd.len = il->cfg->ops->utils->build_addsta_hcmd(sta, data);
1670 ret = il_send_cmd(il, &cmd);
1671
1672 if (ret || (flags & CMD_ASYNC))
1673 return ret;
1674
1675 if (ret == 0) {
1676 pkt = (struct il_rx_pkt *)cmd.reply_page;
1677 ret = il_process_add_sta_resp(il, sta, pkt, true);
1678 }
1679 il_free_pages(il, cmd.reply_page);
1680
1681 return ret;
1682}
1683EXPORT_SYMBOL(il_send_add_sta);
1684
1685static void il_set_ht_add_station(struct il_priv *il, u8 idx,
1686 struct ieee80211_sta *sta,
1687 struct il_rxon_context *ctx)
1688{
1689 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
1690 __le32 sta_flags;
1691 u8 mimo_ps_mode;
1692
1693 if (!sta || !sta_ht_inf->ht_supported)
1694 goto done;
1695
1696 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
1697 D_ASSOC("spatial multiplexing power save mode: %s\n",
1698 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
1699 "static" :
1700 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
1701 "dynamic" : "disabled");
1702
1703 sta_flags = il->stations[idx].sta.station_flags;
1704
1705 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
1706
1707 switch (mimo_ps_mode) {
1708 case WLAN_HT_CAP_SM_PS_STATIC:
1709 sta_flags |= STA_FLG_MIMO_DIS_MSK;
1710 break;
1711 case WLAN_HT_CAP_SM_PS_DYNAMIC:
1712 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
1713 break;
1714 case WLAN_HT_CAP_SM_PS_DISABLED:
1715 break;
1716 default:
1717 IL_WARN("Invalid MIMO PS mode %d\n", mimo_ps_mode);
1718 break;
1719 }
1720
1721 sta_flags |= cpu_to_le32(
1722 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
1723
1724 sta_flags |= cpu_to_le32(
1725 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
1726
1727 if (il_is_ht40_tx_allowed(il, ctx, &sta->ht_cap))
1728 sta_flags |= STA_FLG_HT40_EN_MSK;
1729 else
1730 sta_flags &= ~STA_FLG_HT40_EN_MSK;
1731
1732 il->stations[idx].sta.station_flags = sta_flags;
1733 done:
1734 return;
1735}
1736
1737/**
1738 * il_prep_station - Prepare station information for addition
1739 *
1740 * should be called with sta_lock held
1741 */
1742u8 il_prep_station(struct il_priv *il, struct il_rxon_context *ctx,
1743 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
1744{
1745 struct il_station_entry *station;
1746 int i;
1747 u8 sta_id = IL_INVALID_STATION;
1748 u16 rate;
1749
1750 if (is_ap)
1751 sta_id = ctx->ap_sta_id;
1752 else if (is_broadcast_ether_addr(addr))
1753 sta_id = ctx->bcast_sta_id;
1754 else
1755 for (i = IL_STA_ID; i < il->hw_params.max_stations; i++) {
1756 if (!compare_ether_addr(il->stations[i].sta.sta.addr,
1757 addr)) {
1758 sta_id = i;
1759 break;
1760 }
1761
1762 if (!il->stations[i].used &&
1763 sta_id == IL_INVALID_STATION)
1764 sta_id = i;
1765 }
1766
1767 /*
1768 * These two conditions have the same outcome, but keep them
1769 * separate
1770 */
1771 if (unlikely(sta_id == IL_INVALID_STATION))
1772 return sta_id;
1773
1774 /*
1775 * uCode is not able to deal with multiple requests to add a
1776 * station. Keep track if one is in progress so that we do not send
1777 * another.
1778 */
1779 if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
1780 D_INFO(
1781 "STA %d already in process of being added.\n",
1782 sta_id);
1783 return sta_id;
1784 }
1785
1786 if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
1787 (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) &&
1788 !compare_ether_addr(il->stations[sta_id].sta.sta.addr, addr)) {
1789 D_ASSOC(
1790 "STA %d (%pM) already added, not adding again.\n",
1791 sta_id, addr);
1792 return sta_id;
1793 }
1794
1795 station = &il->stations[sta_id];
1796 station->used = IL_STA_DRIVER_ACTIVE;
1797 D_ASSOC("Add STA to driver ID %d: %pM\n",
1798 sta_id, addr);
1799 il->num_stations++;
1800
1801 /* Set up the C_ADD_STA command to send to device */
1802 memset(&station->sta, 0, sizeof(struct il_addsta_cmd));
1803 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
1804 station->sta.mode = 0;
1805 station->sta.sta.sta_id = sta_id;
1806 station->sta.station_flags = ctx->station_flags;
1807 station->ctxid = ctx->ctxid;
1808
1809 if (sta) {
1810 struct il_station_priv_common *sta_priv;
1811
1812 sta_priv = (void *)sta->drv_priv;
1813 sta_priv->ctx = ctx;
1814 }
1815
1816 /*
1817 * OK to call unconditionally, since local stations (IBSS BSSID
1818 * STA and broadcast STA) pass in a NULL sta, and mac80211
1819 * doesn't allow HT IBSS.
1820 */
1821 il_set_ht_add_station(il, sta_id, sta, ctx);
1822
1823 /* 3945 only */
1824 rate = (il->band == IEEE80211_BAND_5GHZ) ?
1825 RATE_6M_PLCP : RATE_1M_PLCP;
1826 /* Turn on both antennas for the station... */
1827 station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
1828
1829 return sta_id;
1830
1831}
1832EXPORT_SYMBOL_GPL(il_prep_station);
1833
1834#define STA_WAIT_TIMEOUT (HZ/2)
1835
1836/**
1837 * il_add_station_common -
1838 */
1839int
1840il_add_station_common(struct il_priv *il,
1841 struct il_rxon_context *ctx,
1842 const u8 *addr, bool is_ap,
1843 struct ieee80211_sta *sta, u8 *sta_id_r)
1844{
1845 unsigned long flags_spin;
1846 int ret = 0;
1847 u8 sta_id;
1848 struct il_addsta_cmd sta_cmd;
1849
1850 *sta_id_r = 0;
1851 spin_lock_irqsave(&il->sta_lock, flags_spin);
1852 sta_id = il_prep_station(il, ctx, addr, is_ap, sta);
1853 if (sta_id == IL_INVALID_STATION) {
1854 IL_ERR("Unable to prepare station %pM for addition\n",
1855 addr);
1856 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
1857 return -EINVAL;
1858 }
1859
1860 /*
1861 * uCode is not able to deal with multiple requests to add a
1862 * station. Keep track if one is in progress so that we do not send
1863 * another.
1864 */
1865 if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
1866 D_INFO(
1867 "STA %d already in process of being added.\n",
1868 sta_id);
1869 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
1870 return -EEXIST;
1871 }
1872
1873 if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
1874 (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
1875 D_ASSOC(
1876 "STA %d (%pM) already added, not adding again.\n",
1877 sta_id, addr);
1878 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
1879 return -EEXIST;
1880 }
1881
1882 il->stations[sta_id].used |= IL_STA_UCODE_INPROGRESS;
1883 memcpy(&sta_cmd, &il->stations[sta_id].sta,
1884 sizeof(struct il_addsta_cmd));
1885 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
1886
1887 /* Add station to device's station table */
1888 ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
1889 if (ret) {
1890 spin_lock_irqsave(&il->sta_lock, flags_spin);
1891 IL_ERR("Adding station %pM failed.\n",
1892 il->stations[sta_id].sta.sta.addr);
1893 il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
1894 il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
1895 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
1896 }
1897 *sta_id_r = sta_id;
1898 return ret;
1899}
1900EXPORT_SYMBOL(il_add_station_common);
1901
1902/**
1903 * il_sta_ucode_deactivate - deactivate ucode status for a station
1904 *
1905 * il->sta_lock must be held
1906 */
1907static void il_sta_ucode_deactivate(struct il_priv *il, u8 sta_id)
1908{
1909 /* Ucode must be active and driver must be non active */
1910 if ((il->stations[sta_id].used &
1911 (IL_STA_UCODE_ACTIVE | IL_STA_DRIVER_ACTIVE)) !=
1912 IL_STA_UCODE_ACTIVE)
1913 IL_ERR("removed non active STA %u\n", sta_id);
1914
1915 il->stations[sta_id].used &= ~IL_STA_UCODE_ACTIVE;
1916
1917 memset(&il->stations[sta_id], 0, sizeof(struct il_station_entry));
1918 D_ASSOC("Removed STA %u\n", sta_id);
1919}
1920
1921static int il_send_remove_station(struct il_priv *il,
1922 const u8 *addr, int sta_id,
1923 bool temporary)
1924{
1925 struct il_rx_pkt *pkt;
1926 int ret;
1927
1928 unsigned long flags_spin;
1929 struct il_rem_sta_cmd rm_sta_cmd;
1930
1931 struct il_host_cmd cmd = {
1932 .id = C_REM_STA,
1933 .len = sizeof(struct il_rem_sta_cmd),
1934 .flags = CMD_SYNC,
1935 .data = &rm_sta_cmd,
1936 };
1937
1938 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
1939 rm_sta_cmd.num_sta = 1;
1940 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
1941
1942 cmd.flags |= CMD_WANT_SKB;
1943
1944 ret = il_send_cmd(il, &cmd);
1945
1946 if (ret)
1947 return ret;
1948
1949 pkt = (struct il_rx_pkt *)cmd.reply_page;
1950 if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
1951 IL_ERR("Bad return from C_REM_STA (0x%08X)\n",
1952 pkt->hdr.flags);
1953 ret = -EIO;
1954 }
1955
1956 if (!ret) {
1957 switch (pkt->u.rem_sta.status) {
1958 case REM_STA_SUCCESS_MSK:
1959 if (!temporary) {
1960 spin_lock_irqsave(&il->sta_lock, flags_spin);
1961 il_sta_ucode_deactivate(il, sta_id);
1962 spin_unlock_irqrestore(&il->sta_lock,
1963 flags_spin);
1964 }
1965 D_ASSOC("C_REM_STA PASSED\n");
1966 break;
1967 default:
1968 ret = -EIO;
1969 IL_ERR("C_REM_STA failed\n");
1970 break;
1971 }
1972 }
1973 il_free_pages(il, cmd.reply_page);
1974
1975 return ret;
1976}
1977
1978/**
1979 * il_remove_station - Remove driver's knowledge of station.
1980 */
1981int il_remove_station(struct il_priv *il, const u8 sta_id,
1982 const u8 *addr)
1983{
1984 unsigned long flags;
1985
1986 if (!il_is_ready(il)) {
1987 D_INFO(
1988 "Unable to remove station %pM, device not ready.\n",
1989 addr);
1990 /*
1991 * It is typical for stations to be removed when we are
1992 * going down. Return success since device will be down
1993 * soon anyway
1994 */
1995 return 0;
1996 }
1997
1998 D_ASSOC("Removing STA from driver:%d %pM\n",
1999 sta_id, addr);
2000
2001 if (WARN_ON(sta_id == IL_INVALID_STATION))
2002 return -EINVAL;
2003
2004 spin_lock_irqsave(&il->sta_lock, flags);
2005
2006 if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE)) {
2007 D_INFO("Removing %pM but non DRIVER active\n",
2008 addr);
2009 goto out_err;
2010 }
2011
2012 if (!(il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
2013 D_INFO("Removing %pM but non UCODE active\n",
2014 addr);
2015 goto out_err;
2016 }
2017
2018 if (il->stations[sta_id].used & IL_STA_LOCAL) {
2019 kfree(il->stations[sta_id].lq);
2020 il->stations[sta_id].lq = NULL;
2021 }
2022
2023 il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
2024
2025 il->num_stations--;
2026
2027 BUG_ON(il->num_stations < 0);
2028
2029 spin_unlock_irqrestore(&il->sta_lock, flags);
2030
2031 return il_send_remove_station(il, addr, sta_id, false);
2032out_err:
2033 spin_unlock_irqrestore(&il->sta_lock, flags);
2034 return -EINVAL;
2035}
2036EXPORT_SYMBOL_GPL(il_remove_station);
2037
2038/**
2039 * il_clear_ucode_stations - clear ucode station table bits
2040 *
2041 * This function clears all the bits in the driver indicating
2042 * which stations are active in the ucode. Call when something
2043 * other than explicit station management would cause this in
2044 * the ucode, e.g. unassociated RXON.
2045 */
2046void il_clear_ucode_stations(struct il_priv *il,
2047 struct il_rxon_context *ctx)
2048{
2049 int i;
2050 unsigned long flags_spin;
2051 bool cleared = false;
2052
2053 D_INFO("Clearing ucode stations in driver\n");
2054
2055 spin_lock_irqsave(&il->sta_lock, flags_spin);
2056 for (i = 0; i < il->hw_params.max_stations; i++) {
2057 if (ctx && ctx->ctxid != il->stations[i].ctxid)
2058 continue;
2059
2060 if (il->stations[i].used & IL_STA_UCODE_ACTIVE) {
2061 D_INFO(
2062 "Clearing ucode active for station %d\n", i);
2063 il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
2064 cleared = true;
2065 }
2066 }
2067 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2068
2069 if (!cleared)
2070 D_INFO(
2071 "No active stations found to be cleared\n");
2072}
2073EXPORT_SYMBOL(il_clear_ucode_stations);
2074
2075/**
2076 * il_restore_stations() - Restore driver known stations to device
2077 *
2078 * All stations considered active by driver, but not present in ucode, is
2079 * restored.
2080 *
2081 * Function sleeps.
2082 */
2083void
2084il_restore_stations(struct il_priv *il, struct il_rxon_context *ctx)
2085{
2086 struct il_addsta_cmd sta_cmd;
2087 struct il_link_quality_cmd lq;
2088 unsigned long flags_spin;
2089 int i;
2090 bool found = false;
2091 int ret;
2092 bool send_lq;
2093
2094 if (!il_is_ready(il)) {
2095 D_INFO(
2096 "Not ready yet, not restoring any stations.\n");
2097 return;
2098 }
2099
2100 D_ASSOC("Restoring all known stations ... start.\n");
2101 spin_lock_irqsave(&il->sta_lock, flags_spin);
2102 for (i = 0; i < il->hw_params.max_stations; i++) {
2103 if (ctx->ctxid != il->stations[i].ctxid)
2104 continue;
2105 if ((il->stations[i].used & IL_STA_DRIVER_ACTIVE) &&
2106 !(il->stations[i].used & IL_STA_UCODE_ACTIVE)) {
2107 D_ASSOC("Restoring sta %pM\n",
2108 il->stations[i].sta.sta.addr);
2109 il->stations[i].sta.mode = 0;
2110 il->stations[i].used |= IL_STA_UCODE_INPROGRESS;
2111 found = true;
2112 }
2113 }
2114
2115 for (i = 0; i < il->hw_params.max_stations; i++) {
2116 if ((il->stations[i].used & IL_STA_UCODE_INPROGRESS)) {
2117 memcpy(&sta_cmd, &il->stations[i].sta,
2118 sizeof(struct il_addsta_cmd));
2119 send_lq = false;
2120 if (il->stations[i].lq) {
2121 memcpy(&lq, il->stations[i].lq,
2122 sizeof(struct il_link_quality_cmd));
2123 send_lq = true;
2124 }
2125 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2126 ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
2127 if (ret) {
2128 spin_lock_irqsave(&il->sta_lock, flags_spin);
2129 IL_ERR("Adding station %pM failed.\n",
2130 il->stations[i].sta.sta.addr);
2131 il->stations[i].used &=
2132 ~IL_STA_DRIVER_ACTIVE;
2133 il->stations[i].used &=
2134 ~IL_STA_UCODE_INPROGRESS;
2135 spin_unlock_irqrestore(&il->sta_lock,
2136 flags_spin);
2137 }
2138 /*
2139 * Rate scaling has already been initialized, send
2140 * current LQ command
2141 */
2142 if (send_lq)
2143 il_send_lq_cmd(il, ctx, &lq,
2144 CMD_SYNC, true);
2145 spin_lock_irqsave(&il->sta_lock, flags_spin);
2146 il->stations[i].used &= ~IL_STA_UCODE_INPROGRESS;
2147 }
2148 }
2149
2150 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2151 if (!found)
2152 D_INFO("Restoring all known stations"
2153 " .... no stations to be restored.\n");
2154 else
2155 D_INFO("Restoring all known stations"
2156 " .... complete.\n");
2157}
2158EXPORT_SYMBOL(il_restore_stations);
2159
2160int il_get_free_ucode_key_idx(struct il_priv *il)
2161{
2162 int i;
2163
2164 for (i = 0; i < il->sta_key_max_num; i++)
2165 if (!test_and_set_bit(i, &il->ucode_key_table))
2166 return i;
2167
2168 return WEP_INVALID_OFFSET;
2169}
2170EXPORT_SYMBOL(il_get_free_ucode_key_idx);
2171
2172void il_dealloc_bcast_stations(struct il_priv *il)
2173{
2174 unsigned long flags;
2175 int i;
2176
2177 spin_lock_irqsave(&il->sta_lock, flags);
2178 for (i = 0; i < il->hw_params.max_stations; i++) {
2179 if (!(il->stations[i].used & IL_STA_BCAST))
2180 continue;
2181
2182 il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
2183 il->num_stations--;
2184 BUG_ON(il->num_stations < 0);
2185 kfree(il->stations[i].lq);
2186 il->stations[i].lq = NULL;
2187 }
2188 spin_unlock_irqrestore(&il->sta_lock, flags);
2189}
2190EXPORT_SYMBOL_GPL(il_dealloc_bcast_stations);
2191
2192#ifdef CONFIG_IWLEGACY_DEBUG
2193static void il_dump_lq_cmd(struct il_priv *il,
2194 struct il_link_quality_cmd *lq)
2195{
2196 int i;
2197 D_RATE("lq station id 0x%x\n", lq->sta_id);
2198 D_RATE("lq ant 0x%X 0x%X\n",
2199 lq->general_params.single_stream_ant_msk,
2200 lq->general_params.dual_stream_ant_msk);
2201
2202 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2203 D_RATE("lq idx %d 0x%X\n",
2204 i, lq->rs_table[i].rate_n_flags);
2205}
2206#else
2207static inline void il_dump_lq_cmd(struct il_priv *il,
2208 struct il_link_quality_cmd *lq)
2209{
2210}
2211#endif
2212
2213/**
2214 * il_is_lq_table_valid() - Test one aspect of LQ cmd for validity
2215 *
2216 * It sometimes happens when a HT rate has been in use and we
2217 * loose connectivity with AP then mac80211 will first tell us that the
2218 * current channel is not HT anymore before removing the station. In such a
2219 * scenario the RXON flags will be updated to indicate we are not
2220 * communicating HT anymore, but the LQ command may still contain HT rates.
2221 * Test for this to prevent driver from sending LQ command between the time
2222 * RXON flags are updated and when LQ command is updated.
2223 */
2224static bool il_is_lq_table_valid(struct il_priv *il,
2225 struct il_rxon_context *ctx,
2226 struct il_link_quality_cmd *lq)
2227{
2228 int i;
2229
2230 if (ctx->ht.enabled)
2231 return true;
2232
2233 D_INFO("Channel %u is not an HT channel\n",
2234 ctx->active.channel);
2235 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2236 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
2237 RATE_MCS_HT_MSK) {
2238 D_INFO(
2239 "idx %d of LQ expects HT channel\n",
2240 i);
2241 return false;
2242 }
2243 }
2244 return true;
2245}
2246
2247/**
2248 * il_send_lq_cmd() - Send link quality command
2249 * @init: This command is sent as part of station initialization right
2250 * after station has been added.
2251 *
2252 * The link quality command is sent as the last step of station creation.
2253 * This is the special case in which init is set and we call a callback in
2254 * this case to clear the state indicating that station creation is in
2255 * progress.
2256 */
2257int il_send_lq_cmd(struct il_priv *il, struct il_rxon_context *ctx,
2258 struct il_link_quality_cmd *lq, u8 flags, bool init)
2259{
2260 int ret = 0;
2261 unsigned long flags_spin;
2262
2263 struct il_host_cmd cmd = {
2264 .id = C_TX_LINK_QUALITY_CMD,
2265 .len = sizeof(struct il_link_quality_cmd),
2266 .flags = flags,
2267 .data = lq,
2268 };
2269
2270 if (WARN_ON(lq->sta_id == IL_INVALID_STATION))
2271 return -EINVAL;
2272
2273
2274 spin_lock_irqsave(&il->sta_lock, flags_spin);
2275 if (!(il->stations[lq->sta_id].used & IL_STA_DRIVER_ACTIVE)) {
2276 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2277 return -EINVAL;
2278 }
2279 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2280
2281 il_dump_lq_cmd(il, lq);
2282 BUG_ON(init && (cmd.flags & CMD_ASYNC));
2283
2284 if (il_is_lq_table_valid(il, ctx, lq))
2285 ret = il_send_cmd(il, &cmd);
2286 else
2287 ret = -EINVAL;
2288
2289 if (cmd.flags & CMD_ASYNC)
2290 return ret;
2291
2292 if (init) {
2293 D_INFO("init LQ command complete,"
2294 " clearing sta addition status for sta %d\n",
2295 lq->sta_id);
2296 spin_lock_irqsave(&il->sta_lock, flags_spin);
2297 il->stations[lq->sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
2298 spin_unlock_irqrestore(&il->sta_lock, flags_spin);
2299 }
2300 return ret;
2301}
2302EXPORT_SYMBOL(il_send_lq_cmd);
2303
2304int il_mac_sta_remove(struct ieee80211_hw *hw,
2305 struct ieee80211_vif *vif,
2306 struct ieee80211_sta *sta)
2307{
2308 struct il_priv *il = hw->priv;
2309 struct il_station_priv_common *sta_common = (void *)sta->drv_priv;
2310 int ret;
2311
2312 D_INFO("received request to remove station %pM\n",
2313 sta->addr);
2314 mutex_lock(&il->mutex);
2315 D_INFO("proceeding to remove station %pM\n",
2316 sta->addr);
2317 ret = il_remove_station(il, sta_common->sta_id, sta->addr);
2318 if (ret)
2319 IL_ERR("Error removing station %pM\n",
2320 sta->addr);
2321 mutex_unlock(&il->mutex);
2322 return ret;
2323}
2324EXPORT_SYMBOL(il_mac_sta_remove);
2325
2326/************************** RX-FUNCTIONS ****************************/
2327/*
2328 * Rx theory of operation
2329 *
2330 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
2331 * each of which point to Receive Buffers to be filled by the NIC. These get
2332 * used not only for Rx frames, but for any command response or notification
2333 * from the NIC. The driver and NIC manage the Rx buffers by means
2334 * of idxes into the circular buffer.
2335 *
2336 * Rx Queue Indexes
2337 * The host/firmware share two idx registers for managing the Rx buffers.
2338 *
2339 * The READ idx maps to the first position that the firmware may be writing
2340 * to -- the driver can read up to (but not including) this position and get
2341 * good data.
2342 * The READ idx is managed by the firmware once the card is enabled.
2343 *
2344 * The WRITE idx maps to the last position the driver has read from -- the
2345 * position preceding WRITE is the last slot the firmware can place a packet.
2346 *
2347 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
2348 * WRITE = READ.
2349 *
2350 * During initialization, the host sets up the READ queue position to the first
2351 * IDX position, and WRITE to the last (READ - 1 wrapped)
2352 *
2353 * When the firmware places a packet in a buffer, it will advance the READ idx
2354 * and fire the RX interrupt. The driver can then query the READ idx and
2355 * process as many packets as possible, moving the WRITE idx forward as it
2356 * resets the Rx queue buffers with new memory.
2357 *
2358 * The management in the driver is as follows:
2359 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
2360 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
2361 * to replenish the iwl->rxq->rx_free.
2362 * + In il_rx_replenish (scheduled) if 'processed' != 'read' then the
2363 * iwl->rxq is replenished and the READ IDX is updated (updating the
2364 * 'processed' and 'read' driver idxes as well)
2365 * + A received packet is processed and handed to the kernel network stack,
2366 * detached from the iwl->rxq. The driver 'processed' idx is updated.
2367 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
2368 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
2369 * IDX is not incremented and iwl->status(RX_STALLED) is set. If there
2370 * were enough free buffers and RX_STALLED is set it is cleared.
2371 *
2372 *
2373 * Driver sequence:
2374 *
2375 * il_rx_queue_alloc() Allocates rx_free
2376 * il_rx_replenish() Replenishes rx_free list from rx_used, and calls
2377 * il_rx_queue_restock
2378 * il_rx_queue_restock() Moves available buffers from rx_free into Rx
2379 * queue, updates firmware pointers, and updates
2380 * the WRITE idx. If insufficient rx_free buffers
2381 * are available, schedules il_rx_replenish
2382 *
2383 * -- enable interrupts --
2384 * ISR - il_rx() Detach il_rx_bufs from pool up to the
2385 * READ IDX, detaching the SKB from the pool.
2386 * Moves the packet buffer from queue to rx_used.
2387 * Calls il_rx_queue_restock to refill any empty
2388 * slots.
2389 * ...
2390 *
2391 */
2392
2393/**
2394 * il_rx_queue_space - Return number of free slots available in queue.
2395 */
2396int il_rx_queue_space(const struct il_rx_queue *q)
2397{
2398 int s = q->read - q->write;
2399 if (s <= 0)
2400 s += RX_QUEUE_SIZE;
2401 /* keep some buffer to not confuse full and empty queue */
2402 s -= 2;
2403 if (s < 0)
2404 s = 0;
2405 return s;
2406}
2407EXPORT_SYMBOL(il_rx_queue_space);
2408
2409/**
2410 * il_rx_queue_update_write_ptr - Update the write pointer for the RX queue
2411 */
2412void
2413il_rx_queue_update_write_ptr(struct il_priv *il,
2414 struct il_rx_queue *q)
2415{
2416 unsigned long flags;
2417 u32 rx_wrt_ptr_reg = il->hw_params.rx_wrt_ptr_reg;
2418 u32 reg;
2419
2420 spin_lock_irqsave(&q->lock, flags);
2421
2422 if (q->need_update == 0)
2423 goto exit_unlock;
2424
2425 /* If power-saving is in use, make sure device is awake */
2426 if (test_bit(S_POWER_PMI, &il->status)) {
2427 reg = _il_rd(il, CSR_UCODE_DRV_GP1);
2428
2429 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
2430 D_INFO(
2431 "Rx queue requesting wakeup,"
2432 " GP1 = 0x%x\n", reg);
2433 il_set_bit(il, CSR_GP_CNTRL,
2434 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2435 goto exit_unlock;
2436 }
2437
2438 q->write_actual = (q->write & ~0x7);
2439 il_wr(il, rx_wrt_ptr_reg,
2440 q->write_actual);
2441
2442 /* Else device is assumed to be awake */
2443 } else {
2444 /* Device expects a multiple of 8 */
2445 q->write_actual = (q->write & ~0x7);
2446 il_wr(il, rx_wrt_ptr_reg,
2447 q->write_actual);
2448 }
2449
2450 q->need_update = 0;
2451
2452 exit_unlock:
2453 spin_unlock_irqrestore(&q->lock, flags);
2454}
2455EXPORT_SYMBOL(il_rx_queue_update_write_ptr);
2456
2457int il_rx_queue_alloc(struct il_priv *il)
2458{
2459 struct il_rx_queue *rxq = &il->rxq;
2460 struct device *dev = &il->pci_dev->dev;
2461 int i;
2462
2463 spin_lock_init(&rxq->lock);
2464 INIT_LIST_HEAD(&rxq->rx_free);
2465 INIT_LIST_HEAD(&rxq->rx_used);
2466
2467 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
2468 rxq->bd = dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->bd_dma,
2469 GFP_KERNEL);
2470 if (!rxq->bd)
2471 goto err_bd;
2472
2473 rxq->rb_stts = dma_alloc_coherent(dev, sizeof(struct il_rb_status),
2474 &rxq->rb_stts_dma, GFP_KERNEL);
2475 if (!rxq->rb_stts)
2476 goto err_rb;
2477
2478 /* Fill the rx_used queue with _all_ of the Rx buffers */
2479 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
2480 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
2481
2482 /* Set us so that we have processed and used all buffers, but have
2483 * not restocked the Rx queue with fresh buffers */
2484 rxq->read = rxq->write = 0;
2485 rxq->write_actual = 0;
2486 rxq->free_count = 0;
2487 rxq->need_update = 0;
2488 return 0;
2489
2490err_rb:
2491 dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
2492 rxq->bd_dma);
2493err_bd:
2494 return -ENOMEM;
2495}
2496EXPORT_SYMBOL(il_rx_queue_alloc);
2497
2498
2499void il_hdl_spectrum_measurement(struct il_priv *il,
2500 struct il_rx_buf *rxb)
2501{
2502 struct il_rx_pkt *pkt = rxb_addr(rxb);
2503 struct il_spectrum_notification *report = &(pkt->u.spectrum_notif);
2504
2505 if (!report->state) {
2506 D_11H(
2507 "Spectrum Measure Notification: Start\n");
2508 return;
2509 }
2510
2511 memcpy(&il->measure_report, report, sizeof(*report));
2512 il->measurement_status |= MEASUREMENT_READY;
2513}
2514EXPORT_SYMBOL(il_hdl_spectrum_measurement);
2515
2516/*
2517 * returns non-zero if packet should be dropped
2518 */
2519int il_set_decrypted_flag(struct il_priv *il,
2520 struct ieee80211_hdr *hdr,
2521 u32 decrypt_res,
2522 struct ieee80211_rx_status *stats)
2523{
2524 u16 fc = le16_to_cpu(hdr->frame_control);
2525
2526 /*
2527 * All contexts have the same setting here due to it being
2528 * a module parameter, so OK to check any context.
2529 */
2530 if (il->ctx.active.filter_flags &
2531 RXON_FILTER_DIS_DECRYPT_MSK)
2532 return 0;
2533
2534 if (!(fc & IEEE80211_FCTL_PROTECTED))
2535 return 0;
2536
2537 D_RX("decrypt_res:0x%x\n", decrypt_res);
2538 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2539 case RX_RES_STATUS_SEC_TYPE_TKIP:
2540 /* The uCode has got a bad phase 1 Key, pushes the packet.
2541 * Decryption will be done in SW. */
2542 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2543 RX_RES_STATUS_BAD_KEY_TTAK)
2544 break;
2545
2546 case RX_RES_STATUS_SEC_TYPE_WEP:
2547 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2548 RX_RES_STATUS_BAD_ICV_MIC) {
2549 /* bad ICV, the packet is destroyed since the
2550 * decryption is inplace, drop it */
2551 D_RX("Packet destroyed\n");
2552 return -1;
2553 }
2554 case RX_RES_STATUS_SEC_TYPE_CCMP:
2555 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2556 RX_RES_STATUS_DECRYPT_OK) {
2557 D_RX("hw decrypt successfully!!!\n");
2558 stats->flag |= RX_FLAG_DECRYPTED;
2559 }
2560 break;
2561
2562 default:
2563 break;
2564 }
2565 return 0;
2566}
2567EXPORT_SYMBOL(il_set_decrypted_flag);
2568
2569/**
2570 * il_txq_update_write_ptr - Send new write idx to hardware
2571 */
2572void
2573il_txq_update_write_ptr(struct il_priv *il, struct il_tx_queue *txq)
2574{
2575 u32 reg = 0;
2576 int txq_id = txq->q.id;
2577
2578 if (txq->need_update == 0)
2579 return;
2580
2581 /* if we're trying to save power */
2582 if (test_bit(S_POWER_PMI, &il->status)) {
2583 /* wake up nic if it's powered down ...
2584 * uCode will wake up, and interrupt us again, so next
2585 * time we'll skip this part. */
2586 reg = _il_rd(il, CSR_UCODE_DRV_GP1);
2587
2588 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
2589 D_INFO(
2590 "Tx queue %d requesting wakeup,"
2591 " GP1 = 0x%x\n", txq_id, reg);
2592 il_set_bit(il, CSR_GP_CNTRL,
2593 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2594 return;
2595 }
2596
2597 il_wr(il, HBUS_TARG_WRPTR,
2598 txq->q.write_ptr | (txq_id << 8));
2599
2600 /*
2601 * else not in power-save mode,
2602 * uCode will never sleep when we're
2603 * trying to tx (during RFKILL, we're not trying to tx).
2604 */
2605 } else
2606 _il_wr(il, HBUS_TARG_WRPTR,
2607 txq->q.write_ptr | (txq_id << 8));
2608 txq->need_update = 0;
2609}
2610EXPORT_SYMBOL(il_txq_update_write_ptr);
2611
2612/**
2613 * il_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
2614 */
2615void il_tx_queue_unmap(struct il_priv *il, int txq_id)
2616{
2617 struct il_tx_queue *txq = &il->txq[txq_id];
2618 struct il_queue *q = &txq->q;
2619
2620 if (q->n_bd == 0)
2621 return;
2622
2623 while (q->write_ptr != q->read_ptr) {
2624 il->cfg->ops->lib->txq_free_tfd(il, txq);
2625 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
2626 }
2627}
2628EXPORT_SYMBOL(il_tx_queue_unmap);
2629
2630/**
2631 * il_tx_queue_free - Deallocate DMA queue.
2632 * @txq: Transmit queue to deallocate.
2633 *
2634 * Empty queue by removing and destroying all BD's.
2635 * Free all buffers.
2636 * 0-fill, but do not free "txq" descriptor structure.
2637 */
2638void il_tx_queue_free(struct il_priv *il, int txq_id)
2639{
2640 struct il_tx_queue *txq = &il->txq[txq_id];
2641 struct device *dev = &il->pci_dev->dev;
2642 int i;
2643
2644 il_tx_queue_unmap(il, txq_id);
2645
2646 /* De-alloc array of command/tx buffers */
2647 for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
2648 kfree(txq->cmd[i]);
2649
2650 /* De-alloc circular buffer of TFDs */
2651 if (txq->q.n_bd)
2652 dma_free_coherent(dev, il->hw_params.tfd_size *
2653 txq->q.n_bd, txq->tfds, txq->q.dma_addr);
2654
2655 /* De-alloc array of per-TFD driver data */
2656 kfree(txq->txb);
2657 txq->txb = NULL;
2658
2659 /* deallocate arrays */
2660 kfree(txq->cmd);
2661 kfree(txq->meta);
2662 txq->cmd = NULL;
2663 txq->meta = NULL;
2664
2665 /* 0-fill queue descriptor structure */
2666 memset(txq, 0, sizeof(*txq));
2667}
2668EXPORT_SYMBOL(il_tx_queue_free);
2669
2670/**
2671 * il_cmd_queue_unmap - Unmap any remaining DMA mappings from command queue
2672 */
2673void il_cmd_queue_unmap(struct il_priv *il)
2674{
2675 struct il_tx_queue *txq = &il->txq[il->cmd_queue];
2676 struct il_queue *q = &txq->q;
2677 int i;
2678
2679 if (q->n_bd == 0)
2680 return;
2681
2682 while (q->read_ptr != q->write_ptr) {
2683 i = il_get_cmd_idx(q, q->read_ptr, 0);
2684
2685 if (txq->meta[i].flags & CMD_MAPPED) {
2686 pci_unmap_single(il->pci_dev,
2687 dma_unmap_addr(&txq->meta[i], mapping),
2688 dma_unmap_len(&txq->meta[i], len),
2689 PCI_DMA_BIDIRECTIONAL);
2690 txq->meta[i].flags = 0;
2691 }
2692
2693 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
2694 }
2695
2696 i = q->n_win;
2697 if (txq->meta[i].flags & CMD_MAPPED) {
2698 pci_unmap_single(il->pci_dev,
2699 dma_unmap_addr(&txq->meta[i], mapping),
2700 dma_unmap_len(&txq->meta[i], len),
2701 PCI_DMA_BIDIRECTIONAL);
2702 txq->meta[i].flags = 0;
2703 }
2704}
2705EXPORT_SYMBOL(il_cmd_queue_unmap);
2706
2707/**
2708 * il_cmd_queue_free - Deallocate DMA queue.
2709 * @txq: Transmit queue to deallocate.
2710 *
2711 * Empty queue by removing and destroying all BD's.
2712 * Free all buffers.
2713 * 0-fill, but do not free "txq" descriptor structure.
2714 */
2715void il_cmd_queue_free(struct il_priv *il)
2716{
2717 struct il_tx_queue *txq = &il->txq[il->cmd_queue];
2718 struct device *dev = &il->pci_dev->dev;
2719 int i;
2720
2721 il_cmd_queue_unmap(il);
2722
2723 /* De-alloc array of command/tx buffers */
2724 for (i = 0; i <= TFD_CMD_SLOTS; i++)
2725 kfree(txq->cmd[i]);
2726
2727 /* De-alloc circular buffer of TFDs */
2728 if (txq->q.n_bd)
2729 dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
2730 txq->tfds, txq->q.dma_addr);
2731
2732 /* deallocate arrays */
2733 kfree(txq->cmd);
2734 kfree(txq->meta);
2735 txq->cmd = NULL;
2736 txq->meta = NULL;
2737
2738 /* 0-fill queue descriptor structure */
2739 memset(txq, 0, sizeof(*txq));
2740}
2741EXPORT_SYMBOL(il_cmd_queue_free);
2742
2743/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
2744 * DMA services
2745 *
2746 * Theory of operation
2747 *
2748 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
2749 * of buffer descriptors, each of which points to one or more data buffers for
2750 * the device to read from or fill. Driver and device exchange status of each
2751 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
2752 * entries in each circular buffer, to protect against confusing empty and full
2753 * queue states.
2754 *
2755 * The device reads or writes the data in the queues via the device's several
2756 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
2757 *
2758 * For Tx queue, there are low mark and high mark limits. If, after queuing
2759 * the packet for Tx, free space become < low mark, Tx queue stopped. When
2760 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
2761 * Tx queue resumed.
2762 *
2763 * See more detailed info in 4965.h.
2764 ***************************************************/
2765
2766int il_queue_space(const struct il_queue *q)
2767{
2768 int s = q->read_ptr - q->write_ptr;
2769
2770 if (q->read_ptr > q->write_ptr)
2771 s -= q->n_bd;
2772
2773 if (s <= 0)
2774 s += q->n_win;
2775 /* keep some reserve to not confuse empty and full situations */
2776 s -= 2;
2777 if (s < 0)
2778 s = 0;
2779 return s;
2780}
2781EXPORT_SYMBOL(il_queue_space);
2782
2783
2784/**
2785 * il_queue_init - Initialize queue's high/low-water and read/write idxes
2786 */
2787static int il_queue_init(struct il_priv *il, struct il_queue *q,
2788 int count, int slots_num, u32 id)
2789{
2790 q->n_bd = count;
2791 q->n_win = slots_num;
2792 q->id = id;
2793
2794 /* count must be power-of-two size, otherwise il_queue_inc_wrap
2795 * and il_queue_dec_wrap are broken. */
2796 BUG_ON(!is_power_of_2(count));
2797
2798 /* slots_num must be power-of-two size, otherwise
2799 * il_get_cmd_idx is broken. */
2800 BUG_ON(!is_power_of_2(slots_num));
2801
2802 q->low_mark = q->n_win / 4;
2803 if (q->low_mark < 4)
2804 q->low_mark = 4;
2805
2806 q->high_mark = q->n_win / 8;
2807 if (q->high_mark < 2)
2808 q->high_mark = 2;
2809
2810 q->write_ptr = q->read_ptr = 0;
2811
2812 return 0;
2813}
2814
2815/**
2816 * il_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
2817 */
2818static int il_tx_queue_alloc(struct il_priv *il,
2819 struct il_tx_queue *txq, u32 id)
2820{
2821 struct device *dev = &il->pci_dev->dev;
2822 size_t tfd_sz = il->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
2823
2824 /* Driver ilate data, only for Tx (not command) queues,
2825 * not shared with device. */
2826 if (id != il->cmd_queue) {
2827 txq->txb = kzalloc(sizeof(txq->txb[0]) *
2828 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
2829 if (!txq->txb) {
2830 IL_ERR("kmalloc for auxiliary BD "
2831 "structures failed\n");
2832 goto error;
2833 }
2834 } else {
2835 txq->txb = NULL;
2836 }
2837
2838 /* Circular buffer of transmit frame descriptors (TFDs),
2839 * shared with device */
2840 txq->tfds = dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr,
2841 GFP_KERNEL);
2842 if (!txq->tfds) {
2843 IL_ERR("pci_alloc_consistent(%zd) failed\n", tfd_sz);
2844 goto error;
2845 }
2846 txq->q.id = id;
2847
2848 return 0;
2849
2850 error:
2851 kfree(txq->txb);
2852 txq->txb = NULL;
2853
2854 return -ENOMEM;
2855}
2856
2857/**
2858 * il_tx_queue_init - Allocate and initialize one tx/cmd queue
2859 */
2860int il_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq,
2861 int slots_num, u32 txq_id)
2862{
2863 int i, len;
2864 int ret;
2865 int actual_slots = slots_num;
2866
2867 /*
2868 * Alloc buffer array for commands (Tx or other types of commands).
2869 * For the command queue (#4/#9), allocate command space + one big
2870 * command for scan, since scan command is very huge; the system will
2871 * not have two scans at the same time, so only one is needed.
2872 * For normal Tx queues (all other queues), no super-size command
2873 * space is needed.
2874 */
2875 if (txq_id == il->cmd_queue)
2876 actual_slots++;
2877
2878 txq->meta = kzalloc(sizeof(struct il_cmd_meta) * actual_slots,
2879 GFP_KERNEL);
2880 txq->cmd = kzalloc(sizeof(struct il_device_cmd *) * actual_slots,
2881 GFP_KERNEL);
2882
2883 if (!txq->meta || !txq->cmd)
2884 goto out_free_arrays;
2885
2886 len = sizeof(struct il_device_cmd);
2887 for (i = 0; i < actual_slots; i++) {
2888 /* only happens for cmd queue */
2889 if (i == slots_num)
2890 len = IL_MAX_CMD_SIZE;
2891
2892 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
2893 if (!txq->cmd[i])
2894 goto err;
2895 }
2896
2897 /* Alloc driver data array and TFD circular buffer */
2898 ret = il_tx_queue_alloc(il, txq, txq_id);
2899 if (ret)
2900 goto err;
2901
2902 txq->need_update = 0;
2903
2904 /*
2905 * For the default queues 0-3, set up the swq_id
2906 * already -- all others need to get one later
2907 * (if they need one at all).
2908 */
2909 if (txq_id < 4)
2910 il_set_swq_id(txq, txq_id, txq_id);
2911
2912 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
2913 * il_queue_inc_wrap and il_queue_dec_wrap are broken. */
2914 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
2915
2916 /* Initialize queue's high/low-water marks, and head/tail idxes */
2917 il_queue_init(il, &txq->q,
2918 TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
2919
2920 /* Tell device where to find queue */
2921 il->cfg->ops->lib->txq_init(il, txq);
2922
2923 return 0;
2924err:
2925 for (i = 0; i < actual_slots; i++)
2926 kfree(txq->cmd[i]);
2927out_free_arrays:
2928 kfree(txq->meta);
2929 kfree(txq->cmd);
2930
2931 return -ENOMEM;
2932}
2933EXPORT_SYMBOL(il_tx_queue_init);
2934
2935void il_tx_queue_reset(struct il_priv *il, struct il_tx_queue *txq,
2936 int slots_num, u32 txq_id)
2937{
2938 int actual_slots = slots_num;
2939
2940 if (txq_id == il->cmd_queue)
2941 actual_slots++;
2942
2943 memset(txq->meta, 0, sizeof(struct il_cmd_meta) * actual_slots);
2944
2945 txq->need_update = 0;
2946
2947 /* Initialize queue's high/low-water marks, and head/tail idxes */
2948 il_queue_init(il, &txq->q,
2949 TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
2950
2951 /* Tell device where to find queue */
2952 il->cfg->ops->lib->txq_init(il, txq);
2953}
2954EXPORT_SYMBOL(il_tx_queue_reset);
2955
2956/*************** HOST COMMAND QUEUE FUNCTIONS *****/
2957
2958/**
2959 * il_enqueue_hcmd - enqueue a uCode command
2960 * @il: device ilate data point
2961 * @cmd: a point to the ucode command structure
2962 *
2963 * The function returns < 0 values to indicate the operation is
2964 * failed. On success, it turns the idx (> 0) of command in the
2965 * command queue.
2966 */
2967int il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
2968{
2969 struct il_tx_queue *txq = &il->txq[il->cmd_queue];
2970 struct il_queue *q = &txq->q;
2971 struct il_device_cmd *out_cmd;
2972 struct il_cmd_meta *out_meta;
2973 dma_addr_t phys_addr;
2974 unsigned long flags;
2975 int len;
2976 u32 idx;
2977 u16 fix_size;
2978
2979 cmd->len = il->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
2980 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
2981
2982 /* If any of the command structures end up being larger than
2983 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
2984 * we will need to increase the size of the TFD entries
2985 * Also, check to see if command buffer should not exceed the size
2986 * of device_cmd and max_cmd_size. */
2987 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
2988 !(cmd->flags & CMD_SIZE_HUGE));
2989 BUG_ON(fix_size > IL_MAX_CMD_SIZE);
2990
2991 if (il_is_rfkill(il) || il_is_ctkill(il)) {
2992 IL_WARN("Not sending command - %s KILL\n",
2993 il_is_rfkill(il) ? "RF" : "CT");
2994 return -EIO;
2995 }
2996
2997 spin_lock_irqsave(&il->hcmd_lock, flags);
2998
2999 if (il_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
3000 spin_unlock_irqrestore(&il->hcmd_lock, flags);
3001
3002 IL_ERR("Restarting adapter due to command queue full\n");
3003 queue_work(il->workqueue, &il->restart);
3004 return -ENOSPC;
3005 }
3006
3007 idx = il_get_cmd_idx(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
3008 out_cmd = txq->cmd[idx];
3009 out_meta = &txq->meta[idx];
3010
3011 if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
3012 spin_unlock_irqrestore(&il->hcmd_lock, flags);
3013 return -ENOSPC;
3014 }
3015
3016 memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
3017 out_meta->flags = cmd->flags | CMD_MAPPED;
3018 if (cmd->flags & CMD_WANT_SKB)
3019 out_meta->source = cmd;
3020 if (cmd->flags & CMD_ASYNC)
3021 out_meta->callback = cmd->callback;
3022
3023 out_cmd->hdr.cmd = cmd->id;
3024 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
3025
3026 /* At this point, the out_cmd now has all of the incoming cmd
3027 * information */
3028
3029 out_cmd->hdr.flags = 0;
3030 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(il->cmd_queue) |
3031 IDX_TO_SEQ(q->write_ptr));
3032 if (cmd->flags & CMD_SIZE_HUGE)
3033 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
3034 len = sizeof(struct il_device_cmd);
3035 if (idx == TFD_CMD_SLOTS)
3036 len = IL_MAX_CMD_SIZE;
3037
3038#ifdef CONFIG_IWLEGACY_DEBUG
3039 switch (out_cmd->hdr.cmd) {
3040 case C_TX_LINK_QUALITY_CMD:
3041 case C_SENSITIVITY:
3042 D_HC_DUMP(
3043 "Sending command %s (#%x), seq: 0x%04X, "
3044 "%d bytes at %d[%d]:%d\n",
3045 il_get_cmd_string(out_cmd->hdr.cmd),
3046 out_cmd->hdr.cmd,
3047 le16_to_cpu(out_cmd->hdr.sequence), fix_size,
3048 q->write_ptr, idx, il->cmd_queue);
3049 break;
3050 default:
3051 D_HC("Sending command %s (#%x), seq: 0x%04X, "
3052 "%d bytes at %d[%d]:%d\n",
3053 il_get_cmd_string(out_cmd->hdr.cmd),
3054 out_cmd->hdr.cmd,
3055 le16_to_cpu(out_cmd->hdr.sequence), fix_size,
3056 q->write_ptr, idx, il->cmd_queue);
3057 }
3058#endif
3059 txq->need_update = 1;
3060
3061 if (il->cfg->ops->lib->txq_update_byte_cnt_tbl)
3062 /* Set up entry in queue's byte count circular buffer */
3063 il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq, 0);
3064
3065 phys_addr = pci_map_single(il->pci_dev, &out_cmd->hdr,
3066 fix_size, PCI_DMA_BIDIRECTIONAL);
3067 dma_unmap_addr_set(out_meta, mapping, phys_addr);
3068 dma_unmap_len_set(out_meta, len, fix_size);
3069
3070 il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq,
3071 phys_addr, fix_size, 1,
3072 U32_PAD(cmd->len));
3073
3074 /* Increment and update queue's write idx */
3075 q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
3076 il_txq_update_write_ptr(il, txq);
3077
3078 spin_unlock_irqrestore(&il->hcmd_lock, flags);
3079 return idx;
3080}
3081
3082/**
3083 * il_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
3084 *
3085 * When FW advances 'R' idx, all entries between old and new 'R' idx
3086 * need to be reclaimed. As result, some free space forms. If there is
3087 * enough free space (> low mark), wake the stack that feeds us.
3088 */
3089static void il_hcmd_queue_reclaim(struct il_priv *il, int txq_id,
3090 int idx, int cmd_idx)
3091{
3092 struct il_tx_queue *txq = &il->txq[txq_id];
3093 struct il_queue *q = &txq->q;
3094 int nfreed = 0;
3095
3096 if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
3097 IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
3098 "is out of range [0-%d] %d %d.\n", txq_id,
3099 idx, q->n_bd, q->write_ptr, q->read_ptr);
3100 return;
3101 }
3102
3103 for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
3104 q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3105
3106 if (nfreed++ > 0) {
3107 IL_ERR("HCMD skipped: idx (%d) %d %d\n", idx,
3108 q->write_ptr, q->read_ptr);
3109 queue_work(il->workqueue, &il->restart);
3110 }
3111
3112 }
3113}
3114
3115/**
3116 * il_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
3117 * @rxb: Rx buffer to reclaim
3118 *
3119 * If an Rx buffer has an async callback associated with it the callback
3120 * will be executed. The attached skb (if present) will only be freed
3121 * if the callback returns 1
3122 */
3123void
3124il_tx_cmd_complete(struct il_priv *il, struct il_rx_buf *rxb)
3125{
3126 struct il_rx_pkt *pkt = rxb_addr(rxb);
3127 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3128 int txq_id = SEQ_TO_QUEUE(sequence);
3129 int idx = SEQ_TO_IDX(sequence);
3130 int cmd_idx;
3131 bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
3132 struct il_device_cmd *cmd;
3133 struct il_cmd_meta *meta;
3134 struct il_tx_queue *txq = &il->txq[il->cmd_queue];
3135 unsigned long flags;
3136
3137 /* If a Tx command is being handled and it isn't in the actual
3138 * command queue then there a command routing bug has been introduced
3139 * in the queue management code. */
3140 if (WARN(txq_id != il->cmd_queue,
3141 "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
3142 txq_id, il->cmd_queue, sequence,
3143 il->txq[il->cmd_queue].q.read_ptr,
3144 il->txq[il->cmd_queue].q.write_ptr)) {
3145 il_print_hex_error(il, pkt, 32);
3146 return;
3147 }
3148
3149 cmd_idx = il_get_cmd_idx(&txq->q, idx, huge);
3150 cmd = txq->cmd[cmd_idx];
3151 meta = &txq->meta[cmd_idx];
3152
3153 txq->time_stamp = jiffies;
3154
3155 pci_unmap_single(il->pci_dev,
3156 dma_unmap_addr(meta, mapping),
3157 dma_unmap_len(meta, len),
3158 PCI_DMA_BIDIRECTIONAL);
3159
3160 /* Input error checking is done when commands are added to queue. */
3161 if (meta->flags & CMD_WANT_SKB) {
3162 meta->source->reply_page = (unsigned long)rxb_addr(rxb);
3163 rxb->page = NULL;
3164 } else if (meta->callback)
3165 meta->callback(il, cmd, pkt);
3166
3167 spin_lock_irqsave(&il->hcmd_lock, flags);
3168
3169 il_hcmd_queue_reclaim(il, txq_id, idx, cmd_idx);
3170
3171 if (!(meta->flags & CMD_ASYNC)) {
3172 clear_bit(S_HCMD_ACTIVE, &il->status);
3173 D_INFO("Clearing HCMD_ACTIVE for command %s\n",
3174 il_get_cmd_string(cmd->hdr.cmd));
3175 wake_up(&il->wait_command_queue);
3176 }
3177
3178 /* Mark as unmapped */
3179 meta->flags = 0;
3180
3181 spin_unlock_irqrestore(&il->hcmd_lock, flags);
3182}
3183EXPORT_SYMBOL(il_tx_cmd_complete);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003184
3185MODULE_DESCRIPTION("iwl-legacy: common functions for 3945 and 4965");
3186MODULE_VERSION(IWLWIFI_VERSION);
3187MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
3188MODULE_LICENSE("GPL");
3189
3190/*
3191 * set bt_coex_active to true, uCode will do kill/defer
3192 * every time the priority line is asserted (BT is sending signals on the
3193 * priority line in the PCIx).
3194 * set bt_coex_active to false, uCode will ignore the BT activity and
3195 * perform the normal operation
3196 *
3197 * User might experience transmit issue on some platform due to WiFi/BT
3198 * co-exist problem. The possible behaviors are:
3199 * Able to scan and finding all the available AP
3200 * Not able to associate with any AP
3201 * On those platforms, WiFi communication can be restored by set
3202 * "bt_coex_active" module parameter to "false"
3203 *
3204 * default: bt_coex_active = true (BT_COEX_ENABLE)
3205 */
John W. Linvilleef334172011-02-25 15:51:01 -05003206static bool bt_coex_active = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003207module_param(bt_coex_active, bool, S_IRUGO);
3208MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
3209
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02003210u32 il_debug_level;
3211EXPORT_SYMBOL(il_debug_level);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003212
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02003213const u8 il_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3214EXPORT_SYMBOL(il_bcast_addr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003215
3216
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003217/* This function both allocates and initializes hw and il. */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003218struct ieee80211_hw *il_alloc_all(struct il_cfg *cfg)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003219{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003220 struct il_priv *il;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003221 /* mac80211 allocates memory for this device instance, including
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003222 * space for this driver's ilate structure */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003223 struct ieee80211_hw *hw;
3224
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003225 hw = ieee80211_alloc_hw(sizeof(struct il_priv),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003226 cfg->ops->ieee80211_ops);
3227 if (hw == NULL) {
3228 pr_err("%s: Can not allocate network device\n",
3229 cfg->name);
3230 goto out;
3231 }
3232
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003233 il = hw->priv;
3234 il->hw = hw;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003235
3236out:
3237 return hw;
3238}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003239EXPORT_SYMBOL(il_alloc_all);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003240
3241#define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
3242#define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003243static void il_init_ht_hw_capab(const struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003244 struct ieee80211_sta_ht_cap *ht_info,
3245 enum ieee80211_band band)
3246{
3247 u16 max_bit_rate = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003248 u8 rx_chains_num = il->hw_params.rx_chains_num;
3249 u8 tx_chains_num = il->hw_params.tx_chains_num;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003250
3251 ht_info->cap = 0;
3252 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
3253
3254 ht_info->ht_supported = true;
3255
3256 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
3257 max_bit_rate = MAX_BIT_RATE_20_MHZ;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003258 if (il->hw_params.ht40_channel & BIT(band)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003259 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3260 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
3261 ht_info->mcs.rx_mask[4] = 0x01;
3262 max_bit_rate = MAX_BIT_RATE_40_MHZ;
3263 }
3264
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003265 if (il->cfg->mod_params->amsdu_size_8K)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003266 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3267
3268 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
3269 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
3270
3271 ht_info->mcs.rx_mask[0] = 0xFF;
3272 if (rx_chains_num >= 2)
3273 ht_info->mcs.rx_mask[1] = 0xFF;
3274 if (rx_chains_num >= 3)
3275 ht_info->mcs.rx_mask[2] = 0xFF;
3276
3277 /* Highest supported Rx data rate */
3278 max_bit_rate *= rx_chains_num;
3279 WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
3280 ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
3281
3282 /* Tx MCS capabilities */
3283 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
3284 if (tx_chains_num != rx_chains_num) {
3285 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
3286 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
3287 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
3288 }
3289}
3290
3291/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003292 * il_init_geos - Initialize mac80211's geo/channel info based from eeprom
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003293 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003294int il_init_geos(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003295{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003296 struct il_channel_info *ch;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003297 struct ieee80211_supported_band *sband;
3298 struct ieee80211_channel *channels;
3299 struct ieee80211_channel *geo_ch;
3300 struct ieee80211_rate *rates;
3301 int i = 0;
Stanislaw Gruszka332704a2011-04-13 10:56:51 +02003302 s8 max_tx_power = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003303
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003304 if (il->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
3305 il->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003306 D_INFO("Geography modes already initialized.\n");
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003307 set_bit(S_GEO_CONFIGURED, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003308 return 0;
3309 }
3310
3311 channels = kzalloc(sizeof(struct ieee80211_channel) *
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003312 il->channel_count, GFP_KERNEL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003313 if (!channels)
3314 return -ENOMEM;
3315
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02003316 rates = kzalloc((sizeof(struct ieee80211_rate) * RATE_COUNT_LEGACY),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003317 GFP_KERNEL);
3318 if (!rates) {
3319 kfree(channels);
3320 return -ENOMEM;
3321 }
3322
3323 /* 5.2GHz channels start after the 2.4GHz channels */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003324 sband = &il->bands[IEEE80211_BAND_5GHZ];
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02003325 sband->channels = &channels[ARRAY_SIZE(il_eeprom_band_1)];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003326 /* just OFDM */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003327 sband->bitrates = &rates[IL_FIRST_OFDM_RATE];
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02003328 sband->n_bitrates = RATE_COUNT_LEGACY - IL_FIRST_OFDM_RATE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003329
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003330 if (il->cfg->sku & IL_SKU_N)
3331 il_init_ht_hw_capab(il, &sband->ht_cap,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003332 IEEE80211_BAND_5GHZ);
3333
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003334 sband = &il->bands[IEEE80211_BAND_2GHZ];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003335 sband->channels = channels;
3336 /* OFDM & CCK */
3337 sband->bitrates = rates;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02003338 sband->n_bitrates = RATE_COUNT_LEGACY;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003339
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003340 if (il->cfg->sku & IL_SKU_N)
3341 il_init_ht_hw_capab(il, &sband->ht_cap,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003342 IEEE80211_BAND_2GHZ);
3343
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003344 il->ieee_channels = channels;
3345 il->ieee_rates = rates;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003346
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003347 for (i = 0; i < il->channel_count; i++) {
3348 ch = &il->channel_info[i];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003349
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003350 if (!il_is_channel_valid(ch))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003351 continue;
3352
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003353 sband = &il->bands[ch->band];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003354
3355 geo_ch = &sband->channels[sband->n_channels++];
3356
3357 geo_ch->center_freq =
3358 ieee80211_channel_to_frequency(ch->channel, ch->band);
3359 geo_ch->max_power = ch->max_power_avg;
3360 geo_ch->max_antenna_gain = 0xff;
3361 geo_ch->hw_value = ch->channel;
3362
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003363 if (il_is_channel_valid(ch)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003364 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
3365 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
3366
3367 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
3368 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
3369
3370 if (ch->flags & EEPROM_CHANNEL_RADAR)
3371 geo_ch->flags |= IEEE80211_CHAN_RADAR;
3372
3373 geo_ch->flags |= ch->ht40_extension_channel;
3374
Stanislaw Gruszka332704a2011-04-13 10:56:51 +02003375 if (ch->max_power_avg > max_tx_power)
3376 max_tx_power = ch->max_power_avg;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003377 } else {
3378 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
3379 }
3380
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003381 D_INFO("Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003382 ch->channel, geo_ch->center_freq,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003383 il_is_channel_a_band(ch) ? "5.2" : "2.4",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003384 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
3385 "restricted" : "valid",
3386 geo_ch->flags);
3387 }
3388
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003389 il->tx_power_device_lmt = max_tx_power;
3390 il->tx_power_user_lmt = max_tx_power;
3391 il->tx_power_next = max_tx_power;
Stanislaw Gruszka332704a2011-04-13 10:56:51 +02003392
Stanislaw Gruszka232913b2011-08-26 10:45:16 +02003393 if (il->bands[IEEE80211_BAND_5GHZ].n_channels == 0 &&
3394 (il->cfg->sku & IL_SKU_A)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003395 IL_INFO("Incorrectly detected BG card as ABG. "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003396 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003397 il->pci_dev->device,
3398 il->pci_dev->subsystem_device);
3399 il->cfg->sku &= ~IL_SKU_A;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003400 }
3401
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003402 IL_INFO("Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003403 il->bands[IEEE80211_BAND_2GHZ].n_channels,
3404 il->bands[IEEE80211_BAND_5GHZ].n_channels);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003405
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003406 set_bit(S_GEO_CONFIGURED, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003407
3408 return 0;
3409}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003410EXPORT_SYMBOL(il_init_geos);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003411
3412/*
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003413 * il_free_geos - undo allocations in il_init_geos
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003414 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003415void il_free_geos(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003416{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003417 kfree(il->ieee_channels);
3418 kfree(il->ieee_rates);
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003419 clear_bit(S_GEO_CONFIGURED, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003420}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003421EXPORT_SYMBOL(il_free_geos);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003422
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003423static bool il_is_channel_extension(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003424 enum ieee80211_band band,
3425 u16 channel, u8 extension_chan_offset)
3426{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003427 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003428
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003429 ch_info = il_get_channel_info(il, band, channel);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003430 if (!il_is_channel_valid(ch_info))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003431 return false;
3432
3433 if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
3434 return !(ch_info->ht40_extension_channel &
3435 IEEE80211_CHAN_NO_HT40PLUS);
3436 else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
3437 return !(ch_info->ht40_extension_channel &
3438 IEEE80211_CHAN_NO_HT40MINUS);
3439
3440 return false;
3441}
3442
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003443bool il_is_ht40_tx_allowed(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003444 struct il_rxon_context *ctx,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003445 struct ieee80211_sta_ht_cap *ht_cap)
3446{
3447 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
3448 return false;
3449
3450 /*
3451 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
3452 * the bit will not set if it is pure 40MHz case
3453 */
3454 if (ht_cap && !ht_cap->ht_supported)
3455 return false;
3456
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01003457#ifdef CONFIG_IWLEGACY_DEBUGFS
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003458 if (il->disable_ht40)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003459 return false;
3460#endif
3461
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003462 return il_is_channel_extension(il, il->band,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003463 le16_to_cpu(ctx->staging.channel),
3464 ctx->ht.extension_chan_offset);
3465}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003466EXPORT_SYMBOL(il_is_ht40_tx_allowed);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003467
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003468static u16 il_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003469{
3470 u16 new_val;
3471 u16 beacon_factor;
3472
3473 /*
3474 * If mac80211 hasn't given us a beacon interval, program
3475 * the default into the device.
3476 */
3477 if (!beacon_val)
3478 return DEFAULT_BEACON_INTERVAL;
3479
3480 /*
3481 * If the beacon interval we obtained from the peer
3482 * is too large, we'll have to wake up more often
3483 * (and in IBSS case, we'll beacon too much)
3484 *
3485 * For example, if max_beacon_val is 4096, and the
3486 * requested beacon interval is 7000, we'll have to
3487 * use 3500 to be able to wake up on the beacons.
3488 *
3489 * This could badly influence beacon detection stats.
3490 */
3491
3492 beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
3493 new_val = beacon_val / beacon_factor;
3494
3495 if (!new_val)
3496 new_val = max_beacon_val;
3497
3498 return new_val;
3499}
3500
3501int
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003502il_send_rxon_timing(struct il_priv *il, struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003503{
3504 u64 tsf;
3505 s32 interval_tm, rem;
3506 struct ieee80211_conf *conf = NULL;
3507 u16 beacon_int;
3508 struct ieee80211_vif *vif = ctx->vif;
3509
Stanislaw Gruszka6278dda2011-08-31 11:13:05 +02003510 conf = &il->hw->conf;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003511
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003512 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003513
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003514 memset(&ctx->timing, 0, sizeof(struct il_rxon_time_cmd));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003515
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003516 ctx->timing.timestamp = cpu_to_le64(il->timestamp);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003517 ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
3518
3519 beacon_int = vif ? vif->bss_conf.beacon_int : 0;
3520
3521 /*
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +02003522 * TODO: For IBSS we need to get atim_win from mac80211,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003523 * for now just always use 0
3524 */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +02003525 ctx->timing.atim_win = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003526
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003527 beacon_int = il_adjust_beacon_interval(beacon_int,
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003528 il->hw_params.max_beacon_itrvl * TIME_UNIT);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003529 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
3530
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003531 tsf = il->timestamp; /* tsf is modifed by do_div: copy it */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003532 interval_tm = beacon_int * TIME_UNIT;
3533 rem = do_div(tsf, interval_tm);
3534 ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
3535
3536 ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
3537
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003538 D_ASSOC(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003539 "beacon interval %d beacon timer %d beacon tim %d\n",
3540 le16_to_cpu(ctx->timing.beacon_interval),
3541 le32_to_cpu(ctx->timing.beacon_init_val),
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +02003542 le16_to_cpu(ctx->timing.atim_win));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003543
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003544 return il_send_cmd_pdu(il, ctx->rxon_timing_cmd,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003545 sizeof(ctx->timing), &ctx->timing);
3546}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003547EXPORT_SYMBOL(il_send_rxon_timing);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003548
3549void
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003550il_set_rxon_hwcrypto(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003551 struct il_rxon_context *ctx,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003552 int hw_decrypt)
3553{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003554 struct il_rxon_cmd *rxon = &ctx->staging;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003555
3556 if (hw_decrypt)
3557 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
3558 else
3559 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
3560
3561}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003562EXPORT_SYMBOL(il_set_rxon_hwcrypto);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003563
3564/* validate RXON structure is valid */
3565int
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003566il_check_rxon_cmd(struct il_priv *il, struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003567{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003568 struct il_rxon_cmd *rxon = &ctx->staging;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003569 bool error = false;
3570
3571 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
3572 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003573 IL_WARN("check 2.4G: wrong narrow\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003574 error = true;
3575 }
3576 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003577 IL_WARN("check 2.4G: wrong radar\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003578 error = true;
3579 }
3580 } else {
3581 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003582 IL_WARN("check 5.2G: not short slot!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003583 error = true;
3584 }
3585 if (rxon->flags & RXON_FLG_CCK_MSK) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003586 IL_WARN("check 5.2G: CCK!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003587 error = true;
3588 }
3589 }
3590 if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003591 IL_WARN("mac/bssid mcast!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003592 error = true;
3593 }
3594
3595 /* make sure basic rates 6Mbps and 1Mbps are supported */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02003596 if ((rxon->ofdm_basic_rates & RATE_6M_MASK) == 0 &&
3597 (rxon->cck_basic_rates & RATE_1M_MASK) == 0) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003598 IL_WARN("neither 1 nor 6 are basic\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003599 error = true;
3600 }
3601
3602 if (le16_to_cpu(rxon->assoc_id) > 2007) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003603 IL_WARN("aid > 2007\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003604 error = true;
3605 }
3606
3607 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
3608 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003609 IL_WARN("CCK and short slot\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003610 error = true;
3611 }
3612
3613 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
3614 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003615 IL_WARN("CCK and auto detect");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003616 error = true;
3617 }
3618
3619 if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
3620 RXON_FLG_TGG_PROTECT_MSK)) ==
3621 RXON_FLG_TGG_PROTECT_MSK) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003622 IL_WARN("TGg but no auto-detect\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003623 error = true;
3624 }
3625
3626 if (error)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003627 IL_WARN("Tuning to channel %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003628 le16_to_cpu(rxon->channel));
3629
3630 if (error) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003631 IL_ERR("Invalid RXON\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003632 return -EINVAL;
3633 }
3634 return 0;
3635}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003636EXPORT_SYMBOL(il_check_rxon_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003637
3638/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003639 * il_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003640 * @il: staging_rxon is compared to active_rxon
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003641 *
3642 * If the RXON structure is changing enough to require a new tune,
3643 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
3644 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
3645 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003646int il_full_rxon_required(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003647 struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003648{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003649 const struct il_rxon_cmd *staging = &ctx->staging;
3650 const struct il_rxon_cmd *active = &ctx->active;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003651
3652#define CHK(cond) \
3653 if ((cond)) { \
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003654 D_INFO("need full RXON - " #cond "\n"); \
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003655 return 1; \
3656 }
3657
3658#define CHK_NEQ(c1, c2) \
3659 if ((c1) != (c2)) { \
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003660 D_INFO("need full RXON - " \
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003661 #c1 " != " #c2 " - %d != %d\n", \
3662 (c1), (c2)); \
3663 return 1; \
3664 }
3665
3666 /* These items are only settable from the full RXON command */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003667 CHK(!il_is_associated_ctx(ctx));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003668 CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
3669 CHK(compare_ether_addr(staging->node_addr, active->node_addr));
3670 CHK(compare_ether_addr(staging->wlap_bssid_addr,
3671 active->wlap_bssid_addr));
3672 CHK_NEQ(staging->dev_type, active->dev_type);
3673 CHK_NEQ(staging->channel, active->channel);
3674 CHK_NEQ(staging->air_propagation, active->air_propagation);
3675 CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
3676 active->ofdm_ht_single_stream_basic_rates);
3677 CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
3678 active->ofdm_ht_dual_stream_basic_rates);
3679 CHK_NEQ(staging->assoc_id, active->assoc_id);
3680
3681 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
3682 * be updated with the RXON_ASSOC command -- however only some
3683 * flag transitions are allowed using RXON_ASSOC */
3684
3685 /* Check if we are not switching bands */
3686 CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
3687 active->flags & RXON_FLG_BAND_24G_MSK);
3688
3689 /* Check if we are switching association toggle */
3690 CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
3691 active->filter_flags & RXON_FILTER_ASSOC_MSK);
3692
3693#undef CHK
3694#undef CHK_NEQ
3695
3696 return 0;
3697}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003698EXPORT_SYMBOL(il_full_rxon_required);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003699
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003700u8 il_get_lowest_plcp(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003701 struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003702{
3703 /*
3704 * Assign the lowest rate -- should really get this from
3705 * the beacon skb from mac80211.
3706 */
3707 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02003708 return RATE_1M_PLCP;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003709 else
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02003710 return RATE_6M_PLCP;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003711}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003712EXPORT_SYMBOL(il_get_lowest_plcp);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003713
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003714static void _il_set_rxon_ht(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003715 struct il_ht_config *ht_conf,
3716 struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003717{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003718 struct il_rxon_cmd *rxon = &ctx->staging;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003719
3720 if (!ctx->ht.enabled) {
3721 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
3722 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
3723 RXON_FLG_HT40_PROT_MSK |
3724 RXON_FLG_HT_PROT_MSK);
3725 return;
3726 }
3727
3728 rxon->flags |= cpu_to_le32(ctx->ht.protection <<
3729 RXON_FLG_HT_OPERATING_MODE_POS);
3730
3731 /* Set up channel bandwidth:
3732 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
3733 /* clear the HT channel mode before set the mode */
3734 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
3735 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003736 if (il_is_ht40_tx_allowed(il, ctx, NULL)) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003737 /* pure ht40 */
3738 if (ctx->ht.protection ==
3739 IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
3740 rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
3741 /* Note: control channel is opposite of extension channel */
3742 switch (ctx->ht.extension_chan_offset) {
3743 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3744 rxon->flags &=
3745 ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
3746 break;
3747 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3748 rxon->flags |=
3749 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
3750 break;
3751 }
3752 } else {
3753 /* Note: control channel is opposite of extension channel */
3754 switch (ctx->ht.extension_chan_offset) {
3755 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
3756 rxon->flags &=
3757 ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
3758 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
3759 break;
3760 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
3761 rxon->flags |=
3762 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
3763 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
3764 break;
3765 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
3766 default:
3767 /* channel location only valid if in Mixed mode */
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003768 IL_ERR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003769 "invalid extension channel offset\n");
3770 break;
3771 }
3772 }
3773 } else {
3774 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
3775 }
3776
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003777 if (il->cfg->ops->hcmd->set_rxon_chain)
3778 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003779
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003780 D_ASSOC("rxon flags 0x%X operation mode :0x%X "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003781 "extension channel offset 0x%x\n",
3782 le32_to_cpu(rxon->flags), ctx->ht.protection,
3783 ctx->ht.extension_chan_offset);
3784}
3785
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003786void il_set_rxon_ht(struct il_priv *il, struct il_ht_config *ht_conf)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003787{
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003788 _il_set_rxon_ht(il, ht_conf, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003789}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003790EXPORT_SYMBOL(il_set_rxon_ht);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003791
3792/* Return valid, unused, channel for a passive scan to reset the RF */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003793u8 il_get_single_channel_number(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003794 enum ieee80211_band band)
3795{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003796 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003797 int i;
3798 u8 channel = 0;
3799 u8 min, max;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003800
3801 if (band == IEEE80211_BAND_5GHZ) {
3802 min = 14;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003803 max = il->channel_count;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003804 } else {
3805 min = 0;
3806 max = 14;
3807 }
3808
3809 for (i = min; i < max; i++) {
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003810 channel = il->channel_info[i].channel;
3811 if (channel == le16_to_cpu(il->ctx.staging.channel))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003812 continue;
3813
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003814 ch_info = il_get_channel_info(il, band, channel);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003815 if (il_is_channel_valid(ch_info))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003816 break;
3817 }
3818
3819 return channel;
3820}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003821EXPORT_SYMBOL(il_get_single_channel_number);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003822
3823/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003824 * il_set_rxon_channel - Set the band and channel values in staging RXON
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003825 * @ch: requested channel as a pointer to struct ieee80211_channel
3826
3827 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
3828 * in the staging RXON flag structure based on the ch->band
3829 */
3830int
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003831il_set_rxon_channel(struct il_priv *il, struct ieee80211_channel *ch,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003832 struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003833{
3834 enum ieee80211_band band = ch->band;
3835 u16 channel = ch->hw_value;
3836
Stanislaw Gruszka232913b2011-08-26 10:45:16 +02003837 if (le16_to_cpu(ctx->staging.channel) == channel && il->band == band)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003838 return 0;
3839
3840 ctx->staging.channel = cpu_to_le16(channel);
3841 if (band == IEEE80211_BAND_5GHZ)
3842 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
3843 else
3844 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
3845
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003846 il->band = band;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003847
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003848 D_INFO("Staging channel set to %d [%d]\n", channel, band);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003849
3850 return 0;
3851}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003852EXPORT_SYMBOL(il_set_rxon_channel);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003853
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003854void il_set_flags_for_band(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003855 struct il_rxon_context *ctx,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003856 enum ieee80211_band band,
3857 struct ieee80211_vif *vif)
3858{
3859 if (band == IEEE80211_BAND_5GHZ) {
3860 ctx->staging.flags &=
3861 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
3862 | RXON_FLG_CCK_MSK);
3863 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
3864 } else {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003865 /* Copied from il_post_associate() */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003866 if (vif && vif->bss_conf.use_short_slot)
3867 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
3868 else
3869 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3870
3871 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
3872 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
3873 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
3874 }
3875}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003876EXPORT_SYMBOL(il_set_flags_for_band);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003877
3878/*
3879 * initialize rxon structure with default values from eeprom
3880 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003881void il_connection_init_rx_config(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003882 struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003883{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003884 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003885
3886 memset(&ctx->staging, 0, sizeof(ctx->staging));
3887
3888 if (!ctx->vif) {
3889 ctx->staging.dev_type = ctx->unused_devtype;
3890 } else
3891 switch (ctx->vif->type) {
3892
3893 case NL80211_IFTYPE_STATION:
3894 ctx->staging.dev_type = ctx->station_devtype;
3895 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
3896 break;
3897
3898 case NL80211_IFTYPE_ADHOC:
3899 ctx->staging.dev_type = ctx->ibss_devtype;
3900 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
3901 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
3902 RXON_FILTER_ACCEPT_GRP_MSK;
3903 break;
3904
3905 default:
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003906 IL_ERR("Unsupported interface type %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003907 ctx->vif->type);
3908 break;
3909 }
3910
3911#if 0
3912 /* TODO: Figure out when short_preamble would be set and cache from
3913 * that */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003914 if (!hw_to_local(il->hw)->short_preamble)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003915 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3916 else
3917 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3918#endif
3919
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003920 ch_info = il_get_channel_info(il, il->band,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003921 le16_to_cpu(ctx->active.channel));
3922
3923 if (!ch_info)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003924 ch_info = &il->channel_info[0];
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003925
3926 ctx->staging.channel = cpu_to_le16(ch_info->channel);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003927 il->band = ch_info->band;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003928
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003929 il_set_flags_for_band(il, ctx, il->band, ctx->vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003930
3931 ctx->staging.ofdm_basic_rates =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003932 (IL_OFDM_RATES_MASK >> IL_FIRST_OFDM_RATE) & 0xFF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003933 ctx->staging.cck_basic_rates =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003934 (IL_CCK_RATES_MASK >> IL_FIRST_CCK_RATE) & 0xF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003935
3936 /* clear both MIX and PURE40 mode flag */
3937 ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
3938 RXON_FLG_CHANNEL_MODE_PURE_40);
3939 if (ctx->vif)
3940 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
3941
3942 ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
3943 ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
3944}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003945EXPORT_SYMBOL(il_connection_init_rx_config);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003946
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003947void il_set_rate(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003948{
3949 const struct ieee80211_supported_band *hw = NULL;
3950 struct ieee80211_rate *rate;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003951 int i;
3952
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003953 hw = il_get_hw_mode(il, il->band);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003954 if (!hw) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02003955 IL_ERR("Failed to set rate: unable to get hw mode\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003956 return;
3957 }
3958
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003959 il->active_rate = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003960
3961 for (i = 0; i < hw->n_bitrates; i++) {
3962 rate = &(hw->bitrates[i]);
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +02003963 if (rate->hw_value < RATE_COUNT_LEGACY)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003964 il->active_rate |= (1 << rate->hw_value);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003965 }
3966
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01003967 D_RATE("Set active_rate = %0x\n", il->active_rate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003968
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003969 il->ctx.staging.cck_basic_rates =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003970 (IL_CCK_BASIC_RATES_MASK >> IL_FIRST_CCK_RATE) & 0xF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003971
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02003972 il->ctx.staging.ofdm_basic_rates =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003973 (IL_OFDM_BASIC_RATES_MASK >> IL_FIRST_OFDM_RATE) & 0xFF;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003974}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003975EXPORT_SYMBOL(il_set_rate);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003976
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02003977void il_chswitch_done(struct il_priv *il, bool is_success)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003978{
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01003979 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003980
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003981 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003982 return;
3983
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003984 if (test_and_clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003985 ieee80211_chswitch_done(ctx->vif, is_success);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003986}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003987EXPORT_SYMBOL(il_chswitch_done);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003988
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01003989void il_hdl_csa(struct il_priv *il, struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003990{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02003991 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003992 struct il_csa_notification *csa = &(pkt->u.csa_notif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003993
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01003994 struct il_rxon_context *ctx = &il->ctx;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02003995 struct il_rxon_cmd *rxon = (void *)&ctx->active;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003996
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01003997 if (!test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
Stanislaw Gruszka51e65252011-06-08 15:26:31 +02003998 return;
3999
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004000 if (!le32_to_cpu(csa->status) && csa->channel == il->switch_channel) {
Stanislaw Gruszka51e65252011-06-08 15:26:31 +02004001 rxon->channel = csa->channel;
4002 ctx->staging.channel = csa->channel;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004003 D_11H("CSA notif: channel %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004004 le16_to_cpu(csa->channel));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004005 il_chswitch_done(il, true);
Stanislaw Gruszka51e65252011-06-08 15:26:31 +02004006 } else {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004007 IL_ERR("CSA notif (fail) : channel %d\n",
Stanislaw Gruszka51e65252011-06-08 15:26:31 +02004008 le16_to_cpu(csa->channel));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004009 il_chswitch_done(il, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004010 }
4011}
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01004012EXPORT_SYMBOL(il_hdl_csa);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004013
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004014#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004015void il_print_rx_config_cmd(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004016 struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004017{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004018 struct il_rxon_cmd *rxon = &ctx->staging;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004019
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004020 D_RADIO("RX CONFIG:\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004021 il_print_hex_dump(il, IL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004022 D_RADIO("u16 channel: 0x%x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004023 le16_to_cpu(rxon->channel));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004024 D_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4025 D_RADIO("u32 filter_flags: 0x%08x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004026 le32_to_cpu(rxon->filter_flags));
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004027 D_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4028 D_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004029 rxon->ofdm_basic_rates);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004030 D_RADIO("u8 cck_basic_rates: 0x%02x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004031 rxon->cck_basic_rates);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004032 D_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
4033 D_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
4034 D_RADIO("u16 assoc_id: 0x%x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004035 le16_to_cpu(rxon->assoc_id));
4036}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004037EXPORT_SYMBOL(il_print_rx_config_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004038#endif
4039/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004040 * il_irq_handle_error - called for HW or SW error interrupt from card
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004041 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004042void il_irq_handle_error(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004043{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004044 /* Set the FW error flag -- cleared on il_down */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004045 set_bit(S_FW_ERROR, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004046
4047 /* Cancel currently queued command. */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004048 clear_bit(S_HCMD_ACTIVE, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004049
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004050 IL_ERR("Loaded firmware version: %s\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004051 il->hw->wiphy->fw_version);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004052
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004053 il->cfg->ops->lib->dump_nic_error_log(il);
4054 if (il->cfg->ops->lib->dump_fh)
4055 il->cfg->ops->lib->dump_fh(il, NULL, false);
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004056#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004057 if (il_get_debug_level(il) & IL_DL_FW_ERRORS)
4058 il_print_rx_config_cmd(il,
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01004059 &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004060#endif
4061
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004062 wake_up(&il->wait_command_queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004063
4064 /* Keep the restart process from trying to send host
4065 * commands by clearing the INIT status bit */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004066 clear_bit(S_READY, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004067
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004068 if (!test_bit(S_EXIT_PENDING, &il->status)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004069 IL_DBG(IL_DL_FW_ERRORS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004070 "Restarting adapter due to uCode error.\n");
4071
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004072 if (il->cfg->mod_params->restart_fw)
4073 queue_work(il->workqueue, &il->restart);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004074 }
4075}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004076EXPORT_SYMBOL(il_irq_handle_error);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004077
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004078static int il_apm_stop_master(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004079{
4080 int ret = 0;
4081
4082 /* stop device's busmaster DMA activity */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004083 il_set_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004084
Stanislaw Gruszka142b3432011-08-24 15:22:57 +02004085 ret = _il_poll_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004086 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
4087 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004088 IL_WARN("Master Disable Timed Out, 100 usec\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004089
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004090 D_INFO("stop master\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004091
4092 return ret;
4093}
4094
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004095void il_apm_stop(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004096{
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004097 D_INFO("Stop card, put in low power state\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004098
4099 /* Stop device's DMA activity */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004100 il_apm_stop_master(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004101
4102 /* Reset the entire device */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004103 il_set_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004104
4105 udelay(10);
4106
4107 /*
4108 * Clear "initialization complete" bit to move adapter from
4109 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
4110 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004111 il_clear_bit(il, CSR_GP_CNTRL,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004112 CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
4113}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004114EXPORT_SYMBOL(il_apm_stop);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004115
4116
4117/*
4118 * Start up NIC's basic functionality after it has been reset
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004119 * (e.g. after platform boot, or shutdown via il_apm_stop())
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004120 * NOTE: This does not load uCode nor start the embedded processor
4121 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004122int il_apm_init(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004123{
4124 int ret = 0;
4125 u16 lctl;
4126
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004127 D_INFO("Init card's basic functions\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004128
4129 /*
4130 * Use "set_bit" below rather than "write", to preserve any hardware
4131 * bits already set by default after reset.
4132 */
4133
4134 /* Disable L0S exit timer (platform NMI Work/Around) */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004135 il_set_bit(il, CSR_GIO_CHICKEN_BITS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004136 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
4137
4138 /*
4139 * Disable L0s without affecting L1;
4140 * don't wait for ICH L0s (ICH bug W/A)
4141 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004142 il_set_bit(il, CSR_GIO_CHICKEN_BITS,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004143 CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
4144
4145 /* Set FH wait threshold to maximum (HW error during stress W/A) */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004146 il_set_bit(il, CSR_DBG_HPET_MEM_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004147 CSR_DBG_HPET_MEM_REG_VAL);
4148
4149 /*
4150 * Enable HAP INTA (interrupt from management bus) to
4151 * wake device's PCI Express link L1a -> L0s
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004152 * NOTE: This is no-op for 3945 (non-existent bit)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004153 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004154 il_set_bit(il, CSR_HW_IF_CONFIG_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004155 CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
4156
4157 /*
4158 * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
4159 * Check if BIOS (or OS) enabled L1-ASPM on this device.
4160 * If so (likely), disable L0S, so device moves directly L0->L1;
4161 * costs negligible amount of power savings.
4162 * If not (unlikely), enable L0S, so there is at least some
4163 * power savings, even without L1.
4164 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004165 if (il->cfg->base_params->set_l0s) {
4166 lctl = il_pcie_link_ctl(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004167 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
4168 PCI_CFG_LINK_CTRL_VAL_L1_EN) {
4169 /* L1-ASPM enabled; disable(!) L0S */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004170 il_set_bit(il, CSR_GIO_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004171 CSR_GIO_REG_VAL_L0S_ENABLED);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004172 D_POWER("L1 Enabled; Disabling L0S\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004173 } else {
4174 /* L1-ASPM disabled; enable(!) L0S */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004175 il_clear_bit(il, CSR_GIO_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004176 CSR_GIO_REG_VAL_L0S_ENABLED);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004177 D_POWER("L1 Disabled; Enabling L0S\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004178 }
4179 }
4180
4181 /* Configure analog phase-lock-loop before activating to D0A */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004182 if (il->cfg->base_params->pll_cfg_val)
4183 il_set_bit(il, CSR_ANA_PLL_CFG,
4184 il->cfg->base_params->pll_cfg_val);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004185
4186 /*
4187 * Set "initialization complete" bit to move adapter from
4188 * D0U* --> D0A* (powered-up active) state.
4189 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004190 il_set_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004191
4192 /*
4193 * Wait for clock stabilization; once stabilized, access to
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02004194 * device-internal resources is supported, e.g. il_wr_prph()
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004195 * and accesses to uCode SRAM.
4196 */
Stanislaw Gruszka142b3432011-08-24 15:22:57 +02004197 ret = _il_poll_bit(il, CSR_GP_CNTRL,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004198 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
4199 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
4200 if (ret < 0) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004201 D_INFO("Failed to init the card\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004202 goto out;
4203 }
4204
4205 /*
4206 * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
4207 * BSM (Boostrap State Machine) is only in 3945 and 4965.
4208 *
4209 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
4210 * do not disable clocks. This preserves any hardware bits already
4211 * set by default in "CLK_CTRL_REG" after reset.
4212 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004213 if (il->cfg->base_params->use_bsm)
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02004214 il_wr_prph(il, APMG_CLK_EN_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004215 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
4216 else
Stanislaw Gruszkadb54eb52011-08-24 21:06:33 +02004217 il_wr_prph(il, APMG_CLK_EN_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004218 APMG_CLK_VAL_DMA_CLK_RQT);
4219 udelay(20);
4220
4221 /* Disable L1-Active */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004222 il_set_bits_prph(il, APMG_PCIDEV_STT_REG,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004223 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
4224
4225out:
4226 return ret;
4227}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004228EXPORT_SYMBOL(il_apm_init);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004229
4230
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004231int il_set_tx_power(struct il_priv *il, s8 tx_power, bool force)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004232{
4233 int ret;
4234 s8 prev_tx_power;
Stanislaw Gruszka43f12d42011-02-24 14:23:55 +01004235 bool defer;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01004236 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004237
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004238 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004239
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004240 if (il->tx_power_user_lmt == tx_power && !force)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004241 return 0;
4242
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004243 if (!il->cfg->ops->lib->send_tx_power)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004244 return -EOPNOTSUPP;
4245
Stanislaw Gruszka332704a2011-04-13 10:56:51 +02004246 /* 0 dBm mean 1 milliwatt */
4247 if (tx_power < 0) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004248 IL_WARN(
Stanislaw Gruszka332704a2011-04-13 10:56:51 +02004249 "Requested user TXPOWER %d below 1 mW.\n",
4250 tx_power);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004251 return -EINVAL;
4252 }
4253
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004254 if (tx_power > il->tx_power_device_lmt) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004255 IL_WARN(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004256 "Requested user TXPOWER %d above upper limit %d.\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004257 tx_power, il->tx_power_device_lmt);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004258 return -EINVAL;
4259 }
4260
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004261 if (!il_is_ready_rf(il))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004262 return -EIO;
4263
Stanislaw Gruszka43f12d42011-02-24 14:23:55 +01004264 /* scan complete and commit_rxon use tx_power_next value,
4265 * it always need to be updated for newest request */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004266 il->tx_power_next = tx_power;
Stanislaw Gruszka43f12d42011-02-24 14:23:55 +01004267
4268 /* do not set tx power when scanning or channel changing */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004269 defer = test_bit(S_SCANNING, &il->status) ||
Stanislaw Gruszka43f12d42011-02-24 14:23:55 +01004270 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
4271 if (defer && !force) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004272 D_INFO("Deferring tx power set\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004273 return 0;
4274 }
4275
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004276 prev_tx_power = il->tx_power_user_lmt;
4277 il->tx_power_user_lmt = tx_power;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004278
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004279 ret = il->cfg->ops->lib->send_tx_power(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004280
4281 /* if fail to set tx_power, restore the orig. tx power */
4282 if (ret) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004283 il->tx_power_user_lmt = prev_tx_power;
4284 il->tx_power_next = prev_tx_power;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004285 }
4286 return ret;
4287}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004288EXPORT_SYMBOL(il_set_tx_power);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004289
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004290void il_send_bt_config(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004291{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004292 struct il_bt_cmd bt_cmd = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004293 .lead_time = BT_LEAD_TIME_DEF,
4294 .max_kill = BT_MAX_KILL_DEF,
4295 .kill_ack_mask = 0,
4296 .kill_cts_mask = 0,
4297 };
4298
4299 if (!bt_coex_active)
4300 bt_cmd.flags = BT_COEX_DISABLE;
4301 else
4302 bt_cmd.flags = BT_COEX_ENABLE;
4303
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004304 D_INFO("BT coex %s\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004305 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
4306
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004307 if (il_send_cmd_pdu(il, C_BT_CONFIG,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004308 sizeof(struct il_bt_cmd), &bt_cmd))
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004309 IL_ERR("failed to send BT Coex Config\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004310}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004311EXPORT_SYMBOL(il_send_bt_config);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004312
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02004313int il_send_stats_request(struct il_priv *il, u8 flags, bool clear)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004314{
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02004315 struct il_stats_cmd stats_cmd = {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004316 .configuration_flags =
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004317 clear ? IL_STATS_CONF_CLEAR_STATS : 0,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004318 };
4319
4320 if (flags & CMD_ASYNC)
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004321 return il_send_cmd_pdu_async(il, C_STATS,
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02004322 sizeof(struct il_stats_cmd),
4323 &stats_cmd, NULL);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004324 else
Stanislaw Gruszka4d69c752011-08-30 15:26:35 +02004325 return il_send_cmd_pdu(il, C_STATS,
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02004326 sizeof(struct il_stats_cmd),
4327 &stats_cmd);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004328}
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02004329EXPORT_SYMBOL(il_send_stats_request);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004330
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01004331void il_hdl_pm_sleep(struct il_priv *il,
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02004332 struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004333{
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004334#ifdef CONFIG_IWLEGACY_DEBUG
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02004335 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004336 struct il_sleep_notification *sleep = &(pkt->u.sleep_notif);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004337 D_RX("sleep mode: %d, src: %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004338 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
4339#endif
4340}
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01004341EXPORT_SYMBOL(il_hdl_pm_sleep);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004342
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01004343void il_hdl_pm_debug_stats(struct il_priv *il,
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02004344 struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004345{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02004346 struct il_rx_pkt *pkt = rxb_addr(rxb);
Stanislaw Gruszkae94a4092011-08-31 13:23:20 +02004347 u32 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004348 D_RADIO("Dumping %d bytes of unhandled "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004349 "notification for %s:\n", len,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004350 il_get_cmd_string(pkt->hdr.cmd));
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004351 il_print_hex_dump(il, IL_DL_RADIO, pkt->u.raw, len);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004352}
Stanislaw Gruszkad2dfb332011-11-15 13:16:38 +01004353EXPORT_SYMBOL(il_hdl_pm_debug_stats);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004354
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02004355void il_hdl_error(struct il_priv *il,
Stanislaw Gruszkab73bb5f2011-08-26 14:37:54 +02004356 struct il_rx_buf *rxb)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004357{
Stanislaw Gruszkadcae1c62011-08-26 14:36:21 +02004358 struct il_rx_pkt *pkt = rxb_addr(rxb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004359
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004360 IL_ERR("Error Reply type 0x%08X cmd %s (0x%02X) "
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004361 "seq 0x%04X ser 0x%08X\n",
4362 le32_to_cpu(pkt->u.err_resp.error_type),
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004363 il_get_cmd_string(pkt->u.err_resp.cmd_id),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004364 pkt->u.err_resp.cmd_id,
4365 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
4366 le32_to_cpu(pkt->u.err_resp.error_info));
4367}
Stanislaw Gruszka6e9848b42011-08-30 15:45:31 +02004368EXPORT_SYMBOL(il_hdl_error);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004369
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004370void il_clear_isr_stats(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004371{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004372 memset(&il->isr_stats, 0, sizeof(il->isr_stats));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004373}
4374
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004375int il_mac_conf_tx(struct ieee80211_hw *hw,
Eliad Peller8a3a3c82011-10-02 10:15:52 +02004376 struct ieee80211_vif *vif, u16 queue,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004377 const struct ieee80211_tx_queue_params *params)
4378{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004379 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004380 unsigned long flags;
4381 int q;
4382
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004383 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004384
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004385 if (!il_is_ready_rf(il)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004386 D_MAC80211("leave - RF not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004387 return -EIO;
4388 }
4389
4390 if (queue >= AC_NUM) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004391 D_MAC80211("leave - queue >= AC_NUM %d\n", queue);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004392 return 0;
4393 }
4394
4395 q = AC_NUM - 1 - queue;
4396
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004397 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004398
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004399 il->ctx.qos_data.def_qos_parm.ac[q].cw_min =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004400 cpu_to_le16(params->cw_min);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004401 il->ctx.qos_data.def_qos_parm.ac[q].cw_max =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004402 cpu_to_le16(params->cw_max);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004403 il->ctx.qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4404 il->ctx.qos_data.def_qos_parm.ac[q].edca_txop =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004405 cpu_to_le16((params->txop * 32));
4406
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004407 il->ctx.qos_data.def_qos_parm.ac[q].reserved1 = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004408
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004409 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004410
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004411 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004412 return 0;
4413}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004414EXPORT_SYMBOL(il_mac_conf_tx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004415
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004416int il_mac_tx_last_beacon(struct ieee80211_hw *hw)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004417{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004418 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004419
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004420 return il->ibss_manager == IL_IBSS_MANAGER;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004421}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004422EXPORT_SYMBOL_GPL(il_mac_tx_last_beacon);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004423
4424static int
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004425il_set_mode(struct il_priv *il, struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004426{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004427 il_connection_init_rx_config(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004428
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004429 if (il->cfg->ops->hcmd->set_rxon_chain)
4430 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004431
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004432 return il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004433}
4434
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004435static int il_setup_interface(struct il_priv *il,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004436 struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004437{
4438 struct ieee80211_vif *vif = ctx->vif;
4439 int err;
4440
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004441 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004442
4443 /*
4444 * This variable will be correct only when there's just
4445 * a single context, but all code using it is for hardware
4446 * that supports only one context.
4447 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004448 il->iw_mode = vif->type;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004449
4450 ctx->is_active = true;
4451
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004452 err = il_set_mode(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004453 if (err) {
4454 if (!ctx->always_active)
4455 ctx->is_active = false;
4456 return err;
4457 }
4458
4459 return 0;
4460}
4461
4462int
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004463il_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004464{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004465 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004466 struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004467 int err;
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004468 u32 modes;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004469
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004470 D_MAC80211("enter: type %d, addr %pM\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004471 vif->type, vif->addr);
4472
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004473 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004474
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004475 if (!il_is_ready_rf(il)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004476 IL_WARN("Try to add interface when device not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004477 err = -EINVAL;
4478 goto out;
4479 }
4480
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004481
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004482 /* check if busy context is exclusive */
4483 if (il->ctx.vif &&
4484 (il->ctx.exclusive_interface_modes & BIT(il->ctx.vif->type))) {
4485 err = -EINVAL;
4486 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004487 }
4488
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004489 modes = il->ctx.interface_modes | il->ctx.exclusive_interface_modes;
4490 if (!(modes & BIT(vif->type))) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004491 err = -EOPNOTSUPP;
4492 goto out;
4493 }
4494
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004495 vif_priv->ctx = &il->ctx;
4496 il->ctx.vif = vif;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004497
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004498 err = il_setup_interface(il, &il->ctx);
4499 if (err) {
4500 il->ctx.vif = NULL;
4501 il->iw_mode = NL80211_IFTYPE_STATION;
4502 }
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004503
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004504 out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004505 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004506
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004507 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004508 return err;
4509}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004510EXPORT_SYMBOL(il_mac_add_interface);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004511
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004512static void il_teardown_interface(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004513 struct ieee80211_vif *vif,
4514 bool mode_change)
4515{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004516 struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004517
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004518 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004519
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004520 if (il->scan_vif == vif) {
4521 il_scan_cancel_timeout(il, 200);
4522 il_force_scan_end(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004523 }
4524
4525 if (!mode_change) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004526 il_set_mode(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004527 if (!ctx->always_active)
4528 ctx->is_active = false;
4529 }
4530}
4531
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004532void il_mac_remove_interface(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004533 struct ieee80211_vif *vif)
4534{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004535 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004536 struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004537
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004538 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004539
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004540 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004541
4542 WARN_ON(ctx->vif != vif);
4543 ctx->vif = NULL;
4544
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004545 il_teardown_interface(il, vif, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004546
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004547 memset(il->bssid, 0, ETH_ALEN);
4548 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004549
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004550 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004551
4552}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004553EXPORT_SYMBOL(il_mac_remove_interface);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004554
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004555int il_alloc_txq_mem(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004556{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004557 if (!il->txq)
4558 il->txq = kzalloc(
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004559 sizeof(struct il_tx_queue) *
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004560 il->cfg->base_params->num_of_queues,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004561 GFP_KERNEL);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004562 if (!il->txq) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004563 IL_ERR("Not enough memory for txq\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004564 return -ENOMEM;
4565 }
4566 return 0;
4567}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004568EXPORT_SYMBOL(il_alloc_txq_mem);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004569
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004570void il_txq_mem(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004571{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004572 kfree(il->txq);
4573 il->txq = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004574}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004575EXPORT_SYMBOL(il_txq_mem);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004576
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004577#ifdef CONFIG_IWLEGACY_DEBUGFS
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004578
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004579#define IL_TRAFFIC_DUMP_SIZE (IL_TRAFFIC_ENTRY_SIZE * IL_TRAFFIC_ENTRIES)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004580
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004581void il_reset_traffic_log(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004582{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004583 il->tx_traffic_idx = 0;
4584 il->rx_traffic_idx = 0;
4585 if (il->tx_traffic)
4586 memset(il->tx_traffic, 0, IL_TRAFFIC_DUMP_SIZE);
4587 if (il->rx_traffic)
4588 memset(il->rx_traffic, 0, IL_TRAFFIC_DUMP_SIZE);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004589}
4590
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004591int il_alloc_traffic_mem(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004592{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004593 u32 traffic_size = IL_TRAFFIC_DUMP_SIZE;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004594
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02004595 if (il_debug_level & IL_DL_TX) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004596 if (!il->tx_traffic) {
4597 il->tx_traffic =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004598 kzalloc(traffic_size, GFP_KERNEL);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004599 if (!il->tx_traffic)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004600 return -ENOMEM;
4601 }
4602 }
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02004603 if (il_debug_level & IL_DL_RX) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004604 if (!il->rx_traffic) {
4605 il->rx_traffic =
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004606 kzalloc(traffic_size, GFP_KERNEL);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004607 if (!il->rx_traffic)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004608 return -ENOMEM;
4609 }
4610 }
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004611 il_reset_traffic_log(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004612 return 0;
4613}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004614EXPORT_SYMBOL(il_alloc_traffic_mem);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004615
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004616void il_free_traffic_mem(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004617{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004618 kfree(il->tx_traffic);
4619 il->tx_traffic = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004620
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004621 kfree(il->rx_traffic);
4622 il->rx_traffic = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004623}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004624EXPORT_SYMBOL(il_free_traffic_mem);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004625
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004626void il_dbg_log_tx_data_frame(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004627 u16 length, struct ieee80211_hdr *header)
4628{
4629 __le16 fc;
4630 u16 len;
4631
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02004632 if (likely(!(il_debug_level & IL_DL_TX)))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004633 return;
4634
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004635 if (!il->tx_traffic)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004636 return;
4637
4638 fc = header->frame_control;
4639 if (ieee80211_is_data(fc)) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004640 len = (length > IL_TRAFFIC_ENTRY_SIZE)
4641 ? IL_TRAFFIC_ENTRY_SIZE : length;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004642 memcpy((il->tx_traffic +
4643 (il->tx_traffic_idx * IL_TRAFFIC_ENTRY_SIZE)),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004644 header, len);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004645 il->tx_traffic_idx =
4646 (il->tx_traffic_idx + 1) % IL_TRAFFIC_ENTRIES;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004647 }
4648}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004649EXPORT_SYMBOL(il_dbg_log_tx_data_frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004650
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004651void il_dbg_log_rx_data_frame(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004652 u16 length, struct ieee80211_hdr *header)
4653{
4654 __le16 fc;
4655 u16 len;
4656
Stanislaw Gruszkad2ddf622011-08-16 14:17:04 +02004657 if (likely(!(il_debug_level & IL_DL_RX)))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004658 return;
4659
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004660 if (!il->rx_traffic)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004661 return;
4662
4663 fc = header->frame_control;
4664 if (ieee80211_is_data(fc)) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004665 len = (length > IL_TRAFFIC_ENTRY_SIZE)
4666 ? IL_TRAFFIC_ENTRY_SIZE : length;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004667 memcpy((il->rx_traffic +
4668 (il->rx_traffic_idx * IL_TRAFFIC_ENTRY_SIZE)),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004669 header, len);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004670 il->rx_traffic_idx =
4671 (il->rx_traffic_idx + 1) % IL_TRAFFIC_ENTRIES;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004672 }
4673}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004674EXPORT_SYMBOL(il_dbg_log_rx_data_frame);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004675
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004676const char *il_get_mgmt_string(int cmd)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004677{
4678 switch (cmd) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004679 IL_CMD(MANAGEMENT_ASSOC_REQ);
4680 IL_CMD(MANAGEMENT_ASSOC_RESP);
4681 IL_CMD(MANAGEMENT_REASSOC_REQ);
4682 IL_CMD(MANAGEMENT_REASSOC_RESP);
4683 IL_CMD(MANAGEMENT_PROBE_REQ);
4684 IL_CMD(MANAGEMENT_PROBE_RESP);
4685 IL_CMD(MANAGEMENT_BEACON);
4686 IL_CMD(MANAGEMENT_ATIM);
4687 IL_CMD(MANAGEMENT_DISASSOC);
4688 IL_CMD(MANAGEMENT_AUTH);
4689 IL_CMD(MANAGEMENT_DEAUTH);
4690 IL_CMD(MANAGEMENT_ACTION);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004691 default:
4692 return "UNKNOWN";
4693
4694 }
4695}
4696
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004697const char *il_get_ctrl_string(int cmd)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004698{
4699 switch (cmd) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004700 IL_CMD(CONTROL_BACK_REQ);
4701 IL_CMD(CONTROL_BACK);
4702 IL_CMD(CONTROL_PSPOLL);
4703 IL_CMD(CONTROL_RTS);
4704 IL_CMD(CONTROL_CTS);
4705 IL_CMD(CONTROL_ACK);
4706 IL_CMD(CONTROL_CFEND);
4707 IL_CMD(CONTROL_CFENDACK);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004708 default:
4709 return "UNKNOWN";
4710
4711 }
4712}
4713
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004714void il_clear_traffic_stats(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004715{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004716 memset(&il->tx_stats, 0, sizeof(struct traffic_stats));
4717 memset(&il->rx_stats, 0, sizeof(struct traffic_stats));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004718}
4719
4720/*
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004721 * if CONFIG_IWLEGACY_DEBUGFS defined,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004722 * il_update_stats function will
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004723 * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +02004724 * Use debugFs to display the rx/rx_stats
Stanislaw Gruszkad3175162011-11-15 11:25:42 +01004725 * if CONFIG_IWLEGACY_DEBUGFS not being defined, then no MGMT and CTRL
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004726 * information will be recorded, but DATA pkt still will be recorded
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004727 * for the reason of il_led.c need to control the led blinking based on
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004728 * number of tx and rx data.
4729 *
4730 */
4731void
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004732il_update_stats(struct il_priv *il, bool is_tx, __le16 fc, u16 len)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004733{
4734 struct traffic_stats *stats;
4735
4736 if (is_tx)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004737 stats = &il->tx_stats;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004738 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004739 stats = &il->rx_stats;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004740
4741 if (ieee80211_is_mgmt(fc)) {
4742 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
4743 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
4744 stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
4745 break;
4746 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
4747 stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
4748 break;
4749 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
4750 stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
4751 break;
4752 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
4753 stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
4754 break;
4755 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
4756 stats->mgmt[MANAGEMENT_PROBE_REQ]++;
4757 break;
4758 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
4759 stats->mgmt[MANAGEMENT_PROBE_RESP]++;
4760 break;
4761 case cpu_to_le16(IEEE80211_STYPE_BEACON):
4762 stats->mgmt[MANAGEMENT_BEACON]++;
4763 break;
4764 case cpu_to_le16(IEEE80211_STYPE_ATIM):
4765 stats->mgmt[MANAGEMENT_ATIM]++;
4766 break;
4767 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
4768 stats->mgmt[MANAGEMENT_DISASSOC]++;
4769 break;
4770 case cpu_to_le16(IEEE80211_STYPE_AUTH):
4771 stats->mgmt[MANAGEMENT_AUTH]++;
4772 break;
4773 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
4774 stats->mgmt[MANAGEMENT_DEAUTH]++;
4775 break;
4776 case cpu_to_le16(IEEE80211_STYPE_ACTION):
4777 stats->mgmt[MANAGEMENT_ACTION]++;
4778 break;
4779 }
4780 } else if (ieee80211_is_ctl(fc)) {
4781 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
4782 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
4783 stats->ctrl[CONTROL_BACK_REQ]++;
4784 break;
4785 case cpu_to_le16(IEEE80211_STYPE_BACK):
4786 stats->ctrl[CONTROL_BACK]++;
4787 break;
4788 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
4789 stats->ctrl[CONTROL_PSPOLL]++;
4790 break;
4791 case cpu_to_le16(IEEE80211_STYPE_RTS):
4792 stats->ctrl[CONTROL_RTS]++;
4793 break;
4794 case cpu_to_le16(IEEE80211_STYPE_CTS):
4795 stats->ctrl[CONTROL_CTS]++;
4796 break;
4797 case cpu_to_le16(IEEE80211_STYPE_ACK):
4798 stats->ctrl[CONTROL_ACK]++;
4799 break;
4800 case cpu_to_le16(IEEE80211_STYPE_CFEND):
4801 stats->ctrl[CONTROL_CFEND]++;
4802 break;
4803 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
4804 stats->ctrl[CONTROL_CFENDACK]++;
4805 break;
4806 }
4807 } else {
4808 /* data */
4809 stats->data_cnt++;
4810 stats->data_bytes += len;
4811 }
4812}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004813EXPORT_SYMBOL(il_update_stats);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004814#endif
4815
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004816int il_force_reset(struct il_priv *il, bool external)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004817{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004818 struct il_force_reset *force_reset;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004819
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004820 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004821 return -EINVAL;
4822
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004823 force_reset = &il->force_reset;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004824 force_reset->reset_request_count++;
4825 if (!external) {
4826 if (force_reset->last_force_reset_jiffies &&
4827 time_after(force_reset->last_force_reset_jiffies +
4828 force_reset->reset_duration, jiffies)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004829 D_INFO("force reset rejected\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004830 force_reset->reset_reject_count++;
4831 return -EAGAIN;
4832 }
4833 }
4834 force_reset->reset_success_count++;
4835 force_reset->last_force_reset_jiffies = jiffies;
Stanislaw Gruszkadd6d2a82011-06-08 15:28:26 +02004836
4837 /*
4838 * if the request is from external(ex: debugfs),
4839 * then always perform the request in regardless the module
4840 * parameter setting
4841 * if the request is from internal (uCode error or driver
4842 * detect failure), then fw_restart module parameter
4843 * need to be check before performing firmware reload
4844 */
4845
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004846 if (!external && !il->cfg->mod_params->restart_fw) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01004847 D_INFO("Cancel firmware reload based on "
Stanislaw Gruszkadd6d2a82011-06-08 15:28:26 +02004848 "module parameter setting\n");
4849 return 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004850 }
Stanislaw Gruszkadd6d2a82011-06-08 15:28:26 +02004851
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004852 IL_ERR("On demand firmware reload\n");
Stanislaw Gruszkadd6d2a82011-06-08 15:28:26 +02004853
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004854 /* Set the FW error flag -- cleared on il_down */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004855 set_bit(S_FW_ERROR, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004856 wake_up(&il->wait_command_queue);
Stanislaw Gruszkadd6d2a82011-06-08 15:28:26 +02004857 /*
4858 * Keep the restart process from trying to send host
4859 * commands by clearing the INIT status bit
4860 */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004861 clear_bit(S_READY, &il->status);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004862 queue_work(il->workqueue, &il->restart);
Stanislaw Gruszkadd6d2a82011-06-08 15:28:26 +02004863
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004864 return 0;
4865}
4866
4867int
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004868il_mac_change_interface(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004869 struct ieee80211_vif *vif,
4870 enum nl80211_iftype newtype, bool newp2p)
4871{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004872 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004873 struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004874 u32 modes;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004875 int err;
4876
4877 newtype = ieee80211_iftype_p2p(newtype, newp2p);
4878
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004879 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004880
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004881 if (!ctx->vif || !il_is_ready_rf(il)) {
Johannes Bergffd8c742011-03-29 15:28:11 +02004882 /*
4883 * Huh? But wait ... this can maybe happen when
4884 * we're in the middle of a firmware restart!
4885 */
4886 err = -EBUSY;
4887 goto out;
4888 }
4889
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004890 modes = ctx->interface_modes | ctx->exclusive_interface_modes;
4891 if (!(modes & BIT(newtype))) {
4892 err = -EOPNOTSUPP;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004893 goto out;
4894 }
4895
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02004896 if ((il->ctx.exclusive_interface_modes & BIT(il->ctx.vif->type)) ||
4897 (il->ctx.exclusive_interface_modes & BIT(newtype))) {
4898 err = -EINVAL;
4899 goto out;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004900 }
4901
4902 /* success */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004903 il_teardown_interface(il, vif, true);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004904 vif->type = newtype;
Johannes Bergffd8c742011-03-29 15:28:11 +02004905 vif->p2p = newp2p;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004906 err = il_setup_interface(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004907 WARN_ON(err);
4908 /*
4909 * We've switched internally, but submitting to the
4910 * device may have failed for some reason. Mask this
4911 * error, because otherwise mac80211 will not switch
4912 * (and set the interface type back) and we'll be
4913 * out of sync with it.
4914 */
4915 err = 0;
4916
4917 out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004918 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004919 return err;
4920}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004921EXPORT_SYMBOL(il_mac_change_interface);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004922
4923/*
4924 * On every watchdog tick we check (latest) time stamp. If it does not
4925 * change during timeout period and queue is not empty we reset firmware.
4926 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004927static int il_check_stuck_queue(struct il_priv *il, int cnt)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004928{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004929 struct il_tx_queue *txq = &il->txq[cnt];
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004930 struct il_queue *q = &txq->q;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004931 unsigned long timeout;
4932 int ret;
4933
4934 if (q->read_ptr == q->write_ptr) {
4935 txq->time_stamp = jiffies;
4936 return 0;
4937 }
4938
4939 timeout = txq->time_stamp +
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004940 msecs_to_jiffies(il->cfg->base_params->wd_timeout);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004941
4942 if (time_after(jiffies, timeout)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02004943 IL_ERR("Queue %d stuck for %u ms.\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004944 q->id, il->cfg->base_params->wd_timeout);
4945 ret = il_force_reset(il, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004946 return (ret == -EAGAIN) ? 0 : 1;
4947 }
4948
4949 return 0;
4950}
4951
4952/*
4953 * Making watchdog tick be a quarter of timeout assure we will
4954 * discover the queue hung between timeout and 1.25*timeout
4955 */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004956#define IL_WD_TICK(timeout) ((timeout) / 4)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004957
4958/*
4959 * Watchdog timer callback, we check each tx queue for stuck, if if hung
4960 * we reset the firmware. If everything is fine just rearm the timer.
4961 */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004962void il_bg_watchdog(unsigned long data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004963{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004964 struct il_priv *il = (struct il_priv *)data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004965 int cnt;
4966 unsigned long timeout;
4967
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01004968 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004969 return;
4970
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004971 timeout = il->cfg->base_params->wd_timeout;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004972 if (timeout == 0)
4973 return;
4974
4975 /* monitor and check for stuck cmd queue */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004976 if (il_check_stuck_queue(il, il->cmd_queue))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004977 return;
4978
4979 /* monitor and check for other stuck queues */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004980 if (il_is_any_associated(il)) {
4981 for (cnt = 0; cnt < il->hw_params.max_txq_num; cnt++) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004982 /* skip as we already checked the command queue */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004983 if (cnt == il->cmd_queue)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004984 continue;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004985 if (il_check_stuck_queue(il, cnt))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004986 return;
4987 }
4988 }
4989
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004990 mod_timer(&il->watchdog, jiffies +
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004991 msecs_to_jiffies(IL_WD_TICK(timeout)));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004992}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02004993EXPORT_SYMBOL(il_bg_watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004994
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004995void il_setup_watchdog(struct il_priv *il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004996{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02004997 unsigned int timeout = il->cfg->base_params->wd_timeout;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08004998
4999 if (timeout)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005000 mod_timer(&il->watchdog,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005001 jiffies + msecs_to_jiffies(IL_WD_TICK(timeout)));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005002 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005003 del_timer(&il->watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005004}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005005EXPORT_SYMBOL(il_setup_watchdog);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005006
5007/*
5008 * extended beacon time format
5009 * time in usec will be changed into a 32-bit value in extended:internal format
5010 * the extended part is the beacon counts
5011 * the internal part is the time in usec within one beacon interval
5012 */
5013u32
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005014il_usecs_to_beacons(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005015 u32 usec, u32 beacon_interval)
5016{
5017 u32 quot;
5018 u32 rem;
5019 u32 interval = beacon_interval * TIME_UNIT;
5020
5021 if (!interval || !usec)
5022 return 0;
5023
5024 quot = (usec / interval) &
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005025 (il_beacon_time_mask_high(il,
5026 il->hw_params.beacon_time_tsf_bits) >>
5027 il->hw_params.beacon_time_tsf_bits);
5028 rem = (usec % interval) & il_beacon_time_mask_low(il,
5029 il->hw_params.beacon_time_tsf_bits);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005030
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005031 return (quot << il->hw_params.beacon_time_tsf_bits) + rem;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005032}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005033EXPORT_SYMBOL(il_usecs_to_beacons);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005034
5035/* base is usually what we get from ucode with each received frame,
5036 * the same as HW timer counter counting down
5037 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005038__le32 il_add_beacon_time(struct il_priv *il, u32 base,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005039 u32 addon, u32 beacon_interval)
5040{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005041 u32 base_low = base & il_beacon_time_mask_low(il,
5042 il->hw_params.beacon_time_tsf_bits);
5043 u32 addon_low = addon & il_beacon_time_mask_low(il,
5044 il->hw_params.beacon_time_tsf_bits);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005045 u32 interval = beacon_interval * TIME_UNIT;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005046 u32 res = (base & il_beacon_time_mask_high(il,
5047 il->hw_params.beacon_time_tsf_bits)) +
5048 (addon & il_beacon_time_mask_high(il,
5049 il->hw_params.beacon_time_tsf_bits));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005050
5051 if (base_low > addon_low)
5052 res += base_low - addon_low;
5053 else if (base_low < addon_low) {
5054 res += interval + base_low - addon_low;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005055 res += (1 << il->hw_params.beacon_time_tsf_bits);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005056 } else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005057 res += (1 << il->hw_params.beacon_time_tsf_bits);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005058
5059 return cpu_to_le32(res);
5060}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005061EXPORT_SYMBOL(il_add_beacon_time);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005062
5063#ifdef CONFIG_PM
5064
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005065int il_pci_suspend(struct device *device)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005066{
5067 struct pci_dev *pdev = to_pci_dev(device);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005068 struct il_priv *il = pci_get_drvdata(pdev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005069
5070 /*
5071 * This function is called when system goes into suspend state
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005072 * mac80211 will call il_mac_stop() from the mac80211 suspend function
5073 * first but since il_mac_stop() has no knowledge of who the caller is,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005074 * it will not call apm_ops.stop() to stop the DMA operation.
5075 * Calling apm_ops.stop here to make sure we stop the DMA.
5076 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005077 il_apm_stop(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005078
5079 return 0;
5080}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005081EXPORT_SYMBOL(il_pci_suspend);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005082
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005083int il_pci_resume(struct device *device)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005084{
5085 struct pci_dev *pdev = to_pci_dev(device);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005086 struct il_priv *il = pci_get_drvdata(pdev);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005087 bool hw_rfkill = false;
5088
5089 /*
5090 * We disable the RETRY_TIMEOUT register (0x41) to keep
5091 * PCI Tx retries from interfering with C3 CPU state.
5092 */
5093 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
5094
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005095 il_enable_interrupts(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005096
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005097 if (!(_il_rd(il, CSR_GP_CNTRL) &
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005098 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5099 hw_rfkill = true;
5100
5101 if (hw_rfkill)
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005102 set_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005103 else
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005104 clear_bit(S_RF_KILL_HW, &il->status);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005105
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005106 wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rfkill);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005107
5108 return 0;
5109}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005110EXPORT_SYMBOL(il_pci_resume);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005111
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005112const struct dev_pm_ops il_pm_ops = {
5113 .suspend = il_pci_suspend,
5114 .resume = il_pci_resume,
5115 .freeze = il_pci_suspend,
5116 .thaw = il_pci_resume,
5117 .poweroff = il_pci_suspend,
5118 .restore = il_pci_resume,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005119};
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005120EXPORT_SYMBOL(il_pm_ops);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005121
5122#endif /* CONFIG_PM */
5123
5124static void
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005125il_update_qos(struct il_priv *il, struct il_rxon_context *ctx)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005126{
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005127 if (test_bit(S_EXIT_PENDING, &il->status))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005128 return;
5129
5130 if (!ctx->is_active)
5131 return;
5132
5133 ctx->qos_data.def_qos_parm.qos_flags = 0;
5134
5135 if (ctx->qos_data.qos_active)
5136 ctx->qos_data.def_qos_parm.qos_flags |=
5137 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
5138
5139 if (ctx->ht.enabled)
5140 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
5141
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005142 D_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005143 ctx->qos_data.qos_active,
5144 ctx->qos_data.def_qos_parm.qos_flags);
5145
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005146 il_send_cmd_pdu_async(il, ctx->qos_cmd,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005147 sizeof(struct il_qosparam_cmd),
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005148 &ctx->qos_data.def_qos_parm, NULL);
5149}
5150
5151/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005152 * il_mac_config - mac80211 config callback
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005153 */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005154int il_mac_config(struct ieee80211_hw *hw, u32 changed)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005155{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005156 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005157 const struct il_channel_info *ch_info;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005158 struct ieee80211_conf *conf = &hw->conf;
5159 struct ieee80211_channel *channel = conf->channel;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005160 struct il_ht_config *ht_conf = &il->current_ht_config;
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005161 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005162 unsigned long flags = 0;
5163 int ret = 0;
5164 u16 ch;
5165 int scan_active = 0;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005166 bool ht_changed = false;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005167
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005168 if (WARN_ON(!il->cfg->ops->legacy))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005169 return -EOPNOTSUPP;
5170
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005171 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005172
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005173 D_MAC80211("enter to channel %d changed 0x%X\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005174 channel->hw_value, changed);
5175
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005176 if (unlikely(test_bit(S_SCANNING, &il->status))) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005177 scan_active = 1;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005178 D_MAC80211("scan active\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005179 }
5180
5181 if (changed & (IEEE80211_CONF_CHANGE_SMPS |
5182 IEEE80211_CONF_CHANGE_CHANNEL)) {
5183 /* mac80211 uses static for non-HT which is what we want */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005184 il->current_ht_config.smps = conf->smps_mode;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005185
5186 /*
5187 * Recalculate chain counts.
5188 *
5189 * If monitor mode is enabled then mac80211 will
5190 * set up the SM PS mode to OFF if an HT channel is
5191 * configured.
5192 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005193 if (il->cfg->ops->hcmd->set_rxon_chain)
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005194 il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005195 }
5196
5197 /* during scanning mac80211 will delay channel setting until
5198 * scan finish with changed = 0
5199 */
5200 if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005201
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005202 if (scan_active)
5203 goto set_ch_out;
5204
5205 ch = channel->hw_value;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005206 ch_info = il_get_channel_info(il, channel->band, ch);
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005207 if (!il_is_channel_valid(ch_info)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005208 D_MAC80211("leave - invalid channel\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005209 ret = -EINVAL;
5210 goto set_ch_out;
5211 }
5212
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005213 if (il->iw_mode == NL80211_IFTYPE_ADHOC &&
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005214 !il_is_channel_ibss(ch_info)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005215 D_MAC80211("leave - not IBSS channel\n");
Stanislaw Gruszkaeb85de32011-05-07 17:46:21 +02005216 ret = -EINVAL;
5217 goto set_ch_out;
5218 }
5219
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005220 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005221
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005222 /* Configure HT40 channels */
5223 if (ctx->ht.enabled != conf_is_ht(conf)) {
5224 ctx->ht.enabled = conf_is_ht(conf);
5225 ht_changed = true;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005226 }
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005227 if (ctx->ht.enabled) {
5228 if (conf_is_ht40_minus(conf)) {
5229 ctx->ht.extension_chan_offset =
5230 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
5231 ctx->ht.is_40mhz = true;
5232 } else if (conf_is_ht40_plus(conf)) {
5233 ctx->ht.extension_chan_offset =
5234 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
5235 ctx->ht.is_40mhz = true;
5236 } else {
5237 ctx->ht.extension_chan_offset =
5238 IEEE80211_HT_PARAM_CHA_SEC_NONE;
5239 ctx->ht.is_40mhz = false;
5240 }
5241 } else
5242 ctx->ht.is_40mhz = false;
5243
5244 /*
5245 * Default to no protection. Protection mode will
5246 * later be set from BSS config in il_ht_conf
5247 */
5248 ctx->ht.protection =
5249 IEEE80211_HT_OP_MODE_PROTECTION_NONE;
5250
5251 /* if we are switching from ht to 2.4 clear flags
5252 * from any ht related info since 2.4 does not
5253 * support ht */
5254 if ((le16_to_cpu(ctx->staging.channel) != ch))
5255 ctx->staging.flags = 0;
5256
5257 il_set_rxon_channel(il, channel, ctx);
5258 il_set_rxon_ht(il, ht_conf);
5259
5260 il_set_flags_for_band(il, ctx, channel->band,
5261 ctx->vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005262
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005263 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005264
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005265 if (il->cfg->ops->legacy->update_bcast_stations)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005266 ret =
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005267 il->cfg->ops->legacy->update_bcast_stations(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005268
5269 set_ch_out:
5270 /* The list of supported rates and rate mask can be different
5271 * for each band; since the band may have changed, reset
5272 * the rate mask to what mac80211 lists */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005273 il_set_rate(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005274 }
5275
5276 if (changed & (IEEE80211_CONF_CHANGE_PS |
5277 IEEE80211_CONF_CHANGE_IDLE)) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005278 ret = il_power_update_mode(il, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005279 if (ret)
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005280 D_MAC80211("Error setting sleep level\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005281 }
5282
5283 if (changed & IEEE80211_CONF_CHANGE_POWER) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005284 D_MAC80211("TX Power old=%d new=%d\n",
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005285 il->tx_power_user_lmt, conf->power_level);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005286
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005287 il_set_tx_power(il, conf->power_level, false);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005288 }
5289
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005290 if (!il_is_ready(il)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005291 D_MAC80211("leave - not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005292 goto out;
5293 }
5294
5295 if (scan_active)
5296 goto out;
5297
Stanislaw Gruszka17d6e552011-08-29 12:52:20 +02005298 if (memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging)))
5299 il_commit_rxon(il, ctx);
5300 else
5301 D_INFO("Not re-sending same RXON configuration.\n");
5302 if (ht_changed)
5303 il_update_qos(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005304
5305out:
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005306 D_MAC80211("leave\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005307 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005308 return ret;
5309}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005310EXPORT_SYMBOL(il_mac_config);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005311
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005312void il_mac_reset_tsf(struct ieee80211_hw *hw,
Eliad Peller37a41b42011-09-21 14:06:11 +03005313 struct ieee80211_vif *vif)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005314{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005315 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005316 unsigned long flags;
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +01005317 struct il_rxon_context *ctx = &il->ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005318
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005319 if (WARN_ON(!il->cfg->ops->legacy))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005320 return;
5321
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005322 mutex_lock(&il->mutex);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005323 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005324
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005325 spin_lock_irqsave(&il->lock, flags);
5326 memset(&il->current_ht_config, 0, sizeof(struct il_ht_config));
5327 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005328
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005329 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005330
5331 /* new association get rid of ibss beacon skb */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005332 if (il->beacon_skb)
5333 dev_kfree_skb(il->beacon_skb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005334
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005335 il->beacon_skb = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005336
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005337 il->timestamp = 0;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005338
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005339 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005340
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005341 il_scan_cancel_timeout(il, 100);
5342 if (!il_is_ready_rf(il)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005343 D_MAC80211("leave - not ready\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005344 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005345 return;
5346 }
5347
5348 /* we are restarting association process
5349 * clear RXON_FILTER_ASSOC_MSK bit
5350 */
5351 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005352 il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005353
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005354 il_set_rate(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005355
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005356 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005357
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005358 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005359}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005360EXPORT_SYMBOL(il_mac_reset_tsf);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005361
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005362static void il_ht_conf(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005363 struct ieee80211_vif *vif)
5364{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005365 struct il_ht_config *ht_conf = &il->current_ht_config;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005366 struct ieee80211_sta *sta;
5367 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005368 struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005369
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005370 D_ASSOC("enter:\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005371
5372 if (!ctx->ht.enabled)
5373 return;
5374
5375 ctx->ht.protection =
5376 bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
5377 ctx->ht.non_gf_sta_present =
5378 !!(bss_conf->ht_operation_mode &
5379 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
5380
5381 ht_conf->single_chain_sufficient = false;
5382
5383 switch (vif->type) {
5384 case NL80211_IFTYPE_STATION:
5385 rcu_read_lock();
5386 sta = ieee80211_find_sta(vif, bss_conf->bssid);
5387 if (sta) {
5388 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
5389 int maxstreams;
5390
5391 maxstreams = (ht_cap->mcs.tx_params &
5392 IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
5393 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
5394 maxstreams += 1;
5395
Stanislaw Gruszka232913b2011-08-26 10:45:16 +02005396 if (ht_cap->mcs.rx_mask[1] == 0 &&
5397 ht_cap->mcs.rx_mask[2] == 0)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005398 ht_conf->single_chain_sufficient = true;
5399 if (maxstreams <= 1)
5400 ht_conf->single_chain_sufficient = true;
5401 } else {
5402 /*
5403 * If at all, this can only happen through a race
5404 * when the AP disconnects us while we're still
5405 * setting up the connection, in that case mac80211
5406 * will soon tell us about that.
5407 */
5408 ht_conf->single_chain_sufficient = true;
5409 }
5410 rcu_read_unlock();
5411 break;
5412 case NL80211_IFTYPE_ADHOC:
5413 ht_conf->single_chain_sufficient = true;
5414 break;
5415 default:
5416 break;
5417 }
5418
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005419 D_ASSOC("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005420}
5421
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005422static inline void il_set_no_assoc(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005423 struct ieee80211_vif *vif)
5424{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005425 struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005426
5427 /*
5428 * inform the ucode that there is no longer an
5429 * association and that no more packets should be
5430 * sent
5431 */
5432 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5433 ctx->staging.assoc_id = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005434 il_commit_rxon(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005435}
5436
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005437static void il_beacon_update(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005438 struct ieee80211_vif *vif)
5439{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005440 struct il_priv *il = hw->priv;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005441 unsigned long flags;
5442 __le64 timestamp;
5443 struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
5444
5445 if (!skb)
5446 return;
5447
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005448 D_MAC80211("enter\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005449
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005450 lockdep_assert_held(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005451
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005452 if (!il->beacon_ctx) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005453 IL_ERR("update beacon but no beacon context!\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005454 dev_kfree_skb(skb);
5455 return;
5456 }
5457
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005458 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005459
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005460 if (il->beacon_skb)
5461 dev_kfree_skb(il->beacon_skb);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005462
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005463 il->beacon_skb = skb;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005464
5465 timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005466 il->timestamp = le64_to_cpu(timestamp);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005467
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005468 D_MAC80211("leave\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005469 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005470
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005471 if (!il_is_ready_rf(il)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005472 D_MAC80211("leave - RF not ready\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005473 return;
5474 }
5475
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005476 il->cfg->ops->legacy->post_associate(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005477}
5478
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005479void il_mac_bss_info_changed(struct ieee80211_hw *hw,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005480 struct ieee80211_vif *vif,
5481 struct ieee80211_bss_conf *bss_conf,
5482 u32 changes)
5483{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005484 struct il_priv *il = hw->priv;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005485 struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005486 int ret;
5487
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005488 if (WARN_ON(!il->cfg->ops->legacy))
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005489 return;
5490
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005491 D_MAC80211("changes = 0x%X\n", changes);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005492
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005493 mutex_lock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005494
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005495 if (!il_is_alive(il)) {
5496 mutex_unlock(&il->mutex);
Stanislaw Gruszka28a6e572011-04-28 11:51:32 +02005497 return;
5498 }
5499
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005500 if (changes & BSS_CHANGED_QOS) {
5501 unsigned long flags;
5502
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005503 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005504 ctx->qos_data.qos_active = bss_conf->qos;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005505 il_update_qos(il, ctx);
5506 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005507 }
5508
5509 if (changes & BSS_CHANGED_BEACON_ENABLED) {
5510 /*
5511 * the add_interface code must make sure we only ever
5512 * have a single interface that could be beaconing at
5513 * any time.
5514 */
5515 if (vif->bss_conf.enable_beacon)
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005516 il->beacon_ctx = ctx;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005517 else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005518 il->beacon_ctx = NULL;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005519 }
5520
5521 if (changes & BSS_CHANGED_BSSID) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005522 D_MAC80211("BSSID %pM\n", bss_conf->bssid);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005523
5524 /*
5525 * If there is currently a HW scan going on in the
5526 * background then we need to cancel it else the RXON
5527 * below/in post_associate will fail.
5528 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005529 if (il_scan_cancel_timeout(il, 100)) {
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005530 IL_WARN(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005531 "Aborted scan still in progress after 100ms\n");
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005532 D_MAC80211(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005533 "leaving - scan abort failed.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005534 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005535 return;
5536 }
5537
5538 /* mac80211 only sets assoc when in STATION mode */
5539 if (vif->type == NL80211_IFTYPE_ADHOC || bss_conf->assoc) {
5540 memcpy(ctx->staging.bssid_addr,
5541 bss_conf->bssid, ETH_ALEN);
5542
5543 /* currently needed in a few places */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005544 memcpy(il->bssid, bss_conf->bssid, ETH_ALEN);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005545 } else {
5546 ctx->staging.filter_flags &=
5547 ~RXON_FILTER_ASSOC_MSK;
5548 }
5549
5550 }
5551
5552 /*
5553 * This needs to be after setting the BSSID in case
5554 * mac80211 decides to do both changes at once because
5555 * it will invoke post_associate.
5556 */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +02005557 if (vif->type == NL80211_IFTYPE_ADHOC && (changes & BSS_CHANGED_BEACON))
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005558 il_beacon_update(hw, vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005559
5560 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005561 D_MAC80211("ERP_PREAMBLE %d\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005562 bss_conf->use_short_preamble);
5563 if (bss_conf->use_short_preamble)
5564 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5565 else
5566 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5567 }
5568
5569 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005570 D_MAC80211(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005571 "ERP_CTS %d\n", bss_conf->use_cts_prot);
Stanislaw Gruszka232913b2011-08-26 10:45:16 +02005572 if (bss_conf->use_cts_prot && il->band != IEEE80211_BAND_5GHZ)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005573 ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
5574 else
5575 ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
5576 if (bss_conf->use_cts_prot)
5577 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
5578 else
5579 ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
5580 }
5581
5582 if (changes & BSS_CHANGED_BASIC_RATES) {
5583 /* XXX use this information
5584 *
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005585 * To do that, remove code from il_set_rate() and put something
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005586 * like this here:
5587 *
5588 if (A-band)
5589 ctx->staging.ofdm_basic_rates =
5590 bss_conf->basic_rates;
5591 else
5592 ctx->staging.ofdm_basic_rates =
5593 bss_conf->basic_rates >> 4;
5594 ctx->staging.cck_basic_rates =
5595 bss_conf->basic_rates & 0xF;
5596 */
5597 }
5598
5599 if (changes & BSS_CHANGED_HT) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005600 il_ht_conf(il, vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005601
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005602 if (il->cfg->ops->hcmd->set_rxon_chain)
5603 il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005604 }
5605
5606 if (changes & BSS_CHANGED_ASSOC) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005607 D_MAC80211("ASSOC %d\n", bss_conf->assoc);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005608 if (bss_conf->assoc) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005609 il->timestamp = bss_conf->timestamp;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005610
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005611 if (!il_is_rfkill(il))
5612 il->cfg->ops->legacy->post_associate(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005613 } else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005614 il_set_no_assoc(il, vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005615 }
5616
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005617 if (changes && il_is_associated_ctx(ctx) && bss_conf->aid) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005618 D_MAC80211("Changes (%#x) while associated\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005619 changes);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005620 ret = il_send_rxon_assoc(il, ctx);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005621 if (!ret) {
5622 /* Sync active_rxon with latest change. */
5623 memcpy((void *)&ctx->active,
5624 &ctx->staging,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005625 sizeof(struct il_rxon_cmd));
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005626 }
5627 }
5628
5629 if (changes & BSS_CHANGED_BEACON_ENABLED) {
5630 if (vif->bss_conf.enable_beacon) {
5631 memcpy(ctx->staging.bssid_addr,
5632 bss_conf->bssid, ETH_ALEN);
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005633 memcpy(il->bssid, bss_conf->bssid, ETH_ALEN);
5634 il->cfg->ops->legacy->config_ap(il);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005635 } else
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005636 il_set_no_assoc(il, vif);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005637 }
5638
5639 if (changes & BSS_CHANGED_IBSS) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005640 ret = il->cfg->ops->legacy->manage_ibss_station(il, vif,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005641 bss_conf->ibss_joined);
5642 if (ret)
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005643 IL_ERR("failed to %s IBSS station %pM\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005644 bss_conf->ibss_joined ? "add" : "remove",
5645 bss_conf->bssid);
5646 }
5647
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005648 mutex_unlock(&il->mutex);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005649
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005650 D_MAC80211("leave\n");
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005651}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005652EXPORT_SYMBOL(il_mac_bss_info_changed);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005653
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005654irqreturn_t il_isr(int irq, void *data)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005655{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005656 struct il_priv *il = data;
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005657 u32 inta, inta_mask;
5658 u32 inta_fh;
5659 unsigned long flags;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005660 if (!il)
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005661 return IRQ_NONE;
5662
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005663 spin_lock_irqsave(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005664
5665 /* Disable (but don't clear!) interrupts here to avoid
5666 * back-to-back ISRs and sporadic interrupts from our NIC.
5667 * If we have something to service, the tasklet will re-enable ints.
5668 * If we *don't* have something, we'll re-enable before leaving here. */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005669 inta_mask = _il_rd(il, CSR_INT_MASK); /* just for debug */
5670 _il_wr(il, CSR_INT_MASK, 0x00000000);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005671
5672 /* Discover which interrupts are active/pending */
Stanislaw Gruszka841b2cc2011-08-24 15:14:03 +02005673 inta = _il_rd(il, CSR_INT);
5674 inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005675
5676 /* Ignore interrupt if there's nothing in NIC to service.
5677 * This may be due to IRQ shared with another device,
5678 * or due to sporadic interrupts thrown from our NIC. */
5679 if (!inta && !inta_fh) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005680 D_ISR(
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005681 "Ignore interrupt, inta == 0, inta_fh == 0\n");
5682 goto none;
5683 }
5684
Stanislaw Gruszka232913b2011-08-26 10:45:16 +02005685 if (inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0) {
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005686 /* Hardware disappeared. It might have already raised
5687 * an interrupt */
Stanislaw Gruszka9406f792011-08-18 22:07:57 +02005688 IL_WARN("HARDWARE GONE?? INTA == 0x%08x\n", inta);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005689 goto unplugged;
5690 }
5691
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +01005692 D_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005693 inta, inta_mask, inta_fh);
5694
5695 inta &= ~CSR_INT_BIT_SCD;
5696
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005697 /* il_irq_tasklet() will service interrupts and re-enable them */
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005698 if (likely(inta || inta_fh))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005699 tasklet_schedule(&il->irq_tasklet);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005700
5701unplugged:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005702 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005703 return IRQ_HANDLED;
5704
5705none:
5706 /* re-enable interrupts here since we don't have anything to service. */
Stanislaw Gruszka93fd74e2011-04-28 11:51:30 +02005707 /* only Re-enable if disabled by irq */
Stanislaw Gruszkaa6766cc2011-11-15 13:09:01 +01005708 if (test_bit(S_INT_ENABLED, &il->status))
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005709 il_enable_interrupts(il);
5710 spin_unlock_irqrestore(&il->lock, flags);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005711 return IRQ_NONE;
5712}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005713EXPORT_SYMBOL(il_isr);
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005714
5715/*
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005716 * il_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005717 * function.
5718 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +02005719void il_tx_cmd_protection(struct il_priv *il,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08005720 struct ieee80211_tx_info *info,
5721 __le16 fc, __le32 *tx_flags)
5722{
5723 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
5724 *tx_flags |= TX_CMD_FLG_RTS_MSK;
5725 *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
5726 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
5727
5728 if (!ieee80211_is_mgmt(fc))
5729 return;
5730
5731 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
5732 case cpu_to_le16(IEEE80211_STYPE_AUTH):
5733 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
5734 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
5735 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
5736 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
5737 *tx_flags |= TX_CMD_FLG_CTS_MSK;
5738 break;
5739 }
5740 } else if (info->control.rates[0].flags &
5741 IEEE80211_TX_RC_USE_CTS_PROTECT) {
5742 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
5743 *tx_flags |= TX_CMD_FLG_CTS_MSK;
5744 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
5745 }
5746}
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +02005747EXPORT_SYMBOL(il_tx_cmd_protection);