Skip to content

Commit 4534009

Browse files
jonas2515Vudentz
authored andcommitted
Bluetooth: hci_conn: Only do ACL connections sequentially
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 4c67bc7 ("[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. Note that this causes a test failure in mgmt-tester (test "Pair Device - Power off 1") because the hci_abort_conn_sync() changes the error we return on timeout of the "Create Connection". We'll fix this on the mgmt-tester side by adjusting the expected error for the test. Signed-off-by: Jonas Dreßler <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent eeda1bf commit 4534009

File tree

4 files changed

+83
-60
lines changed

4 files changed

+83
-60
lines changed

include/net/bluetooth/hci.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ enum {
437437
#define HCI_NCMD_TIMEOUT msecs_to_jiffies(4000) /* 4 seconds */
438438
#define HCI_ACL_TX_TIMEOUT msecs_to_jiffies(45000) /* 45 seconds */
439439
#define HCI_AUTO_OFF_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */
440+
#define HCI_ACL_CONN_TIMEOUT msecs_to_jiffies(20000) /* 20 seconds */
440441
#define HCI_LE_CONN_TIMEOUT msecs_to_jiffies(20000) /* 20 seconds */
441442
#define HCI_LE_AUTOCONN_TIMEOUT msecs_to_jiffies(4000) /* 4 seconds */
442443

include/net/bluetooth/hci_sync.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,6 @@ int hci_le_terminate_big_sync(struct hci_dev *hdev, u8 handle, u8 reason);
138138
int hci_le_big_terminate_sync(struct hci_dev *hdev, u8 handle);
139139

140140
int hci_le_pa_terminate_sync(struct hci_dev *hdev, u16 handle);
141+
142+
int hci_acl_create_connection_sync(struct hci_dev *hdev,
143+
struct hci_conn *conn);

net/bluetooth/hci_conn.c

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -178,64 +178,6 @@ static void hci_conn_cleanup(struct hci_conn *conn)
178178
hci_dev_put(hdev);
179179
}
180180

181-
static void hci_acl_create_connection(struct hci_conn *conn)
182-
{
183-
struct hci_dev *hdev = conn->hdev;
184-
struct inquiry_entry *ie;
185-
struct hci_cp_create_conn cp;
186-
187-
BT_DBG("hcon %p", conn);
188-
189-
/* Many controllers disallow HCI Create Connection while it is doing
190-
* HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
191-
* Connection. This may cause the MGMT discovering state to become false
192-
* without user space's request but it is okay since the MGMT Discovery
193-
* APIs do not promise that discovery should be done forever. Instead,
194-
* the user space monitors the status of MGMT discovering and it may
195-
* request for discovery again when this flag becomes false.
196-
*/
197-
if (test_bit(HCI_INQUIRY, &hdev->flags)) {
198-
/* Put this connection to "pending" state so that it will be
199-
* executed after the inquiry cancel command complete event.
200-
*/
201-
conn->state = BT_CONNECT2;
202-
hci_send_cmd(hdev, HCI_OP_INQUIRY_CANCEL, 0, NULL);
203-
return;
204-
}
205-
206-
conn->state = BT_CONNECT;
207-
conn->out = true;
208-
conn->role = HCI_ROLE_MASTER;
209-
210-
conn->attempt++;
211-
212-
conn->link_policy = hdev->link_policy;
213-
214-
memset(&cp, 0, sizeof(cp));
215-
bacpy(&cp.bdaddr, &conn->dst);
216-
cp.pscan_rep_mode = 0x02;
217-
218-
ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
219-
if (ie) {
220-
if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
221-
cp.pscan_rep_mode = ie->data.pscan_rep_mode;
222-
cp.pscan_mode = ie->data.pscan_mode;
223-
cp.clock_offset = ie->data.clock_offset |
224-
cpu_to_le16(0x8000);
225-
}
226-
227-
memcpy(conn->dev_class, ie->data.dev_class, 3);
228-
}
229-
230-
cp.pkt_type = cpu_to_le16(conn->pkt_type);
231-
if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
232-
cp.role_switch = 0x01;
233-
else
234-
cp.role_switch = 0x00;
235-
236-
hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
237-
}
238-
239181
int hci_disconnect(struct hci_conn *conn, __u8 reason)
240182
{
241183
BT_DBG("hcon %p", conn);
@@ -1696,10 +1638,17 @@ struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
16961638

16971639
acl->conn_reason = conn_reason;
16981640
if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
1641+
int err;
1642+
16991643
acl->sec_level = BT_SECURITY_LOW;
17001644
acl->pending_sec_level = sec_level;
17011645
acl->auth_type = auth_type;
1702-
hci_acl_create_connection(acl);
1646+
1647+
err = hci_acl_create_connection_sync(hdev, acl);
1648+
if (err) {
1649+
hci_conn_del(acl);
1650+
return ERR_PTR(err);
1651+
}
17031652
}
17041653

17051654
return acl;
@@ -2654,7 +2603,7 @@ void hci_conn_check_pending(struct hci_dev *hdev)
26542603

26552604
conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
26562605
if (conn)
2657-
hci_acl_create_connection(conn);
2606+
hci_acl_create_connection_sync(hdev, conn);
26582607

26592608
hci_dev_unlock(hdev);
26602609
}

net/bluetooth/hci_sync.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6492,3 +6492,73 @@ int hci_update_adv_data(struct hci_dev *hdev, u8 instance)
64926492
return hci_cmd_sync_queue(hdev, _update_adv_data_sync,
64936493
UINT_PTR(instance), NULL);
64946494
}
6495+
6496+
static int __hci_acl_create_connection_sync(struct hci_dev *hdev, void *data)
6497+
{
6498+
struct hci_conn *conn = data;
6499+
struct inquiry_entry *ie;
6500+
struct hci_cp_create_conn cp;
6501+
int err;
6502+
6503+
/* Many controllers disallow HCI Create Connection while it is doing
6504+
* HCI Inquiry. So we cancel the Inquiry first before issuing HCI Create
6505+
* Connection. This may cause the MGMT discovering state to become false
6506+
* without user space's request but it is okay since the MGMT Discovery
6507+
* APIs do not promise that discovery should be done forever. Instead,
6508+
* the user space monitors the status of MGMT discovering and it may
6509+
* request for discovery again when this flag becomes false.
6510+
*/
6511+
if (test_bit(HCI_INQUIRY, &hdev->flags)) {
6512+
err = __hci_cmd_sync_status(hdev, HCI_OP_INQUIRY_CANCEL, 0,
6513+
NULL, HCI_CMD_TIMEOUT);
6514+
if (err)
6515+
bt_dev_warn(hdev, "Failed to cancel inquiry %d", err);
6516+
}
6517+
6518+
conn->state = BT_CONNECT;
6519+
conn->out = true;
6520+
conn->role = HCI_ROLE_MASTER;
6521+
6522+
conn->attempt++;
6523+
6524+
conn->link_policy = hdev->link_policy;
6525+
6526+
memset(&cp, 0, sizeof(cp));
6527+
bacpy(&cp.bdaddr, &conn->dst);
6528+
cp.pscan_rep_mode = 0x02;
6529+
6530+
ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
6531+
if (ie) {
6532+
if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
6533+
cp.pscan_rep_mode = ie->data.pscan_rep_mode;
6534+
cp.pscan_mode = ie->data.pscan_mode;
6535+
cp.clock_offset = ie->data.clock_offset |
6536+
cpu_to_le16(0x8000);
6537+
}
6538+
6539+
memcpy(conn->dev_class, ie->data.dev_class, 3);
6540+
}
6541+
6542+
cp.pkt_type = cpu_to_le16(conn->pkt_type);
6543+
if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
6544+
cp.role_switch = 0x01;
6545+
else
6546+
cp.role_switch = 0x00;
6547+
6548+
err = __hci_cmd_sync_status_sk(hdev, HCI_OP_CREATE_CONN,
6549+
sizeof(cp), &cp,
6550+
HCI_EV_CONN_COMPLETE,
6551+
HCI_ACL_CONN_TIMEOUT, NULL);
6552+
6553+
if (err == -ETIMEDOUT)
6554+
hci_abort_conn_sync(hdev, conn, HCI_ERROR_LOCAL_HOST_TERM);
6555+
6556+
return err;
6557+
}
6558+
6559+
int hci_acl_create_connection_sync(struct hci_dev *hdev,
6560+
struct hci_conn *conn)
6561+
{
6562+
return hci_cmd_sync_queue(hdev, __hci_acl_create_connection_sync,
6563+
conn, NULL);
6564+
}

0 commit comments

Comments
 (0)