1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of host-to-chip commands (aka request/confirmation) of WFxxx
6 * Copyright (c) 2017-2020, 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);
25 static void wfx_fill_header(struct hif_msg *hif, int if_id,
26 unsigned int cmd, size_t size)
31 WARN(cmd > 0x3f, "invalid WSM command %#.2x", cmd);
32 WARN(size > 0xFFF, "requested buffer is too large: %zu bytes", size);
33 WARN(if_id > 0x3, "invalid interface ID %d", if_id);
35 hif->len = cpu_to_le16(size + 4);
37 hif->interface = if_id;
40 static void *wfx_alloc_hif(size_t body_len, struct hif_msg **hif)
42 *hif = kzalloc(sizeof(struct hif_msg) + body_len, GFP_KERNEL);
49 int wfx_cmd_send(struct wfx_dev *wdev, struct hif_msg *request,
50 void *reply, size_t reply_len, bool no_reply)
52 const char *mib_name = "";
53 const char *mib_sep = "";
54 int cmd = request->id;
55 int vif = request->interface;
58 // Do not wait for any reply if chip is frozen
59 if (wdev->chip_frozen)
62 mutex_lock(&wdev->hif_cmd.lock);
63 WARN(wdev->hif_cmd.buf_send, "data locking error");
65 // Note: call to complete() below has an implicit memory barrier that
66 // hopefully protect buf_send
67 wdev->hif_cmd.buf_send = request;
68 wdev->hif_cmd.buf_recv = reply;
69 wdev->hif_cmd.len_recv = reply_len;
70 complete(&wdev->hif_cmd.ready);
72 wfx_bh_request_tx(wdev);
75 // Chip won't reply. Give enough time to the wq to send the
78 wdev->hif_cmd.buf_send = NULL;
79 mutex_unlock(&wdev->hif_cmd.lock);
84 wfx_bh_poll_irq(wdev);
86 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 1 * HZ);
88 dev_err(wdev->dev, "chip is abnormally long to answer\n");
89 reinit_completion(&wdev->hif_cmd.ready);
90 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 3 * HZ);
93 dev_err(wdev->dev, "chip did not answer\n");
94 wfx_pending_dump_old_frames(wdev, 3000);
95 wdev->chip_frozen = true;
96 reinit_completion(&wdev->hif_cmd.done);
99 ret = wdev->hif_cmd.ret;
102 wdev->hif_cmd.buf_send = NULL;
103 mutex_unlock(&wdev->hif_cmd.lock);
106 (cmd == HIF_REQ_ID_READ_MIB || cmd == HIF_REQ_ID_WRITE_MIB)) {
107 mib_name = get_mib_name(((u16 *)request)[2]);
112 "WSM request %s%s%s (%#.2x) on vif %d returned error %d\n",
113 get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
116 "WSM request %s%s%s (%#.2x) on vif %d returned status %d\n",
117 get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
122 // This function is special. After HIF_REQ_ID_SHUT_DOWN, chip won't reply to any
123 // request anymore. Obviously, only call this function during device unregister.
124 int hif_shutdown(struct wfx_dev *wdev)
129 wfx_alloc_hif(0, &hif);
132 wfx_fill_header(hif, -1, HIF_REQ_ID_SHUT_DOWN, 0);
133 ret = wfx_cmd_send(wdev, hif, NULL, 0, true);
134 if (wdev->pdata.gpio_wakeup)
135 gpiod_set_value(wdev->pdata.gpio_wakeup, 0);
137 control_reg_write(wdev, 0);
142 int hif_configuration(struct wfx_dev *wdev, const u8 *conf, size_t len)
145 size_t buf_len = sizeof(struct hif_req_configuration) + len;
147 struct hif_req_configuration *body = wfx_alloc_hif(buf_len, &hif);
151 body->length = cpu_to_le16(len);
152 memcpy(body->pds_data, conf, len);
153 wfx_fill_header(hif, -1, HIF_REQ_ID_CONFIGURATION, buf_len);
154 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
159 int hif_reset(struct wfx_vif *wvif, bool reset_stat)
163 struct hif_req_reset *body = wfx_alloc_hif(sizeof(*body), &hif);
167 body->reset_stat = reset_stat;
168 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_RESET, sizeof(*body));
169 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
174 int hif_read_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id,
175 void *val, size_t val_len)
179 int buf_len = sizeof(struct hif_cnf_read_mib) + val_len;
180 struct hif_req_read_mib *body = wfx_alloc_hif(sizeof(*body), &hif);
181 struct hif_cnf_read_mib *reply = kmalloc(buf_len, GFP_KERNEL);
183 if (!body || !reply) {
187 body->mib_id = cpu_to_le16(mib_id);
188 wfx_fill_header(hif, vif_id, HIF_REQ_ID_READ_MIB, sizeof(*body));
189 ret = wfx_cmd_send(wdev, hif, reply, buf_len, false);
191 if (!ret && mib_id != le16_to_cpu(reply->mib_id)) {
192 dev_warn(wdev->dev, "%s: confirmation mismatch request\n",
197 dev_err(wdev->dev, "buffer is too small to receive %s (%zu < %d)\n",
198 get_mib_name(mib_id), val_len,
199 le16_to_cpu(reply->length));
201 memcpy(val, &reply->mib_data, le16_to_cpu(reply->length));
203 memset(val, 0xFF, val_len);
210 int hif_write_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id,
211 void *val, size_t val_len)
215 int buf_len = sizeof(struct hif_req_write_mib) + val_len;
216 struct hif_req_write_mib *body = wfx_alloc_hif(buf_len, &hif);
220 body->mib_id = cpu_to_le16(mib_id);
221 body->length = cpu_to_le16(val_len);
222 memcpy(&body->mib_data, val, val_len);
223 wfx_fill_header(hif, vif_id, HIF_REQ_ID_WRITE_MIB, buf_len);
224 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
229 int hif_scan(struct wfx_vif *wvif, struct cfg80211_scan_request *req,
230 int chan_start_idx, int chan_num, int *timeout)
235 sizeof(struct hif_req_start_scan_alt) + chan_num * sizeof(u8);
236 struct hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif);
237 int tmo_chan_fg, tmo_chan_bg, tmo;
239 WARN(chan_num > HIF_API_MAX_NB_CHANNELS, "invalid params");
240 WARN(req->n_ssids > HIF_API_MAX_NB_SSIDS, "invalid params");
244 for (i = 0; i < req->n_ssids; i++) {
245 memcpy(body->ssid_def[i].ssid, req->ssids[i].ssid,
246 IEEE80211_MAX_SSID_LEN);
247 body->ssid_def[i].ssid_length =
248 cpu_to_le32(req->ssids[i].ssid_len);
250 body->num_of_ssids = HIF_API_MAX_NB_SSIDS;
251 body->maintain_current_bss = 1;
252 body->disallow_ps = 1;
253 body->tx_power_level =
254 cpu_to_le32(req->channels[chan_start_idx]->max_power);
255 body->num_of_channels = chan_num;
256 for (i = 0; i < chan_num; i++)
257 body->channel_list[i] =
258 req->channels[i + chan_start_idx]->hw_value;
260 body->max_transmit_rate = API_RATE_INDEX_G_6MBPS;
262 body->max_transmit_rate = API_RATE_INDEX_B_1MBPS;
263 if (req->channels[chan_start_idx]->flags & IEEE80211_CHAN_NO_IR) {
264 body->min_channel_time = cpu_to_le32(50);
265 body->max_channel_time = cpu_to_le32(150);
267 body->min_channel_time = cpu_to_le32(10);
268 body->max_channel_time = cpu_to_le32(50);
269 body->num_of_probe_requests = 2;
270 body->probe_delay = 100;
272 tmo_chan_bg = le32_to_cpu(body->max_channel_time) * USEC_PER_TU;
273 tmo_chan_fg = 512 * USEC_PER_TU + body->probe_delay;
274 tmo_chan_fg *= body->num_of_probe_requests;
275 tmo = chan_num * max(tmo_chan_bg, tmo_chan_fg) + 512 * USEC_PER_TU;
277 *timeout = usecs_to_jiffies(tmo);
279 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START_SCAN, buf_len);
280 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
285 int hif_stop_scan(struct wfx_vif *wvif)
289 // body associated to HIF_REQ_ID_STOP_SCAN is empty
290 wfx_alloc_hif(0, &hif);
294 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_STOP_SCAN, 0);
295 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
300 int hif_join(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
301 struct ieee80211_channel *channel, const u8 *ssid, int ssidlen)
305 struct hif_req_join *body = wfx_alloc_hif(sizeof(*body), &hif);
307 WARN_ON(!conf->beacon_int);
308 WARN_ON(!conf->basic_rates);
309 WARN_ON(sizeof(body->ssid) < ssidlen);
310 WARN(!conf->ibss_joined && !ssidlen, "joining an unknown BSS");
311 if (WARN_ON(!channel))
315 body->infrastructure_bss_mode = !conf->ibss_joined;
316 body->short_preamble = conf->use_short_preamble;
317 if (channel->flags & IEEE80211_CHAN_NO_IR)
318 body->probe_for_join = 0;
320 body->probe_for_join = 1;
321 body->channel_number = channel->hw_value;
322 body->beacon_interval = cpu_to_le32(conf->beacon_int);
323 body->basic_rate_set =
324 cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
325 memcpy(body->bssid, conf->bssid, sizeof(body->bssid));
327 body->ssid_length = cpu_to_le32(ssidlen);
328 memcpy(body->ssid, ssid, ssidlen);
330 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_JOIN, sizeof(*body));
331 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
336 int hif_set_bss_params(struct wfx_vif *wvif, int aid, int beacon_lost_count)
340 struct hif_req_set_bss_params *body =
341 wfx_alloc_hif(sizeof(*body), &hif);
345 body->aid = cpu_to_le16(aid);
346 body->beacon_lost_count = beacon_lost_count;
347 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_BSS_PARAMS,
349 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
354 int hif_add_key(struct wfx_dev *wdev, const struct hif_req_add_key *arg)
358 // FIXME: only send necessary bits
359 struct hif_req_add_key *body = wfx_alloc_hif(sizeof(*body), &hif);
363 // FIXME: swap bytes as necessary in body
364 memcpy(body, arg, sizeof(*body));
365 if (wfx_api_older_than(wdev, 1, 5))
366 // Legacy firmwares expect that add_key to be sent on right
368 wfx_fill_header(hif, arg->int_id, HIF_REQ_ID_ADD_KEY,
371 wfx_fill_header(hif, -1, HIF_REQ_ID_ADD_KEY, sizeof(*body));
372 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
377 int hif_remove_key(struct wfx_dev *wdev, int idx)
381 struct hif_req_remove_key *body = wfx_alloc_hif(sizeof(*body), &hif);
385 body->entry_index = idx;
386 wfx_fill_header(hif, -1, HIF_REQ_ID_REMOVE_KEY, sizeof(*body));
387 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
392 int hif_set_edca_queue_params(struct wfx_vif *wvif, u16 queue,
393 const struct ieee80211_tx_queue_params *arg)
397 struct hif_req_edca_queue_params *body = wfx_alloc_hif(sizeof(*body),
403 WARN_ON(arg->aifs > 255);
406 body->aifsn = arg->aifs;
407 body->cw_min = cpu_to_le16(arg->cw_min);
408 body->cw_max = cpu_to_le16(arg->cw_max);
409 body->tx_op_limit = cpu_to_le16(arg->txop * USEC_PER_TXOP);
410 body->queue_id = 3 - queue;
411 // API 2.0 has changed queue IDs values
412 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BE)
413 body->queue_id = HIF_QUEUE_ID_BACKGROUND;
414 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BK)
415 body->queue_id = HIF_QUEUE_ID_BESTEFFORT;
416 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_EDCA_QUEUE_PARAMS,
418 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
423 int hif_set_pm(struct wfx_vif *wvif, bool ps, int dynamic_ps_timeout)
427 struct hif_req_set_pm_mode *body = wfx_alloc_hif(sizeof(*body), &hif);
436 // Firmware does not support more than 128ms
437 body->fast_psm_idle_period = min(dynamic_ps_timeout * 2, 255);
438 if (body->fast_psm_idle_period)
441 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_PM_MODE, sizeof(*body));
442 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
447 int hif_start(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
448 const struct ieee80211_channel *channel)
452 struct hif_req_start *body = wfx_alloc_hif(sizeof(*body), &hif);
454 WARN_ON(!conf->beacon_int);
457 body->dtim_period = conf->dtim_period;
458 body->short_preamble = conf->use_short_preamble;
459 body->channel_number = channel->hw_value;
460 body->beacon_interval = cpu_to_le32(conf->beacon_int);
461 body->basic_rate_set =
462 cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
463 body->ssid_length = conf->ssid_len;
464 memcpy(body->ssid, conf->ssid, conf->ssid_len);
465 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START, sizeof(*body));
466 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
471 int hif_beacon_transmit(struct wfx_vif *wvif, bool enable)
475 struct hif_req_beacon_transmit *body = wfx_alloc_hif(sizeof(*body),
480 body->enable_beaconing = enable ? 1 : 0;
481 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_BEACON_TRANSMIT,
483 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
488 int hif_map_link(struct wfx_vif *wvif, bool unmap, u8 *mac_addr, int sta_id, bool mfp)
492 struct hif_req_map_link *body = wfx_alloc_hif(sizeof(*body), &hif);
497 ether_addr_copy(body->mac_addr, mac_addr);
498 body->mfpc = mfp ? 1 : 0;
499 body->unmap = unmap ? 1 : 0;
500 body->peer_sta_id = sta_id;
501 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_MAP_LINK, sizeof(*body));
502 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
507 int hif_update_ie_beacon(struct wfx_vif *wvif, const u8 *ies, size_t ies_len)
511 int buf_len = sizeof(struct hif_req_update_ie) + ies_len;
512 struct hif_req_update_ie *body = wfx_alloc_hif(buf_len, &hif);
517 body->num_ies = cpu_to_le16(1);
518 memcpy(body->ie, ies, ies_len);
519 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_UPDATE_IE, buf_len);
520 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);