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-2019, Silicon Laboratories, Inc.
7 * Copyright (c) 2010, ST-Ericsson
9 #include <linux/skbuff.h>
10 #include <linux/etherdevice.h>
18 #include "secure_link.h"
19 #include "hif_api_cmd.h"
21 static int hif_generic_confirm(struct wfx_dev *wdev,
22 const struct hif_msg *hif, const void *buf)
24 // All confirm messages start with status
25 int status = le32_to_cpu(*((__le32 *) buf));
27 int len = hif->len - 4; // drop header
29 WARN(!mutex_is_locked(&wdev->hif_cmd.lock), "data locking error");
31 if (!wdev->hif_cmd.buf_send) {
32 dev_warn(wdev->dev, "unexpected confirmation: 0x%.2x\n", cmd);
36 if (cmd != wdev->hif_cmd.buf_send->id) {
38 "chip response mismatch request: 0x%.2x vs 0x%.2x\n",
39 cmd, wdev->hif_cmd.buf_send->id);
43 if (wdev->hif_cmd.buf_recv) {
44 if (wdev->hif_cmd.len_recv >= len)
45 memcpy(wdev->hif_cmd.buf_recv, buf, len);
49 wdev->hif_cmd.ret = status;
51 if (!wdev->hif_cmd.async) {
52 complete(&wdev->hif_cmd.done);
54 wdev->hif_cmd.buf_send = NULL;
55 mutex_unlock(&wdev->hif_cmd.lock);
56 if (cmd != HIF_REQ_ID_SL_EXCHANGE_PUB_KEYS)
57 mutex_unlock(&wdev->hif_cmd.key_renew_lock);
62 static int hif_tx_confirm(struct wfx_dev *wdev,
63 const struct hif_msg *hif, const void *buf)
65 const struct hif_cnf_tx *body = buf;
66 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
72 wfx_tx_confirm_cb(wvif, body);
76 static int hif_multi_tx_confirm(struct wfx_dev *wdev,
77 const struct hif_msg *hif, const void *buf)
79 const struct hif_cnf_multi_transmit *body = buf;
80 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
83 WARN(body->num_tx_confs <= 0, "corrupted message");
88 for (i = 0; i < body->num_tx_confs; i++)
89 wfx_tx_confirm_cb(wvif, &body->tx_conf_payload[i]);
93 static int hif_startup_indication(struct wfx_dev *wdev,
94 const struct hif_msg *hif, const void *buf)
96 const struct hif_ind_startup *body = buf;
98 if (body->status || body->firmware_type > 4) {
99 dev_err(wdev->dev, "received invalid startup indication");
102 memcpy(&wdev->hw_caps, body, sizeof(struct hif_ind_startup));
103 le32_to_cpus(&wdev->hw_caps.status);
104 le16_to_cpus(&wdev->hw_caps.hardware_id);
105 le16_to_cpus(&wdev->hw_caps.num_inp_ch_bufs);
106 le16_to_cpus(&wdev->hw_caps.size_inp_ch_buf);
108 complete(&wdev->firmware_ready);
112 static int hif_wakeup_indication(struct wfx_dev *wdev,
113 const struct hif_msg *hif, const void *buf)
115 if (!wdev->pdata.gpio_wakeup
116 || !gpiod_get_value(wdev->pdata.gpio_wakeup)) {
117 dev_warn(wdev->dev, "unexpected wake-up indication\n");
123 static int hif_keys_indication(struct wfx_dev *wdev,
124 const struct hif_msg *hif, const void *buf)
126 const struct hif_ind_sl_exchange_pub_keys *body = buf;
127 u8 pubkey[API_NCP_PUB_KEY_SIZE];
129 // SL_PUB_KEY_EXCHANGE_STATUS_SUCCESS is used by legacy secure link
130 if (body->status && body->status != SL_PUB_KEY_EXCHANGE_STATUS_SUCCESS)
131 dev_warn(wdev->dev, "secure link negociation error\n");
132 memcpy(pubkey, body->ncp_pub_key, sizeof(pubkey));
133 memreverse(pubkey, sizeof(pubkey));
134 wfx_sl_check_pubkey(wdev, pubkey, body->ncp_pub_key_mac);
138 static int hif_receive_indication(struct wfx_dev *wdev,
139 const struct hif_msg *hif,
140 const void *buf, struct sk_buff *skb)
142 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
143 const struct hif_ind_rx *body = buf;
146 dev_warn(wdev->dev, "ignore rx data for non-existent vif %d\n",
150 skb_pull(skb, sizeof(struct hif_msg) + sizeof(struct hif_ind_rx));
151 wfx_rx_cb(wvif, body, skb);
156 static int hif_event_indication(struct wfx_dev *wdev,
157 const struct hif_msg *hif, const void *buf)
159 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
160 const struct hif_ind_event *body = buf;
161 struct wfx_hif_event *event;
168 event = kzalloc(sizeof(*event), GFP_KERNEL);
172 memcpy(&event->evt, body, sizeof(struct hif_ind_event));
173 spin_lock(&wvif->event_queue_lock);
174 first = list_empty(&wvif->event_queue);
175 list_add_tail(&event->link, &wvif->event_queue);
176 spin_unlock(&wvif->event_queue_lock);
179 schedule_work(&wvif->event_handler_work);
184 static int hif_pm_mode_complete_indication(struct wfx_dev *wdev,
185 const struct hif_msg *hif,
188 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
191 complete(&wvif->set_pm_mode_complete);
196 static int hif_scan_complete_indication(struct wfx_dev *wdev,
197 const struct hif_msg *hif,
200 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
203 wfx_scan_complete(wvif);
208 static int hif_join_complete_indication(struct wfx_dev *wdev,
209 const struct hif_msg *hif,
212 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
215 dev_warn(wdev->dev, "unattended JoinCompleteInd\n");
220 static int hif_suspend_resume_indication(struct wfx_dev *wdev,
221 const struct hif_msg *hif,
224 struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
225 const struct hif_ind_suspend_resume_tx *body = buf;
228 WARN(!body->suspend_resume_flags.bc_mc_only, "unsupported suspend/resume notification");
229 if (body->suspend_resume_flags.resume)
230 wfx_suspend_resume_mc(wvif, STA_NOTIFY_AWAKE);
232 wfx_suspend_resume_mc(wvif, STA_NOTIFY_SLEEP);
237 static int hif_error_indication(struct wfx_dev *wdev,
238 const struct hif_msg *hif, const void *buf)
240 const struct hif_ind_error *body = buf;
241 u8 *pRollback = (u8 *) body->data;
242 u32 *pStatus = (u32 *) body->data;
244 switch (body->type) {
245 case HIF_ERROR_FIRMWARE_ROLLBACK:
247 "asynchronous error: firmware rollback error %d\n",
250 case HIF_ERROR_FIRMWARE_DEBUG_ENABLED:
251 dev_err(wdev->dev, "asynchronous error: firmware debug feature enabled\n");
253 case HIF_ERROR_OUTDATED_SESSION_KEY:
254 dev_err(wdev->dev, "asynchronous error: secure link outdated key: %#.8x\n",
257 case HIF_ERROR_INVALID_SESSION_KEY:
258 dev_err(wdev->dev, "asynchronous error: invalid session key\n");
260 case HIF_ERROR_OOR_VOLTAGE:
261 dev_err(wdev->dev, "asynchronous error: out-of-range overvoltage: %#.8x\n",
264 case HIF_ERROR_PDS_VERSION:
266 "asynchronous error: wrong PDS payload or version: %#.8x\n",
270 dev_err(wdev->dev, "asynchronous error: unknown (%d)\n",
277 static int hif_generic_indication(struct wfx_dev *wdev,
278 const struct hif_msg *hif, const void *buf)
280 const struct hif_ind_generic *body = buf;
282 switch (body->indication_type) {
283 case HIF_GENERIC_INDICATION_TYPE_RAW:
285 case HIF_GENERIC_INDICATION_TYPE_STRING:
286 dev_info(wdev->dev, "firmware says: %s\n",
287 (char *) body->indication_data.raw_data);
289 case HIF_GENERIC_INDICATION_TYPE_RX_STATS:
290 mutex_lock(&wdev->rx_stats_lock);
291 // Older firmware send a generic indication beside RxStats
292 if (!wfx_api_older_than(wdev, 1, 4))
293 dev_info(wdev->dev, "Rx test ongoing. Temperature: %d°C\n",
294 body->indication_data.rx_stats.current_temp);
295 memcpy(&wdev->rx_stats, &body->indication_data.rx_stats,
296 sizeof(wdev->rx_stats));
297 mutex_unlock(&wdev->rx_stats_lock);
301 "generic_indication: unknown indication type: %#.8x\n",
302 body->indication_type);
307 static int hif_exception_indication(struct wfx_dev *wdev,
308 const struct hif_msg *hif, const void *buf)
310 size_t len = hif->len - 4; // drop header
312 dev_err(wdev->dev, "firmware exception\n");
313 print_hex_dump_bytes("Dump: ", DUMP_PREFIX_NONE, buf, len);
314 wdev->chip_frozen = 1;
319 static const struct {
321 int (*handler)(struct wfx_dev *wdev,
322 const struct hif_msg *hif, const void *buf);
325 { HIF_CNF_ID_TX, hif_tx_confirm },
326 { HIF_CNF_ID_MULTI_TRANSMIT, hif_multi_tx_confirm },
328 { HIF_IND_ID_STARTUP, hif_startup_indication },
329 { HIF_IND_ID_WAKEUP, hif_wakeup_indication },
330 { HIF_IND_ID_JOIN_COMPLETE, hif_join_complete_indication },
331 { HIF_IND_ID_SET_PM_MODE_CMPL, hif_pm_mode_complete_indication },
332 { HIF_IND_ID_SCAN_CMPL, hif_scan_complete_indication },
333 { HIF_IND_ID_SUSPEND_RESUME_TX, hif_suspend_resume_indication },
334 { HIF_IND_ID_SL_EXCHANGE_PUB_KEYS, hif_keys_indication },
335 { HIF_IND_ID_EVENT, hif_event_indication },
336 { HIF_IND_ID_GENERIC, hif_generic_indication },
337 { HIF_IND_ID_ERROR, hif_error_indication },
338 { HIF_IND_ID_EXCEPTION, hif_exception_indication },
339 // FIXME: allocate skb_p from hif_receive_indication and make it generic
340 //{ HIF_IND_ID_RX, hif_receive_indication },
343 void wfx_handle_rx(struct wfx_dev *wdev, struct sk_buff *skb)
346 const struct hif_msg *hif = (const struct hif_msg *)skb->data;
347 int hif_id = hif->id;
349 if (hif_id == HIF_IND_ID_RX) {
350 // hif_receive_indication take care of skb lifetime
351 hif_receive_indication(wdev, hif, hif->body, skb);
354 // Note: mutex_is_lock cause an implicit memory barrier that protect
356 if (mutex_is_locked(&wdev->hif_cmd.lock)
357 && wdev->hif_cmd.buf_send
358 && wdev->hif_cmd.buf_send->id == hif_id) {
359 hif_generic_confirm(wdev, hif, hif->body);
362 for (i = 0; i < ARRAY_SIZE(hif_handlers); i++) {
363 if (hif_handlers[i].msg_id == hif_id) {
364 if (hif_handlers[i].handler)
365 hif_handlers[i].handler(wdev, hif, hif->body);
370 dev_err(wdev->dev, "unsupported HIF indication: ID %02x\n",
373 dev_err(wdev->dev, "unexpected HIF confirmation: ID %02x\n",