xref: /wlan-dirver/qca-wifi-host-cmn/htc/htc_packet.h (revision 4bda764146764620920bdc6e272e4f376f785372)
1 /*
2  * Copyright (c) 2013-2014, 2016-2017, 2019 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 
80 #define HTC_TX_PACKET_FLAG_FIXUP_NETBUF (1 << 0)
81 #define HTC_TX_PACKET_FLAG_HTC_HEADER_IN_NETBUF_DATA (1 << 1)
82 
83 /**
84  * struct htc_rx_packet_info - HTC RX Packet information
85  * @ExpectedHdr: HTC Internal use
86  * @HTCRxFlags: HTC Internal use
87  * @IndicationFlags: indication flags set on each RX packet indication
88  */
89 struct htc_rx_packet_info {
90 	uint32_t ExpectedHdr;
91 	uint32_t HTCRxFlags;
92 	uint32_t IndicationFlags;
93 };
94 
95 /* more packets on this endpoint are being fetched */
96 #define HTC_RX_FLAGS_INDICATE_MORE_PKTS  (1 << 0)
97 #define HTC_PACKET_MAGIC_COOKIE          0xdeadbeef
98 
99 /* wrapper around endpoint-specific packets */
100 /**
101  * struct _HTC_PACKET - HTC Packet data structure
102  * @ListLink: double link
103  * @pPktContext: caller's per packet specific context
104  * @pBufferStart: The true buffer start, the caller can store the real buffer
105  *                start here.  In receive callbacks, the HTC layer sets pBuffer
106  *                to the start of the payload past the header. This field allows
107  *                the caller to reset pBuffer when it recycles receive packets
108  *                back to HTC
109  * @pBuffer: payload start (RX/TX)
110  * @BufferLength: length of buffer
111  * @ActualLength: actual length of payload
112  * @Endpoint: endpoint that this packet was sent/recv'd from
113  * @Status: completion status
114  * @PktInfo: Packet specific info
115  * @netbufOrigHeadRoom: Original head room of skb
116  * @Completion: completion
117  * @pContext: HTC private completion context
118  * @pNetBufContext: optimization for network-oriented data, the HTC packet can
119  *                  pass the network buffer corresponding to the HTC packet
120  *                  lower layers may optimized the transfer knowing this is a
121  *                  network buffer
122  * @magic_cookie: HTC Magic cookie
123  */
124 typedef struct _HTC_PACKET {
125 	DL_LIST ListLink;
126 	void *pPktContext;
127 	uint8_t *pBufferStart;
128 	/*
129 	 * Pointer to the start of the buffer. In the transmit
130 	 * direction this points to the start of the payload. In the
131 	 * receive direction, however, the buffer when queued up
132 	 * points to the start of the HTC header but when returned
133 	 * to the caller points to the start of the payload
134 	 */
135 	uint8_t *pBuffer;
136 	uint32_t BufferLength;
137 	uint32_t ActualLength;
138 	HTC_ENDPOINT_ID Endpoint;
139 	QDF_STATUS Status;
140 	union {
141 		struct htc_tx_packet_info AsTx;
142 		struct htc_rx_packet_info AsRx;
143 	} PktInfo;
144 	/* the following fields are for internal HTC use */
145 	uint32_t netbufOrigHeadRoom;
146 	HTC_PACKET_COMPLETION Completion;
147 	void *pContext;
148 	void *pNetBufContext;
149 	uint32_t magic_cookie;
150 } HTC_PACKET;
151 
152 #define COMPLETE_HTC_PACKET(p, status)	     \
153 	{					     \
154 		(p)->Status = (status);			 \
155 		(p)->Completion((p)->pContext, (p));	 \
156 	}
157 
158 #define INIT_HTC_PACKET_INFO(p, b, len)		  \
159 	{						  \
160 		(p)->pBufferStart = (b);		      \
161 		(p)->BufferLength = (len);		      \
162 	}
163 
164 /* macro to set an initial RX packet for refilling HTC */
165 #define SET_HTC_PACKET_INFO_RX_REFILL(p, c, b, len, ep) \
166 	do { \
167 		(p)->pPktContext = (c);			      \
168 		(p)->pBuffer = (b);			      \
169 		(p)->pBufferStart = (b);		      \
170 		(p)->BufferLength = (len);		      \
171 		(p)->Endpoint = (ep);			      \
172 	} while (0)
173 
174 /* fast macro to recycle an RX packet that will be re-queued to HTC */
175 #define HTC_PACKET_RESET_RX(p)		    \
176 	{ (p)->pBuffer = (p)->pBufferStart; (p)->ActualLength = 0; }
177 
178 /* macro to set packet parameters for TX */
179 #define SET_HTC_PACKET_INFO_TX(p, c, b, len, ep, tag)  \
180 	do {						  \
181 		(p)->pPktContext = (c);			      \
182 		(p)->pBuffer = (b);			      \
183 		(p)->ActualLength = (len);		      \
184 		(p)->Endpoint = (ep);			      \
185 		(p)->PktInfo.AsTx.Tag = (tag);		      \
186 		(p)->PktInfo.AsTx.Flags = 0;		      \
187 		(p)->PktInfo.AsTx.SendFlags = 0;	      \
188 	} while (0)
189 
190 #define SET_HTC_PACKET_NET_BUF_CONTEXT(p, nb) \
191 	{ \
192 		(p)->pNetBufContext = (nb); \
193 	}
194 
195 #define GET_HTC_PACKET_NET_BUF_CONTEXT(p)  (p)->pNetBufContext
196 
197 /* HTC Packet Queueing Macros */
198 typedef struct _HTC_PACKET_QUEUE {
199 	DL_LIST QueueHead;
200 	int Depth;
201 } HTC_PACKET_QUEUE;
202 
203 /* initialize queue */
204 #define INIT_HTC_PACKET_QUEUE(pQ)   \
205 	{				    \
206 		DL_LIST_INIT(&(pQ)->QueueHead); \
207 		(pQ)->Depth = 0;		\
208 	}
209 
210 /* enqueue HTC packet to the tail of the queue */
211 #define HTC_PACKET_ENQUEUE(pQ, p)			\
212 	{   dl_list_insert_tail(&(pQ)->QueueHead, &(p)->ListLink); \
213 	    (pQ)->Depth++;					 \
214 	}
215 
216 /* enqueue HTC packet to the tail of the queue */
217 #define HTC_PACKET_ENQUEUE_TO_HEAD(pQ, p)		\
218 	{   dl_list_insert_head(&(pQ)->QueueHead, &(p)->ListLink); \
219 	    (pQ)->Depth++;					 \
220 	}
221 /* test if a queue is empty */
222 #define HTC_QUEUE_EMPTY(pQ)       ((pQ)->Depth == 0)
223 /* get packet at head without removing it */
224 static inline HTC_PACKET *htc_get_pkt_at_head(HTC_PACKET_QUEUE *queue)
225 {
226 	if (queue->Depth == 0)
227 		return NULL;
228 
229 	return A_CONTAINING_STRUCT((DL_LIST_GET_ITEM_AT_HEAD(
230 					&queue->QueueHead)),
231 				    HTC_PACKET, ListLink);
232 }
233 
234 /* remove a packet from a queue, where-ever it is in the queue */
235 #define HTC_PACKET_REMOVE(pQ, p)	    \
236 	{				    \
237 		dl_list_remove(&(p)->ListLink);  \
238 		(pQ)->Depth--;			 \
239 	}
240 
241 /* dequeue an HTC packet from the head of the queue */
242 static inline HTC_PACKET *htc_packet_dequeue(HTC_PACKET_QUEUE *queue)
243 {
244 	DL_LIST *pItem = dl_list_remove_item_from_head(&queue->QueueHead);
245 
246 	if (pItem) {
247 		queue->Depth--;
248 		return A_CONTAINING_STRUCT(pItem, HTC_PACKET, ListLink);
249 	}
250 	return NULL;
251 }
252 
253 /* dequeue an HTC packet from the tail of the queue */
254 static inline HTC_PACKET *htc_packet_dequeue_tail(HTC_PACKET_QUEUE *queue)
255 {
256 	DL_LIST *pItem = dl_list_remove_item_from_tail(&queue->QueueHead);
257 
258 	if (pItem) {
259 		queue->Depth--;
260 		return A_CONTAINING_STRUCT(pItem, HTC_PACKET, ListLink);
261 	}
262 	return NULL;
263 }
264 
265 #define HTC_PACKET_QUEUE_DEPTH(pQ) (pQ)->Depth
266 
267 #define HTC_GET_ENDPOINT_FROM_PKT(p) (p)->Endpoint
268 #define HTC_GET_TAG_FROM_PKT(p)      (p)->PktInfo.AsTx.Tag
269 
270 /* transfer the packets from one queue to the tail of another queue */
271 #define HTC_PACKET_QUEUE_TRANSFER_TO_TAIL(pQDest, pQSrc) \
272 	{ \
273 		dl_list_transfer_items_to_tail(&(pQDest)->QueueHead, \
274 					       &(pQSrc)->QueueHead); \
275 		(pQDest)->Depth += (pQSrc)->Depth; \
276 		(pQSrc)->Depth = 0; \
277 	}
278 
279 /*
280  * Transfer the packets from one queue to the head of another queue.
281  * This xfer_to_head(q1,q2) is basically equivalent to xfer_to_tail(q2,q1),
282  * but it updates the queue descriptor object for the initial queue to refer
283  * to the concatenated queue.
284  */
285 #define HTC_PACKET_QUEUE_TRANSFER_TO_HEAD(pQDest, pQSrc)  \
286 	{ \
287 		dl_list_transfer_items_to_head(&(pQDest)->QueueHead, \
288 					       &(pQSrc)->QueueHead); \
289 		(pQDest)->Depth += (pQSrc)->Depth; \
290 		(pQSrc)->Depth = 0; \
291 	}
292 
293 /* fast version to init and add a single packet to a queue */
294 #define INIT_HTC_PACKET_QUEUE_AND_ADD(pQ, pP) \
295 	{					     \
296 		DL_LIST_INIT_AND_ADD(&(pQ)->QueueHead, &(pP)->ListLink)	\
297 		(pQ)->Depth = 1;					\
298 	}
299 
300 #define HTC_PACKET_QUEUE_ITERATE_ALLOW_REMOVE(pQ, pPTemp) \
301 		ITERATE_OVER_LIST_ALLOW_REMOVE(&(pQ)->QueueHead, \
302 						(pPTemp), HTC_PACKET, ListLink)
303 
304 #define HTC_PACKET_QUEUE_ITERATE_IS_VALID(pQ) ITERATE_IS_VALID(&(pQ)->QueueHead)
305 #define HTC_PACKET_QUEUE_ITERATE_RESET(pQ) ITERATE_RESET(&(pQ)->QueueHead)
306 
307 #define HTC_PACKET_QUEUE_ITERATE_END ITERATE_END
308 
309 /**
310  * htc_packet_set_magic_cookie() - set magic cookie in htc packet
311  * htc_pkt - pointer to htc packet
312  * value - value to set in magic cookie
313  *
314  * This API sets the magic cookie passed in htc packet.
315  *
316  * Return : None
317  */
318 static inline void htc_packet_set_magic_cookie(HTC_PACKET *htc_pkt,
319 			uint32_t value)
320 {
321 	htc_pkt->magic_cookie = value;
322 }
323 
324 /**
325  * htc_packet_set_magic_cookie() - get magic cookie in htc packet
326  * htc_pkt - pointer to htc packet
327  *
328  * This API returns the magic cookie in htc packet.
329  *
330  * Return : magic cookie
331  */
332 static inline uint32_t htc_packet_get_magic_cookie(HTC_PACKET *htc_pkt)
333 {
334 	return htc_pkt->magic_cookie;
335 }
336 
337 #endif /*HTC_PACKET_H_ */
338