1 // SPDX-License-Identifier: GPL-2.0-only
3 * Implementation of chip-to-host event (aka indications) of WFxxx Split Mac
6 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
7 * Copyright (c) 2010, ST-Ericsson
9 #include <linux/skbuff.h>
10 #include <linux/etherdevice.h>
18 #include "hif_api_cmd.h"
20 static int hif_generic_confirm(struct wfx_dev *wdev,
21 const struct hif_msg *hif, const void *buf)
23 // All confirm messages start with status
24 int status = le32_to_cpup((__le32 *)buf);
26 int len = le16_to_cpu(hif->len) - 4; // drop header
28 WARN(!mutex_is_locked(&wdev->hif_cmd.lock), "data locking error");
30 if (!wdev->hif_cmd.buf_send) {
31 dev_warn(wdev->dev, "unexpected confirmation: 0x%.2x\n", cmd);
35 if (cmd != wdev->hif_cmd.buf_send->id) {
37 "chip response mismatch request: 0x%.2x vs 0x%.2x\n",
38 cmd, wdev->hif_cmd.buf_send->id);
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);
48 wdev->hif_cmd.ret = status;
50 complete(&wdev->hif_cmd.done);
54 static int hif_tx_confirm(struct wfx_dev *wdev,
55 const struct hif_msg *hif, const void *buf)
57 const struct hif_cnf_tx *body = buf;
59 wfx_tx_confirm_cb(wdev, body);
63 static int hif_multi_tx_confirm(struct wfx_dev *wdev,
64 const struct hif_msg *hif, const void *buf)
66 const struct hif_cnf_multi_transmit *body = buf;
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]);
75 static int hif_startup_indication(struct wfx_dev *wdev,
76 const struct hif_msg *hif, const void *buf)
78 const struct hif_ind_startup *body = buf;
80 if (body->status || body->firmware_type > 4) {
81 dev_err(wdev->dev, "received invalid startup indication");
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);
90 complete(&wdev->firmware_ready);
94 static int hif_wakeup_indication(struct wfx_dev *wdev,
95 const struct hif_msg *hif, const void *buf)
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");
105 static int hif_receive_indication(struct wfx_dev *wdev,
106 const struct hif_msg *hif,
107 const void *buf, struct sk_buff *skb)
109 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
110 const struct hif_ind_rx *body = buf;
113 dev_warn(wdev->dev, "%s: ignore rx data for non-existent vif %d\n",
114 __func__, hif->interface);
117 skb_pull(skb, sizeof(struct hif_msg) + sizeof(struct hif_ind_rx));
118 wfx_rx_cb(wvif, body, skb);
123 static int hif_event_indication(struct wfx_dev *wdev,
124 const struct hif_msg *hif, const void *buf)
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);
131 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
136 case HIF_EVENT_IND_RCPI_RSSI:
137 wfx_event_report_rssi(wvif, body->event_data.rcpi_rssi);
139 case HIF_EVENT_IND_BSSLOST:
140 schedule_delayed_work(&wvif->beacon_loss_work, 0);
142 case HIF_EVENT_IND_BSSREGAINED:
143 cancel_delayed_work(&wvif->beacon_loss_work);
144 dev_dbg(wdev->dev, "ignore BSSREGAINED indication\n");
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));
151 dev_warn(wdev->dev, "unhandled event indication: %.2x\n",
158 static int hif_pm_mode_complete_indication(struct wfx_dev *wdev,
159 const struct hif_msg *hif,
162 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
165 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
168 complete(&wvif->set_pm_mode_complete);
173 static int hif_scan_complete_indication(struct wfx_dev *wdev,
174 const struct hif_msg *hif,
177 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
180 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
184 wfx_scan_complete(wvif);
189 static int hif_join_complete_indication(struct wfx_dev *wdev,
190 const struct hif_msg *hif,
193 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
196 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
199 dev_warn(wdev->dev, "unattended JoinCompleteInd\n");
204 static int hif_suspend_resume_indication(struct wfx_dev *wdev,
205 const struct hif_msg *hif,
208 const struct hif_ind_suspend_resume_tx *body = buf;
209 struct wfx_vif *wvif;
211 if (body->bc_mc_only) {
212 wvif = wdev_to_wvif(wdev, hif->interface);
214 dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
218 wfx_suspend_resume_mc(wvif, STA_NOTIFY_AWAKE);
220 wfx_suspend_resume_mc(wvif, STA_NOTIFY_SLEEP);
222 WARN(body->peer_sta_set, "misunderstood indication");
223 WARN(hif->interface != 2, "misunderstood indication");
225 wfx_suspend_hot_dev(wdev, STA_NOTIFY_AWAKE);
227 wfx_suspend_hot_dev(wdev, STA_NOTIFY_SLEEP);
233 static int hif_generic_indication(struct wfx_dev *wdev,
234 const struct hif_msg *hif, const void *buf)
236 const struct hif_ind_generic *body = buf;
237 int type = le32_to_cpu(body->type);
240 case HIF_GENERIC_INDICATION_TYPE_RAW:
242 case HIF_GENERIC_INDICATION_TYPE_STRING:
243 dev_info(wdev->dev, "firmware says: %s\n", (char *)&body->data);
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);
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);
263 dev_err(wdev->dev, "generic_indication: unknown indication type: %#.8x\n",
269 static const struct {
274 { HIF_ERROR_FIRMWARE_ROLLBACK,
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" },
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" },
313 static int hif_error_indication(struct wfx_dev *wdev,
314 const struct hif_msg *hif, const void *buf)
316 const struct hif_ind_error *body = buf;
317 int type = le32_to_cpu(body->type);
318 int param = (s8)body->data[0];
321 for (i = 0; i < ARRAY_SIZE(hif_errors); i++)
322 if (type == hif_errors[i].val)
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);
329 dev_err(wdev->dev, "asynchronous error: %s\n",
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;
340 static int hif_exception_indication(struct wfx_dev *wdev,
341 const struct hif_msg *hif, const void *buf)
343 const struct hif_ind_exception *body = buf;
344 int type = le32_to_cpu(body->type);
347 dev_err(wdev->dev, "firmware assert %d\n",
348 le32_to_cpup((__le32 *)body->data));
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;
358 static const struct {
360 int (*handler)(struct wfx_dev *wdev,
361 const struct hif_msg *hif, const void *buf);
364 { HIF_CNF_ID_TX, hif_tx_confirm },
365 { HIF_CNF_ID_MULTI_TRANSMIT, hif_multi_tx_confirm },
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 },
381 void wfx_handle_rx(struct wfx_dev *wdev, struct sk_buff *skb)
384 const struct hif_msg *hif = (const struct hif_msg *)skb->data;
385 int hif_id = hif->id;
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);
392 // Note: mutex_is_lock cause an implicit memory barrier that protect
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);
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);
408 dev_err(wdev->dev, "unsupported HIF indication: ID %02x\n",
411 dev_err(wdev->dev, "unexpected HIF confirmation: ID %02x\n",