1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Implementation of the host-to-chip commands (aka request/confirmation) of the
4  * hardware API.
5  *
6  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
7  * Copyright (c) 2010, ST-Ericsson
8  */
9 #include <linux/etherdevice.h>
10 
11 #include "hif_tx.h"
12 #include "wfx.h"
13 #include "bh.h"
14 #include "hwio.h"
15 #include "debug.h"
16 #include "sta.h"
17 
18 void wfx_init_hif_cmd(struct wfx_hif_cmd *hif_cmd)
19 {
20 	init_completion(&hif_cmd->ready);
21 	init_completion(&hif_cmd->done);
22 	mutex_init(&hif_cmd->lock);
23 }
24 
25 static void wfx_fill_header(struct wfx_hif_msg *hif, int if_id, unsigned int cmd, size_t size)
26 {
27 	if (if_id == -1)
28 		if_id = 2;
29 
30 	WARN(cmd > 0x3f, "invalid hardware command %#.2x", cmd);
31 	WARN(size > 0xFFF, "requested buffer is too large: %zu bytes", size);
32 	WARN(if_id > 0x3, "invalid interface ID %d", if_id);
33 
34 	hif->len = cpu_to_le16(size + 4);
35 	hif->id = cmd;
36 	hif->interface = if_id;
37 }
38 
39 static void *wfx_alloc_hif(size_t body_len, struct wfx_hif_msg **hif)
40 {
41 	*hif = kzalloc(sizeof(struct wfx_hif_msg) + body_len, GFP_KERNEL);
42 	if (*hif)
43 		return (*hif)->body;
44 	else
45 		return NULL;
46 }
47 
48 int wfx_cmd_send(struct wfx_dev *wdev, struct wfx_hif_msg *request,
49 		 void *reply, size_t reply_len, bool no_reply)
50 {
51 	const char *mib_name = "";
52 	const char *mib_sep = "";
53 	int cmd = request->id;
54 	int vif = request->interface;
55 	int ret;
56 
57 	/* Do not wait for any reply if chip is frozen */
58 	if (wdev->chip_frozen)
59 		return -ETIMEDOUT;
60 
61 	mutex_lock(&wdev->hif_cmd.lock);
62 	WARN(wdev->hif_cmd.buf_send, "data locking error");
63 
64 	/* Note: call to complete() below has an implicit memory barrier that hopefully protect
65 	 * buf_send
66 	 */
67 	wdev->hif_cmd.buf_send = request;
68 	wdev->hif_cmd.buf_recv = reply;
69 	wdev->hif_cmd.len_recv = reply_len;
70 	complete(&wdev->hif_cmd.ready);
71 
72 	wfx_bh_request_tx(wdev);
73 
74 	if (no_reply) {
75 		/* Chip won't reply. Ensure the wq has send the buffer before to continue. */
76 		flush_workqueue(wdev->bh_wq);
77 		ret = 0;
78 		goto end;
79 	}
80 
81 	if (wdev->poll_irq)
82 		wfx_bh_poll_irq(wdev);
83 
84 	ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 1 * HZ);
85 	if (!ret) {
86 		dev_err(wdev->dev, "chip is abnormally long to answer\n");
87 		reinit_completion(&wdev->hif_cmd.ready);
88 		ret = wait_for_completion_timeout(&wdev->hif_cmd.done, 3 * HZ);
89 	}
90 	if (!ret) {
91 		dev_err(wdev->dev, "chip did not answer\n");
92 		wfx_pending_dump_old_frames(wdev, 3000);
93 		wdev->chip_frozen = true;
94 		reinit_completion(&wdev->hif_cmd.done);
95 		ret = -ETIMEDOUT;
96 	} else {
97 		ret = wdev->hif_cmd.ret;
98 	}
99 
100 end:
101 	wdev->hif_cmd.buf_send = NULL;
102 	mutex_unlock(&wdev->hif_cmd.lock);
103 
104 	if (ret &&
105 	    (cmd == HIF_REQ_ID_READ_MIB || cmd == HIF_REQ_ID_WRITE_MIB)) {
106 		mib_name = wfx_get_mib_name(((u16 *)request)[2]);
107 		mib_sep = "/";
108 	}
109 	if (ret < 0)
110 		dev_err(wdev->dev, "hardware request %s%s%s (%#.2x) on vif %d returned error %d\n",
111 			wfx_get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
112 	if (ret > 0)
113 		dev_warn(wdev->dev, "hardware request %s%s%s (%#.2x) on vif %d returned status %d\n",
114 			 wfx_get_hif_name(cmd), mib_sep, mib_name, cmd, vif, ret);
115 
116 	return ret;
117 }
118 
119 /* This function is special. After HIF_REQ_ID_SHUT_DOWN, chip won't reply to any request anymore.
120  * Obviously, only call this function during device unregister.
121  */
122 int wfx_hif_shutdown(struct wfx_dev *wdev)
123 {
124 	int ret;
125 	struct wfx_hif_msg *hif;
126 
127 	wfx_alloc_hif(0, &hif);
128 	if (!hif)
129 		return -ENOMEM;
130 	wfx_fill_header(hif, -1, HIF_REQ_ID_SHUT_DOWN, 0);
131 	ret = wfx_cmd_send(wdev, hif, NULL, 0, true);
132 	if (wdev->pdata.gpio_wakeup)
133 		gpiod_set_value(wdev->pdata.gpio_wakeup, 0);
134 	else
135 		wfx_control_reg_write(wdev, 0);
136 	kfree(hif);
137 	return ret;
138 }
139 
140 int wfx_hif_configuration(struct wfx_dev *wdev, const u8 *conf, size_t len)
141 {
142 	int ret;
143 	size_t buf_len = sizeof(struct wfx_hif_req_configuration) + len;
144 	struct wfx_hif_msg *hif;
145 	struct wfx_hif_req_configuration *body = wfx_alloc_hif(buf_len, &hif);
146 
147 	if (!hif)
148 		return -ENOMEM;
149 	body->length = cpu_to_le16(len);
150 	memcpy(body->pds_data, conf, len);
151 	wfx_fill_header(hif, -1, HIF_REQ_ID_CONFIGURATION, buf_len);
152 	ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
153 	kfree(hif);
154 	return ret;
155 }
156 
157 int wfx_hif_reset(struct wfx_vif *wvif, bool reset_stat)
158 {
159 	int ret;
160 	struct wfx_hif_msg *hif;
161 	struct wfx_hif_req_reset *body = wfx_alloc_hif(sizeof(*body), &hif);
162 
163 	if (!hif)
164 		return -ENOMEM;
165 	body->reset_stat = reset_stat;
166 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_RESET, sizeof(*body));
167 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
168 	kfree(hif);
169 	return ret;
170 }
171 
172 int wfx_hif_read_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val, size_t val_len)
173 {
174 	int ret;
175 	struct wfx_hif_msg *hif;
176 	int buf_len = sizeof(struct wfx_hif_cnf_read_mib) + val_len;
177 	struct wfx_hif_req_read_mib *body = wfx_alloc_hif(sizeof(*body), &hif);
178 	struct wfx_hif_cnf_read_mib *reply = kmalloc(buf_len, GFP_KERNEL);
179 
180 	if (!body || !reply) {
181 		ret = -ENOMEM;
182 		goto out;
183 	}
184 	body->mib_id = cpu_to_le16(mib_id);
185 	wfx_fill_header(hif, vif_id, HIF_REQ_ID_READ_MIB, sizeof(*body));
186 	ret = wfx_cmd_send(wdev, hif, reply, buf_len, false);
187 
188 	if (!ret && mib_id != le16_to_cpu(reply->mib_id)) {
189 		dev_warn(wdev->dev, "%s: confirmation mismatch request\n", __func__);
190 		ret = -EIO;
191 	}
192 	if (ret == -ENOMEM)
193 		dev_err(wdev->dev, "buffer is too small to receive %s (%zu < %d)\n",
194 			wfx_get_mib_name(mib_id), val_len, le16_to_cpu(reply->length));
195 	if (!ret)
196 		memcpy(val, &reply->mib_data, le16_to_cpu(reply->length));
197 	else
198 		memset(val, 0xFF, val_len);
199 out:
200 	kfree(hif);
201 	kfree(reply);
202 	return ret;
203 }
204 
205 int wfx_hif_write_mib(struct wfx_dev *wdev, int vif_id, u16 mib_id, void *val, size_t val_len)
206 {
207 	int ret;
208 	struct wfx_hif_msg *hif;
209 	int buf_len = sizeof(struct wfx_hif_req_write_mib) + val_len;
210 	struct wfx_hif_req_write_mib *body = wfx_alloc_hif(buf_len, &hif);
211 
212 	if (!hif)
213 		return -ENOMEM;
214 	body->mib_id = cpu_to_le16(mib_id);
215 	body->length = cpu_to_le16(val_len);
216 	memcpy(&body->mib_data, val, val_len);
217 	wfx_fill_header(hif, vif_id, HIF_REQ_ID_WRITE_MIB, buf_len);
218 	ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
219 	kfree(hif);
220 	return ret;
221 }
222 
223 int wfx_hif_scan(struct wfx_vif *wvif, struct cfg80211_scan_request *req,
224 		 int chan_start_idx, int chan_num)
225 {
226 	int ret, i;
227 	struct wfx_hif_msg *hif;
228 	size_t buf_len = sizeof(struct wfx_hif_req_start_scan_alt) + chan_num * sizeof(u8);
229 	struct wfx_hif_req_start_scan_alt *body = wfx_alloc_hif(buf_len, &hif);
230 
231 	WARN(chan_num > HIF_API_MAX_NB_CHANNELS, "invalid params");
232 	WARN(req->n_ssids > HIF_API_MAX_NB_SSIDS, "invalid params");
233 
234 	if (!hif)
235 		return -ENOMEM;
236 	for (i = 0; i < req->n_ssids; i++) {
237 		memcpy(body->ssid_def[i].ssid, req->ssids[i].ssid, IEEE80211_MAX_SSID_LEN);
238 		body->ssid_def[i].ssid_length = cpu_to_le32(req->ssids[i].ssid_len);
239 	}
240 	body->num_of_ssids = HIF_API_MAX_NB_SSIDS;
241 	body->maintain_current_bss = 1;
242 	body->disallow_ps = 1;
243 	body->tx_power_level = cpu_to_le32(req->channels[chan_start_idx]->max_power);
244 	body->num_of_channels = chan_num;
245 	for (i = 0; i < chan_num; i++)
246 		body->channel_list[i] = req->channels[i + chan_start_idx]->hw_value;
247 	if (req->no_cck)
248 		body->max_transmit_rate = API_RATE_INDEX_G_6MBPS;
249 	else
250 		body->max_transmit_rate = API_RATE_INDEX_B_1MBPS;
251 	if (req->channels[chan_start_idx]->flags & IEEE80211_CHAN_NO_IR) {
252 		body->min_channel_time = cpu_to_le32(50);
253 		body->max_channel_time = cpu_to_le32(150);
254 	} else {
255 		body->min_channel_time = cpu_to_le32(10);
256 		body->max_channel_time = cpu_to_le32(50);
257 		body->num_of_probe_requests = 2;
258 		body->probe_delay = 100;
259 	}
260 
261 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START_SCAN, buf_len);
262 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
263 	kfree(hif);
264 	return ret;
265 }
266 
267 int wfx_hif_stop_scan(struct wfx_vif *wvif)
268 {
269 	int ret;
270 	struct wfx_hif_msg *hif;
271 	/* body associated to HIF_REQ_ID_STOP_SCAN is empty */
272 	wfx_alloc_hif(0, &hif);
273 
274 	if (!hif)
275 		return -ENOMEM;
276 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_STOP_SCAN, 0);
277 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
278 	kfree(hif);
279 	return ret;
280 }
281 
282 int wfx_hif_join(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
283 		 struct ieee80211_channel *channel, const u8 *ssid, int ssidlen)
284 {
285 	int ret;
286 	struct wfx_hif_msg *hif;
287 	struct wfx_hif_req_join *body = wfx_alloc_hif(sizeof(*body), &hif);
288 
289 	WARN_ON(!conf->beacon_int);
290 	WARN_ON(!conf->basic_rates);
291 	WARN_ON(sizeof(body->ssid) < ssidlen);
292 	WARN(!conf->ibss_joined && !ssidlen, "joining an unknown BSS");
293 	if (!hif)
294 		return -ENOMEM;
295 	body->infrastructure_bss_mode = !conf->ibss_joined;
296 	body->short_preamble = conf->use_short_preamble;
297 	body->probe_for_join = !(channel->flags & IEEE80211_CHAN_NO_IR);
298 	body->channel_number = channel->hw_value;
299 	body->beacon_interval = cpu_to_le32(conf->beacon_int);
300 	body->basic_rate_set = cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
301 	memcpy(body->bssid, conf->bssid, sizeof(body->bssid));
302 	if (ssid) {
303 		body->ssid_length = cpu_to_le32(ssidlen);
304 		memcpy(body->ssid, ssid, ssidlen);
305 	}
306 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_JOIN, sizeof(*body));
307 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
308 	kfree(hif);
309 	return ret;
310 }
311 
312 int wfx_hif_set_bss_params(struct wfx_vif *wvif, int aid, int beacon_lost_count)
313 {
314 	int ret;
315 	struct wfx_hif_msg *hif;
316 	struct wfx_hif_req_set_bss_params *body = wfx_alloc_hif(sizeof(*body), &hif);
317 
318 	if (!hif)
319 		return -ENOMEM;
320 	body->aid = cpu_to_le16(aid);
321 	body->beacon_lost_count = beacon_lost_count;
322 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_BSS_PARAMS, sizeof(*body));
323 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
324 	kfree(hif);
325 	return ret;
326 }
327 
328 int wfx_hif_add_key(struct wfx_dev *wdev, const struct wfx_hif_req_add_key *arg)
329 {
330 	int ret;
331 	struct wfx_hif_msg *hif;
332 	/* FIXME: only send necessary bits */
333 	struct wfx_hif_req_add_key *body = wfx_alloc_hif(sizeof(*body), &hif);
334 
335 	if (!hif)
336 		return -ENOMEM;
337 	/* FIXME: swap bytes as necessary in body */
338 	memcpy(body, arg, sizeof(*body));
339 	if (wfx_api_older_than(wdev, 1, 5))
340 		/* Legacy firmwares expect that add_key to be sent on right interface. */
341 		wfx_fill_header(hif, arg->int_id, HIF_REQ_ID_ADD_KEY, sizeof(*body));
342 	else
343 		wfx_fill_header(hif, -1, HIF_REQ_ID_ADD_KEY, sizeof(*body));
344 	ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
345 	kfree(hif);
346 	return ret;
347 }
348 
349 int wfx_hif_remove_key(struct wfx_dev *wdev, int idx)
350 {
351 	int ret;
352 	struct wfx_hif_msg *hif;
353 	struct wfx_hif_req_remove_key *body = wfx_alloc_hif(sizeof(*body), &hif);
354 
355 	if (!hif)
356 		return -ENOMEM;
357 	body->entry_index = idx;
358 	wfx_fill_header(hif, -1, HIF_REQ_ID_REMOVE_KEY, sizeof(*body));
359 	ret = wfx_cmd_send(wdev, hif, NULL, 0, false);
360 	kfree(hif);
361 	return ret;
362 }
363 
364 int wfx_hif_set_edca_queue_params(struct wfx_vif *wvif, u16 queue,
365 				  const struct ieee80211_tx_queue_params *arg)
366 {
367 	int ret;
368 	struct wfx_hif_msg *hif;
369 	struct wfx_hif_req_edca_queue_params *body = wfx_alloc_hif(sizeof(*body), &hif);
370 
371 	if (!body)
372 		return -ENOMEM;
373 
374 	WARN_ON(arg->aifs > 255);
375 	if (!hif)
376 		return -ENOMEM;
377 	body->aifsn = arg->aifs;
378 	body->cw_min = cpu_to_le16(arg->cw_min);
379 	body->cw_max = cpu_to_le16(arg->cw_max);
380 	body->tx_op_limit = cpu_to_le16(arg->txop * USEC_PER_TXOP);
381 	body->queue_id = 3 - queue;
382 	/* API 2.0 has changed queue IDs values */
383 	if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BE)
384 		body->queue_id = HIF_QUEUE_ID_BACKGROUND;
385 	if (wfx_api_older_than(wvif->wdev, 2, 0) && queue == IEEE80211_AC_BK)
386 		body->queue_id = HIF_QUEUE_ID_BESTEFFORT;
387 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_EDCA_QUEUE_PARAMS, sizeof(*body));
388 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
389 	kfree(hif);
390 	return ret;
391 }
392 
393 int wfx_hif_set_pm(struct wfx_vif *wvif, bool ps, int dynamic_ps_timeout)
394 {
395 	int ret;
396 	struct wfx_hif_msg *hif;
397 	struct wfx_hif_req_set_pm_mode *body = wfx_alloc_hif(sizeof(*body), &hif);
398 
399 	if (!body)
400 		return -ENOMEM;
401 
402 	if (!hif)
403 		return -ENOMEM;
404 	if (ps) {
405 		body->enter_psm = 1;
406 		/* Firmware does not support more than 128ms */
407 		body->fast_psm_idle_period = min(dynamic_ps_timeout * 2, 255);
408 		if (body->fast_psm_idle_period)
409 			body->fast_psm = 1;
410 	}
411 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_SET_PM_MODE, sizeof(*body));
412 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
413 	kfree(hif);
414 	return ret;
415 }
416 
417 int wfx_hif_start(struct wfx_vif *wvif, const struct ieee80211_bss_conf *conf,
418 		  const struct ieee80211_channel *channel)
419 {
420 	int ret;
421 	struct wfx_hif_msg *hif;
422 	struct wfx_hif_req_start *body = wfx_alloc_hif(sizeof(*body), &hif);
423 
424 	WARN_ON(!conf->beacon_int);
425 	if (!hif)
426 		return -ENOMEM;
427 	body->dtim_period = conf->dtim_period;
428 	body->short_preamble = conf->use_short_preamble;
429 	body->channel_number = channel->hw_value;
430 	body->beacon_interval = cpu_to_le32(conf->beacon_int);
431 	body->basic_rate_set = cpu_to_le32(wfx_rate_mask_to_hw(wvif->wdev, conf->basic_rates));
432 	body->ssid_length = conf->ssid_len;
433 	memcpy(body->ssid, conf->ssid, conf->ssid_len);
434 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_START, sizeof(*body));
435 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
436 	kfree(hif);
437 	return ret;
438 }
439 
440 int wfx_hif_beacon_transmit(struct wfx_vif *wvif, bool enable)
441 {
442 	int ret;
443 	struct wfx_hif_msg *hif;
444 	struct wfx_hif_req_beacon_transmit *body = wfx_alloc_hif(sizeof(*body), &hif);
445 
446 	if (!hif)
447 		return -ENOMEM;
448 	body->enable_beaconing = enable ? 1 : 0;
449 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_BEACON_TRANSMIT, sizeof(*body));
450 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
451 	kfree(hif);
452 	return ret;
453 }
454 
455 int wfx_hif_map_link(struct wfx_vif *wvif, bool unmap, u8 *mac_addr, int sta_id, bool mfp)
456 {
457 	int ret;
458 	struct wfx_hif_msg *hif;
459 	struct wfx_hif_req_map_link *body = wfx_alloc_hif(sizeof(*body), &hif);
460 
461 	if (!hif)
462 		return -ENOMEM;
463 	if (mac_addr)
464 		ether_addr_copy(body->mac_addr, mac_addr);
465 	body->mfpc = mfp ? 1 : 0;
466 	body->unmap = unmap ? 1 : 0;
467 	body->peer_sta_id = sta_id;
468 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_MAP_LINK, sizeof(*body));
469 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
470 	kfree(hif);
471 	return ret;
472 }
473 
474 int wfx_hif_update_ie_beacon(struct wfx_vif *wvif, const u8 *ies, size_t ies_len)
475 {
476 	int ret;
477 	struct wfx_hif_msg *hif;
478 	int buf_len = sizeof(struct wfx_hif_req_update_ie) + ies_len;
479 	struct wfx_hif_req_update_ie *body = wfx_alloc_hif(buf_len, &hif);
480 
481 	if (!hif)
482 		return -ENOMEM;
483 	body->beacon = 1;
484 	body->num_ies = cpu_to_le16(1);
485 	memcpy(body->ie, ies, ies_len);
486 	wfx_fill_header(hif, wvif->id, HIF_REQ_ID_UPDATE_IE, buf_len);
487 	ret = wfx_cmd_send(wvif->wdev, hif, NULL, 0, false);
488 	kfree(hif);
489 	return ret;
490 }
491