Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp691049ybh; Wed, 11 Mar 2020 08:54:46 -0700 (PDT) X-Google-Smtp-Source: ADFU+vt4KEoYM4JwkCf91zMJucs8cPbbh1uef6JFaxBzH+FSBIryPRpEtWtJk2BRoynigChzXdqw X-Received: by 2002:a05:6808:14d:: with SMTP id h13mr2450997oie.121.1583942085927; Wed, 11 Mar 2020 08:54:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1583942085; cv=none; d=google.com; s=arc-20160816; b=mzM2i3rAhj3O5jvN7OqjLbk+H0ERbN2Q0WtSLBEJI5RWoPzJCLBWrK0dLUIGra3l0r 5NB7enajj++LnZ4YhP/vetNZVozEPnI6rGH3hXL/l9Ru6dfdwm8ei7mi8Q3/1fLqMEUW Ta+UzKbw/YMRZk4qpQQxNOXmvEFHE+HXIlxzcPh2UXVTgtqcTIMMi+OWmXD7ZkYsiC77 PQ/6waKij70cG8ZSgQHE9JVZ8ePqQ/6dTvhxV7PgZ2CLzUddAb59zEqiB5t+mtCaLm22 FC8wF8MGY73PUjpOHpSrs7SxdlELg7nsD6l3j27vfWKcYrna/tssx6ObDz5eq39m0l5+ eE2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Qt2kGw9KM2r51pvBbwSiQbbpNrNb8bMaNsr1uAomvLc=; b=ABa+GKYhag1b0jaex+SN3/qAtFQ88snYUH3p9VrJLifFH9hhWNaiCof6osbnp9DvUx rQPpewhh768NjIts5KHTtQcnm6zE8ICSFOX+I/3GZx7EWK9XS1jZx69IyWVUo/y4Mntu UQlDSMsbrGrvgHIWjvU3X0OTk5PFHMd2yFzhqRtJqUhmTWdR1+8G82A18KpI1AqNzJAJ rRKRTHjzAP3j1/eZp1WFXvzzOBjnLLQob762C5PCSPnH/8IP9iJX4cmZWmEwQPPw3urp gp+eYWZ1O3W9McqzsdPVZgbpRZtCDSmiW3F4XCS+Zm18sC+anl9BdPppZTPV9PwBtG27 VlhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=NIy2peu2; 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 g9si366873otp.89.2020.03.11.08.54.34; Wed, 11 Mar 2020 08:54:45 -0700 (PDT) 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=NIy2peu2; 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 S1730099AbgCKPyP (ORCPT + 99 others); Wed, 11 Mar 2020 11:54:15 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:46674 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730039AbgCKPyN (ORCPT ); Wed, 11 Mar 2020 11:54:13 -0400 Received: by mail-pg1-f195.google.com with SMTP id y30so1418080pga.13 for ; Wed, 11 Mar 2020 08:54:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Qt2kGw9KM2r51pvBbwSiQbbpNrNb8bMaNsr1uAomvLc=; b=NIy2peu29hZ4teyoTPglCwNpA9B20r3PP5rH9fUy6GR/S+5PfOr1JMUfsjdw3kga2Y MTJ0q5WdNFSTxpviLDgpU+tkRxeoJtKkYL8ay02sZKEsPFtPnrSiGLlco6aL1Cx01WJU db8o+bYTl/pMaiMEmXLWL74gk+ep5N118f5+A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Qt2kGw9KM2r51pvBbwSiQbbpNrNb8bMaNsr1uAomvLc=; b=s7nuGe3sf3dBZQlY/1I7NeSoPStuhuhf2Ah7YuGVvxMY0OTbeehjS+F/qdmXysHgWm DIR5xHjx8iXsFymdlX3YHbY+PiIhHfMvK1+zPB60vC/SVBKuGFlwxNuYrNINyhfdCkjk nWyYUGil/qTatbDlU/iNtb1brGsRX+3ag//+h0/rwQuNEXAfUpTsV3wJd51qXv7U5TMu zXvO9xfNUdg8Y/IbWpWdLSRfPKcTHNY6hWLB+qukVvX7tcY7pd8u7CNo23pQTVXiYvNo 6saPUaq3X1NjOp4y1wOCNeaSFVfsWBIzgM+ywzUFFKGMc2szzNJEiJ0gKLBXN+vfqibx Xv0g== X-Gm-Message-State: ANhLgQ0z0+rfa+vGMZlMhZQSU8Eo/5nHFHhUKYB2GeTiIjuWqJVCAUfK hjnfJnLMuEBBlcWr7hsDBGnvwQ== X-Received: by 2002:a63:6d8e:: with SMTP id i136mr3606127pgc.325.1583942051839; Wed, 11 Mar 2020 08:54:11 -0700 (PDT) Received: from apsdesk.mtv.corp.google.com ([2620:15c:202:1:e09a:8d06:a338:aafb]) by smtp.gmail.com with ESMTPSA id a71sm13756265pfa.162.2020.03.11.08.54.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Mar 2020 08:54:11 -0700 (PDT) From: Abhishek Pandit-Subedi To: marcel@holtmann.org, luiz.dentz@gmail.com, alainm@chromium.org Cc: linux-bluetooth@vger.kernel.org, chromeos-bluetooth-upstreaming@chromium.org, Abhishek Pandit-Subedi , "David S. Miller" , Johan Hedberg , netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Jakub Kicinski Subject: [RFC PATCH v6 1/5] Bluetooth: Handle PM_SUSPEND_PREPARE and PM_POST_SUSPEND Date: Wed, 11 Mar 2020 08:54:00 -0700 Message-Id: <20200311085359.RFC.v6.1.I62f17edc39370044c75ad43a55a7382b4b8a5ceb@changeid> X-Mailer: git-send-email 2.25.1.481.gfbce0eb801-goog In-Reply-To: <20200311155404.209990-1-abhishekpandit@chromium.org> References: <20200311155404.209990-1-abhishekpandit@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Register for PM_SUSPEND_PREPARE and PM_POST_SUSPEND to make sure the Bluetooth controller is prepared correctly for suspend/resume. Implement the registration, scheduling and task handling portions only in this patch. Signed-off-by: Abhishek Pandit-Subedi --- Changes in v6: * Removed unused variables in hci_req_prepare_suspend Changes in v5: * Convert BT_DBG to bt_dev_dbg Changes in v4: * Added check for mgmt_powering_down and hdev_is_powered in notifier Changes in v3: None Changes in v2: * Moved pm notifier registration into its own patch and moved params out of separate suspend_state include/net/bluetooth/hci_core.h | 23 +++++++++ net/bluetooth/hci_core.c | 86 ++++++++++++++++++++++++++++++++ net/bluetooth/hci_request.c | 15 ++++++ net/bluetooth/hci_request.h | 2 + 4 files changed, 126 insertions(+) diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index c498ac113930..d6f694b436bf 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -88,6 +88,20 @@ struct discovery_state { unsigned long scan_duration; }; +#define SUSPEND_NOTIFIER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */ + +enum suspend_tasks { + SUSPEND_POWERING_DOWN, + + SUSPEND_PREPARE_NOTIFIER, + __SUSPEND_NUM_TASKS +}; + +enum suspended_state { + BT_RUNNING = 0, + BT_SUSPENDED, +}; + struct hci_conn_hash { struct list_head list; unsigned int acl_num; @@ -390,6 +404,15 @@ struct hci_dev { void *smp_bredr_data; struct discovery_state discovery; + + struct notifier_block suspend_notifier; + struct work_struct suspend_prepare; + enum suspended_state suspend_state_next; + enum suspended_state suspend_state; + + wait_queue_head_t suspend_wait_q; + DECLARE_BITMAP(suspend_tasks, __SUSPEND_NUM_TASKS); + struct hci_conn_hash conn_hash; struct list_head mgmt_pending; diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 196edc039b8e..39aa21a1fe92 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include @@ -1787,6 +1789,9 @@ int hci_dev_do_close(struct hci_dev *hdev) clear_bit(HCI_RUNNING, &hdev->flags); hci_sock_dev_event(hdev, HCI_DEV_CLOSE); + if (test_and_clear_bit(SUSPEND_POWERING_DOWN, hdev->suspend_tasks)) + wake_up(&hdev->suspend_wait_q); + /* After this point our queues are empty * and no tasks are scheduled. */ hdev->close(hdev); @@ -3264,6 +3269,78 @@ void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr, } } +static int hci_suspend_wait_event(struct hci_dev *hdev) +{ +#define WAKE_COND \ + (find_first_bit(hdev->suspend_tasks, __SUSPEND_NUM_TASKS) == \ + __SUSPEND_NUM_TASKS) + + int i; + int ret = wait_event_timeout(hdev->suspend_wait_q, + WAKE_COND, SUSPEND_NOTIFIER_TIMEOUT); + + if (ret == 0) { + bt_dev_dbg(hdev, "Timed out waiting for suspend"); + for (i = 0; i < __SUSPEND_NUM_TASKS; ++i) { + if (test_bit(i, hdev->suspend_tasks)) + bt_dev_dbg(hdev, "Bit %d is set", i); + clear_bit(i, hdev->suspend_tasks); + } + + ret = -ETIMEDOUT; + } else { + ret = 0; + } + + return ret; +} + +static void hci_prepare_suspend(struct work_struct *work) +{ + struct hci_dev *hdev = + container_of(work, struct hci_dev, suspend_prepare); + + hci_dev_lock(hdev); + hci_req_prepare_suspend(hdev, hdev->suspend_state_next); + hci_dev_unlock(hdev); +} + +static int hci_suspend_notifier(struct notifier_block *nb, unsigned long action, + void *data) +{ + struct hci_dev *hdev = + container_of(nb, struct hci_dev, suspend_notifier); + int ret = 0; + + /* If powering down, wait for completion. */ + if (mgmt_powering_down(hdev)) { + set_bit(SUSPEND_POWERING_DOWN, hdev->suspend_tasks); + ret = hci_suspend_wait_event(hdev); + if (ret) + goto done; + } + + /* Suspend notifier should only act on events when powered. */ + if (!hdev_is_powered(hdev)) + goto done; + + if (action == PM_SUSPEND_PREPARE) { + hdev->suspend_state_next = BT_SUSPENDED; + set_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks); + queue_work(hdev->req_workqueue, &hdev->suspend_prepare); + + ret = hci_suspend_wait_event(hdev); + } else if (action == PM_POST_SUSPEND) { + hdev->suspend_state_next = BT_RUNNING; + set_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks); + queue_work(hdev->req_workqueue, &hdev->suspend_prepare); + + ret = hci_suspend_wait_event(hdev); + } + +done: + return ret ? notifier_from_errno(-EBUSY) : NOTIFY_STOP; +} /* Alloc HCI device */ struct hci_dev *hci_alloc_dev(void) { @@ -3341,6 +3418,7 @@ struct hci_dev *hci_alloc_dev(void) INIT_WORK(&hdev->tx_work, hci_tx_work); INIT_WORK(&hdev->power_on, hci_power_on); INIT_WORK(&hdev->error_reset, hci_error_reset); + INIT_WORK(&hdev->suspend_prepare, hci_prepare_suspend); INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); @@ -3349,6 +3427,7 @@ struct hci_dev *hci_alloc_dev(void) skb_queue_head_init(&hdev->raw_q); init_waitqueue_head(&hdev->req_wait_q); + init_waitqueue_head(&hdev->suspend_wait_q); INIT_DELAYED_WORK(&hdev->cmd_timer, hci_cmd_timeout); @@ -3460,6 +3539,11 @@ int hci_register_dev(struct hci_dev *hdev) hci_sock_dev_event(hdev, HCI_DEV_REG); hci_dev_hold(hdev); + hdev->suspend_notifier.notifier_call = hci_suspend_notifier; + error = register_pm_notifier(&hdev->suspend_notifier); + if (error) + goto err_wqueue; + queue_work(hdev->req_workqueue, &hdev->power_on); return id; @@ -3493,6 +3577,8 @@ void hci_unregister_dev(struct hci_dev *hdev) hci_dev_do_close(hdev); + unregister_pm_notifier(&hdev->suspend_notifier); + if (!test_bit(HCI_INIT, &hdev->flags) && !hci_dev_test_flag(hdev, HCI_SETUP) && !hci_dev_test_flag(hdev, HCI_CONFIG)) { diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c index 53179ae856ae..2343166614f0 100644 --- a/net/bluetooth/hci_request.c +++ b/net/bluetooth/hci_request.c @@ -918,6 +918,21 @@ static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance) return adv_instance->scan_rsp_len; } +/* Call with hci_dev_lock */ +void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next) +{ + if (next == hdev->suspend_state) { + bt_dev_dbg(hdev, "Same state before and after: %d", next); + goto done; + } + + hdev->suspend_state = next; + +done: + clear_bit(SUSPEND_PREPARE_NOTIFIER, hdev->suspend_tasks); + wake_up(&hdev->suspend_wait_q); +} + static u8 get_cur_adv_instance_scan_rsp_len(struct hci_dev *hdev) { u8 instance = hdev->cur_adv_instance; diff --git a/net/bluetooth/hci_request.h b/net/bluetooth/hci_request.h index a7019fbeadd3..0e81614d235e 100644 --- a/net/bluetooth/hci_request.h +++ b/net/bluetooth/hci_request.h @@ -68,6 +68,8 @@ void __hci_req_update_eir(struct hci_request *req); void hci_req_add_le_scan_disable(struct hci_request *req); void hci_req_add_le_passive_scan(struct hci_request *req); +void hci_req_prepare_suspend(struct hci_dev *hdev, enum suspended_state next); + void hci_req_reenable_advertising(struct hci_dev *hdev); void __hci_req_enable_advertising(struct hci_request *req); void __hci_req_disable_advertising(struct hci_request *req); -- 2.25.1.481.gfbce0eb801-goog