1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of host-to-chip commands (aka request/confirmation) of WFxxx
6 * Copyright (c) 2017-2019, Silicon Laboratories, Inc.
7 * Copyright (c) 2010, ST-Ericsson
9 #include <linux/etherdevice.h>
18 void wfx_init_hif_cmd(struct wfx_hif_cmd *hif_cmd)
20 init_completion(&hif_cmd->ready);
21 init_completion(&hif_cmd->done);
22 mutex_init(&hif_cmd->lock);
23 mutex_init(&hif_cmd->key_renew_lock);
26 static void wfx_fill_header(struct hif_msg *hif, int if_id,
27 unsigned int cmd, size_t size)
32 WARN(cmd > 0x3f, "invalid WSM command %#.2x", cmd);
33 WARN(size > 0xFFF, "requested buffer is too large: %zu bytes", size);
34 WARN(if_id > 0x3, "invalid interface ID %d", if_id);
36 hif->len = cpu_to_le16(size + 4);
38 hif->interface = if_id;
41 static void *wfx_alloc_hif(size_t body_len, struct hif_msg **hif)
43 *hif = kzalloc(sizeof(struct hif_msg) + body_len, GFP_KERNEL);
50 int wfx_cmd_send(struct wfx_dev *wdev, struct hif_msg *request,
51 void *reply, size_t reply_len, bool async)
53 const char *mib_name = "";
54 const char *mib_sep = "";
55 int cmd = request->id;
56 int vif = request->interface;
59 WARN(wdev->hif_cmd.buf_recv && wdev->hif_cmd.async, "API usage error");
61 // Do not wait for any reply if chip is frozen
62 if (wdev->chip_frozen)
65 if (cmd != HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS)
66 mutex_lock(&wdev->hif_cmd.key_renew_lock);
68 mutex_lock(&wdev->hif_cmd.lock);
69 WARN(wdev->hif_cmd.buf_send, "data locking error");
71 // Note: call to complete() below has an implicit memory barrier that
72 // hopefully protect buf_send
73 wdev->hif_cmd.buf_send = request;
74 wdev->hif_cmd.buf_recv = reply;
75 wdev->hif_cmd.len_recv = reply_len;
76 wdev->hif_cmd.async = async;
77 complete(&wdev->hif_cmd.ready);
79 wfx_bh_request_tx(wdev);
81 // NOTE: no timeout is catched async is enabled
86 wfx_bh_poll_irq(wdev);
88 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 1 * HZ);
90 dev_err(wdev->dev, "chip is abnormally long to answer\n");
91 reinit_completion(&wdev->hif_cmd.ready);
92 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 3 * HZ);
95 dev_err(wdev->dev, "chip did not answer\n");
96 wfx_pending_dump_old_frames(wdev, 3000);
97 wdev->chip_frozen = true;
98 reinit_completion(&wdev->hif_cmd.done);
101 ret = wdev->hif_cmd.ret;
104 wdev->hif_cmd.buf_send = NULL;
105 mutex_unlock(&wdev->hif_cmd.lock);
108 (cmd == HIF_REQ_ID_READ_MIB || cmd == HIF_REQ_ID_WRITE_MIB)) {
109 mib_name = get_mib_name(((u16 *)request)[2]);
114 "WSM request %s%s%s (%#.2x) on vif %d returned error %d\n",
115 get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
118 "WSM request %s%s%s (%#.2x) on vif %d returned status %d\n",
119 get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
121 if (cmd != HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS)
122 mutex_unlock(&wdev->hif_cmd.key_renew_lock);
126 // This function is special. After HIF_REQ_ID_SHUT_DOWN, chip won't reply to any
127 // request anymore. We need to slightly hack struct wfx_hif_cmd for that job. Be
128 // carefull to only call this funcion during device unregister.
129 int hif_shutdown(struct wfx_dev *wdev)
134 if (wdev->chip_frozen)
136 wfx_alloc_hif(0, &hif);
139 wfx_fill_header(hif, -1, HIF_REQ_ID_SHUT_DOWN, 0);
140 ret = wfx_cmd_send(wdev, hif, NULL, 0, true);
141 // After this command, chip won't reply. Be sure to give enough time to
142 // bh to send buffer:
144 wdev->hif_cmd.buf_send = NULL;
145 if (wdev->pdata.gpio_wakeup)
146 gpiod_set_value(wdev->pdata.gpio_wakeup, 0);
148 control_reg_write(wdev, 0);
149 mutex_unlock(&wdev->hif_cmd.lock);
150 mutex_unlock(&wdev->hif_cmd.key_renew_lock);
155 int hif_configuration(struct wfx_dev *wdev, const u8 *conf, size_t len)
158 size_t buf_len = sizeof(struct hif_req_configuration) + len;
160 struct hif_req_configuration *body = wfx_alloc_hif(buf_len, &hif);
164 body->length = cpu_to_le16(len);
165 memcpy(body->pds_data, conf, len);
166 wfx_fill_header(hif, -1, HIF_REQ_ID_CONFIGURATION, buf_len);
167 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
172 int hif_reset(struct wfx_vif *wvif, bool reset_stat)
176 struct hif_req_reset *body = wfx_alloc_hif(sizeof(*body), &hif);
180 body->reset_flags.reset_stat = reset_stat;
181 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_RESET, sizeof(*body));
182 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
187 int hif_read_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id,
188 void *val, size_t val_len)
192 int buf_len = sizeof(struct hif_cnf_read_mib) + val_len;
193 struct hif_req_read_mib *body = wfx_alloc_hif(sizeof(*body), &hif);
194 struct hif_cnf_read_mib *reply = kmalloc(buf_len, GFP_KERNEL);
196 if (!body || !reply) {
200 body->mib_id = cpu_to_le16(mib_id);
201 wfx_fill_header(hif, vif_id, HIF_REQ_ID_READ_MIB, sizeof(*body));
202 ret = wfx_cmd_send(wdev, hif, reply, buf_len, false);
204 if (!ret && mib_id != le16_to_cpu(reply->mib_id)) {
205 dev_warn(wdev->dev, "%s: confirmation mismatch request\n",
210 dev_err(wdev->dev, "buffer is too small to receive %s (%zu < %d)\n",
211 get_mib_name(mib_id), val_len,
212 le16_to_cpu(reply->length));
214 memcpy(val, &reply->mib_data, le16_to_cpu(reply->length));
216 memset(val, 0xFF, val_len);
223 int hif_write_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id,
224 void *val, size_t val_len)
228 int buf_len = sizeof(struct hif_req_write_mib) + val_len;
229 struct hif_req_write_mib *body = wfx_alloc_hif(buf_len, &hif);
233 body->mib_id = cpu_to_le16(mib_id);
234 body->length = cpu_to_le16(val_len);
235 memcpy(&body->mib_data, val, val_len);
236 wfx_fill_header(hif, vif_id, HIF_REQ_ID_WRITE_MIB, buf_len);
237 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
242 int hif_scan(struct wfx_vif *wvif, struct cfg80211_scan_request *req,
243 int chan_start_idx, int chan_num, int *timeout)
248 sizeof(struct hif_req_start_scan_alt) + chan_num * sizeof(u8);
249 struct hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif);
250 int tmo_chan_fg, tmo_chan_bg, tmo;
252 WARN(chan_num > HIF_API_MAX_NB_CHANNELS, "invalid params");
253 WARN(req->n_ssids > HIF_API_MAX_NB_SSIDS, "invalid params");
255 compiletime_assert(IEEE80211_MAX_SSID_LEN == HIF_API_SSID_SIZE,
256 "API inconsistency");
259 for (i = 0; i < req->n_ssids; i++) {
260 memcpy(body->ssid_def[i].ssid, req->ssids[i].ssid,
261 IEEE80211_MAX_SSID_LEN);
262 body->ssid_def[i].ssid_length =
263 cpu_to_le32(req->ssids[i].ssid_len);
265 body->num_of_ssids = HIF_API_MAX_NB_SSIDS;
266 // Background scan is always a good idea
267 body->scan_type.type = 1;
268 body->scan_flags.fbg = 1;
269 body->tx_power_level =
270 cpu_to_le32(req->channels[chan_start_idx]->max_power);
271 body->num_of_channels = chan_num;
272 for (i = 0; i < chan_num; i++)
273 body->channel_list[i] =
274 req->channels[i + chan_start_idx]->hw_value;
276 body->max_transmit_rate = API_RATE_INDEX_G_6MBPS;
278 body->max_transmit_rate = API_RATE_INDEX_B_1MBPS;
279 if (req->channels[chan_start_idx]->flags & IEEE80211_CHAN_NO_IR) {
280 body->min_channel_time = cpu_to_le32(50);
281 body->max_channel_time = cpu_to_le32(150);
283 body->min_channel_time = cpu_to_le32(10);
284 body->max_channel_time = cpu_to_le32(50);
285 body->num_of_probe_requests = 2;
286 body->probe_delay = 100;
288 tmo_chan_bg = le32_to_cpu(body->max_channel_time) * USEC_PER_TU;
289 tmo_chan_fg = 512 * USEC_PER_TU + body->probe_delay;
290 tmo_chan_fg *= body->num_of_probe_requests;
291 tmo = chan_num * max(tmo_chan_bg, tmo_chan_fg) + 512 * USEC_PER_TU;
293 *timeout = usecs_to_jiffies(tmo);
295 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START_SCAN, buf_len);
296 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
301 int hif_stop_scan(struct wfx_vif *wvif)
305 // body associated to HIF_REQ_ID_STOP_SCAN is empty
306 wfx_alloc_hif(0, &hif);
310 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_STOP_SCAN, 0);
311 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
316 int hif_join(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
317 struct ieee80211_channel *channel, const u8 *ssid, int ssidlen)
321 struct hif_req_join *body = wfx_alloc_hif(sizeof(*body), &hif);
323 WARN_ON(!conf->beacon_int);
324 WARN_ON(!conf->basic_rates);
325 WARN_ON(sizeof(body->ssid) < ssidlen);
326 WARN(!conf->ibss_joined && !ssidlen, "joining an unknown BSS");
329 body->infrastructure_bss_mode = !conf->ibss_joined;
330 body->short_preamble = conf->use_short_preamble;
331 if (channel && channel->flags & IEEE80211_CHAN_NO_IR)
332 body->probe_for_join = 0;
334 body->probe_for_join = 1;
335 body->channel_number = channel->hw_value;
336 body->beacon_interval = cpu_to_le32(conf->beacon_int);
337 body->basic_rate_set =
338 cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
339 memcpy(body->bssid, conf->bssid, sizeof(body->bssid));
341 body->ssid_length = cpu_to_le32(ssidlen);
342 memcpy(body->ssid, ssid, ssidlen);
344 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_JOIN, sizeof(*body));
345 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
350 int hif_set_bss_params(struct wfx_vif *wvif, int aid, int beacon_lost_count)
354 struct hif_req_set_bss_params *body =
355 wfx_alloc_hif(sizeof(*body), &hif);
359 body->aid = cpu_to_le16(aid);
360 body->beacon_lost_count = beacon_lost_count;
361 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_BSS_PARAMS,
363 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
368 int hif_add_key(struct wfx_dev *wdev, const struct hif_req_add_key *arg)
372 // FIXME: only send necessary bits
373 struct hif_req_add_key *body = wfx_alloc_hif(sizeof(*body), &hif);
377 // FIXME: swap bytes as necessary in body
378 memcpy(body, arg, sizeof(*body));
379 if (wfx_api_older_than(wdev, 1, 5))
380 // Legacy firmwares expect that add_key to be sent on right
382 wfx_fill_header(hif, arg->int_id, HIF_REQ_ID_ADD_KEY,
385 wfx_fill_header(hif, -1, HIF_REQ_ID_ADD_KEY, sizeof(*body));
386 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
391 int hif_remove_key(struct wfx_dev *wdev, int idx)
395 struct hif_req_remove_key *body = wfx_alloc_hif(sizeof(*body), &hif);
399 body->entry_index = idx;
400 wfx_fill_header(hif, -1, HIF_REQ_ID_REMOVE_KEY, sizeof(*body));
401 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
406 int hif_set_edca_queue_params(struct wfx_vif *wvif, u16 queue,
407 const struct ieee80211_tx_queue_params *arg)
411 struct hif_req_edca_queue_params *body = wfx_alloc_hif(sizeof(*body),
417 WARN_ON(arg->aifs > 255);
420 body->aifsn = arg->aifs;
421 body->cw_min = cpu_to_le16(arg->cw_min);
422 body->cw_max = cpu_to_le16(arg->cw_max);
423 body->tx_op_limit = cpu_to_le16(arg->txop * USEC_PER_TXOP);
424 body->queue_id = 3 - queue;
425 // API 2.0 has changed queue IDs values
426 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BE)
427 body->queue_id = HIF_QUEUE_ID_BACKGROUND;
428 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BK)
429 body->queue_id = HIF_QUEUE_ID_BESTEFFORT;
430 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_EDCA_QUEUE_PARAMS,
432 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
437 int hif_set_pm(struct wfx_vif *wvif, bool ps, int dynamic_ps_timeout)
441 struct hif_req_set_pm_mode *body = wfx_alloc_hif(sizeof(*body), &hif);
449 body->pm_mode.enter_psm = 1;
450 // Firmware does not support more than 128ms
451 body->fast_psm_idle_period = min(dynamic_ps_timeout * 2, 255);
452 if (body->fast_psm_idle_period)
453 body->pm_mode.fast_psm = 1;
455 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_PM_MODE, sizeof(*body));
456 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
461 int hif_start(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
462 const struct ieee80211_channel *channel)
466 struct hif_req_start *body = wfx_alloc_hif(sizeof(*body), &hif);
468 WARN_ON(!conf->beacon_int);
471 body->dtim_period = conf->dtim_period;
472 body->short_preamble = conf->use_short_preamble;
473 body->channel_number = channel->hw_value;
474 body->beacon_interval = cpu_to_le32(conf->beacon_int);
475 body->basic_rate_set =
476 cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
477 body->ssid_length = conf->ssid_len;
478 memcpy(body->ssid, conf->ssid, conf->ssid_len);
479 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START, sizeof(*body));
480 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
485 int hif_beacon_transmit(struct wfx_vif *wvif, bool enable)
489 struct hif_req_beacon_transmit *body = wfx_alloc_hif(sizeof(*body),
494 body->enable_beaconing = enable ? 1 : 0;
495 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_BEACON_TRANSMIT,
497 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
502 int hif_map_link(struct wfx_vif *wvif, u8 *mac_addr, int flags, int sta_id)
506 struct hif_req_map_link *body = wfx_alloc_hif(sizeof(*body), &hif);
511 ether_addr_copy(body->mac_addr, mac_addr);
512 body->map_link_flags = *(struct hif_map_link_flags *)&flags;
513 body->peer_sta_id = sta_id;
514 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_MAP_LINK, sizeof(*body));
515 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
520 int hif_update_ie_beacon(struct wfx_vif *wvif, const u8 *ies, size_t ies_len)
524 int buf_len = sizeof(struct hif_req_update_ie) + ies_len;
525 struct hif_req_update_ie *body = wfx_alloc_hif(buf_len, &hif);
529 body->ie_flags.beacon = 1;
530 body->num_ies = cpu_to_le16(1);
531 memcpy(body->ie, ies, ies_len);
532 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_UPDATE_IE, buf_len);
533 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
538 int hif_sl_send_pub_keys(struct wfx_dev *wdev,
539 const u8 *pubkey, const u8 *pubkey_hmac)
543 struct hif_req_sl_exchange_pub_keys *body = wfx_alloc_hif(sizeof(*body),
548 body->algorithm = HIF_SL_CURVE25519;
549 memcpy(body->host_pub_key, pubkey, sizeof(body->host_pub_key));
550 memcpy(body->host_pub_key_mac, pubkey_hmac,
551 sizeof(body->host_pub_key_mac));
552 wfx_fill_header(hif, -1, HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS,
554 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
556 // Compatibility with legacy secure link
557 if (ret == le32_to_cpu(HIF_STATUS_SLK_NEGO_SUCCESS))
562 int hif_sl_config(struct wfx_dev *wdev, const unsigned long *bitmap)
566 struct hif_req_sl_configure *body = wfx_alloc_hif(sizeof(*body), &hif);
570 memcpy(body->encr_bmp, bitmap, sizeof(body->encr_bmp));
571 wfx_fill_header(hif, -1, HIF_REQ_ID_SL_CONFIGURE, sizeof(*body));
572 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
577 int hif_sl_set_mac_key(struct wfx_dev *wdev, const u8 *slk_key, int destination)
581 struct hif_req_set_sl_mac_key *body = wfx_alloc_hif(sizeof(*body),
586 memcpy(body->key_value, slk_key, sizeof(body->key_value));
587 body->otp_or_ram = destination;
588 wfx_fill_header(hif, -1, HIF_REQ_ID_SET_SL_MAC_KEY, sizeof(*body));
589 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
591 // Compatibility with legacy secure link
592 if (ret == le32_to_cpu(HIF_STATUS_SLK_SET_KEY_SUCCESS))