1 /*
2  * Copyright 2023, Cypress Semiconductor Corporation (an Infineon company)
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /** @file
19  *  Provides generic APSTA functionality that chip specific files use
20  */
21 
22 #include "whd_debug.h"
23 #include "whd_buffer_api.h"
24 
25 /******************************************************
26 ** @cond               Constants
27 *******************************************************/
28 
29 /******************************************************
30 **                   Enumerations
31 *******************************************************/
32 
33 /******************************************************
34 **               Function Declarations
35 *******************************************************/
36 
37 /******************************************************
38  *        Variables Definitions
39  *****************************************************/
40 
41 /******************************************************
42 *               Function Definitions
43 ******************************************************/
44 /** Allocates a packet buffer
45  *
46  *  Implemented in the port layer interface which is specific to the
47  *  buffering scheme in use.
48  *  Attempts to allocate a packet buffer of the size requested. It can do this
49  *  by allocating a pre-existing packet from a pool, using a static buffer,
50  *  or by dynamically allocating memory. The method of allocation does not
51  *  concern WHD, however it must match the way the network stack expects packet
52  *  buffers to be allocated.
53  *
54  *  @param buffer     : A pointer which receives the allocated packet buffer handle
55  *  @param direction  : Indicates transmit/receive direction that the packet buffer is
56  *                      used for. This may be needed if tx/rx pools are separate.
57  *  @param size       : The number of bytes to allocate.
58  *  @param timeout_ms : Maximum period to block for available buffer
59  *
60  *  @return           : WHD_SUCCESS or error code
61  *
62  */
whd_host_buffer_get(whd_driver_t whd_driver,whd_buffer_t * buffer,whd_buffer_dir_t direction,uint16_t size,uint32_t timeout_ms)63 whd_result_t whd_host_buffer_get(whd_driver_t whd_driver, whd_buffer_t *buffer, whd_buffer_dir_t direction,
64                                  uint16_t size, uint32_t timeout_ms)
65 {
66     if (whd_driver->buffer_if->whd_host_buffer_get)
67     {
68         return whd_driver->buffer_if->whd_host_buffer_get(buffer, direction, size, timeout_ms);
69     }
70     else
71     {
72         WPRINT_WHD_INFO( ("Function pointers not provided .\n") );
73     }
74 
75     return WHD_WLAN_NOFUNCTION;
76 }
77 
78 /** Releases a packet buffer
79  *
80  *  Implemented in the port layer interface, which will be specific to the
81  *  buffering scheme in use.
82  *  This function is used by WHD to indicate that it no longer requires
83  *  a packet buffer. The buffer can then be released back into a pool for
84  *  reuse, or the dynamically allocated memory can be freed, according to
85  *  how the packet was allocated.
86  *  Returns void since WHD cannot do anything about failures
87  *
88  *  @param buffer    : The handle of the packet buffer to be released
89  *  @param direction : Indicates transmit/receive direction that the packet buffer has
90  *                     been used for. This might be needed if tx/rx pools are separate.
91  *
92  */
whd_buffer_release(whd_driver_t whd_driver,whd_buffer_t buffer,whd_buffer_dir_t direction)93 whd_result_t whd_buffer_release(whd_driver_t whd_driver, whd_buffer_t buffer, whd_buffer_dir_t direction)
94 {
95     if (whd_driver->buffer_if->whd_buffer_release)
96     {
97         whd_driver->buffer_if->whd_buffer_release(buffer, direction);
98         return WHD_SUCCESS;
99     }
100     else
101     {
102         WPRINT_WHD_INFO( ("Function pointers not provided .\n") );
103     }
104 
105     return WHD_WLAN_NOFUNCTION;
106 }
107 
108 /** Retrieves the current pointer of a packet buffer
109  *
110  *  Implemented in the port layer interface which is specific to the
111  *  buffering scheme in use.
112  *  Since packet buffers usually need to be created with space at the
113  *  front for additional headers, this function allows WHD to get
114  *  the current 'front' location pointer.
115  *
116  *  @param buffer : The handle of the packet buffer whose pointer is to be retrieved
117  *
118  *  @return       : The packet buffer's current pointer.
119  */
whd_buffer_get_current_piece_data_pointer(whd_driver_t whd_driver,whd_buffer_t buffer)120 uint8_t *whd_buffer_get_current_piece_data_pointer(whd_driver_t whd_driver, whd_buffer_t buffer)
121 {
122     if (whd_driver->buffer_if->whd_buffer_get_current_piece_data_pointer)
123     {
124         return whd_driver->buffer_if->whd_buffer_get_current_piece_data_pointer(buffer);
125     }
126     else
127     {
128         WPRINT_WHD_INFO( ("Function pointers not provided .\n") );
129     }
130 
131     return NULL;
132 }
133 
134 /** Retrieves the size of a packet buffer
135  *
136  *  Implemented in the port layer interface which is specific to the
137  *  buffering scheme in use.
138  *  Since packet buffers usually need to be created with space at the
139  *  front for additional headers, the memory block used to contain a packet buffer
140  *  will often be larger than the current size of the packet buffer data.
141  *  This function allows WHD to retrieve the current size of a packet buffer's data.
142  *
143  *  @param buffer : The handle of the packet buffer whose size is to be retrieved
144  *
145  *  @return       :  The size of the packet buffer.
146  */
whd_buffer_get_current_piece_size(whd_driver_t whd_driver,whd_buffer_t buffer)147 uint16_t whd_buffer_get_current_piece_size(whd_driver_t whd_driver, whd_buffer_t buffer)
148 {
149     if (whd_driver->buffer_if->whd_buffer_get_current_piece_size)
150     {
151         return whd_driver->buffer_if->whd_buffer_get_current_piece_size(buffer);
152     }
153     else
154     {
155         WPRINT_WHD_INFO( ("Function pointers not provided .\n") );
156     }
157 
158     return 0;
159 }
160 
161 /** Sets the current size of a WHD packet
162  *
163  *
164  *  Implemented in the port layer interface which is specific to the
165  *  buffering scheme in use.
166  *  This function sets the current length of a WHD packet buffer
167  *
168  *  @param buffer : The packet to be modified
169  *  @param size   : The new size of the packet buffer
170  *
171  *  @return       : WHD_SUCCESS or error code
172  */
whd_buffer_set_size(whd_driver_t whd_driver,whd_buffer_t buffer,uint16_t size)173 whd_result_t whd_buffer_set_size(whd_driver_t whd_driver, whd_buffer_t buffer, uint16_t size)
174 {
175     if (whd_driver->buffer_if->whd_buffer_set_size)
176     {
177         return whd_driver->buffer_if->whd_buffer_set_size(buffer, size);
178     }
179     else
180     {
181         WPRINT_WHD_INFO( ("Function pointers not provided .\n") );
182     }
183 
184     return WHD_WLAN_NOFUNCTION;
185 }
186 
187 /** Moves the current pointer of a packet buffer
188  *
189  *  Implemented in the port layer interface which is specific to the buffering scheme in use.
190  *
191  *  Since packet buffers usually need to be created with space at the front for additional headers,
192  *  this function allows WHD to move  the current 'front' location pointer so that it has space to
193  *  add headers to transmit packets, and so that the network stack does not see the internal WHD
194  *  headers on received packets.
195  *
196  *  @param buffer            : A pointer to the handle of the current packet buffer for which the
197  *                             current pointer will be moved. On return this may contain a pointer
198  *                             to a newly allocated packet buffer which has been daisy chained to
199  *                             the front of the given one. This would be the case if the given packet
200  *                             buffer  didn't have enough space at the front.
201  *  @param add_remove_amount : This is the number of bytes to move the current pointer of the packet
202  *                             buffer - a negative value increases the space for headers at the front
203  *                             of the packet, a positive value decreases the space.
204  *
205  *  @return                  : WHD_SUCCESS or error code
206  */
whd_buffer_add_remove_at_front(whd_driver_t whd_driver,whd_buffer_t * buffer,int32_t add_remove_amount)207 whd_result_t whd_buffer_add_remove_at_front(whd_driver_t whd_driver, whd_buffer_t *buffer, int32_t add_remove_amount)
208 {
209     if (whd_driver->buffer_if->whd_buffer_add_remove_at_front)
210     {
211         return whd_driver->buffer_if->whd_buffer_add_remove_at_front(buffer, add_remove_amount);
212     }
213     else
214     {
215         WPRINT_WHD_INFO( ("Function pointers not provided .\n") );
216     }
217 
218     return WHD_WLAN_NOFUNCTION;
219 }
220 
221