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
wfx_init_hif_cmd(struct wfx_hif_cmd * hif_cmd)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
wfx_fill_header(struct hif_msg * hif,int if_id,unsigned int cmd,size_t size)25 static void wfx_fill_header(struct hif_msg *hif, int if_id,
26 unsigned int cmd, size_t size)
27 {
28 if (if_id == -1)
29 if_id = 2;
30
31 WARN(cmd > 0x3f, "invalid hardware 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);
34
35 hif->len = cpu_to_le16(size + 4);
36 hif->id = cmd;
37 hif->interface = if_id;
38 }
39
wfx_alloc_hif(size_t body_len,struct hif_msg ** hif)40 static void *wfx_alloc_hif(size_t body_len, struct hif_msg **hif)
41 {
42 *hif = kzalloc(sizeof(struct hif_msg) + body_len, GFP_KERNEL);
43 if (*hif)
44 return (*hif)->body;
45 else
46 return NULL;
47 }
48
wfx_cmd_send(struct wfx_dev * wdev,struct hif_msg * request,void * reply,size_t reply_len,bool no_reply)49 int wfx_cmd_send(struct wfx_dev *wdev, struct hif_msg *request,
50 void *reply, size_t reply_len, bool no_reply)
51 {
52 const char *mib_name = "";
53 const char *mib_sep = "";
54 int cmd = request->id;
55 int vif = request->interface;
56 int ret;
57
58 /* Do not wait for any reply if chip is frozen */
59 if (wdev->chip_frozen)
60 return -ETIMEDOUT;
61
62 mutex_lock(&wdev->hif_cmd.lock);
63 WARN(wdev->hif_cmd.buf_send, "data locking error");
64
65 /* Note: call to complete() below has an implicit memory barrier that
66 * hopefully protect buf_send
67 */
68 wdev->hif_cmd.buf_send = request;
69 wdev->hif_cmd.buf_recv = reply;
70 wdev->hif_cmd.len_recv = reply_len;
71 complete(&wdev->hif_cmd.ready);
72
73 wfx_bh_request_tx(wdev);
74
75 if (no_reply) {
76 /* Chip won't reply. Give enough time to the wq to send the
77 * buffer.
78 */
79 msleep(100);
80 wdev->hif_cmd.buf_send = NULL;
81 mutex_unlock(&wdev->hif_cmd.lock);
82 return 0;
83 }
84
85 if (wdev->poll_irq)
86 wfx_bh_poll_irq(wdev);
87
88 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 1 * HZ);
89 if (!ret) {
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);
93 }
94 if (!ret) {
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);
99 ret = -ETIMEDOUT;
100 } else {
101 ret = wdev->hif_cmd.ret;
102 }
103
104 wdev->hif_cmd.buf_send = NULL;
105 mutex_unlock(&wdev->hif_cmd.lock);
106
107 if (ret &&
108 (cmd == HIF_REQ_ID_READ_MIB || cmd == HIF_REQ_ID_WRITE_MIB)) {
109 mib_name = get_mib_name(((u16 *)request)[2]);
110 mib_sep = "/";
111 }
112 if (ret < 0)
113 dev_err(wdev->dev, "hardware request %s%s%s (%#.2x) on vif %d returned error %d\n",
114 get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
115 if (ret > 0)
116 dev_warn(wdev->dev, "hardware request %s%s%s (%#.2x) on vif %d returned status %d\n",
117 get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
118
119 return ret;
120 }
121
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 */
hif_shutdown(struct wfx_dev * wdev)125 int hif_shutdown(struct wfx_dev *wdev)
126 {
127 int ret;
128 struct hif_msg *hif;
129
130 wfx_alloc_hif(0, &hif);
131 if (!hif)
132 return -ENOMEM;
133 wfx_fill_header(hif, -1, HIF_REQ_ID_SHUT_DOWN, 0);
134 ret = wfx_cmd_send(wdev, hif, NULL, 0, true);
135 if (wdev->pdata.gpio_wakeup)
136 gpiod_set_value(wdev->pdata.gpio_wakeup, 0);
137 else
138 control_reg_write(wdev, 0);
139 kfree(hif);
140 return ret;
141 }
142
hif_configuration(struct wfx_dev * wdev,const u8 * conf,size_t len)143 int hif_configuration(struct wfx_dev *wdev, const u8 *conf, size_t len)
144 {
145 int ret;
146 size_t buf_len = sizeof(struct hif_req_configuration) + len;
147 struct hif_msg *hif;
148 struct hif_req_configuration *body = wfx_alloc_hif(buf_len, &hif);
149
150 if (!hif)
151 return -ENOMEM;
152 body->length = cpu_to_le16(len);
153 memcpy(body->pds_data, conf, len);
154 wfx_fill_header(hif, -1, HIF_REQ_ID_CONFIGURATION, buf_len);
155 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
156 kfree(hif);
157 return ret;
158 }
159
hif_reset(struct wfx_vif * wvif,bool reset_stat)160 int hif_reset(struct wfx_vif *wvif, bool reset_stat)
161 {
162 int ret;
163 struct hif_msg *hif;
164 struct hif_req_reset *body = wfx_alloc_hif(sizeof(*body), &hif);
165
166 if (!hif)
167 return -ENOMEM;
168 body->reset_stat = reset_stat;
169 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_RESET, sizeof(*body));
170 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
171 kfree(hif);
172 return ret;
173 }
174
hif_read_mib(struct wfx_dev * wdev,int vif_id,u16 mib_id,void * val,size_t val_len)175 int hif_read_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id,
176 void *val, size_t val_len)
177 {
178 int ret;
179 struct hif_msg *hif;
180 int buf_len = sizeof(struct hif_cnf_read_mib) + val_len;
181 struct hif_req_read_mib *body = wfx_alloc_hif(sizeof(*body), &hif);
182 struct hif_cnf_read_mib *reply = kmalloc(buf_len, GFP_KERNEL);
183
184 if (!body || !reply) {
185 ret = -ENOMEM;
186 goto out;
187 }
188 body->mib_id = cpu_to_le16(mib_id);
189 wfx_fill_header(hif, vif_id, HIF_REQ_ID_READ_MIB, sizeof(*body));
190 ret = wfx_cmd_send(wdev, hif, reply, buf_len, false);
191
192 if (!ret && mib_id != le16_to_cpu(reply->mib_id)) {
193 dev_warn(wdev->dev, "%s: confirmation mismatch request\n",
194 __func__);
195 ret = -EIO;
196 }
197 if (ret == -ENOMEM)
198 dev_err(wdev->dev, "buffer is too small to receive %s (%zu < %d)\n",
199 get_mib_name(mib_id), val_len,
200 le16_to_cpu(reply->length));
201 if (!ret)
202 memcpy(val, &reply->mib_data, le16_to_cpu(reply->length));
203 else
204 memset(val, 0xFF, val_len);
205 out:
206 kfree(hif);
207 kfree(reply);
208 return ret;
209 }
210
hif_write_mib(struct wfx_dev * wdev,int vif_id,u16 mib_id,void * val,size_t val_len)211 int hif_write_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id,
212 void *val, size_t val_len)
213 {
214 int ret;
215 struct hif_msg *hif;
216 int buf_len = sizeof(struct hif_req_write_mib) + val_len;
217 struct hif_req_write_mib *body = wfx_alloc_hif(buf_len, &hif);
218
219 if (!hif)
220 return -ENOMEM;
221 body->mib_id = cpu_to_le16(mib_id);
222 body->length = cpu_to_le16(val_len);
223 memcpy(&body->mib_data, val, val_len);
224 wfx_fill_header(hif, vif_id, HIF_REQ_ID_WRITE_MIB, buf_len);
225 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
226 kfree(hif);
227 return ret;
228 }
229
hif_scan(struct wfx_vif * wvif,struct cfg80211_scan_request * req,int chan_start_idx,int chan_num)230 int hif_scan(struct wfx_vif *wvif, struct cfg80211_scan_request *req,
231 int chan_start_idx, int chan_num)
232 {
233 int ret, i;
234 struct hif_msg *hif;
235 size_t buf_len =
236 sizeof(struct hif_req_start_scan_alt) + chan_num * sizeof(u8);
237 struct hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif);
238
239 WARN(chan_num > HIF_API_MAX_NB_CHANNELS, "invalid params");
240 WARN(req->n_ssids > HIF_API_MAX_NB_SSIDS, "invalid params");
241
242 if (!hif)
243 return -ENOMEM;
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);
249 }
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;
259 if (req->no_cck)
260 body->max_transmit_rate = API_RATE_INDEX_G_6MBPS;
261 else
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);
266 } else {
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;
271 }
272
273 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START_SCAN, buf_len);
274 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
275 kfree(hif);
276 return ret;
277 }
278
hif_stop_scan(struct wfx_vif * wvif)279 int hif_stop_scan(struct wfx_vif *wvif)
280 {
281 int ret;
282 struct hif_msg *hif;
283 /* body associated to HIF_REQ_ID_STOP_SCAN is empty */
284 wfx_alloc_hif(0, &hif);
285
286 if (!hif)
287 return -ENOMEM;
288 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_STOP_SCAN, 0);
289 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
290 kfree(hif);
291 return ret;
292 }
293
hif_join(struct wfx_vif * wvif,const struct ieee80211_bss_conf * conf,struct ieee80211_channel * channel,const u8 * ssid,int ssidlen)294 int hif_join(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
295 struct ieee80211_channel *channel, const u8 *ssid, int ssidlen)
296 {
297 int ret;
298 struct hif_msg *hif;
299 struct hif_req_join *body = wfx_alloc_hif(sizeof(*body), &hif);
300
301 WARN_ON(!conf->beacon_int);
302 WARN_ON(!conf->basic_rates);
303 WARN_ON(sizeof(body->ssid) < ssidlen);
304 WARN(!conf->ibss_joined && !ssidlen, "joining an unknown BSS");
305 if (!hif)
306 return -ENOMEM;
307 body->infrastructure_bss_mode = !conf->ibss_joined;
308 body->short_preamble = conf->use_short_preamble;
309 body->probe_for_join = !(channel->flags & IEEE80211_CHAN_NO_IR);
310 body->channel_number = channel->hw_value;
311 body->beacon_interval = cpu_to_le32(conf->beacon_int);
312 body->basic_rate_set =
313 cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
314 memcpy(body->bssid, conf->bssid, sizeof(body->bssid));
315 if (ssid) {
316 body->ssid_length = cpu_to_le32(ssidlen);
317 memcpy(body->ssid, ssid, ssidlen);
318 }
319 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_JOIN, sizeof(*body));
320 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
321 kfree(hif);
322 return ret;
323 }
324
hif_set_bss_params(struct wfx_vif * wvif,int aid,int beacon_lost_count)325 int hif_set_bss_params(struct wfx_vif *wvif, int aid, int beacon_lost_count)
326 {
327 int ret;
328 struct hif_msg *hif;
329 struct hif_req_set_bss_params *body =
330 wfx_alloc_hif(sizeof(*body), &hif);
331
332 if (!hif)
333 return -ENOMEM;
334 body->aid = cpu_to_le16(aid);
335 body->beacon_lost_count = beacon_lost_count;
336 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_BSS_PARAMS,
337 sizeof(*body));
338 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
339 kfree(hif);
340 return ret;
341 }
342
hif_add_key(struct wfx_dev * wdev,const struct hif_req_add_key * arg)343 int hif_add_key(struct wfx_dev *wdev, const struct hif_req_add_key *arg)
344 {
345 int ret;
346 struct hif_msg *hif;
347 /* FIXME: only send necessary bits */
348 struct hif_req_add_key *body = wfx_alloc_hif(sizeof(*body), &hif);
349
350 if (!hif)
351 return -ENOMEM;
352 /* FIXME: swap bytes as necessary in body */
353 memcpy(body, arg, sizeof(*body));
354 if (wfx_api_older_than(wdev, 1, 5))
355 /* Legacy firmwares expect that add_key to be sent on right
356 * interface.
357 */
358 wfx_fill_header(hif, arg->int_id, HIF_REQ_ID_ADD_KEY,
359 sizeof(*body));
360 else
361 wfx_fill_header(hif, -1, HIF_REQ_ID_ADD_KEY, sizeof(*body));
362 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
363 kfree(hif);
364 return ret;
365 }
366
hif_remove_key(struct wfx_dev * wdev,int idx)367 int hif_remove_key(struct wfx_dev *wdev, int idx)
368 {
369 int ret;
370 struct hif_msg *hif;
371 struct hif_req_remove_key *body = wfx_alloc_hif(sizeof(*body), &hif);
372
373 if (!hif)
374 return -ENOMEM;
375 body->entry_index = idx;
376 wfx_fill_header(hif, -1, HIF_REQ_ID_REMOVE_KEY, sizeof(*body));
377 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
378 kfree(hif);
379 return ret;
380 }
381
hif_set_edca_queue_params(struct wfx_vif * wvif,u16 queue,const struct ieee80211_tx_queue_params * arg)382 int hif_set_edca_queue_params(struct wfx_vif *wvif, u16 queue,
383 const struct ieee80211_tx_queue_params *arg)
384 {
385 int ret;
386 struct hif_msg *hif;
387 struct hif_req_edca_queue_params *body = wfx_alloc_hif(sizeof(*body),
388 &hif);
389
390 if (!body)
391 return -ENOMEM;
392
393 WARN_ON(arg->aifs > 255);
394 if (!hif)
395 return -ENOMEM;
396 body->aifsn = arg->aifs;
397 body->cw_min = cpu_to_le16(arg->cw_min);
398 body->cw_max = cpu_to_le16(arg->cw_max);
399 body->tx_op_limit = cpu_to_le16(arg->txop * USEC_PER_TXOP);
400 body->queue_id = 3 - queue;
401 /* API 2.0 has changed queue IDs values */
402 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BE)
403 body->queue_id = HIF_QUEUE_ID_BACKGROUND;
404 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BK)
405 body->queue_id = HIF_QUEUE_ID_BESTEFFORT;
406 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_EDCA_QUEUE_PARAMS,
407 sizeof(*body));
408 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
409 kfree(hif);
410 return ret;
411 }
412
hif_set_pm(struct wfx_vif * wvif,bool ps,int dynamic_ps_timeout)413 int hif_set_pm(struct wfx_vif *wvif, bool ps, int dynamic_ps_timeout)
414 {
415 int ret;
416 struct hif_msg *hif;
417 struct hif_req_set_pm_mode *body = wfx_alloc_hif(sizeof(*body), &hif);
418
419 if (!body)
420 return -ENOMEM;
421
422 if (!hif)
423 return -ENOMEM;
424 if (ps) {
425 body->enter_psm = 1;
426 /* Firmware does not support more than 128ms */
427 body->fast_psm_idle_period = min(dynamic_ps_timeout * 2, 255);
428 if (body->fast_psm_idle_period)
429 body->fast_psm = 1;
430 }
431 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_PM_MODE, sizeof(*body));
432 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
433 kfree(hif);
434 return ret;
435 }
436
hif_start(struct wfx_vif * wvif,const struct ieee80211_bss_conf * conf,const struct ieee80211_channel * channel)437 int hif_start(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
438 const struct ieee80211_channel *channel)
439 {
440 int ret;
441 struct hif_msg *hif;
442 struct hif_req_start *body = wfx_alloc_hif(sizeof(*body), &hif);
443
444 WARN_ON(!conf->beacon_int);
445 if (!hif)
446 return -ENOMEM;
447 body->dtim_period = conf->dtim_period;
448 body->short_preamble = conf->use_short_preamble;
449 body->channel_number = channel->hw_value;
450 body->beacon_interval = cpu_to_le32(conf->beacon_int);
451 body->basic_rate_set =
452 cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
453 body->ssid_length = conf->ssid_len;
454 memcpy(body->ssid, conf->ssid, conf->ssid_len);
455 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START, sizeof(*body));
456 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
457 kfree(hif);
458 return ret;
459 }
460
hif_beacon_transmit(struct wfx_vif * wvif,bool enable)461 int hif_beacon_transmit(struct wfx_vif *wvif, bool enable)
462 {
463 int ret;
464 struct hif_msg *hif;
465 struct hif_req_beacon_transmit *body = wfx_alloc_hif(sizeof(*body),
466 &hif);
467
468 if (!hif)
469 return -ENOMEM;
470 body->enable_beaconing = enable ? 1 : 0;
471 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_BEACON_TRANSMIT,
472 sizeof(*body));
473 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
474 kfree(hif);
475 return ret;
476 }
477
hif_map_link(struct wfx_vif * wvif,bool unmap,u8 * mac_addr,int sta_id,bool mfp)478 int hif_map_link(struct wfx_vif *wvif, bool unmap, u8 *mac_addr, int sta_id, bool mfp)
479 {
480 int ret;
481 struct hif_msg *hif;
482 struct hif_req_map_link *body = wfx_alloc_hif(sizeof(*body), &hif);
483
484 if (!hif)
485 return -ENOMEM;
486 if (mac_addr)
487 ether_addr_copy(body->mac_addr, mac_addr);
488 body->mfpc = mfp ? 1 : 0;
489 body->unmap = unmap ? 1 : 0;
490 body->peer_sta_id = sta_id;
491 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_MAP_LINK, sizeof(*body));
492 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
493 kfree(hif);
494 return ret;
495 }
496
hif_update_ie_beacon(struct wfx_vif * wvif,const u8 * ies,size_t ies_len)497 int hif_update_ie_beacon(struct wfx_vif *wvif, const u8 *ies, size_t ies_len)
498 {
499 int ret;
500 struct hif_msg *hif;
501 int buf_len = sizeof(struct hif_req_update_ie) + ies_len;
502 struct hif_req_update_ie *body = wfx_alloc_hif(buf_len, &hif);
503
504 if (!hif)
505 return -ENOMEM;
506 body->beacon = 1;
507 body->num_ies = cpu_to_le16(1);
508 memcpy(body->ie, ies, ies_len);
509 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_UPDATE_IE, buf_len);
510 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
511 kfree(hif);
512 return ret;
513 }
514