iwlwifi: removes the RUN_TIME_CALIB ifdef
[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
57 /******************************************************************************
58 *
59 * module boiler plate
60 *
61 ******************************************************************************/
62
63 /*
64 * module name, copyright, version, etc.
65 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
66 */
67
68 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
69
70 #ifdef CONFIG_IWLWIFI_DEBUG
71 #define VD "d"
72 #else
73 #define VD
74 #endif
75
76 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
77 #define VS "s"
78 #else
79 #define VS
80 #endif
81
82 #define DRV_VERSION IWLWIFI_VERSION VD VS
83
84
85 MODULE_DESCRIPTION(DRV_DESCRIPTION);
86 MODULE_VERSION(DRV_VERSION);
87 MODULE_AUTHOR(DRV_COPYRIGHT);
88 MODULE_LICENSE("GPL");
89
90 static int iwl4965_is_empty_essid(const char *essid, int essid_len)
91 {
92 /* Single white space is for Linksys APs */
93 if (essid_len == 1 && essid[0] == ' ')
94 return 1;
95
96 /* Otherwise, if the entire essid is 0, we assume it is hidden */
97 while (essid_len) {
98 essid_len--;
99 if (essid[essid_len] != '\0')
100 return 0;
101 }
102
103 return 1;
104 }
105
106 static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
107 {
108 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
109 const char *s = essid;
110 char *d = escaped;
111
112 if (iwl4965_is_empty_essid(essid, essid_len)) {
113 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
114 return escaped;
115 }
116
117 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
118 while (essid_len--) {
119 if (*s == '\0') {
120 *d++ = '\\';
121 *d++ = '0';
122 s++;
123 } else
124 *d++ = *s++;
125 }
126 *d = '\0';
127 return escaped;
128 }
129
130 /*************** STATION TABLE MANAGEMENT ****
131 * mac80211 should be examined to determine if sta_info is duplicating
132 * the functionality provided here
133 */
134
135 /**************************************************************/
136
137
138
139 static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
140 {
141 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
142
143 if (hw_decrypt)
144 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
145 else
146 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
147
148 }
149
150 /**
151 * iwl4965_check_rxon_cmd - validate RXON structure is valid
152 *
153 * NOTE: This is really only useful during development and can eventually
154 * be #ifdef'd out once the driver is stable and folks aren't actively
155 * making changes
156 */
157 static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
158 {
159 int error = 0;
160 int counter = 1;
161
162 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
163 error |= le32_to_cpu(rxon->flags &
164 (RXON_FLG_TGJ_NARROW_BAND_MSK |
165 RXON_FLG_RADAR_DETECT_MSK));
166 if (error)
167 IWL_WARNING("check 24G fields %d | %d\n",
168 counter++, error);
169 } else {
170 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
171 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
172 if (error)
173 IWL_WARNING("check 52 fields %d | %d\n",
174 counter++, error);
175 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
176 if (error)
177 IWL_WARNING("check 52 CCK %d | %d\n",
178 counter++, error);
179 }
180 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
181 if (error)
182 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
183
184 /* make sure basic rates 6Mbps and 1Mbps are supported */
185 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
186 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
187 if (error)
188 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
189
190 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
191 if (error)
192 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
193
194 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
195 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
196 if (error)
197 IWL_WARNING("check CCK and short slot %d | %d\n",
198 counter++, error);
199
200 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
201 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
202 if (error)
203 IWL_WARNING("check CCK & auto detect %d | %d\n",
204 counter++, error);
205
206 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
207 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
208 if (error)
209 IWL_WARNING("check TGG and auto detect %d | %d\n",
210 counter++, error);
211
212 if (error)
213 IWL_WARNING("Tuning to channel %d\n",
214 le16_to_cpu(rxon->channel));
215
216 if (error) {
217 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
218 return -1;
219 }
220 return 0;
221 }
222
223 /**
224 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
225 * @priv: staging_rxon is compared to active_rxon
226 *
227 * If the RXON structure is changing enough to require a new tune,
228 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
229 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
230 */
231 static int iwl4965_full_rxon_required(struct iwl_priv *priv)
232 {
233
234 /* These items are only settable from the full RXON command */
235 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
236 compare_ether_addr(priv->staging_rxon.bssid_addr,
237 priv->active_rxon.bssid_addr) ||
238 compare_ether_addr(priv->staging_rxon.node_addr,
239 priv->active_rxon.node_addr) ||
240 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
241 priv->active_rxon.wlap_bssid_addr) ||
242 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
243 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
244 (priv->staging_rxon.air_propagation !=
245 priv->active_rxon.air_propagation) ||
246 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
247 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
248 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
249 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
250 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
251 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
252 return 1;
253
254 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
255 * be updated with the RXON_ASSOC command -- however only some
256 * flag transitions are allowed using RXON_ASSOC */
257
258 /* Check if we are not switching bands */
259 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
260 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
261 return 1;
262
263 /* Check if we are switching association toggle */
264 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
265 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
266 return 1;
267
268 return 0;
269 }
270
271 /**
272 * iwl4965_commit_rxon - commit staging_rxon to hardware
273 *
274 * The RXON command in staging_rxon is committed to the hardware and
275 * the active_rxon structure is updated with the new data. This
276 * function correctly transitions out of the RXON_ASSOC_MSK state if
277 * a HW tune is required based on the RXON structure changes.
278 */
279 static int iwl4965_commit_rxon(struct iwl_priv *priv)
280 {
281 /* cast away the const for active_rxon in this function */
282 struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
283 DECLARE_MAC_BUF(mac);
284 int rc = 0;
285
286 if (!iwl_is_alive(priv))
287 return -1;
288
289 /* always get timestamp with Rx frame */
290 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
291
292 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
293 if (rc) {
294 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
295 return -EINVAL;
296 }
297
298 /* If we don't need to send a full RXON, we can use
299 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
300 * and other flags for the current radio configuration. */
301 if (!iwl4965_full_rxon_required(priv)) {
302 rc = iwl_send_rxon_assoc(priv);
303 if (rc) {
304 IWL_ERROR("Error setting RXON_ASSOC "
305 "configuration (%d).\n", rc);
306 return rc;
307 }
308
309 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
310
311 return 0;
312 }
313
314 /* station table will be cleared */
315 priv->assoc_station_added = 0;
316
317 /* If we are currently associated and the new config requires
318 * an RXON_ASSOC and the new config wants the associated mask enabled,
319 * we must clear the associated from the active configuration
320 * before we apply the new config */
321 if (iwl_is_associated(priv) &&
322 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
323 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
324 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
325
326 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
327 sizeof(struct iwl_rxon_cmd),
328 &priv->active_rxon);
329
330 /* If the mask clearing failed then we set
331 * active_rxon back to what it was previously */
332 if (rc) {
333 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
334 IWL_ERROR("Error clearing ASSOC_MSK on current "
335 "configuration (%d).\n", rc);
336 return rc;
337 }
338 }
339
340 IWL_DEBUG_INFO("Sending RXON\n"
341 "* with%s RXON_FILTER_ASSOC_MSK\n"
342 "* channel = %d\n"
343 "* bssid = %s\n",
344 ((priv->staging_rxon.filter_flags &
345 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
346 le16_to_cpu(priv->staging_rxon.channel),
347 print_mac(mac, priv->staging_rxon.bssid_addr));
348
349 iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
350 /* Apply the new configuration */
351 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
352 sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
353 if (rc) {
354 IWL_ERROR("Error setting new configuration (%d).\n", rc);
355 return rc;
356 }
357
358 iwl_remove_station(priv, iwl_bcast_addr, 0);
359 iwlcore_clear_stations_table(priv);
360
361 if (!priv->error_recovering)
362 priv->start_calib = 0;
363
364 iwl_init_sensitivity(priv);
365
366 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
367
368 /* If we issue a new RXON command which required a tune then we must
369 * send a new TXPOWER command or we won't be able to Tx any frames */
370 rc = iwl4965_hw_reg_send_txpower(priv);
371 if (rc) {
372 IWL_ERROR("Error setting Tx power (%d).\n", rc);
373 return rc;
374 }
375
376 /* Add the broadcast address so we can send broadcast frames */
377 if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
378 IWL_INVALID_STATION) {
379 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
380 return -EIO;
381 }
382
383 /* If we have set the ASSOC_MSK and we are in BSS mode then
384 * add the IWL_AP_ID to the station rate table */
385 if (iwl_is_associated(priv) &&
386 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
387 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
388 == IWL_INVALID_STATION) {
389 IWL_ERROR("Error adding AP address for transmit.\n");
390 return -EIO;
391 }
392 priv->assoc_station_added = 1;
393 if (priv->default_wep_key &&
394 iwl_send_static_wepkey_cmd(priv, 0))
395 IWL_ERROR("Could not send WEP static key.\n");
396 }
397
398 return 0;
399 }
400
401 void iwl4965_update_chain_flags(struct iwl_priv *priv)
402 {
403
404 iwl_set_rxon_chain(priv);
405 iwl4965_commit_rxon(priv);
406 }
407
408 static int iwl4965_send_bt_config(struct iwl_priv *priv)
409 {
410 struct iwl4965_bt_cmd bt_cmd = {
411 .flags = 3,
412 .lead_time = 0xAA,
413 .max_kill = 1,
414 .kill_ack_mask = 0,
415 .kill_cts_mask = 0,
416 };
417
418 return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
419 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
420 }
421
422 static int iwl4965_send_scan_abort(struct iwl_priv *priv)
423 {
424 int ret = 0;
425 struct iwl_rx_packet *res;
426 struct iwl_host_cmd cmd = {
427 .id = REPLY_SCAN_ABORT_CMD,
428 .meta.flags = CMD_WANT_SKB,
429 };
430
431 /* If there isn't a scan actively going on in the hardware
432 * then we are in between scan bands and not actually
433 * actively scanning, so don't send the abort command */
434 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
435 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
436 return 0;
437 }
438
439 ret = iwl_send_cmd_sync(priv, &cmd);
440 if (ret) {
441 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
442 return ret;
443 }
444
445 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
446 if (res->u.status != CAN_ABORT_STATUS) {
447 /* The scan abort will return 1 for success or
448 * 2 for "failure". A failure condition can be
449 * due to simply not being in an active scan which
450 * can occur if we send the scan abort before we
451 * the microcode has notified us that a scan is
452 * completed. */
453 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
454 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
455 clear_bit(STATUS_SCAN_HW, &priv->status);
456 }
457
458 dev_kfree_skb_any(cmd.meta.u.skb);
459
460 return ret;
461 }
462
463 /*
464 * CARD_STATE_CMD
465 *
466 * Use: Sets the device's internal card state to enable, disable, or halt
467 *
468 * When in the 'enable' state the card operates as normal.
469 * When in the 'disable' state, the card enters into a low power mode.
470 * When in the 'halt' state, the card is shut down and must be fully
471 * restarted to come back on.
472 */
473 static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
474 {
475 struct iwl_host_cmd cmd = {
476 .id = REPLY_CARD_STATE_CMD,
477 .len = sizeof(u32),
478 .data = &flags,
479 .meta.flags = meta_flag,
480 };
481
482 return iwl_send_cmd(priv, &cmd);
483 }
484
485 static void iwl_clear_free_frames(struct iwl_priv *priv)
486 {
487 struct list_head *element;
488
489 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
490 priv->frames_count);
491
492 while (!list_empty(&priv->free_frames)) {
493 element = priv->free_frames.next;
494 list_del(element);
495 kfree(list_entry(element, struct iwl_frame, list));
496 priv->frames_count--;
497 }
498
499 if (priv->frames_count) {
500 IWL_WARNING("%d frames still in use. Did we lose one?\n",
501 priv->frames_count);
502 priv->frames_count = 0;
503 }
504 }
505
506 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
507 {
508 struct iwl_frame *frame;
509 struct list_head *element;
510 if (list_empty(&priv->free_frames)) {
511 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
512 if (!frame) {
513 IWL_ERROR("Could not allocate frame!\n");
514 return NULL;
515 }
516
517 priv->frames_count++;
518 return frame;
519 }
520
521 element = priv->free_frames.next;
522 list_del(element);
523 return list_entry(element, struct iwl_frame, list);
524 }
525
526 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
527 {
528 memset(frame, 0, sizeof(*frame));
529 list_add(&frame->list, &priv->free_frames);
530 }
531
532 unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
533 struct ieee80211_hdr *hdr,
534 const u8 *dest, int left)
535 {
536
537 if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
538 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
539 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
540 return 0;
541
542 if (priv->ibss_beacon->len > left)
543 return 0;
544
545 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
546
547 return priv->ibss_beacon->len;
548 }
549
550 static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
551 {
552 int i;
553 int rate_mask;
554
555 /* Set rate mask*/
556 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
557 rate_mask = priv->active_rate_basic & 0xF;
558 else
559 rate_mask = priv->active_rate_basic & 0xFF0;
560
561 /* Find lowest valid rate */
562 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
563 i = iwl_rates[i].next_ieee) {
564 if (rate_mask & (1 << i))
565 return iwl_rates[i].plcp;
566 }
567
568 /* No valid rate was found. Assign the lowest one */
569 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
570 return IWL_RATE_1M_PLCP;
571 else
572 return IWL_RATE_6M_PLCP;
573 }
574
575 static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
576 {
577 struct iwl_frame *frame;
578 unsigned int frame_size;
579 int rc;
580 u8 rate;
581
582 frame = iwl_get_free_frame(priv);
583
584 if (!frame) {
585 IWL_ERROR("Could not obtain free frame buffer for beacon "
586 "command.\n");
587 return -ENOMEM;
588 }
589
590 rate = iwl4965_rate_get_lowest_plcp(priv);
591
592 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
593
594 rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
595 &frame->u.cmd[0]);
596
597 iwl_free_frame(priv, frame);
598
599 return rc;
600 }
601
602 /******************************************************************************
603 *
604 * Misc. internal state and helper functions
605 *
606 ******************************************************************************/
607
608 /**
609 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
610 *
611 * return : set the bit for each supported rate insert in ie
612 */
613 static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
614 u16 basic_rate, int *left)
615 {
616 u16 ret_rates = 0, bit;
617 int i;
618 u8 *cnt = ie;
619 u8 *rates = ie + 1;
620
621 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
622 if (bit & supported_rate) {
623 ret_rates |= bit;
624 rates[*cnt] = iwl_rates[i].ieee |
625 ((bit & basic_rate) ? 0x80 : 0x00);
626 (*cnt)++;
627 (*left)--;
628 if ((*left <= 0) ||
629 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
630 break;
631 }
632 }
633
634 return ret_rates;
635 }
636
637 static void iwl4965_ht_conf(struct iwl_priv *priv,
638 struct ieee80211_bss_conf *bss_conf)
639 {
640 struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
641 struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
642 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
643
644 IWL_DEBUG_MAC80211("enter: \n");
645
646 iwl_conf->is_ht = bss_conf->assoc_ht;
647
648 if (!iwl_conf->is_ht)
649 return;
650
651 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
652
653 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
654 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
655 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
656 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
657
658 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
659 iwl_conf->max_amsdu_size =
660 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
661
662 iwl_conf->supported_chan_width =
663 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
664 iwl_conf->extension_chan_offset =
665 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
666 /* If no above or below channel supplied disable FAT channel */
667 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
668 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
669 iwl_conf->supported_chan_width = 0;
670
671 iwl_conf->tx_mimo_ps_mode =
672 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
673 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
674
675 iwl_conf->control_channel = ht_bss_conf->primary_channel;
676 iwl_conf->tx_chan_width =
677 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
678 iwl_conf->ht_protection =
679 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
680 iwl_conf->non_GF_STA_present =
681 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
682
683 IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
684 IWL_DEBUG_MAC80211("leave\n");
685 }
686
687 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
688 u8 *pos, int *left)
689 {
690 struct ieee80211_ht_cap *ht_cap;
691
692 if (!sband || !sband->ht_info.ht_supported)
693 return;
694
695 if (*left < sizeof(struct ieee80211_ht_cap))
696 return;
697
698 *pos++ = sizeof(struct ieee80211_ht_cap);
699 ht_cap = (struct ieee80211_ht_cap *) pos;
700
701 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
702 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
703 ht_cap->ampdu_params_info =
704 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
705 ((sband->ht_info.ampdu_density << 2) &
706 IEEE80211_HT_CAP_AMPDU_DENSITY);
707 *left -= sizeof(struct ieee80211_ht_cap);
708 }
709
710 /**
711 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
712 */
713 static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
714 enum ieee80211_band band,
715 struct ieee80211_mgmt *frame,
716 int left, int is_direct)
717 {
718 int len = 0;
719 u8 *pos = NULL;
720 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
721 const struct ieee80211_supported_band *sband =
722 iwl_get_hw_mode(priv, band);
723
724 /* Make sure there is enough space for the probe request,
725 * two mandatory IEs and the data */
726 left -= 24;
727 if (left < 0)
728 return 0;
729 len += 24;
730
731 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
732 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
733 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
734 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
735 frame->seq_ctrl = 0;
736
737 /* fill in our indirect SSID IE */
738 /* ...next IE... */
739
740 left -= 2;
741 if (left < 0)
742 return 0;
743 len += 2;
744 pos = &(frame->u.probe_req.variable[0]);
745 *pos++ = WLAN_EID_SSID;
746 *pos++ = 0;
747
748 /* fill in our direct SSID IE... */
749 if (is_direct) {
750 /* ...next IE... */
751 left -= 2 + priv->essid_len;
752 if (left < 0)
753 return 0;
754 /* ... fill it in... */
755 *pos++ = WLAN_EID_SSID;
756 *pos++ = priv->essid_len;
757 memcpy(pos, priv->essid, priv->essid_len);
758 pos += priv->essid_len;
759 len += 2 + priv->essid_len;
760 }
761
762 /* fill in supported rate */
763 /* ...next IE... */
764 left -= 2;
765 if (left < 0)
766 return 0;
767
768 /* ... fill it in... */
769 *pos++ = WLAN_EID_SUPP_RATES;
770 *pos = 0;
771
772 /* exclude 60M rate */
773 active_rates = priv->rates_mask;
774 active_rates &= ~IWL_RATE_60M_MASK;
775
776 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
777
778 cck_rates = IWL_CCK_RATES_MASK & active_rates;
779 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
780 active_rate_basic, &left);
781 active_rates &= ~ret_rates;
782
783 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
784 active_rate_basic, &left);
785 active_rates &= ~ret_rates;
786
787 len += 2 + *pos;
788 pos += (*pos) + 1;
789 if (active_rates == 0)
790 goto fill_end;
791
792 /* fill in supported extended rate */
793 /* ...next IE... */
794 left -= 2;
795 if (left < 0)
796 return 0;
797 /* ... fill it in... */
798 *pos++ = WLAN_EID_EXT_SUPP_RATES;
799 *pos = 0;
800 iwl4965_supported_rate_to_ie(pos, active_rates,
801 active_rate_basic, &left);
802 if (*pos > 0)
803 len += 2 + *pos;
804
805 fill_end:
806 /* fill in HT IE */
807 left -= 2;
808 if (left < 0)
809 return 0;
810
811 *pos++ = WLAN_EID_HT_CAPABILITY;
812 *pos = 0;
813
814 iwl_ht_cap_to_ie(sband, pos, &left);
815
816 if (*pos > 0)
817 len += 2 + *pos;
818 return (u16)len;
819 }
820
821 /*
822 * QoS support
823 */
824 static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
825 struct iwl4965_qosparam_cmd *qos)
826 {
827
828 return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
829 sizeof(struct iwl4965_qosparam_cmd), qos);
830 }
831
832 static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
833 {
834 unsigned long flags;
835
836 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
837 return;
838
839 if (!priv->qos_data.qos_enable)
840 return;
841
842 spin_lock_irqsave(&priv->lock, flags);
843 priv->qos_data.def_qos_parm.qos_flags = 0;
844
845 if (priv->qos_data.qos_cap.q_AP.queue_request &&
846 !priv->qos_data.qos_cap.q_AP.txop_request)
847 priv->qos_data.def_qos_parm.qos_flags |=
848 QOS_PARAM_FLG_TXOP_TYPE_MSK;
849 if (priv->qos_data.qos_active)
850 priv->qos_data.def_qos_parm.qos_flags |=
851 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
852
853 if (priv->current_ht_config.is_ht)
854 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
855
856 spin_unlock_irqrestore(&priv->lock, flags);
857
858 if (force || iwl_is_associated(priv)) {
859 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
860 priv->qos_data.qos_active,
861 priv->qos_data.def_qos_parm.qos_flags);
862
863 iwl4965_send_qos_params_command(priv,
864 &(priv->qos_data.def_qos_parm));
865 }
866 }
867
868 int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
869 {
870 /* Filter incoming packets to determine if they are targeted toward
871 * this network, discarding packets coming from ourselves */
872 switch (priv->iw_mode) {
873 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
874 /* packets from our adapter are dropped (echo) */
875 if (!compare_ether_addr(header->addr2, priv->mac_addr))
876 return 0;
877 /* {broad,multi}cast packets to our IBSS go through */
878 if (is_multicast_ether_addr(header->addr1))
879 return !compare_ether_addr(header->addr3, priv->bssid);
880 /* packets to our adapter go through */
881 return !compare_ether_addr(header->addr1, priv->mac_addr);
882 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
883 /* packets from our adapter are dropped (echo) */
884 if (!compare_ether_addr(header->addr3, priv->mac_addr))
885 return 0;
886 /* {broad,multi}cast packets to our BSS go through */
887 if (is_multicast_ether_addr(header->addr1))
888 return !compare_ether_addr(header->addr2, priv->bssid);
889 /* packets to our adapter go through */
890 return !compare_ether_addr(header->addr1, priv->mac_addr);
891 default:
892 break;
893 }
894
895 return 1;
896 }
897
898
899
900 /**
901 * iwl4965_scan_cancel - Cancel any currently executing HW scan
902 *
903 * NOTE: priv->mutex is not required before calling this function
904 */
905 static int iwl4965_scan_cancel(struct iwl_priv *priv)
906 {
907 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
908 clear_bit(STATUS_SCANNING, &priv->status);
909 return 0;
910 }
911
912 if (test_bit(STATUS_SCANNING, &priv->status)) {
913 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
914 IWL_DEBUG_SCAN("Queuing scan abort.\n");
915 set_bit(STATUS_SCAN_ABORTING, &priv->status);
916 queue_work(priv->workqueue, &priv->abort_scan);
917
918 } else
919 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
920
921 return test_bit(STATUS_SCANNING, &priv->status);
922 }
923
924 return 0;
925 }
926
927 /**
928 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
929 * @ms: amount of time to wait (in milliseconds) for scan to abort
930 *
931 * NOTE: priv->mutex must be held before calling this function
932 */
933 static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
934 {
935 unsigned long now = jiffies;
936 int ret;
937
938 ret = iwl4965_scan_cancel(priv);
939 if (ret && ms) {
940 mutex_unlock(&priv->mutex);
941 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
942 test_bit(STATUS_SCANNING, &priv->status))
943 msleep(1);
944 mutex_lock(&priv->mutex);
945
946 return test_bit(STATUS_SCANNING, &priv->status);
947 }
948
949 return ret;
950 }
951
952 static void iwl4965_sequence_reset(struct iwl_priv *priv)
953 {
954 /* Reset ieee stats */
955
956 /* We don't reset the net_device_stats (ieee->stats) on
957 * re-association */
958
959 priv->last_seq_num = -1;
960 priv->last_frag_num = -1;
961 priv->last_packet_time = 0;
962
963 iwl4965_scan_cancel(priv);
964 }
965
966 #define MAX_UCODE_BEACON_INTERVAL 4096
967 #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
968
969 static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
970 {
971 u16 new_val = 0;
972 u16 beacon_factor = 0;
973
974 beacon_factor =
975 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
976 / MAX_UCODE_BEACON_INTERVAL;
977 new_val = beacon_val / beacon_factor;
978
979 return cpu_to_le16(new_val);
980 }
981
982 static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
983 {
984 u64 interval_tm_unit;
985 u64 tsf, result;
986 unsigned long flags;
987 struct ieee80211_conf *conf = NULL;
988 u16 beacon_int = 0;
989
990 conf = ieee80211_get_hw_conf(priv->hw);
991
992 spin_lock_irqsave(&priv->lock, flags);
993 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
994 priv->rxon_timing.timestamp.dw[0] =
995 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
996
997 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
998
999 tsf = priv->timestamp;
1000
1001 beacon_int = priv->beacon_int;
1002 spin_unlock_irqrestore(&priv->lock, flags);
1003
1004 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1005 if (beacon_int == 0) {
1006 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1007 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1008 } else {
1009 priv->rxon_timing.beacon_interval =
1010 cpu_to_le16(beacon_int);
1011 priv->rxon_timing.beacon_interval =
1012 iwl4965_adjust_beacon_interval(
1013 le16_to_cpu(priv->rxon_timing.beacon_interval));
1014 }
1015
1016 priv->rxon_timing.atim_window = 0;
1017 } else {
1018 priv->rxon_timing.beacon_interval =
1019 iwl4965_adjust_beacon_interval(conf->beacon_int);
1020 /* TODO: we need to get atim_window from upper stack
1021 * for now we set to 0 */
1022 priv->rxon_timing.atim_window = 0;
1023 }
1024
1025 interval_tm_unit =
1026 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1027 result = do_div(tsf, interval_tm_unit);
1028 priv->rxon_timing.beacon_init_val =
1029 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1030
1031 IWL_DEBUG_ASSOC
1032 ("beacon interval %d beacon timer %d beacon tim %d\n",
1033 le16_to_cpu(priv->rxon_timing.beacon_interval),
1034 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1035 le16_to_cpu(priv->rxon_timing.atim_window));
1036 }
1037
1038 static int iwl4965_scan_initiate(struct iwl_priv *priv)
1039 {
1040 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1041 IWL_ERROR("APs don't scan.\n");
1042 return 0;
1043 }
1044
1045 if (!iwl_is_ready_rf(priv)) {
1046 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1047 return -EIO;
1048 }
1049
1050 if (test_bit(STATUS_SCANNING, &priv->status)) {
1051 IWL_DEBUG_SCAN("Scan already in progress.\n");
1052 return -EAGAIN;
1053 }
1054
1055 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1056 IWL_DEBUG_SCAN("Scan request while abort pending. "
1057 "Queuing.\n");
1058 return -EAGAIN;
1059 }
1060
1061 IWL_DEBUG_INFO("Starting scan...\n");
1062 priv->scan_bands = 2;
1063 set_bit(STATUS_SCANNING, &priv->status);
1064 priv->scan_start = jiffies;
1065 priv->scan_pass_start = priv->scan_start;
1066
1067 queue_work(priv->workqueue, &priv->request_scan);
1068
1069 return 0;
1070 }
1071
1072
1073 static void iwl_set_flags_for_band(struct iwl_priv *priv,
1074 enum ieee80211_band band)
1075 {
1076 if (band == IEEE80211_BAND_5GHZ) {
1077 priv->staging_rxon.flags &=
1078 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1079 | RXON_FLG_CCK_MSK);
1080 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1081 } else {
1082 /* Copied from iwl4965_post_associate() */
1083 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1084 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1085 else
1086 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1087
1088 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1089 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1090
1091 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1092 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1093 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1094 }
1095 }
1096
1097 /*
1098 * initialize rxon structure with default values from eeprom
1099 */
1100 static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
1101 {
1102 const struct iwl_channel_info *ch_info;
1103
1104 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1105
1106 switch (priv->iw_mode) {
1107 case IEEE80211_IF_TYPE_AP:
1108 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1109 break;
1110
1111 case IEEE80211_IF_TYPE_STA:
1112 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1113 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1114 break;
1115
1116 case IEEE80211_IF_TYPE_IBSS:
1117 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1118 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1119 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1120 RXON_FILTER_ACCEPT_GRP_MSK;
1121 break;
1122
1123 case IEEE80211_IF_TYPE_MNTR:
1124 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1125 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1126 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1127 break;
1128 default:
1129 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1130 break;
1131 }
1132
1133 #if 0
1134 /* TODO: Figure out when short_preamble would be set and cache from
1135 * that */
1136 if (!hw_to_local(priv->hw)->short_preamble)
1137 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1138 else
1139 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1140 #endif
1141
1142 ch_info = iwl_get_channel_info(priv, priv->band,
1143 le16_to_cpu(priv->staging_rxon.channel));
1144
1145 if (!ch_info)
1146 ch_info = &priv->channel_info[0];
1147
1148 /*
1149 * in some case A channels are all non IBSS
1150 * in this case force B/G channel
1151 */
1152 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1153 !(is_channel_ibss(ch_info)))
1154 ch_info = &priv->channel_info[0];
1155
1156 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
1157 priv->band = ch_info->band;
1158
1159 iwl_set_flags_for_band(priv, priv->band);
1160
1161 priv->staging_rxon.ofdm_basic_rates =
1162 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1163 priv->staging_rxon.cck_basic_rates =
1164 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1165
1166 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1167 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1168 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1169 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1170 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1171 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1172 iwl_set_rxon_chain(priv);
1173 }
1174
1175 static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
1176 {
1177 if (mode == IEEE80211_IF_TYPE_IBSS) {
1178 const struct iwl_channel_info *ch_info;
1179
1180 ch_info = iwl_get_channel_info(priv,
1181 priv->band,
1182 le16_to_cpu(priv->staging_rxon.channel));
1183
1184 if (!ch_info || !is_channel_ibss(ch_info)) {
1185 IWL_ERROR("channel %d not IBSS channel\n",
1186 le16_to_cpu(priv->staging_rxon.channel));
1187 return -EINVAL;
1188 }
1189 }
1190
1191 priv->iw_mode = mode;
1192
1193 iwl4965_connection_init_rx_config(priv);
1194 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1195
1196 iwlcore_clear_stations_table(priv);
1197
1198 /* dont commit rxon if rf-kill is on*/
1199 if (!iwl_is_ready_rf(priv))
1200 return -EAGAIN;
1201
1202 cancel_delayed_work(&priv->scan_check);
1203 if (iwl4965_scan_cancel_timeout(priv, 100)) {
1204 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1205 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1206 return -EAGAIN;
1207 }
1208
1209 iwl4965_commit_rxon(priv);
1210
1211 return 0;
1212 }
1213
1214 static void iwl4965_set_rate(struct iwl_priv *priv)
1215 {
1216 const struct ieee80211_supported_band *hw = NULL;
1217 struct ieee80211_rate *rate;
1218 int i;
1219
1220 hw = iwl_get_hw_mode(priv, priv->band);
1221 if (!hw) {
1222 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
1223 return;
1224 }
1225
1226 priv->active_rate = 0;
1227 priv->active_rate_basic = 0;
1228
1229 for (i = 0; i < hw->n_bitrates; i++) {
1230 rate = &(hw->bitrates[i]);
1231 if (rate->hw_value < IWL_RATE_COUNT)
1232 priv->active_rate |= (1 << rate->hw_value);
1233 }
1234
1235 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
1236 priv->active_rate, priv->active_rate_basic);
1237
1238 /*
1239 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
1240 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
1241 * OFDM
1242 */
1243 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
1244 priv->staging_rxon.cck_basic_rates =
1245 ((priv->active_rate_basic &
1246 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
1247 else
1248 priv->staging_rxon.cck_basic_rates =
1249 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1250
1251 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
1252 priv->staging_rxon.ofdm_basic_rates =
1253 ((priv->active_rate_basic &
1254 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
1255 IWL_FIRST_OFDM_RATE) & 0xFF;
1256 else
1257 priv->staging_rxon.ofdm_basic_rates =
1258 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1259 }
1260
1261 void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
1262 {
1263 unsigned long flags;
1264
1265 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1266 return;
1267
1268 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
1269 disable_radio ? "OFF" : "ON");
1270
1271 if (disable_radio) {
1272 iwl4965_scan_cancel(priv);
1273 /* FIXME: This is a workaround for AP */
1274 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
1275 spin_lock_irqsave(&priv->lock, flags);
1276 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1277 CSR_UCODE_SW_BIT_RFKILL);
1278 spin_unlock_irqrestore(&priv->lock, flags);
1279 /* call the host command only if no hw rf-kill set */
1280 if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
1281 iwl_is_ready(priv))
1282 iwl4965_send_card_state(priv,
1283 CARD_STATE_CMD_DISABLE,
1284 0);
1285 set_bit(STATUS_RF_KILL_SW, &priv->status);
1286
1287 /* make sure mac80211 stop sending Tx frame */
1288 if (priv->mac80211_registered)
1289 ieee80211_stop_queues(priv->hw);
1290 }
1291 return;
1292 }
1293
1294 spin_lock_irqsave(&priv->lock, flags);
1295 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1296
1297 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1298 spin_unlock_irqrestore(&priv->lock, flags);
1299
1300 /* wake up ucode */
1301 msleep(10);
1302
1303 spin_lock_irqsave(&priv->lock, flags);
1304 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1305 if (!iwl_grab_nic_access(priv))
1306 iwl_release_nic_access(priv);
1307 spin_unlock_irqrestore(&priv->lock, flags);
1308
1309 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1310 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
1311 "disabled by HW switch\n");
1312 return;
1313 }
1314
1315 queue_work(priv->workqueue, &priv->restart);
1316 return;
1317 }
1318
1319 #define IWL_PACKET_RETRY_TIME HZ
1320
1321 int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
1322 {
1323 u16 sc = le16_to_cpu(header->seq_ctrl);
1324 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1325 u16 frag = sc & IEEE80211_SCTL_FRAG;
1326 u16 *last_seq, *last_frag;
1327 unsigned long *last_time;
1328
1329 switch (priv->iw_mode) {
1330 case IEEE80211_IF_TYPE_IBSS:{
1331 struct list_head *p;
1332 struct iwl4965_ibss_seq *entry = NULL;
1333 u8 *mac = header->addr2;
1334 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
1335
1336 __list_for_each(p, &priv->ibss_mac_hash[index]) {
1337 entry = list_entry(p, struct iwl4965_ibss_seq, list);
1338 if (!compare_ether_addr(entry->mac, mac))
1339 break;
1340 }
1341 if (p == &priv->ibss_mac_hash[index]) {
1342 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1343 if (!entry) {
1344 IWL_ERROR("Cannot malloc new mac entry\n");
1345 return 0;
1346 }
1347 memcpy(entry->mac, mac, ETH_ALEN);
1348 entry->seq_num = seq;
1349 entry->frag_num = frag;
1350 entry->packet_time = jiffies;
1351 list_add(&entry->list, &priv->ibss_mac_hash[index]);
1352 return 0;
1353 }
1354 last_seq = &entry->seq_num;
1355 last_frag = &entry->frag_num;
1356 last_time = &entry->packet_time;
1357 break;
1358 }
1359 case IEEE80211_IF_TYPE_STA:
1360 last_seq = &priv->last_seq_num;
1361 last_frag = &priv->last_frag_num;
1362 last_time = &priv->last_packet_time;
1363 break;
1364 default:
1365 return 0;
1366 }
1367 if ((*last_seq == seq) &&
1368 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
1369 if (*last_frag == frag)
1370 goto drop;
1371 if (*last_frag + 1 != frag)
1372 /* out-of-order fragment */
1373 goto drop;
1374 } else
1375 *last_seq = seq;
1376
1377 *last_frag = frag;
1378 *last_time = jiffies;
1379 return 0;
1380
1381 drop:
1382 return 1;
1383 }
1384
1385 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
1386
1387 #include "iwl-spectrum.h"
1388
1389 #define BEACON_TIME_MASK_LOW 0x00FFFFFF
1390 #define BEACON_TIME_MASK_HIGH 0xFF000000
1391 #define TIME_UNIT 1024
1392
1393 /*
1394 * extended beacon time format
1395 * time in usec will be changed into a 32-bit value in 8:24 format
1396 * the high 1 byte is the beacon counts
1397 * the lower 3 bytes is the time in usec within one beacon interval
1398 */
1399
1400 static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
1401 {
1402 u32 quot;
1403 u32 rem;
1404 u32 interval = beacon_interval * 1024;
1405
1406 if (!interval || !usec)
1407 return 0;
1408
1409 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1410 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1411
1412 return (quot << 24) + rem;
1413 }
1414
1415 /* base is usually what we get from ucode with each received frame,
1416 * the same as HW timer counter counting down
1417 */
1418
1419 static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
1420 {
1421 u32 base_low = base & BEACON_TIME_MASK_LOW;
1422 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1423 u32 interval = beacon_interval * TIME_UNIT;
1424 u32 res = (base & BEACON_TIME_MASK_HIGH) +
1425 (addon & BEACON_TIME_MASK_HIGH);
1426
1427 if (base_low > addon_low)
1428 res += base_low - addon_low;
1429 else if (base_low < addon_low) {
1430 res += interval + base_low - addon_low;
1431 res += (1 << 24);
1432 } else
1433 res += (1 << 24);
1434
1435 return cpu_to_le32(res);
1436 }
1437
1438 static int iwl4965_get_measurement(struct iwl_priv *priv,
1439 struct ieee80211_measurement_params *params,
1440 u8 type)
1441 {
1442 struct iwl4965_spectrum_cmd spectrum;
1443 struct iwl_rx_packet *res;
1444 struct iwl_host_cmd cmd = {
1445 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1446 .data = (void *)&spectrum,
1447 .meta.flags = CMD_WANT_SKB,
1448 };
1449 u32 add_time = le64_to_cpu(params->start_time);
1450 int rc;
1451 int spectrum_resp_status;
1452 int duration = le16_to_cpu(params->duration);
1453
1454 if (iwl_is_associated(priv))
1455 add_time =
1456 iwl4965_usecs_to_beacons(
1457 le64_to_cpu(params->start_time) - priv->last_tsf,
1458 le16_to_cpu(priv->rxon_timing.beacon_interval));
1459
1460 memset(&spectrum, 0, sizeof(spectrum));
1461
1462 spectrum.channel_count = cpu_to_le16(1);
1463 spectrum.flags =
1464 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1465 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1466 cmd.len = sizeof(spectrum);
1467 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1468
1469 if (iwl_is_associated(priv))
1470 spectrum.start_time =
1471 iwl4965_add_beacon_time(priv->last_beacon_time,
1472 add_time,
1473 le16_to_cpu(priv->rxon_timing.beacon_interval));
1474 else
1475 spectrum.start_time = 0;
1476
1477 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1478 spectrum.channels[0].channel = params->channel;
1479 spectrum.channels[0].type = type;
1480 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1481 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1482 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1483
1484 rc = iwl_send_cmd_sync(priv, &cmd);
1485 if (rc)
1486 return rc;
1487
1488 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1489 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1490 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1491 rc = -EIO;
1492 }
1493
1494 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1495 switch (spectrum_resp_status) {
1496 case 0: /* Command will be handled */
1497 if (res->u.spectrum.id != 0xff) {
1498 IWL_DEBUG_INFO
1499 ("Replaced existing measurement: %d\n",
1500 res->u.spectrum.id);
1501 priv->measurement_status &= ~MEASUREMENT_READY;
1502 }
1503 priv->measurement_status |= MEASUREMENT_ACTIVE;
1504 rc = 0;
1505 break;
1506
1507 case 1: /* Command will not be handled */
1508 rc = -EAGAIN;
1509 break;
1510 }
1511
1512 dev_kfree_skb_any(cmd.meta.u.skb);
1513
1514 return rc;
1515 }
1516 #endif
1517
1518 /******************************************************************************
1519 *
1520 * Generic RX handler implementations
1521 *
1522 ******************************************************************************/
1523 static void iwl_rx_reply_alive(struct iwl_priv *priv,
1524 struct iwl_rx_mem_buffer *rxb)
1525 {
1526 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1527 struct iwl_alive_resp *palive;
1528 struct delayed_work *pwork;
1529
1530 palive = &pkt->u.alive_frame;
1531
1532 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1533 "0x%01X 0x%01X\n",
1534 palive->is_valid, palive->ver_type,
1535 palive->ver_subtype);
1536
1537 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1538 IWL_DEBUG_INFO("Initialization Alive received.\n");
1539 memcpy(&priv->card_alive_init,
1540 &pkt->u.alive_frame,
1541 sizeof(struct iwl_init_alive_resp));
1542 pwork = &priv->init_alive_start;
1543 } else {
1544 IWL_DEBUG_INFO("Runtime Alive received.\n");
1545 memcpy(&priv->card_alive, &pkt->u.alive_frame,
1546 sizeof(struct iwl_alive_resp));
1547 pwork = &priv->alive_start;
1548 }
1549
1550 /* We delay the ALIVE response by 5ms to
1551 * give the HW RF Kill time to activate... */
1552 if (palive->is_valid == UCODE_VALID_OK)
1553 queue_delayed_work(priv->workqueue, pwork,
1554 msecs_to_jiffies(5));
1555 else
1556 IWL_WARNING("uCode did not respond OK.\n");
1557 }
1558
1559 static void iwl4965_rx_reply_error(struct iwl_priv *priv,
1560 struct iwl_rx_mem_buffer *rxb)
1561 {
1562 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1563
1564 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1565 "seq 0x%04X ser 0x%08X\n",
1566 le32_to_cpu(pkt->u.err_resp.error_type),
1567 get_cmd_string(pkt->u.err_resp.cmd_id),
1568 pkt->u.err_resp.cmd_id,
1569 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1570 le32_to_cpu(pkt->u.err_resp.error_info));
1571 }
1572
1573 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1574
1575 static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1576 {
1577 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1578 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
1579 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
1580 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1581 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1582 rxon->channel = csa->channel;
1583 priv->staging_rxon.channel = csa->channel;
1584 }
1585
1586 static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
1587 struct iwl_rx_mem_buffer *rxb)
1588 {
1589 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
1590 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1591 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
1592
1593 if (!report->state) {
1594 IWL_DEBUG(IWL_DL_11H,
1595 "Spectrum Measure Notification: Start\n");
1596 return;
1597 }
1598
1599 memcpy(&priv->measure_report, report, sizeof(*report));
1600 priv->measurement_status |= MEASUREMENT_READY;
1601 #endif
1602 }
1603
1604 static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
1605 struct iwl_rx_mem_buffer *rxb)
1606 {
1607 #ifdef CONFIG_IWLWIFI_DEBUG
1608 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1609 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
1610 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1611 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1612 #endif
1613 }
1614
1615 static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1616 struct iwl_rx_mem_buffer *rxb)
1617 {
1618 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1619 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1620 "notification for %s:\n",
1621 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
1622 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
1623 }
1624
1625 static void iwl4965_bg_beacon_update(struct work_struct *work)
1626 {
1627 struct iwl_priv *priv =
1628 container_of(work, struct iwl_priv, beacon_update);
1629 struct sk_buff *beacon;
1630
1631 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
1632 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
1633
1634 if (!beacon) {
1635 IWL_ERROR("update beacon failed\n");
1636 return;
1637 }
1638
1639 mutex_lock(&priv->mutex);
1640 /* new beacon skb is allocated every time; dispose previous.*/
1641 if (priv->ibss_beacon)
1642 dev_kfree_skb(priv->ibss_beacon);
1643
1644 priv->ibss_beacon = beacon;
1645 mutex_unlock(&priv->mutex);
1646
1647 iwl4965_send_beacon_cmd(priv);
1648 }
1649
1650 static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
1651 struct iwl_rx_mem_buffer *rxb)
1652 {
1653 #ifdef CONFIG_IWLWIFI_DEBUG
1654 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1655 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
1656 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
1657
1658 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1659 "tsf %d %d rate %d\n",
1660 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1661 beacon->beacon_notify_hdr.failure_frame,
1662 le32_to_cpu(beacon->ibss_mgr_status),
1663 le32_to_cpu(beacon->high_tsf),
1664 le32_to_cpu(beacon->low_tsf), rate);
1665 #endif
1666
1667 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1668 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1669 queue_work(priv->workqueue, &priv->beacon_update);
1670 }
1671
1672 /* Service response to REPLY_SCAN_CMD (0x80) */
1673 static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
1674 struct iwl_rx_mem_buffer *rxb)
1675 {
1676 #ifdef CONFIG_IWLWIFI_DEBUG
1677 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1678 struct iwl4965_scanreq_notification *notif =
1679 (struct iwl4965_scanreq_notification *)pkt->u.raw;
1680
1681 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
1682 #endif
1683 }
1684
1685 /* Service SCAN_START_NOTIFICATION (0x82) */
1686 static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
1687 struct iwl_rx_mem_buffer *rxb)
1688 {
1689 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1690 struct iwl4965_scanstart_notification *notif =
1691 (struct iwl4965_scanstart_notification *)pkt->u.raw;
1692 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
1693 IWL_DEBUG_SCAN("Scan start: "
1694 "%d [802.11%s] "
1695 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
1696 notif->channel,
1697 notif->band ? "bg" : "a",
1698 notif->tsf_high,
1699 notif->tsf_low, notif->status, notif->beacon_timer);
1700 }
1701
1702 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
1703 static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
1704 struct iwl_rx_mem_buffer *rxb)
1705 {
1706 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1707 struct iwl4965_scanresults_notification *notif =
1708 (struct iwl4965_scanresults_notification *)pkt->u.raw;
1709
1710 IWL_DEBUG_SCAN("Scan ch.res: "
1711 "%d [802.11%s] "
1712 "(TSF: 0x%08X:%08X) - %d "
1713 "elapsed=%lu usec (%dms since last)\n",
1714 notif->channel,
1715 notif->band ? "bg" : "a",
1716 le32_to_cpu(notif->tsf_high),
1717 le32_to_cpu(notif->tsf_low),
1718 le32_to_cpu(notif->statistics[0]),
1719 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
1720 jiffies_to_msecs(elapsed_jiffies
1721 (priv->last_scan_jiffies, jiffies)));
1722
1723 priv->last_scan_jiffies = jiffies;
1724 priv->next_scan_jiffies = 0;
1725 }
1726
1727 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
1728 static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
1729 struct iwl_rx_mem_buffer *rxb)
1730 {
1731 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1732 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
1733
1734 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
1735 scan_notif->scanned_channels,
1736 scan_notif->tsf_low,
1737 scan_notif->tsf_high, scan_notif->status);
1738
1739 /* The HW is no longer scanning */
1740 clear_bit(STATUS_SCAN_HW, &priv->status);
1741
1742 /* The scan completion notification came in, so kill that timer... */
1743 cancel_delayed_work(&priv->scan_check);
1744
1745 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
1746 (priv->scan_bands == 2) ? "2.4" : "5.2",
1747 jiffies_to_msecs(elapsed_jiffies
1748 (priv->scan_pass_start, jiffies)));
1749
1750 /* Remove this scanned band from the list
1751 * of pending bands to scan */
1752 priv->scan_bands--;
1753
1754 /* If a request to abort was given, or the scan did not succeed
1755 * then we reset the scan state machine and terminate,
1756 * re-queuing another scan if one has been requested */
1757 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1758 IWL_DEBUG_INFO("Aborted scan completed.\n");
1759 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1760 } else {
1761 /* If there are more bands on this scan pass reschedule */
1762 if (priv->scan_bands > 0)
1763 goto reschedule;
1764 }
1765
1766 priv->last_scan_jiffies = jiffies;
1767 priv->next_scan_jiffies = 0;
1768 IWL_DEBUG_INFO("Setting scan to off\n");
1769
1770 clear_bit(STATUS_SCANNING, &priv->status);
1771
1772 IWL_DEBUG_INFO("Scan took %dms\n",
1773 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
1774
1775 queue_work(priv->workqueue, &priv->scan_completed);
1776
1777 return;
1778
1779 reschedule:
1780 priv->scan_pass_start = jiffies;
1781 queue_work(priv->workqueue, &priv->request_scan);
1782 }
1783
1784 /* Handle notification from uCode that card's power state is changing
1785 * due to software, hardware, or critical temperature RFKILL */
1786 static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
1787 struct iwl_rx_mem_buffer *rxb)
1788 {
1789 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1790 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1791 unsigned long status = priv->status;
1792
1793 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1794 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1795 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1796
1797 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1798 RF_CARD_DISABLED)) {
1799
1800 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1801 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1802
1803 if (!iwl_grab_nic_access(priv)) {
1804 iwl_write_direct32(
1805 priv, HBUS_TARG_MBX_C,
1806 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1807
1808 iwl_release_nic_access(priv);
1809 }
1810
1811 if (!(flags & RXON_CARD_DISABLED)) {
1812 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
1813 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1814 if (!iwl_grab_nic_access(priv)) {
1815 iwl_write_direct32(
1816 priv, HBUS_TARG_MBX_C,
1817 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1818
1819 iwl_release_nic_access(priv);
1820 }
1821 }
1822
1823 if (flags & RF_CARD_DISABLED) {
1824 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1825 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
1826 iwl_read32(priv, CSR_UCODE_DRV_GP1);
1827 if (!iwl_grab_nic_access(priv))
1828 iwl_release_nic_access(priv);
1829 }
1830 }
1831
1832 if (flags & HW_CARD_DISABLED)
1833 set_bit(STATUS_RF_KILL_HW, &priv->status);
1834 else
1835 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1836
1837
1838 if (flags & SW_CARD_DISABLED)
1839 set_bit(STATUS_RF_KILL_SW, &priv->status);
1840 else
1841 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1842
1843 if (!(flags & RXON_CARD_DISABLED))
1844 iwl4965_scan_cancel(priv);
1845
1846 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1847 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1848 (test_bit(STATUS_RF_KILL_SW, &status) !=
1849 test_bit(STATUS_RF_KILL_SW, &priv->status)))
1850 queue_work(priv->workqueue, &priv->rf_kill);
1851 else
1852 wake_up_interruptible(&priv->wait_command_queue);
1853 }
1854
1855 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
1856 * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
1857 static void iwl4965_rx_reply_rx_phy(struct iwl_priv *priv,
1858 struct iwl_rx_mem_buffer *rxb)
1859 {
1860 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1861 priv->last_phy_res[0] = 1;
1862 memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
1863 sizeof(struct iwl4965_rx_phy_res));
1864 }
1865
1866 /**
1867 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
1868 *
1869 * Setup the RX handlers for each of the reply types sent from the uCode
1870 * to the host.
1871 *
1872 * This function chains into the hardware specific files for them to setup
1873 * any hardware specific handlers as well.
1874 */
1875 static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
1876 {
1877 priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
1878 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1879 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
1880 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
1881 iwl4965_rx_spectrum_measure_notif;
1882 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
1883 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1884 iwl4965_rx_pm_debug_statistics_notif;
1885 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
1886
1887 /*
1888 * The same handler is used for both the REPLY to a discrete
1889 * statistics request from the host as well as for the periodic
1890 * statistics notifications (after received beacons) from the uCode.
1891 */
1892 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
1893 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
1894 /* scan handlers */
1895 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
1896 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
1897 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
1898 iwl4965_rx_scan_results_notif;
1899 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
1900 iwl4965_rx_scan_complete_notif;
1901 /* status change handler */
1902 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
1903
1904 priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1905 iwl_rx_missed_beacon_notif;
1906 /* Rx handlers */
1907 priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
1908 priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
1909 /* Set up hardware specific Rx handlers */
1910 priv->cfg->ops->lib->rx_handler_setup(priv);
1911 }
1912
1913 /*
1914 * this should be called while priv->lock is locked
1915 */
1916 static void __iwl_rx_replenish(struct iwl_priv *priv)
1917 {
1918 iwl_rx_allocate(priv);
1919 iwl_rx_queue_restock(priv);
1920 }
1921
1922
1923 /**
1924 * iwl_rx_handle - Main entry function for receiving responses from uCode
1925 *
1926 * Uses the priv->rx_handlers callback function array to invoke
1927 * the appropriate handlers, including command responses,
1928 * frame-received notifications, and other notifications.
1929 */
1930 void iwl_rx_handle(struct iwl_priv *priv)
1931 {
1932 struct iwl_rx_mem_buffer *rxb;
1933 struct iwl_rx_packet *pkt;
1934 struct iwl_rx_queue *rxq = &priv->rxq;
1935 u32 r, i;
1936 int reclaim;
1937 unsigned long flags;
1938 u8 fill_rx = 0;
1939 u32 count = 8;
1940
1941 /* uCode's read index (stored in shared DRAM) indicates the last Rx
1942 * buffer that the driver may process (last buffer filled by ucode). */
1943 r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
1944 i = rxq->read;
1945
1946 /* Rx interrupt, but nothing sent from uCode */
1947 if (i == r)
1948 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
1949
1950 if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
1951 fill_rx = 1;
1952
1953 while (i != r) {
1954 rxb = rxq->queue[i];
1955
1956 /* If an RXB doesn't have a Rx queue slot associated with it,
1957 * then a bug has been introduced in the queue refilling
1958 * routines -- catch it here */
1959 BUG_ON(rxb == NULL);
1960
1961 rxq->queue[i] = NULL;
1962
1963 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
1964 priv->hw_params.rx_buf_size,
1965 PCI_DMA_FROMDEVICE);
1966 pkt = (struct iwl_rx_packet *)rxb->skb->data;
1967
1968 /* Reclaim a command buffer only if this packet is a response
1969 * to a (driver-originated) command.
1970 * If the packet (e.g. Rx frame) originated from uCode,
1971 * there is no command buffer to reclaim.
1972 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1973 * but apparently a few don't get set; catch them here. */
1974 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1975 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
1976 (pkt->hdr.cmd != REPLY_RX) &&
1977 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
1978 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1979 (pkt->hdr.cmd != REPLY_TX);
1980
1981 /* Based on type of command response or notification,
1982 * handle those that need handling via function in
1983 * rx_handlers table. See iwl4965_setup_rx_handlers() */
1984 if (priv->rx_handlers[pkt->hdr.cmd]) {
1985 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
1986 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1987 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1988 } else {
1989 /* No handling needed */
1990 IWL_DEBUG(IWL_DL_RX,
1991 "r %d i %d No handler needed for %s, 0x%02x\n",
1992 r, i, get_cmd_string(pkt->hdr.cmd),
1993 pkt->hdr.cmd);
1994 }
1995
1996 if (reclaim) {
1997 /* Invoke any callbacks, transfer the skb to caller, and
1998 * fire off the (possibly) blocking iwl_send_cmd()
1999 * as we reclaim the driver command queue */
2000 if (rxb && rxb->skb)
2001 iwl_tx_cmd_complete(priv, rxb);
2002 else
2003 IWL_WARNING("Claim null rxb?\n");
2004 }
2005
2006 /* For now we just don't re-use anything. We can tweak this
2007 * later to try and re-use notification packets and SKBs that
2008 * fail to Rx correctly */
2009 if (rxb->skb != NULL) {
2010 priv->alloc_rxb_skb--;
2011 dev_kfree_skb_any(rxb->skb);
2012 rxb->skb = NULL;
2013 }
2014
2015 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
2016 priv->hw_params.rx_buf_size,
2017 PCI_DMA_FROMDEVICE);
2018 spin_lock_irqsave(&rxq->lock, flags);
2019 list_add_tail(&rxb->list, &priv->rxq.rx_used);
2020 spin_unlock_irqrestore(&rxq->lock, flags);
2021 i = (i + 1) & RX_QUEUE_MASK;
2022 /* If there are a lot of unused frames,
2023 * restock the Rx queue so ucode wont assert. */
2024 if (fill_rx) {
2025 count++;
2026 if (count >= 8) {
2027 priv->rxq.read = i;
2028 __iwl_rx_replenish(priv);
2029 count = 0;
2030 }
2031 }
2032 }
2033
2034 /* Backtrack one entry */
2035 priv->rxq.read = i;
2036 iwl_rx_queue_restock(priv);
2037 }
2038 /* Convert linear signal-to-noise ratio into dB */
2039 static u8 ratio2dB[100] = {
2040 /* 0 1 2 3 4 5 6 7 8 9 */
2041 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
2042 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
2043 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
2044 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
2045 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
2046 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
2047 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
2048 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
2049 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
2050 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
2051 };
2052
2053 /* Calculates a relative dB value from a ratio of linear
2054 * (i.e. not dB) signal levels.
2055 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
2056 int iwl4965_calc_db_from_ratio(int sig_ratio)
2057 {
2058 /* 1000:1 or higher just report as 60 dB */
2059 if (sig_ratio >= 1000)
2060 return 60;
2061
2062 /* 100:1 or higher, divide by 10 and use table,
2063 * add 20 dB to make up for divide by 10 */
2064 if (sig_ratio >= 100)
2065 return (20 + (int)ratio2dB[sig_ratio/10]);
2066
2067 /* We shouldn't see this */
2068 if (sig_ratio < 1)
2069 return 0;
2070
2071 /* Use table for ratios 1:1 - 99:1 */
2072 return (int)ratio2dB[sig_ratio];
2073 }
2074
2075 #define PERFECT_RSSI (-20) /* dBm */
2076 #define WORST_RSSI (-95) /* dBm */
2077 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
2078
2079 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
2080 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
2081 * about formulas used below. */
2082 int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
2083 {
2084 int sig_qual;
2085 int degradation = PERFECT_RSSI - rssi_dbm;
2086
2087 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
2088 * as indicator; formula is (signal dbm - noise dbm).
2089 * SNR at or above 40 is a great signal (100%).
2090 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
2091 * Weakest usable signal is usually 10 - 15 dB SNR. */
2092 if (noise_dbm) {
2093 if (rssi_dbm - noise_dbm >= 40)
2094 return 100;
2095 else if (rssi_dbm < noise_dbm)
2096 return 0;
2097 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
2098
2099 /* Else use just the signal level.
2100 * This formula is a least squares fit of data points collected and
2101 * compared with a reference system that had a percentage (%) display
2102 * for signal quality. */
2103 } else
2104 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
2105 (15 * RSSI_RANGE + 62 * degradation)) /
2106 (RSSI_RANGE * RSSI_RANGE);
2107
2108 if (sig_qual > 100)
2109 sig_qual = 100;
2110 else if (sig_qual < 1)
2111 sig_qual = 0;
2112
2113 return sig_qual;
2114 }
2115
2116 #ifdef CONFIG_IWLWIFI_DEBUG
2117 static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
2118 {
2119 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2120 DECLARE_MAC_BUF(mac);
2121
2122 IWL_DEBUG_RADIO("RX CONFIG:\n");
2123 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
2124 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
2125 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
2126 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
2127 le32_to_cpu(rxon->filter_flags));
2128 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
2129 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
2130 rxon->ofdm_basic_rates);
2131 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
2132 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
2133 print_mac(mac, rxon->node_addr));
2134 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
2135 print_mac(mac, rxon->bssid_addr));
2136 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
2137 }
2138 #endif
2139
2140 static void iwl4965_enable_interrupts(struct iwl_priv *priv)
2141 {
2142 IWL_DEBUG_ISR("Enabling interrupts\n");
2143 set_bit(STATUS_INT_ENABLED, &priv->status);
2144 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
2145 }
2146
2147 /* call this function to flush any scheduled tasklet */
2148 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
2149 {
2150 /* wait to make sure we flush pedding tasklet*/
2151 synchronize_irq(priv->pci_dev->irq);
2152 tasklet_kill(&priv->irq_tasklet);
2153 }
2154
2155 static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
2156 {
2157 clear_bit(STATUS_INT_ENABLED, &priv->status);
2158
2159 /* disable interrupts from uCode/NIC to host */
2160 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
2161
2162 /* acknowledge/clear/reset any interrupts still pending
2163 * from uCode or flow handler (Rx/Tx DMA) */
2164 iwl_write32(priv, CSR_INT, 0xffffffff);
2165 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
2166 IWL_DEBUG_ISR("Disabled interrupts\n");
2167 }
2168
2169
2170 /**
2171 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
2172 */
2173 static void iwl4965_irq_handle_error(struct iwl_priv *priv)
2174 {
2175 /* Set the FW error flag -- cleared on iwl4965_down */
2176 set_bit(STATUS_FW_ERROR, &priv->status);
2177
2178 /* Cancel currently queued command. */
2179 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2180
2181 #ifdef CONFIG_IWLWIFI_DEBUG
2182 if (priv->debug_level & IWL_DL_FW_ERRORS) {
2183 iwl_dump_nic_error_log(priv);
2184 iwl_dump_nic_event_log(priv);
2185 iwl4965_print_rx_config_cmd(priv);
2186 }
2187 #endif
2188
2189 wake_up_interruptible(&priv->wait_command_queue);
2190
2191 /* Keep the restart process from trying to send host
2192 * commands by clearing the INIT status bit */
2193 clear_bit(STATUS_READY, &priv->status);
2194
2195 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2196 IWL_DEBUG(IWL_DL_FW_ERRORS,
2197 "Restarting adapter due to uCode error.\n");
2198
2199 if (iwl_is_associated(priv)) {
2200 memcpy(&priv->recovery_rxon, &priv->active_rxon,
2201 sizeof(priv->recovery_rxon));
2202 priv->error_recovering = 1;
2203 }
2204 if (priv->cfg->mod_params->restart_fw)
2205 queue_work(priv->workqueue, &priv->restart);
2206 }
2207 }
2208
2209 static void iwl4965_error_recovery(struct iwl_priv *priv)
2210 {
2211 unsigned long flags;
2212
2213 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2214 sizeof(priv->staging_rxon));
2215 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2216 iwl4965_commit_rxon(priv);
2217
2218 iwl_rxon_add_station(priv, priv->bssid, 1);
2219
2220 spin_lock_irqsave(&priv->lock, flags);
2221 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2222 priv->error_recovering = 0;
2223 spin_unlock_irqrestore(&priv->lock, flags);
2224 }
2225
2226 static void iwl4965_irq_tasklet(struct iwl_priv *priv)
2227 {
2228 u32 inta, handled = 0;
2229 u32 inta_fh;
2230 unsigned long flags;
2231 #ifdef CONFIG_IWLWIFI_DEBUG
2232 u32 inta_mask;
2233 #endif
2234
2235 spin_lock_irqsave(&priv->lock, flags);
2236
2237 /* Ack/clear/reset pending uCode interrupts.
2238 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2239 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
2240 inta = iwl_read32(priv, CSR_INT);
2241 iwl_write32(priv, CSR_INT, inta);
2242
2243 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2244 * Any new interrupts that happen after this, either while we're
2245 * in this tasklet, or later, will show up in next ISR/tasklet. */
2246 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2247 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
2248
2249 #ifdef CONFIG_IWLWIFI_DEBUG
2250 if (priv->debug_level & IWL_DL_ISR) {
2251 /* just for debug */
2252 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2253 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2254 inta, inta_mask, inta_fh);
2255 }
2256 #endif
2257
2258 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2259 * atomic, make sure that inta covers all the interrupts that
2260 * we've discovered, even if FH interrupt came in just after
2261 * reading CSR_INT. */
2262 if (inta_fh & CSR49_FH_INT_RX_MASK)
2263 inta |= CSR_INT_BIT_FH_RX;
2264 if (inta_fh & CSR49_FH_INT_TX_MASK)
2265 inta |= CSR_INT_BIT_FH_TX;
2266
2267 /* Now service all interrupt bits discovered above. */
2268 if (inta & CSR_INT_BIT_HW_ERR) {
2269 IWL_ERROR("Microcode HW error detected. Restarting.\n");
2270
2271 /* Tell the device to stop sending interrupts */
2272 iwl4965_disable_interrupts(priv);
2273
2274 iwl4965_irq_handle_error(priv);
2275
2276 handled |= CSR_INT_BIT_HW_ERR;
2277
2278 spin_unlock_irqrestore(&priv->lock, flags);
2279
2280 return;
2281 }
2282
2283 #ifdef CONFIG_IWLWIFI_DEBUG
2284 if (priv->debug_level & (IWL_DL_ISR)) {
2285 /* NIC fires this, but we don't use it, redundant with WAKEUP */
2286 if (inta & CSR_INT_BIT_SCD)
2287 IWL_DEBUG_ISR("Scheduler finished to transmit "
2288 "the frame/frames.\n");
2289
2290 /* Alive notification via Rx interrupt will do the real work */
2291 if (inta & CSR_INT_BIT_ALIVE)
2292 IWL_DEBUG_ISR("Alive interrupt\n");
2293 }
2294 #endif
2295 /* Safely ignore these bits for debug checks below */
2296 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
2297
2298 /* HW RF KILL switch toggled */
2299 if (inta & CSR_INT_BIT_RF_KILL) {
2300 int hw_rf_kill = 0;
2301 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2302 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2303 hw_rf_kill = 1;
2304
2305 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
2306 hw_rf_kill ? "disable radio":"enable radio");
2307
2308 /* Queue restart only if RF_KILL switch was set to "kill"
2309 * when we loaded driver, and is now set to "enable".
2310 * After we're Alive, RF_KILL gets handled by
2311 * iwl4965_rx_card_state_notif() */
2312 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
2313 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2314 queue_work(priv->workqueue, &priv->restart);
2315 }
2316
2317 handled |= CSR_INT_BIT_RF_KILL;
2318 }
2319
2320 /* Chip got too hot and stopped itself */
2321 if (inta & CSR_INT_BIT_CT_KILL) {
2322 IWL_ERROR("Microcode CT kill error detected.\n");
2323 handled |= CSR_INT_BIT_CT_KILL;
2324 }
2325
2326 /* Error detected by uCode */
2327 if (inta & CSR_INT_BIT_SW_ERR) {
2328 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
2329 inta);
2330 iwl4965_irq_handle_error(priv);
2331 handled |= CSR_INT_BIT_SW_ERR;
2332 }
2333
2334 /* uCode wakes up after power-down sleep */
2335 if (inta & CSR_INT_BIT_WAKEUP) {
2336 IWL_DEBUG_ISR("Wakeup interrupt\n");
2337 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
2338 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2339 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2340 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2341 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2342 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2343 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
2344
2345 handled |= CSR_INT_BIT_WAKEUP;
2346 }
2347
2348 /* All uCode command responses, including Tx command responses,
2349 * Rx "responses" (frame-received notification), and other
2350 * notifications from uCode come through here*/
2351 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
2352 iwl_rx_handle(priv);
2353 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2354 }
2355
2356 if (inta & CSR_INT_BIT_FH_TX) {
2357 IWL_DEBUG_ISR("Tx interrupt\n");
2358 handled |= CSR_INT_BIT_FH_TX;
2359 /* FH finished to write, send event */
2360 priv->ucode_write_complete = 1;
2361 wake_up_interruptible(&priv->wait_command_queue);
2362 }
2363
2364 if (inta & ~handled)
2365 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
2366
2367 if (inta & ~CSR_INI_SET_MASK) {
2368 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
2369 inta & ~CSR_INI_SET_MASK);
2370 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
2371 }
2372
2373 /* Re-enable all interrupts */
2374 /* only Re-enable if diabled by irq */
2375 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2376 iwl4965_enable_interrupts(priv);
2377
2378 #ifdef CONFIG_IWLWIFI_DEBUG
2379 if (priv->debug_level & (IWL_DL_ISR)) {
2380 inta = iwl_read32(priv, CSR_INT);
2381 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2382 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2383 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
2384 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2385 }
2386 #endif
2387 spin_unlock_irqrestore(&priv->lock, flags);
2388 }
2389
2390 static irqreturn_t iwl4965_isr(int irq, void *data)
2391 {
2392 struct iwl_priv *priv = data;
2393 u32 inta, inta_mask;
2394 u32 inta_fh;
2395 if (!priv)
2396 return IRQ_NONE;
2397
2398 spin_lock(&priv->lock);
2399
2400 /* Disable (but don't clear!) interrupts here to avoid
2401 * back-to-back ISRs and sporadic interrupts from our NIC.
2402 * If we have something to service, the tasklet will re-enable ints.
2403 * If we *don't* have something, we'll re-enable before leaving here. */
2404 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
2405 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
2406
2407 /* Discover which interrupts are active/pending */
2408 inta = iwl_read32(priv, CSR_INT);
2409 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2410
2411 /* Ignore interrupt if there's nothing in NIC to service.
2412 * This may be due to IRQ shared with another device,
2413 * or due to sporadic interrupts thrown from our NIC. */
2414 if (!inta && !inta_fh) {
2415 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
2416 goto none;
2417 }
2418
2419 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
2420 /* Hardware disappeared. It might have already raised
2421 * an interrupt */
2422 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
2423 goto unplugged;
2424 }
2425
2426 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2427 inta, inta_mask, inta_fh);
2428
2429 inta &= ~CSR_INT_BIT_SCD;
2430
2431 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
2432 if (likely(inta || inta_fh))
2433 tasklet_schedule(&priv->irq_tasklet);
2434
2435 unplugged:
2436 spin_unlock(&priv->lock);
2437 return IRQ_HANDLED;
2438
2439 none:
2440 /* re-enable interrupts here since we don't have anything to service. */
2441 /* only Re-enable if diabled by irq */
2442 if (test_bit(STATUS_INT_ENABLED, &priv->status))
2443 iwl4965_enable_interrupts(priv);
2444 spin_unlock(&priv->lock);
2445 return IRQ_NONE;
2446 }
2447
2448 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
2449 * sending probe req. This should be set long enough to hear probe responses
2450 * from more than one AP. */
2451 #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
2452 #define IWL_ACTIVE_DWELL_TIME_52 (10)
2453
2454 /* For faster active scanning, scan will move to the next channel if fewer than
2455 * PLCP_QUIET_THRESH packets are heard on this channel within
2456 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
2457 * time if it's a quiet channel (nothing responded to our probe, and there's
2458 * no other traffic).
2459 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
2460 #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
2461 #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
2462
2463 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
2464 * Must be set longer than active dwell time.
2465 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
2466 #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
2467 #define IWL_PASSIVE_DWELL_TIME_52 (10)
2468 #define IWL_PASSIVE_DWELL_BASE (100)
2469 #define IWL_CHANNEL_TUNE_TIME 5
2470
2471 static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
2472 enum ieee80211_band band)
2473 {
2474 if (band == IEEE80211_BAND_5GHZ)
2475 return IWL_ACTIVE_DWELL_TIME_52;
2476 else
2477 return IWL_ACTIVE_DWELL_TIME_24;
2478 }
2479
2480 static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
2481 enum ieee80211_band band)
2482 {
2483 u16 active = iwl4965_get_active_dwell_time(priv, band);
2484 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
2485 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
2486 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
2487
2488 if (iwl_is_associated(priv)) {
2489 /* If we're associated, we clamp the maximum passive
2490 * dwell time to be 98% of the beacon interval (minus
2491 * 2 * channel tune time) */
2492 passive = priv->beacon_int;
2493 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
2494 passive = IWL_PASSIVE_DWELL_BASE;
2495 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
2496 }
2497
2498 if (passive <= active)
2499 passive = active + 1;
2500
2501 return passive;
2502 }
2503
2504 static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
2505 enum ieee80211_band band,
2506 u8 is_active, u8 direct_mask,
2507 struct iwl4965_scan_channel *scan_ch)
2508 {
2509 const struct ieee80211_channel *channels = NULL;
2510 const struct ieee80211_supported_band *sband;
2511 const struct iwl_channel_info *ch_info;
2512 u16 passive_dwell = 0;
2513 u16 active_dwell = 0;
2514 int added, i;
2515
2516 sband = iwl_get_hw_mode(priv, band);
2517 if (!sband)
2518 return 0;
2519
2520 channels = sband->channels;
2521
2522 active_dwell = iwl4965_get_active_dwell_time(priv, band);
2523 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
2524
2525 for (i = 0, added = 0; i < sband->n_channels; i++) {
2526 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2527 continue;
2528
2529 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
2530
2531 ch_info = iwl_get_channel_info(priv, band,
2532 scan_ch->channel);
2533 if (!is_channel_valid(ch_info)) {
2534 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
2535 scan_ch->channel);
2536 continue;
2537 }
2538
2539 if (!is_active || is_channel_passive(ch_info) ||
2540 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
2541 scan_ch->type = 0; /* passive */
2542 else
2543 scan_ch->type = 1; /* active */
2544
2545 if (scan_ch->type & 1)
2546 scan_ch->type |= (direct_mask << 1);
2547
2548 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2549 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
2550
2551 /* Set txpower levels to defaults */
2552 scan_ch->tpc.dsp_atten = 110;
2553 /* scan_pwr_info->tpc.dsp_atten; */
2554
2555 /*scan_pwr_info->tpc.tx_gain; */
2556 if (band == IEEE80211_BAND_5GHZ)
2557 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2558 else {
2559 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2560 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
2561 * power level:
2562 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
2563 */
2564 }
2565
2566 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
2567 scan_ch->channel,
2568 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2569 (scan_ch->type & 1) ?
2570 active_dwell : passive_dwell);
2571
2572 scan_ch++;
2573 added++;
2574 }
2575
2576 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
2577 return added;
2578 }
2579
2580 /******************************************************************************
2581 *
2582 * uCode download functions
2583 *
2584 ******************************************************************************/
2585
2586 static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
2587 {
2588 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2589 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2590 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2591 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2592 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2593 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
2594 }
2595
2596 static void iwl4965_nic_start(struct iwl_priv *priv)
2597 {
2598 /* Remove all resets to allow NIC to operate */
2599 iwl_write32(priv, CSR_RESET, 0);
2600 }
2601
2602
2603 /**
2604 * iwl4965_read_ucode - Read uCode images from disk file.
2605 *
2606 * Copy into buffers for card to fetch via bus-mastering
2607 */
2608 static int iwl4965_read_ucode(struct iwl_priv *priv)
2609 {
2610 struct iwl4965_ucode *ucode;
2611 int ret;
2612 const struct firmware *ucode_raw;
2613 const char *name = priv->cfg->fw_name;
2614 u8 *src;
2615 size_t len;
2616 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
2617
2618 /* Ask kernel firmware_class module to get the boot firmware off disk.
2619 * request_firmware() is synchronous, file is in memory on return. */
2620 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
2621 if (ret < 0) {
2622 IWL_ERROR("%s firmware file req failed: Reason %d\n",
2623 name, ret);
2624 goto error;
2625 }
2626
2627 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
2628 name, ucode_raw->size);
2629
2630 /* Make sure that we got at least our header! */
2631 if (ucode_raw->size < sizeof(*ucode)) {
2632 IWL_ERROR("File size way too small!\n");
2633 ret = -EINVAL;
2634 goto err_release;
2635 }
2636
2637 /* Data from ucode file: header followed by uCode images */
2638 ucode = (void *)ucode_raw->data;
2639
2640 ver = le32_to_cpu(ucode->ver);
2641 inst_size = le32_to_cpu(ucode->inst_size);
2642 data_size = le32_to_cpu(ucode->data_size);
2643 init_size = le32_to_cpu(ucode->init_size);
2644 init_data_size = le32_to_cpu(ucode->init_data_size);
2645 boot_size = le32_to_cpu(ucode->boot_size);
2646
2647 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
2648 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
2649 inst_size);
2650 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
2651 data_size);
2652 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
2653 init_size);
2654 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
2655 init_data_size);
2656 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
2657 boot_size);
2658
2659 /* Verify size of file vs. image size info in file's header */
2660 if (ucode_raw->size < sizeof(*ucode) +
2661 inst_size + data_size + init_size +
2662 init_data_size + boot_size) {
2663
2664 IWL_DEBUG_INFO("uCode file size %d too small\n",
2665 (int)ucode_raw->size);
2666 ret = -EINVAL;
2667 goto err_release;
2668 }
2669
2670 /* Verify that uCode images will fit in card's SRAM */
2671 if (inst_size > priv->hw_params.max_inst_size) {
2672 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
2673 inst_size);
2674 ret = -EINVAL;
2675 goto err_release;
2676 }
2677
2678 if (data_size > priv->hw_params.max_data_size) {
2679 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
2680 data_size);
2681 ret = -EINVAL;
2682 goto err_release;
2683 }
2684 if (init_size > priv->hw_params.max_inst_size) {
2685 IWL_DEBUG_INFO
2686 ("uCode init instr len %d too large to fit in\n",
2687 init_size);
2688 ret = -EINVAL;
2689 goto err_release;
2690 }
2691 if (init_data_size > priv->hw_params.max_data_size) {
2692 IWL_DEBUG_INFO
2693 ("uCode init data len %d too large to fit in\n",
2694 init_data_size);
2695 ret = -EINVAL;
2696 goto err_release;
2697 }
2698 if (boot_size > priv->hw_params.max_bsm_size) {
2699 IWL_DEBUG_INFO
2700 ("uCode boot instr len %d too large to fit in\n",
2701 boot_size);
2702 ret = -EINVAL;
2703 goto err_release;
2704 }
2705
2706 /* Allocate ucode buffers for card's bus-master loading ... */
2707
2708 /* Runtime instructions and 2 copies of data:
2709 * 1) unmodified from disk
2710 * 2) backup cache for save/restore during power-downs */
2711 priv->ucode_code.len = inst_size;
2712 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
2713
2714 priv->ucode_data.len = data_size;
2715 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
2716
2717 priv->ucode_data_backup.len = data_size;
2718 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2719
2720 /* Initialization instructions and data */
2721 if (init_size && init_data_size) {
2722 priv->ucode_init.len = init_size;
2723 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
2724
2725 priv->ucode_init_data.len = init_data_size;
2726 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2727
2728 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2729 goto err_pci_alloc;
2730 }
2731
2732 /* Bootstrap (instructions only, no data) */
2733 if (boot_size) {
2734 priv->ucode_boot.len = boot_size;
2735 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
2736
2737 if (!priv->ucode_boot.v_addr)
2738 goto err_pci_alloc;
2739 }
2740
2741 /* Copy images into buffers for card's bus-master reads ... */
2742
2743 /* Runtime instructions (first block of data in file) */
2744 src = &ucode->data[0];
2745 len = priv->ucode_code.len;
2746 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
2747 memcpy(priv->ucode_code.v_addr, src, len);
2748 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2749 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2750
2751 /* Runtime data (2nd block)
2752 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
2753 src = &ucode->data[inst_size];
2754 len = priv->ucode_data.len;
2755 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
2756 memcpy(priv->ucode_data.v_addr, src, len);
2757 memcpy(priv->ucode_data_backup.v_addr, src, len);
2758
2759 /* Initialization instructions (3rd block) */
2760 if (init_size) {
2761 src = &ucode->data[inst_size + data_size];
2762 len = priv->ucode_init.len;
2763 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
2764 len);
2765 memcpy(priv->ucode_init.v_addr, src, len);
2766 }
2767
2768 /* Initialization data (4th block) */
2769 if (init_data_size) {
2770 src = &ucode->data[inst_size + data_size + init_size];
2771 len = priv->ucode_init_data.len;
2772 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
2773 len);
2774 memcpy(priv->ucode_init_data.v_addr, src, len);
2775 }
2776
2777 /* Bootstrap instructions (5th block) */
2778 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2779 len = priv->ucode_boot.len;
2780 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
2781 memcpy(priv->ucode_boot.v_addr, src, len);
2782
2783 /* We have our copies now, allow OS release its copies */
2784 release_firmware(ucode_raw);
2785 return 0;
2786
2787 err_pci_alloc:
2788 IWL_ERROR("failed to allocate pci memory\n");
2789 ret = -ENOMEM;
2790 iwl4965_dealloc_ucode_pci(priv);
2791
2792 err_release:
2793 release_firmware(ucode_raw);
2794
2795 error:
2796 return ret;
2797 }
2798
2799 /**
2800 * iwl_alive_start - called after REPLY_ALIVE notification received
2801 * from protocol/runtime uCode (initialization uCode's
2802 * Alive gets handled by iwl_init_alive_start()).
2803 */
2804 static void iwl_alive_start(struct iwl_priv *priv)
2805 {
2806 int ret = 0;
2807
2808 IWL_DEBUG_INFO("Runtime Alive received.\n");
2809
2810 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2811 /* We had an error bringing up the hardware, so take it
2812 * all the way back down so we can try again */
2813 IWL_DEBUG_INFO("Alive failed.\n");
2814 goto restart;
2815 }
2816
2817 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2818 * This is a paranoid check, because we would not have gotten the
2819 * "runtime" alive if code weren't properly loaded. */
2820 if (iwl_verify_ucode(priv)) {
2821 /* Runtime instruction load was bad;
2822 * take it all the way back down so we can try again */
2823 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
2824 goto restart;
2825 }
2826
2827 iwlcore_clear_stations_table(priv);
2828 ret = priv->cfg->ops->lib->alive_notify(priv);
2829 if (ret) {
2830 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
2831 ret);
2832 goto restart;
2833 }
2834
2835 /* After the ALIVE response, we can send host commands to 4965 uCode */
2836 set_bit(STATUS_ALIVE, &priv->status);
2837
2838 /* Clear out the uCode error bit if it is set */
2839 clear_bit(STATUS_FW_ERROR, &priv->status);
2840
2841 if (iwl_is_rfkill(priv))
2842 return;
2843
2844 ieee80211_wake_queues(priv->hw);
2845
2846 priv->active_rate = priv->rates_mask;
2847 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2848
2849 if (iwl_is_associated(priv)) {
2850 struct iwl_rxon_cmd *active_rxon =
2851 (struct iwl_rxon_cmd *)&priv->active_rxon;
2852
2853 memcpy(&priv->staging_rxon, &priv->active_rxon,
2854 sizeof(priv->staging_rxon));
2855 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2856 } else {
2857 /* Initialize our rx_config data */
2858 iwl4965_connection_init_rx_config(priv);
2859 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2860 }
2861
2862 /* Configure Bluetooth device coexistence support */
2863 iwl4965_send_bt_config(priv);
2864
2865 iwl_reset_run_time_calib(priv);
2866
2867 /* Configure the adapter for unassociated operation */
2868 iwl4965_commit_rxon(priv);
2869
2870 /* At this point, the NIC is initialized and operational */
2871 iwl4965_rf_kill_ct_config(priv);
2872
2873 iwl_leds_register(priv);
2874
2875 IWL_DEBUG_INFO("ALIVE processing complete.\n");
2876 set_bit(STATUS_READY, &priv->status);
2877 wake_up_interruptible(&priv->wait_command_queue);
2878
2879 if (priv->error_recovering)
2880 iwl4965_error_recovery(priv);
2881
2882 iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
2883 ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
2884 return;
2885
2886 restart:
2887 queue_work(priv->workqueue, &priv->restart);
2888 }
2889
2890 static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
2891
2892 static void __iwl4965_down(struct iwl_priv *priv)
2893 {
2894 unsigned long flags;
2895 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2896
2897 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2898
2899 if (!exit_pending)
2900 set_bit(STATUS_EXIT_PENDING, &priv->status);
2901
2902 iwl_leds_unregister(priv);
2903
2904 iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
2905
2906 iwlcore_clear_stations_table(priv);
2907
2908 /* Unblock any waiting calls */
2909 wake_up_interruptible_all(&priv->wait_command_queue);
2910
2911 /* Wipe out the EXIT_PENDING status bit if we are not actually
2912 * exiting the module */
2913 if (!exit_pending)
2914 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2915
2916 /* stop and reset the on-board processor */
2917 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
2918
2919 /* tell the device to stop sending interrupts */
2920 spin_lock_irqsave(&priv->lock, flags);
2921 iwl4965_disable_interrupts(priv);
2922 spin_unlock_irqrestore(&priv->lock, flags);
2923 iwl_synchronize_irq(priv);
2924
2925 if (priv->mac80211_registered)
2926 ieee80211_stop_queues(priv->hw);
2927
2928 /* If we have not previously called iwl4965_init() then
2929 * clear all bits but the RF Kill and SUSPEND bits and return */
2930 if (!iwl_is_init(priv)) {
2931 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2932 STATUS_RF_KILL_HW |
2933 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2934 STATUS_RF_KILL_SW |
2935 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2936 STATUS_GEO_CONFIGURED |
2937 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2938 STATUS_IN_SUSPEND;
2939 goto exit;
2940 }
2941
2942 /* ...otherwise clear out all the status bits but the RF Kill and
2943 * SUSPEND bits and continue taking the NIC down. */
2944 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2945 STATUS_RF_KILL_HW |
2946 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2947 STATUS_RF_KILL_SW |
2948 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2949 STATUS_GEO_CONFIGURED |
2950 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2951 STATUS_IN_SUSPEND |
2952 test_bit(STATUS_FW_ERROR, &priv->status) <<
2953 STATUS_FW_ERROR;
2954
2955 spin_lock_irqsave(&priv->lock, flags);
2956 iwl_clear_bit(priv, CSR_GP_CNTRL,
2957 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2958 spin_unlock_irqrestore(&priv->lock, flags);
2959
2960 iwl_txq_ctx_stop(priv);
2961 iwl_rxq_stop(priv);
2962
2963 spin_lock_irqsave(&priv->lock, flags);
2964 if (!iwl_grab_nic_access(priv)) {
2965 iwl_write_prph(priv, APMG_CLK_DIS_REG,
2966 APMG_CLK_VAL_DMA_CLK_RQT);
2967 iwl_release_nic_access(priv);
2968 }
2969 spin_unlock_irqrestore(&priv->lock, flags);
2970
2971 udelay(5);
2972
2973 /* FIXME: apm_ops.suspend(priv) */
2974 priv->cfg->ops->lib->apm_ops.reset(priv);
2975 priv->cfg->ops->lib->free_shared_mem(priv);
2976
2977 exit:
2978 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
2979
2980 if (priv->ibss_beacon)
2981 dev_kfree_skb(priv->ibss_beacon);
2982 priv->ibss_beacon = NULL;
2983
2984 /* clear out any free frames */
2985 iwl_clear_free_frames(priv);
2986 }
2987
2988 static void iwl4965_down(struct iwl_priv *priv)
2989 {
2990 mutex_lock(&priv->mutex);
2991 __iwl4965_down(priv);
2992 mutex_unlock(&priv->mutex);
2993
2994 iwl4965_cancel_deferred_work(priv);
2995 }
2996
2997 #define MAX_HW_RESTARTS 5
2998
2999 static int __iwl4965_up(struct iwl_priv *priv)
3000 {
3001 int i;
3002 int ret;
3003
3004 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3005 IWL_WARNING("Exit pending; will not bring the NIC up\n");
3006 return -EIO;
3007 }
3008
3009 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3010 IWL_WARNING("Radio disabled by SW RF kill (module "
3011 "parameter)\n");
3012 iwl_rfkill_set_hw_state(priv);
3013 return -ENODEV;
3014 }
3015
3016 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3017 IWL_ERROR("ucode not available for device bringup\n");
3018 return -EIO;
3019 }
3020
3021 /* If platform's RF_KILL switch is NOT set to KILL */
3022 if (iwl_read32(priv, CSR_GP_CNTRL) &
3023 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3024 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3025 else {
3026 set_bit(STATUS_RF_KILL_HW, &priv->status);
3027 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
3028 iwl_rfkill_set_hw_state(priv);
3029 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
3030 return -ENODEV;
3031 }
3032 }
3033
3034 iwl_rfkill_set_hw_state(priv);
3035 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
3036
3037 ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
3038 if (ret) {
3039 IWL_ERROR("Unable to allocate shared memory\n");
3040 return ret;
3041 }
3042
3043 ret = iwl_hw_nic_init(priv);
3044 if (ret) {
3045 IWL_ERROR("Unable to init nic\n");
3046 return ret;
3047 }
3048
3049 /* make sure rfkill handshake bits are cleared */
3050 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3051 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
3052 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3053
3054 /* clear (again), then enable host interrupts */
3055 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
3056 iwl4965_enable_interrupts(priv);
3057
3058 /* really make sure rfkill handshake bits are cleared */
3059 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3060 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3061
3062 /* Copy original ucode data image from disk into backup cache.
3063 * This will be used to initialize the on-board processor's
3064 * data SRAM for a clean start when the runtime program first loads. */
3065 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
3066 priv->ucode_data.len);
3067
3068 /* We return success when we resume from suspend and rf_kill is on. */
3069 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
3070 return 0;
3071
3072 for (i = 0; i < MAX_HW_RESTARTS; i++) {
3073
3074 iwlcore_clear_stations_table(priv);
3075
3076 /* load bootstrap state machine,
3077 * load bootstrap program into processor's memory,
3078 * prepare to load the "initialize" uCode */
3079 ret = priv->cfg->ops->lib->load_ucode(priv);
3080
3081 if (ret) {
3082 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
3083 continue;
3084 }
3085
3086 /* start card; "initialize" will load runtime ucode */
3087 iwl4965_nic_start(priv);
3088
3089 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
3090
3091 return 0;
3092 }
3093
3094 set_bit(STATUS_EXIT_PENDING, &priv->status);
3095 __iwl4965_down(priv);
3096
3097 /* tried to restart and config the device for as long as our
3098 * patience could withstand */
3099 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
3100 return -EIO;
3101 }
3102
3103
3104 /*****************************************************************************
3105 *
3106 * Workqueue callbacks
3107 *
3108 *****************************************************************************/
3109
3110 static void iwl_bg_init_alive_start(struct work_struct *data)
3111 {
3112 struct iwl_priv *priv =
3113 container_of(data, struct iwl_priv, init_alive_start.work);
3114
3115 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3116 return;
3117
3118 mutex_lock(&priv->mutex);
3119 priv->cfg->ops->lib->init_alive_start(priv);
3120 mutex_unlock(&priv->mutex);
3121 }
3122
3123 static void iwl_bg_alive_start(struct work_struct *data)
3124 {
3125 struct iwl_priv *priv =
3126 container_of(data, struct iwl_priv, alive_start.work);
3127
3128 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3129 return;
3130
3131 mutex_lock(&priv->mutex);
3132 iwl_alive_start(priv);
3133 mutex_unlock(&priv->mutex);
3134 }
3135
3136 static void iwl4965_bg_rf_kill(struct work_struct *work)
3137 {
3138 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
3139
3140 wake_up_interruptible(&priv->wait_command_queue);
3141
3142 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3143 return;
3144
3145 mutex_lock(&priv->mutex);
3146
3147 if (!iwl_is_rfkill(priv)) {
3148 IWL_DEBUG(IWL_DL_RF_KILL,
3149 "HW and/or SW RF Kill no longer active, restarting "
3150 "device\n");
3151 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3152 queue_work(priv->workqueue, &priv->restart);
3153 } else {
3154 /* make sure mac80211 stop sending Tx frame */
3155 if (priv->mac80211_registered)
3156 ieee80211_stop_queues(priv->hw);
3157
3158 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
3159 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3160 "disabled by SW switch\n");
3161 else
3162 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
3163 "Kill switch must be turned off for "
3164 "wireless networking to work.\n");
3165 }
3166 iwl_rfkill_set_hw_state(priv);
3167
3168 mutex_unlock(&priv->mutex);
3169 }
3170
3171 static void iwl4965_bg_set_monitor(struct work_struct *work)
3172 {
3173 struct iwl_priv *priv = container_of(work,
3174 struct iwl_priv, set_monitor);
3175
3176 IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
3177
3178 mutex_lock(&priv->mutex);
3179
3180 if (!iwl_is_ready(priv))
3181 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
3182 else
3183 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
3184 IWL_ERROR("iwl4965_set_mode() failed\n");
3185
3186 mutex_unlock(&priv->mutex);
3187 }
3188
3189 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3190
3191 static void iwl4965_bg_scan_check(struct work_struct *data)
3192 {
3193 struct iwl_priv *priv =
3194 container_of(data, struct iwl_priv, scan_check.work);
3195
3196 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3197 return;
3198
3199 mutex_lock(&priv->mutex);
3200 if (test_bit(STATUS_SCANNING, &priv->status) ||
3201 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3202 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
3203 "adapter (%dms)\n",
3204 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
3205
3206 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3207 iwl4965_send_scan_abort(priv);
3208 }
3209 mutex_unlock(&priv->mutex);
3210 }
3211
3212 static void iwl4965_bg_request_scan(struct work_struct *data)
3213 {
3214 struct iwl_priv *priv =
3215 container_of(data, struct iwl_priv, request_scan);
3216 struct iwl_host_cmd cmd = {
3217 .id = REPLY_SCAN_CMD,
3218 .len = sizeof(struct iwl4965_scan_cmd),
3219 .meta.flags = CMD_SIZE_HUGE,
3220 };
3221 struct iwl4965_scan_cmd *scan;
3222 struct ieee80211_conf *conf = NULL;
3223 u16 cmd_len;
3224 enum ieee80211_band band;
3225 u8 direct_mask;
3226 int ret = 0;
3227
3228 conf = ieee80211_get_hw_conf(priv->hw);
3229
3230 mutex_lock(&priv->mutex);
3231
3232 if (!iwl_is_ready(priv)) {
3233 IWL_WARNING("request scan called when driver not ready.\n");
3234 goto done;
3235 }
3236
3237 /* Make sure the scan wasn't cancelled before this queued work
3238 * was given the chance to run... */
3239 if (!test_bit(STATUS_SCANNING, &priv->status))
3240 goto done;
3241
3242 /* This should never be called or scheduled if there is currently
3243 * a scan active in the hardware. */
3244 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3245 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
3246 "Ignoring second request.\n");
3247 ret = -EIO;
3248 goto done;
3249 }
3250
3251 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3252 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
3253 goto done;
3254 }
3255
3256 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3257 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
3258 goto done;
3259 }
3260
3261 if (iwl_is_rfkill(priv)) {
3262 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
3263 goto done;
3264 }
3265
3266 if (!test_bit(STATUS_READY, &priv->status)) {
3267 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
3268 goto done;
3269 }
3270
3271 if (!priv->scan_bands) {
3272 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
3273 goto done;
3274 }
3275
3276 if (!priv->scan) {
3277 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
3278 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3279 if (!priv->scan) {
3280 ret = -ENOMEM;
3281 goto done;
3282 }
3283 }
3284 scan = priv->scan;
3285 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
3286
3287 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3288 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3289
3290 if (iwl_is_associated(priv)) {
3291 u16 interval = 0;
3292 u32 extra;
3293 u32 suspend_time = 100;
3294 u32 scan_suspend_time = 100;
3295 unsigned long flags;
3296
3297 IWL_DEBUG_INFO("Scanning while associated...\n");
3298
3299 spin_lock_irqsave(&priv->lock, flags);
3300 interval = priv->beacon_int;
3301 spin_unlock_irqrestore(&priv->lock, flags);
3302
3303 scan->suspend_time = 0;
3304 scan->max_out_time = cpu_to_le32(200 * 1024);
3305 if (!interval)
3306 interval = suspend_time;
3307
3308 extra = (suspend_time / interval) << 22;
3309 scan_suspend_time = (extra |
3310 ((suspend_time % interval) * 1024));
3311 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3312 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
3313 scan_suspend_time, interval);
3314 }
3315
3316 /* We should add the ability for user to lock to PASSIVE ONLY */
3317 if (priv->one_direct_scan) {
3318 IWL_DEBUG_SCAN
3319 ("Kicking off one direct scan for '%s'\n",
3320 iwl4965_escape_essid(priv->direct_ssid,
3321 priv->direct_ssid_len));
3322 scan->direct_scan[0].id = WLAN_EID_SSID;
3323 scan->direct_scan[0].len = priv->direct_ssid_len;
3324 memcpy(scan->direct_scan[0].ssid,
3325 priv->direct_ssid, priv->direct_ssid_len);
3326 direct_mask = 1;
3327 } else if (!iwl_is_associated(priv) && priv->essid_len) {
3328 IWL_DEBUG_SCAN
3329 ("Kicking off one direct scan for '%s' when not associated\n",
3330 iwl4965_escape_essid(priv->essid, priv->essid_len));
3331 scan->direct_scan[0].id = WLAN_EID_SSID;
3332 scan->direct_scan[0].len = priv->essid_len;
3333 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
3334 direct_mask = 1;
3335 } else {
3336 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
3337 direct_mask = 0;
3338 }
3339
3340 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
3341 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
3342 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3343
3344
3345 switch (priv->scan_bands) {
3346 case 2:
3347 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3348 scan->tx_cmd.rate_n_flags =
3349 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
3350 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
3351
3352 scan->good_CRC_th = 0;
3353 band = IEEE80211_BAND_2GHZ;
3354 break;
3355
3356 case 1:
3357 scan->tx_cmd.rate_n_flags =
3358 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
3359 RATE_MCS_ANT_B_MSK);
3360 scan->good_CRC_th = IWL_GOOD_CRC_TH;
3361 band = IEEE80211_BAND_5GHZ;
3362 break;
3363
3364 default:
3365 IWL_WARNING("Invalid scan band count\n");
3366 goto done;
3367 }
3368
3369 /* We don't build a direct scan probe request; the uCode will do
3370 * that based on the direct_mask added to each channel entry */
3371 cmd_len = iwl4965_fill_probe_req(priv, band,
3372 (struct ieee80211_mgmt *)scan->data,
3373 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
3374
3375 scan->tx_cmd.len = cpu_to_le16(cmd_len);
3376 /* select Rx chains */
3377
3378 /* Force use of chains B and C (0x6) for scan Rx.
3379 * Avoid A (0x1) because of its off-channel reception on A-band.
3380 * MIMO is not used here, but value is required to make uCode happy. */
3381 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
3382 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
3383 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
3384 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
3385
3386 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
3387 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3388
3389 if (direct_mask)
3390 scan->channel_count =
3391 iwl4965_get_channels_for_scan(
3392 priv, band, 1, /* active */
3393 direct_mask,
3394 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
3395 else
3396 scan->channel_count =
3397 iwl4965_get_channels_for_scan(
3398 priv, band, 0, /* passive */
3399 direct_mask,
3400 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
3401
3402 scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
3403 RXON_FILTER_BCON_AWARE_MSK);
3404 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
3405 scan->channel_count * sizeof(struct iwl4965_scan_channel);
3406 cmd.data = scan;
3407 scan->len = cpu_to_le16(cmd.len);
3408
3409 set_bit(STATUS_SCAN_HW, &priv->status);
3410 ret = iwl_send_cmd_sync(priv, &cmd);
3411 if (ret)
3412 goto done;
3413
3414 queue_delayed_work(priv->workqueue, &priv->scan_check,
3415 IWL_SCAN_CHECK_WATCHDOG);
3416
3417 mutex_unlock(&priv->mutex);
3418 return;
3419
3420 done:
3421 /* inform mac80211 scan aborted */
3422 queue_work(priv->workqueue, &priv->scan_completed);
3423 mutex_unlock(&priv->mutex);
3424 }
3425
3426 static void iwl_bg_run_time_calib_work(struct work_struct *work)
3427 {
3428 struct iwl_priv *priv = container_of(work, struct iwl_priv,
3429 run_time_calib_work);
3430
3431 mutex_lock(&priv->mutex);
3432
3433 if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
3434 test_bit(STATUS_SCANNING, &priv->status)) {
3435 mutex_unlock(&priv->mutex);
3436 return;
3437 }
3438
3439 if (priv->start_calib) {
3440 iwl_chain_noise_calibration(priv, &priv->statistics);
3441
3442 iwl_sensitivity_calibration(priv, &priv->statistics);
3443 }
3444
3445 mutex_unlock(&priv->mutex);
3446 return;
3447 }
3448
3449 static void iwl4965_bg_up(struct work_struct *data)
3450 {
3451 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
3452
3453 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3454 return;
3455
3456 mutex_lock(&priv->mutex);
3457 __iwl4965_up(priv);
3458 mutex_unlock(&priv->mutex);
3459 }
3460
3461 static void iwl4965_bg_restart(struct work_struct *data)
3462 {
3463 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
3464
3465 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3466 return;
3467
3468 iwl4965_down(priv);
3469 queue_work(priv->workqueue, &priv->up);
3470 }
3471
3472 static void iwl4965_bg_rx_replenish(struct work_struct *data)
3473 {
3474 struct iwl_priv *priv =
3475 container_of(data, struct iwl_priv, rx_replenish);
3476
3477 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3478 return;
3479
3480 mutex_lock(&priv->mutex);
3481 iwl_rx_replenish(priv);
3482 mutex_unlock(&priv->mutex);
3483 }
3484
3485 #define IWL_DELAY_NEXT_SCAN (HZ*2)
3486
3487 static void iwl4965_post_associate(struct iwl_priv *priv)
3488 {
3489 struct ieee80211_conf *conf = NULL;
3490 int ret = 0;
3491 DECLARE_MAC_BUF(mac);
3492
3493 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3494 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
3495 return;
3496 }
3497
3498 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
3499 priv->assoc_id,
3500 print_mac(mac, priv->active_rxon.bssid_addr));
3501
3502
3503 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3504 return;
3505
3506
3507 if (!priv->vif || !priv->is_open)
3508 return;
3509
3510 iwl4965_scan_cancel_timeout(priv, 200);
3511
3512 conf = ieee80211_get_hw_conf(priv->hw);
3513
3514 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3515 iwl4965_commit_rxon(priv);
3516
3517 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3518 iwl4965_setup_rxon_timing(priv);
3519 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3520 sizeof(priv->rxon_timing), &priv->rxon_timing);
3521 if (ret)
3522 IWL_WARNING("REPLY_RXON_TIMING failed - "
3523 "Attempting to continue.\n");
3524
3525 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3526
3527 if (priv->current_ht_config.is_ht)
3528 iwl_set_rxon_ht(priv, &priv->current_ht_config);
3529
3530 iwl_set_rxon_chain(priv);
3531 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3532
3533 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
3534 priv->assoc_id, priv->beacon_int);
3535
3536 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3537 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3538 else
3539 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3540
3541 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3542 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
3543 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
3544 else
3545 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3546
3547 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3548 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3549
3550 }
3551
3552 iwl4965_commit_rxon(priv);
3553
3554 switch (priv->iw_mode) {
3555 case IEEE80211_IF_TYPE_STA:
3556 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
3557 break;
3558
3559 case IEEE80211_IF_TYPE_IBSS:
3560
3561 /* clear out the station table */
3562 iwlcore_clear_stations_table(priv);
3563
3564 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
3565 iwl_rxon_add_station(priv, priv->bssid, 0);
3566 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
3567 iwl4965_send_beacon_cmd(priv);
3568
3569 break;
3570
3571 default:
3572 IWL_ERROR("%s Should not be called in %d mode\n",
3573 __FUNCTION__, priv->iw_mode);
3574 break;
3575 }
3576
3577 iwl4965_sequence_reset(priv);
3578
3579 /* Enable Rx differential gain and sensitivity calibrations */
3580 iwl_chain_noise_reset(priv);
3581 priv->start_calib = 1;
3582
3583 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3584 priv->assoc_station_added = 1;
3585
3586 iwl4965_activate_qos(priv, 0);
3587
3588 iwl_power_update_mode(priv, 0);
3589 /* we have just associated, don't start scan too early */
3590 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
3591 }
3592
3593
3594 static void iwl4965_bg_post_associate(struct work_struct *data)
3595 {
3596 struct iwl_priv *priv = container_of(data, struct iwl_priv,
3597 post_associate.work);
3598
3599 mutex_lock(&priv->mutex);
3600 iwl4965_post_associate(priv);
3601 mutex_unlock(&priv->mutex);
3602
3603 }
3604
3605 static void iwl4965_bg_abort_scan(struct work_struct *work)
3606 {
3607 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
3608
3609 if (!iwl_is_ready(priv))
3610 return;
3611
3612 mutex_lock(&priv->mutex);
3613
3614 set_bit(STATUS_SCAN_ABORTING, &priv->status);
3615 iwl4965_send_scan_abort(priv);
3616
3617 mutex_unlock(&priv->mutex);
3618 }
3619
3620 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
3621
3622 static void iwl4965_bg_scan_completed(struct work_struct *work)
3623 {
3624 struct iwl_priv *priv =
3625 container_of(work, struct iwl_priv, scan_completed);
3626
3627 IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
3628
3629 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3630 return;
3631
3632 if (test_bit(STATUS_CONF_PENDING, &priv->status))
3633 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
3634
3635 ieee80211_scan_completed(priv->hw);
3636
3637 /* Since setting the TXPOWER may have been deferred while
3638 * performing the scan, fire one off */
3639 mutex_lock(&priv->mutex);
3640 iwl4965_hw_reg_send_txpower(priv);
3641 mutex_unlock(&priv->mutex);
3642 }
3643
3644 /*****************************************************************************
3645 *
3646 * mac80211 entry point functions
3647 *
3648 *****************************************************************************/
3649
3650 #define UCODE_READY_TIMEOUT (2 * HZ)
3651
3652 static int iwl4965_mac_start(struct ieee80211_hw *hw)
3653 {
3654 struct iwl_priv *priv = hw->priv;
3655 int ret;
3656
3657 IWL_DEBUG_MAC80211("enter\n");
3658
3659 if (pci_enable_device(priv->pci_dev)) {
3660 IWL_ERROR("Fail to pci_enable_device\n");
3661 return -ENODEV;
3662 }
3663 pci_restore_state(priv->pci_dev);
3664 pci_enable_msi(priv->pci_dev);
3665
3666 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
3667 DRV_NAME, priv);
3668 if (ret) {
3669 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
3670 goto out_disable_msi;
3671 }
3672
3673 /* we should be verifying the device is ready to be opened */
3674 mutex_lock(&priv->mutex);
3675
3676 memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
3677 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3678 * ucode filename and max sizes are card-specific. */
3679
3680 if (!priv->ucode_code.len) {
3681 ret = iwl4965_read_ucode(priv);
3682 if (ret) {
3683 IWL_ERROR("Could not read microcode: %d\n", ret);
3684 mutex_unlock(&priv->mutex);
3685 goto out_release_irq;
3686 }
3687 }
3688
3689 ret = __iwl4965_up(priv);
3690
3691 mutex_unlock(&priv->mutex);
3692
3693 if (ret)
3694 goto out_release_irq;
3695
3696 IWL_DEBUG_INFO("Start UP work done.\n");
3697
3698 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
3699 return 0;
3700
3701 /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
3702 * mac80211 will not be run successfully. */
3703 if (priv->ucode_type == UCODE_RT) {
3704 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3705 test_bit(STATUS_READY, &priv->status),
3706 UCODE_READY_TIMEOUT);
3707 if (!ret) {
3708 if (!test_bit(STATUS_READY, &priv->status)) {
3709 IWL_ERROR("START_ALIVE timeout after %dms.\n",
3710 jiffies_to_msecs(UCODE_READY_TIMEOUT));
3711 ret = -ETIMEDOUT;
3712 goto out_release_irq;
3713 }
3714 }
3715
3716 priv->is_open = 1;
3717 }
3718 IWL_DEBUG_MAC80211("leave\n");
3719 return 0;
3720
3721 out_release_irq:
3722 free_irq(priv->pci_dev->irq, priv);
3723 out_disable_msi:
3724 pci_disable_msi(priv->pci_dev);
3725 pci_disable_device(priv->pci_dev);
3726 priv->is_open = 0;
3727 IWL_DEBUG_MAC80211("leave - failed\n");
3728 return ret;
3729 }
3730
3731 static void iwl4965_mac_stop(struct ieee80211_hw *hw)
3732 {
3733 struct iwl_priv *priv = hw->priv;
3734
3735 IWL_DEBUG_MAC80211("enter\n");
3736
3737 if (!priv->is_open) {
3738 IWL_DEBUG_MAC80211("leave - skip\n");
3739 return;
3740 }
3741
3742 priv->is_open = 0;
3743
3744 if (iwl_is_ready_rf(priv)) {
3745 /* stop mac, cancel any scan request and clear
3746 * RXON_FILTER_ASSOC_MSK BIT
3747 */
3748 mutex_lock(&priv->mutex);
3749 iwl4965_scan_cancel_timeout(priv, 100);
3750 cancel_delayed_work(&priv->post_associate);
3751 mutex_unlock(&priv->mutex);
3752 }
3753
3754 iwl4965_down(priv);
3755
3756 flush_workqueue(priv->workqueue);
3757 free_irq(priv->pci_dev->irq, priv);
3758 pci_disable_msi(priv->pci_dev);
3759 pci_save_state(priv->pci_dev);
3760 pci_disable_device(priv->pci_dev);
3761
3762 IWL_DEBUG_MAC80211("leave\n");
3763 }
3764
3765 static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3766 {
3767 struct iwl_priv *priv = hw->priv;
3768
3769 IWL_DEBUG_MAC80211("enter\n");
3770
3771 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
3772 IWL_DEBUG_MAC80211("leave - monitor\n");
3773 return -1;
3774 }
3775
3776 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
3777 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
3778
3779 if (iwl_tx_skb(priv, skb))
3780 dev_kfree_skb_any(skb);
3781
3782 IWL_DEBUG_MAC80211("leave\n");
3783 return 0;
3784 }
3785
3786 static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
3787 struct ieee80211_if_init_conf *conf)
3788 {
3789 struct iwl_priv *priv = hw->priv;
3790 unsigned long flags;
3791 DECLARE_MAC_BUF(mac);
3792
3793 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
3794
3795 if (priv->vif) {
3796 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
3797 return -EOPNOTSUPP;
3798 }
3799
3800 spin_lock_irqsave(&priv->lock, flags);
3801 priv->vif = conf->vif;
3802
3803 spin_unlock_irqrestore(&priv->lock, flags);
3804
3805 mutex_lock(&priv->mutex);
3806
3807 if (conf->mac_addr) {
3808 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
3809 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3810 }
3811
3812 if (iwl_is_ready(priv))
3813 iwl4965_set_mode(priv, conf->type);
3814
3815 mutex_unlock(&priv->mutex);
3816
3817 IWL_DEBUG_MAC80211("leave\n");
3818 return 0;
3819 }
3820
3821 /**
3822 * iwl4965_mac_config - mac80211 config callback
3823 *
3824 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3825 * be set inappropriately and the driver currently sets the hardware up to
3826 * use it whenever needed.
3827 */
3828 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
3829 {
3830 struct iwl_priv *priv = hw->priv;
3831 const struct iwl_channel_info *ch_info;
3832 unsigned long flags;
3833 int ret = 0;
3834 u16 channel;
3835
3836 mutex_lock(&priv->mutex);
3837 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
3838
3839 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
3840
3841 if (!iwl_is_ready(priv)) {
3842 IWL_DEBUG_MAC80211("leave - not ready\n");
3843 ret = -EIO;
3844 goto out;
3845 }
3846
3847 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
3848 test_bit(STATUS_SCANNING, &priv->status))) {
3849 IWL_DEBUG_MAC80211("leave - scanning\n");
3850 set_bit(STATUS_CONF_PENDING, &priv->status);
3851 mutex_unlock(&priv->mutex);
3852 return 0;
3853 }
3854
3855 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
3856 ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
3857 if (!is_channel_valid(ch_info)) {
3858 IWL_DEBUG_MAC80211("leave - invalid channel\n");
3859 ret = -EINVAL;
3860 goto out;
3861 }
3862
3863 spin_lock_irqsave(&priv->lock, flags);
3864
3865 /* if we are switching from ht to 2.4 clear flags
3866 * from any ht related info since 2.4 does not
3867 * support ht */
3868 if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
3869 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
3870 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
3871 #endif
3872 )
3873 priv->staging_rxon.flags = 0;
3874
3875 iwl_set_rxon_channel(priv, conf->channel->band, channel);
3876
3877 iwl_set_flags_for_band(priv, conf->channel->band);
3878
3879 /* The list of supported rates and rate mask can be different
3880 * for each band; since the band may have changed, reset
3881 * the rate mask to what mac80211 lists */
3882 iwl4965_set_rate(priv);
3883
3884 spin_unlock_irqrestore(&priv->lock, flags);
3885
3886 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
3887 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
3888 iwl4965_hw_channel_switch(priv, conf->channel);
3889 goto out;
3890 }
3891 #endif
3892
3893 if (priv->cfg->ops->lib->radio_kill_sw)
3894 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
3895
3896 if (!conf->radio_enabled) {
3897 IWL_DEBUG_MAC80211("leave - radio disabled\n");
3898 goto out;
3899 }
3900
3901 if (iwl_is_rfkill(priv)) {
3902 IWL_DEBUG_MAC80211("leave - RF kill\n");
3903 ret = -EIO;
3904 goto out;
3905 }
3906
3907 iwl4965_set_rate(priv);
3908
3909 if (memcmp(&priv->active_rxon,
3910 &priv->staging_rxon, sizeof(priv->staging_rxon)))
3911 iwl4965_commit_rxon(priv);
3912 else
3913 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
3914
3915 IWL_DEBUG_MAC80211("leave\n");
3916
3917 out:
3918 clear_bit(STATUS_CONF_PENDING, &priv->status);
3919 mutex_unlock(&priv->mutex);
3920 return ret;
3921 }
3922
3923 static void iwl4965_config_ap(struct iwl_priv *priv)
3924 {
3925 int ret = 0;
3926
3927 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3928 return;
3929
3930 /* The following should be done only at AP bring up */
3931 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
3932
3933 /* RXON - unassoc (to set timing command) */
3934 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3935 iwl4965_commit_rxon(priv);
3936
3937 /* RXON Timing */
3938 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3939 iwl4965_setup_rxon_timing(priv);
3940 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3941 sizeof(priv->rxon_timing), &priv->rxon_timing);
3942 if (ret)
3943 IWL_WARNING("REPLY_RXON_TIMING failed - "
3944 "Attempting to continue.\n");
3945
3946 iwl_set_rxon_chain(priv);
3947
3948 /* FIXME: what should be the assoc_id for AP? */
3949 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3950 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3951 priv->staging_rxon.flags |=
3952 RXON_FLG_SHORT_PREAMBLE_MSK;
3953 else
3954 priv->staging_rxon.flags &=
3955 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3956
3957 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3958 if (priv->assoc_capability &
3959 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3960 priv->staging_rxon.flags |=
3961 RXON_FLG_SHORT_SLOT_MSK;
3962 else
3963 priv->staging_rxon.flags &=
3964 ~RXON_FLG_SHORT_SLOT_MSK;
3965
3966 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3967 priv->staging_rxon.flags &=
3968 ~RXON_FLG_SHORT_SLOT_MSK;
3969 }
3970 /* restore RXON assoc */
3971 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3972 iwl4965_commit_rxon(priv);
3973 iwl4965_activate_qos(priv, 1);
3974 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
3975 }
3976 iwl4965_send_beacon_cmd(priv);
3977
3978 /* FIXME - we need to add code here to detect a totally new
3979 * configuration, reset the AP, unassoc, rxon timing, assoc,
3980 * clear sta table, add BCAST sta... */
3981 }
3982
3983 static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
3984 struct ieee80211_vif *vif,
3985 struct ieee80211_if_conf *conf)
3986 {
3987 struct iwl_priv *priv = hw->priv;
3988 DECLARE_MAC_BUF(mac);
3989 unsigned long flags;
3990 int rc;
3991
3992 if (conf == NULL)
3993 return -EIO;
3994
3995 if (priv->vif != vif) {
3996 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
3997 return 0;
3998 }
3999
4000 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
4001 (!conf->beacon || !conf->ssid_len)) {
4002 IWL_DEBUG_MAC80211
4003 ("Leaving in AP mode because HostAPD is not ready.\n");
4004 return 0;
4005 }
4006
4007 if (!iwl_is_alive(priv))
4008 return -EAGAIN;
4009
4010 mutex_lock(&priv->mutex);
4011
4012 if (conf->bssid)
4013 IWL_DEBUG_MAC80211("bssid: %s\n",
4014 print_mac(mac, conf->bssid));
4015
4016 /*
4017 * very dubious code was here; the probe filtering flag is never set:
4018 *
4019 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4020 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
4021 */
4022
4023 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4024 if (!conf->bssid) {
4025 conf->bssid = priv->mac_addr;
4026 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
4027 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
4028 print_mac(mac, conf->bssid));
4029 }
4030 if (priv->ibss_beacon)
4031 dev_kfree_skb(priv->ibss_beacon);
4032
4033 priv->ibss_beacon = conf->beacon;
4034 }
4035
4036 if (iwl_is_rfkill(priv))
4037 goto done;
4038
4039 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4040 !is_multicast_ether_addr(conf->bssid)) {
4041 /* If there is currently a HW scan going on in the background
4042 * then we need to cancel it else the RXON below will fail. */
4043 if (iwl4965_scan_cancel_timeout(priv, 100)) {
4044 IWL_WARNING("Aborted scan still in progress "
4045 "after 100ms\n");
4046 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
4047 mutex_unlock(&priv->mutex);
4048 return -EAGAIN;
4049 }
4050 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
4051
4052 /* TODO: Audit driver for usage of these members and see
4053 * if mac80211 deprecates them (priv->bssid looks like it
4054 * shouldn't be there, but I haven't scanned the IBSS code
4055 * to verify) - jpk */
4056 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
4057
4058 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
4059 iwl4965_config_ap(priv);
4060 else {
4061 rc = iwl4965_commit_rxon(priv);
4062 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
4063 iwl_rxon_add_station(
4064 priv, priv->active_rxon.bssid_addr, 1);
4065 }
4066
4067 } else {
4068 iwl4965_scan_cancel_timeout(priv, 100);
4069 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4070 iwl4965_commit_rxon(priv);
4071 }
4072
4073 done:
4074 spin_lock_irqsave(&priv->lock, flags);
4075 if (!conf->ssid_len)
4076 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4077 else
4078 memcpy(priv->essid, conf->ssid, conf->ssid_len);
4079
4080 priv->essid_len = conf->ssid_len;
4081 spin_unlock_irqrestore(&priv->lock, flags);
4082
4083 IWL_DEBUG_MAC80211("leave\n");
4084 mutex_unlock(&priv->mutex);
4085
4086 return 0;
4087 }
4088
4089 static void iwl4965_configure_filter(struct ieee80211_hw *hw,
4090 unsigned int changed_flags,
4091 unsigned int *total_flags,
4092 int mc_count, struct dev_addr_list *mc_list)
4093 {
4094 /*
4095 * XXX: dummy
4096 * see also iwl4965_connection_init_rx_config
4097 */
4098 struct iwl_priv *priv = hw->priv;
4099 int new_flags = 0;
4100 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4101 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4102 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
4103 IEEE80211_IF_TYPE_MNTR,
4104 changed_flags, *total_flags);
4105 /* queue work 'cuz mac80211 is holding a lock which
4106 * prevents us from issuing (synchronous) f/w cmds */
4107 queue_work(priv->workqueue, &priv->set_monitor);
4108 new_flags &= FIF_PROMISC_IN_BSS |
4109 FIF_OTHER_BSS |
4110 FIF_ALLMULTI;
4111 }
4112 }
4113 *total_flags = new_flags;
4114 }
4115
4116 static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
4117 struct ieee80211_if_init_conf *conf)
4118 {
4119 struct iwl_priv *priv = hw->priv;
4120
4121 IWL_DEBUG_MAC80211("enter\n");
4122
4123 mutex_lock(&priv->mutex);
4124
4125 if (iwl_is_ready_rf(priv)) {
4126 iwl4965_scan_cancel_timeout(priv, 100);
4127 cancel_delayed_work(&priv->post_associate);
4128 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4129 iwl4965_commit_rxon(priv);
4130 }
4131 if (priv->vif == conf->vif) {
4132 priv->vif = NULL;
4133 memset(priv->bssid, 0, ETH_ALEN);
4134 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4135 priv->essid_len = 0;
4136 }
4137 mutex_unlock(&priv->mutex);
4138
4139 IWL_DEBUG_MAC80211("leave\n");
4140
4141 }
4142
4143 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
4144 static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
4145 struct ieee80211_vif *vif,
4146 struct ieee80211_bss_conf *bss_conf,
4147 u32 changes)
4148 {
4149 struct iwl_priv *priv = hw->priv;
4150
4151 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
4152
4153 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
4154 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
4155 bss_conf->use_short_preamble);
4156 if (bss_conf->use_short_preamble)
4157 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4158 else
4159 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4160 }
4161
4162 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
4163 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
4164 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
4165 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4166 else
4167 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4168 }
4169
4170 if (changes & BSS_CHANGED_HT) {
4171 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
4172 iwl4965_ht_conf(priv, bss_conf);
4173 iwl_set_rxon_chain(priv);
4174 }
4175
4176 if (changes & BSS_CHANGED_ASSOC) {
4177 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
4178 /* This should never happen as this function should
4179 * never be called from interrupt context. */
4180 if (WARN_ON_ONCE(in_interrupt()))
4181 return;
4182 if (bss_conf->assoc) {
4183 priv->assoc_id = bss_conf->aid;
4184 priv->beacon_int = bss_conf->beacon_int;
4185 priv->timestamp = bss_conf->timestamp;
4186 priv->assoc_capability = bss_conf->assoc_capability;
4187 priv->next_scan_jiffies = jiffies +
4188 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
4189 mutex_lock(&priv->mutex);
4190 iwl4965_post_associate(priv);
4191 mutex_unlock(&priv->mutex);
4192 } else {
4193 priv->assoc_id = 0;
4194 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
4195 }
4196 } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4197 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
4198 iwl_send_rxon_assoc(priv);
4199 }
4200
4201 }
4202
4203 static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
4204 {
4205 int rc = 0;
4206 unsigned long flags;
4207 struct iwl_priv *priv = hw->priv;
4208
4209 IWL_DEBUG_MAC80211("enter\n");
4210
4211 mutex_lock(&priv->mutex);
4212 spin_lock_irqsave(&priv->lock, flags);
4213
4214 if (!iwl_is_ready_rf(priv)) {
4215 rc = -EIO;
4216 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
4217 goto out_unlock;
4218 }
4219
4220 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
4221 rc = -EIO;
4222 IWL_ERROR("ERROR: APs don't scan\n");
4223 goto out_unlock;
4224 }
4225
4226 /* we don't schedule scan within next_scan_jiffies period */
4227 if (priv->next_scan_jiffies &&
4228 time_after(priv->next_scan_jiffies, jiffies)) {
4229 rc = -EAGAIN;
4230 goto out_unlock;
4231 }
4232 /* if we just finished scan ask for delay */
4233 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
4234 IWL_DELAY_NEXT_SCAN, jiffies)) {
4235 rc = -EAGAIN;
4236 goto out_unlock;
4237 }
4238 if (len) {
4239 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
4240 iwl4965_escape_essid(ssid, len), (int)len);
4241
4242 priv->one_direct_scan = 1;
4243 priv->direct_ssid_len = (u8)
4244 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
4245 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
4246 } else
4247 priv->one_direct_scan = 0;
4248
4249 rc = iwl4965_scan_initiate(priv);
4250
4251 IWL_DEBUG_MAC80211("leave\n");
4252
4253 out_unlock:
4254 spin_unlock_irqrestore(&priv->lock, flags);
4255 mutex_unlock(&priv->mutex);
4256
4257 return rc;
4258 }
4259
4260 static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
4261 struct ieee80211_key_conf *keyconf, const u8 *addr,
4262 u32 iv32, u16 *phase1key)
4263 {
4264 struct iwl_priv *priv = hw->priv;
4265 u8 sta_id = IWL_INVALID_STATION;
4266 unsigned long flags;
4267 __le16 key_flags = 0;
4268 int i;
4269 DECLARE_MAC_BUF(mac);
4270
4271 IWL_DEBUG_MAC80211("enter\n");
4272
4273 sta_id = iwl_find_station(priv, addr);
4274 if (sta_id == IWL_INVALID_STATION) {
4275 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4276 print_mac(mac, addr));
4277 return;
4278 }
4279
4280 iwl4965_scan_cancel_timeout(priv, 100);
4281
4282 key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
4283 key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
4284 key_flags &= ~STA_KEY_FLG_INVALID;
4285
4286 if (sta_id == priv->hw_params.bcast_sta_id)
4287 key_flags |= STA_KEY_MULTICAST_MSK;
4288
4289 spin_lock_irqsave(&priv->sta_lock, flags);
4290
4291 priv->stations[sta_id].sta.key.key_flags = key_flags;
4292 priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
4293
4294 for (i = 0; i < 5; i++)
4295 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
4296 cpu_to_le16(phase1key[i]);
4297
4298 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
4299 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4300
4301 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
4302
4303 spin_unlock_irqrestore(&priv->sta_lock, flags);
4304
4305 IWL_DEBUG_MAC80211("leave\n");
4306 }
4307
4308 static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4309 const u8 *local_addr, const u8 *addr,
4310 struct ieee80211_key_conf *key)
4311 {
4312 struct iwl_priv *priv = hw->priv;
4313 DECLARE_MAC_BUF(mac);
4314 int ret = 0;
4315 u8 sta_id = IWL_INVALID_STATION;
4316 u8 is_default_wep_key = 0;
4317
4318 IWL_DEBUG_MAC80211("enter\n");
4319
4320 if (priv->hw_params.sw_crypto) {
4321 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
4322 return -EOPNOTSUPP;
4323 }
4324
4325 if (is_zero_ether_addr(addr))
4326 /* only support pairwise keys */
4327 return -EOPNOTSUPP;
4328
4329 sta_id = iwl_find_station(priv, addr);
4330 if (sta_id == IWL_INVALID_STATION) {
4331 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4332 print_mac(mac, addr));
4333 return -EINVAL;
4334
4335 }
4336
4337 mutex_lock(&priv->mutex);
4338 iwl4965_scan_cancel_timeout(priv, 100);
4339 mutex_unlock(&priv->mutex);
4340
4341 /* If we are getting WEP group key and we didn't receive any key mapping
4342 * so far, we are in legacy wep mode (group key only), otherwise we are
4343 * in 1X mode.
4344 * In legacy wep mode, we use another host command to the uCode */
4345 if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
4346 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4347 if (cmd == SET_KEY)
4348 is_default_wep_key = !priv->key_mapping_key;
4349 else
4350 is_default_wep_key =
4351 (key->hw_key_idx == HW_KEY_DEFAULT);
4352 }
4353
4354 switch (cmd) {
4355 case SET_KEY:
4356 if (is_default_wep_key)
4357 ret = iwl_set_default_wep_key(priv, key);
4358 else
4359 ret = iwl_set_dynamic_key(priv, key, sta_id);
4360
4361 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
4362 break;
4363 case DISABLE_KEY:
4364 if (is_default_wep_key)
4365 ret = iwl_remove_default_wep_key(priv, key);
4366 else
4367 ret = iwl_remove_dynamic_key(priv, key, sta_id);
4368
4369 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
4370 break;
4371 default:
4372 ret = -EINVAL;
4373 }
4374
4375 IWL_DEBUG_MAC80211("leave\n");
4376
4377 return ret;
4378 }
4379
4380 static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
4381 const struct ieee80211_tx_queue_params *params)
4382 {
4383 struct iwl_priv *priv = hw->priv;
4384 unsigned long flags;
4385 int q;
4386
4387 IWL_DEBUG_MAC80211("enter\n");
4388
4389 if (!iwl_is_ready_rf(priv)) {
4390 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4391 return -EIO;
4392 }
4393
4394 if (queue >= AC_NUM) {
4395 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
4396 return 0;
4397 }
4398
4399 if (!priv->qos_data.qos_enable) {
4400 priv->qos_data.qos_active = 0;
4401 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
4402 return 0;
4403 }
4404 q = AC_NUM - 1 - queue;
4405
4406 spin_lock_irqsave(&priv->lock, flags);
4407
4408 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
4409 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
4410 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4411 priv->qos_data.def_qos_parm.ac[q].edca_txop =
4412 cpu_to_le16((params->txop * 32));
4413
4414 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
4415 priv->qos_data.qos_active = 1;
4416
4417 spin_unlock_irqrestore(&priv->lock, flags);
4418
4419 mutex_lock(&priv->mutex);
4420 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
4421 iwl4965_activate_qos(priv, 1);
4422 else if (priv->assoc_id && iwl_is_associated(priv))
4423 iwl4965_activate_qos(priv, 0);
4424
4425 mutex_unlock(&priv->mutex);
4426
4427 IWL_DEBUG_MAC80211("leave\n");
4428 return 0;
4429 }
4430
4431 static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
4432 struct ieee80211_tx_queue_stats *stats)
4433 {
4434 struct iwl_priv *priv = hw->priv;
4435 int i, avail;
4436 struct iwl_tx_queue *txq;
4437 struct iwl_queue *q;
4438 unsigned long flags;
4439
4440 IWL_DEBUG_MAC80211("enter\n");
4441
4442 if (!iwl_is_ready_rf(priv)) {
4443 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4444 return -EIO;
4445 }
4446
4447 spin_lock_irqsave(&priv->lock, flags);
4448
4449 for (i = 0; i < AC_NUM; i++) {
4450 txq = &priv->txq[i];
4451 q = &txq->q;
4452 avail = iwl_queue_space(q);
4453
4454 stats[i].len = q->n_window - avail;
4455 stats[i].limit = q->n_window - q->high_mark;
4456 stats[i].count = q->n_window;
4457
4458 }
4459 spin_unlock_irqrestore(&priv->lock, flags);
4460
4461 IWL_DEBUG_MAC80211("leave\n");
4462
4463 return 0;
4464 }
4465
4466 static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
4467 struct ieee80211_low_level_stats *stats)
4468 {
4469 struct iwl_priv *priv = hw->priv;
4470
4471 priv = hw->priv;
4472 IWL_DEBUG_MAC80211("enter\n");
4473 IWL_DEBUG_MAC80211("leave\n");
4474
4475 return 0;
4476 }
4477
4478 static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
4479 {
4480 struct iwl_priv *priv;
4481
4482 priv = hw->priv;
4483 IWL_DEBUG_MAC80211("enter\n");
4484 IWL_DEBUG_MAC80211("leave\n");
4485
4486 return 0;
4487 }
4488
4489 static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
4490 {
4491 struct iwl_priv *priv = hw->priv;
4492 unsigned long flags;
4493
4494 mutex_lock(&priv->mutex);
4495 IWL_DEBUG_MAC80211("enter\n");
4496
4497 priv->lq_mngr.lq_ready = 0;
4498 spin_lock_irqsave(&priv->lock, flags);
4499 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
4500 spin_unlock_irqrestore(&priv->lock, flags);
4501
4502 iwl_reset_qos(priv);
4503
4504 cancel_delayed_work(&priv->post_associate);
4505
4506 spin_lock_irqsave(&priv->lock, flags);
4507 priv->assoc_id = 0;
4508 priv->assoc_capability = 0;
4509 priv->assoc_station_added = 0;
4510
4511 /* new association get rid of ibss beacon skb */
4512 if (priv->ibss_beacon)
4513 dev_kfree_skb(priv->ibss_beacon);
4514
4515 priv->ibss_beacon = NULL;
4516
4517 priv->beacon_int = priv->hw->conf.beacon_int;
4518 priv->timestamp = 0;
4519 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
4520 priv->beacon_int = 0;
4521
4522 spin_unlock_irqrestore(&priv->lock, flags);
4523
4524 if (!iwl_is_ready_rf(priv)) {
4525 IWL_DEBUG_MAC80211("leave - not ready\n");
4526 mutex_unlock(&priv->mutex);
4527 return;
4528 }
4529
4530 /* we are restarting association process
4531 * clear RXON_FILTER_ASSOC_MSK bit
4532 */
4533 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4534 iwl4965_scan_cancel_timeout(priv, 100);
4535 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4536 iwl4965_commit_rxon(priv);
4537 }
4538
4539 iwl_power_update_mode(priv, 0);
4540
4541 /* Per mac80211.h: This is only used in IBSS mode... */
4542 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
4543
4544 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
4545 mutex_unlock(&priv->mutex);
4546 return;
4547 }
4548
4549 iwl4965_set_rate(priv);
4550
4551 mutex_unlock(&priv->mutex);
4552
4553 IWL_DEBUG_MAC80211("leave\n");
4554 }
4555
4556 static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
4557 {
4558 struct iwl_priv *priv = hw->priv;
4559 unsigned long flags;
4560
4561 mutex_lock(&priv->mutex);
4562 IWL_DEBUG_MAC80211("enter\n");
4563
4564 if (!iwl_is_ready_rf(priv)) {
4565 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4566 mutex_unlock(&priv->mutex);
4567 return -EIO;
4568 }
4569
4570 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
4571 IWL_DEBUG_MAC80211("leave - not IBSS\n");
4572 mutex_unlock(&priv->mutex);
4573 return -EIO;
4574 }
4575
4576 spin_lock_irqsave(&priv->lock, flags);
4577
4578 if (priv->ibss_beacon)
4579 dev_kfree_skb(priv->ibss_beacon);
4580
4581 priv->ibss_beacon = skb;
4582
4583 priv->assoc_id = 0;
4584
4585 IWL_DEBUG_MAC80211("leave\n");
4586 spin_unlock_irqrestore(&priv->lock, flags);
4587
4588 iwl_reset_qos(priv);
4589
4590 queue_work(priv->workqueue, &priv->post_associate.work);
4591
4592 mutex_unlock(&priv->mutex);
4593
4594 return 0;
4595 }
4596
4597 /*****************************************************************************
4598 *
4599 * sysfs attributes
4600 *
4601 *****************************************************************************/
4602
4603 #ifdef CONFIG_IWLWIFI_DEBUG
4604
4605 /*
4606 * The following adds a new attribute to the sysfs representation
4607 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
4608 * used for controlling the debug level.
4609 *
4610 * See the level definitions in iwl for details.
4611 */
4612
4613 static ssize_t show_debug_level(struct device *d,
4614 struct device_attribute *attr, char *buf)
4615 {
4616 struct iwl_priv *priv = d->driver_data;
4617
4618 return sprintf(buf, "0x%08X\n", priv->debug_level);
4619 }
4620 static ssize_t store_debug_level(struct device *d,
4621 struct device_attribute *attr,
4622 const char *buf, size_t count)
4623 {
4624 struct iwl_priv *priv = d->driver_data;
4625 char *p = (char *)buf;
4626 u32 val;
4627
4628 val = simple_strtoul(p, &p, 0);
4629 if (p == buf)
4630 printk(KERN_INFO DRV_NAME
4631 ": %s is not in hex or decimal form.\n", buf);
4632 else
4633 priv->debug_level = val;
4634
4635 return strnlen(buf, count);
4636 }
4637
4638 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
4639 show_debug_level, store_debug_level);
4640
4641
4642 #endif /* CONFIG_IWLWIFI_DEBUG */
4643
4644
4645 static ssize_t show_version(struct device *d,
4646 struct device_attribute *attr, char *buf)
4647 {
4648 struct iwl_priv *priv = d->driver_data;
4649 struct iwl_alive_resp *palive = &priv->card_alive;
4650
4651 if (palive->is_valid)
4652 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
4653 "fw type: 0x%01X 0x%01X\n",
4654 palive->ucode_major, palive->ucode_minor,
4655 palive->sw_rev[0], palive->sw_rev[1],
4656 palive->ver_type, palive->ver_subtype);
4657
4658 else
4659 return sprintf(buf, "fw not loaded\n");
4660 }
4661
4662 static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
4663
4664 static ssize_t show_temperature(struct device *d,
4665 struct device_attribute *attr, char *buf)
4666 {
4667 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4668
4669 if (!iwl_is_alive(priv))
4670 return -EAGAIN;
4671
4672 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
4673 }
4674
4675 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
4676
4677 static ssize_t show_rs_window(struct device *d,
4678 struct device_attribute *attr,
4679 char *buf)
4680 {
4681 struct iwl_priv *priv = d->driver_data;
4682 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
4683 }
4684 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
4685
4686 static ssize_t show_tx_power(struct device *d,
4687 struct device_attribute *attr, char *buf)
4688 {
4689 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4690 return sprintf(buf, "%d\n", priv->user_txpower_limit);
4691 }
4692
4693 static ssize_t store_tx_power(struct device *d,
4694 struct device_attribute *attr,
4695 const char *buf, size_t count)
4696 {
4697 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4698 char *p = (char *)buf;
4699 u32 val;
4700
4701 val = simple_strtoul(p, &p, 10);
4702 if (p == buf)
4703 printk(KERN_INFO DRV_NAME
4704 ": %s is not in decimal form.\n", buf);
4705 else
4706 iwl4965_hw_reg_set_txpower(priv, val);
4707
4708 return count;
4709 }
4710
4711 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4712
4713 static ssize_t show_flags(struct device *d,
4714 struct device_attribute *attr, char *buf)
4715 {
4716 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4717
4718 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
4719 }
4720
4721 static ssize_t store_flags(struct device *d,
4722 struct device_attribute *attr,
4723 const char *buf, size_t count)
4724 {
4725 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4726 u32 flags = simple_strtoul(buf, NULL, 0);
4727
4728 mutex_lock(&priv->mutex);
4729 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
4730 /* Cancel any currently running scans... */
4731 if (iwl4965_scan_cancel_timeout(priv, 100))
4732 IWL_WARNING("Could not cancel scan.\n");
4733 else {
4734 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
4735 flags);
4736 priv->staging_rxon.flags = cpu_to_le32(flags);
4737 iwl4965_commit_rxon(priv);
4738 }
4739 }
4740 mutex_unlock(&priv->mutex);
4741
4742 return count;
4743 }
4744
4745 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4746
4747 static ssize_t show_filter_flags(struct device *d,
4748 struct device_attribute *attr, char *buf)
4749 {
4750 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4751
4752 return sprintf(buf, "0x%04X\n",
4753 le32_to_cpu(priv->active_rxon.filter_flags));
4754 }
4755
4756 static ssize_t store_filter_flags(struct device *d,
4757 struct device_attribute *attr,
4758 const char *buf, size_t count)
4759 {
4760 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4761 u32 filter_flags = simple_strtoul(buf, NULL, 0);
4762
4763 mutex_lock(&priv->mutex);
4764 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
4765 /* Cancel any currently running scans... */
4766 if (iwl4965_scan_cancel_timeout(priv, 100))
4767 IWL_WARNING("Could not cancel scan.\n");
4768 else {
4769 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
4770 "0x%04X\n", filter_flags);
4771 priv->staging_rxon.filter_flags =
4772 cpu_to_le32(filter_flags);
4773 iwl4965_commit_rxon(priv);
4774 }
4775 }
4776 mutex_unlock(&priv->mutex);
4777
4778 return count;
4779 }
4780
4781 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4782 store_filter_flags);
4783
4784 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
4785
4786 static ssize_t show_measurement(struct device *d,
4787 struct device_attribute *attr, char *buf)
4788 {
4789 struct iwl_priv *priv = dev_get_drvdata(d);
4790 struct iwl4965_spectrum_notification measure_report;
4791 u32 size = sizeof(measure_report), len = 0, ofs = 0;
4792 u8 *data = (u8 *) & measure_report;
4793 unsigned long flags;
4794
4795 spin_lock_irqsave(&priv->lock, flags);
4796 if (!(priv->measurement_status & MEASUREMENT_READY)) {
4797 spin_unlock_irqrestore(&priv->lock, flags);
4798 return 0;
4799 }
4800 memcpy(&measure_report, &priv->measure_report, size);
4801 priv->measurement_status = 0;
4802 spin_unlock_irqrestore(&priv->lock, flags);
4803
4804 while (size && (PAGE_SIZE - len)) {
4805 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4806 PAGE_SIZE - len, 1);
4807 len = strlen(buf);
4808 if (PAGE_SIZE - len)
4809 buf[len++] = '\n';
4810
4811 ofs += 16;
4812 size -= min(size, 16U);
4813 }
4814
4815 return len;
4816 }
4817
4818 static ssize_t store_measurement(struct device *d,
4819 struct device_attribute *attr,
4820 const char *buf, size_t count)
4821 {
4822 struct iwl_priv *priv = dev_get_drvdata(d);
4823 struct ieee80211_measurement_params params = {
4824 .channel = le16_to_cpu(priv->active_rxon.channel),
4825 .start_time = cpu_to_le64(priv->last_tsf),
4826 .duration = cpu_to_le16(1),
4827 };
4828 u8 type = IWL_MEASURE_BASIC;
4829 u8 buffer[32];
4830 u8 channel;
4831
4832 if (count) {
4833 char *p = buffer;
4834 strncpy(buffer, buf, min(sizeof(buffer), count));
4835 channel = simple_strtoul(p, NULL, 0);
4836 if (channel)
4837 params.channel = channel;
4838
4839 p = buffer;
4840 while (*p && *p != ' ')
4841 p++;
4842 if (*p)
4843 type = simple_strtoul(p + 1, NULL, 0);
4844 }
4845
4846 IWL_DEBUG_INFO("Invoking measurement of type %d on "
4847 "channel %d (for '%s')\n", type, params.channel, buf);
4848 iwl4965_get_measurement(priv, &params, type);
4849
4850 return count;
4851 }
4852
4853 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4854 show_measurement, store_measurement);
4855 #endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
4856
4857 static ssize_t store_retry_rate(struct device *d,
4858 struct device_attribute *attr,
4859 const char *buf, size_t count)
4860 {
4861 struct iwl_priv *priv = dev_get_drvdata(d);
4862
4863 priv->retry_rate = simple_strtoul(buf, NULL, 0);
4864 if (priv->retry_rate <= 0)
4865 priv->retry_rate = 1;
4866
4867 return count;
4868 }
4869
4870 static ssize_t show_retry_rate(struct device *d,
4871 struct device_attribute *attr, char *buf)
4872 {
4873 struct iwl_priv *priv = dev_get_drvdata(d);
4874 return sprintf(buf, "%d", priv->retry_rate);
4875 }
4876
4877 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4878 store_retry_rate);
4879
4880 static ssize_t store_power_level(struct device *d,
4881 struct device_attribute *attr,
4882 const char *buf, size_t count)
4883 {
4884 struct iwl_priv *priv = dev_get_drvdata(d);
4885 int rc;
4886 int mode;
4887
4888 mode = simple_strtoul(buf, NULL, 0);
4889 mutex_lock(&priv->mutex);
4890
4891 if (!iwl_is_ready(priv)) {
4892 rc = -EAGAIN;
4893 goto out;
4894 }
4895
4896 rc = iwl_power_set_user_mode(priv, mode);
4897 if (rc) {
4898 IWL_DEBUG_MAC80211("failed setting power mode.\n");
4899 goto out;
4900 }
4901 rc = count;
4902
4903 out:
4904 mutex_unlock(&priv->mutex);
4905 return rc;
4906 }
4907
4908 #define MAX_WX_STRING 80
4909
4910 /* Values are in microsecond */
4911 static const s32 timeout_duration[] = {
4912 350000,
4913 250000,
4914 75000,
4915 37000,
4916 25000,
4917 };
4918 static const s32 period_duration[] = {
4919 400000,
4920 700000,
4921 1000000,
4922 1000000,
4923 1000000
4924 };
4925
4926 static ssize_t show_power_level(struct device *d,
4927 struct device_attribute *attr, char *buf)
4928 {
4929 struct iwl_priv *priv = dev_get_drvdata(d);
4930 int level = priv->power_data.power_mode;
4931 char *p = buf;
4932
4933 p += sprintf(p, "%d ", level);
4934 switch (level) {
4935 case IWL_POWER_MODE_CAM:
4936 case IWL_POWER_AC:
4937 p += sprintf(p, "(AC)");
4938 break;
4939 case IWL_POWER_BATTERY:
4940 p += sprintf(p, "(BATTERY)");
4941 break;
4942 default:
4943 p += sprintf(p,
4944 "(Timeout %dms, Period %dms)",
4945 timeout_duration[level - 1] / 1000,
4946 period_duration[level - 1] / 1000);
4947 }
4948 /*
4949 if (!(priv->power_mode & IWL_POWER_ENABLED))
4950 p += sprintf(p, " OFF\n");
4951 else
4952 p += sprintf(p, " \n");
4953 */
4954 p += sprintf(p, " \n");
4955 return (p - buf + 1);
4956 }
4957
4958 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
4959 store_power_level);
4960
4961 static ssize_t show_channels(struct device *d,
4962 struct device_attribute *attr, char *buf)
4963 {
4964 /* all this shit doesn't belong into sysfs anyway */
4965 return 0;
4966 }
4967
4968 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4969
4970 static ssize_t show_statistics(struct device *d,
4971 struct device_attribute *attr, char *buf)
4972 {
4973 struct iwl_priv *priv = dev_get_drvdata(d);
4974 u32 size = sizeof(struct iwl4965_notif_statistics);
4975 u32 len = 0, ofs = 0;
4976 u8 *data = (u8 *) & priv->statistics;
4977 int rc = 0;
4978
4979 if (!iwl_is_alive(priv))
4980 return -EAGAIN;
4981
4982 mutex_lock(&priv->mutex);
4983 rc = iwl_send_statistics_request(priv, 0);
4984 mutex_unlock(&priv->mutex);
4985
4986 if (rc) {
4987 len = sprintf(buf,
4988 "Error sending statistics request: 0x%08X\n", rc);
4989 return len;
4990 }
4991
4992 while (size && (PAGE_SIZE - len)) {
4993 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4994 PAGE_SIZE - len, 1);
4995 len = strlen(buf);
4996 if (PAGE_SIZE - len)
4997 buf[len++] = '\n';
4998
4999 ofs += 16;
5000 size -= min(size, 16U);
5001 }
5002
5003 return len;
5004 }
5005
5006 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
5007
5008 static ssize_t show_status(struct device *d,
5009 struct device_attribute *attr, char *buf)
5010 {
5011 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5012 if (!iwl_is_alive(priv))
5013 return -EAGAIN;
5014 return sprintf(buf, "0x%08x\n", (int)priv->status);
5015 }
5016
5017 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5018
5019 /*****************************************************************************
5020 *
5021 * driver setup and teardown
5022 *
5023 *****************************************************************************/
5024
5025 static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
5026 {
5027 priv->workqueue = create_workqueue(DRV_NAME);
5028
5029 init_waitqueue_head(&priv->wait_command_queue);
5030
5031 INIT_WORK(&priv->up, iwl4965_bg_up);
5032 INIT_WORK(&priv->restart, iwl4965_bg_restart);
5033 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
5034 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
5035 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
5036 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
5037 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
5038 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
5039 INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
5040 INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
5041 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
5042 INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
5043 INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
5044 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
5045
5046 iwl4965_hw_setup_deferred_work(priv);
5047
5048 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
5049 iwl4965_irq_tasklet, (unsigned long)priv);
5050 }
5051
5052 static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
5053 {
5054 iwl4965_hw_cancel_deferred_work(priv);
5055
5056 cancel_delayed_work_sync(&priv->init_alive_start);
5057 cancel_delayed_work(&priv->scan_check);
5058 cancel_delayed_work(&priv->alive_start);
5059 cancel_delayed_work(&priv->post_associate);
5060 cancel_work_sync(&priv->beacon_update);
5061 }
5062
5063 static struct attribute *iwl4965_sysfs_entries[] = {
5064 &dev_attr_channels.attr,
5065 &dev_attr_flags.attr,
5066 &dev_attr_filter_flags.attr,
5067 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
5068 &dev_attr_measurement.attr,
5069 #endif
5070 &dev_attr_power_level.attr,
5071 &dev_attr_retry_rate.attr,
5072 &dev_attr_rs_window.attr,
5073 &dev_attr_statistics.attr,
5074 &dev_attr_status.attr,
5075 &dev_attr_temperature.attr,
5076 &dev_attr_tx_power.attr,
5077 #ifdef CONFIG_IWLWIFI_DEBUG
5078 &dev_attr_debug_level.attr,
5079 #endif
5080 &dev_attr_version.attr,
5081
5082 NULL
5083 };
5084
5085 static struct attribute_group iwl4965_attribute_group = {
5086 .name = NULL, /* put in device directory */
5087 .attrs = iwl4965_sysfs_entries,
5088 };
5089
5090 static struct ieee80211_ops iwl4965_hw_ops = {
5091 .tx = iwl4965_mac_tx,
5092 .start = iwl4965_mac_start,
5093 .stop = iwl4965_mac_stop,
5094 .add_interface = iwl4965_mac_add_interface,
5095 .remove_interface = iwl4965_mac_remove_interface,
5096 .config = iwl4965_mac_config,
5097 .config_interface = iwl4965_mac_config_interface,
5098 .configure_filter = iwl4965_configure_filter,
5099 .set_key = iwl4965_mac_set_key,
5100 .update_tkip_key = iwl4965_mac_update_tkip_key,
5101 .get_stats = iwl4965_mac_get_stats,
5102 .get_tx_stats = iwl4965_mac_get_tx_stats,
5103 .conf_tx = iwl4965_mac_conf_tx,
5104 .get_tsf = iwl4965_mac_get_tsf,
5105 .reset_tsf = iwl4965_mac_reset_tsf,
5106 .beacon_update = iwl4965_mac_beacon_update,
5107 .bss_info_changed = iwl4965_bss_info_changed,
5108 .ampdu_action = iwl4965_mac_ampdu_action,
5109 .hw_scan = iwl4965_mac_hw_scan
5110 };
5111
5112 static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
5113 {
5114 int err = 0;
5115 struct iwl_priv *priv;
5116 struct ieee80211_hw *hw;
5117 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
5118 unsigned long flags;
5119 DECLARE_MAC_BUF(mac);
5120
5121 /************************
5122 * 1. Allocating HW data
5123 ************************/
5124
5125 /* Disabling hardware scan means that mac80211 will perform scans
5126 * "the hard way", rather than using device's scan. */
5127 if (cfg->mod_params->disable_hw_scan) {
5128 if (cfg->mod_params->debug & IWL_DL_INFO)
5129 dev_printk(KERN_DEBUG, &(pdev->dev),
5130 "Disabling hw_scan\n");
5131 iwl4965_hw_ops.hw_scan = NULL;
5132 }
5133
5134 hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
5135 if (!hw) {
5136 err = -ENOMEM;
5137 goto out;
5138 }
5139 priv = hw->priv;
5140 /* At this point both hw and priv are allocated. */
5141
5142 SET_IEEE80211_DEV(hw, &pdev->dev);
5143
5144 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
5145 priv->cfg = cfg;
5146 priv->pci_dev = pdev;
5147
5148 #ifdef CONFIG_IWLWIFI_DEBUG
5149 priv->debug_level = priv->cfg->mod_params->debug;
5150 atomic_set(&priv->restrict_refcnt, 0);
5151 #endif
5152
5153 /**************************
5154 * 2. Initializing PCI bus
5155 **************************/
5156 if (pci_enable_device(pdev)) {
5157 err = -ENODEV;
5158 goto out_ieee80211_free_hw;
5159 }
5160
5161 pci_set_master(pdev);
5162
5163 err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
5164 if (!err)
5165 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
5166 if (err) {
5167 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5168 if (!err)
5169 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
5170 /* both attempts failed: */
5171 if (err) {
5172 printk(KERN_WARNING "%s: No suitable DMA available.\n",
5173 DRV_NAME);
5174 goto out_pci_disable_device;
5175 }
5176 }
5177
5178 err = pci_request_regions(pdev, DRV_NAME);
5179 if (err)
5180 goto out_pci_disable_device;
5181
5182 pci_set_drvdata(pdev, priv);
5183
5184 /* We disable the RETRY_TIMEOUT register (0x41) to keep
5185 * PCI Tx retries from interfering with C3 CPU state */
5186 pci_write_config_byte(pdev, 0x41, 0x00);
5187
5188 /***********************
5189 * 3. Read REV register
5190 ***********************/
5191 priv->hw_base = pci_iomap(pdev, 0, 0);
5192 if (!priv->hw_base) {
5193 err = -ENODEV;
5194 goto out_pci_release_regions;
5195 }
5196
5197 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
5198 (unsigned long long) pci_resource_len(pdev, 0));
5199 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
5200
5201 iwl_hw_detect(priv);
5202 printk(KERN_INFO DRV_NAME
5203 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
5204 priv->cfg->name, priv->hw_rev);
5205
5206 /* amp init */
5207 err = priv->cfg->ops->lib->apm_ops.init(priv);
5208 if (err < 0) {
5209 IWL_DEBUG_INFO("Failed to init APMG\n");
5210 goto out_iounmap;
5211 }
5212 /*****************
5213 * 4. Read EEPROM
5214 *****************/
5215 /* Read the EEPROM */
5216 err = iwl_eeprom_init(priv);
5217 if (err) {
5218 IWL_ERROR("Unable to init EEPROM\n");
5219 goto out_iounmap;
5220 }
5221 err = iwl_eeprom_check_version(priv);
5222 if (err)
5223 goto out_iounmap;
5224
5225 /* extract MAC Address */
5226 iwl_eeprom_get_mac(priv, priv->mac_addr);
5227 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
5228 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5229
5230 /************************
5231 * 5. Setup HW constants
5232 ************************/
5233 /* Device-specific setup */
5234 if (priv->cfg->ops->lib->set_hw_params(priv)) {
5235 IWL_ERROR("failed to set hw parameters\n");
5236 goto out_free_eeprom;
5237 }
5238
5239 /*******************
5240 * 6. Setup priv
5241 *******************/
5242
5243 err = iwl_init_drv(priv);
5244 if (err)
5245 goto out_free_eeprom;
5246 /* At this point both hw and priv are initialized. */
5247
5248 /**********************************
5249 * 7. Initialize module parameters
5250 **********************************/
5251
5252 /* Disable radio (SW RF KILL) via parameter when loading driver */
5253 if (priv->cfg->mod_params->disable) {
5254 set_bit(STATUS_RF_KILL_SW, &priv->status);
5255 IWL_DEBUG_INFO("Radio disabled.\n");
5256 }
5257
5258 /********************
5259 * 8. Setup services
5260 ********************/
5261 spin_lock_irqsave(&priv->lock, flags);
5262 iwl4965_disable_interrupts(priv);
5263 spin_unlock_irqrestore(&priv->lock, flags);
5264
5265 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5266 if (err) {
5267 IWL_ERROR("failed to create sysfs device attributes\n");
5268 goto out_uninit_drv;
5269 }
5270
5271
5272 iwl4965_setup_deferred_work(priv);
5273 iwl4965_setup_rx_handlers(priv);
5274
5275 /********************
5276 * 9. Conclude
5277 ********************/
5278 pci_save_state(pdev);
5279 pci_disable_device(pdev);
5280
5281 /**********************************
5282 * 10. Setup and register mac80211
5283 **********************************/
5284
5285 err = iwl_setup_mac(priv);
5286 if (err)
5287 goto out_remove_sysfs;
5288
5289 err = iwl_dbgfs_register(priv, DRV_NAME);
5290 if (err)
5291 IWL_ERROR("failed to create debugfs files\n");
5292
5293 /* notify iwlcore to init */
5294 iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
5295 return 0;
5296
5297 out_remove_sysfs:
5298 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5299 out_uninit_drv:
5300 iwl_uninit_drv(priv);
5301 out_free_eeprom:
5302 iwl_eeprom_free(priv);
5303 out_iounmap:
5304 pci_iounmap(pdev, priv->hw_base);
5305 out_pci_release_regions:
5306 pci_release_regions(pdev);
5307 pci_set_drvdata(pdev, NULL);
5308 out_pci_disable_device:
5309 pci_disable_device(pdev);
5310 out_ieee80211_free_hw:
5311 ieee80211_free_hw(priv->hw);
5312 out:
5313 return err;
5314 }
5315
5316 static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
5317 {
5318 struct iwl_priv *priv = pci_get_drvdata(pdev);
5319 struct list_head *p, *q;
5320 int i;
5321 unsigned long flags;
5322
5323 if (!priv)
5324 return;
5325
5326 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
5327
5328 iwl_dbgfs_unregister(priv);
5329 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5330
5331 if (priv->mac80211_registered) {
5332 ieee80211_unregister_hw(priv->hw);
5333 priv->mac80211_registered = 0;
5334 }
5335
5336 set_bit(STATUS_EXIT_PENDING, &priv->status);
5337
5338 iwl4965_down(priv);
5339
5340 /* make sure we flush any pending irq or
5341 * tasklet for the driver
5342 */
5343 spin_lock_irqsave(&priv->lock, flags);
5344 iwl4965_disable_interrupts(priv);
5345 spin_unlock_irqrestore(&priv->lock, flags);
5346
5347 iwl_synchronize_irq(priv);
5348
5349 /* Free MAC hash list for ADHOC */
5350 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
5351 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
5352 list_del(p);
5353 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
5354 }
5355 }
5356
5357 iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
5358
5359 iwl4965_dealloc_ucode_pci(priv);
5360
5361 if (priv->rxq.bd)
5362 iwl_rx_queue_free(priv, &priv->rxq);
5363 iwl_hw_txq_ctx_free(priv);
5364
5365 iwlcore_clear_stations_table(priv);
5366 iwl_eeprom_free(priv);
5367
5368
5369 /*netif_stop_queue(dev); */
5370 flush_workqueue(priv->workqueue);
5371
5372 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
5373 * priv->workqueue... so we can't take down the workqueue
5374 * until now... */
5375 destroy_workqueue(priv->workqueue);
5376 priv->workqueue = NULL;
5377
5378 pci_iounmap(pdev, priv->hw_base);
5379 pci_release_regions(pdev);
5380 pci_disable_device(pdev);
5381 pci_set_drvdata(pdev, NULL);
5382
5383 iwl_uninit_drv(priv);
5384
5385 if (priv->ibss_beacon)
5386 dev_kfree_skb(priv->ibss_beacon);
5387
5388 ieee80211_free_hw(priv->hw);
5389 }
5390
5391 #ifdef CONFIG_PM
5392
5393 static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
5394 {
5395 struct iwl_priv *priv = pci_get_drvdata(pdev);
5396
5397 if (priv->is_open) {
5398 set_bit(STATUS_IN_SUSPEND, &priv->status);
5399 iwl4965_mac_stop(priv->hw);
5400 priv->is_open = 1;
5401 }
5402
5403 pci_set_power_state(pdev, PCI_D3hot);
5404
5405 return 0;
5406 }
5407
5408 static int iwl4965_pci_resume(struct pci_dev *pdev)
5409 {
5410 struct iwl_priv *priv = pci_get_drvdata(pdev);
5411
5412 pci_set_power_state(pdev, PCI_D0);
5413
5414 if (priv->is_open)
5415 iwl4965_mac_start(priv->hw);
5416
5417 clear_bit(STATUS_IN_SUSPEND, &priv->status);
5418 return 0;
5419 }
5420
5421 #endif /* CONFIG_PM */
5422
5423 /*****************************************************************************
5424 *
5425 * driver and module entry point
5426 *
5427 *****************************************************************************/
5428
5429 /* Hardware specific file defines the PCI IDs table for that hardware module */
5430 static struct pci_device_id iwl_hw_card_ids[] = {
5431 {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
5432 {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
5433 #ifdef CONFIG_IWL5000
5434 {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
5435 {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
5436 {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
5437 #endif /* CONFIG_IWL5000 */
5438 {0}
5439 };
5440 MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
5441
5442 static struct pci_driver iwl_driver = {
5443 .name = DRV_NAME,
5444 .id_table = iwl_hw_card_ids,
5445 .probe = iwl4965_pci_probe,
5446 .remove = __devexit_p(iwl4965_pci_remove),
5447 #ifdef CONFIG_PM
5448 .suspend = iwl4965_pci_suspend,
5449 .resume = iwl4965_pci_resume,
5450 #endif
5451 };
5452
5453 static int __init iwl4965_init(void)
5454 {
5455
5456 int ret;
5457 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
5458 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
5459
5460 ret = iwl4965_rate_control_register();
5461 if (ret) {
5462 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
5463 return ret;
5464 }
5465
5466 ret = pci_register_driver(&iwl_driver);
5467 if (ret) {
5468 IWL_ERROR("Unable to initialize PCI module\n");
5469 goto error_register;
5470 }
5471
5472 return ret;
5473
5474 error_register:
5475 iwl4965_rate_control_unregister();
5476 return ret;
5477 }
5478
5479 static void __exit iwl4965_exit(void)
5480 {
5481 pci_unregister_driver(&iwl_driver);
5482 iwl4965_rate_control_unregister();
5483 }
5484
5485 module_exit(iwl4965_exit);
5486 module_init(iwl4965_init);
This page took 0.150837 seconds and 6 git commands to generate.