From patchwork Mon Jan 8 18:39:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Jonas_Dre=C3=9Fler?= X-Patchwork-Id: 186102 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:37c1:b0:101:2151:f287 with SMTP id y1csp1206002dyq; Mon, 8 Jan 2024 10:41:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IGccFywx5vg3rEQt77jTHzfyV5oWilfmkqHO0/T+Al1zZK8zIa5db8dJQiK8CICKU/dl6iQ X-Received: by 2002:ac8:5c08:0:b0:429:9e31:6914 with SMTP id i8-20020ac85c08000000b004299e316914mr822092qti.125.1704739285503; Mon, 08 Jan 2024 10:41:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704739285; cv=none; d=google.com; s=arc-20160816; b=eKeXffWfrRaYRSbAPY56BP2RH5423GIr/gENnWpedNhZPmgXaJxv9Tou2f9aI3ydfO YafRslNaoNoxeh5NYmvj887/1Gp5AgABvJx3RVVoTFeF5Wt8zPyWQwtjq88/8JmvvAeH NwswM0L+Zqzdnbz64E+ZH6kR7it/jCvSNBpGUpO8wdmZ8dgY9RViWiFROX4r8WfTe7nX JS/VI66tdLoPOEYuxeubl9195NFBxdJSny2b8MpkxDmFK6EJrb6uMD5xF4heR098btjy jxxDJxIKYEy9poX3uniMcxs0Bb8rETRXthpQSAIGyaSmzwcKZoy8Q3mbNLvg/beSSAqV 0Tbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=xKUCurh1FRzD7jS707QRTK91XPLMSaOZa7P4GnlMEIg=; fh=3AqDzvZy8BuFcn015+g5AB3sGQnZc/edyFwniXfpO+U=; b=wHIE8Np0i5W9WbuLIiYnGDKT7ZEQVXtPIbeLciPwZ/Y8T63jtmRD7VVQoXxQMS1hLa W78ZQybgHB1Aqi9GOcJQx1LjXmmw0tgoksyrEWRTld8srbtyab2tTK3PamQpxYJcNx09 1IB3hdEllJxbNWpQb7k8e8hGa2BnqxoBUrLGW4nyjZKZyvHx/7CyD081Tan2ldghSSOc gi72yFto8AtzWNwOoUgfsPIyFrCfLF9y7Flv1GwKR/oz4I5lslAOqTUfe9T6uBpJ7s0n 9xKh+R6oafUtU9pngAAvZgZAk77r8bba2RJ4fq8MeQT7yrWwGFtZNTSfbzNMqD+xhhDZ eW1Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-19983-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-19983-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id br26-20020a05622a1e1a00b0042997a71191si296989qtb.613.2024.01.08.10.41.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 10:41:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-19983-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-19983-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-19983-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 4D76A1C22FAC for ; Mon, 8 Jan 2024 18:41:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 454B655E5D; Mon, 8 Jan 2024 18:40:01 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AABB555775; Mon, 8 Jan 2024 18:39:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=v0yd.nl Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=v0yd.nl Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4T82sw16chz9srJ; Mon, 8 Jan 2024 19:39:48 +0100 (CET) From: =?utf-8?q?Jonas_Dre=C3=9Fler?= To: Marcel Holtmann , Johan Hedberg , Luiz Augusto von Dentz Cc: =?utf-8?q?Jonas_Dre=C3=9Fler?= , linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v2 3/4] Bluetooth: hci_conn: Only do ACL connections sequentially Date: Mon, 8 Jan 2024 19:39:35 +0100 Message-ID: <20240108183938.468426-4-verdre@v0yd.nl> In-Reply-To: <20240108183938.468426-1-verdre@v0yd.nl> References: <20240108183938.468426-1-verdre@v0yd.nl> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1787548700836222320 X-GMAIL-MSGID: 1787548700836222320 Pretty much all bluetooth chipsets only support paging a single device at a time, and if they don't reject a secondary "Create Connection" request while another is still ongoing, they'll most likely serialize those requests in the firware. With commit 4c67bc74f016 ("[Bluetooth] Support concurrent connect requests") we started adding some serialization of our own in case the adapter returns "Command Disallowed" HCI error. This commit was using the BT_CONNECT2 state for the serialization, this state is also used for a few more things (most notably to indicate we're waiting for an inquiry to cancel) and therefore a bit unreliable. Also not all BT firwares would respond with "Command Disallowed" on too many connection requests, some will also respond with "Hardware Failure" (BCM4378), and others will error out later and send a "Connect Complete" event with error "Rejected Limited Resources" (Marvell 88W8897). We can clean things up a bit and also make the serialization more reliable by using our hci_sync machinery to always do "Create Connection" requests in a sequential manner. This is very similar to what we're already doing for establishing LE connections, and it works well there. --- include/net/bluetooth/hci.h | 1 + net/bluetooth/hci_conn.c | 37 ++++++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h index fef723afd..f2bbc0a14 100644 --- a/include/net/bluetooth/hci.h +++ b/include/net/bluetooth/hci.h @@ -427,6 +427,7 @@ enum { #define HCI_ACL_TX_TIMEOUT msecs_to_jiffies(45000) /* 45 seconds */ #define HCI_AUTO_OFF_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */ #define HCI_POWER_OFF_TIMEOUT msecs_to_jiffies(5000) /* 5 seconds */ +#define HCI_ACL_CONN_TIMEOUT msecs_to_jiffies(20000) /* 20 seconds */ #define HCI_LE_CONN_TIMEOUT msecs_to_jiffies(20000) /* 20 seconds */ #define HCI_LE_AUTOCONN_TIMEOUT msecs_to_jiffies(4000) /* 4 seconds */ diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c index 76222565e..541d55301 100644 --- a/net/bluetooth/hci_conn.c +++ b/net/bluetooth/hci_conn.c @@ -229,11 +229,12 @@ static void hci_connect_le_scan_remove(struct hci_conn *conn) schedule_work(&conn->le_scan_cleanup); } -static void hci_acl_create_connection(struct hci_conn *conn) +static int hci_acl_create_connection_sync(struct hci_dev *hdev, void *data) { - struct hci_dev *hdev = conn->hdev; + struct hci_conn *conn = data; struct inquiry_entry *ie; struct hci_cp_create_conn cp; + int err; BT_DBG("hcon %p", conn); @@ -246,12 +247,10 @@ static void hci_acl_create_connection(struct hci_conn *conn) * request for discovery again when this flag becomes false. */ if (test_bit(HCI_INQUIRY, &hdev->flags)) { - /* Put this connection to "pending" state so that it will be - * executed after the inquiry cancel command complete event. - */ - conn->state = BT_CONNECT2; - hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL); - return; + err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0, + NULL, HCI_CMD_TIMEOUT); + if (err) + bt_dev_warn(hdev, "Failed to cancel inquiry %d", err); } conn->state = BT_CONNECT; @@ -284,7 +283,15 @@ static void hci_acl_create_connection(struct hci_conn *conn) else cp.role_switch = 0x00; - hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); + err = __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN, + sizeof(cp), &cp, + HCI_EV_CONN_COMPLETE, + HCI_ACL_CONN_TIMEOUT, NULL); + + if (err == -ETIMEDOUT) + hci_abort_conn(conn, HCI_ERROR_LOCAL_HOST_TERM); + + return err; } int hci_disconnect(struct hci_conn *conn, __u8 reason) @@ -1622,10 +1629,18 @@ struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, acl->conn_reason = conn_reason; if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { + int err; + acl->sec_level = BT_SECURITY_LOW; acl->pending_sec_level = sec_level; acl->auth_type = auth_type; - hci_acl_create_connection(acl); + + err = hci_cmd_sync_queue(hdev, hci_acl_create_connection_sync, + acl, NULL); + if (err) { + hci_conn_del(acl); + return ERR_PTR(err); + } } return acl; @@ -2530,7 +2545,7 @@ void hci_conn_check_pending(struct hci_dev *hdev) conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); if (conn) - hci_acl_create_connection(conn); + hci_cmd_sync_queue(hdev, hci_acl_create_connection_sync, conn, NULL); hci_dev_unlock(hdev); }