1 /*
2  * Copyright (c) 2025 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #if !defined(CONFIG_BT_HCI_RAW) || !defined(CONFIG_HAS_BT_CTLR) || \
8 	!defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
9 /* Following build configurations use configurable CONFIG_BT_BUF_CMD_TX_COUNT:
10  * 1. Host + Controller build with and without Controller to Host data flow control, or
11  * 2. Host-only with and without Controller to Host data flow control, or
12  * 3. Controller-only without Controller to Host data flow control support
13  */
14 #if !defined(CONFIG_BT_HCI_RAW)
15 /* Host + Controller build, and Host-only build */
16 
17 /* Auto initiated additional HCI command buffers enqueued in the Host */
18 #define BT_BUF_CMD_TX_REMOTE_VERSION       COND_CODE_1(CONFIG_BT_REMOTE_VERSION, (1), (0))
19 #define BT_BUF_CMD_TX_AUTO_PHY_UPDATE      COND_CODE_1(CONFIG_BT_AUTO_PHY_UPDATE, (1), (0))
20 #define BT_BUF_CMD_TX_AUTO_DATA_LEN_UPDATE COND_CODE_1(CONFIG_BT_AUTO_DATA_LEN_UPDATE, (1), (0))
21 
22 #else /* CONFIG_BT_HCI_RAW */
23 #if defined(CONFIG_HAS_BT_CTLR)
24 /* Controller-only build need no additional HCI command buffers */
25 BUILD_ASSERT((CONFIG_BT_BUF_CMD_TX_COUNT == CONFIG_BT_CTLR_HCI_NUM_CMD_PKT_MAX),
26 	     "Mismatch in allocated HCI command buffers compared to Controller supported value.");
27 #endif /* CONFIG_HAS_BT_CTLR */
28 
29 /* Controller-only build do not enqueue auto initiated HCI commands */
30 #define BT_BUF_CMD_TX_REMOTE_VERSION       0
31 #define BT_BUF_CMD_TX_AUTO_PHY_UPDATE      0
32 #define BT_BUF_CMD_TX_AUTO_DATA_LEN_UPDATE 0
33 #endif /* !CONFIG_BT_HCI_RAW */
34 
35 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
36 /* When Controller to Host data flow control is supported in Host plus Controller build, or
37  * Host-only build, then we need additional BT_BUF_ACL_RX_COUNT number of HCI command buffers for
38  * enqueuing Host Number of Completed Packets command.
39  *
40  * Host keeps the first, and subsequent, Rx buffers (that comes from the driver) for each connection
41  * to do re-assembly into, up to the L2CAP SDU length required number of Rx buffers.
42  * BT_BUF_ACL_RX_COUNT_EXTRA holds the application configured number of buffers across active
43  * connections for recombination of HCI data packets to L2CAP SDUs.
44  *
45  * BT_BUF_HCI_EVT_RX_COUNT defines the number of available buffers reserved for "synchronous"
46  * processing of HCI events like Number of Completed Packets, disconnection complete etc.
47  *
48  * BT_BUF_HCI_ACL_RX_COUNT defines the number of available buffers for Controller to Host data
49  * flow control; keeping the application configured BT_BUF_ACL_RX_COUNT_EXTRA number of buffers
50  * available for L2CAP recombination, and a reserved number of buffers for processing HCI events.
51  */
52 
53 /* FIXME: Calculate the maximum number of HCI events of different types that a connection can
54  *        enqueue while the Host is slow at processing HCI events.
55  *
56  *        1. Disconnection Complete event
57  *        2. LE Connection Update Complete event
58  *        3. LE Long Term Key Request event
59  *        4. LE Remote Connection Parameter Request Event
60  *        5. LE Data Length Change event
61  *        6. LE PHY Update Complete event
62  *        7. ...
63  *
64  * As there is no HCI event flow control defined, implementations will need a transport level flow
65  * control to restrict buffers required on resource constraint devices, i.e. if these events are not
66  * processed "synchronous".
67  */
68 #define BT_BUF_HCI_EVT_RX_COUNT          1
69 #define BT_BUF_HCI_ACL_RX_COUNT          (BT_BUF_RX_COUNT - BT_BUF_HCI_EVT_RX_COUNT - \
70 					  BT_BUF_ACL_RX_COUNT_EXTRA)
71 #define BT_BUF_CMD_TX_HOST_NUM_CMPLT_PKT (BT_BUF_HCI_ACL_RX_COUNT)
72 
73 #else /* !CONFIG_BT_HCI_ACL_FLOW_CONTROL */
74 #define BT_BUF_CMD_TX_HOST_NUM_CMPLT_PKT 0
75 #endif /* !CONFIG_BT_HCI_ACL_FLOW_CONTROL */
76 
77 /* Based on Host + Controller, Host-only or Controller-only; Calculate the HCI Command Tx count. */
78 #define BT_BUF_CMD_TX_COUNT (CONFIG_BT_BUF_CMD_TX_COUNT + \
79 			     BT_BUF_CMD_TX_HOST_NUM_CMPLT_PKT + \
80 			     BT_BUF_CMD_TX_REMOTE_VERSION + \
81 			     BT_BUF_CMD_TX_AUTO_PHY_UPDATE + \
82 			     BT_BUF_CMD_TX_AUTO_DATA_LEN_UPDATE)
83 
84 #elif defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
85 /* When Controller to Host data flow control is supported in the Controller-only build, ensure
86  * that BT_BUF_CMD_TX_COUNT is greater than or equal to (BT_BUF_RX_COUNT + Ncmd),
87  * where Ncmd is supported maximum Num_HCI_Command_Packets in the Controller implementation.
88  */
89 BUILD_ASSERT(!IS_ENABLED(CONFIG_BT_BUF_CMD_TX_COUNT),
90 	     "Configurable HCI command buffer count disallowed.");
91 BUILD_ASSERT(IS_ENABLED(CONFIG_BT_CTLR_HCI_NUM_CMD_PKT_MAX),
92 	     "Undefined Controller implementation supported Num_HCI_Command_Packets value.");
93 
94 /** Can use all of buffer count needed for HCI ACL, HCI ISO or Event RX buffers for ACL RX */
95 #define BT_BUF_HCI_ACL_RX_COUNT (BT_BUF_RX_COUNT)
96 
97 /* Controller-only with Controller to Host data flow control */
98 #define BT_BUF_CMD_TX_COUNT     (BT_BUF_RX_COUNT + CONFIG_BT_CTLR_HCI_NUM_CMD_PKT_MAX)
99 
100 #endif /* CONFIG_BT_HCI_ACL_FLOW_CONTROL */
101