iwlwifi: generalize iwl4965_send_add_station function
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl4965-base.c
1 /******************************************************************************
2 *
3 * Copyright(c) 2003 - 2008 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 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/init.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/delay.h>
37 #include <linux/skbuff.h>
38 #include <linux/netdevice.h>
39 #include <linux/wireless.h>
40 #include <linux/firmware.h>
41 #include <linux/etherdevice.h>
42 #include <linux/if_arp.h>
43
44 #include <net/mac80211.h>
45
46 #include <asm/div64.h>
47
48 #include "iwl-eeprom.h"
49 #include "iwl-dev.h"
50 #include "iwl-core.h"
51 #include "iwl-io.h"
52 #include "iwl-helpers.h"
53 #include "iwl-sta.h"
54 #include "iwl-calib.h"
55
56 static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
57 struct iwl4965_tx_queue *txq);
58
59 /******************************************************************************
60 *
61 * module boiler plate
62 *
63 ******************************************************************************/
64
65 /*
66 * module name, copyright, version, etc.
67 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
68 */
69
70 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
71
72 #ifdef CONFIG_IWLWIFI_DEBUG
73 #define VD "d"
74 #else
75 #define VD
76 #endif
77
78 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
79 #define VS "s"
80 #else
81 #define VS
82 #endif
83
84 #define DRV_VERSION IWLWIFI_VERSION VD VS
85
86
87 MODULE_DESCRIPTION(DRV_DESCRIPTION);
88 MODULE_VERSION(DRV_VERSION);
89 MODULE_AUTHOR(DRV_COPYRIGHT);
90 MODULE_LICENSE("GPL");
91
92 __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
93 {
94 u16 fc = le16_to_cpu(hdr->frame_control);
95 int hdr_len = ieee80211_get_hdrlen(fc);
96
97 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
98 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
99 return NULL;
100 }
101
102 static const struct ieee80211_supported_band *iwl_get_hw_mode(
103 struct iwl_priv *priv, enum ieee80211_band band)
104 {
105 return priv->hw->wiphy->bands[band];
106 }
107
108 static int iwl4965_is_empty_essid(const char *essid, int essid_len)
109 {
110 /* Single white space is for Linksys APs */
111 if (essid_len == 1 && essid[0] == ' ')
112 return 1;
113
114 /* Otherwise, if the entire essid is 0, we assume it is hidden */
115 while (essid_len) {
116 essid_len--;
117 if (essid[essid_len] != '\0')
118 return 0;
119 }
120
121 return 1;
122 }
123
124 static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
125 {
126 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
127 const char *s = essid;
128 char *d = escaped;
129
130 if (iwl4965_is_empty_essid(essid, essid_len)) {
131 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
132 return escaped;
133 }
134
135 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
136 while (essid_len--) {
137 if (*s == '\0') {
138 *d++ = '\\';
139 *d++ = '0';
140 s++;
141 } else
142 *d++ = *s++;
143 }
144 *d = '\0';
145 return escaped;
146 }
147
148 /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
149 * DMA services
150 *
151 * Theory of operation
152 *
153 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
154 * of buffer descriptors, each of which points to one or more data buffers for
155 * the device to read from or fill. Driver and device exchange status of each
156 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
157 * entries in each circular buffer, to protect against confusing empty and full
158 * queue states.
159 *
160 * The device reads or writes the data in the queues via the device's several
161 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
162 *
163 * For Tx queue, there are low mark and high mark limits. If, after queuing
164 * the packet for Tx, free space become < low mark, Tx queue stopped. When
165 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
166 * Tx queue resumed.
167 *
168 * The 4965 operates with up to 17 queues: One receive queue, one transmit
169 * queue (#4) for sending commands to the device firmware, and 15 other
170 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
171 *
172 * See more detailed info in iwl-4965-hw.h.
173 ***************************************************/
174
175 int iwl4965_queue_space(const struct iwl4965_queue *q)
176 {
177 int s = q->read_ptr - q->write_ptr;
178
179 if (q->read_ptr > q->write_ptr)
180 s -= q->n_bd;
181
182 if (s <= 0)
183 s += q->n_window;
184 /* keep some reserve to not confuse empty and full situations */
185 s -= 2;
186 if (s < 0)
187 s = 0;
188 return s;
189 }
190
191
192 static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
193 {
194 return q->write_ptr > q->read_ptr ?
195 (i >= q->read_ptr && i < q->write_ptr) :
196 !(i < q->read_ptr && i >= q->write_ptr);
197 }
198
199 static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
200 {
201 /* This is for scan command, the big buffer at end of command array */
202 if (is_huge)
203 return q->n_window; /* must be power of 2 */
204
205 /* Otherwise, use normal size buffers */
206 return index & (q->n_window - 1);
207 }
208
209 /**
210 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
211 */
212 static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
213 int count, int slots_num, u32 id)
214 {
215 q->n_bd = count;
216 q->n_window = slots_num;
217 q->id = id;
218
219 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
220 * and iwl_queue_dec_wrap are broken. */
221 BUG_ON(!is_power_of_2(count));
222
223 /* slots_num must be power-of-two size, otherwise
224 * get_cmd_index is broken. */
225 BUG_ON(!is_power_of_2(slots_num));
226
227 q->low_mark = q->n_window / 4;
228 if (q->low_mark < 4)
229 q->low_mark = 4;
230
231 q->high_mark = q->n_window / 8;
232 if (q->high_mark < 2)
233 q->high_mark = 2;
234
235 q->write_ptr = q->read_ptr = 0;
236
237 return 0;
238 }
239
240 /**
241 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
242 */
243 static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
244 struct iwl4965_tx_queue *txq, u32 id)
245 {
246 struct pci_dev *dev = priv->pci_dev;
247
248 /* Driver private data, only for Tx (not command) queues,
249 * not shared with device. */
250 if (id != IWL_CMD_QUEUE_NUM) {
251 txq->txb = kmalloc(sizeof(txq->txb[0]) *
252 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
253 if (!txq->txb) {
254 IWL_ERROR("kmalloc for auxiliary BD "
255 "structures failed\n");
256 goto error;
257 }
258 } else
259 txq->txb = NULL;
260
261 /* Circular buffer of transmit frame descriptors (TFDs),
262 * shared with device */
263 txq->bd = pci_alloc_consistent(dev,
264 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
265 &txq->q.dma_addr);
266
267 if (!txq->bd) {
268 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
269 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
270 goto error;
271 }
272 txq->q.id = id;
273
274 return 0;
275
276 error:
277 if (txq->txb) {
278 kfree(txq->txb);
279 txq->txb = NULL;
280 }
281
282 return -ENOMEM;
283 }
284
285 /**
286 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
287 */
288 int iwl4965_tx_queue_init(struct iwl_priv *priv,
289 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
290 {
291 struct pci_dev *dev = priv->pci_dev;
292 int len;
293 int rc = 0;
294
295 /*
296 * Alloc buffer array for commands (Tx or other types of commands).
297 * For the command queue (#4), allocate command space + one big
298 * command for scan, since scan command is very huge; the system will
299 * not have two scans at the same time, so only one is needed.
300 * For normal Tx queues (all other queues), no super-size command
301 * space is needed.
302 */
303 len = sizeof(struct iwl_cmd) * slots_num;
304 if (txq_id == IWL_CMD_QUEUE_NUM)
305 len += IWL_MAX_SCAN_SIZE;
306 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
307 if (!txq->cmd)
308 return -ENOMEM;
309
310 /* Alloc driver data array and TFD circular buffer */
311 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
312 if (rc) {
313 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
314
315 return -ENOMEM;
316 }
317 txq->need_update = 0;
318
319 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
320 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
321 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
322
323 /* Initialize queue's high/low-water marks, and head/tail indexes */
324 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
325
326 /* Tell device where to find queue */
327 iwl4965_hw_tx_queue_init(priv, txq);
328
329 return 0;
330 }
331
332 /**
333 * iwl4965_tx_queue_free - Deallocate DMA queue.
334 * @txq: Transmit queue to deallocate.
335 *
336 * Empty queue by removing and destroying all BD's.
337 * Free all buffers.
338 * 0-fill, but do not free "txq" descriptor structure.
339 */
340 void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
341 {
342 struct iwl4965_queue *q = &txq->q;
343 struct pci_dev *dev = priv->pci_dev;
344 int len;
345
346 if (q->n_bd == 0)
347 return;
348
349 /* first, empty all BD's */
350 for (; q->write_ptr != q->read_ptr;
351 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
352 iwl4965_hw_txq_free_tfd(priv, txq);
353
354 len = sizeof(struct iwl_cmd) * q->n_window;
355 if (q->id == IWL_CMD_QUEUE_NUM)
356 len += IWL_MAX_SCAN_SIZE;
357
358 /* De-alloc array of command/tx buffers */
359 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
360
361 /* De-alloc circular buffer of TFDs */
362 if (txq->q.n_bd)
363 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
364 txq->q.n_bd, txq->bd, txq->q.dma_addr);
365
366 /* De-alloc array of per-TFD driver data */
367 if (txq->txb) {
368 kfree(txq->txb);
369 txq->txb = NULL;
370 }
371
372 /* 0-fill queue descriptor structure */
373 memset(txq, 0, sizeof(*txq));
374 }
375
376 const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
377
378 /*************** STATION TABLE MANAGEMENT ****
379 * mac80211 should be examined to determine if sta_info is duplicating
380 * the functionality provided here
381 */
382
383 /**************************************************************/
384
385 #if 0 /* temporary disable till we add real remove station */
386 /**
387 * iwl4965_remove_station - Remove driver's knowledge of station.
388 *
389 * NOTE: This does not remove station from device's station table.
390 */
391 static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
392 {
393 int index = IWL_INVALID_STATION;
394 int i;
395 unsigned long flags;
396
397 spin_lock_irqsave(&priv->sta_lock, flags);
398
399 if (is_ap)
400 index = IWL_AP_ID;
401 else if (is_broadcast_ether_addr(addr))
402 index = priv->hw_params.bcast_sta_id;
403 else
404 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
405 if (priv->stations[i].used &&
406 !compare_ether_addr(priv->stations[i].sta.sta.addr,
407 addr)) {
408 index = i;
409 break;
410 }
411
412 if (unlikely(index == IWL_INVALID_STATION))
413 goto out;
414
415 if (priv->stations[index].used) {
416 priv->stations[index].used = 0;
417 priv->num_stations--;
418 }
419
420 BUG_ON(priv->num_stations < 0);
421
422 out:
423 spin_unlock_irqrestore(&priv->sta_lock, flags);
424 return 0;
425 }
426 #endif
427
428 /**
429 * iwl4965_add_station_flags - Add station to tables in driver and device
430 */
431 u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
432 int is_ap, u8 flags, void *ht_data)
433 {
434 int i;
435 int index = IWL_INVALID_STATION;
436 struct iwl_station_entry *station;
437 unsigned long flags_spin;
438 DECLARE_MAC_BUF(mac);
439
440 spin_lock_irqsave(&priv->sta_lock, flags_spin);
441 if (is_ap)
442 index = IWL_AP_ID;
443 else if (is_broadcast_ether_addr(addr))
444 index = priv->hw_params.bcast_sta_id;
445 else
446 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
447 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
448 addr)) {
449 index = i;
450 break;
451 }
452
453 if (!priv->stations[i].used &&
454 index == IWL_INVALID_STATION)
455 index = i;
456 }
457
458
459 /* These two conditions have the same outcome, but keep them separate
460 since they have different meanings */
461 if (unlikely(index == IWL_INVALID_STATION)) {
462 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
463 return index;
464 }
465
466 if (priv->stations[index].used &&
467 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
468 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
469 return index;
470 }
471
472
473 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
474 station = &priv->stations[index];
475 station->used = 1;
476 priv->num_stations++;
477
478 /* Set up the REPLY_ADD_STA command to send to device */
479 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
480 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
481 station->sta.mode = 0;
482 station->sta.sta.sta_id = index;
483 station->sta.station_flags = 0;
484
485 #ifdef CONFIG_IWL4965_HT
486 /* BCAST station and IBSS stations do not work in HT mode */
487 if (index != priv->hw_params.bcast_sta_id &&
488 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
489 iwl4965_set_ht_add_station(priv, index,
490 (struct ieee80211_ht_info *) ht_data);
491 #endif /*CONFIG_IWL4965_HT*/
492
493 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
494
495 /* Add station to device's station table */
496 iwl_send_add_sta(priv, &station->sta, flags);
497 return index;
498
499 }
500
501
502
503 /*************** HOST COMMAND QUEUE FUNCTIONS *****/
504
505 /**
506 * iwl4965_enqueue_hcmd - enqueue a uCode command
507 * @priv: device private data point
508 * @cmd: a point to the ucode command structure
509 *
510 * The function returns < 0 values to indicate the operation is
511 * failed. On success, it turns the index (> 0) of command in the
512 * command queue.
513 */
514 int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
515 {
516 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
517 struct iwl4965_queue *q = &txq->q;
518 struct iwl4965_tfd_frame *tfd;
519 u32 *control_flags;
520 struct iwl_cmd *out_cmd;
521 u32 idx;
522 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
523 dma_addr_t phys_addr;
524 int ret;
525 unsigned long flags;
526
527 /* If any of the command structures end up being larger than
528 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
529 * we will need to increase the size of the TFD entries */
530 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
531 !(cmd->meta.flags & CMD_SIZE_HUGE));
532
533 if (iwl_is_rfkill(priv)) {
534 IWL_DEBUG_INFO("Not sending command - RF KILL");
535 return -EIO;
536 }
537
538 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
539 IWL_ERROR("No space for Tx\n");
540 return -ENOSPC;
541 }
542
543 spin_lock_irqsave(&priv->hcmd_lock, flags);
544
545 tfd = &txq->bd[q->write_ptr];
546 memset(tfd, 0, sizeof(*tfd));
547
548 control_flags = (u32 *) tfd;
549
550 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
551 out_cmd = &txq->cmd[idx];
552
553 out_cmd->hdr.cmd = cmd->id;
554 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
555 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
556
557 /* At this point, the out_cmd now has all of the incoming cmd
558 * information */
559
560 out_cmd->hdr.flags = 0;
561 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
562 INDEX_TO_SEQ(q->write_ptr));
563 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
564 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
565
566 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
567 offsetof(struct iwl_cmd, hdr);
568 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
569
570 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
571 "%d bytes at %d[%d]:%d\n",
572 get_cmd_string(out_cmd->hdr.cmd),
573 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
574 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
575
576 txq->need_update = 1;
577
578 /* Set up entry in queue's byte count circular buffer */
579 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
580
581 /* Increment and update queue's write index */
582 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
583 ret = iwl4965_tx_queue_update_write_ptr(priv, txq);
584
585 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
586 return ret ? ret : idx;
587 }
588
589 static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
590 {
591 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
592
593 if (hw_decrypt)
594 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
595 else
596 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
597
598 }
599
600 /**
601 * iwl4965_rxon_add_station - add station into station table.
602 *
603 * there is only one AP station with id= IWL_AP_ID
604 * NOTE: mutex must be held before calling this fnction
605 */
606 static int iwl4965_rxon_add_station(struct iwl_priv *priv,
607 const u8 *addr, int is_ap)
608 {
609 u8 sta_id;
610
611 /* Add station to device's station table */
612 #ifdef CONFIG_IWL4965_HT
613 struct ieee80211_conf *conf = &priv->hw->conf;
614 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
615
616 if ((is_ap) &&
617 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
618 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
619 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
620 0, cur_ht_config);
621 else
622 #endif /* CONFIG_IWL4965_HT */
623 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
624 0, NULL);
625
626 /* Set up default rate scaling table in device's station table */
627 iwl4965_add_station(priv, addr, is_ap);
628
629 return sta_id;
630 }
631
632 /**
633 * iwl4965_check_rxon_cmd - validate RXON structure is valid
634 *
635 * NOTE: This is really only useful during development and can eventually
636 * be #ifdef'd out once the driver is stable and folks aren't actively
637 * making changes
638 */
639 static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
640 {
641 int error = 0;
642 int counter = 1;
643
644 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
645 error |= le32_to_cpu(rxon->flags &
646 (RXON_FLG_TGJ_NARROW_BAND_MSK |
647 RXON_FLG_RADAR_DETECT_MSK));
648 if (error)
649 IWL_WARNING("check 24G fields %d | %d\n",
650 counter++, error);
651 } else {
652 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
653 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
654 if (error)
655 IWL_WARNING("check 52 fields %d | %d\n",
656 counter++, error);
657 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
658 if (error)
659 IWL_WARNING("check 52 CCK %d | %d\n",
660 counter++, error);
661 }
662 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
663 if (error)
664 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
665
666 /* make sure basic rates 6Mbps and 1Mbps are supported */
667 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
668 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
669 if (error)
670 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
671
672 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
673 if (error)
674 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
675
676 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
677 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
678 if (error)
679 IWL_WARNING("check CCK and short slot %d | %d\n",
680 counter++, error);
681
682 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
683 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
684 if (error)
685 IWL_WARNING("check CCK & auto detect %d | %d\n",
686 counter++, error);
687
688 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
689 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
690 if (error)
691 IWL_WARNING("check TGG and auto detect %d | %d\n",
692 counter++, error);
693
694 if (error)
695 IWL_WARNING("Tuning to channel %d\n",
696 le16_to_cpu(rxon->channel));
697
698 if (error) {
699 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
700 return -1;
701 }
702 return 0;
703 }
704
705 /**
706 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
707 * @priv: staging_rxon is compared to active_rxon
708 *
709 * If the RXON structure is changing enough to require a new tune,
710 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
711 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
712 */
713 static int iwl4965_full_rxon_required(struct iwl_priv *priv)
714 {
715
716 /* These items are only settable from the full RXON command */
717 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
718 compare_ether_addr(priv->staging_rxon.bssid_addr,
719 priv->active_rxon.bssid_addr) ||
720 compare_ether_addr(priv->staging_rxon.node_addr,
721 priv->active_rxon.node_addr) ||
722 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
723 priv->active_rxon.wlap_bssid_addr) ||
724 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
725 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
726 (priv->staging_rxon.air_propagation !=
727 priv->active_rxon.air_propagation) ||
728 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
729 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
730 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
731 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
732 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
733 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
734 return 1;
735
736 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
737 * be updated with the RXON_ASSOC command -- however only some
738 * flag transitions are allowed using RXON_ASSOC */
739
740 /* Check if we are not switching bands */
741 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
742 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
743 return 1;
744
745 /* Check if we are switching association toggle */
746 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
747 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
748 return 1;
749
750 return 0;
751 }
752
753 /**
754 * iwl4965_commit_rxon - commit staging_rxon to hardware
755 *
756 * The RXON command in staging_rxon is committed to the hardware and
757 * the active_rxon structure is updated with the new data. This
758 * function correctly transitions out of the RXON_ASSOC_MSK state if
759 * a HW tune is required based on the RXON structure changes.
760 */
761 static int iwl4965_commit_rxon(struct iwl_priv *priv)
762 {
763 /* cast away the const for active_rxon in this function */
764 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
765 DECLARE_MAC_BUF(mac);
766 int rc = 0;
767
768 if (!iwl_is_alive(priv))
769 return -1;
770
771 /* always get timestamp with Rx frame */
772 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
773
774 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
775 if (rc) {
776 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
777 return -EINVAL;
778 }
779
780 /* If we don't need to send a full RXON, we can use
781 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
782 * and other flags for the current radio configuration. */
783 if (!iwl4965_full_rxon_required(priv)) {
784 rc = iwl_send_rxon_assoc(priv);
785 if (rc) {
786 IWL_ERROR("Error setting RXON_ASSOC "
787 "configuration (%d).\n", rc);
788 return rc;
789 }
790
791 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
792
793 return 0;
794 }
795
796 /* station table will be cleared */
797 priv->assoc_station_added = 0;
798
799 /* If we are currently associated and the new config requires
800 * an RXON_ASSOC and the new config wants the associated mask enabled,
801 * we must clear the associated from the active configuration
802 * before we apply the new config */
803 if (iwl_is_associated(priv) &&
804 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
805 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
806 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
807
808 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
809 sizeof(struct iwl4965_rxon_cmd),
810 &priv->active_rxon);
811
812 /* If the mask clearing failed then we set
813 * active_rxon back to what it was previously */
814 if (rc) {
815 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
816 IWL_ERROR("Error clearing ASSOC_MSK on current "
817 "configuration (%d).\n", rc);
818 return rc;
819 }
820 }
821
822 IWL_DEBUG_INFO("Sending RXON\n"
823 "* with%s RXON_FILTER_ASSOC_MSK\n"
824 "* channel = %d\n"
825 "* bssid = %s\n",
826 ((priv->staging_rxon.filter_flags &
827 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
828 le16_to_cpu(priv->staging_rxon.channel),
829 print_mac(mac, priv->staging_rxon.bssid_addr));
830
831 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
832 /* Apply the new configuration */
833 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
834 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
835 if (rc) {
836 IWL_ERROR("Error setting new configuration (%d).\n", rc);
837 return rc;
838 }
839
840 iwlcore_clear_stations_table(priv);
841
842 if (!priv->error_recovering)
843 priv->start_calib = 0;
844
845 iwl_init_sensitivity(priv);
846
847 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
848
849 /* If we issue a new RXON command which required a tune then we must
850 * send a new TXPOWER command or we won't be able to Tx any frames */
851 rc = iwl4965_hw_reg_send_txpower(priv);
852 if (rc) {
853 IWL_ERROR("Error setting Tx power (%d).\n", rc);
854 return rc;
855 }
856
857 /* Add the broadcast address so we can send broadcast frames */
858 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
859 IWL_INVALID_STATION) {
860 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
861 return -EIO;
862 }
863
864 /* If we have set the ASSOC_MSK and we are in BSS mode then
865 * add the IWL_AP_ID to the station rate table */
866 if (iwl_is_associated(priv) &&
867 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
868 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
869 == IWL_INVALID_STATION) {
870 IWL_ERROR("Error adding AP address for transmit.\n");
871 return -EIO;
872 }
873 priv->assoc_station_added = 1;
874 if (priv->default_wep_key &&
875 iwl_send_static_wepkey_cmd(priv, 0))
876 IWL_ERROR("Could not send WEP static key.\n");
877 }
878
879 return 0;
880 }
881
882 void iwl4965_update_chain_flags(struct iwl_priv *priv)
883 {
884
885 iwl_set_rxon_chain(priv);
886 iwl4965_commit_rxon(priv);
887 }
888
889 static int iwl4965_send_bt_config(struct iwl_priv *priv)
890 {
891 struct iwl4965_bt_cmd bt_cmd = {
892 .flags = 3,
893 .lead_time = 0xAA,
894 .max_kill = 1,
895 .kill_ack_mask = 0,
896 .kill_cts_mask = 0,
897 };
898
899 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
900 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
901 }
902
903 static int iwl4965_send_scan_abort(struct iwl_priv *priv)
904 {
905 int ret = 0;
906 struct iwl_rx_packet *res;
907 struct iwl_host_cmd cmd = {
908 .id = REPLY_SCAN_ABORT_CMD,
909 .meta.flags = CMD_WANT_SKB,
910 };
911
912 /* If there isn't a scan actively going on in the hardware
913 * then we are in between scan bands and not actually
914 * actively scanning, so don't send the abort command */
915 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
916 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
917 return 0;
918 }
919
920 ret = iwl_send_cmd_sync(priv, &cmd);
921 if (ret) {
922 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
923 return ret;
924 }
925
926 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
927 if (res->u.status != CAN_ABORT_STATUS) {
928 /* The scan abort will return 1 for success or
929 * 2 for "failure". A failure condition can be
930 * due to simply not being in an active scan which
931 * can occur if we send the scan abort before we
932 * the microcode has notified us that a scan is
933 * completed. */
934 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
935 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
936 clear_bit(STATUS_SCAN_HW, &priv->status);
937 }
938
939 dev_kfree_skb_any(cmd.meta.u.skb);
940
941 return ret;
942 }
943
944 /*
945 * CARD_STATE_CMD
946 *
947 * Use: Sets the device's internal card state to enable, disable, or halt
948 *
949 * When in the 'enable' state the card operates as normal.
950 * When in the 'disable' state, the card enters into a low power mode.
951 * When in the 'halt' state, the card is shut down and must be fully
952 * restarted to come back on.
953 */
954 static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
955 {
956 struct iwl_host_cmd cmd = {
957 .id = REPLY_CARD_STATE_CMD,
958 .len = sizeof(u32),
959 .data = &flags,
960 .meta.flags = meta_flag,
961 };
962
963 return iwl_send_cmd(priv, &cmd);
964 }
965
966 static void iwl4965_clear_free_frames(struct iwl_priv *priv)
967 {
968 struct list_head *element;
969
970 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
971 priv->frames_count);
972
973 while (!list_empty(&priv->free_frames)) {
974 element = priv->free_frames.next;
975 list_del(element);
976 kfree(list_entry(element, struct iwl4965_frame, list));
977 priv->frames_count--;
978 }
979
980 if (priv->frames_count) {
981 IWL_WARNING("%d frames still in use. Did we lose one?\n",
982 priv->frames_count);
983 priv->frames_count = 0;
984 }
985 }
986
987 static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
988 {
989 struct iwl4965_frame *frame;
990 struct list_head *element;
991 if (list_empty(&priv->free_frames)) {
992 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
993 if (!frame) {
994 IWL_ERROR("Could not allocate frame!\n");
995 return NULL;
996 }
997
998 priv->frames_count++;
999 return frame;
1000 }
1001
1002 element = priv->free_frames.next;
1003 list_del(element);
1004 return list_entry(element, struct iwl4965_frame, list);
1005 }
1006
1007 static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
1008 {
1009 memset(frame, 0, sizeof(*frame));
1010 list_add(&frame->list, &priv->free_frames);
1011 }
1012
1013 unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
1014 struct ieee80211_hdr *hdr,
1015 const u8 *dest, int left)
1016 {
1017
1018 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
1019 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1020 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1021 return 0;
1022
1023 if (priv->ibss_beacon->len > left)
1024 return 0;
1025
1026 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1027
1028 return priv->ibss_beacon->len;
1029 }
1030
1031 static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
1032 {
1033 int i;
1034 int rate_mask;
1035
1036 /* Set rate mask*/
1037 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
1038 rate_mask = priv->active_rate_basic & 0xF;
1039 else
1040 rate_mask = priv->active_rate_basic & 0xFF0;
1041
1042 /* Find lowest valid rate */
1043 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1044 i = iwl4965_rates[i].next_ieee) {
1045 if (rate_mask & (1 << i))
1046 return iwl4965_rates[i].plcp;
1047 }
1048
1049 /* No valid rate was found. Assign the lowest one */
1050 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
1051 return IWL_RATE_1M_PLCP;
1052 else
1053 return IWL_RATE_6M_PLCP;
1054 }
1055
1056 static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
1057 {
1058 struct iwl4965_frame *frame;
1059 unsigned int frame_size;
1060 int rc;
1061 u8 rate;
1062
1063 frame = iwl4965_get_free_frame(priv);
1064
1065 if (!frame) {
1066 IWL_ERROR("Could not obtain free frame buffer for beacon "
1067 "command.\n");
1068 return -ENOMEM;
1069 }
1070
1071 rate = iwl4965_rate_get_lowest_plcp(priv);
1072
1073 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
1074
1075 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1076 &frame->u.cmd[0]);
1077
1078 iwl4965_free_frame(priv, frame);
1079
1080 return rc;
1081 }
1082
1083 /******************************************************************************
1084 *
1085 * Misc. internal state and helper functions
1086 *
1087 ******************************************************************************/
1088
1089 /**
1090 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
1091 *
1092 * return : set the bit for each supported rate insert in ie
1093 */
1094 static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1095 u16 basic_rate, int *left)
1096 {
1097 u16 ret_rates = 0, bit;
1098 int i;
1099 u8 *cnt = ie;
1100 u8 *rates = ie + 1;
1101
1102 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1103 if (bit & supported_rate) {
1104 ret_rates |= bit;
1105 rates[*cnt] = iwl4965_rates[i].ieee |
1106 ((bit & basic_rate) ? 0x80 : 0x00);
1107 (*cnt)++;
1108 (*left)--;
1109 if ((*left <= 0) ||
1110 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1111 break;
1112 }
1113 }
1114
1115 return ret_rates;
1116 }
1117
1118 #ifdef CONFIG_IWL4965_HT
1119 static void iwl4965_ht_conf(struct iwl_priv *priv,
1120 struct ieee80211_bss_conf *bss_conf)
1121 {
1122 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
1123 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
1124 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
1125
1126 IWL_DEBUG_MAC80211("enter: \n");
1127
1128 iwl_conf->is_ht = bss_conf->assoc_ht;
1129
1130 if (!iwl_conf->is_ht)
1131 return;
1132
1133 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
1134
1135 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
1136 iwl_conf->sgf |= 0x1;
1137 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
1138 iwl_conf->sgf |= 0x2;
1139
1140 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
1141 iwl_conf->max_amsdu_size =
1142 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
1143
1144 iwl_conf->supported_chan_width =
1145 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
1146 iwl_conf->extension_chan_offset =
1147 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
1148 /* If no above or below channel supplied disable FAT channel */
1149 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
1150 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
1151 iwl_conf->supported_chan_width = 0;
1152
1153 iwl_conf->tx_mimo_ps_mode =
1154 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
1155 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
1156
1157 iwl_conf->control_channel = ht_bss_conf->primary_channel;
1158 iwl_conf->tx_chan_width =
1159 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
1160 iwl_conf->ht_protection =
1161 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
1162 iwl_conf->non_GF_STA_present =
1163 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
1164
1165 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
1166 IWL_DEBUG_MAC80211("leave\n");
1167 }
1168
1169 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1170 u8 *pos, int *left)
1171 {
1172 struct ieee80211_ht_cap *ht_cap;
1173
1174 if (!sband || !sband->ht_info.ht_supported)
1175 return;
1176
1177 if (*left < sizeof(struct ieee80211_ht_cap))
1178 return;
1179
1180 *pos++ = sizeof(struct ieee80211_ht_cap);
1181 ht_cap = (struct ieee80211_ht_cap *) pos;
1182
1183 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1184 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1185 ht_cap->ampdu_params_info =
1186 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
1187 ((sband->ht_info.ampdu_density << 2) &
1188 IEEE80211_HT_CAP_AMPDU_DENSITY);
1189 *left -= sizeof(struct ieee80211_ht_cap);
1190 }
1191 #else
1192 static inline void iwl4965_ht_conf(struct iwl_priv *priv,
1193 struct ieee80211_bss_conf *bss_conf)
1194 {
1195 }
1196 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
1197 u8 *pos, int *left)
1198 {
1199 }
1200 #endif
1201
1202
1203 /**
1204 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
1205 */
1206 static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
1207 enum ieee80211_band band,
1208 struct ieee80211_mgmt *frame,
1209 int left, int is_direct)
1210 {
1211 int len = 0;
1212 u8 *pos = NULL;
1213 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
1214 const struct ieee80211_supported_band *sband =
1215 iwl_get_hw_mode(priv, band);
1216
1217 /* Make sure there is enough space for the probe request,
1218 * two mandatory IEs and the data */
1219 left -= 24;
1220 if (left < 0)
1221 return 0;
1222 len += 24;
1223
1224 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1225 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
1226 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1227 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
1228 frame->seq_ctrl = 0;
1229
1230 /* fill in our indirect SSID IE */
1231 /* ...next IE... */
1232
1233 left -= 2;
1234 if (left < 0)
1235 return 0;
1236 len += 2;
1237 pos = &(frame->u.probe_req.variable[0]);
1238 *pos++ = WLAN_EID_SSID;
1239 *pos++ = 0;
1240
1241 /* fill in our direct SSID IE... */
1242 if (is_direct) {
1243 /* ...next IE... */
1244 left -= 2 + priv->essid_len;
1245 if (left < 0)
1246 return 0;
1247 /* ... fill it in... */
1248 *pos++ = WLAN_EID_SSID;
1249 *pos++ = priv->essid_len;
1250 memcpy(pos, priv->essid, priv->essid_len);
1251 pos += priv->essid_len;
1252 len += 2 + priv->essid_len;
1253 }
1254
1255 /* fill in supported rate */
1256 /* ...next IE... */
1257 left -= 2;
1258 if (left < 0)
1259 return 0;
1260
1261 /* ... fill it in... */
1262 *pos++ = WLAN_EID_SUPP_RATES;
1263 *pos = 0;
1264
1265 /* exclude 60M rate */
1266 active_rates = priv->rates_mask;
1267 active_rates &= ~IWL_RATE_60M_MASK;
1268
1269 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
1270
1271 cck_rates = IWL_CCK_RATES_MASK & active_rates;
1272 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
1273 active_rate_basic, &left);
1274 active_rates &= ~ret_rates;
1275
1276 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
1277 active_rate_basic, &left);
1278 active_rates &= ~ret_rates;
1279
1280 len += 2 + *pos;
1281 pos += (*pos) + 1;
1282 if (active_rates == 0)
1283 goto fill_end;
1284
1285 /* fill in supported extended rate */
1286 /* ...next IE... */
1287 left -= 2;
1288 if (left < 0)
1289 return 0;
1290 /* ... fill it in... */
1291 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1292 *pos = 0;
1293 iwl4965_supported_rate_to_ie(pos, active_rates,
1294 active_rate_basic, &left);
1295 if (*pos > 0)
1296 len += 2 + *pos;
1297
1298 fill_end:
1299 /* fill in HT IE */
1300 left -= 2;
1301 if (left < 0)
1302 return 0;
1303
1304 *pos++ = WLAN_EID_HT_CAPABILITY;
1305 *pos = 0;
1306
1307 iwl_ht_cap_to_ie(sband, pos, &left);
1308
1309 if (*pos > 0)
1310 len += 2 + *pos;
1311 return (u16)len;
1312 }
1313
1314 /*
1315 * QoS support
1316 */
1317 static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
1318 struct iwl4965_qosparam_cmd *qos)
1319 {
1320
1321 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1322 sizeof(struct iwl4965_qosparam_cmd), qos);
1323 }
1324
1325 static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
1326 {
1327 unsigned long flags;
1328
1329 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1330 return;
1331
1332 if (!priv->qos_data.qos_enable)
1333 return;
1334
1335 spin_lock_irqsave(&priv->lock, flags);
1336 priv->qos_data.def_qos_parm.qos_flags = 0;
1337
1338 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1339 !priv->qos_data.qos_cap.q_AP.txop_request)
1340 priv->qos_data.def_qos_parm.qos_flags |=
1341 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1342 if (priv->qos_data.qos_active)
1343 priv->qos_data.def_qos_parm.qos_flags |=
1344 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1345
1346 #ifdef CONFIG_IWL4965_HT
1347 if (priv->current_ht_config.is_ht)
1348 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
1349 #endif /* CONFIG_IWL4965_HT */
1350
1351 spin_unlock_irqrestore(&priv->lock, flags);
1352
1353 if (force || iwl_is_associated(priv)) {
1354 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1355 priv->qos_data.qos_active,
1356 priv->qos_data.def_qos_parm.qos_flags);
1357
1358 iwl4965_send_qos_params_command(priv,
1359 &(priv->qos_data.def_qos_parm));
1360 }
1361 }
1362
1363 int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
1364 {
1365 /* Filter incoming packets to determine if they are targeted toward
1366 * this network, discarding packets coming from ourselves */
1367 switch (priv->iw_mode) {
1368 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
1369 /* packets from our adapter are dropped (echo) */
1370 if (!compare_ether_addr(header->addr2, priv->mac_addr))
1371 return 0;
1372 /* {broad,multi}cast packets to our IBSS go through */
1373 if (is_multicast_ether_addr(header->addr1))
1374 return !compare_ether_addr(header->addr3, priv->bssid);
1375 /* packets to our adapter go through */
1376 return !compare_ether_addr(header->addr1, priv->mac_addr);
1377 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
1378 /* packets from our adapter are dropped (echo) */
1379 if (!compare_ether_addr(header->addr3, priv->mac_addr))
1380 return 0;
1381 /* {broad,multi}cast packets to our BSS go through */
1382 if (is_multicast_ether_addr(header->addr1))
1383 return !compare_ether_addr(header->addr2, priv->bssid);
1384 /* packets to our adapter go through */
1385 return !compare_ether_addr(header->addr1, priv->mac_addr);
1386 default:
1387 break;
1388 }
1389
1390 return 1;
1391 }
1392
1393 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1394
1395 static const char *iwl4965_get_tx_fail_reason(u32 status)
1396 {
1397 switch (status & TX_STATUS_MSK) {
1398 case TX_STATUS_SUCCESS:
1399 return "SUCCESS";
1400 TX_STATUS_ENTRY(SHORT_LIMIT);
1401 TX_STATUS_ENTRY(LONG_LIMIT);
1402 TX_STATUS_ENTRY(FIFO_UNDERRUN);
1403 TX_STATUS_ENTRY(MGMNT_ABORT);
1404 TX_STATUS_ENTRY(NEXT_FRAG);
1405 TX_STATUS_ENTRY(LIFE_EXPIRE);
1406 TX_STATUS_ENTRY(DEST_PS);
1407 TX_STATUS_ENTRY(ABORTED);
1408 TX_STATUS_ENTRY(BT_RETRY);
1409 TX_STATUS_ENTRY(STA_INVALID);
1410 TX_STATUS_ENTRY(FRAG_DROPPED);
1411 TX_STATUS_ENTRY(TID_DISABLE);
1412 TX_STATUS_ENTRY(FRAME_FLUSHED);
1413 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
1414 TX_STATUS_ENTRY(TX_LOCKED);
1415 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
1416 }
1417
1418 return "UNKNOWN";
1419 }
1420
1421 /**
1422 * iwl4965_scan_cancel - Cancel any currently executing HW scan
1423 *
1424 * NOTE: priv->mutex is not required before calling this function
1425 */
1426 static int iwl4965_scan_cancel(struct iwl_priv *priv)
1427 {
1428 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1429 clear_bit(STATUS_SCANNING, &priv->status);
1430 return 0;
1431 }
1432
1433 if (test_bit(STATUS_SCANNING, &priv->status)) {
1434 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1435 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1436 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1437 queue_work(priv->workqueue, &priv->abort_scan);
1438
1439 } else
1440 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1441
1442 return test_bit(STATUS_SCANNING, &priv->status);
1443 }
1444
1445 return 0;
1446 }
1447
1448 /**
1449 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
1450 * @ms: amount of time to wait (in milliseconds) for scan to abort
1451 *
1452 * NOTE: priv->mutex must be held before calling this function
1453 */
1454 static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
1455 {
1456 unsigned long now = jiffies;
1457 int ret;
1458
1459 ret = iwl4965_scan_cancel(priv);
1460 if (ret && ms) {
1461 mutex_unlock(&priv->mutex);
1462 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1463 test_bit(STATUS_SCANNING, &priv->status))
1464 msleep(1);
1465 mutex_lock(&priv->mutex);
1466
1467 return test_bit(STATUS_SCANNING, &priv->status);
1468 }
1469
1470 return ret;
1471 }
1472
1473 static void iwl4965_sequence_reset(struct iwl_priv *priv)
1474 {
1475 /* Reset ieee stats */
1476
1477 /* We don't reset the net_device_stats (ieee->stats) on
1478 * re-association */
1479
1480 priv->last_seq_num = -1;
1481 priv->last_frag_num = -1;
1482 priv->last_packet_time = 0;
1483
1484 iwl4965_scan_cancel(priv);
1485 }
1486
1487 #define MAX_UCODE_BEACON_INTERVAL 4096
1488 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1489
1490 static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
1491 {
1492 u16 new_val = 0;
1493 u16 beacon_factor = 0;
1494
1495 beacon_factor =
1496 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1497 / MAX_UCODE_BEACON_INTERVAL;
1498 new_val = beacon_val / beacon_factor;
1499
1500 return cpu_to_le16(new_val);
1501 }
1502
1503 static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
1504 {
1505 u64 interval_tm_unit;
1506 u64 tsf, result;
1507 unsigned long flags;
1508 struct ieee80211_conf *conf = NULL;
1509 u16 beacon_int = 0;
1510
1511 conf = ieee80211_get_hw_conf(priv->hw);
1512
1513 spin_lock_irqsave(&priv->lock, flags);
1514 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1515 priv->rxon_timing.timestamp.dw[0] =
1516 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
1517
1518 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1519
1520 tsf = priv->timestamp;
1521
1522 beacon_int = priv->beacon_int;
1523 spin_unlock_irqrestore(&priv->lock, flags);
1524
1525 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1526 if (beacon_int == 0) {
1527 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1528 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1529 } else {
1530 priv->rxon_timing.beacon_interval =
1531 cpu_to_le16(beacon_int);
1532 priv->rxon_timing.beacon_interval =
1533 iwl4965_adjust_beacon_interval(
1534 le16_to_cpu(priv->rxon_timing.beacon_interval));
1535 }
1536
1537 priv->rxon_timing.atim_window = 0;
1538 } else {
1539 priv->rxon_timing.beacon_interval =
1540 iwl4965_adjust_beacon_interval(conf->beacon_int);
1541 /* TODO: we need to get atim_window from upper stack
1542 * for now we set to 0 */
1543 priv->rxon_timing.atim_window = 0;
1544 }
1545
1546 interval_tm_unit =
1547 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1548 result = do_div(tsf, interval_tm_unit);
1549 priv->rxon_timing.beacon_init_val =
1550 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1551
1552 IWL_DEBUG_ASSOC
1553 ("beacon interval %d beacon timer %d beacon tim %d\n",
1554 le16_to_cpu(priv->rxon_timing.beacon_interval),
1555 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1556 le16_to_cpu(priv->rxon_timing.atim_window));
1557 }
1558
1559 static int iwl4965_scan_initiate(struct iwl_priv *priv)
1560 {
1561 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1562 IWL_ERROR("APs don't scan.\n");
1563 return 0;
1564 }
1565
1566 if (!iwl_is_ready_rf(priv)) {
1567 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1568 return -EIO;
1569 }
1570
1571 if (test_bit(STATUS_SCANNING, &priv->status)) {
1572 IWL_DEBUG_SCAN("Scan already in progress.\n");
1573 return -EAGAIN;
1574 }
1575
1576 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1577 IWL_DEBUG_SCAN("Scan request while abort pending. "
1578 "Queuing.\n");
1579 return -EAGAIN;
1580 }
1581
1582 IWL_DEBUG_INFO("Starting scan...\n");
1583 priv->scan_bands = 2;
1584 set_bit(STATUS_SCANNING, &priv->status);
1585 priv->scan_start = jiffies;
1586 priv->scan_pass_start = priv->scan_start;
1587
1588 queue_work(priv->workqueue, &priv->request_scan);
1589
1590 return 0;
1591 }
1592
1593
1594 static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
1595 enum ieee80211_band band)
1596 {
1597 if (band == IEEE80211_BAND_5GHZ) {
1598 priv->staging_rxon.flags &=
1599 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1600 | RXON_FLG_CCK_MSK);
1601 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1602 } else {
1603 /* Copied from iwl4965_post_associate() */
1604 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1605 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1606 else
1607 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1608
1609 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1610 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1611
1612 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1613 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1614 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1615 }
1616 }
1617
1618 /*
1619 * initialize rxon structure with default values from eeprom
1620 */
1621 static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
1622 {
1623 const struct iwl_channel_info *ch_info;
1624
1625 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1626
1627 switch (priv->iw_mode) {
1628 case IEEE80211_IF_TYPE_AP:
1629 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1630 break;
1631
1632 case IEEE80211_IF_TYPE_STA:
1633 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1634 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1635 break;
1636
1637 case IEEE80211_IF_TYPE_IBSS:
1638 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1639 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1640 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1641 RXON_FILTER_ACCEPT_GRP_MSK;
1642 break;
1643
1644 case IEEE80211_IF_TYPE_MNTR:
1645 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1646 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1647 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1648 break;
1649 default:
1650 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1651 break;
1652 }
1653
1654 #if 0
1655 /* TODO: Figure out when short_preamble would be set and cache from
1656 * that */
1657 if (!hw_to_local(priv->hw)->short_preamble)
1658 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1659 else
1660 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1661 #endif
1662
1663 ch_info = iwl_get_channel_info(priv, priv->band,
1664 le16_to_cpu(priv->staging_rxon.channel));
1665
1666 if (!ch_info)
1667 ch_info = &priv->channel_info[0];
1668
1669 /*
1670 * in some case A channels are all non IBSS
1671 * in this case force B/G channel
1672 */
1673 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1674 !(is_channel_ibss(ch_info)))
1675 ch_info = &priv->channel_info[0];
1676
1677 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
1678 priv->band = ch_info->band;
1679
1680 iwl4965_set_flags_for_phymode(priv, priv->band);
1681
1682 priv->staging_rxon.ofdm_basic_rates =
1683 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1684 priv->staging_rxon.cck_basic_rates =
1685 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1686
1687 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1688 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1689 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1690 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1691 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1692 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1693 iwl_set_rxon_chain(priv);
1694 }
1695
1696 static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
1697 {
1698 if (mode == IEEE80211_IF_TYPE_IBSS) {
1699 const struct iwl_channel_info *ch_info;
1700
1701 ch_info = iwl_get_channel_info(priv,
1702 priv->band,
1703 le16_to_cpu(priv->staging_rxon.channel));
1704
1705 if (!ch_info || !is_channel_ibss(ch_info)) {
1706 IWL_ERROR("channel %d not IBSS channel\n",
1707 le16_to_cpu(priv->staging_rxon.channel));
1708 return -EINVAL;
1709 }
1710 }
1711
1712 priv->iw_mode = mode;
1713
1714 iwl4965_connection_init_rx_config(priv);
1715 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1716
1717 iwlcore_clear_stations_table(priv);
1718
1719 /* dont commit rxon if rf-kill is on*/
1720 if (!iwl_is_ready_rf(priv))
1721 return -EAGAIN;
1722
1723 cancel_delayed_work(&priv->scan_check);
1724 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1725 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1726 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1727 return -EAGAIN;
1728 }
1729
1730 iwl4965_commit_rxon(priv);
1731
1732 return 0;
1733 }
1734
1735 static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
1736 struct ieee80211_tx_control *ctl,
1737 struct iwl_cmd *cmd,
1738 struct sk_buff *skb_frag,
1739 int sta_id)
1740 {
1741 struct iwl_hw_key *keyinfo = &priv->stations[sta_id].keyinfo;
1742 struct iwl_wep_key *wepkey;
1743 int keyidx = 0;
1744
1745 BUG_ON(ctl->hw_key->hw_key_idx > 3);
1746
1747 switch (keyinfo->alg) {
1748 case ALG_CCMP:
1749 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
1750 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
1751 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
1752 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
1753 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
1754 break;
1755
1756 case ALG_TKIP:
1757 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
1758 ieee80211_get_tkip_key(keyinfo->conf, skb_frag,
1759 IEEE80211_TKIP_P2_KEY, cmd->cmd.tx.key);
1760 IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
1761 break;
1762
1763 case ALG_WEP:
1764 wepkey = &priv->wep_keys[ctl->hw_key->hw_key_idx];
1765 cmd->cmd.tx.sec_ctl = 0;
1766 if (priv->default_wep_key) {
1767 /* the WEP key was sent as static */
1768 keyidx = ctl->hw_key->hw_key_idx;
1769 memcpy(&cmd->cmd.tx.key[3], wepkey->key,
1770 wepkey->key_size);
1771 if (wepkey->key_size == WEP_KEY_LEN_128)
1772 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1773 } else {
1774 /* the WEP key was sent as dynamic */
1775 keyidx = keyinfo->keyidx;
1776 memcpy(&cmd->cmd.tx.key[3], keyinfo->key,
1777 keyinfo->keylen);
1778 if (keyinfo->keylen == WEP_KEY_LEN_128)
1779 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
1780 }
1781
1782 cmd->cmd.tx.sec_ctl |= (TX_CMD_SEC_WEP |
1783 (keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
1784
1785 IWL_DEBUG_TX("Configuring packet for WEP encryption "
1786 "with key %d\n", keyidx);
1787 break;
1788
1789 default:
1790 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
1791 break;
1792 }
1793 }
1794
1795 /*
1796 * handle build REPLY_TX command notification.
1797 */
1798 static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
1799 struct iwl_cmd *cmd,
1800 struct ieee80211_tx_control *ctrl,
1801 struct ieee80211_hdr *hdr,
1802 int is_unicast, u8 std_id)
1803 {
1804 __le16 *qc;
1805 u16 fc = le16_to_cpu(hdr->frame_control);
1806 __le32 tx_flags = cmd->cmd.tx.tx_flags;
1807
1808 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
1809 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
1810 tx_flags |= TX_CMD_FLG_ACK_MSK;
1811 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
1812 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1813 if (ieee80211_is_probe_response(fc) &&
1814 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
1815 tx_flags |= TX_CMD_FLG_TSF_MSK;
1816 } else {
1817 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
1818 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1819 }
1820
1821 if (ieee80211_is_back_request(fc))
1822 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
1823
1824
1825 cmd->cmd.tx.sta_id = std_id;
1826 if (ieee80211_get_morefrag(hdr))
1827 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
1828
1829 qc = ieee80211_get_qos_ctrl(hdr);
1830 if (qc) {
1831 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
1832 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
1833 } else
1834 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
1835
1836 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
1837 tx_flags |= TX_CMD_FLG_RTS_MSK;
1838 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
1839 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
1840 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
1841 tx_flags |= TX_CMD_FLG_CTS_MSK;
1842 }
1843
1844 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
1845 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
1846
1847 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
1848 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
1849 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
1850 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
1851 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
1852 else
1853 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
1854 } else {
1855 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
1856 }
1857
1858 cmd->cmd.tx.driver_txop = 0;
1859 cmd->cmd.tx.tx_flags = tx_flags;
1860 cmd->cmd.tx.next_frame_len = 0;
1861 }
1862 static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
1863 {
1864 /* 0 - mgmt, 1 - cnt, 2 - data */
1865 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
1866 priv->tx_stats[idx].cnt++;
1867 priv->tx_stats[idx].bytes += len;
1868 }
1869 /**
1870 * iwl4965_get_sta_id - Find station's index within station table
1871 *
1872 * If new IBSS station, create new entry in station table
1873 */
1874 static int iwl4965_get_sta_id(struct iwl_priv *priv,
1875 struct ieee80211_hdr *hdr)
1876 {
1877 int sta_id;
1878 u16 fc = le16_to_cpu(hdr->frame_control);
1879 DECLARE_MAC_BUF(mac);
1880
1881 /* If this frame is broadcast or management, use broadcast station id */
1882 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
1883 is_multicast_ether_addr(hdr->addr1))
1884 return priv->hw_params.bcast_sta_id;
1885
1886 switch (priv->iw_mode) {
1887
1888 /* If we are a client station in a BSS network, use the special
1889 * AP station entry (that's the only station we communicate with) */
1890 case IEEE80211_IF_TYPE_STA:
1891 return IWL_AP_ID;
1892
1893 /* If we are an AP, then find the station, or use BCAST */
1894 case IEEE80211_IF_TYPE_AP:
1895 sta_id = iwl_find_station(priv, hdr->addr1);
1896 if (sta_id != IWL_INVALID_STATION)
1897 return sta_id;
1898 return priv->hw_params.bcast_sta_id;
1899
1900 /* If this frame is going out to an IBSS network, find the station,
1901 * or create a new station table entry */
1902 case IEEE80211_IF_TYPE_IBSS:
1903 sta_id = iwl_find_station(priv, hdr->addr1);
1904 if (sta_id != IWL_INVALID_STATION)
1905 return sta_id;
1906
1907 /* Create new station table entry */
1908 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
1909 0, CMD_ASYNC, NULL);
1910
1911 if (sta_id != IWL_INVALID_STATION)
1912 return sta_id;
1913
1914 IWL_DEBUG_DROP("Station %s not in station map. "
1915 "Defaulting to broadcast...\n",
1916 print_mac(mac, hdr->addr1));
1917 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
1918 return priv->hw_params.bcast_sta_id;
1919
1920 default:
1921 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
1922 return priv->hw_params.bcast_sta_id;
1923 }
1924 }
1925
1926 /*
1927 * start REPLY_TX command process
1928 */
1929 static int iwl4965_tx_skb(struct iwl_priv *priv,
1930 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
1931 {
1932 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1933 struct iwl4965_tfd_frame *tfd;
1934 u32 *control_flags;
1935 int txq_id = ctl->queue;
1936 struct iwl4965_tx_queue *txq = NULL;
1937 struct iwl4965_queue *q = NULL;
1938 dma_addr_t phys_addr;
1939 dma_addr_t txcmd_phys;
1940 dma_addr_t scratch_phys;
1941 struct iwl_cmd *out_cmd = NULL;
1942 u16 len, idx, len_org;
1943 u8 id, hdr_len, unicast;
1944 u8 sta_id;
1945 u16 seq_number = 0;
1946 u16 fc;
1947 __le16 *qc;
1948 u8 wait_write_ptr = 0;
1949 unsigned long flags;
1950 int rc;
1951
1952 spin_lock_irqsave(&priv->lock, flags);
1953 if (iwl_is_rfkill(priv)) {
1954 IWL_DEBUG_DROP("Dropping - RF KILL\n");
1955 goto drop_unlock;
1956 }
1957
1958 if (!priv->vif) {
1959 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
1960 goto drop_unlock;
1961 }
1962
1963 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
1964 IWL_ERROR("ERROR: No TX rate available.\n");
1965 goto drop_unlock;
1966 }
1967
1968 unicast = !is_multicast_ether_addr(hdr->addr1);
1969 id = 0;
1970
1971 fc = le16_to_cpu(hdr->frame_control);
1972
1973 #ifdef CONFIG_IWLWIFI_DEBUG
1974 if (ieee80211_is_auth(fc))
1975 IWL_DEBUG_TX("Sending AUTH frame\n");
1976 else if (ieee80211_is_assoc_request(fc))
1977 IWL_DEBUG_TX("Sending ASSOC frame\n");
1978 else if (ieee80211_is_reassoc_request(fc))
1979 IWL_DEBUG_TX("Sending REASSOC frame\n");
1980 #endif
1981
1982 /* drop all data frame if we are not associated */
1983 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
1984 (!iwl_is_associated(priv) ||
1985 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
1986 !priv->assoc_station_added)) {
1987 IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
1988 goto drop_unlock;
1989 }
1990
1991 spin_unlock_irqrestore(&priv->lock, flags);
1992
1993 hdr_len = ieee80211_get_hdrlen(fc);
1994
1995 /* Find (or create) index into station table for destination station */
1996 sta_id = iwl4965_get_sta_id(priv, hdr);
1997 if (sta_id == IWL_INVALID_STATION) {
1998 DECLARE_MAC_BUF(mac);
1999
2000 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2001 print_mac(mac, hdr->addr1));
2002 goto drop;
2003 }
2004
2005 IWL_DEBUG_TX("station Id %d\n", sta_id);
2006
2007 qc = ieee80211_get_qos_ctrl(hdr);
2008 if (qc) {
2009 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2010 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2011 IEEE80211_SCTL_SEQ;
2012 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2013 (hdr->seq_ctrl &
2014 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2015 seq_number += 0x10;
2016 #ifdef CONFIG_IWL4965_HT
2017 /* aggregation is on for this <sta,tid> */
2018 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2019 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
2020 priv->stations[sta_id].tid[tid].tfds_in_queue++;
2021 #endif /* CONFIG_IWL4965_HT */
2022 }
2023
2024 /* Descriptor for chosen Tx queue */
2025 txq = &priv->txq[txq_id];
2026 q = &txq->q;
2027
2028 spin_lock_irqsave(&priv->lock, flags);
2029
2030 /* Set up first empty TFD within this queue's circular TFD buffer */
2031 tfd = &txq->bd[q->write_ptr];
2032 memset(tfd, 0, sizeof(*tfd));
2033 control_flags = (u32 *) tfd;
2034 idx = get_cmd_index(q, q->write_ptr, 0);
2035
2036 /* Set up driver data for this TFD */
2037 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
2038 txq->txb[q->write_ptr].skb[0] = skb;
2039 memcpy(&(txq->txb[q->write_ptr].status.control),
2040 ctl, sizeof(struct ieee80211_tx_control));
2041
2042 /* Set up first empty entry in queue's array of Tx/cmd buffers */
2043 out_cmd = &txq->cmd[idx];
2044 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2045 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2046
2047 /*
2048 * Set up the Tx-command (not MAC!) header.
2049 * Store the chosen Tx queue and TFD index within the sequence field;
2050 * after Tx, uCode's Tx response will return this value so driver can
2051 * locate the frame within the tx queue and do post-tx processing.
2052 */
2053 out_cmd->hdr.cmd = REPLY_TX;
2054 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
2055 INDEX_TO_SEQ(q->write_ptr)));
2056
2057 /* Copy MAC header from skb into command buffer */
2058 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2059
2060 /*
2061 * Use the first empty entry in this queue's command buffer array
2062 * to contain the Tx command and MAC header concatenated together
2063 * (payload data will be in another buffer).
2064 * Size of this varies, due to varying MAC header length.
2065 * If end is not dword aligned, we'll have 2 extra bytes at the end
2066 * of the MAC header (device reads on dword boundaries).
2067 * We'll tell device about this padding later.
2068 */
2069 len = priv->hw_params.tx_cmd_len +
2070 sizeof(struct iwl_cmd_header) + hdr_len;
2071
2072 len_org = len;
2073 len = (len + 3) & ~3;
2074
2075 if (len_org != len)
2076 len_org = 1;
2077 else
2078 len_org = 0;
2079
2080 /* Physical address of this Tx command's header (not MAC header!),
2081 * within command buffer array. */
2082 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2083 offsetof(struct iwl_cmd, hdr);
2084
2085 /* Add buffer containing Tx command and MAC(!) header to TFD's
2086 * first entry */
2087 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
2088
2089 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
2090 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, sta_id);
2091
2092 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2093 * if any (802.11 null frames have no payload). */
2094 len = skb->len - hdr_len;
2095 if (len) {
2096 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2097 len, PCI_DMA_TODEVICE);
2098 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
2099 }
2100
2101 /* Tell 4965 about any 2-byte padding after MAC header */
2102 if (len_org)
2103 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2104
2105 /* Total # bytes to be transmitted */
2106 len = (u16)skb->len;
2107 out_cmd->cmd.tx.len = cpu_to_le16(len);
2108
2109 /* TODO need this for burst mode later on */
2110 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
2111
2112 /* set is_hcca to 0; it probably will never be implemented */
2113 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
2114
2115 iwl_update_tx_stats(priv, fc, len);
2116
2117 scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
2118 offsetof(struct iwl4965_tx_cmd, scratch);
2119 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2120 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2121
2122 if (!ieee80211_get_morefrag(hdr)) {
2123 txq->need_update = 1;
2124 if (qc) {
2125 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2126 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2127 }
2128 } else {
2129 wait_write_ptr = 1;
2130 txq->need_update = 0;
2131 }
2132
2133 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
2134 sizeof(out_cmd->cmd.tx));
2135
2136 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
2137 ieee80211_get_hdrlen(fc));
2138
2139 /* Set up entry for this TFD in Tx byte-count array */
2140 priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
2141
2142 /* Tell device the write index *just past* this latest filled TFD */
2143 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
2144 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
2145 spin_unlock_irqrestore(&priv->lock, flags);
2146
2147 if (rc)
2148 return rc;
2149
2150 if ((iwl4965_queue_space(q) < q->high_mark)
2151 && priv->mac80211_registered) {
2152 if (wait_write_ptr) {
2153 spin_lock_irqsave(&priv->lock, flags);
2154 txq->need_update = 1;
2155 iwl4965_tx_queue_update_write_ptr(priv, txq);
2156 spin_unlock_irqrestore(&priv->lock, flags);
2157 }
2158
2159 ieee80211_stop_queue(priv->hw, ctl->queue);
2160 }
2161
2162 return 0;
2163
2164 drop_unlock:
2165 spin_unlock_irqrestore(&priv->lock, flags);
2166 drop:
2167 return -1;
2168 }
2169
2170 static void iwl4965_set_rate(struct iwl_priv *priv)
2171 {
2172 const struct ieee80211_supported_band *hw = NULL;
2173 struct ieee80211_rate *rate;
2174 int i;
2175
2176 hw = iwl_get_hw_mode(priv, priv->band);
2177 if (!hw) {
2178 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2179 return;
2180 }
2181
2182 priv->active_rate = 0;
2183 priv->active_rate_basic = 0;
2184
2185 for (i = 0; i < hw->n_bitrates; i++) {
2186 rate = &(hw->bitrates[i]);
2187 if (rate->hw_value < IWL_RATE_COUNT)
2188 priv->active_rate |= (1 << rate->hw_value);
2189 }
2190
2191 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2192 priv->active_rate, priv->active_rate_basic);
2193
2194 /*
2195 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2196 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2197 * OFDM
2198 */
2199 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2200 priv->staging_rxon.cck_basic_rates =
2201 ((priv->active_rate_basic &
2202 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2203 else
2204 priv->staging_rxon.cck_basic_rates =
2205 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2206
2207 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2208 priv->staging_rxon.ofdm_basic_rates =
2209 ((priv->active_rate_basic &
2210 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2211 IWL_FIRST_OFDM_RATE) & 0xFF;
2212 else
2213 priv->staging_rxon.ofdm_basic_rates =
2214 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2215 }
2216
2217 void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
2218 {
2219 unsigned long flags;
2220
2221 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2222 return;
2223
2224 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2225 disable_radio ? "OFF" : "ON");
2226
2227 if (disable_radio) {
2228 iwl4965_scan_cancel(priv);
2229 /* FIXME: This is a workaround for AP */
2230 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2231 spin_lock_irqsave(&priv->lock, flags);
2232 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2233 CSR_UCODE_SW_BIT_RFKILL);
2234 spin_unlock_irqrestore(&priv->lock, flags);
2235 /* call the host command only if no hw rf-kill set */
2236 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
2237 iwl_is_ready(priv))
2238 iwl4965_send_card_state(priv,
2239 CARD_STATE_CMD_DISABLE,
2240 0);
2241 set_bit(STATUS_RF_KILL_SW, &priv->status);
2242
2243 /* make sure mac80211 stop sending Tx frame */
2244 if (priv->mac80211_registered)
2245 ieee80211_stop_queues(priv->hw);
2246 }
2247 return;
2248 }
2249
2250 spin_lock_irqsave(&priv->lock, flags);
2251 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2252
2253 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2254 spin_unlock_irqrestore(&priv->lock, flags);
2255
2256 /* wake up ucode */
2257 msleep(10);
2258
2259 spin_lock_irqsave(&priv->lock, flags);
2260 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2261 if (!iwl_grab_nic_access(priv))
2262 iwl_release_nic_access(priv);
2263 spin_unlock_irqrestore(&priv->lock, flags);
2264
2265 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2266 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2267 "disabled by HW switch\n");
2268 return;
2269 }
2270
2271 queue_work(priv->workqueue, &priv->restart);
2272 return;
2273 }
2274
2275 #define IWL_PACKET_RETRY_TIME HZ
2276
2277 int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
2278 {
2279 u16 sc = le16_to_cpu(header->seq_ctrl);
2280 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2281 u16 frag = sc & IEEE80211_SCTL_FRAG;
2282 u16 *last_seq, *last_frag;
2283 unsigned long *last_time;
2284
2285 switch (priv->iw_mode) {
2286 case IEEE80211_IF_TYPE_IBSS:{
2287 struct list_head *p;
2288 struct iwl4965_ibss_seq *entry = NULL;
2289 u8 *mac = header->addr2;
2290 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2291
2292 __list_for_each(p, &priv->ibss_mac_hash[index]) {
2293 entry = list_entry(p, struct iwl4965_ibss_seq, list);
2294 if (!compare_ether_addr(entry->mac, mac))
2295 break;
2296 }
2297 if (p == &priv->ibss_mac_hash[index]) {
2298 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2299 if (!entry) {
2300 IWL_ERROR("Cannot malloc new mac entry\n");
2301 return 0;
2302 }
2303 memcpy(entry->mac, mac, ETH_ALEN);
2304 entry->seq_num = seq;
2305 entry->frag_num = frag;
2306 entry->packet_time = jiffies;
2307 list_add(&entry->list, &priv->ibss_mac_hash[index]);
2308 return 0;
2309 }
2310 last_seq = &entry->seq_num;
2311 last_frag = &entry->frag_num;
2312 last_time = &entry->packet_time;
2313 break;
2314 }
2315 case IEEE80211_IF_TYPE_STA:
2316 last_seq = &priv->last_seq_num;
2317 last_frag = &priv->last_frag_num;
2318 last_time = &priv->last_packet_time;
2319 break;
2320 default:
2321 return 0;
2322 }
2323 if ((*last_seq == seq) &&
2324 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
2325 if (*last_frag == frag)
2326 goto drop;
2327 if (*last_frag + 1 != frag)
2328 /* out-of-order fragment */
2329 goto drop;
2330 } else
2331 *last_seq = seq;
2332
2333 *last_frag = frag;
2334 *last_time = jiffies;
2335 return 0;
2336
2337 drop:
2338 return 1;
2339 }
2340
2341 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
2342
2343 #include "iwl-spectrum.h"
2344
2345 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
2346 #define BEACON_TIME_MASK_HIGH 0xFF000000
2347 #define TIME_UNIT 1024
2348
2349 /*
2350 * extended beacon time format
2351 * time in usec will be changed into a 32-bit value in 8:24 format
2352 * the high 1 byte is the beacon counts
2353 * the lower 3 bytes is the time in usec within one beacon interval
2354 */
2355
2356 static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
2357 {
2358 u32 quot;
2359 u32 rem;
2360 u32 interval = beacon_interval * 1024;
2361
2362 if (!interval || !usec)
2363 return 0;
2364
2365 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2366 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2367
2368 return (quot << 24) + rem;
2369 }
2370
2371 /* base is usually what we get from ucode with each received frame,
2372 * the same as HW timer counter counting down
2373 */
2374
2375 static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
2376 {
2377 u32 base_low = base & BEACON_TIME_MASK_LOW;
2378 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2379 u32 interval = beacon_interval * TIME_UNIT;
2380 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2381 (addon & BEACON_TIME_MASK_HIGH);
2382
2383 if (base_low > addon_low)
2384 res += base_low - addon_low;
2385 else if (base_low < addon_low) {
2386 res += interval + base_low - addon_low;
2387 res += (1 << 24);
2388 } else
2389 res += (1 << 24);
2390
2391 return cpu_to_le32(res);
2392 }
2393
2394 static int iwl4965_get_measurement(struct iwl_priv *priv,
2395 struct ieee80211_measurement_params *params,
2396 u8 type)
2397 {
2398 struct iwl4965_spectrum_cmd spectrum;
2399 struct iwl_rx_packet *res;
2400 struct iwl_host_cmd cmd = {
2401 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2402 .data = (void *)&spectrum,
2403 .meta.flags = CMD_WANT_SKB,
2404 };
2405 u32 add_time = le64_to_cpu(params->start_time);
2406 int rc;
2407 int spectrum_resp_status;
2408 int duration = le16_to_cpu(params->duration);
2409
2410 if (iwl_is_associated(priv))
2411 add_time =
2412 iwl4965_usecs_to_beacons(
2413 le64_to_cpu(params->start_time) - priv->last_tsf,
2414 le16_to_cpu(priv->rxon_timing.beacon_interval));
2415
2416 memset(&spectrum, 0, sizeof(spectrum));
2417
2418 spectrum.channel_count = cpu_to_le16(1);
2419 spectrum.flags =
2420 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2421 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2422 cmd.len = sizeof(spectrum);
2423 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2424
2425 if (iwl_is_associated(priv))
2426 spectrum.start_time =
2427 iwl4965_add_beacon_time(priv->last_beacon_time,
2428 add_time,
2429 le16_to_cpu(priv->rxon_timing.beacon_interval));
2430 else
2431 spectrum.start_time = 0;
2432
2433 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2434 spectrum.channels[0].channel = params->channel;
2435 spectrum.channels[0].type = type;
2436 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
2437 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2438 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2439
2440 rc = iwl_send_cmd_sync(priv, &cmd);
2441 if (rc)
2442 return rc;
2443
2444 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
2445 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
2446 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
2447 rc = -EIO;
2448 }
2449
2450 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2451 switch (spectrum_resp_status) {
2452 case 0: /* Command will be handled */
2453 if (res->u.spectrum.id != 0xff) {
2454 IWL_DEBUG_INFO
2455 ("Replaced existing measurement: %d\n",
2456 res->u.spectrum.id);
2457 priv->measurement_status &= ~MEASUREMENT_READY;
2458 }
2459 priv->measurement_status |= MEASUREMENT_ACTIVE;
2460 rc = 0;
2461 break;
2462
2463 case 1: /* Command will not be handled */
2464 rc = -EAGAIN;
2465 break;
2466 }
2467
2468 dev_kfree_skb_any(cmd.meta.u.skb);
2469
2470 return rc;
2471 }
2472 #endif
2473
2474 static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
2475 struct iwl4965_tx_info *tx_sta)
2476 {
2477
2478 tx_sta->status.ack_signal = 0;
2479 tx_sta->status.excessive_retries = 0;
2480
2481 if (in_interrupt())
2482 ieee80211_tx_status_irqsafe(priv->hw,
2483 tx_sta->skb[0], &(tx_sta->status));
2484 else
2485 ieee80211_tx_status(priv->hw,
2486 tx_sta->skb[0], &(tx_sta->status));
2487
2488 tx_sta->skb[0] = NULL;
2489 }
2490
2491 /**
2492 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
2493 *
2494 * When FW advances 'R' index, all entries between old and new 'R' index
2495 * need to be reclaimed. As result, some free space forms. If there is
2496 * enough free space (> low mark), wake the stack that feeds us.
2497 */
2498 int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
2499 {
2500 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2501 struct iwl4965_queue *q = &txq->q;
2502 int nfreed = 0;
2503
2504 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
2505 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
2506 "is out of range [0-%d] %d %d.\n", txq_id,
2507 index, q->n_bd, q->write_ptr, q->read_ptr);
2508 return 0;
2509 }
2510
2511 for (index = iwl_queue_inc_wrap(index, q->n_bd);
2512 q->read_ptr != index;
2513 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
2514 if (txq_id != IWL_CMD_QUEUE_NUM) {
2515 iwl4965_txstatus_to_ieee(priv,
2516 &(txq->txb[txq->q.read_ptr]));
2517 iwl4965_hw_txq_free_tfd(priv, txq);
2518 } else if (nfreed > 1) {
2519 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
2520 q->write_ptr, q->read_ptr);
2521 queue_work(priv->workqueue, &priv->restart);
2522 }
2523 nfreed++;
2524 }
2525
2526 return nfreed;
2527 }
2528
2529 static int iwl4965_is_tx_success(u32 status)
2530 {
2531 status &= TX_STATUS_MSK;
2532 return (status == TX_STATUS_SUCCESS)
2533 || (status == TX_STATUS_DIRECT_DONE);
2534 }
2535
2536 /******************************************************************************
2537 *
2538 * Generic RX handler implementations
2539 *
2540 ******************************************************************************/
2541 #ifdef CONFIG_IWL4965_HT
2542
2543 static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
2544 struct ieee80211_hdr *hdr)
2545 {
2546 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
2547 return IWL_AP_ID;
2548 else {
2549 u8 *da = ieee80211_get_DA(hdr);
2550 return iwl_find_station(priv, da);
2551 }
2552 }
2553
2554 static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
2555 struct iwl_priv *priv, int txq_id, int idx)
2556 {
2557 if (priv->txq[txq_id].txb[idx].skb[0])
2558 return (struct ieee80211_hdr *)priv->txq[txq_id].
2559 txb[idx].skb[0]->data;
2560 return NULL;
2561 }
2562
2563 static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
2564 {
2565 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
2566 tx_resp->frame_count);
2567 return le32_to_cpu(*scd_ssn) & MAX_SN;
2568
2569 }
2570
2571 /**
2572 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
2573 */
2574 static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
2575 struct iwl_ht_agg *agg,
2576 struct iwl4965_tx_resp_agg *tx_resp,
2577 u16 start_idx)
2578 {
2579 u16 status;
2580 struct agg_tx_status *frame_status = &tx_resp->status;
2581 struct ieee80211_tx_status *tx_status = NULL;
2582 struct ieee80211_hdr *hdr = NULL;
2583 int i, sh;
2584 int txq_id, idx;
2585 u16 seq;
2586
2587 if (agg->wait_for_ba)
2588 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
2589
2590 agg->frame_count = tx_resp->frame_count;
2591 agg->start_idx = start_idx;
2592 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
2593 agg->bitmap = 0;
2594
2595 /* # frames attempted by Tx command */
2596 if (agg->frame_count == 1) {
2597 /* Only one frame was attempted; no block-ack will arrive */
2598 status = le16_to_cpu(frame_status[0].status);
2599 seq = le16_to_cpu(frame_status[0].sequence);
2600 idx = SEQ_TO_INDEX(seq);
2601 txq_id = SEQ_TO_QUEUE(seq);
2602
2603 /* FIXME: code repetition */
2604 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
2605 agg->frame_count, agg->start_idx, idx);
2606
2607 tx_status = &(priv->txq[txq_id].txb[idx].status);
2608 tx_status->retry_count = tx_resp->failure_frame;
2609 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
2610 tx_status->flags = iwl4965_is_tx_success(status)?
2611 IEEE80211_TX_STATUS_ACK : 0;
2612 iwl4965_hwrate_to_tx_control(priv,
2613 le32_to_cpu(tx_resp->rate_n_flags),
2614 &tx_status->control);
2615 /* FIXME: code repetition end */
2616
2617 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
2618 status & 0xff, tx_resp->failure_frame);
2619 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
2620 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
2621
2622 agg->wait_for_ba = 0;
2623 } else {
2624 /* Two or more frames were attempted; expect block-ack */
2625 u64 bitmap = 0;
2626 int start = agg->start_idx;
2627
2628 /* Construct bit-map of pending frames within Tx window */
2629 for (i = 0; i < agg->frame_count; i++) {
2630 u16 sc;
2631 status = le16_to_cpu(frame_status[i].status);
2632 seq = le16_to_cpu(frame_status[i].sequence);
2633 idx = SEQ_TO_INDEX(seq);
2634 txq_id = SEQ_TO_QUEUE(seq);
2635
2636 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
2637 AGG_TX_STATE_ABORT_MSK))
2638 continue;
2639
2640 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
2641 agg->frame_count, txq_id, idx);
2642
2643 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
2644
2645 sc = le16_to_cpu(hdr->seq_ctrl);
2646 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
2647 IWL_ERROR("BUG_ON idx doesn't match seq control"
2648 " idx=%d, seq_idx=%d, seq=%d\n",
2649 idx, SEQ_TO_SN(sc),
2650 hdr->seq_ctrl);
2651 return -1;
2652 }
2653
2654 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
2655 i, idx, SEQ_TO_SN(sc));
2656
2657 sh = idx - start;
2658 if (sh > 64) {
2659 sh = (start - idx) + 0xff;
2660 bitmap = bitmap << sh;
2661 sh = 0;
2662 start = idx;
2663 } else if (sh < -64)
2664 sh = 0xff - (start - idx);
2665 else if (sh < 0) {
2666 sh = start - idx;
2667 start = idx;
2668 bitmap = bitmap << sh;
2669 sh = 0;
2670 }
2671 bitmap |= (1 << sh);
2672 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
2673 start, (u32)(bitmap & 0xFFFFFFFF));
2674 }
2675
2676 agg->bitmap = bitmap;
2677 agg->start_idx = start;
2678 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
2679 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
2680 agg->frame_count, agg->start_idx,
2681 (unsigned long long)agg->bitmap);
2682
2683 if (bitmap)
2684 agg->wait_for_ba = 1;
2685 }
2686 return 0;
2687 }
2688 #endif
2689
2690 /**
2691 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
2692 */
2693 static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
2694 struct iwl_rx_mem_buffer *rxb)
2695 {
2696 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2697 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2698 int txq_id = SEQ_TO_QUEUE(sequence);
2699 int index = SEQ_TO_INDEX(sequence);
2700 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
2701 struct ieee80211_tx_status *tx_status;
2702 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
2703 u32 status = le32_to_cpu(tx_resp->status);
2704 #ifdef CONFIG_IWL4965_HT
2705 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
2706 struct ieee80211_hdr *hdr;
2707 __le16 *qc;
2708 #endif
2709
2710 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
2711 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
2712 "is out of range [0-%d] %d %d\n", txq_id,
2713 index, txq->q.n_bd, txq->q.write_ptr,
2714 txq->q.read_ptr);
2715 return;
2716 }
2717
2718 #ifdef CONFIG_IWL4965_HT
2719 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
2720 qc = ieee80211_get_qos_ctrl(hdr);
2721
2722 if (qc)
2723 tid = le16_to_cpu(*qc) & 0xf;
2724
2725 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
2726 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
2727 IWL_ERROR("Station not known\n");
2728 return;
2729 }
2730
2731 if (txq->sched_retry) {
2732 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
2733 struct iwl_ht_agg *agg = NULL;
2734
2735 if (!qc)
2736 return;
2737
2738 agg = &priv->stations[sta_id].tid[tid].agg;
2739
2740 iwl4965_tx_status_reply_tx(priv, agg,
2741 (struct iwl4965_tx_resp_agg *)tx_resp, index);
2742
2743 if ((tx_resp->frame_count == 1) &&
2744 !iwl4965_is_tx_success(status)) {
2745 /* TODO: send BAR */
2746 }
2747
2748 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
2749 int freed, ampdu_q;
2750 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
2751 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
2752 "%d index %d\n", scd_ssn , index);
2753 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2754 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2755
2756 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2757 txq_id >= 0 && priv->mac80211_registered &&
2758 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
2759 /* calculate mac80211 ampdu sw queue to wake */
2760 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
2761 priv->hw->queues;
2762 if (agg->state == IWL_AGG_OFF)
2763 ieee80211_wake_queue(priv->hw, txq_id);
2764 else
2765 ieee80211_wake_queue(priv->hw, ampdu_q);
2766 }
2767 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
2768 }
2769 } else {
2770 #endif /* CONFIG_IWL4965_HT */
2771 tx_status = &(txq->txb[txq->q.read_ptr].status);
2772
2773 tx_status->retry_count = tx_resp->failure_frame;
2774 tx_status->flags =
2775 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
2776 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
2777 &tx_status->control);
2778
2779 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
2780 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
2781 status, le32_to_cpu(tx_resp->rate_n_flags),
2782 tx_resp->failure_frame);
2783
2784 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
2785 if (index != -1) {
2786 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
2787 #ifdef CONFIG_IWL4965_HT
2788 if (tid != MAX_TID_COUNT)
2789 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
2790 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
2791 (txq_id >= 0) && priv->mac80211_registered)
2792 ieee80211_wake_queue(priv->hw, txq_id);
2793 if (tid != MAX_TID_COUNT)
2794 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
2795 #endif
2796 }
2797 #ifdef CONFIG_IWL4965_HT
2798 }
2799 #endif /* CONFIG_IWL4965_HT */
2800
2801 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
2802 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
2803 }
2804
2805
2806 static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
2807 struct iwl_rx_mem_buffer *rxb)
2808 {
2809 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2810 struct iwl4965_alive_resp *palive;
2811 struct delayed_work *pwork;
2812
2813 palive = &pkt->u.alive_frame;
2814
2815 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2816 "0x%01X 0x%01X\n",
2817 palive->is_valid, palive->ver_type,
2818 palive->ver_subtype);
2819
2820 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2821 IWL_DEBUG_INFO("Initialization Alive received.\n");
2822 memcpy(&priv->card_alive_init,
2823 &pkt->u.alive_frame,
2824 sizeof(struct iwl4965_init_alive_resp));
2825 pwork = &priv->init_alive_start;
2826 } else {
2827 IWL_DEBUG_INFO("Runtime Alive received.\n");
2828 memcpy(&priv->card_alive, &pkt->u.alive_frame,
2829 sizeof(struct iwl4965_alive_resp));
2830 pwork = &priv->alive_start;
2831 }
2832
2833 /* We delay the ALIVE response by 5ms to
2834 * give the HW RF Kill time to activate... */
2835 if (palive->is_valid == UCODE_VALID_OK)
2836 queue_delayed_work(priv->workqueue, pwork,
2837 msecs_to_jiffies(5));
2838 else
2839 IWL_WARNING("uCode did not respond OK.\n");
2840 }
2841
2842 static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
2843 struct iwl_rx_mem_buffer *rxb)
2844 {
2845 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2846
2847 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2848 return;
2849 }
2850
2851 static void iwl4965_rx_reply_error(struct iwl_priv *priv,
2852 struct iwl_rx_mem_buffer *rxb)
2853 {
2854 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2855
2856 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
2857 "seq 0x%04X ser 0x%08X\n",
2858 le32_to_cpu(pkt->u.err_resp.error_type),
2859 get_cmd_string(pkt->u.err_resp.cmd_id),
2860 pkt->u.err_resp.cmd_id,
2861 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2862 le32_to_cpu(pkt->u.err_resp.error_info));
2863 }
2864
2865 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2866
2867 static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
2868 {
2869 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2870 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
2871 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
2872 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2873 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2874 rxon->channel = csa->channel;
2875 priv->staging_rxon.channel = csa->channel;
2876 }
2877
2878 static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
2879 struct iwl_rx_mem_buffer *rxb)
2880 {
2881 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
2882 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2883 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
2884
2885 if (!report->state) {
2886 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2887 "Spectrum Measure Notification: Start\n");
2888 return;
2889 }
2890
2891 memcpy(&priv->measure_report, report, sizeof(*report));
2892 priv->measurement_status |= MEASUREMENT_READY;
2893 #endif
2894 }
2895
2896 static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
2897 struct iwl_rx_mem_buffer *rxb)
2898 {
2899 #ifdef CONFIG_IWLWIFI_DEBUG
2900 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2901 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
2902 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2903 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2904 #endif
2905 }
2906
2907 static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
2908 struct iwl_rx_mem_buffer *rxb)
2909 {
2910 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2911 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2912 "notification for %s:\n",
2913 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
2914 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
2915 }
2916
2917 static void iwl4965_bg_beacon_update(struct work_struct *work)
2918 {
2919 struct iwl_priv *priv =
2920 container_of(work, struct iwl_priv, beacon_update);
2921 struct sk_buff *beacon;
2922
2923 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
2924 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
2925
2926 if (!beacon) {
2927 IWL_ERROR("update beacon failed\n");
2928 return;
2929 }
2930
2931 mutex_lock(&priv->mutex);
2932 /* new beacon skb is allocated every time; dispose previous.*/
2933 if (priv->ibss_beacon)
2934 dev_kfree_skb(priv->ibss_beacon);
2935
2936 priv->ibss_beacon = beacon;
2937 mutex_unlock(&priv->mutex);
2938
2939 iwl4965_send_beacon_cmd(priv);
2940 }
2941
2942 static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
2943 struct iwl_rx_mem_buffer *rxb)
2944 {
2945 #ifdef CONFIG_IWLWIFI_DEBUG
2946 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2947 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2948 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
2949
2950 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2951 "tsf %d %d rate %d\n",
2952 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2953 beacon->beacon_notify_hdr.failure_frame,
2954 le32_to_cpu(beacon->ibss_mgr_status),
2955 le32_to_cpu(beacon->high_tsf),
2956 le32_to_cpu(beacon->low_tsf), rate);
2957 #endif
2958
2959 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2960 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2961 queue_work(priv->workqueue, &priv->beacon_update);
2962 }
2963
2964 /* Service response to REPLY_SCAN_CMD (0x80) */
2965 static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
2966 struct iwl_rx_mem_buffer *rxb)
2967 {
2968 #ifdef CONFIG_IWLWIFI_DEBUG
2969 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2970 struct iwl4965_scanreq_notification *notif =
2971 (struct iwl4965_scanreq_notification *)pkt->u.raw;
2972
2973 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2974 #endif
2975 }
2976
2977 /* Service SCAN_START_NOTIFICATION (0x82) */
2978 static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
2979 struct iwl_rx_mem_buffer *rxb)
2980 {
2981 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2982 struct iwl4965_scanstart_notification *notif =
2983 (struct iwl4965_scanstart_notification *)pkt->u.raw;
2984 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2985 IWL_DEBUG_SCAN("Scan start: "
2986 "%d [802.11%s] "
2987 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2988 notif->channel,
2989 notif->band ? "bg" : "a",
2990 notif->tsf_high,
2991 notif->tsf_low, notif->status, notif->beacon_timer);
2992 }
2993
2994 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
2995 static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
2996 struct iwl_rx_mem_buffer *rxb)
2997 {
2998 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2999 struct iwl4965_scanresults_notification *notif =
3000 (struct iwl4965_scanresults_notification *)pkt->u.raw;
3001
3002 IWL_DEBUG_SCAN("Scan ch.res: "
3003 "%d [802.11%s] "
3004 "(TSF: 0x%08X:%08X) - %d "
3005 "elapsed=%lu usec (%dms since last)\n",
3006 notif->channel,
3007 notif->band ? "bg" : "a",
3008 le32_to_cpu(notif->tsf_high),
3009 le32_to_cpu(notif->tsf_low),
3010 le32_to_cpu(notif->statistics[0]),
3011 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3012 jiffies_to_msecs(elapsed_jiffies
3013 (priv->last_scan_jiffies, jiffies)));
3014
3015 priv->last_scan_jiffies = jiffies;
3016 priv->next_scan_jiffies = 0;
3017 }
3018
3019 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
3020 static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
3021 struct iwl_rx_mem_buffer *rxb)
3022 {
3023 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3024 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
3025
3026 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3027 scan_notif->scanned_channels,
3028 scan_notif->tsf_low,
3029 scan_notif->tsf_high, scan_notif->status);
3030
3031 /* The HW is no longer scanning */
3032 clear_bit(STATUS_SCAN_HW, &priv->status);
3033
3034 /* The scan completion notification came in, so kill that timer... */
3035 cancel_delayed_work(&priv->scan_check);
3036
3037 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3038 (priv->scan_bands == 2) ? "2.4" : "5.2",
3039 jiffies_to_msecs(elapsed_jiffies
3040 (priv->scan_pass_start, jiffies)));
3041
3042 /* Remove this scanned band from the list
3043 * of pending bands to scan */
3044 priv->scan_bands--;
3045
3046 /* If a request to abort was given, or the scan did not succeed
3047 * then we reset the scan state machine and terminate,
3048 * re-queuing another scan if one has been requested */
3049 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3050 IWL_DEBUG_INFO("Aborted scan completed.\n");
3051 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3052 } else {
3053 /* If there are more bands on this scan pass reschedule */
3054 if (priv->scan_bands > 0)
3055 goto reschedule;
3056 }
3057
3058 priv->last_scan_jiffies = jiffies;
3059 priv->next_scan_jiffies = 0;
3060 IWL_DEBUG_INFO("Setting scan to off\n");
3061
3062 clear_bit(STATUS_SCANNING, &priv->status);
3063
3064 IWL_DEBUG_INFO("Scan took %dms\n",
3065 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3066
3067 queue_work(priv->workqueue, &priv->scan_completed);
3068
3069 return;
3070
3071 reschedule:
3072 priv->scan_pass_start = jiffies;
3073 queue_work(priv->workqueue, &priv->request_scan);
3074 }
3075
3076 /* Handle notification from uCode that card's power state is changing
3077 * due to software, hardware, or critical temperature RFKILL */
3078 static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
3079 struct iwl_rx_mem_buffer *rxb)
3080 {
3081 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3082 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3083 unsigned long status = priv->status;
3084
3085 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3086 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3087 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3088
3089 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3090 RF_CARD_DISABLED)) {
3091
3092 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3093 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3094
3095 if (!iwl_grab_nic_access(priv)) {
3096 iwl_write_direct32(
3097 priv, HBUS_TARG_MBX_C,
3098 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3099
3100 iwl_release_nic_access(priv);
3101 }
3102
3103 if (!(flags & RXON_CARD_DISABLED)) {
3104 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
3105 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3106 if (!iwl_grab_nic_access(priv)) {
3107 iwl_write_direct32(
3108 priv, HBUS_TARG_MBX_C,
3109 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3110
3111 iwl_release_nic_access(priv);
3112 }
3113 }
3114
3115 if (flags & RF_CARD_DISABLED) {
3116 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
3117 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
3118 iwl_read32(priv, CSR_UCODE_DRV_GP1);
3119 if (!iwl_grab_nic_access(priv))
3120 iwl_release_nic_access(priv);
3121 }
3122 }
3123
3124 if (flags & HW_CARD_DISABLED)
3125 set_bit(STATUS_RF_KILL_HW, &priv->status);
3126 else
3127 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3128
3129
3130 if (flags & SW_CARD_DISABLED)
3131 set_bit(STATUS_RF_KILL_SW, &priv->status);
3132 else
3133 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3134
3135 if (!(flags & RXON_CARD_DISABLED))
3136 iwl4965_scan_cancel(priv);
3137
3138 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3139 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3140 (test_bit(STATUS_RF_KILL_SW, &status) !=
3141 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3142 queue_work(priv->workqueue, &priv->rf_kill);
3143 else
3144 wake_up_interruptible(&priv->wait_command_queue);
3145 }
3146
3147 /**
3148 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
3149 *
3150 * Setup the RX handlers for each of the reply types sent from the uCode
3151 * to the host.
3152 *
3153 * This function chains into the hardware specific files for them to setup
3154 * any hardware specific handlers as well.
3155 */
3156 static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
3157 {
3158 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3159 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3160 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3161 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
3162 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
3163 iwl4965_rx_spectrum_measure_notif;
3164 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
3165 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
3166 iwl4965_rx_pm_debug_statistics_notif;
3167 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
3168
3169 /*
3170 * The same handler is used for both the REPLY to a discrete
3171 * statistics request from the host as well as for the periodic
3172 * statistics notifications (after received beacons) from the uCode.
3173 */
3174 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3175 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
3176
3177 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3178 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
3179 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
3180 iwl4965_rx_scan_results_notif;
3181 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
3182 iwl4965_rx_scan_complete_notif;
3183 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3184 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
3185
3186 /* Set up hardware specific Rx handlers */
3187 priv->cfg->ops->lib->rx_handler_setup(priv);
3188 }
3189
3190 /**
3191 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
3192 * @rxb: Rx buffer to reclaim
3193 *
3194 * If an Rx buffer has an async callback associated with it the callback
3195 * will be executed. The attached skb (if present) will only be freed
3196 * if the callback returns 1
3197 */
3198 static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
3199 struct iwl_rx_mem_buffer *rxb)
3200 {
3201 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
3202 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3203 int txq_id = SEQ_TO_QUEUE(sequence);
3204 int index = SEQ_TO_INDEX(sequence);
3205 int huge = sequence & SEQ_HUGE_FRAME;
3206 int cmd_index;
3207 struct iwl_cmd *cmd;
3208
3209 /* If a Tx command is being handled and it isn't in the actual
3210 * command queue then there a command routing bug has been introduced
3211 * in the queue management code. */
3212 if (txq_id != IWL_CMD_QUEUE_NUM)
3213 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3214 txq_id, pkt->hdr.cmd);
3215 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3216
3217 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3218 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3219
3220 /* Input error checking is done when commands are added to queue. */
3221 if (cmd->meta.flags & CMD_WANT_SKB) {
3222 cmd->meta.source->u.skb = rxb->skb;
3223 rxb->skb = NULL;
3224 } else if (cmd->meta.u.callback &&
3225 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3226 rxb->skb = NULL;
3227
3228 iwl4965_tx_queue_reclaim(priv, txq_id, index);
3229
3230 if (!(cmd->meta.flags & CMD_ASYNC)) {
3231 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3232 wake_up_interruptible(&priv->wait_command_queue);
3233 }
3234 }
3235
3236 /*
3237 * this should be called while priv->lock is locked
3238 */
3239 static void __iwl_rx_replenish(struct iwl_priv *priv)
3240 {
3241 iwl_rx_allocate(priv);
3242 iwl_rx_queue_restock(priv);
3243 }
3244
3245
3246 /**
3247 * iwl_rx_handle - Main entry function for receiving responses from uCode
3248 *
3249 * Uses the priv->rx_handlers callback function array to invoke
3250 * the appropriate handlers, including command responses,
3251 * frame-received notifications, and other notifications.
3252 */
3253 void iwl_rx_handle(struct iwl_priv *priv)
3254 {
3255 struct iwl_rx_mem_buffer *rxb;
3256 struct iwl_rx_packet *pkt;
3257 struct iwl_rx_queue *rxq = &priv->rxq;
3258 u32 r, i;
3259 int reclaim;
3260 unsigned long flags;
3261 u8 fill_rx = 0;
3262 u32 count = 8;
3263
3264 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3265 * buffer that the driver may process (last buffer filled by ucode). */
3266 r = iwl4965_hw_get_rx_read(priv);
3267 i = rxq->read;
3268
3269 /* Rx interrupt, but nothing sent from uCode */
3270 if (i == r)
3271 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3272
3273 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3274 fill_rx = 1;
3275
3276 while (i != r) {
3277 rxb = rxq->queue[i];
3278
3279 /* If an RXB doesn't have a Rx queue slot associated with it,
3280 * then a bug has been introduced in the queue refilling
3281 * routines -- catch it here */
3282 BUG_ON(rxb == NULL);
3283
3284 rxq->queue[i] = NULL;
3285
3286 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
3287 priv->hw_params.rx_buf_size,
3288 PCI_DMA_FROMDEVICE);
3289 pkt = (struct iwl_rx_packet *)rxb->skb->data;
3290
3291 /* Reclaim a command buffer only if this packet is a response
3292 * to a (driver-originated) command.
3293 * If the packet (e.g. Rx frame) originated from uCode,
3294 * there is no command buffer to reclaim.
3295 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3296 * but apparently a few don't get set; catch them here. */
3297 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3298 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
3299 (pkt->hdr.cmd != REPLY_RX) &&
3300 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
3301 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3302 (pkt->hdr.cmd != REPLY_TX);
3303
3304 /* Based on type of command response or notification,
3305 * handle those that need handling via function in
3306 * rx_handlers table. See iwl4965_setup_rx_handlers() */
3307 if (priv->rx_handlers[pkt->hdr.cmd]) {
3308 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3309 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3310 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3311 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3312 } else {
3313 /* No handling needed */
3314 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
3315 "r %d i %d No handler needed for %s, 0x%02x\n",
3316 r, i, get_cmd_string(pkt->hdr.cmd),
3317 pkt->hdr.cmd);
3318 }
3319
3320 if (reclaim) {
3321 /* Invoke any callbacks, transfer the skb to caller, and
3322 * fire off the (possibly) blocking iwl_send_cmd()
3323 * as we reclaim the driver command queue */
3324 if (rxb && rxb->skb)
3325 iwl4965_tx_cmd_complete(priv, rxb);
3326 else
3327 IWL_WARNING("Claim null rxb?\n");
3328 }
3329
3330 /* For now we just don't re-use anything. We can tweak this
3331 * later to try and re-use notification packets and SKBs that
3332 * fail to Rx correctly */
3333 if (rxb->skb != NULL) {
3334 priv->alloc_rxb_skb--;
3335 dev_kfree_skb_any(rxb->skb);
3336 rxb->skb = NULL;
3337 }
3338
3339 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
3340 priv->hw_params.rx_buf_size,
3341 PCI_DMA_FROMDEVICE);
3342 spin_lock_irqsave(&rxq->lock, flags);
3343 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3344 spin_unlock_irqrestore(&rxq->lock, flags);
3345 i = (i + 1) & RX_QUEUE_MASK;
3346 /* If there are a lot of unused frames,
3347 * restock the Rx queue so ucode wont assert. */
3348 if (fill_rx) {
3349 count++;
3350 if (count >= 8) {
3351 priv->rxq.read = i;
3352 __iwl_rx_replenish(priv);
3353 count = 0;
3354 }
3355 }
3356 }
3357
3358 /* Backtrack one entry */
3359 priv->rxq.read = i;
3360 iwl_rx_queue_restock(priv);
3361 }
3362 /* Convert linear signal-to-noise ratio into dB */
3363 static u8 ratio2dB[100] = {
3364 /* 0 1 2 3 4 5 6 7 8 9 */
3365 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3366 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3367 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3368 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3369 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3370 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3371 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3372 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3373 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3374 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3375 };
3376
3377 /* Calculates a relative dB value from a ratio of linear
3378 * (i.e. not dB) signal levels.
3379 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
3380 int iwl4965_calc_db_from_ratio(int sig_ratio)
3381 {
3382 /* 1000:1 or higher just report as 60 dB */
3383 if (sig_ratio >= 1000)
3384 return 60;
3385
3386 /* 100:1 or higher, divide by 10 and use table,
3387 * add 20 dB to make up for divide by 10 */
3388 if (sig_ratio >= 100)
3389 return (20 + (int)ratio2dB[sig_ratio/10]);
3390
3391 /* We shouldn't see this */
3392 if (sig_ratio < 1)
3393 return 0;
3394
3395 /* Use table for ratios 1:1 - 99:1 */
3396 return (int)ratio2dB[sig_ratio];
3397 }
3398
3399 #define PERFECT_RSSI (-20) /* dBm */
3400 #define WORST_RSSI (-95) /* dBm */
3401 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3402
3403 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
3404 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3405 * about formulas used below. */
3406 int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
3407 {
3408 int sig_qual;
3409 int degradation = PERFECT_RSSI - rssi_dbm;
3410
3411 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3412 * as indicator; formula is (signal dbm - noise dbm).
3413 * SNR at or above 40 is a great signal (100%).
3414 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3415 * Weakest usable signal is usually 10 - 15 dB SNR. */
3416 if (noise_dbm) {
3417 if (rssi_dbm - noise_dbm >= 40)
3418 return 100;
3419 else if (rssi_dbm < noise_dbm)
3420 return 0;
3421 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3422
3423 /* Else use just the signal level.
3424 * This formula is a least squares fit of data points collected and
3425 * compared with a reference system that had a percentage (%) display
3426 * for signal quality. */
3427 } else
3428 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3429 (15 * RSSI_RANGE + 62 * degradation)) /
3430 (RSSI_RANGE * RSSI_RANGE);
3431
3432 if (sig_qual > 100)
3433 sig_qual = 100;
3434 else if (sig_qual < 1)
3435 sig_qual = 0;
3436
3437 return sig_qual;
3438 }
3439
3440 /**
3441 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
3442 */
3443 static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
3444 struct iwl4965_tx_queue *txq)
3445 {
3446 u32 reg = 0;
3447 int rc = 0;
3448 int txq_id = txq->q.id;
3449
3450 if (txq->need_update == 0)
3451 return rc;
3452
3453 /* if we're trying to save power */
3454 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3455 /* wake up nic if it's powered down ...
3456 * uCode will wake up, and interrupt us again, so next
3457 * time we'll skip this part. */
3458 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
3459
3460 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3461 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
3462 iwl_set_bit(priv, CSR_GP_CNTRL,
3463 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3464 return rc;
3465 }
3466
3467 /* restore this queue's parameters in nic hardware. */
3468 rc = iwl_grab_nic_access(priv);
3469 if (rc)
3470 return rc;
3471 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
3472 txq->q.write_ptr | (txq_id << 8));
3473 iwl_release_nic_access(priv);
3474
3475 /* else not in power-save mode, uCode will never sleep when we're
3476 * trying to tx (during RFKILL, we're not trying to tx). */
3477 } else
3478 iwl_write32(priv, HBUS_TARG_WRPTR,
3479 txq->q.write_ptr | (txq_id << 8));
3480
3481 txq->need_update = 0;
3482
3483 return rc;
3484 }
3485
3486 #ifdef CONFIG_IWLWIFI_DEBUG
3487 static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
3488 {
3489 DECLARE_MAC_BUF(mac);
3490
3491 IWL_DEBUG_RADIO("RX CONFIG:\n");
3492 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
3493 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3494 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3495 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3496 le32_to_cpu(rxon->filter_flags));
3497 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3498 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3499 rxon->ofdm_basic_rates);
3500 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
3501 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
3502 print_mac(mac, rxon->node_addr));
3503 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
3504 print_mac(mac, rxon->bssid_addr));
3505 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3506 }
3507 #endif
3508
3509 static void iwl4965_enable_interrupts(struct iwl_priv *priv)
3510 {
3511 IWL_DEBUG_ISR("Enabling interrupts\n");
3512 set_bit(STATUS_INT_ENABLED, &priv->status);
3513 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
3514 }
3515
3516 /* call this function to flush any scheduled tasklet */
3517 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
3518 {
3519 /* wait to make sure we flush pedding tasklet*/
3520 synchronize_irq(priv->pci_dev->irq);
3521 tasklet_kill(&priv->irq_tasklet);
3522 }
3523
3524 static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
3525 {
3526 clear_bit(STATUS_INT_ENABLED, &priv->status);
3527
3528 /* disable interrupts from uCode/NIC to host */
3529 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
3530
3531 /* acknowledge/clear/reset any interrupts still pending
3532 * from uCode or flow handler (Rx/Tx DMA) */
3533 iwl_write32(priv, CSR_INT, 0xffffffff);
3534 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
3535 IWL_DEBUG_ISR("Disabled interrupts\n");
3536 }
3537
3538 static const char *desc_lookup(int i)
3539 {
3540 switch (i) {
3541 case 1:
3542 return "FAIL";
3543 case 2:
3544 return "BAD_PARAM";
3545 case 3:
3546 return "BAD_CHECKSUM";
3547 case 4:
3548 return "NMI_INTERRUPT";
3549 case 5:
3550 return "SYSASSERT";
3551 case 6:
3552 return "FATAL_ERROR";
3553 }
3554
3555 return "UNKNOWN";
3556 }
3557
3558 #define ERROR_START_OFFSET (1 * sizeof(u32))
3559 #define ERROR_ELEM_SIZE (7 * sizeof(u32))
3560
3561 static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
3562 {
3563 u32 data2, line;
3564 u32 desc, time, count, base, data1;
3565 u32 blink1, blink2, ilink1, ilink2;
3566 int rc;
3567
3568 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3569
3570 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
3571 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
3572 return;
3573 }
3574
3575 rc = iwl_grab_nic_access(priv);
3576 if (rc) {
3577 IWL_WARNING("Can not read from adapter at this time.\n");
3578 return;
3579 }
3580
3581 count = iwl_read_targ_mem(priv, base);
3582
3583 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
3584 IWL_ERROR("Start IWL Error Log Dump:\n");
3585 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
3586 }
3587
3588 desc = iwl_read_targ_mem(priv, base + 1 * sizeof(u32));
3589 blink1 = iwl_read_targ_mem(priv, base + 3 * sizeof(u32));
3590 blink2 = iwl_read_targ_mem(priv, base + 4 * sizeof(u32));
3591 ilink1 = iwl_read_targ_mem(priv, base + 5 * sizeof(u32));
3592 ilink2 = iwl_read_targ_mem(priv, base + 6 * sizeof(u32));
3593 data1 = iwl_read_targ_mem(priv, base + 7 * sizeof(u32));
3594 data2 = iwl_read_targ_mem(priv, base + 8 * sizeof(u32));
3595 line = iwl_read_targ_mem(priv, base + 9 * sizeof(u32));
3596 time = iwl_read_targ_mem(priv, base + 11 * sizeof(u32));
3597
3598 IWL_ERROR("Desc Time "
3599 "data1 data2 line\n");
3600 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
3601 desc_lookup(desc), desc, time, data1, data2, line);
3602 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
3603 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
3604 ilink1, ilink2);
3605
3606 iwl_release_nic_access(priv);
3607 }
3608
3609 #define EVENT_START_OFFSET (4 * sizeof(u32))
3610
3611 /**
3612 * iwl4965_print_event_log - Dump error event log to syslog
3613 *
3614 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
3615 */
3616 static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
3617 u32 num_events, u32 mode)
3618 {
3619 u32 i;
3620 u32 base; /* SRAM byte address of event log header */
3621 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3622 u32 ptr; /* SRAM byte address of log data */
3623 u32 ev, time, data; /* event log data */
3624
3625 if (num_events == 0)
3626 return;
3627
3628 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3629
3630 if (mode == 0)
3631 event_size = 2 * sizeof(u32);
3632 else
3633 event_size = 3 * sizeof(u32);
3634
3635 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3636
3637 /* "time" is actually "data" for mode 0 (no timestamp).
3638 * place event id # at far right for easier visual parsing. */
3639 for (i = 0; i < num_events; i++) {
3640 ev = iwl_read_targ_mem(priv, ptr);
3641 ptr += sizeof(u32);
3642 time = iwl_read_targ_mem(priv, ptr);
3643 ptr += sizeof(u32);
3644 if (mode == 0)
3645 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
3646 else {
3647 data = iwl_read_targ_mem(priv, ptr);
3648 ptr += sizeof(u32);
3649 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
3650 }
3651 }
3652 }
3653
3654 static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
3655 {
3656 int rc;
3657 u32 base; /* SRAM byte address of event log header */
3658 u32 capacity; /* event log capacity in # entries */
3659 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3660 u32 num_wraps; /* # times uCode wrapped to top of log */
3661 u32 next_entry; /* index of next entry to be written by uCode */
3662 u32 size; /* # entries that we'll print */
3663
3664 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3665 if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
3666 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
3667 return;
3668 }
3669
3670 rc = iwl_grab_nic_access(priv);
3671 if (rc) {
3672 IWL_WARNING("Can not read from adapter at this time.\n");
3673 return;
3674 }
3675
3676 /* event log header */
3677 capacity = iwl_read_targ_mem(priv, base);
3678 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3679 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3680 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
3681
3682 size = num_wraps ? capacity : next_entry;
3683
3684 /* bail out if nothing in log */
3685 if (size == 0) {
3686 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
3687 iwl_release_nic_access(priv);
3688 return;
3689 }
3690
3691 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
3692 size, num_wraps);
3693
3694 /* if uCode has wrapped back to top of log, start at the oldest entry,
3695 * i.e the next one that uCode would fill. */
3696 if (num_wraps)
3697 iwl4965_print_event_log(priv, next_entry,
3698 capacity - next_entry, mode);
3699
3700 /* (then/else) start at top of log */
3701 iwl4965_print_event_log(priv, 0, next_entry, mode);
3702
3703 iwl_release_nic_access(priv);
3704 }
3705
3706 /**
3707 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
3708 */
3709 static void iwl4965_irq_handle_error(struct iwl_priv *priv)
3710 {
3711 /* Set the FW error flag -- cleared on iwl4965_down */
3712 set_bit(STATUS_FW_ERROR, &priv->status);
3713
3714 /* Cancel currently queued command. */
3715 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3716
3717 #ifdef CONFIG_IWLWIFI_DEBUG
3718 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
3719 iwl4965_dump_nic_error_log(priv);
3720 iwl4965_dump_nic_event_log(priv);
3721 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
3722 }
3723 #endif
3724
3725 wake_up_interruptible(&priv->wait_command_queue);
3726
3727 /* Keep the restart process from trying to send host
3728 * commands by clearing the INIT status bit */
3729 clear_bit(STATUS_READY, &priv->status);
3730
3731 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3732 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3733 "Restarting adapter due to uCode error.\n");
3734
3735 if (iwl_is_associated(priv)) {
3736 memcpy(&priv->recovery_rxon, &priv->active_rxon,
3737 sizeof(priv->recovery_rxon));
3738 priv->error_recovering = 1;
3739 }
3740 queue_work(priv->workqueue, &priv->restart);
3741 }
3742 }
3743
3744 static void iwl4965_error_recovery(struct iwl_priv *priv)
3745 {
3746 unsigned long flags;
3747
3748 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
3749 sizeof(priv->staging_rxon));
3750 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3751 iwl4965_commit_rxon(priv);
3752
3753 iwl4965_rxon_add_station(priv, priv->bssid, 1);
3754
3755 spin_lock_irqsave(&priv->lock, flags);
3756 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
3757 priv->error_recovering = 0;
3758 spin_unlock_irqrestore(&priv->lock, flags);
3759 }
3760
3761 static void iwl4965_irq_tasklet(struct iwl_priv *priv)
3762 {
3763 u32 inta, handled = 0;
3764 u32 inta_fh;
3765 unsigned long flags;
3766 #ifdef CONFIG_IWLWIFI_DEBUG
3767 u32 inta_mask;
3768 #endif
3769
3770 spin_lock_irqsave(&priv->lock, flags);
3771
3772 /* Ack/clear/reset pending uCode interrupts.
3773 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3774 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
3775 inta = iwl_read32(priv, CSR_INT);
3776 iwl_write32(priv, CSR_INT, inta);
3777
3778 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3779 * Any new interrupts that happen after this, either while we're
3780 * in this tasklet, or later, will show up in next ISR/tasklet. */
3781 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3782 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
3783
3784 #ifdef CONFIG_IWLWIFI_DEBUG
3785 if (iwl_debug_level & IWL_DL_ISR) {
3786 /* just for debug */
3787 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3788 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3789 inta, inta_mask, inta_fh);
3790 }
3791 #endif
3792
3793 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3794 * atomic, make sure that inta covers all the interrupts that
3795 * we've discovered, even if FH interrupt came in just after
3796 * reading CSR_INT. */
3797 if (inta_fh & CSR49_FH_INT_RX_MASK)
3798 inta |= CSR_INT_BIT_FH_RX;
3799 if (inta_fh & CSR49_FH_INT_TX_MASK)
3800 inta |= CSR_INT_BIT_FH_TX;
3801
3802 /* Now service all interrupt bits discovered above. */
3803 if (inta & CSR_INT_BIT_HW_ERR) {
3804 IWL_ERROR("Microcode HW error detected. Restarting.\n");
3805
3806 /* Tell the device to stop sending interrupts */
3807 iwl4965_disable_interrupts(priv);
3808
3809 iwl4965_irq_handle_error(priv);
3810
3811 handled |= CSR_INT_BIT_HW_ERR;
3812
3813 spin_unlock_irqrestore(&priv->lock, flags);
3814
3815 return;
3816 }
3817
3818 #ifdef CONFIG_IWLWIFI_DEBUG
3819 if (iwl_debug_level & (IWL_DL_ISR)) {
3820 /* NIC fires this, but we don't use it, redundant with WAKEUP */
3821 if (inta & CSR_INT_BIT_SCD)
3822 IWL_DEBUG_ISR("Scheduler finished to transmit "
3823 "the frame/frames.\n");
3824
3825 /* Alive notification via Rx interrupt will do the real work */
3826 if (inta & CSR_INT_BIT_ALIVE)
3827 IWL_DEBUG_ISR("Alive interrupt\n");
3828 }
3829 #endif
3830 /* Safely ignore these bits for debug checks below */
3831 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
3832
3833 /* HW RF KILL switch toggled */
3834 if (inta & CSR_INT_BIT_RF_KILL) {
3835 int hw_rf_kill = 0;
3836 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
3837 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
3838 hw_rf_kill = 1;
3839
3840 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
3841 "RF_KILL bit toggled to %s.\n",
3842 hw_rf_kill ? "disable radio":"enable radio");
3843
3844 /* Queue restart only if RF_KILL switch was set to "kill"
3845 * when we loaded driver, and is now set to "enable".
3846 * After we're Alive, RF_KILL gets handled by
3847 * iwl4965_rx_card_state_notif() */
3848 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
3849 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3850 queue_work(priv->workqueue, &priv->restart);
3851 }
3852
3853 handled |= CSR_INT_BIT_RF_KILL;
3854 }
3855
3856 /* Chip got too hot and stopped itself */
3857 if (inta & CSR_INT_BIT_CT_KILL) {
3858 IWL_ERROR("Microcode CT kill error detected.\n");
3859 handled |= CSR_INT_BIT_CT_KILL;
3860 }
3861
3862 /* Error detected by uCode */
3863 if (inta & CSR_INT_BIT_SW_ERR) {
3864 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
3865 inta);
3866 iwl4965_irq_handle_error(priv);
3867 handled |= CSR_INT_BIT_SW_ERR;
3868 }
3869
3870 /* uCode wakes up after power-down sleep */
3871 if (inta & CSR_INT_BIT_WAKEUP) {
3872 IWL_DEBUG_ISR("Wakeup interrupt\n");
3873 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
3874 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
3875 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
3876 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
3877 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
3878 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
3879 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
3880
3881 handled |= CSR_INT_BIT_WAKEUP;
3882 }
3883
3884 /* All uCode command responses, including Tx command responses,
3885 * Rx "responses" (frame-received notification), and other
3886 * notifications from uCode come through here*/
3887 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
3888 iwl_rx_handle(priv);
3889 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3890 }
3891
3892 if (inta & CSR_INT_BIT_FH_TX) {
3893 IWL_DEBUG_ISR("Tx interrupt\n");
3894 handled |= CSR_INT_BIT_FH_TX;
3895 }
3896
3897 if (inta & ~handled)
3898 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
3899
3900 if (inta & ~CSR_INI_SET_MASK) {
3901 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
3902 inta & ~CSR_INI_SET_MASK);
3903 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
3904 }
3905
3906 /* Re-enable all interrupts */
3907 /* only Re-enable if diabled by irq */
3908 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3909 iwl4965_enable_interrupts(priv);
3910
3911 #ifdef CONFIG_IWLWIFI_DEBUG
3912 if (iwl_debug_level & (IWL_DL_ISR)) {
3913 inta = iwl_read32(priv, CSR_INT);
3914 inta_mask = iwl_read32(priv, CSR_INT_MASK);
3915 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3916 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
3917 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
3918 }
3919 #endif
3920 spin_unlock_irqrestore(&priv->lock, flags);
3921 }
3922
3923 static irqreturn_t iwl4965_isr(int irq, void *data)
3924 {
3925 struct iwl_priv *priv = data;
3926 u32 inta, inta_mask;
3927 u32 inta_fh;
3928 if (!priv)
3929 return IRQ_NONE;
3930
3931 spin_lock(&priv->lock);
3932
3933 /* Disable (but don't clear!) interrupts here to avoid
3934 * back-to-back ISRs and sporadic interrupts from our NIC.
3935 * If we have something to service, the tasklet will re-enable ints.
3936 * If we *don't* have something, we'll re-enable before leaving here. */
3937 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
3938 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
3939
3940 /* Discover which interrupts are active/pending */
3941 inta = iwl_read32(priv, CSR_INT);
3942 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3943
3944 /* Ignore interrupt if there's nothing in NIC to service.
3945 * This may be due to IRQ shared with another device,
3946 * or due to sporadic interrupts thrown from our NIC. */
3947 if (!inta && !inta_fh) {
3948 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
3949 goto none;
3950 }
3951
3952 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
3953 /* Hardware disappeared. It might have already raised
3954 * an interrupt */
3955 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
3956 goto unplugged;
3957 }
3958
3959 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3960 inta, inta_mask, inta_fh);
3961
3962 inta &= ~CSR_INT_BIT_SCD;
3963
3964 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
3965 if (likely(inta || inta_fh))
3966 tasklet_schedule(&priv->irq_tasklet);
3967
3968 unplugged:
3969 spin_unlock(&priv->lock);
3970 return IRQ_HANDLED;
3971
3972 none:
3973 /* re-enable interrupts here since we don't have anything to service. */
3974 /* only Re-enable if diabled by irq */
3975 if (test_bit(STATUS_INT_ENABLED, &priv->status))
3976 iwl4965_enable_interrupts(priv);
3977 spin_unlock(&priv->lock);
3978 return IRQ_NONE;
3979 }
3980
3981 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
3982 * sending probe req. This should be set long enough to hear probe responses
3983 * from more than one AP. */
3984 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
3985 #define IWL_ACTIVE_DWELL_TIME_52 (10)
3986
3987 /* For faster active scanning, scan will move to the next channel if fewer than
3988 * PLCP_QUIET_THRESH packets are heard on this channel within
3989 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
3990 * time if it's a quiet channel (nothing responded to our probe, and there's
3991 * no other traffic).
3992 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
3993 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
3994 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
3995
3996 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
3997 * Must be set longer than active dwell time.
3998 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
3999 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4000 #define IWL_PASSIVE_DWELL_TIME_52 (10)
4001 #define IWL_PASSIVE_DWELL_BASE (100)
4002 #define IWL_CHANNEL_TUNE_TIME 5
4003
4004 static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
4005 enum ieee80211_band band)
4006 {
4007 if (band == IEEE80211_BAND_5GHZ)
4008 return IWL_ACTIVE_DWELL_TIME_52;
4009 else
4010 return IWL_ACTIVE_DWELL_TIME_24;
4011 }
4012
4013 static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
4014 enum ieee80211_band band)
4015 {
4016 u16 active = iwl4965_get_active_dwell_time(priv, band);
4017 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
4018 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4019 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4020
4021 if (iwl_is_associated(priv)) {
4022 /* If we're associated, we clamp the maximum passive
4023 * dwell time to be 98% of the beacon interval (minus
4024 * 2 * channel tune time) */
4025 passive = priv->beacon_int;
4026 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4027 passive = IWL_PASSIVE_DWELL_BASE;
4028 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4029 }
4030
4031 if (passive <= active)
4032 passive = active + 1;
4033
4034 return passive;
4035 }
4036
4037 static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
4038 enum ieee80211_band band,
4039 u8 is_active, u8 direct_mask,
4040 struct iwl4965_scan_channel *scan_ch)
4041 {
4042 const struct ieee80211_channel *channels = NULL;
4043 const struct ieee80211_supported_band *sband;
4044 const struct iwl_channel_info *ch_info;
4045 u16 passive_dwell = 0;
4046 u16 active_dwell = 0;
4047 int added, i;
4048
4049 sband = iwl_get_hw_mode(priv, band);
4050 if (!sband)
4051 return 0;
4052
4053 channels = sband->channels;
4054
4055 active_dwell = iwl4965_get_active_dwell_time(priv, band);
4056 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
4057
4058 for (i = 0, added = 0; i < sband->n_channels; i++) {
4059 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4060 continue;
4061
4062 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
4063
4064 ch_info = iwl_get_channel_info(priv, band,
4065 scan_ch->channel);
4066 if (!is_channel_valid(ch_info)) {
4067 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
4068 scan_ch->channel);
4069 continue;
4070 }
4071
4072 if (!is_active || is_channel_passive(ch_info) ||
4073 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
4074 scan_ch->type = 0; /* passive */
4075 else
4076 scan_ch->type = 1; /* active */
4077
4078 if (scan_ch->type & 1)
4079 scan_ch->type |= (direct_mask << 1);
4080
4081 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4082 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
4083
4084 /* Set txpower levels to defaults */
4085 scan_ch->tpc.dsp_atten = 110;
4086 /* scan_pwr_info->tpc.dsp_atten; */
4087
4088 /*scan_pwr_info->tpc.tx_gain; */
4089 if (band == IEEE80211_BAND_5GHZ)
4090 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4091 else {
4092 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4093 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
4094 * power level:
4095 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
4096 */
4097 }
4098
4099 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4100 scan_ch->channel,
4101 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4102 (scan_ch->type & 1) ?
4103 active_dwell : passive_dwell);
4104
4105 scan_ch++;
4106 added++;
4107 }
4108
4109 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4110 return added;
4111 }
4112
4113 /******************************************************************************
4114 *
4115 * uCode download functions
4116 *
4117 ******************************************************************************/
4118
4119 static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
4120 {
4121 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4122 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4123 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4124 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4125 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4126 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
4127 }
4128
4129 static void iwl4965_nic_start(struct iwl_priv *priv)
4130 {
4131 /* Remove all resets to allow NIC to operate */
4132 iwl_write32(priv, CSR_RESET, 0);
4133 }
4134
4135
4136 /**
4137 * iwl4965_read_ucode - Read uCode images from disk file.
4138 *
4139 * Copy into buffers for card to fetch via bus-mastering
4140 */
4141 static int iwl4965_read_ucode(struct iwl_priv *priv)
4142 {
4143 struct iwl4965_ucode *ucode;
4144 int ret;
4145 const struct firmware *ucode_raw;
4146 const char *name = priv->cfg->fw_name;
4147 u8 *src;
4148 size_t len;
4149 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
4150
4151 /* Ask kernel firmware_class module to get the boot firmware off disk.
4152 * request_firmware() is synchronous, file is in memory on return. */
4153 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
4154 if (ret < 0) {
4155 IWL_ERROR("%s firmware file req failed: Reason %d\n",
4156 name, ret);
4157 goto error;
4158 }
4159
4160 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4161 name, ucode_raw->size);
4162
4163 /* Make sure that we got at least our header! */
4164 if (ucode_raw->size < sizeof(*ucode)) {
4165 IWL_ERROR("File size way too small!\n");
4166 ret = -EINVAL;
4167 goto err_release;
4168 }
4169
4170 /* Data from ucode file: header followed by uCode images */
4171 ucode = (void *)ucode_raw->data;
4172
4173 ver = le32_to_cpu(ucode->ver);
4174 inst_size = le32_to_cpu(ucode->inst_size);
4175 data_size = le32_to_cpu(ucode->data_size);
4176 init_size = le32_to_cpu(ucode->init_size);
4177 init_data_size = le32_to_cpu(ucode->init_data_size);
4178 boot_size = le32_to_cpu(ucode->boot_size);
4179
4180 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
4181 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
4182 inst_size);
4183 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
4184 data_size);
4185 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
4186 init_size);
4187 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
4188 init_data_size);
4189 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
4190 boot_size);
4191
4192 /* Verify size of file vs. image size info in file's header */
4193 if (ucode_raw->size < sizeof(*ucode) +
4194 inst_size + data_size + init_size +
4195 init_data_size + boot_size) {
4196
4197 IWL_DEBUG_INFO("uCode file size %d too small\n",
4198 (int)ucode_raw->size);
4199 ret = -EINVAL;
4200 goto err_release;
4201 }
4202
4203 /* Verify that uCode images will fit in card's SRAM */
4204 if (inst_size > priv->hw_params.max_inst_size) {
4205 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4206 inst_size);
4207 ret = -EINVAL;
4208 goto err_release;
4209 }
4210
4211 if (data_size > priv->hw_params.max_data_size) {
4212 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4213 data_size);
4214 ret = -EINVAL;
4215 goto err_release;
4216 }
4217 if (init_size > priv->hw_params.max_inst_size) {
4218 IWL_DEBUG_INFO
4219 ("uCode init instr len %d too large to fit in\n",
4220 init_size);
4221 ret = -EINVAL;
4222 goto err_release;
4223 }
4224 if (init_data_size > priv->hw_params.max_data_size) {
4225 IWL_DEBUG_INFO
4226 ("uCode init data len %d too large to fit in\n",
4227 init_data_size);
4228 ret = -EINVAL;
4229 goto err_release;
4230 }
4231 if (boot_size > priv->hw_params.max_bsm_size) {
4232 IWL_DEBUG_INFO
4233 ("uCode boot instr len %d too large to fit in\n",
4234 boot_size);
4235 ret = -EINVAL;
4236 goto err_release;
4237 }
4238
4239 /* Allocate ucode buffers for card's bus-master loading ... */
4240
4241 /* Runtime instructions and 2 copies of data:
4242 * 1) unmodified from disk
4243 * 2) backup cache for save/restore during power-downs */
4244 priv->ucode_code.len = inst_size;
4245 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
4246
4247 priv->ucode_data.len = data_size;
4248 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
4249
4250 priv->ucode_data_backup.len = data_size;
4251 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4252
4253 /* Initialization instructions and data */
4254 if (init_size && init_data_size) {
4255 priv->ucode_init.len = init_size;
4256 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
4257
4258 priv->ucode_init_data.len = init_data_size;
4259 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4260
4261 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4262 goto err_pci_alloc;
4263 }
4264
4265 /* Bootstrap (instructions only, no data) */
4266 if (boot_size) {
4267 priv->ucode_boot.len = boot_size;
4268 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
4269
4270 if (!priv->ucode_boot.v_addr)
4271 goto err_pci_alloc;
4272 }
4273
4274 /* Copy images into buffers for card's bus-master reads ... */
4275
4276 /* Runtime instructions (first block of data in file) */
4277 src = &ucode->data[0];
4278 len = priv->ucode_code.len;
4279 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
4280 memcpy(priv->ucode_code.v_addr, src, len);
4281 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4282 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4283
4284 /* Runtime data (2nd block)
4285 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
4286 src = &ucode->data[inst_size];
4287 len = priv->ucode_data.len;
4288 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
4289 memcpy(priv->ucode_data.v_addr, src, len);
4290 memcpy(priv->ucode_data_backup.v_addr, src, len);
4291
4292 /* Initialization instructions (3rd block) */
4293 if (init_size) {
4294 src = &ucode->data[inst_size + data_size];
4295 len = priv->ucode_init.len;
4296 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4297 len);
4298 memcpy(priv->ucode_init.v_addr, src, len);
4299 }
4300
4301 /* Initialization data (4th block) */
4302 if (init_data_size) {
4303 src = &ucode->data[inst_size + data_size + init_size];
4304 len = priv->ucode_init_data.len;
4305 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
4306 len);
4307 memcpy(priv->ucode_init_data.v_addr, src, len);
4308 }
4309
4310 /* Bootstrap instructions (5th block) */
4311 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
4312 len = priv->ucode_boot.len;
4313 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
4314 memcpy(priv->ucode_boot.v_addr, src, len);
4315
4316 /* We have our copies now, allow OS release its copies */
4317 release_firmware(ucode_raw);
4318 return 0;
4319
4320 err_pci_alloc:
4321 IWL_ERROR("failed to allocate pci memory\n");
4322 ret = -ENOMEM;
4323 iwl4965_dealloc_ucode_pci(priv);
4324
4325 err_release:
4326 release_firmware(ucode_raw);
4327
4328 error:
4329 return ret;
4330 }
4331
4332
4333 /**
4334 * iwl4965_set_ucode_ptrs - Set uCode address location
4335 *
4336 * Tell initialization uCode where to find runtime uCode.
4337 *
4338 * BSM registers initially contain pointers to initialization uCode.
4339 * We need to replace them to load runtime uCode inst and data,
4340 * and to save runtime data when powering down.
4341 */
4342 static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
4343 {
4344 dma_addr_t pinst;
4345 dma_addr_t pdata;
4346 int rc = 0;
4347 unsigned long flags;
4348
4349 /* bits 35:4 for 4965 */
4350 pinst = priv->ucode_code.p_addr >> 4;
4351 pdata = priv->ucode_data_backup.p_addr >> 4;
4352
4353 spin_lock_irqsave(&priv->lock, flags);
4354 rc = iwl_grab_nic_access(priv);
4355 if (rc) {
4356 spin_unlock_irqrestore(&priv->lock, flags);
4357 return rc;
4358 }
4359
4360 /* Tell bootstrap uCode where to find image to load */
4361 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
4362 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
4363 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
4364 priv->ucode_data.len);
4365
4366 /* Inst bytecount must be last to set up, bit 31 signals uCode
4367 * that all new ptr/size info is in place */
4368 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
4369 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
4370
4371 iwl_release_nic_access(priv);
4372
4373 spin_unlock_irqrestore(&priv->lock, flags);
4374
4375 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
4376
4377 return rc;
4378 }
4379
4380 /**
4381 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
4382 *
4383 * Called after REPLY_ALIVE notification received from "initialize" uCode.
4384 *
4385 * The 4965 "initialize" ALIVE reply contains calibration data for:
4386 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
4387 * (3945 does not contain this data).
4388 *
4389 * Tell "initialize" uCode to go ahead and load the runtime uCode.
4390 */
4391 static void iwl4965_init_alive_start(struct iwl_priv *priv)
4392 {
4393 /* Check alive response for "valid" sign from uCode */
4394 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
4395 /* We had an error bringing up the hardware, so take it
4396 * all the way back down so we can try again */
4397 IWL_DEBUG_INFO("Initialize Alive failed.\n");
4398 goto restart;
4399 }
4400
4401 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
4402 * This is a paranoid check, because we would not have gotten the
4403 * "initialize" alive if code weren't properly loaded. */
4404 if (iwl_verify_ucode(priv)) {
4405 /* Runtime instruction load was bad;
4406 * take it all the way back down so we can try again */
4407 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
4408 goto restart;
4409 }
4410
4411 /* Calculate temperature */
4412 priv->temperature = iwl4965_get_temperature(priv);
4413
4414 /* Send pointers to protocol/runtime uCode image ... init code will
4415 * load and launch runtime uCode, which will send us another "Alive"
4416 * notification. */
4417 IWL_DEBUG_INFO("Initialization Alive received.\n");
4418 if (iwl4965_set_ucode_ptrs(priv)) {
4419 /* Runtime instruction load won't happen;
4420 * take it all the way back down so we can try again */
4421 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
4422 goto restart;
4423 }
4424 return;
4425
4426 restart:
4427 queue_work(priv->workqueue, &priv->restart);
4428 }
4429
4430
4431 /**
4432 * iwl4965_alive_start - called after REPLY_ALIVE notification received
4433 * from protocol/runtime uCode (initialization uCode's
4434 * Alive gets handled by iwl4965_init_alive_start()).
4435 */
4436 static void iwl4965_alive_start(struct iwl_priv *priv)
4437 {
4438 int ret = 0;
4439
4440 IWL_DEBUG_INFO("Runtime Alive received.\n");
4441
4442 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
4443 /* We had an error bringing up the hardware, so take it
4444 * all the way back down so we can try again */
4445 IWL_DEBUG_INFO("Alive failed.\n");
4446 goto restart;
4447 }
4448
4449 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
4450 * This is a paranoid check, because we would not have gotten the
4451 * "runtime" alive if code weren't properly loaded. */
4452 if (iwl_verify_ucode(priv)) {
4453 /* Runtime instruction load was bad;
4454 * take it all the way back down so we can try again */
4455 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
4456 goto restart;
4457 }
4458
4459 iwlcore_clear_stations_table(priv);
4460
4461 ret = priv->cfg->ops->lib->alive_notify(priv);
4462 if (ret) {
4463 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
4464 ret);
4465 goto restart;
4466 }
4467
4468 /* After the ALIVE response, we can send host commands to 4965 uCode */
4469 set_bit(STATUS_ALIVE, &priv->status);
4470
4471 /* Clear out the uCode error bit if it is set */
4472 clear_bit(STATUS_FW_ERROR, &priv->status);
4473
4474 if (iwl_is_rfkill(priv))
4475 return;
4476
4477 ieee80211_start_queues(priv->hw);
4478
4479 priv->active_rate = priv->rates_mask;
4480 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
4481
4482 if (iwl_is_associated(priv)) {
4483 struct iwl4965_rxon_cmd *active_rxon =
4484 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
4485
4486 memcpy(&priv->staging_rxon, &priv->active_rxon,
4487 sizeof(priv->staging_rxon));
4488 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4489 } else {
4490 /* Initialize our rx_config data */
4491 iwl4965_connection_init_rx_config(priv);
4492 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
4493 }
4494
4495 /* Configure Bluetooth device coexistence support */
4496 iwl4965_send_bt_config(priv);
4497
4498 /* Configure the adapter for unassociated operation */
4499 iwl4965_commit_rxon(priv);
4500
4501 /* At this point, the NIC is initialized and operational */
4502 priv->notif_missed_beacons = 0;
4503
4504 iwl4965_rf_kill_ct_config(priv);
4505
4506 iwl_leds_register(priv);
4507
4508 IWL_DEBUG_INFO("ALIVE processing complete.\n");
4509 set_bit(STATUS_READY, &priv->status);
4510 wake_up_interruptible(&priv->wait_command_queue);
4511
4512 if (priv->error_recovering)
4513 iwl4965_error_recovery(priv);
4514
4515 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
4516 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
4517 return;
4518
4519 restart:
4520 queue_work(priv->workqueue, &priv->restart);
4521 }
4522
4523 static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
4524
4525 static void __iwl4965_down(struct iwl_priv *priv)
4526 {
4527 unsigned long flags;
4528 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
4529 struct ieee80211_conf *conf = NULL;
4530
4531 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
4532
4533 conf = ieee80211_get_hw_conf(priv->hw);
4534
4535 if (!exit_pending)
4536 set_bit(STATUS_EXIT_PENDING, &priv->status);
4537
4538 iwl_leds_unregister(priv);
4539
4540 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
4541
4542 iwlcore_clear_stations_table(priv);
4543
4544 /* Unblock any waiting calls */
4545 wake_up_interruptible_all(&priv->wait_command_queue);
4546
4547 /* Wipe out the EXIT_PENDING status bit if we are not actually
4548 * exiting the module */
4549 if (!exit_pending)
4550 clear_bit(STATUS_EXIT_PENDING, &priv->status);
4551
4552 /* stop and reset the on-board processor */
4553 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
4554
4555 /* tell the device to stop sending interrupts */
4556 spin_lock_irqsave(&priv->lock, flags);
4557 iwl4965_disable_interrupts(priv);
4558 spin_unlock_irqrestore(&priv->lock, flags);
4559 iwl_synchronize_irq(priv);
4560
4561 if (priv->mac80211_registered)
4562 ieee80211_stop_queues(priv->hw);
4563
4564 /* If we have not previously called iwl4965_init() then
4565 * clear all bits but the RF Kill and SUSPEND bits and return */
4566 if (!iwl_is_init(priv)) {
4567 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4568 STATUS_RF_KILL_HW |
4569 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4570 STATUS_RF_KILL_SW |
4571 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4572 STATUS_GEO_CONFIGURED |
4573 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4574 STATUS_IN_SUSPEND;
4575 goto exit;
4576 }
4577
4578 /* ...otherwise clear out all the status bits but the RF Kill and
4579 * SUSPEND bits and continue taking the NIC down. */
4580 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
4581 STATUS_RF_KILL_HW |
4582 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
4583 STATUS_RF_KILL_SW |
4584 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
4585 STATUS_GEO_CONFIGURED |
4586 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
4587 STATUS_IN_SUSPEND |
4588 test_bit(STATUS_FW_ERROR, &priv->status) <<
4589 STATUS_FW_ERROR;
4590
4591 spin_lock_irqsave(&priv->lock, flags);
4592 iwl_clear_bit(priv, CSR_GP_CNTRL,
4593 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4594 spin_unlock_irqrestore(&priv->lock, flags);
4595
4596 iwl4965_hw_txq_ctx_stop(priv);
4597 iwl4965_hw_rxq_stop(priv);
4598
4599 spin_lock_irqsave(&priv->lock, flags);
4600 if (!iwl_grab_nic_access(priv)) {
4601 iwl_write_prph(priv, APMG_CLK_DIS_REG,
4602 APMG_CLK_VAL_DMA_CLK_RQT);
4603 iwl_release_nic_access(priv);
4604 }
4605 spin_unlock_irqrestore(&priv->lock, flags);
4606
4607 udelay(5);
4608
4609 iwl4965_hw_nic_stop_master(priv);
4610 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
4611 iwl4965_hw_nic_reset(priv);
4612 priv->cfg->ops->lib->free_shared_mem(priv);
4613
4614 exit:
4615 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
4616
4617 if (priv->ibss_beacon)
4618 dev_kfree_skb(priv->ibss_beacon);
4619 priv->ibss_beacon = NULL;
4620
4621 /* clear out any free frames */
4622 iwl4965_clear_free_frames(priv);
4623 }
4624
4625 static void iwl4965_down(struct iwl_priv *priv)
4626 {
4627 mutex_lock(&priv->mutex);
4628 __iwl4965_down(priv);
4629 mutex_unlock(&priv->mutex);
4630
4631 iwl4965_cancel_deferred_work(priv);
4632 }
4633
4634 #define MAX_HW_RESTARTS 5
4635
4636 static int __iwl4965_up(struct iwl_priv *priv)
4637 {
4638 int i;
4639 int ret;
4640
4641 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4642 IWL_WARNING("Exit pending; will not bring the NIC up\n");
4643 return -EIO;
4644 }
4645
4646 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
4647 IWL_WARNING("Radio disabled by SW RF kill (module "
4648 "parameter)\n");
4649 iwl_rfkill_set_hw_state(priv);
4650 return -ENODEV;
4651 }
4652
4653 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
4654 IWL_ERROR("ucode not available for device bringup\n");
4655 return -EIO;
4656 }
4657
4658 /* If platform's RF_KILL switch is NOT set to KILL */
4659 if (iwl_read32(priv, CSR_GP_CNTRL) &
4660 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
4661 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4662 else {
4663 set_bit(STATUS_RF_KILL_HW, &priv->status);
4664 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
4665 iwl_rfkill_set_hw_state(priv);
4666 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
4667 return -ENODEV;
4668 }
4669 }
4670
4671 iwl_rfkill_set_hw_state(priv);
4672 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
4673
4674 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
4675 if (ret) {
4676 IWL_ERROR("Unable to allocate shared memory\n");
4677 return ret;
4678 }
4679
4680 ret = priv->cfg->ops->lib->hw_nic_init(priv);
4681 if (ret) {
4682 IWL_ERROR("Unable to init nic\n");
4683 return ret;
4684 }
4685
4686 /* make sure rfkill handshake bits are cleared */
4687 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4688 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
4689 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4690
4691 /* clear (again), then enable host interrupts */
4692 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
4693 iwl4965_enable_interrupts(priv);
4694
4695 /* really make sure rfkill handshake bits are cleared */
4696 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4697 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
4698
4699 /* Copy original ucode data image from disk into backup cache.
4700 * This will be used to initialize the on-board processor's
4701 * data SRAM for a clean start when the runtime program first loads. */
4702 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
4703 priv->ucode_data.len);
4704
4705 /* We return success when we resume from suspend and rf_kill is on. */
4706 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
4707 return 0;
4708
4709 for (i = 0; i < MAX_HW_RESTARTS; i++) {
4710
4711 iwlcore_clear_stations_table(priv);
4712
4713 /* load bootstrap state machine,
4714 * load bootstrap program into processor's memory,
4715 * prepare to load the "initialize" uCode */
4716 ret = priv->cfg->ops->lib->load_ucode(priv);
4717
4718 if (ret) {
4719 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
4720 continue;
4721 }
4722
4723 /* start card; "initialize" will load runtime ucode */
4724 iwl4965_nic_start(priv);
4725
4726 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
4727
4728 return 0;
4729 }
4730
4731 set_bit(STATUS_EXIT_PENDING, &priv->status);
4732 __iwl4965_down(priv);
4733
4734 /* tried to restart and config the device for as long as our
4735 * patience could withstand */
4736 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
4737 return -EIO;
4738 }
4739
4740
4741 /*****************************************************************************
4742 *
4743 * Workqueue callbacks
4744 *
4745 *****************************************************************************/
4746
4747 static void iwl4965_bg_init_alive_start(struct work_struct *data)
4748 {
4749 struct iwl_priv *priv =
4750 container_of(data, struct iwl_priv, init_alive_start.work);
4751
4752 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4753 return;
4754
4755 mutex_lock(&priv->mutex);
4756 iwl4965_init_alive_start(priv);
4757 mutex_unlock(&priv->mutex);
4758 }
4759
4760 static void iwl4965_bg_alive_start(struct work_struct *data)
4761 {
4762 struct iwl_priv *priv =
4763 container_of(data, struct iwl_priv, alive_start.work);
4764
4765 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4766 return;
4767
4768 mutex_lock(&priv->mutex);
4769 iwl4965_alive_start(priv);
4770 mutex_unlock(&priv->mutex);
4771 }
4772
4773 static void iwl4965_bg_rf_kill(struct work_struct *work)
4774 {
4775 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
4776
4777 wake_up_interruptible(&priv->wait_command_queue);
4778
4779 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4780 return;
4781
4782 mutex_lock(&priv->mutex);
4783
4784 if (!iwl_is_rfkill(priv)) {
4785 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
4786 "HW and/or SW RF Kill no longer active, restarting "
4787 "device\n");
4788 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4789 queue_work(priv->workqueue, &priv->restart);
4790 } else {
4791 /* make sure mac80211 stop sending Tx frame */
4792 if (priv->mac80211_registered)
4793 ieee80211_stop_queues(priv->hw);
4794
4795 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
4796 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
4797 "disabled by SW switch\n");
4798 else
4799 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
4800 "Kill switch must be turned off for "
4801 "wireless networking to work.\n");
4802 }
4803 iwl_rfkill_set_hw_state(priv);
4804
4805 mutex_unlock(&priv->mutex);
4806 }
4807
4808 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
4809
4810 static void iwl4965_bg_scan_check(struct work_struct *data)
4811 {
4812 struct iwl_priv *priv =
4813 container_of(data, struct iwl_priv, scan_check.work);
4814
4815 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4816 return;
4817
4818 mutex_lock(&priv->mutex);
4819 if (test_bit(STATUS_SCANNING, &priv->status) ||
4820 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4821 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
4822 "Scan completion watchdog resetting adapter (%dms)\n",
4823 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
4824
4825 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
4826 iwl4965_send_scan_abort(priv);
4827 }
4828 mutex_unlock(&priv->mutex);
4829 }
4830
4831 static void iwl4965_bg_request_scan(struct work_struct *data)
4832 {
4833 struct iwl_priv *priv =
4834 container_of(data, struct iwl_priv, request_scan);
4835 struct iwl_host_cmd cmd = {
4836 .id = REPLY_SCAN_CMD,
4837 .len = sizeof(struct iwl4965_scan_cmd),
4838 .meta.flags = CMD_SIZE_HUGE,
4839 };
4840 struct iwl4965_scan_cmd *scan;
4841 struct ieee80211_conf *conf = NULL;
4842 u16 cmd_len;
4843 enum ieee80211_band band;
4844 u8 direct_mask;
4845 int ret = 0;
4846
4847 conf = ieee80211_get_hw_conf(priv->hw);
4848
4849 mutex_lock(&priv->mutex);
4850
4851 if (!iwl_is_ready(priv)) {
4852 IWL_WARNING("request scan called when driver not ready.\n");
4853 goto done;
4854 }
4855
4856 /* Make sure the scan wasn't cancelled before this queued work
4857 * was given the chance to run... */
4858 if (!test_bit(STATUS_SCANNING, &priv->status))
4859 goto done;
4860
4861 /* This should never be called or scheduled if there is currently
4862 * a scan active in the hardware. */
4863 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
4864 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
4865 "Ignoring second request.\n");
4866 ret = -EIO;
4867 goto done;
4868 }
4869
4870 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4871 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
4872 goto done;
4873 }
4874
4875 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
4876 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
4877 goto done;
4878 }
4879
4880 if (iwl_is_rfkill(priv)) {
4881 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
4882 goto done;
4883 }
4884
4885 if (!test_bit(STATUS_READY, &priv->status)) {
4886 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
4887 goto done;
4888 }
4889
4890 if (!priv->scan_bands) {
4891 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
4892 goto done;
4893 }
4894
4895 if (!priv->scan) {
4896 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
4897 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
4898 if (!priv->scan) {
4899 ret = -ENOMEM;
4900 goto done;
4901 }
4902 }
4903 scan = priv->scan;
4904 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
4905
4906 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
4907 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
4908
4909 if (iwl_is_associated(priv)) {
4910 u16 interval = 0;
4911 u32 extra;
4912 u32 suspend_time = 100;
4913 u32 scan_suspend_time = 100;
4914 unsigned long flags;
4915
4916 IWL_DEBUG_INFO("Scanning while associated...\n");
4917
4918 spin_lock_irqsave(&priv->lock, flags);
4919 interval = priv->beacon_int;
4920 spin_unlock_irqrestore(&priv->lock, flags);
4921
4922 scan->suspend_time = 0;
4923 scan->max_out_time = cpu_to_le32(200 * 1024);
4924 if (!interval)
4925 interval = suspend_time;
4926
4927 extra = (suspend_time / interval) << 22;
4928 scan_suspend_time = (extra |
4929 ((suspend_time % interval) * 1024));
4930 scan->suspend_time = cpu_to_le32(scan_suspend_time);
4931 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
4932 scan_suspend_time, interval);
4933 }
4934
4935 /* We should add the ability for user to lock to PASSIVE ONLY */
4936 if (priv->one_direct_scan) {
4937 IWL_DEBUG_SCAN
4938 ("Kicking off one direct scan for '%s'\n",
4939 iwl4965_escape_essid(priv->direct_ssid,
4940 priv->direct_ssid_len));
4941 scan->direct_scan[0].id = WLAN_EID_SSID;
4942 scan->direct_scan[0].len = priv->direct_ssid_len;
4943 memcpy(scan->direct_scan[0].ssid,
4944 priv->direct_ssid, priv->direct_ssid_len);
4945 direct_mask = 1;
4946 } else if (!iwl_is_associated(priv) && priv->essid_len) {
4947 IWL_DEBUG_SCAN
4948 ("Kicking off one direct scan for '%s' when not associated\n",
4949 iwl4965_escape_essid(priv->essid, priv->essid_len));
4950 scan->direct_scan[0].id = WLAN_EID_SSID;
4951 scan->direct_scan[0].len = priv->essid_len;
4952 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
4953 direct_mask = 1;
4954 } else {
4955 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
4956 direct_mask = 0;
4957 }
4958
4959 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
4960 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
4961 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
4962
4963
4964 switch (priv->scan_bands) {
4965 case 2:
4966 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
4967 scan->tx_cmd.rate_n_flags =
4968 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
4969 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
4970
4971 scan->good_CRC_th = 0;
4972 band = IEEE80211_BAND_2GHZ;
4973 break;
4974
4975 case 1:
4976 scan->tx_cmd.rate_n_flags =
4977 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
4978 RATE_MCS_ANT_B_MSK);
4979 scan->good_CRC_th = IWL_GOOD_CRC_TH;
4980 band = IEEE80211_BAND_5GHZ;
4981 break;
4982
4983 default:
4984 IWL_WARNING("Invalid scan band count\n");
4985 goto done;
4986 }
4987
4988 /* We don't build a direct scan probe request; the uCode will do
4989 * that based on the direct_mask added to each channel entry */
4990 cmd_len = iwl4965_fill_probe_req(priv, band,
4991 (struct ieee80211_mgmt *)scan->data,
4992 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
4993
4994 scan->tx_cmd.len = cpu_to_le16(cmd_len);
4995 /* select Rx chains */
4996
4997 /* Force use of chains B and C (0x6) for scan Rx.
4998 * Avoid A (0x1) because of its off-channel reception on A-band.
4999 * MIMO is not used here, but value is required to make uCode happy. */
5000 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
5001 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
5002 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
5003 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
5004
5005 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
5006 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5007
5008 if (direct_mask)
5009 scan->channel_count =
5010 iwl4965_get_channels_for_scan(
5011 priv, band, 1, /* active */
5012 direct_mask,
5013 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
5014 else
5015 scan->channel_count =
5016 iwl4965_get_channels_for_scan(
5017 priv, band, 0, /* passive */
5018 direct_mask,
5019 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
5020
5021 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
5022 RXON_FILTER_BCON_AWARE_MSK);
5023 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
5024 scan->channel_count * sizeof(struct iwl4965_scan_channel);
5025 cmd.data = scan;
5026 scan->len = cpu_to_le16(cmd.len);
5027
5028 set_bit(STATUS_SCAN_HW, &priv->status);
5029 ret = iwl_send_cmd_sync(priv, &cmd);
5030 if (ret)
5031 goto done;
5032
5033 queue_delayed_work(priv->workqueue, &priv->scan_check,
5034 IWL_SCAN_CHECK_WATCHDOG);
5035
5036 mutex_unlock(&priv->mutex);
5037 return;
5038
5039 done:
5040 /* inform mac80211 scan aborted */
5041 queue_work(priv->workqueue, &priv->scan_completed);
5042 mutex_unlock(&priv->mutex);
5043 }
5044
5045 static void iwl4965_bg_up(struct work_struct *data)
5046 {
5047 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
5048
5049 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5050 return;
5051
5052 mutex_lock(&priv->mutex);
5053 __iwl4965_up(priv);
5054 mutex_unlock(&priv->mutex);
5055 }
5056
5057 static void iwl4965_bg_restart(struct work_struct *data)
5058 {
5059 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
5060
5061 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5062 return;
5063
5064 iwl4965_down(priv);
5065 queue_work(priv->workqueue, &priv->up);
5066 }
5067
5068 static void iwl4965_bg_rx_replenish(struct work_struct *data)
5069 {
5070 struct iwl_priv *priv =
5071 container_of(data, struct iwl_priv, rx_replenish);
5072
5073 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5074 return;
5075
5076 mutex_lock(&priv->mutex);
5077 iwl_rx_replenish(priv);
5078 mutex_unlock(&priv->mutex);
5079 }
5080
5081 #define IWL_DELAY_NEXT_SCAN (HZ*2)
5082
5083 static void iwl4965_post_associate(struct iwl_priv *priv)
5084 {
5085 struct ieee80211_conf *conf = NULL;
5086 int ret = 0;
5087 DECLARE_MAC_BUF(mac);
5088
5089 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5090 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
5091 return;
5092 }
5093
5094 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
5095 priv->assoc_id,
5096 print_mac(mac, priv->active_rxon.bssid_addr));
5097
5098
5099 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5100 return;
5101
5102
5103 if (!priv->vif || !priv->is_open)
5104 return;
5105
5106 iwl4965_scan_cancel_timeout(priv, 200);
5107
5108 conf = ieee80211_get_hw_conf(priv->hw);
5109
5110 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5111 iwl4965_commit_rxon(priv);
5112
5113 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5114 iwl4965_setup_rxon_timing(priv);
5115 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
5116 sizeof(priv->rxon_timing), &priv->rxon_timing);
5117 if (ret)
5118 IWL_WARNING("REPLY_RXON_TIMING failed - "
5119 "Attempting to continue.\n");
5120
5121 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
5122
5123 #ifdef CONFIG_IWL4965_HT
5124 if (priv->current_ht_config.is_ht)
5125 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
5126 #endif /* CONFIG_IWL4965_HT*/
5127 iwl_set_rxon_chain(priv);
5128 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5129
5130 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5131 priv->assoc_id, priv->beacon_int);
5132
5133 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5134 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5135 else
5136 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5137
5138 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5139 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
5140 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
5141 else
5142 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5143
5144 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5145 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
5146
5147 }
5148
5149 iwl4965_commit_rxon(priv);
5150
5151 switch (priv->iw_mode) {
5152 case IEEE80211_IF_TYPE_STA:
5153 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
5154 break;
5155
5156 case IEEE80211_IF_TYPE_IBSS:
5157
5158 /* clear out the station table */
5159 iwlcore_clear_stations_table(priv);
5160
5161 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
5162 iwl4965_rxon_add_station(priv, priv->bssid, 0);
5163 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
5164 iwl4965_send_beacon_cmd(priv);
5165
5166 break;
5167
5168 default:
5169 IWL_ERROR("%s Should not be called in %d mode\n",
5170 __FUNCTION__, priv->iw_mode);
5171 break;
5172 }
5173
5174 iwl4965_sequence_reset(priv);
5175
5176 /* Enable Rx differential gain and sensitivity calibrations */
5177 iwl_chain_noise_reset(priv);
5178 priv->start_calib = 1;
5179
5180 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5181 priv->assoc_station_added = 1;
5182
5183 iwl4965_activate_qos(priv, 0);
5184
5185 iwl_power_update_mode(priv, 0);
5186 /* we have just associated, don't start scan too early */
5187 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
5188 }
5189
5190
5191 static void iwl4965_bg_post_associate(struct work_struct *data)
5192 {
5193 struct iwl_priv *priv = container_of(data, struct iwl_priv,
5194 post_associate.work);
5195
5196 mutex_lock(&priv->mutex);
5197 iwl4965_post_associate(priv);
5198 mutex_unlock(&priv->mutex);
5199
5200 }
5201
5202 static void iwl4965_bg_abort_scan(struct work_struct *work)
5203 {
5204 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
5205
5206 if (!iwl_is_ready(priv))
5207 return;
5208
5209 mutex_lock(&priv->mutex);
5210
5211 set_bit(STATUS_SCAN_ABORTING, &priv->status);
5212 iwl4965_send_scan_abort(priv);
5213
5214 mutex_unlock(&priv->mutex);
5215 }
5216
5217 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
5218
5219 static void iwl4965_bg_scan_completed(struct work_struct *work)
5220 {
5221 struct iwl_priv *priv =
5222 container_of(work, struct iwl_priv, scan_completed);
5223
5224 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5225
5226 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5227 return;
5228
5229 if (test_bit(STATUS_CONF_PENDING, &priv->status))
5230 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
5231
5232 ieee80211_scan_completed(priv->hw);
5233
5234 /* Since setting the TXPOWER may have been deferred while
5235 * performing the scan, fire one off */
5236 mutex_lock(&priv->mutex);
5237 iwl4965_hw_reg_send_txpower(priv);
5238 mutex_unlock(&priv->mutex);
5239 }
5240
5241 /*****************************************************************************
5242 *
5243 * mac80211 entry point functions
5244 *
5245 *****************************************************************************/
5246
5247 #define UCODE_READY_TIMEOUT (2 * HZ)
5248
5249 static int iwl4965_mac_start(struct ieee80211_hw *hw)
5250 {
5251 struct iwl_priv *priv = hw->priv;
5252 int ret;
5253
5254 IWL_DEBUG_MAC80211("enter\n");
5255
5256 if (pci_enable_device(priv->pci_dev)) {
5257 IWL_ERROR("Fail to pci_enable_device\n");
5258 return -ENODEV;
5259 }
5260 pci_restore_state(priv->pci_dev);
5261 pci_enable_msi(priv->pci_dev);
5262
5263 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
5264 DRV_NAME, priv);
5265 if (ret) {
5266 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
5267 goto out_disable_msi;
5268 }
5269
5270 /* we should be verifying the device is ready to be opened */
5271 mutex_lock(&priv->mutex);
5272
5273 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
5274 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5275 * ucode filename and max sizes are card-specific. */
5276
5277 if (!priv->ucode_code.len) {
5278 ret = iwl4965_read_ucode(priv);
5279 if (ret) {
5280 IWL_ERROR("Could not read microcode: %d\n", ret);
5281 mutex_unlock(&priv->mutex);
5282 goto out_release_irq;
5283 }
5284 }
5285
5286 ret = __iwl4965_up(priv);
5287
5288 mutex_unlock(&priv->mutex);
5289
5290 if (ret)
5291 goto out_release_irq;
5292
5293 IWL_DEBUG_INFO("Start UP work done.\n");
5294
5295 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5296 return 0;
5297
5298 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5299 * mac80211 will not be run successfully. */
5300 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5301 test_bit(STATUS_READY, &priv->status),
5302 UCODE_READY_TIMEOUT);
5303 if (!ret) {
5304 if (!test_bit(STATUS_READY, &priv->status)) {
5305 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
5306 jiffies_to_msecs(UCODE_READY_TIMEOUT));
5307 ret = -ETIMEDOUT;
5308 goto out_release_irq;
5309 }
5310 }
5311
5312 priv->is_open = 1;
5313 IWL_DEBUG_MAC80211("leave\n");
5314 return 0;
5315
5316 out_release_irq:
5317 free_irq(priv->pci_dev->irq, priv);
5318 out_disable_msi:
5319 pci_disable_msi(priv->pci_dev);
5320 pci_disable_device(priv->pci_dev);
5321 priv->is_open = 0;
5322 IWL_DEBUG_MAC80211("leave - failed\n");
5323 return ret;
5324 }
5325
5326 static void iwl4965_mac_stop(struct ieee80211_hw *hw)
5327 {
5328 struct iwl_priv *priv = hw->priv;
5329
5330 IWL_DEBUG_MAC80211("enter\n");
5331
5332 if (!priv->is_open) {
5333 IWL_DEBUG_MAC80211("leave - skip\n");
5334 return;
5335 }
5336
5337 priv->is_open = 0;
5338
5339 if (iwl_is_ready_rf(priv)) {
5340 /* stop mac, cancel any scan request and clear
5341 * RXON_FILTER_ASSOC_MSK BIT
5342 */
5343 mutex_lock(&priv->mutex);
5344 iwl4965_scan_cancel_timeout(priv, 100);
5345 cancel_delayed_work(&priv->post_associate);
5346 mutex_unlock(&priv->mutex);
5347 }
5348
5349 iwl4965_down(priv);
5350
5351 flush_workqueue(priv->workqueue);
5352 free_irq(priv->pci_dev->irq, priv);
5353 pci_disable_msi(priv->pci_dev);
5354 pci_save_state(priv->pci_dev);
5355 pci_disable_device(priv->pci_dev);
5356
5357 IWL_DEBUG_MAC80211("leave\n");
5358 }
5359
5360 static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
5361 struct ieee80211_tx_control *ctl)
5362 {
5363 struct iwl_priv *priv = hw->priv;
5364
5365 IWL_DEBUG_MAC80211("enter\n");
5366
5367 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
5368 IWL_DEBUG_MAC80211("leave - monitor\n");
5369 return -1;
5370 }
5371
5372 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
5373 ctl->tx_rate->bitrate);
5374
5375 if (iwl4965_tx_skb(priv, skb, ctl))
5376 dev_kfree_skb_any(skb);
5377
5378 IWL_DEBUG_MAC80211("leave\n");
5379 return 0;
5380 }
5381
5382 static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
5383 struct ieee80211_if_init_conf *conf)
5384 {
5385 struct iwl_priv *priv = hw->priv;
5386 unsigned long flags;
5387 DECLARE_MAC_BUF(mac);
5388
5389 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
5390
5391 if (priv->vif) {
5392 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
5393 return -EOPNOTSUPP;
5394 }
5395
5396 spin_lock_irqsave(&priv->lock, flags);
5397 priv->vif = conf->vif;
5398
5399 spin_unlock_irqrestore(&priv->lock, flags);
5400
5401 mutex_lock(&priv->mutex);
5402
5403 if (conf->mac_addr) {
5404 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
5405 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
5406 }
5407
5408 if (iwl_is_ready(priv))
5409 iwl4965_set_mode(priv, conf->type);
5410
5411 mutex_unlock(&priv->mutex);
5412
5413 IWL_DEBUG_MAC80211("leave\n");
5414 return 0;
5415 }
5416
5417 /**
5418 * iwl4965_mac_config - mac80211 config callback
5419 *
5420 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
5421 * be set inappropriately and the driver currently sets the hardware up to
5422 * use it whenever needed.
5423 */
5424 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
5425 {
5426 struct iwl_priv *priv = hw->priv;
5427 const struct iwl_channel_info *ch_info;
5428 unsigned long flags;
5429 int ret = 0;
5430
5431 mutex_lock(&priv->mutex);
5432 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
5433
5434 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
5435
5436 if (!iwl_is_ready(priv)) {
5437 IWL_DEBUG_MAC80211("leave - not ready\n");
5438 ret = -EIO;
5439 goto out;
5440 }
5441
5442 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
5443 test_bit(STATUS_SCANNING, &priv->status))) {
5444 IWL_DEBUG_MAC80211("leave - scanning\n");
5445 set_bit(STATUS_CONF_PENDING, &priv->status);
5446 mutex_unlock(&priv->mutex);
5447 return 0;
5448 }
5449
5450 spin_lock_irqsave(&priv->lock, flags);
5451
5452 ch_info = iwl_get_channel_info(priv, conf->channel->band,
5453 ieee80211_frequency_to_channel(conf->channel->center_freq));
5454 if (!is_channel_valid(ch_info)) {
5455 IWL_DEBUG_MAC80211("leave - invalid channel\n");
5456 spin_unlock_irqrestore(&priv->lock, flags);
5457 ret = -EINVAL;
5458 goto out;
5459 }
5460
5461 #ifdef CONFIG_IWL4965_HT
5462 /* if we are switching from ht to 2.4 clear flags
5463 * from any ht related info since 2.4 does not
5464 * support ht */
5465 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
5466 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
5467 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
5468 #endif
5469 )
5470 priv->staging_rxon.flags = 0;
5471 #endif /* CONFIG_IWL4965_HT */
5472
5473 iwl_set_rxon_channel(priv, conf->channel->band,
5474 ieee80211_frequency_to_channel(conf->channel->center_freq));
5475
5476 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
5477
5478 /* The list of supported rates and rate mask can be different
5479 * for each band; since the band may have changed, reset
5480 * the rate mask to what mac80211 lists */
5481 iwl4965_set_rate(priv);
5482
5483 spin_unlock_irqrestore(&priv->lock, flags);
5484
5485 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
5486 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
5487 iwl4965_hw_channel_switch(priv, conf->channel);
5488 goto out;
5489 }
5490 #endif
5491
5492 if (priv->cfg->ops->lib->radio_kill_sw)
5493 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
5494
5495 if (!conf->radio_enabled) {
5496 IWL_DEBUG_MAC80211("leave - radio disabled\n");
5497 goto out;
5498 }
5499
5500 if (iwl_is_rfkill(priv)) {
5501 IWL_DEBUG_MAC80211("leave - RF kill\n");
5502 ret = -EIO;
5503 goto out;
5504 }
5505
5506 iwl4965_set_rate(priv);
5507
5508 if (memcmp(&priv->active_rxon,
5509 &priv->staging_rxon, sizeof(priv->staging_rxon)))
5510 iwl4965_commit_rxon(priv);
5511 else
5512 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
5513
5514 IWL_DEBUG_MAC80211("leave\n");
5515
5516 out:
5517 clear_bit(STATUS_CONF_PENDING, &priv->status);
5518 mutex_unlock(&priv->mutex);
5519 return ret;
5520 }
5521
5522 static void iwl4965_config_ap(struct iwl_priv *priv)
5523 {
5524 int ret = 0;
5525
5526 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5527 return;
5528
5529 /* The following should be done only at AP bring up */
5530 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
5531
5532 /* RXON - unassoc (to set timing command) */
5533 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5534 iwl4965_commit_rxon(priv);
5535
5536 /* RXON Timing */
5537 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
5538 iwl4965_setup_rxon_timing(priv);
5539 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
5540 sizeof(priv->rxon_timing), &priv->rxon_timing);
5541 if (ret)
5542 IWL_WARNING("REPLY_RXON_TIMING failed - "
5543 "Attempting to continue.\n");
5544
5545 iwl_set_rxon_chain(priv);
5546
5547 /* FIXME: what should be the assoc_id for AP? */
5548 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
5549 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
5550 priv->staging_rxon.flags |=
5551 RXON_FLG_SHORT_PREAMBLE_MSK;
5552 else
5553 priv->staging_rxon.flags &=
5554 ~RXON_FLG_SHORT_PREAMBLE_MSK;
5555
5556 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
5557 if (priv->assoc_capability &
5558 WLAN_CAPABILITY_SHORT_SLOT_TIME)
5559 priv->staging_rxon.flags |=
5560 RXON_FLG_SHORT_SLOT_MSK;
5561 else
5562 priv->staging_rxon.flags &=
5563 ~RXON_FLG_SHORT_SLOT_MSK;
5564
5565 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
5566 priv->staging_rxon.flags &=
5567 ~RXON_FLG_SHORT_SLOT_MSK;
5568 }
5569 /* restore RXON assoc */
5570 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
5571 iwl4965_commit_rxon(priv);
5572 iwl4965_activate_qos(priv, 1);
5573 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
5574 }
5575 iwl4965_send_beacon_cmd(priv);
5576
5577 /* FIXME - we need to add code here to detect a totally new
5578 * configuration, reset the AP, unassoc, rxon timing, assoc,
5579 * clear sta table, add BCAST sta... */
5580 }
5581
5582 static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
5583 struct ieee80211_vif *vif,
5584 struct ieee80211_if_conf *conf)
5585 {
5586 struct iwl_priv *priv = hw->priv;
5587 DECLARE_MAC_BUF(mac);
5588 unsigned long flags;
5589 int rc;
5590
5591 if (conf == NULL)
5592 return -EIO;
5593
5594 if (priv->vif != vif) {
5595 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
5596 return 0;
5597 }
5598
5599 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
5600 (!conf->beacon || !conf->ssid_len)) {
5601 IWL_DEBUG_MAC80211
5602 ("Leaving in AP mode because HostAPD is not ready.\n");
5603 return 0;
5604 }
5605
5606 if (!iwl_is_alive(priv))
5607 return -EAGAIN;
5608
5609 mutex_lock(&priv->mutex);
5610
5611 if (conf->bssid)
5612 IWL_DEBUG_MAC80211("bssid: %s\n",
5613 print_mac(mac, conf->bssid));
5614
5615 /*
5616 * very dubious code was here; the probe filtering flag is never set:
5617 *
5618 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
5619 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
5620 */
5621
5622 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
5623 if (!conf->bssid) {
5624 conf->bssid = priv->mac_addr;
5625 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
5626 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
5627 print_mac(mac, conf->bssid));
5628 }
5629 if (priv->ibss_beacon)
5630 dev_kfree_skb(priv->ibss_beacon);
5631
5632 priv->ibss_beacon = conf->beacon;
5633 }
5634
5635 if (iwl_is_rfkill(priv))
5636 goto done;
5637
5638 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
5639 !is_multicast_ether_addr(conf->bssid)) {
5640 /* If there is currently a HW scan going on in the background
5641 * then we need to cancel it else the RXON below will fail. */
5642 if (iwl4965_scan_cancel_timeout(priv, 100)) {
5643 IWL_WARNING("Aborted scan still in progress "
5644 "after 100ms\n");
5645 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
5646 mutex_unlock(&priv->mutex);
5647 return -EAGAIN;
5648 }
5649 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
5650
5651 /* TODO: Audit driver for usage of these members and see
5652 * if mac80211 deprecates them (priv->bssid looks like it
5653 * shouldn't be there, but I haven't scanned the IBSS code
5654 * to verify) - jpk */
5655 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
5656
5657 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
5658 iwl4965_config_ap(priv);
5659 else {
5660 rc = iwl4965_commit_rxon(priv);
5661 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
5662 iwl4965_rxon_add_station(
5663 priv, priv->active_rxon.bssid_addr, 1);
5664 }
5665
5666 } else {
5667 iwl4965_scan_cancel_timeout(priv, 100);
5668 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5669 iwl4965_commit_rxon(priv);
5670 }
5671
5672 done:
5673 spin_lock_irqsave(&priv->lock, flags);
5674 if (!conf->ssid_len)
5675 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5676 else
5677 memcpy(priv->essid, conf->ssid, conf->ssid_len);
5678
5679 priv->essid_len = conf->ssid_len;
5680 spin_unlock_irqrestore(&priv->lock, flags);
5681
5682 IWL_DEBUG_MAC80211("leave\n");
5683 mutex_unlock(&priv->mutex);
5684
5685 return 0;
5686 }
5687
5688 static void iwl4965_configure_filter(struct ieee80211_hw *hw,
5689 unsigned int changed_flags,
5690 unsigned int *total_flags,
5691 int mc_count, struct dev_addr_list *mc_list)
5692 {
5693 /*
5694 * XXX: dummy
5695 * see also iwl4965_connection_init_rx_config
5696 */
5697 *total_flags = 0;
5698 }
5699
5700 static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
5701 struct ieee80211_if_init_conf *conf)
5702 {
5703 struct iwl_priv *priv = hw->priv;
5704
5705 IWL_DEBUG_MAC80211("enter\n");
5706
5707 mutex_lock(&priv->mutex);
5708
5709 if (iwl_is_ready_rf(priv)) {
5710 iwl4965_scan_cancel_timeout(priv, 100);
5711 cancel_delayed_work(&priv->post_associate);
5712 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5713 iwl4965_commit_rxon(priv);
5714 }
5715 if (priv->vif == conf->vif) {
5716 priv->vif = NULL;
5717 memset(priv->bssid, 0, ETH_ALEN);
5718 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
5719 priv->essid_len = 0;
5720 }
5721 mutex_unlock(&priv->mutex);
5722
5723 IWL_DEBUG_MAC80211("leave\n");
5724
5725 }
5726
5727 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
5728 static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
5729 struct ieee80211_vif *vif,
5730 struct ieee80211_bss_conf *bss_conf,
5731 u32 changes)
5732 {
5733 struct iwl_priv *priv = hw->priv;
5734
5735 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
5736
5737 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
5738 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
5739 bss_conf->use_short_preamble);
5740 if (bss_conf->use_short_preamble)
5741 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5742 else
5743 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5744 }
5745
5746 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
5747 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
5748 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
5749 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
5750 else
5751 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
5752 }
5753
5754 if (changes & BSS_CHANGED_HT) {
5755 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
5756 iwl4965_ht_conf(priv, bss_conf);
5757 iwl_set_rxon_chain(priv);
5758 }
5759
5760 if (changes & BSS_CHANGED_ASSOC) {
5761 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
5762 /* This should never happen as this function should
5763 * never be called from interrupt context. */
5764 if (WARN_ON_ONCE(in_interrupt()))
5765 return;
5766 if (bss_conf->assoc) {
5767 priv->assoc_id = bss_conf->aid;
5768 priv->beacon_int = bss_conf->beacon_int;
5769 priv->timestamp = bss_conf->timestamp;
5770 priv->assoc_capability = bss_conf->assoc_capability;
5771 priv->next_scan_jiffies = jiffies +
5772 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
5773 mutex_lock(&priv->mutex);
5774 iwl4965_post_associate(priv);
5775 mutex_unlock(&priv->mutex);
5776 } else {
5777 priv->assoc_id = 0;
5778 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
5779 }
5780 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
5781 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
5782 iwl_send_rxon_assoc(priv);
5783 }
5784
5785 }
5786
5787 static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
5788 {
5789 int rc = 0;
5790 unsigned long flags;
5791 struct iwl_priv *priv = hw->priv;
5792
5793 IWL_DEBUG_MAC80211("enter\n");
5794
5795 mutex_lock(&priv->mutex);
5796 spin_lock_irqsave(&priv->lock, flags);
5797
5798 if (!iwl_is_ready_rf(priv)) {
5799 rc = -EIO;
5800 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
5801 goto out_unlock;
5802 }
5803
5804 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
5805 rc = -EIO;
5806 IWL_ERROR("ERROR: APs don't scan\n");
5807 goto out_unlock;
5808 }
5809
5810 /* we don't schedule scan within next_scan_jiffies period */
5811 if (priv->next_scan_jiffies &&
5812 time_after(priv->next_scan_jiffies, jiffies)) {
5813 rc = -EAGAIN;
5814 goto out_unlock;
5815 }
5816 /* if we just finished scan ask for delay */
5817 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
5818 IWL_DELAY_NEXT_SCAN, jiffies)) {
5819 rc = -EAGAIN;
5820 goto out_unlock;
5821 }
5822 if (len) {
5823 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
5824 iwl4965_escape_essid(ssid, len), (int)len);
5825
5826 priv->one_direct_scan = 1;
5827 priv->direct_ssid_len = (u8)
5828 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
5829 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
5830 } else
5831 priv->one_direct_scan = 0;
5832
5833 rc = iwl4965_scan_initiate(priv);
5834
5835 IWL_DEBUG_MAC80211("leave\n");
5836
5837 out_unlock:
5838 spin_unlock_irqrestore(&priv->lock, flags);
5839 mutex_unlock(&priv->mutex);
5840
5841 return rc;
5842 }
5843
5844 static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
5845 struct ieee80211_key_conf *keyconf, const u8 *addr,
5846 u32 iv32, u16 *phase1key)
5847 {
5848 struct iwl_priv *priv = hw->priv;
5849 u8 sta_id = IWL_INVALID_STATION;
5850 unsigned long flags;
5851 __le16 key_flags = 0;
5852 int i;
5853 DECLARE_MAC_BUF(mac);
5854
5855 IWL_DEBUG_MAC80211("enter\n");
5856
5857 sta_id = iwl_find_station(priv, addr);
5858 if (sta_id == IWL_INVALID_STATION) {
5859 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5860 print_mac(mac, addr));
5861 return;
5862 }
5863
5864 iwl4965_scan_cancel_timeout(priv, 100);
5865
5866 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
5867 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
5868 key_flags &= ~STA_KEY_FLG_INVALID;
5869
5870 if (sta_id == priv->hw_params.bcast_sta_id)
5871 key_flags |= STA_KEY_MULTICAST_MSK;
5872
5873 spin_lock_irqsave(&priv->sta_lock, flags);
5874
5875 priv->stations[sta_id].sta.key.key_flags = key_flags;
5876 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
5877
5878 for (i = 0; i < 5; i++)
5879 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
5880 cpu_to_le16(phase1key[i]);
5881
5882 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
5883 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
5884
5885 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
5886
5887 spin_unlock_irqrestore(&priv->sta_lock, flags);
5888
5889 IWL_DEBUG_MAC80211("leave\n");
5890 }
5891
5892 static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5893 const u8 *local_addr, const u8 *addr,
5894 struct ieee80211_key_conf *key)
5895 {
5896 struct iwl_priv *priv = hw->priv;
5897 DECLARE_MAC_BUF(mac);
5898 int ret = 0;
5899 u8 sta_id = IWL_INVALID_STATION;
5900 u8 is_default_wep_key = 0;
5901
5902 IWL_DEBUG_MAC80211("enter\n");
5903
5904 if (priv->hw_params.sw_crypto) {
5905 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
5906 return -EOPNOTSUPP;
5907 }
5908
5909 if (is_zero_ether_addr(addr))
5910 /* only support pairwise keys */
5911 return -EOPNOTSUPP;
5912
5913 sta_id = iwl_find_station(priv, addr);
5914 if (sta_id == IWL_INVALID_STATION) {
5915 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
5916 print_mac(mac, addr));
5917 return -EINVAL;
5918
5919 }
5920
5921 mutex_lock(&priv->mutex);
5922 iwl4965_scan_cancel_timeout(priv, 100);
5923 mutex_unlock(&priv->mutex);
5924
5925 /* If we are getting WEP group key and we didn't receive any key mapping
5926 * so far, we are in legacy wep mode (group key only), otherwise we are
5927 * in 1X mode.
5928 * In legacy wep mode, we use another host command to the uCode */
5929 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
5930 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
5931 if (cmd == SET_KEY)
5932 is_default_wep_key = !priv->key_mapping_key;
5933 else
5934 is_default_wep_key = priv->default_wep_key;
5935 }
5936
5937 switch (cmd) {
5938 case SET_KEY:
5939 if (is_default_wep_key)
5940 ret = iwl_set_default_wep_key(priv, key);
5941 else
5942 ret = iwl_set_dynamic_key(priv, key, sta_id);
5943
5944 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
5945 break;
5946 case DISABLE_KEY:
5947 if (is_default_wep_key)
5948 ret = iwl_remove_default_wep_key(priv, key);
5949 else
5950 ret = iwl_remove_dynamic_key(priv, key, sta_id);
5951
5952 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
5953 break;
5954 default:
5955 ret = -EINVAL;
5956 }
5957
5958 IWL_DEBUG_MAC80211("leave\n");
5959
5960 return ret;
5961 }
5962
5963 static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
5964 const struct ieee80211_tx_queue_params *params)
5965 {
5966 struct iwl_priv *priv = hw->priv;
5967 unsigned long flags;
5968 int q;
5969
5970 IWL_DEBUG_MAC80211("enter\n");
5971
5972 if (!iwl_is_ready_rf(priv)) {
5973 IWL_DEBUG_MAC80211("leave - RF not ready\n");
5974 return -EIO;
5975 }
5976
5977 if (queue >= AC_NUM) {
5978 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
5979 return 0;
5980 }
5981
5982 if (!priv->qos_data.qos_enable) {
5983 priv->qos_data.qos_active = 0;
5984 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
5985 return 0;
5986 }
5987 q = AC_NUM - 1 - queue;
5988
5989 spin_lock_irqsave(&priv->lock, flags);
5990
5991 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
5992 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
5993 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
5994 priv->qos_data.def_qos_parm.ac[q].edca_txop =
5995 cpu_to_le16((params->txop * 32));
5996
5997 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
5998 priv->qos_data.qos_active = 1;
5999
6000 spin_unlock_irqrestore(&priv->lock, flags);
6001
6002 mutex_lock(&priv->mutex);
6003 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
6004 iwl4965_activate_qos(priv, 1);
6005 else if (priv->assoc_id && iwl_is_associated(priv))
6006 iwl4965_activate_qos(priv, 0);
6007
6008 mutex_unlock(&priv->mutex);
6009
6010 IWL_DEBUG_MAC80211("leave\n");
6011 return 0;
6012 }
6013
6014 static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
6015 struct ieee80211_tx_queue_stats *stats)
6016 {
6017 struct iwl_priv *priv = hw->priv;
6018 int i, avail;
6019 struct iwl4965_tx_queue *txq;
6020 struct iwl4965_queue *q;
6021 unsigned long flags;
6022
6023 IWL_DEBUG_MAC80211("enter\n");
6024
6025 if (!iwl_is_ready_rf(priv)) {
6026 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6027 return -EIO;
6028 }
6029
6030 spin_lock_irqsave(&priv->lock, flags);
6031
6032 for (i = 0; i < AC_NUM; i++) {
6033 txq = &priv->txq[i];
6034 q = &txq->q;
6035 avail = iwl4965_queue_space(q);
6036
6037 stats[i].len = q->n_window - avail;
6038 stats[i].limit = q->n_window - q->high_mark;
6039 stats[i].count = q->n_window;
6040
6041 }
6042 spin_unlock_irqrestore(&priv->lock, flags);
6043
6044 IWL_DEBUG_MAC80211("leave\n");
6045
6046 return 0;
6047 }
6048
6049 static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
6050 struct ieee80211_low_level_stats *stats)
6051 {
6052 IWL_DEBUG_MAC80211("enter\n");
6053 IWL_DEBUG_MAC80211("leave\n");
6054
6055 return 0;
6056 }
6057
6058 static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
6059 {
6060 IWL_DEBUG_MAC80211("enter\n");
6061 IWL_DEBUG_MAC80211("leave\n");
6062
6063 return 0;
6064 }
6065
6066 static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
6067 {
6068 struct iwl_priv *priv = hw->priv;
6069 unsigned long flags;
6070
6071 mutex_lock(&priv->mutex);
6072 IWL_DEBUG_MAC80211("enter\n");
6073
6074 priv->lq_mngr.lq_ready = 0;
6075 #ifdef CONFIG_IWL4965_HT
6076 spin_lock_irqsave(&priv->lock, flags);
6077 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
6078 spin_unlock_irqrestore(&priv->lock, flags);
6079 #endif /* CONFIG_IWL4965_HT */
6080
6081 iwl_reset_qos(priv);
6082
6083 cancel_delayed_work(&priv->post_associate);
6084
6085 spin_lock_irqsave(&priv->lock, flags);
6086 priv->assoc_id = 0;
6087 priv->assoc_capability = 0;
6088 priv->assoc_station_added = 0;
6089
6090 /* new association get rid of ibss beacon skb */
6091 if (priv->ibss_beacon)
6092 dev_kfree_skb(priv->ibss_beacon);
6093
6094 priv->ibss_beacon = NULL;
6095
6096 priv->beacon_int = priv->hw->conf.beacon_int;
6097 priv->timestamp = 0;
6098 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
6099 priv->beacon_int = 0;
6100
6101 spin_unlock_irqrestore(&priv->lock, flags);
6102
6103 if (!iwl_is_ready_rf(priv)) {
6104 IWL_DEBUG_MAC80211("leave - not ready\n");
6105 mutex_unlock(&priv->mutex);
6106 return;
6107 }
6108
6109 /* we are restarting association process
6110 * clear RXON_FILTER_ASSOC_MSK bit
6111 */
6112 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
6113 iwl4965_scan_cancel_timeout(priv, 100);
6114 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6115 iwl4965_commit_rxon(priv);
6116 }
6117
6118 iwl_power_update_mode(priv, 0);
6119
6120 /* Per mac80211.h: This is only used in IBSS mode... */
6121 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
6122
6123 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6124 mutex_unlock(&priv->mutex);
6125 return;
6126 }
6127
6128 iwl4965_set_rate(priv);
6129
6130 mutex_unlock(&priv->mutex);
6131
6132 IWL_DEBUG_MAC80211("leave\n");
6133 }
6134
6135 static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
6136 struct ieee80211_tx_control *control)
6137 {
6138 struct iwl_priv *priv = hw->priv;
6139 unsigned long flags;
6140
6141 mutex_lock(&priv->mutex);
6142 IWL_DEBUG_MAC80211("enter\n");
6143
6144 if (!iwl_is_ready_rf(priv)) {
6145 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6146 mutex_unlock(&priv->mutex);
6147 return -EIO;
6148 }
6149
6150 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
6151 IWL_DEBUG_MAC80211("leave - not IBSS\n");
6152 mutex_unlock(&priv->mutex);
6153 return -EIO;
6154 }
6155
6156 spin_lock_irqsave(&priv->lock, flags);
6157
6158 if (priv->ibss_beacon)
6159 dev_kfree_skb(priv->ibss_beacon);
6160
6161 priv->ibss_beacon = skb;
6162
6163 priv->assoc_id = 0;
6164
6165 IWL_DEBUG_MAC80211("leave\n");
6166 spin_unlock_irqrestore(&priv->lock, flags);
6167
6168 iwl_reset_qos(priv);
6169
6170 queue_work(priv->workqueue, &priv->post_associate.work);
6171
6172 mutex_unlock(&priv->mutex);
6173
6174 return 0;
6175 }
6176
6177 /*****************************************************************************
6178 *
6179 * sysfs attributes
6180 *
6181 *****************************************************************************/
6182
6183 #ifdef CONFIG_IWLWIFI_DEBUG
6184
6185 /*
6186 * The following adds a new attribute to the sysfs representation
6187 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6188 * used for controlling the debug level.
6189 *
6190 * See the level definitions in iwl for details.
6191 */
6192
6193 static ssize_t show_debug_level(struct device_driver *d, char *buf)
6194 {
6195 return sprintf(buf, "0x%08X\n", iwl_debug_level);
6196 }
6197 static ssize_t store_debug_level(struct device_driver *d,
6198 const char *buf, size_t count)
6199 {
6200 char *p = (char *)buf;
6201 u32 val;
6202
6203 val = simple_strtoul(p, &p, 0);
6204 if (p == buf)
6205 printk(KERN_INFO DRV_NAME
6206 ": %s is not in hex or decimal form.\n", buf);
6207 else
6208 iwl_debug_level = val;
6209
6210 return strnlen(buf, count);
6211 }
6212
6213 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
6214 show_debug_level, store_debug_level);
6215
6216 #endif /* CONFIG_IWLWIFI_DEBUG */
6217
6218
6219 static ssize_t show_temperature(struct device *d,
6220 struct device_attribute *attr, char *buf)
6221 {
6222 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6223
6224 if (!iwl_is_alive(priv))
6225 return -EAGAIN;
6226
6227 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
6228 }
6229
6230 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6231
6232 static ssize_t show_rs_window(struct device *d,
6233 struct device_attribute *attr,
6234 char *buf)
6235 {
6236 struct iwl_priv *priv = d->driver_data;
6237 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
6238 }
6239 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
6240
6241 static ssize_t show_tx_power(struct device *d,
6242 struct device_attribute *attr, char *buf)
6243 {
6244 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6245 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6246 }
6247
6248 static ssize_t store_tx_power(struct device *d,
6249 struct device_attribute *attr,
6250 const char *buf, size_t count)
6251 {
6252 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6253 char *p = (char *)buf;
6254 u32 val;
6255
6256 val = simple_strtoul(p, &p, 10);
6257 if (p == buf)
6258 printk(KERN_INFO DRV_NAME
6259 ": %s is not in decimal form.\n", buf);
6260 else
6261 iwl4965_hw_reg_set_txpower(priv, val);
6262
6263 return count;
6264 }
6265
6266 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6267
6268 static ssize_t show_flags(struct device *d,
6269 struct device_attribute *attr, char *buf)
6270 {
6271 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6272
6273 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
6274 }
6275
6276 static ssize_t store_flags(struct device *d,
6277 struct device_attribute *attr,
6278 const char *buf, size_t count)
6279 {
6280 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6281 u32 flags = simple_strtoul(buf, NULL, 0);
6282
6283 mutex_lock(&priv->mutex);
6284 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
6285 /* Cancel any currently running scans... */
6286 if (iwl4965_scan_cancel_timeout(priv, 100))
6287 IWL_WARNING("Could not cancel scan.\n");
6288 else {
6289 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6290 flags);
6291 priv->staging_rxon.flags = cpu_to_le32(flags);
6292 iwl4965_commit_rxon(priv);
6293 }
6294 }
6295 mutex_unlock(&priv->mutex);
6296
6297 return count;
6298 }
6299
6300 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6301
6302 static ssize_t show_filter_flags(struct device *d,
6303 struct device_attribute *attr, char *buf)
6304 {
6305 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6306
6307 return sprintf(buf, "0x%04X\n",
6308 le32_to_cpu(priv->active_rxon.filter_flags));
6309 }
6310
6311 static ssize_t store_filter_flags(struct device *d,
6312 struct device_attribute *attr,
6313 const char *buf, size_t count)
6314 {
6315 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6316 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6317
6318 mutex_lock(&priv->mutex);
6319 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
6320 /* Cancel any currently running scans... */
6321 if (iwl4965_scan_cancel_timeout(priv, 100))
6322 IWL_WARNING("Could not cancel scan.\n");
6323 else {
6324 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6325 "0x%04X\n", filter_flags);
6326 priv->staging_rxon.filter_flags =
6327 cpu_to_le32(filter_flags);
6328 iwl4965_commit_rxon(priv);
6329 }
6330 }
6331 mutex_unlock(&priv->mutex);
6332
6333 return count;
6334 }
6335
6336 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6337 store_filter_flags);
6338
6339 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
6340
6341 static ssize_t show_measurement(struct device *d,
6342 struct device_attribute *attr, char *buf)
6343 {
6344 struct iwl_priv *priv = dev_get_drvdata(d);
6345 struct iwl4965_spectrum_notification measure_report;
6346 u32 size = sizeof(measure_report), len = 0, ofs = 0;
6347 u8 *data = (u8 *) & measure_report;
6348 unsigned long flags;
6349
6350 spin_lock_irqsave(&priv->lock, flags);
6351 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6352 spin_unlock_irqrestore(&priv->lock, flags);
6353 return 0;
6354 }
6355 memcpy(&measure_report, &priv->measure_report, size);
6356 priv->measurement_status = 0;
6357 spin_unlock_irqrestore(&priv->lock, flags);
6358
6359 while (size && (PAGE_SIZE - len)) {
6360 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6361 PAGE_SIZE - len, 1);
6362 len = strlen(buf);
6363 if (PAGE_SIZE - len)
6364 buf[len++] = '\n';
6365
6366 ofs += 16;
6367 size -= min(size, 16U);
6368 }
6369
6370 return len;
6371 }
6372
6373 static ssize_t store_measurement(struct device *d,
6374 struct device_attribute *attr,
6375 const char *buf, size_t count)
6376 {
6377 struct iwl_priv *priv = dev_get_drvdata(d);
6378 struct ieee80211_measurement_params params = {
6379 .channel = le16_to_cpu(priv->active_rxon.channel),
6380 .start_time = cpu_to_le64(priv->last_tsf),
6381 .duration = cpu_to_le16(1),
6382 };
6383 u8 type = IWL_MEASURE_BASIC;
6384 u8 buffer[32];
6385 u8 channel;
6386
6387 if (count) {
6388 char *p = buffer;
6389 strncpy(buffer, buf, min(sizeof(buffer), count));
6390 channel = simple_strtoul(p, NULL, 0);
6391 if (channel)
6392 params.channel = channel;
6393
6394 p = buffer;
6395 while (*p && *p != ' ')
6396 p++;
6397 if (*p)
6398 type = simple_strtoul(p + 1, NULL, 0);
6399 }
6400
6401 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6402 "channel %d (for '%s')\n", type, params.channel, buf);
6403 iwl4965_get_measurement(priv, &params, type);
6404
6405 return count;
6406 }
6407
6408 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6409 show_measurement, store_measurement);
6410 #endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
6411
6412 static ssize_t store_retry_rate(struct device *d,
6413 struct device_attribute *attr,
6414 const char *buf, size_t count)
6415 {
6416 struct iwl_priv *priv = dev_get_drvdata(d);
6417
6418 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6419 if (priv->retry_rate <= 0)
6420 priv->retry_rate = 1;
6421
6422 return count;
6423 }
6424
6425 static ssize_t show_retry_rate(struct device *d,
6426 struct device_attribute *attr, char *buf)
6427 {
6428 struct iwl_priv *priv = dev_get_drvdata(d);
6429 return sprintf(buf, "%d", priv->retry_rate);
6430 }
6431
6432 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6433 store_retry_rate);
6434
6435 static ssize_t store_power_level(struct device *d,
6436 struct device_attribute *attr,
6437 const char *buf, size_t count)
6438 {
6439 struct iwl_priv *priv = dev_get_drvdata(d);
6440 int rc;
6441 int mode;
6442
6443 mode = simple_strtoul(buf, NULL, 0);
6444 mutex_lock(&priv->mutex);
6445
6446 if (!iwl_is_ready(priv)) {
6447 rc = -EAGAIN;
6448 goto out;
6449 }
6450
6451 rc = iwl_power_set_user_mode(priv, mode);
6452 if (rc) {
6453 IWL_DEBUG_MAC80211("failed setting power mode.\n");
6454 goto out;
6455 }
6456 rc = count;
6457
6458 out:
6459 mutex_unlock(&priv->mutex);
6460 return rc;
6461 }
6462
6463 #define MAX_WX_STRING 80
6464
6465 /* Values are in microsecond */
6466 static const s32 timeout_duration[] = {
6467 350000,
6468 250000,
6469 75000,
6470 37000,
6471 25000,
6472 };
6473 static const s32 period_duration[] = {
6474 400000,
6475 700000,
6476 1000000,
6477 1000000,
6478 1000000
6479 };
6480
6481 static ssize_t show_power_level(struct device *d,
6482 struct device_attribute *attr, char *buf)
6483 {
6484 struct iwl_priv *priv = dev_get_drvdata(d);
6485 int level = priv->power_data.power_mode;
6486 char *p = buf;
6487
6488 p += sprintf(p, "%d ", level);
6489 switch (level) {
6490 case IWL_POWER_MODE_CAM:
6491 case IWL_POWER_AC:
6492 p += sprintf(p, "(AC)");
6493 break;
6494 case IWL_POWER_BATTERY:
6495 p += sprintf(p, "(BATTERY)");
6496 break;
6497 default:
6498 p += sprintf(p,
6499 "(Timeout %dms, Period %dms)",
6500 timeout_duration[level - 1] / 1000,
6501 period_duration[level - 1] / 1000);
6502 }
6503 /*
6504 if (!(priv->power_mode & IWL_POWER_ENABLED))
6505 p += sprintf(p, " OFF\n");
6506 else
6507 p += sprintf(p, " \n");
6508 */
6509 p += sprintf(p, " \n");
6510 return (p - buf + 1);
6511 }
6512
6513 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
6514 store_power_level);
6515
6516 static ssize_t show_channels(struct device *d,
6517 struct device_attribute *attr, char *buf)
6518 {
6519 /* all this shit doesn't belong into sysfs anyway */
6520 return 0;
6521 }
6522
6523 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
6524
6525 static ssize_t show_statistics(struct device *d,
6526 struct device_attribute *attr, char *buf)
6527 {
6528 struct iwl_priv *priv = dev_get_drvdata(d);
6529 u32 size = sizeof(struct iwl4965_notif_statistics);
6530 u32 len = 0, ofs = 0;
6531 u8 *data = (u8 *) & priv->statistics;
6532 int rc = 0;
6533
6534 if (!iwl_is_alive(priv))
6535 return -EAGAIN;
6536
6537 mutex_lock(&priv->mutex);
6538 rc = iwl_send_statistics_request(priv, 0);
6539 mutex_unlock(&priv->mutex);
6540
6541 if (rc) {
6542 len = sprintf(buf,
6543 "Error sending statistics request: 0x%08X\n", rc);
6544 return len;
6545 }
6546
6547 while (size && (PAGE_SIZE - len)) {
6548 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6549 PAGE_SIZE - len, 1);
6550 len = strlen(buf);
6551 if (PAGE_SIZE - len)
6552 buf[len++] = '\n';
6553
6554 ofs += 16;
6555 size -= min(size, 16U);
6556 }
6557
6558 return len;
6559 }
6560
6561 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
6562
6563 static ssize_t show_status(struct device *d,
6564 struct device_attribute *attr, char *buf)
6565 {
6566 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
6567 if (!iwl_is_alive(priv))
6568 return -EAGAIN;
6569 return sprintf(buf, "0x%08x\n", (int)priv->status);
6570 }
6571
6572 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
6573
6574 static ssize_t dump_error_log(struct device *d,
6575 struct device_attribute *attr,
6576 const char *buf, size_t count)
6577 {
6578 char *p = (char *)buf;
6579
6580 if (p[0] == '1')
6581 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
6582
6583 return strnlen(buf, count);
6584 }
6585
6586 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
6587
6588 static ssize_t dump_event_log(struct device *d,
6589 struct device_attribute *attr,
6590 const char *buf, size_t count)
6591 {
6592 char *p = (char *)buf;
6593
6594 if (p[0] == '1')
6595 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
6596
6597 return strnlen(buf, count);
6598 }
6599
6600 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
6601
6602 /*****************************************************************************
6603 *
6604 * driver setup and teardown
6605 *
6606 *****************************************************************************/
6607
6608 static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
6609 {
6610 priv->workqueue = create_workqueue(DRV_NAME);
6611
6612 init_waitqueue_head(&priv->wait_command_queue);
6613
6614 INIT_WORK(&priv->up, iwl4965_bg_up);
6615 INIT_WORK(&priv->restart, iwl4965_bg_restart);
6616 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
6617 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
6618 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
6619 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
6620 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
6621 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
6622 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
6623 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
6624 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
6625 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
6626
6627 iwl4965_hw_setup_deferred_work(priv);
6628
6629 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
6630 iwl4965_irq_tasklet, (unsigned long)priv);
6631 }
6632
6633 static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
6634 {
6635 iwl4965_hw_cancel_deferred_work(priv);
6636
6637 cancel_delayed_work_sync(&priv->init_alive_start);
6638 cancel_delayed_work(&priv->scan_check);
6639 cancel_delayed_work(&priv->alive_start);
6640 cancel_delayed_work(&priv->post_associate);
6641 cancel_work_sync(&priv->beacon_update);
6642 }
6643
6644 static struct attribute *iwl4965_sysfs_entries[] = {
6645 &dev_attr_channels.attr,
6646 &dev_attr_dump_errors.attr,
6647 &dev_attr_dump_events.attr,
6648 &dev_attr_flags.attr,
6649 &dev_attr_filter_flags.attr,
6650 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
6651 &dev_attr_measurement.attr,
6652 #endif
6653 &dev_attr_power_level.attr,
6654 &dev_attr_retry_rate.attr,
6655 &dev_attr_rs_window.attr,
6656 &dev_attr_statistics.attr,
6657 &dev_attr_status.attr,
6658 &dev_attr_temperature.attr,
6659 &dev_attr_tx_power.attr,
6660
6661 NULL
6662 };
6663
6664 static struct attribute_group iwl4965_attribute_group = {
6665 .name = NULL, /* put in device directory */
6666 .attrs = iwl4965_sysfs_entries,
6667 };
6668
6669 static struct ieee80211_ops iwl4965_hw_ops = {
6670 .tx = iwl4965_mac_tx,
6671 .start = iwl4965_mac_start,
6672 .stop = iwl4965_mac_stop,
6673 .add_interface = iwl4965_mac_add_interface,
6674 .remove_interface = iwl4965_mac_remove_interface,
6675 .config = iwl4965_mac_config,
6676 .config_interface = iwl4965_mac_config_interface,
6677 .configure_filter = iwl4965_configure_filter,
6678 .set_key = iwl4965_mac_set_key,
6679 .update_tkip_key = iwl4965_mac_update_tkip_key,
6680 .get_stats = iwl4965_mac_get_stats,
6681 .get_tx_stats = iwl4965_mac_get_tx_stats,
6682 .conf_tx = iwl4965_mac_conf_tx,
6683 .get_tsf = iwl4965_mac_get_tsf,
6684 .reset_tsf = iwl4965_mac_reset_tsf,
6685 .beacon_update = iwl4965_mac_beacon_update,
6686 .bss_info_changed = iwl4965_bss_info_changed,
6687 #ifdef CONFIG_IWL4965_HT
6688 .ampdu_action = iwl4965_mac_ampdu_action,
6689 #endif /* CONFIG_IWL4965_HT */
6690 .hw_scan = iwl4965_mac_hw_scan
6691 };
6692
6693 static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
6694 {
6695 int err = 0;
6696 struct iwl_priv *priv;
6697 struct ieee80211_hw *hw;
6698 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
6699 unsigned long flags;
6700 DECLARE_MAC_BUF(mac);
6701
6702 /************************
6703 * 1. Allocating HW data
6704 ************************/
6705
6706 /* Disabling hardware scan means that mac80211 will perform scans
6707 * "the hard way", rather than using device's scan. */
6708 if (cfg->mod_params->disable_hw_scan) {
6709 IWL_DEBUG_INFO("Disabling hw_scan\n");
6710 iwl4965_hw_ops.hw_scan = NULL;
6711 }
6712
6713 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
6714 if (!hw) {
6715 err = -ENOMEM;
6716 goto out;
6717 }
6718 priv = hw->priv;
6719 /* At this point both hw and priv are allocated. */
6720
6721 SET_IEEE80211_DEV(hw, &pdev->dev);
6722
6723 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
6724 priv->cfg = cfg;
6725 priv->pci_dev = pdev;
6726
6727 #ifdef CONFIG_IWLWIFI_DEBUG
6728 iwl_debug_level = priv->cfg->mod_params->debug;
6729 atomic_set(&priv->restrict_refcnt, 0);
6730 #endif
6731
6732 /**************************
6733 * 2. Initializing PCI bus
6734 **************************/
6735 if (pci_enable_device(pdev)) {
6736 err = -ENODEV;
6737 goto out_ieee80211_free_hw;
6738 }
6739
6740 pci_set_master(pdev);
6741
6742 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
6743 if (!err)
6744 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
6745 if (err) {
6746 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
6747 if (!err)
6748 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
6749 /* both attempts failed: */
6750 if (err) {
6751 printk(KERN_WARNING "%s: No suitable DMA available.\n",
6752 DRV_NAME);
6753 goto out_pci_disable_device;
6754 }
6755 }
6756
6757 err = pci_request_regions(pdev, DRV_NAME);
6758 if (err)
6759 goto out_pci_disable_device;
6760
6761 pci_set_drvdata(pdev, priv);
6762
6763 /* We disable the RETRY_TIMEOUT register (0x41) to keep
6764 * PCI Tx retries from interfering with C3 CPU state */
6765 pci_write_config_byte(pdev, 0x41, 0x00);
6766
6767 /***********************
6768 * 3. Read REV register
6769 ***********************/
6770 priv->hw_base = pci_iomap(pdev, 0, 0);
6771 if (!priv->hw_base) {
6772 err = -ENODEV;
6773 goto out_pci_release_regions;
6774 }
6775
6776 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
6777 (unsigned long long) pci_resource_len(pdev, 0));
6778 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
6779
6780 iwl_hw_detect(priv);
6781 printk(KERN_INFO DRV_NAME
6782 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
6783 priv->cfg->name, priv->hw_rev);
6784
6785 /* amp init */
6786 err = priv->cfg->ops->lib->apm_ops.init(priv);
6787 if (err < 0) {
6788 IWL_DEBUG_INFO("Failed to init APMG\n");
6789 goto out_iounmap;
6790 }
6791 /*****************
6792 * 4. Read EEPROM
6793 *****************/
6794 /* Read the EEPROM */
6795 err = iwl_eeprom_init(priv);
6796 if (err) {
6797 IWL_ERROR("Unable to init EEPROM\n");
6798 goto out_iounmap;
6799 }
6800 err = iwl_eeprom_check_version(priv);
6801 if (err)
6802 goto out_iounmap;
6803
6804 /* MAC Address location in EEPROM same for 3945/4965 */
6805 iwl_eeprom_get_mac(priv, priv->mac_addr);
6806 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
6807 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6808
6809 /************************
6810 * 5. Setup HW constants
6811 ************************/
6812 /* Device-specific setup */
6813 if (priv->cfg->ops->lib->set_hw_params(priv)) {
6814 IWL_ERROR("failed to set hw parameters\n");
6815 goto out_free_eeprom;
6816 }
6817
6818 /*******************
6819 * 6. Setup hw/priv
6820 *******************/
6821
6822 err = iwl_setup(priv);
6823 if (err)
6824 goto out_free_eeprom;
6825 /* At this point both hw and priv are initialized. */
6826
6827 /**********************************
6828 * 7. Initialize module parameters
6829 **********************************/
6830
6831 /* Disable radio (SW RF KILL) via parameter when loading driver */
6832 if (priv->cfg->mod_params->disable) {
6833 set_bit(STATUS_RF_KILL_SW, &priv->status);
6834 IWL_DEBUG_INFO("Radio disabled.\n");
6835 }
6836
6837 if (priv->cfg->mod_params->enable_qos)
6838 priv->qos_data.qos_enable = 1;
6839
6840 /********************
6841 * 8. Setup services
6842 ********************/
6843 spin_lock_irqsave(&priv->lock, flags);
6844 iwl4965_disable_interrupts(priv);
6845 spin_unlock_irqrestore(&priv->lock, flags);
6846
6847 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6848 if (err) {
6849 IWL_ERROR("failed to create sysfs device attributes\n");
6850 goto out_free_eeprom;
6851 }
6852
6853 err = iwl_dbgfs_register(priv, DRV_NAME);
6854 if (err) {
6855 IWL_ERROR("failed to create debugfs files\n");
6856 goto out_remove_sysfs;
6857 }
6858
6859 iwl4965_setup_deferred_work(priv);
6860 iwl4965_setup_rx_handlers(priv);
6861
6862 /********************
6863 * 9. Conclude
6864 ********************/
6865 pci_save_state(pdev);
6866 pci_disable_device(pdev);
6867
6868 /* notify iwlcore to init */
6869 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
6870 return 0;
6871
6872 out_remove_sysfs:
6873 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6874 out_free_eeprom:
6875 iwl_eeprom_free(priv);
6876 out_iounmap:
6877 pci_iounmap(pdev, priv->hw_base);
6878 out_pci_release_regions:
6879 pci_release_regions(pdev);
6880 pci_set_drvdata(pdev, NULL);
6881 out_pci_disable_device:
6882 pci_disable_device(pdev);
6883 out_ieee80211_free_hw:
6884 ieee80211_free_hw(priv->hw);
6885 out:
6886 return err;
6887 }
6888
6889 static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
6890 {
6891 struct iwl_priv *priv = pci_get_drvdata(pdev);
6892 struct list_head *p, *q;
6893 int i;
6894 unsigned long flags;
6895
6896 if (!priv)
6897 return;
6898
6899 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
6900
6901 if (priv->mac80211_registered) {
6902 ieee80211_unregister_hw(priv->hw);
6903 priv->mac80211_registered = 0;
6904 }
6905
6906 set_bit(STATUS_EXIT_PENDING, &priv->status);
6907
6908 iwl4965_down(priv);
6909
6910 /* make sure we flush any pending irq or
6911 * tasklet for the driver
6912 */
6913 spin_lock_irqsave(&priv->lock, flags);
6914 iwl4965_disable_interrupts(priv);
6915 spin_unlock_irqrestore(&priv->lock, flags);
6916
6917 iwl_synchronize_irq(priv);
6918
6919 /* Free MAC hash list for ADHOC */
6920 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
6921 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
6922 list_del(p);
6923 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
6924 }
6925 }
6926
6927 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
6928 iwl_dbgfs_unregister(priv);
6929 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
6930
6931 iwl4965_dealloc_ucode_pci(priv);
6932
6933 if (priv->rxq.bd)
6934 iwl_rx_queue_free(priv, &priv->rxq);
6935 iwl4965_hw_txq_ctx_free(priv);
6936
6937 iwlcore_clear_stations_table(priv);
6938 iwl_eeprom_free(priv);
6939
6940
6941 /*netif_stop_queue(dev); */
6942 flush_workqueue(priv->workqueue);
6943
6944 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
6945 * priv->workqueue... so we can't take down the workqueue
6946 * until now... */
6947 destroy_workqueue(priv->workqueue);
6948 priv->workqueue = NULL;
6949
6950 pci_iounmap(pdev, priv->hw_base);
6951 pci_release_regions(pdev);
6952 pci_disable_device(pdev);
6953 pci_set_drvdata(pdev, NULL);
6954
6955 iwl_free_channel_map(priv);
6956 iwlcore_free_geos(priv);
6957
6958 if (priv->ibss_beacon)
6959 dev_kfree_skb(priv->ibss_beacon);
6960
6961 ieee80211_free_hw(priv->hw);
6962 }
6963
6964 #ifdef CONFIG_PM
6965
6966 static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
6967 {
6968 struct iwl_priv *priv = pci_get_drvdata(pdev);
6969
6970 if (priv->is_open) {
6971 set_bit(STATUS_IN_SUSPEND, &priv->status);
6972 iwl4965_mac_stop(priv->hw);
6973 priv->is_open = 1;
6974 }
6975
6976 pci_set_power_state(pdev, PCI_D3hot);
6977
6978 return 0;
6979 }
6980
6981 static int iwl4965_pci_resume(struct pci_dev *pdev)
6982 {
6983 struct iwl_priv *priv = pci_get_drvdata(pdev);
6984
6985 pci_set_power_state(pdev, PCI_D0);
6986
6987 if (priv->is_open)
6988 iwl4965_mac_start(priv->hw);
6989
6990 clear_bit(STATUS_IN_SUSPEND, &priv->status);
6991 return 0;
6992 }
6993
6994 #endif /* CONFIG_PM */
6995
6996 /*****************************************************************************
6997 *
6998 * driver and module entry point
6999 *
7000 *****************************************************************************/
7001
7002 /* Hardware specific file defines the PCI IDs table for that hardware module */
7003 static struct pci_device_id iwl_hw_card_ids[] = {
7004 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
7005 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
7006 #ifdef CONFIG_IWL5000
7007 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
7008 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
7009 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
7010 #endif /* CONFIG_IWL5000 */
7011 {0}
7012 };
7013 MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
7014
7015 static struct pci_driver iwl_driver = {
7016 .name = DRV_NAME,
7017 .id_table = iwl_hw_card_ids,
7018 .probe = iwl4965_pci_probe,
7019 .remove = __devexit_p(iwl4965_pci_remove),
7020 #ifdef CONFIG_PM
7021 .suspend = iwl4965_pci_suspend,
7022 .resume = iwl4965_pci_resume,
7023 #endif
7024 };
7025
7026 static int __init iwl4965_init(void)
7027 {
7028
7029 int ret;
7030 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7031 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
7032
7033 ret = iwl4965_rate_control_register();
7034 if (ret) {
7035 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
7036 return ret;
7037 }
7038
7039 ret = pci_register_driver(&iwl_driver);
7040 if (ret) {
7041 IWL_ERROR("Unable to initialize PCI module\n");
7042 goto error_register;
7043 }
7044 #ifdef CONFIG_IWLWIFI_DEBUG
7045 ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
7046 if (ret) {
7047 IWL_ERROR("Unable to create driver sysfs file\n");
7048 goto error_debug;
7049 }
7050 #endif
7051
7052 return ret;
7053
7054 #ifdef CONFIG_IWLWIFI_DEBUG
7055 error_debug:
7056 pci_unregister_driver(&iwl_driver);
7057 #endif
7058 error_register:
7059 iwl4965_rate_control_unregister();
7060 return ret;
7061 }
7062
7063 static void __exit iwl4965_exit(void)
7064 {
7065 #ifdef CONFIG_IWLWIFI_DEBUG
7066 driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
7067 #endif
7068 pci_unregister_driver(&iwl_driver);
7069 iwl4965_rate_control_unregister();
7070 }
7071
7072 module_exit(iwl4965_exit);
7073 module_init(iwl4965_init);
This page took 0.306598 seconds and 6 git commands to generate.