1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Implementation of the host-to-chip commands (aka request/confirmation) of the 4 * hardware API. 5 * 6 * Copyright (c) 2017-2020, Silicon Laboratories, Inc. 7 * Copyright (c) 2010, ST-Ericsson 8 */ 9 #include <linux/etherdevice.h> 10 11 #include "hif_tx.h" 12 #include "wfx.h" 13 #include "bh.h" 14 #include "hwio.h" 15 #include "debug.h" 16 #include "sta.h" 17 18 void wfx_init_hif_cmd(struct wfx_hif_cmd *hif_cmd) 19 { 20 init_completion(&hif_cmd->ready); 21 init_completion(&hif_cmd->done); 22 mutex_init(&hif_cmd->lock); 23 } 24 25 static void wfx_fill_header(struct wfx_hif_msg *hif, int if_id, unsigned int cmd, size_t size) 26 { 27 if (if_id == -1) 28 if_id = 2; 29 30 WARN(cmd > 0x3f, "invalid hardware command %#.2x", cmd); 31 WARN(size > 0xFFF, "requested buffer is too large: %zu bytes", size); 32 WARN(if_id > 0x3, "invalid interface ID %d", if_id); 33 34 hif->len = cpu_to_le16(size + 4); 35 hif->id = cmd; 36 hif->interface = if_id; 37 } 38 39 static void *wfx_alloc_hif(size_t body_len, struct wfx_hif_msg **hif) 40 { 41 *hif = kzalloc(sizeof(struct wfx_hif_msg) + body_len, GFP_KERNEL); 42 if (*hif) 43 return (*hif)->body; 44 else 45 return NULL; 46 } 47 48 static u32 wfx_rate_mask_to_hw(struct wfx_dev *wdev, u32 rates) 49 { 50 int i; 51 u32 ret = 0; 52 /* The device only supports 2GHz */ 53 struct ieee80211_supported_band *sband = wdev->hw->wiphy->bands[NL80211_BAND_2GHZ]; 54 55 for (i = 0; i < sband->n_bitrates; i++) { 56 if (rates & BIT(i)) { 57 if (i >= sband->n_bitrates) 58 dev_warn(wdev->dev, "unsupported basic rate\n"); 59 else 60 ret |= BIT(sband->bitrates[i].hw_value); 61 } 62 } 63 return ret; 64 } 65 66 int wfx_cmd_send(struct wfx_dev *wdev, struct wfx_hif_msg *request, 67 void *reply, size_t reply_len, bool no_reply) 68 { 69 const char *mib_name = ""; 70 const char *mib_sep = ""; 71 int cmd = request->id; 72 int vif = request->interface; 73 int ret; 74 75 /* Do not wait for any reply if chip is frozen */ 76 if (wdev->chip_frozen) 77 return -ETIMEDOUT; 78 79 mutex_lock(&wdev->hif_cmd.lock); 80 WARN(wdev->hif_cmd.buf_send, "data locking error"); 81 82 /* Note: call to complete() below has an implicit memory barrier that hopefully protect 83 * buf_send 84 */ 85 wdev->hif_cmd.buf_send = request; 86 wdev->hif_cmd.buf_recv = reply; 87 wdev->hif_cmd.len_recv = reply_len; 88 complete(&wdev->hif_cmd.ready); 89 90 wfx_bh_request_tx(wdev); 91 92 if (no_reply) { 93 /* Chip won't reply. Ensure the wq has send the buffer before to continue. */ 94 flush_workqueue(wdev->bh_wq); 95 ret = 0; 96 goto end; 97 } 98 99 if (wdev->poll_irq) 100 wfx_bh_poll_irq(wdev); 101 102 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 1 * HZ); 103 if (!ret) { 104 dev_err(wdev->dev, "chip is abnormally long to answer\n"); 105 reinit_completion(&wdev->hif_cmd.ready); 106 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 3 * HZ); 107 } 108 if (!ret) { 109 dev_err(wdev->dev, "chip did not answer\n"); 110 wfx_pending_dump_old_frames(wdev, 3000); 111 wdev->chip_frozen = true; 112 reinit_completion(&wdev->hif_cmd.done); 113 ret = -ETIMEDOUT; 114 } else { 115 ret = wdev->hif_cmd.ret; 116 } 117 118 end: 119 wdev->hif_cmd.buf_send = NULL; 120 mutex_unlock(&wdev->hif_cmd.lock); 121 122 if (ret && 123 (cmd == HIF_REQ_ID_READ_MIB || cmd == HIF_REQ_ID_WRITE_MIB)) { 124 mib_name = wfx_get_mib_name(((u16 *)request)[2]); 125 mib_sep = "/"; 126 } 127 if (ret < 0) 128 dev_err(wdev->dev, "hardware request %s%s%s (%#.2x) on vif %d returned error %d\n", 129 wfx_get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret); 130 if (ret > 0) 131 dev_warn(wdev->dev, "hardware request %s%s%s (%#.2x) on vif %d returned status %d\n", 132 wfx_get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret); 133 134 return ret; 135 } 136 137 /* This function is special. After HIF_REQ_ID_SHUT_DOWN, chip won't reply to any request anymore. 138 * Obviously, only call this function during device unregister. 139 */ 140 int wfx_hif_shutdown(struct wfx_dev *wdev) 141 { 142 int ret; 143 struct wfx_hif_msg *hif; 144 145 wfx_alloc_hif(0, &hif); 146 if (!hif) 147 return -ENOMEM; 148 wfx_fill_header(hif, -1, HIF_REQ_ID_SHUT_DOWN, 0); 149 ret = wfx_cmd_send(wdev, hif, NULL, 0, true); 150 if (wdev->pdata.gpio_wakeup) 151 gpiod_set_value(wdev->pdata.gpio_wakeup, 0); 152 else 153 wfx_control_reg_write(wdev, 0); 154 kfree(hif); 155 return ret; 156 } 157 158 int wfx_hif_configuration(struct wfx_dev *wdev, const u8 *conf, size_t len) 159 { 160 int ret; 161 size_t buf_len = sizeof(struct wfx_hif_req_configuration) + len; 162 struct wfx_hif_msg *hif; 163 struct wfx_hif_req_configuration *body = wfx_alloc_hif(buf_len, &hif); 164 165 if (!hif) 166 return -ENOMEM; 167 body->length = cpu_to_le16(len); 168 memcpy(body->pds_data, conf, len); 169 wfx_fill_header(hif, -1, HIF_REQ_ID_CONFIGURATION, buf_len); 170 ret = wfx_cmd_send(wdev, hif, NULL, 0, false); 171 kfree(hif); 172 return ret; 173 } 174 175 int wfx_hif_reset(struct wfx_vif *wvif, bool reset_stat) 176 { 177 int ret; 178 struct wfx_hif_msg *hif; 179 struct wfx_hif_req_reset *body = wfx_alloc_hif(sizeof(*body), &hif); 180 181 if (!hif) 182 return -ENOMEM; 183 body->reset_stat = reset_stat; 184 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_RESET, sizeof(*body)); 185 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 186 kfree(hif); 187 return ret; 188 } 189 190 int wfx_hif_read_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val, size_t val_len) 191 { 192 int ret; 193 struct wfx_hif_msg *hif; 194 int buf_len = sizeof(struct wfx_hif_cnf_read_mib) + val_len; 195 struct wfx_hif_req_read_mib *body = wfx_alloc_hif(sizeof(*body), &hif); 196 struct wfx_hif_cnf_read_mib *reply = kmalloc(buf_len, GFP_KERNEL); 197 198 if (!body || !reply) { 199 ret = -ENOMEM; 200 goto out; 201 } 202 body->mib_id = cpu_to_le16(mib_id); 203 wfx_fill_header(hif, vif_id, HIF_REQ_ID_READ_MIB, sizeof(*body)); 204 ret = wfx_cmd_send(wdev, hif, reply, buf_len, false); 205 206 if (!ret && mib_id != le16_to_cpu(reply->mib_id)) { 207 dev_warn(wdev->dev, "%s: confirmation mismatch request\n", __func__); 208 ret = -EIO; 209 } 210 if (ret == -ENOMEM) 211 dev_err(wdev->dev, "buffer is too small to receive %s (%zu < %d)\n", 212 wfx_get_mib_name(mib_id), val_len, le16_to_cpu(reply->length)); 213 if (!ret) 214 memcpy(val, &reply->mib_data, le16_to_cpu(reply->length)); 215 else 216 memset(val, 0xFF, val_len); 217 out: 218 kfree(hif); 219 kfree(reply); 220 return ret; 221 } 222 223 int wfx_hif_write_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val, size_t val_len) 224 { 225 int ret; 226 struct wfx_hif_msg *hif; 227 int buf_len = sizeof(struct wfx_hif_req_write_mib) + val_len; 228 struct wfx_hif_req_write_mib *body = wfx_alloc_hif(buf_len, &hif); 229 230 if (!hif) 231 return -ENOMEM; 232 body->mib_id = cpu_to_le16(mib_id); 233 body->length = cpu_to_le16(val_len); 234 memcpy(&body->mib_data, val, val_len); 235 wfx_fill_header(hif, vif_id, HIF_REQ_ID_WRITE_MIB, buf_len); 236 ret = wfx_cmd_send(wdev, hif, NULL, 0, false); 237 kfree(hif); 238 return ret; 239 } 240 241 /* Hijack scan request to implement Remain-On-Channel */ 242 int wfx_hif_scan_uniq(struct wfx_vif *wvif, struct ieee80211_channel *chan, int duration) 243 { 244 int ret; 245 struct wfx_hif_msg *hif; 246 size_t buf_len = sizeof(struct wfx_hif_req_start_scan_alt) + sizeof(u8); 247 struct wfx_hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif); 248 249 if (!hif) 250 return -ENOMEM; 251 body->num_of_ssids = HIF_API_MAX_NB_SSIDS; 252 body->maintain_current_bss = 1; 253 body->disallow_ps = 1; 254 body->tx_power_level = cpu_to_le32(chan->max_power); 255 body->num_of_channels = 1; 256 body->channel_list[0] = chan->hw_value; 257 body->max_transmit_rate = API_RATE_INDEX_B_1MBPS; 258 body->min_channel_time = cpu_to_le32(duration); 259 body->max_channel_time = cpu_to_le32(duration * 110 / 100); 260 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START_SCAN, buf_len); 261 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 262 kfree(hif); 263 return ret; 264 } 265 266 int wfx_hif_scan(struct wfx_vif *wvif, struct cfg80211_scan_request *req, 267 int chan_start_idx, int chan_num) 268 { 269 int ret, i; 270 struct wfx_hif_msg *hif; 271 size_t buf_len = sizeof(struct wfx_hif_req_start_scan_alt) + chan_num * sizeof(u8); 272 struct wfx_hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif); 273 274 WARN(chan_num > HIF_API_MAX_NB_CHANNELS, "invalid params"); 275 WARN(req->n_ssids > HIF_API_MAX_NB_SSIDS, "invalid params"); 276 277 if (!hif) 278 return -ENOMEM; 279 for (i = 0; i < req->n_ssids; i++) { 280 memcpy(body->ssid_def[i].ssid, req->ssids[i].ssid, IEEE80211_MAX_SSID_LEN); 281 body->ssid_def[i].ssid_length = cpu_to_le32(req->ssids[i].ssid_len); 282 } 283 body->num_of_ssids = HIF_API_MAX_NB_SSIDS; 284 body->maintain_current_bss = 1; 285 body->disallow_ps = 1; 286 body->tx_power_level = cpu_to_le32(req->channels[chan_start_idx]->max_power); 287 body->num_of_channels = chan_num; 288 for (i = 0; i < chan_num; i++) 289 body->channel_list[i] = req->channels[i + chan_start_idx]->hw_value; 290 if (req->no_cck) 291 body->max_transmit_rate = API_RATE_INDEX_G_6MBPS; 292 else 293 body->max_transmit_rate = API_RATE_INDEX_B_1MBPS; 294 if (req->channels[chan_start_idx]->flags & IEEE80211_CHAN_NO_IR) { 295 body->min_channel_time = cpu_to_le32(50); 296 body->max_channel_time = cpu_to_le32(150); 297 } else { 298 body->min_channel_time = cpu_to_le32(10); 299 body->max_channel_time = cpu_to_le32(50); 300 body->num_of_probe_requests = 2; 301 body->probe_delay = 100; 302 } 303 304 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START_SCAN, buf_len); 305 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 306 kfree(hif); 307 return ret; 308 } 309 310 int wfx_hif_stop_scan(struct wfx_vif *wvif) 311 { 312 int ret; 313 struct wfx_hif_msg *hif; 314 /* body associated to HIF_REQ_ID_STOP_SCAN is empty */ 315 wfx_alloc_hif(0, &hif); 316 317 if (!hif) 318 return -ENOMEM; 319 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_STOP_SCAN, 0); 320 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 321 kfree(hif); 322 return ret; 323 } 324 325 int wfx_hif_join(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf, 326 struct ieee80211_channel *channel, const u8 *ssid, int ssid_len) 327 { 328 struct ieee80211_vif *vif = container_of(conf, struct ieee80211_vif, 329 bss_conf); 330 int ret; 331 struct wfx_hif_msg *hif; 332 struct wfx_hif_req_join *body = wfx_alloc_hif(sizeof(*body), &hif); 333 334 WARN_ON(!conf->beacon_int); 335 WARN_ON(!conf->basic_rates); 336 WARN_ON(sizeof(body->ssid) < ssid_len); 337 WARN(!vif->cfg.ibss_joined && !ssid_len, "joining an unknown BSS"); 338 if (!hif) 339 return -ENOMEM; 340 body->infrastructure_bss_mode = !vif->cfg.ibss_joined; 341 body->short_preamble = conf->use_short_preamble; 342 body->probe_for_join = !(channel->flags & IEEE80211_CHAN_NO_IR); 343 body->channel_number = channel->hw_value; 344 body->beacon_interval = cpu_to_le32(conf->beacon_int); 345 body->basic_rate_set = cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates)); 346 memcpy(body->bssid, conf->bssid, sizeof(body->bssid)); 347 if (ssid) { 348 body->ssid_length = cpu_to_le32(ssid_len); 349 memcpy(body->ssid, ssid, ssid_len); 350 } 351 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_JOIN, sizeof(*body)); 352 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 353 kfree(hif); 354 return ret; 355 } 356 357 int wfx_hif_set_bss_params(struct wfx_vif *wvif, int aid, int beacon_lost_count) 358 { 359 int ret; 360 struct wfx_hif_msg *hif; 361 struct wfx_hif_req_set_bss_params *body = wfx_alloc_hif(sizeof(*body), &hif); 362 363 if (!hif) 364 return -ENOMEM; 365 body->aid = cpu_to_le16(aid); 366 body->beacon_lost_count = beacon_lost_count; 367 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_BSS_PARAMS, sizeof(*body)); 368 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 369 kfree(hif); 370 return ret; 371 } 372 373 int wfx_hif_add_key(struct wfx_dev *wdev, const struct wfx_hif_req_add_key *arg) 374 { 375 int ret; 376 struct wfx_hif_msg *hif; 377 /* FIXME: only send necessary bits */ 378 struct wfx_hif_req_add_key *body = wfx_alloc_hif(sizeof(*body), &hif); 379 380 if (!hif) 381 return -ENOMEM; 382 /* FIXME: swap bytes as necessary in body */ 383 memcpy(body, arg, sizeof(*body)); 384 if (wfx_api_older_than(wdev, 1, 5)) 385 /* Legacy firmwares expect that add_key to be sent on right interface. */ 386 wfx_fill_header(hif, arg->int_id, HIF_REQ_ID_ADD_KEY, sizeof(*body)); 387 else 388 wfx_fill_header(hif, -1, HIF_REQ_ID_ADD_KEY, sizeof(*body)); 389 ret = wfx_cmd_send(wdev, hif, NULL, 0, false); 390 kfree(hif); 391 return ret; 392 } 393 394 int wfx_hif_remove_key(struct wfx_dev *wdev, int idx) 395 { 396 int ret; 397 struct wfx_hif_msg *hif; 398 struct wfx_hif_req_remove_key *body = wfx_alloc_hif(sizeof(*body), &hif); 399 400 if (!hif) 401 return -ENOMEM; 402 body->entry_index = idx; 403 wfx_fill_header(hif, -1, HIF_REQ_ID_REMOVE_KEY, sizeof(*body)); 404 ret = wfx_cmd_send(wdev, hif, NULL, 0, false); 405 kfree(hif); 406 return ret; 407 } 408 409 int wfx_hif_set_edca_queue_params(struct wfx_vif *wvif, u16 queue, 410 const struct ieee80211_tx_queue_params *arg) 411 { 412 int ret; 413 struct wfx_hif_msg *hif; 414 struct wfx_hif_req_edca_queue_params *body = wfx_alloc_hif(sizeof(*body), &hif); 415 416 if (!body) 417 return -ENOMEM; 418 419 WARN_ON(arg->aifs > 255); 420 if (!hif) 421 return -ENOMEM; 422 body->aifsn = arg->aifs; 423 body->cw_min = cpu_to_le16(arg->cw_min); 424 body->cw_max = cpu_to_le16(arg->cw_max); 425 body->tx_op_limit = cpu_to_le16(arg->txop * USEC_PER_TXOP); 426 body->queue_id = 3 - queue; 427 /* API 2.0 has changed queue IDs values */ 428 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BE) 429 body->queue_id = HIF_QUEUE_ID_BACKGROUND; 430 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BK) 431 body->queue_id = HIF_QUEUE_ID_BESTEFFORT; 432 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_EDCA_QUEUE_PARAMS, sizeof(*body)); 433 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 434 kfree(hif); 435 return ret; 436 } 437 438 int wfx_hif_set_pm(struct wfx_vif *wvif, bool ps, int dynamic_ps_timeout) 439 { 440 int ret; 441 struct wfx_hif_msg *hif; 442 struct wfx_hif_req_set_pm_mode *body = wfx_alloc_hif(sizeof(*body), &hif); 443 444 if (!body) 445 return -ENOMEM; 446 447 if (!hif) 448 return -ENOMEM; 449 if (ps) { 450 body->enter_psm = 1; 451 /* Firmware does not support more than 128ms */ 452 body->fast_psm_idle_period = min(dynamic_ps_timeout * 2, 255); 453 if (body->fast_psm_idle_period) 454 body->fast_psm = 1; 455 } 456 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_PM_MODE, sizeof(*body)); 457 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 458 kfree(hif); 459 return ret; 460 } 461 462 int wfx_hif_start(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf, 463 const struct ieee80211_channel *channel) 464 { 465 struct ieee80211_vif *vif = container_of(conf, struct ieee80211_vif, 466 bss_conf); 467 int ret; 468 struct wfx_hif_msg *hif; 469 struct wfx_hif_req_start *body = wfx_alloc_hif(sizeof(*body), &hif); 470 471 WARN_ON(!conf->beacon_int); 472 if (!hif) 473 return -ENOMEM; 474 body->dtim_period = conf->dtim_period; 475 body->short_preamble = conf->use_short_preamble; 476 body->channel_number = channel->hw_value; 477 body->beacon_interval = cpu_to_le32(conf->beacon_int); 478 body->basic_rate_set = cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates)); 479 body->ssid_length = vif->cfg.ssid_len; 480 memcpy(body->ssid, vif->cfg.ssid, vif->cfg.ssid_len); 481 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START, sizeof(*body)); 482 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 483 kfree(hif); 484 return ret; 485 } 486 487 int wfx_hif_beacon_transmit(struct wfx_vif *wvif, bool enable) 488 { 489 int ret; 490 struct wfx_hif_msg *hif; 491 struct wfx_hif_req_beacon_transmit *body = wfx_alloc_hif(sizeof(*body), &hif); 492 493 if (!hif) 494 return -ENOMEM; 495 body->enable_beaconing = enable ? 1 : 0; 496 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_BEACON_TRANSMIT, sizeof(*body)); 497 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 498 kfree(hif); 499 return ret; 500 } 501 502 int wfx_hif_map_link(struct wfx_vif *wvif, bool unmap, u8 *mac_addr, int sta_id, bool mfp) 503 { 504 int ret; 505 struct wfx_hif_msg *hif; 506 struct wfx_hif_req_map_link *body = wfx_alloc_hif(sizeof(*body), &hif); 507 508 if (!hif) 509 return -ENOMEM; 510 if (mac_addr) 511 ether_addr_copy(body->mac_addr, mac_addr); 512 body->mfpc = mfp ? 1 : 0; 513 body->unmap = unmap ? 1 : 0; 514 body->peer_sta_id = sta_id; 515 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_MAP_LINK, sizeof(*body)); 516 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 517 kfree(hif); 518 return ret; 519 } 520 521 int wfx_hif_update_ie_beacon(struct wfx_vif *wvif, const u8 *ies, size_t ies_len) 522 { 523 int ret; 524 struct wfx_hif_msg *hif; 525 int buf_len = sizeof(struct wfx_hif_req_update_ie) + ies_len; 526 struct wfx_hif_req_update_ie *body = wfx_alloc_hif(buf_len, &hif); 527 528 if (!hif) 529 return -ENOMEM; 530 body->beacon = 1; 531 body->num_ies = cpu_to_le16(1); 532 memcpy(body->ie, ies, ies_len); 533 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_UPDATE_IE, buf_len); 534 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false); 535 kfree(hif); 536 return ret; 537 } 538