d6dfab094b0318f13b1934a5a04cd8582a6ea10e
[platform/kernel/linux-starfive.git] / drivers / staging / wfx / hif_rx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Implementation of chip-to-host event (aka indications) of WFxxx Split Mac
4  * (WSM) API.
5  *
6  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
7  * Copyright (c) 2010, ST-Ericsson
8  */
9 #include <linux/skbuff.h>
10 #include <linux/etherdevice.h>
11
12 #include "hif_rx.h"
13 #include "wfx.h"
14 #include "scan.h"
15 #include "bh.h"
16 #include "sta.h"
17 #include "data_rx.h"
18 #include "hif_api_cmd.h"
19
20 static int hif_generic_confirm(struct wfx_dev *wdev,
21                                const struct hif_msg *hif, const void *buf)
22 {
23         // All confirm messages start with status
24         int status = le32_to_cpup((__le32 *)buf);
25         int cmd = hif->id;
26         int len = le16_to_cpu(hif->len) - 4; // drop header
27
28         WARN(!mutex_is_locked(&wdev->hif_cmd.lock), "data locking error");
29
30         if (!wdev->hif_cmd.buf_send) {
31                 dev_warn(wdev->dev, "unexpected confirmation: 0x%.2x\n", cmd);
32                 return -EINVAL;
33         }
34
35         if (cmd != wdev->hif_cmd.buf_send->id) {
36                 dev_warn(wdev->dev,
37                          "chip response mismatch request: 0x%.2x vs 0x%.2x\n",
38                          cmd, wdev->hif_cmd.buf_send->id);
39                 return -EINVAL;
40         }
41
42         if (wdev->hif_cmd.buf_recv) {
43                 if (wdev->hif_cmd.len_recv >= len)
44                         memcpy(wdev->hif_cmd.buf_recv, buf, len);
45                 else
46                         status = -ENOMEM;
47         }
48         wdev->hif_cmd.ret = status;
49
50         complete(&wdev->hif_cmd.done);
51         return status;
52 }
53
54 static int hif_tx_confirm(struct wfx_dev *wdev,
55                           const struct hif_msg *hif, const void *buf)
56 {
57         const struct hif_cnf_tx *body = buf;
58
59         wfx_tx_confirm_cb(wdev, body);
60         return 0;
61 }
62
63 static int hif_multi_tx_confirm(struct wfx_dev *wdev,
64                                 const struct hif_msg *hif, const void *buf)
65 {
66         const struct hif_cnf_multi_transmit *body = buf;
67         int i;
68
69         WARN(body->num_tx_confs <= 0, "corrupted message");
70         for (i = 0; i < body->num_tx_confs; i++)
71                 wfx_tx_confirm_cb(wdev, &body->tx_conf_payload[i]);
72         return 0;
73 }
74
75 static int hif_startup_indication(struct wfx_dev *wdev,
76                                   const struct hif_msg *hif, const void *buf)
77 {
78         const struct hif_ind_startup *body = buf;
79
80         if (body->status || body->firmware_type > 4) {
81                 dev_err(wdev->dev, "received invalid startup indication");
82                 return -EINVAL;
83         }
84         memcpy(&wdev->hw_caps, body, sizeof(struct hif_ind_startup));
85         le16_to_cpus((__le16 *)&wdev->hw_caps.hardware_id);
86         le16_to_cpus((__le16 *)&wdev->hw_caps.num_inp_ch_bufs);
87         le16_to_cpus((__le16 *)&wdev->hw_caps.size_inp_ch_buf);
88         le32_to_cpus((__le32 *)&wdev->hw_caps.supported_rate_mask);
89
90         complete(&wdev->firmware_ready);
91         return 0;
92 }
93
94 static int hif_wakeup_indication(struct wfx_dev *wdev,
95                                  const struct hif_msg *hif, const void *buf)
96 {
97         if (!wdev->pdata.gpio_wakeup
98             || !gpiod_get_value(wdev->pdata.gpio_wakeup)) {
99                 dev_warn(wdev->dev, "unexpected wake-up indication\n");
100                 return -EIO;
101         }
102         return 0;
103 }
104
105 static int hif_receive_indication(struct wfx_dev *wdev,
106                                   const struct hif_msg *hif,
107                                   const void *buf, struct sk_buff *skb)
108 {
109         struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
110         const struct hif_ind_rx *body = buf;
111
112         if (!wvif) {
113                 dev_warn(wdev->dev, "ignore rx data for non-existent vif %d\n",
114                          hif->interface);
115                 return 0;
116         }
117         skb_pull(skb, sizeof(struct hif_msg) + sizeof(struct hif_ind_rx));
118         wfx_rx_cb(wvif, body, skb);
119
120         return 0;
121 }
122
123 static int hif_event_indication(struct wfx_dev *wdev,
124                                 const struct hif_msg *hif, const void *buf)
125 {
126         struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
127         const struct hif_ind_event *body = buf;
128         int type = le32_to_cpu(body->event_id);
129
130         if (!wvif) {
131                 dev_warn(wdev->dev, "received event for non-existent vif\n");
132                 return 0;
133         }
134
135         switch (type) {
136         case HIF_EVENT_IND_RCPI_RSSI:
137                 wfx_event_report_rssi(wvif, body->event_data.rcpi_rssi);
138                 break;
139         case HIF_EVENT_IND_BSSLOST:
140                 schedule_delayed_work(&wvif->beacon_loss_work, 0);
141                 break;
142         case HIF_EVENT_IND_BSSREGAINED:
143                 cancel_delayed_work(&wvif->beacon_loss_work);
144                 dev_dbg(wdev->dev, "ignore BSSREGAINED indication\n");
145                 break;
146         case HIF_EVENT_IND_PS_MODE_ERROR:
147                 dev_warn(wdev->dev, "error while processing power save request: %d\n",
148                          le32_to_cpu(body->event_data.ps_mode_error));
149                 break;
150         default:
151                 dev_warn(wdev->dev, "unhandled event indication: %.2x\n",
152                          type);
153                 break;
154         }
155         return 0;
156 }
157
158 static int hif_pm_mode_complete_indication(struct wfx_dev *wdev,
159                                            const struct hif_msg *hif,
160                                            const void *buf)
161 {
162         struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
163
164         WARN_ON(!wvif);
165         complete(&wvif->set_pm_mode_complete);
166
167         return 0;
168 }
169
170 static int hif_scan_complete_indication(struct wfx_dev *wdev,
171                                         const struct hif_msg *hif,
172                                         const void *buf)
173 {
174         struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
175
176         WARN_ON(!wvif);
177         wfx_scan_complete(wvif);
178
179         return 0;
180 }
181
182 static int hif_join_complete_indication(struct wfx_dev *wdev,
183                                         const struct hif_msg *hif,
184                                         const void *buf)
185 {
186         struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
187
188         WARN_ON(!wvif);
189         dev_warn(wdev->dev, "unattended JoinCompleteInd\n");
190
191         return 0;
192 }
193
194 static int hif_suspend_resume_indication(struct wfx_dev *wdev,
195                                          const struct hif_msg *hif,
196                                          const void *buf)
197 {
198         struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
199         const struct hif_ind_suspend_resume_tx *body = buf;
200
201         if (body->bc_mc_only) {
202                 WARN_ON(!wvif);
203                 if (body->resume)
204                         wfx_suspend_resume_mc(wvif, STA_NOTIFY_AWAKE);
205                 else
206                         wfx_suspend_resume_mc(wvif, STA_NOTIFY_SLEEP);
207         } else {
208                 WARN(body->peer_sta_set, "misunderstood indication");
209                 WARN(hif->interface != 2, "misunderstood indication");
210                 if (body->resume)
211                         wfx_suspend_hot_dev(wdev, STA_NOTIFY_AWAKE);
212                 else
213                         wfx_suspend_hot_dev(wdev, STA_NOTIFY_SLEEP);
214         }
215
216         return 0;
217 }
218
219 static int hif_generic_indication(struct wfx_dev *wdev,
220                                   const struct hif_msg *hif, const void *buf)
221 {
222         const struct hif_ind_generic *body = buf;
223         int type = le32_to_cpu(body->type);
224
225         switch (type) {
226         case HIF_GENERIC_INDICATION_TYPE_RAW:
227                 return 0;
228         case HIF_GENERIC_INDICATION_TYPE_STRING:
229                 dev_info(wdev->dev, "firmware says: %s\n", (char *)&body->data);
230                 return 0;
231         case HIF_GENERIC_INDICATION_TYPE_RX_STATS:
232                 mutex_lock(&wdev->rx_stats_lock);
233                 // Older firmware send a generic indication beside RxStats
234                 if (!wfx_api_older_than(wdev, 1, 4))
235                         dev_info(wdev->dev, "Rx test ongoing. Temperature: %d°C\n",
236                                  body->data.rx_stats.current_temp);
237                 memcpy(&wdev->rx_stats, &body->data.rx_stats,
238                        sizeof(wdev->rx_stats));
239                 mutex_unlock(&wdev->rx_stats_lock);
240                 return 0;
241         case HIF_GENERIC_INDICATION_TYPE_TX_POWER_LOOP_INFO:
242                 mutex_lock(&wdev->tx_power_loop_info_lock);
243                 memcpy(&wdev->tx_power_loop_info,
244                        &body->data.tx_power_loop_info,
245                        sizeof(wdev->tx_power_loop_info));
246                 mutex_unlock(&wdev->tx_power_loop_info_lock);
247                 return 0;
248         default:
249                 dev_err(wdev->dev, "generic_indication: unknown indication type: %#.8x\n",
250                         type);
251                 return -EIO;
252         }
253 }
254
255 static const struct {
256         int val;
257         const char *str;
258         bool has_param;
259 } hif_errors[] = {
260         { HIF_ERROR_FIRMWARE_ROLLBACK,
261                 "rollback status" },
262         { HIF_ERROR_FIRMWARE_DEBUG_ENABLED,
263                 "debug feature enabled" },
264         { HIF_ERROR_PDS_PAYLOAD,
265                 "PDS version is not supported" },
266         { HIF_ERROR_PDS_TESTFEATURE,
267                 "PDS ask for an unknown test mode" },
268         { HIF_ERROR_OOR_VOLTAGE,
269                 "out-of-range power supply voltage", true },
270         { HIF_ERROR_OOR_TEMPERATURE,
271                 "out-of-range temperature", true },
272         { HIF_ERROR_SLK_REQ_DURING_KEY_EXCHANGE,
273                 "secure link does not expect request during key exchange" },
274         { HIF_ERROR_SLK_SESSION_KEY,
275                 "secure link session key is invalid" },
276         { HIF_ERROR_SLK_OVERFLOW,
277                 "secure link overflow" },
278         { HIF_ERROR_SLK_WRONG_ENCRYPTION_STATE,
279                 "secure link messages list does not match message encryption" },
280         { HIF_ERROR_SLK_UNCONFIGURED,
281                 "secure link not yet configured" },
282         { HIF_ERROR_HIF_BUS_FREQUENCY_TOO_LOW,
283                 "bus clock is too slow (<1kHz)" },
284         { HIF_ERROR_HIF_RX_DATA_TOO_LARGE,
285                 "HIF message too large" },
286         // Following errors only exists in old firmware versions:
287         { HIF_ERROR_HIF_TX_QUEUE_FULL,
288                 "HIF messages queue is full" },
289         { HIF_ERROR_HIF_BUS,
290                 "HIF bus" },
291         { HIF_ERROR_SLK_MULTI_TX_UNSUPPORTED,
292                 "secure link does not support multi-tx confirmations" },
293         { HIF_ERROR_SLK_OUTDATED_SESSION_KEY,
294                 "secure link session key is outdated" },
295         { HIF_ERROR_SLK_DECRYPTION,
296                 "secure link params (nonce or tag) mismatch" },
297 };
298
299 static int hif_error_indication(struct wfx_dev *wdev,
300                                 const struct hif_msg *hif, const void *buf)
301 {
302         const struct hif_ind_error *body = buf;
303         int type = le32_to_cpu(body->type);
304         int param = (s8)body->data[0];
305         int i;
306
307         for (i = 0; i < ARRAY_SIZE(hif_errors); i++)
308                 if (type == hif_errors[i].val)
309                         break;
310         if (i < ARRAY_SIZE(hif_errors))
311                 if (hif_errors[i].has_param)
312                         dev_err(wdev->dev, "asynchronous error: %s: %d\n",
313                                 hif_errors[i].str, param);
314                 else
315                         dev_err(wdev->dev, "asynchronous error: %s\n",
316                                 hif_errors[i].str);
317         else
318                 dev_err(wdev->dev, "asynchronous error: unknown: %08x\n", type);
319         print_hex_dump(KERN_INFO, "hif: ", DUMP_PREFIX_OFFSET,
320                        16, 1, hif, le16_to_cpu(hif->len), false);
321         wdev->chip_frozen = true;
322
323         return 0;
324 };
325
326 static int hif_exception_indication(struct wfx_dev *wdev,
327                                     const struct hif_msg *hif, const void *buf)
328 {
329         const struct hif_ind_exception *body = buf;
330         int type = le32_to_cpu(body->type);
331
332         if (type == 4)
333                 dev_err(wdev->dev, "firmware assert %d\n",
334                         le32_to_cpup((__le32 *)body->data));
335         else
336                 dev_err(wdev->dev, "firmware exception\n");
337         print_hex_dump(KERN_INFO, "hif: ", DUMP_PREFIX_OFFSET,
338                        16, 1, hif, le16_to_cpu(hif->len), false);
339         wdev->chip_frozen = true;
340
341         return -1;
342 }
343
344 static const struct {
345         int msg_id;
346         int (*handler)(struct wfx_dev *wdev,
347                        const struct hif_msg *hif, const void *buf);
348 } hif_handlers[] = {
349         /* Confirmations */
350         { HIF_CNF_ID_TX,                   hif_tx_confirm },
351         { HIF_CNF_ID_MULTI_TRANSMIT,       hif_multi_tx_confirm },
352         /* Indications */
353         { HIF_IND_ID_STARTUP,              hif_startup_indication },
354         { HIF_IND_ID_WAKEUP,               hif_wakeup_indication },
355         { HIF_IND_ID_JOIN_COMPLETE,        hif_join_complete_indication },
356         { HIF_IND_ID_SET_PM_MODE_CMPL,     hif_pm_mode_complete_indication },
357         { HIF_IND_ID_SCAN_CMPL,            hif_scan_complete_indication },
358         { HIF_IND_ID_SUSPEND_RESUME_TX,    hif_suspend_resume_indication },
359         { HIF_IND_ID_EVENT,                hif_event_indication },
360         { HIF_IND_ID_GENERIC,              hif_generic_indication },
361         { HIF_IND_ID_ERROR,                hif_error_indication },
362         { HIF_IND_ID_EXCEPTION,            hif_exception_indication },
363         // FIXME: allocate skb_p from hif_receive_indication and make it generic
364         //{ HIF_IND_ID_RX,                 hif_receive_indication },
365 };
366
367 void wfx_handle_rx(struct wfx_dev *wdev, struct sk_buff *skb)
368 {
369         int i;
370         const struct hif_msg *hif = (const struct hif_msg *)skb->data;
371         int hif_id = hif->id;
372
373         if (hif_id == HIF_IND_ID_RX) {
374                 // hif_receive_indication take care of skb lifetime
375                 hif_receive_indication(wdev, hif, hif->body, skb);
376                 return;
377         }
378         // Note: mutex_is_lock cause an implicit memory barrier that protect
379         // buf_send
380         if (mutex_is_locked(&wdev->hif_cmd.lock)
381             && wdev->hif_cmd.buf_send
382             && wdev->hif_cmd.buf_send->id == hif_id) {
383                 hif_generic_confirm(wdev, hif, hif->body);
384                 goto free;
385         }
386         for (i = 0; i < ARRAY_SIZE(hif_handlers); i++) {
387                 if (hif_handlers[i].msg_id == hif_id) {
388                         if (hif_handlers[i].handler)
389                                 hif_handlers[i].handler(wdev, hif, hif->body);
390                         goto free;
391                 }
392         }
393         if (hif_id & 0x80)
394                 dev_err(wdev->dev, "unsupported HIF indication: ID %02x\n",
395                         hif_id);
396         else
397                 dev_err(wdev->dev, "unexpected HIF confirmation: ID %02x\n",
398                         hif_id);
399 free:
400         dev_kfree_skb(skb);
401 }