Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1623022imu; Thu, 17 Jan 2019 00:10:00 -0800 (PST) X-Google-Smtp-Source: ALg8bN6VfQ6BEmHSB74801cb8mbqAJTo4qEfjwbQvs3RUQWSDUvxMHUEJ3t2M1ORq7phqNhtwvqV X-Received: by 2002:a63:9843:: with SMTP id l3mr12524543pgo.413.1547712600593; Thu, 17 Jan 2019 00:10:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547712600; cv=none; d=google.com; s=arc-20160816; b=a9zmwyPBQCFcd2IZENChEZYscdZyiFwcBK/lXtHyuImSAIVc8D/Xcqnxh/DggKSdKa cnz0SAg1BU5NFrRwk0nFSjhoiES8kI43GBLf6ZGZ8gENJpeoTbTE6ynNSWsA+6J01drl XWH5DOv8nhhNKfLPtbcugdwmUSIF+3kjcjno9/+sPhNxfyCaB+ywb7rvCccakYqF+98f lWD2/9bGttfAGZmh3SJf4KFNRnhgMLosRsiI/tDTb7XTS4MXGgTPzLCj9yNpr53HCtSC NxIzwii3ELq0i4GEdC88go1I8hWJRYRKQqoF1x3nBdid1KqcQeJxBc3GdI6hvLHY2hqz U1LQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=cNtoC5c+2YT8QEQeFenQ+Gr8RS5xW2AkUGFuDXVHCZY=; b=GraDJSajaCgD/Hp4Cguvp2WNxQswGjZZ/8tjYt5/h++k/qActrPMVmOwTjZtnTK8tL 8PBYwYqKserfao1P2Fvj+Sb03wkA5j1wrzM7NlQC+bDQK1/AKvhnp37R3xoDJNE6e9EY npACgYrEQMU4XTqA2NVMvYvdNixNRyLN1Da4T1z0iVdKrqVtuZE6a3q3B9BMDZccK8UU QaKY7fRGC+a2sQxlp5Gk4fQFbZNz6UXYrS0Fw74SoUiENmzMBgtT1K9pc4JEE720IEBK sFoqZE3j2JNPMX1n0vCTSXpAC2zMghJXa1p+zBw6UQponD6ulb43uTZQlgVVvi6buGu6 12uw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="m3/DMABm"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j14si1067714pgg.44.2019.01.17.00.09.44; Thu, 17 Jan 2019 00:10:00 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="m3/DMABm"; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388122AbfAPXIG (ORCPT + 99 others); Wed, 16 Jan 2019 18:08:06 -0500 Received: from mail-pg1-f195.google.com ([209.85.215.195]:44256 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387956AbfAPXIE (ORCPT ); Wed, 16 Jan 2019 18:08:04 -0500 Received: by mail-pg1-f195.google.com with SMTP id t13so3483403pgr.11 for ; Wed, 16 Jan 2019 15:08:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=cNtoC5c+2YT8QEQeFenQ+Gr8RS5xW2AkUGFuDXVHCZY=; b=m3/DMABmDqIGq6dA1cgQZV76a4RAkojPOq/bhMtJ7HKfIF//gvaQGe/isvAKiLeYns DdcCfE/MHSstsI9xpXsNH62OuCeFtPXIW/Cwi3SQfoQm+43LmS6/A3SVzEmgByn5XxkX 7E1E9KmvkNTHgAFLtyp5+4DyzNqqQrLxYI1AE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=cNtoC5c+2YT8QEQeFenQ+Gr8RS5xW2AkUGFuDXVHCZY=; b=cqHFMdj9/UGeYD2nisW+ibWEESncwP8qEDrqqOxRfdqWMxqCi7I65uMzbp7vzIrxug RUNKx919GwSnT8sXtPt+Wvfc5Pl6kQt5INto03XdI83mY9dzYtvjeihaYRBWZTWZw8Ft 7ARM4i3TR37YTW4X9eQTrz2T3aJe+F9TISQzCYIg9H6TOkHP3G3eb0Ub3ckN6tuXf2aH KiapPDUyxuA95r24ISeVp+Qs3mu3DM0/vacUj8mBMWfcxTtXIX7JClaqXj8sOKfEQbWO GJ4YbN/B4xoy9Ux256H7H1/AhFnwEspNuouR+y8rJT6lRnC7QbLBkErexGTW2AouhDCu Fttw== X-Gm-Message-State: AJcUukd0q85YFQ/PZAj6tBHxMzdY7KcTTyz4u6mfLa+asGAeaWwkov6z XlEebVkmzaMm/3VgUOavPyNW/w== X-Received: by 2002:a63:b543:: with SMTP id u3mr11106658pgo.420.1547680083440; Wed, 16 Jan 2019 15:08:03 -0800 (PST) Received: from localhost ([2620:15c:202:1:75a:3f6e:21d:9374]) by smtp.gmail.com with ESMTPSA id x7sm7899802pga.68.2019.01.16.15.08.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 16 Jan 2019 15:08:02 -0800 (PST) Date: Wed, 16 Jan 2019 15:08:01 -0800 From: Matthias Kaehlcke To: Balakrishna Godavarthi Cc: marcel@holtmann.org, johan.hedberg@gmail.com, linux-kernel@vger.kernel.org, linux-bluetooth@vger.kernel.org, hemantg@codeaurora.org, linux-arm-msm@vger.kernel.org Subject: Re: [PATCH v8 3/3] Bluetooth: hci_qca: Disable IBS state machine and flush Tx buffer Message-ID: <20190116230801.GT261387@google.com> References: <20190116114603.500-1-bgodavar@codeaurora.org> <20190116114603.500-4-bgodavar@codeaurora.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190116114603.500-4-bgodavar@codeaurora.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 16, 2019 at 05:16:03PM +0530, Balakrishna Godavarthi wrote: > During hci down we observed IBS sleep commands are queued in the Tx > buffer and hci_uart_write_work is sending data to the chip which is > not required as the chip is powered off. This patch will disable IBS > and flush the Tx buffer before we turn off the chip. > > Signed-off-by: Balakrishna Godavarthi > --- > drivers/bluetooth/hci_qca.c | 8 ++++++++ > 1 file changed, 8 insertions(+) > > diff --git a/drivers/bluetooth/hci_qca.c b/drivers/bluetooth/hci_qca.c > index 7e4afcf40da2..7330ba71ada4 100644 > --- a/drivers/bluetooth/hci_qca.c > +++ b/drivers/bluetooth/hci_qca.c > @@ -1275,6 +1275,14 @@ static const struct qca_vreg_data qca_soc_data = { > > static void qca_power_shutdown(struct hci_uart *hu) > { > + struct qca_data *qca = hu->priv; > + > + /* From this point we go into power off state. But serial port is > + * still open, stop queueing the IBS data and flush all the buffered > + * data in skb's. > + */ > + clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags); > + qca_flush(hu); > host_set_baudrate(hu, 2400); > qca_send_power_pulse(hu, QCA_WCN3990_POWEROFF_PULSE); > qca_power_setup(hu, false); Due to a race-condition there could be an IBS sleep command queued even after clearing the bit and flushing the queue. In qca_enqueue() we have this: static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb) { ... /* Don't go to sleep in middle of patch download or * Out-Of-Band(GPIOs control) sleep is selected. */ if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) { skb_queue_tail(&qca->txq, skb); return 0; } spin_lock_irqsave(&qca->hci_ibs_lock, flags); } With process X executing qca_power_shutdown() and process Y running qca_enqueue() this could happen: [X] test_bit(STATE_IN_BAND_SLEEP_ENABLED) => set [Y] clear_bit(STATE_IN_BAND_SLEEP_ENABLED) [Y] qca_flush(hu); [X] skb_queue_tail(&qca->txq, skb); The following should fix this race: --- a/drivers/bluetooth/hci_qca.c +++ b/drivers/bluetooth/hci_qca.c @@ -770,16 +770,17 @@ static int qca_enqueue(struct hci_uart *hu, struct sk_buff *skb) /* Prepend skb with frame type */ memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1); + spin_lock_irqsave(&qca->hci_ibs_lock, flags); + /* Don't go to sleep in middle of patch download or * Out-Of-Band(GPIOs control) sleep is selected. */ if (!test_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags)) { skb_queue_tail(&qca->txq, skb); + spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); return 0; } - spin_lock_irqsave(&qca->hci_ibs_lock, flags); - /* Act according to current state */ switch (qca->tx_ibs_state) { case HCI_IBS_TX_AWAKE: @@ -1275,13 +1276,17 @@ static const struct qca_vreg_data qca_soc_data = { static void qca_power_shutdown(struct hci_uart *hu) { struct qca_data *qca = hu->priv; + unsigned long flags; /* From this point we go into power off state. But serial port is * still open, stop queueing the IBS data and flush all the buffered * data in skb's. */ + spin_lock_irqsave(&qca->hci_ibs_lock, flags); clear_bit(STATE_IN_BAND_SLEEP_ENABLED, &qca->flags); qca_flush(hu); + spin_unlock_irqrestore(&qca->hci_ibs_lock, flags); + host_set_baudrate(hu, 2400); qca_send_power_pulse(hu, QCA_WCN3990_POWEROFF_PULSE); qca_power_setup(hu, false); Cheers Matthias