Bluetooth: Fix Connection persistence, SSP caching

Fix key storage when not bonding
Fix when LE ACL is disconnected after use

Signed-off-by: Brian Gix <bgix@codeaurora.org>
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 65af37f..2528c7f 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -1123,8 +1123,12 @@
 
 	conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, bdaddr);
 
-	if (conn && (conn->auth_type > 0x01 || conn->remote_auth > 0x01))
-		bonded = 1;
+	if (conn) {
+		if (conn->remote_auth > 0x01)
+			bonded = 1;
+		else if (conn->auth_initiator && conn->auth_type > 0x01)
+			bonded = 1;
+	}
 
 	if (new_key)
 		mgmt_new_key(hdev->id, key, bonded);
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 43ad520..d5477cd 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -3030,6 +3030,7 @@
 	conn->handle = __le16_to_cpu(ev->handle);
 	conn->state = BT_CONNECTED;
 	conn->disc_timeout = HCI_DISCONN_TIMEOUT;
+	mgmt_connected(hdev->id, &ev->bdaddr);
 
 	hci_conn_hold_device(conn);
 	hci_conn_add_sysfs(conn);
@@ -3249,6 +3250,8 @@
 	struct hci_event_hdr *hdr = (void *) skb->data;
 	__u8 event = hdr->evt;
 
+	BT_DBG("");
+
 	skb_pull(skb, HCI_EVENT_HDR_SIZE);
 
 	switch (event) {
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index ca18ed5..ea81593 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -1212,12 +1212,15 @@
 
 		conn = hcon->l2cap_data;
 	} else {
-		if (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA)
+		if (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA) {
 			hcon = hci_connect(hdev, LE_LINK, 0, dst,
-					   l2cap_pi(sk)->sec_level, auth_type);
-		else
+					l2cap_pi(sk)->sec_level, auth_type);
+			if (hcon->state == BT_CONNECTED)
+				hci_conn_hold(hcon);
+		} else {
 			hcon = hci_connect(hdev, ACL_LINK, 0, dst,
-					   l2cap_pi(sk)->sec_level, auth_type);
+					l2cap_pi(sk)->sec_level, auth_type);
+		}
 
 		if (IS_ERR(hcon)) {
 			err = PTR_ERR(hcon);
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 16033bb..087fc14 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -1380,8 +1380,6 @@
 		BT_DBG("Unable to find a pending command");
 		return;
 	}
-
-	hci_conn_put(conn);
 }
 
 static void discovery_terminated(struct pending_cmd *cmd, void *data)
@@ -1435,8 +1433,6 @@
 	if (hci_find_adv_entry(hdev, &cp->bdaddr)) {
 		conn = hci_connect(hdev, LE_LINK, 0, &cp->bdaddr, sec_level,
 								auth_type);
-		if (conn && !IS_ERR(conn))
-			hci_conn_hold(conn);
 	} else {
 		/* ACL-SSP does not support io_cap 0x04 (KeyboadDisplay) */
 		if (io_cap == 0x04)