linux/drivers/net/wireless/ti/wlcore/cmd.c
<<
>>
Prefs
   1/*
   2 * This file is part of wl1271
   3 *
   4 * Copyright (C) 2009-2010 Nokia Corporation
   5 *
   6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License
  10 * version 2 as published by the Free Software Foundation.
  11 *
  12 * This program is distributed in the hope that it will be useful, but
  13 * WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15 * General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20 * 02110-1301 USA
  21 *
  22 */
  23
  24#include <linux/module.h>
  25#include <linux/platform_device.h>
  26#include <linux/spi/spi.h>
  27#include <linux/etherdevice.h>
  28#include <linux/ieee80211.h>
  29#include <linux/slab.h>
  30
  31#include "wlcore.h"
  32#include "debug.h"
  33#include "io.h"
  34#include "acx.h"
  35#include "wl12xx_80211.h"
  36#include "cmd.h"
  37#include "event.h"
  38#include "tx.h"
  39#include "hw_ops.h"
  40
  41#define WL1271_CMD_FAST_POLL_COUNT       50
  42#define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
  43
  44/*
  45 * send command to firmware
  46 *
  47 * @wl: wl struct
  48 * @id: command id
  49 * @buf: buffer containing the command, must work with dma
  50 * @len: length of the buffer
  51 * return the cmd status code on success.
  52 */
  53static int __wlcore_cmd_send(struct wl1271 *wl, u16 id, void *buf,
  54                             size_t len, size_t res_len)
  55{
  56        struct wl1271_cmd_header *cmd;
  57        unsigned long timeout;
  58        u32 intr;
  59        int ret;
  60        u16 status;
  61        u16 poll_count = 0;
  62
  63        if (unlikely(wl->state == WLCORE_STATE_RESTARTING &&
  64                     id != CMD_STOP_FWLOGGER))
  65                return -EIO;
  66
  67        if (WARN_ON_ONCE(len < sizeof(*cmd)))
  68                return -EIO;
  69
  70        cmd = buf;
  71        cmd->id = cpu_to_le16(id);
  72        cmd->status = 0;
  73
  74        WARN_ON(len % 4 != 0);
  75        WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
  76
  77        ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
  78        if (ret < 0)
  79                return ret;
  80
  81        /*
  82         * TODO: we just need this because one bit is in a different
  83         * place.  Is there any better way?
  84         */
  85        ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
  86        if (ret < 0)
  87                return ret;
  88
  89        timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  90
  91        ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  92        if (ret < 0)
  93                return ret;
  94
  95        while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  96                if (time_after(jiffies, timeout)) {
  97                        wl1271_error("command complete timeout");
  98                        return -ETIMEDOUT;
  99                }
 100
 101                poll_count++;
 102                if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
 103                        udelay(10);
 104                else
 105                        msleep(1);
 106
 107                ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
 108                if (ret < 0)
 109                        return ret;
 110        }
 111
 112        /* read back the status code of the command */
 113        if (res_len == 0)
 114                res_len = sizeof(struct wl1271_cmd_header);
 115
 116        ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
 117        if (ret < 0)
 118                return ret;
 119
 120        status = le16_to_cpu(cmd->status);
 121
 122        ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
 123                               WL1271_ACX_INTR_CMD_COMPLETE);
 124        if (ret < 0)
 125                return ret;
 126
 127        return status;
 128}
 129
 130/*
 131 * send command to fw and return cmd status on success
 132 * valid_rets contains a bitmap of allowed error codes
 133 */
 134static int wlcore_cmd_send_failsafe(struct wl1271 *wl, u16 id, void *buf,
 135                                    size_t len, size_t res_len,
 136                                    unsigned long valid_rets)
 137{
 138        int ret = __wlcore_cmd_send(wl, id, buf, len, res_len);
 139
 140        if (ret < 0)
 141                goto fail;
 142
 143        /* success is always a valid status */
 144        valid_rets |= BIT(CMD_STATUS_SUCCESS);
 145
 146        if (ret >= MAX_COMMAND_STATUS ||
 147            !test_bit(ret, &valid_rets)) {
 148                wl1271_error("command execute failure %d", ret);
 149                ret = -EIO;
 150                goto fail;
 151        }
 152        return ret;
 153fail:
 154        wl12xx_queue_recovery_work(wl);
 155        return ret;
 156}
 157
 158/*
 159 * wrapper for wlcore_cmd_send that accept only CMD_STATUS_SUCCESS
 160 * return 0 on success.
 161 */
 162int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
 163                    size_t res_len)
 164{
 165        int ret = wlcore_cmd_send_failsafe(wl, id, buf, len, res_len, 0);
 166
 167        if (ret < 0)
 168                return ret;
 169        return 0;
 170}
 171EXPORT_SYMBOL_GPL(wl1271_cmd_send);
 172
 173/*
 174 * Poll the mailbox event field until any of the bits in the mask is set or a
 175 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
 176 */
 177int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
 178                                         u32 mask, bool *timeout)
 179{
 180        u32 *events_vector;
 181        u32 event;
 182        unsigned long timeout_time;
 183        u16 poll_count = 0;
 184        int ret = 0;
 185
 186        *timeout = false;
 187
 188        events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
 189        if (!events_vector)
 190                return -ENOMEM;
 191
 192        timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
 193
 194        do {
 195                if (time_after(jiffies, timeout_time)) {
 196                        wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
 197                                     (int)mask);
 198                        *timeout = true;
 199                        goto out;
 200                }
 201
 202                poll_count++;
 203                if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
 204                        usleep_range(50, 51);
 205                else
 206                        usleep_range(1000, 5000);
 207
 208                /* read from both event fields */
 209                ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
 210                                  sizeof(*events_vector), false);
 211                if (ret < 0)
 212                        goto out;
 213
 214                event = *events_vector & mask;
 215
 216                ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
 217                                  sizeof(*events_vector), false);
 218                if (ret < 0)
 219                        goto out;
 220
 221                event |= *events_vector & mask;
 222        } while (!event);
 223
 224out:
 225        kfree(events_vector);
 226        return ret;
 227}
 228EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
 229
 230int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
 231                           u8 *role_id)
 232{
 233        struct wl12xx_cmd_role_enable *cmd;
 234        int ret;
 235
 236        wl1271_debug(DEBUG_CMD, "cmd role enable");
 237
 238        if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
 239                return -EBUSY;
 240
 241        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 242        if (!cmd) {
 243                ret = -ENOMEM;
 244                goto out;
 245        }
 246
 247        /* get role id */
 248        cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
 249        if (cmd->role_id >= WL12XX_MAX_ROLES) {
 250                ret = -EBUSY;
 251                goto out_free;
 252        }
 253
 254        memcpy(cmd->mac_address, addr, ETH_ALEN);
 255        cmd->role_type = role_type;
 256
 257        ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
 258        if (ret < 0) {
 259                wl1271_error("failed to initiate cmd role enable");
 260                goto out_free;
 261        }
 262
 263        __set_bit(cmd->role_id, wl->roles_map);
 264        *role_id = cmd->role_id;
 265
 266out_free:
 267        kfree(cmd);
 268
 269out:
 270        return ret;
 271}
 272
 273int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
 274{
 275        struct wl12xx_cmd_role_disable *cmd;
 276        int ret;
 277
 278        wl1271_debug(DEBUG_CMD, "cmd role disable");
 279
 280        if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
 281                return -ENOENT;
 282
 283        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 284        if (!cmd) {
 285                ret = -ENOMEM;
 286                goto out;
 287        }
 288        cmd->role_id = *role_id;
 289
 290        ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
 291        if (ret < 0) {
 292                wl1271_error("failed to initiate cmd role disable");
 293                goto out_free;
 294        }
 295
 296        __clear_bit(*role_id, wl->roles_map);
 297        *role_id = WL12XX_INVALID_ROLE_ID;
 298
 299out_free:
 300        kfree(cmd);
 301
 302out:
 303        return ret;
 304}
 305
 306static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid)
 307{
 308        if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX)
 309                wl->session_ids[hlid] = 0;
 310
 311        wl->session_ids[hlid]++;
 312
 313        return wl->session_ids[hlid];
 314}
 315
 316int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
 317{
 318        unsigned long flags;
 319        u8 link = find_first_zero_bit(wl->links_map, wl->num_links);
 320        if (link >= wl->num_links)
 321                return -EBUSY;
 322
 323        wl->session_ids[link] = wlcore_get_new_session_id(wl, link);
 324
 325        /* these bits are used by op_tx */
 326        spin_lock_irqsave(&wl->wl_lock, flags);
 327        __set_bit(link, wl->links_map);
 328        __set_bit(link, wlvif->links_map);
 329        spin_unlock_irqrestore(&wl->wl_lock, flags);
 330
 331        /*
 332         * take the last "freed packets" value from the current FW status.
 333         * on recovery, we might not have fw_status yet, and
 334         * tx_lnk_free_pkts will be NULL. check for it.
 335         */
 336        if (wl->fw_status->counters.tx_lnk_free_pkts)
 337                wl->links[link].prev_freed_pkts =
 338                        wl->fw_status->counters.tx_lnk_free_pkts[link];
 339        wl->links[link].wlvif = wlvif;
 340
 341        /*
 342         * Take saved value for total freed packets from wlvif, in case this is
 343         * recovery/resume
 344         */
 345        if (wlvif->bss_type != BSS_TYPE_AP_BSS)
 346                wl->links[link].total_freed_pkts = wlvif->total_freed_pkts;
 347
 348        *hlid = link;
 349
 350        wl->active_link_count++;
 351        return 0;
 352}
 353
 354void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
 355{
 356        unsigned long flags;
 357
 358        if (*hlid == WL12XX_INVALID_LINK_ID)
 359                return;
 360
 361        /* these bits are used by op_tx */
 362        spin_lock_irqsave(&wl->wl_lock, flags);
 363        __clear_bit(*hlid, wl->links_map);
 364        __clear_bit(*hlid, wlvif->links_map);
 365        spin_unlock_irqrestore(&wl->wl_lock, flags);
 366
 367        wl->links[*hlid].allocated_pkts = 0;
 368        wl->links[*hlid].prev_freed_pkts = 0;
 369        wl->links[*hlid].ba_bitmap = 0;
 370        memset(wl->links[*hlid].addr, 0, ETH_ALEN);
 371
 372        /*
 373         * At this point op_tx() will not add more packets to the queues. We
 374         * can purge them.
 375         */
 376        wl1271_tx_reset_link_queues(wl, *hlid);
 377        wl->links[*hlid].wlvif = NULL;
 378
 379        if (wlvif->bss_type == BSS_TYPE_AP_BSS &&
 380            *hlid == wlvif->ap.bcast_hlid) {
 381                u32 sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING;
 382                /*
 383                 * save the total freed packets in the wlvif, in case this is
 384                 * recovery or suspend
 385                 */
 386                wlvif->total_freed_pkts = wl->links[*hlid].total_freed_pkts;
 387
 388                /*
 389                 * increment the initial seq number on recovery to account for
 390                 * transmitted packets that we haven't yet got in the FW status
 391                 */
 392                if (wlvif->encryption_type == KEY_GEM)
 393                        sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM;
 394
 395                if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
 396                        wlvif->total_freed_pkts += sqn_padding;
 397        }
 398
 399        wl->links[*hlid].total_freed_pkts = 0;
 400
 401        *hlid = WL12XX_INVALID_LINK_ID;
 402        wl->active_link_count--;
 403        WARN_ON_ONCE(wl->active_link_count < 0);
 404}
 405
 406static u8 wlcore_get_native_channel_type(u8 nl_channel_type)
 407{
 408        switch (nl_channel_type) {
 409        case NL80211_CHAN_NO_HT:
 410                return WLCORE_CHAN_NO_HT;
 411        case NL80211_CHAN_HT20:
 412                return WLCORE_CHAN_HT20;
 413        case NL80211_CHAN_HT40MINUS:
 414                return WLCORE_CHAN_HT40MINUS;
 415        case NL80211_CHAN_HT40PLUS:
 416                return WLCORE_CHAN_HT40PLUS;
 417        default:
 418                WARN_ON(1);
 419                return WLCORE_CHAN_NO_HT;
 420        }
 421}
 422
 423static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
 424                                     struct wl12xx_vif *wlvif,
 425                                     enum ieee80211_band band,
 426                                     int channel)
 427{
 428        struct wl12xx_cmd_role_start *cmd;
 429        int ret;
 430
 431        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 432        if (!cmd) {
 433                ret = -ENOMEM;
 434                goto out;
 435        }
 436
 437        wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
 438
 439        cmd->role_id = wlvif->dev_role_id;
 440        if (band == IEEE80211_BAND_5GHZ)
 441                cmd->band = WLCORE_BAND_5GHZ;
 442        cmd->channel = channel;
 443
 444        if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
 445                ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
 446                if (ret)
 447                        goto out_free;
 448        }
 449        cmd->device.hlid = wlvif->dev_hlid;
 450        cmd->device.session = wl->session_ids[wlvif->dev_hlid];
 451
 452        wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
 453                     cmd->role_id, cmd->device.hlid, cmd->device.session);
 454
 455        ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
 456        if (ret < 0) {
 457                wl1271_error("failed to initiate cmd role enable");
 458                goto err_hlid;
 459        }
 460
 461        goto out_free;
 462
 463err_hlid:
 464        /* clear links on error */
 465        wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
 466
 467out_free:
 468        kfree(cmd);
 469
 470out:
 471        return ret;
 472}
 473
 474static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
 475                                    struct wl12xx_vif *wlvif)
 476{
 477        struct wl12xx_cmd_role_stop *cmd;
 478        int ret;
 479
 480        if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
 481                return -EINVAL;
 482
 483        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 484        if (!cmd) {
 485                ret = -ENOMEM;
 486                goto out;
 487        }
 488
 489        wl1271_debug(DEBUG_CMD, "cmd role stop dev");
 490
 491        cmd->role_id = wlvif->dev_role_id;
 492        cmd->disc_type = DISCONNECT_IMMEDIATE;
 493        cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
 494
 495        ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
 496        if (ret < 0) {
 497                wl1271_error("failed to initiate cmd role stop");
 498                goto out_free;
 499        }
 500
 501        wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
 502
 503out_free:
 504        kfree(cmd);
 505
 506out:
 507        return ret;
 508}
 509
 510int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 511{
 512        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
 513        struct wl12xx_cmd_role_start *cmd;
 514        u32 supported_rates;
 515        int ret;
 516
 517        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 518        if (!cmd) {
 519                ret = -ENOMEM;
 520                goto out;
 521        }
 522
 523        wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
 524
 525        cmd->role_id = wlvif->role_id;
 526        if (wlvif->band == IEEE80211_BAND_5GHZ)
 527                cmd->band = WLCORE_BAND_5GHZ;
 528        cmd->channel = wlvif->channel;
 529        cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
 530        cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
 531        cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
 532        cmd->sta.ssid_len = wlvif->ssid_len;
 533        memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
 534        memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
 535
 536        supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
 537                          wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
 538        if (wlvif->p2p)
 539                supported_rates &= ~CONF_TX_CCK_RATES;
 540
 541        cmd->sta.local_rates = cpu_to_le32(supported_rates);
 542
 543        cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
 544
 545        if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
 546                ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
 547                if (ret)
 548                        goto out_free;
 549        }
 550        cmd->sta.hlid = wlvif->sta.hlid;
 551        cmd->sta.session = wl->session_ids[wlvif->sta.hlid];
 552        /*
 553         * We don't have the correct remote rates in this stage.  The
 554         * rates will be reconfigured later, after association, if the
 555         * firmware supports ACX_PEER_CAP.  Otherwise, there's nothing
 556         * we can do, so use all supported_rates here.
 557         */
 558        cmd->sta.remote_rates = cpu_to_le32(supported_rates);
 559
 560        wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
 561                     "basic_rate_set: 0x%x, remote_rates: 0x%x",
 562                     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
 563                     wlvif->basic_rate_set, wlvif->rate_set);
 564
 565        ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
 566        if (ret < 0) {
 567                wl1271_error("failed to initiate cmd role start sta");
 568                goto err_hlid;
 569        }
 570
 571        wlvif->sta.role_chan_type = wlvif->channel_type;
 572        goto out_free;
 573
 574err_hlid:
 575        /* clear links on error. */
 576        wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
 577
 578out_free:
 579        kfree(cmd);
 580
 581out:
 582        return ret;
 583}
 584
 585/* use this function to stop ibss as well */
 586int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 587{
 588        struct wl12xx_cmd_role_stop *cmd;
 589        int ret;
 590
 591        if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
 592                return -EINVAL;
 593
 594        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 595        if (!cmd) {
 596                ret = -ENOMEM;
 597                goto out;
 598        }
 599
 600        wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
 601
 602        cmd->role_id = wlvif->role_id;
 603        cmd->disc_type = DISCONNECT_IMMEDIATE;
 604        cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
 605
 606        ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
 607        if (ret < 0) {
 608                wl1271_error("failed to initiate cmd role stop sta");
 609                goto out_free;
 610        }
 611
 612        wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
 613
 614out_free:
 615        kfree(cmd);
 616
 617out:
 618        return ret;
 619}
 620
 621int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 622{
 623        struct wl12xx_cmd_role_start *cmd;
 624        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
 625        struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
 626        u32 supported_rates;
 627        int ret;
 628
 629        wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
 630
 631        /* trying to use hidden SSID with an old hostapd version */
 632        if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
 633                wl1271_error("got a null SSID from beacon/bss");
 634                ret = -EINVAL;
 635                goto out;
 636        }
 637
 638        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 639        if (!cmd) {
 640                ret = -ENOMEM;
 641                goto out;
 642        }
 643
 644        ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
 645        if (ret < 0)
 646                goto out_free;
 647
 648        ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
 649        if (ret < 0)
 650                goto out_free_global;
 651
 652        /* use the previous security seq, if this is a recovery/resume */
 653        wl->links[wlvif->ap.bcast_hlid].total_freed_pkts =
 654                                                wlvif->total_freed_pkts;
 655
 656        cmd->role_id = wlvif->role_id;
 657        cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
 658        cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
 659        cmd->ap.global_hlid = wlvif->ap.global_hlid;
 660        cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
 661        cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
 662        cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
 663        cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
 664        cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
 665        cmd->ap.dtim_interval = bss_conf->dtim_period;
 666        cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
 667        /* FIXME: Change when adding DFS */
 668        cmd->ap.reset_tsf = 1;  /* By default reset AP TSF */
 669        cmd->ap.wmm = wlvif->wmm_enabled;
 670        cmd->channel = wlvif->channel;
 671        cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
 672
 673        if (!bss_conf->hidden_ssid) {
 674                /* take the SSID from the beacon for backward compatibility */
 675                cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
 676                cmd->ap.ssid_len = wlvif->ssid_len;
 677                memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
 678        } else {
 679                cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
 680                cmd->ap.ssid_len = bss_conf->ssid_len;
 681                memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
 682        }
 683
 684        supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
 685                wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
 686        if (wlvif->p2p)
 687                supported_rates &= ~CONF_TX_CCK_RATES;
 688
 689        wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
 690                     supported_rates);
 691
 692        cmd->ap.local_rates = cpu_to_le32(supported_rates);
 693
 694        switch (wlvif->band) {
 695        case IEEE80211_BAND_2GHZ:
 696                cmd->band = WLCORE_BAND_2_4GHZ;
 697                break;
 698        case IEEE80211_BAND_5GHZ:
 699                cmd->band = WLCORE_BAND_5GHZ;
 700                break;
 701        default:
 702                wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
 703                cmd->band = WLCORE_BAND_2_4GHZ;
 704                break;
 705        }
 706
 707        ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
 708        if (ret < 0) {
 709                wl1271_error("failed to initiate cmd role start ap");
 710                goto out_free_bcast;
 711        }
 712
 713        goto out_free;
 714
 715out_free_bcast:
 716        wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
 717
 718out_free_global:
 719        wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
 720
 721out_free:
 722        kfree(cmd);
 723
 724out:
 725        return ret;
 726}
 727
 728int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 729{
 730        struct wl12xx_cmd_role_stop *cmd;
 731        int ret;
 732
 733        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 734        if (!cmd) {
 735                ret = -ENOMEM;
 736                goto out;
 737        }
 738
 739        wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
 740
 741        cmd->role_id = wlvif->role_id;
 742
 743        ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
 744        if (ret < 0) {
 745                wl1271_error("failed to initiate cmd role stop ap");
 746                goto out_free;
 747        }
 748
 749        wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
 750        wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
 751
 752out_free:
 753        kfree(cmd);
 754
 755out:
 756        return ret;
 757}
 758
 759int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
 760{
 761        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
 762        struct wl12xx_cmd_role_start *cmd;
 763        struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
 764        int ret;
 765
 766        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 767        if (!cmd) {
 768                ret = -ENOMEM;
 769                goto out;
 770        }
 771
 772        wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
 773
 774        cmd->role_id = wlvif->role_id;
 775        if (wlvif->band == IEEE80211_BAND_5GHZ)
 776                cmd->band = WLCORE_BAND_5GHZ;
 777        cmd->channel = wlvif->channel;
 778        cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
 779        cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
 780        cmd->ibss.dtim_interval = bss_conf->dtim_period;
 781        cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
 782        cmd->ibss.ssid_len = wlvif->ssid_len;
 783        memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
 784        memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
 785        cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
 786
 787        if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
 788                ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
 789                if (ret)
 790                        goto out_free;
 791        }
 792        cmd->ibss.hlid = wlvif->sta.hlid;
 793        cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
 794
 795        wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
 796                     "basic_rate_set: 0x%x, remote_rates: 0x%x",
 797                     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
 798                     wlvif->basic_rate_set, wlvif->rate_set);
 799
 800        wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
 801                     vif->bss_conf.bssid);
 802
 803        ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
 804        if (ret < 0) {
 805                wl1271_error("failed to initiate cmd role enable");
 806                goto err_hlid;
 807        }
 808
 809        goto out_free;
 810
 811err_hlid:
 812        /* clear links on error. */
 813        wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
 814
 815out_free:
 816        kfree(cmd);
 817
 818out:
 819        return ret;
 820}
 821
 822
 823/**
 824 * send test command to firmware
 825 *
 826 * @wl: wl struct
 827 * @buf: buffer containing the command, with all headers, must work with dma
 828 * @len: length of the buffer
 829 * @answer: is answer needed
 830 */
 831int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
 832{
 833        int ret;
 834        size_t res_len = 0;
 835
 836        wl1271_debug(DEBUG_CMD, "cmd test");
 837
 838        if (answer)
 839                res_len = buf_len;
 840
 841        ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
 842
 843        if (ret < 0) {
 844                wl1271_warning("TEST command failed");
 845                return ret;
 846        }
 847
 848        return ret;
 849}
 850EXPORT_SYMBOL_GPL(wl1271_cmd_test);
 851
 852/**
 853 * read acx from firmware
 854 *
 855 * @wl: wl struct
 856 * @id: acx id
 857 * @buf: buffer for the response, including all headers, must work with dma
 858 * @len: length of buf
 859 */
 860int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf,
 861                           size_t cmd_len, size_t res_len)
 862{
 863        struct acx_header *acx = buf;
 864        int ret;
 865
 866        wl1271_debug(DEBUG_CMD, "cmd interrogate");
 867
 868        acx->id = cpu_to_le16(id);
 869
 870        /* response payload length, does not include any headers */
 871        acx->len = cpu_to_le16(res_len - sizeof(*acx));
 872
 873        ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, cmd_len, res_len);
 874        if (ret < 0)
 875                wl1271_error("INTERROGATE command failed");
 876
 877        return ret;
 878}
 879
 880/**
 881 * write acx value to firmware
 882 *
 883 * @wl: wl struct
 884 * @id: acx id
 885 * @buf: buffer containing acx, including all headers, must work with dma
 886 * @len: length of buf
 887 * @valid_rets: bitmap of valid cmd status codes (i.e. return values).
 888 * return the cmd status on success.
 889 */
 890int wlcore_cmd_configure_failsafe(struct wl1271 *wl, u16 id, void *buf,
 891                                  size_t len, unsigned long valid_rets)
 892{
 893        struct acx_header *acx = buf;
 894        int ret;
 895
 896        wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
 897
 898        if (WARN_ON_ONCE(len < sizeof(*acx)))
 899                return -EIO;
 900
 901        acx->id = cpu_to_le16(id);
 902
 903        /* payload length, does not include any headers */
 904        acx->len = cpu_to_le16(len - sizeof(*acx));
 905
 906        ret = wlcore_cmd_send_failsafe(wl, CMD_CONFIGURE, acx, len, 0,
 907                                       valid_rets);
 908        if (ret < 0) {
 909                wl1271_warning("CONFIGURE command NOK");
 910                return ret;
 911        }
 912
 913        return ret;
 914}
 915
 916/*
 917 * wrapper for wlcore_cmd_configure that accepts only success status.
 918 * return 0 on success
 919 */
 920int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
 921{
 922        int ret = wlcore_cmd_configure_failsafe(wl, id, buf, len, 0);
 923
 924        if (ret < 0)
 925                return ret;
 926        return 0;
 927}
 928EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
 929
 930int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
 931{
 932        struct cmd_enabledisable_path *cmd;
 933        int ret;
 934        u16 cmd_rx, cmd_tx;
 935
 936        wl1271_debug(DEBUG_CMD, "cmd data path");
 937
 938        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
 939        if (!cmd) {
 940                ret = -ENOMEM;
 941                goto out;
 942        }
 943
 944        /* the channel here is only used for calibration, so hardcoded to 1 */
 945        cmd->channel = 1;
 946
 947        if (enable) {
 948                cmd_rx = CMD_ENABLE_RX;
 949                cmd_tx = CMD_ENABLE_TX;
 950        } else {
 951                cmd_rx = CMD_DISABLE_RX;
 952                cmd_tx = CMD_DISABLE_TX;
 953        }
 954
 955        ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
 956        if (ret < 0) {
 957                wl1271_error("rx %s cmd for channel %d failed",
 958                             enable ? "start" : "stop", cmd->channel);
 959                goto out;
 960        }
 961
 962        wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
 963                     enable ? "start" : "stop", cmd->channel);
 964
 965        ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
 966        if (ret < 0) {
 967                wl1271_error("tx %s cmd for channel %d failed",
 968                             enable ? "start" : "stop", cmd->channel);
 969                goto out;
 970        }
 971
 972        wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
 973                     enable ? "start" : "stop", cmd->channel);
 974
 975out:
 976        kfree(cmd);
 977        return ret;
 978}
 979EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
 980
 981int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
 982                       u8 ps_mode, u16 auto_ps_timeout)
 983{
 984        struct wl1271_cmd_ps_params *ps_params = NULL;
 985        int ret = 0;
 986
 987        wl1271_debug(DEBUG_CMD, "cmd set ps mode");
 988
 989        ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
 990        if (!ps_params) {
 991                ret = -ENOMEM;
 992                goto out;
 993        }
 994
 995        ps_params->role_id = wlvif->role_id;
 996        ps_params->ps_mode = ps_mode;
 997        ps_params->auto_ps_timeout = auto_ps_timeout;
 998
 999        ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
1000                              sizeof(*ps_params), 0);
1001        if (ret < 0) {
1002                wl1271_error("cmd set_ps_mode failed");
1003                goto out;
1004        }
1005
1006out:
1007        kfree(ps_params);
1008        return ret;
1009}
1010
1011int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
1012                            u16 template_id, void *buf, size_t buf_len,
1013                            int index, u32 rates)
1014{
1015        struct wl1271_cmd_template_set *cmd;
1016        int ret = 0;
1017
1018        wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
1019                     template_id, role_id);
1020
1021        WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1022        buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1023
1024        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1025        if (!cmd) {
1026                ret = -ENOMEM;
1027                goto out;
1028        }
1029
1030        /* during initialization wlvif is NULL */
1031        cmd->role_id = role_id;
1032        cmd->len = cpu_to_le16(buf_len);
1033        cmd->template_type = template_id;
1034        cmd->enabled_rates = cpu_to_le32(rates);
1035        cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1036        cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1037        cmd->index = index;
1038
1039        if (buf)
1040                memcpy(cmd->template_data, buf, buf_len);
1041
1042        ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1043        if (ret < 0) {
1044                wl1271_warning("cmd set_template failed: %d", ret);
1045                goto out_free;
1046        }
1047
1048out_free:
1049        kfree(cmd);
1050
1051out:
1052        return ret;
1053}
1054
1055int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1056{
1057        struct sk_buff *skb = NULL;
1058        int size;
1059        void *ptr;
1060        int ret = -ENOMEM;
1061
1062
1063        if (wlvif->bss_type == BSS_TYPE_IBSS) {
1064                size = sizeof(struct wl12xx_null_data_template);
1065                ptr = NULL;
1066        } else {
1067                skb = ieee80211_nullfunc_get(wl->hw,
1068                                             wl12xx_wlvif_to_vif(wlvif));
1069                if (!skb)
1070                        goto out;
1071                size = skb->len;
1072                ptr = skb->data;
1073        }
1074
1075        ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1076                                      CMD_TEMPL_NULL_DATA, ptr, size, 0,
1077                                      wlvif->basic_rate);
1078
1079out:
1080        dev_kfree_skb(skb);
1081        if (ret)
1082                wl1271_warning("cmd buld null data failed %d", ret);
1083
1084        return ret;
1085
1086}
1087
1088int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1089                                   struct wl12xx_vif *wlvif)
1090{
1091        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1092        struct sk_buff *skb = NULL;
1093        int ret = -ENOMEM;
1094
1095        skb = ieee80211_nullfunc_get(wl->hw, vif);
1096        if (!skb)
1097                goto out;
1098
1099        ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
1100                                      skb->data, skb->len,
1101                                      wlvif->sta.klv_template_id,
1102                                      wlvif->basic_rate);
1103
1104out:
1105        dev_kfree_skb(skb);
1106        if (ret)
1107                wl1271_warning("cmd build klv null data failed %d", ret);
1108
1109        return ret;
1110
1111}
1112
1113int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1114                             u16 aid)
1115{
1116        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1117        struct sk_buff *skb;
1118        int ret = 0;
1119
1120        skb = ieee80211_pspoll_get(wl->hw, vif);
1121        if (!skb)
1122                goto out;
1123
1124        ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1125                                      CMD_TEMPL_PS_POLL, skb->data,
1126                                      skb->len, 0, wlvif->basic_rate_set);
1127
1128out:
1129        dev_kfree_skb(skb);
1130        return ret;
1131}
1132
1133int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1134                               u8 role_id, u8 band,
1135                               const u8 *ssid, size_t ssid_len,
1136                               const u8 *ie0, size_t ie0_len, const u8 *ie1,
1137                               size_t ie1_len, bool sched_scan)
1138{
1139        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1140        struct sk_buff *skb;
1141        int ret;
1142        u32 rate;
1143        u16 template_id_2_4 = wl->scan_templ_id_2_4;
1144        u16 template_id_5 = wl->scan_templ_id_5;
1145
1146        wl1271_debug(DEBUG_SCAN, "build probe request band %d", band);
1147
1148        skb = ieee80211_probereq_get(wl->hw, vif->addr, ssid, ssid_len,
1149                                     ie0_len + ie1_len);
1150        if (!skb) {
1151                ret = -ENOMEM;
1152                goto out;
1153        }
1154        if (ie0_len)
1155                memcpy(skb_put(skb, ie0_len), ie0, ie0_len);
1156        if (ie1_len)
1157                memcpy(skb_put(skb, ie1_len), ie1, ie1_len);
1158
1159        if (sched_scan &&
1160            (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
1161                template_id_2_4 = wl->sched_scan_templ_id_2_4;
1162                template_id_5 = wl->sched_scan_templ_id_5;
1163        }
1164
1165        rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1166        if (band == IEEE80211_BAND_2GHZ)
1167                ret = wl1271_cmd_template_set(wl, role_id,
1168                                              template_id_2_4,
1169                                              skb->data, skb->len, 0, rate);
1170        else
1171                ret = wl1271_cmd_template_set(wl, role_id,
1172                                              template_id_5,
1173                                              skb->data, skb->len, 0, rate);
1174
1175out:
1176        dev_kfree_skb(skb);
1177        return ret;
1178}
1179EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
1180
1181struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1182                                              struct wl12xx_vif *wlvif,
1183                                              struct sk_buff *skb)
1184{
1185        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1186        int ret;
1187        u32 rate;
1188
1189        if (!skb)
1190                skb = ieee80211_ap_probereq_get(wl->hw, vif);
1191        if (!skb)
1192                goto out;
1193
1194        wl1271_debug(DEBUG_SCAN, "set ap probe request template");
1195
1196        rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1197        if (wlvif->band == IEEE80211_BAND_2GHZ)
1198                ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1199                                              CMD_TEMPL_CFG_PROBE_REQ_2_4,
1200                                              skb->data, skb->len, 0, rate);
1201        else
1202                ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1203                                              CMD_TEMPL_CFG_PROBE_REQ_5,
1204                                              skb->data, skb->len, 0, rate);
1205
1206        if (ret < 0)
1207                wl1271_error("Unable to set ap probe request template.");
1208
1209out:
1210        return skb;
1211}
1212
1213int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1214{
1215        int ret, extra = 0;
1216        u16 fc;
1217        struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1218        struct sk_buff *skb;
1219        struct wl12xx_arp_rsp_template *tmpl;
1220        struct ieee80211_hdr_3addr *hdr;
1221        struct arphdr *arp_hdr;
1222
1223        skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1224                            WL1271_EXTRA_SPACE_MAX);
1225        if (!skb) {
1226                wl1271_error("failed to allocate buffer for arp rsp template");
1227                return -ENOMEM;
1228        }
1229
1230        skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1231
1232        tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
1233        memset(tmpl, 0, sizeof(*tmpl));
1234
1235        /* llc layer */
1236        memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1237        tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
1238
1239        /* arp header */
1240        arp_hdr = &tmpl->arp_hdr;
1241        arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1242        arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1243        arp_hdr->ar_hln = ETH_ALEN;
1244        arp_hdr->ar_pln = 4;
1245        arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1246
1247        /* arp payload */
1248        memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1249        tmpl->sender_ip = wlvif->ip_addr;
1250
1251        /* encryption space */
1252        switch (wlvif->encryption_type) {
1253        case KEY_TKIP:
1254                if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
1255                        extra = WL1271_EXTRA_SPACE_TKIP;
1256                break;
1257        case KEY_AES:
1258                extra = WL1271_EXTRA_SPACE_AES;
1259                break;
1260        case KEY_NONE:
1261        case KEY_WEP:
1262        case KEY_GEM:
1263                extra = 0;
1264                break;
1265        default:
1266                wl1271_warning("Unknown encryption type: %d",
1267                               wlvif->encryption_type);
1268                ret = -EINVAL;
1269                goto out;
1270        }
1271
1272        if (extra) {
1273                u8 *space = skb_push(skb, extra);
1274                memset(space, 0, extra);
1275        }
1276
1277        /* QoS header - BE */
1278        if (wlvif->sta.qos)
1279                memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1280
1281        /* mac80211 header */
1282        hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
1283        memset(hdr, 0, sizeof(*hdr));
1284        fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1285        if (wlvif->sta.qos)
1286                fc |= IEEE80211_STYPE_QOS_DATA;
1287        else
1288                fc |= IEEE80211_STYPE_DATA;
1289        if (wlvif->encryption_type != KEY_NONE)
1290                fc |= IEEE80211_FCTL_PROTECTED;
1291
1292        hdr->frame_control = cpu_to_le16(fc);
1293        memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1294        memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1295        memset(hdr->addr3, 0xff, ETH_ALEN);
1296
1297        ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
1298                                      skb->data, skb->len, 0,
1299                                      wlvif->basic_rate);
1300out:
1301        dev_kfree_skb(skb);
1302        return ret;
1303}
1304
1305int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1306{
1307        struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1308        struct ieee80211_qos_hdr template;
1309
1310        memset(&template, 0, sizeof(template));
1311
1312        memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1313        memcpy(template.addr2, vif->addr, ETH_ALEN);
1314        memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1315
1316        template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1317                                             IEEE80211_STYPE_QOS_NULLFUNC |
1318                                             IEEE80211_FCTL_TODS);
1319
1320        /* FIXME: not sure what priority to use here */
1321        template.qos_ctrl = cpu_to_le16(0);
1322
1323        return wl1271_cmd_template_set(wl, wlvif->role_id,
1324                                       CMD_TEMPL_QOS_NULL_DATA, &template,
1325                                       sizeof(template), 0,
1326                                       wlvif->basic_rate);
1327}
1328
1329int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1330{
1331        struct wl1271_cmd_set_keys *cmd;
1332        int ret = 0;
1333
1334        wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1335
1336        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1337        if (!cmd) {
1338                ret = -ENOMEM;
1339                goto out;
1340        }
1341
1342        cmd->hlid = hlid;
1343        cmd->key_id = id;
1344        cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1345        cmd->key_action = cpu_to_le16(KEY_SET_ID);
1346        cmd->key_type = KEY_WEP;
1347
1348        ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1349        if (ret < 0) {
1350                wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1351                goto out;
1352        }
1353
1354out:
1355        kfree(cmd);
1356
1357        return ret;
1358}
1359
1360int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1361                       u16 action, u8 id, u8 key_type,
1362                       u8 key_size, const u8 *key, const u8 *addr,
1363                       u32 tx_seq_32, u16 tx_seq_16)
1364{
1365        struct wl1271_cmd_set_keys *cmd;
1366        int ret = 0;
1367
1368        /* hlid might have already been deleted */
1369        if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1370                return 0;
1371
1372        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1373        if (!cmd) {
1374                ret = -ENOMEM;
1375                goto out;
1376        }
1377
1378        cmd->hlid = wlvif->sta.hlid;
1379
1380        if (key_type == KEY_WEP)
1381                cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1382        else if (is_broadcast_ether_addr(addr))
1383                cmd->lid_key_type = BROADCAST_LID_TYPE;
1384        else
1385                cmd->lid_key_type = UNICAST_LID_TYPE;
1386
1387        cmd->key_action = cpu_to_le16(action);
1388        cmd->key_size = key_size;
1389        cmd->key_type = key_type;
1390
1391        cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1392        cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1393
1394        cmd->key_id = id;
1395
1396        if (key_type == KEY_TKIP) {
1397                /*
1398                 * We get the key in the following form:
1399                 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1400                 * but the target is expecting:
1401                 * TKIP - RX MIC - TX MIC
1402                 */
1403                memcpy(cmd->key, key, 16);
1404                memcpy(cmd->key + 16, key + 24, 8);
1405                memcpy(cmd->key + 24, key + 16, 8);
1406
1407        } else {
1408                memcpy(cmd->key, key, key_size);
1409        }
1410
1411        wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1412
1413        ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1414        if (ret < 0) {
1415                wl1271_warning("could not set keys");
1416        goto out;
1417        }
1418
1419out:
1420        kfree(cmd);
1421
1422        return ret;
1423}
1424
1425/*
1426 * TODO: merge with sta/ibss into 1 set_key function.
1427 * note there are slight diffs
1428 */
1429int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1430                          u16 action, u8 id, u8 key_type,
1431                          u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1432                          u16 tx_seq_16)
1433{
1434        struct wl1271_cmd_set_keys *cmd;
1435        int ret = 0;
1436        u8 lid_type;
1437
1438        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1439        if (!cmd)
1440                return -ENOMEM;
1441
1442        if (hlid == wlvif->ap.bcast_hlid) {
1443                if (key_type == KEY_WEP)
1444                        lid_type = WEP_DEFAULT_LID_TYPE;
1445                else
1446                        lid_type = BROADCAST_LID_TYPE;
1447        } else {
1448                lid_type = UNICAST_LID_TYPE;
1449        }
1450
1451        wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1452                     " hlid: %d", (int)action, (int)id, (int)lid_type,
1453                     (int)key_type, (int)hlid);
1454
1455        cmd->lid_key_type = lid_type;
1456        cmd->hlid = hlid;
1457        cmd->key_action = cpu_to_le16(action);
1458        cmd->key_size = key_size;
1459        cmd->key_type = key_type;
1460        cmd->key_id = id;
1461        cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1462        cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1463
1464        if (key_type == KEY_TKIP) {
1465                /*
1466                 * We get the key in the following form:
1467                 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1468                 * but the target is expecting:
1469                 * TKIP - RX MIC - TX MIC
1470                 */
1471                memcpy(cmd->key, key, 16);
1472                memcpy(cmd->key + 16, key + 24, 8);
1473                memcpy(cmd->key + 24, key + 16, 8);
1474        } else {
1475                memcpy(cmd->key, key, key_size);
1476        }
1477
1478        wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1479
1480        ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1481        if (ret < 0) {
1482                wl1271_warning("could not set ap keys");
1483                goto out;
1484        }
1485
1486out:
1487        kfree(cmd);
1488        return ret;
1489}
1490
1491int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1492                              u8 hlid)
1493{
1494        struct wl12xx_cmd_set_peer_state *cmd;
1495        int ret = 0;
1496
1497        wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1498
1499        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1500        if (!cmd) {
1501                ret = -ENOMEM;
1502                goto out;
1503        }
1504
1505        cmd->hlid = hlid;
1506        cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1507
1508        /* wmm param is valid only for station role */
1509        if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1510                cmd->wmm = wlvif->wmm_enabled;
1511
1512        ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1513        if (ret < 0) {
1514                wl1271_error("failed to send set peer state command");
1515                goto out_free;
1516        }
1517
1518out_free:
1519        kfree(cmd);
1520
1521out:
1522        return ret;
1523}
1524
1525int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1526                        struct ieee80211_sta *sta, u8 hlid)
1527{
1528        struct wl12xx_cmd_add_peer *cmd;
1529        int i, ret;
1530        u32 sta_rates;
1531
1532        wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1533
1534        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1535        if (!cmd) {
1536                ret = -ENOMEM;
1537                goto out;
1538        }
1539
1540        memcpy(cmd->addr, sta->addr, ETH_ALEN);
1541        cmd->bss_index = WL1271_AP_BSS_INDEX;
1542        cmd->aid = sta->aid;
1543        cmd->hlid = hlid;
1544        cmd->sp_len = sta->max_sp;
1545        cmd->wmm = sta->wme ? 1 : 0;
1546        cmd->session_id = wl->session_ids[hlid];
1547        cmd->role_id = wlvif->role_id;
1548
1549        for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1550                if (sta->wme && (sta->uapsd_queues & BIT(i)))
1551                        cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1552                                        WL1271_PSD_UPSD_TRIGGER;
1553                else
1554                        cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1555                                        WL1271_PSD_LEGACY;
1556
1557
1558        sta_rates = sta->supp_rates[wlvif->band];
1559        if (sta->ht_cap.ht_supported)
1560                sta_rates |=
1561                        (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
1562                        (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
1563
1564        cmd->supported_rates =
1565                cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1566                                                        wlvif->band));
1567
1568        wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1569                     cmd->supported_rates, sta->uapsd_queues);
1570
1571        ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1572        if (ret < 0) {
1573                wl1271_error("failed to initiate cmd add peer");
1574                goto out_free;
1575        }
1576
1577out_free:
1578        kfree(cmd);
1579
1580out:
1581        return ret;
1582}
1583
1584int wl12xx_cmd_remove_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1585                           u8 hlid)
1586{
1587        struct wl12xx_cmd_remove_peer *cmd;
1588        int ret;
1589        bool timeout = false;
1590
1591        wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1592
1593        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1594        if (!cmd) {
1595                ret = -ENOMEM;
1596                goto out;
1597        }
1598
1599        cmd->hlid = hlid;
1600        /* We never send a deauth, mac80211 is in charge of this */
1601        cmd->reason_opcode = 0;
1602        cmd->send_deauth_flag = 0;
1603        cmd->role_id = wlvif->role_id;
1604
1605        ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1606        if (ret < 0) {
1607                wl1271_error("failed to initiate cmd remove peer");
1608                goto out_free;
1609        }
1610
1611        ret = wl->ops->wait_for_event(wl,
1612                                      WLCORE_EVENT_PEER_REMOVE_COMPLETE,
1613                                      &timeout);
1614
1615        /*
1616         * We are ok with a timeout here. The event is sometimes not sent
1617         * due to a firmware bug. In case of another error (like SDIO timeout)
1618         * queue a recovery.
1619         */
1620        if (ret)
1621                wl12xx_queue_recovery_work(wl);
1622
1623out_free:
1624        kfree(cmd);
1625
1626out:
1627        return ret;
1628}
1629
1630static int wlcore_get_reg_conf_ch_idx(enum ieee80211_band band, u16 ch)
1631{
1632        /*
1633         * map the given band/channel to the respective predefined
1634         * bit expected by the fw
1635         */
1636        switch (band) {
1637        case IEEE80211_BAND_2GHZ:
1638                /* channels 1..14 are mapped to 0..13 */
1639                if (ch >= 1 && ch <= 14)
1640                        return ch - 1;
1641                break;
1642        case IEEE80211_BAND_5GHZ:
1643                switch (ch) {
1644                case 8 ... 16:
1645                        /* channels 8,12,16 are mapped to 18,19,20 */
1646                        return 18 + (ch-8)/4;
1647                case 34 ... 48:
1648                        /* channels 34,36..48 are mapped to 21..28 */
1649                        return 21 + (ch-34)/2;
1650                case 52 ... 64:
1651                        /* channels 52,56..64 are mapped to 29..32 */
1652                        return 29 + (ch-52)/4;
1653                case 100 ... 140:
1654                        /* channels 100,104..140 are mapped to 33..43 */
1655                        return 33 + (ch-100)/4;
1656                case 149 ... 165:
1657                        /* channels 149,153..165 are mapped to 44..48 */
1658                        return 44 + (ch-149)/4;
1659                default:
1660                        break;
1661                }
1662                break;
1663        default:
1664                break;
1665        }
1666
1667        wl1271_error("%s: unknown band/channel: %d/%d", __func__, band, ch);
1668        return -1;
1669}
1670
1671void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
1672                                     enum ieee80211_band band)
1673{
1674        int ch_bit_idx = 0;
1675
1676        if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1677                return;
1678
1679        ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
1680
1681        if (ch_bit_idx >= 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
1682                set_bit(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
1683}
1684
1685int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
1686{
1687        struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
1688        int ret = 0, i, b, ch_bit_idx;
1689        struct ieee80211_channel *channel;
1690        u32 tmp_ch_bitmap[2];
1691        u16 ch;
1692        struct wiphy *wiphy = wl->hw->wiphy;
1693        struct ieee80211_supported_band *band;
1694        bool timeout = false;
1695
1696        if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1697                return 0;
1698
1699        wl1271_debug(DEBUG_CMD, "cmd reg domain config");
1700
1701        memset(tmp_ch_bitmap, 0, sizeof(tmp_ch_bitmap));
1702
1703        for (b = IEEE80211_BAND_2GHZ; b <= IEEE80211_BAND_5GHZ; b++) {
1704                band = wiphy->bands[b];
1705                for (i = 0; i < band->n_channels; i++) {
1706                        channel = &band->channels[i];
1707                        ch = channel->hw_value;
1708
1709                        if (channel->flags & (IEEE80211_CHAN_DISABLED |
1710                                              IEEE80211_CHAN_RADAR |
1711                                              IEEE80211_CHAN_NO_IR))
1712                                continue;
1713
1714                        ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
1715                        if (ch_bit_idx < 0)
1716                                continue;
1717
1718                        set_bit(ch_bit_idx, (long *)tmp_ch_bitmap);
1719                }
1720        }
1721
1722        tmp_ch_bitmap[0] |= wl->reg_ch_conf_pending[0];
1723        tmp_ch_bitmap[1] |= wl->reg_ch_conf_pending[1];
1724
1725        if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
1726                goto out;
1727
1728        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1729        if (!cmd) {
1730                ret = -ENOMEM;
1731                goto out;
1732        }
1733
1734        cmd->ch_bit_map1 = cpu_to_le32(tmp_ch_bitmap[0]);
1735        cmd->ch_bit_map2 = cpu_to_le32(tmp_ch_bitmap[1]);
1736
1737        wl1271_debug(DEBUG_CMD,
1738                     "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
1739                     cmd->ch_bit_map1, cmd->ch_bit_map2);
1740
1741        ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
1742        if (ret < 0) {
1743                wl1271_error("failed to send reg domain dfs config");
1744                goto out;
1745        }
1746
1747        ret = wl->ops->wait_for_event(wl,
1748                                      WLCORE_EVENT_DFS_CONFIG_COMPLETE,
1749                                      &timeout);
1750        if (ret < 0 || timeout) {
1751                wl1271_error("reg domain conf %serror",
1752                             timeout ? "completion " : "");
1753                ret = timeout ? -ETIMEDOUT : ret;
1754                goto out;
1755        }
1756
1757        memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
1758        memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
1759
1760out:
1761        kfree(cmd);
1762        return ret;
1763}
1764
1765int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1766{
1767        struct wl12xx_cmd_config_fwlog *cmd;
1768        int ret = 0;
1769
1770        wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1771
1772        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1773        if (!cmd) {
1774                ret = -ENOMEM;
1775                goto out;
1776        }
1777
1778        cmd->logger_mode = wl->conf.fwlog.mode;
1779        cmd->log_severity = wl->conf.fwlog.severity;
1780        cmd->timestamp = wl->conf.fwlog.timestamp;
1781        cmd->output = wl->conf.fwlog.output;
1782        cmd->threshold = wl->conf.fwlog.threshold;
1783
1784        ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1785        if (ret < 0) {
1786                wl1271_error("failed to send config firmware logger command");
1787                goto out_free;
1788        }
1789
1790out_free:
1791        kfree(cmd);
1792
1793out:
1794        return ret;
1795}
1796
1797int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1798{
1799        struct wl12xx_cmd_start_fwlog *cmd;
1800        int ret = 0;
1801
1802        wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1803
1804        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1805        if (!cmd) {
1806                ret = -ENOMEM;
1807                goto out;
1808        }
1809
1810        ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1811        if (ret < 0) {
1812                wl1271_error("failed to send start firmware logger command");
1813                goto out_free;
1814        }
1815
1816out_free:
1817        kfree(cmd);
1818
1819out:
1820        return ret;
1821}
1822
1823int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1824{
1825        struct wl12xx_cmd_stop_fwlog *cmd;
1826        int ret = 0;
1827
1828        wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1829
1830        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1831        if (!cmd) {
1832                ret = -ENOMEM;
1833                goto out;
1834        }
1835
1836        ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1837        if (ret < 0) {
1838                wl1271_error("failed to send stop firmware logger command");
1839                goto out_free;
1840        }
1841
1842out_free:
1843        kfree(cmd);
1844
1845out:
1846        return ret;
1847}
1848
1849static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1850                          u8 role_id, enum ieee80211_band band, u8 channel)
1851{
1852        struct wl12xx_cmd_roc *cmd;
1853        int ret = 0;
1854
1855        wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
1856
1857        if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1858                return -EINVAL;
1859
1860        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1861        if (!cmd) {
1862                ret = -ENOMEM;
1863                goto out;
1864        }
1865
1866        cmd->role_id = role_id;
1867        cmd->channel = channel;
1868        switch (band) {
1869        case IEEE80211_BAND_2GHZ:
1870                cmd->band = WLCORE_BAND_2_4GHZ;
1871                break;
1872        case IEEE80211_BAND_5GHZ:
1873                cmd->band = WLCORE_BAND_5GHZ;
1874                break;
1875        default:
1876                wl1271_error("roc - unknown band: %d", (int)wlvif->band);
1877                ret = -EINVAL;
1878                goto out_free;
1879        }
1880
1881
1882        ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1883        if (ret < 0) {
1884                wl1271_error("failed to send ROC command");
1885                goto out_free;
1886        }
1887
1888out_free:
1889        kfree(cmd);
1890
1891out:
1892        return ret;
1893}
1894
1895static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1896{
1897        struct wl12xx_cmd_croc *cmd;
1898        int ret = 0;
1899
1900        wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1901
1902        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1903        if (!cmd) {
1904                ret = -ENOMEM;
1905                goto out;
1906        }
1907        cmd->role_id = role_id;
1908
1909        ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1910                              sizeof(*cmd), 0);
1911        if (ret < 0) {
1912                wl1271_error("failed to send ROC command");
1913                goto out_free;
1914        }
1915
1916out_free:
1917        kfree(cmd);
1918
1919out:
1920        return ret;
1921}
1922
1923int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
1924               enum ieee80211_band band, u8 channel)
1925{
1926        int ret = 0;
1927
1928        if (WARN_ON(test_bit(role_id, wl->roc_map)))
1929                return 0;
1930
1931        ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
1932        if (ret < 0)
1933                goto out;
1934
1935        __set_bit(role_id, wl->roc_map);
1936out:
1937        return ret;
1938}
1939
1940int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1941{
1942        int ret = 0;
1943
1944        if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1945                return 0;
1946
1947        ret = wl12xx_cmd_croc(wl, role_id);
1948        if (ret < 0)
1949                goto out;
1950
1951        __clear_bit(role_id, wl->roc_map);
1952
1953        /*
1954         * Rearm the tx watchdog when removing the last ROC. This prevents
1955         * recoveries due to just finished ROCs - when Tx hasn't yet had
1956         * a chance to get out.
1957         */
1958        if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1959                wl12xx_rearm_tx_watchdog_locked(wl);
1960out:
1961        return ret;
1962}
1963
1964int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1965{
1966        struct wl12xx_cmd_stop_channel_switch *cmd;
1967        int ret;
1968
1969        wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1970
1971        cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1972        if (!cmd) {
1973                ret = -ENOMEM;
1974                goto out;
1975        }
1976
1977        cmd->role_id = wlvif->role_id;
1978
1979        ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1980        if (ret < 0) {
1981                wl1271_error("failed to stop channel switch command");
1982                goto out_free;
1983        }
1984
1985out_free:
1986        kfree(cmd);
1987
1988out:
1989        return ret;
1990}
1991
1992/* start dev role and roc on its channel */
1993int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1994                     enum ieee80211_band band, int channel)
1995{
1996        int ret;
1997
1998        if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1999                      wlvif->bss_type == BSS_TYPE_IBSS)))
2000                return -EINVAL;
2001
2002        ret = wl12xx_cmd_role_enable(wl,
2003                                     wl12xx_wlvif_to_vif(wlvif)->addr,
2004                                     WL1271_ROLE_DEVICE,
2005                                     &wlvif->dev_role_id);
2006        if (ret < 0)
2007                goto out;
2008
2009        ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
2010        if (ret < 0)
2011                goto out_disable;
2012
2013        ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
2014        if (ret < 0)
2015                goto out_stop;
2016
2017        return 0;
2018
2019out_stop:
2020        wl12xx_cmd_role_stop_dev(wl, wlvif);
2021out_disable:
2022        wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2023out:
2024        return ret;
2025}
2026
2027/* croc dev hlid, and stop the role */
2028int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2029{
2030        int ret;
2031
2032        if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
2033                      wlvif->bss_type == BSS_TYPE_IBSS)))
2034                return -EINVAL;
2035
2036        /* flush all pending packets */
2037        ret = wlcore_tx_work_locked(wl);
2038        if (ret < 0)
2039                goto out;
2040
2041        if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
2042                ret = wl12xx_croc(wl, wlvif->dev_role_id);
2043                if (ret < 0)
2044                        goto out;
2045        }
2046
2047        ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
2048        if (ret < 0)
2049                goto out;
2050
2051        ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2052        if (ret < 0)
2053                goto out;
2054
2055out:
2056        return ret;
2057}
2058