1 /*
2  * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /**
18  * DOC: contains ll_lt_sap declarations specific to the bearer
19  * switch functionalities
20  */
21 
22 #ifndef _WLAN_LL_LT_SAP_BEARER_SWITCH_H_
23 #define _WLAN_LL_LT_SAP_BEARER_SWITCH_H_
24 
25 #include "wlan_ll_sap_public_structs.h"
26 #include <qdf_atomic.h>
27 #include "wlan_cmn.h"
28 #include "wlan_ll_sap_main.h"
29 #include "wlan_sm_engine.h"
30 
31 /**
32  * enum wlan_bearer_switch_sm_state - Bearer switch states
33  * @BEARER_NON_WLAN: Default state, Bearer non wlan state
34  * @BEARER_NON_WLAN_REQUESTED: State when bearer switch requested to non-wlan
35  * @BEARER_WLAN_REQUESTED: State when bearer switch requested to wlan
36  * @BEARER_WLAN: Bearer non wlan state
37  * @BEARER_SWITCH_MAX: Max state
38  */
39 enum wlan_bearer_switch_sm_state {
40 	BEARER_NON_WLAN = 0,
41 	BEARER_NON_WLAN_REQUESTED = 1,
42 	BEARER_WLAN_REQUESTED = 2,
43 	BEARER_WLAN = 3,
44 	BEARER_SWITCH_MAX = 4,
45 };
46 
47 /**
48  * enum wlan_bearer_switch_sm_evt - Bearer switch related events, if any new
49  * enum is added to this enum, then please update bs_sm_event_names
50  * @WLAN_BS_SM_EV_SWITCH_TO_WLAN: Bearer switch request to WLAN
51  * @WLAN_BS_SM_EV_SWITCH_TO_NON_WLAN: Bearer switch request to NON WLAN
52  * @WLAN_BS_SM_EV_SWITCH_TO_WLAN_TIMEOUT: Bearer switch request to WLA
53  * timeout
54  * @WLAN_BS_SM_EV_SWITCH_TO_NON_WLAN_TIMEOUT: Bearer switch request to NON-WLAN
55  * timeout
56  * @WLAN_BS_SM_EV_SWITCH_TO_WLAN_COMPLETED: Bearer switch request to WLAN
57  * completed
58  * @WLAN_BS_SM_EV_SWITCH_TO_NON_WLAN_COMPLETED: Bearer switch request to
59  * NON-WLAN completed
60  * @WLAN_BS_SM_EV_SWITCH_TO_WLAN_FAILURE: Bearer switch request to WLAN
61  * failure
62  * @WLAN_BS_SM_EV_SWITCH_TO_NON_WLAN_FAILURE: Bearer switch request to NON-WLAN
63  * failure
64  */
65 enum wlan_bearer_switch_sm_evt {
66 	WLAN_BS_SM_EV_SWITCH_TO_WLAN = 0,
67 	WLAN_BS_SM_EV_SWITCH_TO_NON_WLAN = 1,
68 	WLAN_BS_SM_EV_SWITCH_TO_WLAN_TIMEOUT = 2,
69 	WLAN_BS_SM_EV_SWITCH_TO_NON_WLAN_TIMEOUT = 3,
70 	WLAN_BS_SM_EV_SWITCH_TO_WLAN_COMPLETED = 4,
71 	WLAN_BS_SM_EV_SWITCH_TO_NON_WLAN_COMPLETED = 5,
72 	WLAN_BS_SM_EV_SWITCH_TO_WLAN_FAILURE = 6,
73 	WLAN_BS_SM_EV_SWITCH_TO_NON_WLAN_FAILURE = 7,
74 };
75 
76 /**
77  * struct bs_state_sm - Bearer switch state machine
78  * @bs_sm_lock: sm lock
79  * @sm_hdl: sm handlers
80  * @bs_state: bearer switch state
81  */
82 struct bs_state_sm {
83 	qdf_mutex_t bs_sm_lock;
84 	struct wlan_sm *sm_hdl;
85 	enum wlan_bearer_switch_sm_state bs_state;
86 };
87 
88 /**
89  * struct bearer_switch_info - Data structure to store the bearer switch
90  * requests and related information
91  * @vdev: Pointer to the ll lt sap vdev
92  * @request_id: Last allocated request id
93  * @sm: state machine context
94  * @ref_count: Reference count corresponding to each vdev and requester
95  * @fw_ref_count: Reference counts for the firmware requests
96  * @total_ref_count: Total reference counts
97  * @last_status:status of the last bearer switch request
98  * @requests: Array of bearer_switch_requests to cache the request information
99  * @bs_request_timer: Bearer switch request timer
100  */
101 struct bearer_switch_info {
102 	struct wlan_objmgr_vdev *vdev;
103 	qdf_atomic_t request_id;
104 	struct bs_state_sm sm;
105 	qdf_atomic_t ref_count[WLAN_UMAC_PSOC_MAX_VDEVS][BEARER_SWITCH_REQ_MAX];
106 	qdf_atomic_t fw_ref_count;
107 	qdf_atomic_t total_ref_count;
108 	QDF_STATUS last_status;
109 	struct wlan_bearer_switch_request requests[MAX_BEARER_SWITCH_REQUESTERS];
110 	qdf_mc_timer_t bs_request_timer;
111 };
112 
113 /**
114  * ll_lt_sap_bearer_switch_get_id() - Get the request id for bearer switch
115  * request
116  * @psoc: Pointer to psoc
117  * Return: Bearer switch request id
118  */
119 wlan_bs_req_id ll_lt_sap_bearer_switch_get_id(struct wlan_objmgr_psoc *psoc);
120 
121 /**
122  * bs_sm_create() - Invoke SM creation for bearer switch
123  * @bs_ctx:  Bearer switch context
124  *
125  * Return: SUCCESS on successful creation
126  *         FAILURE, if creation fails
127  */
128 QDF_STATUS bs_sm_create(struct bearer_switch_info *bs_ctx);
129 
130 /**
131  * bs_sm_destroy() - Invoke SM deletion for bearer switch
132  * @bs_ctx:  Bearer switch context
133  *
134  * Return: SUCCESS on successful deletion
135  *         FAILURE, if deletion fails
136  */
137 QDF_STATUS bs_sm_destroy(struct bearer_switch_info *bs_ctx);
138 
139 /**
140  * bs_lock_create() - Create BS SM mutex
141  * @bs_ctx:  Bearer switch ctx
142  *
143  * Creates Bearer switch state machine mutex
144  *
145  * Return: void
146  */
bs_lock_create(struct bearer_switch_info * bs_ctx)147 static inline void bs_lock_create(struct bearer_switch_info *bs_ctx)
148 {
149 	qdf_mutex_create(&bs_ctx->sm.bs_sm_lock);
150 }
151 
152 /**
153  * bs_lock_destroy() - Create BS SM mutex
154  * @bs_ctx:  Bearer switch ctx
155  *
156  * Deatroys Bearer switch state machine mutex
157  *
158  * Return: void
159  */
bs_lock_destroy(struct bearer_switch_info * bs_ctx)160 static inline void bs_lock_destroy(struct bearer_switch_info *bs_ctx)
161 {
162 	qdf_mutex_destroy(&bs_ctx->sm.bs_sm_lock);
163 }
164 
165 /**
166  * bs_lock_acquire() - Acquires BS SM mutex
167  * @bs_ctx:  Bearer switch ctx
168  *
169  * Acquire Bearer switch state machine mutex
170  *
171  * Return: void
172  */
bs_lock_acquire(struct bearer_switch_info * bs_ctx)173 static inline void bs_lock_acquire(struct bearer_switch_info *bs_ctx)
174 {
175 	qdf_mutex_acquire(&bs_ctx->sm.bs_sm_lock);
176 }
177 
178 /**
179  * bs_lock_release() - Release BS SM mutex
180  * @bs_ctx:  Bearer switch ctx
181  *
182  * Releases Bearer switch state machine mutex
183  *
184  * Return: void
185  */
bs_lock_release(struct bearer_switch_info * bs_ctx)186 static inline void bs_lock_release(struct bearer_switch_info *bs_ctx)
187 {
188 	qdf_mutex_release(&bs_ctx->sm.bs_sm_lock);
189 }
190 
191 /**
192  * bs_sm_transition_to() - invokes state transition
193  * @bs_ctx:  Bearer switch ctx
194  * @state: new cm state
195  *
196  * API to invoke SM API to move to new state
197  *
198  * Return: void
199  */
bs_sm_transition_to(struct bearer_switch_info * bs_ctx,enum wlan_bearer_switch_sm_state state)200 static inline void bs_sm_transition_to(struct bearer_switch_info *bs_ctx,
201 				       enum wlan_bearer_switch_sm_state state)
202 {
203 	wlan_sm_transition_to(bs_ctx->sm.sm_hdl, state);
204 }
205 
206 /**
207  * bs_sm_deliver_event() - Delivers event to Bearer switch SM
208  * @psoc: Pointer to psoc
209  * @event: BS event
210  * @data_len: data size
211  * @data: event data
212  *
213  * API to dispatch event to Bearer switch state machine. To be used while
214  * posting events from API called from public API.
215  *
216  * Return: SUCCESS: on handling event
217  *         FAILURE: If event not handled
218  */
219 QDF_STATUS bs_sm_deliver_event(struct wlan_objmgr_psoc *psoc,
220 			       enum wlan_bearer_switch_sm_evt event,
221 			       uint16_t data_len, void *data);
222 
223 /**
224  * bs_req_timer_init() - Initialize Bearer switch request timer
225  * @bs_ctx: Bearer switch context
226  *
227  * Return: None
228  */
229 void bs_req_timer_init(struct bearer_switch_info *bs_ctx);
230 
231 /**
232  * bs_req_timer_deinit() - De-initialize Bearer switch request timer
233  * @bs_ctx: Bearer switch context
234  *
235  * Return: None
236  */
237 void bs_req_timer_deinit(struct bearer_switch_info *bs_ctx);
238 
239 /**
240  * ll_lt_sap_is_bs_ctx_valid() - Check if bearer switch context is valid or not
241  * @bs_ctx: Bearer switch context
242  *
243  * Return: True if bearer switch context is valid else return false
244  */
245 #define ll_lt_sap_is_bs_ctx_valid(bs_ctx) \
246 	__ll_lt_sap_is_bs_ctx_valid(bs_ctx, __func__)
247 
248 bool __ll_lt_sap_is_bs_ctx_valid(struct bearer_switch_info *bs_ctx,
249 				 const char *func);
250 
251 /**
252  * ll_lt_sap_is_bs_req_valid() - Check if bearer switch request is valid or not
253  * @bs_req: Bearer switch request
254  *
255  * Return: True if bearer switch request is valid else return false
256  */
257 #define ll_lt_sap_is_bs_req_valid(bs_req) \
258 	__ll_lt_sap_is_bs_req_valid(bs_req, __func__)
259 
260 bool __ll_lt_sap_is_bs_req_valid(struct wlan_bearer_switch_request *bs_req,
261 				 const char *func);
262 
263 /**
264  * ll_lt_sap_switch_bearer_to_ble() - Switch audio transport to BLE
265  * @psoc: Pointer to psoc
266  * @bs_request: Pointer to bearer switch request
267  * Return: QDF_STATUS_SUCCESS on successful bearer switch else failure
268  */
269 QDF_STATUS
270 ll_lt_sap_switch_bearer_to_ble(struct wlan_objmgr_psoc *psoc,
271 			       struct wlan_bearer_switch_request *bs_request);
272 
273 /**
274  * ll_lt_sap_switch_bearer_to_wlan() - Switch audio transport to BLE
275  * @psoc: Pointer to psoc
276  * @bs_request: Pointer to bearer switch request
277  * Return: QDF_STATUS_SUCCESS on successful bearer switch else failure
278  */
279 QDF_STATUS
280 ll_lt_sap_switch_bearer_to_wlan(struct wlan_objmgr_psoc *psoc,
281 				struct wlan_bearer_switch_request *bs_request);
282 
283 /**
284  * ll_lt_sap_request_for_audio_transport_switch() - Handls audio transport
285  * switch request from userspace
286  * @vdev: Vdev on which the request is received
287  * @req_type: requested transport switch type
288  *
289  * Return: True/False
290  */
291 QDF_STATUS
292 ll_lt_sap_request_for_audio_transport_switch(struct wlan_objmgr_vdev *vdev,
293 					enum bearer_switch_req_type req_type);
294 
295 /**
296  * ll_lt_sap_deliver_audio_transport_switch_resp() - Deliver audio
297  * transport switch response
298  * @vdev: Vdev on which the request is received
299  * @req_type: Transport switch type for which the response is received
300  * @status: Status of the response
301  *
302  * Return: None
303  */
304 void
305 ll_lt_sap_deliver_audio_transport_switch_resp(struct wlan_objmgr_vdev *vdev,
306 					enum bearer_switch_req_type req_type,
307 					enum bearer_switch_status status);
308 
309 #endif /* _WLAN_LL_LT_SAP_BEARER_SWITCH_H_ */
310