1 // SPDX-License-Identifier: GPL-2.0-only
3 * Interrupt bottom half (BH).
5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6 * Copyright (c) 2010, ST-Ericsson
8 #include <linux/gpio/consumer.h>
9 #include <net/mac80211.h>
16 #include "hif_api_cmd.h"
18 static void device_wakeup(struct wfx_dev
*wdev
)
22 if (!wdev
->pdata
.gpio_wakeup
)
24 if (gpiod_get_value_cansleep(wdev
->pdata
.gpio_wakeup
) > 0)
27 if (wfx_api_older_than(wdev
, 1, 4)) {
28 gpiod_set_value_cansleep(wdev
->pdata
.gpio_wakeup
, 1);
29 if (!completion_done(&wdev
->hif
.ctrl_ready
))
30 usleep_range(2000, 2500);
34 gpiod_set_value_cansleep(wdev
->pdata
.gpio_wakeup
, 1);
35 /* completion.h does not provide any function to wait completion without consume it
36 * (a kind of wait_for_completion_done_timeout()). So we have to emulate it.
38 if (wait_for_completion_timeout(&wdev
->hif
.ctrl_ready
, msecs_to_jiffies(2))) {
39 complete(&wdev
->hif
.ctrl_ready
);
41 } else if (max_retry
-- > 0) {
42 /* Older firmwares have a race in sleep/wake-up process. Redo the process
43 * is sufficient to unfreeze the chip.
45 dev_err(wdev
->dev
, "timeout while wake up chip\n");
46 gpiod_set_value_cansleep(wdev
->pdata
.gpio_wakeup
, 0);
47 usleep_range(2000, 2500);
49 dev_err(wdev
->dev
, "max wake-up retries reached\n");
55 static void device_release(struct wfx_dev
*wdev
)
57 if (!wdev
->pdata
.gpio_wakeup
)
60 gpiod_set_value_cansleep(wdev
->pdata
.gpio_wakeup
, 0);
63 static int rx_helper(struct wfx_dev
*wdev
, size_t read_len
, int *is_cnf
)
66 struct wfx_hif_msg
*hif
;
72 WARN(read_len
> round_down(0xFFF, 2) * sizeof(u16
), "request exceed the chip capability");
74 /* Add 2 to take into account piggyback size */
75 alloc_len
= wdev
->hwbus_ops
->align_size(wdev
->hwbus_priv
, read_len
+ 2);
76 skb
= dev_alloc_skb(alloc_len
);
80 if (wfx_data_read(wdev
, skb
->data
, alloc_len
))
83 piggyback
= le16_to_cpup((__le16
*)(skb
->data
+ alloc_len
- 2));
84 _trace_piggyback(piggyback
, false);
86 hif
= (struct wfx_hif_msg
*)skb
->data
;
87 WARN(hif
->encrypted
& 0x3, "encryption is unsupported");
88 if (WARN(read_len
< sizeof(struct wfx_hif_msg
), "corrupted read"))
90 computed_len
= le16_to_cpu(hif
->len
);
91 computed_len
= round_up(computed_len
, 2);
92 if (computed_len
!= read_len
) {
93 dev_err(wdev
->dev
, "inconsistent message length: %zu != %zu\n",
94 computed_len
, read_len
);
95 print_hex_dump(KERN_INFO
, "hif: ", DUMP_PREFIX_OFFSET
, 16, 1,
100 if (!(hif
->id
& HIF_ID_IS_INDICATION
)) {
102 if (hif
->id
== HIF_CNF_ID_MULTI_TRANSMIT
)
104 ((struct wfx_hif_cnf_multi_transmit
*)hif
->body
)->num_tx_confs
;
107 WARN(wdev
->hif
.tx_buffers_used
< release_count
, "corrupted buffer counter");
108 wdev
->hif
.tx_buffers_used
-= release_count
;
110 _trace_hif_recv(hif
, wdev
->hif
.tx_buffers_used
);
112 if (hif
->id
!= HIF_IND_ID_EXCEPTION
&& hif
->id
!= HIF_IND_ID_ERROR
) {
113 if (hif
->seqnum
!= wdev
->hif
.rx_seqnum
)
114 dev_warn(wdev
->dev
, "wrong message sequence: %d != %d\n",
115 hif
->seqnum
, wdev
->hif
.rx_seqnum
);
116 wdev
->hif
.rx_seqnum
= (hif
->seqnum
+ 1) % (HIF_COUNTER_MAX
+ 1);
119 skb_put(skb
, le16_to_cpu(hif
->len
));
120 /* wfx_handle_rx takes care on SKB livetime */
121 wfx_handle_rx(wdev
, skb
);
122 if (!wdev
->hif
.tx_buffers_used
)
123 wake_up(&wdev
->hif
.tx_buffers_empty
);
133 static int bh_work_rx(struct wfx_dev
*wdev
, int max_msg
, int *num_cnf
)
137 int ctrl_reg
, piggyback
;
140 for (i
= 0; i
< max_msg
; i
++) {
141 if (piggyback
& CTRL_NEXT_LEN_MASK
)
142 ctrl_reg
= piggyback
;
143 else if (try_wait_for_completion(&wdev
->hif
.ctrl_ready
))
144 ctrl_reg
= atomic_xchg(&wdev
->hif
.ctrl_reg
, 0);
147 if (!(ctrl_reg
& CTRL_NEXT_LEN_MASK
))
149 /* ctrl_reg units are 16bits words */
150 len
= (ctrl_reg
& CTRL_NEXT_LEN_MASK
) * 2;
151 piggyback
= rx_helper(wdev
, len
, num_cnf
);
154 if (!(piggyback
& CTRL_WLAN_READY
))
155 dev_err(wdev
->dev
, "unexpected piggyback value: ready bit not set: %04x\n",
158 if (piggyback
& CTRL_NEXT_LEN_MASK
) {
159 ctrl_reg
= atomic_xchg(&wdev
->hif
.ctrl_reg
, piggyback
);
160 complete(&wdev
->hif
.ctrl_ready
);
162 dev_err(wdev
->dev
, "unexpected IRQ happened: %04x/%04x\n",
163 ctrl_reg
, piggyback
);
168 static void tx_helper(struct wfx_dev
*wdev
, struct wfx_hif_msg
*hif
)
172 bool is_encrypted
= false;
173 size_t len
= le16_to_cpu(hif
->len
);
175 WARN(len
< sizeof(*hif
), "try to send corrupted data");
177 hif
->seqnum
= wdev
->hif
.tx_seqnum
;
178 wdev
->hif
.tx_seqnum
= (wdev
->hif
.tx_seqnum
+ 1) % (HIF_COUNTER_MAX
+ 1);
181 WARN(len
> le16_to_cpu(wdev
->hw_caps
.size_inp_ch_buf
),
182 "request exceed the chip capability: %zu > %d\n",
183 len
, le16_to_cpu(wdev
->hw_caps
.size_inp_ch_buf
));
184 len
= wdev
->hwbus_ops
->align_size(wdev
->hwbus_priv
, len
);
185 ret
= wfx_data_write(wdev
, data
, len
);
189 wdev
->hif
.tx_buffers_used
++;
190 _trace_hif_send(hif
, wdev
->hif
.tx_buffers_used
);
196 static int bh_work_tx(struct wfx_dev
*wdev
, int max_msg
)
198 struct wfx_hif_msg
*hif
;
201 for (i
= 0; i
< max_msg
; i
++) {
203 if (wdev
->hif
.tx_buffers_used
< le16_to_cpu(wdev
->hw_caps
.num_inp_ch_bufs
)) {
204 if (try_wait_for_completion(&wdev
->hif_cmd
.ready
)) {
205 WARN(!mutex_is_locked(&wdev
->hif_cmd
.lock
), "data locking error");
206 hif
= wdev
->hif_cmd
.buf_send
;
208 hif
= wfx_tx_queues_get(wdev
);
213 tx_helper(wdev
, hif
);
218 /* In SDIO mode, it is necessary to make an access to a register to acknowledge last received
219 * message. It could be possible to restrict this acknowledge to SDIO mode and only if last
222 static void ack_sdio_data(struct wfx_dev
*wdev
)
226 wfx_config_reg_read(wdev
, &cfg_reg
);
227 if (cfg_reg
& 0xFF) {
228 dev_warn(wdev
->dev
, "chip reports errors: %02x\n", cfg_reg
& 0xFF);
229 wfx_config_reg_write_bits(wdev
, 0xFF, 0x00);
233 static void bh_work(struct work_struct
*work
)
235 struct wfx_dev
*wdev
= container_of(work
, struct wfx_dev
, hif
.bh
);
236 int stats_req
= 0, stats_cnf
= 0, stats_ind
= 0;
237 bool release_chip
= false, last_op_is_rx
= false;
242 num_tx
= bh_work_tx(wdev
, 32);
245 last_op_is_rx
= false;
246 num_rx
= bh_work_rx(wdev
, 32, &stats_cnf
);
249 last_op_is_rx
= true;
250 } while (num_rx
|| num_tx
);
251 stats_ind
-= stats_cnf
;
255 if (!wdev
->hif
.tx_buffers_used
&& !work_pending(work
)) {
256 device_release(wdev
);
259 _trace_bh_stats(stats_ind
, stats_req
, stats_cnf
, wdev
->hif
.tx_buffers_used
, release_chip
);
262 /* An IRQ from chip did occur */
263 void wfx_bh_request_rx(struct wfx_dev
*wdev
)
267 wfx_control_reg_read(wdev
, &cur
);
268 prev
= atomic_xchg(&wdev
->hif
.ctrl_reg
, cur
);
269 complete(&wdev
->hif
.ctrl_ready
);
270 queue_work(wdev
->bh_wq
, &wdev
->hif
.bh
);
272 if (!(cur
& CTRL_NEXT_LEN_MASK
))
273 dev_err(wdev
->dev
, "unexpected control register value: length field is 0: %04x\n",
276 dev_err(wdev
->dev
, "received IRQ but previous data was not (yet) read: %04x/%04x\n",
280 /* Driver want to send data */
281 void wfx_bh_request_tx(struct wfx_dev
*wdev
)
283 queue_work(wdev
->bh_wq
, &wdev
->hif
.bh
);
286 /* If IRQ is not available, this function allow to manually poll the control register and simulate
287 * an IRQ ahen an event happened.
289 * Note that the device has a bug: If an IRQ raise while host read control register, the IRQ is
290 * lost. So, use this function carefully (only duing device initialisation).
292 void wfx_bh_poll_irq(struct wfx_dev
*wdev
)
297 WARN(!wdev
->poll_irq
, "unexpected IRQ polling can mask IRQ");
298 flush_workqueue(wdev
->bh_wq
);
301 wfx_control_reg_read(wdev
, ®
);
305 if (ktime_after(now
, ktime_add_ms(start
, 1000))) {
306 dev_err(wdev
->dev
, "time out while polling control register\n");
311 wfx_bh_request_rx(wdev
);
314 void wfx_bh_register(struct wfx_dev
*wdev
)
316 INIT_WORK(&wdev
->hif
.bh
, bh_work
);
317 init_completion(&wdev
->hif
.ctrl_ready
);
318 init_waitqueue_head(&wdev
->hif
.tx_buffers_empty
);
321 void wfx_bh_unregister(struct wfx_dev
*wdev
)
323 flush_work(&wdev
->hif
.bh
);