]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/staging/wfx/hif_tx.c
staging: wfx: check for memory allocation failures from wfx_alloc_hif
[thirdparty/linux.git] / drivers / staging / wfx / hif_tx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Implementation of host-to-chip commands (aka request/confirmation) of WFxxx
4 * Split Mac (WSM) API.
5 *
6 * Copyright (c) 2017-2019, 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 mutex_init(&hif_cmd->key_renew_lock);
24 }
25
26 static void wfx_fill_header(struct hif_msg *hif, int if_id, unsigned int cmd,
27 size_t size)
28 {
29 if (if_id == -1)
30 if_id = 2;
31
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);
35
36 hif->len = cpu_to_le16(size + 4);
37 hif->id = cmd;
38 hif->interface = if_id;
39 }
40
41 static void *wfx_alloc_hif(size_t body_len, struct hif_msg **hif)
42 {
43 *hif = kzalloc(sizeof(struct hif_msg) + body_len, GFP_KERNEL);
44 if (*hif)
45 return (*hif)->body;
46 else
47 return NULL;
48 }
49
50 int wfx_cmd_send(struct wfx_dev *wdev, struct hif_msg *request, void *reply,
51 size_t reply_len, bool async)
52 {
53 const char *mib_name = "";
54 const char *mib_sep = "";
55 int cmd = request->id;
56 int vif = request->interface;
57 int ret;
58
59 WARN(wdev->hif_cmd.buf_recv && wdev->hif_cmd.async, "API usage error");
60
61 // Do not wait for any reply if chip is frozen
62 if (wdev->chip_frozen)
63 return -ETIMEDOUT;
64
65 if (cmd != HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS)
66 mutex_lock(&wdev->hif_cmd.key_renew_lock);
67
68 mutex_lock(&wdev->hif_cmd.lock);
69 WARN(wdev->hif_cmd.buf_send, "data locking error");
70
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);
78
79 wfx_bh_request_tx(wdev);
80
81 // NOTE: no timeout is catched async is enabled
82 if (async)
83 return 0;
84
85 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 1 * HZ);
86 if (!ret) {
87 dev_err(wdev->dev, "chip is abnormally long to answer\n");
88 reinit_completion(&wdev->hif_cmd.ready);
89 ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 3 * HZ);
90 }
91 if (!ret) {
92 dev_err(wdev->dev, "chip did not answer\n");
93 wfx_pending_dump_old_frames(wdev, 3000);
94 wdev->chip_frozen = 1;
95 reinit_completion(&wdev->hif_cmd.done);
96 ret = -ETIMEDOUT;
97 } else {
98 ret = wdev->hif_cmd.ret;
99 }
100
101 wdev->hif_cmd.buf_send = NULL;
102 mutex_unlock(&wdev->hif_cmd.lock);
103
104 if (ret &&
105 (cmd == HIF_REQ_ID_READ_MIB || cmd == HIF_REQ_ID_WRITE_MIB)) {
106 mib_name = get_mib_name(((u16 *) request)[2]);
107 mib_sep = "/";
108 }
109 if (ret < 0)
110 dev_err(wdev->dev,
111 "WSM request %s%s%s (%#.2x) on vif %d returned error %d\n",
112 get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
113 if (ret > 0)
114 dev_warn(wdev->dev,
115 "WSM request %s%s%s (%#.2x) on vif %d returned status %d\n",
116 get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
117
118 if (cmd != HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS)
119 mutex_unlock(&wdev->hif_cmd.key_renew_lock);
120 return ret;
121 }
122
123 // This function is special. After HIF_REQ_ID_SHUT_DOWN, chip won't reply to any
124 // request anymore. We need to slightly hack struct wfx_hif_cmd for that job. Be
125 // carefull to only call this funcion during device unregister.
126 int hif_shutdown(struct wfx_dev *wdev)
127 {
128 int ret;
129 struct hif_msg *hif;
130
131 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 // After this command, chip won't reply. Be sure to give enough time to
135 // bh to send buffer:
136 msleep(100);
137 wdev->hif_cmd.buf_send = NULL;
138 if (wdev->pdata.gpio_wakeup)
139 gpiod_set_value(wdev->pdata.gpio_wakeup, 0);
140 else
141 control_reg_write(wdev, 0);
142 mutex_unlock(&wdev->hif_cmd.lock);
143 kfree(hif);
144 return ret;
145 }
146
147 int hif_configuration(struct wfx_dev *wdev, const u8 *conf, size_t len)
148 {
149 int ret;
150 size_t buf_len = sizeof(struct hif_req_configuration) + len;
151 struct hif_msg *hif;
152 struct hif_req_configuration *body = wfx_alloc_hif(buf_len, &hif);
153
154 body->length = cpu_to_le16(len);
155 memcpy(body->pds_data, conf, len);
156 wfx_fill_header(hif, -1, HIF_REQ_ID_CONFIGURATION, buf_len);
157 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
158 kfree(hif);
159 return ret;
160 }
161
162 int hif_reset(struct wfx_vif *wvif, bool reset_stat)
163 {
164 int ret;
165 struct hif_msg *hif;
166 struct hif_req_reset *body = wfx_alloc_hif(sizeof(*body), &hif);
167
168 body->reset_flags.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
175 int hif_read_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val,
176 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 body->mib_id = cpu_to_le16(mib_id);
185 wfx_fill_header(hif, vif_id, HIF_REQ_ID_READ_MIB, sizeof(*body));
186 ret = wfx_cmd_send(wdev, hif, reply, buf_len, false);
187
188 if (!ret && mib_id != reply->mib_id) {
189 dev_warn(wdev->dev,
190 "%s: confirmation mismatch request\n", __func__);
191 ret = -EIO;
192 }
193 if (ret == -ENOMEM)
194 dev_err(wdev->dev,
195 "buffer is too small to receive %s (%zu < %d)\n",
196 get_mib_name(mib_id), val_len, reply->length);
197 if (!ret)
198 memcpy(val, &reply->mib_data, reply->length);
199 else
200 memset(val, 0xFF, val_len);
201 kfree(hif);
202 kfree(reply);
203 return ret;
204 }
205
206 int hif_write_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val,
207 size_t val_len)
208 {
209 int ret;
210 struct hif_msg *hif;
211 int buf_len = sizeof(struct hif_req_write_mib) + val_len;
212 struct hif_req_write_mib *body = wfx_alloc_hif(buf_len, &hif);
213
214 body->mib_id = cpu_to_le16(mib_id);
215 body->length = cpu_to_le16(val_len);
216 memcpy(&body->mib_data, val, val_len);
217 wfx_fill_header(hif, vif_id, HIF_REQ_ID_WRITE_MIB, buf_len);
218 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
219 kfree(hif);
220 return ret;
221 }
222
223 int hif_scan(struct wfx_vif *wvif, struct cfg80211_scan_request *req,
224 int chan_start_idx, int chan_num)
225 {
226 int ret, i;
227 struct hif_msg *hif;
228 size_t buf_len =
229 sizeof(struct hif_req_start_scan_alt) + chan_num * sizeof(u8);
230 struct hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif);
231 int tmo_chan_fg, tmo_chan_bg, tmo;
232
233 WARN(chan_num > HIF_API_MAX_NB_CHANNELS, "invalid params");
234 WARN(req->n_ssids > HIF_API_MAX_NB_SSIDS, "invalid params");
235
236 compiletime_assert(IEEE80211_MAX_SSID_LEN == HIF_API_SSID_SIZE,
237 "API inconsistency");
238 for (i = 0; i < req->n_ssids; i++) {
239 memcpy(body->ssid_def[i].ssid, req->ssids[i].ssid,
240 IEEE80211_MAX_SSID_LEN);
241 body->ssid_def[i].ssid_length =
242 cpu_to_le32(req->ssids[i].ssid_len);
243 }
244 body->num_of_ssids = HIF_API_MAX_NB_SSIDS;
245 // Background scan is always a good idea
246 body->scan_type.type = 1;
247 body->scan_flags.fbg = 1;
248 body->tx_power_level =
249 cpu_to_le32(req->channels[chan_start_idx]->max_power);
250 body->num_of_channels = chan_num;
251 for (i = 0; i < chan_num; i++)
252 body->channel_list[i] =
253 req->channels[i + chan_start_idx]->hw_value;
254 if (req->no_cck)
255 body->max_transmit_rate = API_RATE_INDEX_G_6MBPS;
256 else
257 body->max_transmit_rate = API_RATE_INDEX_B_1MBPS;
258 if (req->channels[chan_start_idx]->flags & IEEE80211_CHAN_NO_IR) {
259 body->min_channel_time = cpu_to_le32(50);
260 body->max_channel_time = cpu_to_le32(150);
261 } else {
262 body->min_channel_time = cpu_to_le32(10);
263 body->max_channel_time = cpu_to_le32(50);
264 body->num_of_probe_requests = 2;
265 body->probe_delay = 100;
266 }
267 tmo_chan_bg = le32_to_cpu(body->max_channel_time) * USEC_PER_TU;
268 tmo_chan_fg = 512 * USEC_PER_TU + body->probe_delay;
269 tmo_chan_fg *= body->num_of_probe_requests;
270 tmo = chan_num * max(tmo_chan_bg, tmo_chan_fg);
271
272 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START_SCAN, buf_len);
273 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
274 kfree(hif);
275 return ret ? ret : usecs_to_jiffies(tmo);
276 }
277
278 int hif_stop_scan(struct wfx_vif *wvif)
279 {
280 int ret;
281 struct hif_msg *hif;
282 // body associated to HIF_REQ_ID_STOP_SCAN is empty
283 wfx_alloc_hif(0, &hif);
284
285 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_STOP_SCAN, 0);
286 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
287 kfree(hif);
288 return ret;
289 }
290
291 int hif_join(struct wfx_vif *wvif, const struct hif_req_join *arg)
292 {
293 int ret;
294 struct hif_msg *hif;
295 struct hif_req_join *body = wfx_alloc_hif(sizeof(*body), &hif);
296
297 memcpy(body, arg, sizeof(struct hif_req_join));
298 cpu_to_le16s(&body->channel_number);
299 cpu_to_le16s(&body->atim_window);
300 cpu_to_le32s(&body->ssid_length);
301 cpu_to_le32s(&body->beacon_interval);
302 cpu_to_le32s(&body->basic_rate_set);
303 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_JOIN, sizeof(*body));
304 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
305 kfree(hif);
306 return ret;
307 }
308
309 int hif_set_bss_params(struct wfx_vif *wvif,
310 const struct hif_req_set_bss_params *arg)
311 {
312 int ret;
313 struct hif_msg *hif;
314 struct hif_req_set_bss_params *body = wfx_alloc_hif(sizeof(*body),
315 &hif);
316
317 memcpy(body, arg, sizeof(*body));
318 cpu_to_le16s(&body->aid);
319 cpu_to_le32s(&body->operational_rate_set);
320 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_BSS_PARAMS,
321 sizeof(*body));
322 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
323 kfree(hif);
324 return ret;
325 }
326
327 int hif_add_key(struct wfx_dev *wdev, const struct hif_req_add_key *arg)
328 {
329 int ret;
330 struct hif_msg *hif;
331 // FIXME: only send necessary bits
332 struct hif_req_add_key *body = wfx_alloc_hif(sizeof(*body), &hif);
333
334 // FIXME: swap bytes as necessary in body
335 memcpy(body, arg, sizeof(*body));
336 if (wfx_api_older_than(wdev, 1, 5))
337 // Legacy firmwares expect that add_key to be sent on right
338 // interface.
339 wfx_fill_header(hif, arg->int_id, HIF_REQ_ID_ADD_KEY,
340 sizeof(*body));
341 else
342 wfx_fill_header(hif, -1, HIF_REQ_ID_ADD_KEY, sizeof(*body));
343 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
344 kfree(hif);
345 return ret;
346 }
347
348 int hif_remove_key(struct wfx_dev *wdev, int idx)
349 {
350 int ret;
351 struct hif_msg *hif;
352 struct hif_req_remove_key *body = wfx_alloc_hif(sizeof(*body), &hif);
353
354 body->entry_index = idx;
355 wfx_fill_header(hif, -1, HIF_REQ_ID_REMOVE_KEY, sizeof(*body));
356 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
357 kfree(hif);
358 return ret;
359 }
360
361 int hif_set_edca_queue_params(struct wfx_vif *wvif, u16 queue,
362 const struct ieee80211_tx_queue_params *arg)
363 {
364 int ret;
365 struct hif_msg *hif;
366 struct hif_req_edca_queue_params *body = wfx_alloc_hif(sizeof(*body),
367 &hif);
368
369 if (!body)
370 return -ENOMEM;
371
372 WARN_ON(arg->aifs > 255);
373 body->aifsn = arg->aifs;
374 body->cw_min = cpu_to_le16(arg->cw_min);
375 body->cw_max = cpu_to_le16(arg->cw_max);
376 body->tx_op_limit = cpu_to_le16(arg->txop * USEC_PER_TXOP);
377 body->queue_id = 3 - queue;
378 // API 2.0 has changed queue IDs values
379 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BE)
380 body->queue_id = HIF_QUEUE_ID_BACKGROUND;
381 if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BK)
382 body->queue_id = HIF_QUEUE_ID_BESTEFFORT;
383 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_EDCA_QUEUE_PARAMS,
384 sizeof(*body));
385 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
386 kfree(hif);
387 return ret;
388 }
389
390 int hif_set_pm(struct wfx_vif *wvif, bool ps, int dynamic_ps_timeout)
391 {
392 int ret;
393 struct hif_msg *hif;
394 struct hif_req_set_pm_mode *body = wfx_alloc_hif(sizeof(*body), &hif);
395
396 if (!body)
397 return -ENOMEM;
398
399 if (ps) {
400 body->pm_mode.enter_psm = 1;
401 // Firmware does not support more than 128ms
402 body->fast_psm_idle_period = min(dynamic_ps_timeout * 2, 255);
403 if (body->fast_psm_idle_period)
404 body->pm_mode.fast_psm = 1;
405 }
406 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_PM_MODE, sizeof(*body));
407 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
408 kfree(hif);
409 return ret;
410 }
411
412 int hif_start(struct wfx_vif *wvif, const struct hif_req_start *arg)
413 {
414 int ret;
415 struct hif_msg *hif;
416 struct hif_req_start *body = wfx_alloc_hif(sizeof(*body), &hif);
417
418 memcpy(body, arg, sizeof(*body));
419 cpu_to_le16s(&body->channel_number);
420 cpu_to_le32s(&body->beacon_interval);
421 cpu_to_le32s(&body->basic_rate_set);
422 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START, sizeof(*body));
423 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
424 kfree(hif);
425 return ret;
426 }
427
428 int hif_beacon_transmit(struct wfx_vif *wvif, bool enable_beaconing)
429 {
430 int ret;
431 struct hif_msg *hif;
432 struct hif_req_beacon_transmit *body = wfx_alloc_hif(sizeof(*body),
433 &hif);
434
435 body->enable_beaconing = enable_beaconing ? 1 : 0;
436 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_BEACON_TRANSMIT,
437 sizeof(*body));
438 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
439 kfree(hif);
440 return ret;
441 }
442
443 int hif_map_link(struct wfx_vif *wvif, u8 *mac_addr, int flags, int sta_id)
444 {
445 int ret;
446 struct hif_msg *hif;
447 struct hif_req_map_link *body = wfx_alloc_hif(sizeof(*body), &hif);
448
449 if (mac_addr)
450 ether_addr_copy(body->mac_addr, mac_addr);
451 body->map_link_flags = *(struct hif_map_link_flags *) &flags;
452 body->peer_sta_id = sta_id;
453 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_MAP_LINK, sizeof(*body));
454 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
455 kfree(hif);
456 return ret;
457 }
458
459 int hif_update_ie(struct wfx_vif *wvif, const struct hif_ie_flags *target_frame,
460 const u8 *ies, size_t ies_len)
461 {
462 int ret;
463 struct hif_msg *hif;
464 int buf_len = sizeof(struct hif_req_update_ie) + ies_len;
465 struct hif_req_update_ie *body = wfx_alloc_hif(buf_len, &hif);
466
467 memcpy(&body->ie_flags, target_frame, sizeof(struct hif_ie_flags));
468 body->num_ies = cpu_to_le16(1);
469 memcpy(body->ie, ies, ies_len);
470 wfx_fill_header(hif, wvif->id, HIF_REQ_ID_UPDATE_IE, buf_len);
471 ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
472 kfree(hif);
473 return ret;
474 }
475
476 int hif_sl_send_pub_keys(struct wfx_dev *wdev, const uint8_t *pubkey,
477 const uint8_t *pubkey_hmac)
478 {
479 int ret;
480 struct hif_msg *hif;
481 struct hif_req_sl_exchange_pub_keys *body = wfx_alloc_hif(sizeof(*body),
482 &hif);
483
484 body->algorithm = HIF_SL_CURVE25519;
485 memcpy(body->host_pub_key, pubkey, sizeof(body->host_pub_key));
486 memcpy(body->host_pub_key_mac, pubkey_hmac,
487 sizeof(body->host_pub_key_mac));
488 wfx_fill_header(hif, -1, HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS,
489 sizeof(*body));
490 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
491 kfree(hif);
492 // Compatibility with legacy secure link
493 if (ret == SL_PUB_KEY_EXCHANGE_STATUS_SUCCESS)
494 ret = 0;
495 return ret;
496 }
497
498 int hif_sl_config(struct wfx_dev *wdev, const unsigned long *bitmap)
499 {
500 int ret;
501 struct hif_msg *hif;
502 struct hif_req_sl_configure *body = wfx_alloc_hif(sizeof(*body), &hif);
503
504 memcpy(body->encr_bmp, bitmap, sizeof(body->encr_bmp));
505 wfx_fill_header(hif, -1, HIF_REQ_ID_SL_CONFIGURE, sizeof(*body));
506 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
507 kfree(hif);
508 return ret;
509 }
510
511 int hif_sl_set_mac_key(struct wfx_dev *wdev, const u8 *slk_key,
512 int destination)
513 {
514 int ret;
515 struct hif_msg *hif;
516 struct hif_req_set_sl_mac_key *body = wfx_alloc_hif(sizeof(*body),
517 &hif);
518
519 memcpy(body->key_value, slk_key, sizeof(body->key_value));
520 body->otp_or_ram = destination;
521 wfx_fill_header(hif, -1, HIF_REQ_ID_SET_SL_MAC_KEY, sizeof(*body));
522 ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
523 kfree(hif);
524 // Compatibility with legacy secure link
525 if (ret == SL_MAC_KEY_STATUS_SUCCESS)
526 ret = 0;
527 return ret;
528 }