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
hif_generic_confirm(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)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 && len > 0)
44 memcpy(wdev->hif_cmd.buf_recv, buf, len);
45 else
46 status = -EIO;
47 }
48 wdev->hif_cmd.ret = status;
49
50 complete(&wdev->hif_cmd.done);
51 return status;
52 }
53
hif_tx_confirm(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)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
hif_multi_tx_confirm(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)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
hif_startup_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)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
hif_wakeup_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)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) == 0) {
99 dev_warn(wdev->dev, "unexpected wake-up indication\n");
100 return -EIO;
101 }
102 return 0;
103 }
104
hif_receive_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf,struct sk_buff * skb)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, "%s: ignore rx data for non-existent vif %d\n",
114 __func__, hif->interface);
115 return -EIO;
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
hif_event_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)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, "%s: received event for non-existent vif\n", __func__);
132 return -EIO;
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
hif_pm_mode_complete_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)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 if (!wvif) {
165 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
166 return -EIO;
167 }
168 complete(&wvif->set_pm_mode_complete);
169
170 return 0;
171 }
172
hif_scan_complete_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)173 static int hif_scan_complete_indication(struct wfx_dev *wdev,
174 const struct hif_msg *hif,
175 const void *buf)
176 {
177 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
178
179 if (!wvif) {
180 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
181 return -EIO;
182 }
183
184 wfx_scan_complete(wvif);
185
186 return 0;
187 }
188
hif_join_complete_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)189 static int hif_join_complete_indication(struct wfx_dev *wdev,
190 const struct hif_msg *hif,
191 const void *buf)
192 {
193 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
194
195 if (!wvif) {
196 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
197 return -EIO;
198 }
199 dev_warn(wdev->dev, "unattended JoinCompleteInd\n");
200
201 return 0;
202 }
203
hif_suspend_resume_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)204 static int hif_suspend_resume_indication(struct wfx_dev *wdev,
205 const struct hif_msg *hif,
206 const void *buf)
207 {
208 const struct hif_ind_suspend_resume_tx *body = buf;
209 struct wfx_vif *wvif;
210
211 if (body->bc_mc_only) {
212 wvif = wdev_to_wvif(wdev, hif->interface);
213 if (!wvif) {
214 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
215 return -EIO;
216 }
217 if (body->resume)
218 wfx_suspend_resume_mc(wvif, STA_NOTIFY_AWAKE);
219 else
220 wfx_suspend_resume_mc(wvif, STA_NOTIFY_SLEEP);
221 } else {
222 WARN(body->peer_sta_set, "misunderstood indication");
223 WARN(hif->interface != 2, "misunderstood indication");
224 if (body->resume)
225 wfx_suspend_hot_dev(wdev, STA_NOTIFY_AWAKE);
226 else
227 wfx_suspend_hot_dev(wdev, STA_NOTIFY_SLEEP);
228 }
229
230 return 0;
231 }
232
hif_generic_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)233 static int hif_generic_indication(struct wfx_dev *wdev,
234 const struct hif_msg *hif, const void *buf)
235 {
236 const struct hif_ind_generic *body = buf;
237 int type = le32_to_cpu(body->type);
238
239 switch (type) {
240 case HIF_GENERIC_INDICATION_TYPE_RAW:
241 return 0;
242 case HIF_GENERIC_INDICATION_TYPE_STRING:
243 dev_info(wdev->dev, "firmware says: %s\n", (char *)&body->data);
244 return 0;
245 case HIF_GENERIC_INDICATION_TYPE_RX_STATS:
246 mutex_lock(&wdev->rx_stats_lock);
247 // Older firmware send a generic indication beside RxStats
248 if (!wfx_api_older_than(wdev, 1, 4))
249 dev_info(wdev->dev, "Rx test ongoing. Temperature: %d degrees C\n",
250 body->data.rx_stats.current_temp);
251 memcpy(&wdev->rx_stats, &body->data.rx_stats,
252 sizeof(wdev->rx_stats));
253 mutex_unlock(&wdev->rx_stats_lock);
254 return 0;
255 case HIF_GENERIC_INDICATION_TYPE_TX_POWER_LOOP_INFO:
256 mutex_lock(&wdev->tx_power_loop_info_lock);
257 memcpy(&wdev->tx_power_loop_info,
258 &body->data.tx_power_loop_info,
259 sizeof(wdev->tx_power_loop_info));
260 mutex_unlock(&wdev->tx_power_loop_info_lock);
261 return 0;
262 default:
263 dev_err(wdev->dev, "generic_indication: unknown indication type: %#.8x\n",
264 type);
265 return -EIO;
266 }
267 }
268
269 static const struct {
270 int val;
271 const char *str;
272 bool has_param;
273 } hif_errors[] = {
274 { HIF_ERROR_FIRMWARE_ROLLBACK,
275 "rollback status" },
276 { HIF_ERROR_FIRMWARE_DEBUG_ENABLED,
277 "debug feature enabled" },
278 { HIF_ERROR_PDS_PAYLOAD,
279 "PDS version is not supported" },
280 { HIF_ERROR_PDS_TESTFEATURE,
281 "PDS ask for an unknown test mode" },
282 { HIF_ERROR_OOR_VOLTAGE,
283 "out-of-range power supply voltage", true },
284 { HIF_ERROR_OOR_TEMPERATURE,
285 "out-of-range temperature", true },
286 { HIF_ERROR_SLK_REQ_DURING_KEY_EXCHANGE,
287 "secure link does not expect request during key exchange" },
288 { HIF_ERROR_SLK_SESSION_KEY,
289 "secure link session key is invalid" },
290 { HIF_ERROR_SLK_OVERFLOW,
291 "secure link overflow" },
292 { HIF_ERROR_SLK_WRONG_ENCRYPTION_STATE,
293 "secure link messages list does not match message encryption" },
294 { HIF_ERROR_SLK_UNCONFIGURED,
295 "secure link not yet configured" },
296 { HIF_ERROR_HIF_BUS_FREQUENCY_TOO_LOW,
297 "bus clock is too slow (<1kHz)" },
298 { HIF_ERROR_HIF_RX_DATA_TOO_LARGE,
299 "HIF message too large" },
300 // Following errors only exists in old firmware versions:
301 { HIF_ERROR_HIF_TX_QUEUE_FULL,
302 "HIF messages queue is full" },
303 { HIF_ERROR_HIF_BUS,
304 "HIF bus" },
305 { HIF_ERROR_SLK_MULTI_TX_UNSUPPORTED,
306 "secure link does not support multi-tx confirmations" },
307 { HIF_ERROR_SLK_OUTDATED_SESSION_KEY,
308 "secure link session key is outdated" },
309 { HIF_ERROR_SLK_DECRYPTION,
310 "secure link params (nonce or tag) mismatch" },
311 };
312
hif_error_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)313 static int hif_error_indication(struct wfx_dev *wdev,
314 const struct hif_msg *hif, const void *buf)
315 {
316 const struct hif_ind_error *body = buf;
317 int type = le32_to_cpu(body->type);
318 int param = (s8)body->data[0];
319 int i;
320
321 for (i = 0; i < ARRAY_SIZE(hif_errors); i++)
322 if (type == hif_errors[i].val)
323 break;
324 if (i < ARRAY_SIZE(hif_errors))
325 if (hif_errors[i].has_param)
326 dev_err(wdev->dev, "asynchronous error: %s: %d\n",
327 hif_errors[i].str, param);
328 else
329 dev_err(wdev->dev, "asynchronous error: %s\n",
330 hif_errors[i].str);
331 else
332 dev_err(wdev->dev, "asynchronous error: unknown: %08x\n", type);
333 print_hex_dump(KERN_INFO, "hif: ", DUMP_PREFIX_OFFSET,
334 16, 1, hif, le16_to_cpu(hif->len), false);
335 wdev->chip_frozen = true;
336
337 return 0;
338 };
339
hif_exception_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)340 static int hif_exception_indication(struct wfx_dev *wdev,
341 const struct hif_msg *hif, const void *buf)
342 {
343 const struct hif_ind_exception *body = buf;
344 int type = le32_to_cpu(body->type);
345
346 if (type == 4)
347 dev_err(wdev->dev, "firmware assert %d\n",
348 le32_to_cpup((__le32 *)body->data));
349 else
350 dev_err(wdev->dev, "firmware exception\n");
351 print_hex_dump(KERN_INFO, "hif: ", DUMP_PREFIX_OFFSET,
352 16, 1, hif, le16_to_cpu(hif->len), false);
353 wdev->chip_frozen = true;
354
355 return -1;
356 }
357
358 static const struct {
359 int msg_id;
360 int (*handler)(struct wfx_dev *wdev,
361 const struct hif_msg *hif, const void *buf);
362 } hif_handlers[] = {
363 /* Confirmations */
364 { HIF_CNF_ID_TX, hif_tx_confirm },
365 { HIF_CNF_ID_MULTI_TRANSMIT, hif_multi_tx_confirm },
366 /* Indications */
367 { HIF_IND_ID_STARTUP, hif_startup_indication },
368 { HIF_IND_ID_WAKEUP, hif_wakeup_indication },
369 { HIF_IND_ID_JOIN_COMPLETE, hif_join_complete_indication },
370 { HIF_IND_ID_SET_PM_MODE_CMPL, hif_pm_mode_complete_indication },
371 { HIF_IND_ID_SCAN_CMPL, hif_scan_complete_indication },
372 { HIF_IND_ID_SUSPEND_RESUME_TX, hif_suspend_resume_indication },
373 { HIF_IND_ID_EVENT, hif_event_indication },
374 { HIF_IND_ID_GENERIC, hif_generic_indication },
375 { HIF_IND_ID_ERROR, hif_error_indication },
376 { HIF_IND_ID_EXCEPTION, hif_exception_indication },
377 // FIXME: allocate skb_p from hif_receive_indication and make it generic
378 //{ HIF_IND_ID_RX, hif_receive_indication },
379 };
380
wfx_handle_rx(struct wfx_dev * wdev,struct sk_buff * skb)381 void wfx_handle_rx(struct wfx_dev *wdev, struct sk_buff *skb)
382 {
383 int i;
384 const struct hif_msg *hif = (const struct hif_msg *)skb->data;
385 int hif_id = hif->id;
386
387 if (hif_id == HIF_IND_ID_RX) {
388 // hif_receive_indication take care of skb lifetime
389 hif_receive_indication(wdev, hif, hif->body, skb);
390 return;
391 }
392 // Note: mutex_is_lock cause an implicit memory barrier that protect
393 // buf_send
394 if (mutex_is_locked(&wdev->hif_cmd.lock)
395 && wdev->hif_cmd.buf_send
396 && wdev->hif_cmd.buf_send->id == hif_id) {
397 hif_generic_confirm(wdev, hif, hif->body);
398 goto free;
399 }
400 for (i = 0; i < ARRAY_SIZE(hif_handlers); i++) {
401 if (hif_handlers[i].msg_id == hif_id) {
402 if (hif_handlers[i].handler)
403 hif_handlers[i].handler(wdev, hif, hif->body);
404 goto free;
405 }
406 }
407 if (hif_id & 0x80)
408 dev_err(wdev->dev, "unsupported HIF indication: ID %02x\n",
409 hif_id);
410 else
411 dev_err(wdev->dev, "unexpected HIF confirmation: ID %02x\n",
412 hif_id);
413 free:
414 dev_kfree_skb(skb);
415 }
416