Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Interrupt bottom half (BH).
0004  *
0005  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
0006  * Copyright (c) 2010, ST-Ericsson
0007  */
0008 #include <linux/gpio/consumer.h>
0009 #include <net/mac80211.h>
0010 
0011 #include "bh.h"
0012 #include "wfx.h"
0013 #include "hwio.h"
0014 #include "traces.h"
0015 #include "hif_rx.h"
0016 #include "hif_api_cmd.h"
0017 
0018 static void device_wakeup(struct wfx_dev *wdev)
0019 {
0020     int max_retry = 3;
0021 
0022     if (!wdev->pdata.gpio_wakeup)
0023         return;
0024     if (gpiod_get_value_cansleep(wdev->pdata.gpio_wakeup) > 0)
0025         return;
0026 
0027     if (wfx_api_older_than(wdev, 1, 4)) {
0028         gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1);
0029         if (!completion_done(&wdev->hif.ctrl_ready))
0030             usleep_range(2000, 2500);
0031         return;
0032     }
0033     for (;;) {
0034         gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1);
0035         /* completion.h does not provide any function to wait completion without consume it
0036          * (a kind of wait_for_completion_done_timeout()). So we have to emulate it.
0037          */
0038         if (wait_for_completion_timeout(&wdev->hif.ctrl_ready, msecs_to_jiffies(2))) {
0039             complete(&wdev->hif.ctrl_ready);
0040             return;
0041         } else if (max_retry-- > 0) {
0042             /* Older firmwares have a race in sleep/wake-up process.  Redo the process
0043              * is sufficient to unfreeze the chip.
0044              */
0045             dev_err(wdev->dev, "timeout while wake up chip\n");
0046             gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 0);
0047             usleep_range(2000, 2500);
0048         } else {
0049             dev_err(wdev->dev, "max wake-up retries reached\n");
0050             return;
0051         }
0052     }
0053 }
0054 
0055 static void device_release(struct wfx_dev *wdev)
0056 {
0057     if (!wdev->pdata.gpio_wakeup)
0058         return;
0059 
0060     gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 0);
0061 }
0062 
0063 static int rx_helper(struct wfx_dev *wdev, size_t read_len, int *is_cnf)
0064 {
0065     struct sk_buff *skb;
0066     struct wfx_hif_msg *hif;
0067     size_t alloc_len;
0068     size_t computed_len;
0069     int release_count;
0070     int piggyback = 0;
0071 
0072     WARN(read_len > round_down(0xFFF, 2) * sizeof(u16), "request exceed the chip capability");
0073 
0074     /* Add 2 to take into account piggyback size */
0075     alloc_len = wdev->hwbus_ops->align_size(wdev->hwbus_priv, read_len + 2);
0076     skb = dev_alloc_skb(alloc_len);
0077     if (!skb)
0078         return -ENOMEM;
0079 
0080     if (wfx_data_read(wdev, skb->data, alloc_len))
0081         goto err;
0082 
0083     piggyback = le16_to_cpup((__le16 *)(skb->data + alloc_len - 2));
0084     _trace_piggyback(piggyback, false);
0085 
0086     hif = (struct wfx_hif_msg *)skb->data;
0087     WARN(hif->encrypted & 0x3, "encryption is unsupported");
0088     if (WARN(read_len < sizeof(struct wfx_hif_msg), "corrupted read"))
0089         goto err;
0090     computed_len = le16_to_cpu(hif->len);
0091     computed_len = round_up(computed_len, 2);
0092     if (computed_len != read_len) {
0093         dev_err(wdev->dev, "inconsistent message length: %zu != %zu\n",
0094             computed_len, read_len);
0095         print_hex_dump(KERN_INFO, "hif: ", DUMP_PREFIX_OFFSET, 16, 1,
0096                    hif, read_len, true);
0097         goto err;
0098     }
0099 
0100     if (!(hif->id & HIF_ID_IS_INDICATION)) {
0101         (*is_cnf)++;
0102         if (hif->id == HIF_CNF_ID_MULTI_TRANSMIT)
0103             release_count =
0104                 ((struct wfx_hif_cnf_multi_transmit *)hif->body)->num_tx_confs;
0105         else
0106             release_count = 1;
0107         WARN(wdev->hif.tx_buffers_used < release_count, "corrupted buffer counter");
0108         wdev->hif.tx_buffers_used -= release_count;
0109     }
0110     _trace_hif_recv(hif, wdev->hif.tx_buffers_used);
0111 
0112     if (hif->id != HIF_IND_ID_EXCEPTION && hif->id != HIF_IND_ID_ERROR) {
0113         if (hif->seqnum != wdev->hif.rx_seqnum)
0114             dev_warn(wdev->dev, "wrong message sequence: %d != %d\n",
0115                  hif->seqnum, wdev->hif.rx_seqnum);
0116         wdev->hif.rx_seqnum = (hif->seqnum + 1) % (HIF_COUNTER_MAX + 1);
0117     }
0118 
0119     skb_put(skb, le16_to_cpu(hif->len));
0120     /* wfx_handle_rx takes care on SKB livetime */
0121     wfx_handle_rx(wdev, skb);
0122     if (!wdev->hif.tx_buffers_used)
0123         wake_up(&wdev->hif.tx_buffers_empty);
0124 
0125     return piggyback;
0126 
0127 err:
0128     if (skb)
0129         dev_kfree_skb(skb);
0130     return -EIO;
0131 }
0132 
0133 static int bh_work_rx(struct wfx_dev *wdev, int max_msg, int *num_cnf)
0134 {
0135     size_t len;
0136     int i;
0137     int ctrl_reg, piggyback;
0138 
0139     piggyback = 0;
0140     for (i = 0; i < max_msg; i++) {
0141         if (piggyback & CTRL_NEXT_LEN_MASK)
0142             ctrl_reg = piggyback;
0143         else if (try_wait_for_completion(&wdev->hif.ctrl_ready))
0144             ctrl_reg = atomic_xchg(&wdev->hif.ctrl_reg, 0);
0145         else
0146             ctrl_reg = 0;
0147         if (!(ctrl_reg & CTRL_NEXT_LEN_MASK))
0148             return i;
0149         /* ctrl_reg units are 16bits words */
0150         len = (ctrl_reg & CTRL_NEXT_LEN_MASK) * 2;
0151         piggyback = rx_helper(wdev, len, num_cnf);
0152         if (piggyback < 0)
0153             return i;
0154         if (!(piggyback & CTRL_WLAN_READY))
0155             dev_err(wdev->dev, "unexpected piggyback value: ready bit not set: %04x\n",
0156                 piggyback);
0157     }
0158     if (piggyback & CTRL_NEXT_LEN_MASK) {
0159         ctrl_reg = atomic_xchg(&wdev->hif.ctrl_reg, piggyback);
0160         complete(&wdev->hif.ctrl_ready);
0161         if (ctrl_reg)
0162             dev_err(wdev->dev, "unexpected IRQ happened: %04x/%04x\n",
0163                 ctrl_reg, piggyback);
0164     }
0165     return i;
0166 }
0167 
0168 static void tx_helper(struct wfx_dev *wdev, struct wfx_hif_msg *hif)
0169 {
0170     int ret;
0171     void *data;
0172     bool is_encrypted = false;
0173     size_t len = le16_to_cpu(hif->len);
0174 
0175     WARN(len < sizeof(*hif), "try to send corrupted data");
0176 
0177     hif->seqnum = wdev->hif.tx_seqnum;
0178     wdev->hif.tx_seqnum = (wdev->hif.tx_seqnum + 1) % (HIF_COUNTER_MAX + 1);
0179 
0180     data = hif;
0181     WARN(len > le16_to_cpu(wdev->hw_caps.size_inp_ch_buf),
0182          "request exceed the chip capability: %zu > %d\n",
0183          len, le16_to_cpu(wdev->hw_caps.size_inp_ch_buf));
0184     len = wdev->hwbus_ops->align_size(wdev->hwbus_priv, len);
0185     ret = wfx_data_write(wdev, data, len);
0186     if (ret)
0187         goto end;
0188 
0189     wdev->hif.tx_buffers_used++;
0190     _trace_hif_send(hif, wdev->hif.tx_buffers_used);
0191 end:
0192     if (is_encrypted)
0193         kfree(data);
0194 }
0195 
0196 static int bh_work_tx(struct wfx_dev *wdev, int max_msg)
0197 {
0198     struct wfx_hif_msg *hif;
0199     int i;
0200 
0201     for (i = 0; i < max_msg; i++) {
0202         hif = NULL;
0203         if (wdev->hif.tx_buffers_used < le16_to_cpu(wdev->hw_caps.num_inp_ch_bufs)) {
0204             if (try_wait_for_completion(&wdev->hif_cmd.ready)) {
0205                 WARN(!mutex_is_locked(&wdev->hif_cmd.lock), "data locking error");
0206                 hif = wdev->hif_cmd.buf_send;
0207             } else {
0208                 hif = wfx_tx_queues_get(wdev);
0209             }
0210         }
0211         if (!hif)
0212             return i;
0213         tx_helper(wdev, hif);
0214     }
0215     return i;
0216 }
0217 
0218 /* In SDIO mode, it is necessary to make an access to a register to acknowledge last received
0219  * message. It could be possible to restrict this acknowledge to SDIO mode and only if last
0220  * operation was rx.
0221  */
0222 static void ack_sdio_data(struct wfx_dev *wdev)
0223 {
0224     u32 cfg_reg;
0225 
0226     wfx_config_reg_read(wdev, &cfg_reg);
0227     if (cfg_reg & 0xFF) {
0228         dev_warn(wdev->dev, "chip reports errors: %02x\n", cfg_reg & 0xFF);
0229         wfx_config_reg_write_bits(wdev, 0xFF, 0x00);
0230     }
0231 }
0232 
0233 static void bh_work(struct work_struct *work)
0234 {
0235     struct wfx_dev *wdev = container_of(work, struct wfx_dev, hif.bh);
0236     int stats_req = 0, stats_cnf = 0, stats_ind = 0;
0237     bool release_chip = false, last_op_is_rx = false;
0238     int num_tx, num_rx;
0239 
0240     device_wakeup(wdev);
0241     do {
0242         num_tx = bh_work_tx(wdev, 32);
0243         stats_req += num_tx;
0244         if (num_tx)
0245             last_op_is_rx = false;
0246         num_rx = bh_work_rx(wdev, 32, &stats_cnf);
0247         stats_ind += num_rx;
0248         if (num_rx)
0249             last_op_is_rx = true;
0250     } while (num_rx || num_tx);
0251     stats_ind -= stats_cnf;
0252 
0253     if (last_op_is_rx)
0254         ack_sdio_data(wdev);
0255     if (!wdev->hif.tx_buffers_used && !work_pending(work)) {
0256         device_release(wdev);
0257         release_chip = true;
0258     }
0259     _trace_bh_stats(stats_ind, stats_req, stats_cnf, wdev->hif.tx_buffers_used, release_chip);
0260 }
0261 
0262 /* An IRQ from chip did occur */
0263 void wfx_bh_request_rx(struct wfx_dev *wdev)
0264 {
0265     u32 cur, prev;
0266 
0267     wfx_control_reg_read(wdev, &cur);
0268     prev = atomic_xchg(&wdev->hif.ctrl_reg, cur);
0269     complete(&wdev->hif.ctrl_ready);
0270     queue_work(wdev->bh_wq, &wdev->hif.bh);
0271 
0272     if (!(cur & CTRL_NEXT_LEN_MASK))
0273         dev_err(wdev->dev, "unexpected control register value: length field is 0: %04x\n",
0274             cur);
0275     if (prev != 0)
0276         dev_err(wdev->dev, "received IRQ but previous data was not (yet) read: %04x/%04x\n",
0277             prev, cur);
0278 }
0279 
0280 /* Driver want to send data */
0281 void wfx_bh_request_tx(struct wfx_dev *wdev)
0282 {
0283     queue_work(wdev->bh_wq, &wdev->hif.bh);
0284 }
0285 
0286 /* If IRQ is not available, this function allow to manually poll the control register and simulate
0287  * an IRQ ahen an event happened.
0288  *
0289  * Note that the device has a bug: If an IRQ raise while host read control register, the IRQ is
0290  * lost. So, use this function carefully (only duing device initialisation).
0291  */
0292 void wfx_bh_poll_irq(struct wfx_dev *wdev)
0293 {
0294     ktime_t now, start;
0295     u32 reg;
0296 
0297     WARN(!wdev->poll_irq, "unexpected IRQ polling can mask IRQ");
0298     flush_workqueue(wdev->bh_wq);
0299     start = ktime_get();
0300     for (;;) {
0301         wfx_control_reg_read(wdev, &reg);
0302         now = ktime_get();
0303         if (reg & 0xFFF)
0304             break;
0305         if (ktime_after(now, ktime_add_ms(start, 1000))) {
0306             dev_err(wdev->dev, "time out while polling control register\n");
0307             return;
0308         }
0309         udelay(200);
0310     }
0311     wfx_bh_request_rx(wdev);
0312 }
0313 
0314 void wfx_bh_register(struct wfx_dev *wdev)
0315 {
0316     INIT_WORK(&wdev->hif.bh, bh_work);
0317     init_completion(&wdev->hif.ctrl_ready);
0318     init_waitqueue_head(&wdev->hif.tx_buffers_empty);
0319 }
0320 
0321 void wfx_bh_unregister(struct wfx_dev *wdev)
0322 {
0323     flush_work(&wdev->hif.bh);
0324 }