From patchwork Tue Oct 5 13:53:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?SsOpcsO0bWUgUG91aWxsZXI=?= X-Patchwork-Id: 12536785 X-Patchwork-Delegate: kvalo@adurom.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EE205C43219 for ; Tue, 5 Oct 2021 13:56:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D98A26023D for ; Tue, 5 Oct 2021 13:56:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235635AbhJEN6B (ORCPT ); Tue, 5 Oct 2021 09:58:01 -0400 Received: from mail-bn8nam11on2074.outbound.protection.outlook.com ([40.107.236.74]:47152 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235888AbhJEN5B (ORCPT ); Tue, 5 Oct 2021 09:57:01 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OR4MdQ7iI2ZkksIj7WGdAg5iynDeTEb1eVGovy+pq+Qt2EDHCOKmF4f15b1NMyWd3kGRFEYLGlYlVjy87w751oqc3hmQknU7bSH/2QqyDwqWGCQUV0VEZBbd5vU6dJFqwe3hSlaRDB3RBmCerFeGoAYhpzzNiGqHD4cDexHdj1dyl7l2ohFYyMe9PM4X1hQnw7BYUul9qAbUAVIFFfT+bW9qHuRTT0SqdLKpUvGhjONdIzKd0EKuT8NMw+Pa1ifOhmR8mVYYNKR0dd4rWDBDbU7ZQZVNifM3V6HCVg9pROlhSJkrV+GXuqa8XUT8tKwRnv1n92Dk1fsnoYnerpWM8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=gsQIP6+Yx2+JFJUZgYmmadZN1sDsQk3ghocjv7zWQZg=; b=OM1gXXNQOtM3UN/GpnEJX9r1jVDLaSfETC7Hrz333I39H7A7VEtp/z4v7mpS2utYQfGkoVGH4qw9XyXtsnKOBQZ6Ycwg46BGa63++sCTNRZlh+H9+hWpPpj3P4bsMOYpZN8CkR8WuU1NLJn5jt8ic6NdtIFkMDwEhS2YLbGnfy9UkpHQuq3elVNraWbTgJiOQk+xXGg7yJFL8kRa8z7fAX/6zLVTqQd4HDMUYB3rWfHIWJX9vM4JsVqywHooaoEwASVIspyWVhGCOUWncVWHxy6WVp58lZXuhKOzbkDKJslT8adZe0nTcR1wnGfD1hLPYkLOTaa5J57tdqah0ly3/A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=silabs.com; dmarc=pass action=none header.from=silabs.com; dkim=pass header.d=silabs.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=silabs.onmicrosoft.com; s=selector2-silabs-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gsQIP6+Yx2+JFJUZgYmmadZN1sDsQk3ghocjv7zWQZg=; b=ERXMI2ZIwcTNNaRRlA9VEIx9qdH1bfI8PDS2L7ChXoTqAhg923UTbrDPKsa0Vlf+zLPuqlVZSuEea+OsS0qw6VRD7lmg2+xfhux0xxhl+f9gZzZys0jT+eo2M+wa/b/a7fFfOBDoKZV0pr9Tj1eCUmHv8taCcDylrwPJPXShPAM= Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=silabs.com; Received: from PH0PR11MB5657.namprd11.prod.outlook.com (2603:10b6:510:ee::19) by PH0PR11MB5644.namprd11.prod.outlook.com (2603:10b6:510:ef::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.14; Tue, 5 Oct 2021 13:54:52 +0000 Received: from PH0PR11MB5657.namprd11.prod.outlook.com ([fe80::31cb:3b13:b0e8:d8f4]) by PH0PR11MB5657.namprd11.prod.outlook.com ([fe80::31cb:3b13:b0e8:d8f4%9]) with mapi id 15.20.4566.022; Tue, 5 Oct 2021 13:54:52 +0000 From: Jerome Pouiller To: linux-wireless@vger.kernel.org, netdev@vger.kernel.org, Kalle Valo Cc: devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Greg Kroah-Hartman , "David S . Miller" , devicetree@vger.kernel.org, Rob Herring , linux-mmc@vger.kernel.org, =?utf-8?q?Pali?= =?utf-8?q?_Roh=C3=A1r?= , Ulf Hansson , =?utf-8?b?SsOpcsO0bWUgUG91aWxsZXI=?= Subject: [PATCH v8 11/24] wfx: add bh.c/bh.h Date: Tue, 5 Oct 2021 15:53:47 +0200 Message-Id: <20211005135400.788058-12-Jerome.Pouiller@silabs.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211005135400.788058-1-Jerome.Pouiller@silabs.com> References: <20211005135400.788058-1-Jerome.Pouiller@silabs.com> X-ClientProxiedBy: PR3P189CA0084.EURP189.PROD.OUTLOOK.COM (2603:10a6:102:b4::29) To PH0PR11MB5657.namprd11.prod.outlook.com (2603:10b6:510:ee::19) MIME-Version: 1.0 Received: from pc-42.silabs.com (37.71.187.125) by PR3P189CA0084.EURP189.PROD.OUTLOOK.COM (2603:10a6:102:b4::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4566.14 via Frontend Transport; Tue, 5 Oct 2021 13:54:49 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: df666f70-8d20-4766-69ab-08d98807b43f X-MS-TrafficTypeDiagnostic: PH0PR11MB5644: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0+OGQL3mow2UtgumE4GnB9Z+uza0JrFFeYWwWmCYop/wD7xM6OVi7pMo3wSoru3+yH5bKThhk9jtl1MhPU4/4om5htlVaQy9Hpn9QeeVZ4FcOwREXRKBIzwY92FfoHzAb8BonlGm4VWYrDebxg9ep6kqIOBf1rlSjY9znZrmFeF+QTKFD8WMdpobdBLOCVd4GcnufRMri/r5R7CyB5295unp5PDzuiyxZKFfb+olXC9CI4xnwOuqNzG8mW1ocHwojNt+1XtEQevdKhmsjLrBfptKgw2TfAIH6nad7DySweXew9qjnHRqg0LSv7+Qj271ymhrZ7rilLy81tcGuEOawLUeMAdsSR863QOebRTIu/wDEvyK6aDxsowh1CdmCi8OTBd6Dmll4L83r7r3yuAGp25Yo2gkrmsJwjot7XiIIB5o5eC5gUCH6/qC2Pa9xmzRrkEJJSksZJemslqhOmK3+I1PmdGxPGUuyn7y35MlkuuxbRT4XoNXw63rTPAsyQlG023KuchRnWhw7B4wK/zmK0Euug0x0RwoJroUIfH71bvQ+bfr2R6AzsT5J1McyQhH5IqsyAwb1L5FpnyqRH+KnUsG0bxVDbHRc0S9mCx4lxOieYBt5iT1RxmbSbSGhQGr/8C0v/kaVlqwwszvLO/YJ6axF5LuY1yzUuJ2a9FggmJVOdaBcCAymx4EMk1b27xdmSutvMI4JU+j4KEoQZ44jQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH0PR11MB5657.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(66946007)(66556008)(8936002)(6916009)(83380400001)(956004)(508600001)(2616005)(66476007)(26005)(186003)(7416002)(2906002)(36756003)(4326008)(30864003)(8676002)(107886003)(66574015)(7696005)(52116002)(38100700002)(316002)(1076003)(5660300002)(86362001)(54906003)(38350700002)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?HpwCFhOmc3Q2CJ2zkm+/4CRgGiar?= =?utf-8?q?s82/exJN005HxE6RQ0gKl7ZLZy2CcdJLZ33QL9WhdzUZRLJXCZ9iGAyO7MwVYjXKx?= =?utf-8?q?UR3IJcpiLkFIEcdtM1vDNCZLhHPORYiYzLyLHG3t0Ta3nTDpjo1Jx8Jziz3ppPGeN?= =?utf-8?q?4XBHDuTGxKaIZg55V/HSS8zKvqgFYBAVRaN4eFfLgP4jfRF5N+S0NxZNM7aQyTGnQ?= =?utf-8?q?DVdV8avoBNuQ1D/0L9kvj0nqYrpJ3c2dxxBooNCf3G1QA/2Q97DqeUqpI490yN428?= =?utf-8?q?UVwmSi/jXMN0XmLOGGXizuAozVNJ6Io97cmnUj9ClFU/jrnCvgZwzYFgNuHy+br39?= =?utf-8?q?xro7UH1wiz6TvMtRKPJtnbGSn8clIGu4KR4YpruVMRolSRn5zABhU+XwCVUfUo1mz?= =?utf-8?q?APEoQT9kApuEB/PX37LnEWpvYrc27v67aaqELY+LvLAMMmBF5nTHzm+Sjctqbk1/S?= =?utf-8?q?SKkSUqTzOw5oWnziWEIWTuR5URLM33sgO1nwzidJBeohnjIzQ91Og4i2udVTXcPvp?= =?utf-8?q?JfT/iw6uP023x5GNKVpZfjIGV7G4g9j5aI2XVlUc2rb8y3FZywgwa+M2gV7U9+fuy?= =?utf-8?q?zYiXChX13SGFq7r2voHVeLgHYdBZc0m58YJ5PR4/uwBy9jjGVWNKI1TGAD4TisYda?= =?utf-8?q?1GEB6ZjKnwxNWGsi7oHNErPt/RRVa+PV5jJrZ2YOm/hh/NirHCCh47CvuXWWG1AvC?= =?utf-8?q?RQxpMBYq98Z5amC4SvkDQcIJ24okUzZYGOy0Gx+SV2CCe2TjNAqXYDyvS90ScrJMc?= =?utf-8?q?N4xRMS/DE/OvbYUebIOr3B25HBBR2J05p6OgAZpjsIBzyWATp1VdEfaD6cilezBFi?= =?utf-8?q?R9aeVpIoTcP9sBR0RiJjiUhiteWGNlpt03W/fKt50XycYp5yTJXzYVTxDMQygqq7y?= =?utf-8?q?nAClwurxuICbo3+P3rdSdwX6G7KrcGIOAHVY/ifTMLR6M1UFZMuslBu+8+9L5l+9w?= =?utf-8?q?+RUhKaphNLH9ZV39HdMWbHQK1wUq56PfNkljNStan8rZHDzAuAEmr1yo8ZZbuYgga?= =?utf-8?q?QdPgJPMNUJdqVALouB7TTPHTSgU3898tGfCBzEiL+GmOs1R0Qm7/oX5z1MA12FhIx?= =?utf-8?q?oFPBFxF9dHZ7TktcjdyfZf3dgLLx4UAVLyEFseM5y/YR3PsrlaCLsHyKDJipX26H4?= =?utf-8?q?w3ogzuK66Xy8KTWGk7dV7vgL9++PIZYLOjOM3T01LFsS50631RZZThnoIp32Zzs/9?= =?utf-8?q?DVKz/SF9s2gijU/KEtKFVhk25SWyZnWBjCkLAkvzLSVU0U7dfuTahQFt9R11CpcnC?= =?utf-8?q?mTXxP+83QN49n7pZ?= X-OriginatorOrg: silabs.com X-MS-Exchange-CrossTenant-Network-Message-Id: df666f70-8d20-4766-69ab-08d98807b43f X-MS-Exchange-CrossTenant-AuthSource: PH0PR11MB5657.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Oct 2021 13:54:52.2397 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 54dbd822-5231-4b20-944d-6f4abcd541fb X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: FIxgqWIn0LBEV3hckzpf6Oa/m+nvNzp9BpJbbwgQ700nICxrefTgRRVpPmxC1VpFmh/qRmIN1T9npZC7Wui5+w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR11MB5644 Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org From: Jérôme Pouiller Signed-off-by: Jérôme Pouiller --- drivers/net/wireless/silabs/wfx/bh.c | 329 +++++++++++++++++++++++++++ drivers/net/wireless/silabs/wfx/bh.h | 34 +++ 2 files changed, 363 insertions(+) create mode 100644 drivers/net/wireless/silabs/wfx/bh.c create mode 100644 drivers/net/wireless/silabs/wfx/bh.h diff --git a/drivers/net/wireless/silabs/wfx/bh.c b/drivers/net/wireless/silabs/wfx/bh.c new file mode 100644 index 000000000000..c8c1428e945b --- /dev/null +++ b/drivers/net/wireless/silabs/wfx/bh.c @@ -0,0 +1,329 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Interrupt bottom half (BH). + * + * Copyright (c) 2017-2020, Silicon Laboratories, Inc. + * Copyright (c) 2010, ST-Ericsson + */ +#include +#include + +#include "bh.h" +#include "wfx.h" +#include "hwio.h" +#include "traces.h" +#include "hif_rx.h" +#include "hif_api_cmd.h" + +static void device_wakeup(struct wfx_dev *wdev) +{ + int max_retry = 3; + + if (!wdev->pdata.gpio_wakeup) + return; + if (gpiod_get_value_cansleep(wdev->pdata.gpio_wakeup) > 0) + return; + + if (wfx_api_older_than(wdev, 1, 4)) { + gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1); + if (!completion_done(&wdev->hif.ctrl_ready)) + usleep_range(2000, 2500); + return; + } + for (;;) { + gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 1); + /* completion.h does not provide any function to wait + * completion without consume it (a kind of + * wait_for_completion_done_timeout()). So we have to emulate + * it. + */ + if (wait_for_completion_timeout(&wdev->hif.ctrl_ready, + msecs_to_jiffies(2))) { + complete(&wdev->hif.ctrl_ready); + return; + } else if (max_retry-- > 0) { + /* Older firmwares have a race in sleep/wake-up process. + * Redo the process is sufficient to unfreeze the + * chip. + */ + dev_err(wdev->dev, "timeout while wake up chip\n"); + gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 0); + usleep_range(2000, 2500); + } else { + dev_err(wdev->dev, "max wake-up retries reached\n"); + return; + } + } +} + +static void device_release(struct wfx_dev *wdev) +{ + if (!wdev->pdata.gpio_wakeup) + return; + + gpiod_set_value_cansleep(wdev->pdata.gpio_wakeup, 0); +} + +static int rx_helper(struct wfx_dev *wdev, size_t read_len, int *is_cnf) +{ + struct sk_buff *skb; + struct wfx_hif_msg *hif; + size_t alloc_len; + size_t computed_len; + int release_count; + int piggyback = 0; + + WARN(read_len > round_down(0xFFF, 2) * sizeof(u16), "request exceed the chip capability"); + + /* Add 2 to take into account piggyback size */ + alloc_len = wdev->hwbus_ops->align_size(wdev->hwbus_priv, read_len + 2); + skb = dev_alloc_skb(alloc_len); + if (!skb) + return -ENOMEM; + + if (wfx_data_read(wdev, skb->data, alloc_len)) + goto err; + + piggyback = le16_to_cpup((__le16 *)(skb->data + alloc_len - 2)); + _trace_piggyback(piggyback, false); + + hif = (struct wfx_hif_msg *)skb->data; + WARN(hif->encrypted & 0x3, "encryption is unsupported"); + if (WARN(read_len < sizeof(struct wfx_hif_msg), "corrupted read")) + goto err; + computed_len = le16_to_cpu(hif->len); + computed_len = round_up(computed_len, 2); + if (computed_len != read_len) { + dev_err(wdev->dev, "inconsistent message length: %zu != %zu\n", + computed_len, read_len); + print_hex_dump(KERN_INFO, "hif: ", DUMP_PREFIX_OFFSET, 16, 1, + hif, read_len, true); + goto err; + } + + if (!(hif->id & HIF_ID_IS_INDICATION)) { + (*is_cnf)++; + if (hif->id == HIF_CNF_ID_MULTI_TRANSMIT) + release_count = ((struct wfx_hif_cnf_multi_transmit *)hif->body)->num_tx_confs; + else + release_count = 1; + WARN(wdev->hif.tx_buffers_used < release_count, "corrupted buffer counter"); + wdev->hif.tx_buffers_used -= release_count; + } + _trace_hif_recv(hif, wdev->hif.tx_buffers_used); + + if (hif->id != HIF_IND_ID_EXCEPTION && hif->id != HIF_IND_ID_ERROR) { + if (hif->seqnum != wdev->hif.rx_seqnum) + dev_warn(wdev->dev, "wrong message sequence: %d != %d\n", + hif->seqnum, wdev->hif.rx_seqnum); + wdev->hif.rx_seqnum = (hif->seqnum + 1) % (HIF_COUNTER_MAX + 1); + } + + skb_put(skb, le16_to_cpu(hif->len)); + /* wfx_handle_rx takes care on SKB livetime */ + wfx_handle_rx(wdev, skb); + if (!wdev->hif.tx_buffers_used) + wake_up(&wdev->hif.tx_buffers_empty); + + return piggyback; + +err: + if (skb) + dev_kfree_skb(skb); + return -EIO; +} + +static int bh_work_rx(struct wfx_dev *wdev, int max_msg, int *num_cnf) +{ + size_t len; + int i; + int ctrl_reg, piggyback; + + piggyback = 0; + for (i = 0; i < max_msg; i++) { + if (piggyback & CTRL_NEXT_LEN_MASK) + ctrl_reg = piggyback; + else if (try_wait_for_completion(&wdev->hif.ctrl_ready)) + ctrl_reg = atomic_xchg(&wdev->hif.ctrl_reg, 0); + else + ctrl_reg = 0; + if (!(ctrl_reg & CTRL_NEXT_LEN_MASK)) + return i; + /* ctrl_reg units are 16bits words */ + len = (ctrl_reg & CTRL_NEXT_LEN_MASK) * 2; + piggyback = rx_helper(wdev, len, num_cnf); + if (piggyback < 0) + return i; + if (!(piggyback & CTRL_WLAN_READY)) + dev_err(wdev->dev, "unexpected piggyback value: ready bit not set: %04x\n", + piggyback); + } + if (piggyback & CTRL_NEXT_LEN_MASK) { + ctrl_reg = atomic_xchg(&wdev->hif.ctrl_reg, piggyback); + complete(&wdev->hif.ctrl_ready); + if (ctrl_reg) + dev_err(wdev->dev, "unexpected IRQ happened: %04x/%04x\n", + ctrl_reg, piggyback); + } + return i; +} + +static void tx_helper(struct wfx_dev *wdev, struct wfx_hif_msg *hif) +{ + int ret; + void *data; + bool is_encrypted = false; + size_t len = le16_to_cpu(hif->len); + + WARN(len < sizeof(*hif), "try to send corrupted data"); + + hif->seqnum = wdev->hif.tx_seqnum; + wdev->hif.tx_seqnum = (wdev->hif.tx_seqnum + 1) % (HIF_COUNTER_MAX + 1); + + data = hif; + WARN(len > le16_to_cpu(wdev->hw_caps.size_inp_ch_buf), + "request exceed the chip capability: %zu > %d\n", + len, le16_to_cpu(wdev->hw_caps.size_inp_ch_buf)); + len = wdev->hwbus_ops->align_size(wdev->hwbus_priv, len); + ret = wfx_data_write(wdev, data, len); + if (ret) + goto end; + + wdev->hif.tx_buffers_used++; + _trace_hif_send(hif, wdev->hif.tx_buffers_used); +end: + if (is_encrypted) + kfree(data); +} + +static int bh_work_tx(struct wfx_dev *wdev, int max_msg) +{ + struct wfx_hif_msg *hif; + int i; + + for (i = 0; i < max_msg; i++) { + hif = NULL; + if (wdev->hif.tx_buffers_used < le16_to_cpu(wdev->hw_caps.num_inp_ch_bufs)) { + if (try_wait_for_completion(&wdev->hif_cmd.ready)) { + WARN(!mutex_is_locked(&wdev->hif_cmd.lock), "data locking error"); + hif = wdev->hif_cmd.buf_send; + } else { + hif = wfx_tx_queues_get(wdev); + } + } + if (!hif) + return i; + tx_helper(wdev, hif); + } + return i; +} + +/* In SDIO mode, it is necessary to make an access to a register to acknowledge + * last received message. It could be possible to restrict this acknowledge to + * SDIO mode and only if last operation was rx. + */ +static void ack_sdio_data(struct wfx_dev *wdev) +{ + u32 cfg_reg; + + wfx_config_reg_read(wdev, &cfg_reg); + if (cfg_reg & 0xFF) { + dev_warn(wdev->dev, "chip reports errors: %02x\n", + cfg_reg & 0xFF); + wfx_config_reg_write_bits(wdev, 0xFF, 0x00); + } +} + +static void bh_work(struct work_struct *work) +{ + struct wfx_dev *wdev = container_of(work, struct wfx_dev, hif.bh); + int stats_req = 0, stats_cnf = 0, stats_ind = 0; + bool release_chip = false, last_op_is_rx = false; + int num_tx, num_rx; + + device_wakeup(wdev); + do { + num_tx = bh_work_tx(wdev, 32); + stats_req += num_tx; + if (num_tx) + last_op_is_rx = false; + num_rx = bh_work_rx(wdev, 32, &stats_cnf); + stats_ind += num_rx; + if (num_rx) + last_op_is_rx = true; + } while (num_rx || num_tx); + stats_ind -= stats_cnf; + + if (last_op_is_rx) + ack_sdio_data(wdev); + if (!wdev->hif.tx_buffers_used && !work_pending(work)) { + device_release(wdev); + release_chip = true; + } + _trace_bh_stats(stats_ind, stats_req, stats_cnf, + wdev->hif.tx_buffers_used, release_chip); +} + +/* An IRQ from chip did occur */ +void wfx_bh_request_rx(struct wfx_dev *wdev) +{ + u32 cur, prev; + + wfx_control_reg_read(wdev, &cur); + prev = atomic_xchg(&wdev->hif.ctrl_reg, cur); + complete(&wdev->hif.ctrl_ready); + queue_work(system_highpri_wq, &wdev->hif.bh); + + if (!(cur & CTRL_NEXT_LEN_MASK)) + dev_err(wdev->dev, "unexpected control register value: length field is 0: %04x\n", + cur); + if (prev != 0) + dev_err(wdev->dev, "received IRQ but previous data was not (yet) read: %04x/%04x\n", + prev, cur); +} + +/* Driver want to send data */ +void wfx_bh_request_tx(struct wfx_dev *wdev) +{ + queue_work(system_highpri_wq, &wdev->hif.bh); +} + +/* If IRQ is not available, this function allow to manually poll the control + * register and simulate an IRQ ahen an event happened. + * + * Note that the device has a bug: If an IRQ raise while host read control + * register, the IRQ is lost. So, use this function carefully (only duing + * device initialisation). + */ +void wfx_bh_poll_irq(struct wfx_dev *wdev) +{ + ktime_t now, start; + u32 reg; + + WARN(!wdev->poll_irq, "unexpected IRQ polling can mask IRQ"); + start = ktime_get(); + for (;;) { + wfx_control_reg_read(wdev, ®); + now = ktime_get(); + if (reg & 0xFFF) + break; + if (ktime_after(now, ktime_add_ms(start, 1000))) { + dev_err(wdev->dev, "time out while polling control register\n"); + return; + } + udelay(200); + } + wfx_bh_request_rx(wdev); +} + +void wfx_bh_register(struct wfx_dev *wdev) +{ + INIT_WORK(&wdev->hif.bh, bh_work); + init_completion(&wdev->hif.ctrl_ready); + init_waitqueue_head(&wdev->hif.tx_buffers_empty); +} + +void wfx_bh_unregister(struct wfx_dev *wdev) +{ + flush_work(&wdev->hif.bh); +} diff --git a/drivers/net/wireless/silabs/wfx/bh.h b/drivers/net/wireless/silabs/wfx/bh.h new file mode 100644 index 000000000000..a44c8b421b7c --- /dev/null +++ b/drivers/net/wireless/silabs/wfx/bh.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Interrupt bottom half (BH). + * + * Copyright (c) 2017-2020, Silicon Laboratories, Inc. + * Copyright (c) 2010, ST-Ericsson + */ +#ifndef WFX_BH_H +#define WFX_BH_H + +#include +#include +#include +#include + +struct wfx_dev; + +struct wfx_hif { + struct work_struct bh; + struct completion ctrl_ready; + wait_queue_head_t tx_buffers_empty; + atomic_t ctrl_reg; + int rx_seqnum; + int tx_seqnum; + int tx_buffers_used; +}; + +void wfx_bh_register(struct wfx_dev *wdev); +void wfx_bh_unregister(struct wfx_dev *wdev); +void wfx_bh_request_rx(struct wfx_dev *wdev); +void wfx_bh_request_tx(struct wfx_dev *wdev); +void wfx_bh_poll_irq(struct wfx_dev *wdev); + +#endif