Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Implementation of the host-to-chip commands (aka request/confirmation) of the
0004  * hardware API.
0005  *
0006  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
0007  * Copyright (c) 2010, ST-Ericsson
0008  */
0009 #include <linux/etherdevice.h>
0010 
0011 #include "hif_tx.h"
0012 #include "wfx.h"
0013 #include "bh.h"
0014 #include "hwio.h"
0015 #include "debug.h"
0016 #include "sta.h"
0017 
0018 void wfx_init_hif_cmd(struct wfx_hif_cmd *hif_cmd)
0019 {
0020     init_completion(&hif_cmd->ready);
0021     init_completion(&hif_cmd->done);
0022     mutex_init(&hif_cmd->lock);
0023 }
0024 
0025 static void wfx_fill_header(struct wfx_hif_msg *hif, int if_id, unsigned int cmd, size_t size)
0026 {
0027     if (if_id == -1)
0028         if_id = 2;
0029 
0030     WARN(cmd > 0x3f, "invalid hardware command %#.2x", cmd);
0031     WARN(size > 0xFFF, "requested buffer is too large: %zu bytes", size);
0032     WARN(if_id > 0x3, "invalid interface ID %d", if_id);
0033 
0034     hif->len = cpu_to_le16(size + 4);
0035     hif->id = cmd;
0036     hif->interface = if_id;
0037 }
0038 
0039 static void *wfx_alloc_hif(size_t body_len, struct wfx_hif_msg **hif)
0040 {
0041     *hif = kzalloc(sizeof(struct wfx_hif_msg) + body_len, GFP_KERNEL);
0042     if (*hif)
0043         return (*hif)->body;
0044     else
0045         return NULL;
0046 }
0047 
0048 int wfx_cmd_send(struct wfx_dev *wdev, struct wfx_hif_msg *request,
0049          void *reply, size_t reply_len, bool no_reply)
0050 {
0051     const char *mib_name = "";
0052     const char *mib_sep = "";
0053     int cmd = request->id;
0054     int vif = request->interface;
0055     int ret;
0056 
0057     /* Do not wait for any reply if chip is frozen */
0058     if (wdev->chip_frozen)
0059         return -ETIMEDOUT;
0060 
0061     mutex_lock(&wdev->hif_cmd.lock);
0062     WARN(wdev->hif_cmd.buf_send, "data locking error");
0063 
0064     /* Note: call to complete() below has an implicit memory barrier that hopefully protect
0065      * buf_send
0066      */
0067     wdev->hif_cmd.buf_send = request;
0068     wdev->hif_cmd.buf_recv = reply;
0069     wdev->hif_cmd.len_recv = reply_len;
0070     complete(&wdev->hif_cmd.ready);
0071 
0072     wfx_bh_request_tx(wdev);
0073 
0074     if (no_reply) {
0075         /* Chip won't reply. Ensure the wq has send the buffer before to continue. */
0076         flush_workqueue(wdev->bh_wq);
0077         ret = 0;
0078         goto end;
0079     }
0080 
0081     if (wdev->poll_irq)
0082         wfx_bh_poll_irq(wdev);
0083 
0084     ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 1 * HZ);
0085     if (!ret) {
0086         dev_err(wdev->dev, "chip is abnormally long to answer\n");
0087         reinit_completion(&wdev->hif_cmd.ready);
0088         ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 3 * HZ);
0089     }
0090     if (!ret) {
0091         dev_err(wdev->dev, "chip did not answer\n");
0092         wfx_pending_dump_old_frames(wdev, 3000);
0093         wdev->chip_frozen = true;
0094         reinit_completion(&wdev->hif_cmd.done);
0095         ret = -ETIMEDOUT;
0096     } else {
0097         ret = wdev->hif_cmd.ret;
0098     }
0099 
0100 end:
0101     wdev->hif_cmd.buf_send = NULL;
0102     mutex_unlock(&wdev->hif_cmd.lock);
0103 
0104     if (ret &&
0105         (cmd == HIF_REQ_ID_READ_MIB || cmd == HIF_REQ_ID_WRITE_MIB)) {
0106         mib_name = wfx_get_mib_name(((u16 *)request)[2]);
0107         mib_sep = "/";
0108     }
0109     if (ret < 0)
0110         dev_err(wdev->dev, "hardware request %s%s%s (%#.2x) on vif %d returned error %d\n",
0111             wfx_get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
0112     if (ret > 0)
0113         dev_warn(wdev->dev, "hardware request %s%s%s (%#.2x) on vif %d returned status %d\n",
0114              wfx_get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
0115 
0116     return ret;
0117 }
0118 
0119 /* This function is special. After HIF_REQ_ID_SHUT_DOWN, chip won't reply to any request anymore.
0120  * Obviously, only call this function during device unregister.
0121  */
0122 int wfx_hif_shutdown(struct wfx_dev *wdev)
0123 {
0124     int ret;
0125     struct wfx_hif_msg *hif;
0126 
0127     wfx_alloc_hif(0, &hif);
0128     if (!hif)
0129         return -ENOMEM;
0130     wfx_fill_header(hif, -1, HIF_REQ_ID_SHUT_DOWN, 0);
0131     ret = wfx_cmd_send(wdev, hif, NULL, 0, true);
0132     if (wdev->pdata.gpio_wakeup)
0133         gpiod_set_value(wdev->pdata.gpio_wakeup, 0);
0134     else
0135         wfx_control_reg_write(wdev, 0);
0136     kfree(hif);
0137     return ret;
0138 }
0139 
0140 int wfx_hif_configuration(struct wfx_dev *wdev, const u8 *conf, size_t len)
0141 {
0142     int ret;
0143     size_t buf_len = sizeof(struct wfx_hif_req_configuration) + len;
0144     struct wfx_hif_msg *hif;
0145     struct wfx_hif_req_configuration *body = wfx_alloc_hif(buf_len, &hif);
0146 
0147     if (!hif)
0148         return -ENOMEM;
0149     body->length = cpu_to_le16(len);
0150     memcpy(body->pds_data, conf, len);
0151     wfx_fill_header(hif, -1, HIF_REQ_ID_CONFIGURATION, buf_len);
0152     ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
0153     kfree(hif);
0154     return ret;
0155 }
0156 
0157 int wfx_hif_reset(struct wfx_vif *wvif, bool reset_stat)
0158 {
0159     int ret;
0160     struct wfx_hif_msg *hif;
0161     struct wfx_hif_req_reset *body = wfx_alloc_hif(sizeof(*body), &hif);
0162 
0163     if (!hif)
0164         return -ENOMEM;
0165     body->reset_stat = reset_stat;
0166     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_RESET, sizeof(*body));
0167     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0168     kfree(hif);
0169     return ret;
0170 }
0171 
0172 int wfx_hif_read_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val, size_t val_len)
0173 {
0174     int ret;
0175     struct wfx_hif_msg *hif;
0176     int buf_len = sizeof(struct wfx_hif_cnf_read_mib) + val_len;
0177     struct wfx_hif_req_read_mib *body = wfx_alloc_hif(sizeof(*body), &hif);
0178     struct wfx_hif_cnf_read_mib *reply = kmalloc(buf_len, GFP_KERNEL);
0179 
0180     if (!body || !reply) {
0181         ret = -ENOMEM;
0182         goto out;
0183     }
0184     body->mib_id = cpu_to_le16(mib_id);
0185     wfx_fill_header(hif, vif_id, HIF_REQ_ID_READ_MIB, sizeof(*body));
0186     ret = wfx_cmd_send(wdev, hif, reply, buf_len, false);
0187 
0188     if (!ret && mib_id != le16_to_cpu(reply->mib_id)) {
0189         dev_warn(wdev->dev, "%s: confirmation mismatch request\n", __func__);
0190         ret = -EIO;
0191     }
0192     if (ret == -ENOMEM)
0193         dev_err(wdev->dev, "buffer is too small to receive %s (%zu < %d)\n",
0194             wfx_get_mib_name(mib_id), val_len, le16_to_cpu(reply->length));
0195     if (!ret)
0196         memcpy(val, &reply->mib_data, le16_to_cpu(reply->length));
0197     else
0198         memset(val, 0xFF, val_len);
0199 out:
0200     kfree(hif);
0201     kfree(reply);
0202     return ret;
0203 }
0204 
0205 int wfx_hif_write_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val, size_t val_len)
0206 {
0207     int ret;
0208     struct wfx_hif_msg *hif;
0209     int buf_len = sizeof(struct wfx_hif_req_write_mib) + val_len;
0210     struct wfx_hif_req_write_mib *body = wfx_alloc_hif(buf_len, &hif);
0211 
0212     if (!hif)
0213         return -ENOMEM;
0214     body->mib_id = cpu_to_le16(mib_id);
0215     body->length = cpu_to_le16(val_len);
0216     memcpy(&body->mib_data, val, val_len);
0217     wfx_fill_header(hif, vif_id, HIF_REQ_ID_WRITE_MIB, buf_len);
0218     ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
0219     kfree(hif);
0220     return ret;
0221 }
0222 
0223 int wfx_hif_scan(struct wfx_vif *wvif, struct cfg80211_scan_request *req,
0224          int chan_start_idx, int chan_num)
0225 {
0226     int ret, i;
0227     struct wfx_hif_msg *hif;
0228     size_t buf_len = sizeof(struct wfx_hif_req_start_scan_alt) + chan_num * sizeof(u8);
0229     struct wfx_hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif);
0230 
0231     WARN(chan_num > HIF_API_MAX_NB_CHANNELS, "invalid params");
0232     WARN(req->n_ssids > HIF_API_MAX_NB_SSIDS, "invalid params");
0233 
0234     if (!hif)
0235         return -ENOMEM;
0236     for (i = 0; i < req->n_ssids; i++) {
0237         memcpy(body->ssid_def[i].ssid, req->ssids[i].ssid, IEEE80211_MAX_SSID_LEN);
0238         body->ssid_def[i].ssid_length = cpu_to_le32(req->ssids[i].ssid_len);
0239     }
0240     body->num_of_ssids = HIF_API_MAX_NB_SSIDS;
0241     body->maintain_current_bss = 1;
0242     body->disallow_ps = 1;
0243     body->tx_power_level = cpu_to_le32(req->channels[chan_start_idx]->max_power);
0244     body->num_of_channels = chan_num;
0245     for (i = 0; i < chan_num; i++)
0246         body->channel_list[i] = req->channels[i + chan_start_idx]->hw_value;
0247     if (req->no_cck)
0248         body->max_transmit_rate = API_RATE_INDEX_G_6MBPS;
0249     else
0250         body->max_transmit_rate = API_RATE_INDEX_B_1MBPS;
0251     if (req->channels[chan_start_idx]->flags & IEEE80211_CHAN_NO_IR) {
0252         body->min_channel_time = cpu_to_le32(50);
0253         body->max_channel_time = cpu_to_le32(150);
0254     } else {
0255         body->min_channel_time = cpu_to_le32(10);
0256         body->max_channel_time = cpu_to_le32(50);
0257         body->num_of_probe_requests = 2;
0258         body->probe_delay = 100;
0259     }
0260 
0261     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START_SCAN, buf_len);
0262     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0263     kfree(hif);
0264     return ret;
0265 }
0266 
0267 int wfx_hif_stop_scan(struct wfx_vif *wvif)
0268 {
0269     int ret;
0270     struct wfx_hif_msg *hif;
0271     /* body associated to HIF_REQ_ID_STOP_SCAN is empty */
0272     wfx_alloc_hif(0, &hif);
0273 
0274     if (!hif)
0275         return -ENOMEM;
0276     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_STOP_SCAN, 0);
0277     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0278     kfree(hif);
0279     return ret;
0280 }
0281 
0282 int wfx_hif_join(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
0283          struct ieee80211_channel *channel, const u8 *ssid, int ssid_len)
0284 {
0285     struct ieee80211_vif *vif = container_of(conf, struct ieee80211_vif,
0286                          bss_conf);
0287     int ret;
0288     struct wfx_hif_msg *hif;
0289     struct wfx_hif_req_join *body = wfx_alloc_hif(sizeof(*body), &hif);
0290 
0291     WARN_ON(!conf->beacon_int);
0292     WARN_ON(!conf->basic_rates);
0293     WARN_ON(sizeof(body->ssid) < ssid_len);
0294     WARN(!vif->cfg.ibss_joined && !ssid_len, "joining an unknown BSS");
0295     if (!hif)
0296         return -ENOMEM;
0297     body->infrastructure_bss_mode = !vif->cfg.ibss_joined;
0298     body->short_preamble = conf->use_short_preamble;
0299     body->probe_for_join = !(channel->flags & IEEE80211_CHAN_NO_IR);
0300     body->channel_number = channel->hw_value;
0301     body->beacon_interval = cpu_to_le32(conf->beacon_int);
0302     body->basic_rate_set = cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
0303     memcpy(body->bssid, conf->bssid, sizeof(body->bssid));
0304     if (ssid) {
0305         body->ssid_length = cpu_to_le32(ssid_len);
0306         memcpy(body->ssid, ssid, ssid_len);
0307     }
0308     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_JOIN, sizeof(*body));
0309     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0310     kfree(hif);
0311     return ret;
0312 }
0313 
0314 int wfx_hif_set_bss_params(struct wfx_vif *wvif, int aid, int beacon_lost_count)
0315 {
0316     int ret;
0317     struct wfx_hif_msg *hif;
0318     struct wfx_hif_req_set_bss_params *body = wfx_alloc_hif(sizeof(*body), &hif);
0319 
0320     if (!hif)
0321         return -ENOMEM;
0322     body->aid = cpu_to_le16(aid);
0323     body->beacon_lost_count = beacon_lost_count;
0324     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_BSS_PARAMS, sizeof(*body));
0325     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0326     kfree(hif);
0327     return ret;
0328 }
0329 
0330 int wfx_hif_add_key(struct wfx_dev *wdev, const struct wfx_hif_req_add_key *arg)
0331 {
0332     int ret;
0333     struct wfx_hif_msg *hif;
0334     /* FIXME: only send necessary bits */
0335     struct wfx_hif_req_add_key *body = wfx_alloc_hif(sizeof(*body), &hif);
0336 
0337     if (!hif)
0338         return -ENOMEM;
0339     /* FIXME: swap bytes as necessary in body */
0340     memcpy(body, arg, sizeof(*body));
0341     if (wfx_api_older_than(wdev, 1, 5))
0342         /* Legacy firmwares expect that add_key to be sent on right interface. */
0343         wfx_fill_header(hif, arg->int_id, HIF_REQ_ID_ADD_KEY, sizeof(*body));
0344     else
0345         wfx_fill_header(hif, -1, HIF_REQ_ID_ADD_KEY, sizeof(*body));
0346     ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
0347     kfree(hif);
0348     return ret;
0349 }
0350 
0351 int wfx_hif_remove_key(struct wfx_dev *wdev, int idx)
0352 {
0353     int ret;
0354     struct wfx_hif_msg *hif;
0355     struct wfx_hif_req_remove_key *body = wfx_alloc_hif(sizeof(*body), &hif);
0356 
0357     if (!hif)
0358         return -ENOMEM;
0359     body->entry_index = idx;
0360     wfx_fill_header(hif, -1, HIF_REQ_ID_REMOVE_KEY, sizeof(*body));
0361     ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
0362     kfree(hif);
0363     return ret;
0364 }
0365 
0366 int wfx_hif_set_edca_queue_params(struct wfx_vif *wvif, u16 queue,
0367                   const struct ieee80211_tx_queue_params *arg)
0368 {
0369     int ret;
0370     struct wfx_hif_msg *hif;
0371     struct wfx_hif_req_edca_queue_params *body = wfx_alloc_hif(sizeof(*body), &hif);
0372 
0373     if (!body)
0374         return -ENOMEM;
0375 
0376     WARN_ON(arg->aifs > 255);
0377     if (!hif)
0378         return -ENOMEM;
0379     body->aifsn = arg->aifs;
0380     body->cw_min = cpu_to_le16(arg->cw_min);
0381     body->cw_max = cpu_to_le16(arg->cw_max);
0382     body->tx_op_limit = cpu_to_le16(arg->txop * USEC_PER_TXOP);
0383     body->queue_id = 3 - queue;
0384     /* API 2.0 has changed queue IDs values */
0385     if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BE)
0386         body->queue_id = HIF_QUEUE_ID_BACKGROUND;
0387     if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BK)
0388         body->queue_id = HIF_QUEUE_ID_BESTEFFORT;
0389     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_EDCA_QUEUE_PARAMS, sizeof(*body));
0390     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0391     kfree(hif);
0392     return ret;
0393 }
0394 
0395 int wfx_hif_set_pm(struct wfx_vif *wvif, bool ps, int dynamic_ps_timeout)
0396 {
0397     int ret;
0398     struct wfx_hif_msg *hif;
0399     struct wfx_hif_req_set_pm_mode *body = wfx_alloc_hif(sizeof(*body), &hif);
0400 
0401     if (!body)
0402         return -ENOMEM;
0403 
0404     if (!hif)
0405         return -ENOMEM;
0406     if (ps) {
0407         body->enter_psm = 1;
0408         /* Firmware does not support more than 128ms */
0409         body->fast_psm_idle_period = min(dynamic_ps_timeout * 2, 255);
0410         if (body->fast_psm_idle_period)
0411             body->fast_psm = 1;
0412     }
0413     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_PM_MODE, sizeof(*body));
0414     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0415     kfree(hif);
0416     return ret;
0417 }
0418 
0419 int wfx_hif_start(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
0420           const struct ieee80211_channel *channel)
0421 {
0422         struct ieee80211_vif *vif = container_of(conf, struct ieee80211_vif,
0423                          bss_conf);
0424     int ret;
0425     struct wfx_hif_msg *hif;
0426     struct wfx_hif_req_start *body = wfx_alloc_hif(sizeof(*body), &hif);
0427 
0428     WARN_ON(!conf->beacon_int);
0429     if (!hif)
0430         return -ENOMEM;
0431     body->dtim_period = conf->dtim_period;
0432     body->short_preamble = conf->use_short_preamble;
0433     body->channel_number = channel->hw_value;
0434     body->beacon_interval = cpu_to_le32(conf->beacon_int);
0435     body->basic_rate_set = cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
0436     body->ssid_length = vif->cfg.ssid_len;
0437     memcpy(body->ssid, vif->cfg.ssid, vif->cfg.ssid_len);
0438     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START, sizeof(*body));
0439     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0440     kfree(hif);
0441     return ret;
0442 }
0443 
0444 int wfx_hif_beacon_transmit(struct wfx_vif *wvif, bool enable)
0445 {
0446     int ret;
0447     struct wfx_hif_msg *hif;
0448     struct wfx_hif_req_beacon_transmit *body = wfx_alloc_hif(sizeof(*body), &hif);
0449 
0450     if (!hif)
0451         return -ENOMEM;
0452     body->enable_beaconing = enable ? 1 : 0;
0453     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_BEACON_TRANSMIT, sizeof(*body));
0454     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0455     kfree(hif);
0456     return ret;
0457 }
0458 
0459 int wfx_hif_map_link(struct wfx_vif *wvif, bool unmap, u8 *mac_addr, int sta_id, bool mfp)
0460 {
0461     int ret;
0462     struct wfx_hif_msg *hif;
0463     struct wfx_hif_req_map_link *body = wfx_alloc_hif(sizeof(*body), &hif);
0464 
0465     if (!hif)
0466         return -ENOMEM;
0467     if (mac_addr)
0468         ether_addr_copy(body->mac_addr, mac_addr);
0469     body->mfpc = mfp ? 1 : 0;
0470     body->unmap = unmap ? 1 : 0;
0471     body->peer_sta_id = sta_id;
0472     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_MAP_LINK, sizeof(*body));
0473     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0474     kfree(hif);
0475     return ret;
0476 }
0477 
0478 int wfx_hif_update_ie_beacon(struct wfx_vif *wvif, const u8 *ies, size_t ies_len)
0479 {
0480     int ret;
0481     struct wfx_hif_msg *hif;
0482     int buf_len = sizeof(struct wfx_hif_req_update_ie) + ies_len;
0483     struct wfx_hif_req_update_ie *body = wfx_alloc_hif(buf_len, &hif);
0484 
0485     if (!hif)
0486         return -ENOMEM;
0487     body->beacon = 1;
0488     body->num_ies = cpu_to_le16(1);
0489     memcpy(body->ie, ies, ies_len);
0490     wfx_fill_header(hif, wvif->id, HIF_REQ_ID_UPDATE_IE, buf_len);
0491     ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
0492     kfree(hif);
0493     return ret;
0494 }