Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Interrupt bottom half (BH).
4 *
5 * Copyright (c) 2017-2019, Silicon Laboratories, Inc.
6 * Copyright (c) 2010, ST-Ericsson
7 */
8#include <linux/gpio/consumer.h>
9#include <net/mac80211.h>
10
11#include "bh.h"
12#include "wfx.h"
13#include "hwio.h"
14#include "traces.h"
15#include "secure_link.h"
16#include "hif_rx.h"
17#include "hif_api_cmd.h"
18
19static void device_wakeup(struct wfx_dev *wdev)
20{
21 if (!wdev->pdata.gpio_wakeup)
22 return;
23 if (gpiod_get_value_cansleep(wdev->pdata.gpio_wakeup))
24 return;
25
26 gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1);
27 if (wfx_api_older_than(wdev, 1, 4)) {
28 if (!completion_done(&wdev->hif.ctrl_ready))
29 usleep_range(2000, 2500);
30 } else {
31 // completion.h does not provide any function to wait
32 // completion without consume it (a kind of
33 // wait_for_completion_done_timeout()). So we have to emulate
34 // it.
35 if (wait_for_completion_timeout(&wdev->hif.ctrl_ready,
36 msecs_to_jiffies(2) + 1))
37 complete(&wdev->hif.ctrl_ready);
38 else
39 dev_err(wdev->dev, "timeout while wake up chip\n");
40 }
41}
42
43static void device_release(struct wfx_dev *wdev)
44{
45 if (!wdev->pdata.gpio_wakeup)
46 return;
47
48 gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 0);
49}
50
51static int rx_helper(struct wfx_dev *wdev, size_t read_len, int *is_cnf)
52{
53 struct sk_buff *skb;
54 struct hif_msg *hif;
55 size_t alloc_len;
56 size_t computed_len;
57 int release_count;
58 int piggyback = 0;
59
60 WARN(read_len > round_down(0xFFF, 2) * sizeof(u16),
61 "%s: request exceed WFx capability", __func__);
62
63 // Add 2 to take into account piggyback size
64 alloc_len = wdev->hwbus_ops->align_size(wdev->hwbus_priv, read_len + 2);
65 skb = dev_alloc_skb(alloc_len);
66 if (!skb)
67 return -ENOMEM;
68
69 if (wfx_data_read(wdev, skb->data, alloc_len))
70 goto err;
71
72 piggyback = le16_to_cpup((__le16 *)(skb->data + alloc_len - 2));
73 _trace_piggyback(piggyback, false);
74
75 hif = (struct hif_msg *)skb->data;
76 WARN(hif->encrypted & 0x1, "unsupported encryption type");
77 if (hif->encrypted == 0x2) {
78 if (WARN(read_len < sizeof(struct hif_sl_msg), "corrupted read"))
79 goto err;
80 computed_len = le16_to_cpu(((struct hif_sl_msg *)hif)->len);
81 computed_len = round_up(computed_len - sizeof(u16), 16);
82 computed_len += sizeof(struct hif_sl_msg);
83 computed_len += sizeof(struct hif_sl_tag);
84 } else {
85 if (WARN(read_len < sizeof(struct hif_msg), "corrupted read"))
86 goto err;
87 computed_len = le16_to_cpu(hif->len);
88 computed_len = round_up(computed_len, 2);
89 }
90 if (computed_len != read_len) {
91 dev_err(wdev->dev, "inconsistent message length: %zu != %zu\n",
92 computed_len, read_len);
93 print_hex_dump(KERN_INFO, "hif: ", DUMP_PREFIX_OFFSET, 16, 1,
94 hif, read_len, true);
95 goto err;
96 }
97 if (hif->encrypted == 0x2) {
98 if (wfx_sl_decode(wdev, (struct hif_sl_msg *)hif)) {
99 dev_kfree_skb(skb);
100 // If frame was a confirmation, expect trouble in next
101 // exchange. However, it is harmless to fail to decode
102 // an indication frame, so try to continue. Anyway,
103 // piggyback is probably correct.
104 return piggyback;
105 }
106 }
107
108 if (!(hif->id & HIF_ID_IS_INDICATION)) {
109 (*is_cnf)++;
110 if (hif->id == HIF_CNF_ID_MULTI_TRANSMIT)
111 release_count = ((struct hif_cnf_multi_transmit *)hif->body)->num_tx_confs;
112 else
113 release_count = 1;
114 WARN(wdev->hif.tx_buffers_used < release_count, "corrupted buffer counter");
115 wdev->hif.tx_buffers_used -= release_count;
116 }
117 _trace_hif_recv(hif, wdev->hif.tx_buffers_used);
118
119 if (hif->id != HIF_IND_ID_EXCEPTION && hif->id != HIF_IND_ID_ERROR) {
120 if (hif->seqnum != wdev->hif.rx_seqnum)
121 dev_warn(wdev->dev, "wrong message sequence: %d != %d\n",
122 hif->seqnum, wdev->hif.rx_seqnum);
123 wdev->hif.rx_seqnum = (hif->seqnum + 1) % (HIF_COUNTER_MAX + 1);
124 }
125
126 skb_put(skb, le16_to_cpu(hif->len));
127 // wfx_handle_rx takes care on SKB livetime
128 wfx_handle_rx(wdev, skb);
129 if (!wdev->hif.tx_buffers_used)
130 wake_up(&wdev->hif.tx_buffers_empty);
131
132 return piggyback;
133
134err:
135 if (skb)
136 dev_kfree_skb(skb);
137 return -EIO;
138}
139
140static int bh_work_rx(struct wfx_dev *wdev, int max_msg, int *num_cnf)
141{
142 size_t len;
143 int i;
144 int ctrl_reg, piggyback;
145
146 piggyback = 0;
147 for (i = 0; i < max_msg; i++) {
148 if (piggyback & CTRL_NEXT_LEN_MASK)
149 ctrl_reg = piggyback;
150 else if (try_wait_for_completion(&wdev->hif.ctrl_ready))
151 ctrl_reg = atomic_xchg(&wdev->hif.ctrl_reg, 0);
152 else
153 ctrl_reg = 0;
154 if (!(ctrl_reg & CTRL_NEXT_LEN_MASK))
155 return i;
156 // ctrl_reg units are 16bits words
157 len = (ctrl_reg & CTRL_NEXT_LEN_MASK) * 2;
158 piggyback = rx_helper(wdev, len, num_cnf);
159 if (piggyback < 0)
160 return i;
161 if (!(piggyback & CTRL_WLAN_READY))
162 dev_err(wdev->dev, "unexpected piggyback value: ready bit not set: %04x\n",
163 piggyback);
164 }
165 if (piggyback & CTRL_NEXT_LEN_MASK) {
166 ctrl_reg = atomic_xchg(&wdev->hif.ctrl_reg, piggyback);
167 complete(&wdev->hif.ctrl_ready);
168 if (ctrl_reg)
169 dev_err(wdev->dev, "unexpected IRQ happened: %04x/%04x\n",
170 ctrl_reg, piggyback);
171 }
172 return i;
173}
174
175static void tx_helper(struct wfx_dev *wdev, struct hif_msg *hif)
176{
177 int ret;
178 void *data;
179 bool is_encrypted = false;
180 size_t len = le16_to_cpu(hif->len);
181
182 WARN(len < sizeof(*hif), "try to send corrupted data");
183
184 hif->seqnum = wdev->hif.tx_seqnum;
185 wdev->hif.tx_seqnum = (wdev->hif.tx_seqnum + 1) % (HIF_COUNTER_MAX + 1);
186
187 if (wfx_is_secure_command(wdev, hif->id)) {
188 len = round_up(len - sizeof(hif->len), 16) + sizeof(hif->len) +
189 sizeof(struct hif_sl_msg_hdr) +
190 sizeof(struct hif_sl_tag);
191 // AES support encryption in-place. However, mac80211 access to
192 // 802.11 header after frame was sent (to get MAC addresses).
193 // So, keep origin buffer clear.
194 data = kmalloc(len, GFP_KERNEL);
195 if (!data)
196 goto end;
197 is_encrypted = true;
198 ret = wfx_sl_encode(wdev, hif, data);
199 if (ret)
200 goto end;
201 } else {
202 data = hif;
203 }
204 WARN(len > wdev->hw_caps.size_inp_ch_buf,
205 "%s: request exceed WFx capability: %zu > %d\n", __func__,
206 len, wdev->hw_caps.size_inp_ch_buf);
207 len = wdev->hwbus_ops->align_size(wdev->hwbus_priv, len);
208 ret = wfx_data_write(wdev, data, len);
209 if (ret)
210 goto end;
211
212 wdev->hif.tx_buffers_used++;
213 _trace_hif_send(hif, wdev->hif.tx_buffers_used);
214end:
215 if (is_encrypted)
216 kfree(data);
217}
218
219static int bh_work_tx(struct wfx_dev *wdev, int max_msg)
220{
221 struct hif_msg *hif;
222 int i;
223
224 for (i = 0; i < max_msg; i++) {
225 hif = NULL;
226 if (wdev->hif.tx_buffers_used < wdev->hw_caps.num_inp_ch_bufs) {
227 if (try_wait_for_completion(&wdev->hif_cmd.ready)) {
228 WARN(!mutex_is_locked(&wdev->hif_cmd.lock), "data locking error");
229 hif = wdev->hif_cmd.buf_send;
230 } else {
231 hif = wfx_tx_queues_get(wdev);
232 }
233 }
234 if (!hif)
235 return i;
236 tx_helper(wdev, hif);
237 }
238 return i;
239}
240
241/* In SDIO mode, it is necessary to make an access to a register to acknowledge
242 * last received message. It could be possible to restrict this acknowledge to
243 * SDIO mode and only if last operation was rx.
244 */
245static void ack_sdio_data(struct wfx_dev *wdev)
246{
247 u32 cfg_reg;
248
249 config_reg_read(wdev, &cfg_reg);
250 if (cfg_reg & 0xFF) {
251 dev_warn(wdev->dev, "chip reports errors: %02x\n",
252 cfg_reg & 0xFF);
253 config_reg_write_bits(wdev, 0xFF, 0x00);
254 }
255}
256
257static void bh_work(struct work_struct *work)
258{
259 struct wfx_dev *wdev = container_of(work, struct wfx_dev, hif.bh);
260 int stats_req = 0, stats_cnf = 0, stats_ind = 0;
261 bool release_chip = false, last_op_is_rx = false;
262 int num_tx, num_rx;
263
264 device_wakeup(wdev);
265 do {
266 num_tx = bh_work_tx(wdev, 32);
267 stats_req += num_tx;
268 if (num_tx)
269 last_op_is_rx = false;
270 num_rx = bh_work_rx(wdev, 32, &stats_cnf);
271 stats_ind += num_rx;
272 if (num_rx)
273 last_op_is_rx = true;
274 } while (num_rx || num_tx);
275 stats_ind -= stats_cnf;
276
277 if (last_op_is_rx)
278 ack_sdio_data(wdev);
279 if (!wdev->hif.tx_buffers_used && !work_pending(work)) {
280 device_release(wdev);
281 release_chip = true;
282 }
283 _trace_bh_stats(stats_ind, stats_req, stats_cnf,
284 wdev->hif.tx_buffers_used, release_chip);
285}
286
287/*
288 * An IRQ from chip did occur
289 */
290void wfx_bh_request_rx(struct wfx_dev *wdev)
291{
292 u32 cur, prev;
293
294 control_reg_read(wdev, &cur);
295 prev = atomic_xchg(&wdev->hif.ctrl_reg, cur);
296 complete(&wdev->hif.ctrl_ready);
297 queue_work(system_highpri_wq, &wdev->hif.bh);
298
299 if (!(cur & CTRL_NEXT_LEN_MASK))
300 dev_err(wdev->dev, "unexpected control register value: length field is 0: %04x\n",
301 cur);
302 if (prev != 0)
303 dev_err(wdev->dev, "received IRQ but previous data was not (yet) read: %04x/%04x\n",
304 prev, cur);
305}
306
307/*
308 * Driver want to send data
309 */
310void wfx_bh_request_tx(struct wfx_dev *wdev)
311{
312 queue_work(system_highpri_wq, &wdev->hif.bh);
313}
314
315/*
316 * If IRQ is not available, this function allow to manually poll the control
317 * register and simulate an IRQ ahen an event happened.
318 *
319 * Note that the device has a bug: If an IRQ raise while host read control
320 * register, the IRQ is lost. So, use this function carefully (only duing
321 * device initialisation).
322 */
323void wfx_bh_poll_irq(struct wfx_dev *wdev)
324{
325 ktime_t now, start;
326 u32 reg;
327
328 WARN(!wdev->poll_irq, "unexpected IRQ polling can mask IRQ");
329 start = ktime_get();
330 for (;;) {
331 control_reg_read(wdev, ®);
332 now = ktime_get();
333 if (reg & 0xFFF)
334 break;
335 if (ktime_after(now, ktime_add_ms(start, 1000))) {
336 dev_err(wdev->dev, "time out while polling control register\n");
337 return;
338 }
339 udelay(200);
340 }
341 wfx_bh_request_rx(wdev);
342}
343
344void wfx_bh_register(struct wfx_dev *wdev)
345{
346 INIT_WORK(&wdev->hif.bh, bh_work);
347 init_completion(&wdev->hif.ctrl_ready);
348 init_waitqueue_head(&wdev->hif.tx_buffers_empty);
349}
350
351void wfx_bh_unregister(struct wfx_dev *wdev)
352{
353 flush_work(&wdev->hif.bh);
354}