Merge tag 'pci-v5.7-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaas/pci
[linux-2.6-microblaze.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         mutex_unlock(&wdev->hif_cmd.key_renew_lock);
144         kfree(hif);
145         return ret;
146 }
147
148 int hif_configuration(struct wfx_dev *wdev, const u8 *conf, size_t len)
149 {
150         int ret;
151         size_t buf_len = sizeof(struct hif_req_configuration) + len;
152         struct hif_msg *hif;
153         struct hif_req_configuration *body = wfx_alloc_hif(buf_len, &hif);
154
155         body->length = cpu_to_le16(len);
156         memcpy(body->pds_data, conf, len);
157         wfx_fill_header(hif, -1, HIF_REQ_ID_CONFIGURATION, buf_len);
158         ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
159         kfree(hif);
160         return ret;
161 }
162
163 int hif_reset(struct wfx_vif *wvif, bool reset_stat)
164 {
165         int ret;
166         struct hif_msg *hif;
167         struct hif_req_reset *body = wfx_alloc_hif(sizeof(*body), &hif);
168
169         body->reset_flags.reset_stat = reset_stat;
170         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_RESET, sizeof(*body));
171         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
172         kfree(hif);
173         return ret;
174 }
175
176 int hif_read_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val,
177                  size_t val_len)
178 {
179         int ret;
180         struct hif_msg *hif;
181         int buf_len = sizeof(struct hif_cnf_read_mib) + val_len;
182         struct hif_req_read_mib *body = wfx_alloc_hif(sizeof(*body), &hif);
183         struct hif_cnf_read_mib *reply = kmalloc(buf_len, GFP_KERNEL);
184
185         body->mib_id = cpu_to_le16(mib_id);
186         wfx_fill_header(hif, vif_id, HIF_REQ_ID_READ_MIB, sizeof(*body));
187         ret = wfx_cmd_send(wdev, hif, reply, buf_len, false);
188
189         if (!ret && mib_id != reply->mib_id) {
190                 dev_warn(wdev->dev,
191                          "%s: confirmation mismatch request\n", __func__);
192                 ret = -EIO;
193         }
194         if (ret == -ENOMEM)
195                 dev_err(wdev->dev,
196                         "buffer is too small to receive %s (%zu < %d)\n",
197                         get_mib_name(mib_id), val_len, reply->length);
198         if (!ret)
199                 memcpy(val, &reply->mib_data, reply->length);
200         else
201                 memset(val, 0xFF, val_len);
202         kfree(hif);
203         kfree(reply);
204         return ret;
205 }
206
207 int hif_write_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val,
208                   size_t val_len)
209 {
210         int ret;
211         struct hif_msg *hif;
212         int buf_len = sizeof(struct hif_req_write_mib) + val_len;
213         struct hif_req_write_mib *body = wfx_alloc_hif(buf_len, &hif);
214
215         body->mib_id = cpu_to_le16(mib_id);
216         body->length = cpu_to_le16(val_len);
217         memcpy(&body->mib_data, val, val_len);
218         wfx_fill_header(hif, vif_id, HIF_REQ_ID_WRITE_MIB, buf_len);
219         ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
220         kfree(hif);
221         return ret;
222 }
223
224 int hif_scan(struct wfx_vif *wvif, struct cfg80211_scan_request *req,
225              int chan_start_idx, int chan_num)
226 {
227         int ret, i;
228         struct hif_msg *hif;
229         size_t buf_len =
230                 sizeof(struct hif_req_start_scan_alt) + chan_num * sizeof(u8);
231         struct hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif);
232         int tmo_chan_fg, tmo_chan_bg, tmo;
233
234         WARN(chan_num > HIF_API_MAX_NB_CHANNELS, "invalid params");
235         WARN(req->n_ssids > HIF_API_MAX_NB_SSIDS, "invalid params");
236
237         compiletime_assert(IEEE80211_MAX_SSID_LEN == HIF_API_SSID_SIZE,
238                            "API inconsistency");
239         for (i = 0; i < req->n_ssids; i++) {
240                 memcpy(body->ssid_def[i].ssid, req->ssids[i].ssid,
241                        IEEE80211_MAX_SSID_LEN);
242                 body->ssid_def[i].ssid_length =
243                         cpu_to_le32(req->ssids[i].ssid_len);
244         }
245         body->num_of_ssids = HIF_API_MAX_NB_SSIDS;
246         // Background scan is always a good idea
247         body->scan_type.type = 1;
248         body->scan_flags.fbg = 1;
249         body->tx_power_level =
250                 cpu_to_le32(req->channels[chan_start_idx]->max_power);
251         body->num_of_channels = chan_num;
252         for (i = 0; i < chan_num; i++)
253                 body->channel_list[i] =
254                         req->channels[i + chan_start_idx]->hw_value;
255         if (req->no_cck)
256                 body->max_transmit_rate = API_RATE_INDEX_G_6MBPS;
257         else
258                 body->max_transmit_rate = API_RATE_INDEX_B_1MBPS;
259         if (req->channels[chan_start_idx]->flags & IEEE80211_CHAN_NO_IR) {
260                 body->min_channel_time = cpu_to_le32(50);
261                 body->max_channel_time = cpu_to_le32(150);
262         } else {
263                 body->min_channel_time = cpu_to_le32(10);
264                 body->max_channel_time = cpu_to_le32(50);
265                 body->num_of_probe_requests = 2;
266                 body->probe_delay = 100;
267         }
268         tmo_chan_bg = le32_to_cpu(body->max_channel_time) * USEC_PER_TU;
269         tmo_chan_fg = 512 * USEC_PER_TU + body->probe_delay;
270         tmo_chan_fg *= body->num_of_probe_requests;
271         tmo = chan_num * max(tmo_chan_bg, tmo_chan_fg);
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 ? ret : usecs_to_jiffies(tmo);
277 }
278
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         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_STOP_SCAN, 0);
287         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
288         kfree(hif);
289         return ret;
290 }
291
292 int hif_join(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
293              struct ieee80211_channel *channel, const u8 *ssid, int ssidlen)
294 {
295         int ret;
296         struct hif_msg *hif;
297         struct hif_req_join *body = wfx_alloc_hif(sizeof(*body), &hif);
298
299         WARN_ON(!conf->basic_rates);
300         body->infrastructure_bss_mode = !conf->ibss_joined;
301         body->short_preamble = conf->use_short_preamble;
302         if (channel && channel->flags & IEEE80211_CHAN_NO_IR)
303                 body->probe_for_join = 0;
304         else
305                 body->probe_for_join = 1;
306         body->channel_number = cpu_to_le16(channel->hw_value);
307         body->beacon_interval = cpu_to_le32(conf->beacon_int);
308         body->basic_rate_set =
309                 cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
310         memcpy(body->bssid, conf->bssid, sizeof(body->bssid));
311         if (!conf->ibss_joined && ssid) {
312                 body->ssid_length = cpu_to_le32(ssidlen);
313                 memcpy(body->ssid, ssid, ssidlen);
314         }
315         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_JOIN, sizeof(*body));
316         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
317         kfree(hif);
318         return ret;
319 }
320
321 int hif_set_bss_params(struct wfx_vif *wvif,
322                        const struct hif_req_set_bss_params *arg)
323 {
324         int ret;
325         struct hif_msg *hif;
326         struct hif_req_set_bss_params *body = wfx_alloc_hif(sizeof(*body),
327                                                             &hif);
328
329         memcpy(body, arg, sizeof(*body));
330         cpu_to_le16s(&body->aid);
331         cpu_to_le32s(&body->operational_rate_set);
332         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_BSS_PARAMS,
333                         sizeof(*body));
334         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
335         kfree(hif);
336         return ret;
337 }
338
339 int hif_add_key(struct wfx_dev *wdev, const struct hif_req_add_key *arg)
340 {
341         int ret;
342         struct hif_msg *hif;
343         // FIXME: only send necessary bits
344         struct hif_req_add_key *body = wfx_alloc_hif(sizeof(*body), &hif);
345
346         // FIXME: swap bytes as necessary in body
347         memcpy(body, arg, sizeof(*body));
348         if (wfx_api_older_than(wdev, 1, 5))
349                 // Legacy firmwares expect that add_key to be sent on right
350                 // interface.
351                 wfx_fill_header(hif, arg->int_id, HIF_REQ_ID_ADD_KEY,
352                                 sizeof(*body));
353         else
354                 wfx_fill_header(hif, -1, HIF_REQ_ID_ADD_KEY, sizeof(*body));
355         ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
356         kfree(hif);
357         return ret;
358 }
359
360 int hif_remove_key(struct wfx_dev *wdev, int idx)
361 {
362         int ret;
363         struct hif_msg *hif;
364         struct hif_req_remove_key *body = wfx_alloc_hif(sizeof(*body), &hif);
365
366         body->entry_index = idx;
367         wfx_fill_header(hif, -1, HIF_REQ_ID_REMOVE_KEY, sizeof(*body));
368         ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
369         kfree(hif);
370         return ret;
371 }
372
373 int hif_set_edca_queue_params(struct wfx_vif *wvif, u16 queue,
374                               const struct ieee80211_tx_queue_params *arg)
375 {
376         int ret;
377         struct hif_msg *hif;
378         struct hif_req_edca_queue_params *body = wfx_alloc_hif(sizeof(*body),
379                                                                &hif);
380
381         if (!body)
382                 return -ENOMEM;
383
384         WARN_ON(arg->aifs > 255);
385         body->aifsn = arg->aifs;
386         body->cw_min = cpu_to_le16(arg->cw_min);
387         body->cw_max = cpu_to_le16(arg->cw_max);
388         body->tx_op_limit = cpu_to_le16(arg->txop * USEC_PER_TXOP);
389         body->queue_id = 3 - queue;
390         // API 2.0 has changed queue IDs values
391         if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BE)
392                 body->queue_id = HIF_QUEUE_ID_BACKGROUND;
393         if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BK)
394                 body->queue_id = HIF_QUEUE_ID_BESTEFFORT;
395         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_EDCA_QUEUE_PARAMS,
396                         sizeof(*body));
397         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
398         kfree(hif);
399         return ret;
400 }
401
402 int hif_set_pm(struct wfx_vif *wvif, bool ps, int dynamic_ps_timeout)
403 {
404         int ret;
405         struct hif_msg *hif;
406         struct hif_req_set_pm_mode *body = wfx_alloc_hif(sizeof(*body), &hif);
407
408         if (!body)
409                 return -ENOMEM;
410
411         if (ps) {
412                 body->pm_mode.enter_psm = 1;
413                 // Firmware does not support more than 128ms
414                 body->fast_psm_idle_period = min(dynamic_ps_timeout * 2, 255);
415                 if (body->fast_psm_idle_period)
416                         body->pm_mode.fast_psm = 1;
417         }
418         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_PM_MODE, sizeof(*body));
419         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
420         kfree(hif);
421         return ret;
422 }
423
424 int hif_start(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
425               const struct ieee80211_channel *channel)
426 {
427         int ret;
428         struct hif_msg *hif;
429         struct hif_req_start *body = wfx_alloc_hif(sizeof(*body), &hif);
430
431         body->dtim_period = conf->dtim_period;
432         body->short_preamble = conf->use_short_preamble;
433         body->channel_number = cpu_to_le16(channel->hw_value);
434         body->beacon_interval = cpu_to_le32(conf->beacon_int);
435         body->basic_rate_set =
436                 cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
437         body->ssid_length = conf->ssid_len;
438         memcpy(body->ssid, conf->ssid, conf->ssid_len);
439         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START, sizeof(*body));
440         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
441         kfree(hif);
442         return ret;
443 }
444
445 int hif_beacon_transmit(struct wfx_vif *wvif, bool enable)
446 {
447         int ret;
448         struct hif_msg *hif;
449         struct hif_req_beacon_transmit *body = wfx_alloc_hif(sizeof(*body),
450                                                              &hif);
451
452         body->enable_beaconing = enable ? 1 : 0;
453         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_BEACON_TRANSMIT,
454                         sizeof(*body));
455         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
456         kfree(hif);
457         return ret;
458 }
459
460 int hif_map_link(struct wfx_vif *wvif, u8 *mac_addr, int flags, int sta_id)
461 {
462         int ret;
463         struct hif_msg *hif;
464         struct hif_req_map_link *body = wfx_alloc_hif(sizeof(*body), &hif);
465
466         if (mac_addr)
467                 ether_addr_copy(body->mac_addr, mac_addr);
468         body->map_link_flags = *(struct hif_map_link_flags *) &flags;
469         body->peer_sta_id = sta_id;
470         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_MAP_LINK, sizeof(*body));
471         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
472         kfree(hif);
473         return ret;
474 }
475
476 int hif_update_ie_beacon(struct wfx_vif *wvif, const u8 *ies, size_t ies_len)
477 {
478         int ret;
479         struct hif_msg *hif;
480         int buf_len = sizeof(struct hif_req_update_ie) + ies_len;
481         struct hif_req_update_ie *body = wfx_alloc_hif(buf_len, &hif);
482
483         body->ie_flags.beacon = 1;
484         body->num_ies = cpu_to_le16(1);
485         memcpy(body->ie, ies, ies_len);
486         wfx_fill_header(hif, wvif->id, HIF_REQ_ID_UPDATE_IE, buf_len);
487         ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
488         kfree(hif);
489         return ret;
490 }
491
492 int hif_sl_send_pub_keys(struct wfx_dev *wdev, const uint8_t *pubkey,
493                          const uint8_t *pubkey_hmac)
494 {
495         int ret;
496         struct hif_msg *hif;
497         struct hif_req_sl_exchange_pub_keys *body = wfx_alloc_hif(sizeof(*body),
498                                                                   &hif);
499
500         body->algorithm = HIF_SL_CURVE25519;
501         memcpy(body->host_pub_key, pubkey, sizeof(body->host_pub_key));
502         memcpy(body->host_pub_key_mac, pubkey_hmac,
503                sizeof(body->host_pub_key_mac));
504         wfx_fill_header(hif, -1, HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS,
505                         sizeof(*body));
506         ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
507         kfree(hif);
508         // Compatibility with legacy secure link
509         if (ret == SL_PUB_KEY_EXCHANGE_STATUS_SUCCESS)
510                 ret = 0;
511         return ret;
512 }
513
514 int hif_sl_config(struct wfx_dev *wdev, const unsigned long *bitmap)
515 {
516         int ret;
517         struct hif_msg *hif;
518         struct hif_req_sl_configure *body = wfx_alloc_hif(sizeof(*body), &hif);
519
520         memcpy(body->encr_bmp, bitmap, sizeof(body->encr_bmp));
521         wfx_fill_header(hif, -1, HIF_REQ_ID_SL_CONFIGURE, sizeof(*body));
522         ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
523         kfree(hif);
524         return ret;
525 }
526
527 int hif_sl_set_mac_key(struct wfx_dev *wdev, const u8 *slk_key,
528                        int destination)
529 {
530         int ret;
531         struct hif_msg *hif;
532         struct hif_req_set_sl_mac_key *body = wfx_alloc_hif(sizeof(*body),
533                                                             &hif);
534
535         memcpy(body->key_value, slk_key, sizeof(body->key_value));
536         body->otp_or_ram = destination;
537         wfx_fill_header(hif, -1, HIF_REQ_ID_SET_SL_MAC_KEY, sizeof(*body));
538         ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
539         kfree(hif);
540         // Compatibility with legacy secure link
541         if (ret == SL_MAC_KEY_STATUS_SUCCESS)
542                 ret = 0;
543         return ret;
544 }