1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Handling of the chip-to-host events (aka indications) of the hardware API.
4  *
5  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  */
8 #include <linux/skbuff.h>
9 #include <linux/etherdevice.h>
10 
11 #include "hif_rx.h"
12 #include "wfx.h"
13 #include "scan.h"
14 #include "bh.h"
15 #include "sta.h"
16 #include "data_rx.h"
17 #include "hif_api_cmd.h"
18 
hif_generic_confirm(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)19 static int hif_generic_confirm(struct wfx_dev *wdev,
20 			       const struct hif_msg *hif, const void *buf)
21 {
22 	/* All confirm messages start with status */
23 	int status = le32_to_cpup((__le32 *)buf);
24 	int cmd = hif->id;
25 	int len = le16_to_cpu(hif->len) - 4; /* drop header */
26 
27 	WARN(!mutex_is_locked(&wdev->hif_cmd.lock), "data locking error");
28 
29 	if (!wdev->hif_cmd.buf_send) {
30 		dev_warn(wdev->dev, "unexpected confirmation: 0x%.2x\n", cmd);
31 		return -EINVAL;
32 	}
33 
34 	if (cmd != wdev->hif_cmd.buf_send->id) {
35 		dev_warn(wdev->dev,
36 			 "chip response mismatch request: 0x%.2x vs 0x%.2x\n",
37 			 cmd, wdev->hif_cmd.buf_send->id);
38 		return -EINVAL;
39 	}
40 
41 	if (wdev->hif_cmd.buf_recv) {
42 		if (wdev->hif_cmd.len_recv >= len && len > 0)
43 			memcpy(wdev->hif_cmd.buf_recv, buf, len);
44 		else
45 			status = -EIO;
46 	}
47 	wdev->hif_cmd.ret = status;
48 
49 	complete(&wdev->hif_cmd.done);
50 	return status;
51 }
52 
hif_tx_confirm(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)53 static int hif_tx_confirm(struct wfx_dev *wdev,
54 			  const struct hif_msg *hif, const void *buf)
55 {
56 	const struct hif_cnf_tx *body = buf;
57 
58 	wfx_tx_confirm_cb(wdev, body);
59 	return 0;
60 }
61 
hif_multi_tx_confirm(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)62 static int hif_multi_tx_confirm(struct wfx_dev *wdev,
63 				const struct hif_msg *hif, const void *buf)
64 {
65 	const struct hif_cnf_multi_transmit *body = buf;
66 	int i;
67 
68 	WARN(body->num_tx_confs <= 0, "corrupted message");
69 	for (i = 0; i < body->num_tx_confs; i++)
70 		wfx_tx_confirm_cb(wdev, &body->tx_conf_payload[i]);
71 	return 0;
72 }
73 
hif_startup_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)74 static int hif_startup_indication(struct wfx_dev *wdev,
75 				  const struct hif_msg *hif, const void *buf)
76 {
77 	const struct hif_ind_startup *body = buf;
78 
79 	if (body->status || body->firmware_type > 4) {
80 		dev_err(wdev->dev, "received invalid startup indication");
81 		return -EINVAL;
82 	}
83 	memcpy(&wdev->hw_caps, body, sizeof(struct hif_ind_startup));
84 	le16_to_cpus((__le16 *)&wdev->hw_caps.hardware_id);
85 	le16_to_cpus((__le16 *)&wdev->hw_caps.num_inp_ch_bufs);
86 	le16_to_cpus((__le16 *)&wdev->hw_caps.size_inp_ch_buf);
87 	le32_to_cpus((__le32 *)&wdev->hw_caps.supported_rate_mask);
88 
89 	complete(&wdev->firmware_ready);
90 	return 0;
91 }
92 
hif_wakeup_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)93 static int hif_wakeup_indication(struct wfx_dev *wdev,
94 				 const struct hif_msg *hif, const void *buf)
95 {
96 	if (!wdev->pdata.gpio_wakeup ||
97 	    gpiod_get_value(wdev->pdata.gpio_wakeup) == 0) {
98 		dev_warn(wdev->dev, "unexpected wake-up indication\n");
99 		return -EIO;
100 	}
101 	return 0;
102 }
103 
hif_receive_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf,struct sk_buff * skb)104 static int hif_receive_indication(struct wfx_dev *wdev,
105 				  const struct hif_msg *hif,
106 				  const void *buf, struct sk_buff *skb)
107 {
108 	struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
109 	const struct hif_ind_rx *body = buf;
110 
111 	if (!wvif) {
112 		dev_warn(wdev->dev, "%s: ignore rx data for non-existent vif %d\n",
113 			 __func__, hif->interface);
114 		return -EIO;
115 	}
116 	skb_pull(skb, sizeof(struct hif_msg) + sizeof(struct hif_ind_rx));
117 	wfx_rx_cb(wvif, body, skb);
118 
119 	return 0;
120 }
121 
hif_event_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)122 static int hif_event_indication(struct wfx_dev *wdev,
123 				const struct hif_msg *hif, const void *buf)
124 {
125 	struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
126 	const struct hif_ind_event *body = buf;
127 	int type = le32_to_cpu(body->event_id);
128 
129 	if (!wvif) {
130 		dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
131 		return -EIO;
132 	}
133 
134 	switch (type) {
135 	case HIF_EVENT_IND_RCPI_RSSI:
136 		wfx_event_report_rssi(wvif, body->event_data.rcpi_rssi);
137 		break;
138 	case HIF_EVENT_IND_BSSLOST:
139 		schedule_delayed_work(&wvif->beacon_loss_work, 0);
140 		break;
141 	case HIF_EVENT_IND_BSSREGAINED:
142 		cancel_delayed_work(&wvif->beacon_loss_work);
143 		dev_dbg(wdev->dev, "ignore BSSREGAINED indication\n");
144 		break;
145 	case HIF_EVENT_IND_PS_MODE_ERROR:
146 		dev_warn(wdev->dev, "error while processing power save request: %d\n",
147 			 le32_to_cpu(body->event_data.ps_mode_error));
148 		break;
149 	default:
150 		dev_warn(wdev->dev, "unhandled event indication: %.2x\n",
151 			 type);
152 		break;
153 	}
154 	return 0;
155 }
156 
hif_pm_mode_complete_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)157 static int hif_pm_mode_complete_indication(struct wfx_dev *wdev,
158 					   const struct hif_msg *hif,
159 					   const void *buf)
160 {
161 	struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
162 
163 	if (!wvif) {
164 		dev_warn(wdev->dev, "%s: received event for non-existent vif\n", __func__);
165 		return -EIO;
166 	}
167 	complete(&wvif->set_pm_mode_complete);
168 
169 	return 0;
170 }
171 
hif_scan_complete_indication(struct wfx_dev * wdev,const struct hif_msg * hif,const void * buf)172 static int hif_scan_complete_indication(struct wfx_dev *wdev,
173 					const struct hif_msg *hif,
174 					const void *buf)
175 {
176 	struct wfx_vif *wvif = wdev_to_wvif(wdev, hif->interface);
177 	const struct hif_ind_scan_cmpl *body = buf;
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, body->num_channels_completed);
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 	 */
395 	if (mutex_is_locked(&wdev->hif_cmd.lock) &&
396 	    wdev->hif_cmd.buf_send &&
397 	    wdev->hif_cmd.buf_send->id == hif_id) {
398 		hif_generic_confirm(wdev, hif, hif->body);
399 		goto free;
400 	}
401 	for (i = 0; i < ARRAY_SIZE(hif_handlers); i++) {
402 		if (hif_handlers[i].msg_id == hif_id) {
403 			if (hif_handlers[i].handler)
404 				hif_handlers[i].handler(wdev, hif, hif->body);
405 			goto free;
406 		}
407 	}
408 	if (hif_id & 0x80)
409 		dev_err(wdev->dev, "unsupported HIF indication: ID %02x\n",
410 			hif_id);
411 	else
412 		dev_err(wdev->dev, "unexpected HIF confirmation: ID %02x\n",
413 			hif_id);
414 free:
415 	dev_kfree_skb(skb);
416 }
417