xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_packet.h (revision 2f4b444fb7e689b83a4ab0e7b3b38f0bf4def8e0) !
1 /*
2  * Copyright (c) 2013-2014, 2016-2017, 2019-2021 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef HTC_PACKET_H_
20 #define HTC_PACKET_H_
21 
22 #include <osdep.h>
23 #include "dl_list.h"
24 
25 /* ------ Endpoint IDS ------ */
26 typedef enum {
27 	ENDPOINT_UNUSED = -1,
28 	ENDPOINT_0 = 0,
29 	ENDPOINT_1 = 1,
30 	ENDPOINT_2 = 2,
31 	ENDPOINT_3,
32 	ENDPOINT_4,
33 	ENDPOINT_5,
34 	ENDPOINT_6,
35 	ENDPOINT_7,
36 	ENDPOINT_8,
37 	ENDPOINT_MAX,
38 } HTC_ENDPOINT_ID;
39 
40 struct _HTC_PACKET;
41 
42 typedef void (*HTC_PACKET_COMPLETION)(void *, struct _HTC_PACKET *);
43 
44 typedef uint16_t HTC_TX_TAG;
45 
46 /**
47  * struct htc_tx_packet_info - HTC TX packet information
48  * @Tag: tag used to selective flush packets
49  * @CreditsUsed: number of credits used for this TX packet (HTC internal)
50  * @SendFlags: send flags (HTC internal)
51  * @SeqNo: internal seq no for debugging (HTC internal)
52  * @Flags: Internal use
53  */
54 struct htc_tx_packet_info {
55 	HTC_TX_TAG Tag;
56 	int CreditsUsed;
57 	uint8_t SendFlags;
58 	int SeqNo;
59 	uint32_t Flags;
60 };
61 
62 /**
63  * HTC_TX_PACKET_TAG_XXX - #defines for tagging packets for special handling
64  * HTC_TX_PACKET_TAG_ALL: zero is reserved and used to flush ALL packets
65  * HTC_TX_PACKET_TAG_INTERNAL: internal tags start here
66  * HTC_TX_PACKET_TAG_USER_DEFINED: user-defined tags start here
67  * HTC_TX_PACKET_TAG_BUNDLED: indicate this is a bundled tx packet
68  * HTC_TX_PACKET_TAG_AUTO_PM: indicate a power management wmi command
69  */
70 #define HTC_TX_PACKET_TAG_ALL          0
71 #define HTC_TX_PACKET_TAG_INTERNAL     1
72 #define HTC_TX_PACKET_TAG_USER_DEFINED (HTC_TX_PACKET_TAG_INTERNAL + 9)
73 #define HTC_TX_PACKET_TAG_BUNDLED      (HTC_TX_PACKET_TAG_USER_DEFINED + 1)
74 #define HTC_TX_PACKET_TAG_AUTO_PM      (HTC_TX_PACKET_TAG_USER_DEFINED + 2)
75 
76 /* Tag packet for runtime put after sending */
77 #define HTC_TX_PACKET_TAG_RUNTIME_PUT  (HTC_TX_PACKET_TAG_USER_DEFINED + 3)
78 
79 /*Tag packet for runtime put in response or cleanup */
80 #define HTC_TX_PACKET_TAG_RTPM_PUT_RC  (HTC_TX_PACKET_TAG_USER_DEFINED + 4)
81 
82 #define HTC_TX_PACKET_SYSTEM_SUSPEND   (HTC_TX_PACKET_TAG_USER_DEFINED + 5)
83 #define HTC_TX_PACKET_SYSTEM_RESUME    (HTC_TX_PACKET_TAG_USER_DEFINED + 6)
84 
85 #define HTC_TX_PACKET_FLAG_FIXUP_NETBUF (1 << 0)
86 #define HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA (1 << 1)
87 
88 /**
89  * struct htc_rx_packet_info - HTC RX Packet information
90  * @ExpectedHdr: HTC Internal use
91  * @HTCRxFlags: HTC Internal use
92  * @IndicationFlags: indication flags set on each RX packet indication
93  */
94 struct htc_rx_packet_info {
95 	uint32_t ExpectedHdr;
96 	uint32_t HTCRxFlags;
97 	uint32_t IndicationFlags;
98 };
99 
100 /* more packets on this endpoint are being fetched */
101 #define HTC_RX_FLAGS_INDICATE_MORE_PKTS  (1 << 0)
102 #define HTC_PACKET_MAGIC_COOKIE          0xdeadbeef
103 
104 /* wrapper around endpoint-specific packets */
105 /**
106  * struct _HTC_PACKET - HTC Packet data structure
107  * @ListLink: double link
108  * @pPktContext: caller's per packet specific context
109  * @pBufferStart: The true buffer start, the caller can store the real buffer
110  *                start here.  In receive callbacks, the HTC layer sets pBuffer
111  *                to the start of the payload past the header. This field allows
112  *                the caller to reset pBuffer when it recycles receive packets
113  *                back to HTC
114  * @pBuffer: payload start (RX/TX)
115  * @BufferLength: length of buffer
116  * @ActualLength: actual length of payload
117  * @Endpoint: endpoint that this packet was sent/recv'd from
118  * @Status: completion status
119  * @PktInfo: Packet specific info
120  * @netbufOrigHeadRoom: Original head room of skb
121  * @Completion: completion
122  * @pContext: HTC private completion context
123  * @pNetBufContext: optimization for network-oriented data, the HTC packet can
124  *                  pass the network buffer corresponding to the HTC packet
125  *                  lower layers may optimized the transfer knowing this is a
126  *                  network buffer
127  * @magic_cookie: HTC Magic cookie
128  */
129 typedef struct _HTC_PACKET {
130 	DL_LIST ListLink;
131 	void *pPktContext;
132 	uint8_t *pBufferStart;
133 	/*
134 	 * Pointer to the start of the buffer. In the transmit
135 	 * direction this points to the start of the payload. In the
136 	 * receive direction, however, the buffer when queued up
137 	 * points to the start of the HTC header but when returned
138 	 * to the caller points to the start of the payload
139 	 */
140 	uint8_t *pBuffer;
141 	uint32_t BufferLength;
142 	uint32_t ActualLength;
143 	HTC_ENDPOINT_ID Endpoint;
144 	QDF_STATUS Status;
145 	union {
146 		struct htc_tx_packet_info AsTx;
147 		struct htc_rx_packet_info AsRx;
148 	} PktInfo;
149 	/* the following fields are for internal HTC use */
150 	uint32_t netbufOrigHeadRoom;
151 	HTC_PACKET_COMPLETION Completion;
152 	void *pContext;
153 	void *pNetBufContext;
154 	uint32_t magic_cookie;
155 } HTC_PACKET;
156 
157 #define COMPLETE_HTC_PACKET(p, status)	     \
158 	{					     \
159 		(p)->Status = (status);			 \
160 		(p)->Completion((p)->pContext, (p));	 \
161 	}
162 
163 #define INIT_HTC_PACKET_INFO(p, b, len)		  \
164 	{						  \
165 		(p)->pBufferStart = (b);		      \
166 		(p)->BufferLength = (len);		      \
167 	}
168 
169 /* macro to set an initial RX packet for refilling HTC */
170 #define SET_HTC_PACKET_INFO_RX_REFILL(p, c, b, len, ep) \
171 	do { \
172 		(p)->pPktContext = (c);			      \
173 		(p)->pBuffer = (b);			      \
174 		(p)->pBufferStart = (b);		      \
175 		(p)->BufferLength = (len);		      \
176 		(p)->Endpoint = (ep);			      \
177 	} while (0)
178 
179 /* fast macro to recycle an RX packet that will be re-queued to HTC */
180 #define HTC_PACKET_RESET_RX(p)		    \
181 	{ (p)->pBuffer = (p)->pBufferStart; (p)->ActualLength = 0; }
182 
183 /* macro to set packet parameters for TX */
184 #define SET_HTC_PACKET_INFO_TX(p, c, b, len, ep, tag)  \
185 	do {						  \
186 		(p)->pPktContext = (c);			      \
187 		(p)->pBuffer = (b);			      \
188 		(p)->ActualLength = (len);		      \
189 		(p)->Endpoint = (ep);			      \
190 		(p)->PktInfo.AsTx.Tag = (tag);		      \
191 		(p)->PktInfo.AsTx.Flags = 0;		      \
192 		(p)->PktInfo.AsTx.SendFlags = 0;	      \
193 	} while (0)
194 
195 #define SET_HTC_PACKET_NET_BUF_CONTEXT(p, nb) \
196 	{ \
197 		(p)->pNetBufContext = (nb); \
198 	}
199 
200 #define GET_HTC_PACKET_NET_BUF_CONTEXT(p)  (p)->pNetBufContext
201 
202 /* HTC Packet Queueing Macros */
203 typedef struct _HTC_PACKET_QUEUE {
204 	DL_LIST QueueHead;
205 	int Depth;
206 } HTC_PACKET_QUEUE;
207 
208 /* initialize queue */
209 #define INIT_HTC_PACKET_QUEUE(pQ)   \
210 	{				    \
211 		DL_LIST_INIT(&(pQ)->QueueHead); \
212 		(pQ)->Depth = 0;		\
213 	}
214 
215 /* enqueue HTC packet to the tail of the queue */
216 #define HTC_PACKET_ENQUEUE(pQ, p)			\
217 	{   dl_list_insert_tail(&(pQ)->QueueHead, &(p)->ListLink); \
218 	    (pQ)->Depth++;					 \
219 	}
220 
221 /* enqueue HTC packet to the tail of the queue */
222 #define HTC_PACKET_ENQUEUE_TO_HEAD(pQ, p)		\
223 	{   dl_list_insert_head(&(pQ)->QueueHead, &(p)->ListLink); \
224 	    (pQ)->Depth++;					 \
225 	}
226 /* test if a queue is empty */
227 #define HTC_QUEUE_EMPTY(pQ)       ((pQ)->Depth == 0)
228 /* get packet at head without removing it */
229 static inline HTC_PACKET *htc_get_pkt_at_head(HTC_PACKET_QUEUE *queue)
230 {
231 	if (queue->Depth == 0)
232 		return NULL;
233 
234 	return A_CONTAINING_STRUCT((DL_LIST_GET_ITEM_AT_HEAD(
235 					&queue->QueueHead)),
236 				    HTC_PACKET, ListLink);
237 }
238 
239 /* remove a packet from a queue, where-ever it is in the queue */
240 #define HTC_PACKET_REMOVE(pQ, p)	    \
241 	{				    \
242 		dl_list_remove(&(p)->ListLink);  \
243 		(pQ)->Depth--;			 \
244 	}
245 
246 /* dequeue an HTC packet from the head of the queue */
247 static inline HTC_PACKET *htc_packet_dequeue(HTC_PACKET_QUEUE *queue)
248 {
249 	DL_LIST *pItem = dl_list_remove_item_from_head(&queue->QueueHead);
250 
251 	if (pItem) {
252 		queue->Depth--;
253 		return A_CONTAINING_STRUCT(pItem, HTC_PACKET, ListLink);
254 	}
255 	return NULL;
256 }
257 
258 /* dequeue an HTC packet from the tail of the queue */
259 static inline HTC_PACKET *htc_packet_dequeue_tail(HTC_PACKET_QUEUE *queue)
260 {
261 	DL_LIST *pItem = dl_list_remove_item_from_tail(&queue->QueueHead);
262 
263 	if (pItem) {
264 		queue->Depth--;
265 		return A_CONTAINING_STRUCT(pItem, HTC_PACKET, ListLink);
266 	}
267 	return NULL;
268 }
269 
270 #define HTC_PACKET_QUEUE_DEPTH(pQ) (pQ)->Depth
271 
272 #define HTC_GET_ENDPOINT_FROM_PKT(p) (p)->Endpoint
273 #define HTC_GET_TAG_FROM_PKT(p)      (p)->PktInfo.AsTx.Tag
274 
275 /* transfer the packets from one queue to the tail of another queue */
276 #define HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(pQDest, pQSrc) \
277 	{ \
278 		dl_list_transfer_items_to_tail(&(pQDest)->QueueHead, \
279 					       &(pQSrc)->QueueHead); \
280 		(pQDest)->Depth += (pQSrc)->Depth; \
281 		(pQSrc)->Depth = 0; \
282 	}
283 
284 /*
285  * Transfer the packets from one queue to the head of another queue.
286  * This xfer_to_head(q1,q2) is basically equivalent to xfer_to_tail(q2,q1),
287  * but it updates the queue descriptor object for the initial queue to refer
288  * to the concatenated queue.
289  */
290 #define HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(pQDest, pQSrc)  \
291 	{ \
292 		dl_list_transfer_items_to_head(&(pQDest)->QueueHead, \
293 					       &(pQSrc)->QueueHead); \
294 		(pQDest)->Depth += (pQSrc)->Depth; \
295 		(pQSrc)->Depth = 0; \
296 	}
297 
298 /* fast version to init and add a single packet to a queue */
299 #define INIT_HTC_PACKET_QUEUE_AND_ADD(pQ, pP) \
300 	{					     \
301 		DL_LIST_INIT_AND_ADD(&(pQ)->QueueHead, &(pP)->ListLink)	\
302 		(pQ)->Depth = 1;					\
303 	}
304 
305 #define HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQ, pPTemp) \
306 		ITERATE_OVER_LIST_ALLOW_REMOVE(&(pQ)->QueueHead, \
307 						(pPTemp), HTC_PACKET, ListLink)
308 
309 #define HTC_PACKET_QUEUE_ITERATE_IS_VALID(pQ) ITERATE_IS_VALID(&(pQ)->QueueHead)
310 #define HTC_PACKET_QUEUE_ITERATE_RESET(pQ) ITERATE_RESET(&(pQ)->QueueHead)
311 
312 #define HTC_PACKET_QUEUE_ITERATE_END ITERATE_END
313 
314 /**
315  * htc_packet_set_magic_cookie() - set magic cookie in htc packet
316  * htc_pkt - pointer to htc packet
317  * value - value to set in magic cookie
318  *
319  * This API sets the magic cookie passed in htc packet.
320  *
321  * Return : None
322  */
323 static inline void htc_packet_set_magic_cookie(HTC_PACKET *htc_pkt,
324 			uint32_t value)
325 {
326 	htc_pkt->magic_cookie = value;
327 }
328 
329 /**
330  * htc_packet_set_magic_cookie() - get magic cookie in htc packet
331  * htc_pkt - pointer to htc packet
332  *
333  * This API returns the magic cookie in htc packet.
334  *
335  * Return : magic cookie
336  */
337 static inline uint32_t htc_packet_get_magic_cookie(HTC_PACKET *htc_pkt)
338 {
339 	return htc_pkt->magic_cookie;
340 }
341 
342 #endif /*HTC_PACKET_H_ */
343