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)
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)) {
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, "ignore rx data for non-existent vif %d\n",
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, "received event for non-existent vif\n");
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 complete(&wvif->set_pm_mode_complete);
170 static int hif_scan_complete_indication(struct wfx_dev *wdev,
171 const struct hif_msg *hif,
174 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
177 wfx_scan_complete(wvif);
182 static int hif_join_complete_indication(struct wfx_dev *wdev,
183 const struct hif_msg *hif,
186 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
189 dev_warn(wdev->dev, "unattended JoinCompleteInd\n");
194 static int hif_suspend_resume_indication(struct wfx_dev *wdev,
195 const struct hif_msg *hif,
198 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
199 const struct hif_ind_suspend_resume_tx *body = buf;
201 if (body->bc_mc_only) {
204 wfx_suspend_resume_mc(wvif, STA_NOTIFY_AWAKE);
206 wfx_suspend_resume_mc(wvif, STA_NOTIFY_SLEEP);
208 WARN(body->peer_sta_set, "misunderstood indication");
209 WARN(hif->interface != 2, "misunderstood indication");
211 wfx_suspend_hot_dev(wdev, STA_NOTIFY_AWAKE);
213 wfx_suspend_hot_dev(wdev, STA_NOTIFY_SLEEP);
219 static int hif_generic_indication(struct wfx_dev *wdev,
220 const struct hif_msg *hif, const void *buf)
222 const struct hif_ind_generic *body = buf;
223 int type = le32_to_cpu(body->type);
226 case HIF_GENERIC_INDICATION_TYPE_RAW:
228 case HIF_GENERIC_INDICATION_TYPE_STRING:
229 dev_info(wdev->dev, "firmware says: %s\n", (char *)&body->data);
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);
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);
249 dev_err(wdev->dev, "generic_indication: unknown indication type: %#.8x\n",
255 static const struct {
260 { HIF_ERROR_FIRMWARE_ROLLBACK,
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" },
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" },
299 static int hif_error_indication(struct wfx_dev *wdev,
300 const struct hif_msg *hif, const void *buf)
302 const struct hif_ind_error *body = buf;
303 int type = le32_to_cpu(body->type);
304 int param = (s8)body->data[0];
307 for (i = 0; i < ARRAY_SIZE(hif_errors); i++)
308 if (type == hif_errors[i].val)
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);
315 dev_err(wdev->dev, "asynchronous error: %s\n",
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;
326 static int hif_exception_indication(struct wfx_dev *wdev,
327 const struct hif_msg *hif, const void *buf)
329 const struct hif_ind_exception *body = buf;
330 int type = le32_to_cpu(body->type);
333 dev_err(wdev->dev, "firmware assert %d\n",
334 le32_to_cpup((__le32 *)body->data));
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;
344 static const struct {
346 int (*handler)(struct wfx_dev *wdev,
347 const struct hif_msg *hif, const void *buf);
350 { HIF_CNF_ID_TX, hif_tx_confirm },
351 { HIF_CNF_ID_MULTI_TRANSMIT, hif_multi_tx_confirm },
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 },
367 void wfx_handle_rx(struct wfx_dev *wdev, struct sk_buff *skb)
370 const struct hif_msg *hif = (const struct hif_msg *)skb->data;
371 int hif_id = hif->id;
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);
378 // Note: mutex_is_lock cause an implicit memory barrier that protect
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);
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);
394 dev_err(wdev->dev, "unsupported HIF indication: ID %02x\n",
397 dev_err(wdev->dev, "unexpected HIF confirmation: ID %02x\n",