Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp58639pxj; Tue, 1 Jun 2021 15:06:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz4/GxRaJVDxROb+N/5PmOTXziSGAy4PR8xsuDVrj3QmunBFcGSP1ejjn7lZUjcTBBhpht6 X-Received: by 2002:a05:6402:510b:: with SMTP id m11mr34608777edd.26.1622585209308; Tue, 01 Jun 2021 15:06:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622585209; cv=none; d=google.com; s=arc-20160816; b=SAlmjsU2fAbv4Lk8QOC5UJWbh9dFbkB/F7fX6E2t8Yjt7d0gbP9m8kPDdrZQ+XSypK 6w5zE1lFv+5nWhVPsCDaVZAot/59UpdNpm1t/G6cxCCu137M65WrOzkilQ+6+LyCQTfA ERb5kvuWww5fqZLbJSwag2gH20kw89h0mjra31nEkxM/zerYU6vADRsXAf8iKFaPEb8m wLaeGQtBpiVZouUWWb8DFBWTm3h8CNOQUNgPao+v0ZUoBm/7RTs4zyxH1cI2w5X8017R fRlqZKLBThGEQrf2YBOCxfCSHiKASF1NR1DraHhhIQuvBUzohXEBnU8vqc97Gd3/ung9 AVVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:to:from:dkim-signature; bh=JivnPwUdgtiMEUtJMjXPlP8jDXsxA2fny+NDGNihgHw=; b=dSuGvmlI302DWOPorFTK0S70ZCxBkdZDoSYbhZEwO0xj6VVO5NC9KxrYh7cJOks46K nh5CHLEUWuWuU/GlN/n1yLfXW2N1RPDaKbOueO6R+BSgyVnV/4WWsc3aBS2Ewck7U+b6 /YY+C8nZCeP1Mt3ljeKYYEpFUmEG66ltAx7mPcHqVH7xhXRdwQowHVGn9nHShq1+80CN CHi+NU6a6p/w2p6NYYLuEhaKraeRAF1O0XIz1PxJqltoAKf+2K5jbol2OKINgkaWD74O V23ryj6NM13aqKdwLVdSc9Bx8FqJf9LGbmWa/J7xr9GCbCOv4rTKkqqIABErUPgKKVrj ObHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="hUwFAK/Q"; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i14si20789163eje.207.2021.06.01.15.06.23; Tue, 01 Jun 2021 15:06:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="hUwFAK/Q"; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234740AbhFAWFg (ORCPT + 99 others); Tue, 1 Jun 2021 18:05:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234671AbhFAWFf (ORCPT ); Tue, 1 Jun 2021 18:05:35 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6706C061574 for ; Tue, 1 Jun 2021 15:03:53 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id d16so558431pfn.12 for ; Tue, 01 Jun 2021 15:03:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=JivnPwUdgtiMEUtJMjXPlP8jDXsxA2fny+NDGNihgHw=; b=hUwFAK/QwXtmuhJx/iTks1Zedhs9Uapvsm3j+AORTn2jL7SNx6F9hfzxHTqiiRBMnC /enAI0qssx2t+y/MPqfAQznSPeBgieCMwUlouAWUKZt2F3B+fJFi1qH4n4tfafkagDSK vaaLHAA1+NtslKVfdM+EVRKGtidCViiePolyzioP3VpsGfkH7QuXbYc5/D9NknYSf62K Tg/Aj0yPPFjS9VL7rTFtzaZOUBaMEPg6PL3l2k/Ad0qiDHXQQ1yvs90DLMuFMybnlYmb uXXb+REGfEj+6Y4OvsSfa102gkfZmORNNMonHHrKXmi3s18rbXZXFAbW/NQ5Pz7sqxNm 4maA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=JivnPwUdgtiMEUtJMjXPlP8jDXsxA2fny+NDGNihgHw=; b=uaCuItPTgWbENIOAFVAXTuC94C0wiwhm2e5mHO6j6ASelbZjLTkTQ9ZY9d6GZK5bWh v/Gu6ftHsgyR0GYx3lVIL4a1rBFHltcl/dXe/KopAWOYjYyqizP+TSSltNrTgqnydDiH bpqBhLEoCSI2LPJiM/DNugDbl+SXjX/66Me53TX07THkzTWGcoYPSnPaFzH7V6B0bgGs TrQ/2LlKFbiu2tU+AE4v8VueAkksRCxLiOpAxdLnwvQYY32SjUdH7H8adGaUNlo2m36w QcGuwGVKz8pgHc/KPLhXJAvw7M9yUxg2t6XoLqo/aLvrio/SY5oltPnKAdszpi6IDZEA 9sQg== X-Gm-Message-State: AOAM533bq7NPiQW83dOkwfiOYZ4yTm5MZvPx0+7B7O6GfazM0i7BUZuk Rvjod3tUzmrfiRLU1ltzWFUhUTFCrUo= X-Received: by 2002:a63:b64:: with SMTP id a36mr30334350pgl.437.1622585032923; Tue, 01 Jun 2021 15:03:52 -0700 (PDT) Received: from localhost.localdomain (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id f9sm13573780pfc.42.2021.06.01.15.03.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Jun 2021 15:03:52 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH v1 1/5] Bluetooth: Add helper for serialized HCI command execution Date: Tue, 1 Jun 2021 15:03:45 -0700 Message-Id: <20210601220349.375619-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Marcel Holtmann The usage of __hci_cmd_sync() within the hdev->setup() callback allows for a nice and simple serialized execution of HCI commands. More importantly it allows for result processing before issueing the next command. With the current usage of hci_req_run() it is possible to batch up commands and execute them, but it is impossible to react to their results or errors. This is an attempt to generalize the hdev->setup() handling and provide a simple way of running multiple HCI commands from a single function context. There are multiple struct work that are decdicated to certain tasks already used right now. It is add a lot of bloat to hci_dev struct and extra handling code. So it might be possible to put all of these behind a common HCI command infrastructure and just execute the HCI commands from the same work context in a serialized fashion. For example updating the white list and resolving list can be done now without having to know the list size ahead of time. Also preparing for suspend or resume shouldn't require a state machine anymore. There are other tasks that should be simplified as well. Signed-off-by: Marcel Holtmann Signed-off-by: Luiz Augusto von Dentz --- include/net/bluetooth/hci_core.h | 17 +++++++ net/bluetooth/hci_core.c | 82 ++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index 43b08bebae74..de95c47aaf77 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -302,6 +302,17 @@ struct amp_assoc { #define HCI_MAX_PAGES 3 +typedef int (*cmd_sync_work_func_t)(struct hci_dev *hdev, void *data); +typedef void (*cmd_sync_work_destroy_t)(struct hci_dev *hdev, void *data, + int err); + +struct cmd_sync_work_entry { + struct list_head list; + cmd_sync_work_func_t func; + void *data; + cmd_sync_work_destroy_t destroy; +}; + struct hci_dev { struct list_head list; struct mutex lock; @@ -463,6 +474,9 @@ struct hci_dev { struct work_struct power_on; struct delayed_work power_off; struct work_struct error_reset; + struct work_struct cmd_sync_work; + struct list_head cmd_sync_work_list; + struct mutex cmd_sync_work_lock; __u16 discov_timeout; struct delayed_work discov_off; @@ -1701,6 +1715,9 @@ void *hci_sent_cmd_data(struct hci_dev *hdev, __u16 opcode); struct sk_buff *hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen, const void *param, u32 timeout); +int hci_cmd_sync_queue(struct hci_dev *hdev, cmd_sync_work_func_t func, + void *data, cmd_sync_work_destroy_t destroy); + u32 hci_conn_get_phy(struct hci_conn *conn); /* ----- HCI Sockets ----- */ diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index 6eedf334f943..ba407976066b 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -2329,6 +2329,81 @@ static void hci_error_reset(struct work_struct *work) hci_dev_do_open(hdev); } +static void hci_cmd_sync_work(struct work_struct *work) +{ + struct hci_dev *hdev = container_of(work, struct hci_dev, cmd_sync_work); + struct cmd_sync_work_entry *entry; + cmd_sync_work_func_t func; + cmd_sync_work_destroy_t destroy; + void *data; + + bt_dev_dbg(hdev, ""); + + mutex_lock(&hdev->cmd_sync_work_lock); + entry = list_first_entry(&hdev->cmd_sync_work_list, + struct cmd_sync_work_entry, list); + if (entry) { + list_del(&entry->list); + func = entry->func; + data = entry->data; + destroy = entry->destroy; + kfree(entry); + } else { + func = NULL; + data = NULL; + destroy = NULL; + } + mutex_unlock(&hdev->cmd_sync_work_lock); + + if (func) { + int err; + + hci_req_sync_lock(hdev); + + err = func(hdev, data); + + if (destroy) + destroy(hdev, data, err); + + hci_req_sync_unlock(hdev); + } +} + +int hci_cmd_sync_queue(struct hci_dev *hdev, cmd_sync_work_func_t func, + void *data, cmd_sync_work_destroy_t destroy) +{ + struct cmd_sync_work_entry *entry; + + entry = kmalloc(sizeof(*entry), GFP_KERNEL); + if (!entry) + return -ENOMEM; + + entry->func = func; + entry->data = data; + entry->destroy = destroy; + + mutex_lock(&hdev->cmd_sync_work_lock); + list_add_tail(&entry->list, &hdev->cmd_sync_work_list); + mutex_unlock(&hdev->cmd_sync_work_lock); + + queue_work(hdev->req_workqueue, &hdev->cmd_sync_work); + + return 0; +} + +static void hci_cmd_sync_clear(struct hci_dev *hdev) +{ + struct cmd_sync_work_entry *entry, *tmp; + + list_for_each_entry_safe(entry, tmp, &hdev->cmd_sync_work_list, list) { + if (entry->destroy) + entry->destroy(hdev, entry->data, -ECANCELED); + + list_del(&entry->list); + kfree(entry); + } +} + void hci_uuids_clear(struct hci_dev *hdev) { struct bt_uuid *uuid, *tmp; @@ -3845,6 +3920,10 @@ struct hci_dev *hci_alloc_dev(void) INIT_WORK(&hdev->error_reset, hci_error_reset); INIT_WORK(&hdev->suspend_prepare, hci_prepare_suspend); + INIT_WORK(&hdev->cmd_sync_work, hci_cmd_sync_work); + INIT_LIST_HEAD(&hdev->cmd_sync_work_list); + mutex_init(&hdev->cmd_sync_work_lock); + INIT_DELAYED_WORK(&hdev->power_off, hci_power_off); skb_queue_head_init(&hdev->rx_q); @@ -4005,6 +4084,9 @@ void hci_unregister_dev(struct hci_dev *hdev) cancel_work_sync(&hdev->power_on); + cancel_work_sync(&hdev->cmd_sync_work); + hci_cmd_sync_clear(hdev); + if (!test_bit(HCI_QUIRK_NO_SUSPEND_NOTIFIER, &hdev->quirks)) { hci_suspend_clear_tasks(hdev); unregister_pm_notifier(&hdev->suspend_notifier); -- 2.31.1