linux/drivers/net/wireless/st/cw1200/scan.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Scan implementation for ST-Ericsson CW1200 mac80211 drivers
   4 *
   5 * Copyright (c) 2010, ST-Ericsson
   6 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
   7 */
   8
   9#include <linux/sched.h>
  10#include "cw1200.h"
  11#include "scan.h"
  12#include "sta.h"
  13#include "pm.h"
  14
  15static void cw1200_scan_restart_delayed(struct cw1200_common *priv);
  16
  17static int cw1200_scan_start(struct cw1200_common *priv, struct wsm_scan *scan)
  18{
  19        int ret, i;
  20        int tmo = 2000;
  21
  22        switch (priv->join_status) {
  23        case CW1200_JOIN_STATUS_PRE_STA:
  24        case CW1200_JOIN_STATUS_JOINING:
  25                return -EBUSY;
  26        default:
  27                break;
  28        }
  29
  30        wiphy_dbg(priv->hw->wiphy, "[SCAN] hw req, type %d, %d channels, flags: 0x%x.\n",
  31                  scan->type, scan->num_channels, scan->flags);
  32
  33        for (i = 0; i < scan->num_channels; ++i)
  34                tmo += scan->ch[i].max_chan_time + 10;
  35
  36        cancel_delayed_work_sync(&priv->clear_recent_scan_work);
  37        atomic_set(&priv->scan.in_progress, 1);
  38        atomic_set(&priv->recent_scan, 1);
  39        cw1200_pm_stay_awake(&priv->pm_state, msecs_to_jiffies(tmo));
  40        queue_delayed_work(priv->workqueue, &priv->scan.timeout,
  41                           msecs_to_jiffies(tmo));
  42        ret = wsm_scan(priv, scan);
  43        if (ret) {
  44                atomic_set(&priv->scan.in_progress, 0);
  45                cancel_delayed_work_sync(&priv->scan.timeout);
  46                cw1200_scan_restart_delayed(priv);
  47        }
  48        return ret;
  49}
  50
  51int cw1200_hw_scan(struct ieee80211_hw *hw,
  52                   struct ieee80211_vif *vif,
  53                   struct ieee80211_scan_request *hw_req)
  54{
  55        struct cw1200_common *priv = hw->priv;
  56        struct cfg80211_scan_request *req = &hw_req->req;
  57        struct wsm_template_frame frame = {
  58                .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
  59        };
  60        int i, ret;
  61
  62        if (!priv->vif)
  63                return -EINVAL;
  64
  65        /* Scan when P2P_GO corrupt firmware MiniAP mode */
  66        if (priv->join_status == CW1200_JOIN_STATUS_AP)
  67                return -EOPNOTSUPP;
  68
  69        if (req->n_ssids == 1 && !req->ssids[0].ssid_len)
  70                req->n_ssids = 0;
  71
  72        wiphy_dbg(hw->wiphy, "[SCAN] Scan request for %d SSIDs.\n",
  73                  req->n_ssids);
  74
  75        if (req->n_ssids > WSM_SCAN_MAX_NUM_OF_SSIDS)
  76                return -EINVAL;
  77
  78        frame.skb = ieee80211_probereq_get(hw, priv->vif->addr, NULL, 0,
  79                req->ie_len);
  80        if (!frame.skb)
  81                return -ENOMEM;
  82
  83        if (req->ie_len)
  84                skb_put_data(frame.skb, req->ie, req->ie_len);
  85
  86        /* will be unlocked in cw1200_scan_work() */
  87        down(&priv->scan.lock);
  88        mutex_lock(&priv->conf_mutex);
  89
  90        ret = wsm_set_template_frame(priv, &frame);
  91        if (!ret) {
  92                /* Host want to be the probe responder. */
  93                ret = wsm_set_probe_responder(priv, true);
  94        }
  95        if (ret) {
  96                mutex_unlock(&priv->conf_mutex);
  97                up(&priv->scan.lock);
  98                dev_kfree_skb(frame.skb);
  99                return ret;
 100        }
 101
 102        wsm_lock_tx(priv);
 103
 104        BUG_ON(priv->scan.req);
 105        priv->scan.req = req;
 106        priv->scan.n_ssids = 0;
 107        priv->scan.status = 0;
 108        priv->scan.begin = &req->channels[0];
 109        priv->scan.curr = priv->scan.begin;
 110        priv->scan.end = &req->channels[req->n_channels];
 111        priv->scan.output_power = priv->output_power;
 112
 113        for (i = 0; i < req->n_ssids; ++i) {
 114                struct wsm_ssid *dst = &priv->scan.ssids[priv->scan.n_ssids];
 115                memcpy(&dst->ssid[0], req->ssids[i].ssid, sizeof(dst->ssid));
 116                dst->length = req->ssids[i].ssid_len;
 117                ++priv->scan.n_ssids;
 118        }
 119
 120        mutex_unlock(&priv->conf_mutex);
 121
 122        if (frame.skb)
 123                dev_kfree_skb(frame.skb);
 124        queue_work(priv->workqueue, &priv->scan.work);
 125        return 0;
 126}
 127
 128void cw1200_scan_work(struct work_struct *work)
 129{
 130        struct cw1200_common *priv = container_of(work, struct cw1200_common,
 131                                                        scan.work);
 132        struct ieee80211_channel **it;
 133        struct wsm_scan scan = {
 134                .type = WSM_SCAN_TYPE_FOREGROUND,
 135                .flags = WSM_SCAN_FLAG_SPLIT_METHOD,
 136        };
 137        bool first_run = (priv->scan.begin == priv->scan.curr &&
 138                          priv->scan.begin != priv->scan.end);
 139        int i;
 140
 141        if (first_run) {
 142                /* Firmware gets crazy if scan request is sent
 143                 * when STA is joined but not yet associated.
 144                 * Force unjoin in this case.
 145                 */
 146                if (cancel_delayed_work_sync(&priv->join_timeout) > 0)
 147                        cw1200_join_timeout(&priv->join_timeout.work);
 148        }
 149
 150        mutex_lock(&priv->conf_mutex);
 151
 152        if (first_run) {
 153                if (priv->join_status == CW1200_JOIN_STATUS_STA &&
 154                    !(priv->powersave_mode.mode & WSM_PSM_PS)) {
 155                        struct wsm_set_pm pm = priv->powersave_mode;
 156                        pm.mode = WSM_PSM_PS;
 157                        cw1200_set_pm(priv, &pm);
 158                } else if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
 159                        /* FW bug: driver has to restart p2p-dev mode
 160                         * after scan
 161                         */
 162                        cw1200_disable_listening(priv);
 163                }
 164        }
 165
 166        if (!priv->scan.req || (priv->scan.curr == priv->scan.end)) {
 167                struct cfg80211_scan_info info = {
 168                        .aborted = priv->scan.status ? 1 : 0,
 169                };
 170
 171                if (priv->scan.output_power != priv->output_power)
 172                        wsm_set_output_power(priv, priv->output_power * 10);
 173                if (priv->join_status == CW1200_JOIN_STATUS_STA &&
 174                    !(priv->powersave_mode.mode & WSM_PSM_PS))
 175                        cw1200_set_pm(priv, &priv->powersave_mode);
 176
 177                if (priv->scan.status < 0)
 178                        wiphy_warn(priv->hw->wiphy,
 179                                   "[SCAN] Scan failed (%d).\n",
 180                                   priv->scan.status);
 181                else if (priv->scan.req)
 182                        wiphy_dbg(priv->hw->wiphy,
 183                                  "[SCAN] Scan completed.\n");
 184                else
 185                        wiphy_dbg(priv->hw->wiphy,
 186                                  "[SCAN] Scan canceled.\n");
 187
 188                priv->scan.req = NULL;
 189                cw1200_scan_restart_delayed(priv);
 190                wsm_unlock_tx(priv);
 191                mutex_unlock(&priv->conf_mutex);
 192                ieee80211_scan_completed(priv->hw, &info);
 193                up(&priv->scan.lock);
 194                return;
 195        } else {
 196                struct ieee80211_channel *first = *priv->scan.curr;
 197                for (it = priv->scan.curr + 1, i = 1;
 198                     it != priv->scan.end && i < WSM_SCAN_MAX_NUM_OF_CHANNELS;
 199                     ++it, ++i) {
 200                        if ((*it)->band != first->band)
 201                                break;
 202                        if (((*it)->flags ^ first->flags) &
 203                                        IEEE80211_CHAN_NO_IR)
 204                                break;
 205                        if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
 206                            (*it)->max_power != first->max_power)
 207                                break;
 208                }
 209                scan.band = first->band;
 210
 211                if (priv->scan.req->no_cck)
 212                        scan.max_tx_rate = WSM_TRANSMIT_RATE_6;
 213                else
 214                        scan.max_tx_rate = WSM_TRANSMIT_RATE_1;
 215                scan.num_probes =
 216                        (first->flags & IEEE80211_CHAN_NO_IR) ? 0 : 2;
 217                scan.num_ssids = priv->scan.n_ssids;
 218                scan.ssids = &priv->scan.ssids[0];
 219                scan.num_channels = it - priv->scan.curr;
 220                /* TODO: Is it optimal? */
 221                scan.probe_delay = 100;
 222                /* It is not stated in WSM specification, however
 223                 * FW team says that driver may not use FG scan
 224                 * when joined.
 225                 */
 226                if (priv->join_status == CW1200_JOIN_STATUS_STA) {
 227                        scan.type = WSM_SCAN_TYPE_BACKGROUND;
 228                        scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
 229                }
 230                scan.ch = kcalloc(it - priv->scan.curr,
 231                                  sizeof(struct wsm_scan_ch),
 232                                  GFP_KERNEL);
 233                if (!scan.ch) {
 234                        priv->scan.status = -ENOMEM;
 235                        goto fail;
 236                }
 237                for (i = 0; i < scan.num_channels; ++i) {
 238                        scan.ch[i].number = priv->scan.curr[i]->hw_value;
 239                        if (priv->scan.curr[i]->flags & IEEE80211_CHAN_NO_IR) {
 240                                scan.ch[i].min_chan_time = 50;
 241                                scan.ch[i].max_chan_time = 100;
 242                        } else {
 243                                scan.ch[i].min_chan_time = 10;
 244                                scan.ch[i].max_chan_time = 25;
 245                        }
 246                }
 247                if (!(first->flags & IEEE80211_CHAN_NO_IR) &&
 248                    priv->scan.output_power != first->max_power) {
 249                        priv->scan.output_power = first->max_power;
 250                        wsm_set_output_power(priv,
 251                                             priv->scan.output_power * 10);
 252                }
 253                priv->scan.status = cw1200_scan_start(priv, &scan);
 254                kfree(scan.ch);
 255                if (priv->scan.status)
 256                        goto fail;
 257                priv->scan.curr = it;
 258        }
 259        mutex_unlock(&priv->conf_mutex);
 260        return;
 261
 262fail:
 263        priv->scan.curr = priv->scan.end;
 264        mutex_unlock(&priv->conf_mutex);
 265        queue_work(priv->workqueue, &priv->scan.work);
 266        return;
 267}
 268
 269static void cw1200_scan_restart_delayed(struct cw1200_common *priv)
 270{
 271        /* FW bug: driver has to restart p2p-dev mode after scan. */
 272        if (priv->join_status == CW1200_JOIN_STATUS_MONITOR) {
 273                cw1200_enable_listening(priv);
 274                cw1200_update_filtering(priv);
 275        }
 276
 277        if (priv->delayed_unjoin) {
 278                priv->delayed_unjoin = false;
 279                if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
 280                        wsm_unlock_tx(priv);
 281        } else if (priv->delayed_link_loss) {
 282                        wiphy_dbg(priv->hw->wiphy, "[CQM] Requeue BSS loss.\n");
 283                        priv->delayed_link_loss = 0;
 284                        cw1200_cqm_bssloss_sm(priv, 1, 0, 0);
 285        }
 286}
 287
 288static void cw1200_scan_complete(struct cw1200_common *priv)
 289{
 290        queue_delayed_work(priv->workqueue, &priv->clear_recent_scan_work, HZ);
 291        if (priv->scan.direct_probe) {
 292                wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe complete.\n");
 293                cw1200_scan_restart_delayed(priv);
 294                priv->scan.direct_probe = 0;
 295                up(&priv->scan.lock);
 296                wsm_unlock_tx(priv);
 297        } else {
 298                cw1200_scan_work(&priv->scan.work);
 299        }
 300}
 301
 302void cw1200_scan_failed_cb(struct cw1200_common *priv)
 303{
 304        if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
 305                /* STA is stopped. */
 306                return;
 307
 308        if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
 309                priv->scan.status = -EIO;
 310                queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
 311        }
 312}
 313
 314
 315void cw1200_scan_complete_cb(struct cw1200_common *priv,
 316                                struct wsm_scan_complete *arg)
 317{
 318        if (priv->mode == NL80211_IFTYPE_UNSPECIFIED)
 319                /* STA is stopped. */
 320                return;
 321
 322        if (cancel_delayed_work_sync(&priv->scan.timeout) > 0) {
 323                priv->scan.status = 1;
 324                queue_delayed_work(priv->workqueue, &priv->scan.timeout, 0);
 325        }
 326}
 327
 328void cw1200_clear_recent_scan_work(struct work_struct *work)
 329{
 330        struct cw1200_common *priv =
 331                container_of(work, struct cw1200_common,
 332                             clear_recent_scan_work.work);
 333        atomic_xchg(&priv->recent_scan, 0);
 334}
 335
 336void cw1200_scan_timeout(struct work_struct *work)
 337{
 338        struct cw1200_common *priv =
 339                container_of(work, struct cw1200_common, scan.timeout.work);
 340        if (atomic_xchg(&priv->scan.in_progress, 0)) {
 341                if (priv->scan.status > 0) {
 342                        priv->scan.status = 0;
 343                } else if (!priv->scan.status) {
 344                        wiphy_warn(priv->hw->wiphy,
 345                                   "Timeout waiting for scan complete notification.\n");
 346                        priv->scan.status = -ETIMEDOUT;
 347                        priv->scan.curr = priv->scan.end;
 348                        wsm_stop_scan(priv);
 349                }
 350                cw1200_scan_complete(priv);
 351        }
 352}
 353
 354void cw1200_probe_work(struct work_struct *work)
 355{
 356        struct cw1200_common *priv =
 357                container_of(work, struct cw1200_common, scan.probe_work.work);
 358        u8 queue_id = cw1200_queue_get_queue_id(priv->pending_frame_id);
 359        struct cw1200_queue *queue = &priv->tx_queue[queue_id];
 360        const struct cw1200_txpriv *txpriv;
 361        struct wsm_tx *wsm;
 362        struct wsm_template_frame frame = {
 363                .frame_type = WSM_FRAME_TYPE_PROBE_REQUEST,
 364        };
 365        struct wsm_ssid ssids[1] = {{
 366                .length = 0,
 367        } };
 368        struct wsm_scan_ch ch[1] = {{
 369                .min_chan_time = 0,
 370                .max_chan_time = 10,
 371        } };
 372        struct wsm_scan scan = {
 373                .type = WSM_SCAN_TYPE_FOREGROUND,
 374                .num_probes = 1,
 375                .probe_delay = 0,
 376                .num_channels = 1,
 377                .ssids = ssids,
 378                .ch = ch,
 379        };
 380        u8 *ies;
 381        size_t ies_len;
 382        int ret;
 383
 384        wiphy_dbg(priv->hw->wiphy, "[SCAN] Direct probe work.\n");
 385
 386        mutex_lock(&priv->conf_mutex);
 387        if (down_trylock(&priv->scan.lock)) {
 388                /* Scan is already in progress. Requeue self. */
 389                schedule();
 390                queue_delayed_work(priv->workqueue, &priv->scan.probe_work,
 391                                   msecs_to_jiffies(100));
 392                mutex_unlock(&priv->conf_mutex);
 393                return;
 394        }
 395
 396        /* Make sure we still have a pending probe req */
 397        if (cw1200_queue_get_skb(queue, priv->pending_frame_id,
 398                                 &frame.skb, &txpriv)) {
 399                up(&priv->scan.lock);
 400                mutex_unlock(&priv->conf_mutex);
 401                wsm_unlock_tx(priv);
 402                return;
 403        }
 404        wsm = (struct wsm_tx *)frame.skb->data;
 405        scan.max_tx_rate = wsm->max_tx_rate;
 406        scan.band = (priv->channel->band == NL80211_BAND_5GHZ) ?
 407                WSM_PHY_BAND_5G : WSM_PHY_BAND_2_4G;
 408        if (priv->join_status == CW1200_JOIN_STATUS_STA ||
 409            priv->join_status == CW1200_JOIN_STATUS_IBSS) {
 410                scan.type = WSM_SCAN_TYPE_BACKGROUND;
 411                scan.flags = WSM_SCAN_FLAG_FORCE_BACKGROUND;
 412        }
 413        ch[0].number = priv->channel->hw_value;
 414
 415        skb_pull(frame.skb, txpriv->offset);
 416
 417        ies = &frame.skb->data[sizeof(struct ieee80211_hdr_3addr)];
 418        ies_len = frame.skb->len - sizeof(struct ieee80211_hdr_3addr);
 419
 420        if (ies_len) {
 421                u8 *ssidie =
 422                        (u8 *)cfg80211_find_ie(WLAN_EID_SSID, ies, ies_len);
 423                if (ssidie && ssidie[1] && ssidie[1] <= sizeof(ssids[0].ssid)) {
 424                        u8 *nextie = &ssidie[2 + ssidie[1]];
 425                        /* Remove SSID from the IE list. It has to be provided
 426                         * as a separate argument in cw1200_scan_start call
 427                         */
 428
 429                        /* Store SSID localy */
 430                        ssids[0].length = ssidie[1];
 431                        memcpy(ssids[0].ssid, &ssidie[2], ssids[0].length);
 432                        scan.num_ssids = 1;
 433
 434                        /* Remove SSID from IE list */
 435                        ssidie[1] = 0;
 436                        memmove(&ssidie[2], nextie, &ies[ies_len] - nextie);
 437                        skb_trim(frame.skb, frame.skb->len - ssids[0].length);
 438                }
 439        }
 440
 441        /* FW bug: driver has to restart p2p-dev mode after scan */
 442        if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
 443                cw1200_disable_listening(priv);
 444        ret = wsm_set_template_frame(priv, &frame);
 445        priv->scan.direct_probe = 1;
 446        if (!ret) {
 447                wsm_flush_tx(priv);
 448                ret = cw1200_scan_start(priv, &scan);
 449        }
 450        mutex_unlock(&priv->conf_mutex);
 451
 452        skb_push(frame.skb, txpriv->offset);
 453        if (!ret)
 454                IEEE80211_SKB_CB(frame.skb)->flags |= IEEE80211_TX_STAT_ACK;
 455        BUG_ON(cw1200_queue_remove(queue, priv->pending_frame_id));
 456
 457        if (ret) {
 458                priv->scan.direct_probe = 0;
 459                up(&priv->scan.lock);
 460                wsm_unlock_tx(priv);
 461        }
 462
 463        return;
 464}
 465