Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging: wfx: add IRQ handling

bh_work() is in charge to schedule all HIF message from/to chip.

On normal operation, when an IRQ is received, driver can get size of
next message in control register. In order to save control register
access, when chip send a message, it also appends a copy of control
register after the message (this register is not accounted in message
length declared in message header, but must accounted in bus request).
This copy of control register is called "piggyback".

It also handles a power saving mechanism specific to WFxxx series. This
mechanism is based on a GPIO called "wakeup" GPIO. Obviously, this gpio
is not part of SPI/SDIO standard buses and must be declared
independently (this is the main reason for why SDIO mode try to get
parameters from DT).

When wakeup is enabled, host can communicate with chip only if it is
awake. To wake up chip, there are two cases:
- host receive an IRQ from chip (chip initiate communication): host
just have to set wakeup GPIO before reading data
- host want to send data to chip: host set wakeup GPIO, then wait
for an IRQ (in fact, wait for an empty message) and finally send data

bh_work() is also in charge to track usage of chip buffers. Normally
each request expect a confirmation. However, you can notice that special
"multi tx" confirmation can acknowledge multiple requests at time.

Finally, note that wfx_bh_request_rx() is not atomic (because of
control_reg_read()). So, in SPI mode, hard-irq handler only postpone all
processing to wfx_spi_request_rx().

Signed-off-by: Jérôme Pouiller <jerome.pouiller@silabs.com>
Link: https://lore.kernel.org/r/20190919142527.31797-8-Jerome.Pouiller@silabs.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Jérôme Pouiller and committed by
Greg Kroah-Hartman
b0998f0c e4ee3cb3

+345 -1
+1
drivers/staging/wfx/Makefile
··· 4 4 CFLAGS_debug.o = -I$(src) 5 5 6 6 wfx-y := \ 7 + bh.o \ 7 8 hwio.o \ 8 9 fwio.o \ 9 10 main.o \
+277
drivers/staging/wfx/bh.c
··· 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 "hif_api_cmd.h" 15 + 16 + static void device_wakeup(struct wfx_dev *wdev) 17 + { 18 + if (!wdev->pdata.gpio_wakeup) 19 + return; 20 + if (gpiod_get_value(wdev->pdata.gpio_wakeup)) 21 + return; 22 + 23 + gpiod_set_value(wdev->pdata.gpio_wakeup, 1); 24 + if (wfx_api_older_than(wdev, 1, 4)) { 25 + if (!completion_done(&wdev->hif.ctrl_ready)) 26 + udelay(2000); 27 + } else { 28 + // completion.h does not provide any function to wait 29 + // completion without consume it (a kind of 30 + // wait_for_completion_done_timeout()). So we have to emulate 31 + // it. 32 + if (wait_for_completion_timeout(&wdev->hif.ctrl_ready, msecs_to_jiffies(2) + 1)) 33 + complete(&wdev->hif.ctrl_ready); 34 + else 35 + dev_err(wdev->dev, "timeout while wake up chip\n"); 36 + } 37 + } 38 + 39 + static void device_release(struct wfx_dev *wdev) 40 + { 41 + if (!wdev->pdata.gpio_wakeup) 42 + return; 43 + 44 + gpiod_set_value(wdev->pdata.gpio_wakeup, 0); 45 + } 46 + 47 + static int rx_helper(struct wfx_dev *wdev, size_t read_len, int *is_cnf) 48 + { 49 + struct sk_buff *skb; 50 + struct hif_msg *hif; 51 + size_t alloc_len; 52 + size_t computed_len; 53 + int release_count; 54 + int piggyback = 0; 55 + 56 + WARN_ON(read_len < 4); 57 + WARN(read_len > round_down(0xFFF, 2) * sizeof(u16), 58 + "%s: request exceed WFx capability", __func__); 59 + 60 + // Add 2 to take into account piggyback size 61 + alloc_len = wdev->hwbus_ops->align_size(wdev->hwbus_priv, read_len + 2); 62 + skb = dev_alloc_skb(alloc_len); 63 + if (!skb) 64 + return -ENOMEM; 65 + 66 + if (wfx_data_read(wdev, skb->data, alloc_len)) 67 + goto err; 68 + 69 + piggyback = le16_to_cpup((u16 *) (skb->data + alloc_len - 2)); 70 + 71 + hif = (struct hif_msg *) skb->data; 72 + WARN(hif->encrypted & 0x1, "unsupported encryption type"); 73 + if (hif->encrypted == 0x2) { 74 + BUG(); // Not yet implemented 75 + } else { 76 + le16_to_cpus(hif->len); 77 + computed_len = round_up(hif->len, 2); 78 + } 79 + if (computed_len != read_len) { 80 + dev_err(wdev->dev, "inconsistent message length: %zu != %zu\n", 81 + computed_len, read_len); 82 + print_hex_dump(KERN_INFO, "hif: ", DUMP_PREFIX_OFFSET, 16, 1, 83 + hif, read_len, true); 84 + goto err; 85 + } 86 + 87 + if (!(hif->id & HIF_ID_IS_INDICATION)) { 88 + (*is_cnf)++; 89 + if (hif->id == HIF_CNF_ID_MULTI_TRANSMIT) 90 + release_count = le32_to_cpu(((struct hif_cnf_multi_transmit *) hif->body)->num_tx_confs); 91 + else 92 + release_count = 1; 93 + WARN(wdev->hif.tx_buffers_used < release_count, "corrupted buffer counter"); 94 + wdev->hif.tx_buffers_used -= release_count; 95 + if (!wdev->hif.tx_buffers_used) 96 + wake_up(&wdev->hif.tx_buffers_empty); 97 + } 98 + 99 + if (hif->id != HIF_IND_ID_EXCEPTION && hif->id != HIF_IND_ID_ERROR) { 100 + if (hif->seqnum != wdev->hif.rx_seqnum) 101 + dev_warn(wdev->dev, "wrong message sequence: %d != %d\n", 102 + hif->seqnum, wdev->hif.rx_seqnum); 103 + wdev->hif.rx_seqnum = (hif->seqnum + 1) % (HIF_COUNTER_MAX + 1); 104 + } 105 + 106 + skb_put(skb, hif->len); 107 + dev_kfree_skb(skb); /* FIXME: handle received data */ 108 + 109 + return piggyback; 110 + 111 + err: 112 + if (skb) 113 + dev_kfree_skb(skb); 114 + return -EIO; 115 + } 116 + 117 + static int bh_work_rx(struct wfx_dev *wdev, int max_msg, int *num_cnf) 118 + { 119 + size_t len; 120 + int i; 121 + int ctrl_reg, piggyback; 122 + 123 + piggyback = 0; 124 + for (i = 0; i < max_msg; i++) { 125 + if (piggyback & CTRL_NEXT_LEN_MASK) 126 + ctrl_reg = piggyback; 127 + else if (try_wait_for_completion(&wdev->hif.ctrl_ready)) 128 + ctrl_reg = atomic_xchg(&wdev->hif.ctrl_reg, 0); 129 + else 130 + ctrl_reg = 0; 131 + if (!(ctrl_reg & CTRL_NEXT_LEN_MASK)) 132 + return i; 133 + // ctrl_reg units are 16bits words 134 + len = (ctrl_reg & CTRL_NEXT_LEN_MASK) * 2; 135 + piggyback = rx_helper(wdev, len, num_cnf); 136 + if (piggyback < 0) 137 + return i; 138 + if (!(piggyback & CTRL_WLAN_READY)) 139 + dev_err(wdev->dev, "unexpected piggyback value: ready bit not set: %04x\n", 140 + piggyback); 141 + } 142 + if (piggyback & CTRL_NEXT_LEN_MASK) { 143 + ctrl_reg = atomic_xchg(&wdev->hif.ctrl_reg, piggyback); 144 + complete(&wdev->hif.ctrl_ready); 145 + if (ctrl_reg) 146 + dev_err(wdev->dev, "unexpected IRQ happened: %04x/%04x\n", 147 + ctrl_reg, piggyback); 148 + } 149 + return i; 150 + } 151 + 152 + static void tx_helper(struct wfx_dev *wdev, struct hif_msg *hif) 153 + { 154 + int ret; 155 + void *data; 156 + bool is_encrypted = false; 157 + size_t len = le16_to_cpu(hif->len); 158 + 159 + BUG_ON(len < sizeof(*hif)); 160 + 161 + hif->seqnum = wdev->hif.tx_seqnum; 162 + wdev->hif.tx_seqnum = (wdev->hif.tx_seqnum + 1) % (HIF_COUNTER_MAX + 1); 163 + 164 + data = hif; 165 + WARN(len > wdev->hw_caps.size_inp_ch_buf, 166 + "%s: request exceed WFx capability: %zu > %d\n", __func__, 167 + len, wdev->hw_caps.size_inp_ch_buf); 168 + len = wdev->hwbus_ops->align_size(wdev->hwbus_priv, len); 169 + ret = wfx_data_write(wdev, data, len); 170 + if (ret) 171 + goto end; 172 + 173 + wdev->hif.tx_buffers_used++; 174 + end: 175 + if (is_encrypted) 176 + kfree(data); 177 + } 178 + 179 + static int bh_work_tx(struct wfx_dev *wdev, int max_msg) 180 + { 181 + struct hif_msg *hif; 182 + int i; 183 + 184 + for (i = 0; i < max_msg; i++) { 185 + hif = NULL; 186 + if (wdev->hif.tx_buffers_used < wdev->hw_caps.num_inp_ch_bufs) { 187 + /* FIXME: get queued data */ 188 + } 189 + if (!hif) 190 + return i; 191 + tx_helper(wdev, hif); 192 + } 193 + return i; 194 + } 195 + 196 + /* In SDIO mode, it is necessary to make an access to a register to acknowledge 197 + * last received message. It could be possible to restrict this acknowledge to 198 + * SDIO mode and only if last operation was rx. 199 + */ 200 + static void ack_sdio_data(struct wfx_dev *wdev) 201 + { 202 + uint32_t cfg_reg; 203 + 204 + config_reg_read(wdev, &cfg_reg); 205 + if (cfg_reg & 0xFF) { 206 + dev_warn(wdev->dev, "chip reports errors: %02x\n", cfg_reg & 0xFF); 207 + config_reg_write_bits(wdev, 0xFF, 0x00); 208 + } 209 + } 210 + 211 + static void bh_work(struct work_struct *work) 212 + { 213 + struct wfx_dev *wdev = container_of(work, struct wfx_dev, hif.bh); 214 + int stats_req = 0, stats_cnf = 0, stats_ind = 0; 215 + bool release_chip = false, last_op_is_rx = false; 216 + int num_tx, num_rx; 217 + 218 + device_wakeup(wdev); 219 + do { 220 + num_tx = bh_work_tx(wdev, 32); 221 + stats_req += num_tx; 222 + if (num_tx) 223 + last_op_is_rx = false; 224 + num_rx = bh_work_rx(wdev, 32, &stats_cnf); 225 + stats_ind += num_rx; 226 + if (num_rx) 227 + last_op_is_rx = true; 228 + } while (num_rx || num_tx); 229 + stats_ind -= stats_cnf; 230 + 231 + if (last_op_is_rx) 232 + ack_sdio_data(wdev); 233 + if (!wdev->hif.tx_buffers_used && !work_pending(work)) { 234 + device_release(wdev); 235 + release_chip = true; 236 + } 237 + } 238 + 239 + /* 240 + * An IRQ from chip did occur 241 + */ 242 + void wfx_bh_request_rx(struct wfx_dev *wdev) 243 + { 244 + u32 cur, prev; 245 + 246 + control_reg_read(wdev, &cur); 247 + prev = atomic_xchg(&wdev->hif.ctrl_reg, cur); 248 + complete(&wdev->hif.ctrl_ready); 249 + queue_work(system_highpri_wq, &wdev->hif.bh); 250 + 251 + if (!(cur & CTRL_NEXT_LEN_MASK)) 252 + dev_err(wdev->dev, "unexpected control register value: length field is 0: %04x\n", 253 + cur); 254 + if (prev != 0) 255 + dev_err(wdev->dev, "received IRQ but previous data was not (yet) read: %04x/%04x\n", 256 + prev, cur); 257 + } 258 + 259 + /* 260 + * Driver want to send data 261 + */ 262 + void wfx_bh_request_tx(struct wfx_dev *wdev) 263 + { 264 + queue_work(system_highpri_wq, &wdev->hif.bh); 265 + } 266 + 267 + void wfx_bh_register(struct wfx_dev *wdev) 268 + { 269 + INIT_WORK(&wdev->hif.bh, bh_work); 270 + init_completion(&wdev->hif.ctrl_ready); 271 + init_waitqueue_head(&wdev->hif.tx_buffers_empty); 272 + } 273 + 274 + void wfx_bh_unregister(struct wfx_dev *wdev) 275 + { 276 + flush_work(&wdev->hif.bh); 277 + }
+32
drivers/staging/wfx/bh.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + /* 3 + * Interrupt bottom half. 4 + * 5 + * Copyright (c) 2017-2019, Silicon Laboratories, Inc. 6 + * Copyright (c) 2010, ST-Ericsson 7 + */ 8 + #ifndef WFX_BH_H 9 + #define WFX_BH_H 10 + 11 + #include <linux/atomic.h> 12 + #include <linux/wait.h> 13 + #include <linux/workqueue.h> 14 + 15 + struct wfx_dev; 16 + 17 + struct wfx_hif { 18 + struct work_struct bh; 19 + struct completion ctrl_ready; 20 + wait_queue_head_t tx_buffers_empty; 21 + atomic_t ctrl_reg; 22 + int rx_seqnum; 23 + int tx_seqnum; 24 + int tx_buffers_used; 25 + }; 26 + 27 + void wfx_bh_register(struct wfx_dev *wdev); 28 + void wfx_bh_unregister(struct wfx_dev *wdev); 29 + void wfx_bh_request_rx(struct wfx_dev *wdev); 30 + void wfx_bh_request_tx(struct wfx_dev *wdev); 31 + 32 + #endif /* WFX_BH_H */
+3 -1
drivers/staging/wfx/bus_sdio.c
··· 15 15 #include "wfx.h" 16 16 #include "hwio.h" 17 17 #include "main.h" 18 + #include "bh.h" 18 19 19 20 static const struct wfx_platform_data wfx_sdio_pdata = { 20 21 .file_fw = "wfm_wf200", ··· 91 90 struct wfx_sdio_priv *bus = sdio_get_drvdata(func); 92 91 93 92 if (bus->core) 94 - /* empty */; 93 + wfx_bh_request_rx(bus->core); 95 94 else 96 95 WARN(!bus->core, "race condition in driver init/deinit"); 97 96 } ··· 105 104 return IRQ_NONE; 106 105 } 107 106 sdio_claim_host(bus->func); 107 + wfx_bh_request_rx(bus->core); 108 108 sdio_release_host(bus->func); 109 109 return IRQ_HANDLED; 110 110 }
+5
drivers/staging/wfx/bus_spi.c
··· 18 18 #include "wfx.h" 19 19 #include "hwio.h" 20 20 #include "main.h" 21 + #include "bh.h" 21 22 22 23 static int gpio_reset = -2; 23 24 module_param(gpio_reset, int, 0644); ··· 145 144 146 145 static void wfx_spi_request_rx(struct work_struct *work) 147 146 { 147 + struct wfx_spi_priv *bus = 148 + container_of(work, struct wfx_spi_priv, request_rx); 149 + 150 + wfx_bh_request_rx(bus->core); 148 151 } 149 152 150 153 static size_t wfx_spi_align_size(void *priv, size_t size)
+21
drivers/staging/wfx/main.c
··· 23 23 #include "fwio.h" 24 24 #include "hwio.h" 25 25 #include "bus.h" 26 + #include "bh.h" 26 27 #include "wfx_version.h" 27 28 28 29 MODULE_DESCRIPTION("Silicon Labs 802.11 Wireless LAN driver for WFx"); 29 30 MODULE_AUTHOR("Jérôme Pouiller <jerome.pouiller@silabs.com>"); 30 31 MODULE_LICENSE("GPL"); 31 32 MODULE_VERSION(WFX_LABEL); 33 + 34 + static int gpio_wakeup = -2; 35 + module_param(gpio_wakeup, int, 0644); 36 + MODULE_PARM_DESC(gpio_wakeup, "gpio number for wakeup. -1 for none."); 37 + 38 + bool wfx_api_older_than(struct wfx_dev *wdev, int major, int minor) 39 + { 40 + if (wdev->hw_caps.api_version_major < major) 41 + return true; 42 + if (wdev->hw_caps.api_version_major > major) 43 + return false; 44 + if (wdev->hw_caps.api_version_minor < minor) 45 + return true; 46 + return false; 47 + } 32 48 33 49 struct gpio_desc *wfx_get_gpio(struct device *dev, int override, const char *label) 34 50 { ··· 98 82 { 99 83 int err; 100 84 85 + wfx_bh_register(wdev); 86 + 101 87 err = wfx_init_device(wdev); 102 88 if (err) 103 89 goto err1; 104 90 91 + 105 92 return 0; 106 93 107 94 err1: 95 + wfx_bh_unregister(wdev); 108 96 return err; 109 97 } 110 98 111 99 void wfx_release(struct wfx_dev *wdev) 112 100 { 101 + wfx_bh_unregister(wdev); 113 102 } 114 103 115 104 static int __init wfx_core_init(void)
+2
drivers/staging/wfx/main.h
··· 20 20 struct wfx_platform_data { 21 21 /* Keyset and ".sec" extention will appended to this string */ 22 22 const char *file_fw; 23 + struct gpio_desc *gpio_wakeup; 23 24 /* 24 25 * if true HIF D_out is sampled on the rising edge of the clock 25 26 * (intended to be used in 50Mhz SDIO) ··· 39 38 40 39 struct gpio_desc *wfx_get_gpio(struct device *dev, int override, 41 40 const char *label); 41 + bool wfx_api_older_than(struct wfx_dev *wdev, int major, int minor); 42 42 43 43 #endif
+4
drivers/staging/wfx/wfx.h
··· 10 10 #ifndef WFX_H 11 11 #define WFX_H 12 12 13 + #include "bh.h" 13 14 #include "main.h" 15 + #include "hif_api_general.h" 14 16 15 17 struct hwbus_ops; 16 18 ··· 23 21 void *hwbus_priv; 24 22 25 23 u8 keyset; 24 + struct hif_ind_startup hw_caps; 25 + struct wfx_hif hif; 26 26 }; 27 27 28 28 #endif /* WFX_H */