1*5113495bSYour Name /*
2*5113495bSYour Name * Copyright (c) 2018-2019, 2021 The Linux Foundation. All rights reserved.
3*5113495bSYour Name * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4*5113495bSYour Name *
5*5113495bSYour Name * Permission to use, copy, modify, and/or distribute this software for
6*5113495bSYour Name * any purpose with or without fee is hereby granted, provided that the
7*5113495bSYour Name * above copyright notice and this permission notice appear in all
8*5113495bSYour Name * copies.
9*5113495bSYour Name *
10*5113495bSYour Name * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11*5113495bSYour Name * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12*5113495bSYour Name * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13*5113495bSYour Name * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14*5113495bSYour Name * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15*5113495bSYour Name * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16*5113495bSYour Name * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17*5113495bSYour Name * PERFORMANCE OF THIS SOFTWARE.
18*5113495bSYour Name */
19*5113495bSYour Name
20*5113495bSYour Name #include "linux/device.h"
21*5113495bSYour Name #include "linux/netdevice.h"
22*5113495bSYour Name #include "__osif_psoc_sync.h"
23*5113495bSYour Name #include "__osif_vdev_sync.h"
24*5113495bSYour Name #include "osif_vdev_sync.h"
25*5113495bSYour Name #include "qdf_lock.h"
26*5113495bSYour Name #include "qdf_status.h"
27*5113495bSYour Name #include "qdf_types.h"
28*5113495bSYour Name #include <qdf_trace.h>
29*5113495bSYour Name #include <wlan_cfg80211.h>
30*5113495bSYour Name
31*5113495bSYour Name static struct osif_vdev_sync __osif_vdev_sync_arr[WLAN_MAX_VDEVS +
32*5113495bSYour Name WLAN_MAX_ML_VDEVS];
33*5113495bSYour Name static qdf_spinlock_t __osif_vdev_sync_lock;
34*5113495bSYour Name
35*5113495bSYour Name #define osif_vdev_sync_lock_create() qdf_spinlock_create(&__osif_vdev_sync_lock)
36*5113495bSYour Name #define osif_vdev_sync_lock_destroy() \
37*5113495bSYour Name qdf_spinlock_destroy(&__osif_vdev_sync_lock)
38*5113495bSYour Name #define osif_vdev_sync_lock() qdf_spin_lock_bh(&__osif_vdev_sync_lock)
39*5113495bSYour Name #define osif_vdev_sync_unlock() qdf_spin_unlock_bh(&__osif_vdev_sync_lock)
40*5113495bSYour Name
osif_vdev_sync_lookup(struct net_device * net_dev)41*5113495bSYour Name static struct osif_vdev_sync *osif_vdev_sync_lookup(struct net_device *net_dev)
42*5113495bSYour Name {
43*5113495bSYour Name int i;
44*5113495bSYour Name
45*5113495bSYour Name for (i = 0; i < QDF_ARRAY_SIZE(__osif_vdev_sync_arr); i++) {
46*5113495bSYour Name struct osif_vdev_sync *vdev_sync = __osif_vdev_sync_arr + i;
47*5113495bSYour Name
48*5113495bSYour Name if (!vdev_sync->in_use)
49*5113495bSYour Name continue;
50*5113495bSYour Name
51*5113495bSYour Name if (vdev_sync->net_dev == net_dev)
52*5113495bSYour Name return vdev_sync;
53*5113495bSYour Name }
54*5113495bSYour Name
55*5113495bSYour Name return NULL;
56*5113495bSYour Name }
57*5113495bSYour Name
osif_get_vdev_sync_arr(void)58*5113495bSYour Name struct osif_vdev_sync *osif_get_vdev_sync_arr(void)
59*5113495bSYour Name {
60*5113495bSYour Name return __osif_vdev_sync_arr;
61*5113495bSYour Name }
62*5113495bSYour Name
osif_vdev_sync_get(void)63*5113495bSYour Name static struct osif_vdev_sync *osif_vdev_sync_get(void)
64*5113495bSYour Name {
65*5113495bSYour Name int i;
66*5113495bSYour Name
67*5113495bSYour Name for (i = 0; i < QDF_ARRAY_SIZE(__osif_vdev_sync_arr); i++) {
68*5113495bSYour Name struct osif_vdev_sync *vdev_sync = __osif_vdev_sync_arr + i;
69*5113495bSYour Name
70*5113495bSYour Name if (!vdev_sync->in_use) {
71*5113495bSYour Name vdev_sync->in_use = true;
72*5113495bSYour Name return vdev_sync;
73*5113495bSYour Name }
74*5113495bSYour Name }
75*5113495bSYour Name
76*5113495bSYour Name return NULL;
77*5113495bSYour Name }
78*5113495bSYour Name
osif_vdev_sync_put(struct osif_vdev_sync * vdev_sync)79*5113495bSYour Name static void osif_vdev_sync_put(struct osif_vdev_sync *vdev_sync)
80*5113495bSYour Name {
81*5113495bSYour Name qdf_mem_zero(vdev_sync, sizeof(*vdev_sync));
82*5113495bSYour Name }
83*5113495bSYour Name
osif_vdev_sync_create(struct device * dev,struct osif_vdev_sync ** out_vdev_sync)84*5113495bSYour Name int osif_vdev_sync_create(struct device *dev,
85*5113495bSYour Name struct osif_vdev_sync **out_vdev_sync)
86*5113495bSYour Name {
87*5113495bSYour Name struct osif_vdev_sync *vdev_sync;
88*5113495bSYour Name QDF_STATUS status;
89*5113495bSYour Name
90*5113495bSYour Name QDF_BUG(dev);
91*5113495bSYour Name if (!dev)
92*5113495bSYour Name return -EINVAL;
93*5113495bSYour Name
94*5113495bSYour Name QDF_BUG(out_vdev_sync);
95*5113495bSYour Name if (!out_vdev_sync)
96*5113495bSYour Name return -EINVAL;
97*5113495bSYour Name
98*5113495bSYour Name osif_vdev_sync_lock();
99*5113495bSYour Name vdev_sync = osif_vdev_sync_get();
100*5113495bSYour Name osif_vdev_sync_unlock();
101*5113495bSYour Name if (!vdev_sync)
102*5113495bSYour Name return -ENOMEM;
103*5113495bSYour Name
104*5113495bSYour Name status = osif_psoc_sync_dsc_vdev_create(dev, &vdev_sync->dsc_vdev);
105*5113495bSYour Name if (QDF_IS_STATUS_ERROR(status))
106*5113495bSYour Name goto sync_put;
107*5113495bSYour Name
108*5113495bSYour Name *out_vdev_sync = vdev_sync;
109*5113495bSYour Name
110*5113495bSYour Name return 0;
111*5113495bSYour Name
112*5113495bSYour Name sync_put:
113*5113495bSYour Name osif_vdev_sync_lock();
114*5113495bSYour Name osif_vdev_sync_put(vdev_sync);
115*5113495bSYour Name osif_vdev_sync_unlock();
116*5113495bSYour Name
117*5113495bSYour Name return qdf_status_to_os_return(status);
118*5113495bSYour Name }
119*5113495bSYour Name
__osif_vdev_sync_create_and_trans(struct device * dev,struct osif_vdev_sync ** out_vdev_sync,const char * desc)120*5113495bSYour Name int __osif_vdev_sync_create_and_trans(struct device *dev,
121*5113495bSYour Name struct osif_vdev_sync **out_vdev_sync,
122*5113495bSYour Name const char *desc)
123*5113495bSYour Name {
124*5113495bSYour Name struct osif_vdev_sync *vdev_sync;
125*5113495bSYour Name QDF_STATUS status;
126*5113495bSYour Name int errno;
127*5113495bSYour Name
128*5113495bSYour Name errno = osif_vdev_sync_create(dev, &vdev_sync);
129*5113495bSYour Name if (errno)
130*5113495bSYour Name return errno;
131*5113495bSYour Name
132*5113495bSYour Name status = dsc_vdev_trans_start(vdev_sync->dsc_vdev, desc);
133*5113495bSYour Name if (QDF_IS_STATUS_ERROR(status))
134*5113495bSYour Name goto sync_destroy;
135*5113495bSYour Name
136*5113495bSYour Name *out_vdev_sync = vdev_sync;
137*5113495bSYour Name
138*5113495bSYour Name return 0;
139*5113495bSYour Name
140*5113495bSYour Name sync_destroy:
141*5113495bSYour Name osif_vdev_sync_destroy(vdev_sync);
142*5113495bSYour Name
143*5113495bSYour Name return qdf_status_to_os_return(status);
144*5113495bSYour Name }
145*5113495bSYour Name
osif_vdev_sync_destroy(struct osif_vdev_sync * vdev_sync)146*5113495bSYour Name void osif_vdev_sync_destroy(struct osif_vdev_sync *vdev_sync)
147*5113495bSYour Name {
148*5113495bSYour Name QDF_BUG(vdev_sync);
149*5113495bSYour Name if (!vdev_sync)
150*5113495bSYour Name return;
151*5113495bSYour Name
152*5113495bSYour Name dsc_vdev_destroy(&vdev_sync->dsc_vdev);
153*5113495bSYour Name
154*5113495bSYour Name osif_vdev_sync_lock();
155*5113495bSYour Name osif_vdev_sync_put(vdev_sync);
156*5113495bSYour Name osif_vdev_sync_unlock();
157*5113495bSYour Name }
158*5113495bSYour Name
osif_vdev_sync_register(struct net_device * net_dev,struct osif_vdev_sync * vdev_sync)159*5113495bSYour Name void osif_vdev_sync_register(struct net_device *net_dev,
160*5113495bSYour Name struct osif_vdev_sync *vdev_sync)
161*5113495bSYour Name {
162*5113495bSYour Name QDF_BUG(net_dev);
163*5113495bSYour Name QDF_BUG(vdev_sync);
164*5113495bSYour Name if (!vdev_sync)
165*5113495bSYour Name return;
166*5113495bSYour Name
167*5113495bSYour Name osif_vdev_sync_lock();
168*5113495bSYour Name vdev_sync->net_dev = net_dev;
169*5113495bSYour Name osif_vdev_sync_unlock();
170*5113495bSYour Name }
171*5113495bSYour Name
osif_vdev_sync_unregister(struct net_device * net_dev)172*5113495bSYour Name struct osif_vdev_sync *osif_vdev_sync_unregister(struct net_device *net_dev)
173*5113495bSYour Name {
174*5113495bSYour Name struct osif_vdev_sync *vdev_sync;
175*5113495bSYour Name
176*5113495bSYour Name QDF_BUG(net_dev);
177*5113495bSYour Name if (!net_dev)
178*5113495bSYour Name return NULL;
179*5113495bSYour Name
180*5113495bSYour Name osif_vdev_sync_lock();
181*5113495bSYour Name vdev_sync = osif_vdev_sync_lookup(net_dev);
182*5113495bSYour Name if (vdev_sync)
183*5113495bSYour Name vdev_sync->net_dev = NULL;
184*5113495bSYour Name osif_vdev_sync_unlock();
185*5113495bSYour Name
186*5113495bSYour Name return vdev_sync;
187*5113495bSYour Name }
188*5113495bSYour Name
189*5113495bSYour Name typedef QDF_STATUS (*vdev_start_func)(struct dsc_vdev *, const char *);
190*5113495bSYour Name
191*5113495bSYour Name static int
__osif_vdev_sync_start_callback(struct net_device * net_dev,struct osif_vdev_sync ** out_vdev_sync,const char * desc,vdev_start_func vdev_start_cb)192*5113495bSYour Name __osif_vdev_sync_start_callback(struct net_device *net_dev,
193*5113495bSYour Name struct osif_vdev_sync **out_vdev_sync,
194*5113495bSYour Name const char *desc,
195*5113495bSYour Name vdev_start_func vdev_start_cb)
196*5113495bSYour Name {
197*5113495bSYour Name QDF_STATUS status;
198*5113495bSYour Name struct osif_vdev_sync *vdev_sync;
199*5113495bSYour Name
200*5113495bSYour Name *out_vdev_sync = NULL;
201*5113495bSYour Name
202*5113495bSYour Name vdev_sync = osif_vdev_sync_lookup(net_dev);
203*5113495bSYour Name if (!vdev_sync)
204*5113495bSYour Name return -EAGAIN;
205*5113495bSYour Name
206*5113495bSYour Name *out_vdev_sync = vdev_sync;
207*5113495bSYour Name
208*5113495bSYour Name status = vdev_start_cb(vdev_sync->dsc_vdev, desc);
209*5113495bSYour Name if (QDF_IS_STATUS_ERROR(status))
210*5113495bSYour Name return qdf_status_to_os_return(status);
211*5113495bSYour Name
212*5113495bSYour Name return 0;
213*5113495bSYour Name }
214*5113495bSYour Name
215*5113495bSYour Name static int
__osif_vdev_sync_start_wait_callback(struct net_device * net_dev,struct osif_vdev_sync ** out_vdev_sync,const char * desc,vdev_start_func vdev_start_cb)216*5113495bSYour Name __osif_vdev_sync_start_wait_callback(struct net_device *net_dev,
217*5113495bSYour Name struct osif_vdev_sync **out_vdev_sync,
218*5113495bSYour Name const char *desc,
219*5113495bSYour Name vdev_start_func vdev_start_cb)
220*5113495bSYour Name {
221*5113495bSYour Name QDF_STATUS status;
222*5113495bSYour Name struct osif_vdev_sync *vdev_sync;
223*5113495bSYour Name
224*5113495bSYour Name *out_vdev_sync = NULL;
225*5113495bSYour Name
226*5113495bSYour Name osif_vdev_sync_lock();
227*5113495bSYour Name vdev_sync = osif_vdev_sync_lookup(net_dev);
228*5113495bSYour Name osif_vdev_sync_unlock();
229*5113495bSYour Name if (!vdev_sync)
230*5113495bSYour Name return -EAGAIN;
231*5113495bSYour Name
232*5113495bSYour Name status = vdev_start_cb(vdev_sync->dsc_vdev, desc);
233*5113495bSYour Name if (QDF_IS_STATUS_ERROR(status))
234*5113495bSYour Name return qdf_status_to_os_return(status);
235*5113495bSYour Name
236*5113495bSYour Name *out_vdev_sync = vdev_sync;
237*5113495bSYour Name
238*5113495bSYour Name return 0;
239*5113495bSYour Name }
240*5113495bSYour Name
241*5113495bSYour Name /**
242*5113495bSYour Name * osif_vdev_sync_wait_for_uptree_ops - Wait for psoc/driver operations
243*5113495bSYour Name * @vdev_sync: vdev sync pointer
244*5113495bSYour Name *
245*5113495bSYour Name * If there are any psoc/driver operations are taking place, then vdev
246*5113495bSYour Name * trans/ops should wait for these operations to be completed to avoid
247*5113495bSYour Name * memory domain mismatch issues. For example, if modules are closed
248*5113495bSYour Name * because of idle shutdown, memory domain will be init domain and at
249*5113495bSYour Name * that time if some psoc ops starts, memory allocated as part of this
250*5113495bSYour Name * ops will be allocated in init domain and if at the same time if vdev
251*5113495bSYour Name * up starts which will trigger the vdev trans and will start the
252*5113495bSYour Name * modules and change the memory domain to active domain, now when the
253*5113495bSYour Name * memory allocated as part of psoc operation is release on psoc ops
254*5113495bSYour Name * completion will be released in the active domain which leads the
255*5113495bSYour Name * memory domain mismatch.
256*5113495bSYour Name *
257*5113495bSYour Name * Return: None.
258*5113495bSYour Name */
osif_vdev_sync_wait_for_uptree_ops(struct osif_vdev_sync * vdev_sync)259*5113495bSYour Name static void osif_vdev_sync_wait_for_uptree_ops(struct osif_vdev_sync *vdev_sync)
260*5113495bSYour Name {
261*5113495bSYour Name dsc_vdev_wait_for_uptree_ops(vdev_sync->dsc_vdev);
262*5113495bSYour Name }
263*5113495bSYour Name
__osif_vdev_sync_trans_start(struct net_device * net_dev,struct osif_vdev_sync ** out_vdev_sync,const char * desc)264*5113495bSYour Name int __osif_vdev_sync_trans_start(struct net_device *net_dev,
265*5113495bSYour Name struct osif_vdev_sync **out_vdev_sync,
266*5113495bSYour Name const char *desc)
267*5113495bSYour Name {
268*5113495bSYour Name int errno;
269*5113495bSYour Name
270*5113495bSYour Name osif_vdev_sync_lock();
271*5113495bSYour Name errno = __osif_vdev_sync_start_callback(net_dev, out_vdev_sync, desc,
272*5113495bSYour Name dsc_vdev_trans_start);
273*5113495bSYour Name osif_vdev_sync_unlock();
274*5113495bSYour Name
275*5113495bSYour Name if (!errno) {
276*5113495bSYour Name osif_vdev_sync_wait_for_ops(*out_vdev_sync);
277*5113495bSYour Name osif_vdev_sync_wait_for_uptree_ops(*out_vdev_sync);
278*5113495bSYour Name }
279*5113495bSYour Name
280*5113495bSYour Name return errno;
281*5113495bSYour Name }
282*5113495bSYour Name
__osif_vdev_sync_trans_start_wait(struct net_device * net_dev,struct osif_vdev_sync ** out_vdev_sync,const char * desc)283*5113495bSYour Name int __osif_vdev_sync_trans_start_wait(struct net_device *net_dev,
284*5113495bSYour Name struct osif_vdev_sync **out_vdev_sync,
285*5113495bSYour Name const char *desc)
286*5113495bSYour Name {
287*5113495bSYour Name int errno;
288*5113495bSYour Name
289*5113495bSYour Name /* since dsc_vdev_trans_start_wait may sleep do not take lock here */
290*5113495bSYour Name errno = __osif_vdev_sync_start_wait_callback(net_dev,
291*5113495bSYour Name out_vdev_sync, desc,
292*5113495bSYour Name dsc_vdev_trans_start_wait);
293*5113495bSYour Name
294*5113495bSYour Name if (!errno) {
295*5113495bSYour Name osif_vdev_sync_wait_for_ops(*out_vdev_sync);
296*5113495bSYour Name osif_vdev_sync_wait_for_uptree_ops(*out_vdev_sync);
297*5113495bSYour Name }
298*5113495bSYour Name
299*5113495bSYour Name return errno;
300*5113495bSYour Name }
301*5113495bSYour Name
osif_vdev_sync_trans_stop(struct osif_vdev_sync * vdev_sync)302*5113495bSYour Name void osif_vdev_sync_trans_stop(struct osif_vdev_sync *vdev_sync)
303*5113495bSYour Name {
304*5113495bSYour Name dsc_vdev_trans_stop(vdev_sync->dsc_vdev);
305*5113495bSYour Name }
306*5113495bSYour Name
osif_vdev_sync_assert_trans_protected(struct net_device * net_dev)307*5113495bSYour Name void osif_vdev_sync_assert_trans_protected(struct net_device *net_dev)
308*5113495bSYour Name {
309*5113495bSYour Name struct osif_vdev_sync *vdev_sync;
310*5113495bSYour Name
311*5113495bSYour Name osif_vdev_sync_lock();
312*5113495bSYour Name
313*5113495bSYour Name vdev_sync = osif_vdev_sync_lookup(net_dev);
314*5113495bSYour Name QDF_BUG(vdev_sync);
315*5113495bSYour Name if (vdev_sync)
316*5113495bSYour Name dsc_vdev_assert_trans_protected(vdev_sync->dsc_vdev);
317*5113495bSYour Name
318*5113495bSYour Name osif_vdev_sync_unlock();
319*5113495bSYour Name }
320*5113495bSYour Name
__osif_vdev_sync_op_start(struct net_device * net_dev,struct osif_vdev_sync ** out_vdev_sync,const char * func)321*5113495bSYour Name int __osif_vdev_sync_op_start(struct net_device *net_dev,
322*5113495bSYour Name struct osif_vdev_sync **out_vdev_sync,
323*5113495bSYour Name const char *func)
324*5113495bSYour Name {
325*5113495bSYour Name int errno;
326*5113495bSYour Name
327*5113495bSYour Name osif_vdev_sync_lock();
328*5113495bSYour Name errno = __osif_vdev_sync_start_callback(net_dev, out_vdev_sync, func,
329*5113495bSYour Name _dsc_vdev_op_start);
330*5113495bSYour Name osif_vdev_sync_unlock();
331*5113495bSYour Name
332*5113495bSYour Name return errno;
333*5113495bSYour Name }
334*5113495bSYour Name
__osif_vdev_sync_op_stop(struct osif_vdev_sync * vdev_sync,const char * func)335*5113495bSYour Name void __osif_vdev_sync_op_stop(struct osif_vdev_sync *vdev_sync,
336*5113495bSYour Name const char *func)
337*5113495bSYour Name {
338*5113495bSYour Name _dsc_vdev_op_stop(vdev_sync->dsc_vdev, func);
339*5113495bSYour Name }
340*5113495bSYour Name
osif_vdev_sync_wait_for_ops(struct osif_vdev_sync * vdev_sync)341*5113495bSYour Name void osif_vdev_sync_wait_for_ops(struct osif_vdev_sync *vdev_sync)
342*5113495bSYour Name {
343*5113495bSYour Name dsc_vdev_wait_for_ops(vdev_sync->dsc_vdev);
344*5113495bSYour Name }
345*5113495bSYour Name
osif_vdev_sync_init(void)346*5113495bSYour Name void osif_vdev_sync_init(void)
347*5113495bSYour Name {
348*5113495bSYour Name osif_vdev_sync_lock_create();
349*5113495bSYour Name }
350*5113495bSYour Name
osif_vdev_sync_deinit(void)351*5113495bSYour Name void osif_vdev_sync_deinit(void)
352*5113495bSYour Name {
353*5113495bSYour Name osif_vdev_sync_lock_destroy();
354*5113495bSYour Name }
355*5113495bSYour Name
osif_vdev_get_cached_cmd(struct osif_vdev_sync * vdev_sync)356*5113495bSYour Name uint8_t osif_vdev_get_cached_cmd(struct osif_vdev_sync *vdev_sync)
357*5113495bSYour Name {
358*5113495bSYour Name return dsc_vdev_get_cached_cmd(vdev_sync->dsc_vdev);
359*5113495bSYour Name }
360*5113495bSYour Name
osif_vdev_cache_command(struct osif_vdev_sync * vdev_sync,uint8_t cmd_id)361*5113495bSYour Name void osif_vdev_cache_command(struct osif_vdev_sync *vdev_sync, uint8_t cmd_id)
362*5113495bSYour Name {
363*5113495bSYour Name dsc_vdev_cache_command(vdev_sync->dsc_vdev, cmd_id);
364*5113495bSYour Name osif_debug("Set cache cmd to %d", cmd_id);
365*5113495bSYour Name }
366*5113495bSYour Name
367