linux/drivers/net/wireless/wl12xx/main.c
<<
>>
Prefs
   1
   2/*
   3 * This file is part of wl1271
   4 *
   5 * Copyright (C) 2008-2010 Nokia Corporation
   6 *
   7 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * version 2 as published by the Free Software Foundation.
  12 *
  13 * This program is distributed in the hope that it will be useful, but
  14 * WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16 * General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21 * 02110-1301 USA
  22 *
  23 */
  24
  25#include <linux/module.h>
  26#include <linux/firmware.h>
  27#include <linux/delay.h>
  28#include <linux/spi/spi.h>
  29#include <linux/crc32.h>
  30#include <linux/etherdevice.h>
  31#include <linux/vmalloc.h>
  32#include <linux/platform_device.h>
  33#include <linux/slab.h>
  34#include <linux/wl12xx.h>
  35#include <linux/sched.h>
  36#include <linux/interrupt.h>
  37
  38#include "wl12xx.h"
  39#include "debug.h"
  40#include "wl12xx_80211.h"
  41#include "reg.h"
  42#include "io.h"
  43#include "event.h"
  44#include "tx.h"
  45#include "rx.h"
  46#include "ps.h"
  47#include "init.h"
  48#include "debugfs.h"
  49#include "cmd.h"
  50#include "boot.h"
  51#include "testmode.h"
  52#include "scan.h"
  53
  54#define WL1271_BOOT_RETRIES 3
  55
  56static struct conf_drv_settings default_conf = {
  57        .sg = {
  58                .params = {
  59                        [CONF_SG_ACL_BT_MASTER_MIN_BR] = 10,
  60                        [CONF_SG_ACL_BT_MASTER_MAX_BR] = 180,
  61                        [CONF_SG_ACL_BT_SLAVE_MIN_BR] = 10,
  62                        [CONF_SG_ACL_BT_SLAVE_MAX_BR] = 180,
  63                        [CONF_SG_ACL_BT_MASTER_MIN_EDR] = 10,
  64                        [CONF_SG_ACL_BT_MASTER_MAX_EDR] = 80,
  65                        [CONF_SG_ACL_BT_SLAVE_MIN_EDR] = 10,
  66                        [CONF_SG_ACL_BT_SLAVE_MAX_EDR] = 80,
  67                        [CONF_SG_ACL_WLAN_PS_MASTER_BR] = 8,
  68                        [CONF_SG_ACL_WLAN_PS_SLAVE_BR] = 8,
  69                        [CONF_SG_ACL_WLAN_PS_MASTER_EDR] = 20,
  70                        [CONF_SG_ACL_WLAN_PS_SLAVE_EDR] = 20,
  71                        [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MIN_BR] = 20,
  72                        [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MAX_BR] = 35,
  73                        [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MIN_BR] = 16,
  74                        [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MAX_BR] = 35,
  75                        [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MIN_EDR] = 32,
  76                        [CONF_SG_ACL_WLAN_ACTIVE_MASTER_MAX_EDR] = 50,
  77                        [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MIN_EDR] = 28,
  78                        [CONF_SG_ACL_WLAN_ACTIVE_SLAVE_MAX_EDR] = 50,
  79                        [CONF_SG_ACL_ACTIVE_SCAN_WLAN_BR] = 10,
  80                        [CONF_SG_ACL_ACTIVE_SCAN_WLAN_EDR] = 20,
  81                        [CONF_SG_ACL_PASSIVE_SCAN_BT_BR] = 75,
  82                        [CONF_SG_ACL_PASSIVE_SCAN_WLAN_BR] = 15,
  83                        [CONF_SG_ACL_PASSIVE_SCAN_BT_EDR] = 27,
  84                        [CONF_SG_ACL_PASSIVE_SCAN_WLAN_EDR] = 17,
  85                        /* active scan params */
  86                        [CONF_SG_AUTO_SCAN_PROBE_REQ] = 170,
  87                        [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_HV3] = 50,
  88                        [CONF_SG_ACTIVE_SCAN_DURATION_FACTOR_A2DP] = 100,
  89                        /* passive scan params */
  90                        [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP_BR] = 800,
  91                        [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_A2DP_EDR] = 200,
  92                        [CONF_SG_PASSIVE_SCAN_DURATION_FACTOR_HV3] = 200,
  93                        /* passive scan in dual antenna params */
  94                        [CONF_SG_CONSECUTIVE_HV3_IN_PASSIVE_SCAN] = 0,
  95                        [CONF_SG_BCN_HV3_COLLISION_THRESH_IN_PASSIVE_SCAN] = 0,
  96                        [CONF_SG_TX_RX_PROTECTION_BWIDTH_IN_PASSIVE_SCAN] = 0,
  97                        /* general params */
  98                        [CONF_SG_STA_FORCE_PS_IN_BT_SCO] = 1,
  99                        [CONF_SG_ANTENNA_CONFIGURATION] = 0,
 100                        [CONF_SG_BEACON_MISS_PERCENT] = 60,
 101                        [CONF_SG_DHCP_TIME] = 5000,
 102                        [CONF_SG_RXT] = 1200,
 103                        [CONF_SG_TXT] = 1000,
 104                        [CONF_SG_ADAPTIVE_RXT_TXT] = 1,
 105                        [CONF_SG_GENERAL_USAGE_BIT_MAP] = 3,
 106                        [CONF_SG_HV3_MAX_SERVED] = 6,
 107                        [CONF_SG_PS_POLL_TIMEOUT] = 10,
 108                        [CONF_SG_UPSD_TIMEOUT] = 10,
 109                        [CONF_SG_CONSECUTIVE_CTS_THRESHOLD] = 2,
 110                        [CONF_SG_STA_RX_WINDOW_AFTER_DTIM] = 5,
 111                        [CONF_SG_STA_CONNECTION_PROTECTION_TIME] = 30,
 112                        /* AP params */
 113                        [CONF_AP_BEACON_MISS_TX] = 3,
 114                        [CONF_AP_RX_WINDOW_AFTER_BEACON] = 10,
 115                        [CONF_AP_BEACON_WINDOW_INTERVAL] = 2,
 116                        [CONF_AP_CONNECTION_PROTECTION_TIME] = 0,
 117                        [CONF_AP_BT_ACL_VAL_BT_SERVE_TIME] = 25,
 118                        [CONF_AP_BT_ACL_VAL_WL_SERVE_TIME] = 25,
 119                        /* CTS Diluting params */
 120                        [CONF_SG_CTS_DILUTED_BAD_RX_PACKETS_TH] = 0,
 121                        [CONF_SG_CTS_CHOP_IN_DUAL_ANT_SCO_MASTER] = 0,
 122                },
 123                .state = CONF_SG_PROTECTIVE,
 124        },
 125        .rx = {
 126                .rx_msdu_life_time           = 512000,
 127                .packet_detection_threshold  = 0,
 128                .ps_poll_timeout             = 15,
 129                .upsd_timeout                = 15,
 130                .rts_threshold               = IEEE80211_MAX_RTS_THRESHOLD,
 131                .rx_cca_threshold            = 0,
 132                .irq_blk_threshold           = 0xFFFF,
 133                .irq_pkt_threshold           = 0,
 134                .irq_timeout                 = 600,
 135                .queue_type                  = CONF_RX_QUEUE_TYPE_LOW_PRIORITY,
 136        },
 137        .tx = {
 138                .tx_energy_detection         = 0,
 139                .sta_rc_conf                 = {
 140                        .enabled_rates       = 0,
 141                        .short_retry_limit   = 10,
 142                        .long_retry_limit    = 10,
 143                        .aflags              = 0,
 144                },
 145                .ac_conf_count               = 4,
 146                .ac_conf                     = {
 147                        [CONF_TX_AC_BE] = {
 148                                .ac          = CONF_TX_AC_BE,
 149                                .cw_min      = 15,
 150                                .cw_max      = 63,
 151                                .aifsn       = 3,
 152                                .tx_op_limit = 0,
 153                        },
 154                        [CONF_TX_AC_BK] = {
 155                                .ac          = CONF_TX_AC_BK,
 156                                .cw_min      = 15,
 157                                .cw_max      = 63,
 158                                .aifsn       = 7,
 159                                .tx_op_limit = 0,
 160                        },
 161                        [CONF_TX_AC_VI] = {
 162                                .ac          = CONF_TX_AC_VI,
 163                                .cw_min      = 15,
 164                                .cw_max      = 63,
 165                                .aifsn       = CONF_TX_AIFS_PIFS,
 166                                .tx_op_limit = 3008,
 167                        },
 168                        [CONF_TX_AC_VO] = {
 169                                .ac          = CONF_TX_AC_VO,
 170                                .cw_min      = 15,
 171                                .cw_max      = 63,
 172                                .aifsn       = CONF_TX_AIFS_PIFS,
 173                                .tx_op_limit = 1504,
 174                        },
 175                },
 176                .max_tx_retries = 100,
 177                .ap_aging_period = 300,
 178                .tid_conf_count = 4,
 179                .tid_conf = {
 180                        [CONF_TX_AC_BE] = {
 181                                .queue_id    = CONF_TX_AC_BE,
 182                                .channel_type = CONF_CHANNEL_TYPE_EDCF,
 183                                .tsid        = CONF_TX_AC_BE,
 184                                .ps_scheme   = CONF_PS_SCHEME_LEGACY,
 185                                .ack_policy  = CONF_ACK_POLICY_LEGACY,
 186                                .apsd_conf   = {0, 0},
 187                        },
 188                        [CONF_TX_AC_BK] = {
 189                                .queue_id    = CONF_TX_AC_BK,
 190                                .channel_type = CONF_CHANNEL_TYPE_EDCF,
 191                                .tsid        = CONF_TX_AC_BK,
 192                                .ps_scheme   = CONF_PS_SCHEME_LEGACY,
 193                                .ack_policy  = CONF_ACK_POLICY_LEGACY,
 194                                .apsd_conf   = {0, 0},
 195                        },
 196                        [CONF_TX_AC_VI] = {
 197                                .queue_id    = CONF_TX_AC_VI,
 198                                .channel_type = CONF_CHANNEL_TYPE_EDCF,
 199                                .tsid        = CONF_TX_AC_VI,
 200                                .ps_scheme   = CONF_PS_SCHEME_LEGACY,
 201                                .ack_policy  = CONF_ACK_POLICY_LEGACY,
 202                                .apsd_conf   = {0, 0},
 203                        },
 204                        [CONF_TX_AC_VO] = {
 205                                .queue_id    = CONF_TX_AC_VO,
 206                                .channel_type = CONF_CHANNEL_TYPE_EDCF,
 207                                .tsid        = CONF_TX_AC_VO,
 208                                .ps_scheme   = CONF_PS_SCHEME_LEGACY,
 209                                .ack_policy  = CONF_ACK_POLICY_LEGACY,
 210                                .apsd_conf   = {0, 0},
 211                        },
 212                },
 213                .frag_threshold              = IEEE80211_MAX_FRAG_THRESHOLD,
 214                .tx_compl_timeout            = 700,
 215                .tx_compl_threshold          = 4,
 216                .basic_rate                  = CONF_HW_BIT_RATE_1MBPS,
 217                .basic_rate_5                = CONF_HW_BIT_RATE_6MBPS,
 218                .tmpl_short_retry_limit      = 10,
 219                .tmpl_long_retry_limit       = 10,
 220                .tx_watchdog_timeout         = 5000,
 221        },
 222        .conn = {
 223                .wake_up_event               = CONF_WAKE_UP_EVENT_DTIM,
 224                .listen_interval             = 1,
 225                .suspend_wake_up_event       = CONF_WAKE_UP_EVENT_N_DTIM,
 226                .suspend_listen_interval     = 3,
 227                .bcn_filt_mode               = CONF_BCN_FILT_MODE_ENABLED,
 228                .bcn_filt_ie_count           = 2,
 229                .bcn_filt_ie = {
 230                        [0] = {
 231                                .ie          = WLAN_EID_CHANNEL_SWITCH,
 232                                .rule        = CONF_BCN_RULE_PASS_ON_APPEARANCE,
 233                        },
 234                        [1] = {
 235                                .ie          = WLAN_EID_HT_INFORMATION,
 236                                .rule        = CONF_BCN_RULE_PASS_ON_CHANGE,
 237                        },
 238                },
 239                .synch_fail_thold            = 10,
 240                .bss_lose_timeout            = 100,
 241                .beacon_rx_timeout           = 10000,
 242                .broadcast_timeout           = 20000,
 243                .rx_broadcast_in_ps          = 1,
 244                .ps_poll_threshold           = 10,
 245                .bet_enable                  = CONF_BET_MODE_ENABLE,
 246                .bet_max_consecutive         = 50,
 247                .psm_entry_retries           = 8,
 248                .psm_exit_retries            = 16,
 249                .psm_entry_nullfunc_retries  = 3,
 250                .dynamic_ps_timeout          = 200,
 251                .forced_ps                   = false,
 252                .keep_alive_interval         = 55000,
 253                .max_listen_interval         = 20,
 254        },
 255        .itrim = {
 256                .enable = false,
 257                .timeout = 50000,
 258        },
 259        .pm_config = {
 260                .host_clk_settling_time = 5000,
 261                .host_fast_wakeup_support = false
 262        },
 263        .roam_trigger = {
 264                .trigger_pacing               = 1,
 265                .avg_weight_rssi_beacon       = 20,
 266                .avg_weight_rssi_data         = 10,
 267                .avg_weight_snr_beacon        = 20,
 268                .avg_weight_snr_data          = 10,
 269        },
 270        .scan = {
 271                .min_dwell_time_active        = 7500,
 272                .max_dwell_time_active        = 30000,
 273                .min_dwell_time_passive       = 100000,
 274                .max_dwell_time_passive       = 100000,
 275                .num_probe_reqs               = 2,
 276                .split_scan_timeout           = 50000,
 277        },
 278        .sched_scan = {
 279                /* sched_scan requires dwell times in TU instead of TU/1000 */
 280                .min_dwell_time_active = 30,
 281                .max_dwell_time_active = 60,
 282                .dwell_time_passive    = 100,
 283                .dwell_time_dfs        = 150,
 284                .num_probe_reqs        = 2,
 285                .rssi_threshold        = -90,
 286                .snr_threshold         = 0,
 287        },
 288        .rf = {
 289                .tx_per_channel_power_compensation_2 = {
 290                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 291                },
 292                .tx_per_channel_power_compensation_5 = {
 293                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 294                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 295                        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 296                },
 297        },
 298        .ht = {
 299                .rx_ba_win_size = 8,
 300                .tx_ba_win_size = 64,
 301                .inactivity_timeout = 10000,
 302                .tx_ba_tid_bitmap = CONF_TX_BA_ENABLED_TID_BITMAP,
 303        },
 304        .mem_wl127x = {
 305                .num_stations                 = 1,
 306                .ssid_profiles                = 1,
 307                .rx_block_num                 = 70,
 308                .tx_min_block_num             = 40,
 309                .dynamic_memory               = 1,
 310                .min_req_tx_blocks            = 100,
 311                .min_req_rx_blocks            = 22,
 312                .tx_min                       = 27,
 313        },
 314        .mem_wl128x = {
 315                .num_stations                 = 1,
 316                .ssid_profiles                = 1,
 317                .rx_block_num                 = 40,
 318                .tx_min_block_num             = 40,
 319                .dynamic_memory               = 1,
 320                .min_req_tx_blocks            = 45,
 321                .min_req_rx_blocks            = 22,
 322                .tx_min                       = 27,
 323        },
 324        .fm_coex = {
 325                .enable                       = true,
 326                .swallow_period               = 5,
 327                .n_divider_fref_set_1         = 0xff,       /* default */
 328                .n_divider_fref_set_2         = 12,
 329                .m_divider_fref_set_1         = 148,
 330                .m_divider_fref_set_2         = 0xffff,     /* default */
 331                .coex_pll_stabilization_time  = 0xffffffff, /* default */
 332                .ldo_stabilization_time       = 0xffff,     /* default */
 333                .fm_disturbed_band_margin     = 0xff,       /* default */
 334                .swallow_clk_diff             = 0xff,       /* default */
 335        },
 336        .rx_streaming = {
 337                .duration                      = 150,
 338                .queues                        = 0x1,
 339                .interval                      = 20,
 340                .always                        = 0,
 341        },
 342        .fwlog = {
 343                .mode                         = WL12XX_FWLOG_ON_DEMAND,
 344                .mem_blocks                   = 2,
 345                .severity                     = 0,
 346                .timestamp                    = WL12XX_FWLOG_TIMESTAMP_DISABLED,
 347                .output                       = WL12XX_FWLOG_OUTPUT_HOST,
 348                .threshold                    = 0,
 349        },
 350        .hci_io_ds = HCI_IO_DS_6MA,
 351        .rate = {
 352                .rate_retry_score = 32000,
 353                .per_add = 8192,
 354                .per_th1 = 2048,
 355                .per_th2 = 4096,
 356                .max_per = 8100,
 357                .inverse_curiosity_factor = 5,
 358                .tx_fail_low_th = 4,
 359                .tx_fail_high_th = 10,
 360                .per_alpha_shift = 4,
 361                .per_add_shift = 13,
 362                .per_beta1_shift = 10,
 363                .per_beta2_shift = 8,
 364                .rate_check_up = 2,
 365                .rate_check_down = 12,
 366                .rate_retry_policy = {
 367                        0x00, 0x00, 0x00, 0x00, 0x00,
 368                        0x00, 0x00, 0x00, 0x00, 0x00,
 369                        0x00, 0x00, 0x00,
 370                },
 371        },
 372        .hangover = {
 373                .recover_time               = 0,
 374                .hangover_period            = 20,
 375                .dynamic_mode               = 1,
 376                .early_termination_mode     = 1,
 377                .max_period                 = 20,
 378                .min_period                 = 1,
 379                .increase_delta             = 1,
 380                .decrease_delta             = 2,
 381                .quiet_time                 = 4,
 382                .increase_time              = 1,
 383                .window_size                = 16,
 384        },
 385};
 386
 387static char *fwlog_param;
 388static bool bug_on_recovery;
 389
 390static void __wl1271_op_remove_interface(struct wl1271 *wl,
 391                                         struct ieee80211_vif *vif,
 392                                         bool reset_tx_queues);
 393static void wl1271_op_stop(struct ieee80211_hw *hw);
 394static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
 395
 396static int wl12xx_set_authorized(struct wl1271 *wl,
 397                                 struct wl12xx_vif *wlvif)
 398{
 399        int ret;
 400
 401        if (WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS))
 402                return -EINVAL;
 403
 404        if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
 405                return 0;
 406
 407        if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
 408                return 0;
 409
 410        ret = wl12xx_cmd_set_peer_state(wl, wlvif->sta.hlid);
 411        if (ret < 0)
 412                return ret;
 413
 414        wl12xx_croc(wl, wlvif->role_id);
 415
 416        wl1271_info("Association completed.");
 417        return 0;
 418}
 419
 420static int wl1271_reg_notify(struct wiphy *wiphy,
 421                             struct regulatory_request *request)
 422{
 423        struct ieee80211_supported_band *band;
 424        struct ieee80211_channel *ch;
 425        int i;
 426
 427        band = wiphy->bands[IEEE80211_BAND_5GHZ];
 428        for (i = 0; i < band->n_channels; i++) {
 429                ch = &band->channels[i];
 430                if (ch->flags & IEEE80211_CHAN_DISABLED)
 431                        continue;
 432
 433                if (ch->flags & IEEE80211_CHAN_RADAR)
 434                        ch->flags |= IEEE80211_CHAN_NO_IBSS |
 435                                     IEEE80211_CHAN_PASSIVE_SCAN;
 436
 437        }
 438
 439        return 0;
 440}
 441
 442static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 443                                   bool enable)
 444{
 445        int ret = 0;
 446
 447        /* we should hold wl->mutex */
 448        ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
 449        if (ret < 0)
 450                goto out;
 451
 452        if (enable)
 453                set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
 454        else
 455                clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
 456out:
 457        return ret;
 458}
 459
 460/*
 461 * this function is being called when the rx_streaming interval
 462 * has beed changed or rx_streaming should be disabled
 463 */
 464int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 465{
 466        int ret = 0;
 467        int period = wl->conf.rx_streaming.interval;
 468
 469        /* don't reconfigure if rx_streaming is disabled */
 470        if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
 471                goto out;
 472
 473        /* reconfigure/disable according to new streaming_period */
 474        if (period &&
 475            test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
 476            (wl->conf.rx_streaming.always ||
 477             test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
 478                ret = wl1271_set_rx_streaming(wl, wlvif, true);
 479        else {
 480                ret = wl1271_set_rx_streaming(wl, wlvif, false);
 481                /* don't cancel_work_sync since we might deadlock */
 482                del_timer_sync(&wlvif->rx_streaming_timer);
 483        }
 484out:
 485        return ret;
 486}
 487
 488static void wl1271_rx_streaming_enable_work(struct work_struct *work)
 489{
 490        int ret;
 491        struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
 492                                                rx_streaming_enable_work);
 493        struct wl1271 *wl = wlvif->wl;
 494
 495        mutex_lock(&wl->mutex);
 496
 497        if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
 498            !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
 499            (!wl->conf.rx_streaming.always &&
 500             !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
 501                goto out;
 502
 503        if (!wl->conf.rx_streaming.interval)
 504                goto out;
 505
 506        ret = wl1271_ps_elp_wakeup(wl);
 507        if (ret < 0)
 508                goto out;
 509
 510        ret = wl1271_set_rx_streaming(wl, wlvif, true);
 511        if (ret < 0)
 512                goto out_sleep;
 513
 514        /* stop it after some time of inactivity */
 515        mod_timer(&wlvif->rx_streaming_timer,
 516                  jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
 517
 518out_sleep:
 519        wl1271_ps_elp_sleep(wl);
 520out:
 521        mutex_unlock(&wl->mutex);
 522}
 523
 524static void wl1271_rx_streaming_disable_work(struct work_struct *work)
 525{
 526        int ret;
 527        struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
 528                                                rx_streaming_disable_work);
 529        struct wl1271 *wl = wlvif->wl;
 530
 531        mutex_lock(&wl->mutex);
 532
 533        if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
 534                goto out;
 535
 536        ret = wl1271_ps_elp_wakeup(wl);
 537        if (ret < 0)
 538                goto out;
 539
 540        ret = wl1271_set_rx_streaming(wl, wlvif, false);
 541        if (ret)
 542                goto out_sleep;
 543
 544out_sleep:
 545        wl1271_ps_elp_sleep(wl);
 546out:
 547        mutex_unlock(&wl->mutex);
 548}
 549
 550static void wl1271_rx_streaming_timer(unsigned long data)
 551{
 552        struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
 553        struct wl1271 *wl = wlvif->wl;
 554        ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
 555}
 556
 557/* wl->mutex must be taken */
 558void wl12xx_rearm_tx_watchdog_locked(struct wl1271 *wl)
 559{
 560        /* if the watchdog is not armed, don't do anything */
 561        if (wl->tx_allocated_blocks == 0)
 562                return;
 563
 564        cancel_delayed_work(&wl->tx_watchdog_work);
 565        ieee80211_queue_delayed_work(wl->hw, &wl->tx_watchdog_work,
 566                msecs_to_jiffies(wl->conf.tx.tx_watchdog_timeout));
 567}
 568
 569static void wl12xx_tx_watchdog_work(struct work_struct *work)
 570{
 571        struct delayed_work *dwork;
 572        struct wl1271 *wl;
 573
 574        dwork = container_of(work, struct delayed_work, work);
 575        wl = container_of(dwork, struct wl1271, tx_watchdog_work);
 576
 577        mutex_lock(&wl->mutex);
 578
 579        if (unlikely(wl->state == WL1271_STATE_OFF))
 580                goto out;
 581
 582        /* Tx went out in the meantime - everything is ok */
 583        if (unlikely(wl->tx_allocated_blocks == 0))
 584                goto out;
 585
 586        /*
 587         * if a ROC is in progress, we might not have any Tx for a long
 588         * time (e.g. pending Tx on the non-ROC channels)
 589         */
 590        if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
 591                wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to ROC",
 592                             wl->conf.tx.tx_watchdog_timeout);
 593                wl12xx_rearm_tx_watchdog_locked(wl);
 594                goto out;
 595        }
 596
 597        /*
 598         * if a scan is in progress, we might not have any Tx for a long
 599         * time
 600         */
 601        if (wl->scan.state != WL1271_SCAN_STATE_IDLE) {
 602                wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to scan",
 603                             wl->conf.tx.tx_watchdog_timeout);
 604                wl12xx_rearm_tx_watchdog_locked(wl);
 605                goto out;
 606        }
 607
 608        /*
 609        * AP might cache a frame for a long time for a sleeping station,
 610        * so rearm the timer if there's an AP interface with stations. If
 611        * Tx is genuinely stuck we will most hopefully discover it when all
 612        * stations are removed due to inactivity.
 613        */
 614        if (wl->active_sta_count) {
 615                wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms. AP has "
 616                             " %d stations",
 617                              wl->conf.tx.tx_watchdog_timeout,
 618                              wl->active_sta_count);
 619                wl12xx_rearm_tx_watchdog_locked(wl);
 620                goto out;
 621        }
 622
 623        wl1271_error("Tx stuck (in FW) for %d ms. Starting recovery",
 624                     wl->conf.tx.tx_watchdog_timeout);
 625        wl12xx_queue_recovery_work(wl);
 626
 627out:
 628        mutex_unlock(&wl->mutex);
 629}
 630
 631static void wl1271_conf_init(struct wl1271 *wl)
 632{
 633
 634        /*
 635         * This function applies the default configuration to the driver. This
 636         * function is invoked upon driver load (spi probe.)
 637         *
 638         * The configuration is stored in a run-time structure in order to
 639         * facilitate for run-time adjustment of any of the parameters. Making
 640         * changes to the configuration structure will apply the new values on
 641         * the next interface up (wl1271_op_start.)
 642         */
 643
 644        /* apply driver default configuration */
 645        memcpy(&wl->conf, &default_conf, sizeof(default_conf));
 646
 647        /* Adjust settings according to optional module parameters */
 648        if (fwlog_param) {
 649                if (!strcmp(fwlog_param, "continuous")) {
 650                        wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
 651                } else if (!strcmp(fwlog_param, "ondemand")) {
 652                        wl->conf.fwlog.mode = WL12XX_FWLOG_ON_DEMAND;
 653                } else if (!strcmp(fwlog_param, "dbgpins")) {
 654                        wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
 655                        wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
 656                } else if (!strcmp(fwlog_param, "disable")) {
 657                        wl->conf.fwlog.mem_blocks = 0;
 658                        wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
 659                } else {
 660                        wl1271_error("Unknown fwlog parameter %s", fwlog_param);
 661                }
 662        }
 663}
 664
 665static int wl1271_plt_init(struct wl1271 *wl)
 666{
 667        int ret;
 668
 669        if (wl->chip.id == CHIP_ID_1283_PG20)
 670                ret = wl128x_cmd_general_parms(wl);
 671        else
 672                ret = wl1271_cmd_general_parms(wl);
 673        if (ret < 0)
 674                return ret;
 675
 676        if (wl->chip.id == CHIP_ID_1283_PG20)
 677                ret = wl128x_cmd_radio_parms(wl);
 678        else
 679                ret = wl1271_cmd_radio_parms(wl);
 680        if (ret < 0)
 681                return ret;
 682
 683        if (wl->chip.id != CHIP_ID_1283_PG20) {
 684                ret = wl1271_cmd_ext_radio_parms(wl);
 685                if (ret < 0)
 686                        return ret;
 687        }
 688
 689        /* Chip-specific initializations */
 690        ret = wl1271_chip_specific_init(wl);
 691        if (ret < 0)
 692                return ret;
 693
 694        ret = wl1271_acx_init_mem_config(wl);
 695        if (ret < 0)
 696                return ret;
 697
 698        ret = wl12xx_acx_mem_cfg(wl);
 699        if (ret < 0)
 700                goto out_free_memmap;
 701
 702        /* Enable data path */
 703        ret = wl1271_cmd_data_path(wl, 1);
 704        if (ret < 0)
 705                goto out_free_memmap;
 706
 707        /* Configure for CAM power saving (ie. always active) */
 708        ret = wl1271_acx_sleep_auth(wl, WL1271_PSM_CAM);
 709        if (ret < 0)
 710                goto out_free_memmap;
 711
 712        /* configure PM */
 713        ret = wl1271_acx_pm_config(wl);
 714        if (ret < 0)
 715                goto out_free_memmap;
 716
 717        return 0;
 718
 719 out_free_memmap:
 720        kfree(wl->target_mem_map);
 721        wl->target_mem_map = NULL;
 722
 723        return ret;
 724}
 725
 726static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
 727                                        struct wl12xx_vif *wlvif,
 728                                        u8 hlid, u8 tx_pkts)
 729{
 730        bool fw_ps, single_sta;
 731
 732        fw_ps = test_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
 733        single_sta = (wl->active_sta_count == 1);
 734
 735        /*
 736         * Wake up from high level PS if the STA is asleep with too little
 737         * packets in FW or if the STA is awake.
 738         */
 739        if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
 740                wl12xx_ps_link_end(wl, wlvif, hlid);
 741
 742        /*
 743         * Start high-level PS if the STA is asleep with enough blocks in FW.
 744         * Make an exception if this is the only connected station. In this
 745         * case FW-memory congestion is not a problem.
 746         */
 747        else if (!single_sta && fw_ps && tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
 748                wl12xx_ps_link_start(wl, wlvif, hlid, true);
 749}
 750
 751static void wl12xx_irq_update_links_status(struct wl1271 *wl,
 752                                           struct wl12xx_vif *wlvif,
 753                                           struct wl12xx_fw_status *status)
 754{
 755        struct wl1271_link *lnk;
 756        u32 cur_fw_ps_map;
 757        u8 hlid, cnt;
 758
 759        /* TODO: also use link_fast_bitmap here */
 760
 761        cur_fw_ps_map = le32_to_cpu(status->link_ps_bitmap);
 762        if (wl->ap_fw_ps_map != cur_fw_ps_map) {
 763                wl1271_debug(DEBUG_PSM,
 764                             "link ps prev 0x%x cur 0x%x changed 0x%x",
 765                             wl->ap_fw_ps_map, cur_fw_ps_map,
 766                             wl->ap_fw_ps_map ^ cur_fw_ps_map);
 767
 768                wl->ap_fw_ps_map = cur_fw_ps_map;
 769        }
 770
 771        for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, WL12XX_MAX_LINKS) {
 772                lnk = &wl->links[hlid];
 773                cnt = status->tx_lnk_free_pkts[hlid] - lnk->prev_freed_pkts;
 774
 775                lnk->prev_freed_pkts = status->tx_lnk_free_pkts[hlid];
 776                lnk->allocated_pkts -= cnt;
 777
 778                wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
 779                                            lnk->allocated_pkts);
 780        }
 781}
 782
 783static void wl12xx_fw_status(struct wl1271 *wl,
 784                             struct wl12xx_fw_status *status)
 785{
 786        struct wl12xx_vif *wlvif;
 787        struct timespec ts;
 788        u32 old_tx_blk_count = wl->tx_blocks_available;
 789        int avail, freed_blocks;
 790        int i;
 791
 792        wl1271_raw_read(wl, FW_STATUS_ADDR, status, sizeof(*status), false);
 793
 794        wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
 795                     "drv_rx_counter = %d, tx_results_counter = %d)",
 796                     status->intr,
 797                     status->fw_rx_counter,
 798                     status->drv_rx_counter,
 799                     status->tx_results_counter);
 800
 801        for (i = 0; i < NUM_TX_QUEUES; i++) {
 802                /* prevent wrap-around in freed-packets counter */
 803                wl->tx_allocated_pkts[i] -=
 804                                (status->tx_released_pkts[i] -
 805                                wl->tx_pkts_freed[i]) & 0xff;
 806
 807                wl->tx_pkts_freed[i] = status->tx_released_pkts[i];
 808        }
 809
 810        /* prevent wrap-around in total blocks counter */
 811        if (likely(wl->tx_blocks_freed <=
 812                   le32_to_cpu(status->total_released_blks)))
 813                freed_blocks = le32_to_cpu(status->total_released_blks) -
 814                               wl->tx_blocks_freed;
 815        else
 816                freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
 817                               le32_to_cpu(status->total_released_blks);
 818
 819        wl->tx_blocks_freed = le32_to_cpu(status->total_released_blks);
 820
 821        wl->tx_allocated_blocks -= freed_blocks;
 822
 823        /*
 824         * If the FW freed some blocks:
 825         * If we still have allocated blocks - re-arm the timer, Tx is
 826         * not stuck. Otherwise, cancel the timer (no Tx currently).
 827         */
 828        if (freed_blocks) {
 829                if (wl->tx_allocated_blocks)
 830                        wl12xx_rearm_tx_watchdog_locked(wl);
 831                else
 832                        cancel_delayed_work(&wl->tx_watchdog_work);
 833        }
 834
 835        avail = le32_to_cpu(status->tx_total) - wl->tx_allocated_blocks;
 836
 837        /*
 838         * The FW might change the total number of TX memblocks before
 839         * we get a notification about blocks being released. Thus, the
 840         * available blocks calculation might yield a temporary result
 841         * which is lower than the actual available blocks. Keeping in
 842         * mind that only blocks that were allocated can be moved from
 843         * TX to RX, tx_blocks_available should never decrease here.
 844         */
 845        wl->tx_blocks_available = max((int)wl->tx_blocks_available,
 846                                      avail);
 847
 848        /* if more blocks are available now, tx work can be scheduled */
 849        if (wl->tx_blocks_available > old_tx_blk_count)
 850                clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
 851
 852        /* for AP update num of allocated TX blocks per link and ps status */
 853        wl12xx_for_each_wlvif_ap(wl, wlvif) {
 854                wl12xx_irq_update_links_status(wl, wlvif, status);
 855        }
 856
 857        /* update the host-chipset time offset */
 858        getnstimeofday(&ts);
 859        wl->time_offset = (timespec_to_ns(&ts) >> 10) -
 860                (s64)le32_to_cpu(status->fw_localtime);
 861}
 862
 863static void wl1271_flush_deferred_work(struct wl1271 *wl)
 864{
 865        struct sk_buff *skb;
 866
 867        /* Pass all received frames to the network stack */
 868        while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
 869                ieee80211_rx_ni(wl->hw, skb);
 870
 871        /* Return sent skbs to the network stack */
 872        while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
 873                ieee80211_tx_status_ni(wl->hw, skb);
 874}
 875
 876static void wl1271_netstack_work(struct work_struct *work)
 877{
 878        struct wl1271 *wl =
 879                container_of(work, struct wl1271, netstack_work);
 880
 881        do {
 882                wl1271_flush_deferred_work(wl);
 883        } while (skb_queue_len(&wl->deferred_rx_queue));
 884}
 885
 886#define WL1271_IRQ_MAX_LOOPS 256
 887
 888static irqreturn_t wl1271_irq(int irq, void *cookie)
 889{
 890        int ret;
 891        u32 intr;
 892        int loopcount = WL1271_IRQ_MAX_LOOPS;
 893        struct wl1271 *wl = (struct wl1271 *)cookie;
 894        bool done = false;
 895        unsigned int defer_count;
 896        unsigned long flags;
 897
 898        /* TX might be handled here, avoid redundant work */
 899        set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
 900        cancel_work_sync(&wl->tx_work);
 901
 902        /*
 903         * In case edge triggered interrupt must be used, we cannot iterate
 904         * more than once without introducing race conditions with the hardirq.
 905         */
 906        if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
 907                loopcount = 1;
 908
 909        mutex_lock(&wl->mutex);
 910
 911        wl1271_debug(DEBUG_IRQ, "IRQ work");
 912
 913        if (unlikely(wl->state == WL1271_STATE_OFF))
 914                goto out;
 915
 916        ret = wl1271_ps_elp_wakeup(wl);
 917        if (ret < 0)
 918                goto out;
 919
 920        while (!done && loopcount--) {
 921                /*
 922                 * In order to avoid a race with the hardirq, clear the flag
 923                 * before acknowledging the chip. Since the mutex is held,
 924                 * wl1271_ps_elp_wakeup cannot be called concurrently.
 925                 */
 926                clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
 927                smp_mb__after_clear_bit();
 928
 929                wl12xx_fw_status(wl, wl->fw_status);
 930                intr = le32_to_cpu(wl->fw_status->intr);
 931                intr &= WL1271_INTR_MASK;
 932                if (!intr) {
 933                        done = true;
 934                        continue;
 935                }
 936
 937                if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
 938                        wl1271_error("watchdog interrupt received! "
 939                                     "starting recovery.");
 940                        wl12xx_queue_recovery_work(wl);
 941
 942                        /* restarting the chip. ignore any other interrupt. */
 943                        goto out;
 944                }
 945
 946                if (likely(intr & WL1271_ACX_INTR_DATA)) {
 947                        wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
 948
 949                        wl12xx_rx(wl, wl->fw_status);
 950
 951                        /* Check if any tx blocks were freed */
 952                        spin_lock_irqsave(&wl->wl_lock, flags);
 953                        if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
 954                            wl1271_tx_total_queue_count(wl) > 0) {
 955                                spin_unlock_irqrestore(&wl->wl_lock, flags);
 956                                /*
 957                                 * In order to avoid starvation of the TX path,
 958                                 * call the work function directly.
 959                                 */
 960                                wl1271_tx_work_locked(wl);
 961                        } else {
 962                                spin_unlock_irqrestore(&wl->wl_lock, flags);
 963                        }
 964
 965                        /* check for tx results */
 966                        if (wl->fw_status->tx_results_counter !=
 967                            (wl->tx_results_count & 0xff))
 968                                wl1271_tx_complete(wl);
 969
 970                        /* Make sure the deferred queues don't get too long */
 971                        defer_count = skb_queue_len(&wl->deferred_tx_queue) +
 972                                      skb_queue_len(&wl->deferred_rx_queue);
 973                        if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
 974                                wl1271_flush_deferred_work(wl);
 975                }
 976
 977                if (intr & WL1271_ACX_INTR_EVENT_A) {
 978                        wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
 979                        wl1271_event_handle(wl, 0);
 980                }
 981
 982                if (intr & WL1271_ACX_INTR_EVENT_B) {
 983                        wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
 984                        wl1271_event_handle(wl, 1);
 985                }
 986
 987                if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
 988                        wl1271_debug(DEBUG_IRQ,
 989                                     "WL1271_ACX_INTR_INIT_COMPLETE");
 990
 991                if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
 992                        wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
 993        }
 994
 995        wl1271_ps_elp_sleep(wl);
 996
 997out:
 998        spin_lock_irqsave(&wl->wl_lock, flags);
 999        /* In case TX was not handled here, queue TX work */
1000        clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
1001        if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1002            wl1271_tx_total_queue_count(wl) > 0)
1003                ieee80211_queue_work(wl->hw, &wl->tx_work);
1004        spin_unlock_irqrestore(&wl->wl_lock, flags);
1005
1006        mutex_unlock(&wl->mutex);
1007
1008        return IRQ_HANDLED;
1009}
1010
1011struct vif_counter_data {
1012        u8 counter;
1013
1014        struct ieee80211_vif *cur_vif;
1015        bool cur_vif_running;
1016};
1017
1018static void wl12xx_vif_count_iter(void *data, u8 *mac,
1019                                  struct ieee80211_vif *vif)
1020{
1021        struct vif_counter_data *counter = data;
1022
1023        counter->counter++;
1024        if (counter->cur_vif == vif)
1025                counter->cur_vif_running = true;
1026}
1027
1028/* caller must not hold wl->mutex, as it might deadlock */
1029static void wl12xx_get_vif_count(struct ieee80211_hw *hw,
1030                               struct ieee80211_vif *cur_vif,
1031                               struct vif_counter_data *data)
1032{
1033        memset(data, 0, sizeof(*data));
1034        data->cur_vif = cur_vif;
1035
1036        ieee80211_iterate_active_interfaces(hw,
1037                                            wl12xx_vif_count_iter, data);
1038}
1039
1040static int wl12xx_fetch_firmware(struct wl1271 *wl, bool plt)
1041{
1042        const struct firmware *fw;
1043        const char *fw_name;
1044        enum wl12xx_fw_type fw_type;
1045        int ret;
1046
1047        if (plt) {
1048                fw_type = WL12XX_FW_TYPE_PLT;
1049                if (wl->chip.id == CHIP_ID_1283_PG20)
1050                        fw_name = WL128X_PLT_FW_NAME;
1051                else
1052                        fw_name = WL127X_PLT_FW_NAME;
1053        } else {
1054                /*
1055                 * we can't call wl12xx_get_vif_count() here because
1056                 * wl->mutex is taken, so use the cached last_vif_count value
1057                 */
1058                if (wl->last_vif_count > 1) {
1059                        fw_type = WL12XX_FW_TYPE_MULTI;
1060                        if (wl->chip.id == CHIP_ID_1283_PG20)
1061                                fw_name = WL128X_FW_NAME_MULTI;
1062                        else
1063                                fw_name = WL127X_FW_NAME_MULTI;
1064                } else {
1065                        fw_type = WL12XX_FW_TYPE_NORMAL;
1066                        if (wl->chip.id == CHIP_ID_1283_PG20)
1067                                fw_name = WL128X_FW_NAME_SINGLE;
1068                        else
1069                                fw_name = WL127X_FW_NAME_SINGLE;
1070                }
1071        }
1072
1073        if (wl->fw_type == fw_type)
1074                return 0;
1075
1076        wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
1077
1078        ret = request_firmware(&fw, fw_name, wl->dev);
1079
1080        if (ret < 0) {
1081                wl1271_error("could not get firmware %s: %d", fw_name, ret);
1082                return ret;
1083        }
1084
1085        if (fw->size % 4) {
1086                wl1271_error("firmware size is not multiple of 32 bits: %zu",
1087                             fw->size);
1088                ret = -EILSEQ;
1089                goto out;
1090        }
1091
1092        vfree(wl->fw);
1093        wl->fw_type = WL12XX_FW_TYPE_NONE;
1094        wl->fw_len = fw->size;
1095        wl->fw = vmalloc(wl->fw_len);
1096
1097        if (!wl->fw) {
1098                wl1271_error("could not allocate memory for the firmware");
1099                ret = -ENOMEM;
1100                goto out;
1101        }
1102
1103        memcpy(wl->fw, fw->data, wl->fw_len);
1104        ret = 0;
1105        wl->fw_type = fw_type;
1106out:
1107        release_firmware(fw);
1108
1109        return ret;
1110}
1111
1112static int wl1271_fetch_nvs(struct wl1271 *wl)
1113{
1114        const struct firmware *fw;
1115        int ret;
1116
1117        ret = request_firmware(&fw, WL12XX_NVS_NAME, wl->dev);
1118
1119        if (ret < 0) {
1120                wl1271_error("could not get nvs file %s: %d", WL12XX_NVS_NAME,
1121                             ret);
1122                return ret;
1123        }
1124
1125        wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
1126
1127        if (!wl->nvs) {
1128                wl1271_error("could not allocate memory for the nvs file");
1129                ret = -ENOMEM;
1130                goto out;
1131        }
1132
1133        wl->nvs_len = fw->size;
1134
1135out:
1136        release_firmware(fw);
1137
1138        return ret;
1139}
1140
1141void wl12xx_queue_recovery_work(struct wl1271 *wl)
1142{
1143        if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1144                ieee80211_queue_work(wl->hw, &wl->recovery_work);
1145}
1146
1147size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
1148{
1149        size_t len = 0;
1150
1151        /* The FW log is a length-value list, find where the log end */
1152        while (len < maxlen) {
1153                if (memblock[len] == 0)
1154                        break;
1155                if (len + memblock[len] + 1 > maxlen)
1156                        break;
1157                len += memblock[len] + 1;
1158        }
1159
1160        /* Make sure we have enough room */
1161        len = min(len, (size_t)(PAGE_SIZE - wl->fwlog_size));
1162
1163        /* Fill the FW log file, consumed by the sysfs fwlog entry */
1164        memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
1165        wl->fwlog_size += len;
1166
1167        return len;
1168}
1169
1170static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
1171{
1172        u32 addr;
1173        u32 first_addr;
1174        u8 *block;
1175
1176        if ((wl->quirks & WL12XX_QUIRK_FWLOG_NOT_IMPLEMENTED) ||
1177            (wl->conf.fwlog.mode != WL12XX_FWLOG_ON_DEMAND) ||
1178            (wl->conf.fwlog.mem_blocks == 0))
1179                return;
1180
1181        wl1271_info("Reading FW panic log");
1182
1183        block = kmalloc(WL12XX_HW_BLOCK_SIZE, GFP_KERNEL);
1184        if (!block)
1185                return;
1186
1187        /*
1188         * Make sure the chip is awake and the logger isn't active.
1189         * This might fail if the firmware hanged.
1190         */
1191        if (!wl1271_ps_elp_wakeup(wl))
1192                wl12xx_cmd_stop_fwlog(wl);
1193
1194        /* Read the first memory block address */
1195        wl12xx_fw_status(wl, wl->fw_status);
1196        first_addr = le32_to_cpu(wl->fw_status->log_start_addr);
1197        if (!first_addr)
1198                goto out;
1199
1200        /* Traverse the memory blocks linked list */
1201        addr = first_addr;
1202        do {
1203                memset(block, 0, WL12XX_HW_BLOCK_SIZE);
1204                wl1271_read_hwaddr(wl, addr, block, WL12XX_HW_BLOCK_SIZE,
1205                                   false);
1206
1207                /*
1208                 * Memory blocks are linked to one another. The first 4 bytes
1209                 * of each memory block hold the hardware address of the next
1210                 * one. The last memory block points to the first one.
1211                 */
1212                addr = le32_to_cpup((__le32 *)block);
1213                if (!wl12xx_copy_fwlog(wl, block + sizeof(addr),
1214                                       WL12XX_HW_BLOCK_SIZE - sizeof(addr)))
1215                        break;
1216        } while (addr && (addr != first_addr));
1217
1218        wake_up_interruptible(&wl->fwlog_waitq);
1219
1220out:
1221        kfree(block);
1222}
1223
1224static void wl1271_recovery_work(struct work_struct *work)
1225{
1226        struct wl1271 *wl =
1227                container_of(work, struct wl1271, recovery_work);
1228        struct wl12xx_vif *wlvif;
1229        struct ieee80211_vif *vif;
1230
1231        mutex_lock(&wl->mutex);
1232
1233        if (wl->state != WL1271_STATE_ON || wl->plt)
1234                goto out_unlock;
1235
1236        /* Avoid a recursive recovery */
1237        set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1238
1239        wl12xx_read_fwlog_panic(wl);
1240
1241        wl1271_info("Hardware recovery in progress. FW ver: %s pc: 0x%x",
1242                    wl->chip.fw_ver_str, wl1271_read32(wl, SCR_PAD4));
1243
1244        BUG_ON(bug_on_recovery &&
1245               !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags));
1246
1247        /*
1248         * Advance security sequence number to overcome potential progress
1249         * in the firmware during recovery. This doens't hurt if the network is
1250         * not encrypted.
1251         */
1252        wl12xx_for_each_wlvif(wl, wlvif) {
1253                if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
1254                    test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1255                        wlvif->tx_security_seq +=
1256                                WL1271_TX_SQN_POST_RECOVERY_PADDING;
1257        }
1258
1259        /* Prevent spurious TX during FW restart */
1260        ieee80211_stop_queues(wl->hw);
1261
1262        if (wl->sched_scanning) {
1263                ieee80211_sched_scan_stopped(wl->hw);
1264                wl->sched_scanning = false;
1265        }
1266
1267        /* reboot the chipset */
1268        while (!list_empty(&wl->wlvif_list)) {
1269                wlvif = list_first_entry(&wl->wlvif_list,
1270                                       struct wl12xx_vif, list);
1271                vif = wl12xx_wlvif_to_vif(wlvif);
1272                __wl1271_op_remove_interface(wl, vif, false);
1273        }
1274        mutex_unlock(&wl->mutex);
1275        wl1271_op_stop(wl->hw);
1276
1277        clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
1278
1279        ieee80211_restart_hw(wl->hw);
1280
1281        /*
1282         * Its safe to enable TX now - the queues are stopped after a request
1283         * to restart the HW.
1284         */
1285        ieee80211_wake_queues(wl->hw);
1286        return;
1287out_unlock:
1288        mutex_unlock(&wl->mutex);
1289}
1290
1291static void wl1271_fw_wakeup(struct wl1271 *wl)
1292{
1293        u32 elp_reg;
1294
1295        elp_reg = ELPCTRL_WAKE_UP;
1296        wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
1297}
1298
1299static int wl1271_setup(struct wl1271 *wl)
1300{
1301        wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
1302        if (!wl->fw_status)
1303                return -ENOMEM;
1304
1305        wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
1306        if (!wl->tx_res_if) {
1307                kfree(wl->fw_status);
1308                return -ENOMEM;
1309        }
1310
1311        return 0;
1312}
1313
1314static int wl12xx_set_power_on(struct wl1271 *wl)
1315{
1316        int ret;
1317
1318        msleep(WL1271_PRE_POWER_ON_SLEEP);
1319        ret = wl1271_power_on(wl);
1320        if (ret < 0)
1321                goto out;
1322        msleep(WL1271_POWER_ON_SLEEP);
1323        wl1271_io_reset(wl);
1324        wl1271_io_init(wl);
1325
1326        wl1271_set_partition(wl, &wl12xx_part_table[PART_DOWN]);
1327
1328        /* ELP module wake up */
1329        wl1271_fw_wakeup(wl);
1330
1331out:
1332        return ret;
1333}
1334
1335static int wl12xx_chip_wakeup(struct wl1271 *wl, bool plt)
1336{
1337        int ret = 0;
1338
1339        ret = wl12xx_set_power_on(wl);
1340        if (ret < 0)
1341                goto out;
1342
1343        /*
1344         * For wl127x based devices we could use the default block
1345         * size (512 bytes), but due to a bug in the sdio driver, we
1346         * need to set it explicitly after the chip is powered on.  To
1347         * simplify the code and since the performance impact is
1348         * negligible, we use the same block size for all different
1349         * chip types.
1350         */
1351        if (!wl1271_set_block_size(wl))
1352                wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
1353
1354        switch (wl->chip.id) {
1355        case CHIP_ID_1271_PG10:
1356                wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
1357                               wl->chip.id);
1358
1359                ret = wl1271_setup(wl);
1360                if (ret < 0)
1361                        goto out;
1362                wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
1363                break;
1364
1365        case CHIP_ID_1271_PG20:
1366                wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
1367                             wl->chip.id);
1368
1369                ret = wl1271_setup(wl);
1370                if (ret < 0)
1371                        goto out;
1372                wl->quirks |= WL12XX_QUIRK_NO_BLOCKSIZE_ALIGNMENT;
1373                break;
1374
1375        case CHIP_ID_1283_PG20:
1376                wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1283 PG20)",
1377                             wl->chip.id);
1378
1379                ret = wl1271_setup(wl);
1380                if (ret < 0)
1381                        goto out;
1382                break;
1383        case CHIP_ID_1283_PG10:
1384        default:
1385                wl1271_warning("unsupported chip id: 0x%x", wl->chip.id);
1386                ret = -ENODEV;
1387                goto out;
1388        }
1389
1390        ret = wl12xx_fetch_firmware(wl, plt);
1391        if (ret < 0)
1392                goto out;
1393
1394        /* No NVS from netlink, try to get it from the filesystem */
1395        if (wl->nvs == NULL) {
1396                ret = wl1271_fetch_nvs(wl);
1397                if (ret < 0)
1398                        goto out;
1399        }
1400
1401out:
1402        return ret;
1403}
1404
1405int wl1271_plt_start(struct wl1271 *wl)
1406{
1407        int retries = WL1271_BOOT_RETRIES;
1408        struct wiphy *wiphy = wl->hw->wiphy;
1409        int ret;
1410
1411        mutex_lock(&wl->mutex);
1412
1413        wl1271_notice("power up");
1414
1415        if (wl->state != WL1271_STATE_OFF) {
1416                wl1271_error("cannot go into PLT state because not "
1417                             "in off state: %d", wl->state);
1418                ret = -EBUSY;
1419                goto out;
1420        }
1421
1422        while (retries) {
1423                retries--;
1424                ret = wl12xx_chip_wakeup(wl, true);
1425                if (ret < 0)
1426                        goto power_off;
1427
1428                ret = wl1271_boot(wl);
1429                if (ret < 0)
1430                        goto power_off;
1431
1432                ret = wl1271_plt_init(wl);
1433                if (ret < 0)
1434                        goto irq_disable;
1435
1436                wl->plt = true;
1437                wl->state = WL1271_STATE_ON;
1438                wl1271_notice("firmware booted in PLT mode (%s)",
1439                              wl->chip.fw_ver_str);
1440
1441                /* update hw/fw version info in wiphy struct */
1442                wiphy->hw_version = wl->chip.id;
1443                strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1444                        sizeof(wiphy->fw_version));
1445
1446                goto out;
1447
1448irq_disable:
1449                mutex_unlock(&wl->mutex);
1450                /* Unlocking the mutex in the middle of handling is
1451                   inherently unsafe. In this case we deem it safe to do,
1452                   because we need to let any possibly pending IRQ out of
1453                   the system (and while we are WL1271_STATE_OFF the IRQ
1454                   work function will not do anything.) Also, any other
1455                   possible concurrent operations will fail due to the
1456                   current state, hence the wl1271 struct should be safe. */
1457                wl1271_disable_interrupts(wl);
1458                wl1271_flush_deferred_work(wl);
1459                cancel_work_sync(&wl->netstack_work);
1460                mutex_lock(&wl->mutex);
1461power_off:
1462                wl1271_power_off(wl);
1463        }
1464
1465        wl1271_error("firmware boot in PLT mode failed despite %d retries",
1466                     WL1271_BOOT_RETRIES);
1467out:
1468        mutex_unlock(&wl->mutex);
1469
1470        return ret;
1471}
1472
1473int wl1271_plt_stop(struct wl1271 *wl)
1474{
1475        int ret = 0;
1476
1477        wl1271_notice("power down");
1478
1479        /*
1480         * Interrupts must be disabled before setting the state to OFF.
1481         * Otherwise, the interrupt handler might be called and exit without
1482         * reading the interrupt status.
1483         */
1484        wl1271_disable_interrupts(wl);
1485        mutex_lock(&wl->mutex);
1486        if (!wl->plt) {
1487                mutex_unlock(&wl->mutex);
1488
1489                /*
1490                 * This will not necessarily enable interrupts as interrupts
1491                 * may have been disabled when op_stop was called. It will,
1492                 * however, balance the above call to disable_interrupts().
1493                 */
1494                wl1271_enable_interrupts(wl);
1495
1496                wl1271_error("cannot power down because not in PLT "
1497                             "state: %d", wl->state);
1498                ret = -EBUSY;
1499                goto out;
1500        }
1501
1502        mutex_unlock(&wl->mutex);
1503
1504        wl1271_flush_deferred_work(wl);
1505        cancel_work_sync(&wl->netstack_work);
1506        cancel_work_sync(&wl->recovery_work);
1507        cancel_delayed_work_sync(&wl->elp_work);
1508        cancel_delayed_work_sync(&wl->tx_watchdog_work);
1509
1510        mutex_lock(&wl->mutex);
1511        wl1271_power_off(wl);
1512        wl->flags = 0;
1513        wl->state = WL1271_STATE_OFF;
1514        wl->plt = false;
1515        wl->rx_counter = 0;
1516        mutex_unlock(&wl->mutex);
1517
1518out:
1519        return ret;
1520}
1521
1522static void wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1523{
1524        struct wl1271 *wl = hw->priv;
1525        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1526        struct ieee80211_vif *vif = info->control.vif;
1527        struct wl12xx_vif *wlvif = NULL;
1528        unsigned long flags;
1529        int q, mapping;
1530        u8 hlid;
1531
1532        if (vif)
1533                wlvif = wl12xx_vif_to_data(vif);
1534
1535        mapping = skb_get_queue_mapping(skb);
1536        q = wl1271_tx_get_queue(mapping);
1537
1538        hlid = wl12xx_tx_get_hlid(wl, wlvif, skb);
1539
1540        spin_lock_irqsave(&wl->wl_lock, flags);
1541
1542        /* queue the packet */
1543        if (hlid == WL12XX_INVALID_LINK_ID ||
1544            (wlvif && !test_bit(hlid, wlvif->links_map))) {
1545                wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
1546                ieee80211_free_txskb(hw, skb);
1547                goto out;
1548        }
1549
1550        wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d len %d",
1551                     hlid, q, skb->len);
1552        skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1553
1554        wl->tx_queue_count[q]++;
1555
1556        /*
1557         * The workqueue is slow to process the tx_queue and we need stop
1558         * the queue here, otherwise the queue will get too long.
1559         */
1560        if (wl->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK) {
1561                wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1562                ieee80211_stop_queue(wl->hw, mapping);
1563                set_bit(q, &wl->stopped_queues_map);
1564        }
1565
1566        /*
1567         * The chip specific setup must run before the first TX packet -
1568         * before that, the tx_work will not be initialized!
1569         */
1570
1571        if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1572            !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
1573                ieee80211_queue_work(wl->hw, &wl->tx_work);
1574
1575out:
1576        spin_unlock_irqrestore(&wl->wl_lock, flags);
1577}
1578
1579int wl1271_tx_dummy_packet(struct wl1271 *wl)
1580{
1581        unsigned long flags;
1582        int q;
1583
1584        /* no need to queue a new dummy packet if one is already pending */
1585        if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1586                return 0;
1587
1588        q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
1589
1590        spin_lock_irqsave(&wl->wl_lock, flags);
1591        set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
1592        wl->tx_queue_count[q]++;
1593        spin_unlock_irqrestore(&wl->wl_lock, flags);
1594
1595        /* The FW is low on RX memory blocks, so send the dummy packet asap */
1596        if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
1597                wl1271_tx_work_locked(wl);
1598
1599        /*
1600         * If the FW TX is busy, TX work will be scheduled by the threaded
1601         * interrupt handler function
1602         */
1603        return 0;
1604}
1605
1606/*
1607 * The size of the dummy packet should be at least 1400 bytes. However, in
1608 * order to minimize the number of bus transactions, aligning it to 512 bytes
1609 * boundaries could be beneficial, performance wise
1610 */
1611#define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1612
1613static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
1614{
1615        struct sk_buff *skb;
1616        struct ieee80211_hdr_3addr *hdr;
1617        unsigned int dummy_packet_size;
1618
1619        dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1620                            sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
1621
1622        skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
1623        if (!skb) {
1624                wl1271_warning("Failed to allocate a dummy packet skb");
1625                return NULL;
1626        }
1627
1628        skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1629
1630        hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
1631        memset(hdr, 0, sizeof(*hdr));
1632        hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1633                                         IEEE80211_STYPE_NULLFUNC |
1634                                         IEEE80211_FCTL_TODS);
1635
1636        memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
1637
1638        /* Dummy packets require the TID to be management */
1639        skb->priority = WL1271_TID_MGMT;
1640
1641        /* Initialize all fields that might be used */
1642        skb_set_queue_mapping(skb, 0);
1643        memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
1644
1645        return skb;
1646}
1647
1648
1649#ifdef CONFIG_PM
1650static int wl1271_configure_suspend_sta(struct wl1271 *wl,
1651                                        struct wl12xx_vif *wlvif)
1652{
1653        int ret = 0;
1654
1655        mutex_lock(&wl->mutex);
1656
1657        if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1658                goto out_unlock;
1659
1660        ret = wl1271_ps_elp_wakeup(wl);
1661        if (ret < 0)
1662                goto out_unlock;
1663
1664        ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1665                                    wl->conf.conn.suspend_wake_up_event,
1666                                    wl->conf.conn.suspend_listen_interval);
1667
1668        if (ret < 0)
1669                wl1271_error("suspend: set wake up conditions failed: %d", ret);
1670
1671
1672        wl1271_ps_elp_sleep(wl);
1673
1674out_unlock:
1675        mutex_unlock(&wl->mutex);
1676        return ret;
1677
1678}
1679
1680static int wl1271_configure_suspend_ap(struct wl1271 *wl,
1681                                       struct wl12xx_vif *wlvif)
1682{
1683        int ret = 0;
1684
1685        mutex_lock(&wl->mutex);
1686
1687        if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1688                goto out_unlock;
1689
1690        ret = wl1271_ps_elp_wakeup(wl);
1691        if (ret < 0)
1692                goto out_unlock;
1693
1694        ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
1695
1696        wl1271_ps_elp_sleep(wl);
1697out_unlock:
1698        mutex_unlock(&wl->mutex);
1699        return ret;
1700
1701}
1702
1703static int wl1271_configure_suspend(struct wl1271 *wl,
1704                                    struct wl12xx_vif *wlvif)
1705{
1706        if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1707                return wl1271_configure_suspend_sta(wl, wlvif);
1708        if (wlvif->bss_type == BSS_TYPE_AP_BSS)
1709                return wl1271_configure_suspend_ap(wl, wlvif);
1710        return 0;
1711}
1712
1713static void wl1271_configure_resume(struct wl1271 *wl,
1714                                    struct wl12xx_vif *wlvif)
1715{
1716        int ret = 0;
1717        bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
1718        bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
1719
1720        if ((!is_ap) && (!is_sta))
1721                return;
1722
1723        mutex_lock(&wl->mutex);
1724        ret = wl1271_ps_elp_wakeup(wl);
1725        if (ret < 0)
1726                goto out;
1727
1728        if (is_sta) {
1729                ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1730                                    wl->conf.conn.wake_up_event,
1731                                    wl->conf.conn.listen_interval);
1732
1733                if (ret < 0)
1734                        wl1271_error("resume: wake up conditions failed: %d",
1735                                     ret);
1736
1737        } else if (is_ap) {
1738                ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1739        }
1740
1741        wl1271_ps_elp_sleep(wl);
1742out:
1743        mutex_unlock(&wl->mutex);
1744}
1745
1746static int wl1271_op_suspend(struct ieee80211_hw *hw,
1747                            struct cfg80211_wowlan *wow)
1748{
1749        struct wl1271 *wl = hw->priv;
1750        struct wl12xx_vif *wlvif;
1751        int ret;
1752
1753        wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
1754        WARN_ON(!wow || !wow->any);
1755
1756        wl1271_tx_flush(wl);
1757
1758        wl->wow_enabled = true;
1759        wl12xx_for_each_wlvif(wl, wlvif) {
1760                ret = wl1271_configure_suspend(wl, wlvif);
1761                if (ret < 0) {
1762                        wl1271_warning("couldn't prepare device to suspend");
1763                        return ret;
1764                }
1765        }
1766        /* flush any remaining work */
1767        wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
1768
1769        /*
1770         * disable and re-enable interrupts in order to flush
1771         * the threaded_irq
1772         */
1773        wl1271_disable_interrupts(wl);
1774
1775        /*
1776         * set suspended flag to avoid triggering a new threaded_irq
1777         * work. no need for spinlock as interrupts are disabled.
1778         */
1779        set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1780
1781        wl1271_enable_interrupts(wl);
1782        flush_work(&wl->tx_work);
1783        flush_delayed_work(&wl->elp_work);
1784
1785        return 0;
1786}
1787
1788static int wl1271_op_resume(struct ieee80211_hw *hw)
1789{
1790        struct wl1271 *wl = hw->priv;
1791        struct wl12xx_vif *wlvif;
1792        unsigned long flags;
1793        bool run_irq_work = false;
1794
1795        wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1796                     wl->wow_enabled);
1797        WARN_ON(!wl->wow_enabled);
1798
1799        /*
1800         * re-enable irq_work enqueuing, and call irq_work directly if
1801         * there is a pending work.
1802         */
1803        spin_lock_irqsave(&wl->wl_lock, flags);
1804        clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1805        if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1806                run_irq_work = true;
1807        spin_unlock_irqrestore(&wl->wl_lock, flags);
1808
1809        if (run_irq_work) {
1810                wl1271_debug(DEBUG_MAC80211,
1811                             "run postponed irq_work directly");
1812                wl1271_irq(0, wl);
1813                wl1271_enable_interrupts(wl);
1814        }
1815        wl12xx_for_each_wlvif(wl, wlvif) {
1816                wl1271_configure_resume(wl, wlvif);
1817        }
1818        wl->wow_enabled = false;
1819
1820        return 0;
1821}
1822#endif
1823
1824static int wl1271_op_start(struct ieee80211_hw *hw)
1825{
1826        wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1827
1828        /*
1829         * We have to delay the booting of the hardware because
1830         * we need to know the local MAC address before downloading and
1831         * initializing the firmware. The MAC address cannot be changed
1832         * after boot, and without the proper MAC address, the firmware
1833         * will not function properly.
1834         *
1835         * The MAC address is first known when the corresponding interface
1836         * is added. That is where we will initialize the hardware.
1837         */
1838
1839        return 0;
1840}
1841
1842static void wl1271_op_stop(struct ieee80211_hw *hw)
1843{
1844        struct wl1271 *wl = hw->priv;
1845        int i;
1846
1847        wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
1848
1849        /*
1850         * Interrupts must be disabled before setting the state to OFF.
1851         * Otherwise, the interrupt handler might be called and exit without
1852         * reading the interrupt status.
1853         */
1854        wl1271_disable_interrupts(wl);
1855        mutex_lock(&wl->mutex);
1856        if (wl->state == WL1271_STATE_OFF) {
1857                mutex_unlock(&wl->mutex);
1858
1859                /*
1860                 * This will not necessarily enable interrupts as interrupts
1861                 * may have been disabled when op_stop was called. It will,
1862                 * however, balance the above call to disable_interrupts().
1863                 */
1864                wl1271_enable_interrupts(wl);
1865                return;
1866        }
1867
1868        /*
1869         * this must be before the cancel_work calls below, so that the work
1870         * functions don't perform further work.
1871         */
1872        wl->state = WL1271_STATE_OFF;
1873        mutex_unlock(&wl->mutex);
1874
1875        wl1271_flush_deferred_work(wl);
1876        cancel_delayed_work_sync(&wl->scan_complete_work);
1877        cancel_work_sync(&wl->netstack_work);
1878        cancel_work_sync(&wl->tx_work);
1879        cancel_delayed_work_sync(&wl->elp_work);
1880        cancel_delayed_work_sync(&wl->tx_watchdog_work);
1881
1882        /* let's notify MAC80211 about the remaining pending TX frames */
1883        wl12xx_tx_reset(wl, true);
1884        mutex_lock(&wl->mutex);
1885
1886        wl1271_power_off(wl);
1887
1888        wl->band = IEEE80211_BAND_2GHZ;
1889
1890        wl->rx_counter = 0;
1891        wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1892        wl->tx_blocks_available = 0;
1893        wl->tx_allocated_blocks = 0;
1894        wl->tx_results_count = 0;
1895        wl->tx_packets_count = 0;
1896        wl->time_offset = 0;
1897        wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
1898        wl->ap_fw_ps_map = 0;
1899        wl->ap_ps_map = 0;
1900        wl->sched_scanning = false;
1901        memset(wl->roles_map, 0, sizeof(wl->roles_map));
1902        memset(wl->links_map, 0, sizeof(wl->links_map));
1903        memset(wl->roc_map, 0, sizeof(wl->roc_map));
1904        wl->active_sta_count = 0;
1905
1906        /* The system link is always allocated */
1907        __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1908
1909        /*
1910         * this is performed after the cancel_work calls and the associated
1911         * mutex_lock, so that wl1271_op_add_interface does not accidentally
1912         * get executed before all these vars have been reset.
1913         */
1914        wl->flags = 0;
1915
1916        wl->tx_blocks_freed = 0;
1917
1918        for (i = 0; i < NUM_TX_QUEUES; i++) {
1919                wl->tx_pkts_freed[i] = 0;
1920                wl->tx_allocated_pkts[i] = 0;
1921        }
1922
1923        wl1271_debugfs_reset(wl);
1924
1925        kfree(wl->fw_status);
1926        wl->fw_status = NULL;
1927        kfree(wl->tx_res_if);
1928        wl->tx_res_if = NULL;
1929        kfree(wl->target_mem_map);
1930        wl->target_mem_map = NULL;
1931
1932        mutex_unlock(&wl->mutex);
1933}
1934
1935static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
1936{
1937        u8 policy = find_first_zero_bit(wl->rate_policies_map,
1938                                        WL12XX_MAX_RATE_POLICIES);
1939        if (policy >= WL12XX_MAX_RATE_POLICIES)
1940                return -EBUSY;
1941
1942        __set_bit(policy, wl->rate_policies_map);
1943        *idx = policy;
1944        return 0;
1945}
1946
1947static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
1948{
1949        if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
1950                return;
1951
1952        __clear_bit(*idx, wl->rate_policies_map);
1953        *idx = WL12XX_MAX_RATE_POLICIES;
1954}
1955
1956static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1957{
1958        switch (wlvif->bss_type) {
1959        case BSS_TYPE_AP_BSS:
1960                if (wlvif->p2p)
1961                        return WL1271_ROLE_P2P_GO;
1962                else
1963                        return WL1271_ROLE_AP;
1964
1965        case BSS_TYPE_STA_BSS:
1966                if (wlvif->p2p)
1967                        return WL1271_ROLE_P2P_CL;
1968                else
1969                        return WL1271_ROLE_STA;
1970
1971        case BSS_TYPE_IBSS:
1972                return WL1271_ROLE_IBSS;
1973
1974        default:
1975                wl1271_error("invalid bss_type: %d", wlvif->bss_type);
1976        }
1977        return WL12XX_INVALID_ROLE_TYPE;
1978}
1979
1980static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1981{
1982        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1983        int i;
1984
1985        /* clear everything but the persistent data */
1986        memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
1987
1988        switch (ieee80211_vif_type_p2p(vif)) {
1989        case NL80211_IFTYPE_P2P_CLIENT:
1990                wlvif->p2p = 1;
1991                /* fall-through */
1992        case NL80211_IFTYPE_STATION:
1993                wlvif->bss_type = BSS_TYPE_STA_BSS;
1994                break;
1995        case NL80211_IFTYPE_ADHOC:
1996                wlvif->bss_type = BSS_TYPE_IBSS;
1997                break;
1998        case NL80211_IFTYPE_P2P_GO:
1999                wlvif->p2p = 1;
2000                /* fall-through */
2001        case NL80211_IFTYPE_AP:
2002                wlvif->bss_type = BSS_TYPE_AP_BSS;
2003                break;
2004        default:
2005                wlvif->bss_type = MAX_BSS_TYPE;
2006                return -EOPNOTSUPP;
2007        }
2008
2009        wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2010        wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2011        wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2012
2013        if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2014            wlvif->bss_type == BSS_TYPE_IBSS) {
2015                /* init sta/ibss data */
2016                wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2017                wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2018                wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2019                wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2020        } else {
2021                /* init ap data */
2022                wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2023                wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2024                wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2025                wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2026                for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2027                        wl12xx_allocate_rate_policy(wl,
2028                                                &wlvif->ap.ucast_rate_idx[i]);
2029        }
2030
2031        wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
2032        wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
2033        wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
2034        wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
2035        wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
2036        wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
2037
2038        /*
2039         * mac80211 configures some values globally, while we treat them
2040         * per-interface. thus, on init, we have to copy them from wl
2041         */
2042        wlvif->band = wl->band;
2043        wlvif->channel = wl->channel;
2044        wlvif->power_level = wl->power_level;
2045
2046        INIT_WORK(&wlvif->rx_streaming_enable_work,
2047                  wl1271_rx_streaming_enable_work);
2048        INIT_WORK(&wlvif->rx_streaming_disable_work,
2049                  wl1271_rx_streaming_disable_work);
2050        INIT_LIST_HEAD(&wlvif->list);
2051
2052        setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
2053                    (unsigned long) wlvif);
2054        return 0;
2055}
2056
2057static bool wl12xx_init_fw(struct wl1271 *wl)
2058{
2059        int retries = WL1271_BOOT_RETRIES;
2060        bool booted = false;
2061        struct wiphy *wiphy = wl->hw->wiphy;
2062        int ret;
2063
2064        while (retries) {
2065                retries--;
2066                ret = wl12xx_chip_wakeup(wl, false);
2067                if (ret < 0)
2068                        goto power_off;
2069
2070                ret = wl1271_boot(wl);
2071                if (ret < 0)
2072                        goto power_off;
2073
2074                ret = wl1271_hw_init(wl);
2075                if (ret < 0)
2076                        goto irq_disable;
2077
2078                booted = true;
2079                break;
2080
2081irq_disable:
2082                mutex_unlock(&wl->mutex);
2083                /* Unlocking the mutex in the middle of handling is
2084                   inherently unsafe. In this case we deem it safe to do,
2085                   because we need to let any possibly pending IRQ out of
2086                   the system (and while we are WL1271_STATE_OFF the IRQ
2087                   work function will not do anything.) Also, any other
2088                   possible concurrent operations will fail due to the
2089                   current state, hence the wl1271 struct should be safe. */
2090                wl1271_disable_interrupts(wl);
2091                wl1271_flush_deferred_work(wl);
2092                cancel_work_sync(&wl->netstack_work);
2093                mutex_lock(&wl->mutex);
2094power_off:
2095                wl1271_power_off(wl);
2096        }
2097
2098        if (!booted) {
2099                wl1271_error("firmware boot failed despite %d retries",
2100                             WL1271_BOOT_RETRIES);
2101                goto out;
2102        }
2103
2104        wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
2105
2106        /* update hw/fw version info in wiphy struct */
2107        wiphy->hw_version = wl->chip.id;
2108        strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
2109                sizeof(wiphy->fw_version));
2110
2111        /*
2112         * Now we know if 11a is supported (info from the NVS), so disable
2113         * 11a channels if not supported
2114         */
2115        if (!wl->enable_11a)
2116                wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
2117
2118        wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2119                     wl->enable_11a ? "" : "not ");
2120
2121        wl->state = WL1271_STATE_ON;
2122out:
2123        return booted;
2124}
2125
2126static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2127{
2128        return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2129}
2130
2131/*
2132 * Check whether a fw switch (i.e. moving from one loaded
2133 * fw to another) is needed. This function is also responsible
2134 * for updating wl->last_vif_count, so it must be called before
2135 * loading a non-plt fw (so the correct fw (single-role/multi-role)
2136 * will be used).
2137 */
2138static bool wl12xx_need_fw_change(struct wl1271 *wl,
2139                                  struct vif_counter_data vif_counter_data,
2140                                  bool add)
2141{
2142        enum wl12xx_fw_type current_fw = wl->fw_type;
2143        u8 vif_count = vif_counter_data.counter;
2144
2145        if (test_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags))
2146                return false;
2147
2148        /* increase the vif count if this is a new vif */
2149        if (add && !vif_counter_data.cur_vif_running)
2150                vif_count++;
2151
2152        wl->last_vif_count = vif_count;
2153
2154        /* no need for fw change if the device is OFF */
2155        if (wl->state == WL1271_STATE_OFF)
2156                return false;
2157
2158        if (vif_count > 1 && current_fw == WL12XX_FW_TYPE_NORMAL)
2159                return true;
2160        if (vif_count <= 1 && current_fw == WL12XX_FW_TYPE_MULTI)
2161                return true;
2162
2163        return false;
2164}
2165
2166/*
2167 * Enter "forced psm". Make sure the sta is in psm against the ap,
2168 * to make the fw switch a bit more disconnection-persistent.
2169 */
2170static void wl12xx_force_active_psm(struct wl1271 *wl)
2171{
2172        struct wl12xx_vif *wlvif;
2173
2174        wl12xx_for_each_wlvif_sta(wl, wlvif) {
2175                wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
2176        }
2177}
2178
2179static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2180                                   struct ieee80211_vif *vif)
2181{
2182        struct wl1271 *wl = hw->priv;
2183        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2184        struct vif_counter_data vif_count;
2185        int ret = 0;
2186        u8 role_type;
2187        bool booted = false;
2188
2189        vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2190                             IEEE80211_VIF_SUPPORTS_CQM_RSSI;
2191
2192        wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2193                     ieee80211_vif_type_p2p(vif), vif->addr);
2194
2195        wl12xx_get_vif_count(hw, vif, &vif_count);
2196
2197        mutex_lock(&wl->mutex);
2198        ret = wl1271_ps_elp_wakeup(wl);
2199        if (ret < 0)
2200                goto out_unlock;
2201
2202        /*
2203         * in some very corner case HW recovery scenarios its possible to
2204         * get here before __wl1271_op_remove_interface is complete, so
2205         * opt out if that is the case.
2206         */
2207        if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2208            test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
2209                ret = -EBUSY;
2210                goto out;
2211        }
2212
2213
2214        ret = wl12xx_init_vif_data(wl, vif);
2215        if (ret < 0)
2216                goto out;
2217
2218        wlvif->wl = wl;
2219        role_type = wl12xx_get_role_type(wl, wlvif);
2220        if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2221                ret = -EINVAL;
2222                goto out;
2223        }
2224
2225        if (wl12xx_need_fw_change(wl, vif_count, true)) {
2226                wl12xx_force_active_psm(wl);
2227                set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2228                mutex_unlock(&wl->mutex);
2229                wl1271_recovery_work(&wl->recovery_work);
2230                return 0;
2231        }
2232
2233        /*
2234         * TODO: after the nvs issue will be solved, move this block
2235         * to start(), and make sure here the driver is ON.
2236         */
2237        if (wl->state == WL1271_STATE_OFF) {
2238                /*
2239                 * we still need this in order to configure the fw
2240                 * while uploading the nvs
2241                 */
2242                memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
2243
2244                booted = wl12xx_init_fw(wl);
2245                if (!booted) {
2246                        ret = -EINVAL;
2247                        goto out;
2248                }
2249        }
2250
2251        if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2252            wlvif->bss_type == BSS_TYPE_IBSS) {
2253                /*
2254                 * The device role is a special role used for
2255                 * rx and tx frames prior to association (as
2256                 * the STA role can get packets only from
2257                 * its associated bssid)
2258                 */
2259                ret = wl12xx_cmd_role_enable(wl, vif->addr,
2260                                                 WL1271_ROLE_DEVICE,
2261                                                 &wlvif->dev_role_id);
2262                if (ret < 0)
2263                        goto out;
2264        }
2265
2266        ret = wl12xx_cmd_role_enable(wl, vif->addr,
2267                                     role_type, &wlvif->role_id);
2268        if (ret < 0)
2269                goto out;
2270
2271        ret = wl1271_init_vif_specific(wl, vif);
2272        if (ret < 0)
2273                goto out;
2274
2275        list_add(&wlvif->list, &wl->wlvif_list);
2276        set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
2277
2278        if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2279                wl->ap_count++;
2280        else
2281                wl->sta_count++;
2282out:
2283        wl1271_ps_elp_sleep(wl);
2284out_unlock:
2285        mutex_unlock(&wl->mutex);
2286
2287        return ret;
2288}
2289
2290static void __wl1271_op_remove_interface(struct wl1271 *wl,
2291                                         struct ieee80211_vif *vif,
2292                                         bool reset_tx_queues)
2293{
2294        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2295        int i, ret;
2296
2297        wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
2298
2299        if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2300                return;
2301
2302        /* because of hardware recovery, we may get here twice */
2303        if (wl->state != WL1271_STATE_ON)
2304                return;
2305
2306        wl1271_info("down");
2307
2308        if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
2309            wl->scan_vif == vif) {
2310                /*
2311                 * Rearm the tx watchdog just before idling scan. This
2312                 * prevents just-finished scans from triggering the watchdog
2313                 */
2314                wl12xx_rearm_tx_watchdog_locked(wl);
2315
2316                wl->scan.state = WL1271_SCAN_STATE_IDLE;
2317                memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
2318                wl->scan_vif = NULL;
2319                wl->scan.req = NULL;
2320                ieee80211_scan_completed(wl->hw, true);
2321        }
2322
2323        if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2324                /* disable active roles */
2325                ret = wl1271_ps_elp_wakeup(wl);
2326                if (ret < 0)
2327                        goto deinit;
2328
2329                if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2330                    wlvif->bss_type == BSS_TYPE_IBSS) {
2331                        if (wl12xx_dev_role_started(wlvif))
2332                                wl12xx_stop_dev(wl, wlvif);
2333
2334                        ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2335                        if (ret < 0)
2336                                goto deinit;
2337                }
2338
2339                ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
2340                if (ret < 0)
2341                        goto deinit;
2342
2343                wl1271_ps_elp_sleep(wl);
2344        }
2345deinit:
2346        /* clear all hlids (except system_hlid) */
2347        wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2348
2349        if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2350            wlvif->bss_type == BSS_TYPE_IBSS) {
2351                wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2352                wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2353                wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2354                wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2355        } else {
2356                wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2357                wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2358                wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2359                wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2360                for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2361                        wl12xx_free_rate_policy(wl,
2362                                                &wlvif->ap.ucast_rate_idx[i]);
2363        }
2364
2365        wl12xx_tx_reset_wlvif(wl, wlvif);
2366        wl1271_free_ap_keys(wl, wlvif);
2367        if (wl->last_wlvif == wlvif)
2368                wl->last_wlvif = NULL;
2369        list_del(&wlvif->list);
2370        memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
2371        wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2372        wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2373
2374        if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2375                wl->ap_count--;
2376        else
2377                wl->sta_count--;
2378
2379        mutex_unlock(&wl->mutex);
2380
2381        del_timer_sync(&wlvif->rx_streaming_timer);
2382        cancel_work_sync(&wlvif->rx_streaming_enable_work);
2383        cancel_work_sync(&wlvif->rx_streaming_disable_work);
2384
2385        mutex_lock(&wl->mutex);
2386}
2387
2388static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2389                                       struct ieee80211_vif *vif)
2390{
2391        struct wl1271 *wl = hw->priv;
2392        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2393        struct wl12xx_vif *iter;
2394        struct vif_counter_data vif_count;
2395        bool cancel_recovery = true;
2396
2397        wl12xx_get_vif_count(hw, vif, &vif_count);
2398        mutex_lock(&wl->mutex);
2399
2400        if (wl->state == WL1271_STATE_OFF ||
2401            !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2402                goto out;
2403
2404        /*
2405         * wl->vif can be null here if someone shuts down the interface
2406         * just when hardware recovery has been started.
2407         */
2408        wl12xx_for_each_wlvif(wl, iter) {
2409                if (iter != wlvif)
2410                        continue;
2411
2412                __wl1271_op_remove_interface(wl, vif, true);
2413                break;
2414        }
2415        WARN_ON(iter != wlvif);
2416        if (wl12xx_need_fw_change(wl, vif_count, false)) {
2417                wl12xx_force_active_psm(wl);
2418                set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2419                wl12xx_queue_recovery_work(wl);
2420                cancel_recovery = false;
2421        }
2422out:
2423        mutex_unlock(&wl->mutex);
2424        if (cancel_recovery)
2425                cancel_work_sync(&wl->recovery_work);
2426}
2427
2428static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2429                                      struct ieee80211_vif *vif,
2430                                      enum nl80211_iftype new_type, bool p2p)
2431{
2432        struct wl1271 *wl = hw->priv;
2433        int ret;
2434
2435        set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2436        wl1271_op_remove_interface(hw, vif);
2437
2438        vif->type = new_type;
2439        vif->p2p = p2p;
2440        ret = wl1271_op_add_interface(hw, vif);
2441
2442        clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2443        return ret;
2444}
2445
2446static int wl1271_join(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2447                          bool set_assoc)
2448{
2449        int ret;
2450        bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
2451
2452        /*
2453         * One of the side effects of the JOIN command is that is clears
2454         * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2455         * to a WPA/WPA2 access point will therefore kill the data-path.
2456         * Currently the only valid scenario for JOIN during association
2457         * is on roaming, in which case we will also be given new keys.
2458         * Keep the below message for now, unless it starts bothering
2459         * users who really like to roam a lot :)
2460         */
2461        if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2462                wl1271_info("JOIN while associated.");
2463
2464        /* clear encryption type */
2465        wlvif->encryption_type = KEY_NONE;
2466
2467        if (set_assoc)
2468                set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2469
2470        if (is_ibss)
2471                ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
2472        else
2473                ret = wl12xx_cmd_role_start_sta(wl, wlvif);
2474        if (ret < 0)
2475                goto out;
2476
2477        if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2478                goto out;
2479
2480        /*
2481         * The join command disable the keep-alive mode, shut down its process,
2482         * and also clear the template config, so we need to reset it all after
2483         * the join. The acx_aid starts the keep-alive process, and the order
2484         * of the commands below is relevant.
2485         */
2486        ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
2487        if (ret < 0)
2488                goto out;
2489
2490        ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
2491        if (ret < 0)
2492                goto out;
2493
2494        ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
2495        if (ret < 0)
2496                goto out;
2497
2498        ret = wl1271_acx_keep_alive_config(wl, wlvif,
2499                                           CMD_TEMPL_KLV_IDX_NULL_DATA,
2500                                           ACX_KEEP_ALIVE_TPL_VALID);
2501        if (ret < 0)
2502                goto out;
2503
2504out:
2505        return ret;
2506}
2507
2508static int wl1271_unjoin(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2509{
2510        int ret;
2511
2512        if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
2513                struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2514
2515                wl12xx_cmd_stop_channel_switch(wl);
2516                ieee80211_chswitch_done(vif, false);
2517        }
2518
2519        /* to stop listening to a channel, we disconnect */
2520        ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
2521        if (ret < 0)
2522                goto out;
2523
2524        /* reset TX security counters on a clean disconnect */
2525        wlvif->tx_security_last_seq_lsb = 0;
2526        wlvif->tx_security_seq = 0;
2527
2528out:
2529        return ret;
2530}
2531
2532static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2533{
2534        wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
2535        wlvif->rate_set = wlvif->basic_rate_set;
2536}
2537
2538static int wl1271_sta_handle_idle(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2539                                  bool idle)
2540{
2541        int ret;
2542        bool cur_idle = !test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2543
2544        if (idle == cur_idle)
2545                return 0;
2546
2547        if (idle) {
2548                /* no need to croc if we weren't busy (e.g. during boot) */
2549                if (wl12xx_dev_role_started(wlvif)) {
2550                        ret = wl12xx_stop_dev(wl, wlvif);
2551                        if (ret < 0)
2552                                goto out;
2553                }
2554                wlvif->rate_set =
2555                        wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2556                ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2557                if (ret < 0)
2558                        goto out;
2559                ret = wl1271_acx_keep_alive_config(
2560                        wl, wlvif, CMD_TEMPL_KLV_IDX_NULL_DATA,
2561                        ACX_KEEP_ALIVE_TPL_INVALID);
2562                if (ret < 0)
2563                        goto out;
2564                clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2565        } else {
2566                /* The current firmware only supports sched_scan in idle */
2567                if (wl->sched_scanning) {
2568                        wl1271_scan_sched_scan_stop(wl);
2569                        ieee80211_sched_scan_stopped(wl->hw);
2570                }
2571
2572                ret = wl12xx_start_dev(wl, wlvif);
2573                if (ret < 0)
2574                        goto out;
2575                set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
2576        }
2577
2578out:
2579        return ret;
2580}
2581
2582static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2583                             struct ieee80211_conf *conf, u32 changed)
2584{
2585        bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2586        int channel, ret;
2587
2588        channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2589
2590        /* if the channel changes while joined, join again */
2591        if (changed & IEEE80211_CONF_CHANGE_CHANNEL &&
2592            ((wlvif->band != conf->channel->band) ||
2593             (wlvif->channel != channel))) {
2594                /* send all pending packets */
2595                wl1271_tx_work_locked(wl);
2596                wlvif->band = conf->channel->band;
2597                wlvif->channel = channel;
2598
2599                if (!is_ap) {
2600                        /*
2601                         * FIXME: the mac80211 should really provide a fixed
2602                         * rate to use here. for now, just use the smallest
2603                         * possible rate for the band as a fixed rate for
2604                         * association frames and other control messages.
2605                         */
2606                        if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2607                                wl1271_set_band_rate(wl, wlvif);
2608
2609                        wlvif->basic_rate =
2610                                wl1271_tx_min_rate_get(wl,
2611                                                       wlvif->basic_rate_set);
2612                        ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2613                        if (ret < 0)
2614                                wl1271_warning("rate policy for channel "
2615                                               "failed %d", ret);
2616
2617                        /*
2618                         * change the ROC channel. do it only if we are
2619                         * not idle. otherwise, CROC will be called
2620                         * anyway.
2621                         */
2622                        if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED,
2623                                      &wlvif->flags) &&
2624                            wl12xx_dev_role_started(wlvif) &&
2625                            !(conf->flags & IEEE80211_CONF_IDLE)) {
2626                                ret = wl12xx_stop_dev(wl, wlvif);
2627                                if (ret < 0)
2628                                        return ret;
2629
2630                                ret = wl12xx_start_dev(wl, wlvif);
2631                                if (ret < 0)
2632                                        return ret;
2633                        }
2634                }
2635        }
2636
2637        if ((changed & IEEE80211_CONF_CHANGE_PS) && !is_ap) {
2638
2639                if ((conf->flags & IEEE80211_CONF_PS) &&
2640                    test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
2641                    !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
2642
2643                        int ps_mode;
2644                        char *ps_mode_str;
2645
2646                        if (wl->conf.conn.forced_ps) {
2647                                ps_mode = STATION_POWER_SAVE_MODE;
2648                                ps_mode_str = "forced";
2649                        } else {
2650                                ps_mode = STATION_AUTO_PS_MODE;
2651                                ps_mode_str = "auto";
2652                        }
2653
2654                        wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
2655
2656                        ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
2657
2658                        if (ret < 0)
2659                                wl1271_warning("enter %s ps failed %d",
2660                                               ps_mode_str, ret);
2661
2662                } else if (!(conf->flags & IEEE80211_CONF_PS) &&
2663                           test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
2664
2665                        wl1271_debug(DEBUG_PSM, "auto ps disabled");
2666
2667                        ret = wl1271_ps_set_mode(wl, wlvif,
2668                                                 STATION_ACTIVE_MODE);
2669                        if (ret < 0)
2670                                wl1271_warning("exit auto ps failed %d", ret);
2671                }
2672        }
2673
2674        if (conf->power_level != wlvif->power_level) {
2675                ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
2676                if (ret < 0)
2677                        return ret;
2678
2679                wlvif->power_level = conf->power_level;
2680        }
2681
2682        return 0;
2683}
2684
2685static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2686{
2687        struct wl1271 *wl = hw->priv;
2688        struct wl12xx_vif *wlvif;
2689        struct ieee80211_conf *conf = &hw->conf;
2690        int channel, ret = 0;
2691
2692        channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2693
2694        wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d %s"
2695                     " changed 0x%x",
2696                     channel,
2697                     conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2698                     conf->power_level,
2699                     conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2700                         changed);
2701
2702        /*
2703         * mac80211 will go to idle nearly immediately after transmitting some
2704         * frames, such as the deauth. To make sure those frames reach the air,
2705         * wait here until the TX queue is fully flushed.
2706         */
2707        if ((changed & IEEE80211_CONF_CHANGE_IDLE) &&
2708            (conf->flags & IEEE80211_CONF_IDLE))
2709                wl1271_tx_flush(wl);
2710
2711        mutex_lock(&wl->mutex);
2712
2713        /* we support configuring the channel and band even while off */
2714        if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2715                wl->band = conf->channel->band;
2716                wl->channel = channel;
2717        }
2718
2719        if (changed & IEEE80211_CONF_CHANGE_POWER)
2720                wl->power_level = conf->power_level;
2721
2722        if (unlikely(wl->state == WL1271_STATE_OFF))
2723                goto out;
2724
2725        ret = wl1271_ps_elp_wakeup(wl);
2726        if (ret < 0)
2727                goto out;
2728
2729        /* configure each interface */
2730        wl12xx_for_each_wlvif(wl, wlvif) {
2731                ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2732                if (ret < 0)
2733                        goto out_sleep;
2734        }
2735
2736out_sleep:
2737        wl1271_ps_elp_sleep(wl);
2738
2739out:
2740        mutex_unlock(&wl->mutex);
2741
2742        return ret;
2743}
2744
2745struct wl1271_filter_params {
2746        bool enabled;
2747        int mc_list_length;
2748        u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2749};
2750
2751static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2752                                       struct netdev_hw_addr_list *mc_list)
2753{
2754        struct wl1271_filter_params *fp;
2755        struct netdev_hw_addr *ha;
2756        struct wl1271 *wl = hw->priv;
2757
2758        if (unlikely(wl->state == WL1271_STATE_OFF))
2759                return 0;
2760
2761        fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
2762        if (!fp) {
2763                wl1271_error("Out of memory setting filters.");
2764                return 0;
2765        }
2766
2767        /* update multicast filtering parameters */
2768        fp->mc_list_length = 0;
2769        if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2770                fp->enabled = false;
2771        } else {
2772                fp->enabled = true;
2773                netdev_hw_addr_list_for_each(ha, mc_list) {
2774                        memcpy(fp->mc_list[fp->mc_list_length],
2775                                        ha->addr, ETH_ALEN);
2776                        fp->mc_list_length++;
2777                }
2778        }
2779
2780        return (u64)(unsigned long)fp;
2781}
2782
2783#define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2784                                  FIF_ALLMULTI | \
2785                                  FIF_FCSFAIL | \
2786                                  FIF_BCN_PRBRESP_PROMISC | \
2787                                  FIF_CONTROL | \
2788                                  FIF_OTHER_BSS)
2789
2790static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2791                                       unsigned int changed,
2792                                       unsigned int *total, u64 multicast)
2793{
2794        struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
2795        struct wl1271 *wl = hw->priv;
2796        struct wl12xx_vif *wlvif;
2797
2798        int ret;
2799
2800        wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2801                     " total %x", changed, *total);
2802
2803        mutex_lock(&wl->mutex);
2804
2805        *total &= WL1271_SUPPORTED_FILTERS;
2806        changed &= WL1271_SUPPORTED_FILTERS;
2807
2808        if (unlikely(wl->state == WL1271_STATE_OFF))
2809                goto out;
2810
2811        ret = wl1271_ps_elp_wakeup(wl);
2812        if (ret < 0)
2813                goto out;
2814
2815        wl12xx_for_each_wlvif(wl, wlvif) {
2816                if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
2817                        if (*total & FIF_ALLMULTI)
2818                                ret = wl1271_acx_group_address_tbl(wl, wlvif,
2819                                                                   false,
2820                                                                   NULL, 0);
2821                        else if (fp)
2822                                ret = wl1271_acx_group_address_tbl(wl, wlvif,
2823                                                        fp->enabled,
2824                                                        fp->mc_list,
2825                                                        fp->mc_list_length);
2826                        if (ret < 0)
2827                                goto out_sleep;
2828                }
2829        }
2830
2831        /*
2832         * the fw doesn't provide an api to configure the filters. instead,
2833         * the filters configuration is based on the active roles / ROC
2834         * state.
2835         */
2836
2837out_sleep:
2838        wl1271_ps_elp_sleep(wl);
2839
2840out:
2841        mutex_unlock(&wl->mutex);
2842        kfree(fp);
2843}
2844
2845static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2846                                u8 id, u8 key_type, u8 key_size,
2847                                const u8 *key, u8 hlid, u32 tx_seq_32,
2848                                u16 tx_seq_16)
2849{
2850        struct wl1271_ap_key *ap_key;
2851        int i;
2852
2853        wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
2854
2855        if (key_size > MAX_KEY_SIZE)
2856                return -EINVAL;
2857
2858        /*
2859         * Find next free entry in ap_keys. Also check we are not replacing
2860         * an existing key.
2861         */
2862        for (i = 0; i < MAX_NUM_KEYS; i++) {
2863                if (wlvif->ap.recorded_keys[i] == NULL)
2864                        break;
2865
2866                if (wlvif->ap.recorded_keys[i]->id == id) {
2867                        wl1271_warning("trying to record key replacement");
2868                        return -EINVAL;
2869                }
2870        }
2871
2872        if (i == MAX_NUM_KEYS)
2873                return -EBUSY;
2874
2875        ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
2876        if (!ap_key)
2877                return -ENOMEM;
2878
2879        ap_key->id = id;
2880        ap_key->key_type = key_type;
2881        ap_key->key_size = key_size;
2882        memcpy(ap_key->key, key, key_size);
2883        ap_key->hlid = hlid;
2884        ap_key->tx_seq_32 = tx_seq_32;
2885        ap_key->tx_seq_16 = tx_seq_16;
2886
2887        wlvif->ap.recorded_keys[i] = ap_key;
2888        return 0;
2889}
2890
2891static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2892{
2893        int i;
2894
2895        for (i = 0; i < MAX_NUM_KEYS; i++) {
2896                kfree(wlvif->ap.recorded_keys[i]);
2897                wlvif->ap.recorded_keys[i] = NULL;
2898        }
2899}
2900
2901static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2902{
2903        int i, ret = 0;
2904        struct wl1271_ap_key *key;
2905        bool wep_key_added = false;
2906
2907        for (i = 0; i < MAX_NUM_KEYS; i++) {
2908                u8 hlid;
2909                if (wlvif->ap.recorded_keys[i] == NULL)
2910                        break;
2911
2912                key = wlvif->ap.recorded_keys[i];
2913                hlid = key->hlid;
2914                if (hlid == WL12XX_INVALID_LINK_ID)
2915                        hlid = wlvif->ap.bcast_hlid;
2916
2917                ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
2918                                            key->id, key->key_type,
2919                                            key->key_size, key->key,
2920                                            hlid, key->tx_seq_32,
2921                                            key->tx_seq_16);
2922                if (ret < 0)
2923                        goto out;
2924
2925                if (key->key_type == KEY_WEP)
2926                        wep_key_added = true;
2927        }
2928
2929        if (wep_key_added) {
2930                ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
2931                                                     wlvif->ap.bcast_hlid);
2932                if (ret < 0)
2933                        goto out;
2934        }
2935
2936out:
2937        wl1271_free_ap_keys(wl, wlvif);
2938        return ret;
2939}
2940
2941static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2942                       u16 action, u8 id, u8 key_type,
2943                       u8 key_size, const u8 *key, u32 tx_seq_32,
2944                       u16 tx_seq_16, struct ieee80211_sta *sta)
2945{
2946        int ret;
2947        bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2948
2949        if (is_ap) {
2950                struct wl1271_station *wl_sta;
2951                u8 hlid;
2952
2953                if (sta) {
2954                        wl_sta = (struct wl1271_station *)sta->drv_priv;
2955                        hlid = wl_sta->hlid;
2956                } else {
2957                        hlid = wlvif->ap.bcast_hlid;
2958                }
2959
2960                if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
2961                        /*
2962                         * We do not support removing keys after AP shutdown.
2963                         * Pretend we do to make mac80211 happy.
2964                         */
2965                        if (action != KEY_ADD_OR_REPLACE)
2966                                return 0;
2967
2968                        ret = wl1271_record_ap_key(wl, wlvif, id,
2969                                             key_type, key_size,
2970                                             key, hlid, tx_seq_32,
2971                                             tx_seq_16);
2972                } else {
2973                        ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
2974                                             id, key_type, key_size,
2975                                             key, hlid, tx_seq_32,
2976                                             tx_seq_16);
2977                }
2978
2979                if (ret < 0)
2980                        return ret;
2981        } else {
2982                const u8 *addr;
2983                static const u8 bcast_addr[ETH_ALEN] = {
2984                        0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2985                };
2986
2987                /*
2988                 * A STA set to GEM cipher requires 2 tx spare blocks.
2989                 * Return to default value when GEM cipher key is removed
2990                 */
2991                if (key_type == KEY_GEM) {
2992                        if (action == KEY_ADD_OR_REPLACE)
2993                                wl->tx_spare_blocks = 2;
2994                        else if (action == KEY_REMOVE)
2995                                wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
2996                }
2997
2998                addr = sta ? sta->addr : bcast_addr;
2999
3000                if (is_zero_ether_addr(addr)) {
3001                        /* We dont support TX only encryption */
3002                        return -EOPNOTSUPP;
3003                }
3004
3005                /* The wl1271 does not allow to remove unicast keys - they
3006                   will be cleared automatically on next CMD_JOIN. Ignore the
3007                   request silently, as we dont want the mac80211 to emit
3008                   an error message. */
3009                if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
3010                        return 0;
3011
3012                /* don't remove key if hlid was already deleted */
3013                if (action == KEY_REMOVE &&
3014                    wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
3015                        return 0;
3016
3017                ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
3018                                             id, key_type, key_size,
3019                                             key, addr, tx_seq_32,
3020                                             tx_seq_16);
3021                if (ret < 0)
3022                        return ret;
3023
3024                /* the default WEP key needs to be configured at least once */
3025                if (key_type == KEY_WEP) {
3026                        ret = wl12xx_cmd_set_default_wep_key(wl,
3027                                                        wlvif->default_key,
3028                                                        wlvif->sta.hlid);
3029                        if (ret < 0)
3030                                return ret;
3031                }
3032        }
3033
3034        return 0;
3035}
3036
3037static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3038                             struct ieee80211_vif *vif,
3039                             struct ieee80211_sta *sta,
3040                             struct ieee80211_key_conf *key_conf)
3041{
3042        struct wl1271 *wl = hw->priv;
3043        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3044        int ret;
3045        u32 tx_seq_32 = 0;
3046        u16 tx_seq_16 = 0;
3047        u8 key_type;
3048
3049        wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
3050
3051        wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
3052        wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
3053                     key_conf->cipher, key_conf->keyidx,
3054                     key_conf->keylen, key_conf->flags);
3055        wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3056
3057        mutex_lock(&wl->mutex);
3058
3059        if (unlikely(wl->state == WL1271_STATE_OFF)) {
3060                ret = -EAGAIN;
3061                goto out_unlock;
3062        }
3063
3064        ret = wl1271_ps_elp_wakeup(wl);
3065        if (ret < 0)
3066                goto out_unlock;
3067
3068        switch (key_conf->cipher) {
3069        case WLAN_CIPHER_SUITE_WEP40:
3070        case WLAN_CIPHER_SUITE_WEP104:
3071                key_type = KEY_WEP;
3072
3073                key_conf->hw_key_idx = key_conf->keyidx;
3074                break;
3075        case WLAN_CIPHER_SUITE_TKIP:
3076                key_type = KEY_TKIP;
3077
3078                key_conf->hw_key_idx = key_conf->keyidx;
3079                tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3080                tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3081                break;
3082        case WLAN_CIPHER_SUITE_CCMP:
3083                key_type = KEY_AES;
3084
3085                key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3086                tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3087                tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3088                break;
3089        case WL1271_CIPHER_SUITE_GEM:
3090                key_type = KEY_GEM;
3091                tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3092                tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
3093                break;
3094        default:
3095                wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
3096
3097                ret = -EOPNOTSUPP;
3098                goto out_sleep;
3099        }
3100
3101        switch (cmd) {
3102        case SET_KEY:
3103                ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
3104                                 key_conf->keyidx, key_type,
3105                                 key_conf->keylen, key_conf->key,
3106                                 tx_seq_32, tx_seq_16, sta);
3107                if (ret < 0) {
3108                        wl1271_error("Could not add or replace key");
3109                        goto out_sleep;
3110                }
3111
3112                /*
3113                 * reconfiguring arp response if the unicast (or common)
3114                 * encryption key type was changed
3115                 */
3116                if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3117                    (sta || key_type == KEY_WEP) &&
3118                    wlvif->encryption_type != key_type) {
3119                        wlvif->encryption_type = key_type;
3120                        ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3121                        if (ret < 0) {
3122                                wl1271_warning("build arp rsp failed: %d", ret);
3123                                goto out_sleep;
3124                        }
3125                }
3126                break;
3127
3128        case DISABLE_KEY:
3129                ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
3130                                     key_conf->keyidx, key_type,
3131                                     key_conf->keylen, key_conf->key,
3132                                     0, 0, sta);
3133                if (ret < 0) {
3134                        wl1271_error("Could not remove key");
3135                        goto out_sleep;
3136                }
3137                break;
3138
3139        default:
3140                wl1271_error("Unsupported key cmd 0x%x", cmd);
3141                ret = -EOPNOTSUPP;
3142                break;
3143        }
3144
3145out_sleep:
3146        wl1271_ps_elp_sleep(wl);
3147
3148out_unlock:
3149        mutex_unlock(&wl->mutex);
3150
3151        return ret;
3152}
3153
3154static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
3155                             struct ieee80211_vif *vif,
3156                             struct cfg80211_scan_request *req)
3157{
3158        struct wl1271 *wl = hw->priv;
3159        int ret;
3160        u8 *ssid = NULL;
3161        size_t len = 0;
3162
3163        wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3164
3165        if (req->n_ssids) {
3166                ssid = req->ssids[0].ssid;
3167                len = req->ssids[0].ssid_len;
3168        }
3169
3170        mutex_lock(&wl->mutex);
3171
3172        if (wl->state == WL1271_STATE_OFF) {
3173                /*
3174                 * We cannot return -EBUSY here because cfg80211 will expect
3175                 * a call to ieee80211_scan_completed if we do - in this case
3176                 * there won't be any call.
3177                 */
3178                ret = -EAGAIN;
3179                goto out;
3180        }
3181
3182        ret = wl1271_ps_elp_wakeup(wl);
3183        if (ret < 0)
3184                goto out;
3185
3186        /* fail if there is any role in ROC */
3187        if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
3188                /* don't allow scanning right now */
3189                ret = -EBUSY;
3190                goto out_sleep;
3191        }
3192
3193        ret = wl1271_scan(hw->priv, vif, ssid, len, req);
3194out_sleep:
3195        wl1271_ps_elp_sleep(wl);
3196out:
3197        mutex_unlock(&wl->mutex);
3198
3199        return ret;
3200}
3201
3202static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3203                                     struct ieee80211_vif *vif)
3204{
3205        struct wl1271 *wl = hw->priv;
3206        int ret;
3207
3208        wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3209
3210        mutex_lock(&wl->mutex);
3211
3212        if (wl->state == WL1271_STATE_OFF)
3213                goto out;
3214
3215        if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3216                goto out;
3217
3218        ret = wl1271_ps_elp_wakeup(wl);
3219        if (ret < 0)
3220                goto out;
3221
3222        if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3223                ret = wl1271_scan_stop(wl);
3224                if (ret < 0)
3225                        goto out_sleep;
3226        }
3227
3228        /*
3229         * Rearm the tx watchdog just before idling scan. This
3230         * prevents just-finished scans from triggering the watchdog
3231         */
3232        wl12xx_rearm_tx_watchdog_locked(wl);
3233
3234        wl->scan.state = WL1271_SCAN_STATE_IDLE;
3235        memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
3236        wl->scan_vif = NULL;
3237        wl->scan.req = NULL;
3238        ieee80211_scan_completed(wl->hw, true);
3239
3240out_sleep:
3241        wl1271_ps_elp_sleep(wl);
3242out:
3243        mutex_unlock(&wl->mutex);
3244
3245        cancel_delayed_work_sync(&wl->scan_complete_work);
3246}
3247
3248static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3249                                      struct ieee80211_vif *vif,
3250                                      struct cfg80211_sched_scan_request *req,
3251                                      struct ieee80211_sched_scan_ies *ies)
3252{
3253        struct wl1271 *wl = hw->priv;
3254        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3255        int ret;
3256
3257        wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3258
3259        mutex_lock(&wl->mutex);
3260
3261        if (wl->state == WL1271_STATE_OFF) {
3262                ret = -EAGAIN;
3263                goto out;
3264        }
3265
3266        ret = wl1271_ps_elp_wakeup(wl);
3267        if (ret < 0)
3268                goto out;
3269
3270        ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
3271        if (ret < 0)
3272                goto out_sleep;
3273
3274        ret = wl1271_scan_sched_scan_start(wl, wlvif);
3275        if (ret < 0)
3276                goto out_sleep;
3277
3278        wl->sched_scanning = true;
3279
3280out_sleep:
3281        wl1271_ps_elp_sleep(wl);
3282out:
3283        mutex_unlock(&wl->mutex);
3284        return ret;
3285}
3286
3287static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3288                                      struct ieee80211_vif *vif)
3289{
3290        struct wl1271 *wl = hw->priv;
3291        int ret;
3292
3293        wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3294
3295        mutex_lock(&wl->mutex);
3296
3297        if (wl->state == WL1271_STATE_OFF)
3298                goto out;
3299
3300        ret = wl1271_ps_elp_wakeup(wl);
3301        if (ret < 0)
3302                goto out;
3303
3304        wl1271_scan_sched_scan_stop(wl);
3305
3306        wl1271_ps_elp_sleep(wl);
3307out:
3308        mutex_unlock(&wl->mutex);
3309}
3310
3311static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3312{
3313        struct wl1271 *wl = hw->priv;
3314        int ret = 0;
3315
3316        mutex_lock(&wl->mutex);
3317
3318        if (unlikely(wl->state == WL1271_STATE_OFF)) {
3319                ret = -EAGAIN;
3320                goto out;
3321        }
3322
3323        ret = wl1271_ps_elp_wakeup(wl);
3324        if (ret < 0)
3325                goto out;
3326
3327        ret = wl1271_acx_frag_threshold(wl, value);
3328        if (ret < 0)
3329                wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3330
3331        wl1271_ps_elp_sleep(wl);
3332
3333out:
3334        mutex_unlock(&wl->mutex);
3335
3336        return ret;
3337}
3338
3339static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3340{
3341        struct wl1271 *wl = hw->priv;
3342        struct wl12xx_vif *wlvif;
3343        int ret = 0;
3344
3345        mutex_lock(&wl->mutex);
3346
3347        if (unlikely(wl->state == WL1271_STATE_OFF)) {
3348                ret = -EAGAIN;
3349                goto out;
3350        }
3351
3352        ret = wl1271_ps_elp_wakeup(wl);
3353        if (ret < 0)
3354                goto out;
3355
3356        wl12xx_for_each_wlvif(wl, wlvif) {
3357                ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3358                if (ret < 0)
3359                        wl1271_warning("set rts threshold failed: %d", ret);
3360        }
3361        wl1271_ps_elp_sleep(wl);
3362
3363out:
3364        mutex_unlock(&wl->mutex);
3365
3366        return ret;
3367}
3368
3369static int wl1271_ssid_set(struct ieee80211_vif *vif, struct sk_buff *skb,
3370                            int offset)
3371{
3372        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3373        u8 ssid_len;
3374        const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
3375                                         skb->len - offset);
3376
3377        if (!ptr) {
3378                wl1271_error("No SSID in IEs!");
3379                return -ENOENT;
3380        }
3381
3382        ssid_len = ptr[1];
3383        if (ssid_len > IEEE80211_MAX_SSID_LEN) {
3384                wl1271_error("SSID is too long!");
3385                return -EINVAL;
3386        }
3387
3388        wlvif->ssid_len = ssid_len;
3389        memcpy(wlvif->ssid, ptr+2, ssid_len);
3390        return 0;
3391}
3392
3393static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3394{
3395        int len;
3396        const u8 *next, *end = skb->data + skb->len;
3397        u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3398                                        skb->len - ieoffset);
3399        if (!ie)
3400                return;
3401        len = ie[1] + 2;
3402        next = ie + len;
3403        memmove(ie, next, end - next);
3404        skb_trim(skb, skb->len - len);
3405}
3406
3407static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3408                                            unsigned int oui, u8 oui_type,
3409                                            int ieoffset)
3410{
3411        int len;
3412        const u8 *next, *end = skb->data + skb->len;
3413        u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3414                                               skb->data + ieoffset,
3415                                               skb->len - ieoffset);
3416        if (!ie)
3417                return;
3418        len = ie[1] + 2;
3419        next = ie + len;
3420        memmove(ie, next, end - next);
3421        skb_trim(skb, skb->len - len);
3422}
3423
3424static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3425                                         struct ieee80211_vif *vif)
3426{
3427        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3428        struct sk_buff *skb;
3429        int ret;
3430
3431        skb = ieee80211_proberesp_get(wl->hw, vif);
3432        if (!skb)
3433                return -EOPNOTSUPP;
3434
3435        ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3436                                      CMD_TEMPL_AP_PROBE_RESPONSE,
3437                                      skb->data,
3438                                      skb->len, 0,
3439                                      rates);
3440
3441        dev_kfree_skb(skb);
3442        return ret;
3443}
3444
3445static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3446                                             struct ieee80211_vif *vif,
3447                                             u8 *probe_rsp_data,
3448                                             size_t probe_rsp_len,
3449                                             u32 rates)
3450{
3451        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3452        struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
3453        u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3454        int ssid_ie_offset, ie_offset, templ_len;
3455        const u8 *ptr;
3456
3457        /* no need to change probe response if the SSID is set correctly */
3458        if (wlvif->ssid_len > 0)
3459                return wl1271_cmd_template_set(wl, wlvif->role_id,
3460                                               CMD_TEMPL_AP_PROBE_RESPONSE,
3461                                               probe_rsp_data,
3462                                               probe_rsp_len, 0,
3463                                               rates);
3464
3465        if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3466                wl1271_error("probe_rsp template too big");
3467                return -EINVAL;
3468        }
3469
3470        /* start searching from IE offset */
3471        ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3472
3473        ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3474                               probe_rsp_len - ie_offset);
3475        if (!ptr) {
3476                wl1271_error("No SSID in beacon!");
3477                return -EINVAL;
3478        }
3479
3480        ssid_ie_offset = ptr - probe_rsp_data;
3481        ptr += (ptr[1] + 2);
3482
3483        memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3484
3485        /* insert SSID from bss_conf */
3486        probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3487        probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3488        memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3489               bss_conf->ssid, bss_conf->ssid_len);
3490        templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3491
3492        memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3493               ptr, probe_rsp_len - (ptr - probe_rsp_data));
3494        templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3495
3496        return wl1271_cmd_template_set(wl, wlvif->role_id,
3497                                       CMD_TEMPL_AP_PROBE_RESPONSE,
3498                                       probe_rsp_templ,
3499                                       templ_len, 0,
3500                                       rates);
3501}
3502
3503static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
3504                                       struct ieee80211_vif *vif,
3505                                       struct ieee80211_bss_conf *bss_conf,
3506                                       u32 changed)
3507{
3508        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3509        int ret = 0;
3510
3511        if (changed & BSS_CHANGED_ERP_SLOT) {
3512                if (bss_conf->use_short_slot)
3513                        ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
3514                else
3515                        ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
3516                if (ret < 0) {
3517                        wl1271_warning("Set slot time failed %d", ret);
3518                        goto out;
3519                }
3520        }
3521
3522        if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3523                if (bss_conf->use_short_preamble)
3524                        wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
3525                else
3526                        wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
3527        }
3528
3529        if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3530                if (bss_conf->use_cts_prot)
3531                        ret = wl1271_acx_cts_protect(wl, wlvif,
3532                                                     CTSPROTECT_ENABLE);
3533                else
3534                        ret = wl1271_acx_cts_protect(wl, wlvif,
3535                                                     CTSPROTECT_DISABLE);
3536                if (ret < 0) {
3537                        wl1271_warning("Set ctsprotect failed %d", ret);
3538                        goto out;
3539                }
3540        }
3541
3542out:
3543        return ret;
3544}
3545
3546static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3547                                          struct ieee80211_vif *vif,
3548                                          struct ieee80211_bss_conf *bss_conf,
3549                                          u32 changed)
3550{
3551        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3552        bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3553        int ret = 0;
3554
3555        if ((changed & BSS_CHANGED_BEACON_INT)) {
3556                wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
3557                        bss_conf->beacon_int);
3558
3559                wlvif->beacon_int = bss_conf->beacon_int;
3560        }
3561
3562        if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3563                u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3564                if (!wl1271_ap_set_probe_resp_tmpl(wl, rate, vif)) {
3565                        wl1271_debug(DEBUG_AP, "probe response updated");
3566                        set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3567                }
3568        }
3569
3570        if ((changed & BSS_CHANGED_BEACON)) {
3571                struct ieee80211_hdr *hdr;
3572                u32 min_rate;
3573                int ieoffset = offsetof(struct ieee80211_mgmt,
3574                                        u.beacon.variable);
3575                struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3576                u16 tmpl_id;
3577
3578                if (!beacon) {
3579                        ret = -EINVAL;
3580                        goto out;
3581                }
3582
3583                wl1271_debug(DEBUG_MASTER, "beacon updated");
3584
3585                ret = wl1271_ssid_set(vif, beacon, ieoffset);
3586                if (ret < 0) {
3587                        dev_kfree_skb(beacon);
3588                        goto out;
3589                }
3590                min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3591                tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3592                                  CMD_TEMPL_BEACON;
3593                ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3594                                              beacon->data,
3595                                              beacon->len, 0,
3596                                              min_rate);
3597                if (ret < 0) {
3598                        dev_kfree_skb(beacon);
3599                        goto out;
3600                }
3601
3602                /*
3603                 * In case we already have a probe-resp beacon set explicitly
3604                 * by usermode, don't use the beacon data.
3605                 */
3606                if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3607                        goto end_bcn;
3608
3609                /* remove TIM ie from probe response */
3610                wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3611
3612                /*
3613                 * remove p2p ie from probe response.
3614                 * the fw reponds to probe requests that don't include
3615                 * the p2p ie. probe requests with p2p ie will be passed,
3616                 * and will be responded by the supplicant (the spec
3617                 * forbids including the p2p ie when responding to probe
3618                 * requests that didn't include it).
3619                 */
3620                wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3621                                        WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3622
3623                hdr = (struct ieee80211_hdr *) beacon->data;
3624                hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3625                                                 IEEE80211_STYPE_PROBE_RESP);
3626                if (is_ap)
3627                        ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
3628                                                beacon->data,
3629                                                beacon->len,
3630                                                min_rate);
3631                else
3632                        ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3633                                                CMD_TEMPL_PROBE_RESPONSE,
3634                                                beacon->data,
3635                                                beacon->len, 0,
3636                                                min_rate);
3637end_bcn:
3638                dev_kfree_skb(beacon);
3639                if (ret < 0)
3640                        goto out;
3641        }
3642
3643out:
3644        if (ret != 0)
3645                wl1271_error("beacon info change failed: %d", ret);
3646        return ret;
3647}
3648
3649/* AP mode changes */
3650static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3651                                       struct ieee80211_vif *vif,
3652                                       struct ieee80211_bss_conf *bss_conf,
3653                                       u32 changed)
3654{
3655        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3656        int ret = 0;
3657
3658        if ((changed & BSS_CHANGED_BASIC_RATES)) {
3659                u32 rates = bss_conf->basic_rates;
3660
3661                wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
3662                                                                 wlvif->band);
3663                wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
3664                                                        wlvif->basic_rate_set);
3665
3666                ret = wl1271_init_ap_rates(wl, wlvif);
3667                if (ret < 0) {
3668                        wl1271_error("AP rate policy change failed %d", ret);
3669                        goto out;
3670                }
3671
3672                ret = wl1271_ap_init_templates(wl, vif);
3673                if (ret < 0)
3674                        goto out;
3675        }
3676
3677        ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3678        if (ret < 0)
3679                goto out;
3680
3681        if ((changed & BSS_CHANGED_BEACON_ENABLED)) {
3682                if (bss_conf->enable_beacon) {
3683                        if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3684                                ret = wl12xx_cmd_role_start_ap(wl, wlvif);
3685                                if (ret < 0)
3686                                        goto out;
3687
3688                                ret = wl1271_ap_init_hwenc(wl, wlvif);
3689                                if (ret < 0)
3690                                        goto out;
3691
3692                                set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3693                                wl1271_debug(DEBUG_AP, "started AP");
3694                        }
3695                } else {
3696                        if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3697                                ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
3698                                if (ret < 0)
3699                                        goto out;
3700
3701                                clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
3702                                clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3703                                          &wlvif->flags);
3704                                wl1271_debug(DEBUG_AP, "stopped AP");
3705                        }
3706                }
3707        }
3708
3709        ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3710        if (ret < 0)
3711                goto out;
3712
3713        /* Handle HT information change */
3714        if ((changed & BSS_CHANGED_HT) &&
3715            (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3716                ret = wl1271_acx_set_ht_information(wl, wlvif,
3717                                        bss_conf->ht_operation_mode);
3718                if (ret < 0) {
3719                        wl1271_warning("Set ht information failed %d", ret);
3720                        goto out;
3721                }
3722        }
3723
3724out:
3725        return;
3726}
3727
3728/* STA/IBSS mode changes */
3729static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3730                                        struct ieee80211_vif *vif,
3731                                        struct ieee80211_bss_conf *bss_conf,
3732                                        u32 changed)
3733{
3734        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3735        bool do_join = false, set_assoc = false;
3736        bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
3737        bool ibss_joined = false;
3738        u32 sta_rate_set = 0;
3739        int ret;
3740        struct ieee80211_sta *sta;
3741        bool sta_exists = false;
3742        struct ieee80211_sta_ht_cap sta_ht_cap;
3743
3744        if (is_ibss) {
3745                ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3746                                                     changed);
3747                if (ret < 0)
3748                        goto out;
3749        }
3750
3751        if (changed & BSS_CHANGED_IBSS) {
3752                if (bss_conf->ibss_joined) {
3753                        set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
3754                        ibss_joined = true;
3755                } else {
3756                        if (test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED,
3757                                               &wlvif->flags))
3758                                wl1271_unjoin(wl, wlvif);
3759                }
3760        }
3761
3762        if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
3763                do_join = true;
3764
3765        /* Need to update the SSID (for filtering etc) */
3766        if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
3767                do_join = true;
3768
3769        if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
3770                wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3771                             bss_conf->enable_beacon ? "enabled" : "disabled");
3772
3773                do_join = true;
3774        }
3775
3776        if (changed & BSS_CHANGED_IDLE && !is_ibss) {
3777                ret = wl1271_sta_handle_idle(wl, wlvif, bss_conf->idle);
3778                if (ret < 0)
3779                        wl1271_warning("idle mode change failed %d", ret);
3780        }
3781
3782        if ((changed & BSS_CHANGED_CQM)) {
3783                bool enable = false;
3784                if (bss_conf->cqm_rssi_thold)
3785                        enable = true;
3786                ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
3787                                                  bss_conf->cqm_rssi_thold,
3788                                                  bss_conf->cqm_rssi_hyst);
3789                if (ret < 0)
3790                        goto out;
3791                wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
3792        }
3793
3794        if (changed & BSS_CHANGED_BSSID &&
3795            (is_ibss || bss_conf->assoc))
3796                if (!is_zero_ether_addr(bss_conf->bssid)) {
3797                        ret = wl12xx_cmd_build_null_data(wl, wlvif);
3798                        if (ret < 0)
3799                                goto out;
3800
3801                        ret = wl1271_build_qos_null_data(wl, vif);
3802                        if (ret < 0)
3803                                goto out;
3804
3805                        /* Need to update the BSSID (for filtering etc) */
3806                        do_join = true;
3807                }
3808
3809        if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_HT)) {
3810                rcu_read_lock();
3811                sta = ieee80211_find_sta(vif, bss_conf->bssid);
3812                if (!sta)
3813                        goto sta_not_found;
3814
3815                /* save the supp_rates of the ap */
3816                sta_rate_set = sta->supp_rates[wl->hw->conf.channel->band];
3817                if (sta->ht_cap.ht_supported)
3818                        sta_rate_set |=
3819                            (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET);
3820                sta_ht_cap = sta->ht_cap;
3821                sta_exists = true;
3822
3823sta_not_found:
3824                rcu_read_unlock();
3825        }
3826
3827        if ((changed & BSS_CHANGED_ASSOC)) {
3828                if (bss_conf->assoc) {
3829                        u32 rates;
3830                        int ieoffset;
3831                        wlvif->aid = bss_conf->aid;
3832                        wlvif->beacon_int = bss_conf->beacon_int;
3833                        set_assoc = true;
3834
3835                        /*
3836                         * use basic rates from AP, and determine lowest rate
3837                         * to use with control frames.
3838                         */
3839                        rates = bss_conf->basic_rates;
3840                        wlvif->basic_rate_set =
3841                                wl1271_tx_enabled_rates_get(wl, rates,
3842                                                            wlvif->band);
3843                        wlvif->basic_rate =
3844                                wl1271_tx_min_rate_get(wl,
3845                                                       wlvif->basic_rate_set);
3846                        if (sta_rate_set)
3847                                wlvif->rate_set =
3848                                        wl1271_tx_enabled_rates_get(wl,
3849                                                                sta_rate_set,
3850                                                                wlvif->band);
3851                        ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3852                        if (ret < 0)
3853                                goto out;
3854
3855                        /*
3856                         * with wl1271, we don't need to update the
3857                         * beacon_int and dtim_period, because the firmware
3858                         * updates it by itself when the first beacon is
3859                         * received after a join.
3860                         */
3861                        ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
3862                        if (ret < 0)
3863                                goto out;
3864
3865                        /*
3866                         * Get a template for hardware connection maintenance
3867                         */
3868                        dev_kfree_skb(wlvif->probereq);
3869                        wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
3870                                                                        wlvif,
3871                                                                        NULL);
3872                        ieoffset = offsetof(struct ieee80211_mgmt,
3873                                            u.probe_req.variable);
3874                        wl1271_ssid_set(vif, wlvif->probereq, ieoffset);
3875
3876                        /* enable the connection monitoring feature */
3877                        ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
3878                        if (ret < 0)
3879                                goto out;
3880                } else {
3881                        /* use defaults when not associated */
3882                        bool was_assoc =
3883                            !!test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED,
3884                                                 &wlvif->flags);
3885                        bool was_ifup =
3886                            !!test_and_clear_bit(WLVIF_FLAG_STA_STATE_SENT,
3887                                                 &wlvif->flags);
3888                        wlvif->aid = 0;
3889
3890                        /* free probe-request template */
3891                        dev_kfree_skb(wlvif->probereq);
3892                        wlvif->probereq = NULL;
3893
3894                        /* revert back to minimum rates for the current band */
3895                        wl1271_set_band_rate(wl, wlvif);
3896                        wlvif->basic_rate =
3897                                wl1271_tx_min_rate_get(wl,
3898                                                       wlvif->basic_rate_set);
3899                        ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3900                        if (ret < 0)
3901                                goto out;
3902
3903                        /* disable connection monitor features */
3904                        ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
3905
3906                        /* Disable the keep-alive feature */
3907                        ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
3908                        if (ret < 0)
3909                                goto out;
3910
3911                        /* restore the bssid filter and go to dummy bssid */
3912                        if (was_assoc) {
3913                                /*
3914                                 * we might have to disable roc, if there was
3915                                 * no IF_OPER_UP notification.
3916                                 */
3917                                if (!was_ifup) {
3918                                        ret = wl12xx_croc(wl, wlvif->role_id);
3919                                        if (ret < 0)
3920                                                goto out;
3921                                }
3922                                /*
3923                                 * (we also need to disable roc in case of
3924                                 * roaming on the same channel. until we will
3925                                 * have a better flow...)
3926                                 */
3927                                if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
3928                                        ret = wl12xx_croc(wl,
3929                                                          wlvif->dev_role_id);
3930                                        if (ret < 0)
3931                                                goto out;
3932                                }
3933
3934                                wl1271_unjoin(wl, wlvif);
3935                                if (!bss_conf->idle)
3936                                        wl12xx_start_dev(wl, wlvif);
3937                        }
3938                }
3939        }
3940
3941        if (changed & BSS_CHANGED_IBSS) {
3942                wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
3943                             bss_conf->ibss_joined);
3944
3945                if (bss_conf->ibss_joined) {
3946                        u32 rates = bss_conf->basic_rates;
3947                        wlvif->basic_rate_set =
3948                                wl1271_tx_enabled_rates_get(wl, rates,
3949                                                            wlvif->band);
3950                        wlvif->basic_rate =
3951                                wl1271_tx_min_rate_get(wl,
3952                                                       wlvif->basic_rate_set);
3953
3954                        /* by default, use 11b + OFDM rates */
3955                        wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
3956                        ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3957                        if (ret < 0)
3958                                goto out;
3959                }
3960        }
3961
3962        ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
3963        if (ret < 0)
3964                goto out;
3965
3966        if (do_join) {
3967                ret = wl1271_join(wl, wlvif, set_assoc);
3968                if (ret < 0) {
3969                        wl1271_warning("cmd join failed %d", ret);
3970                        goto out;
3971                }
3972
3973                /* ROC until connected (after EAPOL exchange) */
3974                if (!is_ibss) {
3975                        ret = wl12xx_roc(wl, wlvif, wlvif->role_id);
3976                        if (ret < 0)
3977                                goto out;
3978
3979                        if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
3980                                wl12xx_set_authorized(wl, wlvif);
3981                }
3982                /*
3983                 * stop device role if started (we might already be in
3984                 * STA/IBSS role).
3985                 */
3986                if (wl12xx_dev_role_started(wlvif)) {
3987                        ret = wl12xx_stop_dev(wl, wlvif);
3988                        if (ret < 0)
3989                                goto out;
3990                }
3991        }
3992
3993        /* Handle new association with HT. Do this after join. */
3994        if (sta_exists) {
3995                if ((changed & BSS_CHANGED_HT) &&
3996                    (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
3997                        ret = wl1271_acx_set_ht_capabilities(wl,
3998                                                             &sta_ht_cap,
3999                                                             true,
4000                                                             wlvif->sta.hlid);
4001                        if (ret < 0) {
4002                                wl1271_warning("Set ht cap true failed %d",
4003                                               ret);
4004                                goto out;
4005                        }
4006                }
4007                /* handle new association without HT and disassociation */
4008                else if (changed & BSS_CHANGED_ASSOC) {
4009                        ret = wl1271_acx_set_ht_capabilities(wl,
4010                                                             &sta_ht_cap,
4011                                                             false,
4012                                                             wlvif->sta.hlid);
4013                        if (ret < 0) {
4014                                wl1271_warning("Set ht cap false failed %d",
4015                                               ret);
4016                                goto out;
4017                        }
4018                }
4019        }
4020
4021        /* Handle HT information change. Done after join. */
4022        if ((changed & BSS_CHANGED_HT) &&
4023            (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
4024                ret = wl1271_acx_set_ht_information(wl, wlvif,
4025                                        bss_conf->ht_operation_mode);
4026                if (ret < 0) {
4027                        wl1271_warning("Set ht information failed %d", ret);
4028                        goto out;
4029                }
4030        }
4031
4032        /* Handle arp filtering. Done after join. */
4033        if ((changed & BSS_CHANGED_ARP_FILTER) ||
4034            (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4035                __be32 addr = bss_conf->arp_addr_list[0];
4036                wlvif->sta.qos = bss_conf->qos;
4037                WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4038
4039                if (bss_conf->arp_addr_cnt == 1 &&
4040                    bss_conf->arp_filter_enabled) {
4041                        wlvif->ip_addr = addr;
4042                        /*
4043                         * The template should have been configured only upon
4044                         * association. however, it seems that the correct ip
4045                         * isn't being set (when sending), so we have to
4046                         * reconfigure the template upon every ip change.
4047                         */
4048                        ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4049                        if (ret < 0) {
4050                                wl1271_warning("build arp rsp failed: %d", ret);
4051                                goto out;
4052                        }
4053
4054                        ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4055                                (ACX_ARP_FILTER_ARP_FILTERING |
4056                                 ACX_ARP_FILTER_AUTO_ARP),
4057                                addr);
4058                } else {
4059                        wlvif->ip_addr = 0;
4060                        ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4061                }
4062
4063                if (ret < 0)
4064                        goto out;
4065        }
4066
4067out:
4068        return;
4069}
4070
4071static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4072                                       struct ieee80211_vif *vif,
4073                                       struct ieee80211_bss_conf *bss_conf,
4074                                       u32 changed)
4075{
4076        struct wl1271 *wl = hw->priv;
4077        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4078        bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4079        int ret;
4080
4081        wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed 0x%x",
4082                     (int)changed);
4083
4084        mutex_lock(&wl->mutex);
4085
4086        if (unlikely(wl->state == WL1271_STATE_OFF))
4087                goto out;
4088
4089        if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4090                goto out;
4091
4092        ret = wl1271_ps_elp_wakeup(wl);
4093        if (ret < 0)
4094                goto out;
4095
4096        if (is_ap)
4097                wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4098        else
4099                wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4100
4101        wl1271_ps_elp_sleep(wl);
4102
4103out:
4104        mutex_unlock(&wl->mutex);
4105}
4106
4107static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4108                             struct ieee80211_vif *vif, u16 queue,
4109                             const struct ieee80211_tx_queue_params *params)
4110{
4111        struct wl1271 *wl = hw->priv;
4112        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4113        u8 ps_scheme;
4114        int ret = 0;
4115
4116        mutex_lock(&wl->mutex);
4117
4118        wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4119
4120        if (params->uapsd)
4121                ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4122        else
4123                ps_scheme = CONF_PS_SCHEME_LEGACY;
4124
4125        if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4126                goto out;
4127
4128        ret = wl1271_ps_elp_wakeup(wl);
4129        if (ret < 0)
4130                goto out;
4131
4132        /*
4133         * the txop is confed in units of 32us by the mac80211,
4134         * we need us
4135         */
4136        ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4137                                params->cw_min, params->cw_max,
4138                                params->aifs, params->txop << 5);
4139        if (ret < 0)
4140                goto out_sleep;
4141
4142        ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4143                                 CONF_CHANNEL_TYPE_EDCF,
4144                                 wl1271_tx_get_queue(queue),
4145                                 ps_scheme, CONF_ACK_POLICY_LEGACY,
4146                                 0, 0);
4147
4148out_sleep:
4149        wl1271_ps_elp_sleep(wl);
4150
4151out:
4152        mutex_unlock(&wl->mutex);
4153
4154        return ret;
4155}
4156
4157static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4158                             struct ieee80211_vif *vif)
4159{
4160
4161        struct wl1271 *wl = hw->priv;
4162        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4163        u64 mactime = ULLONG_MAX;
4164        int ret;
4165
4166        wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4167
4168        mutex_lock(&wl->mutex);
4169
4170        if (unlikely(wl->state == WL1271_STATE_OFF))
4171                goto out;
4172
4173        ret = wl1271_ps_elp_wakeup(wl);
4174        if (ret < 0)
4175                goto out;
4176
4177        ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
4178        if (ret < 0)
4179                goto out_sleep;
4180
4181out_sleep:
4182        wl1271_ps_elp_sleep(wl);
4183
4184out:
4185        mutex_unlock(&wl->mutex);
4186        return mactime;
4187}
4188
4189static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4190                                struct survey_info *survey)
4191{
4192        struct wl1271 *wl = hw->priv;
4193        struct ieee80211_conf *conf = &hw->conf;
4194
4195        if (idx != 0)
4196                return -ENOENT;
4197
4198        survey->channel = conf->channel;
4199        survey->filled = SURVEY_INFO_NOISE_DBM;
4200        survey->noise = wl->noise;
4201
4202        return 0;
4203}
4204
4205static int wl1271_allocate_sta(struct wl1271 *wl,
4206                             struct wl12xx_vif *wlvif,
4207                             struct ieee80211_sta *sta)
4208{
4209        struct wl1271_station *wl_sta;
4210        int ret;
4211
4212
4213        if (wl->active_sta_count >= AP_MAX_STATIONS) {
4214                wl1271_warning("could not allocate HLID - too much stations");
4215                return -EBUSY;
4216        }
4217
4218        wl_sta = (struct wl1271_station *)sta->drv_priv;
4219        ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4220        if (ret < 0) {
4221                wl1271_warning("could not allocate HLID - too many links");
4222                return -EBUSY;
4223        }
4224
4225        set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
4226        memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
4227        wl->active_sta_count++;
4228        return 0;
4229}
4230
4231void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
4232{
4233        if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
4234                return;
4235
4236        clear_bit(hlid, wlvif->ap.sta_hlid_map);
4237        memset(wl->links[hlid].addr, 0, ETH_ALEN);
4238        wl->links[hlid].ba_bitmap = 0;
4239        __clear_bit(hlid, &wl->ap_ps_map);
4240        __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
4241        wl12xx_free_link(wl, wlvif, &hlid);
4242        wl->active_sta_count--;
4243
4244        /*
4245         * rearm the tx watchdog when the last STA is freed - give the FW a
4246         * chance to return STA-buffered packets before complaining.
4247         */
4248        if (wl->active_sta_count == 0)
4249                wl12xx_rearm_tx_watchdog_locked(wl);
4250}
4251
4252static int wl12xx_sta_add(struct wl1271 *wl,
4253                          struct wl12xx_vif *wlvif,
4254                          struct ieee80211_sta *sta)
4255{
4256        struct wl1271_station *wl_sta;
4257        int ret = 0;
4258        u8 hlid;
4259
4260        wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4261
4262        ret = wl1271_allocate_sta(wl, wlvif, sta);
4263        if (ret < 0)
4264                return ret;
4265
4266        wl_sta = (struct wl1271_station *)sta->drv_priv;
4267        hlid = wl_sta->hlid;
4268
4269        ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
4270        if (ret < 0)
4271                wl1271_free_sta(wl, wlvif, hlid);
4272
4273        return ret;
4274}
4275
4276static int wl12xx_sta_remove(struct wl1271 *wl,
4277                             struct wl12xx_vif *wlvif,
4278                             struct ieee80211_sta *sta)
4279{
4280        struct wl1271_station *wl_sta;
4281        int ret = 0, id;
4282
4283        wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4284
4285        wl_sta = (struct wl1271_station *)sta->drv_priv;
4286        id = wl_sta->hlid;
4287        if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4288                return -EINVAL;
4289
4290        ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
4291        if (ret < 0)
4292                return ret;
4293
4294        wl1271_free_sta(wl, wlvif, wl_sta->hlid);
4295        return ret;
4296}
4297
4298static int wl12xx_update_sta_state(struct wl1271 *wl,
4299                                   struct wl12xx_vif *wlvif,
4300                                   struct ieee80211_sta *sta,
4301                                   enum ieee80211_sta_state old_state,
4302                                   enum ieee80211_sta_state new_state)
4303{
4304        struct wl1271_station *wl_sta;
4305        u8 hlid;
4306        bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4307        bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4308        int ret;
4309
4310        wl_sta = (struct wl1271_station *)sta->drv_priv;
4311        hlid = wl_sta->hlid;
4312
4313        /* Add station (AP mode) */
4314        if (is_ap &&
4315            old_state == IEEE80211_STA_NOTEXIST &&
4316            new_state == IEEE80211_STA_NONE)
4317                return wl12xx_sta_add(wl, wlvif, sta);
4318
4319        /* Remove station (AP mode) */
4320        if (is_ap &&
4321            old_state == IEEE80211_STA_NONE &&
4322            new_state == IEEE80211_STA_NOTEXIST) {
4323                /* must not fail */
4324                wl12xx_sta_remove(wl, wlvif, sta);
4325                return 0;
4326        }
4327
4328        /* Authorize station (AP mode) */
4329        if (is_ap &&
4330            new_state == IEEE80211_STA_AUTHORIZED) {
4331                ret = wl12xx_cmd_set_peer_state(wl, hlid);
4332                if (ret < 0)
4333                        return ret;
4334
4335                ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4336                                                     hlid);
4337                return ret;
4338        }
4339
4340        /* Authorize station */
4341        if (is_sta &&
4342            new_state == IEEE80211_STA_AUTHORIZED) {
4343                set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4344                return wl12xx_set_authorized(wl, wlvif);
4345        }
4346
4347        if (is_sta &&
4348            old_state == IEEE80211_STA_AUTHORIZED &&
4349            new_state == IEEE80211_STA_ASSOC) {
4350                clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
4351                return 0;
4352        }
4353
4354        return 0;
4355}
4356
4357static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4358                               struct ieee80211_vif *vif,
4359                               struct ieee80211_sta *sta,
4360                               enum ieee80211_sta_state old_state,
4361                               enum ieee80211_sta_state new_state)
4362{
4363        struct wl1271 *wl = hw->priv;
4364        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4365        int ret;
4366
4367        wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4368                     sta->aid, old_state, new_state);
4369
4370        mutex_lock(&wl->mutex);
4371
4372        if (unlikely(wl->state == WL1271_STATE_OFF)) {
4373                ret = -EBUSY;
4374                goto out;
4375        }
4376
4377        ret = wl1271_ps_elp_wakeup(wl);
4378        if (ret < 0)
4379                goto out;
4380
4381        ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
4382
4383        wl1271_ps_elp_sleep(wl);
4384out:
4385        mutex_unlock(&wl->mutex);
4386        if (new_state < old_state)
4387                return 0;
4388        return ret;
4389}
4390
4391static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4392                                  struct ieee80211_vif *vif,
4393                                  enum ieee80211_ampdu_mlme_action action,
4394                                  struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4395                                  u8 buf_size)
4396{
4397        struct wl1271 *wl = hw->priv;
4398        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4399        int ret;
4400        u8 hlid, *ba_bitmap;
4401
4402        wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4403                     tid);
4404
4405        /* sanity check - the fields in FW are only 8bits wide */
4406        if (WARN_ON(tid > 0xFF))
4407                return -ENOTSUPP;
4408
4409        mutex_lock(&wl->mutex);
4410
4411        if (unlikely(wl->state == WL1271_STATE_OFF)) {
4412                ret = -EAGAIN;
4413                goto out;
4414        }
4415
4416        if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
4417                hlid = wlvif->sta.hlid;
4418                ba_bitmap = &wlvif->sta.ba_rx_bitmap;
4419        } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
4420                struct wl1271_station *wl_sta;
4421
4422                wl_sta = (struct wl1271_station *)sta->drv_priv;
4423                hlid = wl_sta->hlid;
4424                ba_bitmap = &wl->links[hlid].ba_bitmap;
4425        } else {
4426                ret = -EINVAL;
4427                goto out;
4428        }
4429
4430        ret = wl1271_ps_elp_wakeup(wl);
4431        if (ret < 0)
4432                goto out;
4433
4434        wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4435                     tid, action);
4436
4437        switch (action) {
4438        case IEEE80211_AMPDU_RX_START:
4439                if (!wlvif->ba_support || !wlvif->ba_allowed) {
4440                        ret = -ENOTSUPP;
4441                        break;
4442                }
4443
4444                if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4445                        ret = -EBUSY;
4446                        wl1271_error("exceeded max RX BA sessions");
4447                        break;
4448                }
4449
4450                if (*ba_bitmap & BIT(tid)) {
4451                        ret = -EINVAL;
4452                        wl1271_error("cannot enable RX BA session on active "
4453                                     "tid: %d", tid);
4454                        break;
4455                }
4456
4457                ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4458                                                         hlid);
4459                if (!ret) {
4460                        *ba_bitmap |= BIT(tid);
4461                        wl->ba_rx_session_count++;
4462                }
4463                break;
4464
4465        case IEEE80211_AMPDU_RX_STOP:
4466                if (!(*ba_bitmap & BIT(tid))) {
4467                        ret = -EINVAL;
4468                        wl1271_error("no active RX BA session on tid: %d",
4469                                     tid);
4470                        break;
4471                }
4472
4473                ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4474                                                         hlid);
4475                if (!ret) {
4476                        *ba_bitmap &= ~BIT(tid);
4477                        wl->ba_rx_session_count--;
4478                }
4479                break;
4480
4481        /*
4482         * The BA initiator session management in FW independently.
4483         * Falling break here on purpose for all TX APDU commands.
4484         */
4485        case IEEE80211_AMPDU_TX_START:
4486        case IEEE80211_AMPDU_TX_STOP:
4487        case IEEE80211_AMPDU_TX_OPERATIONAL:
4488                ret = -EINVAL;
4489                break;
4490
4491        default:
4492                wl1271_error("Incorrect ampdu action id=%x\n", action);
4493                ret = -EINVAL;
4494        }
4495
4496        wl1271_ps_elp_sleep(wl);
4497
4498out:
4499        mutex_unlock(&wl->mutex);
4500
4501        return ret;
4502}
4503
4504static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4505                                   struct ieee80211_vif *vif,
4506                                   const struct cfg80211_bitrate_mask *mask)
4507{
4508        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4509        struct wl1271 *wl = hw->priv;
4510        int i, ret = 0;
4511
4512        wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4513                mask->control[NL80211_BAND_2GHZ].legacy,
4514                mask->control[NL80211_BAND_5GHZ].legacy);
4515
4516        mutex_lock(&wl->mutex);
4517
4518        for (i = 0; i < IEEE80211_NUM_BANDS; i++)
4519                wlvif->bitrate_masks[i] =
4520                        wl1271_tx_enabled_rates_get(wl,
4521                                                    mask->control[i].legacy,
4522                                                    i);
4523
4524        if (unlikely(wl->state == WL1271_STATE_OFF))
4525                goto out;
4526
4527        if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4528            !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4529
4530                ret = wl1271_ps_elp_wakeup(wl);
4531                if (ret < 0)
4532                        goto out;
4533
4534                wl1271_set_band_rate(wl, wlvif);
4535                wlvif->basic_rate =
4536                        wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4537                ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4538
4539                wl1271_ps_elp_sleep(wl);
4540        }
4541out:
4542        mutex_unlock(&wl->mutex);
4543
4544        return ret;
4545}
4546
4547static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4548                                     struct ieee80211_channel_switch *ch_switch)
4549{
4550        struct wl1271 *wl = hw->priv;
4551        struct wl12xx_vif *wlvif;
4552        int ret;
4553
4554        wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4555
4556        wl1271_tx_flush(wl);
4557
4558        mutex_lock(&wl->mutex);
4559
4560        if (unlikely(wl->state == WL1271_STATE_OFF)) {
4561                wl12xx_for_each_wlvif_sta(wl, wlvif) {
4562                        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4563                        ieee80211_chswitch_done(vif, false);
4564                }
4565                goto out;
4566        }
4567
4568        ret = wl1271_ps_elp_wakeup(wl);
4569        if (ret < 0)
4570                goto out;
4571
4572        /* TODO: change mac80211 to pass vif as param */
4573        wl12xx_for_each_wlvif_sta(wl, wlvif) {
4574                ret = wl12xx_cmd_channel_switch(wl, wlvif, ch_switch);
4575
4576                if (!ret)
4577                        set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4578        }
4579
4580        wl1271_ps_elp_sleep(wl);
4581
4582out:
4583        mutex_unlock(&wl->mutex);
4584}
4585
4586static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
4587{
4588        struct wl1271 *wl = hw->priv;
4589        bool ret = false;
4590
4591        mutex_lock(&wl->mutex);
4592
4593        if (unlikely(wl->state == WL1271_STATE_OFF))
4594                goto out;
4595
4596        /* packets are considered pending if in the TX queue or the FW */
4597        ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
4598out:
4599        mutex_unlock(&wl->mutex);
4600
4601        return ret;
4602}
4603
4604/* can't be const, mac80211 writes to this */
4605static struct ieee80211_rate wl1271_rates[] = {
4606        { .bitrate = 10,
4607          .hw_value = CONF_HW_BIT_RATE_1MBPS,
4608          .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
4609        { .bitrate = 20,
4610          .hw_value = CONF_HW_BIT_RATE_2MBPS,
4611          .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
4612          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4613        { .bitrate = 55,
4614          .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
4615          .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
4616          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4617        { .bitrate = 110,
4618          .hw_value = CONF_HW_BIT_RATE_11MBPS,
4619          .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
4620          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4621        { .bitrate = 60,
4622          .hw_value = CONF_HW_BIT_RATE_6MBPS,
4623          .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4624        { .bitrate = 90,
4625          .hw_value = CONF_HW_BIT_RATE_9MBPS,
4626          .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4627        { .bitrate = 120,
4628          .hw_value = CONF_HW_BIT_RATE_12MBPS,
4629          .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4630        { .bitrate = 180,
4631          .hw_value = CONF_HW_BIT_RATE_18MBPS,
4632          .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4633        { .bitrate = 240,
4634          .hw_value = CONF_HW_BIT_RATE_24MBPS,
4635          .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4636        { .bitrate = 360,
4637         .hw_value = CONF_HW_BIT_RATE_36MBPS,
4638         .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4639        { .bitrate = 480,
4640          .hw_value = CONF_HW_BIT_RATE_48MBPS,
4641          .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4642        { .bitrate = 540,
4643          .hw_value = CONF_HW_BIT_RATE_54MBPS,
4644          .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4645};
4646
4647/* can't be const, mac80211 writes to this */
4648static struct ieee80211_channel wl1271_channels[] = {
4649        { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
4650        { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
4651        { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
4652        { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
4653        { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
4654        { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
4655        { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
4656        { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
4657        { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
4658        { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
4659        { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
4660        { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
4661        { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
4662        { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
4663};
4664
4665/* mapping to indexes for wl1271_rates */
4666static const u8 wl1271_rate_to_idx_2ghz[] = {
4667        /* MCS rates are used only with 11n */
4668        7,                            /* CONF_HW_RXTX_RATE_MCS7_SGI */
4669        7,                            /* CONF_HW_RXTX_RATE_MCS7 */
4670        6,                            /* CONF_HW_RXTX_RATE_MCS6 */
4671        5,                            /* CONF_HW_RXTX_RATE_MCS5 */
4672        4,                            /* CONF_HW_RXTX_RATE_MCS4 */
4673        3,                            /* CONF_HW_RXTX_RATE_MCS3 */
4674        2,                            /* CONF_HW_RXTX_RATE_MCS2 */
4675        1,                            /* CONF_HW_RXTX_RATE_MCS1 */
4676        0,                            /* CONF_HW_RXTX_RATE_MCS0 */
4677
4678        11,                            /* CONF_HW_RXTX_RATE_54   */
4679        10,                            /* CONF_HW_RXTX_RATE_48   */
4680        9,                             /* CONF_HW_RXTX_RATE_36   */
4681        8,                             /* CONF_HW_RXTX_RATE_24   */
4682
4683        /* TI-specific rate */
4684        CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22   */
4685
4686        7,                             /* CONF_HW_RXTX_RATE_18   */
4687        6,                             /* CONF_HW_RXTX_RATE_12   */
4688        3,                             /* CONF_HW_RXTX_RATE_11   */
4689        5,                             /* CONF_HW_RXTX_RATE_9    */
4690        4,                             /* CONF_HW_RXTX_RATE_6    */
4691        2,                             /* CONF_HW_RXTX_RATE_5_5  */
4692        1,                             /* CONF_HW_RXTX_RATE_2    */
4693        0                              /* CONF_HW_RXTX_RATE_1    */
4694};
4695
4696/* 11n STA capabilities */
4697#define HW_RX_HIGHEST_RATE      72
4698
4699#define WL12XX_HT_CAP { \
4700        .cap = IEEE80211_HT_CAP_GRN_FLD | IEEE80211_HT_CAP_SGI_20 | \
4701               (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT), \
4702        .ht_supported = true, \
4703        .ampdu_factor = IEEE80211_HT_MAX_AMPDU_8K, \
4704        .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
4705        .mcs = { \
4706                .rx_mask = { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, }, \
4707                .rx_highest = cpu_to_le16(HW_RX_HIGHEST_RATE), \
4708                .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
4709                }, \
4710}
4711
4712/* can't be const, mac80211 writes to this */
4713static struct ieee80211_supported_band wl1271_band_2ghz = {
4714        .channels = wl1271_channels,
4715        .n_channels = ARRAY_SIZE(wl1271_channels),
4716        .bitrates = wl1271_rates,
4717        .n_bitrates = ARRAY_SIZE(wl1271_rates),
4718        .ht_cap = WL12XX_HT_CAP,
4719};
4720
4721/* 5 GHz data rates for WL1273 */
4722static struct ieee80211_rate wl1271_rates_5ghz[] = {
4723        { .bitrate = 60,
4724          .hw_value = CONF_HW_BIT_RATE_6MBPS,
4725          .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4726        { .bitrate = 90,
4727          .hw_value = CONF_HW_BIT_RATE_9MBPS,
4728          .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4729        { .bitrate = 120,
4730          .hw_value = CONF_HW_BIT_RATE_12MBPS,
4731          .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4732        { .bitrate = 180,
4733          .hw_value = CONF_HW_BIT_RATE_18MBPS,
4734          .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4735        { .bitrate = 240,
4736          .hw_value = CONF_HW_BIT_RATE_24MBPS,
4737          .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4738        { .bitrate = 360,
4739         .hw_value = CONF_HW_BIT_RATE_36MBPS,
4740         .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4741        { .bitrate = 480,
4742          .hw_value = CONF_HW_BIT_RATE_48MBPS,
4743          .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4744        { .bitrate = 540,
4745          .hw_value = CONF_HW_BIT_RATE_54MBPS,
4746          .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4747};
4748
4749/* 5 GHz band channels for WL1273 */
4750static struct ieee80211_channel wl1271_channels_5ghz[] = {
4751        { .hw_value = 7, .center_freq = 5035, .max_power = 25 },
4752        { .hw_value = 8, .center_freq = 5040, .max_power = 25 },
4753        { .hw_value = 9, .center_freq = 5045, .max_power = 25 },
4754        { .hw_value = 11, .center_freq = 5055, .max_power = 25 },
4755        { .hw_value = 12, .center_freq = 5060, .max_power = 25 },
4756        { .hw_value = 16, .center_freq = 5080, .max_power = 25 },
4757        { .hw_value = 34, .center_freq = 5170, .max_power = 25 },
4758        { .hw_value = 36, .center_freq = 5180, .max_power = 25 },
4759        { .hw_value = 38, .center_freq = 5190, .max_power = 25 },
4760        { .hw_value = 40, .center_freq = 5200, .max_power = 25 },
4761        { .hw_value = 42, .center_freq = 5210, .max_power = 25 },
4762        { .hw_value = 44, .center_freq = 5220, .max_power = 25 },
4763        { .hw_value = 46, .center_freq = 5230, .max_power = 25 },
4764        { .hw_value = 48, .center_freq = 5240, .max_power = 25 },
4765        { .hw_value = 52, .center_freq = 5260, .max_power = 25 },
4766        { .hw_value = 56, .center_freq = 5280, .max_power = 25 },
4767        { .hw_value = 60, .center_freq = 5300, .max_power = 25 },
4768        { .hw_value = 64, .center_freq = 5320, .max_power = 25 },
4769        { .hw_value = 100, .center_freq = 5500, .max_power = 25 },
4770        { .hw_value = 104, .center_freq = 5520, .max_power = 25 },
4771        { .hw_value = 108, .center_freq = 5540, .max_power = 25 },
4772        { .hw_value = 112, .center_freq = 5560, .max_power = 25 },
4773        { .hw_value = 116, .center_freq = 5580, .max_power = 25 },
4774        { .hw_value = 120, .center_freq = 5600, .max_power = 25 },
4775        { .hw_value = 124, .center_freq = 5620, .max_power = 25 },
4776        { .hw_value = 128, .center_freq = 5640, .max_power = 25 },
4777        { .hw_value = 132, .center_freq = 5660, .max_power = 25 },
4778        { .hw_value = 136, .center_freq = 5680, .max_power = 25 },
4779        { .hw_value = 140, .center_freq = 5700, .max_power = 25 },
4780        { .hw_value = 149, .center_freq = 5745, .max_power = 25 },
4781        { .hw_value = 153, .center_freq = 5765, .max_power = 25 },
4782        { .hw_value = 157, .center_freq = 5785, .max_power = 25 },
4783        { .hw_value = 161, .center_freq = 5805, .max_power = 25 },
4784        { .hw_value = 165, .center_freq = 5825, .max_power = 25 },
4785};
4786
4787/* mapping to indexes for wl1271_rates_5ghz */
4788static const u8 wl1271_rate_to_idx_5ghz[] = {
4789        /* MCS rates are used only with 11n */
4790        7,                            /* CONF_HW_RXTX_RATE_MCS7_SGI */
4791        7,                            /* CONF_HW_RXTX_RATE_MCS7 */
4792        6,                            /* CONF_HW_RXTX_RATE_MCS6 */
4793        5,                            /* CONF_HW_RXTX_RATE_MCS5 */
4794        4,                            /* CONF_HW_RXTX_RATE_MCS4 */
4795        3,                            /* CONF_HW_RXTX_RATE_MCS3 */
4796        2,                            /* CONF_HW_RXTX_RATE_MCS2 */
4797        1,                            /* CONF_HW_RXTX_RATE_MCS1 */
4798        0,                            /* CONF_HW_RXTX_RATE_MCS0 */
4799
4800        7,                             /* CONF_HW_RXTX_RATE_54   */
4801        6,                             /* CONF_HW_RXTX_RATE_48   */
4802        5,                             /* CONF_HW_RXTX_RATE_36   */
4803        4,                             /* CONF_HW_RXTX_RATE_24   */
4804
4805        /* TI-specific rate */
4806        CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_22   */
4807
4808        3,                             /* CONF_HW_RXTX_RATE_18   */
4809        2,                             /* CONF_HW_RXTX_RATE_12   */
4810        CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_11   */
4811        1,                             /* CONF_HW_RXTX_RATE_9    */
4812        0,                             /* CONF_HW_RXTX_RATE_6    */
4813        CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_5_5  */
4814        CONF_HW_RXTX_RATE_UNSUPPORTED, /* CONF_HW_RXTX_RATE_2    */
4815        CONF_HW_RXTX_RATE_UNSUPPORTED  /* CONF_HW_RXTX_RATE_1    */
4816};
4817
4818static struct ieee80211_supported_band wl1271_band_5ghz = {
4819        .channels = wl1271_channels_5ghz,
4820        .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
4821        .bitrates = wl1271_rates_5ghz,
4822        .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
4823        .ht_cap = WL12XX_HT_CAP,
4824};
4825
4826static const u8 *wl1271_band_rate_to_idx[] = {
4827        [IEEE80211_BAND_2GHZ] = wl1271_rate_to_idx_2ghz,
4828        [IEEE80211_BAND_5GHZ] = wl1271_rate_to_idx_5ghz
4829};
4830
4831static const struct ieee80211_ops wl1271_ops = {
4832        .start = wl1271_op_start,
4833        .stop = wl1271_op_stop,
4834        .add_interface = wl1271_op_add_interface,
4835        .remove_interface = wl1271_op_remove_interface,
4836        .change_interface = wl12xx_op_change_interface,
4837#ifdef CONFIG_PM
4838        .suspend = wl1271_op_suspend,
4839        .resume = wl1271_op_resume,
4840#endif
4841        .config = wl1271_op_config,
4842        .prepare_multicast = wl1271_op_prepare_multicast,
4843        .configure_filter = wl1271_op_configure_filter,
4844        .tx = wl1271_op_tx,
4845        .set_key = wl1271_op_set_key,
4846        .hw_scan = wl1271_op_hw_scan,
4847        .cancel_hw_scan = wl1271_op_cancel_hw_scan,
4848        .sched_scan_start = wl1271_op_sched_scan_start,
4849        .sched_scan_stop = wl1271_op_sched_scan_stop,
4850        .bss_info_changed = wl1271_op_bss_info_changed,
4851        .set_frag_threshold = wl1271_op_set_frag_threshold,
4852        .set_rts_threshold = wl1271_op_set_rts_threshold,
4853        .conf_tx = wl1271_op_conf_tx,
4854        .get_tsf = wl1271_op_get_tsf,
4855        .get_survey = wl1271_op_get_survey,
4856        .sta_state = wl12xx_op_sta_state,
4857        .ampdu_action = wl1271_op_ampdu_action,
4858        .tx_frames_pending = wl1271_tx_frames_pending,
4859        .set_bitrate_mask = wl12xx_set_bitrate_mask,
4860        .channel_switch = wl12xx_op_channel_switch,
4861        CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
4862};
4863
4864
4865u8 wl1271_rate_to_idx(int rate, enum ieee80211_band band)
4866{
4867        u8 idx;
4868
4869        BUG_ON(band >= sizeof(wl1271_band_rate_to_idx)/sizeof(u8 *));
4870
4871        if (unlikely(rate >= CONF_HW_RXTX_RATE_MAX)) {
4872                wl1271_error("Illegal RX rate from HW: %d", rate);
4873                return 0;
4874        }
4875
4876        idx = wl1271_band_rate_to_idx[band][rate];
4877        if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
4878                wl1271_error("Unsupported RX rate from HW: %d", rate);
4879                return 0;
4880        }
4881
4882        return idx;
4883}
4884
4885static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
4886                                               struct device_attribute *attr,
4887                                               char *buf)
4888{
4889        struct wl1271 *wl = dev_get_drvdata(dev);
4890        ssize_t len;
4891
4892        len = PAGE_SIZE;
4893
4894        mutex_lock(&wl->mutex);
4895        len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
4896                       wl->sg_enabled);
4897        mutex_unlock(&wl->mutex);
4898
4899        return len;
4900
4901}
4902
4903static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
4904                                                struct device_attribute *attr,
4905                                                const char *buf, size_t count)
4906{
4907        struct wl1271 *wl = dev_get_drvdata(dev);
4908        unsigned long res;
4909        int ret;
4910
4911        ret = kstrtoul(buf, 10, &res);
4912        if (ret < 0) {
4913                wl1271_warning("incorrect value written to bt_coex_mode");
4914                return count;
4915        }
4916
4917        mutex_lock(&wl->mutex);
4918
4919        res = !!res;
4920
4921        if (res == wl->sg_enabled)
4922                goto out;
4923
4924        wl->sg_enabled = res;
4925
4926        if (wl->state == WL1271_STATE_OFF)
4927                goto out;
4928
4929        ret = wl1271_ps_elp_wakeup(wl);
4930        if (ret < 0)
4931                goto out;
4932
4933        wl1271_acx_sg_enable(wl, wl->sg_enabled);
4934        wl1271_ps_elp_sleep(wl);
4935
4936 out:
4937        mutex_unlock(&wl->mutex);
4938        return count;
4939}
4940
4941static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
4942                   wl1271_sysfs_show_bt_coex_state,
4943                   wl1271_sysfs_store_bt_coex_state);
4944
4945static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
4946                                           struct device_attribute *attr,
4947                                           char *buf)
4948{
4949        struct wl1271 *wl = dev_get_drvdata(dev);
4950        ssize_t len;
4951
4952        len = PAGE_SIZE;
4953
4954        mutex_lock(&wl->mutex);
4955        if (wl->hw_pg_ver >= 0)
4956                len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
4957        else
4958                len = snprintf(buf, len, "n/a\n");
4959        mutex_unlock(&wl->mutex);
4960
4961        return len;
4962}
4963
4964static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
4965                   wl1271_sysfs_show_hw_pg_ver, NULL);
4966
4967static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
4968                                       struct bin_attribute *bin_attr,
4969                                       char *buffer, loff_t pos, size_t count)
4970{
4971        struct device *dev = container_of(kobj, struct device, kobj);
4972        struct wl1271 *wl = dev_get_drvdata(dev);
4973        ssize_t len;
4974        int ret;
4975
4976        ret = mutex_lock_interruptible(&wl->mutex);
4977        if (ret < 0)
4978                return -ERESTARTSYS;
4979
4980        /* Let only one thread read the log at a time, blocking others */
4981        while (wl->fwlog_size == 0) {
4982                DEFINE_WAIT(wait);
4983
4984                prepare_to_wait_exclusive(&wl->fwlog_waitq,
4985                                          &wait,
4986                                          TASK_INTERRUPTIBLE);
4987
4988                if (wl->fwlog_size != 0) {
4989                        finish_wait(&wl->fwlog_waitq, &wait);
4990                        break;
4991                }
4992
4993                mutex_unlock(&wl->mutex);
4994
4995                schedule();
4996                finish_wait(&wl->fwlog_waitq, &wait);
4997
4998                if (signal_pending(current))
4999                        return -ERESTARTSYS;
5000
5001                ret = mutex_lock_interruptible(&wl->mutex);
5002                if (ret < 0)
5003                        return -ERESTARTSYS;
5004        }
5005
5006        /* Check if the fwlog is still valid */
5007        if (wl->fwlog_size < 0) {
5008                mutex_unlock(&wl->mutex);
5009                return 0;
5010        }
5011
5012        /* Seeking is not supported - old logs are not kept. Disregard pos. */
5013        len = min(count, (size_t)wl->fwlog_size);
5014        wl->fwlog_size -= len;
5015        memcpy(buffer, wl->fwlog, len);
5016
5017        /* Make room for new messages */
5018        memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
5019
5020        mutex_unlock(&wl->mutex);
5021
5022        return len;
5023}
5024
5025static struct bin_attribute fwlog_attr = {
5026        .attr = {.name = "fwlog", .mode = S_IRUSR},
5027        .read = wl1271_sysfs_read_fwlog,
5028};
5029
5030static bool wl12xx_mac_in_fuse(struct wl1271 *wl)
5031{
5032        bool supported = false;
5033        u8 major, minor;
5034
5035        if (wl->chip.id == CHIP_ID_1283_PG20) {
5036                major = WL128X_PG_GET_MAJOR(wl->hw_pg_ver);
5037                minor = WL128X_PG_GET_MINOR(wl->hw_pg_ver);
5038
5039                /* in wl128x we have the MAC address if the PG is >= (2, 1) */
5040                if (major > 2 || (major == 2 && minor >= 1))
5041                        supported = true;
5042        } else {
5043                major = WL127X_PG_GET_MAJOR(wl->hw_pg_ver);
5044                minor = WL127X_PG_GET_MINOR(wl->hw_pg_ver);
5045
5046                /* in wl127x we have the MAC address if the PG is >= (3, 1) */
5047                if (major == 3 && minor >= 1)
5048                        supported = true;
5049        }
5050
5051        wl1271_debug(DEBUG_PROBE,
5052                     "PG Ver major = %d minor = %d, MAC %s present",
5053                     major, minor, supported ? "is" : "is not");
5054
5055        return supported;
5056}
5057
5058static void wl12xx_derive_mac_addresses(struct wl1271 *wl,
5059                                        u32 oui, u32 nic, int n)
5060{
5061        int i;
5062
5063        wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x, n %d",
5064                     oui, nic, n);
5065
5066        if (nic + n - 1 > 0xffffff)
5067                wl1271_warning("NIC part of the MAC address wraps around!");
5068
5069        for (i = 0; i < n; i++) {
5070                wl->addresses[i].addr[0] = (u8)(oui >> 16);
5071                wl->addresses[i].addr[1] = (u8)(oui >> 8);
5072                wl->addresses[i].addr[2] = (u8) oui;
5073                wl->addresses[i].addr[3] = (u8)(nic >> 16);
5074                wl->addresses[i].addr[4] = (u8)(nic >> 8);
5075                wl->addresses[i].addr[5] = (u8) nic;
5076                nic++;
5077        }
5078
5079        wl->hw->wiphy->n_addresses = n;
5080        wl->hw->wiphy->addresses = wl->addresses;
5081}
5082
5083static void wl12xx_get_fuse_mac(struct wl1271 *wl)
5084{
5085        u32 mac1, mac2;
5086
5087        wl1271_set_partition(wl, &wl12xx_part_table[PART_DRPW]);
5088
5089        mac1 = wl1271_read32(wl, WL12XX_REG_FUSE_BD_ADDR_1);
5090        mac2 = wl1271_read32(wl, WL12XX_REG_FUSE_BD_ADDR_2);
5091
5092        /* these are the two parts of the BD_ADDR */
5093        wl->fuse_oui_addr = ((mac2 & 0xffff) << 8) +
5094                ((mac1 & 0xff000000) >> 24);
5095        wl->fuse_nic_addr = mac1 & 0xffffff;
5096
5097        wl1271_set_partition(wl, &wl12xx_part_table[PART_DOWN]);
5098}
5099
5100static int wl12xx_get_hw_info(struct wl1271 *wl)
5101{
5102        int ret;
5103        u32 die_info;
5104
5105        ret = wl12xx_set_power_on(wl);
5106        if (ret < 0)
5107                goto out;
5108
5109        wl->chip.id = wl1271_read32(wl, CHIP_ID_B);
5110
5111        if (wl->chip.id == CHIP_ID_1283_PG20)
5112                die_info = wl1271_top_reg_read(wl, WL128X_REG_FUSE_DATA_2_1);
5113        else
5114                die_info = wl1271_top_reg_read(wl, WL127X_REG_FUSE_DATA_2_1);
5115
5116        wl->hw_pg_ver = (s8) (die_info & PG_VER_MASK) >> PG_VER_OFFSET;
5117
5118        if (!wl12xx_mac_in_fuse(wl)) {
5119                wl->fuse_oui_addr = 0;
5120                wl->fuse_nic_addr = 0;
5121        } else {
5122                wl12xx_get_fuse_mac(wl);
5123        }
5124
5125        wl1271_power_off(wl);
5126out:
5127        return ret;
5128}
5129
5130static int wl1271_register_hw(struct wl1271 *wl)
5131{
5132        int ret;
5133        u32 oui_addr = 0, nic_addr = 0;
5134
5135        if (wl->mac80211_registered)
5136                return 0;
5137
5138        ret = wl12xx_get_hw_info(wl);
5139        if (ret < 0) {
5140                wl1271_error("couldn't get hw info");
5141                goto out;
5142        }
5143
5144        ret = wl1271_fetch_nvs(wl);
5145        if (ret == 0) {
5146                /* NOTE: The wl->nvs->nvs element must be first, in
5147                 * order to simplify the casting, we assume it is at
5148                 * the beginning of the wl->nvs structure.
5149                 */
5150                u8 *nvs_ptr = (u8 *)wl->nvs;
5151
5152                oui_addr =
5153                        (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
5154                nic_addr =
5155                        (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
5156        }
5157
5158        /* if the MAC address is zeroed in the NVS derive from fuse */
5159        if (oui_addr == 0 && nic_addr == 0) {
5160                oui_addr = wl->fuse_oui_addr;
5161                /* fuse has the BD_ADDR, the WLAN addresses are the next two */
5162                nic_addr = wl->fuse_nic_addr + 1;
5163        }
5164
5165        wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr, 2);
5166
5167        ret = ieee80211_register_hw(wl->hw);
5168        if (ret < 0) {
5169                wl1271_error("unable to register mac80211 hw: %d", ret);
5170                goto out;
5171        }
5172
5173        wl->mac80211_registered = true;
5174
5175        wl1271_debugfs_init(wl);
5176
5177        wl1271_notice("loaded");
5178
5179out:
5180        return ret;
5181}
5182
5183static void wl1271_unregister_hw(struct wl1271 *wl)
5184{
5185        if (wl->plt)
5186                wl1271_plt_stop(wl);
5187
5188        ieee80211_unregister_hw(wl->hw);
5189        wl->mac80211_registered = false;
5190
5191}
5192
5193static int wl1271_init_ieee80211(struct wl1271 *wl)
5194{
5195        static const u32 cipher_suites[] = {
5196                WLAN_CIPHER_SUITE_WEP40,
5197                WLAN_CIPHER_SUITE_WEP104,
5198                WLAN_CIPHER_SUITE_TKIP,
5199                WLAN_CIPHER_SUITE_CCMP,
5200                WL1271_CIPHER_SUITE_GEM,
5201        };
5202
5203        /* The tx descriptor buffer and the TKIP space. */
5204        wl->hw->extra_tx_headroom = WL1271_EXTRA_SPACE_TKIP +
5205                sizeof(struct wl1271_tx_hw_descr);
5206
5207        /* unit us */
5208        /* FIXME: find a proper value */
5209        wl->hw->channel_change_time = 10000;
5210        wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
5211
5212        wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5213                IEEE80211_HW_SUPPORTS_PS |
5214                IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5215                IEEE80211_HW_SUPPORTS_UAPSD |
5216                IEEE80211_HW_HAS_RATE_CONTROL |
5217                IEEE80211_HW_CONNECTION_MONITOR |
5218                IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5219                IEEE80211_HW_SPECTRUM_MGMT |
5220                IEEE80211_HW_AP_LINK_PS |
5221                IEEE80211_HW_AMPDU_AGGREGATION |
5222                IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5223                IEEE80211_HW_SCAN_WHILE_IDLE;
5224
5225        wl->hw->wiphy->cipher_suites = cipher_suites;
5226        wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5227
5228        wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
5229                BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5230                BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
5231        wl->hw->wiphy->max_scan_ssids = 1;
5232        wl->hw->wiphy->max_sched_scan_ssids = 16;
5233        wl->hw->wiphy->max_match_sets = 16;
5234        /*
5235         * Maximum length of elements in scanning probe request templates
5236         * should be the maximum length possible for a template, without
5237         * the IEEE80211 header of the template
5238         */
5239        wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5240                        sizeof(struct ieee80211_header);
5241
5242        wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
5243                sizeof(struct ieee80211_header);
5244
5245        wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5246
5247        /* make sure all our channels fit in the scanned_ch bitmask */
5248        BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5249                     ARRAY_SIZE(wl1271_channels_5ghz) >
5250                     WL1271_MAX_CHANNELS);
5251        /*
5252         * We keep local copies of the band structs because we need to
5253         * modify them on a per-device basis.
5254         */
5255        memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5256               sizeof(wl1271_band_2ghz));
5257        memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5258               sizeof(wl1271_band_5ghz));
5259
5260        wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5261                &wl->bands[IEEE80211_BAND_2GHZ];
5262        wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5263                &wl->bands[IEEE80211_BAND_5GHZ];
5264
5265        wl->hw->queues = 4;
5266        wl->hw->max_rates = 1;
5267
5268        wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5269
5270        /* the FW answers probe-requests in AP-mode */
5271        wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5272        wl->hw->wiphy->probe_resp_offload =
5273                NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5274                NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5275                NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5276
5277        SET_IEEE80211_DEV(wl->hw, wl->dev);
5278
5279        wl->hw->sta_data_size = sizeof(struct wl1271_station);
5280        wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
5281
5282        wl->hw->max_rx_aggregation_subframes = 8;
5283
5284        return 0;
5285}
5286
5287#define WL1271_DEFAULT_CHANNEL 0
5288
5289static struct ieee80211_hw *wl1271_alloc_hw(void)
5290{
5291        struct ieee80211_hw *hw;
5292        struct wl1271 *wl;
5293        int i, j, ret;
5294        unsigned int order;
5295
5296        BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
5297
5298        hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5299        if (!hw) {
5300                wl1271_error("could not alloc ieee80211_hw");
5301                ret = -ENOMEM;
5302                goto err_hw_alloc;
5303        }
5304
5305        wl = hw->priv;
5306        memset(wl, 0, sizeof(*wl));
5307
5308        INIT_LIST_HEAD(&wl->wlvif_list);
5309
5310        wl->hw = hw;
5311
5312        for (i = 0; i < NUM_TX_QUEUES; i++)
5313                for (j = 0; j < WL12XX_MAX_LINKS; j++)
5314                        skb_queue_head_init(&wl->links[j].tx_queue[i]);
5315
5316        skb_queue_head_init(&wl->deferred_rx_queue);
5317        skb_queue_head_init(&wl->deferred_tx_queue);
5318
5319        INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
5320        INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
5321        INIT_WORK(&wl->tx_work, wl1271_tx_work);
5322        INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5323        INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
5324        INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
5325
5326        wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5327        if (!wl->freezable_wq) {
5328                ret = -ENOMEM;
5329                goto err_hw;
5330        }
5331
5332        wl->channel = WL1271_DEFAULT_CHANNEL;
5333        wl->rx_counter = 0;
5334        wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
5335        wl->band = IEEE80211_BAND_2GHZ;
5336        wl->flags = 0;
5337        wl->sg_enabled = true;
5338        wl->hw_pg_ver = -1;
5339        wl->ap_ps_map = 0;
5340        wl->ap_fw_ps_map = 0;
5341        wl->quirks = 0;
5342        wl->platform_quirks = 0;
5343        wl->sched_scanning = false;
5344        wl->tx_spare_blocks = TX_HW_BLOCK_SPARE_DEFAULT;
5345        wl->system_hlid = WL12XX_SYSTEM_HLID;
5346        wl->active_sta_count = 0;
5347        wl->fwlog_size = 0;
5348        init_waitqueue_head(&wl->fwlog_waitq);
5349
5350        /* The system link is always allocated */
5351        __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5352
5353        memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
5354        for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
5355                wl->tx_frames[i] = NULL;
5356
5357        spin_lock_init(&wl->wl_lock);
5358
5359        wl->state = WL1271_STATE_OFF;
5360        wl->fw_type = WL12XX_FW_TYPE_NONE;
5361        mutex_init(&wl->mutex);
5362
5363        /* Apply default driver configuration. */
5364        wl1271_conf_init(wl);
5365
5366        order = get_order(WL1271_AGGR_BUFFER_SIZE);
5367        wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5368        if (!wl->aggr_buf) {
5369                ret = -ENOMEM;
5370                goto err_wq;
5371        }
5372
5373        wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5374        if (!wl->dummy_packet) {
5375                ret = -ENOMEM;
5376                goto err_aggr;
5377        }
5378
5379        /* Allocate one page for the FW log */
5380        wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5381        if (!wl->fwlog) {
5382                ret = -ENOMEM;
5383                goto err_dummy_packet;
5384        }
5385
5386        return hw;
5387
5388err_dummy_packet:
5389        dev_kfree_skb(wl->dummy_packet);
5390
5391err_aggr:
5392        free_pages((unsigned long)wl->aggr_buf, order);
5393
5394err_wq:
5395        destroy_workqueue(wl->freezable_wq);
5396
5397err_hw:
5398        wl1271_debugfs_exit(wl);
5399        ieee80211_free_hw(hw);
5400
5401err_hw_alloc:
5402
5403        return ERR_PTR(ret);
5404}
5405
5406static int wl1271_free_hw(struct wl1271 *wl)
5407{
5408        /* Unblock any fwlog readers */
5409        mutex_lock(&wl->mutex);
5410        wl->fwlog_size = -1;
5411        wake_up_interruptible_all(&wl->fwlog_waitq);
5412        mutex_unlock(&wl->mutex);
5413
5414        device_remove_bin_file(wl->dev, &fwlog_attr);
5415
5416        device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5417
5418        device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5419        free_page((unsigned long)wl->fwlog);
5420        dev_kfree_skb(wl->dummy_packet);
5421        free_pages((unsigned long)wl->aggr_buf,
5422                        get_order(WL1271_AGGR_BUFFER_SIZE));
5423
5424        wl1271_debugfs_exit(wl);
5425
5426        vfree(wl->fw);
5427        wl->fw = NULL;
5428        wl->fw_type = WL12XX_FW_TYPE_NONE;
5429        kfree(wl->nvs);
5430        wl->nvs = NULL;
5431
5432        kfree(wl->fw_status);
5433        kfree(wl->tx_res_if);
5434        destroy_workqueue(wl->freezable_wq);
5435
5436        ieee80211_free_hw(wl->hw);
5437
5438        return 0;
5439}
5440
5441static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5442{
5443        struct wl1271 *wl = cookie;
5444        unsigned long flags;
5445
5446        wl1271_debug(DEBUG_IRQ, "IRQ");
5447
5448        /* complete the ELP completion */
5449        spin_lock_irqsave(&wl->wl_lock, flags);
5450        set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5451        if (wl->elp_compl) {
5452                complete(wl->elp_compl);
5453                wl->elp_compl = NULL;
5454        }
5455
5456        if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5457                /* don't enqueue a work right now. mark it as pending */
5458                set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5459                wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5460                disable_irq_nosync(wl->irq);
5461                pm_wakeup_event(wl->dev, 0);
5462                spin_unlock_irqrestore(&wl->wl_lock, flags);
5463                return IRQ_HANDLED;
5464        }
5465        spin_unlock_irqrestore(&wl->wl_lock, flags);
5466
5467        return IRQ_WAKE_THREAD;
5468}
5469
5470static int __devinit wl12xx_probe(struct platform_device *pdev)
5471{
5472        struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
5473        struct ieee80211_hw *hw;
5474        struct wl1271 *wl;
5475        unsigned long irqflags;
5476        int ret = -ENODEV;
5477
5478        hw = wl1271_alloc_hw();
5479        if (IS_ERR(hw)) {
5480                wl1271_error("can't allocate hw");
5481                ret = PTR_ERR(hw);
5482                goto out;
5483        }
5484
5485        wl = hw->priv;
5486        wl->irq = platform_get_irq(pdev, 0);
5487        wl->ref_clock = pdata->board_ref_clock;
5488        wl->tcxo_clock = pdata->board_tcxo_clock;
5489        wl->platform_quirks = pdata->platform_quirks;
5490        wl->set_power = pdata->set_power;
5491        wl->dev = &pdev->dev;
5492        wl->if_ops = pdata->ops;
5493
5494        platform_set_drvdata(pdev, wl);
5495
5496        if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5497                irqflags = IRQF_TRIGGER_RISING;
5498        else
5499                irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5500
5501        ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wl1271_irq,
5502                                   irqflags,
5503                                   pdev->name, wl);
5504        if (ret < 0) {
5505                wl1271_error("request_irq() failed: %d", ret);
5506                goto out_free_hw;
5507        }
5508
5509        ret = enable_irq_wake(wl->irq);
5510        if (!ret) {
5511                wl->irq_wake_enabled = true;
5512                device_init_wakeup(wl->dev, 1);
5513                if (pdata->pwr_in_suspend)
5514                        hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
5515
5516        }
5517        disable_irq(wl->irq);
5518
5519        ret = wl1271_init_ieee80211(wl);
5520        if (ret)
5521                goto out_irq;
5522
5523        ret = wl1271_register_hw(wl);
5524        if (ret)
5525                goto out_irq;
5526
5527        /* Create sysfs file to control bt coex state */
5528        ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5529        if (ret < 0) {
5530                wl1271_error("failed to create sysfs file bt_coex_state");
5531                goto out_irq;
5532        }
5533
5534        /* Create sysfs file to get HW PG version */
5535        ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5536        if (ret < 0) {
5537                wl1271_error("failed to create sysfs file hw_pg_ver");
5538                goto out_bt_coex_state;
5539        }
5540
5541        /* Create sysfs file for the FW log */
5542        ret = device_create_bin_file(wl->dev, &fwlog_attr);
5543        if (ret < 0) {
5544                wl1271_error("failed to create sysfs file fwlog");
5545                goto out_hw_pg_ver;
5546        }
5547
5548        return 0;
5549
5550out_hw_pg_ver:
5551        device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5552
5553out_bt_coex_state:
5554        device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5555
5556out_irq:
5557        free_irq(wl->irq, wl);
5558
5559out_free_hw:
5560        wl1271_free_hw(wl);
5561
5562out:
5563        return ret;
5564}
5565
5566static int __devexit wl12xx_remove(struct platform_device *pdev)
5567{
5568        struct wl1271 *wl = platform_get_drvdata(pdev);
5569
5570        if (wl->irq_wake_enabled) {
5571                device_init_wakeup(wl->dev, 0);
5572                disable_irq_wake(wl->irq);
5573        }
5574        wl1271_unregister_hw(wl);
5575        free_irq(wl->irq, wl);
5576        wl1271_free_hw(wl);
5577
5578        return 0;
5579}
5580
5581static const struct platform_device_id wl12xx_id_table[] __devinitconst = {
5582        { "wl12xx", 0 },
5583        {  } /* Terminating Entry */
5584};
5585MODULE_DEVICE_TABLE(platform, wl12xx_id_table);
5586
5587static struct platform_driver wl12xx_driver = {
5588        .probe          = wl12xx_probe,
5589        .remove         = __devexit_p(wl12xx_remove),
5590        .id_table       = wl12xx_id_table,
5591        .driver = {
5592                .name   = "wl12xx_driver",
5593                .owner  = THIS_MODULE,
5594        }
5595};
5596
5597static int __init wl12xx_init(void)
5598{
5599        return platform_driver_register(&wl12xx_driver);
5600}
5601module_init(wl12xx_init);
5602
5603static void __exit wl12xx_exit(void)
5604{
5605        platform_driver_unregister(&wl12xx_driver);
5606}
5607module_exit(wl12xx_exit);
5608
5609u32 wl12xx_debug_level = DEBUG_NONE;
5610EXPORT_SYMBOL_GPL(wl12xx_debug_level);
5611module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
5612MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
5613
5614module_param_named(fwlog, fwlog_param, charp, 0);
5615MODULE_PARM_DESC(fwlog,
5616                 "FW logger options: continuous, ondemand, dbgpins or disable");
5617
5618module_param(bug_on_recovery, bool, S_IRUSR | S_IWUSR);
5619MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
5620
5621MODULE_LICENSE("GPL");
5622MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
5623MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
5624