iwlwifi: remove cck_flag from iwl_driver_hw_info
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl3945-base.c
1 /******************************************************************************
2 *
3 * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
4 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30 /*
31 * NOTE: This file (iwl-base.c) is used to build to multiple hardware targets
32 * by defining IWL to either 3945 or 4965. The Makefile used when building
33 * the base targets will create base-3945.o and base-4965.o
34 *
35 * The eventual goal is to move as many of the #if IWL / #endif blocks out of
36 * this file and into the hardware specific implementation files (iwl-XXXX.c)
37 * and leave only the common (non #ifdef sprinkled) code in this file
38 */
39
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/version.h>
43 #include <linux/init.h>
44 #include <linux/pci.h>
45 #include <linux/dma-mapping.h>
46 #include <linux/delay.h>
47 #include <linux/skbuff.h>
48 #include <linux/netdevice.h>
49 #include <linux/wireless.h>
50 #include <linux/firmware.h>
51 #include <linux/etherdevice.h>
52 #include <linux/if_arp.h>
53
54 #include <net/ieee80211_radiotap.h>
55 #include <net/mac80211.h>
56
57 #include <asm/div64.h>
58
59 #define IWL 3945
60
61 #include "iwlwifi.h"
62 #include "iwl-3945.h"
63 #include "iwl-helpers.h"
64
65 #ifdef CONFIG_IWLWIFI_DEBUG
66 u32 iwl_debug_level;
67 #endif
68
69 /******************************************************************************
70 *
71 * module boiler plate
72 *
73 ******************************************************************************/
74
75 /* module parameters */
76 int iwl_param_disable_hw_scan;
77 int iwl_param_debug;
78 int iwl_param_disable; /* def: enable radio */
79 int iwl_param_antenna; /* def: 0 = both antennas (use diversity) */
80 int iwl_param_hwcrypto; /* def: using software encryption */
81 int iwl_param_qos_enable = 1;
82 int iwl_param_queues_num = IWL_MAX_NUM_QUEUES;
83
84 /*
85 * module name, copyright, version, etc.
86 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
87 */
88
89 #define DRV_DESCRIPTION \
90 "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
91
92 #ifdef CONFIG_IWLWIFI_DEBUG
93 #define VD "d"
94 #else
95 #define VD
96 #endif
97
98 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
99 #define VS "s"
100 #else
101 #define VS
102 #endif
103
104 #define IWLWIFI_VERSION "1.1.17k" VD VS
105 #define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
106 #define DRV_VERSION IWLWIFI_VERSION
107
108 /* Change firmware file name, using "-" and incrementing number,
109 * *only* when uCode interface or architecture changes so that it
110 * is not compatible with earlier drivers.
111 * This number will also appear in << 8 position of 1st dword of uCode file */
112 #define IWL3945_UCODE_API "-1"
113
114 MODULE_DESCRIPTION(DRV_DESCRIPTION);
115 MODULE_VERSION(DRV_VERSION);
116 MODULE_AUTHOR(DRV_COPYRIGHT);
117 MODULE_LICENSE("GPL");
118
119 __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
120 {
121 u16 fc = le16_to_cpu(hdr->frame_control);
122 int hdr_len = ieee80211_get_hdrlen(fc);
123
124 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
125 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
126 return NULL;
127 }
128
129 static const struct ieee80211_hw_mode *iwl_get_hw_mode(
130 struct iwl_priv *priv, int mode)
131 {
132 int i;
133
134 for (i = 0; i < 3; i++)
135 if (priv->modes[i].mode == mode)
136 return &priv->modes[i];
137
138 return NULL;
139 }
140
141 static int iwl_is_empty_essid(const char *essid, int essid_len)
142 {
143 /* Single white space is for Linksys APs */
144 if (essid_len == 1 && essid[0] == ' ')
145 return 1;
146
147 /* Otherwise, if the entire essid is 0, we assume it is hidden */
148 while (essid_len) {
149 essid_len--;
150 if (essid[essid_len] != '\0')
151 return 0;
152 }
153
154 return 1;
155 }
156
157 static const char *iwl_escape_essid(const char *essid, u8 essid_len)
158 {
159 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
160 const char *s = essid;
161 char *d = escaped;
162
163 if (iwl_is_empty_essid(essid, essid_len)) {
164 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
165 return escaped;
166 }
167
168 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
169 while (essid_len--) {
170 if (*s == '\0') {
171 *d++ = '\\';
172 *d++ = '0';
173 s++;
174 } else
175 *d++ = *s++;
176 }
177 *d = '\0';
178 return escaped;
179 }
180
181 static void iwl_print_hex_dump(int level, void *p, u32 len)
182 {
183 #ifdef CONFIG_IWLWIFI_DEBUG
184 if (!(iwl_debug_level & level))
185 return;
186
187 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
188 p, len, 1);
189 #endif
190 }
191
192 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
193 * DMA services
194 *
195 * Theory of operation
196 *
197 * A queue is a circular buffers with 'Read' and 'Write' pointers.
198 * 2 empty entries always kept in the buffer to protect from overflow.
199 *
200 * For Tx queue, there are low mark and high mark limits. If, after queuing
201 * the packet for Tx, free space become < low mark, Tx queue stopped. When
202 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
203 * Tx queue resumed.
204 *
205 * The IWL operates with six queues, one receive queue in the device's
206 * sram, one transmit queue for sending commands to the device firmware,
207 * and four transmit queues for data.
208 ***************************************************/
209
210 static int iwl_queue_space(const struct iwl_queue *q)
211 {
212 int s = q->read_ptr - q->write_ptr;
213
214 if (q->read_ptr > q->write_ptr)
215 s -= q->n_bd;
216
217 if (s <= 0)
218 s += q->n_window;
219 /* keep some reserve to not confuse empty and full situations */
220 s -= 2;
221 if (s < 0)
222 s = 0;
223 return s;
224 }
225
226 /* XXX: n_bd must be power-of-two size */
227 static inline int iwl_queue_inc_wrap(int index, int n_bd)
228 {
229 return ++index & (n_bd - 1);
230 }
231
232 /* XXX: n_bd must be power-of-two size */
233 static inline int iwl_queue_dec_wrap(int index, int n_bd)
234 {
235 return --index & (n_bd - 1);
236 }
237
238 static inline int x2_queue_used(const struct iwl_queue *q, int i)
239 {
240 return q->write_ptr > q->read_ptr ?
241 (i >= q->read_ptr && i < q->write_ptr) :
242 !(i < q->read_ptr && i >= q->write_ptr);
243 }
244
245 static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
246 {
247 if (is_huge)
248 return q->n_window;
249
250 return index & (q->n_window - 1);
251 }
252
253 static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
254 int count, int slots_num, u32 id)
255 {
256 q->n_bd = count;
257 q->n_window = slots_num;
258 q->id = id;
259
260 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
261 * and iwl_queue_dec_wrap are broken. */
262 BUG_ON(!is_power_of_2(count));
263
264 /* slots_num must be power-of-two size, otherwise
265 * get_cmd_index is broken. */
266 BUG_ON(!is_power_of_2(slots_num));
267
268 q->low_mark = q->n_window / 4;
269 if (q->low_mark < 4)
270 q->low_mark = 4;
271
272 q->high_mark = q->n_window / 8;
273 if (q->high_mark < 2)
274 q->high_mark = 2;
275
276 q->write_ptr = q->read_ptr = 0;
277
278 return 0;
279 }
280
281 static int iwl_tx_queue_alloc(struct iwl_priv *priv,
282 struct iwl_tx_queue *txq, u32 id)
283 {
284 struct pci_dev *dev = priv->pci_dev;
285
286 if (id != IWL_CMD_QUEUE_NUM) {
287 txq->txb = kmalloc(sizeof(txq->txb[0]) *
288 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
289 if (!txq->txb) {
290 IWL_ERROR("kmalloc for auxiliary BD "
291 "structures failed\n");
292 goto error;
293 }
294 } else
295 txq->txb = NULL;
296
297 txq->bd = pci_alloc_consistent(dev,
298 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
299 &txq->q.dma_addr);
300
301 if (!txq->bd) {
302 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
303 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
304 goto error;
305 }
306 txq->q.id = id;
307
308 return 0;
309
310 error:
311 if (txq->txb) {
312 kfree(txq->txb);
313 txq->txb = NULL;
314 }
315
316 return -ENOMEM;
317 }
318
319 int iwl_tx_queue_init(struct iwl_priv *priv,
320 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
321 {
322 struct pci_dev *dev = priv->pci_dev;
323 int len;
324 int rc = 0;
325
326 /* allocate command space + one big command for scan since scan
327 * command is very huge the system will not have two scan at the
328 * same time */
329 len = sizeof(struct iwl_cmd) * slots_num;
330 if (txq_id == IWL_CMD_QUEUE_NUM)
331 len += IWL_MAX_SCAN_SIZE;
332 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
333 if (!txq->cmd)
334 return -ENOMEM;
335
336 rc = iwl_tx_queue_alloc(priv, txq, txq_id);
337 if (rc) {
338 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
339
340 return -ENOMEM;
341 }
342 txq->need_update = 0;
343
344 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
345 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
346 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
347 iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
348
349 iwl_hw_tx_queue_init(priv, txq);
350
351 return 0;
352 }
353
354 /**
355 * iwl_tx_queue_free - Deallocate DMA queue.
356 * @txq: Transmit queue to deallocate.
357 *
358 * Empty queue by removing and destroying all BD's.
359 * Free all buffers. txq itself is not freed.
360 *
361 */
362 void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
363 {
364 struct iwl_queue *q = &txq->q;
365 struct pci_dev *dev = priv->pci_dev;
366 int len;
367
368 if (q->n_bd == 0)
369 return;
370
371 /* first, empty all BD's */
372 for (; q->write_ptr != q->read_ptr;
373 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
374 iwl_hw_txq_free_tfd(priv, txq);
375
376 len = sizeof(struct iwl_cmd) * q->n_window;
377 if (q->id == IWL_CMD_QUEUE_NUM)
378 len += IWL_MAX_SCAN_SIZE;
379
380 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
381
382 /* free buffers belonging to queue itself */
383 if (txq->q.n_bd)
384 pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
385 txq->q.n_bd, txq->bd, txq->q.dma_addr);
386
387 if (txq->txb) {
388 kfree(txq->txb);
389 txq->txb = NULL;
390 }
391
392 /* 0 fill whole structure */
393 memset(txq, 0, sizeof(*txq));
394 }
395
396 const u8 BROADCAST_ADDR[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
397
398 /*************** STATION TABLE MANAGEMENT ****
399 *
400 * NOTE: This needs to be overhauled to better synchronize between
401 * how the iwl-4965.c is using iwl_hw_find_station vs. iwl-3945.c
402 *
403 * mac80211 should also be examined to determine if sta_info is duplicating
404 * the functionality provided here
405 */
406
407 /**************************************************************/
408 #if 0 /* temporary disable till we add real remove station */
409 static u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
410 {
411 int index = IWL_INVALID_STATION;
412 int i;
413 unsigned long flags;
414
415 spin_lock_irqsave(&priv->sta_lock, flags);
416
417 if (is_ap)
418 index = IWL_AP_ID;
419 else if (is_broadcast_ether_addr(addr))
420 index = priv->hw_setting.bcast_sta_id;
421 else
422 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
423 if (priv->stations[i].used &&
424 !compare_ether_addr(priv->stations[i].sta.sta.addr,
425 addr)) {
426 index = i;
427 break;
428 }
429
430 if (unlikely(index == IWL_INVALID_STATION))
431 goto out;
432
433 if (priv->stations[index].used) {
434 priv->stations[index].used = 0;
435 priv->num_stations--;
436 }
437
438 BUG_ON(priv->num_stations < 0);
439
440 out:
441 spin_unlock_irqrestore(&priv->sta_lock, flags);
442 return 0;
443 }
444 #endif
445 static void iwl_clear_stations_table(struct iwl_priv *priv)
446 {
447 unsigned long flags;
448
449 spin_lock_irqsave(&priv->sta_lock, flags);
450
451 priv->num_stations = 0;
452 memset(priv->stations, 0, sizeof(priv->stations));
453
454 spin_unlock_irqrestore(&priv->sta_lock, flags);
455 }
456
457
458 u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
459 {
460 int i;
461 int index = IWL_INVALID_STATION;
462 struct iwl_station_entry *station;
463 unsigned long flags_spin;
464 DECLARE_MAC_BUF(mac);
465 u8 rate;
466
467 spin_lock_irqsave(&priv->sta_lock, flags_spin);
468 if (is_ap)
469 index = IWL_AP_ID;
470 else if (is_broadcast_ether_addr(addr))
471 index = priv->hw_setting.bcast_sta_id;
472 else
473 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
474 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
475 addr)) {
476 index = i;
477 break;
478 }
479
480 if (!priv->stations[i].used &&
481 index == IWL_INVALID_STATION)
482 index = i;
483 }
484
485 /* These two conditions has the same outcome but keep them separate
486 since they have different meaning */
487 if (unlikely(index == IWL_INVALID_STATION)) {
488 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
489 return index;
490 }
491
492 if (priv->stations[index].used &&
493 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
494 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
495 return index;
496 }
497
498 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
499 station = &priv->stations[index];
500 station->used = 1;
501 priv->num_stations++;
502
503 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
504 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
505 station->sta.mode = 0;
506 station->sta.sta.sta_id = index;
507 station->sta.station_flags = 0;
508
509 if (priv->phymode == MODE_IEEE80211A)
510 rate = IWL_RATE_6M_PLCP;
511 else
512 rate = IWL_RATE_1M_PLCP;
513
514 /* Turn on both antennas for the station... */
515 station->sta.rate_n_flags =
516 iwl_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
517 station->current_rate.rate_n_flags =
518 le16_to_cpu(station->sta.rate_n_flags);
519
520 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
521 iwl_send_add_station(priv, &station->sta, flags);
522 return index;
523
524 }
525
526 /*************** DRIVER STATUS FUNCTIONS *****/
527
528 static inline int iwl_is_ready(struct iwl_priv *priv)
529 {
530 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
531 * set but EXIT_PENDING is not */
532 return test_bit(STATUS_READY, &priv->status) &&
533 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
534 !test_bit(STATUS_EXIT_PENDING, &priv->status);
535 }
536
537 static inline int iwl_is_alive(struct iwl_priv *priv)
538 {
539 return test_bit(STATUS_ALIVE, &priv->status);
540 }
541
542 static inline int iwl_is_init(struct iwl_priv *priv)
543 {
544 return test_bit(STATUS_INIT, &priv->status);
545 }
546
547 static inline int iwl_is_rfkill(struct iwl_priv *priv)
548 {
549 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
550 test_bit(STATUS_RF_KILL_SW, &priv->status);
551 }
552
553 static inline int iwl_is_ready_rf(struct iwl_priv *priv)
554 {
555
556 if (iwl_is_rfkill(priv))
557 return 0;
558
559 return iwl_is_ready(priv);
560 }
561
562 /*************** HOST COMMAND QUEUE FUNCTIONS *****/
563
564 #define IWL_CMD(x) case x : return #x
565
566 static const char *get_cmd_string(u8 cmd)
567 {
568 switch (cmd) {
569 IWL_CMD(REPLY_ALIVE);
570 IWL_CMD(REPLY_ERROR);
571 IWL_CMD(REPLY_RXON);
572 IWL_CMD(REPLY_RXON_ASSOC);
573 IWL_CMD(REPLY_QOS_PARAM);
574 IWL_CMD(REPLY_RXON_TIMING);
575 IWL_CMD(REPLY_ADD_STA);
576 IWL_CMD(REPLY_REMOVE_STA);
577 IWL_CMD(REPLY_REMOVE_ALL_STA);
578 IWL_CMD(REPLY_3945_RX);
579 IWL_CMD(REPLY_TX);
580 IWL_CMD(REPLY_RATE_SCALE);
581 IWL_CMD(REPLY_LEDS_CMD);
582 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
583 IWL_CMD(RADAR_NOTIFICATION);
584 IWL_CMD(REPLY_QUIET_CMD);
585 IWL_CMD(REPLY_CHANNEL_SWITCH);
586 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
587 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
588 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
589 IWL_CMD(POWER_TABLE_CMD);
590 IWL_CMD(PM_SLEEP_NOTIFICATION);
591 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
592 IWL_CMD(REPLY_SCAN_CMD);
593 IWL_CMD(REPLY_SCAN_ABORT_CMD);
594 IWL_CMD(SCAN_START_NOTIFICATION);
595 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
596 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
597 IWL_CMD(BEACON_NOTIFICATION);
598 IWL_CMD(REPLY_TX_BEACON);
599 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
600 IWL_CMD(QUIET_NOTIFICATION);
601 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
602 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
603 IWL_CMD(REPLY_BT_CONFIG);
604 IWL_CMD(REPLY_STATISTICS_CMD);
605 IWL_CMD(STATISTICS_NOTIFICATION);
606 IWL_CMD(REPLY_CARD_STATE_CMD);
607 IWL_CMD(CARD_STATE_NOTIFICATION);
608 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
609 default:
610 return "UNKNOWN";
611
612 }
613 }
614
615 #define HOST_COMPLETE_TIMEOUT (HZ / 2)
616
617 /**
618 * iwl_enqueue_hcmd - enqueue a uCode command
619 * @priv: device private data point
620 * @cmd: a point to the ucode command structure
621 *
622 * The function returns < 0 values to indicate the operation is
623 * failed. On success, it turns the index (> 0) of command in the
624 * command queue.
625 */
626 static int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
627 {
628 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
629 struct iwl_queue *q = &txq->q;
630 struct iwl_tfd_frame *tfd;
631 u32 *control_flags;
632 struct iwl_cmd *out_cmd;
633 u32 idx;
634 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
635 dma_addr_t phys_addr;
636 int pad;
637 u16 count;
638 int ret;
639 unsigned long flags;
640
641 /* If any of the command structures end up being larger than
642 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
643 * we will need to increase the size of the TFD entries */
644 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
645 !(cmd->meta.flags & CMD_SIZE_HUGE));
646
647 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
648 IWL_ERROR("No space for Tx\n");
649 return -ENOSPC;
650 }
651
652 spin_lock_irqsave(&priv->hcmd_lock, flags);
653
654 tfd = &txq->bd[q->write_ptr];
655 memset(tfd, 0, sizeof(*tfd));
656
657 control_flags = (u32 *) tfd;
658
659 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
660 out_cmd = &txq->cmd[idx];
661
662 out_cmd->hdr.cmd = cmd->id;
663 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
664 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
665
666 /* At this point, the out_cmd now has all of the incoming cmd
667 * information */
668
669 out_cmd->hdr.flags = 0;
670 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
671 INDEX_TO_SEQ(q->write_ptr));
672 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
673 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
674
675 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
676 offsetof(struct iwl_cmd, hdr);
677 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
678
679 pad = U32_PAD(cmd->len);
680 count = TFD_CTL_COUNT_GET(*control_flags);
681 *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
682
683 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
684 "%d bytes at %d[%d]:%d\n",
685 get_cmd_string(out_cmd->hdr.cmd),
686 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
687 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
688
689 txq->need_update = 1;
690 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
691 ret = iwl_tx_queue_update_write_ptr(priv, txq);
692
693 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
694 return ret ? ret : idx;
695 }
696
697 int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
698 {
699 int ret;
700
701 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
702
703 /* An asynchronous command can not expect an SKB to be set. */
704 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
705
706 /* An asynchronous command MUST have a callback. */
707 BUG_ON(!cmd->meta.u.callback);
708
709 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
710 return -EBUSY;
711
712 ret = iwl_enqueue_hcmd(priv, cmd);
713 if (ret < 0) {
714 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
715 get_cmd_string(cmd->id), ret);
716 return ret;
717 }
718 return 0;
719 }
720
721 int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
722 {
723 int cmd_idx;
724 int ret;
725 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
726
727 BUG_ON(cmd->meta.flags & CMD_ASYNC);
728
729 /* A synchronous command can not have a callback set. */
730 BUG_ON(cmd->meta.u.callback != NULL);
731
732 if (atomic_xchg(&entry, 1)) {
733 IWL_ERROR("Error sending %s: Already sending a host command\n",
734 get_cmd_string(cmd->id));
735 return -EBUSY;
736 }
737
738 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
739
740 if (cmd->meta.flags & CMD_WANT_SKB)
741 cmd->meta.source = &cmd->meta;
742
743 cmd_idx = iwl_enqueue_hcmd(priv, cmd);
744 if (cmd_idx < 0) {
745 ret = cmd_idx;
746 IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
747 get_cmd_string(cmd->id), ret);
748 goto out;
749 }
750
751 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
752 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
753 HOST_COMPLETE_TIMEOUT);
754 if (!ret) {
755 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
756 IWL_ERROR("Error sending %s: time out after %dms.\n",
757 get_cmd_string(cmd->id),
758 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
759
760 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
761 ret = -ETIMEDOUT;
762 goto cancel;
763 }
764 }
765
766 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
767 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
768 get_cmd_string(cmd->id));
769 ret = -ECANCELED;
770 goto fail;
771 }
772 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
773 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
774 get_cmd_string(cmd->id));
775 ret = -EIO;
776 goto fail;
777 }
778 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
779 IWL_ERROR("Error: Response NULL in '%s'\n",
780 get_cmd_string(cmd->id));
781 ret = -EIO;
782 goto out;
783 }
784
785 ret = 0;
786 goto out;
787
788 cancel:
789 if (cmd->meta.flags & CMD_WANT_SKB) {
790 struct iwl_cmd *qcmd;
791
792 /* Cancel the CMD_WANT_SKB flag for the cmd in the
793 * TX cmd queue. Otherwise in case the cmd comes
794 * in later, it will possibly set an invalid
795 * address (cmd->meta.source). */
796 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
797 qcmd->meta.flags &= ~CMD_WANT_SKB;
798 }
799 fail:
800 if (cmd->meta.u.skb) {
801 dev_kfree_skb_any(cmd->meta.u.skb);
802 cmd->meta.u.skb = NULL;
803 }
804 out:
805 atomic_set(&entry, 0);
806 return ret;
807 }
808
809 int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
810 {
811 /* A command can not be asynchronous AND expect an SKB to be set. */
812 BUG_ON((cmd->meta.flags & CMD_ASYNC) &&
813 (cmd->meta.flags & CMD_WANT_SKB));
814
815 if (cmd->meta.flags & CMD_ASYNC)
816 return iwl_send_cmd_async(priv, cmd);
817
818 return iwl_send_cmd_sync(priv, cmd);
819 }
820
821 int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
822 {
823 struct iwl_host_cmd cmd = {
824 .id = id,
825 .len = len,
826 .data = data,
827 };
828
829 return iwl_send_cmd_sync(priv, &cmd);
830 }
831
832 static int __must_check iwl_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
833 {
834 struct iwl_host_cmd cmd = {
835 .id = id,
836 .len = sizeof(val),
837 .data = &val,
838 };
839
840 return iwl_send_cmd_sync(priv, &cmd);
841 }
842
843 int iwl_send_statistics_request(struct iwl_priv *priv)
844 {
845 return iwl_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
846 }
847
848 /**
849 * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
850 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
851 * @channel: Any channel valid for the requested phymode
852
853 * In addition to setting the staging RXON, priv->phymode is also set.
854 *
855 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
856 * in the staging RXON flag structure based on the phymode
857 */
858 static int iwl_set_rxon_channel(struct iwl_priv *priv, u8 phymode, u16 channel)
859 {
860 if (!iwl_get_channel_info(priv, phymode, channel)) {
861 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
862 channel, phymode);
863 return -EINVAL;
864 }
865
866 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
867 (priv->phymode == phymode))
868 return 0;
869
870 priv->staging_rxon.channel = cpu_to_le16(channel);
871 if (phymode == MODE_IEEE80211A)
872 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
873 else
874 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
875
876 priv->phymode = phymode;
877
878 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
879
880 return 0;
881 }
882
883 /**
884 * iwl_check_rxon_cmd - validate RXON structure is valid
885 *
886 * NOTE: This is really only useful during development and can eventually
887 * be #ifdef'd out once the driver is stable and folks aren't actively
888 * making changes
889 */
890 static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
891 {
892 int error = 0;
893 int counter = 1;
894
895 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
896 error |= le32_to_cpu(rxon->flags &
897 (RXON_FLG_TGJ_NARROW_BAND_MSK |
898 RXON_FLG_RADAR_DETECT_MSK));
899 if (error)
900 IWL_WARNING("check 24G fields %d | %d\n",
901 counter++, error);
902 } else {
903 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
904 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
905 if (error)
906 IWL_WARNING("check 52 fields %d | %d\n",
907 counter++, error);
908 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
909 if (error)
910 IWL_WARNING("check 52 CCK %d | %d\n",
911 counter++, error);
912 }
913 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
914 if (error)
915 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
916
917 /* make sure basic rates 6Mbps and 1Mbps are supported */
918 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
919 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
920 if (error)
921 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
922
923 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
924 if (error)
925 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
926
927 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
928 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
929 if (error)
930 IWL_WARNING("check CCK and short slot %d | %d\n",
931 counter++, error);
932
933 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
934 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
935 if (error)
936 IWL_WARNING("check CCK & auto detect %d | %d\n",
937 counter++, error);
938
939 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
940 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
941 if (error)
942 IWL_WARNING("check TGG and auto detect %d | %d\n",
943 counter++, error);
944
945 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
946 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
947 RXON_FLG_ANT_A_MSK)) == 0);
948 if (error)
949 IWL_WARNING("check antenna %d %d\n", counter++, error);
950
951 if (error)
952 IWL_WARNING("Tuning to channel %d\n",
953 le16_to_cpu(rxon->channel));
954
955 if (error) {
956 IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
957 return -1;
958 }
959 return 0;
960 }
961
962 /**
963 * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
964 * @priv: staging_rxon is compared to active_rxon
965 *
966 * If the RXON structure is changing sufficient to require a new
967 * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
968 * to indicate a new tune is required.
969 */
970 static int iwl_full_rxon_required(struct iwl_priv *priv)
971 {
972
973 /* These items are only settable from the full RXON command */
974 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
975 compare_ether_addr(priv->staging_rxon.bssid_addr,
976 priv->active_rxon.bssid_addr) ||
977 compare_ether_addr(priv->staging_rxon.node_addr,
978 priv->active_rxon.node_addr) ||
979 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
980 priv->active_rxon.wlap_bssid_addr) ||
981 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
982 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
983 (priv->staging_rxon.air_propagation !=
984 priv->active_rxon.air_propagation) ||
985 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
986 return 1;
987
988 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
989 * be updated with the RXON_ASSOC command -- however only some
990 * flag transitions are allowed using RXON_ASSOC */
991
992 /* Check if we are not switching bands */
993 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
994 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
995 return 1;
996
997 /* Check if we are switching association toggle */
998 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
999 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1000 return 1;
1001
1002 return 0;
1003 }
1004
1005 static int iwl_send_rxon_assoc(struct iwl_priv *priv)
1006 {
1007 int rc = 0;
1008 struct iwl_rx_packet *res = NULL;
1009 struct iwl_rxon_assoc_cmd rxon_assoc;
1010 struct iwl_host_cmd cmd = {
1011 .id = REPLY_RXON_ASSOC,
1012 .len = sizeof(rxon_assoc),
1013 .meta.flags = CMD_WANT_SKB,
1014 .data = &rxon_assoc,
1015 };
1016 const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
1017 const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
1018
1019 if ((rxon1->flags == rxon2->flags) &&
1020 (rxon1->filter_flags == rxon2->filter_flags) &&
1021 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1022 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1023 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1024 return 0;
1025 }
1026
1027 rxon_assoc.flags = priv->staging_rxon.flags;
1028 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1029 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1030 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1031 rxon_assoc.reserved = 0;
1032
1033 rc = iwl_send_cmd_sync(priv, &cmd);
1034 if (rc)
1035 return rc;
1036
1037 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1038 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1039 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1040 rc = -EIO;
1041 }
1042
1043 priv->alloc_rxb_skb--;
1044 dev_kfree_skb_any(cmd.meta.u.skb);
1045
1046 return rc;
1047 }
1048
1049 /**
1050 * iwl_commit_rxon - commit staging_rxon to hardware
1051 *
1052 * The RXON command in staging_rxon is committed to the hardware and
1053 * the active_rxon structure is updated with the new data. This
1054 * function correctly transitions out of the RXON_ASSOC_MSK state if
1055 * a HW tune is required based on the RXON structure changes.
1056 */
1057 static int iwl_commit_rxon(struct iwl_priv *priv)
1058 {
1059 /* cast away the const for active_rxon in this function */
1060 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
1061 int rc = 0;
1062 DECLARE_MAC_BUF(mac);
1063
1064 if (!iwl_is_alive(priv))
1065 return -1;
1066
1067 /* always get timestamp with Rx frame */
1068 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1069
1070 /* select antenna */
1071 priv->staging_rxon.flags &=
1072 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
1073 priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
1074
1075 rc = iwl_check_rxon_cmd(&priv->staging_rxon);
1076 if (rc) {
1077 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1078 return -EINVAL;
1079 }
1080
1081 /* If we don't need to send a full RXON, we can use
1082 * iwl_rxon_assoc_cmd which is used to reconfigure filter
1083 * and other flags for the current radio configuration. */
1084 if (!iwl_full_rxon_required(priv)) {
1085 rc = iwl_send_rxon_assoc(priv);
1086 if (rc) {
1087 IWL_ERROR("Error setting RXON_ASSOC "
1088 "configuration (%d).\n", rc);
1089 return rc;
1090 }
1091
1092 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1093
1094 return 0;
1095 }
1096
1097 /* If we are currently associated and the new config requires
1098 * an RXON_ASSOC and the new config wants the associated mask enabled,
1099 * we must clear the associated from the active configuration
1100 * before we apply the new config */
1101 if (iwl_is_associated(priv) &&
1102 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1103 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1104 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1105
1106 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1107 sizeof(struct iwl_rxon_cmd),
1108 &priv->active_rxon);
1109
1110 /* If the mask clearing failed then we set
1111 * active_rxon back to what it was previously */
1112 if (rc) {
1113 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1114 IWL_ERROR("Error clearing ASSOC_MSK on current "
1115 "configuration (%d).\n", rc);
1116 return rc;
1117 }
1118 }
1119
1120 IWL_DEBUG_INFO("Sending RXON\n"
1121 "* with%s RXON_FILTER_ASSOC_MSK\n"
1122 "* channel = %d\n"
1123 "* bssid = %s\n",
1124 ((priv->staging_rxon.filter_flags &
1125 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1126 le16_to_cpu(priv->staging_rxon.channel),
1127 print_mac(mac, priv->staging_rxon.bssid_addr));
1128
1129 /* Apply the new configuration */
1130 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
1131 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
1132 if (rc) {
1133 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1134 return rc;
1135 }
1136
1137 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1138
1139 iwl_clear_stations_table(priv);
1140
1141 /* If we issue a new RXON command which required a tune then we must
1142 * send a new TXPOWER command or we won't be able to Tx any frames */
1143 rc = iwl_hw_reg_send_txpower(priv);
1144 if (rc) {
1145 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1146 return rc;
1147 }
1148
1149 /* Add the broadcast address so we can send broadcast frames */
1150 if (iwl_add_station(priv, BROADCAST_ADDR, 0, 0) ==
1151 IWL_INVALID_STATION) {
1152 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1153 return -EIO;
1154 }
1155
1156 /* If we have set the ASSOC_MSK and we are in BSS mode then
1157 * add the IWL_AP_ID to the station rate table */
1158 if (iwl_is_associated(priv) &&
1159 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
1160 if (iwl_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
1161 == IWL_INVALID_STATION) {
1162 IWL_ERROR("Error adding AP address for transmit.\n");
1163 return -EIO;
1164 }
1165
1166 /* Init the hardware's rate fallback order based on the
1167 * phymode */
1168 rc = iwl3945_init_hw_rate_table(priv);
1169 if (rc) {
1170 IWL_ERROR("Error setting HW rate table: %02X\n", rc);
1171 return -EIO;
1172 }
1173
1174 return 0;
1175 }
1176
1177 static int iwl_send_bt_config(struct iwl_priv *priv)
1178 {
1179 struct iwl_bt_cmd bt_cmd = {
1180 .flags = 3,
1181 .lead_time = 0xAA,
1182 .max_kill = 1,
1183 .kill_ack_mask = 0,
1184 .kill_cts_mask = 0,
1185 };
1186
1187 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1188 sizeof(struct iwl_bt_cmd), &bt_cmd);
1189 }
1190
1191 static int iwl_send_scan_abort(struct iwl_priv *priv)
1192 {
1193 int rc = 0;
1194 struct iwl_rx_packet *res;
1195 struct iwl_host_cmd cmd = {
1196 .id = REPLY_SCAN_ABORT_CMD,
1197 .meta.flags = CMD_WANT_SKB,
1198 };
1199
1200 /* If there isn't a scan actively going on in the hardware
1201 * then we are in between scan bands and not actually
1202 * actively scanning, so don't send the abort command */
1203 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1204 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1205 return 0;
1206 }
1207
1208 rc = iwl_send_cmd_sync(priv, &cmd);
1209 if (rc) {
1210 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1211 return rc;
1212 }
1213
1214 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1215 if (res->u.status != CAN_ABORT_STATUS) {
1216 /* The scan abort will return 1 for success or
1217 * 2 for "failure". A failure condition can be
1218 * due to simply not being in an active scan which
1219 * can occur if we send the scan abort before we
1220 * the microcode has notified us that a scan is
1221 * completed. */
1222 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1223 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1224 clear_bit(STATUS_SCAN_HW, &priv->status);
1225 }
1226
1227 dev_kfree_skb_any(cmd.meta.u.skb);
1228
1229 return rc;
1230 }
1231
1232 static int iwl_card_state_sync_callback(struct iwl_priv *priv,
1233 struct iwl_cmd *cmd,
1234 struct sk_buff *skb)
1235 {
1236 return 1;
1237 }
1238
1239 /*
1240 * CARD_STATE_CMD
1241 *
1242 * Use: Sets the internal card state to enable, disable, or halt
1243 *
1244 * When in the 'enable' state the card operates as normal.
1245 * When in the 'disable' state, the card enters into a low power mode.
1246 * When in the 'halt' state, the card is shut down and must be fully
1247 * restarted to come back on.
1248 */
1249 static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1250 {
1251 struct iwl_host_cmd cmd = {
1252 .id = REPLY_CARD_STATE_CMD,
1253 .len = sizeof(u32),
1254 .data = &flags,
1255 .meta.flags = meta_flag,
1256 };
1257
1258 if (meta_flag & CMD_ASYNC)
1259 cmd.meta.u.callback = iwl_card_state_sync_callback;
1260
1261 return iwl_send_cmd(priv, &cmd);
1262 }
1263
1264 static int iwl_add_sta_sync_callback(struct iwl_priv *priv,
1265 struct iwl_cmd *cmd, struct sk_buff *skb)
1266 {
1267 struct iwl_rx_packet *res = NULL;
1268
1269 if (!skb) {
1270 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1271 return 1;
1272 }
1273
1274 res = (struct iwl_rx_packet *)skb->data;
1275 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1276 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1277 res->hdr.flags);
1278 return 1;
1279 }
1280
1281 switch (res->u.add_sta.status) {
1282 case ADD_STA_SUCCESS_MSK:
1283 break;
1284 default:
1285 break;
1286 }
1287
1288 /* We didn't cache the SKB; let the caller free it */
1289 return 1;
1290 }
1291
1292 int iwl_send_add_station(struct iwl_priv *priv,
1293 struct iwl_addsta_cmd *sta, u8 flags)
1294 {
1295 struct iwl_rx_packet *res = NULL;
1296 int rc = 0;
1297 struct iwl_host_cmd cmd = {
1298 .id = REPLY_ADD_STA,
1299 .len = sizeof(struct iwl_addsta_cmd),
1300 .meta.flags = flags,
1301 .data = sta,
1302 };
1303
1304 if (flags & CMD_ASYNC)
1305 cmd.meta.u.callback = iwl_add_sta_sync_callback;
1306 else
1307 cmd.meta.flags |= CMD_WANT_SKB;
1308
1309 rc = iwl_send_cmd(priv, &cmd);
1310
1311 if (rc || (flags & CMD_ASYNC))
1312 return rc;
1313
1314 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1315 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1316 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1317 res->hdr.flags);
1318 rc = -EIO;
1319 }
1320
1321 if (rc == 0) {
1322 switch (res->u.add_sta.status) {
1323 case ADD_STA_SUCCESS_MSK:
1324 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1325 break;
1326 default:
1327 rc = -EIO;
1328 IWL_WARNING("REPLY_ADD_STA failed\n");
1329 break;
1330 }
1331 }
1332
1333 priv->alloc_rxb_skb--;
1334 dev_kfree_skb_any(cmd.meta.u.skb);
1335
1336 return rc;
1337 }
1338
1339 static int iwl_update_sta_key_info(struct iwl_priv *priv,
1340 struct ieee80211_key_conf *keyconf,
1341 u8 sta_id)
1342 {
1343 unsigned long flags;
1344 __le16 key_flags = 0;
1345
1346 switch (keyconf->alg) {
1347 case ALG_CCMP:
1348 key_flags |= STA_KEY_FLG_CCMP;
1349 key_flags |= cpu_to_le16(
1350 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1351 key_flags &= ~STA_KEY_FLG_INVALID;
1352 break;
1353 case ALG_TKIP:
1354 case ALG_WEP:
1355 return -EINVAL;
1356 default:
1357 return -EINVAL;
1358 }
1359 spin_lock_irqsave(&priv->sta_lock, flags);
1360 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1361 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1362 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1363 keyconf->keylen);
1364
1365 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1366 keyconf->keylen);
1367 priv->stations[sta_id].sta.key.key_flags = key_flags;
1368 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1369 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1370
1371 spin_unlock_irqrestore(&priv->sta_lock, flags);
1372
1373 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
1374 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1375 return 0;
1376 }
1377
1378 static int iwl_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
1379 {
1380 unsigned long flags;
1381
1382 spin_lock_irqsave(&priv->sta_lock, flags);
1383 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
1384 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl_keyinfo));
1385 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1386 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1387 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1388 spin_unlock_irqrestore(&priv->sta_lock, flags);
1389
1390 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
1391 iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1392 return 0;
1393 }
1394
1395 static void iwl_clear_free_frames(struct iwl_priv *priv)
1396 {
1397 struct list_head *element;
1398
1399 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1400 priv->frames_count);
1401
1402 while (!list_empty(&priv->free_frames)) {
1403 element = priv->free_frames.next;
1404 list_del(element);
1405 kfree(list_entry(element, struct iwl_frame, list));
1406 priv->frames_count--;
1407 }
1408
1409 if (priv->frames_count) {
1410 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1411 priv->frames_count);
1412 priv->frames_count = 0;
1413 }
1414 }
1415
1416 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
1417 {
1418 struct iwl_frame *frame;
1419 struct list_head *element;
1420 if (list_empty(&priv->free_frames)) {
1421 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1422 if (!frame) {
1423 IWL_ERROR("Could not allocate frame!\n");
1424 return NULL;
1425 }
1426
1427 priv->frames_count++;
1428 return frame;
1429 }
1430
1431 element = priv->free_frames.next;
1432 list_del(element);
1433 return list_entry(element, struct iwl_frame, list);
1434 }
1435
1436 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
1437 {
1438 memset(frame, 0, sizeof(*frame));
1439 list_add(&frame->list, &priv->free_frames);
1440 }
1441
1442 unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
1443 struct ieee80211_hdr *hdr,
1444 const u8 *dest, int left)
1445 {
1446
1447 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
1448 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1449 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1450 return 0;
1451
1452 if (priv->ibss_beacon->len > left)
1453 return 0;
1454
1455 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1456
1457 return priv->ibss_beacon->len;
1458 }
1459
1460 static int iwl_rate_index_from_plcp(int plcp)
1461 {
1462 int i = 0;
1463
1464 for (i = 0; i < IWL_RATE_COUNT; i++)
1465 if (iwl_rates[i].plcp == plcp)
1466 return i;
1467 return -1;
1468 }
1469
1470 static u8 iwl_rate_get_lowest_plcp(int rate_mask)
1471 {
1472 u8 i;
1473
1474 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1475 i = iwl_rates[i].next_ieee) {
1476 if (rate_mask & (1 << i))
1477 return iwl_rates[i].plcp;
1478 }
1479
1480 return IWL_RATE_INVALID;
1481 }
1482
1483 static int iwl_send_beacon_cmd(struct iwl_priv *priv)
1484 {
1485 struct iwl_frame *frame;
1486 unsigned int frame_size;
1487 int rc;
1488 u8 rate;
1489
1490 frame = iwl_get_free_frame(priv);
1491
1492 if (!frame) {
1493 IWL_ERROR("Could not obtain free frame buffer for beacon "
1494 "command.\n");
1495 return -ENOMEM;
1496 }
1497
1498 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1499 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic &
1500 0xFF0);
1501 if (rate == IWL_INVALID_RATE)
1502 rate = IWL_RATE_6M_PLCP;
1503 } else {
1504 rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
1505 if (rate == IWL_INVALID_RATE)
1506 rate = IWL_RATE_1M_PLCP;
1507 }
1508
1509 frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
1510
1511 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1512 &frame->u.cmd[0]);
1513
1514 iwl_free_frame(priv, frame);
1515
1516 return rc;
1517 }
1518
1519 /******************************************************************************
1520 *
1521 * EEPROM related functions
1522 *
1523 ******************************************************************************/
1524
1525 static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
1526 {
1527 memcpy(mac, priv->eeprom.mac_address, 6);
1528 }
1529
1530 /**
1531 * iwl_eeprom_init - read EEPROM contents
1532 *
1533 * Load the EEPROM from adapter into priv->eeprom
1534 *
1535 * NOTE: This routine uses the non-debug IO access functions.
1536 */
1537 int iwl_eeprom_init(struct iwl_priv *priv)
1538 {
1539 u16 *e = (u16 *)&priv->eeprom;
1540 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
1541 u32 r;
1542 int sz = sizeof(priv->eeprom);
1543 int rc;
1544 int i;
1545 u16 addr;
1546
1547 /* The EEPROM structure has several padding buffers within it
1548 * and when adding new EEPROM maps is subject to programmer errors
1549 * which may be very difficult to identify without explicitly
1550 * checking the resulting size of the eeprom map. */
1551 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1552
1553 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1554 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1555 return -ENOENT;
1556 }
1557
1558 rc = iwl_eeprom_acquire_semaphore(priv);
1559 if (rc < 0) {
1560 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
1561 return -ENOENT;
1562 }
1563
1564 /* eeprom is an array of 16bit values */
1565 for (addr = 0; addr < sz; addr += sizeof(u16)) {
1566 _iwl_write32(priv, CSR_EEPROM_REG, addr << 1);
1567 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1568
1569 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1570 i += IWL_EEPROM_ACCESS_DELAY) {
1571 r = _iwl_read_restricted(priv, CSR_EEPROM_REG);
1572 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1573 break;
1574 udelay(IWL_EEPROM_ACCESS_DELAY);
1575 }
1576
1577 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1578 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1579 return -ETIMEDOUT;
1580 }
1581 e[addr / 2] = le16_to_cpu(r >> 16);
1582 }
1583
1584 return 0;
1585 }
1586
1587 /******************************************************************************
1588 *
1589 * Misc. internal state and helper functions
1590 *
1591 ******************************************************************************/
1592 #ifdef CONFIG_IWLWIFI_DEBUG
1593
1594 /**
1595 * iwl_report_frame - dump frame to syslog during debug sessions
1596 *
1597 * hack this function to show different aspects of received frames,
1598 * including selective frame dumps.
1599 * group100 parameter selects whether to show 1 out of 100 good frames.
1600 *
1601 * TODO: ieee80211_hdr stuff is common to 3945 and 4965, so frame type
1602 * info output is okay, but some of this stuff (e.g. iwl_rx_frame_stats)
1603 * is 3945-specific and gives bad output for 4965. Need to split the
1604 * functionality, keep common stuff here.
1605 */
1606 void iwl_report_frame(struct iwl_priv *priv,
1607 struct iwl_rx_packet *pkt,
1608 struct ieee80211_hdr *header, int group100)
1609 {
1610 u32 to_us;
1611 u32 print_summary = 0;
1612 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1613 u32 hundred = 0;
1614 u32 dataframe = 0;
1615 u16 fc;
1616 u16 seq_ctl;
1617 u16 channel;
1618 u16 phy_flags;
1619 int rate_sym;
1620 u16 length;
1621 u16 status;
1622 u16 bcn_tmr;
1623 u32 tsf_low;
1624 u64 tsf;
1625 u8 rssi;
1626 u8 agc;
1627 u16 sig_avg;
1628 u16 noise_diff;
1629 struct iwl_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1630 struct iwl_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1631 struct iwl_rx_frame_end *rx_end = IWL_RX_END(pkt);
1632 u8 *data = IWL_RX_DATA(pkt);
1633
1634 /* MAC header */
1635 fc = le16_to_cpu(header->frame_control);
1636 seq_ctl = le16_to_cpu(header->seq_ctrl);
1637
1638 /* metadata */
1639 channel = le16_to_cpu(rx_hdr->channel);
1640 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1641 rate_sym = rx_hdr->rate;
1642 length = le16_to_cpu(rx_hdr->len);
1643
1644 /* end-of-frame status and timestamp */
1645 status = le32_to_cpu(rx_end->status);
1646 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1647 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1648 tsf = le64_to_cpu(rx_end->timestamp);
1649
1650 /* signal statistics */
1651 rssi = rx_stats->rssi;
1652 agc = rx_stats->agc;
1653 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1654 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1655
1656 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1657
1658 /* if data frame is to us and all is good,
1659 * (optionally) print summary for only 1 out of every 100 */
1660 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1661 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1662 dataframe = 1;
1663 if (!group100)
1664 print_summary = 1; /* print each frame */
1665 else if (priv->framecnt_to_us < 100) {
1666 priv->framecnt_to_us++;
1667 print_summary = 0;
1668 } else {
1669 priv->framecnt_to_us = 0;
1670 print_summary = 1;
1671 hundred = 1;
1672 }
1673 } else {
1674 /* print summary for all other frames */
1675 print_summary = 1;
1676 }
1677
1678 if (print_summary) {
1679 char *title;
1680 u32 rate;
1681
1682 if (hundred)
1683 title = "100Frames";
1684 else if (fc & IEEE80211_FCTL_RETRY)
1685 title = "Retry";
1686 else if (ieee80211_is_assoc_response(fc))
1687 title = "AscRsp";
1688 else if (ieee80211_is_reassoc_response(fc))
1689 title = "RasRsp";
1690 else if (ieee80211_is_probe_response(fc)) {
1691 title = "PrbRsp";
1692 print_dump = 1; /* dump frame contents */
1693 } else if (ieee80211_is_beacon(fc)) {
1694 title = "Beacon";
1695 print_dump = 1; /* dump frame contents */
1696 } else if (ieee80211_is_atim(fc))
1697 title = "ATIM";
1698 else if (ieee80211_is_auth(fc))
1699 title = "Auth";
1700 else if (ieee80211_is_deauth(fc))
1701 title = "DeAuth";
1702 else if (ieee80211_is_disassoc(fc))
1703 title = "DisAssoc";
1704 else
1705 title = "Frame";
1706
1707 rate = iwl_rate_index_from_plcp(rate_sym);
1708 if (rate == -1)
1709 rate = 0;
1710 else
1711 rate = iwl_rates[rate].ieee / 2;
1712
1713 /* print frame summary.
1714 * MAC addresses show just the last byte (for brevity),
1715 * but you can hack it to show more, if you'd like to. */
1716 if (dataframe)
1717 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1718 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1719 title, fc, header->addr1[5],
1720 length, rssi, channel, rate);
1721 else {
1722 /* src/dst addresses assume managed mode */
1723 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1724 "src=0x%02x, rssi=%u, tim=%lu usec, "
1725 "phy=0x%02x, chnl=%d\n",
1726 title, fc, header->addr1[5],
1727 header->addr3[5], rssi,
1728 tsf_low - priv->scan_start_tsf,
1729 phy_flags, channel);
1730 }
1731 }
1732 if (print_dump)
1733 iwl_print_hex_dump(IWL_DL_RX, data, length);
1734 }
1735 #endif
1736
1737 static void iwl_unset_hw_setting(struct iwl_priv *priv)
1738 {
1739 if (priv->hw_setting.shared_virt)
1740 pci_free_consistent(priv->pci_dev,
1741 sizeof(struct iwl_shared),
1742 priv->hw_setting.shared_virt,
1743 priv->hw_setting.shared_phys);
1744 }
1745
1746 /**
1747 * iwl_supported_rate_to_ie - fill in the supported rate in IE field
1748 *
1749 * return : set the bit for each supported rate insert in ie
1750 */
1751 static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1752 u16 basic_rate, int *left)
1753 {
1754 u16 ret_rates = 0, bit;
1755 int i;
1756 u8 *cnt = ie;
1757 u8 *rates = ie + 1;
1758
1759 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1760 if (bit & supported_rate) {
1761 ret_rates |= bit;
1762 rates[*cnt] = iwl_rates[i].ieee |
1763 ((bit & basic_rate) ? 0x80 : 0x00);
1764 (*cnt)++;
1765 (*left)--;
1766 if ((*left <= 0) ||
1767 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1768 break;
1769 }
1770 }
1771
1772 return ret_rates;
1773 }
1774
1775 /**
1776 * iwl_fill_probe_req - fill in all required fields and IE for probe request
1777 */
1778 static u16 iwl_fill_probe_req(struct iwl_priv *priv,
1779 struct ieee80211_mgmt *frame,
1780 int left, int is_direct)
1781 {
1782 int len = 0;
1783 u8 *pos = NULL;
1784 u16 active_rates, ret_rates, cck_rates;
1785
1786 /* Make sure there is enough space for the probe request,
1787 * two mandatory IEs and the data */
1788 left -= 24;
1789 if (left < 0)
1790 return 0;
1791 len += 24;
1792
1793 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1794 memcpy(frame->da, BROADCAST_ADDR, ETH_ALEN);
1795 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1796 memcpy(frame->bssid, BROADCAST_ADDR, ETH_ALEN);
1797 frame->seq_ctrl = 0;
1798
1799 /* fill in our indirect SSID IE */
1800 /* ...next IE... */
1801
1802 left -= 2;
1803 if (left < 0)
1804 return 0;
1805 len += 2;
1806 pos = &(frame->u.probe_req.variable[0]);
1807 *pos++ = WLAN_EID_SSID;
1808 *pos++ = 0;
1809
1810 /* fill in our direct SSID IE... */
1811 if (is_direct) {
1812 /* ...next IE... */
1813 left -= 2 + priv->essid_len;
1814 if (left < 0)
1815 return 0;
1816 /* ... fill it in... */
1817 *pos++ = WLAN_EID_SSID;
1818 *pos++ = priv->essid_len;
1819 memcpy(pos, priv->essid, priv->essid_len);
1820 pos += priv->essid_len;
1821 len += 2 + priv->essid_len;
1822 }
1823
1824 /* fill in supported rate */
1825 /* ...next IE... */
1826 left -= 2;
1827 if (left < 0)
1828 return 0;
1829
1830 /* ... fill it in... */
1831 *pos++ = WLAN_EID_SUPP_RATES;
1832 *pos = 0;
1833
1834 priv->active_rate = priv->rates_mask;
1835 active_rates = priv->active_rate;
1836 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1837
1838 cck_rates = IWL_CCK_RATES_MASK & active_rates;
1839 ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
1840 priv->active_rate_basic, &left);
1841 active_rates &= ~ret_rates;
1842
1843 ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
1844 priv->active_rate_basic, &left);
1845 active_rates &= ~ret_rates;
1846
1847 len += 2 + *pos;
1848 pos += (*pos) + 1;
1849 if (active_rates == 0)
1850 goto fill_end;
1851
1852 /* fill in supported extended rate */
1853 /* ...next IE... */
1854 left -= 2;
1855 if (left < 0)
1856 return 0;
1857 /* ... fill it in... */
1858 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1859 *pos = 0;
1860 iwl_supported_rate_to_ie(pos, active_rates,
1861 priv->active_rate_basic, &left);
1862 if (*pos > 0)
1863 len += 2 + *pos;
1864
1865 fill_end:
1866 return (u16)len;
1867 }
1868
1869 /*
1870 * QoS support
1871 */
1872 #ifdef CONFIG_IWLWIFI_QOS
1873 static int iwl_send_qos_params_command(struct iwl_priv *priv,
1874 struct iwl_qosparam_cmd *qos)
1875 {
1876
1877 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1878 sizeof(struct iwl_qosparam_cmd), qos);
1879 }
1880
1881 static void iwl_reset_qos(struct iwl_priv *priv)
1882 {
1883 u16 cw_min = 15;
1884 u16 cw_max = 1023;
1885 u8 aifs = 2;
1886 u8 is_legacy = 0;
1887 unsigned long flags;
1888 int i;
1889
1890 spin_lock_irqsave(&priv->lock, flags);
1891 priv->qos_data.qos_active = 0;
1892
1893 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1894 if (priv->qos_data.qos_enable)
1895 priv->qos_data.qos_active = 1;
1896 if (!(priv->active_rate & 0xfff0)) {
1897 cw_min = 31;
1898 is_legacy = 1;
1899 }
1900 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1901 if (priv->qos_data.qos_enable)
1902 priv->qos_data.qos_active = 1;
1903 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1904 cw_min = 31;
1905 is_legacy = 1;
1906 }
1907
1908 if (priv->qos_data.qos_active)
1909 aifs = 3;
1910
1911 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1912 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1913 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1914 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1915 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1916
1917 if (priv->qos_data.qos_active) {
1918 i = 1;
1919 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1920 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1921 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1922 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1923 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1924
1925 i = 2;
1926 priv->qos_data.def_qos_parm.ac[i].cw_min =
1927 cpu_to_le16((cw_min + 1) / 2 - 1);
1928 priv->qos_data.def_qos_parm.ac[i].cw_max =
1929 cpu_to_le16(cw_max);
1930 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1931 if (is_legacy)
1932 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1933 cpu_to_le16(6016);
1934 else
1935 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1936 cpu_to_le16(3008);
1937 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1938
1939 i = 3;
1940 priv->qos_data.def_qos_parm.ac[i].cw_min =
1941 cpu_to_le16((cw_min + 1) / 4 - 1);
1942 priv->qos_data.def_qos_parm.ac[i].cw_max =
1943 cpu_to_le16((cw_max + 1) / 2 - 1);
1944 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1945 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1946 if (is_legacy)
1947 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1948 cpu_to_le16(3264);
1949 else
1950 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1951 cpu_to_le16(1504);
1952 } else {
1953 for (i = 1; i < 4; i++) {
1954 priv->qos_data.def_qos_parm.ac[i].cw_min =
1955 cpu_to_le16(cw_min);
1956 priv->qos_data.def_qos_parm.ac[i].cw_max =
1957 cpu_to_le16(cw_max);
1958 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1959 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1960 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1961 }
1962 }
1963 IWL_DEBUG_QOS("set QoS to default \n");
1964
1965 spin_unlock_irqrestore(&priv->lock, flags);
1966 }
1967
1968 static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
1969 {
1970 unsigned long flags;
1971
1972 if (priv == NULL)
1973 return;
1974
1975 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1976 return;
1977
1978 if (!priv->qos_data.qos_enable)
1979 return;
1980
1981 spin_lock_irqsave(&priv->lock, flags);
1982 priv->qos_data.def_qos_parm.qos_flags = 0;
1983
1984 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1985 !priv->qos_data.qos_cap.q_AP.txop_request)
1986 priv->qos_data.def_qos_parm.qos_flags |=
1987 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1988
1989 if (priv->qos_data.qos_active)
1990 priv->qos_data.def_qos_parm.qos_flags |=
1991 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1992
1993 spin_unlock_irqrestore(&priv->lock, flags);
1994
1995 if (force || iwl_is_associated(priv)) {
1996 IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
1997 priv->qos_data.qos_active);
1998
1999 iwl_send_qos_params_command(priv,
2000 &(priv->qos_data.def_qos_parm));
2001 }
2002 }
2003
2004 #endif /* CONFIG_IWLWIFI_QOS */
2005 /*
2006 * Power management (not Tx power!) functions
2007 */
2008 #define MSEC_TO_USEC 1024
2009
2010 #define NOSLP __constant_cpu_to_le32(0)
2011 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
2012 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2013 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2014 __constant_cpu_to_le32(X1), \
2015 __constant_cpu_to_le32(X2), \
2016 __constant_cpu_to_le32(X3), \
2017 __constant_cpu_to_le32(X4)}
2018
2019
2020 /* default power management (not Tx power) table values */
2021 /* for tim 0-10 */
2022 static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
2023 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2024 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2025 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2026 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2027 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2028 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2029 };
2030
2031 /* for tim > 10 */
2032 static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
2033 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2034 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2035 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2036 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2037 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2038 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2039 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2040 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2041 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2042 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2043 };
2044
2045 int iwl_power_init_handle(struct iwl_priv *priv)
2046 {
2047 int rc = 0, i;
2048 struct iwl_power_mgr *pow_data;
2049 int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
2050 u16 pci_pm;
2051
2052 IWL_DEBUG_POWER("Initialize power \n");
2053
2054 pow_data = &(priv->power_data);
2055
2056 memset(pow_data, 0, sizeof(*pow_data));
2057
2058 pow_data->active_index = IWL_POWER_RANGE_0;
2059 pow_data->dtim_val = 0xffff;
2060
2061 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2062 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2063
2064 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2065 if (rc != 0)
2066 return 0;
2067 else {
2068 struct iwl_powertable_cmd *cmd;
2069
2070 IWL_DEBUG_POWER("adjust power command flags\n");
2071
2072 for (i = 0; i < IWL_POWER_AC; i++) {
2073 cmd = &pow_data->pwr_range_0[i].cmd;
2074
2075 if (pci_pm & 0x1)
2076 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2077 else
2078 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2079 }
2080 }
2081 return rc;
2082 }
2083
2084 static int iwl_update_power_cmd(struct iwl_priv *priv,
2085 struct iwl_powertable_cmd *cmd, u32 mode)
2086 {
2087 int rc = 0, i;
2088 u8 skip;
2089 u32 max_sleep = 0;
2090 struct iwl_power_vec_entry *range;
2091 u8 period = 0;
2092 struct iwl_power_mgr *pow_data;
2093
2094 if (mode > IWL_POWER_INDEX_5) {
2095 IWL_DEBUG_POWER("Error invalid power mode \n");
2096 return -1;
2097 }
2098 pow_data = &(priv->power_data);
2099
2100 if (pow_data->active_index == IWL_POWER_RANGE_0)
2101 range = &pow_data->pwr_range_0[0];
2102 else
2103 range = &pow_data->pwr_range_1[1];
2104
2105 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
2106
2107 #ifdef IWL_MAC80211_DISABLE
2108 if (priv->assoc_network != NULL) {
2109 unsigned long flags;
2110
2111 period = priv->assoc_network->tim.tim_period;
2112 }
2113 #endif /*IWL_MAC80211_DISABLE */
2114 skip = range[mode].no_dtim;
2115
2116 if (period == 0) {
2117 period = 1;
2118 skip = 0;
2119 }
2120
2121 if (skip == 0) {
2122 max_sleep = period;
2123 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2124 } else {
2125 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2126 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2127 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2128 }
2129
2130 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2131 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2132 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2133 }
2134
2135 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2136 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2137 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2138 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2139 le32_to_cpu(cmd->sleep_interval[0]),
2140 le32_to_cpu(cmd->sleep_interval[1]),
2141 le32_to_cpu(cmd->sleep_interval[2]),
2142 le32_to_cpu(cmd->sleep_interval[3]),
2143 le32_to_cpu(cmd->sleep_interval[4]));
2144
2145 return rc;
2146 }
2147
2148 static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
2149 {
2150 u32 final_mode = mode;
2151 int rc;
2152 struct iwl_powertable_cmd cmd;
2153
2154 /* If on battery, set to 3,
2155 * if plugged into AC power, set to CAM ("continuously aware mode"),
2156 * else user level */
2157 switch (mode) {
2158 case IWL_POWER_BATTERY:
2159 final_mode = IWL_POWER_INDEX_3;
2160 break;
2161 case IWL_POWER_AC:
2162 final_mode = IWL_POWER_MODE_CAM;
2163 break;
2164 default:
2165 final_mode = mode;
2166 break;
2167 }
2168
2169 iwl_update_power_cmd(priv, &cmd, final_mode);
2170
2171 rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2172
2173 if (final_mode == IWL_POWER_MODE_CAM)
2174 clear_bit(STATUS_POWER_PMI, &priv->status);
2175 else
2176 set_bit(STATUS_POWER_PMI, &priv->status);
2177
2178 return rc;
2179 }
2180
2181 int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2182 {
2183 /* Filter incoming packets to determine if they are targeted toward
2184 * this network, discarding packets coming from ourselves */
2185 switch (priv->iw_mode) {
2186 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2187 /* packets from our adapter are dropped (echo) */
2188 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2189 return 0;
2190 /* {broad,multi}cast packets to our IBSS go through */
2191 if (is_multicast_ether_addr(header->addr1))
2192 return !compare_ether_addr(header->addr3, priv->bssid);
2193 /* packets to our adapter go through */
2194 return !compare_ether_addr(header->addr1, priv->mac_addr);
2195 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2196 /* packets from our adapter are dropped (echo) */
2197 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2198 return 0;
2199 /* {broad,multi}cast packets to our BSS go through */
2200 if (is_multicast_ether_addr(header->addr1))
2201 return !compare_ether_addr(header->addr2, priv->bssid);
2202 /* packets to our adapter go through */
2203 return !compare_ether_addr(header->addr1, priv->mac_addr);
2204 }
2205
2206 return 1;
2207 }
2208
2209 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2210
2211 const char *iwl_get_tx_fail_reason(u32 status)
2212 {
2213 switch (status & TX_STATUS_MSK) {
2214 case TX_STATUS_SUCCESS:
2215 return "SUCCESS";
2216 TX_STATUS_ENTRY(SHORT_LIMIT);
2217 TX_STATUS_ENTRY(LONG_LIMIT);
2218 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2219 TX_STATUS_ENTRY(MGMNT_ABORT);
2220 TX_STATUS_ENTRY(NEXT_FRAG);
2221 TX_STATUS_ENTRY(LIFE_EXPIRE);
2222 TX_STATUS_ENTRY(DEST_PS);
2223 TX_STATUS_ENTRY(ABORTED);
2224 TX_STATUS_ENTRY(BT_RETRY);
2225 TX_STATUS_ENTRY(STA_INVALID);
2226 TX_STATUS_ENTRY(FRAG_DROPPED);
2227 TX_STATUS_ENTRY(TID_DISABLE);
2228 TX_STATUS_ENTRY(FRAME_FLUSHED);
2229 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2230 TX_STATUS_ENTRY(TX_LOCKED);
2231 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2232 }
2233
2234 return "UNKNOWN";
2235 }
2236
2237 /**
2238 * iwl_scan_cancel - Cancel any currently executing HW scan
2239 *
2240 * NOTE: priv->mutex is not required before calling this function
2241 */
2242 static int iwl_scan_cancel(struct iwl_priv *priv)
2243 {
2244 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2245 clear_bit(STATUS_SCANNING, &priv->status);
2246 return 0;
2247 }
2248
2249 if (test_bit(STATUS_SCANNING, &priv->status)) {
2250 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2251 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2252 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2253 queue_work(priv->workqueue, &priv->abort_scan);
2254
2255 } else
2256 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2257
2258 return test_bit(STATUS_SCANNING, &priv->status);
2259 }
2260
2261 return 0;
2262 }
2263
2264 /**
2265 * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
2266 * @ms: amount of time to wait (in milliseconds) for scan to abort
2267 *
2268 * NOTE: priv->mutex must be held before calling this function
2269 */
2270 static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
2271 {
2272 unsigned long now = jiffies;
2273 int ret;
2274
2275 ret = iwl_scan_cancel(priv);
2276 if (ret && ms) {
2277 mutex_unlock(&priv->mutex);
2278 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2279 test_bit(STATUS_SCANNING, &priv->status))
2280 msleep(1);
2281 mutex_lock(&priv->mutex);
2282
2283 return test_bit(STATUS_SCANNING, &priv->status);
2284 }
2285
2286 return ret;
2287 }
2288
2289 static void iwl_sequence_reset(struct iwl_priv *priv)
2290 {
2291 /* Reset ieee stats */
2292
2293 /* We don't reset the net_device_stats (ieee->stats) on
2294 * re-association */
2295
2296 priv->last_seq_num = -1;
2297 priv->last_frag_num = -1;
2298 priv->last_packet_time = 0;
2299
2300 iwl_scan_cancel(priv);
2301 }
2302
2303 #define MAX_UCODE_BEACON_INTERVAL 1024
2304 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2305
2306 static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
2307 {
2308 u16 new_val = 0;
2309 u16 beacon_factor = 0;
2310
2311 beacon_factor =
2312 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2313 / MAX_UCODE_BEACON_INTERVAL;
2314 new_val = beacon_val / beacon_factor;
2315
2316 return cpu_to_le16(new_val);
2317 }
2318
2319 static void iwl_setup_rxon_timing(struct iwl_priv *priv)
2320 {
2321 u64 interval_tm_unit;
2322 u64 tsf, result;
2323 unsigned long flags;
2324 struct ieee80211_conf *conf = NULL;
2325 u16 beacon_int = 0;
2326
2327 conf = ieee80211_get_hw_conf(priv->hw);
2328
2329 spin_lock_irqsave(&priv->lock, flags);
2330 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2331 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2332
2333 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2334
2335 tsf = priv->timestamp1;
2336 tsf = ((tsf << 32) | priv->timestamp0);
2337
2338 beacon_int = priv->beacon_int;
2339 spin_unlock_irqrestore(&priv->lock, flags);
2340
2341 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2342 if (beacon_int == 0) {
2343 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2344 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2345 } else {
2346 priv->rxon_timing.beacon_interval =
2347 cpu_to_le16(beacon_int);
2348 priv->rxon_timing.beacon_interval =
2349 iwl_adjust_beacon_interval(
2350 le16_to_cpu(priv->rxon_timing.beacon_interval));
2351 }
2352
2353 priv->rxon_timing.atim_window = 0;
2354 } else {
2355 priv->rxon_timing.beacon_interval =
2356 iwl_adjust_beacon_interval(conf->beacon_int);
2357 /* TODO: we need to get atim_window from upper stack
2358 * for now we set to 0 */
2359 priv->rxon_timing.atim_window = 0;
2360 }
2361
2362 interval_tm_unit =
2363 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2364 result = do_div(tsf, interval_tm_unit);
2365 priv->rxon_timing.beacon_init_val =
2366 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2367
2368 IWL_DEBUG_ASSOC
2369 ("beacon interval %d beacon timer %d beacon tim %d\n",
2370 le16_to_cpu(priv->rxon_timing.beacon_interval),
2371 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2372 le16_to_cpu(priv->rxon_timing.atim_window));
2373 }
2374
2375 static int iwl_scan_initiate(struct iwl_priv *priv)
2376 {
2377 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2378 IWL_ERROR("APs don't scan.\n");
2379 return 0;
2380 }
2381
2382 if (!iwl_is_ready_rf(priv)) {
2383 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2384 return -EIO;
2385 }
2386
2387 if (test_bit(STATUS_SCANNING, &priv->status)) {
2388 IWL_DEBUG_SCAN("Scan already in progress.\n");
2389 return -EAGAIN;
2390 }
2391
2392 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2393 IWL_DEBUG_SCAN("Scan request while abort pending. "
2394 "Queuing.\n");
2395 return -EAGAIN;
2396 }
2397
2398 IWL_DEBUG_INFO("Starting scan...\n");
2399 priv->scan_bands = 2;
2400 set_bit(STATUS_SCANNING, &priv->status);
2401 priv->scan_start = jiffies;
2402 priv->scan_pass_start = priv->scan_start;
2403
2404 queue_work(priv->workqueue, &priv->request_scan);
2405
2406 return 0;
2407 }
2408
2409 static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
2410 {
2411 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2412
2413 if (hw_decrypt)
2414 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2415 else
2416 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2417
2418 return 0;
2419 }
2420
2421 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
2422 {
2423 if (phymode == MODE_IEEE80211A) {
2424 priv->staging_rxon.flags &=
2425 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2426 | RXON_FLG_CCK_MSK);
2427 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2428 } else {
2429 /* Copied from iwl_bg_post_associate() */
2430 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2431 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2432 else
2433 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2434
2435 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2436 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2437
2438 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2439 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2440 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2441 }
2442 }
2443
2444 /*
2445 * initialize rxon structure with default values from eeprom
2446 */
2447 static void iwl_connection_init_rx_config(struct iwl_priv *priv)
2448 {
2449 const struct iwl_channel_info *ch_info;
2450
2451 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2452
2453 switch (priv->iw_mode) {
2454 case IEEE80211_IF_TYPE_AP:
2455 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2456 break;
2457
2458 case IEEE80211_IF_TYPE_STA:
2459 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2460 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2461 break;
2462
2463 case IEEE80211_IF_TYPE_IBSS:
2464 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2465 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2466 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2467 RXON_FILTER_ACCEPT_GRP_MSK;
2468 break;
2469
2470 case IEEE80211_IF_TYPE_MNTR:
2471 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2472 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2473 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2474 break;
2475 }
2476
2477 #if 0
2478 /* TODO: Figure out when short_preamble would be set and cache from
2479 * that */
2480 if (!hw_to_local(priv->hw)->short_preamble)
2481 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2482 else
2483 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2484 #endif
2485
2486 ch_info = iwl_get_channel_info(priv, priv->phymode,
2487 le16_to_cpu(priv->staging_rxon.channel));
2488
2489 if (!ch_info)
2490 ch_info = &priv->channel_info[0];
2491
2492 /*
2493 * in some case A channels are all non IBSS
2494 * in this case force B/G channel
2495 */
2496 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2497 !(is_channel_ibss(ch_info)))
2498 ch_info = &priv->channel_info[0];
2499
2500 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2501 if (is_channel_a_band(ch_info))
2502 priv->phymode = MODE_IEEE80211A;
2503 else
2504 priv->phymode = MODE_IEEE80211G;
2505
2506 iwl_set_flags_for_phymode(priv, priv->phymode);
2507
2508 priv->staging_rxon.ofdm_basic_rates =
2509 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2510 priv->staging_rxon.cck_basic_rates =
2511 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2512 }
2513
2514 static int iwl_set_mode(struct iwl_priv *priv, int mode)
2515 {
2516 if (!iwl_is_ready_rf(priv))
2517 return -EAGAIN;
2518
2519 if (mode == IEEE80211_IF_TYPE_IBSS) {
2520 const struct iwl_channel_info *ch_info;
2521
2522 ch_info = iwl_get_channel_info(priv,
2523 priv->phymode,
2524 le16_to_cpu(priv->staging_rxon.channel));
2525
2526 if (!ch_info || !is_channel_ibss(ch_info)) {
2527 IWL_ERROR("channel %d not IBSS channel\n",
2528 le16_to_cpu(priv->staging_rxon.channel));
2529 return -EINVAL;
2530 }
2531 }
2532
2533 cancel_delayed_work(&priv->scan_check);
2534 if (iwl_scan_cancel_timeout(priv, 100)) {
2535 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2536 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2537 return -EAGAIN;
2538 }
2539
2540 priv->iw_mode = mode;
2541
2542 iwl_connection_init_rx_config(priv);
2543 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2544
2545 iwl_clear_stations_table(priv);
2546
2547 iwl_commit_rxon(priv);
2548
2549 return 0;
2550 }
2551
2552 static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
2553 struct ieee80211_tx_control *ctl,
2554 struct iwl_cmd *cmd,
2555 struct sk_buff *skb_frag,
2556 int last_frag)
2557 {
2558 struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2559
2560 switch (keyinfo->alg) {
2561 case ALG_CCMP:
2562 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2563 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2564 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2565 break;
2566
2567 case ALG_TKIP:
2568 #if 0
2569 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2570
2571 if (last_frag)
2572 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2573 8);
2574 else
2575 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2576 #endif
2577 break;
2578
2579 case ALG_WEP:
2580 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2581 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2582
2583 if (keyinfo->keylen == 13)
2584 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2585
2586 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2587
2588 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2589 "with key %d\n", ctl->key_idx);
2590 break;
2591
2592 default:
2593 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2594 break;
2595 }
2596 }
2597
2598 /*
2599 * handle build REPLY_TX command notification.
2600 */
2601 static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
2602 struct iwl_cmd *cmd,
2603 struct ieee80211_tx_control *ctrl,
2604 struct ieee80211_hdr *hdr,
2605 int is_unicast, u8 std_id)
2606 {
2607 __le16 *qc;
2608 u16 fc = le16_to_cpu(hdr->frame_control);
2609 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2610
2611 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2612 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2613 tx_flags |= TX_CMD_FLG_ACK_MSK;
2614 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2615 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2616 if (ieee80211_is_probe_response(fc) &&
2617 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2618 tx_flags |= TX_CMD_FLG_TSF_MSK;
2619 } else {
2620 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2621 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2622 }
2623
2624 cmd->cmd.tx.sta_id = std_id;
2625 if (ieee80211_get_morefrag(hdr))
2626 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2627
2628 qc = ieee80211_get_qos_ctrl(hdr);
2629 if (qc) {
2630 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2631 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2632 } else
2633 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2634
2635 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2636 tx_flags |= TX_CMD_FLG_RTS_MSK;
2637 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2638 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2639 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2640 tx_flags |= TX_CMD_FLG_CTS_MSK;
2641 }
2642
2643 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2644 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2645
2646 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2647 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2648 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2649 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2650 cmd->cmd.tx.timeout.pm_frame_timeout =
2651 cpu_to_le16(3);
2652 else
2653 cmd->cmd.tx.timeout.pm_frame_timeout =
2654 cpu_to_le16(2);
2655 } else
2656 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2657
2658 cmd->cmd.tx.driver_txop = 0;
2659 cmd->cmd.tx.tx_flags = tx_flags;
2660 cmd->cmd.tx.next_frame_len = 0;
2661 }
2662
2663 static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
2664 {
2665 int sta_id;
2666 u16 fc = le16_to_cpu(hdr->frame_control);
2667
2668 /* If this frame is broadcast or not data then use the broadcast
2669 * station id */
2670 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2671 is_multicast_ether_addr(hdr->addr1))
2672 return priv->hw_setting.bcast_sta_id;
2673
2674 switch (priv->iw_mode) {
2675
2676 /* If this frame is part of a BSS network (we're a station), then
2677 * we use the AP's station id */
2678 case IEEE80211_IF_TYPE_STA:
2679 return IWL_AP_ID;
2680
2681 /* If we are an AP, then find the station, or use BCAST */
2682 case IEEE80211_IF_TYPE_AP:
2683 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2684 if (sta_id != IWL_INVALID_STATION)
2685 return sta_id;
2686 return priv->hw_setting.bcast_sta_id;
2687
2688 /* If this frame is part of a IBSS network, then we use the
2689 * target specific station id */
2690 case IEEE80211_IF_TYPE_IBSS: {
2691 DECLARE_MAC_BUF(mac);
2692
2693 sta_id = iwl_hw_find_station(priv, hdr->addr1);
2694 if (sta_id != IWL_INVALID_STATION)
2695 return sta_id;
2696
2697 sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
2698
2699 if (sta_id != IWL_INVALID_STATION)
2700 return sta_id;
2701
2702 IWL_DEBUG_DROP("Station %s not in station map. "
2703 "Defaulting to broadcast...\n",
2704 print_mac(mac, hdr->addr1));
2705 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2706 return priv->hw_setting.bcast_sta_id;
2707 }
2708 default:
2709 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
2710 return priv->hw_setting.bcast_sta_id;
2711 }
2712 }
2713
2714 /*
2715 * start REPLY_TX command process
2716 */
2717 static int iwl_tx_skb(struct iwl_priv *priv,
2718 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2719 {
2720 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2721 struct iwl_tfd_frame *tfd;
2722 u32 *control_flags;
2723 int txq_id = ctl->queue;
2724 struct iwl_tx_queue *txq = NULL;
2725 struct iwl_queue *q = NULL;
2726 dma_addr_t phys_addr;
2727 dma_addr_t txcmd_phys;
2728 struct iwl_cmd *out_cmd = NULL;
2729 u16 len, idx, len_org;
2730 u8 id, hdr_len, unicast;
2731 u8 sta_id;
2732 u16 seq_number = 0;
2733 u16 fc;
2734 __le16 *qc;
2735 u8 wait_write_ptr = 0;
2736 unsigned long flags;
2737 int rc;
2738
2739 spin_lock_irqsave(&priv->lock, flags);
2740 if (iwl_is_rfkill(priv)) {
2741 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2742 goto drop_unlock;
2743 }
2744
2745 if (!priv->interface_id) {
2746 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2747 goto drop_unlock;
2748 }
2749
2750 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2751 IWL_ERROR("ERROR: No TX rate available.\n");
2752 goto drop_unlock;
2753 }
2754
2755 unicast = !is_multicast_ether_addr(hdr->addr1);
2756 id = 0;
2757
2758 fc = le16_to_cpu(hdr->frame_control);
2759
2760 #ifdef CONFIG_IWLWIFI_DEBUG
2761 if (ieee80211_is_auth(fc))
2762 IWL_DEBUG_TX("Sending AUTH frame\n");
2763 else if (ieee80211_is_assoc_request(fc))
2764 IWL_DEBUG_TX("Sending ASSOC frame\n");
2765 else if (ieee80211_is_reassoc_request(fc))
2766 IWL_DEBUG_TX("Sending REASSOC frame\n");
2767 #endif
2768
2769 if (!iwl_is_associated(priv) &&
2770 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2771 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
2772 goto drop_unlock;
2773 }
2774
2775 spin_unlock_irqrestore(&priv->lock, flags);
2776
2777 hdr_len = ieee80211_get_hdrlen(fc);
2778 sta_id = iwl_get_sta_id(priv, hdr);
2779 if (sta_id == IWL_INVALID_STATION) {
2780 DECLARE_MAC_BUF(mac);
2781
2782 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2783 print_mac(mac, hdr->addr1));
2784 goto drop;
2785 }
2786
2787 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2788
2789 qc = ieee80211_get_qos_ctrl(hdr);
2790 if (qc) {
2791 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2792 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2793 IEEE80211_SCTL_SEQ;
2794 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2795 (hdr->seq_ctrl &
2796 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2797 seq_number += 0x10;
2798 }
2799 txq = &priv->txq[txq_id];
2800 q = &txq->q;
2801
2802 spin_lock_irqsave(&priv->lock, flags);
2803
2804 tfd = &txq->bd[q->write_ptr];
2805 memset(tfd, 0, sizeof(*tfd));
2806 control_flags = (u32 *) tfd;
2807 idx = get_cmd_index(q, q->write_ptr, 0);
2808
2809 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
2810 txq->txb[q->write_ptr].skb[0] = skb;
2811 memcpy(&(txq->txb[q->write_ptr].status.control),
2812 ctl, sizeof(struct ieee80211_tx_control));
2813 out_cmd = &txq->cmd[idx];
2814 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2815 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2816 out_cmd->hdr.cmd = REPLY_TX;
2817 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2818 INDEX_TO_SEQ(q->write_ptr)));
2819 /* copy frags header */
2820 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2821
2822 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2823 len = priv->hw_setting.tx_cmd_len +
2824 sizeof(struct iwl_cmd_header) + hdr_len;
2825
2826 len_org = len;
2827 len = (len + 3) & ~3;
2828
2829 if (len_org != len)
2830 len_org = 1;
2831 else
2832 len_org = 0;
2833
2834 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2835 offsetof(struct iwl_cmd, hdr);
2836
2837 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2838
2839 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2840 iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
2841
2842 /* 802.11 null functions have no payload... */
2843 len = skb->len - hdr_len;
2844 if (len) {
2845 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2846 len, PCI_DMA_TODEVICE);
2847 iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2848 }
2849
2850 /* If there is no payload, then only one TFD is used */
2851 if (!len)
2852 *control_flags = TFD_CTL_COUNT_SET(1);
2853 else
2854 *control_flags = TFD_CTL_COUNT_SET(2) |
2855 TFD_CTL_PAD_SET(U32_PAD(len));
2856
2857 len = (u16)skb->len;
2858 out_cmd->cmd.tx.len = cpu_to_le16(len);
2859
2860 /* TODO need this for burst mode later on */
2861 iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2862
2863 /* set is_hcca to 0; it probably will never be implemented */
2864 iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2865
2866 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2867 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2868
2869 if (!ieee80211_get_morefrag(hdr)) {
2870 txq->need_update = 1;
2871 if (qc) {
2872 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2873 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2874 }
2875 } else {
2876 wait_write_ptr = 1;
2877 txq->need_update = 0;
2878 }
2879
2880 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2881 sizeof(out_cmd->cmd.tx));
2882
2883 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2884 ieee80211_get_hdrlen(fc));
2885
2886 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
2887 rc = iwl_tx_queue_update_write_ptr(priv, txq);
2888 spin_unlock_irqrestore(&priv->lock, flags);
2889
2890 if (rc)
2891 return rc;
2892
2893 if ((iwl_queue_space(q) < q->high_mark)
2894 && priv->mac80211_registered) {
2895 if (wait_write_ptr) {
2896 spin_lock_irqsave(&priv->lock, flags);
2897 txq->need_update = 1;
2898 iwl_tx_queue_update_write_ptr(priv, txq);
2899 spin_unlock_irqrestore(&priv->lock, flags);
2900 }
2901
2902 ieee80211_stop_queue(priv->hw, ctl->queue);
2903 }
2904
2905 return 0;
2906
2907 drop_unlock:
2908 spin_unlock_irqrestore(&priv->lock, flags);
2909 drop:
2910 return -1;
2911 }
2912
2913 static void iwl_set_rate(struct iwl_priv *priv)
2914 {
2915 const struct ieee80211_hw_mode *hw = NULL;
2916 struct ieee80211_rate *rate;
2917 int i;
2918
2919 hw = iwl_get_hw_mode(priv, priv->phymode);
2920 if (!hw) {
2921 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2922 return;
2923 }
2924
2925 priv->active_rate = 0;
2926 priv->active_rate_basic = 0;
2927
2928 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
2929 hw->mode == MODE_IEEE80211A ?
2930 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
2931
2932 for (i = 0; i < hw->num_rates; i++) {
2933 rate = &(hw->rates[i]);
2934 if ((rate->val < IWL_RATE_COUNT) &&
2935 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
2936 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
2937 rate->val, iwl_rates[rate->val].plcp,
2938 (rate->flags & IEEE80211_RATE_BASIC) ?
2939 "*" : "");
2940 priv->active_rate |= (1 << rate->val);
2941 if (rate->flags & IEEE80211_RATE_BASIC)
2942 priv->active_rate_basic |= (1 << rate->val);
2943 } else
2944 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
2945 rate->val, iwl_rates[rate->val].plcp);
2946 }
2947
2948 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2949 priv->active_rate, priv->active_rate_basic);
2950
2951 /*
2952 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2953 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2954 * OFDM
2955 */
2956 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2957 priv->staging_rxon.cck_basic_rates =
2958 ((priv->active_rate_basic &
2959 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2960 else
2961 priv->staging_rxon.cck_basic_rates =
2962 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2963
2964 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2965 priv->staging_rxon.ofdm_basic_rates =
2966 ((priv->active_rate_basic &
2967 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2968 IWL_FIRST_OFDM_RATE) & 0xFF;
2969 else
2970 priv->staging_rxon.ofdm_basic_rates =
2971 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2972 }
2973
2974 static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
2975 {
2976 unsigned long flags;
2977
2978 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2979 return;
2980
2981 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2982 disable_radio ? "OFF" : "ON");
2983
2984 if (disable_radio) {
2985 iwl_scan_cancel(priv);
2986 /* FIXME: This is a workaround for AP */
2987 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2988 spin_lock_irqsave(&priv->lock, flags);
2989 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2990 CSR_UCODE_SW_BIT_RFKILL);
2991 spin_unlock_irqrestore(&priv->lock, flags);
2992 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
2993 set_bit(STATUS_RF_KILL_SW, &priv->status);
2994 }
2995 return;
2996 }
2997
2998 spin_lock_irqsave(&priv->lock, flags);
2999 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3000
3001 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3002 spin_unlock_irqrestore(&priv->lock, flags);
3003
3004 /* wake up ucode */
3005 msleep(10);
3006
3007 spin_lock_irqsave(&priv->lock, flags);
3008 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3009 if (!iwl_grab_restricted_access(priv))
3010 iwl_release_restricted_access(priv);
3011 spin_unlock_irqrestore(&priv->lock, flags);
3012
3013 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3014 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3015 "disabled by HW switch\n");
3016 return;
3017 }
3018
3019 queue_work(priv->workqueue, &priv->restart);
3020 return;
3021 }
3022
3023 void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
3024 u32 decrypt_res, struct ieee80211_rx_status *stats)
3025 {
3026 u16 fc =
3027 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3028
3029 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3030 return;
3031
3032 if (!(fc & IEEE80211_FCTL_PROTECTED))
3033 return;
3034
3035 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3036 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3037 case RX_RES_STATUS_SEC_TYPE_TKIP:
3038 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3039 RX_RES_STATUS_BAD_ICV_MIC)
3040 stats->flag |= RX_FLAG_MMIC_ERROR;
3041 case RX_RES_STATUS_SEC_TYPE_WEP:
3042 case RX_RES_STATUS_SEC_TYPE_CCMP:
3043 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3044 RX_RES_STATUS_DECRYPT_OK) {
3045 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3046 stats->flag |= RX_FLAG_DECRYPTED;
3047 }
3048 break;
3049
3050 default:
3051 break;
3052 }
3053 }
3054
3055 void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
3056 struct iwl_rx_mem_buffer *rxb,
3057 void *data, short len,
3058 struct ieee80211_rx_status *stats,
3059 u16 phy_flags)
3060 {
3061 struct iwl_rt_rx_hdr *iwl_rt;
3062
3063 /* First cache any information we need before we overwrite
3064 * the information provided in the skb from the hardware */
3065 s8 signal = stats->ssi;
3066 s8 noise = 0;
3067 int rate = stats->rate;
3068 u64 tsf = stats->mactime;
3069 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3070
3071 /* We received data from the HW, so stop the watchdog */
3072 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
3073 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3074 return;
3075 }
3076
3077 /* copy the frame data to write after where the radiotap header goes */
3078 iwl_rt = (void *)rxb->skb->data;
3079 memmove(iwl_rt->payload, data, len);
3080
3081 iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3082 iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
3083
3084 /* total header + data */
3085 iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
3086
3087 /* Set the size of the skb to the size of the frame */
3088 skb_put(rxb->skb, sizeof(*iwl_rt) + len);
3089
3090 /* Big bitfield of all the fields we provide in radiotap */
3091 iwl_rt->rt_hdr.it_present =
3092 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3093 (1 << IEEE80211_RADIOTAP_FLAGS) |
3094 (1 << IEEE80211_RADIOTAP_RATE) |
3095 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3096 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3097 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3098 (1 << IEEE80211_RADIOTAP_ANTENNA));
3099
3100 /* Zero the flags, we'll add to them as we go */
3101 iwl_rt->rt_flags = 0;
3102
3103 iwl_rt->rt_tsf = cpu_to_le64(tsf);
3104
3105 /* Convert to dBm */
3106 iwl_rt->rt_dbmsignal = signal;
3107 iwl_rt->rt_dbmnoise = noise;
3108
3109 /* Convert the channel frequency and set the flags */
3110 iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
3111 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
3112 iwl_rt->rt_chbitmask =
3113 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3114 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
3115 iwl_rt->rt_chbitmask =
3116 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3117 else /* 802.11g */
3118 iwl_rt->rt_chbitmask =
3119 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3120
3121 rate = iwl_rate_index_from_plcp(rate);
3122 if (rate == -1)
3123 iwl_rt->rt_rate = 0;
3124 else
3125 iwl_rt->rt_rate = iwl_rates[rate].ieee;
3126
3127 /* antenna number */
3128 iwl_rt->rt_antenna =
3129 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3130
3131 /* set the preamble flag if we have it */
3132 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
3133 iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3134
3135 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3136
3137 stats->flag |= RX_FLAG_RADIOTAP;
3138 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3139 rxb->skb = NULL;
3140 }
3141
3142
3143 #define IWL_PACKET_RETRY_TIME HZ
3144
3145 int is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
3146 {
3147 u16 sc = le16_to_cpu(header->seq_ctrl);
3148 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3149 u16 frag = sc & IEEE80211_SCTL_FRAG;
3150 u16 *last_seq, *last_frag;
3151 unsigned long *last_time;
3152
3153 switch (priv->iw_mode) {
3154 case IEEE80211_IF_TYPE_IBSS:{
3155 struct list_head *p;
3156 struct iwl_ibss_seq *entry = NULL;
3157 u8 *mac = header->addr2;
3158 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3159
3160 __list_for_each(p, &priv->ibss_mac_hash[index]) {
3161 entry =
3162 list_entry(p, struct iwl_ibss_seq, list);
3163 if (!compare_ether_addr(entry->mac, mac))
3164 break;
3165 }
3166 if (p == &priv->ibss_mac_hash[index]) {
3167 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3168 if (!entry) {
3169 IWL_ERROR
3170 ("Cannot malloc new mac entry\n");
3171 return 0;
3172 }
3173 memcpy(entry->mac, mac, ETH_ALEN);
3174 entry->seq_num = seq;
3175 entry->frag_num = frag;
3176 entry->packet_time = jiffies;
3177 list_add(&entry->list,
3178 &priv->ibss_mac_hash[index]);
3179 return 0;
3180 }
3181 last_seq = &entry->seq_num;
3182 last_frag = &entry->frag_num;
3183 last_time = &entry->packet_time;
3184 break;
3185 }
3186 case IEEE80211_IF_TYPE_STA:
3187 last_seq = &priv->last_seq_num;
3188 last_frag = &priv->last_frag_num;
3189 last_time = &priv->last_packet_time;
3190 break;
3191 default:
3192 return 0;
3193 }
3194 if ((*last_seq == seq) &&
3195 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3196 if (*last_frag == frag)
3197 goto drop;
3198 if (*last_frag + 1 != frag)
3199 /* out-of-order fragment */
3200 goto drop;
3201 } else
3202 *last_seq = seq;
3203
3204 *last_frag = frag;
3205 *last_time = jiffies;
3206 return 0;
3207
3208 drop:
3209 return 1;
3210 }
3211
3212 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3213
3214 #include "iwl-spectrum.h"
3215
3216 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
3217 #define BEACON_TIME_MASK_HIGH 0xFF000000
3218 #define TIME_UNIT 1024
3219
3220 /*
3221 * extended beacon time format
3222 * time in usec will be changed into a 32-bit value in 8:24 format
3223 * the high 1 byte is the beacon counts
3224 * the lower 3 bytes is the time in usec within one beacon interval
3225 */
3226
3227 static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
3228 {
3229 u32 quot;
3230 u32 rem;
3231 u32 interval = beacon_interval * 1024;
3232
3233 if (!interval || !usec)
3234 return 0;
3235
3236 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3237 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3238
3239 return (quot << 24) + rem;
3240 }
3241
3242 /* base is usually what we get from ucode with each received frame,
3243 * the same as HW timer counter counting down
3244 */
3245
3246 static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
3247 {
3248 u32 base_low = base & BEACON_TIME_MASK_LOW;
3249 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3250 u32 interval = beacon_interval * TIME_UNIT;
3251 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3252 (addon & BEACON_TIME_MASK_HIGH);
3253
3254 if (base_low > addon_low)
3255 res += base_low - addon_low;
3256 else if (base_low < addon_low) {
3257 res += interval + base_low - addon_low;
3258 res += (1 << 24);
3259 } else
3260 res += (1 << 24);
3261
3262 return cpu_to_le32(res);
3263 }
3264
3265 static int iwl_get_measurement(struct iwl_priv *priv,
3266 struct ieee80211_measurement_params *params,
3267 u8 type)
3268 {
3269 struct iwl_spectrum_cmd spectrum;
3270 struct iwl_rx_packet *res;
3271 struct iwl_host_cmd cmd = {
3272 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3273 .data = (void *)&spectrum,
3274 .meta.flags = CMD_WANT_SKB,
3275 };
3276 u32 add_time = le64_to_cpu(params->start_time);
3277 int rc;
3278 int spectrum_resp_status;
3279 int duration = le16_to_cpu(params->duration);
3280
3281 if (iwl_is_associated(priv))
3282 add_time =
3283 iwl_usecs_to_beacons(
3284 le64_to_cpu(params->start_time) - priv->last_tsf,
3285 le16_to_cpu(priv->rxon_timing.beacon_interval));
3286
3287 memset(&spectrum, 0, sizeof(spectrum));
3288
3289 spectrum.channel_count = cpu_to_le16(1);
3290 spectrum.flags =
3291 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3292 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3293 cmd.len = sizeof(spectrum);
3294 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3295
3296 if (iwl_is_associated(priv))
3297 spectrum.start_time =
3298 iwl_add_beacon_time(priv->last_beacon_time,
3299 add_time,
3300 le16_to_cpu(priv->rxon_timing.beacon_interval));
3301 else
3302 spectrum.start_time = 0;
3303
3304 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3305 spectrum.channels[0].channel = params->channel;
3306 spectrum.channels[0].type = type;
3307 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3308 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3309 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3310
3311 rc = iwl_send_cmd_sync(priv, &cmd);
3312 if (rc)
3313 return rc;
3314
3315 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
3316 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3317 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3318 rc = -EIO;
3319 }
3320
3321 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3322 switch (spectrum_resp_status) {
3323 case 0: /* Command will be handled */
3324 if (res->u.spectrum.id != 0xff) {
3325 IWL_DEBUG_INFO
3326 ("Replaced existing measurement: %d\n",
3327 res->u.spectrum.id);
3328 priv->measurement_status &= ~MEASUREMENT_READY;
3329 }
3330 priv->measurement_status |= MEASUREMENT_ACTIVE;
3331 rc = 0;
3332 break;
3333
3334 case 1: /* Command will not be handled */
3335 rc = -EAGAIN;
3336 break;
3337 }
3338
3339 dev_kfree_skb_any(cmd.meta.u.skb);
3340
3341 return rc;
3342 }
3343 #endif
3344
3345 static void iwl_txstatus_to_ieee(struct iwl_priv *priv,
3346 struct iwl_tx_info *tx_sta)
3347 {
3348
3349 tx_sta->status.ack_signal = 0;
3350 tx_sta->status.excessive_retries = 0;
3351 tx_sta->status.queue_length = 0;
3352 tx_sta->status.queue_number = 0;
3353
3354 if (in_interrupt())
3355 ieee80211_tx_status_irqsafe(priv->hw,
3356 tx_sta->skb[0], &(tx_sta->status));
3357 else
3358 ieee80211_tx_status(priv->hw,
3359 tx_sta->skb[0], &(tx_sta->status));
3360
3361 tx_sta->skb[0] = NULL;
3362 }
3363
3364 /**
3365 * iwl_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
3366 *
3367 * When FW advances 'R' index, all entries between old and
3368 * new 'R' index need to be reclaimed. As result, some free space
3369 * forms. If there is enough free space (> low mark), wake Tx queue.
3370 */
3371 int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
3372 {
3373 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3374 struct iwl_queue *q = &txq->q;
3375 int nfreed = 0;
3376
3377 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3378 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3379 "is out of range [0-%d] %d %d.\n", txq_id,
3380 index, q->n_bd, q->write_ptr, q->read_ptr);
3381 return 0;
3382 }
3383
3384 for (index = iwl_queue_inc_wrap(index, q->n_bd);
3385 q->read_ptr != index;
3386 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3387 if (txq_id != IWL_CMD_QUEUE_NUM) {
3388 iwl_txstatus_to_ieee(priv,
3389 &(txq->txb[txq->q.read_ptr]));
3390 iwl_hw_txq_free_tfd(priv, txq);
3391 } else if (nfreed > 1) {
3392 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
3393 q->write_ptr, q->read_ptr);
3394 queue_work(priv->workqueue, &priv->restart);
3395 }
3396 nfreed++;
3397 }
3398
3399 if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
3400 (txq_id != IWL_CMD_QUEUE_NUM) &&
3401 priv->mac80211_registered)
3402 ieee80211_wake_queue(priv->hw, txq_id);
3403
3404
3405 return nfreed;
3406 }
3407
3408 static int iwl_is_tx_success(u32 status)
3409 {
3410 return (status & 0xFF) == 0x1;
3411 }
3412
3413 /******************************************************************************
3414 *
3415 * Generic RX handler implementations
3416 *
3417 ******************************************************************************/
3418 static void iwl_rx_reply_tx(struct iwl_priv *priv,
3419 struct iwl_rx_mem_buffer *rxb)
3420 {
3421 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3422 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3423 int txq_id = SEQ_TO_QUEUE(sequence);
3424 int index = SEQ_TO_INDEX(sequence);
3425 struct iwl_tx_queue *txq = &priv->txq[txq_id];
3426 struct ieee80211_tx_status *tx_status;
3427 struct iwl_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
3428 u32 status = le32_to_cpu(tx_resp->status);
3429
3430 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3431 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3432 "is out of range [0-%d] %d %d\n", txq_id,
3433 index, txq->q.n_bd, txq->q.write_ptr,
3434 txq->q.read_ptr);
3435 return;
3436 }
3437
3438 tx_status = &(txq->txb[txq->q.read_ptr].status);
3439
3440 tx_status->retry_count = tx_resp->failure_frame;
3441 tx_status->queue_number = status;
3442 tx_status->queue_length = tx_resp->bt_kill_count;
3443 tx_status->queue_length |= tx_resp->failure_rts;
3444
3445 tx_status->flags =
3446 iwl_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
3447
3448 tx_status->control.tx_rate = iwl_rate_index_from_plcp(tx_resp->rate);
3449
3450 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) plcp rate %d retries %d\n",
3451 txq_id, iwl_get_tx_fail_reason(status), status,
3452 tx_resp->rate, tx_resp->failure_frame);
3453
3454 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3455 if (index != -1)
3456 iwl_tx_queue_reclaim(priv, txq_id, index);
3457
3458 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3459 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3460 }
3461
3462
3463 static void iwl_rx_reply_alive(struct iwl_priv *priv,
3464 struct iwl_rx_mem_buffer *rxb)
3465 {
3466 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3467 struct iwl_alive_resp *palive;
3468 struct delayed_work *pwork;
3469
3470 palive = &pkt->u.alive_frame;
3471
3472 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3473 "0x%01X 0x%01X\n",
3474 palive->is_valid, palive->ver_type,
3475 palive->ver_subtype);
3476
3477 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3478 IWL_DEBUG_INFO("Initialization Alive received.\n");
3479 memcpy(&priv->card_alive_init,
3480 &pkt->u.alive_frame,
3481 sizeof(struct iwl_init_alive_resp));
3482 pwork = &priv->init_alive_start;
3483 } else {
3484 IWL_DEBUG_INFO("Runtime Alive received.\n");
3485 memcpy(&priv->card_alive, &pkt->u.alive_frame,
3486 sizeof(struct iwl_alive_resp));
3487 pwork = &priv->alive_start;
3488 iwl_disable_events(priv);
3489 }
3490
3491 /* We delay the ALIVE response by 5ms to
3492 * give the HW RF Kill time to activate... */
3493 if (palive->is_valid == UCODE_VALID_OK)
3494 queue_delayed_work(priv->workqueue, pwork,
3495 msecs_to_jiffies(5));
3496 else
3497 IWL_WARNING("uCode did not respond OK.\n");
3498 }
3499
3500 static void iwl_rx_reply_add_sta(struct iwl_priv *priv,
3501 struct iwl_rx_mem_buffer *rxb)
3502 {
3503 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3504
3505 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3506 return;
3507 }
3508
3509 static void iwl_rx_reply_error(struct iwl_priv *priv,
3510 struct iwl_rx_mem_buffer *rxb)
3511 {
3512 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3513
3514 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3515 "seq 0x%04X ser 0x%08X\n",
3516 le32_to_cpu(pkt->u.err_resp.error_type),
3517 get_cmd_string(pkt->u.err_resp.cmd_id),
3518 pkt->u.err_resp.cmd_id,
3519 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3520 le32_to_cpu(pkt->u.err_resp.error_info));
3521 }
3522
3523 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3524
3525 static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
3526 {
3527 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3528 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
3529 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
3530 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3531 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3532 rxon->channel = csa->channel;
3533 priv->staging_rxon.channel = csa->channel;
3534 }
3535
3536 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
3537 struct iwl_rx_mem_buffer *rxb)
3538 {
3539 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
3540 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3541 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
3542
3543 if (!report->state) {
3544 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3545 "Spectrum Measure Notification: Start\n");
3546 return;
3547 }
3548
3549 memcpy(&priv->measure_report, report, sizeof(*report));
3550 priv->measurement_status |= MEASUREMENT_READY;
3551 #endif
3552 }
3553
3554 static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
3555 struct iwl_rx_mem_buffer *rxb)
3556 {
3557 #ifdef CONFIG_IWLWIFI_DEBUG
3558 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3559 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
3560 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3561 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3562 #endif
3563 }
3564
3565 static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
3566 struct iwl_rx_mem_buffer *rxb)
3567 {
3568 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3569 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3570 "notification for %s:\n",
3571 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
3572 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
3573 }
3574
3575 static void iwl_bg_beacon_update(struct work_struct *work)
3576 {
3577 struct iwl_priv *priv =
3578 container_of(work, struct iwl_priv, beacon_update);
3579 struct sk_buff *beacon;
3580
3581 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3582 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3583
3584 if (!beacon) {
3585 IWL_ERROR("update beacon failed\n");
3586 return;
3587 }
3588
3589 mutex_lock(&priv->mutex);
3590 /* new beacon skb is allocated every time; dispose previous.*/
3591 if (priv->ibss_beacon)
3592 dev_kfree_skb(priv->ibss_beacon);
3593
3594 priv->ibss_beacon = beacon;
3595 mutex_unlock(&priv->mutex);
3596
3597 iwl_send_beacon_cmd(priv);
3598 }
3599
3600 static void iwl_rx_beacon_notif(struct iwl_priv *priv,
3601 struct iwl_rx_mem_buffer *rxb)
3602 {
3603 #ifdef CONFIG_IWLWIFI_DEBUG
3604 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3605 struct iwl_beacon_notif *beacon = &(pkt->u.beacon_status);
3606 u8 rate = beacon->beacon_notify_hdr.rate;
3607
3608 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3609 "tsf %d %d rate %d\n",
3610 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3611 beacon->beacon_notify_hdr.failure_frame,
3612 le32_to_cpu(beacon->ibss_mgr_status),
3613 le32_to_cpu(beacon->high_tsf),
3614 le32_to_cpu(beacon->low_tsf), rate);
3615 #endif
3616
3617 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3618 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3619 queue_work(priv->workqueue, &priv->beacon_update);
3620 }
3621
3622 /* Service response to REPLY_SCAN_CMD (0x80) */
3623 static void iwl_rx_reply_scan(struct iwl_priv *priv,
3624 struct iwl_rx_mem_buffer *rxb)
3625 {
3626 #ifdef CONFIG_IWLWIFI_DEBUG
3627 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3628 struct iwl_scanreq_notification *notif =
3629 (struct iwl_scanreq_notification *)pkt->u.raw;
3630
3631 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3632 #endif
3633 }
3634
3635 /* Service SCAN_START_NOTIFICATION (0x82) */
3636 static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
3637 struct iwl_rx_mem_buffer *rxb)
3638 {
3639 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3640 struct iwl_scanstart_notification *notif =
3641 (struct iwl_scanstart_notification *)pkt->u.raw;
3642 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3643 IWL_DEBUG_SCAN("Scan start: "
3644 "%d [802.11%s] "
3645 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3646 notif->channel,
3647 notif->band ? "bg" : "a",
3648 notif->tsf_high,
3649 notif->tsf_low, notif->status, notif->beacon_timer);
3650 }
3651
3652 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
3653 static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
3654 struct iwl_rx_mem_buffer *rxb)
3655 {
3656 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3657 struct iwl_scanresults_notification *notif =
3658 (struct iwl_scanresults_notification *)pkt->u.raw;
3659
3660 IWL_DEBUG_SCAN("Scan ch.res: "
3661 "%d [802.11%s] "
3662 "(TSF: 0x%08X:%08X) - %d "
3663 "elapsed=%lu usec (%dms since last)\n",
3664 notif->channel,
3665 notif->band ? "bg" : "a",
3666 le32_to_cpu(notif->tsf_high),
3667 le32_to_cpu(notif->tsf_low),
3668 le32_to_cpu(notif->statistics[0]),
3669 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3670 jiffies_to_msecs(elapsed_jiffies
3671 (priv->last_scan_jiffies, jiffies)));
3672
3673 priv->last_scan_jiffies = jiffies;
3674 }
3675
3676 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
3677 static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
3678 struct iwl_rx_mem_buffer *rxb)
3679 {
3680 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3681 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
3682
3683 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3684 scan_notif->scanned_channels,
3685 scan_notif->tsf_low,
3686 scan_notif->tsf_high, scan_notif->status);
3687
3688 /* The HW is no longer scanning */
3689 clear_bit(STATUS_SCAN_HW, &priv->status);
3690
3691 /* The scan completion notification came in, so kill that timer... */
3692 cancel_delayed_work(&priv->scan_check);
3693
3694 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3695 (priv->scan_bands == 2) ? "2.4" : "5.2",
3696 jiffies_to_msecs(elapsed_jiffies
3697 (priv->scan_pass_start, jiffies)));
3698
3699 /* Remove this scanned band from the list
3700 * of pending bands to scan */
3701 priv->scan_bands--;
3702
3703 /* If a request to abort was given, or the scan did not succeed
3704 * then we reset the scan state machine and terminate,
3705 * re-queuing another scan if one has been requested */
3706 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3707 IWL_DEBUG_INFO("Aborted scan completed.\n");
3708 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3709 } else {
3710 /* If there are more bands on this scan pass reschedule */
3711 if (priv->scan_bands > 0)
3712 goto reschedule;
3713 }
3714
3715 priv->last_scan_jiffies = jiffies;
3716 IWL_DEBUG_INFO("Setting scan to off\n");
3717
3718 clear_bit(STATUS_SCANNING, &priv->status);
3719
3720 IWL_DEBUG_INFO("Scan took %dms\n",
3721 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3722
3723 queue_work(priv->workqueue, &priv->scan_completed);
3724
3725 return;
3726
3727 reschedule:
3728 priv->scan_pass_start = jiffies;
3729 queue_work(priv->workqueue, &priv->request_scan);
3730 }
3731
3732 /* Handle notification from uCode that card's power state is changing
3733 * due to software, hardware, or critical temperature RFKILL */
3734 static void iwl_rx_card_state_notif(struct iwl_priv *priv,
3735 struct iwl_rx_mem_buffer *rxb)
3736 {
3737 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
3738 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3739 unsigned long status = priv->status;
3740
3741 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3742 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3743 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3744
3745 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3746 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3747
3748 if (flags & HW_CARD_DISABLED)
3749 set_bit(STATUS_RF_KILL_HW, &priv->status);
3750 else
3751 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3752
3753
3754 if (flags & SW_CARD_DISABLED)
3755 set_bit(STATUS_RF_KILL_SW, &priv->status);
3756 else
3757 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3758
3759 iwl_scan_cancel(priv);
3760
3761 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3762 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3763 (test_bit(STATUS_RF_KILL_SW, &status) !=
3764 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3765 queue_work(priv->workqueue, &priv->rf_kill);
3766 else
3767 wake_up_interruptible(&priv->wait_command_queue);
3768 }
3769
3770 /**
3771 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
3772 *
3773 * Setup the RX handlers for each of the reply types sent from the uCode
3774 * to the host.
3775 *
3776 * This function chains into the hardware specific files for them to setup
3777 * any hardware specific handlers as well.
3778 */
3779 static void iwl_setup_rx_handlers(struct iwl_priv *priv)
3780 {
3781 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
3782 priv->rx_handlers[REPLY_ADD_STA] = iwl_rx_reply_add_sta;
3783 priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
3784 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
3785 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
3786 iwl_rx_spectrum_measure_notif;
3787 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
3788 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
3789 iwl_rx_pm_debug_statistics_notif;
3790 priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
3791
3792 /* NOTE: iwl_rx_statistics is different based on whether
3793 * the build is for the 3945 or the 4965. See the
3794 * corresponding implementation in iwl-XXXX.c
3795 *
3796 * The same handler is used for both the REPLY to a
3797 * discrete statistics request from the host as well as
3798 * for the periodic statistics notification from the uCode
3799 */
3800 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_hw_rx_statistics;
3801 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_hw_rx_statistics;
3802
3803 priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
3804 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
3805 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
3806 iwl_rx_scan_results_notif;
3807 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
3808 iwl_rx_scan_complete_notif;
3809 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
3810 priv->rx_handlers[REPLY_TX] = iwl_rx_reply_tx;
3811
3812 /* Setup hardware specific Rx handlers */
3813 iwl_hw_rx_handler_setup(priv);
3814 }
3815
3816 /**
3817 * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
3818 * @rxb: Rx buffer to reclaim
3819 *
3820 * If an Rx buffer has an async callback associated with it the callback
3821 * will be executed. The attached skb (if present) will only be freed
3822 * if the callback returns 1
3823 */
3824 static void iwl_tx_cmd_complete(struct iwl_priv *priv,
3825 struct iwl_rx_mem_buffer *rxb)
3826 {
3827 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3828 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3829 int txq_id = SEQ_TO_QUEUE(sequence);
3830 int index = SEQ_TO_INDEX(sequence);
3831 int huge = sequence & SEQ_HUGE_FRAME;
3832 int cmd_index;
3833 struct iwl_cmd *cmd;
3834
3835 /* If a Tx command is being handled and it isn't in the actual
3836 * command queue then there a command routing bug has been introduced
3837 * in the queue management code. */
3838 if (txq_id != IWL_CMD_QUEUE_NUM)
3839 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3840 txq_id, pkt->hdr.cmd);
3841 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3842
3843 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3844 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3845
3846 /* Input error checking is done when commands are added to queue. */
3847 if (cmd->meta.flags & CMD_WANT_SKB) {
3848 cmd->meta.source->u.skb = rxb->skb;
3849 rxb->skb = NULL;
3850 } else if (cmd->meta.u.callback &&
3851 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3852 rxb->skb = NULL;
3853
3854 iwl_tx_queue_reclaim(priv, txq_id, index);
3855
3856 if (!(cmd->meta.flags & CMD_ASYNC)) {
3857 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3858 wake_up_interruptible(&priv->wait_command_queue);
3859 }
3860 }
3861
3862 /************************** RX-FUNCTIONS ****************************/
3863 /*
3864 * Rx theory of operation
3865 *
3866 * The host allocates 32 DMA target addresses and passes the host address
3867 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3868 * 0 to 31
3869 *
3870 * Rx Queue Indexes
3871 * The host/firmware share two index registers for managing the Rx buffers.
3872 *
3873 * The READ index maps to the first position that the firmware may be writing
3874 * to -- the driver can read up to (but not including) this position and get
3875 * good data.
3876 * The READ index is managed by the firmware once the card is enabled.
3877 *
3878 * The WRITE index maps to the last position the driver has read from -- the
3879 * position preceding WRITE is the last slot the firmware can place a packet.
3880 *
3881 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3882 * WRITE = READ.
3883 *
3884 * During initialization the host sets up the READ queue position to the first
3885 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3886 *
3887 * When the firmware places a packet in a buffer it will advance the READ index
3888 * and fire the RX interrupt. The driver can then query the READ index and
3889 * process as many packets as possible, moving the WRITE index forward as it
3890 * resets the Rx queue buffers with new memory.
3891 *
3892 * The management in the driver is as follows:
3893 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3894 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
3895 * to replenish the iwl->rxq->rx_free.
3896 * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
3897 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3898 * 'processed' and 'read' driver indexes as well)
3899 * + A received packet is processed and handed to the kernel network stack,
3900 * detached from the iwl->rxq. The driver 'processed' index is updated.
3901 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3902 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3903 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3904 * were enough free buffers and RX_STALLED is set it is cleared.
3905 *
3906 *
3907 * Driver sequence:
3908 *
3909 * iwl_rx_queue_alloc() Allocates rx_free
3910 * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
3911 * iwl_rx_queue_restock
3912 * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
3913 * queue, updates firmware pointers, and updates
3914 * the WRITE index. If insufficient rx_free buffers
3915 * are available, schedules iwl_rx_replenish
3916 *
3917 * -- enable interrupts --
3918 * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
3919 * READ INDEX, detaching the SKB from the pool.
3920 * Moves the packet buffer from queue to rx_used.
3921 * Calls iwl_rx_queue_restock to refill any empty
3922 * slots.
3923 * ...
3924 *
3925 */
3926
3927 /**
3928 * iwl_rx_queue_space - Return number of free slots available in queue.
3929 */
3930 static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
3931 {
3932 int s = q->read - q->write;
3933 if (s <= 0)
3934 s += RX_QUEUE_SIZE;
3935 /* keep some buffer to not confuse full and empty queue */
3936 s -= 2;
3937 if (s < 0)
3938 s = 0;
3939 return s;
3940 }
3941
3942 /**
3943 * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
3944 *
3945 * NOTE: This function has 3945 and 4965 specific code sections
3946 * but is declared in base due to the majority of the
3947 * implementation being the same (only a numeric constant is
3948 * different)
3949 *
3950 */
3951 int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
3952 {
3953 u32 reg = 0;
3954 int rc = 0;
3955 unsigned long flags;
3956
3957 spin_lock_irqsave(&q->lock, flags);
3958
3959 if (q->need_update == 0)
3960 goto exit_unlock;
3961
3962 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3963 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
3964
3965 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3966 iwl_set_bit(priv, CSR_GP_CNTRL,
3967 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3968 goto exit_unlock;
3969 }
3970
3971 rc = iwl_grab_restricted_access(priv);
3972 if (rc)
3973 goto exit_unlock;
3974
3975 iwl_write_restricted(priv, FH_RSCSR_CHNL0_WPTR,
3976 q->write & ~0x7);
3977 iwl_release_restricted_access(priv);
3978 } else
3979 iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
3980
3981
3982 q->need_update = 0;
3983
3984 exit_unlock:
3985 spin_unlock_irqrestore(&q->lock, flags);
3986 return rc;
3987 }
3988
3989 /**
3990 * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
3991 *
3992 * NOTE: This function has 3945 and 4965 specific code paths in it.
3993 */
3994 static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
3995 dma_addr_t dma_addr)
3996 {
3997 return cpu_to_le32((u32)dma_addr);
3998 }
3999
4000 /**
4001 * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
4002 *
4003 * If there are slots in the RX queue that need to be restocked,
4004 * and we have free pre-allocated buffers, fill the ranks as much
4005 * as we can pulling from rx_free.
4006 *
4007 * This moves the 'write' index forward to catch up with 'processed', and
4008 * also updates the memory address in the firmware to reference the new
4009 * target buffer.
4010 */
4011 int iwl_rx_queue_restock(struct iwl_priv *priv)
4012 {
4013 struct iwl_rx_queue *rxq = &priv->rxq;
4014 struct list_head *element;
4015 struct iwl_rx_mem_buffer *rxb;
4016 unsigned long flags;
4017 int write, rc;
4018
4019 spin_lock_irqsave(&rxq->lock, flags);
4020 write = rxq->write & ~0x7;
4021 while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
4022 element = rxq->rx_free.next;
4023 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4024 list_del(element);
4025 rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->dma_addr);
4026 rxq->queue[rxq->write] = rxb;
4027 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4028 rxq->free_count--;
4029 }
4030 spin_unlock_irqrestore(&rxq->lock, flags);
4031 /* If the pre-allocated buffer pool is dropping low, schedule to
4032 * refill it */
4033 if (rxq->free_count <= RX_LOW_WATERMARK)
4034 queue_work(priv->workqueue, &priv->rx_replenish);
4035
4036
4037 /* If we've added more space for the firmware to place data, tell it */
4038 if ((write != (rxq->write & ~0x7))
4039 || (abs(rxq->write - rxq->read) > 7)) {
4040 spin_lock_irqsave(&rxq->lock, flags);
4041 rxq->need_update = 1;
4042 spin_unlock_irqrestore(&rxq->lock, flags);
4043 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
4044 if (rc)
4045 return rc;
4046 }
4047
4048 return 0;
4049 }
4050
4051 /**
4052 * iwl_rx_replenish - Move all used packet from rx_used to rx_free
4053 *
4054 * When moving to rx_free an SKB is allocated for the slot.
4055 *
4056 * Also restock the Rx queue via iwl_rx_queue_restock.
4057 * This is called as a scheduled work item (except for during initialization)
4058 */
4059 void iwl_rx_replenish(void *data)
4060 {
4061 struct iwl_priv *priv = data;
4062 struct iwl_rx_queue *rxq = &priv->rxq;
4063 struct list_head *element;
4064 struct iwl_rx_mem_buffer *rxb;
4065 unsigned long flags;
4066 spin_lock_irqsave(&rxq->lock, flags);
4067 while (!list_empty(&rxq->rx_used)) {
4068 element = rxq->rx_used.next;
4069 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
4070 rxb->skb =
4071 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4072 if (!rxb->skb) {
4073 if (net_ratelimit())
4074 printk(KERN_CRIT DRV_NAME
4075 ": Can not allocate SKB buffers\n");
4076 /* We don't reschedule replenish work here -- we will
4077 * call the restock method and if it still needs
4078 * more buffers it will schedule replenish */
4079 break;
4080 }
4081 priv->alloc_rxb_skb++;
4082 list_del(element);
4083 rxb->dma_addr =
4084 pci_map_single(priv->pci_dev, rxb->skb->data,
4085 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4086 list_add_tail(&rxb->list, &rxq->rx_free);
4087 rxq->free_count++;
4088 }
4089 spin_unlock_irqrestore(&rxq->lock, flags);
4090
4091 spin_lock_irqsave(&priv->lock, flags);
4092 iwl_rx_queue_restock(priv);
4093 spin_unlock_irqrestore(&priv->lock, flags);
4094 }
4095
4096 /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
4097 * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
4098 * This free routine walks the list of POOL entries and if SKB is set to
4099 * non NULL it is unmapped and freed
4100 */
4101 void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4102 {
4103 int i;
4104 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4105 if (rxq->pool[i].skb != NULL) {
4106 pci_unmap_single(priv->pci_dev,
4107 rxq->pool[i].dma_addr,
4108 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4109 dev_kfree_skb(rxq->pool[i].skb);
4110 }
4111 }
4112
4113 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4114 rxq->dma_addr);
4115 rxq->bd = NULL;
4116 }
4117
4118 int iwl_rx_queue_alloc(struct iwl_priv *priv)
4119 {
4120 struct iwl_rx_queue *rxq = &priv->rxq;
4121 struct pci_dev *dev = priv->pci_dev;
4122 int i;
4123
4124 spin_lock_init(&rxq->lock);
4125 INIT_LIST_HEAD(&rxq->rx_free);
4126 INIT_LIST_HEAD(&rxq->rx_used);
4127 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4128 if (!rxq->bd)
4129 return -ENOMEM;
4130 /* Fill the rx_used queue with _all_ of the Rx buffers */
4131 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4132 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4133 /* Set us so that we have processed and used all buffers, but have
4134 * not restocked the Rx queue with fresh buffers */
4135 rxq->read = rxq->write = 0;
4136 rxq->free_count = 0;
4137 rxq->need_update = 0;
4138 return 0;
4139 }
4140
4141 void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
4142 {
4143 unsigned long flags;
4144 int i;
4145 spin_lock_irqsave(&rxq->lock, flags);
4146 INIT_LIST_HEAD(&rxq->rx_free);
4147 INIT_LIST_HEAD(&rxq->rx_used);
4148 /* Fill the rx_used queue with _all_ of the Rx buffers */
4149 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4150 /* In the reset function, these buffers may have been allocated
4151 * to an SKB, so we need to unmap and free potential storage */
4152 if (rxq->pool[i].skb != NULL) {
4153 pci_unmap_single(priv->pci_dev,
4154 rxq->pool[i].dma_addr,
4155 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4156 priv->alloc_rxb_skb--;
4157 dev_kfree_skb(rxq->pool[i].skb);
4158 rxq->pool[i].skb = NULL;
4159 }
4160 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4161 }
4162
4163 /* Set us so that we have processed and used all buffers, but have
4164 * not restocked the Rx queue with fresh buffers */
4165 rxq->read = rxq->write = 0;
4166 rxq->free_count = 0;
4167 spin_unlock_irqrestore(&rxq->lock, flags);
4168 }
4169
4170 /* Convert linear signal-to-noise ratio into dB */
4171 static u8 ratio2dB[100] = {
4172 /* 0 1 2 3 4 5 6 7 8 9 */
4173 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4174 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4175 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4176 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4177 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4178 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4179 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4180 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4181 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4182 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4183 };
4184
4185 /* Calculates a relative dB value from a ratio of linear
4186 * (i.e. not dB) signal levels.
4187 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
4188 int iwl_calc_db_from_ratio(int sig_ratio)
4189 {
4190 /* Anything above 1000:1 just report as 60 dB */
4191 if (sig_ratio > 1000)
4192 return 60;
4193
4194 /* Above 100:1, divide by 10 and use table,
4195 * add 20 dB to make up for divide by 10 */
4196 if (sig_ratio > 100)
4197 return (20 + (int)ratio2dB[sig_ratio/10]);
4198
4199 /* We shouldn't see this */
4200 if (sig_ratio < 1)
4201 return 0;
4202
4203 /* Use table for ratios 1:1 - 99:1 */
4204 return (int)ratio2dB[sig_ratio];
4205 }
4206
4207 #define PERFECT_RSSI (-20) /* dBm */
4208 #define WORST_RSSI (-95) /* dBm */
4209 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4210
4211 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
4212 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4213 * about formulas used below. */
4214 int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
4215 {
4216 int sig_qual;
4217 int degradation = PERFECT_RSSI - rssi_dbm;
4218
4219 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4220 * as indicator; formula is (signal dbm - noise dbm).
4221 * SNR at or above 40 is a great signal (100%).
4222 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4223 * Weakest usable signal is usually 10 - 15 dB SNR. */
4224 if (noise_dbm) {
4225 if (rssi_dbm - noise_dbm >= 40)
4226 return 100;
4227 else if (rssi_dbm < noise_dbm)
4228 return 0;
4229 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4230
4231 /* Else use just the signal level.
4232 * This formula is a least squares fit of data points collected and
4233 * compared with a reference system that had a percentage (%) display
4234 * for signal quality. */
4235 } else
4236 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4237 (15 * RSSI_RANGE + 62 * degradation)) /
4238 (RSSI_RANGE * RSSI_RANGE);
4239
4240 if (sig_qual > 100)
4241 sig_qual = 100;
4242 else if (sig_qual < 1)
4243 sig_qual = 0;
4244
4245 return sig_qual;
4246 }
4247
4248 /**
4249 * iwl_rx_handle - Main entry function for receiving responses from the uCode
4250 *
4251 * Uses the priv->rx_handlers callback function array to invoke
4252 * the appropriate handlers, including command responses,
4253 * frame-received notifications, and other notifications.
4254 */
4255 static void iwl_rx_handle(struct iwl_priv *priv)
4256 {
4257 struct iwl_rx_mem_buffer *rxb;
4258 struct iwl_rx_packet *pkt;
4259 struct iwl_rx_queue *rxq = &priv->rxq;
4260 u32 r, i;
4261 int reclaim;
4262 unsigned long flags;
4263
4264 r = iwl_hw_get_rx_read(priv);
4265 i = rxq->read;
4266
4267 /* Rx interrupt, but nothing sent from uCode */
4268 if (i == r)
4269 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4270
4271 while (i != r) {
4272 rxb = rxq->queue[i];
4273
4274 /* If an RXB doesn't have a queue slot associated with it
4275 * then a bug has been introduced in the queue refilling
4276 * routines -- catch it here */
4277 BUG_ON(rxb == NULL);
4278
4279 rxq->queue[i] = NULL;
4280
4281 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4282 IWL_RX_BUF_SIZE,
4283 PCI_DMA_FROMDEVICE);
4284 pkt = (struct iwl_rx_packet *)rxb->skb->data;
4285
4286 /* Reclaim a command buffer only if this packet is a response
4287 * to a (driver-originated) command.
4288 * If the packet (e.g. Rx frame) originated from uCode,
4289 * there is no command buffer to reclaim.
4290 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4291 * but apparently a few don't get set; catch them here. */
4292 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4293 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4294 (pkt->hdr.cmd != REPLY_TX);
4295
4296 /* Based on type of command response or notification,
4297 * handle those that need handling via function in
4298 * rx_handlers table. See iwl_setup_rx_handlers() */
4299 if (priv->rx_handlers[pkt->hdr.cmd]) {
4300 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4301 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4302 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4303 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4304 } else {
4305 /* No handling needed */
4306 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4307 "r %d i %d No handler needed for %s, 0x%02x\n",
4308 r, i, get_cmd_string(pkt->hdr.cmd),
4309 pkt->hdr.cmd);
4310 }
4311
4312 if (reclaim) {
4313 /* Invoke any callbacks, transfer the skb to caller,
4314 * and fire off the (possibly) blocking iwl_send_cmd()
4315 * as we reclaim the driver command queue */
4316 if (rxb && rxb->skb)
4317 iwl_tx_cmd_complete(priv, rxb);
4318 else
4319 IWL_WARNING("Claim null rxb?\n");
4320 }
4321
4322 /* For now we just don't re-use anything. We can tweak this
4323 * later to try and re-use notification packets and SKBs that
4324 * fail to Rx correctly */
4325 if (rxb->skb != NULL) {
4326 priv->alloc_rxb_skb--;
4327 dev_kfree_skb_any(rxb->skb);
4328 rxb->skb = NULL;
4329 }
4330
4331 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4332 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4333 spin_lock_irqsave(&rxq->lock, flags);
4334 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4335 spin_unlock_irqrestore(&rxq->lock, flags);
4336 i = (i + 1) & RX_QUEUE_MASK;
4337 }
4338
4339 /* Backtrack one entry */
4340 priv->rxq.read = i;
4341 iwl_rx_queue_restock(priv);
4342 }
4343
4344 int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
4345 struct iwl_tx_queue *txq)
4346 {
4347 u32 reg = 0;
4348 int rc = 0;
4349 int txq_id = txq->q.id;
4350
4351 if (txq->need_update == 0)
4352 return rc;
4353
4354 /* if we're trying to save power */
4355 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4356 /* wake up nic if it's powered down ...
4357 * uCode will wake up, and interrupt us again, so next
4358 * time we'll skip this part. */
4359 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
4360
4361 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4362 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
4363 iwl_set_bit(priv, CSR_GP_CNTRL,
4364 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4365 return rc;
4366 }
4367
4368 /* restore this queue's parameters in nic hardware. */
4369 rc = iwl_grab_restricted_access(priv);
4370 if (rc)
4371 return rc;
4372 iwl_write_restricted(priv, HBUS_TARG_WRPTR,
4373 txq->q.write_ptr | (txq_id << 8));
4374 iwl_release_restricted_access(priv);
4375
4376 /* else not in power-save mode, uCode will never sleep when we're
4377 * trying to tx (during RFKILL, we're not trying to tx). */
4378 } else
4379 iwl_write32(priv, HBUS_TARG_WRPTR,
4380 txq->q.write_ptr | (txq_id << 8));
4381
4382 txq->need_update = 0;
4383
4384 return rc;
4385 }
4386
4387 #ifdef CONFIG_IWLWIFI_DEBUG
4388 static void iwl_print_rx_config_cmd(struct iwl_rxon_cmd *rxon)
4389 {
4390 DECLARE_MAC_BUF(mac);
4391
4392 IWL_DEBUG_RADIO("RX CONFIG:\n");
4393 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
4394 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4395 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4396 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4397 le32_to_cpu(rxon->filter_flags));
4398 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4399 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4400 rxon->ofdm_basic_rates);
4401 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
4402 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4403 print_mac(mac, rxon->node_addr));
4404 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4405 print_mac(mac, rxon->bssid_addr));
4406 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4407 }
4408 #endif
4409
4410 static void iwl_enable_interrupts(struct iwl_priv *priv)
4411 {
4412 IWL_DEBUG_ISR("Enabling interrupts\n");
4413 set_bit(STATUS_INT_ENABLED, &priv->status);
4414 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
4415 }
4416
4417 static inline void iwl_disable_interrupts(struct iwl_priv *priv)
4418 {
4419 clear_bit(STATUS_INT_ENABLED, &priv->status);
4420
4421 /* disable interrupts from uCode/NIC to host */
4422 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4423
4424 /* acknowledge/clear/reset any interrupts still pending
4425 * from uCode or flow handler (Rx/Tx DMA) */
4426 iwl_write32(priv, CSR_INT, 0xffffffff);
4427 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
4428 IWL_DEBUG_ISR("Disabled interrupts\n");
4429 }
4430
4431 static const char *desc_lookup(int i)
4432 {
4433 switch (i) {
4434 case 1:
4435 return "FAIL";
4436 case 2:
4437 return "BAD_PARAM";
4438 case 3:
4439 return "BAD_CHECKSUM";
4440 case 4:
4441 return "NMI_INTERRUPT";
4442 case 5:
4443 return "SYSASSERT";
4444 case 6:
4445 return "FATAL_ERROR";
4446 }
4447
4448 return "UNKNOWN";
4449 }
4450
4451 #define ERROR_START_OFFSET (1 * sizeof(u32))
4452 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
4453
4454 static void iwl_dump_nic_error_log(struct iwl_priv *priv)
4455 {
4456 u32 i;
4457 u32 desc, time, count, base, data1;
4458 u32 blink1, blink2, ilink1, ilink2;
4459 int rc;
4460
4461 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4462
4463 if (!iwl_hw_valid_rtc_data_addr(base)) {
4464 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4465 return;
4466 }
4467
4468 rc = iwl_grab_restricted_access(priv);
4469 if (rc) {
4470 IWL_WARNING("Can not read from adapter at this time.\n");
4471 return;
4472 }
4473
4474 count = iwl_read_restricted_mem(priv, base);
4475
4476 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4477 IWL_ERROR("Start IWL Error Log Dump:\n");
4478 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4479 priv->status, priv->config, count);
4480 }
4481
4482 IWL_ERROR("Desc Time asrtPC blink2 "
4483 "ilink1 nmiPC Line\n");
4484 for (i = ERROR_START_OFFSET;
4485 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
4486 i += ERROR_ELEM_SIZE) {
4487 desc = iwl_read_restricted_mem(priv, base + i);
4488 time =
4489 iwl_read_restricted_mem(priv, base + i + 1 * sizeof(u32));
4490 blink1 =
4491 iwl_read_restricted_mem(priv, base + i + 2 * sizeof(u32));
4492 blink2 =
4493 iwl_read_restricted_mem(priv, base + i + 3 * sizeof(u32));
4494 ilink1 =
4495 iwl_read_restricted_mem(priv, base + i + 4 * sizeof(u32));
4496 ilink2 =
4497 iwl_read_restricted_mem(priv, base + i + 5 * sizeof(u32));
4498 data1 =
4499 iwl_read_restricted_mem(priv, base + i + 6 * sizeof(u32));
4500
4501 IWL_ERROR
4502 ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
4503 desc_lookup(desc), desc, time, blink1, blink2,
4504 ilink1, ilink2, data1);
4505 }
4506
4507 iwl_release_restricted_access(priv);
4508
4509 }
4510
4511 #define EVENT_START_OFFSET (4 * sizeof(u32))
4512
4513 /**
4514 * iwl_print_event_log - Dump error event log to syslog
4515 *
4516 * NOTE: Must be called with iwl_grab_restricted_access() already obtained!
4517 */
4518 static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
4519 u32 num_events, u32 mode)
4520 {
4521 u32 i;
4522 u32 base; /* SRAM byte address of event log header */
4523 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4524 u32 ptr; /* SRAM byte address of log data */
4525 u32 ev, time, data; /* event log data */
4526
4527 if (num_events == 0)
4528 return;
4529
4530 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4531
4532 if (mode == 0)
4533 event_size = 2 * sizeof(u32);
4534 else
4535 event_size = 3 * sizeof(u32);
4536
4537 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4538
4539 /* "time" is actually "data" for mode 0 (no timestamp).
4540 * place event id # at far right for easier visual parsing. */
4541 for (i = 0; i < num_events; i++) {
4542 ev = iwl_read_restricted_mem(priv, ptr);
4543 ptr += sizeof(u32);
4544 time = iwl_read_restricted_mem(priv, ptr);
4545 ptr += sizeof(u32);
4546 if (mode == 0)
4547 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4548 else {
4549 data = iwl_read_restricted_mem(priv, ptr);
4550 ptr += sizeof(u32);
4551 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4552 }
4553 }
4554 }
4555
4556 static void iwl_dump_nic_event_log(struct iwl_priv *priv)
4557 {
4558 int rc;
4559 u32 base; /* SRAM byte address of event log header */
4560 u32 capacity; /* event log capacity in # entries */
4561 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4562 u32 num_wraps; /* # times uCode wrapped to top of log */
4563 u32 next_entry; /* index of next entry to be written by uCode */
4564 u32 size; /* # entries that we'll print */
4565
4566 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4567 if (!iwl_hw_valid_rtc_data_addr(base)) {
4568 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4569 return;
4570 }
4571
4572 rc = iwl_grab_restricted_access(priv);
4573 if (rc) {
4574 IWL_WARNING("Can not read from adapter at this time.\n");
4575 return;
4576 }
4577
4578 /* event log header */
4579 capacity = iwl_read_restricted_mem(priv, base);
4580 mode = iwl_read_restricted_mem(priv, base + (1 * sizeof(u32)));
4581 num_wraps = iwl_read_restricted_mem(priv, base + (2 * sizeof(u32)));
4582 next_entry = iwl_read_restricted_mem(priv, base + (3 * sizeof(u32)));
4583
4584 size = num_wraps ? capacity : next_entry;
4585
4586 /* bail out if nothing in log */
4587 if (size == 0) {
4588 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
4589 iwl_release_restricted_access(priv);
4590 return;
4591 }
4592
4593 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
4594 size, num_wraps);
4595
4596 /* if uCode has wrapped back to top of log, start at the oldest entry,
4597 * i.e the next one that uCode would fill. */
4598 if (num_wraps)
4599 iwl_print_event_log(priv, next_entry,
4600 capacity - next_entry, mode);
4601
4602 /* (then/else) start at top of log */
4603 iwl_print_event_log(priv, 0, next_entry, mode);
4604
4605 iwl_release_restricted_access(priv);
4606 }
4607
4608 /**
4609 * iwl_irq_handle_error - called for HW or SW error interrupt from card
4610 */
4611 static void iwl_irq_handle_error(struct iwl_priv *priv)
4612 {
4613 /* Set the FW error flag -- cleared on iwl_down */
4614 set_bit(STATUS_FW_ERROR, &priv->status);
4615
4616 /* Cancel currently queued command. */
4617 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4618
4619 #ifdef CONFIG_IWLWIFI_DEBUG
4620 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
4621 iwl_dump_nic_error_log(priv);
4622 iwl_dump_nic_event_log(priv);
4623 iwl_print_rx_config_cmd(&priv->staging_rxon);
4624 }
4625 #endif
4626
4627 wake_up_interruptible(&priv->wait_command_queue);
4628
4629 /* Keep the restart process from trying to send host
4630 * commands by clearing the INIT status bit */
4631 clear_bit(STATUS_READY, &priv->status);
4632
4633 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4634 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4635 "Restarting adapter due to uCode error.\n");
4636
4637 if (iwl_is_associated(priv)) {
4638 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4639 sizeof(priv->recovery_rxon));
4640 priv->error_recovering = 1;
4641 }
4642 queue_work(priv->workqueue, &priv->restart);
4643 }
4644 }
4645
4646 static void iwl_error_recovery(struct iwl_priv *priv)
4647 {
4648 unsigned long flags;
4649
4650 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4651 sizeof(priv->staging_rxon));
4652 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4653 iwl_commit_rxon(priv);
4654
4655 iwl_add_station(priv, priv->bssid, 1, 0);
4656
4657 spin_lock_irqsave(&priv->lock, flags);
4658 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4659 priv->error_recovering = 0;
4660 spin_unlock_irqrestore(&priv->lock, flags);
4661 }
4662
4663 static void iwl_irq_tasklet(struct iwl_priv *priv)
4664 {
4665 u32 inta, handled = 0;
4666 u32 inta_fh;
4667 unsigned long flags;
4668 #ifdef CONFIG_IWLWIFI_DEBUG
4669 u32 inta_mask;
4670 #endif
4671
4672 spin_lock_irqsave(&priv->lock, flags);
4673
4674 /* Ack/clear/reset pending uCode interrupts.
4675 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4676 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
4677 inta = iwl_read32(priv, CSR_INT);
4678 iwl_write32(priv, CSR_INT, inta);
4679
4680 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4681 * Any new interrupts that happen after this, either while we're
4682 * in this tasklet, or later, will show up in next ISR/tasklet. */
4683 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4684 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
4685
4686 #ifdef CONFIG_IWLWIFI_DEBUG
4687 if (iwl_debug_level & IWL_DL_ISR) {
4688 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4689 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4690 inta, inta_mask, inta_fh);
4691 }
4692 #endif
4693
4694 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4695 * atomic, make sure that inta covers all the interrupts that
4696 * we've discovered, even if FH interrupt came in just after
4697 * reading CSR_INT. */
4698 if (inta_fh & CSR_FH_INT_RX_MASK)
4699 inta |= CSR_INT_BIT_FH_RX;
4700 if (inta_fh & CSR_FH_INT_TX_MASK)
4701 inta |= CSR_INT_BIT_FH_TX;
4702
4703 /* Now service all interrupt bits discovered above. */
4704 if (inta & CSR_INT_BIT_HW_ERR) {
4705 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4706
4707 /* Tell the device to stop sending interrupts */
4708 iwl_disable_interrupts(priv);
4709
4710 iwl_irq_handle_error(priv);
4711
4712 handled |= CSR_INT_BIT_HW_ERR;
4713
4714 spin_unlock_irqrestore(&priv->lock, flags);
4715
4716 return;
4717 }
4718
4719 #ifdef CONFIG_IWLWIFI_DEBUG
4720 if (iwl_debug_level & (IWL_DL_ISR)) {
4721 /* NIC fires this, but we don't use it, redundant with WAKEUP */
4722 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
4723 IWL_DEBUG_ISR("Microcode started or stopped.\n");
4724
4725 /* Alive notification via Rx interrupt will do the real work */
4726 if (inta & CSR_INT_BIT_ALIVE)
4727 IWL_DEBUG_ISR("Alive interrupt\n");
4728 }
4729 #endif
4730 /* Safely ignore these bits for debug checks below */
4731 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
4732
4733 /* HW RF KILL switch toggled (4965 only) */
4734 if (inta & CSR_INT_BIT_RF_KILL) {
4735 int hw_rf_kill = 0;
4736 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
4737 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4738 hw_rf_kill = 1;
4739
4740 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4741 "RF_KILL bit toggled to %s.\n",
4742 hw_rf_kill ? "disable radio":"enable radio");
4743
4744 /* Queue restart only if RF_KILL switch was set to "kill"
4745 * when we loaded driver, and is now set to "enable".
4746 * After we're Alive, RF_KILL gets handled by
4747 * iwl_rx_card_state_notif() */
4748 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4749 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4750 queue_work(priv->workqueue, &priv->restart);
4751 }
4752
4753 handled |= CSR_INT_BIT_RF_KILL;
4754 }
4755
4756 /* Chip got too hot and stopped itself (4965 only) */
4757 if (inta & CSR_INT_BIT_CT_KILL) {
4758 IWL_ERROR("Microcode CT kill error detected.\n");
4759 handled |= CSR_INT_BIT_CT_KILL;
4760 }
4761
4762 /* Error detected by uCode */
4763 if (inta & CSR_INT_BIT_SW_ERR) {
4764 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4765 inta);
4766 iwl_irq_handle_error(priv);
4767 handled |= CSR_INT_BIT_SW_ERR;
4768 }
4769
4770 /* uCode wakes up after power-down sleep */
4771 if (inta & CSR_INT_BIT_WAKEUP) {
4772 IWL_DEBUG_ISR("Wakeup interrupt\n");
4773 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
4774 iwl_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4775 iwl_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4776 iwl_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4777 iwl_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4778 iwl_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4779 iwl_tx_queue_update_write_ptr(priv, &priv->txq[5]);
4780
4781 handled |= CSR_INT_BIT_WAKEUP;
4782 }
4783
4784 /* All uCode command responses, including Tx command responses,
4785 * Rx "responses" (frame-received notification), and other
4786 * notifications from uCode come through here*/
4787 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
4788 iwl_rx_handle(priv);
4789 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4790 }
4791
4792 if (inta & CSR_INT_BIT_FH_TX) {
4793 IWL_DEBUG_ISR("Tx interrupt\n");
4794
4795 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4796 if (!iwl_grab_restricted_access(priv)) {
4797 iwl_write_restricted(priv,
4798 FH_TCSR_CREDIT
4799 (ALM_FH_SRVC_CHNL), 0x0);
4800 iwl_release_restricted_access(priv);
4801 }
4802 handled |= CSR_INT_BIT_FH_TX;
4803 }
4804
4805 if (inta & ~handled)
4806 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4807
4808 if (inta & ~CSR_INI_SET_MASK) {
4809 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4810 inta & ~CSR_INI_SET_MASK);
4811 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4812 }
4813
4814 /* Re-enable all interrupts */
4815 iwl_enable_interrupts(priv);
4816
4817 #ifdef CONFIG_IWLWIFI_DEBUG
4818 if (iwl_debug_level & (IWL_DL_ISR)) {
4819 inta = iwl_read32(priv, CSR_INT);
4820 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4821 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4822 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4823 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4824 }
4825 #endif
4826 spin_unlock_irqrestore(&priv->lock, flags);
4827 }
4828
4829 static irqreturn_t iwl_isr(int irq, void *data)
4830 {
4831 struct iwl_priv *priv = data;
4832 u32 inta, inta_mask;
4833 u32 inta_fh;
4834 if (!priv)
4835 return IRQ_NONE;
4836
4837 spin_lock(&priv->lock);
4838
4839 /* Disable (but don't clear!) interrupts here to avoid
4840 * back-to-back ISRs and sporadic interrupts from our NIC.
4841 * If we have something to service, the tasklet will re-enable ints.
4842 * If we *don't* have something, we'll re-enable before leaving here. */
4843 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4844 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
4845
4846 /* Discover which interrupts are active/pending */
4847 inta = iwl_read32(priv, CSR_INT);
4848 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4849
4850 /* Ignore interrupt if there's nothing in NIC to service.
4851 * This may be due to IRQ shared with another device,
4852 * or due to sporadic interrupts thrown from our NIC. */
4853 if (!inta && !inta_fh) {
4854 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4855 goto none;
4856 }
4857
4858 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4859 /* Hardware disappeared */
4860 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
4861 goto unplugged;
4862 }
4863
4864 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4865 inta, inta_mask, inta_fh);
4866
4867 /* iwl_irq_tasklet() will service interrupts and re-enable them */
4868 tasklet_schedule(&priv->irq_tasklet);
4869 unplugged:
4870 spin_unlock(&priv->lock);
4871
4872 return IRQ_HANDLED;
4873
4874 none:
4875 /* re-enable interrupts here since we don't have anything to service. */
4876 iwl_enable_interrupts(priv);
4877 spin_unlock(&priv->lock);
4878 return IRQ_NONE;
4879 }
4880
4881 /************************** EEPROM BANDS ****************************
4882 *
4883 * The iwl_eeprom_band definitions below provide the mapping from the
4884 * EEPROM contents to the specific channel number supported for each
4885 * band.
4886 *
4887 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
4888 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4889 * The specific geography and calibration information for that channel
4890 * is contained in the eeprom map itself.
4891 *
4892 * During init, we copy the eeprom information and channel map
4893 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4894 *
4895 * channel_map_24/52 provides the index in the channel_info array for a
4896 * given channel. We have to have two separate maps as there is channel
4897 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4898 * band_2
4899 *
4900 * A value of 0xff stored in the channel_map indicates that the channel
4901 * is not supported by the hardware at all.
4902 *
4903 * A value of 0xfe in the channel_map indicates that the channel is not
4904 * valid for Tx with the current hardware. This means that
4905 * while the system can tune and receive on a given channel, it may not
4906 * be able to associate or transmit any frames on that
4907 * channel. There is no corresponding channel information for that
4908 * entry.
4909 *
4910 *********************************************************************/
4911
4912 /* 2.4 GHz */
4913 static const u8 iwl_eeprom_band_1[14] = {
4914 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4915 };
4916
4917 /* 5.2 GHz bands */
4918 static const u8 iwl_eeprom_band_2[] = {
4919 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4920 };
4921
4922 static const u8 iwl_eeprom_band_3[] = { /* 5205-5320MHz */
4923 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4924 };
4925
4926 static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
4927 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4928 };
4929
4930 static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
4931 145, 149, 153, 157, 161, 165
4932 };
4933
4934 static void iwl_init_band_reference(const struct iwl_priv *priv, int band,
4935 int *eeprom_ch_count,
4936 const struct iwl_eeprom_channel
4937 **eeprom_ch_info,
4938 const u8 **eeprom_ch_index)
4939 {
4940 switch (band) {
4941 case 1: /* 2.4GHz band */
4942 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
4943 *eeprom_ch_info = priv->eeprom.band_1_channels;
4944 *eeprom_ch_index = iwl_eeprom_band_1;
4945 break;
4946 case 2: /* 5.2GHz band */
4947 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
4948 *eeprom_ch_info = priv->eeprom.band_2_channels;
4949 *eeprom_ch_index = iwl_eeprom_band_2;
4950 break;
4951 case 3: /* 5.2GHz band */
4952 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
4953 *eeprom_ch_info = priv->eeprom.band_3_channels;
4954 *eeprom_ch_index = iwl_eeprom_band_3;
4955 break;
4956 case 4: /* 5.2GHz band */
4957 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
4958 *eeprom_ch_info = priv->eeprom.band_4_channels;
4959 *eeprom_ch_index = iwl_eeprom_band_4;
4960 break;
4961 case 5: /* 5.2GHz band */
4962 *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
4963 *eeprom_ch_info = priv->eeprom.band_5_channels;
4964 *eeprom_ch_index = iwl_eeprom_band_5;
4965 break;
4966 default:
4967 BUG();
4968 return;
4969 }
4970 }
4971
4972 const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
4973 int phymode, u16 channel)
4974 {
4975 int i;
4976
4977 switch (phymode) {
4978 case MODE_IEEE80211A:
4979 for (i = 14; i < priv->channel_count; i++) {
4980 if (priv->channel_info[i].channel == channel)
4981 return &priv->channel_info[i];
4982 }
4983 break;
4984
4985 case MODE_IEEE80211B:
4986 case MODE_IEEE80211G:
4987 if (channel >= 1 && channel <= 14)
4988 return &priv->channel_info[channel - 1];
4989 break;
4990
4991 }
4992
4993 return NULL;
4994 }
4995
4996 #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4997 ? # x " " : "")
4998
4999 static int iwl_init_channel_map(struct iwl_priv *priv)
5000 {
5001 int eeprom_ch_count = 0;
5002 const u8 *eeprom_ch_index = NULL;
5003 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
5004 int band, ch;
5005 struct iwl_channel_info *ch_info;
5006
5007 if (priv->channel_count) {
5008 IWL_DEBUG_INFO("Channel map already initialized.\n");
5009 return 0;
5010 }
5011
5012 if (priv->eeprom.version < 0x2f) {
5013 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5014 priv->eeprom.version);
5015 return -EINVAL;
5016 }
5017
5018 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5019
5020 priv->channel_count =
5021 ARRAY_SIZE(iwl_eeprom_band_1) +
5022 ARRAY_SIZE(iwl_eeprom_band_2) +
5023 ARRAY_SIZE(iwl_eeprom_band_3) +
5024 ARRAY_SIZE(iwl_eeprom_band_4) +
5025 ARRAY_SIZE(iwl_eeprom_band_5);
5026
5027 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5028
5029 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
5030 priv->channel_count, GFP_KERNEL);
5031 if (!priv->channel_info) {
5032 IWL_ERROR("Could not allocate channel_info\n");
5033 priv->channel_count = 0;
5034 return -ENOMEM;
5035 }
5036
5037 ch_info = priv->channel_info;
5038
5039 /* Loop through the 5 EEPROM bands adding them in order to the
5040 * channel map we maintain (that contains additional information than
5041 * what just in the EEPROM) */
5042 for (band = 1; band <= 5; band++) {
5043
5044 iwl_init_band_reference(priv, band, &eeprom_ch_count,
5045 &eeprom_ch_info, &eeprom_ch_index);
5046
5047 /* Loop through each band adding each of the channels */
5048 for (ch = 0; ch < eeprom_ch_count; ch++) {
5049 ch_info->channel = eeprom_ch_index[ch];
5050 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5051 MODE_IEEE80211A;
5052
5053 /* permanently store EEPROM's channel regulatory flags
5054 * and max power in channel info database. */
5055 ch_info->eeprom = eeprom_ch_info[ch];
5056
5057 /* Copy the run-time flags so they are there even on
5058 * invalid channels */
5059 ch_info->flags = eeprom_ch_info[ch].flags;
5060
5061 if (!(is_channel_valid(ch_info))) {
5062 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5063 "No traffic\n",
5064 ch_info->channel,
5065 ch_info->flags,
5066 is_channel_a_band(ch_info) ?
5067 "5.2" : "2.4");
5068 ch_info++;
5069 continue;
5070 }
5071
5072 /* Initialize regulatory-based run-time data */
5073 ch_info->max_power_avg = ch_info->curr_txpow =
5074 eeprom_ch_info[ch].max_power_avg;
5075 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5076 ch_info->min_power = 0;
5077
5078 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5079 " %ddBm): Ad-Hoc %ssupported\n",
5080 ch_info->channel,
5081 is_channel_a_band(ch_info) ?
5082 "5.2" : "2.4",
5083 CHECK_AND_PRINT(IBSS),
5084 CHECK_AND_PRINT(ACTIVE),
5085 CHECK_AND_PRINT(RADAR),
5086 CHECK_AND_PRINT(WIDE),
5087 CHECK_AND_PRINT(NARROW),
5088 CHECK_AND_PRINT(DFS),
5089 eeprom_ch_info[ch].flags,
5090 eeprom_ch_info[ch].max_power_avg,
5091 ((eeprom_ch_info[ch].
5092 flags & EEPROM_CHANNEL_IBSS)
5093 && !(eeprom_ch_info[ch].
5094 flags & EEPROM_CHANNEL_RADAR))
5095 ? "" : "not ");
5096
5097 /* Set the user_txpower_limit to the highest power
5098 * supported by any channel */
5099 if (eeprom_ch_info[ch].max_power_avg >
5100 priv->user_txpower_limit)
5101 priv->user_txpower_limit =
5102 eeprom_ch_info[ch].max_power_avg;
5103
5104 ch_info++;
5105 }
5106 }
5107
5108 if (iwl3945_txpower_set_from_eeprom(priv))
5109 return -EIO;
5110
5111 return 0;
5112 }
5113
5114 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5115 * sending probe req. This should be set long enough to hear probe responses
5116 * from more than one AP. */
5117 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5118 #define IWL_ACTIVE_DWELL_TIME_52 (10)
5119
5120 /* For faster active scanning, scan will move to the next channel if fewer than
5121 * PLCP_QUIET_THRESH packets are heard on this channel within
5122 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5123 * time if it's a quiet channel (nothing responded to our probe, and there's
5124 * no other traffic).
5125 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5126 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5127 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5128
5129 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5130 * Must be set longer than active dwell time.
5131 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5132 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5133 #define IWL_PASSIVE_DWELL_TIME_52 (10)
5134 #define IWL_PASSIVE_DWELL_BASE (100)
5135 #define IWL_CHANNEL_TUNE_TIME 5
5136
5137 static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, int phymode)
5138 {
5139 if (phymode == MODE_IEEE80211A)
5140 return IWL_ACTIVE_DWELL_TIME_52;
5141 else
5142 return IWL_ACTIVE_DWELL_TIME_24;
5143 }
5144
5145 static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, int phymode)
5146 {
5147 u16 active = iwl_get_active_dwell_time(priv, phymode);
5148 u16 passive = (phymode != MODE_IEEE80211A) ?
5149 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5150 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5151
5152 if (iwl_is_associated(priv)) {
5153 /* If we're associated, we clamp the maximum passive
5154 * dwell time to be 98% of the beacon interval (minus
5155 * 2 * channel tune time) */
5156 passive = priv->beacon_int;
5157 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5158 passive = IWL_PASSIVE_DWELL_BASE;
5159 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5160 }
5161
5162 if (passive <= active)
5163 passive = active + 1;
5164
5165 return passive;
5166 }
5167
5168 static int iwl_get_channels_for_scan(struct iwl_priv *priv, int phymode,
5169 u8 is_active, u8 direct_mask,
5170 struct iwl_scan_channel *scan_ch)
5171 {
5172 const struct ieee80211_channel *channels = NULL;
5173 const struct ieee80211_hw_mode *hw_mode;
5174 const struct iwl_channel_info *ch_info;
5175 u16 passive_dwell = 0;
5176 u16 active_dwell = 0;
5177 int added, i;
5178
5179 hw_mode = iwl_get_hw_mode(priv, phymode);
5180 if (!hw_mode)
5181 return 0;
5182
5183 channels = hw_mode->channels;
5184
5185 active_dwell = iwl_get_active_dwell_time(priv, phymode);
5186 passive_dwell = iwl_get_passive_dwell_time(priv, phymode);
5187
5188 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5189 if (channels[i].chan ==
5190 le16_to_cpu(priv->active_rxon.channel)) {
5191 if (iwl_is_associated(priv)) {
5192 IWL_DEBUG_SCAN
5193 ("Skipping current channel %d\n",
5194 le16_to_cpu(priv->active_rxon.channel));
5195 continue;
5196 }
5197 } else if (priv->only_active_channel)
5198 continue;
5199
5200 scan_ch->channel = channels[i].chan;
5201
5202 ch_info = iwl_get_channel_info(priv, phymode, scan_ch->channel);
5203 if (!is_channel_valid(ch_info)) {
5204 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5205 scan_ch->channel);
5206 continue;
5207 }
5208
5209 if (!is_active || is_channel_passive(ch_info) ||
5210 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5211 scan_ch->type = 0; /* passive */
5212 else
5213 scan_ch->type = 1; /* active */
5214
5215 if (scan_ch->type & 1)
5216 scan_ch->type |= (direct_mask << 1);
5217
5218 if (is_channel_narrow(ch_info))
5219 scan_ch->type |= (1 << 7);
5220
5221 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5222 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5223
5224 /* Set power levels to defaults */
5225 scan_ch->tpc.dsp_atten = 110;
5226 /* scan_pwr_info->tpc.dsp_atten; */
5227
5228 /*scan_pwr_info->tpc.tx_gain; */
5229 if (phymode == MODE_IEEE80211A)
5230 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5231 else {
5232 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5233 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
5234 * power level
5235 scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
5236 */
5237 }
5238
5239 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5240 scan_ch->channel,
5241 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5242 (scan_ch->type & 1) ?
5243 active_dwell : passive_dwell);
5244
5245 scan_ch++;
5246 added++;
5247 }
5248
5249 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5250 return added;
5251 }
5252
5253 static void iwl_reset_channel_flag(struct iwl_priv *priv)
5254 {
5255 int i, j;
5256 for (i = 0; i < 3; i++) {
5257 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5258 for (j = 0; j < hw_mode->num_channels; j++)
5259 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5260 }
5261 }
5262
5263 static void iwl_init_hw_rates(struct iwl_priv *priv,
5264 struct ieee80211_rate *rates)
5265 {
5266 int i;
5267
5268 for (i = 0; i < IWL_RATE_COUNT; i++) {
5269 rates[i].rate = iwl_rates[i].ieee * 5;
5270 rates[i].val = i; /* Rate scaling will work on indexes */
5271 rates[i].val2 = i;
5272 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5273 /* Only OFDM have the bits-per-symbol set */
5274 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5275 rates[i].flags |= IEEE80211_RATE_OFDM;
5276 else {
5277 /*
5278 * If CCK 1M then set rate flag to CCK else CCK_2
5279 * which is CCK | PREAMBLE2
5280 */
5281 rates[i].flags |= (iwl_rates[i].plcp == 10) ?
5282 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5283 }
5284
5285 /* Set up which ones are basic rates... */
5286 if (IWL_BASIC_RATES_MASK & (1 << i))
5287 rates[i].flags |= IEEE80211_RATE_BASIC;
5288 }
5289 }
5290
5291 /**
5292 * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
5293 */
5294 static int iwl_init_geos(struct iwl_priv *priv)
5295 {
5296 struct iwl_channel_info *ch;
5297 struct ieee80211_hw_mode *modes;
5298 struct ieee80211_channel *channels;
5299 struct ieee80211_channel *geo_ch;
5300 struct ieee80211_rate *rates;
5301 int i = 0;
5302 enum {
5303 A = 0,
5304 B = 1,
5305 G = 2,
5306 };
5307 int mode_count = 3;
5308
5309 if (priv->modes) {
5310 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5311 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5312 return 0;
5313 }
5314
5315 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5316 GFP_KERNEL);
5317 if (!modes)
5318 return -ENOMEM;
5319
5320 channels = kzalloc(sizeof(struct ieee80211_channel) *
5321 priv->channel_count, GFP_KERNEL);
5322 if (!channels) {
5323 kfree(modes);
5324 return -ENOMEM;
5325 }
5326
5327 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5328 GFP_KERNEL);
5329 if (!rates) {
5330 kfree(modes);
5331 kfree(channels);
5332 return -ENOMEM;
5333 }
5334
5335 /* 0 = 802.11a
5336 * 1 = 802.11b
5337 * 2 = 802.11g
5338 */
5339
5340 /* 5.2GHz channels start after the 2.4GHz channels */
5341 modes[A].mode = MODE_IEEE80211A;
5342 modes[A].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
5343 modes[A].rates = &rates[4];
5344 modes[A].num_rates = 8; /* just OFDM */
5345 modes[A].num_channels = 0;
5346
5347 modes[B].mode = MODE_IEEE80211B;
5348 modes[B].channels = channels;
5349 modes[B].rates = rates;
5350 modes[B].num_rates = 4; /* just CCK */
5351 modes[B].num_channels = 0;
5352
5353 modes[G].mode = MODE_IEEE80211G;
5354 modes[G].channels = channels;
5355 modes[G].rates = rates;
5356 modes[G].num_rates = 12; /* OFDM & CCK */
5357 modes[G].num_channels = 0;
5358
5359 priv->ieee_channels = channels;
5360 priv->ieee_rates = rates;
5361
5362 iwl_init_hw_rates(priv, rates);
5363
5364 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5365 ch = &priv->channel_info[i];
5366
5367 if (!is_channel_valid(ch)) {
5368 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5369 "skipping.\n",
5370 ch->channel, is_channel_a_band(ch) ?
5371 "5.2" : "2.4");
5372 continue;
5373 }
5374
5375 if (is_channel_a_band(ch))
5376 geo_ch = &modes[A].channels[modes[A].num_channels++];
5377 else {
5378 geo_ch = &modes[B].channels[modes[B].num_channels++];
5379 modes[G].num_channels++;
5380 }
5381
5382 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5383 geo_ch->chan = ch->channel;
5384 geo_ch->power_level = ch->max_power_avg;
5385 geo_ch->antenna_max = 0xff;
5386
5387 if (is_channel_valid(ch)) {
5388 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5389 if (ch->flags & EEPROM_CHANNEL_IBSS)
5390 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5391
5392 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5393 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5394
5395 if (ch->flags & EEPROM_CHANNEL_RADAR)
5396 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5397
5398 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5399 priv->max_channel_txpower_limit =
5400 ch->max_power_avg;
5401 }
5402
5403 geo_ch->val = geo_ch->flag;
5404 }
5405
5406 if ((modes[A].num_channels == 0) && priv->is_abg) {
5407 printk(KERN_INFO DRV_NAME
5408 ": Incorrectly detected BG card as ABG. Please send "
5409 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5410 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5411 priv->is_abg = 0;
5412 }
5413
5414 printk(KERN_INFO DRV_NAME
5415 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5416 modes[G].num_channels, modes[A].num_channels);
5417
5418 /*
5419 * NOTE: We register these in preference of order -- the
5420 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5421 * a phymode based on rates or AP capabilities but seems to
5422 * configure it purely on if the channel being configured
5423 * is supported by a mode -- and the first match is taken
5424 */
5425
5426 if (modes[G].num_channels)
5427 ieee80211_register_hwmode(priv->hw, &modes[G]);
5428 if (modes[B].num_channels)
5429 ieee80211_register_hwmode(priv->hw, &modes[B]);
5430 if (modes[A].num_channels)
5431 ieee80211_register_hwmode(priv->hw, &modes[A]);
5432
5433 priv->modes = modes;
5434 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5435
5436 return 0;
5437 }
5438
5439 /******************************************************************************
5440 *
5441 * uCode download functions
5442 *
5443 ******************************************************************************/
5444
5445 static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
5446 {
5447 if (priv->ucode_code.v_addr != NULL) {
5448 pci_free_consistent(priv->pci_dev,
5449 priv->ucode_code.len,
5450 priv->ucode_code.v_addr,
5451 priv->ucode_code.p_addr);
5452 priv->ucode_code.v_addr = NULL;
5453 }
5454 if (priv->ucode_data.v_addr != NULL) {
5455 pci_free_consistent(priv->pci_dev,
5456 priv->ucode_data.len,
5457 priv->ucode_data.v_addr,
5458 priv->ucode_data.p_addr);
5459 priv->ucode_data.v_addr = NULL;
5460 }
5461 if (priv->ucode_data_backup.v_addr != NULL) {
5462 pci_free_consistent(priv->pci_dev,
5463 priv->ucode_data_backup.len,
5464 priv->ucode_data_backup.v_addr,
5465 priv->ucode_data_backup.p_addr);
5466 priv->ucode_data_backup.v_addr = NULL;
5467 }
5468 if (priv->ucode_init.v_addr != NULL) {
5469 pci_free_consistent(priv->pci_dev,
5470 priv->ucode_init.len,
5471 priv->ucode_init.v_addr,
5472 priv->ucode_init.p_addr);
5473 priv->ucode_init.v_addr = NULL;
5474 }
5475 if (priv->ucode_init_data.v_addr != NULL) {
5476 pci_free_consistent(priv->pci_dev,
5477 priv->ucode_init_data.len,
5478 priv->ucode_init_data.v_addr,
5479 priv->ucode_init_data.p_addr);
5480 priv->ucode_init_data.v_addr = NULL;
5481 }
5482 if (priv->ucode_boot.v_addr != NULL) {
5483 pci_free_consistent(priv->pci_dev,
5484 priv->ucode_boot.len,
5485 priv->ucode_boot.v_addr,
5486 priv->ucode_boot.p_addr);
5487 priv->ucode_boot.v_addr = NULL;
5488 }
5489 }
5490
5491 /**
5492 * iwl_verify_inst_full - verify runtime uCode image in card vs. host,
5493 * looking at all data.
5494 */
5495 static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 * image, u32 len)
5496 {
5497 u32 val;
5498 u32 save_len = len;
5499 int rc = 0;
5500 u32 errcnt;
5501
5502 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5503
5504 rc = iwl_grab_restricted_access(priv);
5505 if (rc)
5506 return rc;
5507
5508 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
5509
5510 errcnt = 0;
5511 for (; len > 0; len -= sizeof(u32), image++) {
5512 /* read data comes through single port, auto-incr addr */
5513 /* NOTE: Use the debugless read so we don't flood kernel log
5514 * if IWL_DL_IO is set */
5515 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5516 if (val != le32_to_cpu(*image)) {
5517 IWL_ERROR("uCode INST section is invalid at "
5518 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5519 save_len - len, val, le32_to_cpu(*image));
5520 rc = -EIO;
5521 errcnt++;
5522 if (errcnt >= 20)
5523 break;
5524 }
5525 }
5526
5527 iwl_release_restricted_access(priv);
5528
5529 if (!errcnt)
5530 IWL_DEBUG_INFO
5531 ("ucode image in INSTRUCTION memory is good\n");
5532
5533 return rc;
5534 }
5535
5536
5537 /**
5538 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
5539 * using sample data 100 bytes apart. If these sample points are good,
5540 * it's a pretty good bet that everything between them is good, too.
5541 */
5542 static int iwl_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
5543 {
5544 u32 val;
5545 int rc = 0;
5546 u32 errcnt = 0;
5547 u32 i;
5548
5549 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5550
5551 rc = iwl_grab_restricted_access(priv);
5552 if (rc)
5553 return rc;
5554
5555 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5556 /* read data comes through single port, auto-incr addr */
5557 /* NOTE: Use the debugless read so we don't flood kernel log
5558 * if IWL_DL_IO is set */
5559 iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR,
5560 i + RTC_INST_LOWER_BOUND);
5561 val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
5562 if (val != le32_to_cpu(*image)) {
5563 #if 0 /* Enable this if you want to see details */
5564 IWL_ERROR("uCode INST section is invalid at "
5565 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5566 i, val, *image);
5567 #endif
5568 rc = -EIO;
5569 errcnt++;
5570 if (errcnt >= 3)
5571 break;
5572 }
5573 }
5574
5575 iwl_release_restricted_access(priv);
5576
5577 return rc;
5578 }
5579
5580
5581 /**
5582 * iwl_verify_ucode - determine which instruction image is in SRAM,
5583 * and verify its contents
5584 */
5585 static int iwl_verify_ucode(struct iwl_priv *priv)
5586 {
5587 __le32 *image;
5588 u32 len;
5589 int rc = 0;
5590
5591 /* Try bootstrap */
5592 image = (__le32 *)priv->ucode_boot.v_addr;
5593 len = priv->ucode_boot.len;
5594 rc = iwl_verify_inst_sparse(priv, image, len);
5595 if (rc == 0) {
5596 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5597 return 0;
5598 }
5599
5600 /* Try initialize */
5601 image = (__le32 *)priv->ucode_init.v_addr;
5602 len = priv->ucode_init.len;
5603 rc = iwl_verify_inst_sparse(priv, image, len);
5604 if (rc == 0) {
5605 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5606 return 0;
5607 }
5608
5609 /* Try runtime/protocol */
5610 image = (__le32 *)priv->ucode_code.v_addr;
5611 len = priv->ucode_code.len;
5612 rc = iwl_verify_inst_sparse(priv, image, len);
5613 if (rc == 0) {
5614 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5615 return 0;
5616 }
5617
5618 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5619
5620 /* Show first several data entries in instruction SRAM.
5621 * Selection of bootstrap image is arbitrary. */
5622 image = (__le32 *)priv->ucode_boot.v_addr;
5623 len = priv->ucode_boot.len;
5624 rc = iwl_verify_inst_full(priv, image, len);
5625
5626 return rc;
5627 }
5628
5629
5630 /* check contents of special bootstrap uCode SRAM */
5631 static int iwl_verify_bsm(struct iwl_priv *priv)
5632 {
5633 __le32 *image = priv->ucode_boot.v_addr;
5634 u32 len = priv->ucode_boot.len;
5635 u32 reg;
5636 u32 val;
5637
5638 IWL_DEBUG_INFO("Begin verify bsm\n");
5639
5640 /* verify BSM SRAM contents */
5641 val = iwl_read_restricted_reg(priv, BSM_WR_DWCOUNT_REG);
5642 for (reg = BSM_SRAM_LOWER_BOUND;
5643 reg < BSM_SRAM_LOWER_BOUND + len;
5644 reg += sizeof(u32), image ++) {
5645 val = iwl_read_restricted_reg(priv, reg);
5646 if (val != le32_to_cpu(*image)) {
5647 IWL_ERROR("BSM uCode verification failed at "
5648 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5649 BSM_SRAM_LOWER_BOUND,
5650 reg - BSM_SRAM_LOWER_BOUND, len,
5651 val, le32_to_cpu(*image));
5652 return -EIO;
5653 }
5654 }
5655
5656 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5657
5658 return 0;
5659 }
5660
5661 /**
5662 * iwl_load_bsm - Load bootstrap instructions
5663 *
5664 * BSM operation:
5665 *
5666 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5667 * in special SRAM that does not power down during RFKILL. When powering back
5668 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5669 * the bootstrap program into the on-board processor, and starts it.
5670 *
5671 * The bootstrap program loads (via DMA) instructions and data for a new
5672 * program from host DRAM locations indicated by the host driver in the
5673 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5674 * automatically.
5675 *
5676 * When initializing the NIC, the host driver points the BSM to the
5677 * "initialize" uCode image. This uCode sets up some internal data, then
5678 * notifies host via "initialize alive" that it is complete.
5679 *
5680 * The host then replaces the BSM_DRAM_* pointer values to point to the
5681 * normal runtime uCode instructions and a backup uCode data cache buffer
5682 * (filled initially with starting data values for the on-board processor),
5683 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5684 * which begins normal operation.
5685 *
5686 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5687 * the backup data cache in DRAM before SRAM is powered down.
5688 *
5689 * When powering back up, the BSM loads the bootstrap program. This reloads
5690 * the runtime uCode instructions and the backup data cache into SRAM,
5691 * and re-launches the runtime uCode from where it left off.
5692 */
5693 static int iwl_load_bsm(struct iwl_priv *priv)
5694 {
5695 __le32 *image = priv->ucode_boot.v_addr;
5696 u32 len = priv->ucode_boot.len;
5697 dma_addr_t pinst;
5698 dma_addr_t pdata;
5699 u32 inst_len;
5700 u32 data_len;
5701 int rc;
5702 int i;
5703 u32 done;
5704 u32 reg_offset;
5705
5706 IWL_DEBUG_INFO("Begin load bsm\n");
5707
5708 /* make sure bootstrap program is no larger than BSM's SRAM size */
5709 if (len > IWL_MAX_BSM_SIZE)
5710 return -EINVAL;
5711
5712 /* Tell bootstrap uCode where to find the "Initialize" uCode
5713 * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
5714 * NOTE: iwl_initialize_alive_start() will replace these values,
5715 * after the "initialize" uCode has run, to point to
5716 * runtime/protocol instructions and backup data cache. */
5717 pinst = priv->ucode_init.p_addr;
5718 pdata = priv->ucode_init_data.p_addr;
5719 inst_len = priv->ucode_init.len;
5720 data_len = priv->ucode_init_data.len;
5721
5722 rc = iwl_grab_restricted_access(priv);
5723 if (rc)
5724 return rc;
5725
5726 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
5727 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5728 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5729 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
5730
5731 /* Fill BSM memory with bootstrap instructions */
5732 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5733 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5734 reg_offset += sizeof(u32), image++)
5735 _iwl_write_restricted_reg(priv, reg_offset,
5736 le32_to_cpu(*image));
5737
5738 rc = iwl_verify_bsm(priv);
5739 if (rc) {
5740 iwl_release_restricted_access(priv);
5741 return rc;
5742 }
5743
5744 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
5745 iwl_write_restricted_reg(priv, BSM_WR_MEM_SRC_REG, 0x0);
5746 iwl_write_restricted_reg(priv, BSM_WR_MEM_DST_REG,
5747 RTC_INST_LOWER_BOUND);
5748 iwl_write_restricted_reg(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
5749
5750 /* Load bootstrap code into instruction SRAM now,
5751 * to prepare to load "initialize" uCode */
5752 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
5753 BSM_WR_CTRL_REG_BIT_START);
5754
5755 /* Wait for load of bootstrap uCode to finish */
5756 for (i = 0; i < 100; i++) {
5757 done = iwl_read_restricted_reg(priv, BSM_WR_CTRL_REG);
5758 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5759 break;
5760 udelay(10);
5761 }
5762 if (i < 100)
5763 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5764 else {
5765 IWL_ERROR("BSM write did not complete!\n");
5766 return -EIO;
5767 }
5768
5769 /* Enable future boot loads whenever power management unit triggers it
5770 * (e.g. when powering back up after power-save shutdown) */
5771 iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
5772 BSM_WR_CTRL_REG_BIT_START_EN);
5773
5774 iwl_release_restricted_access(priv);
5775
5776 return 0;
5777 }
5778
5779 static void iwl_nic_start(struct iwl_priv *priv)
5780 {
5781 /* Remove all resets to allow NIC to operate */
5782 iwl_write32(priv, CSR_RESET, 0);
5783 }
5784
5785 /**
5786 * iwl_read_ucode - Read uCode images from disk file.
5787 *
5788 * Copy into buffers for card to fetch via bus-mastering
5789 */
5790 static int iwl_read_ucode(struct iwl_priv *priv)
5791 {
5792 struct iwl_ucode *ucode;
5793 int rc = 0;
5794 const struct firmware *ucode_raw;
5795 /* firmware file name contains uCode/driver compatibility version */
5796 const char *name = "iwlwifi-3945" IWL3945_UCODE_API ".ucode";
5797 u8 *src;
5798 size_t len;
5799 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5800
5801 /* Ask kernel firmware_class module to get the boot firmware off disk.
5802 * request_firmware() is synchronous, file is in memory on return. */
5803 rc = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5804 if (rc < 0) {
5805 IWL_ERROR("%s firmware file req failed: Reason %d\n", name, rc);
5806 goto error;
5807 }
5808
5809 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5810 name, ucode_raw->size);
5811
5812 /* Make sure that we got at least our header! */
5813 if (ucode_raw->size < sizeof(*ucode)) {
5814 IWL_ERROR("File size way too small!\n");
5815 rc = -EINVAL;
5816 goto err_release;
5817 }
5818
5819 /* Data from ucode file: header followed by uCode images */
5820 ucode = (void *)ucode_raw->data;
5821
5822 ver = le32_to_cpu(ucode->ver);
5823 inst_size = le32_to_cpu(ucode->inst_size);
5824 data_size = le32_to_cpu(ucode->data_size);
5825 init_size = le32_to_cpu(ucode->init_size);
5826 init_data_size = le32_to_cpu(ucode->init_data_size);
5827 boot_size = le32_to_cpu(ucode->boot_size);
5828
5829 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5830 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5831 inst_size);
5832 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5833 data_size);
5834 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5835 init_size);
5836 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5837 init_data_size);
5838 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5839 boot_size);
5840
5841 /* Verify size of file vs. image size info in file's header */
5842 if (ucode_raw->size < sizeof(*ucode) +
5843 inst_size + data_size + init_size +
5844 init_data_size + boot_size) {
5845
5846 IWL_DEBUG_INFO("uCode file size %d too small\n",
5847 (int)ucode_raw->size);
5848 rc = -EINVAL;
5849 goto err_release;
5850 }
5851
5852 /* Verify that uCode images will fit in card's SRAM */
5853 if (inst_size > IWL_MAX_INST_SIZE) {
5854 IWL_DEBUG_INFO("uCode instr len %d too large to fit in card\n",
5855 (int)inst_size);
5856 rc = -EINVAL;
5857 goto err_release;
5858 }
5859
5860 if (data_size > IWL_MAX_DATA_SIZE) {
5861 IWL_DEBUG_INFO("uCode data len %d too large to fit in card\n",
5862 (int)data_size);
5863 rc = -EINVAL;
5864 goto err_release;
5865 }
5866 if (init_size > IWL_MAX_INST_SIZE) {
5867 IWL_DEBUG_INFO
5868 ("uCode init instr len %d too large to fit in card\n",
5869 (int)init_size);
5870 rc = -EINVAL;
5871 goto err_release;
5872 }
5873 if (init_data_size > IWL_MAX_DATA_SIZE) {
5874 IWL_DEBUG_INFO
5875 ("uCode init data len %d too large to fit in card\n",
5876 (int)init_data_size);
5877 rc = -EINVAL;
5878 goto err_release;
5879 }
5880 if (boot_size > IWL_MAX_BSM_SIZE) {
5881 IWL_DEBUG_INFO
5882 ("uCode boot instr len %d too large to fit in bsm\n",
5883 (int)boot_size);
5884 rc = -EINVAL;
5885 goto err_release;
5886 }
5887
5888 /* Allocate ucode buffers for card's bus-master loading ... */
5889
5890 /* Runtime instructions and 2 copies of data:
5891 * 1) unmodified from disk
5892 * 2) backup cache for save/restore during power-downs */
5893 priv->ucode_code.len = inst_size;
5894 priv->ucode_code.v_addr =
5895 pci_alloc_consistent(priv->pci_dev,
5896 priv->ucode_code.len,
5897 &(priv->ucode_code.p_addr));
5898
5899 priv->ucode_data.len = data_size;
5900 priv->ucode_data.v_addr =
5901 pci_alloc_consistent(priv->pci_dev,
5902 priv->ucode_data.len,
5903 &(priv->ucode_data.p_addr));
5904
5905 priv->ucode_data_backup.len = data_size;
5906 priv->ucode_data_backup.v_addr =
5907 pci_alloc_consistent(priv->pci_dev,
5908 priv->ucode_data_backup.len,
5909 &(priv->ucode_data_backup.p_addr));
5910
5911
5912 /* Initialization instructions and data */
5913 priv->ucode_init.len = init_size;
5914 priv->ucode_init.v_addr =
5915 pci_alloc_consistent(priv->pci_dev,
5916 priv->ucode_init.len,
5917 &(priv->ucode_init.p_addr));
5918
5919 priv->ucode_init_data.len = init_data_size;
5920 priv->ucode_init_data.v_addr =
5921 pci_alloc_consistent(priv->pci_dev,
5922 priv->ucode_init_data.len,
5923 &(priv->ucode_init_data.p_addr));
5924
5925 /* Bootstrap (instructions only, no data) */
5926 priv->ucode_boot.len = boot_size;
5927 priv->ucode_boot.v_addr =
5928 pci_alloc_consistent(priv->pci_dev,
5929 priv->ucode_boot.len,
5930 &(priv->ucode_boot.p_addr));
5931
5932 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
5933 !priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr ||
5934 !priv->ucode_boot.v_addr || !priv->ucode_data_backup.v_addr)
5935 goto err_pci_alloc;
5936
5937 /* Copy images into buffers for card's bus-master reads ... */
5938
5939 /* Runtime instructions (first block of data in file) */
5940 src = &ucode->data[0];
5941 len = priv->ucode_code.len;
5942 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %d\n",
5943 (int)len);
5944 memcpy(priv->ucode_code.v_addr, src, len);
5945 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5946 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5947
5948 /* Runtime data (2nd block)
5949 * NOTE: Copy into backup buffer will be done in iwl_up() */
5950 src = &ucode->data[inst_size];
5951 len = priv->ucode_data.len;
5952 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %d\n",
5953 (int)len);
5954 memcpy(priv->ucode_data.v_addr, src, len);
5955 memcpy(priv->ucode_data_backup.v_addr, src, len);
5956
5957 /* Initialization instructions (3rd block) */
5958 if (init_size) {
5959 src = &ucode->data[inst_size + data_size];
5960 len = priv->ucode_init.len;
5961 IWL_DEBUG_INFO("Copying (but not loading) init instr len %d\n",
5962 (int)len);
5963 memcpy(priv->ucode_init.v_addr, src, len);
5964 }
5965
5966 /* Initialization data (4th block) */
5967 if (init_data_size) {
5968 src = &ucode->data[inst_size + data_size + init_size];
5969 len = priv->ucode_init_data.len;
5970 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5971 (int)len);
5972 memcpy(priv->ucode_init_data.v_addr, src, len);
5973 }
5974
5975 /* Bootstrap instructions (5th block) */
5976 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5977 len = priv->ucode_boot.len;
5978 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5979 (int)len);
5980 memcpy(priv->ucode_boot.v_addr, src, len);
5981
5982 /* We have our copies now, allow OS release its copies */
5983 release_firmware(ucode_raw);
5984 return 0;
5985
5986 err_pci_alloc:
5987 IWL_ERROR("failed to allocate pci memory\n");
5988 rc = -ENOMEM;
5989 iwl_dealloc_ucode_pci(priv);
5990
5991 err_release:
5992 release_firmware(ucode_raw);
5993
5994 error:
5995 return rc;
5996 }
5997
5998
5999 /**
6000 * iwl_set_ucode_ptrs - Set uCode address location
6001 *
6002 * Tell initialization uCode where to find runtime uCode.
6003 *
6004 * BSM registers initially contain pointers to initialization uCode.
6005 * We need to replace them to load runtime uCode inst and data,
6006 * and to save runtime data when powering down.
6007 */
6008 static int iwl_set_ucode_ptrs(struct iwl_priv *priv)
6009 {
6010 dma_addr_t pinst;
6011 dma_addr_t pdata;
6012 int rc = 0;
6013 unsigned long flags;
6014
6015 /* bits 31:0 for 3945 */
6016 pinst = priv->ucode_code.p_addr;
6017 pdata = priv->ucode_data_backup.p_addr;
6018
6019 spin_lock_irqsave(&priv->lock, flags);
6020 rc = iwl_grab_restricted_access(priv);
6021 if (rc) {
6022 spin_unlock_irqrestore(&priv->lock, flags);
6023 return rc;
6024 }
6025
6026 /* Tell bootstrap uCode where to find image to load */
6027 iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
6028 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6029 iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
6030 priv->ucode_data.len);
6031
6032 /* Inst bytecount must be last to set up, bit 31 signals uCode
6033 * that all new ptr/size info is in place */
6034 iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG,
6035 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6036
6037 iwl_release_restricted_access(priv);
6038
6039 spin_unlock_irqrestore(&priv->lock, flags);
6040
6041 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6042
6043 return rc;
6044 }
6045
6046 /**
6047 * iwl_init_alive_start - Called after REPLY_ALIVE notification received
6048 *
6049 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6050 *
6051 * The 4965 "initialize" ALIVE reply contains calibration data for:
6052 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6053 * (3945 does not contain this data).
6054 *
6055 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6056 */
6057 static void iwl_init_alive_start(struct iwl_priv *priv)
6058 {
6059 /* Check alive response for "valid" sign from uCode */
6060 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6061 /* We had an error bringing up the hardware, so take it
6062 * all the way back down so we can try again */
6063 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6064 goto restart;
6065 }
6066
6067 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6068 * This is a paranoid check, because we would not have gotten the
6069 * "initialize" alive if code weren't properly loaded. */
6070 if (iwl_verify_ucode(priv)) {
6071 /* Runtime instruction load was bad;
6072 * take it all the way back down so we can try again */
6073 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6074 goto restart;
6075 }
6076
6077 /* Send pointers to protocol/runtime uCode image ... init code will
6078 * load and launch runtime uCode, which will send us another "Alive"
6079 * notification. */
6080 IWL_DEBUG_INFO("Initialization Alive received.\n");
6081 if (iwl_set_ucode_ptrs(priv)) {
6082 /* Runtime instruction load won't happen;
6083 * take it all the way back down so we can try again */
6084 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6085 goto restart;
6086 }
6087 return;
6088
6089 restart:
6090 queue_work(priv->workqueue, &priv->restart);
6091 }
6092
6093
6094 /**
6095 * iwl_alive_start - called after REPLY_ALIVE notification received
6096 * from protocol/runtime uCode (initialization uCode's
6097 * Alive gets handled by iwl_init_alive_start()).
6098 */
6099 static void iwl_alive_start(struct iwl_priv *priv)
6100 {
6101 int rc = 0;
6102 int thermal_spin = 0;
6103 u32 rfkill;
6104
6105 IWL_DEBUG_INFO("Runtime Alive received.\n");
6106
6107 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6108 /* We had an error bringing up the hardware, so take it
6109 * all the way back down so we can try again */
6110 IWL_DEBUG_INFO("Alive failed.\n");
6111 goto restart;
6112 }
6113
6114 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6115 * This is a paranoid check, because we would not have gotten the
6116 * "runtime" alive if code weren't properly loaded. */
6117 if (iwl_verify_ucode(priv)) {
6118 /* Runtime instruction load was bad;
6119 * take it all the way back down so we can try again */
6120 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6121 goto restart;
6122 }
6123
6124 iwl_clear_stations_table(priv);
6125
6126 rc = iwl_grab_restricted_access(priv);
6127 if (rc) {
6128 IWL_WARNING("Can not read rfkill status from adapter\n");
6129 return;
6130 }
6131
6132 rfkill = iwl_read_restricted_reg(priv, APMG_RFKILL_REG);
6133 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
6134 iwl_release_restricted_access(priv);
6135
6136 if (rfkill & 0x1) {
6137 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6138 /* if rfkill is not on, then wait for thermal
6139 * sensor in adapter to kick in */
6140 while (iwl_hw_get_temperature(priv) == 0) {
6141 thermal_spin++;
6142 udelay(10);
6143 }
6144
6145 if (thermal_spin)
6146 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
6147 thermal_spin * 10);
6148 } else
6149 set_bit(STATUS_RF_KILL_HW, &priv->status);
6150
6151 /* After the ALIVE response, we can process host commands */
6152 set_bit(STATUS_ALIVE, &priv->status);
6153
6154 /* Clear out the uCode error bit if it is set */
6155 clear_bit(STATUS_FW_ERROR, &priv->status);
6156
6157 rc = iwl_init_channel_map(priv);
6158 if (rc) {
6159 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6160 return;
6161 }
6162
6163 iwl_init_geos(priv);
6164
6165 if (iwl_is_rfkill(priv))
6166 return;
6167
6168 if (!priv->mac80211_registered) {
6169 /* Unlock so any user space entry points can call back into
6170 * the driver without a deadlock... */
6171 mutex_unlock(&priv->mutex);
6172 iwl_rate_control_register(priv->hw);
6173 rc = ieee80211_register_hw(priv->hw);
6174 priv->hw->conf.beacon_int = 100;
6175 mutex_lock(&priv->mutex);
6176
6177 if (rc) {
6178 iwl_rate_control_unregister(priv->hw);
6179 IWL_ERROR("Failed to register network "
6180 "device (error %d)\n", rc);
6181 return;
6182 }
6183
6184 priv->mac80211_registered = 1;
6185
6186 iwl_reset_channel_flag(priv);
6187 } else
6188 ieee80211_start_queues(priv->hw);
6189
6190 priv->active_rate = priv->rates_mask;
6191 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6192
6193 iwl_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
6194
6195 if (iwl_is_associated(priv)) {
6196 struct iwl_rxon_cmd *active_rxon =
6197 (struct iwl_rxon_cmd *)(&priv->active_rxon);
6198
6199 memcpy(&priv->staging_rxon, &priv->active_rxon,
6200 sizeof(priv->staging_rxon));
6201 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6202 } else {
6203 /* Initialize our rx_config data */
6204 iwl_connection_init_rx_config(priv);
6205 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6206 }
6207
6208 /* Configure BT coexistence */
6209 iwl_send_bt_config(priv);
6210
6211 /* Configure the adapter for unassociated operation */
6212 iwl_commit_rxon(priv);
6213
6214 /* At this point, the NIC is initialized and operational */
6215 priv->notif_missed_beacons = 0;
6216 set_bit(STATUS_READY, &priv->status);
6217
6218 iwl3945_reg_txpower_periodic(priv);
6219
6220 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6221
6222 if (priv->error_recovering)
6223 iwl_error_recovery(priv);
6224
6225 return;
6226
6227 restart:
6228 queue_work(priv->workqueue, &priv->restart);
6229 }
6230
6231 static void iwl_cancel_deferred_work(struct iwl_priv *priv);
6232
6233 static void __iwl_down(struct iwl_priv *priv)
6234 {
6235 unsigned long flags;
6236 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6237 struct ieee80211_conf *conf = NULL;
6238
6239 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6240
6241 conf = ieee80211_get_hw_conf(priv->hw);
6242
6243 if (!exit_pending)
6244 set_bit(STATUS_EXIT_PENDING, &priv->status);
6245
6246 iwl_clear_stations_table(priv);
6247
6248 /* Unblock any waiting calls */
6249 wake_up_interruptible_all(&priv->wait_command_queue);
6250
6251 /* Wipe out the EXIT_PENDING status bit if we are not actually
6252 * exiting the module */
6253 if (!exit_pending)
6254 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6255
6256 /* stop and reset the on-board processor */
6257 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
6258
6259 /* tell the device to stop sending interrupts */
6260 iwl_disable_interrupts(priv);
6261
6262 if (priv->mac80211_registered)
6263 ieee80211_stop_queues(priv->hw);
6264
6265 /* If we have not previously called iwl_init() then
6266 * clear all bits but the RF Kill and SUSPEND bits and return */
6267 if (!iwl_is_init(priv)) {
6268 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6269 STATUS_RF_KILL_HW |
6270 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6271 STATUS_RF_KILL_SW |
6272 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6273 STATUS_IN_SUSPEND;
6274 goto exit;
6275 }
6276
6277 /* ...otherwise clear out all the status bits but the RF Kill and
6278 * SUSPEND bits and continue taking the NIC down. */
6279 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6280 STATUS_RF_KILL_HW |
6281 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6282 STATUS_RF_KILL_SW |
6283 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6284 STATUS_IN_SUSPEND |
6285 test_bit(STATUS_FW_ERROR, &priv->status) <<
6286 STATUS_FW_ERROR;
6287
6288 spin_lock_irqsave(&priv->lock, flags);
6289 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
6290 spin_unlock_irqrestore(&priv->lock, flags);
6291
6292 iwl_hw_txq_ctx_stop(priv);
6293 iwl_hw_rxq_stop(priv);
6294
6295 spin_lock_irqsave(&priv->lock, flags);
6296 if (!iwl_grab_restricted_access(priv)) {
6297 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
6298 APMG_CLK_VAL_DMA_CLK_RQT);
6299 iwl_release_restricted_access(priv);
6300 }
6301 spin_unlock_irqrestore(&priv->lock, flags);
6302
6303 udelay(5);
6304
6305 iwl_hw_nic_stop_master(priv);
6306 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6307 iwl_hw_nic_reset(priv);
6308
6309 exit:
6310 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
6311
6312 if (priv->ibss_beacon)
6313 dev_kfree_skb(priv->ibss_beacon);
6314 priv->ibss_beacon = NULL;
6315
6316 /* clear out any free frames */
6317 iwl_clear_free_frames(priv);
6318 }
6319
6320 static void iwl_down(struct iwl_priv *priv)
6321 {
6322 mutex_lock(&priv->mutex);
6323 __iwl_down(priv);
6324 mutex_unlock(&priv->mutex);
6325
6326 iwl_cancel_deferred_work(priv);
6327 }
6328
6329 #define MAX_HW_RESTARTS 5
6330
6331 static int __iwl_up(struct iwl_priv *priv)
6332 {
6333 DECLARE_MAC_BUF(mac);
6334 int rc, i;
6335
6336 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6337 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6338 return -EIO;
6339 }
6340
6341 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6342 IWL_WARNING("Radio disabled by SW RF kill (module "
6343 "parameter)\n");
6344 return 0;
6345 }
6346
6347 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6348 IWL_ERROR("ucode not available for device bringup\n");
6349 return -EIO;
6350 }
6351
6352 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6353
6354 rc = iwl_hw_nic_init(priv);
6355 if (rc) {
6356 IWL_ERROR("Unable to int nic\n");
6357 return rc;
6358 }
6359
6360 /* make sure rfkill handshake bits are cleared */
6361 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6362 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
6363 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6364
6365 /* clear (again), then enable host interrupts */
6366 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
6367 iwl_enable_interrupts(priv);
6368
6369 /* really make sure rfkill handshake bits are cleared */
6370 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6371 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6372
6373 /* Copy original ucode data image from disk into backup cache.
6374 * This will be used to initialize the on-board processor's
6375 * data SRAM for a clean start when the runtime program first loads. */
6376 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6377 priv->ucode_data.len);
6378
6379 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6380
6381 iwl_clear_stations_table(priv);
6382
6383 /* load bootstrap state machine,
6384 * load bootstrap program into processor's memory,
6385 * prepare to load the "initialize" uCode */
6386 rc = iwl_load_bsm(priv);
6387
6388 if (rc) {
6389 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6390 continue;
6391 }
6392
6393 /* start card; "initialize" will load runtime ucode */
6394 iwl_nic_start(priv);
6395
6396 /* MAC Address location in EEPROM same for 3945/4965 */
6397 get_eeprom_mac(priv, priv->mac_addr);
6398 IWL_DEBUG_INFO("MAC address: %s\n",
6399 print_mac(mac, priv->mac_addr));
6400
6401 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6402
6403 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6404
6405 return 0;
6406 }
6407
6408 set_bit(STATUS_EXIT_PENDING, &priv->status);
6409 __iwl_down(priv);
6410
6411 /* tried to restart and config the device for as long as our
6412 * patience could withstand */
6413 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6414 return -EIO;
6415 }
6416
6417
6418 /*****************************************************************************
6419 *
6420 * Workqueue callbacks
6421 *
6422 *****************************************************************************/
6423
6424 static void iwl_bg_init_alive_start(struct work_struct *data)
6425 {
6426 struct iwl_priv *priv =
6427 container_of(data, struct iwl_priv, init_alive_start.work);
6428
6429 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6430 return;
6431
6432 mutex_lock(&priv->mutex);
6433 iwl_init_alive_start(priv);
6434 mutex_unlock(&priv->mutex);
6435 }
6436
6437 static void iwl_bg_alive_start(struct work_struct *data)
6438 {
6439 struct iwl_priv *priv =
6440 container_of(data, struct iwl_priv, alive_start.work);
6441
6442 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6443 return;
6444
6445 mutex_lock(&priv->mutex);
6446 iwl_alive_start(priv);
6447 mutex_unlock(&priv->mutex);
6448 }
6449
6450 static void iwl_bg_rf_kill(struct work_struct *work)
6451 {
6452 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
6453
6454 wake_up_interruptible(&priv->wait_command_queue);
6455
6456 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6457 return;
6458
6459 mutex_lock(&priv->mutex);
6460
6461 if (!iwl_is_rfkill(priv)) {
6462 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6463 "HW and/or SW RF Kill no longer active, restarting "
6464 "device\n");
6465 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6466 queue_work(priv->workqueue, &priv->restart);
6467 } else {
6468
6469 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6470 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6471 "disabled by SW switch\n");
6472 else
6473 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6474 "Kill switch must be turned off for "
6475 "wireless networking to work.\n");
6476 }
6477 mutex_unlock(&priv->mutex);
6478 }
6479
6480 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6481
6482 static void iwl_bg_scan_check(struct work_struct *data)
6483 {
6484 struct iwl_priv *priv =
6485 container_of(data, struct iwl_priv, scan_check.work);
6486
6487 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6488 return;
6489
6490 mutex_lock(&priv->mutex);
6491 if (test_bit(STATUS_SCANNING, &priv->status) ||
6492 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6493 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6494 "Scan completion watchdog resetting adapter (%dms)\n",
6495 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
6496
6497 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6498 iwl_send_scan_abort(priv);
6499 }
6500 mutex_unlock(&priv->mutex);
6501 }
6502
6503 static void iwl_bg_request_scan(struct work_struct *data)
6504 {
6505 struct iwl_priv *priv =
6506 container_of(data, struct iwl_priv, request_scan);
6507 struct iwl_host_cmd cmd = {
6508 .id = REPLY_SCAN_CMD,
6509 .len = sizeof(struct iwl_scan_cmd),
6510 .meta.flags = CMD_SIZE_HUGE,
6511 };
6512 int rc = 0;
6513 struct iwl_scan_cmd *scan;
6514 struct ieee80211_conf *conf = NULL;
6515 u8 direct_mask;
6516 int phymode;
6517
6518 conf = ieee80211_get_hw_conf(priv->hw);
6519
6520 mutex_lock(&priv->mutex);
6521
6522 if (!iwl_is_ready(priv)) {
6523 IWL_WARNING("request scan called when driver not ready.\n");
6524 goto done;
6525 }
6526
6527 /* Make sure the scan wasn't cancelled before this queued work
6528 * was given the chance to run... */
6529 if (!test_bit(STATUS_SCANNING, &priv->status))
6530 goto done;
6531
6532 /* This should never be called or scheduled if there is currently
6533 * a scan active in the hardware. */
6534 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6535 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6536 "Ignoring second request.\n");
6537 rc = -EIO;
6538 goto done;
6539 }
6540
6541 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6542 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6543 goto done;
6544 }
6545
6546 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6547 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6548 goto done;
6549 }
6550
6551 if (iwl_is_rfkill(priv)) {
6552 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6553 goto done;
6554 }
6555
6556 if (!test_bit(STATUS_READY, &priv->status)) {
6557 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6558 goto done;
6559 }
6560
6561 if (!priv->scan_bands) {
6562 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6563 goto done;
6564 }
6565
6566 if (!priv->scan) {
6567 priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
6568 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6569 if (!priv->scan) {
6570 rc = -ENOMEM;
6571 goto done;
6572 }
6573 }
6574 scan = priv->scan;
6575 memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
6576
6577 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6578 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6579
6580 if (iwl_is_associated(priv)) {
6581 u16 interval = 0;
6582 u32 extra;
6583 u32 suspend_time = 100;
6584 u32 scan_suspend_time = 100;
6585 unsigned long flags;
6586
6587 IWL_DEBUG_INFO("Scanning while associated...\n");
6588
6589 spin_lock_irqsave(&priv->lock, flags);
6590 interval = priv->beacon_int;
6591 spin_unlock_irqrestore(&priv->lock, flags);
6592
6593 scan->suspend_time = 0;
6594 scan->max_out_time = cpu_to_le32(200 * 1024);
6595 if (!interval)
6596 interval = suspend_time;
6597 /*
6598 * suspend time format:
6599 * 0-19: beacon interval in usec (time before exec.)
6600 * 20-23: 0
6601 * 24-31: number of beacons (suspend between channels)
6602 */
6603
6604 extra = (suspend_time / interval) << 24;
6605 scan_suspend_time = 0xFF0FFFFF &
6606 (extra | ((suspend_time % interval) * 1024));
6607
6608 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6609 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6610 scan_suspend_time, interval);
6611 }
6612
6613 /* We should add the ability for user to lock to PASSIVE ONLY */
6614 if (priv->one_direct_scan) {
6615 IWL_DEBUG_SCAN
6616 ("Kicking off one direct scan for '%s'\n",
6617 iwl_escape_essid(priv->direct_ssid,
6618 priv->direct_ssid_len));
6619 scan->direct_scan[0].id = WLAN_EID_SSID;
6620 scan->direct_scan[0].len = priv->direct_ssid_len;
6621 memcpy(scan->direct_scan[0].ssid,
6622 priv->direct_ssid, priv->direct_ssid_len);
6623 direct_mask = 1;
6624 } else if (!iwl_is_associated(priv) && priv->essid_len) {
6625 scan->direct_scan[0].id = WLAN_EID_SSID;
6626 scan->direct_scan[0].len = priv->essid_len;
6627 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6628 direct_mask = 1;
6629 } else
6630 direct_mask = 0;
6631
6632 /* We don't build a direct scan probe request; the uCode will do
6633 * that based on the direct_mask added to each channel entry */
6634 scan->tx_cmd.len = cpu_to_le16(
6635 iwl_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
6636 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6637 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6638 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6639 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6640
6641 /* flags + rate selection */
6642
6643 switch (priv->scan_bands) {
6644 case 2:
6645 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6646 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
6647 scan->good_CRC_th = 0;
6648 phymode = MODE_IEEE80211G;
6649 break;
6650
6651 case 1:
6652 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
6653 scan->good_CRC_th = IWL_GOOD_CRC_TH;
6654 phymode = MODE_IEEE80211A;
6655 break;
6656
6657 default:
6658 IWL_WARNING("Invalid scan band count\n");
6659 goto done;
6660 }
6661
6662 /* select Rx antennas */
6663 scan->flags |= iwl3945_get_antenna_flags(priv);
6664
6665 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6666 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6667
6668 if (direct_mask)
6669 IWL_DEBUG_SCAN
6670 ("Initiating direct scan for %s.\n",
6671 iwl_escape_essid(priv->essid, priv->essid_len));
6672 else
6673 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
6674
6675 scan->channel_count =
6676 iwl_get_channels_for_scan(
6677 priv, phymode, 1, /* active */
6678 direct_mask,
6679 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6680
6681 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
6682 scan->channel_count * sizeof(struct iwl_scan_channel);
6683 cmd.data = scan;
6684 scan->len = cpu_to_le16(cmd.len);
6685
6686 set_bit(STATUS_SCAN_HW, &priv->status);
6687 rc = iwl_send_cmd_sync(priv, &cmd);
6688 if (rc)
6689 goto done;
6690
6691 queue_delayed_work(priv->workqueue, &priv->scan_check,
6692 IWL_SCAN_CHECK_WATCHDOG);
6693
6694 mutex_unlock(&priv->mutex);
6695 return;
6696
6697 done:
6698 /* inform mac80211 scan aborted */
6699 queue_work(priv->workqueue, &priv->scan_completed);
6700 mutex_unlock(&priv->mutex);
6701 }
6702
6703 static void iwl_bg_up(struct work_struct *data)
6704 {
6705 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
6706
6707 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6708 return;
6709
6710 mutex_lock(&priv->mutex);
6711 __iwl_up(priv);
6712 mutex_unlock(&priv->mutex);
6713 }
6714
6715 static void iwl_bg_restart(struct work_struct *data)
6716 {
6717 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
6718
6719 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6720 return;
6721
6722 iwl_down(priv);
6723 queue_work(priv->workqueue, &priv->up);
6724 }
6725
6726 static void iwl_bg_rx_replenish(struct work_struct *data)
6727 {
6728 struct iwl_priv *priv =
6729 container_of(data, struct iwl_priv, rx_replenish);
6730
6731 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6732 return;
6733
6734 mutex_lock(&priv->mutex);
6735 iwl_rx_replenish(priv);
6736 mutex_unlock(&priv->mutex);
6737 }
6738
6739 static void iwl_bg_post_associate(struct work_struct *data)
6740 {
6741 struct iwl_priv *priv = container_of(data, struct iwl_priv,
6742 post_associate.work);
6743
6744 int rc = 0;
6745 struct ieee80211_conf *conf = NULL;
6746 DECLARE_MAC_BUF(mac);
6747
6748 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6749 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6750 return;
6751 }
6752
6753
6754 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6755 priv->assoc_id,
6756 print_mac(mac, priv->active_rxon.bssid_addr));
6757
6758 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6759 return;
6760
6761 mutex_lock(&priv->mutex);
6762
6763 if (!priv->interface_id || !priv->is_open) {
6764 mutex_unlock(&priv->mutex);
6765 return;
6766 }
6767 iwl_scan_cancel_timeout(priv, 200);
6768
6769 conf = ieee80211_get_hw_conf(priv->hw);
6770
6771 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6772 iwl_commit_rxon(priv);
6773
6774 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
6775 iwl_setup_rxon_timing(priv);
6776 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
6777 sizeof(priv->rxon_timing), &priv->rxon_timing);
6778 if (rc)
6779 IWL_WARNING("REPLY_RXON_TIMING failed - "
6780 "Attempting to continue.\n");
6781
6782 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6783
6784 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6785
6786 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6787 priv->assoc_id, priv->beacon_int);
6788
6789 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6790 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6791 else
6792 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6793
6794 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6795 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6796 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6797 else
6798 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6799
6800 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6801 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6802
6803 }
6804
6805 iwl_commit_rxon(priv);
6806
6807 switch (priv->iw_mode) {
6808 case IEEE80211_IF_TYPE_STA:
6809 iwl_rate_scale_init(priv->hw, IWL_AP_ID);
6810 break;
6811
6812 case IEEE80211_IF_TYPE_IBSS:
6813
6814 /* clear out the station table */
6815 iwl_clear_stations_table(priv);
6816
6817 iwl_add_station(priv, BROADCAST_ADDR, 0, 0);
6818 iwl_add_station(priv, priv->bssid, 0, 0);
6819 iwl3945_sync_sta(priv, IWL_STA_ID,
6820 (priv->phymode == MODE_IEEE80211A)?
6821 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6822 CMD_ASYNC);
6823 iwl_rate_scale_init(priv->hw, IWL_STA_ID);
6824 iwl_send_beacon_cmd(priv);
6825
6826 break;
6827
6828 default:
6829 IWL_ERROR("%s Should not be called in %d mode\n",
6830 __FUNCTION__, priv->iw_mode);
6831 break;
6832 }
6833
6834 iwl_sequence_reset(priv);
6835
6836 #ifdef CONFIG_IWLWIFI_QOS
6837 iwl_activate_qos(priv, 0);
6838 #endif /* CONFIG_IWLWIFI_QOS */
6839 mutex_unlock(&priv->mutex);
6840 }
6841
6842 static void iwl_bg_abort_scan(struct work_struct *work)
6843 {
6844 struct iwl_priv *priv = container_of(work, struct iwl_priv,
6845 abort_scan);
6846
6847 if (!iwl_is_ready(priv))
6848 return;
6849
6850 mutex_lock(&priv->mutex);
6851
6852 set_bit(STATUS_SCAN_ABORTING, &priv->status);
6853 iwl_send_scan_abort(priv);
6854
6855 mutex_unlock(&priv->mutex);
6856 }
6857
6858 static void iwl_bg_scan_completed(struct work_struct *work)
6859 {
6860 struct iwl_priv *priv =
6861 container_of(work, struct iwl_priv, scan_completed);
6862
6863 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6864
6865 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6866 return;
6867
6868 ieee80211_scan_completed(priv->hw);
6869
6870 /* Since setting the TXPOWER may have been deferred while
6871 * performing the scan, fire one off */
6872 mutex_lock(&priv->mutex);
6873 iwl_hw_reg_send_txpower(priv);
6874 mutex_unlock(&priv->mutex);
6875 }
6876
6877 /*****************************************************************************
6878 *
6879 * mac80211 entry point functions
6880 *
6881 *****************************************************************************/
6882
6883 static int iwl_mac_start(struct ieee80211_hw *hw)
6884 {
6885 struct iwl_priv *priv = hw->priv;
6886
6887 IWL_DEBUG_MAC80211("enter\n");
6888
6889 /* we should be verifying the device is ready to be opened */
6890 mutex_lock(&priv->mutex);
6891
6892 priv->is_open = 1;
6893
6894 if (!iwl_is_rfkill(priv))
6895 ieee80211_start_queues(priv->hw);
6896
6897 mutex_unlock(&priv->mutex);
6898 IWL_DEBUG_MAC80211("leave\n");
6899 return 0;
6900 }
6901
6902 static void iwl_mac_stop(struct ieee80211_hw *hw)
6903 {
6904 struct iwl_priv *priv = hw->priv;
6905
6906 IWL_DEBUG_MAC80211("enter\n");
6907
6908
6909 mutex_lock(&priv->mutex);
6910 /* stop mac, cancel any scan request and clear
6911 * RXON_FILTER_ASSOC_MSK BIT
6912 */
6913 priv->is_open = 0;
6914 iwl_scan_cancel_timeout(priv, 100);
6915 cancel_delayed_work(&priv->post_associate);
6916 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6917 iwl_commit_rxon(priv);
6918 mutex_unlock(&priv->mutex);
6919
6920 IWL_DEBUG_MAC80211("leave\n");
6921 }
6922
6923 static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
6924 struct ieee80211_tx_control *ctl)
6925 {
6926 struct iwl_priv *priv = hw->priv;
6927
6928 IWL_DEBUG_MAC80211("enter\n");
6929
6930 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
6931 IWL_DEBUG_MAC80211("leave - monitor\n");
6932 return -1;
6933 }
6934
6935 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
6936 ctl->tx_rate);
6937
6938 if (iwl_tx_skb(priv, skb, ctl))
6939 dev_kfree_skb_any(skb);
6940
6941 IWL_DEBUG_MAC80211("leave\n");
6942 return 0;
6943 }
6944
6945 static int iwl_mac_add_interface(struct ieee80211_hw *hw,
6946 struct ieee80211_if_init_conf *conf)
6947 {
6948 struct iwl_priv *priv = hw->priv;
6949 unsigned long flags;
6950 DECLARE_MAC_BUF(mac);
6951
6952 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
6953
6954 if (priv->interface_id) {
6955 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
6956 return -EOPNOTSUPP;
6957 }
6958
6959 spin_lock_irqsave(&priv->lock, flags);
6960 priv->interface_id = conf->if_id;
6961
6962 spin_unlock_irqrestore(&priv->lock, flags);
6963
6964 mutex_lock(&priv->mutex);
6965
6966 if (conf->mac_addr) {
6967 IWL_DEBUG_MAC80211("Set: %s\n", print_mac(mac, conf->mac_addr));
6968 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6969 }
6970
6971 iwl_set_mode(priv, conf->type);
6972
6973 IWL_DEBUG_MAC80211("leave\n");
6974 mutex_unlock(&priv->mutex);
6975
6976 return 0;
6977 }
6978
6979 /**
6980 * iwl_mac_config - mac80211 config callback
6981 *
6982 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6983 * be set inappropriately and the driver currently sets the hardware up to
6984 * use it whenever needed.
6985 */
6986 static int iwl_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
6987 {
6988 struct iwl_priv *priv = hw->priv;
6989 const struct iwl_channel_info *ch_info;
6990 unsigned long flags;
6991
6992 mutex_lock(&priv->mutex);
6993 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
6994
6995 if (!iwl_is_ready(priv)) {
6996 IWL_DEBUG_MAC80211("leave - not ready\n");
6997 mutex_unlock(&priv->mutex);
6998 return -EIO;
6999 }
7000
7001 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
7002 * what is exposed through include/ declarations */
7003 if (unlikely(!iwl_param_disable_hw_scan &&
7004 test_bit(STATUS_SCANNING, &priv->status))) {
7005 IWL_DEBUG_MAC80211("leave - scanning\n");
7006 mutex_unlock(&priv->mutex);
7007 return 0;
7008 }
7009
7010 spin_lock_irqsave(&priv->lock, flags);
7011
7012 ch_info = iwl_get_channel_info(priv, conf->phymode, conf->channel);
7013 if (!is_channel_valid(ch_info)) {
7014 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7015 conf->channel, conf->phymode);
7016 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7017 spin_unlock_irqrestore(&priv->lock, flags);
7018 mutex_unlock(&priv->mutex);
7019 return -EINVAL;
7020 }
7021
7022 iwl_set_rxon_channel(priv, conf->phymode, conf->channel);
7023
7024 iwl_set_flags_for_phymode(priv, conf->phymode);
7025
7026 /* The list of supported rates and rate mask can be different
7027 * for each phymode; since the phymode may have changed, reset
7028 * the rate mask to what mac80211 lists */
7029 iwl_set_rate(priv);
7030
7031 spin_unlock_irqrestore(&priv->lock, flags);
7032
7033 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
7034 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
7035 iwl_hw_channel_switch(priv, conf->channel);
7036 mutex_unlock(&priv->mutex);
7037 return 0;
7038 }
7039 #endif
7040
7041 iwl_radio_kill_sw(priv, !conf->radio_enabled);
7042
7043 if (!conf->radio_enabled) {
7044 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7045 mutex_unlock(&priv->mutex);
7046 return 0;
7047 }
7048
7049 if (iwl_is_rfkill(priv)) {
7050 IWL_DEBUG_MAC80211("leave - RF kill\n");
7051 mutex_unlock(&priv->mutex);
7052 return -EIO;
7053 }
7054
7055 iwl_set_rate(priv);
7056
7057 if (memcmp(&priv->active_rxon,
7058 &priv->staging_rxon, sizeof(priv->staging_rxon)))
7059 iwl_commit_rxon(priv);
7060 else
7061 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7062
7063 IWL_DEBUG_MAC80211("leave\n");
7064
7065 mutex_unlock(&priv->mutex);
7066
7067 return 0;
7068 }
7069
7070 static void iwl_config_ap(struct iwl_priv *priv)
7071 {
7072 int rc = 0;
7073
7074 if (priv->status & STATUS_EXIT_PENDING)
7075 return;
7076
7077 /* The following should be done only at AP bring up */
7078 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7079
7080 /* RXON - unassoc (to set timing command) */
7081 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7082 iwl_commit_rxon(priv);
7083
7084 /* RXON Timing */
7085 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
7086 iwl_setup_rxon_timing(priv);
7087 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
7088 sizeof(priv->rxon_timing), &priv->rxon_timing);
7089 if (rc)
7090 IWL_WARNING("REPLY_RXON_TIMING failed - "
7091 "Attempting to continue.\n");
7092
7093 /* FIXME: what should be the assoc_id for AP? */
7094 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7095 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7096 priv->staging_rxon.flags |=
7097 RXON_FLG_SHORT_PREAMBLE_MSK;
7098 else
7099 priv->staging_rxon.flags &=
7100 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7101
7102 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7103 if (priv->assoc_capability &
7104 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7105 priv->staging_rxon.flags |=
7106 RXON_FLG_SHORT_SLOT_MSK;
7107 else
7108 priv->staging_rxon.flags &=
7109 ~RXON_FLG_SHORT_SLOT_MSK;
7110
7111 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7112 priv->staging_rxon.flags &=
7113 ~RXON_FLG_SHORT_SLOT_MSK;
7114 }
7115 /* restore RXON assoc */
7116 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7117 iwl_commit_rxon(priv);
7118 iwl_add_station(priv, BROADCAST_ADDR, 0, 0);
7119 }
7120 iwl_send_beacon_cmd(priv);
7121
7122 /* FIXME - we need to add code here to detect a totally new
7123 * configuration, reset the AP, unassoc, rxon timing, assoc,
7124 * clear sta table, add BCAST sta... */
7125 }
7126
7127 static int iwl_mac_config_interface(struct ieee80211_hw *hw, int if_id,
7128 struct ieee80211_if_conf *conf)
7129 {
7130 struct iwl_priv *priv = hw->priv;
7131 DECLARE_MAC_BUF(mac);
7132 unsigned long flags;
7133 int rc;
7134
7135 if (conf == NULL)
7136 return -EIO;
7137
7138 /* XXX: this MUST use conf->mac_addr */
7139
7140 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7141 (!conf->beacon || !conf->ssid_len)) {
7142 IWL_DEBUG_MAC80211
7143 ("Leaving in AP mode because HostAPD is not ready.\n");
7144 return 0;
7145 }
7146
7147 mutex_lock(&priv->mutex);
7148
7149 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7150 if (conf->bssid)
7151 IWL_DEBUG_MAC80211("bssid: %s\n",
7152 print_mac(mac, conf->bssid));
7153
7154 /*
7155 * very dubious code was here; the probe filtering flag is never set:
7156 *
7157 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7158 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
7159 */
7160 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
7161 IWL_DEBUG_MAC80211("leave - scanning\n");
7162 mutex_unlock(&priv->mutex);
7163 return 0;
7164 }
7165
7166 if (priv->interface_id != if_id) {
7167 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7168 mutex_unlock(&priv->mutex);
7169 return 0;
7170 }
7171
7172 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7173 if (!conf->bssid) {
7174 conf->bssid = priv->mac_addr;
7175 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
7176 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7177 print_mac(mac, conf->bssid));
7178 }
7179 if (priv->ibss_beacon)
7180 dev_kfree_skb(priv->ibss_beacon);
7181
7182 priv->ibss_beacon = conf->beacon;
7183 }
7184
7185 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7186 !is_multicast_ether_addr(conf->bssid)) {
7187 /* If there is currently a HW scan going on in the background
7188 * then we need to cancel it else the RXON below will fail. */
7189 if (iwl_scan_cancel_timeout(priv, 100)) {
7190 IWL_WARNING("Aborted scan still in progress "
7191 "after 100ms\n");
7192 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7193 mutex_unlock(&priv->mutex);
7194 return -EAGAIN;
7195 }
7196 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7197
7198 /* TODO: Audit driver for usage of these members and see
7199 * if mac80211 deprecates them (priv->bssid looks like it
7200 * shouldn't be there, but I haven't scanned the IBSS code
7201 * to verify) - jpk */
7202 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7203
7204 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7205 iwl_config_ap(priv);
7206 else {
7207 rc = iwl_commit_rxon(priv);
7208 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
7209 iwl_add_station(priv,
7210 priv->active_rxon.bssid_addr, 1, 0);
7211 }
7212
7213 } else {
7214 iwl_scan_cancel_timeout(priv, 100);
7215 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7216 iwl_commit_rxon(priv);
7217 }
7218
7219 spin_lock_irqsave(&priv->lock, flags);
7220 if (!conf->ssid_len)
7221 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7222 else
7223 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7224
7225 priv->essid_len = conf->ssid_len;
7226 spin_unlock_irqrestore(&priv->lock, flags);
7227
7228 IWL_DEBUG_MAC80211("leave\n");
7229 mutex_unlock(&priv->mutex);
7230
7231 return 0;
7232 }
7233
7234 static void iwl_configure_filter(struct ieee80211_hw *hw,
7235 unsigned int changed_flags,
7236 unsigned int *total_flags,
7237 int mc_count, struct dev_addr_list *mc_list)
7238 {
7239 /*
7240 * XXX: dummy
7241 * see also iwl_connection_init_rx_config
7242 */
7243 *total_flags = 0;
7244 }
7245
7246 static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
7247 struct ieee80211_if_init_conf *conf)
7248 {
7249 struct iwl_priv *priv = hw->priv;
7250
7251 IWL_DEBUG_MAC80211("enter\n");
7252
7253 mutex_lock(&priv->mutex);
7254
7255 iwl_scan_cancel_timeout(priv, 100);
7256 cancel_delayed_work(&priv->post_associate);
7257 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7258 iwl_commit_rxon(priv);
7259
7260 if (priv->interface_id == conf->if_id) {
7261 priv->interface_id = 0;
7262 memset(priv->bssid, 0, ETH_ALEN);
7263 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7264 priv->essid_len = 0;
7265 }
7266 mutex_unlock(&priv->mutex);
7267
7268 IWL_DEBUG_MAC80211("leave\n");
7269
7270 }
7271
7272 #define IWL_DELAY_NEXT_SCAN (HZ*2)
7273 static int iwl_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
7274 {
7275 int rc = 0;
7276 unsigned long flags;
7277 struct iwl_priv *priv = hw->priv;
7278
7279 IWL_DEBUG_MAC80211("enter\n");
7280
7281 mutex_lock(&priv->mutex);
7282 spin_lock_irqsave(&priv->lock, flags);
7283
7284 if (!iwl_is_ready_rf(priv)) {
7285 rc = -EIO;
7286 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7287 goto out_unlock;
7288 }
7289
7290 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7291 rc = -EIO;
7292 IWL_ERROR("ERROR: APs don't scan\n");
7293 goto out_unlock;
7294 }
7295
7296 /* if we just finished scan ask for delay */
7297 if (priv->last_scan_jiffies &&
7298 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
7299 jiffies)) {
7300 rc = -EAGAIN;
7301 goto out_unlock;
7302 }
7303 if (len) {
7304 IWL_DEBUG_SCAN("direct scan for "
7305 "%s [%d]\n ",
7306 iwl_escape_essid(ssid, len), (int)len);
7307
7308 priv->one_direct_scan = 1;
7309 priv->direct_ssid_len = (u8)
7310 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7311 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
7312 } else
7313 priv->one_direct_scan = 0;
7314
7315 rc = iwl_scan_initiate(priv);
7316
7317 IWL_DEBUG_MAC80211("leave\n");
7318
7319 out_unlock:
7320 spin_unlock_irqrestore(&priv->lock, flags);
7321 mutex_unlock(&priv->mutex);
7322
7323 return rc;
7324 }
7325
7326 static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
7327 const u8 *local_addr, const u8 *addr,
7328 struct ieee80211_key_conf *key)
7329 {
7330 struct iwl_priv *priv = hw->priv;
7331 int rc = 0;
7332 u8 sta_id;
7333
7334 IWL_DEBUG_MAC80211("enter\n");
7335
7336 if (!iwl_param_hwcrypto) {
7337 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7338 return -EOPNOTSUPP;
7339 }
7340
7341 if (is_zero_ether_addr(addr))
7342 /* only support pairwise keys */
7343 return -EOPNOTSUPP;
7344
7345 sta_id = iwl_hw_find_station(priv, addr);
7346 if (sta_id == IWL_INVALID_STATION) {
7347 DECLARE_MAC_BUF(mac);
7348
7349 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7350 print_mac(mac, addr));
7351 return -EINVAL;
7352 }
7353
7354 mutex_lock(&priv->mutex);
7355
7356 iwl_scan_cancel_timeout(priv, 100);
7357
7358 switch (cmd) {
7359 case SET_KEY:
7360 rc = iwl_update_sta_key_info(priv, key, sta_id);
7361 if (!rc) {
7362 iwl_set_rxon_hwcrypto(priv, 1);
7363 iwl_commit_rxon(priv);
7364 key->hw_key_idx = sta_id;
7365 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7366 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7367 }
7368 break;
7369 case DISABLE_KEY:
7370 rc = iwl_clear_sta_key_info(priv, sta_id);
7371 if (!rc) {
7372 iwl_set_rxon_hwcrypto(priv, 0);
7373 iwl_commit_rxon(priv);
7374 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7375 }
7376 break;
7377 default:
7378 rc = -EINVAL;
7379 }
7380
7381 IWL_DEBUG_MAC80211("leave\n");
7382 mutex_unlock(&priv->mutex);
7383
7384 return rc;
7385 }
7386
7387 static int iwl_mac_conf_tx(struct ieee80211_hw *hw, int queue,
7388 const struct ieee80211_tx_queue_params *params)
7389 {
7390 struct iwl_priv *priv = hw->priv;
7391 #ifdef CONFIG_IWLWIFI_QOS
7392 unsigned long flags;
7393 int q;
7394 #endif /* CONFIG_IWL_QOS */
7395
7396 IWL_DEBUG_MAC80211("enter\n");
7397
7398 if (!iwl_is_ready_rf(priv)) {
7399 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7400 return -EIO;
7401 }
7402
7403 if (queue >= AC_NUM) {
7404 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7405 return 0;
7406 }
7407
7408 #ifdef CONFIG_IWLWIFI_QOS
7409 if (!priv->qos_data.qos_enable) {
7410 priv->qos_data.qos_active = 0;
7411 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7412 return 0;
7413 }
7414 q = AC_NUM - 1 - queue;
7415
7416 spin_lock_irqsave(&priv->lock, flags);
7417
7418 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7419 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7420 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7421 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7422 cpu_to_le16((params->burst_time * 100));
7423
7424 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7425 priv->qos_data.qos_active = 1;
7426
7427 spin_unlock_irqrestore(&priv->lock, flags);
7428
7429 mutex_lock(&priv->mutex);
7430 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
7431 iwl_activate_qos(priv, 1);
7432 else if (priv->assoc_id && iwl_is_associated(priv))
7433 iwl_activate_qos(priv, 0);
7434
7435 mutex_unlock(&priv->mutex);
7436
7437 #endif /*CONFIG_IWLWIFI_QOS */
7438
7439 IWL_DEBUG_MAC80211("leave\n");
7440 return 0;
7441 }
7442
7443 static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
7444 struct ieee80211_tx_queue_stats *stats)
7445 {
7446 struct iwl_priv *priv = hw->priv;
7447 int i, avail;
7448 struct iwl_tx_queue *txq;
7449 struct iwl_queue *q;
7450 unsigned long flags;
7451
7452 IWL_DEBUG_MAC80211("enter\n");
7453
7454 if (!iwl_is_ready_rf(priv)) {
7455 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7456 return -EIO;
7457 }
7458
7459 spin_lock_irqsave(&priv->lock, flags);
7460
7461 for (i = 0; i < AC_NUM; i++) {
7462 txq = &priv->txq[i];
7463 q = &txq->q;
7464 avail = iwl_queue_space(q);
7465
7466 stats->data[i].len = q->n_window - avail;
7467 stats->data[i].limit = q->n_window - q->high_mark;
7468 stats->data[i].count = q->n_window;
7469
7470 }
7471 spin_unlock_irqrestore(&priv->lock, flags);
7472
7473 IWL_DEBUG_MAC80211("leave\n");
7474
7475 return 0;
7476 }
7477
7478 static int iwl_mac_get_stats(struct ieee80211_hw *hw,
7479 struct ieee80211_low_level_stats *stats)
7480 {
7481 IWL_DEBUG_MAC80211("enter\n");
7482 IWL_DEBUG_MAC80211("leave\n");
7483
7484 return 0;
7485 }
7486
7487 static u64 iwl_mac_get_tsf(struct ieee80211_hw *hw)
7488 {
7489 IWL_DEBUG_MAC80211("enter\n");
7490 IWL_DEBUG_MAC80211("leave\n");
7491
7492 return 0;
7493 }
7494
7495 static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
7496 {
7497 struct iwl_priv *priv = hw->priv;
7498 unsigned long flags;
7499
7500 mutex_lock(&priv->mutex);
7501 IWL_DEBUG_MAC80211("enter\n");
7502
7503 #ifdef CONFIG_IWLWIFI_QOS
7504 iwl_reset_qos(priv);
7505 #endif
7506 cancel_delayed_work(&priv->post_associate);
7507
7508 spin_lock_irqsave(&priv->lock, flags);
7509 priv->assoc_id = 0;
7510 priv->assoc_capability = 0;
7511 priv->call_post_assoc_from_beacon = 0;
7512
7513 /* new association get rid of ibss beacon skb */
7514 if (priv->ibss_beacon)
7515 dev_kfree_skb(priv->ibss_beacon);
7516
7517 priv->ibss_beacon = NULL;
7518
7519 priv->beacon_int = priv->hw->conf.beacon_int;
7520 priv->timestamp1 = 0;
7521 priv->timestamp0 = 0;
7522 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7523 priv->beacon_int = 0;
7524
7525 spin_unlock_irqrestore(&priv->lock, flags);
7526
7527 /* we are restarting association process
7528 * clear RXON_FILTER_ASSOC_MSK bit
7529 */
7530 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
7531 iwl_scan_cancel_timeout(priv, 100);
7532 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7533 iwl_commit_rxon(priv);
7534 }
7535
7536 /* Per mac80211.h: This is only used in IBSS mode... */
7537 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7538
7539 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7540 mutex_unlock(&priv->mutex);
7541 return;
7542 }
7543
7544 if (!iwl_is_ready_rf(priv)) {
7545 IWL_DEBUG_MAC80211("leave - not ready\n");
7546 mutex_unlock(&priv->mutex);
7547 return;
7548 }
7549
7550 priv->only_active_channel = 0;
7551
7552 iwl_set_rate(priv);
7553
7554 mutex_unlock(&priv->mutex);
7555
7556 IWL_DEBUG_MAC80211("leave\n");
7557
7558 }
7559
7560 static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
7561 struct ieee80211_tx_control *control)
7562 {
7563 struct iwl_priv *priv = hw->priv;
7564 unsigned long flags;
7565
7566 mutex_lock(&priv->mutex);
7567 IWL_DEBUG_MAC80211("enter\n");
7568
7569 if (!iwl_is_ready_rf(priv)) {
7570 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7571 mutex_unlock(&priv->mutex);
7572 return -EIO;
7573 }
7574
7575 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7576 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7577 mutex_unlock(&priv->mutex);
7578 return -EIO;
7579 }
7580
7581 spin_lock_irqsave(&priv->lock, flags);
7582
7583 if (priv->ibss_beacon)
7584 dev_kfree_skb(priv->ibss_beacon);
7585
7586 priv->ibss_beacon = skb;
7587
7588 priv->assoc_id = 0;
7589
7590 IWL_DEBUG_MAC80211("leave\n");
7591 spin_unlock_irqrestore(&priv->lock, flags);
7592
7593 #ifdef CONFIG_IWLWIFI_QOS
7594 iwl_reset_qos(priv);
7595 #endif
7596
7597 queue_work(priv->workqueue, &priv->post_associate.work);
7598
7599 mutex_unlock(&priv->mutex);
7600
7601 return 0;
7602 }
7603
7604 /*****************************************************************************
7605 *
7606 * sysfs attributes
7607 *
7608 *****************************************************************************/
7609
7610 #ifdef CONFIG_IWLWIFI_DEBUG
7611
7612 /*
7613 * The following adds a new attribute to the sysfs representation
7614 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7615 * used for controlling the debug level.
7616 *
7617 * See the level definitions in iwl for details.
7618 */
7619
7620 static ssize_t show_debug_level(struct device_driver *d, char *buf)
7621 {
7622 return sprintf(buf, "0x%08X\n", iwl_debug_level);
7623 }
7624 static ssize_t store_debug_level(struct device_driver *d,
7625 const char *buf, size_t count)
7626 {
7627 char *p = (char *)buf;
7628 u32 val;
7629
7630 val = simple_strtoul(p, &p, 0);
7631 if (p == buf)
7632 printk(KERN_INFO DRV_NAME
7633 ": %s is not in hex or decimal form.\n", buf);
7634 else
7635 iwl_debug_level = val;
7636
7637 return strnlen(buf, count);
7638 }
7639
7640 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7641 show_debug_level, store_debug_level);
7642
7643 #endif /* CONFIG_IWLWIFI_DEBUG */
7644
7645 static ssize_t show_rf_kill(struct device *d,
7646 struct device_attribute *attr, char *buf)
7647 {
7648 /*
7649 * 0 - RF kill not enabled
7650 * 1 - SW based RF kill active (sysfs)
7651 * 2 - HW based RF kill active
7652 * 3 - Both HW and SW based RF kill active
7653 */
7654 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7655 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7656 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7657
7658 return sprintf(buf, "%i\n", val);
7659 }
7660
7661 static ssize_t store_rf_kill(struct device *d,
7662 struct device_attribute *attr,
7663 const char *buf, size_t count)
7664 {
7665 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7666
7667 mutex_lock(&priv->mutex);
7668 iwl_radio_kill_sw(priv, buf[0] == '1');
7669 mutex_unlock(&priv->mutex);
7670
7671 return count;
7672 }
7673
7674 static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7675
7676 static ssize_t show_temperature(struct device *d,
7677 struct device_attribute *attr, char *buf)
7678 {
7679 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7680
7681 if (!iwl_is_alive(priv))
7682 return -EAGAIN;
7683
7684 return sprintf(buf, "%d\n", iwl_hw_get_temperature(priv));
7685 }
7686
7687 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7688
7689 static ssize_t show_rs_window(struct device *d,
7690 struct device_attribute *attr,
7691 char *buf)
7692 {
7693 struct iwl_priv *priv = d->driver_data;
7694 return iwl_fill_rs_info(priv->hw, buf, IWL_AP_ID);
7695 }
7696 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
7697
7698 static ssize_t show_tx_power(struct device *d,
7699 struct device_attribute *attr, char *buf)
7700 {
7701 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7702 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7703 }
7704
7705 static ssize_t store_tx_power(struct device *d,
7706 struct device_attribute *attr,
7707 const char *buf, size_t count)
7708 {
7709 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7710 char *p = (char *)buf;
7711 u32 val;
7712
7713 val = simple_strtoul(p, &p, 10);
7714 if (p == buf)
7715 printk(KERN_INFO DRV_NAME
7716 ": %s is not in decimal form.\n", buf);
7717 else
7718 iwl_hw_reg_set_txpower(priv, val);
7719
7720 return count;
7721 }
7722
7723 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7724
7725 static ssize_t show_flags(struct device *d,
7726 struct device_attribute *attr, char *buf)
7727 {
7728 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7729
7730 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
7731 }
7732
7733 static ssize_t store_flags(struct device *d,
7734 struct device_attribute *attr,
7735 const char *buf, size_t count)
7736 {
7737 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7738 u32 flags = simple_strtoul(buf, NULL, 0);
7739
7740 mutex_lock(&priv->mutex);
7741 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
7742 /* Cancel any currently running scans... */
7743 if (iwl_scan_cancel_timeout(priv, 100))
7744 IWL_WARNING("Could not cancel scan.\n");
7745 else {
7746 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7747 flags);
7748 priv->staging_rxon.flags = cpu_to_le32(flags);
7749 iwl_commit_rxon(priv);
7750 }
7751 }
7752 mutex_unlock(&priv->mutex);
7753
7754 return count;
7755 }
7756
7757 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7758
7759 static ssize_t show_filter_flags(struct device *d,
7760 struct device_attribute *attr, char *buf)
7761 {
7762 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7763
7764 return sprintf(buf, "0x%04X\n",
7765 le32_to_cpu(priv->active_rxon.filter_flags));
7766 }
7767
7768 static ssize_t store_filter_flags(struct device *d,
7769 struct device_attribute *attr,
7770 const char *buf, size_t count)
7771 {
7772 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7773 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7774
7775 mutex_lock(&priv->mutex);
7776 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
7777 /* Cancel any currently running scans... */
7778 if (iwl_scan_cancel_timeout(priv, 100))
7779 IWL_WARNING("Could not cancel scan.\n");
7780 else {
7781 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7782 "0x%04X\n", filter_flags);
7783 priv->staging_rxon.filter_flags =
7784 cpu_to_le32(filter_flags);
7785 iwl_commit_rxon(priv);
7786 }
7787 }
7788 mutex_unlock(&priv->mutex);
7789
7790 return count;
7791 }
7792
7793 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7794 store_filter_flags);
7795
7796 static ssize_t show_tune(struct device *d,
7797 struct device_attribute *attr, char *buf)
7798 {
7799 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7800
7801 return sprintf(buf, "0x%04X\n",
7802 (priv->phymode << 8) |
7803 le16_to_cpu(priv->active_rxon.channel));
7804 }
7805
7806 static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode);
7807
7808 static ssize_t store_tune(struct device *d,
7809 struct device_attribute *attr,
7810 const char *buf, size_t count)
7811 {
7812 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
7813 char *p = (char *)buf;
7814 u16 tune = simple_strtoul(p, &p, 0);
7815 u8 phymode = (tune >> 8) & 0xff;
7816 u16 channel = tune & 0xff;
7817
7818 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
7819
7820 mutex_lock(&priv->mutex);
7821 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
7822 (priv->phymode != phymode)) {
7823 const struct iwl_channel_info *ch_info;
7824
7825 ch_info = iwl_get_channel_info(priv, phymode, channel);
7826 if (!ch_info) {
7827 IWL_WARNING("Requested invalid phymode/channel "
7828 "combination: %d %d\n", phymode, channel);
7829 mutex_unlock(&priv->mutex);
7830 return -EINVAL;
7831 }
7832
7833 /* Cancel any currently running scans... */
7834 if (iwl_scan_cancel_timeout(priv, 100))
7835 IWL_WARNING("Could not cancel scan.\n");
7836 else {
7837 IWL_DEBUG_INFO("Committing phymode and "
7838 "rxon.channel = %d %d\n",
7839 phymode, channel);
7840
7841 iwl_set_rxon_channel(priv, phymode, channel);
7842 iwl_set_flags_for_phymode(priv, phymode);
7843
7844 iwl_set_rate(priv);
7845 iwl_commit_rxon(priv);
7846 }
7847 }
7848 mutex_unlock(&priv->mutex);
7849
7850 return count;
7851 }
7852
7853 static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
7854
7855 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
7856
7857 static ssize_t show_measurement(struct device *d,
7858 struct device_attribute *attr, char *buf)
7859 {
7860 struct iwl_priv *priv = dev_get_drvdata(d);
7861 struct iwl_spectrum_notification measure_report;
7862 u32 size = sizeof(measure_report), len = 0, ofs = 0;
7863 u8 *data = (u8 *) & measure_report;
7864 unsigned long flags;
7865
7866 spin_lock_irqsave(&priv->lock, flags);
7867 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7868 spin_unlock_irqrestore(&priv->lock, flags);
7869 return 0;
7870 }
7871 memcpy(&measure_report, &priv->measure_report, size);
7872 priv->measurement_status = 0;
7873 spin_unlock_irqrestore(&priv->lock, flags);
7874
7875 while (size && (PAGE_SIZE - len)) {
7876 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7877 PAGE_SIZE - len, 1);
7878 len = strlen(buf);
7879 if (PAGE_SIZE - len)
7880 buf[len++] = '\n';
7881
7882 ofs += 16;
7883 size -= min(size, 16U);
7884 }
7885
7886 return len;
7887 }
7888
7889 static ssize_t store_measurement(struct device *d,
7890 struct device_attribute *attr,
7891 const char *buf, size_t count)
7892 {
7893 struct iwl_priv *priv = dev_get_drvdata(d);
7894 struct ieee80211_measurement_params params = {
7895 .channel = le16_to_cpu(priv->active_rxon.channel),
7896 .start_time = cpu_to_le64(priv->last_tsf),
7897 .duration = cpu_to_le16(1),
7898 };
7899 u8 type = IWL_MEASURE_BASIC;
7900 u8 buffer[32];
7901 u8 channel;
7902
7903 if (count) {
7904 char *p = buffer;
7905 strncpy(buffer, buf, min(sizeof(buffer), count));
7906 channel = simple_strtoul(p, NULL, 0);
7907 if (channel)
7908 params.channel = channel;
7909
7910 p = buffer;
7911 while (*p && *p != ' ')
7912 p++;
7913 if (*p)
7914 type = simple_strtoul(p + 1, NULL, 0);
7915 }
7916
7917 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7918 "channel %d (for '%s')\n", type, params.channel, buf);
7919 iwl_get_measurement(priv, &params, type);
7920
7921 return count;
7922 }
7923
7924 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7925 show_measurement, store_measurement);
7926 #endif /* CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT */
7927
7928 static ssize_t show_rate(struct device *d,
7929 struct device_attribute *attr, char *buf)
7930 {
7931 struct iwl_priv *priv = dev_get_drvdata(d);
7932 unsigned long flags;
7933 int i;
7934
7935 spin_lock_irqsave(&priv->sta_lock, flags);
7936 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
7937 i = priv->stations[IWL_AP_ID].current_rate.s.rate;
7938 else
7939 i = priv->stations[IWL_STA_ID].current_rate.s.rate;
7940 spin_unlock_irqrestore(&priv->sta_lock, flags);
7941
7942 i = iwl_rate_index_from_plcp(i);
7943 if (i == -1)
7944 return sprintf(buf, "0\n");
7945
7946 return sprintf(buf, "%d%s\n",
7947 (iwl_rates[i].ieee >> 1),
7948 (iwl_rates[i].ieee & 0x1) ? ".5" : "");
7949 }
7950
7951 static DEVICE_ATTR(rate, S_IRUSR, show_rate, NULL);
7952
7953 static ssize_t store_retry_rate(struct device *d,
7954 struct device_attribute *attr,
7955 const char *buf, size_t count)
7956 {
7957 struct iwl_priv *priv = dev_get_drvdata(d);
7958
7959 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7960 if (priv->retry_rate <= 0)
7961 priv->retry_rate = 1;
7962
7963 return count;
7964 }
7965
7966 static ssize_t show_retry_rate(struct device *d,
7967 struct device_attribute *attr, char *buf)
7968 {
7969 struct iwl_priv *priv = dev_get_drvdata(d);
7970 return sprintf(buf, "%d", priv->retry_rate);
7971 }
7972
7973 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7974 store_retry_rate);
7975
7976 static ssize_t store_power_level(struct device *d,
7977 struct device_attribute *attr,
7978 const char *buf, size_t count)
7979 {
7980 struct iwl_priv *priv = dev_get_drvdata(d);
7981 int rc;
7982 int mode;
7983
7984 mode = simple_strtoul(buf, NULL, 0);
7985 mutex_lock(&priv->mutex);
7986
7987 if (!iwl_is_ready(priv)) {
7988 rc = -EAGAIN;
7989 goto out;
7990 }
7991
7992 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
7993 mode = IWL_POWER_AC;
7994 else
7995 mode |= IWL_POWER_ENABLED;
7996
7997 if (mode != priv->power_mode) {
7998 rc = iwl_send_power_mode(priv, IWL_POWER_LEVEL(mode));
7999 if (rc) {
8000 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8001 goto out;
8002 }
8003 priv->power_mode = mode;
8004 }
8005
8006 rc = count;
8007
8008 out:
8009 mutex_unlock(&priv->mutex);
8010 return rc;
8011 }
8012
8013 #define MAX_WX_STRING 80
8014
8015 /* Values are in microsecond */
8016 static const s32 timeout_duration[] = {
8017 350000,
8018 250000,
8019 75000,
8020 37000,
8021 25000,
8022 };
8023 static const s32 period_duration[] = {
8024 400000,
8025 700000,
8026 1000000,
8027 1000000,
8028 1000000
8029 };
8030
8031 static ssize_t show_power_level(struct device *d,
8032 struct device_attribute *attr, char *buf)
8033 {
8034 struct iwl_priv *priv = dev_get_drvdata(d);
8035 int level = IWL_POWER_LEVEL(priv->power_mode);
8036 char *p = buf;
8037
8038 p += sprintf(p, "%d ", level);
8039 switch (level) {
8040 case IWL_POWER_MODE_CAM:
8041 case IWL_POWER_AC:
8042 p += sprintf(p, "(AC)");
8043 break;
8044 case IWL_POWER_BATTERY:
8045 p += sprintf(p, "(BATTERY)");
8046 break;
8047 default:
8048 p += sprintf(p,
8049 "(Timeout %dms, Period %dms)",
8050 timeout_duration[level - 1] / 1000,
8051 period_duration[level - 1] / 1000);
8052 }
8053
8054 if (!(priv->power_mode & IWL_POWER_ENABLED))
8055 p += sprintf(p, " OFF\n");
8056 else
8057 p += sprintf(p, " \n");
8058
8059 return (p - buf + 1);
8060
8061 }
8062
8063 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8064 store_power_level);
8065
8066 static ssize_t show_channels(struct device *d,
8067 struct device_attribute *attr, char *buf)
8068 {
8069 struct iwl_priv *priv = dev_get_drvdata(d);
8070 int len = 0, i;
8071 struct ieee80211_channel *channels = NULL;
8072 const struct ieee80211_hw_mode *hw_mode = NULL;
8073 int count = 0;
8074
8075 if (!iwl_is_ready(priv))
8076 return -EAGAIN;
8077
8078 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211G);
8079 if (!hw_mode)
8080 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211B);
8081 if (hw_mode) {
8082 channels = hw_mode->channels;
8083 count = hw_mode->num_channels;
8084 }
8085
8086 len +=
8087 sprintf(&buf[len],
8088 "Displaying %d channels in 2.4GHz band "
8089 "(802.11bg):\n", count);
8090
8091 for (i = 0; i < count; i++)
8092 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8093 channels[i].chan,
8094 channels[i].power_level,
8095 channels[i].
8096 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8097 " (IEEE 802.11h required)" : "",
8098 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8099 || (channels[i].
8100 flag &
8101 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8102 ", IBSS",
8103 channels[i].
8104 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8105 "active/passive" : "passive only");
8106
8107 hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211A);
8108 if (hw_mode) {
8109 channels = hw_mode->channels;
8110 count = hw_mode->num_channels;
8111 } else {
8112 channels = NULL;
8113 count = 0;
8114 }
8115
8116 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8117 "(802.11a):\n", count);
8118
8119 for (i = 0; i < count; i++)
8120 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8121 channels[i].chan,
8122 channels[i].power_level,
8123 channels[i].
8124 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8125 " (IEEE 802.11h required)" : "",
8126 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8127 || (channels[i].
8128 flag &
8129 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8130 ", IBSS",
8131 channels[i].
8132 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8133 "active/passive" : "passive only");
8134
8135 return len;
8136 }
8137
8138 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8139
8140 static ssize_t show_statistics(struct device *d,
8141 struct device_attribute *attr, char *buf)
8142 {
8143 struct iwl_priv *priv = dev_get_drvdata(d);
8144 u32 size = sizeof(struct iwl_notif_statistics);
8145 u32 len = 0, ofs = 0;
8146 u8 *data = (u8 *) & priv->statistics;
8147 int rc = 0;
8148
8149 if (!iwl_is_alive(priv))
8150 return -EAGAIN;
8151
8152 mutex_lock(&priv->mutex);
8153 rc = iwl_send_statistics_request(priv);
8154 mutex_unlock(&priv->mutex);
8155
8156 if (rc) {
8157 len = sprintf(buf,
8158 "Error sending statistics request: 0x%08X\n", rc);
8159 return len;
8160 }
8161
8162 while (size && (PAGE_SIZE - len)) {
8163 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8164 PAGE_SIZE - len, 1);
8165 len = strlen(buf);
8166 if (PAGE_SIZE - len)
8167 buf[len++] = '\n';
8168
8169 ofs += 16;
8170 size -= min(size, 16U);
8171 }
8172
8173 return len;
8174 }
8175
8176 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8177
8178 static ssize_t show_antenna(struct device *d,
8179 struct device_attribute *attr, char *buf)
8180 {
8181 struct iwl_priv *priv = dev_get_drvdata(d);
8182
8183 if (!iwl_is_alive(priv))
8184 return -EAGAIN;
8185
8186 return sprintf(buf, "%d\n", priv->antenna);
8187 }
8188
8189 static ssize_t store_antenna(struct device *d,
8190 struct device_attribute *attr,
8191 const char *buf, size_t count)
8192 {
8193 int ant;
8194 struct iwl_priv *priv = dev_get_drvdata(d);
8195
8196 if (count == 0)
8197 return 0;
8198
8199 if (sscanf(buf, "%1i", &ant) != 1) {
8200 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8201 return count;
8202 }
8203
8204 if ((ant >= 0) && (ant <= 2)) {
8205 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
8206 priv->antenna = (enum iwl_antenna)ant;
8207 } else
8208 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8209
8210
8211 return count;
8212 }
8213
8214 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8215
8216 static ssize_t show_status(struct device *d,
8217 struct device_attribute *attr, char *buf)
8218 {
8219 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
8220 if (!iwl_is_alive(priv))
8221 return -EAGAIN;
8222 return sprintf(buf, "0x%08x\n", (int)priv->status);
8223 }
8224
8225 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8226
8227 static ssize_t dump_error_log(struct device *d,
8228 struct device_attribute *attr,
8229 const char *buf, size_t count)
8230 {
8231 char *p = (char *)buf;
8232
8233 if (p[0] == '1')
8234 iwl_dump_nic_error_log((struct iwl_priv *)d->driver_data);
8235
8236 return strnlen(buf, count);
8237 }
8238
8239 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8240
8241 static ssize_t dump_event_log(struct device *d,
8242 struct device_attribute *attr,
8243 const char *buf, size_t count)
8244 {
8245 char *p = (char *)buf;
8246
8247 if (p[0] == '1')
8248 iwl_dump_nic_event_log((struct iwl_priv *)d->driver_data);
8249
8250 return strnlen(buf, count);
8251 }
8252
8253 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8254
8255 /*****************************************************************************
8256 *
8257 * driver setup and teardown
8258 *
8259 *****************************************************************************/
8260
8261 static void iwl_setup_deferred_work(struct iwl_priv *priv)
8262 {
8263 priv->workqueue = create_workqueue(DRV_NAME);
8264
8265 init_waitqueue_head(&priv->wait_command_queue);
8266
8267 INIT_WORK(&priv->up, iwl_bg_up);
8268 INIT_WORK(&priv->restart, iwl_bg_restart);
8269 INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
8270 INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
8271 INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
8272 INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
8273 INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
8274 INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
8275 INIT_DELAYED_WORK(&priv->post_associate, iwl_bg_post_associate);
8276 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
8277 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
8278 INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
8279
8280 iwl_hw_setup_deferred_work(priv);
8281
8282 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
8283 iwl_irq_tasklet, (unsigned long)priv);
8284 }
8285
8286 static void iwl_cancel_deferred_work(struct iwl_priv *priv)
8287 {
8288 iwl_hw_cancel_deferred_work(priv);
8289
8290 cancel_delayed_work_sync(&priv->init_alive_start);
8291 cancel_delayed_work(&priv->scan_check);
8292 cancel_delayed_work(&priv->alive_start);
8293 cancel_delayed_work(&priv->post_associate);
8294 cancel_work_sync(&priv->beacon_update);
8295 }
8296
8297 static struct attribute *iwl_sysfs_entries[] = {
8298 &dev_attr_antenna.attr,
8299 &dev_attr_channels.attr,
8300 &dev_attr_dump_errors.attr,
8301 &dev_attr_dump_events.attr,
8302 &dev_attr_flags.attr,
8303 &dev_attr_filter_flags.attr,
8304 #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
8305 &dev_attr_measurement.attr,
8306 #endif
8307 &dev_attr_power_level.attr,
8308 &dev_attr_rate.attr,
8309 &dev_attr_retry_rate.attr,
8310 &dev_attr_rf_kill.attr,
8311 &dev_attr_rs_window.attr,
8312 &dev_attr_statistics.attr,
8313 &dev_attr_status.attr,
8314 &dev_attr_temperature.attr,
8315 &dev_attr_tune.attr,
8316 &dev_attr_tx_power.attr,
8317
8318 NULL
8319 };
8320
8321 static struct attribute_group iwl_attribute_group = {
8322 .name = NULL, /* put in device directory */
8323 .attrs = iwl_sysfs_entries,
8324 };
8325
8326 static struct ieee80211_ops iwl_hw_ops = {
8327 .tx = iwl_mac_tx,
8328 .start = iwl_mac_start,
8329 .stop = iwl_mac_stop,
8330 .add_interface = iwl_mac_add_interface,
8331 .remove_interface = iwl_mac_remove_interface,
8332 .config = iwl_mac_config,
8333 .config_interface = iwl_mac_config_interface,
8334 .configure_filter = iwl_configure_filter,
8335 .set_key = iwl_mac_set_key,
8336 .get_stats = iwl_mac_get_stats,
8337 .get_tx_stats = iwl_mac_get_tx_stats,
8338 .conf_tx = iwl_mac_conf_tx,
8339 .get_tsf = iwl_mac_get_tsf,
8340 .reset_tsf = iwl_mac_reset_tsf,
8341 .beacon_update = iwl_mac_beacon_update,
8342 .hw_scan = iwl_mac_hw_scan
8343 };
8344
8345 static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
8346 {
8347 int err = 0;
8348 u32 pci_id;
8349 struct iwl_priv *priv;
8350 struct ieee80211_hw *hw;
8351 int i;
8352
8353 if (iwl_param_disable_hw_scan) {
8354 IWL_DEBUG_INFO("Disabling hw_scan\n");
8355 iwl_hw_ops.hw_scan = NULL;
8356 }
8357
8358 if ((iwl_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8359 (iwl_param_queues_num < IWL_MIN_NUM_QUEUES)) {
8360 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8361 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8362 err = -EINVAL;
8363 goto out;
8364 }
8365
8366 /* mac80211 allocates memory for this device instance, including
8367 * space for this driver's private structure */
8368 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl_hw_ops);
8369 if (hw == NULL) {
8370 IWL_ERROR("Can not allocate network device\n");
8371 err = -ENOMEM;
8372 goto out;
8373 }
8374 SET_IEEE80211_DEV(hw, &pdev->dev);
8375
8376 hw->rate_control_algorithm = "iwl-3945-rs";
8377
8378 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8379 priv = hw->priv;
8380 priv->hw = hw;
8381
8382 priv->pci_dev = pdev;
8383 priv->antenna = (enum iwl_antenna)iwl_param_antenna;
8384 #ifdef CONFIG_IWLWIFI_DEBUG
8385 iwl_debug_level = iwl_param_debug;
8386 atomic_set(&priv->restrict_refcnt, 0);
8387 #endif
8388 priv->retry_rate = 1;
8389
8390 priv->ibss_beacon = NULL;
8391
8392 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8393 * the range of signal quality values that we'll provide.
8394 * Negative values for level/noise indicate that we'll provide dBm.
8395 * For WE, at least, non-0 values here *enable* display of values
8396 * in app (iwconfig). */
8397 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8398 hw->max_noise = -20; /* noise level, negative indicates dBm */
8399 hw->max_signal = 100; /* link quality indication (%) */
8400
8401 /* Tell mac80211 our Tx characteristics */
8402 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8403
8404 hw->queues = 4;
8405
8406 spin_lock_init(&priv->lock);
8407 spin_lock_init(&priv->power_data.lock);
8408 spin_lock_init(&priv->sta_lock);
8409 spin_lock_init(&priv->hcmd_lock);
8410
8411 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8412 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8413
8414 INIT_LIST_HEAD(&priv->free_frames);
8415
8416 mutex_init(&priv->mutex);
8417 if (pci_enable_device(pdev)) {
8418 err = -ENODEV;
8419 goto out_ieee80211_free_hw;
8420 }
8421
8422 pci_set_master(pdev);
8423
8424 iwl_clear_stations_table(priv);
8425
8426 priv->data_retry_limit = -1;
8427 priv->ieee_channels = NULL;
8428 priv->ieee_rates = NULL;
8429 priv->phymode = -1;
8430
8431 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8432 if (!err)
8433 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8434 if (err) {
8435 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
8436 goto out_pci_disable_device;
8437 }
8438
8439 pci_set_drvdata(pdev, priv);
8440 err = pci_request_regions(pdev, DRV_NAME);
8441 if (err)
8442 goto out_pci_disable_device;
8443 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8444 * PCI Tx retries from interfering with C3 CPU state */
8445 pci_write_config_byte(pdev, 0x41, 0x00);
8446 priv->hw_base = pci_iomap(pdev, 0, 0);
8447 if (!priv->hw_base) {
8448 err = -ENODEV;
8449 goto out_pci_release_regions;
8450 }
8451
8452 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8453 (unsigned long long) pci_resource_len(pdev, 0));
8454 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8455
8456 /* Initialize module parameter values here */
8457
8458 if (iwl_param_disable) {
8459 set_bit(STATUS_RF_KILL_SW, &priv->status);
8460 IWL_DEBUG_INFO("Radio disabled.\n");
8461 }
8462
8463 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8464
8465 pci_id =
8466 (priv->pci_dev->device << 16) | priv->pci_dev->subsystem_device;
8467
8468 switch (pci_id) {
8469 case 0x42221005: /* 0x4222 0x8086 0x1005 is BG SKU */
8470 case 0x42221034: /* 0x4222 0x8086 0x1034 is BG SKU */
8471 case 0x42271014: /* 0x4227 0x8086 0x1014 is BG SKU */
8472 case 0x42221044: /* 0x4222 0x8086 0x1044 is BG SKU */
8473 priv->is_abg = 0;
8474 break;
8475
8476 /*
8477 * Rest are assumed ABG SKU -- if this is not the
8478 * case then the card will get the wrong 'Detected'
8479 * line in the kernel log however the code that
8480 * initializes the GEO table will detect no A-band
8481 * channels and remove the is_abg mask.
8482 */
8483 default:
8484 priv->is_abg = 1;
8485 break;
8486 }
8487
8488 printk(KERN_INFO DRV_NAME
8489 ": Detected Intel PRO/Wireless 3945%sBG Network Connection\n",
8490 priv->is_abg ? "A" : "");
8491
8492 /* Device-specific setup */
8493 if (iwl_hw_set_hw_setting(priv)) {
8494 IWL_ERROR("failed to set hw settings\n");
8495 mutex_unlock(&priv->mutex);
8496 goto out_iounmap;
8497 }
8498
8499 #ifdef CONFIG_IWLWIFI_QOS
8500 if (iwl_param_qos_enable)
8501 priv->qos_data.qos_enable = 1;
8502
8503 iwl_reset_qos(priv);
8504
8505 priv->qos_data.qos_active = 0;
8506 priv->qos_data.qos_cap.val = 0;
8507 #endif /* CONFIG_IWLWIFI_QOS */
8508
8509 iwl_set_rxon_channel(priv, MODE_IEEE80211G, 6);
8510 iwl_setup_deferred_work(priv);
8511 iwl_setup_rx_handlers(priv);
8512
8513 priv->rates_mask = IWL_RATES_MASK;
8514 /* If power management is turned on, default to AC mode */
8515 priv->power_mode = IWL_POWER_AC;
8516 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8517
8518 pci_enable_msi(pdev);
8519
8520 err = request_irq(pdev->irq, iwl_isr, IRQF_SHARED, DRV_NAME, priv);
8521 if (err) {
8522 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
8523 goto out_disable_msi;
8524 }
8525
8526 mutex_lock(&priv->mutex);
8527
8528 err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
8529 if (err) {
8530 IWL_ERROR("failed to create sysfs device attributes\n");
8531 mutex_unlock(&priv->mutex);
8532 goto out_release_irq;
8533 }
8534
8535 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
8536 * ucode filename and max sizes are card-specific. */
8537 err = iwl_read_ucode(priv);
8538 if (err) {
8539 IWL_ERROR("Could not read microcode: %d\n", err);
8540 mutex_unlock(&priv->mutex);
8541 goto out_pci_alloc;
8542 }
8543
8544 mutex_unlock(&priv->mutex);
8545
8546 IWL_DEBUG_INFO("Queueing UP work.\n");
8547
8548 queue_work(priv->workqueue, &priv->up);
8549
8550 return 0;
8551
8552 out_pci_alloc:
8553 iwl_dealloc_ucode_pci(priv);
8554
8555 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
8556
8557 out_release_irq:
8558 free_irq(pdev->irq, priv);
8559
8560 out_disable_msi:
8561 pci_disable_msi(pdev);
8562 destroy_workqueue(priv->workqueue);
8563 priv->workqueue = NULL;
8564 iwl_unset_hw_setting(priv);
8565
8566 out_iounmap:
8567 pci_iounmap(pdev, priv->hw_base);
8568 out_pci_release_regions:
8569 pci_release_regions(pdev);
8570 out_pci_disable_device:
8571 pci_disable_device(pdev);
8572 pci_set_drvdata(pdev, NULL);
8573 out_ieee80211_free_hw:
8574 ieee80211_free_hw(priv->hw);
8575 out:
8576 return err;
8577 }
8578
8579 static void iwl_pci_remove(struct pci_dev *pdev)
8580 {
8581 struct iwl_priv *priv = pci_get_drvdata(pdev);
8582 struct list_head *p, *q;
8583 int i;
8584
8585 if (!priv)
8586 return;
8587
8588 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8589
8590 set_bit(STATUS_EXIT_PENDING, &priv->status);
8591
8592 iwl_down(priv);
8593
8594 /* Free MAC hash list for ADHOC */
8595 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8596 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8597 list_del(p);
8598 kfree(list_entry(p, struct iwl_ibss_seq, list));
8599 }
8600 }
8601
8602 sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
8603
8604 iwl_dealloc_ucode_pci(priv);
8605
8606 if (priv->rxq.bd)
8607 iwl_rx_queue_free(priv, &priv->rxq);
8608 iwl_hw_txq_ctx_free(priv);
8609
8610 iwl_unset_hw_setting(priv);
8611 iwl_clear_stations_table(priv);
8612
8613 if (priv->mac80211_registered) {
8614 ieee80211_unregister_hw(priv->hw);
8615 iwl_rate_control_unregister(priv->hw);
8616 }
8617
8618 /*netif_stop_queue(dev); */
8619 flush_workqueue(priv->workqueue);
8620
8621 /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
8622 * priv->workqueue... so we can't take down the workqueue
8623 * until now... */
8624 destroy_workqueue(priv->workqueue);
8625 priv->workqueue = NULL;
8626
8627 free_irq(pdev->irq, priv);
8628 pci_disable_msi(pdev);
8629 pci_iounmap(pdev, priv->hw_base);
8630 pci_release_regions(pdev);
8631 pci_disable_device(pdev);
8632 pci_set_drvdata(pdev, NULL);
8633
8634 kfree(priv->channel_info);
8635
8636 kfree(priv->ieee_channels);
8637 kfree(priv->ieee_rates);
8638
8639 if (priv->ibss_beacon)
8640 dev_kfree_skb(priv->ibss_beacon);
8641
8642 ieee80211_free_hw(priv->hw);
8643 }
8644
8645 #ifdef CONFIG_PM
8646
8647 static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
8648 {
8649 struct iwl_priv *priv = pci_get_drvdata(pdev);
8650
8651 set_bit(STATUS_IN_SUSPEND, &priv->status);
8652
8653 /* Take down the device; powers it off, etc. */
8654 iwl_down(priv);
8655
8656 if (priv->mac80211_registered)
8657 ieee80211_stop_queues(priv->hw);
8658
8659 pci_save_state(pdev);
8660 pci_disable_device(pdev);
8661 pci_set_power_state(pdev, PCI_D3hot);
8662
8663 return 0;
8664 }
8665
8666 static void iwl_resume(struct iwl_priv *priv)
8667 {
8668 unsigned long flags;
8669
8670 /* The following it a temporary work around due to the
8671 * suspend / resume not fully initializing the NIC correctly.
8672 * Without all of the following, resume will not attempt to take
8673 * down the NIC (it shouldn't really need to) and will just try
8674 * and bring the NIC back up. However that fails during the
8675 * ucode verification process. This then causes iwl_down to be
8676 * called *after* iwl_hw_nic_init() has succeeded -- which
8677 * then lets the next init sequence succeed. So, we've
8678 * replicated all of that NIC init code here... */
8679
8680 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
8681
8682 iwl_hw_nic_init(priv);
8683
8684 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8685 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
8686 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
8687 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
8688 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8689 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
8690
8691 /* tell the device to stop sending interrupts */
8692 iwl_disable_interrupts(priv);
8693
8694 spin_lock_irqsave(&priv->lock, flags);
8695 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
8696
8697 if (!iwl_grab_restricted_access(priv)) {
8698 iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
8699 APMG_CLK_VAL_DMA_CLK_RQT);
8700 iwl_release_restricted_access(priv);
8701 }
8702 spin_unlock_irqrestore(&priv->lock, flags);
8703
8704 udelay(5);
8705
8706 iwl_hw_nic_reset(priv);
8707
8708 /* Bring the device back up */
8709 clear_bit(STATUS_IN_SUSPEND, &priv->status);
8710 queue_work(priv->workqueue, &priv->up);
8711 }
8712
8713 static int iwl_pci_resume(struct pci_dev *pdev)
8714 {
8715 struct iwl_priv *priv = pci_get_drvdata(pdev);
8716 int err;
8717
8718 printk(KERN_INFO "Coming out of suspend...\n");
8719
8720 pci_set_power_state(pdev, PCI_D0);
8721 err = pci_enable_device(pdev);
8722 pci_restore_state(pdev);
8723
8724 /*
8725 * Suspend/Resume resets the PCI configuration space, so we have to
8726 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
8727 * from interfering with C3 CPU state. pci_restore_state won't help
8728 * here since it only restores the first 64 bytes pci config header.
8729 */
8730 pci_write_config_byte(pdev, 0x41, 0x00);
8731
8732 iwl_resume(priv);
8733
8734 return 0;
8735 }
8736
8737 #endif /* CONFIG_PM */
8738
8739 /*****************************************************************************
8740 *
8741 * driver and module entry point
8742 *
8743 *****************************************************************************/
8744
8745 static struct pci_driver iwl_driver = {
8746 .name = DRV_NAME,
8747 .id_table = iwl_hw_card_ids,
8748 .probe = iwl_pci_probe,
8749 .remove = __devexit_p(iwl_pci_remove),
8750 #ifdef CONFIG_PM
8751 .suspend = iwl_pci_suspend,
8752 .resume = iwl_pci_resume,
8753 #endif
8754 };
8755
8756 static int __init iwl_init(void)
8757 {
8758
8759 int ret;
8760 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8761 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
8762 ret = pci_register_driver(&iwl_driver);
8763 if (ret) {
8764 IWL_ERROR("Unable to initialize PCI module\n");
8765 return ret;
8766 }
8767 #ifdef CONFIG_IWLWIFI_DEBUG
8768 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
8769 if (ret) {
8770 IWL_ERROR("Unable to create driver sysfs file\n");
8771 pci_unregister_driver(&iwl_driver);
8772 return ret;
8773 }
8774 #endif
8775
8776 return ret;
8777 }
8778
8779 static void __exit iwl_exit(void)
8780 {
8781 #ifdef CONFIG_IWLWIFI_DEBUG
8782 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
8783 #endif
8784 pci_unregister_driver(&iwl_driver);
8785 }
8786
8787 module_param_named(antenna, iwl_param_antenna, int, 0444);
8788 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
8789 module_param_named(disable, iwl_param_disable, int, 0444);
8790 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
8791 module_param_named(hwcrypto, iwl_param_hwcrypto, int, 0444);
8792 MODULE_PARM_DESC(hwcrypto,
8793 "using hardware crypto engine (default 0 [software])\n");
8794 module_param_named(debug, iwl_param_debug, int, 0444);
8795 MODULE_PARM_DESC(debug, "debug output mask");
8796 module_param_named(disable_hw_scan, iwl_param_disable_hw_scan, int, 0444);
8797 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8798
8799 module_param_named(queues_num, iwl_param_queues_num, int, 0444);
8800 MODULE_PARM_DESC(queues_num, "number of hw queues.");
8801
8802 /* QoS */
8803 module_param_named(qos_enable, iwl_param_qos_enable, int, 0444);
8804 MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
8805
8806 module_exit(iwl_exit);
8807 module_init(iwl_init);
This page took 0.285077 seconds and 6 git commands to generate.